Counter Strike : Global Offensive Source Code
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.

273 lines
6.4 KiB

  1. //========= Copyright (c) 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #ifdef OSX
  8. #include <Carbon/Carbon.h>
  9. #include <CoreAudio/CoreAudio.h>
  10. #endif
  11. #include "tier0/platform.h"
  12. #include "ivoicerecord.h"
  13. #include "voice_mixer_controls.h"
  14. // memdbgon must be the last include file in a .cpp file!!!
  15. #include "tier0/memdbgon.h"
  16. #ifndef OSX
  17. class CMixerControls : public IMixerControls
  18. {
  19. public:
  20. CMixerControls() {}
  21. virtual ~CMixerControls() {}
  22. virtual void Release() {}
  23. virtual bool GetValue_Float(Control iControl, float &value ) {return false;}
  24. virtual bool SetValue_Float(Control iControl, float value) {return false;}
  25. virtual bool SelectMicrophoneForWaveInput() {return false;}
  26. virtual const char *GetMixerName() {return "Linux"; }
  27. private:
  28. };
  29. IMixerControls* g_pMixerControls = NULL;
  30. void InitMixerControls()
  31. {
  32. if ( !g_pMixerControls )
  33. {
  34. g_pMixerControls = new CMixerControls;
  35. }
  36. }
  37. void ShutdownMixerControls()
  38. {
  39. delete g_pMixerControls;
  40. g_pMixerControls = NULL;
  41. }
  42. #elif defined(OSX)
  43. class CMixerControls : public IMixerControls
  44. {
  45. public:
  46. CMixerControls();
  47. virtual ~CMixerControls();
  48. virtual void Release();
  49. virtual bool GetValue_Float(Control iControl, float &value);
  50. virtual bool SetValue_Float(Control iControl, float value);
  51. virtual bool SelectMicrophoneForWaveInput();
  52. virtual const char *GetMixerName();
  53. private:
  54. AudioObjectID GetDefaultInputDevice();
  55. char *m_szMixerName;
  56. AudioObjectID m_theDefaultDeviceID;
  57. };
  58. CMixerControls::CMixerControls()
  59. {
  60. m_szMixerName = NULL;
  61. m_theDefaultDeviceID = GetDefaultInputDevice();
  62. OSStatus theStatus;
  63. UInt32 outSize = sizeof(UInt32);
  64. theStatus = AudioDeviceGetPropertyInfo( m_theDefaultDeviceID,
  65. 0,
  66. TRUE,
  67. kAudioDevicePropertyDeviceName,
  68. &outSize,
  69. NULL);
  70. if ( theStatus == noErr )
  71. {
  72. m_szMixerName = (char *)malloc( outSize*sizeof(char));
  73. theStatus = AudioDeviceGetProperty( m_theDefaultDeviceID,
  74. 0,
  75. TRUE,
  76. kAudioDevicePropertyDeviceName,
  77. &outSize,
  78. m_szMixerName);
  79. if ( theStatus != noErr )
  80. {
  81. free( m_szMixerName );
  82. m_szMixerName = NULL;
  83. }
  84. }
  85. }
  86. CMixerControls::~CMixerControls()
  87. {
  88. if ( m_szMixerName )
  89. free( m_szMixerName );
  90. }
  91. void CMixerControls::Release()
  92. {
  93. }
  94. bool CMixerControls::SelectMicrophoneForWaveInput()
  95. {
  96. return true; // not needed
  97. }
  98. const char *CMixerControls::GetMixerName()
  99. {
  100. return m_szMixerName;
  101. }
  102. bool CMixerControls::GetValue_Float(Control iControl, float &value)
  103. {
  104. switch( iControl)
  105. {
  106. case MicBoost:
  107. {
  108. value = 0.0f;
  109. return true;
  110. }
  111. case MicVolume:
  112. {
  113. Float32 theVolume = 0;
  114. UInt32 theVolumeSize = sizeof(Float32);
  115. OSStatus theError = paramErr;
  116. theError = AudioDeviceGetProperty( m_theDefaultDeviceID,
  117. 1,
  118. TRUE,
  119. kAudioDevicePropertyVolumeScalar,
  120. &theVolumeSize,
  121. &theVolume);
  122. value = theVolume;
  123. return theError == noErr;
  124. }
  125. case MicMute:
  126. // Mic playback muting. You usually want this set to false, otherwise the sound card echoes whatever you say into the mic.
  127. {
  128. Float32 theMute = 0;
  129. UInt32 theMuteSize = sizeof(Float32);
  130. OSStatus theError = paramErr;
  131. theError = AudioDeviceGetProperty( m_theDefaultDeviceID,
  132. 0,
  133. TRUE,
  134. kAudioDevicePropertyMute,
  135. &theMuteSize,
  136. &theMute);
  137. value = theMute;
  138. return theError == noErr;
  139. }
  140. default:
  141. assert( !"Invalid Control type" );
  142. value = 0.0f;
  143. return false;
  144. };
  145. }
  146. bool CMixerControls::SetValue_Float(Control iControl, float value)
  147. {
  148. switch( iControl)
  149. {
  150. case MicBoost:
  151. {
  152. return false;
  153. }
  154. case MicVolume:
  155. {
  156. Float32 theVolume = value;
  157. UInt32 size = sizeof(Float32);
  158. Boolean canset = false;
  159. AudioObjectID defaultInputDevice = m_theDefaultDeviceID;
  160. size = sizeof(canset);
  161. OSStatus err = AudioDeviceGetPropertyInfo( defaultInputDevice, 0, true, kAudioDevicePropertyVolumeScalar, &size, &canset);
  162. if(err==noErr && canset==true)
  163. {
  164. size = sizeof(theVolume);
  165. err = AudioDeviceSetProperty( defaultInputDevice, NULL, 0, true, kAudioDevicePropertyVolumeScalar, size, &theVolume);
  166. return err==noErr;
  167. }
  168. // try seperate channels
  169. // get channels
  170. UInt32 channels[2];
  171. size = sizeof(channels);
  172. err = AudioDeviceGetProperty(defaultInputDevice, 0, true, kAudioDevicePropertyPreferredChannelsForStereo, &size,&channels);
  173. if(err!=noErr)
  174. return false;
  175. // set volume
  176. size = sizeof(float);
  177. err = AudioDeviceSetProperty(defaultInputDevice, 0, channels[0], true, kAudioDevicePropertyVolumeScalar, size, &theVolume);
  178. //AssertMsg1( noErr==err, "error setting volume of channel %d\n",(int)channels[0]);
  179. err = AudioDeviceSetProperty(defaultInputDevice, 0, channels[1], true, kAudioDevicePropertyVolumeScalar, size, &theVolume);
  180. //AssertMsg1( noErr==err, "error setting volume of channel %d\n",(int)channels[1]);
  181. return err == noErr;
  182. }
  183. case MicMute:
  184. // Mic playback muting. You usually want this set to false, otherwise the sound card echoes whatever you say into the mic.
  185. {
  186. Float32 theMute = value;
  187. UInt32 theMuteSize = sizeof(Float32);
  188. OSStatus theError = paramErr;
  189. theError = AudioDeviceSetProperty( m_theDefaultDeviceID,
  190. NULL,
  191. 0,
  192. TRUE,
  193. kAudioDevicePropertyMute,
  194. theMuteSize,
  195. &theMute);
  196. return theError == noErr;
  197. }
  198. default:
  199. assert( !"Invalid Control type" );
  200. return false;
  201. };
  202. }
  203. AudioObjectID CMixerControls::GetDefaultInputDevice()
  204. {
  205. AudioObjectID theDefaultDeviceID = kAudioObjectUnknown;
  206. AudioObjectPropertyAddress theDefaultDeviceAddress = { kAudioHardwarePropertyDefaultInputDevice, kAudioObjectPropertyScopeGlobal, kAudioObjectPropertyElementMaster };
  207. UInt32 theDefaultDeviceSize = sizeof(AudioObjectID);
  208. OSStatus theError = AudioObjectGetPropertyData (kAudioObjectSystemObject, &theDefaultDeviceAddress, 0, NULL, &theDefaultDeviceSize, &theDefaultDeviceID);
  209. return theDefaultDeviceID;
  210. }
  211. IMixerControls* g_pMixerControls = NULL;
  212. void InitMixerControls()
  213. {
  214. if ( !g_pMixerControls )
  215. {
  216. g_pMixerControls = new CMixerControls;
  217. }
  218. }
  219. void ShutdownMixerControls()
  220. {
  221. delete g_pMixerControls;
  222. g_pMixerControls = NULL;
  223. }
  224. #else
  225. #error
  226. #endif