2 * Copyright 2003-2013, Haiku, Inc.
3 * Distributed under the terms of the MIT license.
8 * Axel Dörfler, axeld@pinc-software.de.
9 * Puck Meerburg, puck@puckipedia.nl
10 * Dario Casalinuovo, b.vitruvio@gmail.com
14 #include "MixerControl.h"
19 #include <ParameterWeb.h>
22 MixerControl::MixerControl(int32 volumeWhich
)
24 fVolumeWhich(volumeWhich
),
25 fGainMediaNode(media_node::null
),
26 fMuteMediaNode(media_node::null
),
28 fMixerParameter(NULL
),
35 fRoster
= BMediaRoster::Roster();
39 MixerControl::~MixerControl()
46 MixerControl::Connect(int32 volumeWhich
, float* _value
, const char** _error
)
48 fVolumeWhich
= volumeWhich
;
52 status_t status
= B_OK
;
53 const char* errorString
= NULL
;
55 fRoster
= BMediaRoster::Roster(&status
);
57 if (BMediaRoster::IsRunning() && fRoster
!= NULL
59 switch (volumeWhich
) {
60 case VOLUME_USE_MIXER
:
61 status
= fRoster
->GetAudioMixer(&fGainMediaNode
);
63 case VOLUME_USE_PHYS_OUTPUT
:
64 status
= fRoster
->GetAudioOutput(&fGainMediaNode
);
68 status
= fRoster
->GetParameterWebFor(fGainMediaNode
, &fParameterWeb
);
70 // Finding the Mixer slider in the audio output ParameterWeb
71 int32 numParams
= fParameterWeb
->CountParameters();
73 bool foundMixerLabel
= false;
74 for (int i
= 0; i
< numParams
; i
++) {
75 p
= fParameterWeb
->ParameterAt(i
);
77 // assume the mute preceeding master gain control
78 if (!strcmp(p
->Kind(), B_MUTE
)) {
80 fMuteMediaNode
= fMuteParameter
->Web()->Node();
83 PRINT(("BParameter[%i]: %s\n", i
, p
->Name()));
84 if (volumeWhich
== VOLUME_USE_MIXER
) {
85 if (!strcmp(p
->Kind(), B_MASTER_GAIN
))
87 } else if (volumeWhich
== VOLUME_USE_PHYS_OUTPUT
) {
88 /* not all cards use the same name, and
89 * they don't seem to use Kind() == B_MASTER_GAIN
91 if (!strcmp(p
->Kind(), B_MASTER_GAIN
))
93 PRINT(("not MASTER_GAIN \n"));
97 if (!strcmp(p
->Name(), "Master"))
99 PRINT(("not 'Master' \n"));
101 /* some Ensonic card have all controls names 'Volume', so
102 * need to fint the one that has the 'Mixer' text label
104 if (foundMixerLabel
&& !strcmp(p
->Name(), "Volume"))
106 if (!strcmp(p
->Name(), "Mixer"))
107 foundMixerLabel
= true;
108 PRINT(("not 'Mixer' \n"));
111 //if (!strcmp(p->Name(), "Master")) {
112 if (!strcmp(p
->Kind(), B_MASTER_GAIN
)) {
113 for (; i
< numParams
; i
++) {
114 p
= fParamWeb
->ParameterAt(i
);
115 if (strcmp(p
->Kind(), B_MASTER_GAIN
))
127 errorString
= volumeWhich
? "Could not find the soundcard"
128 : "Could not find the mixer";
129 } else if (p
->Type() != BParameter::B_CONTINUOUS_PARAMETER
) {
130 errorString
= volumeWhich
? "Soundcard control unknown"
131 : "Mixer control unknown";
133 fMixerParameter
= static_cast<BContinuousParameter
*>(p
);
134 fMin
= fMixerParameter
->MinValue();
135 fMax
= fMixerParameter
->MaxValue();
136 fStep
= fMixerParameter
->ValueStep();
138 if (_value
!= NULL
) {
140 bigtime_t lastChange
;
141 size_t size
= sizeof(float);
142 fMixerParameter
->GetValue(&volume
, &size
, &lastChange
);
148 errorString
= "No parameter web";
149 fParameterWeb
= NULL
;
152 errorString
= volumeWhich
? "No Audio output" : "No Mixer";
155 errorString
= "Media services not running";
157 if (status
!= B_OK
) {
158 fGainMediaNode
= media_node::null
;
159 fMuteMediaNode
= media_node::null
;
163 fprintf(stderr
, "MixerControl: %s.\n", errorString
);
165 *_error
= errorString
;
167 if (fMixerParameter
== NULL
&& _value
!= NULL
)
170 return errorString
== NULL
;
175 MixerControl::Connected()
177 return fGainMediaNode
!= media_node::null
;
182 MixerControl::VolumeWhich() const
189 MixerControl::SetMute(bool muted
)
191 if (fMuteParameter
== NULL
)
194 int32 mute
= muted
? 1 : 0;
195 fMuteParameter
->SetValue(&mute
, sizeof(int32
), system_time());
202 if (fMuteParameter
== NULL
)
206 bigtime_t lastChange
= 0;
207 size_t size
= sizeof(int32
);
208 fMuteParameter
->GetValue(&mute
, &size
, &lastChange
);
214 MixerControl::Volume() const
216 if (fMixerParameter
== NULL
)
220 bigtime_t lastChange
;
221 size_t size
= sizeof(float);
222 fMixerParameter
->GetValue(&volume
, &size
, &lastChange
);
229 MixerControl::SetVolume(float volume
)
231 if (fMixerParameter
== NULL
)
236 else if (volume
> fMax
)
239 if (volume
!= Volume())
240 fMixerParameter
->SetValue(&volume
, sizeof(float), system_time());
245 MixerControl::ChangeVolumeBy(float value
)
247 if (fMixerParameter
== NULL
|| value
== 0.0f
)
250 float volume
= Volume();
251 SetVolume(volume
+ value
);
256 MixerControl::_Disconnect()
258 delete fParameterWeb
;
259 fParameterWeb
= NULL
;
260 fMixerParameter
= NULL
;
263 fRoster
= BMediaRoster::Roster();
265 if (fRoster
!= NULL
&& fGainMediaNode
!= media_node::null
)
266 fRoster
->ReleaseNode(fGainMediaNode
);
268 fGainMediaNode
= media_node::null
;