diff --git a/jail_audio.cpp b/jail_audio.cpp index 1b8768b..87ef6d5 100644 --- a/jail_audio.cpp +++ b/jail_audio.cpp @@ -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); diff --git a/jail_audio.h b/jail_audio.h index 4228b11..ae73a3c 100644 --- a/jail_audio.h +++ b/jail_audio.h @@ -1,8 +1,9 @@ #pragma once +#include typedef struct JA_Sound_t *JA_Sound; -void JA_Init(); +void JA_Init(const int freq, const SDL_AudioFormat format, const int channels); void JA_PlayMusic(const char* filename, const bool loop = true); void JA_PauseMusic(); diff --git a/main.cpp b/main.cpp index 20e4600..7a729b0 100644 --- a/main.cpp +++ b/main.cpp @@ -9,7 +9,7 @@ SDL_Window *sdlWindow; int main(int argc, char **argv) { SDL_Init(SDL_INIT_EVERYTHING); sdlWindow = SDL_CreateWindow("JailAudio", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 320, 240, SDL_WINDOW_SHOWN); - JA_Init(); + JA_Init(48000, AUDIO_S16, 2); JA_PlayMusic("intro2.ogg"); JA_Sound peiv = JA_LoadSound("menu_select.wav");