summaryrefslogtreecommitdiff
path: root/soundsystem/snd_wave_mixer.cpp
blob: 556e65c9c48ad476626a611de6ed8a8cbff40c7c (plain) (blame)
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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: 
//
// $Workfile:     $
// $Date:         $
// $NoKeywords: $
//===========================================================================//
#include <stdio.h>

#include "snd_dev_wave.h"
#include "snd_wave_source.h"
#include "soundsystem/snd_audio_source.h"
#include "snd_wave_mixer_private.h"
#include "snd_wave_mixer_adpcm.h"
#include "tier2/riff.h"

//-----------------------------------------------------------------------------
// These mixers provide an abstraction layer between the audio device and 
// mixing/decoding code.  They allow data to be decoded and mixed using 
// optimized, format sensitive code by calling back into the device that
// controls them.
//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------
// Purpose: maps mixing to 8-bit mono mixer
//-----------------------------------------------------------------------------
class CAudioMixerWave8Mono : public CAudioMixerWave
{
public:
	CAudioMixerWave8Mono( CWaveData *data ) : CAudioMixerWave( data ) {}
	virtual void Mix( IAudioDevice *pDevice, channel_t *pChannel, void *pData, int outputOffset, int inputOffset, fixedint fracRate, int outCount, int timecompress, bool forward = true )
	{
		pDevice->Mix8Mono( pChannel, (char *)pData, outputOffset, inputOffset, fracRate, outCount, timecompress, forward );
	}
};

//-----------------------------------------------------------------------------
// Purpose: maps mixing to 8-bit stereo mixer
//-----------------------------------------------------------------------------
class CAudioMixerWave8Stereo : public CAudioMixerWave
{
public:
	CAudioMixerWave8Stereo( CWaveData *data ) : CAudioMixerWave( data ) {}
	virtual void Mix( IAudioDevice *pDevice, channel_t *pChannel, void *pData, int outputOffset, int inputOffset, fixedint fracRate, int outCount, int timecompress, bool forward = true )
	{
		pDevice->Mix8Stereo( pChannel, (char *)pData, outputOffset, inputOffset, fracRate, outCount, timecompress, forward );
	}
};

//-----------------------------------------------------------------------------
// Purpose: maps mixing to 16-bit mono mixer
//-----------------------------------------------------------------------------
class CAudioMixerWave16Mono : public CAudioMixerWave
{
public:
	CAudioMixerWave16Mono( CWaveData *data ) : CAudioMixerWave( data ) {}
	virtual void Mix( IAudioDevice *pDevice, channel_t *pChannel, void *pData, int outputOffset, int inputOffset, fixedint fracRate, int outCount, int timecompress, bool forward = true )
	{
		pDevice->Mix16Mono( pChannel, (short *)pData, outputOffset, inputOffset, fracRate, outCount, timecompress, forward );
	}
};


//-----------------------------------------------------------------------------
// Purpose: maps mixing to 16-bit stereo mixer
//-----------------------------------------------------------------------------
class CAudioMixerWave16Stereo : public CAudioMixerWave
{
public:
	CAudioMixerWave16Stereo( CWaveData *data ) : CAudioMixerWave( data ) {}
	virtual void Mix( IAudioDevice *pDevice, channel_t *pChannel, void *pData, int outputOffset, int inputOffset, fixedint fracRate, int outCount, int timecompress, bool forward = true )
	{
		pDevice->Mix16Stereo( pChannel, (short *)pData, outputOffset, inputOffset, fracRate, outCount, timecompress, forward );
	}
};



