@ -81,8 +81,13 @@ namespace olc
// set whether audio will continue playing when the app has lost focus
void SetBackgroundPlay ( bool state ) ;
public : // LOADING ROUTINES
const size_t LoadSound ( const std : : string & path ) ;
public : // LOADING ROUTINES
enum SoundEffectFlag {
SFX ,
BGM ,
} ;
const size_t LoadSound ( const std : : string & path , const SoundEffectFlag soundType = BGM ) ; //Setting sound effect to true avoids loading it from a resource pack.
const size_t LoadResource ( const std : : string & path ) ;
void UnloadSound ( const int id ) ;
public : // PLAYBACK CONTROLS
@ -153,6 +158,7 @@ namespace olc
// this is where the sounds are kept
std : : vector < ma_sound * > vecSounds ;
std : : vector < ma_sound * > vecOneOffSounds ;
std : : vector < std : : pair < size_t , ma_audio_buffer > > vecResourcePackBuffers ;
} ;
/**
@ -200,6 +206,10 @@ namespace olc
# ifdef OLC_PGEX_MINIAUDIO
# undef OLC_PGEX_MINIAUDIO
# include "AdventuresInLestoria.h"
INCLUDE_game
namespace olc
{
bool MiniAudio : : backgroundPlay = false ;
@ -290,28 +300,45 @@ namespace olc
MiniAudio : : backgroundPlay = state ;
}
const size_t MiniAudio : : LoadSound ( const std : : string & path )
const size_t MiniAudio : : LoadSound ( const std : : string & path , const SoundEffectFlag soundType )
{
// create the sound
ma_sound * sound = new ma_sound ( ) ;
// assume no empty slots...
size_t id = vecSounds . size ( ) ;
// load it from the file and decode it
if ( ma_sound_init_from_file ( & engine , path . c_str ( ) , MA_SOUND_FLAG_DECODE | MA_SOUND_FLAG_ASYNC , NULL , NULL , sound ) ! = MA_SUCCESS )
throw MiniAudioSoundException ( ) ;
bool foundSound { false } ;
// attempt to re-use an empty slot
for ( int i = 0 ; i < vecSounds . size ( ) ; i + + )
{
if ( vecSounds . at ( i ) = = nullptr )
{
vecSounds . at ( i ) = sound ;
return i ;
id = i ;
foundSound = true ;
break ;
}
}
// no empty slots, make more room!
const size_t id = vecSounds . size ( ) ;
vecSounds . push_back ( sound ) ;
if ( ! foundSound ) vecSounds . emplace_back ( sound ) ;
if ( soundType = = BGM ) {
ma_audio_buffer newBuffer { } ;
ResourceBuffer rb { game - > gamepack . GetFileBuffer ( path ) } ;
short * decodedOggFile ;
int numSamples { stb_vorbis_decode_memory ( ( const unsigned char * ) ( rb . vMemory . data ( ) ) , rb . vMemory . size ( ) , ( int * ) ( & device . playback . channels ) , ( int * ) ( & device . sampleRate ) , & decodedOggFile ) } ;
if ( numSamples = = - 1 ) ERR ( std : : format ( " Failed to decode Ogg Vorbis file! {} " , path ) ) ;
LOG ( std : : format ( " Samples: {}, Channels: {}, Sample Rate: {} " , numSamples , device . playback . channels , device . sampleRate ) ) ;
ma_audio_buffer_config config { ma_audio_buffer_config_init ( device . playback . format , device . playback . channels , numSamples , decodedOggFile , nullptr ) } ;
if ( ma_audio_buffer_init ( & config , & newBuffer ) ! = MA_SUCCESS ) ERR ( std : : format ( " WARNING! Failed to load audio buffer~! {} " , path ) ) ;
if ( ma_sound_init_from_data_source ( & engine , & newBuffer , MA_SOUND_FLAG_DECODE | MA_SOUND_FLAG_ASYNC , nullptr , sound ) ! = MA_SUCCESS ) ERR ( std : : format ( " Could not initialize sound! {} " , path ) ) ;
vecResourcePackBuffers . emplace_back ( std : : pair < size_t , ma_audio_buffer > { id , newBuffer } ) ;
} else { //Sound effects
// load it from the file and decode it
if ( ma_sound_init_from_file ( & engine , path . c_str ( ) , MA_SOUND_FLAG_DECODE | MA_SOUND_FLAG_ASYNC , NULL , NULL , sound ) ! = MA_SUCCESS )
throw MiniAudioSoundException ( ) ;
}
return id ;
}
@ -321,6 +348,7 @@ namespace olc
ma_sound_uninit ( vecSounds . at ( id ) ) ;
delete vecSounds . at ( id ) ;
vecSounds . at ( id ) = nullptr ;
std : : erase_if ( vecResourcePackBuffers , [ & id ] ( const std : : pair < size_t , ma_audio_buffer > & bufferData ) { return id = = bufferData . first ; } ) ;
}
void MiniAudio : : Play ( const int id , const bool loop )