diff options
| author | StapleButter <thetotalworm@gmail.com> | 2018-12-13 14:34:15 +0100 | 
|---|---|---|
| committer | StapleButter <thetotalworm@gmail.com> | 2018-12-13 14:34:15 +0100 | 
| commit | ef79cc06f3c49e136e88f5dbfb0a1c6bd98f76cd (patch) | |
| tree | e71a2aec00705cd52528ba2ba692294a770bf702 /src | |
| parent | 4d71da04ec5ea736b5143a65f2764caf8020df06 (diff) | |
proof-of-concept code for feeding a WAV to microphone input.
this eventually needs a proper UI.
Diffstat (limited to 'src')
| -rw-r--r-- | src/libui_sdl/main.cpp | 86 | 
1 files changed, 86 insertions, 0 deletions
diff --git a/src/libui_sdl/main.cpp b/src/libui_sdl/main.cpp index 0c7cee2..fdbe795 100644 --- a/src/libui_sdl/main.cpp +++ b/src/libui_sdl/main.cpp @@ -113,6 +113,9 @@ const u32 kMicBufferSize = 2048; // must be power of two  s16 MicBuffer[kMicBufferSize];  u32 MicBufferReadPos, MicBufferWritePos; +u32 MicWavLength; +s16* MicWavBuffer; +  void SetupScreenRects(int width, int height); @@ -140,6 +143,75 @@ bool LocalFileExists(const char* name)  } +void MicLoadWav(char* name) +{ +    SDL_AudioSpec format; +    memset(&format, 0, sizeof(SDL_AudioSpec)); + +    u8* buf; +    u32 len; +    SDL_LoadWAV(name, &format, &buf, &len); + +    const int dstfreq = 44100; + +    if (format.format == AUDIO_S16 || format.format == AUDIO_U16) +    { +        int srcinc = format.channels; +        len /= 2; + +        MicWavLength = (len * dstfreq) / format.freq; +        MicWavBuffer = new s16[MicWavLength]; + +        float res_incr = len / (float)MicWavLength; +        float res_timer = 0; +        int res_pos = 0; + +        for (int i = 0; i < MicWavLength; i++) +        { +            u16 val = ((u16*)buf)[res_pos]; +            if (SDL_AUDIO_ISUNSIGNED(format.format)) val ^= 0x8000; + +            MicWavBuffer[i] = val; + +            res_timer += res_incr; +            while (res_timer >= 1.0) +            { +                res_timer -= 1.0; +                res_pos += srcinc; +            } +        } +    } +    else if (format.format == AUDIO_S8 || format.format == AUDIO_U8) +    { +        int srcinc = format.channels; + +        MicWavLength = (len * dstfreq) / format.freq; +        MicWavBuffer = new s16[MicWavLength]; + +        float res_incr = len / (float)MicWavLength; +        float res_timer = 0; +        int res_pos = 0; + +        for (int i = 0; i < MicWavLength; i++) +        { +            u16 val = buf[res_pos] << 8; +            if (SDL_AUDIO_ISUNSIGNED(format.format)) val ^= 0x8000; + +            MicWavBuffer[i] = val; + +            res_timer += res_incr; +            while (res_timer >= 1.0) +            { +                res_timer -= 1.0; +                res_pos += srcinc; +            } +        } +    } +    else +        printf("bad WAV format %08X\n", format.format); +} + +  void UpdateWindowTitle(void* data)  {      uiWindowSetTitle(MainWindow, (const char*)data); @@ -287,6 +359,18 @@ int EmuThreadFunc(void* burp)                  NDS::MicInputFrame(&MicBuffer[MicBufferReadPos], 735);              MicBufferReadPos += 735;              MicBufferReadPos &= (kMicBufferSize-1); +            /*if ((MicBufferReadPos + 735) > MicWavLength) +            { +                s16 tmp[735]; +                u32 len1 = MicWavLength-MicBufferReadPos; +                memcpy(&tmp[0], &MicWavBuffer[MicBufferReadPos], len1*sizeof(s16)); +                memcpy(&tmp[len1], &MicWavBuffer[0], (735-len1)*sizeof(s16)); +                NDS::MicInputFrame(tmp, 735); +            } +            else +                NDS::MicInputFrame(&MicWavBuffer[MicBufferReadPos], 735); +            MicBufferReadPos += 735; +            MicBufferReadPos %= MicWavLength;*/              // emulate              u32 nlines = NDS::RunFrame(); @@ -1648,6 +1732,8 @@ int main(int argc, char** argv)      MicBufferReadPos = 0;      MicBufferWritePos = 0; +    //MicLoadWav("blorp.wav"); +      // TODO: support more joysticks      if (SDL_NumJoysticks() > 0)          Joystick = SDL_JoystickOpen(0);  |