2022-08-16 08:45:56 +02:00
|
|
|
/*
|
|
|
|
voice.c - voice chat implementation
|
|
|
|
Copyright (C) 2022 Velaron
|
|
|
|
Copyright (C) 2022 SNMetamorph
|
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
*/
|
|
|
|
|
2021-05-09 15:32:53 +02:00
|
|
|
#include "voice.h"
|
|
|
|
|
|
|
|
wavdata_t *input_file;
|
|
|
|
fs_offset_t input_pos;
|
|
|
|
|
|
|
|
voice_state_t voice;
|
|
|
|
|
2022-08-16 08:48:56 +02:00
|
|
|
CVAR_DEFINE_AUTO( voice_enable, "1", FCVAR_PRIVILEGED|FCVAR_ARCHIVE, "enable voice chat" );
|
|
|
|
CVAR_DEFINE_AUTO( voice_loopback, "0", FCVAR_PRIVILEGED, "loopback voice back to the speaker" );
|
|
|
|
CVAR_DEFINE_AUTO( voice_scale, "1.0", FCVAR_PRIVILEGED|FCVAR_ARCHIVE, "incoming voice volume scale" );
|
|
|
|
CVAR_DEFINE_AUTO( voice_avggain, "0.5", FCVAR_PRIVILEGED|FCVAR_ARCHIVE, "automatic voice gain control (average)" );
|
|
|
|
CVAR_DEFINE_AUTO( voice_maxgain, "5.0", FCVAR_PRIVILEGED|FCVAR_ARCHIVE, "automatic voice gain control (maximum)" );
|
|
|
|
CVAR_DEFINE_AUTO( voice_inputfromfile, "0", FCVAR_PRIVILEGED, "input voice from voice_input.wav" );
|
2021-05-09 15:32:53 +02:00
|
|
|
|
2022-08-15 09:24:56 +02:00
|
|
|
static const char* Voice_GetBandwidthTypeName( int bandwidthType )
|
|
|
|
{
|
|
|
|
switch( bandwidthType )
|
|
|
|
{
|
|
|
|
case OPUS_BANDWIDTH_FULLBAND: return "Full Band (20 kHz)";
|
|
|
|
case OPUS_BANDWIDTH_SUPERWIDEBAND: return "Super Wide Band (12 kHz)";
|
|
|
|
case OPUS_BANDWIDTH_WIDEBAND: return "Wide Band (8 kHz)";
|
|
|
|
case OPUS_BANDWIDTH_MEDIUMBAND: return "Medium Band (6 kHz)";
|
|
|
|
case OPUS_BANDWIDTH_NARROWBAND: return "Narrow Band (4 kHz)";
|
|
|
|
default: return "Unknown";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void Voice_CodecInfo_f( void )
|
|
|
|
{
|
|
|
|
int encoderComplexity;
|
|
|
|
opus_int32 encoderBitrate;
|
|
|
|
opus_int32 encoderBandwidthType;
|
|
|
|
|
|
|
|
opus_encoder_ctl( voice.encoder, OPUS_GET_BITRATE( &encoderBitrate ));
|
|
|
|
opus_encoder_ctl( voice.encoder, OPUS_GET_COMPLEXITY( &encoderComplexity ));
|
|
|
|
opus_encoder_ctl( voice.encoder, OPUS_GET_BANDWIDTH( &encoderBandwidthType ));
|
|
|
|
|
|
|
|
Con_Printf( "Encoder:\n" );
|
2022-08-16 17:29:31 +02:00
|
|
|
Con_Printf( " Bitrate: %.3f kbps\n", encoderBitrate / 1000.0f );
|
2022-08-15 09:24:56 +02:00
|
|
|
Con_Printf( " Complexity: %d\n", encoderComplexity );
|
|
|
|
Con_Printf( " Bandwidth: " );
|
|
|
|
Con_Printf( Voice_GetBandwidthTypeName( encoderBandwidthType ));
|
|
|
|
Con_Printf( "\n" );
|
|
|
|
}
|
|
|
|
|
2021-05-09 15:32:53 +02:00
|
|
|
void Voice_RegisterCvars( void )
|
|
|
|
{
|
|
|
|
Cvar_RegisterVariable( &voice_enable );
|
|
|
|
Cvar_RegisterVariable( &voice_loopback );
|
|
|
|
Cvar_RegisterVariable( &voice_scale );
|
2022-08-15 22:23:33 +02:00
|
|
|
Cvar_RegisterVariable( &voice_avggain );
|
|
|
|
Cvar_RegisterVariable( &voice_maxgain );
|
2021-05-09 15:32:53 +02:00
|
|
|
Cvar_RegisterVariable( &voice_inputfromfile );
|
2022-08-15 09:24:56 +02:00
|
|
|
Cmd_AddClientCommand( "voice_codecinfo", Voice_CodecInfo_f );
|
2021-05-09 15:32:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void Voice_Status( int entindex, qboolean bTalking )
|
|
|
|
{
|
|
|
|
clgame.dllFuncs.pfnVoiceStatus( entindex, bTalking );
|
|
|
|
}
|
|
|
|
|
2022-08-15 09:42:06 +02:00
|
|
|
static uint Voice_GetFrameSize( float durationMsec )
|
|
|
|
{
|
|
|
|
return voice.channels * voice.width * (( float )voice.samplerate / ( 1000.0f / durationMsec ));
|
|
|
|
}
|
|
|
|
|
2022-08-15 22:23:33 +02:00
|
|
|
static void Voice_ApplyGainAdjust( opus_int16 *samples, int count )
|
|
|
|
{
|
|
|
|
float gain, modifiedMax;
|
|
|
|
int average, adjustedSample;
|
|
|
|
int blockOffset = 0;
|
|
|
|
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int localMax = 0;
|
|
|
|
int localSum = 0;
|
|
|
|
int blockSize = Q_min( count - ( blockOffset + voice.autogain.block_size ), voice.autogain.block_size );
|
|
|
|
|
|
|
|
if( blockSize < 1 )
|
|
|
|
break;
|
|
|
|
|
|
|
|
for( i = 0; i < blockSize; ++i )
|
|
|
|
{
|
|
|
|
int sample = samples[blockOffset + i];
|
|
|
|
if( abs( sample ) > localMax ) {
|
|
|
|
localMax = abs( sample );
|
|
|
|
}
|
|
|
|
localSum += sample;
|
|
|
|
|
|
|
|
gain = voice.autogain.current_gain + i * voice.autogain.gain_multiplier;
|
|
|
|
adjustedSample = Q_min( 32767, Q_max(( int )( sample * gain ), -32768 ));
|
|
|
|
samples[blockOffset + i] = adjustedSample;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( blockOffset % voice.autogain.block_size == 0 )
|
|
|
|
{
|
|
|
|
average = localSum / blockSize;
|
|
|
|
modifiedMax = average + ( localMax - average ) * voice_avggain.value;
|
|
|
|
voice.autogain.current_gain = voice.autogain.next_gain * voice_scale.value;
|
|
|
|
voice.autogain.next_gain = Q_min( 32767.0f / modifiedMax, voice_maxgain.value ) * voice_scale.value;
|
|
|
|
voice.autogain.gain_multiplier = ( voice.autogain.next_gain - voice.autogain.current_gain ) / ( voice.autogain.block_size - 1 );
|
|
|
|
}
|
|
|
|
blockOffset += blockSize;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-09 15:32:53 +02:00
|
|
|
// parameters currently unused
|
|
|
|
qboolean Voice_Init( const char *pszCodecName, int quality )
|
|
|
|
{
|
|
|
|
int err;
|
|
|
|
|
|
|
|
if ( !voice_enable.value )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
Voice_DeInit();
|
|
|
|
|
2022-08-15 09:42:06 +02:00
|
|
|
voice.initialized = true;
|
2021-05-09 15:32:53 +02:00
|
|
|
voice.channels = 1;
|
|
|
|
voice.width = 2;
|
|
|
|
voice.samplerate = SOUND_48k;
|
2022-08-18 09:07:43 +02:00
|
|
|
voice.frame_size = Voice_GetFrameSize( 40.0f );
|
2022-08-15 22:23:33 +02:00
|
|
|
voice.autogain.block_size = 128;
|
2021-05-09 15:32:53 +02:00
|
|
|
|
|
|
|
if ( !VoiceCapture_Init() )
|
|
|
|
{
|
|
|
|
Voice_DeInit();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
voice.encoder = opus_encoder_create( voice.samplerate, voice.channels, OPUS_APPLICATION_VOIP, &err );
|
2022-08-15 09:40:55 +02:00
|
|
|
|
|
|
|
if( err != OPUS_OK )
|
|
|
|
return false;
|
|
|
|
|
2021-05-09 15:32:53 +02:00
|
|
|
voice.decoder = opus_decoder_create( voice.samplerate, voice.channels, &err );
|
|
|
|
|
2022-08-15 09:40:55 +02:00
|
|
|
switch( quality )
|
|
|
|
{
|
|
|
|
case 1: // 4800 bits per second, <4 kHz bandwidth
|
|
|
|
opus_encoder_ctl( voice.encoder, OPUS_SET_BITRATE( 4800 ));
|
|
|
|
opus_encoder_ctl( voice.encoder, OPUS_SET_BANDWIDTH( OPUS_BANDWIDTH_NARROWBAND ));
|
|
|
|
break;
|
|
|
|
case 2: // 12000 bits per second, <6 kHz bandwidth
|
|
|
|
opus_encoder_ctl( voice.encoder, OPUS_SET_BITRATE( 12000 ));
|
|
|
|
opus_encoder_ctl( voice.encoder, OPUS_SET_BANDWIDTH( OPUS_BANDWIDTH_MEDIUMBAND ));
|
|
|
|
break;
|
|
|
|
case 4: // automatic bitrate, full band (20 kHz)
|
|
|
|
opus_encoder_ctl( voice.encoder, OPUS_SET_BITRATE( OPUS_AUTO ));
|
|
|
|
opus_encoder_ctl( voice.encoder, OPUS_SET_BANDWIDTH( OPUS_BANDWIDTH_FULLBAND ));
|
|
|
|
break;
|
|
|
|
case 5: // maximum bitrate, full band (20 kHz)
|
|
|
|
opus_encoder_ctl( voice.encoder, OPUS_SET_BITRATE( OPUS_BITRATE_MAX ));
|
|
|
|
opus_encoder_ctl( voice.encoder, OPUS_SET_BANDWIDTH( OPUS_BANDWIDTH_FULLBAND ));
|
|
|
|
break;
|
|
|
|
default: // 36000 bits per second, <12 kHz bandwidth
|
|
|
|
opus_encoder_ctl( voice.encoder, OPUS_SET_BITRATE( 36000 ));
|
|
|
|
opus_encoder_ctl( voice.encoder, OPUS_SET_BANDWIDTH( OPUS_BANDWIDTH_SUPERWIDEBAND ));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return err == OPUS_OK;
|
2021-05-09 15:32:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Voice_DeInit( void )
|
|
|
|
{
|
2022-08-15 09:42:06 +02:00
|
|
|
if ( !voice.initialized )
|
2021-05-09 15:32:53 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
Voice_RecordStop();
|
|
|
|
|
|
|
|
opus_encoder_destroy( voice.encoder );
|
|
|
|
opus_decoder_destroy( voice.decoder );
|
|
|
|
|
2022-08-15 09:42:06 +02:00
|
|
|
voice.initialized = false;
|
2021-05-09 15:32:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
uint Voice_GetCompressedData( byte *out, uint maxsize, uint *frames )
|
|
|
|
{
|
|
|
|
uint ofs, size = 0;
|
|
|
|
|
|
|
|
if ( input_file )
|
|
|
|
{
|
|
|
|
uint numbytes;
|
2022-08-18 11:07:47 +02:00
|
|
|
double updateInterval;
|
2021-05-09 15:32:53 +02:00
|
|
|
|
2022-08-18 11:07:47 +02:00
|
|
|
updateInterval = cl.mtime[0] - cl.mtime[1];
|
|
|
|
numbytes = updateInterval * voice.samplerate * voice.width * voice.channels;
|
2021-05-09 15:32:53 +02:00
|
|
|
numbytes = Q_min( numbytes, input_file->size - input_pos );
|
2022-08-15 09:42:06 +02:00
|
|
|
numbytes = Q_min( numbytes, sizeof( voice.input_buffer ) - voice.input_buffer_pos );
|
2021-05-09 15:32:53 +02:00
|
|
|
|
2022-08-15 09:42:06 +02:00
|
|
|
memcpy( voice.input_buffer + voice.input_buffer_pos, input_file->buffer + input_pos, numbytes );
|
|
|
|
voice.input_buffer_pos += numbytes;
|
2021-05-09 15:32:53 +02:00
|
|
|
input_pos += numbytes;
|
|
|
|
}
|
|
|
|
|
2022-08-15 09:42:06 +02:00
|
|
|
for ( ofs = 0; voice.input_buffer_pos - ofs >= voice.frame_size && ofs <= voice.input_buffer_pos; ofs += voice.frame_size )
|
2021-05-09 15:32:53 +02:00
|
|
|
{
|
|
|
|
int bytes;
|
|
|
|
|
2022-08-18 11:07:47 +02:00
|
|
|
if (!input_file)
|
|
|
|
{
|
|
|
|
// adjust gain before encoding, but only for input from voice
|
|
|
|
Voice_ApplyGainAdjust((opus_int16*)voice.input_buffer + ofs, voice.frame_size);
|
|
|
|
}
|
|
|
|
|
2022-08-15 09:42:06 +02:00
|
|
|
bytes = opus_encode( voice.encoder, (const opus_int16*)(voice.input_buffer + ofs), voice.frame_size / voice.width, out + size, maxsize );
|
|
|
|
memmove( voice.input_buffer, voice.input_buffer + voice.frame_size, sizeof( voice.input_buffer ) - voice.frame_size );
|
|
|
|
voice.input_buffer_pos -= voice.frame_size;
|
2021-05-09 15:32:53 +02:00
|
|
|
|
|
|
|
if ( bytes > 0 )
|
|
|
|
{
|
|
|
|
size += bytes;
|
|
|
|
(*frames)++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Voice_Idle( float frametime )
|
|
|
|
{
|
2022-08-15 10:39:36 +02:00
|
|
|
int i;
|
|
|
|
|
2021-05-09 15:32:53 +02:00
|
|
|
if ( !voice_enable.value )
|
|
|
|
{
|
|
|
|
Voice_DeInit();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( voice.talking_ack )
|
|
|
|
{
|
|
|
|
voice.talking_timeout += frametime;
|
2022-08-15 10:39:36 +02:00
|
|
|
if( voice.talking_timeout > 0.2f )
|
2021-05-09 15:32:53 +02:00
|
|
|
{
|
|
|
|
voice.talking_ack = false;
|
|
|
|
Voice_Status( -2, false );
|
|
|
|
}
|
|
|
|
}
|
2022-08-15 10:39:36 +02:00
|
|
|
|
|
|
|
for ( i = 0; i < 32; i++ )
|
|
|
|
{
|
|
|
|
if ( voice.players_status[i].talking_ack )
|
|
|
|
{
|
|
|
|
voice.players_status[i].talking_timeout += frametime;
|
|
|
|
if ( voice.players_status[i].talking_timeout > 0.2f )
|
|
|
|
{
|
|
|
|
voice.players_status[i].talking_ack = false;
|
|
|
|
if ( i < cl.maxclients )
|
|
|
|
Voice_Status( i, false );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-05-09 15:32:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
qboolean Voice_IsRecording( void )
|
|
|
|
{
|
|
|
|
return voice.is_recording;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Voice_RecordStop( void )
|
|
|
|
{
|
|
|
|
if ( input_file )
|
|
|
|
{
|
|
|
|
FS_FreeSound( input_file );
|
|
|
|
input_file = NULL;
|
|
|
|
}
|
|
|
|
|
2022-08-15 09:42:06 +02:00
|
|
|
voice.input_buffer_pos = 0;
|
|
|
|
memset( voice.input_buffer, 0, sizeof( voice.input_buffer ) );
|
2021-05-09 15:32:53 +02:00
|
|
|
|
|
|
|
if ( Voice_IsRecording() )
|
|
|
|
Voice_Status( -1, false );
|
|
|
|
|
|
|
|
VoiceCapture_RecordStop();
|
|
|
|
|
|
|
|
voice.is_recording = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Voice_RecordStart( void )
|
|
|
|
{
|
|
|
|
Voice_RecordStop();
|
|
|
|
|
|
|
|
if ( voice_inputfromfile.value )
|
|
|
|
{
|
|
|
|
input_file = FS_LoadSound( "voice_input.wav", NULL, 0 );
|
|
|
|
|
|
|
|
if ( input_file )
|
|
|
|
{
|
|
|
|
Sound_Process( &input_file, voice.samplerate, voice.width, SOUND_RESAMPLE );
|
|
|
|
input_pos = 0;
|
|
|
|
|
|
|
|
voice.start_time = Sys_DoubleTime();
|
|
|
|
voice.is_recording = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
FS_FreeSound( input_file );
|
|
|
|
input_file = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( !Voice_IsRecording() )
|
|
|
|
voice.is_recording = VoiceCapture_RecordStart();
|
|
|
|
|
|
|
|
if ( Voice_IsRecording() )
|
|
|
|
Voice_Status( -1, true );
|
|
|
|
}
|
|
|
|
|
2022-08-16 08:39:21 +02:00
|
|
|
void Voice_AddIncomingData( int ent, const byte *data, uint size, uint frames )
|
2021-05-09 15:32:53 +02:00
|
|
|
{
|
2022-08-16 08:39:21 +02:00
|
|
|
int samples = opus_decode( voice.decoder, data, size, (short *)voice.decompress_buffer, voice.frame_size / voice.width * frames, false );
|
2021-05-09 15:32:53 +02:00
|
|
|
|
2022-08-15 09:42:06 +02:00
|
|
|
if ( samples > 0 )
|
|
|
|
Voice_StartChannel( samples, voice.decompress_buffer, ent );
|
2021-05-09 15:32:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CL_AddVoiceToDatagram( void )
|
|
|
|
{
|
|
|
|
uint size, frames = 0;
|
|
|
|
|
|
|
|
if ( cls.state != ca_active || !Voice_IsRecording() )
|
|
|
|
return;
|
|
|
|
|
2022-08-15 09:42:06 +02:00
|
|
|
size = Voice_GetCompressedData( voice.output_buffer, sizeof( voice.output_buffer ), &frames );
|
2021-05-09 15:32:53 +02:00
|
|
|
|
|
|
|
if ( size > 0 && MSG_GetNumBytesLeft( &cls.datagram ) >= size + 32 )
|
|
|
|
{
|
|
|
|
MSG_BeginClientCmd( &cls.datagram, clc_voicedata );
|
|
|
|
MSG_WriteByte( &cls.datagram, Voice_GetLoopback() );
|
|
|
|
MSG_WriteByte( &cls.datagram, frames );
|
|
|
|
MSG_WriteShort( &cls.datagram, size );
|
2022-08-15 09:42:06 +02:00
|
|
|
MSG_WriteBytes( &cls.datagram, voice.output_buffer, size );
|
2021-05-09 15:32:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
qboolean Voice_GetLoopback( void )
|
|
|
|
{
|
|
|
|
return voice_loopback.value;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Voice_LocalPlayerTalkingAck( void )
|
|
|
|
{
|
|
|
|
if ( !voice.talking_ack )
|
|
|
|
{
|
|
|
|
Voice_Status( -2, true );
|
|
|
|
}
|
|
|
|
|
|
|
|
voice.talking_ack = true;
|
|
|
|
voice.talking_timeout = 0.0f;
|
|
|
|
}
|
|
|
|
|
2022-08-15 10:39:36 +02:00
|
|
|
void Voice_PlayerTalkingAck(int playerIndex)
|
|
|
|
{
|
|
|
|
if( !voice.players_status[playerIndex].talking_ack )
|
|
|
|
{
|
|
|
|
Voice_Status( playerIndex, true );
|
|
|
|
}
|
|
|
|
|
|
|
|
voice.players_status[playerIndex].talking_ack = true;
|
|
|
|
voice.players_status[playerIndex].talking_timeout = 0.0f;
|
|
|
|
}
|
|
|
|
|
2021-05-09 15:32:53 +02:00
|
|
|
void Voice_StartChannel( uint samples, byte *data, int entnum )
|
|
|
|
{
|
|
|
|
SND_ForceInitMouth( entnum );
|
|
|
|
Voice_Status( entnum, true );
|
2022-08-15 22:23:33 +02:00
|
|
|
S_RawEntSamples( entnum, samples, voice.samplerate, voice.width, voice.channels, data, 255 );
|
2022-08-15 09:42:06 +02:00
|
|
|
}
|