aboutsummaryrefslogtreecommitdiff
path: root/src/libui_sdl
diff options
context:
space:
mode:
authorStapleButter <thetotalworm@gmail.com>2018-12-12 16:33:40 +0100
committerStapleButter <thetotalworm@gmail.com>2018-12-12 16:33:40 +0100
commit758bed93bc321e4909ee5a35cba349c233aa36dd (patch)
tree14fe080f875711ec2e4e6891bbe6149ed9daed1e /src/libui_sdl
parent24d6bd27866648aaf02d4a6fbbcf68c208712180 (diff)
preliminary microphone code. requires an actual microphone. resulting audio is farty and blargy as fuck.
Diffstat (limited to 'src/libui_sdl')
-rw-r--r--src/libui_sdl/main.cpp56
1 files changed, 55 insertions, 1 deletions
diff --git a/src/libui_sdl/main.cpp b/src/libui_sdl/main.cpp
index d620d4d..0c7cee2 100644
--- a/src/libui_sdl/main.cpp
+++ b/src/libui_sdl/main.cpp
@@ -109,6 +109,10 @@ bool Touching = false;
u32 KeyInputMask;
SDL_Joystick* Joystick;
+const u32 kMicBufferSize = 2048; // must be power of two
+s16 MicBuffer[kMicBufferSize];
+u32 MicBufferReadPos, MicBufferWritePos;
+
void SetupScreenRects(int width, int height);
@@ -147,7 +151,6 @@ void AudioCallback(void* data, Uint8* stream, int len)
// 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;
@@ -185,6 +188,23 @@ void AudioCallback(void* data, Uint8* stream, int len)
}
}
+void MicCallback(void* data, Uint8* stream, int len)
+{
+ s16* input = (s16*)stream;
+ len /= sizeof(s16);
+
+ if ((MicBufferWritePos + len) > kMicBufferSize)
+ {
+ u32 len1 = kMicBufferSize-MicBufferWritePos;
+ memcpy(&MicBuffer[MicBufferWritePos], &input[0], len1*sizeof(s16));
+ memcpy(&MicBuffer[0], &input[len1], (len-len1)*sizeof(s16));
+ }
+ else
+ memcpy(&MicBuffer[MicBufferWritePos], input, len*sizeof(s16));
+ MicBufferWritePos += len;
+ MicBufferWritePos &= (kMicBufferSize-1);
+}
+
int EmuThreadFunc(void* burp)
{
NDS::Init();
@@ -254,6 +274,20 @@ int EmuThreadFunc(void* burp)
}
NDS::SetKeyMask(keymask & joymask);
+ // microphone input
+ if ((MicBufferReadPos + 735) > kMicBufferSize)
+ {
+ s16 tmp[735];
+ u32 len1 = kMicBufferSize-MicBufferReadPos;
+ memcpy(&tmp[0], &MicBuffer[MicBufferReadPos], len1*sizeof(s16));
+ memcpy(&tmp[len1], &MicBuffer[0], (735-len1)*sizeof(s16));
+ NDS::MicInputFrame(tmp, 735);
+ }
+ else
+ NDS::MicInputFrame(&MicBuffer[MicBufferReadPos], 735);
+ MicBufferReadPos += 735;
+ MicBufferReadPos &= (kMicBufferSize-1);
+
// emulate
u32 nlines = NDS::RunFrame();
@@ -1594,6 +1628,26 @@ int main(int argc, char** argv)
SDL_PauseAudioDevice(audio, 0);
}
+ memset(&whatIwant, 0, sizeof(SDL_AudioSpec));
+ whatIwant.freq = 44100;
+ whatIwant.format = AUDIO_S16LSB;
+ whatIwant.channels = 1;
+ whatIwant.samples = 1024;
+ whatIwant.callback = MicCallback;
+ audio = SDL_OpenAudioDevice(NULL, 1, &whatIwant, &whatIget, 0);
+ if (!audio)
+ {
+ printf("Mic init failed: %s\n", SDL_GetError());
+ }
+ else
+ {
+ SDL_PauseAudioDevice(audio, 0);
+ }
+
+ memset(MicBuffer, 0, sizeof(MicBuffer));
+ MicBufferReadPos = 0;
+ MicBufferWritePos = 0;
+
// TODO: support more joysticks
if (SDL_NumJoysticks() > 0)
Joystick = SDL_JoystickOpen(0);