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
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
|
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
// $NoKeywords: $
//
//=============================================================================//
#include "audio_pch.h"
#include "snd_mp3_source.h"
#include "snd_dma.h"
#include "snd_wave_mixer_mp3.h"
#include "filesystem_engine.h"
#include "utldict.h"
// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"
#ifndef DEDICATED // have to test this because VPC is forcing us to compile this file.
// How many bytes initial data bytes of the mp3 should be saved in the soundcache, in addition to the small amount of
// metadata (playbackrate, etc). This will increase memory usage by
// ( N * number-of-precached-mp3-sounds-in-the-whole-game ) at all times, as the soundcache is held in memory.
//
// Right now we're setting this to zero. The IsReadyToMix() logic at the data layer will delay mixing of the sound until
// it arrives. Setting this to anything above zero, however, will allow the sound to start, so it needs to either be
// enough to cover SND_ASYNC_LOOKAHEAD_SECONDS or none at all.
#define MP3_STARTUP_DATA_SIZE_BYTES 0
CUtlDict< CSentence *, int> g_PhonemeFileSentences;
bool g_bAllPhonemesLoaded;
void PhonemeMP3Shutdown( void )
{
g_PhonemeFileSentences.PurgeAndDeleteElements();
g_bAllPhonemesLoaded = false;
}
void AddPhonemesFromFile( const char *pszFileName )
{
// If all Phonemes are loaded, do not load anymore
if ( g_bAllPhonemesLoaded && g_PhonemeFileSentences.Count() != 0 )
return;
// Empty file name implies stop loading more phonemes
if ( pszFileName == NULL )
{
g_bAllPhonemesLoaded = true;
return;
}
// Load this file
g_bAllPhonemesLoaded = false;
CUtlBuffer buf( 0, 0, CUtlBuffer::TEXT_BUFFER );
if ( g_pFileSystem->ReadFile( pszFileName, "MOD", buf ) )
{
while ( 1 )
{
char token[4096];
buf.GetString( token );
V_FixSlashes( token );
int iIndex = g_PhonemeFileSentences.Find( token );
if ( iIndex != g_PhonemeFileSentences.InvalidIndex() )
{
delete g_PhonemeFileSentences.Element( iIndex );
g_PhonemeFileSentences.Remove( token );
}
CSentence *pSentence = new CSentence;
g_PhonemeFileSentences.Insert( token, pSentence );
buf.GetString( token );
if ( strlen( token ) <= 0 )
break;
if ( !stricmp( token, "{" ) )
{
pSentence->InitFromBuffer( buf );
}
}
}
}
CAudioSourceMP3::CAudioSourceMP3( CSfxTable *pSfx )
{
m_sampleRate = 0;
m_pSfx = pSfx;
m_refCount = 0;
m_dataStart = 0;
int file = g_pSndIO->open( pSfx->GetFileName() );
if ( file != -1 )
{
m_dataSize = g_pSndIO->size( file );
g_pSndIO->close( file );
}
else
{
// No sound cache, the file isn't here, print this so that the relatively deep failure points that are about to
// spew make a little more sense
Warning( "MP3 is completely missing, sound system will be upset to learn of this [ %s ]\n", pSfx->GetFileName() );
m_dataSize = 0;
}
m_nCachedDataSize = 0;
m_bIsPlayOnce = false;
m_bIsSentenceWord = false;
m_bCheckedForPendingSentence = false;
}
CAudioSourceMP3::CAudioSourceMP3( CSfxTable *pSfx, CAudioSourceCachedInfo *info )
{
m_pSfx = pSfx;
m_refCount = 0;
m_sampleRate = info->SampleRate();
m_dataSize = info->DataSize();
m_dataStart = info->DataStart();
m_nCachedDataSize = 0;
m_bIsPlayOnce = false;
m_bCheckedForPendingSentence = false;
CheckAudioSourceCache();
}
CAudioSourceMP3::~CAudioSourceMP3()
{
}
// mixer's references
void CAudioSourceMP3::ReferenceAdd( CAudioMixer * )
{
m_refCount++;
}
void CAudioSourceMP3::ReferenceRemove( CAudioMixer * )
{
m_refCount--;
if ( m_refCount == 0 && IsPlayOnce() )
{
SetPlayOnce( false ); // in case it gets used again
CacheUnload();
}
}
//-----------------------------------------------------------------------------
// Purpose:
// Output : Returns true on success, false on failure.
//-----------------------------------------------------------------------------
bool CAudioSourceMP3::IsAsyncLoad()
{
// If there's a bit of "cached data" then we don't have to lazy/async load (we still async load the remaining data,
// but we run from the cache initially)
return ( m_nCachedDataSize > 0 ) ? false : true;
}
// check reference count, return true if nothing is referencing this
bool CAudioSourceMP3::CanDelete( void )
{
return m_refCount > 0 ? false : true;
}
//-----------------------------------------------------------------------------
// Purpose:
// Output : int
//-----------------------------------------------------------------------------
int CAudioSourceMP3::GetType()
{
return AUDIO_SOURCE_MP3;
}
//-----------------------------------------------------------------------------
void CAudioSourceMP3::SetSentence( CSentence *pSentence )
{
CAudioSourceCachedInfo *info = m_AudioCacheHandle.FastGet();
if ( !info )
return;
if ( info && info->Sentence() )
return;
CSentence *pNewSentence = new CSentence;
pNewSentence->Append( 0.0f, *pSentence );
pNewSentence->MakeRuntimeOnly();
info->SetSentence( pNewSentence );
}
int CAudioSourceMP3::SampleRate()
{
if ( !m_sampleRate )
{
// This should've come from the sound cache. We can avoid sync I/O jank if and only if we've started streaming
// data already for some other reason. (Despite the name, CreateWaveDataMemory is just creating a wrapper class
// that manages access to the wave data cache)
IWaveData *pData = CreateWaveDataMemory( *this );
if ( !pData->IsReadyToMix() && SND_IsInGame() )
{
// If you hit this, you're creating a sound source that isn't in the sound cache, and asking for its sample
// rate before it has streamed enough data in to read it from the underlying file. Your options are:
// - Rebuild sound cache or figure out why this sound wasn't included.
// - Precache this sound at level load so this doesn't happen during gameplay.
// - Somehow call CacheLoad() on this source earlier so it has time to get data into memory so the data
// shows up as IsReadyToMix here, and this crutch won't jank.
Warning( "MP3 initialized with no sound cache, this may cause janking. [ %s ]\n", GetFileName() );
// The code below will still go fine, but the mixer will emit a jank warning that the data wasn't ready and
// do sync I/O
}
CAudioMixerWaveMP3 *pMixer = new CAudioMixerWaveMP3( pData );
m_sampleRate = pMixer->GetStreamOutputRate();
// pData ownership is passed to, and free'd by, pMixer
delete pMixer;
}
return m_sampleRate;
}
void CAudioSourceMP3::GetCacheData( CAudioSourceCachedInfo *info )
{
// Don't want to replicate our cached sample rate back into the new cache, ensure we recompute it.
CAudioMixerWaveMP3 *pTempMixer = new CAudioMixerWaveMP3( CreateWaveDataMemory(*this) );
m_sampleRate = pTempMixer->GetStreamOutputRate();
delete pTempMixer;
AssertMsg( m_sampleRate, "Creating cache with invalid sample rate data" );
if ( !m_sampleRate )
{
Warning( "Failed to find sample rate creating cache data for MP3, cache will be invalid [ %s ]\n", GetFileName() );
}
info->SetSampleRate( m_sampleRate );
info->SetDataStart( 0 );
int file = g_pSndIO->open( m_pSfx->GetFileName() );
if ( !file )
{
Warning( "Failed to find file for building soundcache [ %s ]\n", m_pSfx->GetFileName() );
// Don't re-use old cached value
m_dataSize = 0;
}
else
{
m_dataSize = (int)g_pSndIO->size( file );
}
Assert( m_dataSize > 0 );
// Do we need to actually load any audio data?
#if MP3_STARTUP_DATA_SIZE_BYTES > 0 // We may have defined the startup data to nothingness
if ( info->s_bIsPrecacheSound && m_dataSize > 0 )
{
// Ideally this would mimic the wave startup data code and figure out this calculation:
// int bytesNeeded = m_channels * ( m_bits >> 3 ) * m_rate * SND_ASYNC_LOOKAHEAD_SECONDS;
// (plus header)
int dataSize = min( MP3_STARTUP_DATA_SIZE_BYTES, m_dataSize );
byte *data = new byte[ dataSize ]();
int readSize = g_pSndIO->read( data, dataSize, file );
if ( readSize != dataSize )
{
Warning( "Building soundcache, expected %i bytes of data but got %i [ %s ]\n", dataSize, readSize, m_pSfx->GetFileName() );
dataSize = readSize;
}
info->SetCachedDataSize( dataSize );
info->SetCachedData( data );
}
#endif // MP3_STARTUP_DATA_SIZE_BYTES > 0
g_pSndIO->close( file );
// Data size gets computed in GetStartupData!!!
info->SetDataSize( m_dataSize );
}
//-----------------------------------------------------------------------------
// Purpose:
// Output : char const
//-----------------------------------------------------------------------------
char const *CAudioSourceMP3::GetFileName()
{
return m_pSfx ? m_pSfx->GetFileName() : "NULL m_pSfx";
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CAudioSourceMP3::CheckAudioSourceCache()
{
Assert( m_pSfx );
if ( !m_pSfx->IsPrecachedSound() )
{
return;
}
// This will "re-cache" this if it's not in this level's cache already
m_AudioCacheHandle.Get( GetType(), true, m_pSfx, &m_nCachedDataSize );
}
//-----------------------------------------------------------------------------
// Purpose: NULL the wave data pointer (we haven't loaded yet)
//-----------------------------------------------------------------------------
CAudioSourceMP3Cache::CAudioSourceMP3Cache( CSfxTable *pSfx ) :
CAudioSourceMP3( pSfx )
{
m_hCache = 0;
}
CAudioSourceMP3Cache::CAudioSourceMP3Cache( CSfxTable *pSfx, CAudioSourceCachedInfo *info ) :
CAudioSourceMP3( pSfx, info )
{
m_hCache = 0;
m_dataSize = info->DataSize();
m_dataStart = info->DataStart();
m_bNoSentence = false;
}
//-----------------------------------------------------------------------------
// Purpose: Free any wave data we've allocated
//-----------------------------------------------------------------------------
CAudioSourceMP3Cache::~CAudioSourceMP3Cache( void )
{
CacheUnload();
}
int CAudioSourceMP3Cache::GetCacheStatus( void )
{
bool bCacheValid;
int loaded = wavedatacache->IsDataLoadCompleted( m_hCache, &bCacheValid ) ? AUDIO_IS_LOADED : AUDIO_NOT_LOADED;
if ( !bCacheValid )
{
wavedatacache->RestartDataLoad( &m_hCache, m_pSfx->GetFileName(), m_dataSize, m_dataStart );
}
return loaded;
}
void CAudioSourceMP3Cache::CacheLoad( void )
{
// Commence lazy load?
if ( m_hCache != 0 )
{
GetCacheStatus();
return;
}
m_hCache = wavedatacache->AsyncLoadCache( m_pSfx->GetFileName(), m_dataSize, m_dataStart );
}
void CAudioSourceMP3Cache::CacheUnload( void )
{
if ( m_hCache != 0 )
{
wavedatacache->Unload( m_hCache );
}
}
char *CAudioSourceMP3Cache::GetDataPointer( void )
{
char *pMP3Data = NULL;
bool dummy = false;
if ( m_hCache == 0 )
{
CacheLoad();
}
wavedatacache->GetDataPointer(
m_hCache,
m_pSfx->GetFileName(),
m_dataSize,
m_dataStart,
(void **)&pMP3Data,
0,
&dummy );
return pMP3Data;
}
int CAudioSourceMP3Cache::GetOutputData( void **pData, int samplePosition, int sampleCount, char copyBuf[AUDIOSOURCE_COPYBUF_SIZE] )
{
// how many bytes are available ?
int totalSampleCount = m_dataSize - samplePosition;
// may be asking for a sample out of range, clip at zero
if ( totalSampleCount < 0 )
totalSampleCount = 0;
// clip max output samples to max available
if ( sampleCount > totalSampleCount )
sampleCount = totalSampleCount;
// if we are returning some samples, store the pointer
if ( sampleCount )
{
// Starting past end of "preloaded" data, just use regular cache
if ( samplePosition >= m_nCachedDataSize )
{
*pData = GetDataPointer();
}
else
{
// Start async loader if we haven't already done so
CacheLoad();
// Return less data if we are about to run out of uncached data
if ( samplePosition + sampleCount >= m_nCachedDataSize )
{
sampleCount = m_nCachedDataSize - samplePosition;
}
// Point at preloaded/cached data from .cache file for now
*pData = GetCachedDataPointer();
}
if ( *pData )
{
*pData = (char *)*pData + samplePosition;
}
else
{
// Out of data or file i/o problem
sampleCount = 0;
}
}
return sampleCount;
}
CAudioMixer *CAudioSourceMP3Cache::CreateMixer( int initialStreamPosition )
{
CAudioMixer *pMixer = new CAudioMixerWaveMP3( CreateWaveDataMemory(*this) );
return pMixer;
}
CSentence *CAudioSourceMP3Cache::GetSentence( void )
{
// Already checked and this wav doesn't have sentence data...
if ( m_bNoSentence == true )
{
return NULL;
}
// Look up sentence from cache
CAudioSourceCachedInfo *info = m_AudioCacheHandle.FastGet();
if ( !info )
{
info = m_AudioCacheHandle.Get( CAudioSource::AUDIO_SOURCE_WAV, m_pSfx->IsPrecachedSound(), m_pSfx, &m_nCachedDataSize );
}
Assert( info );
if ( !info )
{
m_bNoSentence = true;
return NULL;
}
CSentence *sentence = info->Sentence();
if ( !sentence )
{
if ( !m_bCheckedForPendingSentence )
{
int iSentence = g_PhonemeFileSentences.Find( m_pSfx->GetFileName() );
if ( iSentence != g_PhonemeFileSentences.InvalidIndex() )
{
sentence = g_PhonemeFileSentences.Element( iSentence );
SetSentence( sentence );
}
m_bCheckedForPendingSentence = true;
}
}
if ( !sentence )
{
m_bNoSentence = true;
return NULL;
}
if ( sentence->m_bIsValid )
{
return sentence;
}
m_bNoSentence = true;
return NULL;
}
//-----------------------------------------------------------------------------
// CAudioSourceStreamMP3
//-----------------------------------------------------------------------------
CAudioSourceStreamMP3::CAudioSourceStreamMP3( CSfxTable *pSfx ) :
CAudioSourceMP3( pSfx )
{
}
CAudioSourceStreamMP3::CAudioSourceStreamMP3( CSfxTable *pSfx, CAudioSourceCachedInfo *info ) :
CAudioSourceMP3( pSfx, info )
{
m_dataSize = info->DataSize();
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CAudioSourceStreamMP3::Prefetch()
{
PrefetchDataStream( m_pSfx->GetFileName(), 0, m_dataSize );
}
CAudioMixer *CAudioSourceStreamMP3::CreateMixer( int intialStreamPosition )
{
// BUGBUG: Source constructs the IWaveData, mixer frees it, fix this?
IWaveData *pWaveData = CreateWaveDataStream( *this, static_cast<IWaveStreamSource *>( this ), m_pSfx->GetFileName(), 0, m_dataSize, m_pSfx, 0 );
if ( pWaveData )
{
CAudioMixer *pMixer = new CAudioMixerWaveMP3( pWaveData );
if ( pMixer )
{
if ( !m_bCheckedForPendingSentence )
{
int iSentence = g_PhonemeFileSentences.Find( m_pSfx->GetFileName() );
if ( iSentence != g_PhonemeFileSentences.InvalidIndex() )
{
SetSentence( g_PhonemeFileSentences.Element( iSentence ) );
}
m_bCheckedForPendingSentence = true;
}
return pMixer;
}
// no mixer but pWaveData was deleted in mixer's destructor
// so no need to delete
}
return NULL;
}
int CAudioSourceStreamMP3::GetOutputData( void **pData, int samplePosition, int sampleCount, char copyBuf[AUDIOSOURCE_COPYBUF_SIZE] )
{
return 0;
}
bool Audio_IsMP3( const char *pName )
{
int len = strlen(pName);
if ( len > 4 )
{
if ( !Q_strnicmp( &pName[len - 4], ".mp3", 4 ) )
{
return true;
}
}
return false;
}
CAudioSource *Audio_CreateStreamedMP3( CSfxTable *pSfx )
{
CAudioSourceStreamMP3 *pMP3 = NULL;
CAudioSourceCachedInfo *info = audiosourcecache->GetInfo( CAudioSource::AUDIO_SOURCE_MP3, pSfx->IsPrecachedSound(), pSfx );
if ( info )
{
pMP3 = new CAudioSourceStreamMP3( pSfx, info );
}
else
{
pMP3 = new CAudioSourceStreamMP3( pSfx );
}
return pMP3;
}
CAudioSource *Audio_CreateMemoryMP3( CSfxTable *pSfx )
{
CAudioSourceMP3Cache *pMP3 = NULL;
CAudioSourceCachedInfo *info = audiosourcecache->GetInfo( CAudioSource::AUDIO_SOURCE_MP3, pSfx->IsPrecachedSound(), pSfx );
if ( info )
{
pMP3 = new CAudioSourceMP3Cache( pSfx, info );
}
else
{
pMP3 = new CAudioSourceMP3Cache( pSfx );
}
return pMP3;
}
#endif
|