Actualitzat jail_audio

This commit is contained in:
2024-12-30 13:30:06 +01:00
parent e61daeb92e
commit cfaa143c44
2 changed files with 20 additions and 4 deletions

View File

@@ -20,6 +20,7 @@ struct JA_Channel_t {
struct JA_Music_t { struct JA_Music_t {
int samples {0}; int samples {0};
Uint32 length {0};
int pos {0}; int pos {0};
int times {0}; int times {0};
short* output {NULL}; short* output {NULL};
@@ -60,13 +61,13 @@ void audioCallback(void * userdata, uint8_t * stream, int len) {
volume = JA_musicVolume * (1.0 - percent); volume = JA_musicVolume * (1.0 - percent);
} }
} }
const int size = SDL_min(len, (current_music->samples-current_music->pos)*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); SDL_MixAudioFormat(stream, (Uint8*)(current_music->output)+current_music->pos, AUDIO_S16, size, volume);
current_music->pos += size/2; current_music->pos += size;
if (size < len) { if (size < len) {
if (current_music->times != 0) { if (current_music->times != 0) {
SDL_MixAudioFormat(stream+size, (Uint8*)current_music->output, AUDIO_S16, len-size, volume); 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--; if (current_music->times > 0) current_music->times--;
} else { } else {
current_music->pos = 0; 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)); SDL_Log("Music length: %f\n", float(music->samples)/float(JA_freq));
if (cvt.needed) { if (cvt.needed) {
cvt.len = music->samples * chan * 2; cvt.len = music->samples * chan * 2;
music->length = cvt.len;
cvt.buf = (Uint8 *) SDL_malloc(cvt.len * cvt.len_mult); cvt.buf = (Uint8 *) SDL_malloc(cvt.len * cvt.len_mult);
SDL_memcpy(cvt.buf, music->output, cvt.len); SDL_memcpy(cvt.buf, music->output, cvt.len);
SDL_ConvertAudio(&cvt); SDL_ConvertAudio(&cvt);
@@ -316,6 +318,19 @@ int JA_PlaySound(JA_Sound_t *sound, const int loop)
return channel; 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) void JA_DeleteSound(JA_Sound_t *sound)
{ {
for (int i = 0; i < JA_MAX_SIMULTANEOUS_CHANNELS; i++) { for (int i = 0; i < JA_MAX_SIMULTANEOUS_CHANNELS; i++) {

View File

@@ -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(Uint8* buffer, Uint32 length);
JA_Sound_t *JA_LoadSound(const char* filename); JA_Sound_t *JA_LoadSound(const char* filename);
int JA_PlaySound(JA_Sound_t *sound, const int loop = 0); 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_PauseChannel(const int channel);
void JA_ResumeChannel(const int channel); void JA_ResumeChannel(const int channel);
void JA_StopChannel(const int channel); void JA_StopChannel(const int channel);