| 1 |
/*************************************************************************/ |
| 2 |
/* */ |
| 3 |
/* Language Technologies Institute */ |
| 4 |
/* Carnegie Mellon University */ |
| 5 |
/* Copyright (c) 2000 */ |
| 6 |
/* All Rights Reserved. */ |
| 7 |
/* */ |
| 8 |
/* Permission is hereby granted, free of charge, to use and distribute */ |
| 9 |
/* this software and its documentation without restriction, including */ |
| 10 |
/* without limitation the rights to use, copy, modify, merge, publish, */ |
| 11 |
/* distribute, sublicense, and/or sell copies of this work, and to */ |
| 12 |
/* permit persons to whom this work is furnished to do so, subject to */ |
| 13 |
/* the following conditions: */ |
| 14 |
/* 1. The code must retain the above copyright notice, this list of */ |
| 15 |
/* conditions and the following disclaimer. */ |
| 16 |
/* 2. Any modifications must be clearly marked as such. */ |
| 17 |
/* 3. Original authors' names are not deleted. */ |
| 18 |
/* 4. The authors' names are not used to endorse or promote products */ |
| 19 |
/* derived from this software without specific prior written */ |
| 20 |
/* permission. */ |
| 21 |
/* */ |
| 22 |
/* CARNEGIE MELLON UNIVERSITY AND THE CONTRIBUTORS TO THIS WORK */ |
| 23 |
/* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ |
| 24 |
/* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ |
| 25 |
/* SHALL CARNEGIE MELLON UNIVERSITY NOR THE CONTRIBUTORS BE LIABLE */ |
| 26 |
/* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ |
| 27 |
/* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ |
| 28 |
/* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ |
| 29 |
/* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ |
| 30 |
/* THIS SOFTWARE. */ |
| 31 |
/* */ |
| 32 |
/*************************************************************************/ |
| 33 |
/* Author: Alan W Black ([email protected]) */ |
| 34 |
/* Date: October 2000 */ |
| 35 |
/*************************************************************************/ |
| 36 |
/* */ |
| 37 |
/* Access to audio devices , */ |
| 38 |
/* */ |
| 39 |
/*************************************************************************/ |
| 40 |
#include "cst_string.h" |
| 41 |
#include "cst_wave.h" |
| 42 |
#include "cst_audio.h" |
| 43 |
#include "native_audio.h" |
| 44 |
|
| 45 |
int audio_bps(cst_audiofmt fmt) |
| 46 |
{ |
| 47 |
switch (fmt) |
| 48 |
{ |
| 49 |
case CST_AUDIO_LINEAR16: |
| 50 |
return 2; |
| 51 |
case CST_AUDIO_LINEAR8: |
| 52 |
case CST_AUDIO_MULAW: |
| 53 |
return 1; |
| 54 |
} |
| 55 |
return 0; |
| 56 |
} |
| 57 |
|
| 58 |
cst_audiodev *audio_open(int sps, int channels, cst_audiofmt fmt) |
| 59 |
{ |
| 60 |
cst_audiodev *ad; |
| 61 |
int up, down; |
| 62 |
|
| 63 |
ad = AUDIO_OPEN_NATIVE(sps, channels, fmt); |
| 64 |
if (ad == NULL) |
| 65 |
return NULL; |
| 66 |
|
| 67 |
down = sps / 1000; |
| 68 |
up = ad->real_sps / 1000; |
| 69 |
|
| 70 |
if (up != down) |
| 71 |
ad->rateconv = new_rateconv(up, down, channels); |
| 72 |
|
| 73 |
return ad; |
| 74 |
} |
| 75 |
|
| 76 |
int audio_close(cst_audiodev *ad) |
| 77 |
{ |
| 78 |
if (ad->rateconv) |
| 79 |
delete_rateconv(ad->rateconv); |
| 80 |
|
| 81 |
return AUDIO_CLOSE_NATIVE(ad); |
| 82 |
} |
| 83 |
|
| 84 |
int audio_write(cst_audiodev *ad,void *buff,int num_bytes) |
| 85 |
{ |
| 86 |
void *abuf = buff, *nbuf = NULL; |
| 87 |
int rv, i, real_num_bytes = num_bytes; |
| 88 |
|
| 89 |
if (ad->rateconv) |
| 90 |
{ |
| 91 |
short *in, *out; |
| 92 |
int insize, outsize, n; |
| 93 |
|
| 94 |
insize = real_num_bytes / 2; |
| 95 |
in = (short *)buff; |
| 96 |
|
| 97 |
outsize = ad->rateconv->outsize; |
| 98 |
nbuf = out = cst_alloc(short, outsize); |
| 99 |
real_num_bytes = outsize * 2; |
| 100 |
|
| 101 |
while ((n = cst_rateconv_in(ad->rateconv, in, insize)) > 0) |
| 102 |
{ |
| 103 |
in += n; |
| 104 |
insize -= n; |
| 105 |
while ((n = cst_rateconv_out(ad->rateconv, out, outsize)) > 0) |
| 106 |
{ |
| 107 |
out += n; |
| 108 |
outsize -= n; |
| 109 |
} |
| 110 |
} |
| 111 |
real_num_bytes -= outsize * 2; |
| 112 |
if (abuf != buff) |
| 113 |
cst_free(abuf); |
| 114 |
abuf = nbuf; |
| 115 |
} |
| 116 |
if (ad->real_channels != ad->channels) |
| 117 |
{ |
| 118 |
/* Yeah, we only do mono->stereo for now */ |
| 119 |
if (ad->real_channels != 2 || ad->channels != 1) |
| 120 |
{ |
| 121 |
cst_errmsg("audio_write: unsupported channel mapping requested (%d => %d).\n", |
| 122 |
ad->channels, ad->real_channels); |
| 123 |
} |
| 124 |
nbuf = cst_alloc(char, real_num_bytes * ad->real_channels / ad->channels); |
| 125 |
|
| 126 |
if (audio_bps(ad->fmt) == 2) |
| 127 |
{ |
| 128 |
for (i = 0; i < real_num_bytes / 2; ++i) |
| 129 |
{ |
| 130 |
((short *)nbuf)[i*2] = ((short *)abuf)[i]; |
| 131 |
((short *)nbuf)[i*2+1] = ((short *)abuf)[i]; |
| 132 |
} |
| 133 |
} |
| 134 |
else if (audio_bps(ad->fmt) == 1) |
| 135 |
{ |
| 136 |
for (i = 0; i < real_num_bytes / 2; ++i) |
| 137 |
{ |
| 138 |
((unsigned char *)nbuf)[i*2] = ((unsigned char *)abuf)[i]; |
| 139 |
((unsigned char *)nbuf)[i*2+1] = ((unsigned char *)abuf)[i]; |
| 140 |
} |
| 141 |
} |
| 142 |
else |
| 143 |
{ |
| 144 |
cst_errmsg("audio_write: unknown format %d\n", ad->fmt); |
| 145 |
cst_free(nbuf); |
| 146 |
if (abuf != buff) |
| 147 |
cst_free(abuf); |
| 148 |
cst_error(); |
| 149 |
} |
| 150 |
|
| 151 |
if (abuf != buff) |
| 152 |
cst_free(abuf); |
| 153 |
abuf = nbuf; |
| 154 |
real_num_bytes = real_num_bytes * ad->real_channels / ad->channels; |
| 155 |
} |
| 156 |
if (ad->real_fmt != ad->fmt) |
| 157 |
{ |
| 158 |
if (ad->real_fmt == CST_AUDIO_LINEAR16 |
| 159 |
&& ad->fmt == CST_AUDIO_MULAW) |
| 160 |
{ |
| 161 |
nbuf = cst_alloc(char, real_num_bytes * 2); |
| 162 |
for (i = 0; i < real_num_bytes; ++i) |
| 163 |
((short *)nbuf)[i] = cst_ulaw_to_short(((unsigned char *)abuf)[i]); |
| 164 |
real_num_bytes *= 2; |
| 165 |
} |
| 166 |
else if (ad->real_fmt == CST_AUDIO_MULAW |
| 167 |
&& ad->fmt == CST_AUDIO_LINEAR16) |
| 168 |
{ |
| 169 |
nbuf = cst_alloc(char, real_num_bytes / 2); |
| 170 |
for (i = 0; i < real_num_bytes / 2; ++i) |
| 171 |
((unsigned char *)nbuf)[i] = cst_short_to_ulaw(((short *)abuf)[i]); |
| 172 |
real_num_bytes /= 2; |
| 173 |
} |
| 174 |
else if (ad->real_fmt == CST_AUDIO_LINEAR8 |
| 175 |
&& ad->fmt == CST_AUDIO_LINEAR16) |
| 176 |
{ |
| 177 |
nbuf = cst_alloc(char, real_num_bytes / 2); |
| 178 |
for (i = 0; i < real_num_bytes / 2; ++i) |
| 179 |
((unsigned char *)nbuf)[i] = (((short *)abuf)[i] >> 8) + 128; |
| 180 |
real_num_bytes /= 2; |
| 181 |
} |
| 182 |
else |
| 183 |
{ |
| 184 |
cst_errmsg("audio_write: unknown format conversion (%d => %d) requested.\n", |
| 185 |
ad->fmt, ad->real_fmt); |
| 186 |
cst_free(nbuf); |
| 187 |
if (abuf != buff) |
| 188 |
cst_free(abuf); |
| 189 |
cst_error(); |
| 190 |
} |
| 191 |
if (abuf != buff) |
| 192 |
cst_free(abuf); |
| 193 |
abuf = nbuf; |
| 194 |
} |
| 195 |
if (ad->byteswap && audio_bps(ad->real_fmt) == 2) |
| 196 |
swap_bytes_short((short *)abuf, real_num_bytes/2); |
| 197 |
|
| 198 |
if (real_num_bytes) |
| 199 |
rv = AUDIO_WRITE_NATIVE(ad,abuf,real_num_bytes); |
| 200 |
else |
| 201 |
rv = 0; |
| 202 |
|
| 203 |
if (abuf != buff) |
| 204 |
cst_free(abuf); |
| 205 |
|
| 206 |
/* Callers expect to get the same num_bytes back as they passed |
| 207 |
in. Funny, that ... */ |
| 208 |
return (rv == real_num_bytes) ? num_bytes : 0; |
| 209 |
} |
| 210 |
|
| 211 |
int audio_drain(cst_audiodev *ad) |
| 212 |
{ |
| 213 |
return AUDIO_DRAIN_NATIVE(ad); |
| 214 |
} |
| 215 |
|
| 216 |
int audio_flush(cst_audiodev *ad) |
| 217 |
{ |
| 218 |
return AUDIO_FLUSH_NATIVE(ad); |
| 219 |
} |
| 220 |
|
| 221 |
int play_wave(cst_wave *w) |
| 222 |
{ |
| 223 |
cst_audiodev *ad; |
| 224 |
int i,n,r; |
| 225 |
int num_shorts; |
| 226 |
|
| 227 |
if (!w) |
| 228 |
return CST_ERROR_FORMAT; |
| 229 |
|
| 230 |
if ((ad = audio_open(w->sample_rate, w->num_channels, |
| 231 |
/* FIXME: should be able to determine this somehow */ |
| 232 |
CST_AUDIO_LINEAR16)) == NULL) |
| 233 |
return CST_ERROR_FORMAT; |
| 234 |
|
| 235 |
num_shorts = w->num_samples*w->num_channels; |
| 236 |
for (i=0; i < num_shorts; i += r/2) |
| 237 |
{ |
| 238 |
if (num_shorts > i+CST_AUDIOBUFFSIZE) |
| 239 |
n = CST_AUDIOBUFFSIZE; |
| 240 |
else |
| 241 |
n = num_shorts-i; |
| 242 |
r = audio_write(ad,&w->samples[i],n*2); |
| 243 |
if (r <= 0) |
| 244 |
{ |
| 245 |
cst_errmsg("failed to write %d samples\n",n); |
| 246 |
break; |
| 247 |
} |
| 248 |
} |
| 249 |
|
| 250 |
audio_flush(ad); |
| 251 |
audio_close(ad); |
| 252 |
|
| 253 |
return CST_OK_FORMAT; |
| 254 |
} |
| 255 |
|
| 256 |
int play_wave_sync(cst_wave *w, cst_relation *rel, |
| 257 |
int (*call_back)(cst_item *)) |
| 258 |
{ |
| 259 |
int q,i,n,r; |
| 260 |
cst_audiodev *ad; |
| 261 |
float r_pos; |
| 262 |
cst_item *item; |
| 263 |
|
| 264 |
if (!w) |
| 265 |
return CST_ERROR_FORMAT; |
| 266 |
|
| 267 |
if ((ad = audio_open(w->sample_rate,w->num_channels, |
| 268 |
CST_AUDIO_LINEAR16)) == NULL) |
| 269 |
return CST_ERROR_FORMAT; |
| 270 |
|
| 271 |
q=0; |
| 272 |
item = relation_head(rel); |
| 273 |
r_pos = w->sample_rate * 0; |
| 274 |
for (i=0; i < w->num_samples; i += r/2) |
| 275 |
{ |
| 276 |
if (i >= r_pos) |
| 277 |
{ |
| 278 |
audio_flush(ad); |
| 279 |
|
| 280 |
if ((*call_back)(item) != CST_OK_FORMAT) |
| 281 |
break; |
| 282 |
item = item_next(item); |
| 283 |
if (item) |
| 284 |
r_pos = w->sample_rate * val_float(ffeature(item,"p.end")); |
| 285 |
else |
| 286 |
r_pos = w->num_samples; |
| 287 |
} |
| 288 |
if (w->num_samples > i+CST_AUDIOBUFFSIZE) |
| 289 |
n = CST_AUDIOBUFFSIZE; |
| 290 |
else |
| 291 |
n = w->num_samples-i; |
| 292 |
r = audio_write(ad,&w->samples[i],n*2); |
| 293 |
q +=r; |
| 294 |
if (r <= 0) |
| 295 |
cst_errmsg("failed to write %d samples\n",n); |
| 296 |
} |
| 297 |
|
| 298 |
audio_close(ad); |
| 299 |
|
| 300 |
return CST_OK_FORMAT; |
| 301 |
} |