diff options
| author | StapleButter <thetotalworm@gmail.com> | 2017-12-11 18:41:05 +0100 | 
|---|---|---|
| committer | StapleButter <thetotalworm@gmail.com> | 2017-12-11 18:41:05 +0100 | 
| commit | 73bf4471ee6bd25b1216328b46cfa8204b02f383 (patch) | |
| tree | 3eb6cc5568bf0820d8d6cfc60b89866ca25f9c6d /src/libui_sdl | |
| parent | c6391525416f9994b14278f2610f4ceeed6a6fd4 (diff) | |
audio: change output rate to 47340Hz, add resampler with small margin to elimiate pops/clicks due to output rate fluctuation
output rate is picked such that 1024 samples at that rate equal exactly 710 samples at the SPU's output rate
Diffstat (limited to 'src/libui_sdl')
| -rw-r--r-- | src/libui_sdl/main.cpp | 49 | 
1 files changed, 41 insertions, 8 deletions
diff --git a/src/libui_sdl/main.cpp b/src/libui_sdl/main.cpp index 32252f5..eb6a108 100644 --- a/src/libui_sdl/main.cpp +++ b/src/libui_sdl/main.cpp @@ -96,7 +96,46 @@ void UpdateWindowTitle(void* data)  void AudioCallback(void* data, Uint8* stream, int len)  { -    SPU::ReadOutput((s16*)stream, len>>2); +    // resampling: +    // buffer length is 1024 samples +    // which is 710 samples at the original sample rate + +    //SPU::ReadOutput((s16*)stream, len>>2); +    s16 buf_in[710*2]; +    s16* buf_out = (s16*)stream; + +    int num_in = SPU::ReadOutput(buf_in, 710); +    int num_out = 1024; + +    int margin = 6; +    if (num_in < 710-margin) +    { +        int last = num_in-1; +        if (last < 0) last = 0; + +        for (int i = num_in; i < 710-margin; i++) +            ((u32*)buf_in)[i] = ((u32*)buf_in)[last]; + +        num_in = 710-margin; +    } + +    float res_incr = num_in / (float)num_out; +    float res_timer = 0; +    int res_pos = 0; + +    for (int i = 0; i < 1024; i++) +    { +        // TODO: interp!! +        buf_out[i*2  ] = buf_in[res_pos*2  ]; +        buf_out[i*2+1] = buf_in[res_pos*2+1]; + +        res_timer += res_incr; +        while (res_timer >= 1.0) +        { +            res_timer -= 1.0; +            res_pos++; +        } +    }  }  int EmuThreadFunc(void* burp) @@ -111,15 +150,9 @@ int EmuThreadFunc(void* burp)      ScreenDrawInited = false;      Touching = false; -    // DS: -    // 547.060546875 samples per frame -    // 32823.6328125 samples per second -    // -    // 48000 samples per second: -    // 800 samples per frame      SDL_AudioSpec whatIwant, whatIget;      memset(&whatIwant, 0, sizeof(SDL_AudioSpec)); -    whatIwant.freq = 32824; // 32823.6328125 +    whatIwant.freq = 47340;      whatIwant.format = AUDIO_S16LSB;      whatIwant.channels = 2;      whatIwant.samples = 1024;  |