SDL2: Use the newer sound API and make some requests to improve audio rendition

This commit is contained in:
Jonathan Campbell 2025-01-30 20:27:07 -08:00
parent 706fd0ecd1
commit 75aa320879
3 changed files with 65 additions and 2 deletions

View File

@ -20,6 +20,16 @@
#ifndef DOSBOX_MIXER_H
#define DOSBOX_MIXER_H
/*
#ifdef C_SDL2
#define SDL_LockAudio __DO_NOT_USE__1
#define SDL_UnlockAudio __DO_NOT_USE__2
#define SDL_PauseAudio __DO_NOT_USE__3
#define SDL_CloseAudio __DO_NOT_USE__4
#define SDL_OpenAudio __DO_NOT_USE__5
#endif
*/
typedef void (*MIXER_MixHandler)(uint8_t * sampdate,uint32_t len);
typedef void (*MIXER_Handler)(Bitu len);

View File

@ -64,6 +64,10 @@
#define MIXER_SSIZE 4
#define MIXER_VOLSHIFT 13
#ifdef C_SDL2
SDL_AudioDeviceID SDL2_AudioDevice = 0; /* valid IDs are 2 or higher, 1 for compat, 0 is never a valid ID */
#endif
static INLINE int16_t MIXER_CLIP(Bits SAMP) {
if (SAMP < MAX_AUDIO) {
if (SAMP > MIN_AUDIO)
@ -837,11 +841,19 @@ static void MIXER_MixData(Bitu fracs/*render up to*/) {
}
static void MIXER_FillUp(void) {
#ifdef C_SDL2
SDL_LockAudioDevice(SDL2_AudioDevice);
#else
SDL_LockAudio();
#endif
float index = PIC_TickIndex();
if (index < 0) index = 0;
MIXER_MixData((Bitu)((double)index * ((Bitu)mixer.samples_this_ms.w * mixer.samples_this_ms.fd)));
#ifdef C_SDL2
SDL_UnlockAudioDevice(SDL2_AudioDevice);
#else
SDL_UnlockAudio();
#endif
}
void MixerChannel::FillUp(void) {
@ -856,7 +868,11 @@ void MIXER_MixSingle(Bitu /*val*/) {
static void MIXER_Mix(void) {
Bitu thr;
#ifdef C_SDL2
SDL_LockAudioDevice(SDL2_AudioDevice);
#else
SDL_LockAudio();
#endif
/* render */
assert((mixer.work_in+mixer.samples_per_ms.w) <= MIXER_BUFSIZE);
@ -884,7 +900,11 @@ static void MIXER_Mix(void) {
memset(&mixer.work[mixer.work_in][0],0,sizeof(int32_t)*2*mixer.samples_this_ms.w);
mixer.samples_rendered_ms.fn = 0;
mixer.samples_rendered_ms.w = 0;
#ifdef C_SDL2
SDL_UnlockAudioDevice(SDL2_AudioDevice);
#else
SDL_UnlockAudio();
#endif
MIXER_FillUp();
}
@ -1208,10 +1228,21 @@ void MIXER_Init() {
spec.userdata=NULL;
spec.samples=(Uint16)mixer.blocksize;
#ifdef C_SDL2
if (SDL_InitSubSystem(SDL_INIT_AUDIO) < 0) {
LOG(LOG_MISC,LOG_DEBUG)("MIXER:Can't initialize SDL audio: %s , running in nosound mode.",SDL_GetError());
mixer.nosound = true;
}
#endif
if (mixer.nosound) {
LOG(LOG_MISC,LOG_DEBUG)("MIXER:No Sound Mode Selected.");
TIMER_AddTickHandler(MIXER_Mix);
#ifdef C_SDL2
} else if ((SDL2_AudioDevice=SDL_OpenAudioDevice(NULL, 0, &spec, &obtained, SDL_AUDIO_ALLOW_FREQUENCY_CHANGE | SDL_AUDIO_ALLOW_SAMPLES_CHANGE)) == 0) {
#else
} else if (SDL_OpenAudio(&spec, &obtained) <0 ) {
#endif
mixer.nosound = true;
LOG(LOG_MISC,LOG_DEBUG)("MIXER:Can't open audio: %s , running in nosound mode.",SDL_GetError());
TIMER_AddTickHandler(MIXER_Mix);
@ -1219,7 +1250,12 @@ void MIXER_Init() {
mixer.nosound = true;
LOG(LOG_MISC,LOG_DEBUG)("MIXER:Failed to get the sample format I wanted.");
TIMER_AddTickHandler(MIXER_Mix);
#ifdef C_SDL2
SDL_CloseAudioDevice(SDL2_AudioDevice);
SDL2_AudioDevice = 0;
#else
SDL_CloseAudio();
#endif
} else {
if(((Bitu)mixer.freq != (Bitu)obtained.freq) || ((Bitu)mixer.blocksize != (Bitu)obtained.samples))
LOG(LOG_MISC,LOG_DEBUG)("MIXER:Got different values from SDL: freq %d, blocksize %d",(int)obtained.freq,(int)obtained.samples);
@ -1228,7 +1264,11 @@ void MIXER_Init() {
mixer.blocksize=obtained.samples;
TIMER_AddTickHandler(MIXER_Mix);
if (mixer.sampleaccurate) PIC_AddEvent(MIXER_MixSingle,1000.0 / mixer.freq);
#ifdef C_SDL2
SDL_PauseAudioDevice(SDL2_AudioDevice, 0);
#else
SDL_PauseAudio(0);
#endif
}
mixer_start_pic_time = PIC_FullIndex();
mixer_sample_counter = 0;

View File

@ -13,6 +13,7 @@
#include "mapper.h"
#include "control.h"
#include "logging.h"
#include "mixer.h"
#include "build_timestamp.h"
#ifdef WIN32
#include "direct.h"
@ -36,6 +37,10 @@
#include "../libs/tinyfiledialogs/tinyfiledialogs.h"
#endif
#ifdef C_SDL2
extern SDL_AudioDeviceID SDL2_AudioDevice; /* valid IDs are 2 or higher, 1 for compat, 0 is never a valid ID */
#endif
extern unsigned int page;
extern int autosave_last[10], autosave_count;
extern std::string autosave_name[10], savefilename;
@ -1056,7 +1061,11 @@ int flagged_restore(char* zip);
void SaveState::save(size_t slot) { //throw (Error)
if (slot >= SLOT_COUNT*MAX_PAGE) return;
SDL_PauseAudio(0);
#ifdef C_SDL2
SDL_PauseAudioDevice(SDL2_AudioDevice, 0);
#else
SDL_PauseAudio(0);
#endif
bool save_err=false;
if((MEM_TotalPages()*4096/1024/1024)>1024) {
LOG_MSG("Stopped. 1 GB is the maximum memory size for saving/loading states.");
@ -1278,7 +1287,11 @@ void SaveState::load(size_t slot) const { //throw (Error)
notifyError("Unsupported memory size for loading states.", false);
return;
}
SDL_PauseAudio(0);
#ifdef C_SDL2
SDL_PauseAudioDevice(SDL2_AudioDevice, 0);
#else
SDL_PauseAudio(0);
#endif
extern const char* RunningProgram;
bool read_version=false;
bool read_title=false;