From 74df385b202ccd06e2175c826a5e045209f0a98d Mon Sep 17 00:00:00 2001 From: Jeffery Myers Date: Sat, 20 Mar 2021 18:47:28 -0700 Subject: [PATCH] Init MinAudio to a sample rate of 0 to let the device pick the rate. Read the rate from the device after it starts up. Convert AUDIO_DEVICE_SAMPLE_RATE from a #def into an int, that is set from the device's rate Set all sample systems to use the AUDIO_DEVICE_SAMPLE_RATE as the target rate to minimize resampling. --- src/config.h | 1 - src/raudio.c | 15 ++++++++------- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/config.h b/src/config.h index ce943901d..0f8884779 100644 --- a/src/config.h +++ b/src/config.h @@ -195,7 +195,6 @@ //------------------------------------------------------------------------------------ #define AUDIO_DEVICE_FORMAT ma_format_f32 // Device output format (miniaudio: float-32bit) #define AUDIO_DEVICE_CHANNELS 2 // Device output channels: stereo -#define AUDIO_DEVICE_SAMPLE_RATE 44100 // Device output sample rate #define DEFAULT_AUDIO_BUFFER_SIZE 4096 // Default audio buffer size for streaming #define MAX_AUDIO_BUFFER_POOL_CHANNELS 16 // Maximum number of audio pool channels diff --git a/src/raudio.c b/src/raudio.c index 7fc847fe3..d1f197061 100644 --- a/src/raudio.c +++ b/src/raudio.c @@ -256,9 +256,8 @@ typedef struct tagBITMAPINFOHEADER { #ifndef AUDIO_DEVICE_CHANNELS #define AUDIO_DEVICE_CHANNELS 2 // Device output channels: stereo #endif -#ifndef AUDIO_DEVICE_SAMPLE_RATE - #define AUDIO_DEVICE_SAMPLE_RATE 44100 // Device output sample rate -#endif +int AUDIO_DEVICE_SAMPLE_RATE = 0; + #ifndef MAX_AUDIO_BUFFER_POOL_CHANNELS #define MAX_AUDIO_BUFFER_POOL_CHANNELS 16 // Audio pool channels #endif @@ -434,7 +433,7 @@ void InitAudioDevice(void) config.capture.pDeviceID = NULL; // NULL for the default capture AUDIO.System.device. config.capture.format = ma_format_s16; config.capture.channels = 1; - config.sampleRate = AUDIO_DEVICE_SAMPLE_RATE; + config.sampleRate = 0; config.dataCallback = OnSendAudioDataToDevice; config.pUserData = NULL; @@ -457,6 +456,8 @@ void InitAudioDevice(void) return; } + AUDIO_DEVICE_SAMPLE_RATE = AUDIO.System.device.sampleRate; + // Mixing happens on a seperate thread which means we need to synchronize. I'm using a mutex here to make things simple, but may // want to look at something a bit smarter later on to keep everything real-time, if that's necessary. if (ma_mutex_init(&AUDIO.System.lock) != MA_SUCCESS) @@ -1219,7 +1220,7 @@ Music LoadMusicStream(const char *fileName) else if (IsFileExtension(fileName, ".xm")) { jar_xm_context_t *ctxXm = NULL; - int result = jar_xm_create_context_from_file(&ctxXm, 48000, fileName); + int result = jar_xm_create_context_from_file(&ctxXm, AUDIO_DEVICE_SAMPLE_RATE, fileName); music.ctxType = MUSIC_MODULE_XM; music.ctxData = ctxXm; @@ -1229,7 +1230,7 @@ Music LoadMusicStream(const char *fileName) jar_xm_set_max_loop_count(ctxXm, 0); // Set infinite number of loops // NOTE: Only stereo is supported for XM - music.stream = InitAudioStream(48000, 16, 2); + music.stream = InitAudioStream(AUDIO_DEVICE_SAMPLE_RATE, 16, AUDIO_DEVICE_CHANNELS); music.sampleCount = (unsigned int)jar_xm_get_remaining_samples(ctxXm)*2; // 2 channels music.looping = true; // Looping enabled by default jar_xm_reset(ctxXm); // make sure we start at the beginning of the song @@ -1250,7 +1251,7 @@ Music LoadMusicStream(const char *fileName) if (result > 0) { // NOTE: Only stereo is supported for MOD - music.stream = InitAudioStream(48000, 16, 2); + music.stream = InitAudioStream(AUDIO_DEVICE_SAMPLE_RATE, 16, AUDIO_DEVICE_CHANNELS); music.sampleCount = (unsigned int)jar_mod_max_samples(ctxMod)*2; // 2 channels music.looping = true; // Looping enabled by default musicLoaded = true;