12024
|
1 /*
|
|
2 The mediastreamer library aims at providing modular media processing and I/O
|
|
3 for linphone, but also for any telephony application.
|
|
4 Copyright (C) 2001 Simon MORLAT simon.morlat@linphone.org
|
|
5
|
|
6 This library is free software; you can redistribute it and/or
|
|
7 modify it under the terms of the GNU Lesser General Public
|
|
8 License as published by the Free Software Foundation; either
|
|
9 version 2.1 of the License, or (at your option) any later version.
|
|
10
|
|
11 This library is distributed in the hope that it will be useful,
|
|
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
14 Lesser General Public License for more details.
|
|
15
|
|
16 You should have received a copy of the GNU Lesser General Public
|
|
17 License along with this library; if not, write to the Free Software
|
|
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
19 */
|
|
20
|
|
21 #include "alsacard.h"
|
|
22
|
|
23 #ifdef HAVE_ALSA_ASOUNDLIB_H
|
|
24
|
|
25 static gchar *over_pcmdev=NULL;
|
|
26
|
|
27 #include "msossread.h"
|
|
28 #include "msosswrite.h"
|
|
29
|
|
30 #include <signal.h>
|
|
31
|
|
32 int __alsa_card_write(AlsaCard *obj,char *buf,int size);
|
|
33
|
|
34 int alsa_set_params(AlsaCard *obj, int rw, int bits, int stereo, int rate)
|
|
35 {
|
|
36 snd_pcm_hw_params_t *hwparams=NULL;
|
|
37 snd_pcm_sw_params_t *swparams=NULL;
|
|
38 snd_pcm_t *pcm_handle;
|
|
39 gint dir,exact_value;
|
|
40 gint channels;
|
|
41 gint fsize=0;
|
|
42 gint periods=8;
|
|
43 gint periodsize=256;
|
|
44 gint err;
|
|
45 int format;
|
|
46
|
|
47 if (rw) {
|
|
48 pcm_handle=obj->write_handle;
|
|
49 }
|
|
50 else pcm_handle=obj->read_handle;
|
|
51
|
|
52 /* Allocate the snd_pcm_hw_params_t structure on the stack. */
|
|
53 snd_pcm_hw_params_alloca(&hwparams);
|
|
54
|
|
55 /* Init hwparams with full configuration space */
|
|
56 if (snd_pcm_hw_params_any(pcm_handle, hwparams) < 0) {
|
|
57 g_warning("alsa_set_params: Cannot configure this PCM device.\n");
|
|
58 return(-1);
|
|
59 }
|
|
60
|
|
61 if (snd_pcm_hw_params_set_access(pcm_handle, hwparams, SND_PCM_ACCESS_RW_INTERLEAVED) < 0) {
|
|
62 g_warning("alsa_set_params: Error setting access.\n");
|
|
63 return(-1);
|
|
64 }
|
|
65 /* Set sample format */
|
|
66 #ifdef WORDS_BIGENDIAN
|
|
67 format=SND_PCM_FORMAT_S16_BE;
|
|
68 #else
|
|
69 format=SND_PCM_FORMAT_S16_LE;
|
|
70 #endif
|
|
71 if (snd_pcm_hw_params_set_format(pcm_handle, hwparams, format) < 0) {
|
|
72 g_warning("alsa_set_params: Error setting format.\n");
|
|
73 return(-1);
|
|
74 }
|
|
75 /* Set number of channels */
|
|
76 if (stereo) channels=2;
|
|
77 else channels=1;
|
|
78 if (snd_pcm_hw_params_set_channels(pcm_handle, hwparams, channels) < 0) {
|
|
79 g_warning("alsa_set_params: Error setting channels.\n");
|
|
80 return(-1);
|
|
81 }
|
|
82 /* Set sample rate. If the exact rate is not supported */
|
|
83 /* by the hardware, use nearest possible rate. */
|
|
84 exact_value=rate;
|
|
85 dir=0;
|
|
86 if ((err=snd_pcm_hw_params_set_rate_near(pcm_handle, hwparams, &exact_value, &dir))<0){
|
|
87 g_warning("alsa_set_params: Error setting rate to %i:%s",rate,snd_strerror(err));
|
|
88 return -1;
|
|
89 }
|
|
90 if (dir != 0) {
|
|
91 g_warning("alsa_set_params: The rate %d Hz is not supported by your hardware.\n "
|
|
92 "==> Using %d Hz instead.\n", rate, exact_value);
|
|
93 }
|
|
94 /* choose greater period size when rate is high */
|
|
95 periodsize=periodsize*(rate/8000);
|
|
96
|
|
97 /* Set buffer size (in frames). The resulting latency is given by */
|
|
98 /* latency = periodsize * periods / (rate * bytes_per_frame) */
|
|
99 /*
|
|
100 fsize=periodsize * periods;
|
|
101 exact_value=fsize;
|
|
102 if ((err=snd_pcm_hw_params_set_buffer_size_near(pcm_handle, hwparams,&exact_value)) < 0) {
|
|
103 g_warning("alsa_set_params: Error setting buffer size:%s",snd_strerror(err));
|
|
104 return(-1);
|
|
105 }
|
|
106 if (fsize!= exact_value) {
|
|
107 g_warning("alsa_set_params: The buffer size %d is not supported by your hardware.\n "
|
|
108 "==> Using %d instead.\n", fsize, exact_value);
|
|
109 }
|
|
110 */
|
|
111 /* set period size */
|
|
112 exact_value=periodsize;
|
|
113 dir=0;
|
|
114 if (snd_pcm_hw_params_set_period_size_near(pcm_handle, hwparams, &exact_value, &dir) < 0) {
|
|
115 g_warning("alsa_set_params: Error setting period size.\n");
|
|
116 return(-1);
|
|
117 }
|
|
118 if (dir != 0) {
|
|
119 g_warning("alsa_set_params: The period size %d is not supported by your hardware.\n "
|
|
120 "==> Using %d instead.\n", periodsize, exact_value);
|
|
121 }
|
|
122 periodsize=exact_value;
|
|
123 /* Set number of periods. Periods used to be called fragments. */
|
|
124 exact_value=periods;
|
|
125 dir=0;
|
|
126 if (snd_pcm_hw_params_set_periods_near(pcm_handle, hwparams, &exact_value, &dir) < 0) {
|
|
127 g_warning("alsa_set_params: Error setting periods.\n");
|
|
128 return(-1);
|
|
129 }
|
|
130 if (dir != 0) {
|
|
131 g_warning("alsa_set_params: The number of periods %d is not supported by your hardware.\n "
|
|
132 "==> Using %d instead.\n", periods, exact_value);
|
|
133 }
|
|
134 /* Apply HW parameter settings to */
|
|
135 /* PCM device and prepare device */
|
|
136 if ((err=snd_pcm_hw_params(pcm_handle, hwparams)) < 0) {
|
|
137 g_warning("alsa_set_params: Error setting HW params:%s",snd_strerror(err));
|
|
138 return(-1);
|
|
139 }
|
|
140 /*prepare sw params */
|
|
141 if (rw){
|
|
142 snd_pcm_sw_params_alloca(&swparams);
|
|
143 snd_pcm_sw_params_current(pcm_handle, swparams);
|
|
144 if ((err=snd_pcm_sw_params_set_start_threshold(pcm_handle, swparams,periodsize*2 ))<0){
|
|
145 g_warning("alsa_set_params: Error setting start threshold:%s",snd_strerror(err));
|
|
146 return -1;
|
|
147 }
|
|
148 if ((err=snd_pcm_sw_params(pcm_handle, swparams))<0){
|
|
149 g_warning("alsa_set_params: Error setting SW params:%s",snd_strerror(err));
|
|
150 return(-1);
|
|
151 }
|
|
152 }
|
|
153 obj->frame_size=channels*(bits/8);
|
|
154 SND_CARD(obj)->bsize=periodsize*obj->frame_size;
|
|
155 //SND_CARD(obj)->bsize=4096;
|
|
156 obj->frames=periodsize;
|
|
157 g_message("alsa_set_params: blocksize=%i.",SND_CARD(obj)->bsize);
|
|
158 return SND_CARD(obj)->bsize;
|
|
159 }
|
|
160
|
|
161 int alsa_card_open_r(AlsaCard *obj,int bits,int stereo,int rate)
|
|
162 {
|
|
163 int bsize;
|
|
164 int err;
|
|
165 snd_pcm_t *pcm_handle;
|
|
166 gchar *pcmdev;
|
|
167 if (over_pcmdev!=NULL) pcmdev=over_pcmdev;
|
|
168 else pcmdev=obj->pcmdev;
|
|
169
|
|
170 if (snd_pcm_open(&pcm_handle, pcmdev,SND_PCM_STREAM_CAPTURE,SND_PCM_NONBLOCK) < 0) {
|
|
171 g_warning("alsa_card_open_r: Error opening PCM device %s\n",obj->pcmdev );
|
|
172 return -1;
|
|
173 }
|
|
174 g_return_val_if_fail(pcm_handle!=NULL,-1);
|
|
175 obj->read_handle=pcm_handle;
|
|
176 if ((bsize=alsa_set_params(obj,0,bits,stereo,rate))<0){
|
|
177 snd_pcm_close(pcm_handle);
|
|
178 obj->read_handle=NULL;
|
|
179 return -1;
|
|
180 }
|
|
181 obj->readbuf=g_malloc0(bsize);
|
|
182
|
|
183 err=snd_pcm_start(obj->read_handle);
|
|
184 if (err<0){
|
|
185 g_warning("Cannot start read pcm: %s", snd_strerror(err));
|
|
186 }
|
|
187 obj->readpos=0;
|
|
188 SND_CARD(obj)->bsize=bsize;
|
|
189 SND_CARD(obj)->flags|=SND_CARD_FLAGS_OPENED;
|
|
190 return 0;
|
|
191 }
|
|
192
|
|
193 int alsa_card_open_w(AlsaCard *obj,int bits,int stereo,int rate)
|
|
194 {
|
|
195 int err,bsize;
|
|
196 snd_pcm_t *pcm_handle;
|
|
197 gchar *pcmdev;
|
|
198 if (over_pcmdev!=NULL) pcmdev=over_pcmdev;
|
|
199 else pcmdev=obj->pcmdev;
|
|
200
|
|
201 if (snd_pcm_open(&pcm_handle, pcmdev,SND_PCM_STREAM_PLAYBACK,SND_PCM_NONBLOCK) < 0) {
|
|
202 g_warning("alsa_card_open_w: Error opening PCM device %s\n", obj->pcmdev);
|
|
203 return -1;
|
|
204 }
|
|
205 obj->write_handle=pcm_handle;
|
|
206 if ((bsize=alsa_set_params(obj,1,bits,stereo,rate))<0){
|
|
207 snd_pcm_close(pcm_handle);
|
|
208 obj->write_handle=NULL;
|
|
209 return -1;
|
|
210 }
|
|
211 obj->writebuf=g_malloc0(bsize);
|
|
212
|
|
213 obj->writepos=0;
|
|
214 SND_CARD(obj)->bsize=bsize;
|
|
215 SND_CARD(obj)->flags|=SND_CARD_FLAGS_OPENED;
|
|
216 return 0;
|
|
217 }
|
|
218
|
|
219
|
|
220 void alsa_card_set_blocking_mode(AlsaCard *obj, gboolean yesno){
|
|
221 if (obj->read_handle!=NULL) snd_pcm_nonblock(obj->read_handle,!yesno);
|
|
222 if (obj->write_handle!=NULL) snd_pcm_nonblock(obj->write_handle,!yesno);
|
|
223 }
|
|
224
|
|
225 void alsa_card_close_r(AlsaCard *obj)
|
|
226 {
|
|
227 if (obj->read_handle!=NULL){
|
|
228 snd_pcm_close(obj->read_handle);
|
|
229 obj->read_handle=NULL;
|
|
230 g_free(obj->readbuf);
|
|
231 obj->readbuf=NULL;
|
|
232 }
|
|
233 }
|
|
234
|
|
235 void alsa_card_close_w(AlsaCard *obj)
|
|
236 {
|
|
237 if (obj->write_handle!=NULL){
|
|
238 snd_pcm_close(obj->write_handle);
|
|
239 obj->write_handle=NULL;
|
|
240 g_free(obj->writebuf);
|
|
241 obj->writebuf=NULL;
|
|
242 }
|
|
243 }
|
|
244
|
|
245 int alsa_card_probe(AlsaCard *obj,int bits,int stereo,int rate)
|
|
246 {
|
|
247 int ret;
|
|
248 ret=alsa_card_open_w(obj,bits,stereo,rate);
|
|
249 if (ret<0) return -1;
|
|
250 ret=SND_CARD(obj)->bsize;
|
|
251 alsa_card_close_w(obj);
|
|
252 return ret;
|
|
253 }
|
|
254
|
|
255
|
|
256 void alsa_card_destroy(AlsaCard *obj)
|
|
257 {
|
|
258 snd_card_uninit(SND_CARD(obj));
|
|
259 g_free(obj->pcmdev);
|
|
260 if (obj->readbuf!=0) g_free(obj->readbuf);
|
|
261 if (obj->writebuf!=0) g_free(obj->writebuf);
|
|
262 }
|
|
263
|
|
264 gboolean alsa_card_can_read(AlsaCard *obj)
|
|
265 {
|
|
266 int frames;
|
|
267 g_return_val_if_fail(obj->read_handle!=NULL,0);
|
|
268 if (obj->readpos!=0) return TRUE;
|
|
269 if ( frames=snd_pcm_avail_update(obj->read_handle)>=obj->frames) return 1;
|
|
270 //g_message("frames=%i",frames);
|
|
271 return 0;
|
|
272 }
|
|
273
|
|
274
|
|
275
|
|
276 int __alsa_card_read(AlsaCard *obj,char *buf,int bsize)
|
|
277 {
|
|
278 int err;
|
|
279 sigset_t set;
|
|
280 sigemptyset(&set);
|
|
281 sigaddset(&set,SIGALRM);
|
|
282 sigprocmask(SIG_BLOCK,&set,NULL);
|
|
283 err=snd_pcm_readi(obj->read_handle,buf,bsize/obj->frame_size);
|
|
284 if (err<0) {
|
|
285 if (err!=-EPIPE){
|
|
286 g_warning("alsa_card_read: snd_pcm_readi() failed:%s.",snd_strerror(err));
|
|
287 }
|
|
288 snd_pcm_prepare(obj->read_handle);
|
|
289 err=snd_pcm_readi(obj->read_handle,buf,bsize/obj->frame_size);
|
|
290 if (err<0) g_warning("alsa_card_read: snd_pcm_readi() failed:%s.",snd_strerror(err));
|
|
291 }
|
|
292 sigprocmask(SIG_UNBLOCK,&set,NULL);
|
|
293 return err*obj->frame_size;
|
|
294 }
|
|
295
|
|
296 int alsa_card_read(AlsaCard *obj,char *buf,int size)
|
|
297 {
|
|
298 int err;
|
|
299 gint bsize=SND_CARD(obj)->bsize;
|
|
300 g_return_val_if_fail(obj->read_handle!=NULL,-1);
|
|
301 if (size<bsize){
|
|
302 gint canread=MIN(bsize-obj->readpos,size);
|
|
303
|
|
304 if (obj->readpos==0){
|
|
305 err=__alsa_card_read(obj,obj->readbuf,bsize);
|
|
306 }
|
|
307
|
|
308 memcpy(buf,&obj->readbuf[obj->readpos],canread);
|
|
309 obj->readpos+=canread;
|
|
310 if (obj->readpos>=bsize) obj->readpos=0;
|
|
311 return canread;
|
|
312 }else{
|
|
313 err=__alsa_card_read(obj,buf,size);
|
|
314 return err;
|
|
315 }
|
|
316
|
|
317 }
|
|
318
|
|
319 int __alsa_card_write(AlsaCard *obj,char *buf,int size)
|
|
320 {
|
|
321 int err;
|
|
322 sigset_t set;
|
|
323 sigemptyset(&set);
|
|
324 sigaddset(&set,SIGALRM);
|
|
325 sigprocmask(SIG_BLOCK,&set,NULL);
|
|
326 if ((err=snd_pcm_writei(obj->write_handle,buf,size/obj->frame_size))<0){
|
|
327 if (err!=-EPIPE){
|
|
328 g_warning("alsa_card_write: snd_pcm_writei() failed:%s.",snd_strerror(err));
|
|
329 }
|
|
330 snd_pcm_prepare(obj->write_handle);
|
|
331 err=snd_pcm_writei(obj->write_handle,buf,size/obj->frame_size);
|
|
332 if (err<0) g_warning("alsa_card_write: Error writing sound buffer (size=%i):%s",size,snd_strerror(err));
|
|
333
|
|
334 }
|
|
335 sigprocmask(SIG_UNBLOCK,&set,NULL);
|
|
336 return err;
|
|
337 }
|
|
338
|
|
339 int alsa_card_write(AlsaCard *obj,char *buf,int size)
|
|
340 {
|
|
341 int err;
|
|
342 gint bsize=SND_CARD(obj)->bsize;
|
|
343 g_return_val_if_fail(obj->write_handle!=NULL,-1);
|
|
344 if (size<bsize){
|
|
345 gint canwrite;
|
|
346
|
|
347 canwrite=MIN(bsize-obj->writepos,size);
|
|
348 memcpy(&obj->writebuf[obj->writepos],buf,canwrite);
|
|
349 obj->writepos+=canwrite;
|
|
350 if (obj->writepos>=bsize){
|
|
351 err=__alsa_card_write(obj,obj->writebuf,bsize);
|
|
352 obj->writepos=0;
|
|
353 }
|
|
354 return canwrite;
|
|
355 }else{
|
|
356 return __alsa_card_write(obj,buf,bsize);
|
|
357 }
|
|
358 }
|
|
359
|
|
360 snd_mixer_t *alsa_mixer_open(AlsaCard *obj){
|
|
361 snd_mixer_t *mixer=NULL;
|
|
362 int err;
|
|
363 err=snd_mixer_open(&mixer,0);
|
|
364 if (err<0){
|
|
365 g_warning("Could not open alsa mixer: %s",snd_strerror(err));
|
|
366 return NULL;
|
|
367 }
|
|
368 if ((err = snd_mixer_attach (mixer, obj->mixdev)) < 0){
|
|
369 g_warning("Could not attach mixer to card: %s",snd_strerror(err));
|
|
370 snd_mixer_close(mixer);
|
|
371 return NULL;
|
|
372 }
|
|
373 if ((err = snd_mixer_selem_register (mixer, NULL, NULL)) < 0){
|
|
374 g_warning("snd_mixer_selem_register: %s",snd_strerror(err));
|
|
375 snd_mixer_close(mixer);
|
|
376 return NULL;
|
|
377 }
|
|
378 if ((err = snd_mixer_load (mixer)) < 0){
|
|
379 g_warning("snd_mixer_load: %s",snd_strerror(err));
|
|
380 snd_mixer_close(mixer);
|
|
381 return NULL;
|
|
382 }
|
|
383 obj->mixer=mixer;
|
|
384 return mixer;
|
|
385 }
|
|
386
|
|
387 void alsa_mixer_close(AlsaCard *obj){
|
|
388 snd_mixer_close(obj->mixer);
|
|
389 obj->mixer=NULL;
|
|
390 }
|
|
391
|
|
392 typedef enum {CAPTURE, PLAYBACK, CAPTURE_SWITCH, PLAYBACK_SWITCH} MixerAction;
|
|
393
|
|
394 static gint get_mixer_element(snd_mixer_t *mixer,const char *name, MixerAction action){
|
|
395 long value=0;
|
|
396 const char *elemname;
|
|
397 snd_mixer_elem_t *elem;
|
|
398 int err;
|
|
399 long sndMixerPMin;
|
|
400 long sndMixerPMax;
|
|
401 long newvol;
|
|
402 elem=snd_mixer_first_elem(mixer);
|
|
403 while (elem!=NULL){
|
|
404 elemname=snd_mixer_selem_get_name(elem);
|
|
405 //g_message("Found alsa mixer element %s.",elemname);
|
|
406 if (strcmp(elemname,name)==0){
|
|
407 switch (action){
|
|
408 case CAPTURE:
|
|
409 if (snd_mixer_selem_has_capture_volume(elem)){
|
|
410 snd_mixer_selem_get_playback_volume_range(elem, &sndMixerPMin, &sndMixerPMax);
|
|
411 err=snd_mixer_selem_get_capture_volume(elem,SND_MIXER_SCHN_UNKNOWN,&newvol);
|
|
412 newvol-=sndMixerPMin;
|
|
413 value=(100*newvol)/(sndMixerPMax-sndMixerPMin);
|
|
414 if (err<0) g_warning("Could not get capture volume for %s:%s",name,snd_strerror(err));
|
|
415 //else g_message("Succesfully get capture level for %s.",elemname);
|
|
416 break;
|
|
417 }
|
|
418 break;
|
|
419 case PLAYBACK:
|
|
420 if (snd_mixer_selem_has_playback_volume(elem)){
|
|
421 snd_mixer_selem_get_playback_volume_range(elem, &sndMixerPMin, &sndMixerPMax);
|
|
422 err=snd_mixer_selem_get_playback_volume(elem,SND_MIXER_SCHN_FRONT_LEFT,&newvol);
|
|
423 newvol-=sndMixerPMin;
|
|
424 value=(100*newvol)/(sndMixerPMax-sndMixerPMin);
|
|
425 if (err<0) g_warning("Could not get playback volume for %s:%s",name,snd_strerror(err));
|
|
426 //else g_message("Succesfully get playback level for %s.",elemname);
|
|
427 break;
|
|
428 }
|
|
429 break;
|
|
430 case CAPTURE_SWITCH:
|
|
431
|
|
432 break;
|
|
433 }
|
|
434 }
|
|
435 elem=snd_mixer_elem_next(elem);
|
|
436 }
|
|
437
|
|
438 return value;
|
|
439 }
|
|
440
|
|
441
|
|
442 static void set_mixer_element(snd_mixer_t *mixer,const char *name, gint level,MixerAction action){
|
|
443 const char *elemname;
|
|
444 snd_mixer_elem_t *elem;
|
|
445 int tmp;
|
|
446 long sndMixerPMin;
|
|
447 long sndMixerPMax;
|
|
448 long newvol;
|
|
449
|
|
450 elem=snd_mixer_first_elem(mixer);
|
|
451
|
|
452 while (elem!=NULL){
|
|
453 elemname=snd_mixer_selem_get_name(elem);
|
|
454 //g_message("Found alsa mixer element %s.",elemname);
|
|
455 if (strcmp(elemname,name)==0){
|
|
456 switch(action){
|
|
457 case CAPTURE:
|
|
458 if (snd_mixer_selem_has_capture_volume(elem)){
|
|
459 snd_mixer_selem_get_playback_volume_range(elem, &sndMixerPMin, &sndMixerPMax);
|
|
460 newvol=(((sndMixerPMax-sndMixerPMin)*level)/100)+sndMixerPMin;
|
|
461 snd_mixer_selem_set_capture_volume_all(elem,newvol);
|
|
462 //g_message("Succesfully set capture level for %s.",elemname);
|
|
463 return;
|
|
464 }
|
|
465 break;
|
|
466 case PLAYBACK:
|
|
467 if (snd_mixer_selem_has_playback_volume(elem)){
|
|
468 snd_mixer_selem_get_playback_volume_range(elem, &sndMixerPMin, &sndMixerPMax);
|
|
469 newvol=(((sndMixerPMax-sndMixerPMin)*level)/100)+sndMixerPMin;
|
|
470 snd_mixer_selem_set_playback_volume_all(elem,newvol);
|
|
471 //g_message("Succesfully set playback level for %s.",elemname);
|
|
472 return;
|
|
473 }
|
|
474 break;
|
|
475 case CAPTURE_SWITCH:
|
|
476 if (snd_mixer_selem_has_capture_switch(elem)){
|
|
477 snd_mixer_selem_set_capture_switch_all(elem,level);
|
|
478 //g_message("Succesfully set capture switch for %s.",elemname);
|
|
479 }
|
|
480 break;
|
|
481 case PLAYBACK_SWITCH:
|
|
482 if (snd_mixer_selem_has_playback_switch(elem)){
|
|
483 snd_mixer_selem_set_playback_switch_all(elem,level);
|
|
484 //g_message("Succesfully set capture switch for %s.",elemname);
|
|
485 }
|
|
486 break;
|
|
487
|
|
488 }
|
|
489 }
|
|
490 elem=snd_mixer_elem_next(elem);
|
|
491 }
|
|
492
|
|
493 return ;
|
|
494 }
|
|
495
|
|
496
|
|
497 void alsa_card_set_level(AlsaCard *obj,gint way,gint a)
|
|
498 {
|
|
499 snd_mixer_t *mixer;
|
|
500 mixer=alsa_mixer_open(obj);
|
|
501 if (mixer==NULL) return ;
|
|
502 switch(way){
|
|
503 case SND_CARD_LEVEL_GENERAL:
|
|
504 set_mixer_element(mixer,"Master",a,PLAYBACK);
|
|
505 break;
|
|
506 case SND_CARD_LEVEL_INPUT:
|
|
507 set_mixer_element(mixer,"Capture",a,CAPTURE);
|
|
508 break;
|
|
509 case SND_CARD_LEVEL_OUTPUT:
|
|
510 set_mixer_element(mixer,"PCM",a,PLAYBACK);
|
|
511 break;
|
|
512 default:
|
|
513 g_warning("oss_card_set_level: unsupported command.");
|
|
514 }
|
|
515 alsa_mixer_close(obj);
|
|
516 }
|
|
517
|
|
518 gint alsa_card_get_level(AlsaCard *obj,gint way)
|
|
519 {
|
|
520 snd_mixer_t *mixer;
|
|
521 gint value;
|
|
522 mixer=alsa_mixer_open(obj);
|
|
523 if (mixer==NULL) return 0;
|
|
524 switch(way){
|
|
525 case SND_CARD_LEVEL_GENERAL:
|
|
526 value=get_mixer_element(mixer,"Master",PLAYBACK);
|
|
527 break;
|
|
528 case SND_CARD_LEVEL_INPUT:
|
|
529 value=get_mixer_element(mixer,"Capture",CAPTURE);
|
|
530 break;
|
|
531 case SND_CARD_LEVEL_OUTPUT:
|
|
532 value=get_mixer_element(mixer,"PCM",PLAYBACK);
|
|
533 break;
|
|
534 default:
|
|
535 g_warning("oss_card_set_level: unsupported command.");
|
|
536 }
|
|
537 alsa_mixer_close(obj);
|
|
538 return value;
|
|
539 }
|
|
540
|
|
541 void alsa_card_set_source(AlsaCard *obj,int source)
|
|
542 {
|
|
543 snd_mixer_t *mixer;
|
|
544 mixer=alsa_mixer_open(obj);
|
|
545 if (mixer==NULL) return;
|
|
546 switch (source){
|
|
547 case 'm':
|
|
548 set_mixer_element(mixer,"Mic",1,CAPTURE_SWITCH);
|
|
549 set_mixer_element(mixer,"Capture",1,CAPTURE_SWITCH);
|
|
550 break;
|
|
551 case 'l':
|
|
552 set_mixer_element(mixer,"Line",1,CAPTURE_SWITCH);
|
|
553 set_mixer_element(mixer,"Capture",1,CAPTURE_SWITCH);
|
|
554 break;
|
|
555 }
|
|
556 }
|
|
557
|
|
558 MSFilter *alsa_card_create_read_filter(AlsaCard *card)
|
|
559 {
|
|
560 MSFilter *f=ms_oss_read_new();
|
|
561 ms_oss_read_set_device(MS_OSS_READ(f),SND_CARD(card)->index);
|
|
562 return f;
|
|
563 }
|
|
564
|
|
565 MSFilter *alsa_card_create_write_filter(AlsaCard *card)
|
|
566 {
|
|
567 MSFilter *f=ms_oss_write_new();
|
|
568 ms_oss_write_set_device(MS_OSS_WRITE(f),SND_CARD(card)->index);
|
|
569 return f;
|
|
570 }
|
|
571
|
|
572
|
|
573 SndCard * alsa_card_new(gint devid)
|
|
574 {
|
|
575 AlsaCard * obj;
|
|
576 SndCard *base;
|
|
577 int err;
|
|
578 gchar *name=NULL;
|
|
579
|
|
580 /* carefull: this is an alsalib call despite its name! */
|
|
581 err=snd_card_get_name(devid,&name);
|
|
582 if (err<0) {
|
|
583 return NULL;
|
|
584 }
|
|
585 obj= g_new0(AlsaCard,1);
|
|
586 base= SND_CARD(obj);
|
|
587 snd_card_init(base);
|
|
588
|
|
589 base->card_name=g_strdup_printf("%s (Advanced Linux Sound Architecture)",name);
|
|
590 base->_probe=(SndCardOpenFunc)alsa_card_probe;
|
|
591 base->_open_r=(SndCardOpenFunc)alsa_card_open_r;
|
|
592 base->_open_w=(SndCardOpenFunc)alsa_card_open_w;
|
|
593 base->_can_read=(SndCardPollFunc)alsa_card_can_read;
|
|
594 base->_set_blocking_mode=(SndCardSetBlockingModeFunc)alsa_card_set_blocking_mode;
|
|
595 base->_read=(SndCardIOFunc)alsa_card_read;
|
|
596 base->_write=(SndCardIOFunc)alsa_card_write;
|
|
597 base->_close_r=(SndCardCloseFunc)alsa_card_close_r;
|
|
598 base->_close_w=(SndCardCloseFunc)alsa_card_close_w;
|
|
599 base->_set_rec_source=(SndCardMixerSetRecSourceFunc)alsa_card_set_source;
|
|
600 base->_set_level=(SndCardMixerSetLevelFunc)alsa_card_set_level;
|
|
601 base->_get_level=(SndCardMixerGetLevelFunc)alsa_card_get_level;
|
|
602 base->_destroy=(SndCardDestroyFunc)alsa_card_destroy;
|
|
603 base->_create_read_filter=(SndCardCreateFilterFunc)alsa_card_create_read_filter;
|
|
604 base->_create_write_filter=(SndCardCreateFilterFunc)alsa_card_create_write_filter;
|
|
605
|
|
606
|
|
607 obj->pcmdev=g_strdup_printf("plughw:%i,0",devid);
|
|
608 obj->mixdev=g_strdup_printf("hw:%i",devid);
|
|
609 obj->readbuf=NULL;
|
|
610 obj->writebuf=NULL;
|
|
611 return base;
|
|
612 }
|
|
613
|
|
614
|
|
615 gint alsa_card_manager_init(SndCardManager *m, gint index)
|
|
616 {
|
|
617 gint devindex;
|
|
618 gint i;
|
|
619 gint found=0;
|
|
620 gchar *name=NULL;
|
|
621 for(devindex=0;index<MAX_SND_CARDS && devindex<MAX_SND_CARDS ;devindex++){
|
|
622 if (snd_card_get_name(devindex,&name)==0){
|
|
623 g_message("Found ALSA device: %s",name);
|
|
624 m->cards[index]=alsa_card_new(devindex);
|
|
625 m->cards[index]->index=index;
|
|
626 found++;
|
|
627 index++;
|
|
628 }
|
|
629 }
|
|
630 return found;
|
|
631 }
|
|
632
|
|
633 void alsa_card_manager_set_default_pcm_device(const gchar *pcmdev){
|
|
634 if (over_pcmdev!=NULL){
|
|
635 g_free(over_pcmdev);
|
|
636 }
|
|
637 over_pcmdev=g_strdup(pcmdev);
|
|
638 }
|
|
639
|
|
640 #endif
|