@ -158,7 +158,15 @@ namespace olc
// this is where the sounds are kept
// this is where the sounds are kept
std : : vector < ma_sound * > vecSounds ;
std : : vector < ma_sound * > vecSounds ;
std : : vector < ma_sound * > vecOneOffSounds ;
std : : vector < ma_sound * > vecOneOffSounds ;
std : : vector < std : : pair < size_t , ma_audio_buffer > > vecResourcePackBuffers ;
struct ResourceData {
ma_engine * engine ;
ResourceBuffer data ;
std : : string pathName ;
~ ResourceData ( ) {
if ( ma_resource_manager_unregister_data ( ma_engine_get_resource_manager ( engine ) , pathName . data ( ) ) ! = MA_SUCCESS ) ERR ( std : : format ( " WARNING! Could not clear resources for {}! " , pathName ) ) ;
} ;
} ;
std : : unordered_map < size_t , ResourceData > vecResourceData ;
} ;
} ;
/**
/**
@ -324,21 +332,11 @@ namespace olc
if ( ! foundSound ) vecSounds . emplace_back ( sound ) ;
if ( ! foundSound ) vecSounds . emplace_back ( sound ) ;
if ( soundType = = BGM ) {
if ( soundType = = BGM ) {
ma_audio_buffer newBuffer { } ;
vecResourceData [ id ] = { & engine , game - > gamepack . GetFileBuffer ( path ) , path } ;
ResourceBuffer rb { game - > gamepack . GetFileBuffer ( path ) } ;
if ( ma_resource_manager_register_encoded_data ( ma_engine_get_resource_manager ( & engine ) , path . data ( ) , vecResourceData [ id ] . data . vMemory . data ( ) , vecResourceData [ id ] . data . vMemory . size ( ) ) ! = MA_SUCCESS ) ERR ( std : : format ( " WARNING! Could not load resources for {}! " , 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 ( ) ;
}
}
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 ;
return id ;
}
}
@ -348,7 +346,7 @@ namespace olc
ma_sound_uninit ( vecSounds . at ( id ) ) ;
ma_sound_uninit ( vecSounds . at ( id ) ) ;
delete vecSounds . at ( id ) ;
delete vecSounds . at ( id ) ;
vecSounds . at ( id ) = nullptr ;
vecSounds . at ( id ) = nullptr ;
std : : erase_if ( vecResourcePackBuffers , [ & id ] ( const std : : pair < size_t , ma_audio_buffer > & bufferData ) { return id = = bufferData . first ; } ) ;
if ( vecResourceData . count ( id ) ) vecResourceData . erase ( id ) ;
}
}
void MiniAudio : : Play ( const int id , const bool loop )
void MiniAudio : : Play ( const int id , const bool loop )