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 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615
|
/* sqUnixSoundNAS.c -- sound support for the Network Audio System
*
* Author: Lex Spoon <lex@cc.gatech.edu>
*
* Copyright (C) 1996-2004 by Ian Piumarta and other authors/contributors
* listed elsewhere in this file.
* All rights reserved.
*
* This file is part of Unix Squeak.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include "sq.h"
#include "sqaio.h"
#include <audio/audiolib.h>
#include <assert.h>
#ifdef DEBUG
# define debugf printf
#else
static void debugf(char *fmt, ...) {}
#endif
#ifdef WORDS_BIGENDIAN
# define AU_FORMAT AuFormatLinearSigned16MSB
#else
# define AU_FORMAT AuFormatLinearSigned16LSB
#endif
static int sound_Stop(void);
/** routines for converting samples to different formats **/
/* XXX actually, I don't think NAS will need conversion. However,
SunOS could use it... */
#define BYTES_PER_SAMPLE 2 /* Squeak always uses 16-bit samples */
#ifdef HAS_MSB_FIRST
# define IS_BIGENDIAN 1
#else
# define IS_BIGENDIAN 0
#endif
/* specification of the output format. (Squeak's format is fixed:
stereo, 16-bit, host-endian, signed) */
static int fmtBytes= 2; /* bytes per sample the device is using */
static int fmtSigned= 1; /* whether the device uses signed samples */
static int fmtStereo= 0; /* whether the device is in stereo */
static int fmtIsBigendian= 0; /* whether the device is big-endion */
/* whether the device is differently-ended than Squeak */
#define auSwapBytes (fmtIsBigendian != IS_BIGENDIAN)
/* calculate number of bytes per frame, given the current mode */
static int bytesPerPlayFrame(void)
{
#if 1 /* ikp doesn't understand why this ... */
int bytes= 1;
bytes *= fmtBytes;
if (fmtStereo)
bytes *= 2;
return bytes;
#else /* ... doesn't look like this */
return fmtBytes * (fmtStereo ? 2 : 1);
#endif
}
#define FAIL(X) { success(false); return X; }
static AuServer *server = NULL; /* the audio server to write to */
static int recording=0; /* whether this module is recording
or playing. Only valid if
server!= NULL . */
static AuFlowID flow; /* the NAS flow being used */
static int semaIndex; /* the semaphore to signal Squeak with */
static int stereo; /* whether Squeak sees stereo or not */
static sqInt bytesAvail; /* current number of bytes that may be written
or read from the server */
static int sampleRate; /* the sample rate of the device.
Currently not accurate. */
static sqInt sound_AvailableSpace(void)
{
if (server == NULL)
return 0;
return bytesAvail;
}
static sqInt sound_InsertSamplesFromLeadTime(sqInt frameCount, void *srcBufPtr,
sqInt samplesOfLeadTime)
{
/* not possible, I don't think using NAS */
success(false);
return 0;
}
static sqInt sound_Stop(void)
{
if (server != NULL) {
aioDisable(AuServerConnectionNumber(server));
AuCloseServer(server);
server = NULL;
}
return 0;
}
static sqInt sound_PlaySamplesFromAtLength(sqInt frameCount, void *srcBufPtr, sqInt startIndex)
{
int bytesToPlay;
int framesToPlay;
char *buf; /* buffer to play from; it may not be srcBufPtr if a
conversion is necessary */
debugf("PlaySamples(frameCount=%d, srcBufPtr=%d, startIndex=%d\n", frameCount, srcBufPtr, startIndex);
/* figure out how much to play */
bytesToPlay = frameCount * bytesPerPlayFrame();
if (bytesToPlay > bytesAvail)
bytesToPlay = bytesAvail;
framesToPlay = bytesToPlay / bytesPerPlayFrame();
/* convert the buffer when not in stereo; when playing back, Squeak
will send mono data as stereo, where the right channel is to be
ignored */
if (stereo)
{
buf= (char *) (srcBufPtr+ 4*startIndex);
}
else
{
int i;
short *sbuf; /* the buffer, as short's instead of char's */
debugf("converting\n");
buf= malloc(2 * frameCount);
if (buf == NULL)
{
fprintf(stderr, "out of memory\n");
return 0;
}
sbuf= (short *) buf;
for(i=0; i<frameCount; i++)
{
sbuf[i]= ((short *) (srcBufPtr + 4*startIndex)) [2*i];
}
}
debugf("writing %d bytes (%d frames)\n", bytesToPlay, framesToPlay);
AuWriteElement(server, flow, 0,
bytesToPlay,
buf,
AuFalse,
NULL);
AuFlush(server);
bytesAvail -= bytesToPlay;
if (!stereo)
{
free(buf);
}
return framesToPlay;
}
/* Process audio events from the NAS server. The same routine is used
whether we are recording or playing back */
static void handleAudioEvents(int fd, void *data, int flags)
{
if (!server) {
debugf( "handleAudioEvents called while unconnected!\n");
return;
}
/* read events once */
AuEventsQueued(server, AuEventsQueuedAfterReading);
/* then loop through the read queue */
while(AuEventsQueued(server, AuEventsQueuedAlready)) {
AuEvent event;
AuNextEvent(server, AuTrue, &event);
debugf("event of type %d\n", event.type);
switch(event.type) {
case 0:
{
AuErrorEvent *errEvent = (AuErrorEvent *) &event;
char errdesc[1000];
AuGetErrorText(server, errEvent->error_code, errdesc, sizeof(errdesc));
fprintf(stderr, "audio error: %s\n", errdesc);
sound_Stop();
return; /* return, not break, so that we don't
process the now-closed server any longer! */
}
case AuEventTypeElementNotify:
{
AuElementNotifyEvent *enEvent = (AuElementNotifyEvent *)&event;
switch(enEvent->kind) {
case AuElementNotifyKindLowWater:
debugf("low water event\n");
bytesAvail += enEvent->num_bytes;
break;
case AuElementNotifyKindHighWater:
debugf("high water event\n");
bytesAvail += enEvent->num_bytes;
break;
case AuElementNotifyKindState:
debugf("state change (%d->%d)\n", enEvent->prev_state, enEvent->cur_state);
bytesAvail += enEvent->num_bytes;
if (enEvent->cur_state == AuStatePause) {
/* if the flow has stopped, then arrange for it to get started again */
/* XXX there is probably a more intelligent place to do
this, in case there is a real reason it has paused */
debugf("unpausing\n");
AuStartFlow(server, flow, NULL);
AuFlush(server);
}
break;
}
}
}
}
if (bytesAvail > 0) {
debugf("bytesAvail: %d\n", bytesAvail);
signalSemaphoreWithIndex(semaIndex);
}
aioHandle(fd, handleAudioEvents, flags & AIO_RW);
}
static sqInt sound_PlaySilence(void)
{
return 0;
}
static AuDeviceID choose_nas_device(AuServer *server, int samplesPerSec, int stereo, int recording)
{
int desiredDeviceKind=
recording ?
AuComponentKindPhysicalInput :
AuComponentKindPhysicalOutput;
int desired_channels= stereo ? 2 : 1;
int i;
/* look for a physical device of the proper kind, with the proper number of channels */
for (i = 0; i < AuServerNumDevices(server); i++) {
if ((AuDeviceKind(AuServerDevice(server, i))
== desiredDeviceKind)
&& (AuDeviceNumTracks(AuServerDevice(server, i))
== desired_channels))
return AuDeviceIdentifier(AuServerDevice(server, i));
}
/* look for a physical device of the proper kind; ignore number of channels */
for (i = 0; i < AuServerNumDevices(server); i++) {
if (AuDeviceKind(AuServerDevice(server, i))
== desiredDeviceKind)
return AuDeviceIdentifier(AuServerDevice(server, i));
}
return AuNone;
}
static sqInt sound_Start(sqInt frameCount, sqInt samplesPerSec, sqInt stereo0, sqInt semaIndex0)
{
AuElement elements[2]; /* first is a client element, second is
a device output element */
AuDeviceID device; /* ID of the device to play to */
/* open the server */
debugf("opening server\n");
server = AuOpenServer(NULL, 0, NULL, 0, NULL, NULL);
if (server == NULL) {
debugf("failed to open audio server\n");
return false;
}
/* XXX should check the protocol version! */
/* record requested info */
semaIndex = semaIndex0;
stereo = stereo0;
sampleRate= samplesPerSec;
/* pick a device to play to */
device = choose_nas_device(server, samplesPerSec, stereo, 0);
if (device == AuNone) {
debugf("no available device on the server!\n");
AuCloseServer(server);
server = NULL;
return false;
}
/* set up output parameters */
fmtBytes=2;
fmtSigned=1;
fmtStereo=stereo;
fmtIsBigendian=0;
recording=0;
/* create a flow to write on */
debugf("creating flow\n");
flow = AuCreateFlow(server, NULL);
/* create client and device elements to play with */
debugf("creating elements(%d,%d)\n", frameCount, frameCount / 4);
AuMakeElementImportClient(&elements[0],
samplesPerSec,
AuFormatLinearSigned16LSB, /* XXX this should be chosen based on the platform */
stereo ? 2 : 1,
AuTrue,
2*frameCount, /* max: 2 buffers */
frameCount, /* low */
0, NULL);
AuMakeElementExportDevice(&elements[1],
0,
device,
samplesPerSec,
AuUnlimitedSamples,
0, NULL);
/* set up the flow with these elements */
AuSetElements(server, flow,
AuTrue,
2, elements,
NULL);
/* start her up */
debugf("starting flow\n");
AuStartFlow(server, flow, NULL);
AuFlush(server);
/* initialize the space indication */
bytesAvail = 0;
/* arrange to be informed when events come in from the server */
aioEnable(AuServerConnectionNumber(server), 0, AIO_EXT);
aioHandle(AuServerConnectionNumber(server), handleAudioEvents, AIO_R);
return true;
}
/* StartRecording: open the device for recording.
XXX this routine is almost identical to snd_Start(). The two should
be factored into a single function!
*/
static sqInt sound_StartRecording(sqInt desiredSamplesPerSec, sqInt stereo0, sqInt semaIndex0)
{
AuElement elements[2]; /* elements for the NAS flow to assemble:
element 0 = physical input
element 1 = client export */
AuDeviceID device; /* physical device ID to use */
debugf("StartRecording\n");
sound_Stop();
debugf("opening server\n");
server = AuOpenServer(NULL, 0, NULL, 0, NULL, NULL);
if (server == NULL) {
debugf("failed to open audio server\n");
return false;
}
/* XXX check protocol version of the server */
semaIndex= semaIndex0;
stereo= stereo0;
sampleRate= desiredSamplesPerSec;
device= choose_nas_device(server, desiredSamplesPerSec, stereo, 1);
if (device == AuNone) {
debugf("no available device on the server!\n");
AuCloseServer(server);
server = NULL;
return false;
}
/* record format info */
fmtBytes=2;
fmtSigned=1;
fmtStereo=stereo;
fmtIsBigendian=0;
recording=1;
/* create a flow to read from */
debugf("creating flow\n");
flow = AuCreateFlow(server, NULL);
/* create client and device elements to record with */
debugf("creating elements\n");
AuMakeElementImportDevice(&elements[0],
desiredSamplesPerSec, /* XXX should use the actual sampling rate of device */
device,
AuUnlimitedSamples,
0, NULL);
AuMakeElementExportClient(&elements[1],
0,
desiredSamplesPerSec,
AuFormatLinearSigned16LSB, /* XXX this should be chosen based on the platform */
stereo ? 2 : 1,
AuTrue,
1000000, /* was AuUnlimitedSamples */
1000, /* water mark: go ahead and send frequently! */
0, NULL);
/* set up the flow with these elements */
AuSetElements(server, flow,
AuTrue,
2, elements,
NULL);
/* start her up */
debugf("starting flow\n");
AuStartFlow(server, flow, NULL);
AuFlush(server);
/* initialize the space indication */
bytesAvail = 0;
/* arrange to be informed when events come in from the server */
aioEnable(AuServerConnectionNumber(server), NULL, AIO_EXT);
aioHandle(AuServerConnectionNumber(server), handleAudioEvents, AIO_W);
return true;
}
static sqInt sound_StopRecording(void)
{
return sound_Stop();
}
static double sound_GetRecordingSampleRate(void)
{
return sampleRate;
}
static sqInt sound_RecordSamplesIntoAtLength(void *buf, sqInt startSliceIndex,
sqInt bufferSizeInBytes)
{
int bytesToRead;
int sliceSize= (stereo ? 4 : 2); /* a "slice" seems to be a "frame": one sample from each channel */
debugf("RecordSamplesIntoAtLength(buf=%d, startSliceIndex=%d, bufferSizeInBytes=%d\n",
buf, startSliceIndex, bufferSizeInBytes);
/* sanity checks */
if (server==NULL || !recording) {
success(false);
return 0;
}
if (bytesAvail <= 0)
return 0;
/* figure out how much to read */
bytesToRead= bufferSizeInBytes - (startSliceIndex * sliceSize);
if (bytesToRead > bytesAvail)
bytesToRead= bytesAvail;
debugf("reading %d bytes\n", bytesToRead);
/* read it */
AuReadElement(server,
flow,
1, /* element 1 is the client export */
bytesToRead,
(char *) (buf + startSliceIndex*sliceSize),
NULL);
bytesAvail -= bytesToRead;
return bytesToRead/sliceSize; /* return number of samples read (or slices?!) */
}
/* mixer settings */
static sqInt sound_SetRecordLevel(sqInt level)
{
return level;
}
static void sound_Volume(double *left, double *right)
{
return;
}
static void sound_SetVolume(double left, double right)
{
return;
}
static sqInt sound_SetSwitch(sqInt id, sqInt captureFlag, sqInt parameter)
{
return -1;
}
static sqInt sound_GetSwitch(sqInt id, sqInt captureFlag, sqInt channel)
{
return -1;
}
static sqInt sound_SetDevice(sqInt id, char *arg)
{
return -1;
}
#include "SqSound.h"
SqSoundDefine(NAS);
#include "SqModule.h"
static void sound_parseEnvironment(void) {}
static int sound_parseArgument(int argc, char **argv)
{
if (!strcmp(argv[0], "-nas")) return 1;
return 0;
}
static void sound_printUsage(void) {}
static void sound_printUsageNotes(void) {}
static void *sound_makeInterface(void) { return &sound_NAS_itf; }
SqModuleDefine(sound, NAS);
|