summaryrefslogtreecommitdiff
path: root/engine/audio/private/voice_mixer_controls_openal.cpp
blob: 832815c8683e69d8ca2c2bc85ecf4ab84a32e4fa (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
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: 
//
// $NoKeywords: $
//=============================================================================//


#ifdef OSX
#include <Carbon/Carbon.h>
#include <CoreAudio/CoreAudio.h>
#endif

#include "tier0/platform.h"
#include "ivoicerecord.h"
#include "voice_mixer_controls.h"

// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"



#ifndef OSX


class CMixerControls : public IMixerControls
{
public:
	CMixerControls() {}
	virtual			~CMixerControls() {}
	
	virtual void	Release() {}
	virtual bool	GetValue_Float(Control iControl, float &value ) {return false;}
	virtual bool	SetValue_Float(Control iControl, float value) {return false;}
	virtual bool	SelectMicrophoneForWaveInput() {return false;}
	virtual const char *GetMixerName() {return "Linux"; }
	
private:
};

IMixerControls* g_pMixerControls = NULL;
void InitMixerControls()
{
	if ( !g_pMixerControls )
	{
		g_pMixerControls = new CMixerControls;
	}
}

void ShutdownMixerControls()
{
	delete g_pMixerControls;
	g_pMixerControls = NULL;
}

#elif defined(OSX)

class CMixerControls : public IMixerControls
{
public:
	CMixerControls();
	virtual			~CMixerControls();
	
	virtual void	Release();
	virtual bool	GetValue_Float(Control iControl, float &value);
	virtual bool	SetValue_Float(Control iControl, float value);
	virtual bool	SelectMicrophoneForWaveInput();
	virtual const char *GetMixerName();
	
private:
	AudioObjectID GetDefaultInputDevice();
	char *m_szMixerName;
	AudioObjectID m_theDefaultDeviceID;
};


CMixerControls::CMixerControls()
{
	m_szMixerName = NULL;
	
	m_theDefaultDeviceID = GetDefaultInputDevice();
	
	OSStatus theStatus;
	UInt32 outSize = sizeof(UInt32);	
	theStatus = AudioDeviceGetPropertyInfo( m_theDefaultDeviceID,
										   0,
										   TRUE,
										   kAudioDevicePropertyDeviceName,
										   &outSize,
										   NULL);
	if ( theStatus == noErr )
	{	
		m_szMixerName = (char *)malloc( outSize*sizeof(char));
		
		theStatus = AudioDeviceGetProperty( m_theDefaultDeviceID,
										   0,
										   TRUE,
										   kAudioDevicePropertyDeviceName,
										   &outSize,
										   m_szMixerName);
		
		if ( theStatus != noErr )
		{
			free( m_szMixerName );
			m_szMixerName = NULL;
		}
	}
}

CMixerControls::~CMixerControls()
{
	if ( m_szMixerName )
		free( m_szMixerName );
}

void CMixerControls::Release()
{
}

bool CMixerControls::SelectMicrophoneForWaveInput()
{
	return true; // not needed
}


const char *CMixerControls::GetMixerName()
{
	return m_szMixerName;
}


bool CMixerControls::GetValue_Float(Control iControl, float &value)
{
	switch( iControl)
	{
		case MicBoost:
		{
			value = 0.0f;
			return true;
		}
		case MicVolume:
		{
			OSStatus theError = noErr;
			for ( int iChannel = 0; iChannel < 3; iChannel++ )
			{
				// scan the channel list until you find a channel set to non-zero, then use that
				Float32 theVolume = 0;
				UInt32 theSize = sizeof(Float32);
				AudioObjectPropertyAddress theAddress = { kAudioDevicePropertyVolumeScalar,	kAudioDevicePropertyScopeInput, iChannel };

				theError = AudioObjectGetPropertyData(m_theDefaultDeviceID,
															   &theAddress,
															   0,
															   NULL,
															   &theSize,
															   &theVolume);
				value = theVolume;
				if ( theError == noErr && theVolume != 0.0f )
					break;
			}
			
			return theError == noErr;
		}
			
		case MicMute:
			// Mic playback muting. You usually want this set to false, otherwise the sound card echoes whatever you say into the mic.
		{
			Float32 theMute = 0;
			UInt32 theSize = sizeof(Float32);
			AudioObjectPropertyAddress theAddress = { kAudioDevicePropertyMute,	kAudioDevicePropertyScopeInput,	1 };
			
			OSStatus theError = AudioObjectGetPropertyData(m_theDefaultDeviceID,
														   &theAddress,
														   0,
														   NULL,
														   &theSize,
														   &theMute);
			value = theMute;
			return theError == noErr;
		}		
		default:
			assert( !"Invalid Control type" );	
			value = 0.0f;
			return false;
	};
}


bool CMixerControls::SetValue_Float(Control iControl, float value)
{
	switch( iControl)
	{
		case MicBoost:
		{
			return false;
		}
		case MicVolume:
		{
			if ( value <= 0.0 )
				return false; // don't let the volume be set to zero
			
			Float32 theVolume = value;
			UInt32 size = sizeof(Float32);
			Boolean	canset	= false;
			AudioObjectID defaultInputDevice = m_theDefaultDeviceID;
			
			size = sizeof(canset);
			OSStatus err = AudioDeviceGetPropertyInfo( defaultInputDevice, 0, true, kAudioDevicePropertyVolumeScalar, &size, &canset);
			if(err==noErr && canset==true) 
			{
				size = sizeof(theVolume);
				err = AudioDeviceSetProperty( defaultInputDevice, NULL, 0, true, kAudioDevicePropertyVolumeScalar, size, &theVolume);
				return err==noErr;
			}
			
			// try seperate channels
			// get channels
			UInt32	channels[2];
			size = sizeof(channels);
			err = AudioDeviceGetProperty(defaultInputDevice, 0, true, kAudioDevicePropertyPreferredChannelsForStereo, &size,&channels);
			if(err!=noErr)
				return false;
			
			// set volume
			size = sizeof(float);
			err = AudioDeviceSetProperty(defaultInputDevice, 0, channels[0], true, kAudioDevicePropertyVolumeScalar, size, &theVolume);
			//AssertMsg1( noErr==err, "error setting volume of channel %d\n",(int)channels[0]);
			err = AudioDeviceSetProperty(defaultInputDevice, 0, channels[1], true, kAudioDevicePropertyVolumeScalar, size, &theVolume);
			//AssertMsg1( noErr==err, "error setting volume of channel %d\n",(int)channels[1]);
			
			return err == noErr;
			
		}
		case MicMute:
			// Mic playback muting. You usually want this set to false, otherwise the sound card echoes whatever you say into the mic.
		{
			Float32 theMute = value;
			UInt32 theMuteSize = sizeof(Float32);
			OSStatus theError = paramErr;
			theError = AudioDeviceSetProperty( m_theDefaultDeviceID,
											  NULL,
											  0,
											  TRUE,
											  kAudioDevicePropertyMute,
											  theMuteSize,
											  &theMute);
			return theError == noErr;
		}		
		default:
			assert( !"Invalid Control type" );	
			return false;
	};
}


AudioObjectID CMixerControls::GetDefaultInputDevice()
{
	AudioObjectID theDefaultDeviceID = kAudioObjectUnknown;
	AudioObjectPropertyAddress theDefaultDeviceAddress = { kAudioHardwarePropertyDefaultInputDevice, kAudioObjectPropertyScopeGlobal, kAudioObjectPropertyElementMaster };
	UInt32 theDefaultDeviceSize = sizeof(AudioObjectID);
	OSStatus theError = AudioObjectGetPropertyData (kAudioObjectSystemObject, &theDefaultDeviceAddress, 0, NULL, &theDefaultDeviceSize, &theDefaultDeviceID);
	return theDefaultDeviceID;
}


IMixerControls* g_pMixerControls = NULL;
void InitMixerControls()
{
	if ( !g_pMixerControls )
	{
		g_pMixerControls = new CMixerControls;
	}
}

void ShutdownMixerControls()
{
	delete g_pMixerControls;
	g_pMixerControls = NULL;
}



#else
#error
#endif