proof-of-concept code for feeding a WAV to microphone input.

this eventually needs a proper UI.
This commit is contained in:
StapleButter 2018-12-13 14:34:15 +01:00
parent 4d71da04ec
commit ef79cc06f3
1 changed files with 86 additions and 0 deletions

View File

@ -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);