//-----------------------------------------------------------------------------
// Purpose: Create an approprite mixer type given the data format
// Input  : *data - data access abstraction
//			format - pcm or adpcm (1 or 2 -- RIFF format)
//			channels - number of audio channels (1 = mono, 2 = stereo)
//			bits - bits per sample
// Output : CAudioMixer * abstract mixer type that maps mixing to appropriate code
//-----------------------------------------------------------------------------
CAudioMixer *CreateWaveMixer( CWaveData *data, int format, int channels, int bits )
{
	if ( format == WAVE_FORMAT_PCM )
	{
		if ( channels > 1 )
		{
			if ( bits == 8 )
				return new CAudioMixerWave8Stereo( data );
			else
				return new CAudioMixerWave16Stereo( data );
		}
		else
		{
			if ( bits == 8 )
				return new CAudioMixerWave8Mono( data );
			else
				return new CAudioMixerWave16Mono( data );
		}
	}
	else if ( format == WAVE_FORMAT_ADPCM )
	{
		return CreateADPCMMixer( data );
	}
	return NULL;
}


//-----------------------------------------------------------------------------
// Purpose: Init the base WAVE mixer.
// Input  : *data - data access object
//-----------------------------------------------------------------------------
CAudioMixerWave::CAudioMixerWave( CWaveData *data ) : m_pData(data), m_pChannel(NULL)
{
	m_loop = 0;
	m_sample = 0;
	m_absoluteSample = 0;
	m_scubSample = -1;
	m_fracOffset = 0;
	m_bActive = false;
	m_nModelIndex = -1;
	m_bForward = true;
	m_bAutoDelete = true;
	m_pChannel = new channel_t;
	m_pChannel->leftvol   = 127;
	m_pChannel->rightvol  = 127;
	m_pChannel->pitch		= 1.0;
}

//-----------------------------------------------------------------------------
// Purpose: Frees the data access object (we own it after construction)
//-----------------------------------------------------------------------------
CAudioMixerWave::~CAudioMixerWave( void )
{
	delete m_pData;
	delete m_pChannel;
}


//-----------------------------------------------------------------------------
// Purpose: Decode and read the data
//			by default we just pass the request on to the data access object
//			other mixers may need to buffer or decode the data for some reason
//
// Input  : **pData - dest pointer
//			sampleCount - number of samples needed
// Output : number of samples available in this batch
//-----------------------------------------------------------------------------
int	CAudioMixerWave::GetOutputData( void **pData, int samplePosition, int sampleCount, bool forward /*= true*/ )
{
	if ( samplePosition != m_sample )
	{
		// Seek
		m_sample = samplePosition;
		m_absoluteSample = samplePosition;
	}

	return m_pData->ReadSourceData( pData, m_sample, sampleCount, forward );
}


//-----------------------------------------------------------------------------
// Purpose: calls through the wavedata to get the audio source
// Output : CAudioSource
//-----------------------------------------------------------------------------
CAudioSource *CAudioMixerWave::GetSource( void )
{
	if ( m_pData )
		return &m_pData->Source();

	return NULL;
}

//-----------------------------------------------------------------------------
// Purpose: Gets the current sample location in playback
// Output : int (samples from start of wave)
//-----------------------------------------------------------------------------
int CAudioMixerWave::GetSamplePosition( void )
{
	return m_sample;
}


//-----------------------------------------------------------------------------
// Purpose: Gets the current sample location in playback
// Output : int (samples from start of wave)
//-----------------------------------------------------------------------------
int CAudioMixerWave::GetScubPosition( void )
{
	if (m_scubSample != -1)
	{
		return m_scubSample;
	}
	return m_sample;
}

//-----------------------------------------------------------------------------
// Purpose: 
// Input  : position - 
//-----------------------------------------------------------------------------
bool CAudioMixerWave::SetSamplePosition( int position, bool scrubbing )
{
	position = max( 0, position );

	m_sample = position;
	m_absoluteSample = position;
	m_startpos = m_sample;
	if (scrubbing)
	{
		m_scubSample = position;
	}
	else
	{
		m_scubSample = -1;
	}
		
	return true;
}

//-----------------------------------------------------------------------------
// Purpose: 
// Input  : position - 
//-----------------------------------------------------------------------------
void CAudioMixerWave::SetLoopPosition( int position )
{
	m_loop = position;
}

//-----------------------------------------------------------------------------
// Purpose: 
// Output : int
//-----------------------------------------------------------------------------
int CAudioMixerWave::GetStartPosition( void )
{
	return m_startpos;
}

