1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
|
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
// $NoKeywords: $
//=============================================================================//
#include "audio_pch.h"
#include <assert.h>
#include "voice.h"
#include "ivoicecodec.h"
#if defined( _X360 )
#include "xauddefs.h"
#endif
// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"
// ------------------------------------------------------------------------- //
// CAudioSourceVoice.
// This feeds the data from an incoming voice channel (a guy on the server
// who is speaking) into the sound engine.
// ------------------------------------------------------------------------- //
class CAudioSourceVoice : public CAudioSourceWave
{
public:
CAudioSourceVoice(CSfxTable *pSfx, int iEntity);
virtual ~CAudioSourceVoice();
virtual int GetType( void )
{
return AUDIO_SOURCE_VOICE;
}
virtual void GetCacheData( CAudioSourceCachedInfo *info )
{
Assert( 0 );
}
virtual CAudioMixer *CreateMixer( int initialStreamPosition = 0 );
virtual int GetOutputData( void **pData, int samplePosition, int sampleCount, char copyBuf[AUDIOSOURCE_COPYBUF_SIZE] );
virtual int SampleRate( void );
// Sample size is in bytes. It will not be accurate for compressed audio. This is a best estimate.
// The compressed audio mixers understand this, but in general do not assume that SampleSize() * SampleCount() = filesize
// or even that SampleSize() is 100% accurate due to compression.
virtual int SampleSize( void );
// Total number of samples in this source. NOTE: Some sources are infinite (mic input), they should return
// a count equal to one second of audio at their current rate.
virtual int SampleCount( void );
virtual bool IsVoiceSource() {return true;}
virtual bool IsLooped() {return false;}
virtual bool IsStreaming() {return true;}
virtual bool IsStereoWav() {return false;}
virtual int GetCacheStatus() {return AUDIO_IS_LOADED;}
virtual void CacheLoad() {}
virtual void CacheUnload() {}
virtual CSentence *GetSentence() {return NULL;}
virtual int ZeroCrossingBefore( int sample ) {return sample;}
virtual int ZeroCrossingAfter( int sample ) {return sample;}
// mixer's references
virtual void ReferenceAdd( CAudioMixer *pMixer );
virtual void ReferenceRemove( CAudioMixer *pMixer );
// check reference count, return true if nothing is referencing this
virtual bool CanDelete();
virtual void Prefetch() {}
// Nothing, not a cache object...
virtual void CheckAudioSourceCache() {}
private:
class CWaveDataVoice : public IWaveData
{
public:
CWaveDataVoice( CAudioSourceWave &source ) : m_source(source) {}
~CWaveDataVoice( void ) {}
virtual CAudioSource &Source( void )
{
return m_source;
}
// this file is in memory, simply pass along the data request to the source
virtual int ReadSourceData( void **pData, int sampleIndex, int sampleCount, char copyBuf[AUDIOSOURCE_COPYBUF_SIZE] )
{
return m_source.GetOutputData( pData, sampleIndex, sampleCount, copyBuf );
}
virtual bool IsReadyToMix()
{
return true;
}
private:
CAudioSourceWave &m_source; // pointer to source
};
private:
CAudioSourceVoice( const CAudioSourceVoice & );
// Which entity's voice this is for.
int m_iChannel;
// How many mixers are referencing us.
int m_refCount;
};
// ----------------------------------------------------------------------------- //
// Globals.
// ----------------------------------------------------------------------------- //
// The format we sample voice in.
extern WAVEFORMATEX g_VoiceSampleFormat;
class CVoiceSfx : public CSfxTable
{
public:
virtual const char *getname()
{
return "?VoiceSfx";
}
};
static CVoiceSfx g_CVoiceSfx[VOICE_NUM_CHANNELS];
static float g_VoiceOverdriveDuration = 0;
static bool g_bVoiceOverdriveOn = false;
// When voice is on, all other sounds are decreased by this factor.
static ConVar voice_overdrive( "voice_overdrive", "2" );
static ConVar voice_overdrivefadetime( "voice_overdrivefadetime", "0.4" ); // How long it takes to fade in and out of the voice overdrive.
// The sound engine uses this to lower all sound volumes.
// All non-voice sounds are multiplied by this and divided by 256.
int g_SND_VoiceOverdriveInt = 256;
extern int Voice_SamplesPerSec();
extern int Voice_AvgBytesPerSec();
// ----------------------------------------------------------------------------- //
// CAudioSourceVoice implementation.
// ----------------------------------------------------------------------------- //
CAudioSourceVoice::CAudioSourceVoice( CSfxTable *pSfx, int iChannel )
: CAudioSourceWave( pSfx )
{
m_iChannel = iChannel;
m_refCount = 0;
WAVEFORMATEX tmp = g_VoiceSampleFormat;
tmp.nSamplesPerSec = Voice_SamplesPerSec();
tmp.nAvgBytesPerSec = Voice_AvgBytesPerSec();
Init((char*)&tmp, sizeof(tmp));
m_sampleCount = tmp.nSamplesPerSec;
}
CAudioSourceVoice::~CAudioSourceVoice()
{
Voice_OnAudioSourceShutdown( m_iChannel );
}
CAudioMixer *CAudioSourceVoice::CreateMixer( int initialStreamPosition )
{
CWaveDataVoice *pVoice = new CWaveDataVoice(*this);
if(!pVoice)
return NULL;
CAudioMixer *pMixer = CreateWaveMixer( pVoice, WAVE_FORMAT_PCM, 1, BYTES_PER_SAMPLE*8, 0 );
if(!pMixer)
{
delete pVoice;
return NULL;
}
return pMixer;
}
int CAudioSourceVoice::GetOutputData( void **pData, int samplePosition, int sampleCount, char copyBuf[AUDIOSOURCE_COPYBUF_SIZE] )
{
int nSamplesGotten = Voice_GetOutputData(
m_iChannel,
copyBuf,
AUDIOSOURCE_COPYBUF_SIZE,
samplePosition,
sampleCount );
// If there weren't enough bytes in the received data channel, pad it with zeros.
if( nSamplesGotten < sampleCount )
{
memset( ©Buf[nSamplesGotten], 0, (sampleCount - nSamplesGotten) * BYTES_PER_SAMPLE );
nSamplesGotten = sampleCount;
}
*pData = copyBuf;
return nSamplesGotten;
}
int CAudioSourceVoice::SampleRate()
{
return Voice_SamplesPerSec();
}
int CAudioSourceVoice::SampleSize()
{
return BYTES_PER_SAMPLE;
}
int CAudioSourceVoice::SampleCount()
{
return Voice_SamplesPerSec();
}
void CAudioSourceVoice::ReferenceAdd(CAudioMixer *pMixer)
{
m_refCount++;
}
void CAudioSourceVoice::ReferenceRemove(CAudioMixer *pMixer)
{
m_refCount--;
if ( m_refCount <= 0 )
delete this;
}
bool CAudioSourceVoice::CanDelete()
{
return m_refCount == 0;
}
// ----------------------------------------------------------------------------- //
// Interface implementation.
// ----------------------------------------------------------------------------- //
bool VoiceSE_Init()
{
if( !snd_initialized )
return false;
g_SND_VoiceOverdriveInt = 256;
return true;
}
void VoiceSE_Term()
{
// Disable voice ducking.
g_SND_VoiceOverdriveInt = 256;
}
void VoiceSE_Idle(float frametime)
{
g_SND_VoiceOverdriveInt = 256;
if( g_bVoiceOverdriveOn )
{
g_VoiceOverdriveDuration = min( g_VoiceOverdriveDuration+frametime, voice_overdrivefadetime.GetFloat() );
}
else
{
if(g_VoiceOverdriveDuration == 0)
return;
g_VoiceOverdriveDuration = max(g_VoiceOverdriveDuration-frametime, 0.f);
}
float percent = g_VoiceOverdriveDuration / voice_overdrivefadetime.GetFloat();
percent = (float)(-cos(percent * 3.1415926535) * 0.5 + 0.5); // Smooth it out..
float voiceOverdrive = 1 + (voice_overdrive.GetFloat() - 1) * percent;
g_SND_VoiceOverdriveInt = (int)(256 / voiceOverdrive);
}
int VoiceSE_StartChannel(
int iChannel, //! Which channel to start.
int iEntity,
bool bProximity,
int nViewEntityIndex )
{
Assert( iChannel >= 0 && iChannel < VOICE_NUM_CHANNELS );
// Start the sound.
CSfxTable *sfx = &g_CVoiceSfx[iChannel];
sfx->pSource = NULL;
Vector vOrigin(0,0,0);
StartSoundParams_t params;
params.staticsound = false;
params.entchannel = (CHAN_VOICE_BASE+iChannel);
params.pSfx = sfx;
params.origin = vOrigin;
params.fvol = 1.0f;
params.flags = 0;
params.pitch = PITCH_NORM;
if ( bProximity == true )
{
params.bUpdatePositions = true;
params.soundlevel = SNDLVL_TALKING;
params.soundsource = iEntity;
}
else
{
params.soundlevel = SNDLVL_IDLE;
params.soundsource = nViewEntityIndex;
}
return S_StartSound( params );
}
void VoiceSE_EndChannel(
int iChannel, //! Which channel to stop.
int iEntity
)
{
Assert( iChannel >= 0 && iChannel < VOICE_NUM_CHANNELS );
S_StopSound( iEntity, CHAN_VOICE_BASE+iChannel );
// Start the sound.
CSfxTable *sfx = &g_CVoiceSfx[iChannel];
sfx->pSource = NULL;
}
void VoiceSE_StartOverdrive()
{
g_bVoiceOverdriveOn = true;
}
void VoiceSE_EndOverdrive()
{
g_bVoiceOverdriveOn = false;
}
void VoiceSE_InitMouth(int entnum)
{
}
void VoiceSE_CloseMouth(int entnum)
{
}
void VoiceSE_MoveMouth(int entnum, short *pSamples, int nSamples)
{
}
CAudioSource* Voice_SetupAudioSource( int soundsource, int entchannel )
{
int iChannel = entchannel - CHAN_VOICE_BASE;
if( iChannel >= 0 && iChannel < VOICE_NUM_CHANNELS )
{
CSfxTable *sfx = &g_CVoiceSfx[iChannel];
return new CAudioSourceVoice( sfx, iChannel );
}
else
return NULL;
}
|