]>
Commit | Line | Data |
---|---|---|
158879ad | 1 | --- flite-1.2-release/src/audio/au_alsa.c.alsa 2001-12-04 21:05:52 +0300 |
2 | +++ flite-1.2-release/src/audio/au_alsa.c 2005-10-31 14:53:35 +0300 | |
3 | @@ -46,98 +46,117 @@ | |
4 | #include "cst_wave.h" | |
5 | #include "cst_audio.h" | |
6 | ||
7 | -#include <sys/asoundlib.h> | |
8 | +#include <alsa/asoundlib.h> | |
9 | ||
10 | #include <sys/stat.h> | |
11 | #include <fcntl.h> | |
12 | ||
13 | -static int alsa_card = 0, alsa_device = 0; | |
14 | +#ifndef CST_AUDIO_ALSA_DEVICE | |
15 | +#define CST_AUDIO_ALSA_DEVICE "default" | |
16 | +#endif | |
17 | + | |
18 | +#ifdef CST_AUDIO_ALSA_DEBUG | |
19 | +static snd_output_t *sndout; | |
20 | +#endif | |
21 | + | |
22 | +static int framebits; | |
23 | ||
24 | cst_audiodev *audio_open_alsa(int sps, int channels, cst_audiofmt fmt) | |
25 | { | |
26 | - snd_pcm_channel_info_t pinfo; | |
27 | - snd_pcm_channel_params_t params; | |
28 | - snd_pcm_channel_setup_t setup; | |
29 | snd_pcm_t *pcm; | |
30 | cst_audiodev *ad; | |
31 | + snd_pcm_hw_params_t *params; | |
32 | + snd_pcm_format_t format = SND_PCM_FORMAT_S16; | |
33 | + snd_pcm_access_t access = SND_PCM_ACCESS_RW_INTERLEAVED; | |
34 | + unsigned int buffer_time = 500000; /* 0.5 sec */ | |
35 | + unsigned int period_time = 100000; /* 0.1 sec */ | |
36 | + unsigned int rate = sps; | |
37 | int err; | |
38 | ||
39 | -#ifdef __QNXNTO__ | |
40 | - if (snd_pcm_open_preferred(&pcm,&alsa_card,&alsa_device,SND_PCM_OPEN_PLAYBACK) < 0) | |
41 | - { | |
42 | - cst_errmsg("alsa_audio: failed to open audio device\n"); | |
43 | - cst_error(); | |
44 | - } | |
45 | - if (snd_pcm_plugin_set_disable(pcm,PLUGIN_DISABLE_MMAP) < 0) | |
46 | - { | |
47 | - cst_errmsg("alsa_audio: failed to disable mmap\n"); | |
48 | - snd_pcm_close(pcm); | |
49 | - cst_error(); | |
50 | - } | |
51 | -#else | |
52 | - if (snd_pcm_open(&pcm,alsa_card,alsa_device,SND_PCM_OPEN_PLAYBACK) < 0) | |
53 | - { | |
54 | - cst_errmsg("alsa_audio: failed to open audio device\n"); | |
55 | - cst_error(); | |
56 | - } | |
57 | -#endif | |
58 | - | |
59 | - | |
60 | - memset(&pinfo, 0, sizeof(pinfo)); | |
61 | - memset(¶ms, 0, sizeof(params)); | |
62 | - memset(&setup, 0, sizeof(setup)); | |
63 | - | |
64 | - pinfo.channel = SND_PCM_CHANNEL_PLAYBACK; | |
65 | - snd_pcm_plugin_info(pcm,&pinfo); | |
66 | - | |
67 | - params.mode = SND_PCM_MODE_BLOCK; | |
68 | - params.channel = SND_PCM_CHANNEL_PLAYBACK; | |
69 | - params.start_mode = SND_PCM_START_DATA; | |
70 | - params.stop_mode = SND_PCM_STOP_STOP; | |
71 | - | |
72 | - params.buf.block.frag_size = pinfo.max_fragment_size; | |
73 | - params.buf.block.frags_max = 1; | |
74 | - params.buf.block.frags_min = 1; | |
75 | - | |
76 | - params.format.interleave = 1; | |
77 | - params.format.rate = sps; | |
78 | - params.format.voices = channels; | |
79 | - | |
80 | - switch (fmt) | |
81 | - { | |
82 | + switch (fmt) { | |
83 | case CST_AUDIO_LINEAR16: | |
84 | - if (CST_LITTLE_ENDIAN) | |
85 | - params.format.format = SND_PCM_SFMT_S16_LE; | |
86 | - else | |
87 | - params.format.format = SND_PCM_SFMT_S16_BE; | |
88 | - break; | |
89 | + if (CST_LITTLE_ENDIAN) | |
90 | + format = SND_PCM_FORMAT_S16_LE; | |
91 | + else | |
92 | + format = SND_PCM_FORMAT_S16_BE; | |
93 | + break; | |
94 | case CST_AUDIO_LINEAR8: | |
95 | - params.format.format = SND_PCM_SFMT_U8; | |
96 | - break; | |
97 | + format = SND_PCM_FORMAT_U8; | |
98 | + break; | |
99 | case CST_AUDIO_MULAW: | |
100 | - params.format.format = SND_PCM_SFMT_MU_LAW; | |
101 | - break; | |
102 | + format = SND_PCM_FORMAT_MU_LAW; | |
103 | + break; | |
104 | + default: | |
105 | + cst_errmsg("alsa_audio: unexpected format requested\n"); | |
106 | } | |
107 | ||
108 | - if((err = snd_pcm_plugin_params(pcm,¶ms)) < 0) | |
109 | - { | |
110 | - cst_errmsg("alsa_audio params setting failed: %s\n",snd_strerror(err)); | |
111 | - snd_pcm_close(pcm); | |
112 | - cst_error(); | |
113 | - } | |
114 | - if((err = snd_pcm_plugin_setup(pcm,SND_PCM_CHANNEL_PLAYBACK)) > 0) { | |
115 | - cst_errmsg("alsa_audio sound prepare setting failed: %s\n",snd_strerror(err)); | |
116 | - snd_pcm_close(pcm); | |
117 | - cst_error(); | |
118 | - } | |
119 | - if((err = snd_pcm_plugin_prepare(pcm,SND_PCM_CHANNEL_PLAYBACK)) > 0) { | |
120 | - cst_errmsg("alsa_audio sound prepare setting failed: %s\n",snd_strerror(err)); | |
121 | - snd_pcm_close(pcm); | |
122 | - cst_error(); | |
123 | - } | |
124 | +#ifdef CST_AUDIO_ALSA_DEBUG | |
125 | + if (0 > (err = snd_output_stdio_attach(&sndout, stderr, 0))) { | |
126 | + cst_errmsg("alsa_audio: failed to open log\n"); | |
127 | + cst_error(); | |
128 | + return NULL; | |
129 | + } | |
130 | +#endif | |
131 | ||
132 | - pinfo.channel = SND_PCM_CHANNEL_PLAYBACK; | |
133 | - snd_pcm_plugin_info(pcm,&pinfo); | |
134 | + if (0 > (err = snd_pcm_open(&pcm, CST_AUDIO_ALSA_DEVICE, SND_PCM_STREAM_PLAYBACK, 0))) { | |
135 | + cst_errmsg("alsa_audio: failed to open audio device: %s\n", snd_strerror(err)); | |
136 | + cst_error(); | |
137 | + return NULL; | |
138 | + } | |
139 | + | |
140 | + snd_pcm_hw_params_alloca(¶ms); | |
141 | + | |
142 | + if ((err = snd_pcm_hw_params_any(pcm, params)) < 0) { | |
143 | + cst_errmsg("alsa_audio: failed to get configuration: %s\n", snd_strerror(err)); | |
144 | + goto bailout; | |
145 | + } | |
146 | + | |
147 | + if ((err = snd_pcm_hw_params_set_access(pcm, params, access)) < 0) { | |
148 | + cst_errmsg("alsa_audio: failed to set access type: %s\n", snd_strerror(err)); | |
149 | + goto bailout; | |
150 | + } | |
151 | + | |
152 | + if (0 > (err = snd_pcm_hw_params_set_format (pcm, params, format))) { | |
153 | + cst_errmsg("alsa_audio: failed to set sample format: %s\n", snd_strerror(err)); | |
154 | + goto bailout; | |
155 | + } | |
156 | + | |
157 | + if (0 > (err = snd_pcm_hw_params_set_channels(pcm, params, channels))) { | |
158 | + cst_errmsg("alsa_audio: failed to set channels count %d: %s\n", channels, snd_strerror(err)); | |
159 | + goto bailout; | |
160 | + } | |
161 | + | |
162 | + if (0 > (err = snd_pcm_hw_params_set_rate(pcm, params, rate, 0))) { | |
163 | + cst_errmsg("alsa_audio: failed to set sample rate: %s\n", snd_strerror(err)); | |
164 | + goto bailout; | |
165 | + } | |
166 | + | |
167 | + if (0 > (err = snd_pcm_hw_params_set_buffer_time_near(pcm, params, &buffer_time, NULL))) { | |
168 | + cst_errmsg("alsa_audio: failed to set buffer time: %s\n", snd_strerror(err)); | |
169 | + goto bailout; | |
170 | + } | |
171 | + | |
172 | + if (0 > (err = snd_pcm_hw_params_set_period_time_near(pcm, params, &period_time, NULL))) { | |
173 | + cst_errmsg("alsa_audio: failed to set period time: %s\n", snd_strerror(err)); | |
174 | + goto bailout; | |
175 | + } | |
176 | + | |
177 | + if (0 > (err = snd_pcm_hw_params(pcm, params))) { | |
178 | + cst_errmsg("alsa_audio: failed to set hw params: %s\n", snd_strerror(err)); | |
179 | + goto bailout; | |
180 | + } | |
181 | + | |
182 | + /* FIXME */ | |
183 | + if (0 > (err = snd_pcm_hw_params_get_sbits(params))) { | |
184 | + cst_errmsg("alsa_audio: failed to get sbits: %s\n", snd_strerror(err)); | |
185 | + goto bailout; | |
186 | + } | |
187 | + | |
188 | + framebits = err * channels; | |
189 | + | |
190 | +#ifdef CST_AUDIO_ALSA_DEBUG | |
191 | + snd_pcm_hw_params_dump(params, sndout); | |
192 | +#endif | |
193 | ||
194 | ad = cst_alloc(cst_audiodev, 1); | |
195 | ad->platform_data = pcm; | |
196 | @@ -146,6 +165,16 @@ | |
197 | ad->fmt = ad->real_fmt = fmt; | |
198 | ||
199 | return ad; | |
200 | + | |
201 | + bailout: | |
202 | + | |
203 | +#ifdef CST_AUDIO_ALSA_DEBUG | |
204 | + snd_pcm_hw_params_dump(params, sndout); | |
205 | +#endif | |
206 | + snd_pcm_close(pcm); | |
207 | + cst_error(); | |
208 | + | |
209 | + return NULL; | |
210 | } | |
211 | ||
212 | int audio_close_alsa(cst_audiodev *ad) | |
213 | @@ -156,7 +185,7 @@ | |
214 | return 0; | |
215 | ||
216 | pcm = ad->platform_data; | |
217 | - snd_pcm_plugin_flush(pcm,0); | |
218 | + snd_pcm_drop(pcm); | |
219 | snd_pcm_close(pcm); | |
220 | cst_free(ad); | |
221 | ||
222 | @@ -166,21 +195,35 @@ | |
223 | int audio_write_alsa(cst_audiodev *ad, void *samples, int num_bytes) | |
224 | { | |
225 | snd_pcm_t *pcm = ad->platform_data; | |
226 | + snd_pcm_uframes_t nframes = (num_bytes << 3) / framebits; | |
227 | + int err; | |
228 | ||
229 | - return snd_pcm_plugin_write(pcm,samples,num_bytes); | |
230 | + while (nframes > 0) { | |
231 | + if (0 > (err = snd_pcm_writei(pcm, samples, nframes))) { | |
232 | + if (err == -EAGAIN) continue; | |
233 | + if (err == -EPIPE) { | |
234 | + if (snd_pcm_prepare(pcm) < 0) return 0; | |
235 | + else continue; | |
236 | + } | |
237 | + } | |
238 | + | |
239 | + samples += (err * framebits) >> 3; | |
240 | + nframes -= err; | |
241 | + } | |
242 | + | |
243 | + return num_bytes; | |
244 | } | |
245 | ||
246 | int audio_flush_alsa(cst_audiodev *ad) | |
247 | { | |
248 | snd_pcm_t *pcm = ad->platform_data; | |
249 | ||
250 | - return snd_pcm_plugin_flush(pcm,0); | |
251 | + return snd_pcm_drop(pcm); | |
252 | } | |
253 | ||
254 | int audio_drain_alsa(cst_audiodev *ad) | |
255 | { | |
256 | snd_pcm_t *pcm = ad->platform_data; | |
257 | ||
258 | - return snd_pcm_plugin_playback_drain(pcm); | |
259 | + return snd_pcm_drain(pcm); | |
260 | } | |
261 | - | |
262 | --- flite-1.2-release/configure.in.alsa 2003-02-18 03:45:45 +0300 | |
263 | +++ flite-1.2-release/configure.in 2005-10-31 14:50:54 +0300 | |
264 | @@ -131,9 +131,10 @@ | |
265 | AC_CHECK_HEADER(sys/audioio.h, | |
266 | [AUDIODRIVER="sun" | |
267 | AUDIODEFS=-DCST_AUDIO_SUNOS]) | |
268 | -AC_CHECK_HEADER(sys/asoundlib.h, | |
269 | +AC_CHECK_HEADER(alsa/asoundlib.h, | |
270 | [AUDIODRIVER="alsa" | |
271 | - AUDIODEFS=-DCST_AUDIO_ALSA]) | |
272 | + AUDIODEFS=-DCST_AUDIO_ALSA | |
273 | + AUDIOLIBS=-lasound]) | |
274 | AC_CHECK_HEADER(mmsystem.h, | |
275 | [AUDIODRIVER="wince" | |
276 | AUDIODEFS=-DCST_AUDIO_WINCE |