make mic input less shitty (and less dangerous)

This commit is contained in:
Arisotura 2024-11-01 01:31:45 +01:00
parent 78aae252d5
commit 58ee191cc8
3 changed files with 50 additions and 15 deletions

View File

@ -140,7 +140,6 @@ EmuInstance::EmuInstance(int inst) : deleting(false),
// if any extra windows were saved as enabled, open them // if any extra windows were saved as enabled, open them
for (int i = 1; i < kMaxWindows; i++) 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"; std::string key = "Window" + std::to_string(i) + ".Enabled";
bool enable = localCfg.GetBool(key); bool enable = localCfg.GetBool(key);
if (enable) if (enable)

View File

@ -298,8 +298,9 @@ private:
int mpAudioMode; int mpAudioMode;
SDL_AudioDeviceID micDevice; SDL_AudioDeviceID micDevice;
melonDS::s16 micExtBuffer[2048]; melonDS::s16 micExtBuffer[4096];
melonDS::u32 micExtBufferWritePos; melonDS::u32 micExtBufferWritePos;
melonDS::u32 micExtBufferCount;
melonDS::u32 micWavLength; melonDS::u32 micWavLength;
melonDS::s16* micWavBuffer; melonDS::s16* micWavBuffer;
@ -308,6 +309,8 @@ private:
melonDS::u32 micBufferLength; melonDS::u32 micBufferLength;
melonDS::u32 micBufferReadPos; melonDS::u32 micBufferReadPos;
SDL_mutex* micLock;
//int audioInterp; //int audioInterp;
int audioVolume; int audioVolume;
bool audioDSiVolumeSync; bool audioDSiVolumeSync;

View File

@ -107,8 +107,12 @@ void EmuInstance::micCallback(void* data, Uint8* stream, int len)
s16* input = (s16*)stream; s16* input = (s16*)stream;
len /= sizeof(s16); len /= sizeof(s16);
SDL_LockMutex(inst->micLock);
int maxlen = sizeof(micExtBuffer) / sizeof(s16); int maxlen = sizeof(micExtBuffer) / sizeof(s16);
if ((inst->micExtBufferCount + len) > maxlen)
len = maxlen - inst->micExtBufferCount;
if ((inst->micExtBufferWritePos + len) > maxlen) if ((inst->micExtBufferWritePos + len) > maxlen)
{ {
u32 len1 = maxlen - inst->micExtBufferWritePos; u32 len1 = maxlen - inst->micExtBufferWritePos;
@ -121,6 +125,9 @@ void EmuInstance::micCallback(void* data, Uint8* stream, int len)
memcpy(&inst->micExtBuffer[inst->micExtBufferWritePos], input, len*sizeof(s16)); memcpy(&inst->micExtBuffer[inst->micExtBufferWritePos], input, len*sizeof(s16));
inst->micExtBufferWritePos += len; inst->micExtBufferWritePos += len;
} }
inst->micExtBufferCount += len;
SDL_UnlockMutex(inst->micLock);
} }
void EmuInstance::audioMute() void EmuInstance::audioMute()
@ -270,6 +277,8 @@ void EmuInstance::micLoadWav(const std::string& name)
void EmuInstance::micProcess() void EmuInstance::micProcess()
{ {
SDL_LockMutex(micLock);
int type = micInputType; int type = micInputType;
bool cmd = hotkeyDown(HK_Mic); bool cmd = hotkeyDown(HK_Mic);
@ -278,6 +287,8 @@ void EmuInstance::micProcess()
type = micInputType_Silence; type = micInputType_Silence;
} }
const int kFrameLen = 735;
switch (type) switch (type)
{ {
case micInputType_Silence: // no mic case micInputType_Silence: // no mic
@ -289,21 +300,35 @@ void EmuInstance::micProcess()
case micInputType_Wav: // WAV case micInputType_Wav: // WAV
if (micBuffer) if (micBuffer)
{ {
if ((micBufferReadPos + 735) > micBufferLength) int len = kFrameLen;
{ if (micExtBufferCount < len)
s16 tmp[735]; len = micExtBufferCount;
u32 len1 = micBufferLength - micBufferReadPos;
memcpy(&tmp[0], &micBuffer[micBufferReadPos], len1*sizeof(s16));
memcpy(&tmp[len1], &micBuffer[0], (735 - len1)*sizeof(s16));
nds->MicInputFrame(tmp, 735); s16 tmp[kFrameLen];
micBufferReadPos = 735 - len1;
if ((micBufferReadPos + len) > micBufferLength)
{
u32 part1 = micBufferLength - micBufferReadPos;
memcpy(&tmp[0], &micBuffer[micBufferReadPos], part1*sizeof(s16));
memcpy(&tmp[part1], &micBuffer[0], (len - part1)*sizeof(s16));
micBufferReadPos = len - part1;
} }
else else
{ {
nds->MicInputFrame(&micBuffer[micBufferReadPos], 735); memcpy(&tmp[0], &micBuffer[micBufferReadPos], len*sizeof(s16));
micBufferReadPos += 735;
micBufferReadPos += len;
} }
if (len < kFrameLen)
{
for (int i = len; i < kFrameLen; i++)
tmp[i] = tmp[len-1];
}
nds->MicInputFrame(tmp, 735);
micExtBufferCount -= len;
} }
else else
{ {
@ -317,19 +342,21 @@ void EmuInstance::micProcess()
int sample_len = sizeof(mic_blow) / sizeof(u16); int sample_len = sizeof(mic_blow) / sizeof(u16);
static int sample_pos = 0; 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; tmp[i] = mic_blow[sample_pos] ^ 0x8000;
sample_pos++; sample_pos++;
if (sample_pos >= sample_len) sample_pos = 0; if (sample_pos >= sample_len) sample_pos = 0;
} }
nds->MicInputFrame(tmp, 735); nds->MicInputFrame(tmp, kFrameLen);
} }
break; break;
} }
SDL_UnlockMutex(micLock);
} }
void EmuInstance::setupMicInputData() void EmuInstance::setupMicInputData()
@ -402,12 +429,15 @@ void EmuInstance::audioInit()
memset(micExtBuffer, 0, sizeof(micExtBuffer)); memset(micExtBuffer, 0, sizeof(micExtBuffer));
micExtBufferWritePos = 0; micExtBufferWritePos = 0;
micExtBufferCount = 0;
micWavBuffer = nullptr; micWavBuffer = nullptr;
micBuffer = nullptr; micBuffer = nullptr;
micBufferLength = 0; micBufferLength = 0;
micBufferReadPos = 0; micBufferReadPos = 0;
micLock = SDL_CreateMutex();
setupMicInputData(); setupMicInputData();
} }
@ -425,6 +455,9 @@ void EmuInstance::audioDeInit()
if (micWavBuffer) delete[] micWavBuffer; if (micWavBuffer) delete[] micWavBuffer;
micWavBuffer = nullptr; micWavBuffer = nullptr;
if (micLock) SDL_DestroyMutex(micLock);
micLock = nullptr;
} }
void EmuInstance::audioSync() void EmuInstance::audioSync()