Skip to content

Commit

Permalink
make mic input less shitty (and less dangerous)
Browse files Browse the repository at this point in the history
  • Loading branch information
Arisotura committed Nov 1, 2024
1 parent 78aae25 commit 58ee191
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 14 deletions.
1 change: 0 additions & 1 deletion src/frontend/qt_sdl/EmuInstance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,6 @@ EmuInstance::EmuInstance(int inst) : deleting(false),
// if any extra windows were saved as enabled, open them
for (int i = 1; i < kMaxWindows; i++)
{
//Config::Table tbl = localCfg.GetTable("Window"+std::to_string(i), "Window0");
std::string key = "Window" + std::to_string(i) + ".Enabled";
bool enable = localCfg.GetBool(key);
if (enable)
Expand Down
5 changes: 4 additions & 1 deletion src/frontend/qt_sdl/EmuInstance.h
Original file line number Diff line number Diff line change
Expand Up @@ -298,8 +298,9 @@ class EmuInstance
int mpAudioMode;

SDL_AudioDeviceID micDevice;
melonDS::s16 micExtBuffer[2048];
melonDS::s16 micExtBuffer[4096];
melonDS::u32 micExtBufferWritePos;
melonDS::u32 micExtBufferCount;

melonDS::u32 micWavLength;
melonDS::s16* micWavBuffer;
Expand All @@ -308,6 +309,8 @@ class EmuInstance
melonDS::u32 micBufferLength;
melonDS::u32 micBufferReadPos;

SDL_mutex* micLock;

//int audioInterp;
int audioVolume;
bool audioDSiVolumeSync;
Expand Down
57 changes: 45 additions & 12 deletions src/frontend/qt_sdl/EmuInstanceAudio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,12 @@ void EmuInstance::micCallback(void* data, Uint8* stream, int len)
s16* input = (s16*)stream;
len /= sizeof(s16);

SDL_LockMutex(inst->micLock);
int maxlen = sizeof(micExtBuffer) / sizeof(s16);

if ((inst->micExtBufferCount + len) > maxlen)
len = maxlen - inst->micExtBufferCount;

if ((inst->micExtBufferWritePos + len) > maxlen)
{
u32 len1 = maxlen - inst->micExtBufferWritePos;
Expand All @@ -121,6 +125,9 @@ void EmuInstance::micCallback(void* data, Uint8* stream, int len)
memcpy(&inst->micExtBuffer[inst->micExtBufferWritePos], input, len*sizeof(s16));
inst->micExtBufferWritePos += len;
}

inst->micExtBufferCount += len;
SDL_UnlockMutex(inst->micLock);
}

void EmuInstance::audioMute()
Expand Down Expand Up @@ -270,6 +277,8 @@ void EmuInstance::micLoadWav(const std::string& name)

void EmuInstance::micProcess()
{
SDL_LockMutex(micLock);

int type = micInputType;
bool cmd = hotkeyDown(HK_Mic);

Expand All @@ -278,6 +287,8 @@ void EmuInstance::micProcess()
type = micInputType_Silence;
}

const int kFrameLen = 735;

switch (type)
{
case micInputType_Silence: // no mic
Expand All @@ -289,21 +300,35 @@ void EmuInstance::micProcess()
case micInputType_Wav: // WAV
if (micBuffer)
{
if ((micBufferReadPos + 735) > micBufferLength)
int len = kFrameLen;
if (micExtBufferCount < len)
len = micExtBufferCount;

s16 tmp[kFrameLen];

if ((micBufferReadPos + len) > micBufferLength)
{
s16 tmp[735];
u32 len1 = micBufferLength - micBufferReadPos;
memcpy(&tmp[0], &micBuffer[micBufferReadPos], len1*sizeof(s16));
memcpy(&tmp[len1], &micBuffer[0], (735 - len1)*sizeof(s16));
u32 part1 = micBufferLength - micBufferReadPos;
memcpy(&tmp[0], &micBuffer[micBufferReadPos], part1*sizeof(s16));
memcpy(&tmp[part1], &micBuffer[0], (len - part1)*sizeof(s16));

nds->MicInputFrame(tmp, 735);
micBufferReadPos = 735 - len1;
micBufferReadPos = len - part1;
}
else
{
nds->MicInputFrame(&micBuffer[micBufferReadPos], 735);
micBufferReadPos += 735;
memcpy(&tmp[0], &micBuffer[micBufferReadPos], len*sizeof(s16));

micBufferReadPos += len;
}

if (len < kFrameLen)
{
for (int i = len; i < kFrameLen; i++)
tmp[i] = tmp[len-1];
}
nds->MicInputFrame(tmp, 735);

micExtBufferCount -= len;
}
else
{
Expand All @@ -317,19 +342,21 @@ void EmuInstance::micProcess()
int sample_len = sizeof(mic_blow) / sizeof(u16);
static int sample_pos = 0;

s16 tmp[735];
s16 tmp[kFrameLen];

for (int i = 0; i < 735; i++)
for (int i = 0; i < kFrameLen; i++)
{
tmp[i] = mic_blow[sample_pos] ^ 0x8000;
sample_pos++;
if (sample_pos >= sample_len) sample_pos = 0;
}

nds->MicInputFrame(tmp, 735);
nds->MicInputFrame(tmp, kFrameLen);
}
break;
}

SDL_UnlockMutex(micLock);
}

void EmuInstance::setupMicInputData()
Expand Down Expand Up @@ -402,12 +429,15 @@ void EmuInstance::audioInit()

memset(micExtBuffer, 0, sizeof(micExtBuffer));
micExtBufferWritePos = 0;
micExtBufferCount = 0;
micWavBuffer = nullptr;

micBuffer = nullptr;
micBufferLength = 0;
micBufferReadPos = 0;

micLock = SDL_CreateMutex();

setupMicInputData();
}

Expand All @@ -425,6 +455,9 @@ void EmuInstance::audioDeInit()

if (micWavBuffer) delete[] micWavBuffer;
micWavBuffer = nullptr;

if (micLock) SDL_DestroyMutex(micLock);
micLock = nullptr;
}

void EmuInstance::audioSync()
Expand Down

0 comments on commit 58ee191

Please sign in to comment.