ignore complete radio silence > 5 seconds

This commit is contained in:
BotoX 2021-05-09 18:37:35 +02:00
parent 36a3225a2b
commit 39fb1f70f8
3 changed files with 17 additions and 7 deletions

View File

@ -56,13 +56,13 @@ public:
m_Samples += numSamples;
}
void PadSilence(uint32_t numSamples)
void PadSilence(uint64_t numSamples)
{
if(!m_Enc || m_Samples >= numSamples)
return;
static const int16_t silence[128] = {0};
uint32_t pad = numSamples - m_Samples;
uint64_t pad = numSamples - m_Samples;
while(pad > 0)
{
const int samples = MIN(sizeof(silence) / bytesPerSample, pad);
@ -75,7 +75,7 @@ public:
private:
OggOpusComments *m_Comments = nullptr;
OggOpusEnc *m_Enc = nullptr;
uint32_t m_Samples = 0;
uint64_t m_Samples = 0;
static const uint32_t bytesPerSample = 2;
};

View File

@ -154,7 +154,7 @@ void VoiceDataWriter::Finish()
state.second.celt_decoder.Destroy();
state.second.silk_decoder.Destroy();
state.second.fileWriter.PadSilence((m_curTick * state.second.sampleRate) / context->fTickRate);
state.second.fileWriter.PadSilence(((uint64_t)(m_curTick - m_silenceTicks) * state.second.sampleRate) / context->fTickRate);
state.second.fileWriter.Close();
state.second.lastVoiceDataTick = -1;
}
@ -168,6 +168,7 @@ void VoiceDataWriter::Finish()
void VoiceDataWriter::StartCommandPacket(const CommandPacket& packet)
{
m_lastTick = m_curTick;
m_curTick = packet.tick;
}
@ -177,10 +178,14 @@ void VoiceDataWriter::EndCommandPacket(const PacketTrailingBits& trailingBits)
if (m_curTick <= tickMargin)
return;
// Skip silence if noone talks for at least 5 seconds
if((m_curTick - m_lastVoiceTick) / context->fTickRate > 5.0)
m_silenceTicks += (m_curTick - m_lastTick);
for(auto& state : m_playerVoiceStates)
{
if((m_curTick - state.second.lastVoiceDataTick) > tickMargin)
state.second.fileWriter.PadSilence((m_curTick * state.second.sampleRate) / context->fTickRate);
state.second.fileWriter.PadSilence(((uint64_t)(m_curTick - m_silenceTicks) * state.second.sampleRate) / context->fTickRate);
}
}
@ -361,7 +366,7 @@ void VoiceDataWriter::OnNetPacket(NetPacket& packet)
{
std::string name = std::string(m_outputPath) + "/" + std::string(guid) + ".opus";
state.fileWriter.Init(name.c_str(), state.sampleRate);
state.fileWriter.PadSilence((m_curTick * state.sampleRate) / context->fTickRate);
state.fileWriter.PadSilence(((uint64_t)(m_curTick - m_silenceTicks) * state.sampleRate) / context->fTickRate);
}
state.fileWriter.WriteSamples(m_decodeBuffer, numDecompressedSamples);
@ -369,5 +374,6 @@ void VoiceDataWriter::OnNetPacket(NetPacket& packet)
context->logic->OnClientVoiceChat(voiceData->fromClientIndex, (float)numDecompressedSamples / (float)state.sampleRate);
state.lastVoiceDataTick = m_curTick;
m_lastVoiceTick = m_curTick;
}
}

View File

@ -81,7 +81,11 @@ private:
CELTMode* m_celtMode = nullptr;
std::map<std::string, PlayerVoiceState> m_playerVoiceStates;
int32_t m_curTick = -1;
int32_t m_curTick = 0;
int32_t m_lastTick = 0;
int32_t m_lastVoiceTick = 0;
int32_t m_silenceTicks = 0;
const char* m_outputPath = nullptr;
int16_t m_decodeBuffer[8192];