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.

127 lines
3.6 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #ifndef AUDIOWAVEOUTPUT_H
  8. #define AUDIOWAVEOUTPUT_H
  9. #ifdef _WIN32
  10. #pragma once
  11. #endif
  12. #include "sound.h"
  13. #include "utlvector.h"
  14. #define OUTPUT_BUFFER_COUNT 32
  15. #define MAX_CHANNELS 16
  16. class CAudioMixer;
  17. class CAudioMixerState
  18. {
  19. public:
  20. CAudioMixer *mixer;
  21. int submit_mixer_sample;
  22. };
  23. class CAudioBuffer
  24. {
  25. public:
  26. WAVEHDR *hdr;
  27. bool submitted;
  28. int submit_sample_count;
  29. CUtlVector< CAudioMixerState > m_Referenced;
  30. };
  31. #define OUTPUT_SAMPLE_RATE 44100
  32. #define PAINTBUFFER_SIZE 1024
  33. typedef struct
  34. {
  35. int left;
  36. int right;
  37. } portable_samplepair_t;
  38. class CAudioDeviceSWMix : public IAudioDevice
  39. {
  40. public:
  41. virtual void Mix8Mono( channel_t *pChannel, char *pData, int outputOffset, int inputOffset, int rateScaleFix, int outCount, int timecompress, bool forward = true );
  42. virtual void Mix8Stereo( channel_t *pChannel, char *pData, int outputOffset, int inputOffset, int rateScaleFix, int outCount, int timecompress, bool forward = true );
  43. virtual void Mix16Mono( channel_t *pChannel, short *pData, int outputOffset, int inputOffset, int rateScaleFix, int outCount, int timecompress, bool forward = true );
  44. virtual void Mix16Stereo( channel_t *pChannel, short *pData, int outputOffset, int inputOffset, int rateScaleFix, int outCount, int timecompress, bool forward = true );
  45. virtual int MaxSampleCount( void );
  46. virtual void MixBegin( void );
  47. void TransferBufferStereo16( short *pOutput, int sampleCount );
  48. private:
  49. portable_samplepair_t m_paintbuffer[ PAINTBUFFER_SIZE ];
  50. };
  51. class CAudioWaveOutput : public CAudioOutput
  52. {
  53. public:
  54. CAudioWaveOutput( void );
  55. ~CAudioWaveOutput( void );
  56. // returns the size of each sample in bytes
  57. virtual int SampleSize( void ) { return 2; }
  58. // returns the sampling rate of the data
  59. virtual int SampleRate( void ) { return OUTPUT_SAMPLE_RATE; }
  60. // returns the mono/stereo status of this device (true if stereo)
  61. virtual bool IsStereo( void ) { return true; }
  62. // mix a buffer up to time (time is absolute)
  63. virtual void Update( float time );
  64. virtual void Flush( void );
  65. virtual void AddSource( CAudioMixer *pSource );
  66. virtual void StopSounds( void );
  67. virtual int FindSourceIndex( CAudioMixer *pSource );
  68. virtual int GetOutputPosition( void );
  69. virtual float GetAmountofTimeAhead( void );
  70. virtual int GetNumberofSamplesAhead( void );
  71. virtual CAudioMixer *GetMixerForSource( CAudioSource *pSource );
  72. private:
  73. void OpenDevice( void );
  74. bool ValidDevice( void ) { return m_deviceHandle != 0; }
  75. void ClearDevice( void ) { m_deviceHandle = NULL; }
  76. CAudioBuffer *GetEmptyBuffer( void );
  77. void SilenceBuffer( short *pSamples, int sampleCount );
  78. void SetChannel( int channelIndex, CAudioMixer *pSource );
  79. void FreeChannel( int channelIndex );
  80. void RemoveMixerChannelReferences( CAudioMixer *mixer );
  81. void AddToReferencedList( CAudioMixer *mixer, CAudioBuffer *buffer );
  82. void RemoveFromReferencedList( CAudioMixer *mixer, CAudioBuffer *buffer );
  83. bool IsSourceReferencedByActiveBuffer( CAudioMixer *mixer );
  84. bool IsSoundInReferencedList( CAudioMixer *mixer, CAudioBuffer *buffer );
  85. // Compute how many samples we've mixed since most recent buffer submission
  86. void ComputeSampleAheadAmount( void );
  87. HWAVEOUT m_deviceHandle;
  88. float m_mixTime;
  89. float m_baseTime;
  90. int m_sampleIndex;
  91. CAudioBuffer m_buffers[ OUTPUT_BUFFER_COUNT ];
  92. CAudioMixer *m_sourceList[MAX_CHANNELS];
  93. int m_nEstimatedSamplesAhead;
  94. public:
  95. CAudioDeviceSWMix m_audioDevice;
  96. };
  97. #endif // AUDIOWAVEOUTPUT_H