Debian Codegraph @

src/libout123/modules/tinyalsa.c

raw
1 /*
2 tinyalsa: sound output with TINY Advanced Linux Sound Architecture
3
4 copyright 2006-8 by the mpg123 project - free software under the terms of the LGPL 2.1
5 see COPYING and AUTHORS files in distribution or http://mpg123.org
6
7 initially written by Jarno Lehtinen <[email protected]>
8 */
9
10 #include "../out123_int.h"
11 #include <errno.h>
12
13 #include <tinyalsa/asoundlib.h>
14
15 #include "../../common/debug.h"
16
17
18 typedef struct
19 {
20 struct pcm *pcm;
21 struct pcm_params *params;
22 struct pcm_config config;
23
24 unsigned int device;
25 unsigned int card;
26 } mpg123_tinyalsa_t;
27
28
29 static int initialize_device(out123_handle *ao)
30 {
31 mpg123_tinyalsa_t* ta = (mpg123_tinyalsa_t*)ao->userptr;
32
33 ta->config.channels = ao->channels;
34 ta->config.rate = ao->rate;
35 ta->config.period_size = 1024;
36 ta->config.period_count = 4;
37 ta->config.format = PCM_FORMAT_S16_LE;
38 ta->config.start_threshold = 0;
39 ta->config.stop_threshold = 0;
40 ta->config.silence_threshold = 0;
41
42 ta->pcm = pcm_open(ta->card, ta->device, PCM_OUT, &ta->config);
43 if (!ta->pcm || !pcm_is_ready(ta->pcm))
44 {
45 if(!AOQUIET)
46 error3( "(open) Unable to open card %u PCM device %u (%s)\n"
47 , ta->card, ta->device, pcm_get_error(ta->pcm) );
48 return -1;
49 }
50
51 return 0;
52 }
53
54
55 static int open_tinyalsa(out123_handle *ao)
56 {
57 debug("open_tinyalsa()");
58
59 mpg123_tinyalsa_t* ta = (mpg123_tinyalsa_t*)ao->userptr;
60
61 if (ao->format != -1)
62 {
63 /* we're going to play: initalize sample format */
64 return initialize_device(ao);
65 }
66 else
67 {
68 /* query mode; sample format will be set for each query */
69 return 0;
70 }
71 }
72
73
74 static int get_formats_tinyalsa(out123_handle *ao)
75 {
76 debug("get_formats_tinyalsa()");
77
78 mpg123_tinyalsa_t* ta = (mpg123_tinyalsa_t*)ao->userptr;
79
80 if ( ao->rate >= pcm_params_get_min(ta->params, PCM_PARAM_RATE) && ao->rate <= pcm_params_get_max(ta->params, PCM_PARAM_RATE) && ao->channels >= pcm_params_get_min(ta->params, PCM_PARAM_CHANNELS) && ao->channels <= pcm_params_get_max(ta->params, PCM_PARAM_CHANNELS) )
81 {
82 return MPG123_ENC_SIGNED_16;
83 }
84 else
85 {
86 return 0;
87 }
88 }
89
90
91 static int write_tinyalsa(out123_handle *ao, unsigned char *buf, int bytes)
92 {
93 mpg123_tinyalsa_t* ta = (mpg123_tinyalsa_t*)ao->userptr;
94
95 if (ta->pcm)
96 {
97 if(pcm_write(ta->pcm, buf, bytes))
98 {
99 if(!AOQUIET)
100 error("Error playing sample\n");
101 return -1;
102 }
103 }
104 return bytes;
105 }
106
107
108 static void flush_tinyalsa(out123_handle *ao)
109 {
110 debug("flush_tinyalsa()");
111 }
112
113
114 static int close_tinyalsa(out123_handle *ao)
115 {
116 debug("close_tinyalsa()");
117
118 mpg123_tinyalsa_t* ta = (mpg123_tinyalsa_t*)ao->userptr;
119
120 if (ta->pcm)
121 {
122 pcm_close(ta->pcm);
123 }
124
125 return 0;
126 }
127
128
129 static void deinit_tinyalsa(out123_handle* ao)
130 {
131 mpg123_tinyalsa_t* ta = (mpg123_tinyalsa_t*)ao->userptr;
132
133 /* Free up card/device parameters */
134 pcm_params_free(ta->params);
135
136 /* Free up memory */
137 if(ao->userptr)
138 {
139 free( ao->userptr );
140 ao->userptr = NULL;
141 }
142 }
143
144
145 static int init_tinyalsa(out123_handle* ao)
146 {
147 if (ao==NULL) return -1;
148
149 /* Set callbacks */
150 ao->open = open_tinyalsa;
151 ao->flush = flush_tinyalsa;
152 ao->write = write_tinyalsa;
153 ao->get_formats = get_formats_tinyalsa;
154 ao->close = close_tinyalsa;
155 ao->deinit = deinit_tinyalsa;
156
157 /* Allocate memory for data structure */
158 ao->userptr = malloc( sizeof( mpg123_tinyalsa_t ) );
159 if(ao->userptr==NULL)
160 {
161 if(!AOQUIET)
162 error("failed to malloc memory for 'mpg123_tinyalsa_t'");
163 return -1;
164 }
165 memset( ao->userptr, 0, sizeof(mpg123_tinyalsa_t) );
166
167 /* Set card and device */
168 mpg123_tinyalsa_t* ta = (mpg123_tinyalsa_t*)ao->userptr;
169
170 ta->card = 0;
171 ta->device = 0;
172
173 if (ao->device)
174 {
175 char *ptr = ao->device;
176 ta->card = (unsigned int)strtol(ptr, &ptr, 10);
177 if (strlen(ptr) > 0)
178 {
179 ta->device = (unsigned int)strtol(++ptr, &ptr, 10);
180 }
181 }
182
183 /* Get card/device parameters */
184 ta->params = pcm_params_get(ta->card, ta->device, PCM_OUT);
185 if (ta->params == NULL)
186 {
187 if(!AOQUIET)
188 error2( "(params) Unable to open card %u PCM device %u.\n"
189 , ta->card, ta->device );
190 return -1;
191 }
192
193 /* Success */
194 return 0;
195 }
196
197
198 /*
199 Module information data structure
200 */
201 mpg123_module_t mpg123_output_module_info = {
202 /* api_version */ MPG123_MODULE_API_VERSION,
203 /* name */ "tinyalsa",
204 /* description */ "Output audio using TINY Advanced Linux Sound Architecture (TINYALSA).",
205 /* revision */ "$Rev:$",
206 /* handle */ NULL,
207
208 /* init_output */ init_tinyalsa,
209 };
210