diff options
Diffstat (limited to 'Source/Sfx.c')
| -rw-r--r-- | Source/Sfx.c | 80 |
1 files changed, 53 insertions, 27 deletions
diff --git a/Source/Sfx.c b/Source/Sfx.c index 4e6d324..2f8a565 100644 --- a/Source/Sfx.c +++ b/Source/Sfx.c @@ -7,8 +7,6 @@ * Defines * *************************************/ #define MAX_VOLUME SPU_MAXVOL -#define SILENT 0 - #define NUMBER_OF_VOICES 24 /* ************************************* @@ -18,14 +16,14 @@ /* ************************************* * Local Variables * *************************************/ -static uint8_t voiceIndex; static uint16_t SfxGlobalVolumeReduction; +static bool usedVoices[NUMBER_OF_VOICES]; #ifndef NO_CDDA static uint16_t SfxCddaVolumeReduction; #endif // NO_CDDA -void SfxPlaySound(SsVag * sound) +void SfxPlaySound(SsVag* sound) { if (sound->data_size != 0) { @@ -33,48 +31,76 @@ void SfxPlaySound(SsVag * sound) } } -bool SfxUploadSound(char* file_path, SsVag * vag) +bool SfxUploadSound_Ex(char* file_path, SsVag* vag, uint8_t voiceIndex) { - static size_t SPUBytesUsed; + static size_t SPUBytesUsed; - if (SystemLoadFile(file_path) == false) + if (voiceIndex >= NUMBER_OF_VOICES) { + Serial_printf( "Invalid input voice index %d. Only indexes [%d-%d] are allowed.\n", + voiceIndex, + 0, + NUMBER_OF_VOICES - 1 ); return false; } - if (voiceIndex < NUMBER_OF_VOICES) + if (usedVoices[voiceIndex] != false) { - SsReadVag(vag,SystemGetBufferAddress()); + Serial_printf("Voice number %d is already being used.\n", voiceIndex); + return false; + } - SsUploadVag(vag); + if (SystemLoadFile(file_path) == false) + { + return false; + } - vag->cur_voice = voiceIndex; + SsReadVag(vag, SystemGetBufferAddress()); - voiceIndex++; + SsUploadVag(vag); - Serial_printf("SPU voices used = %d\n", voiceIndex); + vag->cur_voice = voiceIndex; - SPUBytesUsed += vag->data_size; + usedVoices[voiceIndex] = true; - if (SPUBytesUsed != 0) - { - enum - { - SPU_MAX_ALLOWED_BYTES = 512 * 1024 // 512 KBytes - }; + SPUBytesUsed += vag->data_size; - uint16_t percentage = SPUBytesUsed * 100 / SPU_MAX_ALLOWED_BYTES; + if (SPUBytesUsed != 0) + { + enum + { + SPU_MAX_ALLOWED_BYTES = 512 * 1024 // 512 KBytes + }; - dprintf("SPU usage: %d%%\n", percentage); - } + uint16_t percentage = SPUBytesUsed * 100 / SPU_MAX_ALLOWED_BYTES; + + dprintf("SPU usage: %d%%\n", percentage); } - else + + return true; +} + +bool SfxUploadSound(char* file_path, SsVag* vag) +{ + bool success = false; + uint8_t i; + + for (i = 0; i < NUMBER_OF_VOICES; i++) + { + if (usedVoices[i] == false) + { + success = true; + break; + } + } + + if (success == false) { - Serial_printf("Maximum number of SPU voices exceeded!\n"); - return false; //Maximum voices exceeded + Serial_printf("Could not find any free SPU slot.\n"); + return false; } - return true; + return SfxUploadSound_Ex(file_path, vag, i); } void SfxPlayTrack(MUSIC_TRACKS track) |
