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

View File

@ -298,8 +298,9 @@ private:
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;
@ -308,6 +309,8 @@ private:
melonDS::u32 micBufferLength;
melonDS::u32 micBufferReadPos;
SDL_mutex* micLock;
//int audioInterp;
int audioVolume;
bool audioDSiVolumeSync;

View File

@ -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;
@ -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()
@ -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);
@ -278,6 +287,8 @@ void EmuInstance::micProcess()
type = micInputType_Silence;
}
const int kFrameLen = 735;
switch (type)
{
case micInputType_Silence: // no mic
@ -289,21 +300,35 @@ void EmuInstance::micProcess()
case micInputType_Wav: // WAV
if (micBuffer)
{
if ((micBufferReadPos + 735) > 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));
int len = kFrameLen;
if (micExtBufferCount < len)
len = micExtBufferCount;
nds->MicInputFrame(tmp, 735);
micBufferReadPos = 735 - len1;
s16 tmp[kFrameLen];
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
{
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
{
@ -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()
@ -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();
}
@ -425,6 +455,9 @@ void EmuInstance::audioDeInit()
if (micWavBuffer) delete[] micWavBuffer;
micWavBuffer = nullptr;
if (micLock) SDL_DestroyMutex(micLock);
micLock = nullptr;
}
void EmuInstance::audioSync()