|
Lines 2-8
Link Here
|
| 2 |
/* */ |
2 |
/* */ |
| 3 |
/* Language Technologies Institute */ |
3 |
/* Language Technologies Institute */ |
| 4 |
/* Carnegie Mellon University */ |
4 |
/* Carnegie Mellon University */ |
| 5 |
/* Copyright (c) 2001 */ |
5 |
/* Copyright (c) 2000 */ |
| 6 |
/* All Rights Reserved. */ |
6 |
/* All Rights Reserved. */ |
| 7 |
/* */ |
7 |
/* */ |
| 8 |
/* Permission is hereby granted, free of charge, to use and distribute */ |
8 |
/* Permission is hereby granted, free of charge, to use and distribute */ |
|
Lines 29-186
Link Here
|
| 29 |
/* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ |
29 |
/* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ |
| 30 |
/* THIS SOFTWARE. */ |
30 |
/* THIS SOFTWARE. */ |
| 31 |
/* */ |
31 |
/* */ |
|
|
32 |
/*********************************************************************** */ |
| 33 |
/* Author: Lukas Loehrer ( */ |
| 34 |
/* Date: January 2005 */ |
| 32 |
/*************************************************************************/ |
35 |
/*************************************************************************/ |
| 33 |
/* Author: Geoff Harrison (mandrake@cepstral.com) */ |
|
|
| 34 |
/* Date: Sepetember 2001 */ |
| 35 |
/*************************************************************************/ |
| 36 |
/* */ |
36 |
/* */ |
| 37 |
/* Access to ALSA audio devices */ |
37 |
/* Native access to alsa audio devices on Linux */ |
| 38 |
/* */ |
38 |
/* Tested with libasound version 1.0.10 */ |
| 39 |
/*************************************************************************/ |
39 |
/*************************************************************************/ |
| 40 |
|
40 |
|
| 41 |
#include <stdio.h> |
|
|
| 42 |
#include <stdlib.h> |
41 |
#include <stdlib.h> |
| 43 |
#include <unistd.h> |
42 |
#include <unistd.h> |
| 44 |
#include <sys/types.h> |
43 |
#include <sys/types.h> |
|
|
44 |
#include <assert.h> |
| 45 |
#include <errno.h> |
| 46 |
|
| 45 |
#include "cst_string.h" |
47 |
#include "cst_string.h" |
| 46 |
#include "cst_wave.h" |
48 |
#include "cst_wave.h" |
| 47 |
#include "cst_audio.h" |
49 |
#include "cst_audio.h" |
| 48 |
|
50 |
|
| 49 |
#include <sys/asoundlib.h> |
51 |
#include <alsa/asoundlib.h> |
| 50 |
|
52 |
|
| 51 |
#include <sys/stat.h> |
|
|
| 52 |
#include <fcntl.h> |
| 53 |
|
53 |
|
| 54 |
static int alsa_card = 0, alsa_device = 0; |
54 |
/*static char *pcm_dev_name = "hw:0,0"; */ |
|
|
55 |
static char *pcm_dev_name ="default"; |
| 55 |
|
56 |
|
|
|
57 |
static inline void print_pcm_state(snd_pcm_t *handle, char *msg) |
| 58 |
{ |
| 59 |
fprintf(stderr, "PCM state at %s = %s\n", msg, |
| 60 |
snd_pcm_state_name(snd_pcm_state(handle))); |
| 61 |
} |
| 62 |
|
| 56 |
cst_audiodev *audio_open_alsa(int sps, int channels, cst_audiofmt fmt) |
63 |
cst_audiodev *audio_open_alsa(int sps, int channels, cst_audiofmt fmt) |
| 57 |
{ |
64 |
{ |
| 58 |
snd_pcm_channel_info_t pinfo; |
65 |
cst_audiodev *ad; |
| 59 |
snd_pcm_channel_params_t params; |
66 |
unsigned int real_rate; |
| 60 |
snd_pcm_channel_setup_t setup; |
67 |
int err; |
| 61 |
snd_pcm_t *pcm; |
|
|
| 62 |
cst_audiodev *ad; |
| 63 |
int err; |
| 64 |
|
68 |
|
| 65 |
#ifdef __QNXNTO__ |
69 |
/* alsa specific stuff */ |
| 66 |
if (snd_pcm_open_preferred(&pcm,&alsa_card,&alsa_device,SND_PCM_OPEN_PLAYBACK) < 0) |
70 |
snd_pcm_t *pcm_handle; |
| 67 |
{ |
71 |
snd_pcm_stream_t stream = SND_PCM_STREAM_PLAYBACK; |
| 68 |
cst_errmsg("alsa_audio: failed to open audio device\n"); |
72 |
snd_pcm_hw_params_t *hwparams; |
| 69 |
cst_error(); |
73 |
snd_pcm_format_t format; |
| 70 |
} |
74 |
snd_pcm_access_t access = SND_PCM_ACCESS_RW_INTERLEAVED; |
| 71 |
if (snd_pcm_plugin_set_disable(pcm,PLUGIN_DISABLE_MMAP) < 0) |
|
|
| 72 |
{ |
| 73 |
cst_errmsg("alsa_audio: failed to disable mmap\n"); |
| 74 |
snd_pcm_close(pcm); |
| 75 |
cst_error(); |
| 76 |
} |
| 77 |
#else |
| 78 |
if (snd_pcm_open(&pcm,alsa_card,alsa_device,SND_PCM_OPEN_PLAYBACK) < 0) |
| 79 |
{ |
| 80 |
cst_errmsg("alsa_audio: failed to open audio device\n"); |
| 81 |
cst_error(); |
| 82 |
} |
| 83 |
#endif |
| 84 |
|
75 |
|
|
|
76 |
/* Allocate the snd_pcm_hw_params_t structure on the stack. */ |
| 77 |
snd_pcm_hw_params_alloca(&hwparams); |
| 85 |
|
78 |
|
| 86 |
memset(&pinfo, 0, sizeof(pinfo)); |
79 |
/* Open pcm device */ |
| 87 |
memset(¶ms, 0, sizeof(params)); |
80 |
err = snd_pcm_open(&pcm_handle, pcm_dev_name, stream, 0); |
| 88 |
memset(&setup, 0, sizeof(setup)); |
81 |
if (err < 0) |
|
|
82 |
{ |
| 83 |
cst_errmsg("audio_open_alsa: failed to open audio device %s. %s\n", |
| 84 |
pcm_dev_name, snd_strerror(err)); |
| 85 |
return NULL; |
| 86 |
} |
| 89 |
|
87 |
|
| 90 |
pinfo.channel = SND_PCM_CHANNEL_PLAYBACK; |
88 |
/* Init hwparams with full configuration space */ |
| 91 |
snd_pcm_plugin_info(pcm,&pinfo); |
89 |
err = snd_pcm_hw_params_any(pcm_handle, hwparams); |
|
|
90 |
if (err < 0) |
| 91 |
{ |
| 92 |
snd_pcm_close(pcm_handle); |
| 93 |
cst_errmsg("audio_open_alsa: failed to get hardware parameters from audio device. %s\n", snd_strerror(err)); |
| 94 |
return NULL; |
| 95 |
} |
| 92 |
|
96 |
|
| 93 |
params.mode = SND_PCM_MODE_BLOCK; |
97 |
/* Set access mode */ |
| 94 |
params.channel = SND_PCM_CHANNEL_PLAYBACK; |
98 |
err = snd_pcm_hw_params_set_access(pcm_handle, hwparams, access); |
| 95 |
params.start_mode = SND_PCM_START_DATA; |
99 |
if (err < 0) |
| 96 |
params.stop_mode = SND_PCM_STOP_STOP; |
100 |
{ |
|
|
101 |
snd_pcm_close(pcm_handle); |
| 102 |
cst_errmsg("audio_open_alsa: failed to set access mode. %s.\n", snd_strerror(err)); |
| 103 |
return NULL; |
| 104 |
} |
| 97 |
|
105 |
|
| 98 |
params.buf.block.frag_size = pinfo.max_fragment_size; |
106 |
/* Determine matching alsa sample format */ |
| 99 |
params.buf.block.frags_max = 1; |
107 |
/* This could be implemented in a more */ |
| 100 |
params.buf.block.frags_min = 1; |
108 |
/* flexible way (byte order conversion). */ |
| 101 |
|
109 |
switch (fmt) |
| 102 |
params.format.interleave = 1; |
110 |
{ |
| 103 |
params.format.rate = sps; |
111 |
case CST_AUDIO_LINEAR16: |
| 104 |
params.format.voices = channels; |
|
|
| 105 |
|
| 106 |
switch (fmt) |
| 107 |
{ |
| 108 |
case CST_AUDIO_LINEAR16: |
| 109 |
if (CST_LITTLE_ENDIAN) |
112 |
if (CST_LITTLE_ENDIAN) |
| 110 |
params.format.format = SND_PCM_SFMT_S16_LE; |
113 |
format = SND_PCM_FORMAT_S16_LE; |
| 111 |
else |
114 |
else |
| 112 |
params.format.format = SND_PCM_SFMT_S16_BE; |
115 |
format = SND_PCM_FORMAT_S16_BE; |
| 113 |
break; |
116 |
break; |
| 114 |
case CST_AUDIO_LINEAR8: |
117 |
case CST_AUDIO_LINEAR8: |
| 115 |
params.format.format = SND_PCM_SFMT_U8; |
118 |
format = SND_PCM_FORMAT_U8; |
| 116 |
break; |
119 |
break; |
| 117 |
case CST_AUDIO_MULAW: |
120 |
case CST_AUDIO_MULAW: |
| 118 |
params.format.format = SND_PCM_SFMT_MU_LAW; |
121 |
format = SND_PCM_FORMAT_MU_LAW; |
| 119 |
break; |
122 |
break; |
| 120 |
} |
123 |
default: |
|
|
124 |
snd_pcm_close(pcm_handle); |
| 125 |
cst_errmsg("audio_open_alsa: failed to find suitable format.\n"); |
| 126 |
return NULL; |
| 127 |
break; |
| 128 |
} |
| 121 |
|
129 |
|
| 122 |
if((err = snd_pcm_plugin_params(pcm,¶ms)) < 0) |
130 |
/* Set samble format */ |
| 123 |
{ |
131 |
err = snd_pcm_hw_params_set_format(pcm_handle, hwparams, format); |
| 124 |
cst_errmsg("alsa_audio params setting failed: %s\n",snd_strerror(err)); |
132 |
if (err <0) |
| 125 |
snd_pcm_close(pcm); |
133 |
{ |
| 126 |
cst_error(); |
134 |
snd_pcm_close(pcm_handle); |
| 127 |
} |
135 |
cst_errmsg("audio_open_alsa: failed to set format. %s.\n", snd_strerror(err)); |
| 128 |
if((err = snd_pcm_plugin_setup(pcm,SND_PCM_CHANNEL_PLAYBACK)) > 0) { |
136 |
return NULL; |
| 129 |
cst_errmsg("alsa_audio sound prepare setting failed: %s\n",snd_strerror(err)); |
137 |
} |
| 130 |
snd_pcm_close(pcm); |
|
|
| 131 |
cst_error(); |
| 132 |
} |
| 133 |
if((err = snd_pcm_plugin_prepare(pcm,SND_PCM_CHANNEL_PLAYBACK)) > 0) { |
| 134 |
cst_errmsg("alsa_audio sound prepare setting failed: %s\n",snd_strerror(err)); |
| 135 |
snd_pcm_close(pcm); |
| 136 |
cst_error(); |
| 137 |
} |
| 138 |
|
138 |
|
| 139 |
pinfo.channel = SND_PCM_CHANNEL_PLAYBACK; |
139 |
/* Set sample rate near the disired rate */ |
| 140 |
snd_pcm_plugin_info(pcm,&pinfo); |
140 |
real_rate = sps; |
|
|
141 |
err = snd_pcm_hw_params_set_rate_near(pcm_handle, hwparams, &real_rate, 0); |
| 142 |
if (err < 0) |
| 143 |
{ |
| 144 |
snd_pcm_close(pcm_handle); |
| 145 |
cst_errmsg("audio_open_alsa: failed to set sample rate near %d. %s.\n", sps, snd_strerror(err)); |
| 146 |
return NULL; |
| 147 |
} |
| 148 |
/*FIXME: This is probably too strict */ |
| 149 |
assert(sps == real_rate); |
| 141 |
|
150 |
|
| 142 |
ad = cst_alloc(cst_audiodev, 1); |
151 |
/* Set number of channels */ |
| 143 |
ad->platform_data = pcm; |
152 |
assert(channels >0); |
| 144 |
ad->sps = ad->real_sps = sps; |
153 |
err = snd_pcm_hw_params_set_channels(pcm_handle, hwparams, channels); |
| 145 |
ad->channels = ad->real_channels = channels; |
154 |
if (err < 0) |
| 146 |
ad->fmt = ad->real_fmt = fmt; |
155 |
{ |
|
|
156 |
snd_pcm_close(pcm_handle); |
| 157 |
cst_errmsg("audio_open_alsa: failed to set number of channels to %d. %s.\n", channels, snd_strerror(err)); |
| 158 |
return NULL; |
| 159 |
} |
| 147 |
|
160 |
|
| 148 |
return ad; |
161 |
/* Commit hardware parameters */ |
|
|
162 |
err = snd_pcm_hw_params(pcm_handle, hwparams); |
| 163 |
if (err < 0) |
| 164 |
{ |
| 165 |
snd_pcm_close(pcm_handle); |
| 166 |
cst_errmsg("audio_open_alsa: failed to set hw parameters. %s.\n", snd_strerror(err)); |
| 167 |
return NULL; |
| 168 |
} |
| 169 |
|
| 170 |
/* Make sure the device is ready to accept data */ |
| 171 |
assert(snd_pcm_state(pcm_handle) == SND_PCM_STATE_PREPARED); |
| 172 |
|
| 173 |
/* Write hardware parameters to flite audio device data structure */ |
| 174 |
ad = cst_alloc(cst_audiodev, 1); |
| 175 |
assert(ad != NULL); |
| 176 |
ad->real_sps = ad->sps = sps; |
| 177 |
ad->real_channels = ad->channels = channels; |
| 178 |
ad->real_fmt = ad->fmt = fmt; |
| 179 |
ad->platform_data = (void *) pcm_handle; |
| 180 |
|
| 181 |
return ad; |
| 149 |
} |
182 |
} |
| 150 |
|
183 |
|
| 151 |
int audio_close_alsa(cst_audiodev *ad) |
184 |
int audio_close_alsa(cst_audiodev *ad) |
| 152 |
{ |
185 |
{ |
| 153 |
snd_pcm_t *pcm; |
186 |
int result; |
|
|
187 |
snd_pcm_t *pcm_handle; |
| 154 |
|
188 |
|
| 155 |
if (ad == NULL) |
189 |
if (ad == NULL) |
| 156 |
return 0; |
190 |
return 0; |
| 157 |
|
191 |
|
| 158 |
pcm = ad->platform_data; |
192 |
pcm_handle = (snd_pcm_t *) ad->platform_data; |
| 159 |
snd_pcm_plugin_flush(pcm,0); |
193 |
result = snd_pcm_close(pcm_handle); |
| 160 |
snd_pcm_close(pcm); |
194 |
if (result < 0) |
| 161 |
cst_free(ad); |
195 |
{ |
|
|
196 |
cst_errmsg("audio_close_alsa: Error: %s.\n", snd_strerror(result)); |
| 197 |
} |
| 198 |
cst_free(ad); |
| 199 |
return result; |
| 200 |
} |
| 162 |
|
201 |
|
| 163 |
return 0; |
202 |
/* Returns zero if recovery was successful. */ |
|
|
203 |
static int recover_from_error(snd_pcm_t *pcm_handle, ssize_t res) |
| 204 |
{ |
| 205 |
if (res == -EPIPE) /* xrun */ |
| 206 |
{ |
| 207 |
res = snd_pcm_prepare(pcm_handle); |
| 208 |
if (res < 0) |
| 209 |
{ |
| 210 |
/* Failed to recover from xrun */ |
| 211 |
cst_errmsg("recover_from_write_error: failed to recover from xrun. %s\n.", snd_strerror(res)); |
| 212 |
return res; |
| 213 |
} |
| 214 |
} |
| 215 |
else if (res == -ESTRPIPE) /* Suspend */ |
| 216 |
{ |
| 217 |
while ((res = snd_pcm_resume(pcm_handle)) == -EAGAIN) |
| 218 |
{ |
| 219 |
snd_pcm_wait(pcm_handle, 1000); |
| 220 |
} |
| 221 |
if (res < 0) |
| 222 |
{ |
| 223 |
res = snd_pcm_prepare(pcm_handle); |
| 224 |
if (res <0) |
| 225 |
{ |
| 226 |
/* Resume failed */ |
| 227 |
cst_errmsg("audio_recover_from_write_error: failed to resume after suspend. %s\n.", snd_strerror(res)); |
| 228 |
return res; |
| 229 |
} |
| 230 |
} |
| 231 |
} |
| 232 |
else if (res < 0) |
| 233 |
{ |
| 234 |
/* Unknown failure */ |
| 235 |
cst_errmsg("audio_recover_from_write_error: %s.\n", snd_strerror(res)); |
| 236 |
return res; |
| 237 |
} |
| 238 |
return 0; |
| 164 |
} |
239 |
} |
| 165 |
|
240 |
|
| 166 |
int audio_write_alsa(cst_audiodev *ad, void *samples, int num_bytes) |
241 |
int audio_write_alsa(cst_audiodev *ad, void *samples, int num_bytes) |
| 167 |
{ |
242 |
{ |
| 168 |
snd_pcm_t *pcm = ad->platform_data; |
243 |
size_t frame_size; |
|
|
244 |
ssize_t num_frames, res; |
| 245 |
snd_pcm_t *pcm_handle; |
| 246 |
char *buf = (char *) samples; |
| 169 |
|
247 |
|
| 170 |
return snd_pcm_plugin_write(pcm,samples,num_bytes); |
248 |
/* Determine frame size in bytes */ |
|
|
249 |
frame_size = audio_bps(ad->real_fmt) * ad->real_channels; |
| 250 |
/* Require that only complete frames are handed in */ |
| 251 |
assert((num_bytes % frame_size) == 0); |
| 252 |
num_frames = num_bytes / frame_size; |
| 253 |
pcm_handle = (snd_pcm_t *) ad->platform_data; |
| 254 |
|
| 255 |
while (num_frames > 0) |
| 256 |
{ |
| 257 |
res = snd_pcm_writei(pcm_handle, buf, num_frames); |
| 258 |
if (res != num_frames) |
| 259 |
{ |
| 260 |
if (res == -EAGAIN || (res > 0 && res < num_frames)) |
| 261 |
{ |
| 262 |
snd_pcm_wait(pcm_handle, 100); |
| 263 |
} |
| 264 |
else if (recover_from_error(pcm_handle, res) < 0) |
| 265 |
{ |
| 266 |
return -1; |
| 267 |
} |
| 268 |
} |
| 269 |
|
| 270 |
if (res >0) |
| 271 |
{ |
| 272 |
num_frames -= res; |
| 273 |
buf += res * frame_size; |
| 274 |
} |
| 275 |
} |
| 276 |
return num_bytes; |
| 171 |
} |
277 |
} |
| 172 |
|
278 |
|
| 173 |
int audio_flush_alsa(cst_audiodev *ad) |
279 |
int audio_flush_alsa(cst_audiodev *ad) |
| 174 |
{ |
280 |
{ |
| 175 |
snd_pcm_t *pcm = ad->platform_data; |
281 |
int result; |
| 176 |
|
282 |
result = snd_pcm_drain((snd_pcm_t *) ad->platform_data); |
| 177 |
return snd_pcm_plugin_flush(pcm,0); |
283 |
if (result < 0) |
|
|
284 |
{ |
| 285 |
cst_errmsg("audio_flush_alsa: Error: %s.\n", snd_strerror(result)); |
| 286 |
} |
| 287 |
/* Prepare device for more data */ |
| 288 |
result = snd_pcm_prepare((snd_pcm_t *) ad->platform_data); |
| 289 |
if (result < 0) |
| 290 |
{ |
| 291 |
cst_errmsg("audio_flush_alsa: Error: %s.\n", snd_strerror(result)); |
| 292 |
} |
| 293 |
return result; |
| 178 |
} |
294 |
} |
| 179 |
|
295 |
|
| 180 |
int audio_drain_alsa(cst_audiodev *ad) |
296 |
int audio_drain_alsa(cst_audiodev *ad) |
| 181 |
{ |
297 |
{ |
| 182 |
snd_pcm_t *pcm = ad->platform_data; |
298 |
int result; |
| 183 |
|
299 |
result = snd_pcm_drop((snd_pcm_t *) ad->platform_data); |
| 184 |
return snd_pcm_plugin_playback_drain(pcm); |
300 |
if (result < 0) |
|
|
301 |
{ |
| 302 |
cst_errmsg("audio_drain_alsa: Error: %s.\n", snd_strerror(result)); |
| 303 |
} |
| 304 |
/* Prepare device for more data */ |
| 305 |
result = snd_pcm_prepare((snd_pcm_t *) ad->platform_data); |
| 306 |
if (result < 0) |
| 307 |
{ |
| 308 |
cst_errmsg("audio_drain_alsa: Error: %s.\n", snd_strerror(result)); |
| 309 |
} |
| 310 |
return result; |
| 185 |
} |
311 |
} |
| 186 |
|
|
|