comparison src/alsa-ng/alsa-core.c @ 3177:1117166277f7

alsa-ng: Mixer support. Should be 100% plug-and-play. alsaplug_guess_mixer_elem() tries to determine the best choice for the mixer. It may need tweaking.
author William Pitcock <nenolod@atheme.org>
date Tue, 09 Jun 2009 23:34:50 -0500
parents dca77b467ea2
children 4634dab72608
comparison
equal deleted inserted replaced
3176:dca77b467ea2 3177:1117166277f7
36 36
37 /******************************************************************************** 37 /********************************************************************************
38 * ALSA Mixer setting functions. * 38 * ALSA Mixer setting functions. *
39 ********************************************************************************/ 39 ********************************************************************************/
40 40
41 static snd_mixer_t *amixer = NULL;
42 static gboolean mixer_ready = FALSE;
43
44 static snd_mixer_elem_t *
45 alsaplug_get_mixer_elem_by_name(snd_mixer_t *mixer, gchar *name)
46 {
47 snd_mixer_selem_id_t *selem_id;
48 snd_mixer_elem_t *elem;
49
50 g_return_val_if_fail(mixer != NULL, NULL);
51 g_return_val_if_fail(name != NULL, NULL);
52
53 snd_mixer_selem_id_alloca(&selem_id);
54 snd_mixer_selem_id_set_name(selem_id, name);
55
56 elem = snd_mixer_find_selem(mixer, selem_id);
57 if (elem == NULL) {
58 _ERROR("Requested mixer element %p/%s not found", mixer, name);
59 return NULL;
60 }
61
62 snd_mixer_selem_set_playback_volume_range(elem, 0, 100);
63
64 return elem;
65 }
66
67 /* try to determine the best choice... may need tweaking. --nenolod */
68 static snd_mixer_elem_t *
69 alsaplug_guess_mixer_elem(snd_mixer_t *mixer)
70 {
71 gchar *elem_names[] = { "Wave", "PCM", "Front", "Master" };
72 gint i;
73 snd_mixer_elem_t *elem;
74
75 for (i = 0; i < G_N_ELEMENTS(elem_names); i++)
76 {
77 elem = alsaplug_get_mixer_elem_by_name(mixer, elem_names[i]);
78 if (elem != NULL)
79 return elem;
80 }
81
82 return NULL;
83 }
84
85 static gint
86 alsaplug_mixer_new(snd_mixer_t **mixer)
87 {
88 gint ret;
89
90 _ERROR("setting up mixer");
91
92 ret = snd_mixer_open(mixer, 0);
93 if (ret < 0)
94 {
95 _ERROR("mixer initialization failed: %s", snd_strerror(ret));
96 return ret;
97 }
98
99 ret = snd_mixer_attach(*mixer, "default");
100 if (ret < 0)
101 {
102 snd_mixer_close(*mixer);
103 _ERROR("failed to attach to hardware mixer: %s", snd_strerror(ret));
104 return ret;
105 }
106
107 ret = snd_mixer_selem_register(*mixer, NULL, NULL);
108 if (ret < 0)
109 {
110 snd_mixer_detach(*mixer, "default");
111 snd_mixer_close(*mixer);
112 _ERROR("failed to register hardware mixer: %s", snd_strerror(ret));
113 return ret;
114 }
115
116 ret = snd_mixer_load(*mixer);
117 if (ret < 0)
118 {
119 snd_mixer_detach(*mixer, "default");
120 snd_mixer_close(*mixer);
121 _ERROR("failed to load hardware mixer controls: %s", snd_strerror(ret));
122 return ret;
123 }
124
125 return 0;
126 }
127
128 static void
129 alsaplug_set_volume(gint l, gint r)
130 {
131 snd_mixer_elem_t *elem = alsaplug_guess_mixer_elem(amixer);
132
133 if (elem == NULL)
134 return;
135
136 if (snd_mixer_selem_is_playback_mono(elem))
137 {
138 gint vol = (l > r) ? l : r;
139
140 snd_mixer_selem_set_playback_volume(elem, SND_MIXER_SCHN_MONO, vol);
141
142 if (snd_mixer_selem_has_playback_switch(elem))
143 snd_mixer_selem_set_playback_switch(elem, SND_MIXER_SCHN_MONO, vol != 0);
144 }
145 else
146 {
147 snd_mixer_selem_set_playback_volume(elem, SND_MIXER_SCHN_FRONT_LEFT, l);
148 snd_mixer_selem_set_playback_volume(elem, SND_MIXER_SCHN_FRONT_RIGHT, r);
149
150 if (snd_mixer_selem_has_playback_switch(elem) && !snd_mixer_selem_has_playback_switch_joined(elem))
151 {
152 snd_mixer_selem_set_playback_switch(elem, SND_MIXER_SCHN_FRONT_LEFT, l != 0);
153 snd_mixer_selem_set_playback_switch(elem, SND_MIXER_SCHN_FRONT_RIGHT, r != 0);
154 }
155 }
156
157 snd_mixer_handle_events(amixer);
158 }
159
41 /******************************************************************************** 160 /********************************************************************************
42 * ALSA PCM I/O functions. * 161 * ALSA PCM I/O functions. *
43 ********************************************************************************/ 162 ********************************************************************************/
44 163
45 static void 164 static void
134 pcm_flush_cond = g_cond_new(); 253 pcm_flush_cond = g_cond_new();
135 254
136 if (snd_card_next(&card) != 0) 255 if (snd_card_next(&card) != 0)
137 return OUTPUT_PLUGIN_INIT_NO_DEVICES; 256 return OUTPUT_PLUGIN_INIT_NO_DEVICES;
138 257
258 if (!alsaplug_mixer_new(&amixer))
259 mixer_ready = TRUE;
260
139 return OUTPUT_PLUGIN_INIT_FOUND_DEVICES; 261 return OUTPUT_PLUGIN_INIT_FOUND_DEVICES;
140 } 262 }
141 263
142 static gint 264 static gint
143 alsaplug_open_audio(AFormat fmt, gint rate, gint nch) 265 alsaplug_open_audio(AFormat fmt, gint rate, gint nch)
200 } 322 }
201 323
202 static void 324 static void
203 alsaplug_write_audio(gpointer data, gint length) 325 alsaplug_write_audio(gpointer data, gint length)
204 { 326 {
327 /* software pause... snd_pcm_pause() is not safe. --nenolod */
205 if (paused) 328 if (paused)
206 { 329 {
207 g_mutex_lock(pcm_pause_mutex); 330 g_mutex_lock(pcm_pause_mutex);
208 g_cond_wait(pcm_pause_cond, pcm_pause_mutex); 331 g_cond_wait(pcm_pause_cond, pcm_pause_mutex);
209 g_mutex_unlock(pcm_pause_mutex); 332 g_mutex_unlock(pcm_pause_mutex);
330 .written_time = alsaplug_written_time, 453 .written_time = alsaplug_written_time,
331 .buffer_free = alsaplug_buffer_free, 454 .buffer_free = alsaplug_buffer_free,
332 .buffer_playing = alsaplug_buffer_playing, 455 .buffer_playing = alsaplug_buffer_playing,
333 .flush = alsaplug_flush, 456 .flush = alsaplug_flush,
334 .pause = alsaplug_pause, 457 .pause = alsaplug_pause,
458 .set_volume = alsaplug_set_volume,
335 }; 459 };
336 460
337 OutputPlugin *alsa_oplist[] = { &alsa_op, NULL }; 461 OutputPlugin *alsa_oplist[] = { &alsa_op, NULL };
338 SIMPLE_OUTPUT_PLUGIN(alsa, alsa_oplist); 462 SIMPLE_OUTPUT_PLUGIN(alsa, alsa_oplist);