aboutsummaryrefslogtreecommitdiffstats
path: root/src/bin/desklink/MixerControl.cpp
blob: 7b989f3aff9a9110e0dc2eb6ea396935711962a6 (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
/*
 * Copyright 2003-2013, Haiku, Inc.
 * Distributed under the terms of the MIT license.
 *
 * Authors:
 *		Jérôme Duval
 *		François Revol
 *		Axel Dörfler, axeld@pinc-software.de.
 *		Puck Meerburg, puck@puckipedia.nl
 *		Dario Casalinuovo, b.vitruvio@gmail.com
 */


#include "MixerControl.h"

#include <string.h>

#include <Debug.h>
#include <ParameterWeb.h>


MixerControl::MixerControl(int32 volumeWhich)
	:
	fVolumeWhich(volumeWhich),
	fGainMediaNode(media_node::null),
	fMuteMediaNode(media_node::null),
	fParameterWeb(NULL),
	fMixerParameter(NULL),
	fMuteParameter(NULL),
	fMin(0.0f),
	fMax(0.0f),
	fStep(0.0f),
	fRoster(NULL)
{
	fRoster = BMediaRoster::Roster();
}


MixerControl::~MixerControl()
{
	_Disconnect();
}


bool
MixerControl::Connect(int32 volumeWhich, float* _value, const char** _error)
{
	fVolumeWhich = volumeWhich;

	_Disconnect();

	status_t status = B_OK;
	const char* errorString = NULL;
	if (fRoster == NULL)
		fRoster = BMediaRoster::Roster(&status);

	if (BMediaRoster::IsRunning() && fRoster != NULL
			&& status == B_OK) {
		switch (volumeWhich) {
			case VOLUME_USE_MIXER:
				status = fRoster->GetAudioMixer(&fGainMediaNode);
				break;
			case VOLUME_USE_PHYS_OUTPUT:
				status = fRoster->GetAudioOutput(&fGainMediaNode);
				break;
		}
		if (status == B_OK) {
			status = fRoster->GetParameterWebFor(fGainMediaNode, &fParameterWeb);
			if (status == B_OK) {
				// Finding the Mixer slider in the audio output ParameterWeb
				int32 numParams = fParameterWeb->CountParameters();
				BParameter* p = NULL;
				bool foundMixerLabel = false;
				for (int i = 0; i < numParams; i++) {
					p = fParameterWeb->ParameterAt(i);

					// assume the mute preceeding master gain control
					if (!strcmp(p->Kind(), B_MUTE)) {
						fMuteParameter = p;
						fMuteMediaNode = fMuteParameter->Web()->Node();
					}

					PRINT(("BParameter[%i]: %s\n", i, p->Name()));
					if (volumeWhich == VOLUME_USE_MIXER) {
						if (!strcmp(p->Kind(), B_MASTER_GAIN))
							break;
					} else if (volumeWhich == VOLUME_USE_PHYS_OUTPUT) {
						/* not all cards use the same name, and
						 * they don't seem to use Kind() == B_MASTER_GAIN
						 */
						if (!strcmp(p->Kind(), B_MASTER_GAIN))
							break;
						PRINT(("not MASTER_GAIN \n"));

						/* some audio card
						 */
						if (!strcmp(p->Name(), "Master"))
							break;
						PRINT(("not 'Master' \n"));

						/* some Ensonic card have all controls names 'Volume', so
						 * need to fint the one that has the 'Mixer' text label
						 */
						if (foundMixerLabel && !strcmp(p->Name(), "Volume"))
							break;
						if (!strcmp(p->Name(), "Mixer"))
							foundMixerLabel = true;
						PRINT(("not 'Mixer' \n"));
					}
#if 0
					//if (!strcmp(p->Name(), "Master")) {
					if (!strcmp(p->Kind(), B_MASTER_GAIN)) {
						for (; i < numParams; i++) {
							p = fParamWeb->ParameterAt(i);
							if (strcmp(p->Kind(), B_MASTER_GAIN))
								p = NULL;
							else
								break;
						}
						break;
					} else
						p = NULL;
#endif
					p = NULL;
				}
				if (p == NULL) {
					errorString = volumeWhich ? "Could not find the soundcard"
						: "Could not find the mixer";
				} else if (p->Type() != BParameter::B_CONTINUOUS_PARAMETER) {
					errorString = volumeWhich ? "Soundcard control unknown"
						: "Mixer control unknown";
				} else {
					fMixerParameter = static_cast<BContinuousParameter*>(p);
					fMin = fMixerParameter->MinValue();
					fMax = fMixerParameter->MaxValue();
					fStep = fMixerParameter->ValueStep();

					if (_value != NULL) {
						float volume;
						bigtime_t lastChange;
						size_t size = sizeof(float);
						fMixerParameter->GetValue(&volume, &size, &lastChange);

						*_value = volume;
					}
				}
			} else {
				errorString = "No parameter web";
				fParameterWeb = NULL;
			}
		} else
			errorString = volumeWhich ? "No Audio output" : "No Mixer";

	} else
		errorString = "Media services not running";

	if (status != B_OK) {
		_Disconnect();
		fMuteMediaNode = media_node::null;
	}

	if (errorString) {
		fprintf(stderr, "MixerControl: %s.\n", errorString);
		if (_error)
			*_error = errorString;
	}
	if (fMixerParameter == NULL && _value != NULL)
		*_value = 0;

	return errorString == NULL;
}


bool
MixerControl::Connected()
{
	return fGainMediaNode != media_node::null;
}


int32
MixerControl::VolumeWhich() const
{
	return fVolumeWhich;
}


void
MixerControl::SetMute(bool muted)
{
	if (fMuteParameter == NULL)
		return;

	int32 mute = muted ? 1 : 0;
	fMuteParameter->SetValue(&mute, sizeof(int32), system_time());
}


bool
MixerControl::Mute()
{
	if (fMuteParameter == NULL)
		return false;

	int32 mute = 0;
	bigtime_t lastChange = 0;
	size_t size = sizeof(int32);
	fMuteParameter->GetValue(&mute, &size, &lastChange);
	return mute != 0;
}


float
MixerControl::Volume() const
{
	if (fMixerParameter == NULL)
		return 0.0f;

	float volume = 0;
	bigtime_t lastChange;
	size_t size = sizeof(float);
	fMixerParameter->GetValue(&volume, &size, &lastChange);

	return volume;
}


void
MixerControl::SetVolume(float volume)
{
	if (fMixerParameter == NULL)
		return;

	if (volume < fMin)
		volume = fMin;
	else if (volume > fMax)
		volume = fMax;

	if (volume != Volume())
		fMixerParameter->SetValue(&volume, sizeof(float), system_time());
}


void
MixerControl::ChangeVolumeBy(float value)
{
	if (fMixerParameter == NULL || value == 0.0f)
		return;

	float volume = Volume();
	SetVolume(volume + value);
}


void
MixerControl::_Disconnect()
{
	delete fParameterWeb;
	fParameterWeb = NULL;
	fMixerParameter = NULL;

	if (fRoster == NULL)
		fRoster = BMediaRoster::Roster();

	if (fRoster != NULL && fGainMediaNode != media_node::null)
		fRoster->ReleaseNode(fGainMediaNode);

	fGainMediaNode = media_node::null;
}