bool CAudioMixerWave::GetActive( void )
{
	return m_bActive;
}

void CAudioMixerWave::SetActive( bool active )
{
	m_bActive = active;
}

void CAudioMixerWave::SetModelIndex( int index )
{
	m_nModelIndex = index;
}

int CAudioMixerWave::GetModelIndex( void ) const
{
	return m_nModelIndex;
}

void CAudioMixerWave::SetDirection( bool forward )
{
	m_bForward = forward;
}

bool CAudioMixerWave::GetDirection( void ) const
{
	return m_bForward;
}

void CAudioMixerWave::SetAutoDelete( bool autodelete )
{
	m_bAutoDelete = autodelete;
}

bool CAudioMixerWave::GetAutoDelete( void ) const
{
	return m_bAutoDelete;
}

void CAudioMixerWave::SetVolume( float volume )
{
	int ivolume = (int)( clamp( volume, 0.0f, 1.0f ) * 127.0f );

	m_pChannel->leftvol   = ivolume;
	m_pChannel->rightvol  = ivolume;
}

channel_t *CAudioMixerWave::GetChannel()
{
	Assert( m_pChannel );
	return m_pChannel;
}

//-----------------------------------------------------------------------------
// Purpose: 
// Input  : *pChannel - 
//			sampleCount - 
//			outputRate - 
//-----------------------------------------------------------------------------
void CAudioMixerWave::IncrementSamples( channel_t *pChannel, int startSample, int sampleCount,int outputRate, bool forward /*= true*/ )
{
	int inputSampleRate = (int)(pChannel->pitch * m_pData->Source().SampleRate());
	float rate = (float)inputSampleRate / outputRate;

	int startpos = startSample;

	if ( !forward )
	{
		int requestedstart = startSample - (int)( sampleCount * rate );
		if ( requestedstart < 0 )
			return;

		startpos = max( 0, requestedstart );
		SetSamplePosition( startpos );
	}

	while ( sampleCount > 0 )
	{
		int inputSampleCount;
		int outputSampleCount = sampleCount;
		
		if ( outputRate != inputSampleRate )
		{
			inputSampleCount = (int)(sampleCount * rate);
		}
		else
		{
			inputSampleCount = sampleCount;
		}
		
		sampleCount -= outputSampleCount;
		if ( forward )
		{
			m_sample += inputSampleCount;
			m_absoluteSample += inputSampleCount;
		}
	}
}

//-----------------------------------------------------------------------------
// Purpose: The device calls this to request data.  The mixer must provide the
//			full amount of samples or have silence in its output stream.
// Input  : *pDevice - requesting device
//			sampleCount - number of samples at the output rate
//			outputRate - sampling rate of the request
// Output : Returns true to keep mixing, false to delete this mixer
//-----------------------------------------------------------------------------
bool CAudioMixerWave::SkipSamples( IAudioDevice *pDevice, channel_t *pChannel, 
	int startSample, int sampleCount, int outputRate, bool forward /*= true*/ )
{
	int offset = 0;

	int inputSampleRate = (int)(pChannel->pitch * m_pData->Source().SampleRate());
	float rate = (float)inputSampleRate / outputRate;

	sampleCount = min( sampleCount, pDevice->PaintBufferSampleCount() );

	int startpos = startSample;

	if ( !forward )
	{
		int requestedstart = startSample - (int)( sampleCount * rate );
		if ( requestedstart < 0 )
			return false;

		startpos = max( 0, requestedstart );
		SetSamplePosition( startpos );
	}

	while ( sampleCount > 0 )
	{
		int availableSamples;
		int inputSampleCount;
		char *pData = NULL;
		int outputSampleCount = sampleCount;
		
		if ( outputRate != inputSampleRate )
		{
			inputSampleCount = (int)(sampleCount * rate);
			if ( !forward )
			{
				startSample = max( 0, startSample - inputSampleCount );
			}
			int availableSamples = GetOutputData( (void **)&pData, startSample, inputSampleCount, forward );
			if ( !availableSamples )
				break;

			if ( availableSamples < inputSampleCount )
			{
				outputSampleCount = (int)(availableSamples / rate);
				inputSampleCount = availableSamples;
			}

			// compute new fraction part of sample index
			float offset = (m_fracOffset / FIX_SCALE) + (rate * outputSampleCount);
			offset = offset - (float)((int)offset);
			m_fracOffset = FIX_FLOAT(offset);
		}
		else
		{
			if ( !forward )
			{
				startSample = max( 0, startSample - sampleCount );
			}
			availableSamples = GetOutputData( (void **)&pData, startSample, sampleCount, forward );
			if ( !availableSamples )
				break;
			outputSampleCount = availableSamples;
			inputSampleCount = availableSamples;

		}
		offset += outputSampleCount;
		sampleCount -= outputSampleCount;
		if ( forward )
		{
			m_sample += inputSampleCount;
			m_absoluteSample += inputSampleCount;
		}

		if ( m_loop != 0 && m_sample >= m_loop )
		{
			SetSamplePosition( m_startpos );
		}

	}

	if ( sampleCount > 0 )
		return false;

	return true;
}

