diff --git a/source/jail_audio.cpp b/source/jail_audio.cpp index 76592df..a34c20e 100644 --- a/source/jail_audio.cpp +++ b/source/jail_audio.cpp @@ -20,6 +20,7 @@ struct JA_Channel_t { struct JA_Music_t { int samples {0}; + Uint32 length {0}; int pos {0}; int times {0}; short* output {NULL}; @@ -60,13 +61,13 @@ void audioCallback(void * userdata, uint8_t * stream, int len) { volume = JA_musicVolume * (1.0 - percent); } } - const int size = SDL_min(len, (current_music->samples-current_music->pos)*2); - SDL_MixAudioFormat(stream, (Uint8*)(current_music->output+current_music->pos), AUDIO_S16, size, volume); - current_music->pos += size/2; + const int size = SDL_min(len, current_music->length - current_music->pos); + SDL_MixAudioFormat(stream, (Uint8*)(current_music->output)+current_music->pos, AUDIO_S16, size, volume); + current_music->pos += size; if (size < len) { if (current_music->times != 0) { SDL_MixAudioFormat(stream+size, (Uint8*)current_music->output, AUDIO_S16, len-size, volume); - current_music->pos = (len-size)/2; + current_music->pos = len-size; if (current_music->times > 0) current_music->times--; } else { current_music->pos = 0; @@ -136,6 +137,7 @@ JA_Music_t *JA_LoadMusic(Uint8* buffer, Uint32 length) SDL_Log("Music length: %f\n", float(music->samples)/float(JA_freq)); if (cvt.needed) { cvt.len = music->samples * chan * 2; + music->length = cvt.len; cvt.buf = (Uint8 *) SDL_malloc(cvt.len * cvt.len_mult); SDL_memcpy(cvt.buf, music->output, cvt.len); SDL_ConvertAudio(&cvt); @@ -316,6 +318,19 @@ int JA_PlaySound(JA_Sound_t *sound, const int loop) return channel; } +int JA_PlaySoundOnChannel(JA_Sound_t *sound, const int channel, const int loop) +{ + if (!JA_soundEnabled) return -1; + + if (channel >= JA_MAX_SIMULTANEOUS_CHANNELS) return -1; + + channels[channel].sound = sound; + channels[channel].times = loop; + channels[channel].pos = 0; + channels[channel].state = JA_CHANNEL_PLAYING; + return channel; +} + void JA_DeleteSound(JA_Sound_t *sound) { for (int i = 0; i < JA_MAX_SIMULTANEOUS_CHANNELS; i++) { diff --git a/source/jail_audio.h b/source/jail_audio.h index d4d8772..67138c7 100644 --- a/source/jail_audio.h +++ b/source/jail_audio.h @@ -28,6 +28,7 @@ JA_Sound_t *JA_NewSound(Uint8* buffer, Uint32 length); JA_Sound_t *JA_LoadSound(Uint8* buffer, Uint32 length); JA_Sound_t *JA_LoadSound(const char* filename); int JA_PlaySound(JA_Sound_t *sound, const int loop = 0); +int JA_PlaySoundOnChannel(JA_Sound_t *sound, const int channel, const int loop = 0); void JA_PauseChannel(const int channel); void JA_ResumeChannel(const int channel); void JA_StopChannel(const int channel);