2 * GStreamer - SunAudio mixer interface element
3 * Copyright (C) 2005,2006,2008 Sun Microsystems, Inc.,
4 * Brian Cameron <brian.cameron@sun.com>
5 * Copyright (C) 2008 Sun Microsystems, Inc.,
6 * Jan Schmidt <jan.schmidt@sun.com>
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
18 * You should have received a copy of the GNU Library General Public
19 * License along with this library; if not, write to the
20 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 * Boston, MA 02111-1307, USA.
34 #include <sys/ioctl.h>
35 #include <sys/mixer.h>
37 #include <gst/gst-i18n-plugin.h>
39 #include "gstsunaudiomixerctrl.h"
40 #include "gstsunaudiomixertrack.h"
42 GST_DEBUG_CATEGORY_EXTERN (sunaudio_debug);
43 #define GST_CAT_DEFAULT sunaudio_debug
46 gst_sunaudiomixer_ctrl_open (GstSunAudioMixerCtrl * mixer)
50 /* First try to open non-blocking */
51 fd = open (mixer->device, O_RDWR | O_NONBLOCK);
55 fd = open (mixer->device, O_WRONLY);
59 GST_DEBUG_OBJECT (mixer,
60 "Failed to open mixer device %s, mixing disabled: %s", mixer->device,
67 /* Try to set the multiple open flag if we can, but ignore errors */
68 ioctl (mixer->mixer_fd, AUDIO_MIXER_MULTIPLE_OPEN);
74 gst_sunaudiomixer_ctrl_build_list (GstSunAudioMixerCtrl * mixer)
78 struct audio_info audioinfo;
81 * Do not continue appending the same 3 static tracks onto the list
83 if (mixer->tracklist == NULL) {
84 g_return_if_fail (mixer->mixer_fd != -1);
86 /* Output & should be MASTER when it's the only one. */
87 track = gst_sunaudiomixer_track_new (GST_SUNAUDIO_TRACK_OUTPUT,
88 2, GST_MIXER_TRACK_OUTPUT | GST_MIXER_TRACK_MASTER);
89 mixer->tracklist = g_list_append (mixer->tracklist, track);
92 track = gst_sunaudiomixer_track_new (GST_SUNAUDIO_TRACK_LINE_IN,
93 2, GST_MIXER_TRACK_INPUT);
95 /* Set whether we are recording from microphone or from line-in */
96 if (ioctl (mixer->mixer_fd, AUDIO_GETINFO, &audioinfo) < 0) {
97 g_warning ("Error getting audio device volume");
101 /* Set initial RECORD status */
102 if (audioinfo.record.port == AUDIO_MICROPHONE) {
103 mixer->recdevs |= (1 << GST_SUNAUDIO_TRACK_LINE_IN);
104 track->flags |= GST_MIXER_TRACK_RECORD;
106 mixer->recdevs &= ~(1 << GST_SUNAUDIO_TRACK_LINE_IN);
107 track->flags &= ~GST_MIXER_TRACK_RECORD;
111 mixer->tracklist = g_list_append (mixer->tracklist, track);
112 track = gst_sunaudiomixer_track_new (GST_SUNAUDIO_TRACK_MONITOR,
113 2, GST_MIXER_TRACK_INPUT);
114 mixer->tracklist = g_list_append (mixer->tracklist, track);
118 GstSunAudioMixerCtrl *
119 gst_sunaudiomixer_ctrl_new (const char *device)
121 GstSunAudioMixerCtrl *ret = NULL;
123 g_return_val_if_fail (device != NULL, NULL);
125 ret = g_new0 (GstSunAudioMixerCtrl, 1);
127 ret->device = g_strdup (device);
129 ret->tracklist = NULL;
131 if (!gst_sunaudiomixer_ctrl_open (ret))
138 gst_sunaudiomixer_ctrl_free (ret);
144 gst_sunaudiomixer_ctrl_free (GstSunAudioMixerCtrl * mixer)
146 g_return_if_fail (mixer != NULL);
149 g_free (mixer->device);
150 mixer->device = NULL;
153 if (mixer->tracklist) {
154 g_list_foreach (mixer->tracklist, (GFunc) g_object_unref, NULL);
155 g_list_free (mixer->tracklist);
156 mixer->tracklist = NULL;
159 if (mixer->mixer_fd != -1) {
160 close (mixer->mixer_fd);
161 mixer->mixer_fd = -1;
168 gst_sunaudiomixer_ctrl_list_tracks (GstSunAudioMixerCtrl * mixer)
170 gst_sunaudiomixer_ctrl_build_list (mixer);
172 return (const GList *) mixer->tracklist;
176 gst_sunaudiomixer_ctrl_get_volume (GstSunAudioMixerCtrl * mixer,
177 GstMixerTrack * track, gint * volumes)
183 struct audio_info audioinfo;
185 GstSunAudioMixerTrack *sunaudiotrack = GST_SUNAUDIO_MIXER_TRACK (track);
187 g_return_if_fail (mixer->mixer_fd != -1);
189 if (ioctl (mixer->mixer_fd, AUDIO_GETINFO, &audioinfo) < 0) {
190 g_warning ("Error getting audio device volume");
194 switch (sunaudiotrack->track_num) {
195 case GST_SUNAUDIO_TRACK_OUTPUT:
196 gain = (int) audioinfo.play.gain;
197 balance = audioinfo.play.balance;
199 case GST_SUNAUDIO_TRACK_LINE_IN:
200 gain = (int) audioinfo.record.gain;
201 balance = audioinfo.record.balance;
203 case GST_SUNAUDIO_TRACK_MONITOR:
204 gain = (int) audioinfo.monitor_gain;
205 balance = audioinfo.record.balance;
209 if (balance == AUDIO_MID_BALANCE) {
212 } else if (balance < AUDIO_MID_BALANCE) {
214 ratio = 1 - (float) (AUDIO_MID_BALANCE - balance) /
215 (float) AUDIO_MID_BALANCE;
216 volumes[1] = (int) ((float) gain * ratio + 0.5);
219 ratio = 1 - (float) (balance - AUDIO_MID_BALANCE) /
220 (float) AUDIO_MID_BALANCE;
221 volumes[0] = (int) ((float) gain * ratio + 0.5);
225 * Reset whether we are recording from microphone or from line-in.
226 * This can change if another program resets the value (such as
227 * sdtaudiocontrol), so it is good to update the flag when we
228 * get the volume. The gnome-volume-control program calls this
229 * function in a loop so the value will update properly when
232 if ((audioinfo.record.port == AUDIO_MICROPHONE &&
233 !GST_MIXER_TRACK_HAS_FLAG (track, GST_MIXER_TRACK_RECORD)) ||
234 (audioinfo.record.port == AUDIO_LINE_IN &&
235 GST_MIXER_TRACK_HAS_FLAG (track, GST_MIXER_TRACK_RECORD))) {
237 if (audioinfo.record.port == AUDIO_MICROPHONE) {
238 mixer->recdevs |= (1 << GST_SUNAUDIO_TRACK_LINE_IN);
239 track->flags |= GST_MIXER_TRACK_RECORD;
241 mixer->recdevs &= ~(1 << GST_SUNAUDIO_TRACK_LINE_IN);
242 track->flags &= ~GST_MIXER_TRACK_RECORD;
246 /* Likewise reset MUTE */
247 if ((sunaudiotrack->track_num == GST_SUNAUDIO_TRACK_OUTPUT &&
248 audioinfo.output_muted == 1) ||
249 (sunaudiotrack->track_num != GST_SUNAUDIO_TRACK_OUTPUT && gain == 0)) {
251 * If MUTE is set, then gain is always 0, so don't bother
252 * resetting our internal value.
254 track->flags |= GST_MIXER_TRACK_MUTE;
256 sunaudiotrack->gain = gain;
257 sunaudiotrack->balance = balance;
258 track->flags &= ~GST_MIXER_TRACK_MUTE;
263 gst_sunaudiomixer_ctrl_set_volume (GstSunAudioMixerCtrl * mixer,
264 GstMixerTrack * track, gint * volumes)
278 struct audio_info audioinfo;
280 GstSunAudioMixerTrack *sunaudiotrack = GST_SUNAUDIO_MIXER_TRACK (track);
284 l_real_gain = volumes[0];
285 r_real_gain = volumes[1];
287 if (l_real_gain == r_real_gain) {
289 balance = AUDIO_MID_BALANCE;
290 } else if (l_real_gain < r_real_gain) {
292 ratio = (float) l_real_gain / (float) r_real_gain;
294 AUDIO_RIGHT_BALANCE - (int) (ratio * (float) AUDIO_MID_BALANCE + 0.5);
297 ratio = (float) r_real_gain / (float) l_real_gain;
299 AUDIO_LEFT_BALANCE + (int) (ratio * (float) AUDIO_MID_BALANCE + 0.5);
302 sunaudiotrack->gain = gain;
303 sunaudiotrack->balance = balance;
305 if (GST_MIXER_TRACK_HAS_FLAG (track, GST_MIXER_TRACK_MUTE)) {
306 if (sunaudiotrack->track_num == GST_SUNAUDIO_TRACK_OUTPUT) {
308 } else if (gain == 0) {
312 * If the volume is set to a non-zero value for LINE_IN
313 * or MONITOR, then unset MUTE.
315 track->flags &= ~GST_MIXER_TRACK_MUTE;
320 AUDIO_INITINFO (&audioinfo);
322 switch (sunaudiotrack->track_num) {
323 case GST_SUNAUDIO_TRACK_OUTPUT:
324 audioinfo.play.gain = gain;
325 audioinfo.play.balance = balance;
327 case GST_SUNAUDIO_TRACK_LINE_IN:
328 audioinfo.record.gain = gain;
329 audioinfo.record.balance = balance;
331 case GST_SUNAUDIO_TRACK_MONITOR:
332 audioinfo.monitor_gain = gain;
333 audioinfo.record.balance = balance;
337 g_return_if_fail (mixer->mixer_fd != -1);
339 if (ioctl (mixer->mixer_fd, AUDIO_SETINFO, &audioinfo) < 0) {
340 g_warning ("Error setting audio device volume");
346 gst_sunaudiomixer_ctrl_set_mute (GstSunAudioMixerCtrl * mixer,
347 GstMixerTrack * track, gboolean mute)
349 struct audio_info audioinfo;
351 GstSunAudioMixerTrack *sunaudiotrack = GST_SUNAUDIO_MIXER_TRACK (track);
353 gint volume, balance;
355 AUDIO_INITINFO (&audioinfo);
359 track->flags |= GST_MIXER_TRACK_MUTE;
361 volume = sunaudiotrack->gain;
362 track->flags &= ~GST_MIXER_TRACK_MUTE;
365 balance = sunaudiotrack->balance;
367 switch (sunaudiotrack->track_num) {
368 case GST_SUNAUDIO_TRACK_OUTPUT:
371 audioinfo.output_muted = 1;
373 audioinfo.output_muted = 0;
375 audioinfo.play.gain = volume;
376 audioinfo.play.balance = balance;
378 case GST_SUNAUDIO_TRACK_LINE_IN:
379 audioinfo.record.gain = volume;
380 audioinfo.record.balance = balance;
382 case GST_SUNAUDIO_TRACK_MONITOR:
383 audioinfo.monitor_gain = volume;
384 audioinfo.record.balance = balance;
388 g_return_if_fail (mixer->mixer_fd != -1);
390 if (ioctl (mixer->mixer_fd, AUDIO_SETINFO, &audioinfo) < 0) {
391 g_warning ("Error setting audio device volume");
397 gst_sunaudiomixer_ctrl_set_record (GstSunAudioMixerCtrl * mixer,
398 GstMixerTrack * track, gboolean record)
400 GstSunAudioMixerTrack *sunaudiotrack = GST_SUNAUDIO_MIXER_TRACK (track);
402 struct audio_info audioinfo;
406 /* Don't change the setting */
407 if ((record && GST_MIXER_TRACK_HAS_FLAG (track, GST_MIXER_TRACK_RECORD)) ||
408 (!record && !GST_MIXER_TRACK_HAS_FLAG (track, GST_MIXER_TRACK_RECORD)))
412 * So there is probably no need to look for others, but reset them all
415 for (trk = mixer->tracklist; trk != NULL; trk = trk->next) {
416 GstMixerTrack *turn = (GstMixerTrack *) trk->data;
418 turn->flags &= ~GST_MIXER_TRACK_RECORD;
423 AUDIO_INITINFO (&audioinfo);
426 audioinfo.record.port = AUDIO_MICROPHONE;
427 mixer->recdevs |= (1 << sunaudiotrack->track_num);
428 track->flags |= GST_MIXER_TRACK_RECORD;
430 audioinfo.record.port = AUDIO_LINE_IN;
431 mixer->recdevs &= ~(1 << sunaudiotrack->track_num);
432 track->flags &= ~GST_MIXER_TRACK_RECORD;
435 g_return_if_fail (mixer->mixer_fd != -1);
437 if (ioctl (mixer->mixer_fd, AUDIO_SETINFO, &audioinfo) < 0) {
438 g_warning ("Error setting audio device volume");