//-----------------------------------------------------------------------------
// Purpose: The device calls this to request data.  The mixer must provide the
//			full amount of samples or have silence in its output stream.
// Input  : *pDevice - requesting device
//			sampleCount - number of samples at the output rate
//			outputRate - sampling rate of the request
// Output : Returns true to keep mixing, false to delete this mixer
//-----------------------------------------------------------------------------
bool CAudioMixerWave::MixDataToDevice( IAudioDevice *pDevice, channel_t *pChannel, int startSample, int sampleCount, int outputRate, bool forward /*= true*/ )
{
	int offset = 0;

	int inputSampleRate = (int)(pChannel->pitch * m_pData->Source().SampleRate());
	float rate = (float)inputSampleRate / outputRate;
	fixedint fracstep = FIX_FLOAT( rate );

	sampleCount = min( sampleCount, pDevice->PaintBufferSampleCount() );

	int startpos = startSample;

	if ( !forward )
	{
		int requestedstart = startSample - (int)( sampleCount * rate );
		if ( requestedstart < 0 )
			return false;

		startpos = max( 0, requestedstart );
		SetSamplePosition( startpos );
	}

	while ( sampleCount > 0 )
	{
		int availableSamples;
		int inputSampleCount;
		char *pData = NULL;
		int outputSampleCount = sampleCount;
		
		
		if ( outputRate != inputSampleRate )
		{
			inputSampleCount = (int)(sampleCount * rate);

			int availableSamples = GetOutputData( (void **)&pData, startpos, inputSampleCount, forward );
			if ( !availableSamples )
				break;

			if ( availableSamples < inputSampleCount )
			{
				outputSampleCount = (int)(availableSamples / rate);
				inputSampleCount = availableSamples;
			}

			Mix( pDevice, pChannel, pData, offset, m_fracOffset, fracstep, outputSampleCount, 0, forward );
			
			// compute new fraction part of sample index
			float offset = (m_fracOffset / FIX_SCALE) + (rate * outputSampleCount);
			offset = offset - (float)((int)offset);
			m_fracOffset = FIX_FLOAT(offset);
		}
		else
		{
			availableSamples = GetOutputData( (void **)&pData, startpos, sampleCount, forward );
			if ( !availableSamples )
				break;

			outputSampleCount = availableSamples;
			inputSampleCount = availableSamples;

			Mix( pDevice, pChannel, pData, offset, m_fracOffset, FIX(1), outputSampleCount, 0, forward );
		}
		offset += outputSampleCount;
		sampleCount -= outputSampleCount;

		if ( forward )
		{
			m_sample += inputSampleCount;
			m_absoluteSample += inputSampleCount;
		}

		if ( m_loop != 0 && m_sample >= m_loop )
		{
			SetSamplePosition( m_startpos );
		}

	}

	if ( sampleCount > 0 )
		return false;

	return true;
}