diff options
author | Arisotura <thetotalworm@gmail.com> | 2019-09-01 15:41:46 +0200 |
---|---|---|
committer | Arisotura <thetotalworm@gmail.com> | 2019-09-01 15:41:46 +0200 |
commit | 455b0e568987b66e757f98869450decb94e02d28 (patch) | |
tree | 6db5678edbc88e9c7e74d16e320748fbe1da50ae /src | |
parent | 210c1056b065a003f13506693da97f4af46fa1db (diff) |
more fixes to audio output
maybe by 2034 we'll finally get that shit going
Diffstat (limited to 'src')
-rw-r--r-- | src/SPU.cpp | 17 | ||||
-rw-r--r-- | src/SPU.h | 2 | ||||
-rw-r--r-- | src/libui_sdl/main.cpp | 20 |
3 files changed, 34 insertions, 5 deletions
diff --git a/src/SPU.cpp b/src/SPU.cpp index 6b594b0..b36becf 100644 --- a/src/SPU.cpp +++ b/src/SPU.cpp @@ -98,9 +98,7 @@ void DeInit() void Reset() { - memset(OutputBuffer, 0, 2*OutputBufferSize*2); - OutputReadOffset = 0; - OutputWriteOffset = 0;//OutputBufferSize; + InitOutput(); Cnt = 0; MasterVolume = 0; @@ -740,6 +738,19 @@ void Mix(u32 samples) } +void DrainOutput() +{ + OutputReadOffset = 0; + OutputWriteOffset = 0; +} + +void InitOutput() +{ + memset(OutputBuffer, 0, 2*OutputBufferSize*2); + OutputReadOffset = 0; + OutputWriteOffset = OutputBufferSize; +} + int GetOutputSize() { int ret; @@ -35,6 +35,8 @@ void SetBias(u16 bias); void Mix(u32 samples); +void DrainOutput(); +void InitOutput(); int GetOutputSize(); void Sync(bool wait); int ReadOutput(s16* data, int samples); diff --git a/src/libui_sdl/main.cpp b/src/libui_sdl/main.cpp index 7d7bce0..1b4a9c6 100644 --- a/src/libui_sdl/main.cpp +++ b/src/libui_sdl/main.cpp @@ -159,6 +159,7 @@ int JoystickID; SDL_Joystick* Joystick; int AudioFreq; +float AudioSampleFrac; SDL_AudioDeviceID AudioDevice, MicDevice; u32 MicBufferLength = 2048; @@ -565,7 +566,10 @@ void AudioCallback(void* data, Uint8* stream, int len) // resample incoming audio to match the output sample rate - int len_in = (int)ceil((len * 32823.6328125) / (float)AudioFreq); + float f_len_in = (len * 32823.6328125) / (float)AudioFreq; + f_len_in += AudioSampleFrac; + int len_in = (int)floor(f_len_in); + AudioSampleFrac = f_len_in - len_in; s16 buf_in[1024*2]; s16* buf_out = (s16*)stream; @@ -573,6 +577,12 @@ void AudioCallback(void* data, Uint8* stream, int len) int num_in = SPU::ReadOutput(buf_in, len_in); int num_out = len; + if (num_in < 1) + { + memset(stream, 0, len*sizeof(s16)*2); + return; + } + int margin = 6; if (num_in < len_in-margin) { @@ -591,7 +601,7 @@ void AudioCallback(void* data, Uint8* stream, int len) int volume = Config::AudioVolume; - for (int i = 0; i < 1024; i++) + for (int i = 0; i < len; i++) { buf_out[i*2 ] = (buf_in[res_pos*2 ] * volume) >> 8; buf_out[i*2+1] = (buf_in[res_pos*2+1] * volume) >> 8; @@ -1522,6 +1532,8 @@ void Run() EmuRunning = 1; RunningSomething = true; + SPU::InitOutput(); + AudioSampleFrac = 0; SDL_PauseAudioDevice(AudioDevice, 0); SDL_PauseAudioDevice(MicDevice, 0); @@ -1560,6 +1572,7 @@ void TogglePause(void* blarg) EmuRunning = 2; uiMenuItemSetChecked(MenuItem_Pause, 1); + SPU::DrainOutput(); SDL_PauseAudioDevice(AudioDevice, 1); SDL_PauseAudioDevice(MicDevice, 1); @@ -1571,6 +1584,8 @@ void TogglePause(void* blarg) EmuRunning = 1; uiMenuItemSetChecked(MenuItem_Pause, 0); + SPU::InitOutput(); + AudioSampleFrac = 0; SDL_PauseAudioDevice(AudioDevice, 0); SDL_PauseAudioDevice(MicDevice, 0); @@ -1621,6 +1636,7 @@ void Stop(bool internal) uiAreaQueueRedrawAll(MainDrawArea); + SPU::DrainOutput(); SDL_PauseAudioDevice(AudioDevice, 1); SDL_PauseAudioDevice(MicDevice, 1); |