Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

186 lines
6.0 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #ifndef SND_MP3_SOURCE_H
  8. #define SND_MP3_SOURCE_H
  9. #ifdef _WIN32
  10. #pragma once
  11. #endif
  12. #include "snd_audio_source.h"
  13. #include "snd_wave_data.h"
  14. #include "snd_sfx.h"
  15. class IWaveData;
  16. class CAudioMixer;
  17. abstract_class CAudioSourceMP3 : public CAudioSource
  18. {
  19. public:
  20. CAudioSourceMP3( CSfxTable *pSfx );
  21. CAudioSourceMP3( CSfxTable *pSfx, CAudioSourceCachedInfo *info );
  22. virtual ~CAudioSourceMP3();
  23. // Create an instance (mixer) of this audio source
  24. virtual CAudioMixer *CreateMixer( int initialStreamPosition = 0 ) = 0;
  25. virtual int GetType( void );
  26. virtual void GetCacheData( CAudioSourceCachedInfo *info );
  27. // Provide samples for the mixer. You can point pData at your own data, or if you prefer to copy the data,
  28. // you can copy it into copyBuf and set pData to copyBuf.
  29. virtual int GetOutputData( void **pData, int samplePosition, int sampleCount, char copyBuf[AUDIOSOURCE_COPYBUF_SIZE] ) = 0;
  30. virtual int SampleRate( void );
  31. // Returns true if the source is a voice source.
  32. // This affects the voice_overdrive behavior (all sounds get quieter when
  33. // someone is speaking).
  34. virtual bool IsVoiceSource() { return false; }
  35. virtual int SampleSize( void ) { return 1; }
  36. // Total number of samples in this source. NOTE: Some sources are infinite (mic input), they should return
  37. // a count equal to one second of audio at their current rate.
  38. virtual int SampleCount( void ) { return m_dataSize; }
  39. virtual int Format() { return 0; }
  40. virtual int DataSize( void ) { return 0; }
  41. virtual bool IsLooped( void ) { return false; }
  42. virtual bool IsStereoWav( void ) { return false; }
  43. virtual bool IsStreaming( void ) { return false; }
  44. virtual int GetCacheStatus( void ) { return AUDIO_IS_LOADED; }
  45. virtual void CacheLoad( void ) {}
  46. virtual void CacheUnload( void ) {}
  47. virtual CSentence *GetSentence( void ) { return NULL; }
  48. virtual int ZeroCrossingBefore( int sample ) { return sample; }
  49. virtual int ZeroCrossingAfter( int sample ) { return sample; }
  50. // mixer's references
  51. virtual void ReferenceAdd( CAudioMixer *pMixer );
  52. virtual void ReferenceRemove( CAudioMixer *pMixer );
  53. // check reference count, return true if nothing is referencing this
  54. virtual bool CanDelete( void );
  55. virtual bool IsAsyncLoad();
  56. virtual void CheckAudioSourceCache();
  57. virtual char const *GetFileName();
  58. virtual void SetPlayOnce( bool isPlayOnce ) { m_bIsPlayOnce = isPlayOnce; }
  59. virtual bool IsPlayOnce() { return m_bIsPlayOnce; }
  60. virtual void SetSentenceWord( bool bIsWord ) { m_bIsSentenceWord = bIsWord; }
  61. virtual bool IsSentenceWord() { return m_bIsSentenceWord; }
  62. virtual int SampleToStreamPosition( int samplePosition ) { return 0; }
  63. virtual int StreamToSamplePosition( int streamPosition ) { return 0; }
  64. virtual void SetSentence( CSentence *pSentence );
  65. protected:
  66. //-----------------------------------------------------------------------------
  67. // Purpose:
  68. // Output : byte
  69. //-----------------------------------------------------------------------------
  70. inline byte *GetCachedDataPointer()
  71. {
  72. VPROF("CAudioSourceMP3::GetCachedDataPointer");
  73. CAudioSourceCachedInfo *info = m_AudioCacheHandle.Get( CAudioSource::AUDIO_SOURCE_MP3, m_pSfx->IsPrecachedSound(), m_pSfx, &m_nCachedDataSize );
  74. if ( !info )
  75. {
  76. Assert( !"CAudioSourceMP3::GetCachedDataPointer info == NULL" );
  77. return NULL;
  78. }
  79. return (byte *)info->CachedData();
  80. }
  81. CAudioSourceCachedInfoHandle_t m_AudioCacheHandle;
  82. int m_nCachedDataSize;
  83. protected:
  84. CSfxTable *m_pSfx;
  85. int m_sampleRate;
  86. int m_dataSize;
  87. int m_dataStart;
  88. int m_refCount;
  89. bool m_bIsPlayOnce : 1;
  90. bool m_bIsSentenceWord : 1;
  91. bool m_bCheckedForPendingSentence;
  92. };
  93. //-----------------------------------------------------------------------------
  94. // Purpose: Streaming MP3 file
  95. //-----------------------------------------------------------------------------
  96. class CAudioSourceStreamMP3 : public CAudioSourceMP3, public IWaveStreamSource
  97. {
  98. public:
  99. CAudioSourceStreamMP3( CSfxTable *pSfx );
  100. CAudioSourceStreamMP3( CSfxTable *pSfx, CAudioSourceCachedInfo *info );
  101. ~CAudioSourceStreamMP3() {}
  102. bool IsStreaming( void ) OVERRIDE { return true; }
  103. bool IsStereoWav( void ) OVERRIDE { return false; }
  104. CAudioMixer *CreateMixer( int initialStreamPosition = 0 ) OVERRIDE;
  105. int GetOutputData( void **pData, int samplePosition, int sampleCount, char copyBuf[AUDIOSOURCE_COPYBUF_SIZE] ) OVERRIDE;
  106. // IWaveStreamSource
  107. int UpdateLoopingSamplePosition( int samplePosition ) OVERRIDE
  108. {
  109. return samplePosition;
  110. }
  111. void UpdateSamples( char *pData, int sampleCount ) OVERRIDE {}
  112. int GetLoopingInfo( int *pLoopBlock, int *pNumLeadingSamples, int *pNumTrailingSamples ) OVERRIDE
  113. {
  114. return 0;
  115. }
  116. void Prefetch() OVERRIDE;
  117. private:
  118. CAudioSourceStreamMP3( const CAudioSourceStreamMP3 & ); // not implemented, not accessible
  119. };
  120. class CAudioSourceMP3Cache : public CAudioSourceMP3
  121. {
  122. public:
  123. CAudioSourceMP3Cache( CSfxTable *pSfx );
  124. CAudioSourceMP3Cache( CSfxTable *pSfx, CAudioSourceCachedInfo *info );
  125. ~CAudioSourceMP3Cache( void );
  126. int GetCacheStatus( void ) OVERRIDE;
  127. void CacheLoad( void ) OVERRIDE;
  128. void CacheUnload( void ) OVERRIDE;
  129. // NOTE: "samples" are bytes for MP3
  130. int GetOutputData( void **pData, int samplePosition, int sampleCount, char copyBuf[AUDIOSOURCE_COPYBUF_SIZE] ) OVERRIDE;
  131. CAudioMixer *CreateMixer( int initialStreamPosition = 0 ) OVERRIDE;
  132. CSentence *GetSentence( void ) OVERRIDE;
  133. void Prefetch() OVERRIDE {}
  134. protected:
  135. virtual char *GetDataPointer( void );
  136. memhandle_t m_hCache;
  137. private:
  138. CAudioSourceMP3Cache( const CAudioSourceMP3Cache & );
  139. unsigned int m_bNoSentence : 1;
  140. };
  141. bool Audio_IsMP3( const char *pName );
  142. CAudioSource *Audio_CreateStreamedMP3( CSfxTable *pSfx );
  143. CAudioSource *Audio_CreateMemoryMP3( CSfxTable *pSfx );
  144. #endif // SND_MP3_SOURCE_H