use device sample rate instead of separate var

let config define a device sample rate if it wants to, but let the default be 0 for native rate.
This commit is contained in:
Jeffery Myers 2021-03-21 13:28:46 -07:00
parent 74df385b20
commit 2812702e6f
2 changed files with 14 additions and 13 deletions

View File

@ -195,6 +195,7 @@
//------------------------------------------------------------------------------------ //------------------------------------------------------------------------------------
#define AUDIO_DEVICE_FORMAT ma_format_f32 // Device output format (miniaudio: float-32bit) #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_CHANNELS 2 // Device output channels: stereo
#define AUDIO_DEVICE_SAMPLE_RATE 0 // Device sample rate (device default)
#define DEFAULT_AUDIO_BUFFER_SIZE 4096 // Default audio buffer size for streaming #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 #define MAX_AUDIO_BUFFER_POOL_CHANNELS 16 // Maximum number of audio pool channels

View File

@ -256,8 +256,10 @@ typedef struct tagBITMAPINFOHEADER {
#ifndef AUDIO_DEVICE_CHANNELS #ifndef AUDIO_DEVICE_CHANNELS
#define AUDIO_DEVICE_CHANNELS 2 // Device output channels: stereo #define AUDIO_DEVICE_CHANNELS 2 // Device output channels: stereo
#endif #endif
int AUDIO_DEVICE_SAMPLE_RATE = 0;
#ifndef AUDIO_DEVICE_SAMPLE_RATE
#define AUDIO_DEVICE_SAMPLE_RATE 0 // Device output channels: stereo
#endif
#ifndef MAX_AUDIO_BUFFER_POOL_CHANNELS #ifndef MAX_AUDIO_BUFFER_POOL_CHANNELS
#define MAX_AUDIO_BUFFER_POOL_CHANNELS 16 // Audio pool channels #define MAX_AUDIO_BUFFER_POOL_CHANNELS 16 // Audio pool channels
#endif #endif
@ -433,7 +435,7 @@ void InitAudioDevice(void)
config.capture.pDeviceID = NULL; // NULL for the default capture AUDIO.System.device. config.capture.pDeviceID = NULL; // NULL for the default capture AUDIO.System.device.
config.capture.format = ma_format_s16; config.capture.format = ma_format_s16;
config.capture.channels = 1; config.capture.channels = 1;
config.sampleRate = 0; config.sampleRate = AUDIO_DEVICE_SAMPLE_RATE;
config.dataCallback = OnSendAudioDataToDevice; config.dataCallback = OnSendAudioDataToDevice;
config.pUserData = NULL; config.pUserData = NULL;
@ -456,8 +458,6 @@ void InitAudioDevice(void)
return; 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 // 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. // 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) if (ma_mutex_init(&AUDIO.System.lock) != MA_SUCCESS)
@ -473,7 +473,7 @@ void InitAudioDevice(void)
{ {
// WARNING: An empty audioBuffer is created (data = 0) // WARNING: An empty audioBuffer is created (data = 0)
// AudioBuffer data just points to loaded sound data // AudioBuffer data just points to loaded sound data
AUDIO.MultiChannel.pool[i] = LoadAudioBuffer(AUDIO_DEVICE_FORMAT, AUDIO_DEVICE_CHANNELS, AUDIO_DEVICE_SAMPLE_RATE, 0, AUDIO_BUFFER_USAGE_STATIC); AUDIO.MultiChannel.pool[i] = LoadAudioBuffer(AUDIO_DEVICE_FORMAT, AUDIO_DEVICE_CHANNELS, AUDIO.System.device.sampleRate, 0, AUDIO_BUFFER_USAGE_STATIC);
} }
TRACELOG(LOG_INFO, "AUDIO: Device initialized successfully"); TRACELOG(LOG_INFO, "AUDIO: Device initialized successfully");
@ -546,7 +546,7 @@ AudioBuffer *LoadAudioBuffer(ma_format format, ma_uint32 channels, ma_uint32 sam
if (sizeInFrames > 0) audioBuffer->data = RL_CALLOC(sizeInFrames*channels*ma_get_bytes_per_sample(format), 1); if (sizeInFrames > 0) audioBuffer->data = RL_CALLOC(sizeInFrames*channels*ma_get_bytes_per_sample(format), 1);
// Audio data runs through a format converter // Audio data runs through a format converter
ma_data_converter_config converterConfig = ma_data_converter_config_init(format, AUDIO_DEVICE_FORMAT, channels, AUDIO_DEVICE_CHANNELS, sampleRate, AUDIO_DEVICE_SAMPLE_RATE); ma_data_converter_config converterConfig = ma_data_converter_config_init(format, AUDIO_DEVICE_FORMAT, channels, AUDIO_DEVICE_CHANNELS, sampleRate, AUDIO.System.device.sampleRate);
converterConfig.resampling.allowDynamicSampleRate = true; // Required for pitch shifting converterConfig.resampling.allowDynamicSampleRate = true; // Required for pitch shifting
ma_result result = ma_data_converter_init(&converterConfig, &audioBuffer->converter); ma_result result = ma_data_converter_init(&converterConfig, &audioBuffer->converter);
@ -782,21 +782,21 @@ Sound LoadSoundFromWave(Wave wave)
ma_format formatIn = ((wave.sampleSize == 8)? ma_format_u8 : ((wave.sampleSize == 16)? ma_format_s16 : ma_format_f32)); ma_format formatIn = ((wave.sampleSize == 8)? ma_format_u8 : ((wave.sampleSize == 16)? ma_format_s16 : ma_format_f32));
ma_uint32 frameCountIn = wave.sampleCount/wave.channels; ma_uint32 frameCountIn = wave.sampleCount/wave.channels;
ma_uint32 frameCount = (ma_uint32)ma_convert_frames(NULL, 0, AUDIO_DEVICE_FORMAT, AUDIO_DEVICE_CHANNELS, AUDIO_DEVICE_SAMPLE_RATE, NULL, frameCountIn, formatIn, wave.channels, wave.sampleRate); ma_uint32 frameCount = (ma_uint32)ma_convert_frames(NULL, 0, AUDIO_DEVICE_FORMAT, AUDIO_DEVICE_CHANNELS, AUDIO.System.device.sampleRate, NULL, frameCountIn, formatIn, wave.channels, wave.sampleRate);
if (frameCount == 0) TRACELOG(LOG_WARNING, "SOUND: Failed to get frame count for format conversion"); if (frameCount == 0) TRACELOG(LOG_WARNING, "SOUND: Failed to get frame count for format conversion");
AudioBuffer *audioBuffer = LoadAudioBuffer(AUDIO_DEVICE_FORMAT, AUDIO_DEVICE_CHANNELS, AUDIO_DEVICE_SAMPLE_RATE, frameCount, AUDIO_BUFFER_USAGE_STATIC); AudioBuffer *audioBuffer = LoadAudioBuffer(AUDIO_DEVICE_FORMAT, AUDIO_DEVICE_CHANNELS, AUDIO.System.device.sampleRate, frameCount, AUDIO_BUFFER_USAGE_STATIC);
if (audioBuffer == NULL) if (audioBuffer == NULL)
{ {
TRACELOG(LOG_WARNING, "SOUND: Failed to create buffer"); TRACELOG(LOG_WARNING, "SOUND: Failed to create buffer");
return sound; // early return to avoid dereferencing the audioBuffer null pointer return sound; // early return to avoid dereferencing the audioBuffer null pointer
} }
frameCount = (ma_uint32)ma_convert_frames(audioBuffer->data, frameCount, AUDIO_DEVICE_FORMAT, AUDIO_DEVICE_CHANNELS, AUDIO_DEVICE_SAMPLE_RATE, wave.data, frameCountIn, formatIn, wave.channels, wave.sampleRate); frameCount = (ma_uint32)ma_convert_frames(audioBuffer->data, frameCount, AUDIO_DEVICE_FORMAT, AUDIO_DEVICE_CHANNELS, AUDIO.System.device.sampleRate, wave.data, frameCountIn, formatIn, wave.channels, wave.sampleRate);
if (frameCount == 0) TRACELOG(LOG_WARNING, "SOUND: Failed format conversion"); if (frameCount == 0) TRACELOG(LOG_WARNING, "SOUND: Failed format conversion");
sound.sampleCount = frameCount*AUDIO_DEVICE_CHANNELS; sound.sampleCount = frameCount*AUDIO_DEVICE_CHANNELS;
sound.stream.sampleRate = AUDIO_DEVICE_SAMPLE_RATE; sound.stream.sampleRate = AUDIO.System.device.sampleRate;
sound.stream.sampleSize = 32; sound.stream.sampleSize = 32;
sound.stream.channels = AUDIO_DEVICE_CHANNELS; sound.stream.channels = AUDIO_DEVICE_CHANNELS;
sound.stream.buffer = audioBuffer; sound.stream.buffer = audioBuffer;
@ -1220,7 +1220,7 @@ Music LoadMusicStream(const char *fileName)
else if (IsFileExtension(fileName, ".xm")) else if (IsFileExtension(fileName, ".xm"))
{ {
jar_xm_context_t *ctxXm = NULL; jar_xm_context_t *ctxXm = NULL;
int result = jar_xm_create_context_from_file(&ctxXm, AUDIO_DEVICE_SAMPLE_RATE, fileName); int result = jar_xm_create_context_from_file(&ctxXm, AUDIO.System.device.sampleRate, fileName);
music.ctxType = MUSIC_MODULE_XM; music.ctxType = MUSIC_MODULE_XM;
music.ctxData = ctxXm; music.ctxData = ctxXm;
@ -1230,7 +1230,7 @@ Music LoadMusicStream(const char *fileName)
jar_xm_set_max_loop_count(ctxXm, 0); // Set infinite number of loops jar_xm_set_max_loop_count(ctxXm, 0); // Set infinite number of loops
// NOTE: Only stereo is supported for XM // NOTE: Only stereo is supported for XM
music.stream = InitAudioStream(AUDIO_DEVICE_SAMPLE_RATE, 16, AUDIO_DEVICE_CHANNELS); music.stream = InitAudioStream(AUDIO.System.device.sampleRate, 16, AUDIO_DEVICE_CHANNELS);
music.sampleCount = (unsigned int)jar_xm_get_remaining_samples(ctxXm)*2; // 2 channels music.sampleCount = (unsigned int)jar_xm_get_remaining_samples(ctxXm)*2; // 2 channels
music.looping = true; // Looping enabled by default music.looping = true; // Looping enabled by default
jar_xm_reset(ctxXm); // make sure we start at the beginning of the song jar_xm_reset(ctxXm); // make sure we start at the beginning of the song
@ -1251,7 +1251,7 @@ Music LoadMusicStream(const char *fileName)
if (result > 0) if (result > 0)
{ {
// NOTE: Only stereo is supported for MOD // NOTE: Only stereo is supported for MOD
music.stream = InitAudioStream(AUDIO_DEVICE_SAMPLE_RATE, 16, AUDIO_DEVICE_CHANNELS); music.stream = InitAudioStream(AUDIO.System.device.sampleRate, 16, AUDIO_DEVICE_CHANNELS);
music.sampleCount = (unsigned int)jar_mod_max_samples(ctxMod)*2; // 2 channels music.sampleCount = (unsigned int)jar_mod_max_samples(ctxMod)*2; // 2 channels
music.looping = true; // Looping enabled by default music.looping = true; // Looping enabled by default
musicLoaded = true; musicLoaded = true;