@ -40,21 +40,192 @@ All rights reserved.
# include "DEFINES.h"
INCLUDE_game
INCLUDE_DATA
float Audio : : defaultFadeTime ;
void Audio : : Initialize ( ) {
Self ( ) . events . insert ( " Default Volume " ) ;
for ( auto & [ key , data ] : DATA [ " Events " ] ) {
Self ( ) . events . insert ( key ) ;
}
for ( auto & [ songFileName , size ] : DATA [ " BGM " ] ) {
auto & data = DATA [ " BGM " ] [ songFileName ] ;
if ( songFileName ! = " Default Fade Time " ) {
int channelCounter = 0 ;
BGM & bgm = Self ( ) . bgm [ songFileName ] ;
bgm . SetFileName ( songFileName ) ;
bgm . SetName ( data [ " Track Name " ] . GetString ( ) ) ;
while ( data . HasProperty ( std : : format ( " channel[{}] " , channelCounter ) ) ) {
std : : string channelName = data [ std : : format ( " channel[{}] " , channelCounter ) ] . GetString ( ) ;
if ( ! std : : filesystem : : exists ( " bgm_directory " _S + channelName ) ) ERR ( std : : format ( " WARNING! Could not load file {} for track {} " , channelName , songFileName ) ) ;
bgm . AddChannel ( channelName ) ;
channelCounter + + ;
}
if ( ! data . HasProperty ( " Default Volume " ) ) ERR ( std : : format ( " WARNING! Track {} does not have a Default Volume parameter! " , bgm . GetName ( ) ) ) ;
if ( data [ " Default Volume " ] . GetValueCount ( ) ! = bgm . GetChannelCount ( ) ) ERR ( std : : format ( " WARNING! Default Volume parameters do not match channel count. {} != {} " , data [ " Default Volume " ] . GetValueCount ( ) , bgm . GetChannelCount ( ) ) ) ;
VolumeList volumes ;
for ( int i = 0 ; i < data [ " Default Volume " ] . GetValueCount ( ) ; i + + ) {
volumes . push_back ( data [ " Default Volume " ] . GetInt ( i ) / 100.f ) ;
}
bgm . AddEventVolumes ( " Default Volume " , volumes ) ;
if ( data . HasProperty ( " Fade Time " ) ) {
bgm . SetFadeTime ( data [ " Fade Time " ] . GetReal ( ) ) ;
} else {
bgm . SetFadeTime ( defaultFadeTime ) ;
}
if ( data . HasProperty ( " Events " ) ) {
for ( auto & [ eventName , size ] : DATA [ " Events " ] ) {
auto & eventData = data [ " Events " ] [ eventName ] ;
if ( eventData . GetValueCount ( ) ! = bgm . GetChannelCount ( ) ) ERR ( std : : format ( " WARNING! {} parameters do not match channel count. {} != {} " , eventName , eventData . GetValueCount ( ) , bgm . GetChannelCount ( ) ) ) ;
VolumeList volumes ;
for ( int i = 0 ; i < eventData . GetValueCount ( ) ; i + + ) {
volumes . push_back ( eventData . GetInt ( i ) / 100.f ) ;
}
bgm . AddEventVolumes ( eventName , volumes ) ;
}
}
} else {
defaultFadeTime = data . GetReal ( ) ;
}
}
}
MiniAudio & Audio : : Engine ( ) {
return game - > audioEngine . audioEngine ;
}
void Audio : : Play ( const std : : string_view sound ) {
Engine ( ) . Play ( std : : string ( sound ) ) ;
Engine ( ) . Play ( std : : string ( sound ) ) ;
} ;
void Audio : : PlayBGM ( const std : : string_view sound , const bool loop = true ) {
if ( Engine ( ) . LoadS )
Engine ( ) . Play ( std : : string ( sound ) ) ;
void Audio : : PlayBGM ( const std : : string_view sound , const bool loop ) {
BGM & track = Self ( ) . bgm [ std : : string ( sound ) ] ;
StopBGM ( ) ; //Stop any currently playing track.
track . Load ( ) ;
for ( int channelListIndex = 0 ; int trackID : track . GetChannelIDs ( ) ) {
Engine ( ) . SetVolume ( trackID , track . GetVolume ( Self ( ) . currentAudioEvent , channelListIndex ) ) ;
Engine ( ) . Play ( trackID , loop ) ;
channelListIndex + + ;
}
} ;
void Audio : : StopBGM ( ) {
if ( Self ( ) . BGMIsPlaying ( ) ) {
BGM & currentTrack = Self ( ) . bgm [ Self ( ) . currentBGM ] ;
for ( int trackID : currentTrack . GetChannelIDs ( ) ) {
Engine ( ) . Stop ( trackID ) ;
}
}
}
const bool Audio : : BGMIsPlaying ( ) {
return Self ( ) . currentBGM . length ( ) > 0 ;
}
const Volume & Audio : : BGM : : GetVolume ( const Event & eventName , const ChannelID & id ) const {
return eventVolumes . GetVolumes ( eventName ) . at ( id ) ;
}
void Audio : : BGM : : Load ( ) {
BGM & bgm = Self ( ) . bgm [ Self ( ) . currentBGM ] ;
if ( Self ( ) . BGMIsPlaying ( ) ) {
bgm . Unload ( ) ;
}
Self ( ) . currentBGM = songFileName ;
BGM & newBgm = Self ( ) . bgm [ songFileName ] ;
if ( newBgm . channels . size ( ) > 0 ) ERR ( std : : format ( " WARNING! The size of the channels list is greater than zero! Size: {} " , bgm . channels . size ( ) ) ) ;
for ( const ChannelName & channel : newBgm . GetChannels ( ) ) {
ChannelID soundID = Engine ( ) . LoadSound ( " bgm_directory " _S + channel ) ;
newBgm . channels . push_back ( soundID ) ;
}
}
void Audio : : BGM : : Unload ( ) {
BGM & bgm = Self ( ) . bgm [ Self ( ) . currentBGM ] ;
for ( const ChannelID & id : channels ) {
Engine ( ) . UnloadSound ( id ) ;
}
channels . clear ( ) ;
Self ( ) . currentBGM = " " ;
}
const ChannelID & Audio : : BGM : : GetChannelID ( const int index ) {
return channels [ index ] ;
}
const ChannelIDList & Audio : : BGM : : GetChannelIDs ( ) const {
return channels ;
}
const std : : vector < ChannelName > & Audio : : BGM : : GetChannels ( ) const {
return channelNames ;
}
void Audio : : BGM : : SetFadeTime ( const float fadeTime ) {
this - > fadeTime = fadeTime ;
}
void Audio : : BGM : : AddEventVolumes ( const Event & eventName , const VolumeList & volumes ) {
eventVolumes . AddEventInfo ( eventName , volumes ) ;
}
const size_t Audio : : BGM : : GetChannelCount ( ) const {
return channelNames . size ( ) ;
}
const SongName & Audio : : BGM : : GetName ( ) const {
return songName ;
}
void Audio : : BGM : : SetName ( std : : string_view name ) {
songName = name ;
}
void Audio : : BGM : : SetFileName ( std : : string_view name ) {
songFileName = name ;
}
void Audio : : BGM : : AddChannel ( const ChannelName & name ) {
channelNames . push_back ( name ) ;
}
const VolumeList & Audio : : EventData : : GetVolumes ( const Event & event ) const {
if ( eventInfo . find ( event ) ! = eventInfo . end ( ) ) return eventInfo . at ( event ) ;
return eventInfo . at ( " Default Volume " ) ;
}
void Audio : : EventData : : AddEventInfo ( const Event & eventName , const VolumeList & volumes ) {
eventInfo [ eventName ] = volumes ;
}
Audio & Audio : : Self ( ) {
return game - > audioEngine ;
}
const Event & Audio : : GetAudioEvent ( ) {
return Self ( ) . currentAudioEvent ;
}
void Audio : : SetAudioEvent ( const Event & eventName ) {
if ( Self ( ) . events . find ( eventName ) = = Self ( ) . events . end ( ) ) ERR ( std : : format ( " WARNING! cannot find event {} " , eventName ) ) ;
Self ( ) . currentAudioEvent = eventName ;
if ( Audio : : BGMIsPlaying ( ) ) {
BGM & currentBgm = Self ( ) . bgm [ Self ( ) . currentBGM ] ;
for ( int currentTrackIndex = 0 ; int trackID : currentBgm . GetChannelIDs ( ) ) {
Engine ( ) . SetVolume ( trackID , currentBgm . GetVolume ( eventName , currentTrackIndex ) ) ;
currentTrackIndex + + ;
}
}
}
std : : string operator " " _SFX ( const char * key , size_t length ) {
return " sfx_directory " _S + std : : string ( key , length ) ;
}
std : : string operator " " _BGM ( const char * key , size_t length ) {
return " bgm_directory " _S + std : : string ( key , length ) ;
}