1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476
|
#if defined(OSS_PLAYER)
/*
* Copyright 1997 Chris Bagwell And Sundry Contributors
* This source code is freely redistributable and may be used for
* any purpose. This copyright notice must be maintained.
* Chris Bagwell And Sundry Contributors are not
* responsible for the consequences of using this software.
*/
/* Direct to Open Sound System (OSS) sound driver
* OSS is a popular unix sound driver for Intel x86 unices (eg. Linux)
* and several other unixes (such as SunOS/Solaris).
* This driver is compatible with OSS original source that was called
* USS, Voxware and TASD.
*
* added by Chris Bagwell (cbagwell@sprynet.com) on 2/19/96
* based on info grabed from vplay.c in Voxware snd-utils-3.5 package.
* and on LINUX_PLAYER patches added by Greg Lee
* which was originally from Directo to Sound Blaster device driver (sbdsp.c).
* SBLAST patches by John T. Kohl.
*/
#include <malloc.h>
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/soundcard.h>
#include <signal.h>
#include "st.h"
#include "libst.h"
static got_int = 0;
static int abuf_size = 0;
static int abuf_cnt = 0;
static char *audiobuf;
/* This is how we know when to stop recording. User sends interrupt
* (eg. control-c) and then we mark a flag to show we are done.
* Must call "sigint(0)" during init so that the OS can be notified
* what to do.
*/
static void
sigint(s)
{
if (s) got_int = 1;
else signal(SIGINT, sigint);
}
/*
* Do anything required before you start reading samples.
* Read file header.
* Find out sampling rate,
* size and style of samples,
* mono/stereo/quad.
*/
ossdspstartread(ft)
ft_t ft;
{
int tmp;
int samplesize = 8, dsp_stereo;
if (ft->info.rate == 0.0) ft->info.rate = 8000;
if (ft->info.size == -1) ft->info.size = BYTE;
if (ft->info.size == BYTE) {
samplesize = 8;
if (ft->info.style == -1)
ft->info.style = UNSIGNED;
if (ft->info.style != UNSIGNED) {
fail("OSS driver only supports unsigned with bytes");
}
}
else if (ft->info.size == WORD) {
samplesize = 16;
if (ft->info.style == -1)
ft->info.style = SIGN2;
if (ft->info.style != SIGN2) {
fail("OSS driver only supports signed with words");
}
}
else {
fail("OSS driver only supports bytes and words");
}
if (ft->info.channels == -1) ft->info.channels = 1;
else if (ft->info.channels > 2) ft->info.channels = 2;
ioctl(fileno(ft->fp), SNDCTL_DSP_RESET, 0);
ioctl (fileno(ft->fp), SNDCTL_DSP_GETBLKSIZE, &abuf_size);
if (abuf_size < 4 || abuf_size > 65536) {
fail("Invalid audio buffer size %d", abuf_size);
}
if ((audiobuf = malloc (abuf_size)) == NULL) {
fail("Unable to allocate input/output buffer of size %d", abuf_size);
}
if (ioctl(fileno(ft->fp), SNDCTL_DSP_SYNC, NULL) < 0) {
fail("Unable to sync dsp");
}
tmp = samplesize;
ioctl(fileno(ft->fp), SNDCTL_DSP_SAMPLESIZE, &tmp);
if (tmp != samplesize) {
fail("Unable to set the sample size to %d", samplesize);
}
if (ft->info.channels == 2) dsp_stereo = 1;
else dsp_stereo = 0;
tmp = dsp_stereo;
ioctl(fileno(ft->fp), SNDCTL_DSP_STEREO, &tmp);
if (tmp != dsp_stereo) {
ft->info.channels = 1;
warn("Couldn't set to %s", dsp_stereo? "stereo":"mono");
dsp_stereo = 0;
}
tmp = ft->info.rate;
ioctl (fileno(ft->fp), SNDCTL_DSP_SPEED, &tmp);
if (ft->info.rate != tmp) {
if (ft->info.rate - tmp > tmp/10 || tmp - ft->info.rate > tmp/10)
warn("Unable to set audio speed to %d (set to %d)",
ft->info.rate, tmp);
ft->info.rate = tmp;
}
sigint(0); /* Prepare to catch SIGINT */
}
int dspget(ft)
ft_t ft;
{
int rval;
if (abuf_cnt < 1) {
abuf_cnt = read (fileno(ft->fp), (char *)audiobuf, abuf_size);
if (abuf_cnt == 0) {
got_int = 1; /* Act like user said end record */
return(0);
}
}
rval = *(audiobuf + (abuf_size-abuf_cnt));
abuf_cnt--;
return(rval);
}
/* Read short. */
unsigned short dsprshort(ft)
ft_t ft;
{
unsigned short rval;
if (abuf_cnt < 2) {
abuf_cnt = read (fileno(ft->fp), (char *)audiobuf, abuf_size);
if (abuf_cnt == 0) {
got_int = 1; /* act like user said end recording */
return(0);
}
}
rval = *((unsigned short *)(audiobuf + (abuf_size-abuf_cnt)));
abuf_cnt -= 2;
return(rval);
}
/*
* Read up to len samples from file.
* Convert to signed longs.
* Place in buf[].
* Return number of samples read.
*/
ossdspread(ft, buf, len)
ft_t ft;
LONG *buf, len;
{
register int datum;
int done = 0;
if (got_int)
return(0); /* Return with length 0 read so program will end */
switch(ft->info.size) {
case BYTE:
switch(ft->info.style) {
case SIGN2:
while(done < len) {
datum = dspget(ft);
if (got_int || feof(ft->fp))
return(done);
/* scale signed up to long's range */
*buf++ = LEFT(datum, 24);
done++;
}
return done;
case UNSIGNED:
while(done < len) {
datum = dspget(ft);
if (got_int || feof(ft->fp))
return(done);
/* Convert to unsigned */
datum ^= 128;
/* scale signed up to long's range */
*buf++ = LEFT(datum, 24);
done++;
}
return done;
case ULAW:
/* grab table from Posk stuff */
while(done < len) {
datum = dspget(ft);
if (got_int || feof(ft->fp))
return(done);
datum = st_ulaw_to_linear(datum);
/* scale signed up to long's range */
*buf++ = LEFT(datum, 16);
done++;
}
return done;
case ALAW:
while(done < len) {
datum = dspget(ft);
if (got_int || feof(ft->fp))
return(done);
datum = st_Alaw_to_linear(datum);
/* scale signed up to long's range */
*buf++ = LEFT(datum, 16);
done++;
}
return done;
}
case WORD:
switch(ft->info.style) {
case SIGN2:
while(done < len) {
datum = dsprshort(ft);
if (got_int || feof(ft->fp))
return(done);
/* scale signed up to long's range */
*buf++ = LEFT(datum, 16);
done++;
}
return done;
case UNSIGNED:
while(done < len) {
datum = dsprshort(ft);
if (got_int || feof(ft->fp))
return(done);
/* Convert to unsigned */
datum ^= 0x8000;
/* scale signed up to long's range */
*buf++ = (datum, 16);
done++;
}
return done;
case ULAW:
fail("No U-Law support for shorts");
return done;
case ALAW:
fail("No A-Law support");
return done;
}
}
fail("Drop through in ossdspread!");
/* Return number of samples read */
return(done);
}
/*
* Do anything required when you stop reading samples.
* Don't close input file!
*/
ossdspstopread(ft)
ft_t ft;
{
}
ossdspstartwrite(ft)
ft_t ft;
{
int samplesize = 8, dsp_stereo;
int tmp;
if (ft->info.rate == 0.0) ft->info.rate = 8000;
if (ft->info.size == -1) ft->info.size = BYTE;
if (ft->info.size == BYTE) {
samplesize = 8;
if (ft->info.style == -1)
ft->info.style = UNSIGNED;
if (ft->info.style != UNSIGNED) {
report("OSS driver only supports unsigned with bytes");
report("Forcing to unsigned");
ft->info.style = UNSIGNED;
}
}
else if (ft->info.size == WORD) {
samplesize = 16;
if (ft->info.style == -1)
ft->info.style = SIGN2;
if (ft->info.style != SIGN2) {
report("OSS driver only supports signed with words");
report("Forcing to signed linear");
ft->info.style = SIGN2;
}
}
else {
ft->info.size = WORD;
ft->info.style = SIGN2;
report("OSS driver only supports bytes and words");
report("Forcing to signed linear word");
}
if (ft->info.channels == -1) ft->info.channels = 1;
else if (ft->info.channels > 2) ft->info.channels = 2;
ioctl(fileno(ft->fp), SNDCTL_DSP_RESET, 0);
ioctl (fileno(ft->fp), SNDCTL_DSP_GETBLKSIZE, &abuf_size);
if (abuf_size < 4 || abuf_size > 65536) {
fail("Invalid audio buffer size %d", abuf_size);
}
if ((audiobuf = malloc (abuf_size)) == NULL) {
fail("Unable to allocate input/output buffer of size %d", abuf_size);
}
if (ioctl(fileno(ft->fp), SNDCTL_DSP_SYNC, NULL) < 0) {
fail("Unable to sync dsp");
}
tmp = samplesize;
ioctl(fileno(ft->fp), SNDCTL_DSP_SAMPLESIZE, &tmp);
if (tmp != samplesize) {
fail("Unable to set the sample size to %d", samplesize);
}
if (ft->info.channels == 2) dsp_stereo = 1;
else dsp_stereo = 0;
tmp = dsp_stereo;
ioctl(fileno(ft->fp), SNDCTL_DSP_STEREO, &tmp);
if (tmp != dsp_stereo) {
ft->info.channels = 1;
warn("Couldn't set to %s", dsp_stereo? "stereo":"mono");
dsp_stereo = 0;
}
tmp = ft->info.rate;
ioctl (fileno(ft->fp), SNDCTL_DSP_SPEED, &tmp);
if (ft->info.rate != tmp) {
if (ft->info.rate - tmp > tmp/10 || tmp - ft->info.rate > tmp/10) {
warn("Unable to set audio speed to %d (set to %d)",
ft->info.rate, tmp);
ft->info.rate = tmp;
}
}
}
void dspflush(ft)
ft_t ft;
{
if (write (fileno(ft->fp), audiobuf, abuf_cnt) != abuf_cnt) {
fail("Error writing to sound driver");
}
abuf_cnt = 0;
}
void dspput(ft,c)
ft_t ft;
int c;
{
if (abuf_cnt > abuf_size-1) dspflush(ft);
*(audiobuf + abuf_cnt) = c;
abuf_cnt++;
}
/* Write short. */
void
dspshort(ft,ui)
ft_t ft;
unsigned short ui;
{
if (abuf_cnt > abuf_size-2) dspflush(ft);
*((unsigned short *)(audiobuf + abuf_cnt)) = ui;
abuf_cnt += 2;
}
void ossdspwrite(ft, buf, len)
ft_t ft;
LONG *buf, len;
{
register int datum;
int done = 0;
switch(ft->info.size) {
case BYTE:
switch(ft->info.style) {
case SIGN2:
while(done < len) {
/* scale signed up to long's range */
datum = RIGHT(*buf++, 24);
dspput(ft,datum);
done++;
}
return;
case UNSIGNED:
while(done < len) {
/* scale signed up to long's range */
datum = RIGHT(*buf++, 24);
/* Convert to unsigned */
datum ^= 128;
dspput(ft,datum);
done++;
}
return;
case ULAW:
/* grab table from Posk stuff */
while(done < len) {
/* scale signed up to long's range */
datum = RIGHT(*buf++, 16);
datum = st_linear_to_ulaw(datum);
dspput(ft,datum);
done++;
}
return;
case ALAW:
while(done < len) {
/* scale signed up to long's range */
datum = RIGHT(*buf++, 16);
/* round up to 12 bits of data */
datum += 0x8; /* + 0b1000 */
datum = st_linear_to_Alaw(datum);
dspput(ft,datum);
done++;
}
return;
}
case WORD:
switch(ft->info.style) {
case SIGN2:
while(done < len) {
/* scale signed up to long's range */
datum = RIGHT(*buf++, 16);
dspshort(ft,datum);
done++;
}
return;
case UNSIGNED:
while(done < len) {
/* scale signed up to long's range */
datum = RIGHT(*buf++, 16);
/* Convert to unsigned */
datum ^= 0x8000;
dspshort(ft,datum);
done++;
}
return;
case ULAW:
fail("No U-Law support for shorts");
return;
case ALAW:
fail("No A-Law support");
return;
}
}
fail("Drop through in ossdspwrite!");
}
ossdspstopwrite(ft)
ft_t ft;
{
dspflush(ft);
}
#endif
|