Frequency, format and channels are configurable

This commit is contained in:
2021-02-02 21:18:07 +01:00
parent 6ce37e56e7
commit 3cc806bd17
3 changed files with 14 additions and 6 deletions

View File

@@ -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);

View File

@@ -1,8 +1,9 @@
#pragma once
#include <SDL2/SDL.h>
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();

View File

@@ -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");