forked from JailDoctor/JailAudio
Frequency, format and channels are configurable
This commit is contained in:
@@ -33,6 +33,10 @@ JA_Sound_Playing_t *first_sound {NULL};
|
||||
JA_Sound_Playing_t *last_sound {NULL};
|
||||
JA_Sound_Playing_t *free_sounds_list {NULL};
|
||||
|
||||
int JA_freq {48000};
|
||||
SDL_AudioFormat JA_format {AUDIO_S16};
|
||||
int JA_channels {2};
|
||||
|
||||
void audioCallback(void * userdata, uint8_t * stream, int len) {
|
||||
SDL_memset(stream, 0, len);
|
||||
if (music.state == JA_MUSIC_PLAYING) {
|
||||
@@ -77,8 +81,11 @@ void audioCallback(void * userdata, uint8_t * stream, int len) {
|
||||
}
|
||||
}
|
||||
|
||||
void JA_Init() {
|
||||
SDL_AudioSpec audioSpec{48000, AUDIO_S16, 2, 0, 1024, 0, 0, audioCallback, NULL};
|
||||
void JA_Init(const int freq, const SDL_AudioFormat format, const int channels) {
|
||||
JA_freq = freq;
|
||||
JA_format = format;
|
||||
JA_channels = channels;
|
||||
SDL_AudioSpec audioSpec{JA_freq, JA_format, JA_channels, 0, 1024, 0, 0, audioCallback, NULL};
|
||||
SDL_AudioDeviceID sdlAudioDevice = SDL_OpenAudioDevice(NULL, 0, &audioSpec, NULL, 0);
|
||||
free_sounds_list = &sounds[0];
|
||||
for (int i = 0; i < JA_MAX_SIMULTANEOUS_SOUNDS-2;i++) {
|
||||
@@ -93,7 +100,7 @@ void JA_PlayMusic(const char* filename, const bool loop) {
|
||||
music.samples = stb_vorbis_decode_filename(filename, &chan, &samplerate, &music.output);
|
||||
|
||||
SDL_AudioCVT cvt;
|
||||
SDL_BuildAudioCVT(&cvt, AUDIO_S16, chan, samplerate, AUDIO_S16, 2, 48000);
|
||||
SDL_BuildAudioCVT(&cvt, AUDIO_S16, chan, samplerate, JA_format, JA_channels, JA_freq);
|
||||
cvt.len = music.samples * chan * 2;
|
||||
cvt.buf = (Uint8 *) SDL_malloc(cvt.len * cvt.len_mult);
|
||||
SDL_memcpy(cvt.buf, music.output, cvt.len);
|
||||
@@ -128,7 +135,7 @@ JA_Sound JA_LoadSound(const char* filename) {
|
||||
SDL_LoadWAV(filename, &wavSpec, &sound->buffer, &sound->length);
|
||||
|
||||
SDL_AudioCVT cvt;
|
||||
SDL_BuildAudioCVT(&cvt, wavSpec.format, wavSpec.channels, wavSpec.freq, AUDIO_S16, 2, 48000);
|
||||
SDL_BuildAudioCVT(&cvt, wavSpec.format, wavSpec.channels, wavSpec.freq, JA_format, JA_channels, JA_freq);
|
||||
cvt.len = sound->length;
|
||||
cvt.buf = (Uint8 *) SDL_malloc(cvt.len * cvt.len_mult);
|
||||
SDL_memcpy(cvt.buf, sound->buffer, sound->length);
|
||||
|
||||
Reference in New Issue
Block a user