File: sgi_dac.C.old

package info (click to toggle)
mixviews 1.20-10.1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 2,928 kB
  • ctags: 5,960
  • sloc: cpp: 32,879; ansic: 2,110; makefile: 445; sh: 17
file content (723 lines) | stat: -rw-r--r-- 20,382 bytes parent folder | download | duplicates (2)
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
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
// sgi_dac.C

/******************************************************************************
 *
 *  MiXViews - an X window system based sound & data editor/processor
 *
 *  Copyright (c) 1993, 1994 Regents of the University of California
 *
 *  Author:     Douglas Scott
 *  Date:       December 13, 1994
 *
 *  Permission to use, copy and modify this software and its documentation
 *  for research and/or educational purposes and without fee is hereby granted,
 *  provided that the above copyright notice appear in all copies and that
 *  both that copyright notice and this permission notice appear in
 *  supporting documentation. The author reserves the right to distribute this
 *  software and its documentation.  The University of California and the author
 *  make no representations about the suitability of this software for any 
 *  purpose, and in no event shall University of California be liable for any
 *  damage, loss of data, or profits resulting from its use.
 *  It is provided "as is" without express or implied warranty.
 *
 ******************************************************************************/


#ifdef __GNUG__
#pragma implementation
#endif

#include <math.h>
#include <errno.h>
#include <unistd.h>
#include <signal.h>

#include "sgi_dac.h"
#include "application.h"
#include "statusaction.h"
#include "typeconvert.h"

#undef DEBUG_SGI

boolean SGIConverter::dontInterrupt = false;
int SGIConverter::inputDevice = AL_DEFAULT_INPUT;
int SGIConverter::outputDevice = AL_DEFAULT_OUTPUT;
int SGIConverter::inputDeviceCount = 0;
int SGIConverter::outputDeviceCount = 0;

SGIConverter::SGIConverter()
	: inPortConfig(nil), outPortConfig(nil), audio_port(nil) {
    if(initialize())
        catchSignals(true);
    else
        fail();
}

SGIConverter::~SGIConverter() {
    resetDefaults();
    if(inPortConfig)
        alFreeConfig(inPortConfig);
    if(outPortConfig)
        alFreeConfig(outPortConfig);
    if(audio_port)
        alClosePort(audio_port);
    catchSignals(false);
}

int
SGIConverter::inputDeviceToIndex(int deviceID) const {
    int value = -1;
    for (int i = 0; i < inputDeviceCount; i++) {
	if (deviceID == inputDevices[i].resourceID) {
	    value = i;
	    break;
	}
    }
    return value;
}

int
SGIConverter::outputDeviceToIndex(int deviceID) const {
    int value = -1;
    for (int i = 0; i < outputDeviceCount; i++) {
	if (deviceID == outputDevices[i].resourceID) {
	    value = i;
	    break;
	}
    }
    return value;
}

ChoiceValue
SGIConverter::inputDeviceToChoice(int deviceID) const {
    ChoiceValue value = 0;
    int ndx = inputDeviceToIndex(deviceID);
    if (ndx >= 0)
        value = inputDevices[ndx].choiceIndex;
    return value;
}

ChoiceValue
SGIConverter::outputDeviceToChoice(int deviceID) const {
    ChoiceValue value = 0;
    int ndx = outputDeviceToIndex(deviceID);
    if (ndx >= 0)
        value = outputDevices[ndx].choiceIndex;
    return value;
}

int
SGIConverter::inputChoiceToDevice(ChoiceValue index) const {
    int value = -1;
    for (int i = 0; i < inputDeviceCount; i++) {
	if (index == inputDevices[i].choiceIndex) {
	    value = inputDevices[i].resourceID;
	    break;
	}
    }
    return value;
}

int
SGIConverter::outputChoiceToDevice(ChoiceValue index) const {
    int value = -1;
    for (int i = 0; i < outputDeviceCount; i++) {
	if (index == outputDevices[i].choiceIndex) {
	    value = outputDevices[i].resourceID;
	    break;
	}
    }
    return value;
}

int
SGIConverter::displayError(const char *msg) {
    Application::alert(msg ? msg : "Error:", alGetErrorString(oserror()));
    return false;
}

int
SGIConverter::initialize() {
    BUG("SGIConverter::initialize()");
    alSetErrorHandler(nil);        // SGIConverter will handle errors
    inPortConfig = alNewConfig();
    outPortConfig = alNewConfig();
    if(!inPortConfig || !outPortConfig)
        return displayError("alNewConfig:");
    else
        return getDefaults();
}

MXINT32
SGIConverter::getParameter(MXINT32 param) const {
    ALpv pv;
    pv.param = param;
    int stat = alGetParams(currentDevice(), &pv, 1);
#ifdef DEBUG_SGI
    if (stat != 0) fprintf(stderr, "alGetParams: %s\n", alGetErrorString(oserror()));
#endif
    return (MXINT32) pv.value.ll;
}

int
SGIConverter::setParameter(MXINT32 param, INT32 value) {
    pvarray[0].param = param;
    pvarray[0].value.i = value;
    return (alSetParams(currentDevice(), &pvarray[0], 1) == 0) ?
	    true : displayError("setParameter:");
}

char *strdup(const char *str) {
    char *s = new char[strlen(str) + 1];
    strcpy(s, str);
    return s;
}

int
SGIConverter::getDefaults() {
#ifdef AL_SYSTEM
#ifndef STRSIZE
#define STRSIZE 32
#endif
    // Find all the audio devices on the system as well as the default
    int         nValues;
    ALvalue     alValues[32];
    ALpv        qual;
    char        str[STRSIZE];   /* 32 is magic number from AL */

    // Do Inputs

    qual.param   = AL_TYPE;
    qual.value.i = AL_INPUT_DEVICE_TYPE;

    nValues = alQueryValues(AL_SYSTEM, AL_DEVICES, alValues, 16, &qual, 1);
    nValues++;          /* need and extra for "Default In" */

    if (nValues > 0) {
	ALpv pv[2];
        ALparamInfo paramInfo;

        inputDeviceCount = nValues;
        inputDevices[0].label = strdup("Default Input");
        inputDevices[0].resourceID = AL_DEFAULT_INPUT;
	inputDevices[0].choiceIndex = 1;
        alGetParamInfo(AL_DEFAULT_INPUT, AL_GAIN, &paramInfo); 
        inputDevices[0].maxGain = alFixedToDouble(paramInfo.max.ll); 
        inputDevices[0].minGain = alFixedToDouble(paramInfo.min.ll); 

        pv[0].param     = AL_LABEL;
        pv[0].value.ptr = str;
        pv[0].sizeIn    = STRSIZE;

        for (int i = 1; i < nValues && i < 8; i++) {
	    int res = alValues[i-1].i;
            if (alGetParams(res, pv, 1) != 1) {
                fprintf(stderr, "alGetParams failed\n");
		return false;
            }
            inputDevices[i].label = strdup(str);
            inputDevices[i].resourceID = res;
	    inputDevices[i].choiceIndex = 2 << (i - 1);

            alGetParamInfo(res, AL_GAIN, &paramInfo); 
            inputDevices[i].maxGain = alFixedToDouble(paramInfo.max.ll); 
            inputDevices[i].minGain = alFixedToDouble(paramInfo.min.ll); 
        }
    }

    // Do Outputs

    qual.param   = AL_TYPE;
    qual.value.i = AL_OUTPUT_DEVICE_TYPE;

    nValues = alQueryValues(AL_SYSTEM, AL_DEVICES, alValues, 16, &qual, 1);
    nValues++;          /* need and extra for "Default out" */
    if (nValues > 0) {
	ALpv pv[2];
        ALparamInfo paramInfo;
        outputDeviceCount = nValues;
        outputDevices[0].label = strdup("Default Output");
        outputDevices[0].resourceID = AL_DEFAULT_OUTPUT;
	outputDevices[0].choiceIndex = 1;
        alGetParamInfo(AL_DEFAULT_OUTPUT, AL_GAIN, &paramInfo); 
        outputDevices[0].maxGain = alFixedToDouble(paramInfo.max.ll); 
        outputDevices[0].minGain = alFixedToDouble(paramInfo.min.ll); 

        pv[0].param     = AL_LABEL;
        pv[0].value.ptr = str;
        pv[0].sizeIn    = STRSIZE;

        for (int i = 1; i < nValues && i < 8; i++) {
	    int res = alValues[i-1].i;
            if (alGetParams(res, pv, 1) != 1) {
                fprintf(stderr, "alGetParams failed\n");
		return false;
            }
            outputDevices[i].label = strdup(str);
            outputDevices[i].resourceID = res;
	    outputDevices[i].choiceIndex = 2 << (i - 1);

            alGetParamInfo(res, AL_GAIN, &paramInfo); 
            outputDevices[i].maxGain = alFixedToDouble(paramInfo.max.ll); 
            outputDevices[i].minGain = alFixedToDouble(paramInfo.min.ll); 
        }
    }
#endif
    return true;
}

void
SGIConverter::resetDefaults() {
}

boolean
SGIConverter::isPlayableFormat(DataType type) {
    return (type != UnsignedCharData
        && type != MuLawData && type != IntData);
}

int
SGIConverter::checkDataType(DataType type) {
    MXINT32 sampwidth = AL_SAMPLE_16;
    MXINT32 format;
    switch(type) {
    case FloatData: format = AL_SAMPFMT_FLOAT;  break;
    case DoubleData: format = AL_SAMPFMT_DOUBLE;  break;
    case ShortData:  format = AL_SAMPFMT_TWOSCOMP;  break;
    case SignedCharData:
        format = AL_SAMPFMT_TWOSCOMP;
        sampwidth = AL_SAMPLE_8;
        break;
    }
    if(alSetSampFmt(currentConfig(), format) < 0
       || alSetWidth(currentConfig(), sampwidth) < 0)
        return displayError("alSetSampFmt/alSetWidth:");
    else if(type >= FloatData)
        alSetFloatMax(currentConfig(), peakAmplitude());
    return true;
}

int
SGIConverter::checkSampleRate(int sampleRate) {
    pvarray[0].param = AL_RATE;
    pvarray[0].value.ll = alDoubleToFixed((double) sampleRate);
    pvarray[1].param = AL_MASTER_CLOCK;
    pvarray[1].value.i = AL_CRYSTAL_MCLK_TYPE;
    if (alSetParams(currentDevice(), pvarray, 2) < 0)
	return displayError("checkSampleRate: alSetParams:");
    return true;
}

int
SGIConverter::checkChannels(int chans) {
    if (alSetChannels(currentConfig(), chans) < 0)
	return displayError("checkChannels: alSetChannels:");
    return true;
}

// Needed to redefine this here to check port use before doing any config

int
SGIConverter::configure(Sound* sound, GoingTo wantTo) {
    // need to determine whether audio is in use (other than us).
    if(dontInterrupt && getParameter(AL_OUTPUT_COUNT) > 1) {
        Application::alert("Audio hardware currently in use.");
        return false;
    }
    return Super::configure(sound, wantTo);
}

int
SGIConverter::doConfigure() {
    BUG("SGIConverter::doConfigure()");
#ifdef DEBUG_SGI
    fprintf(stderr, "Queue size set to %ld.\n", bestQueueSize());
#endif
    if(alSetQueueSize(currentConfig(), bestQueueSize()) < 0)
        return displayError("alSetQueueSize:");
    if(audio_port) alClosePort(audio_port);
    if((audio_port = alOpenPort("mxv", willRecord() ? "r" : "w",
				currentConfig())) == 0) {
	return displayError("Failed to open audio port:");
    }
    if(alSetFillPoint(audio_port, bestQueueSize()/4) < 0)
        return displayError("alSetFillPoint:");
    return true;
}

// read and write 1/10 second's worth of sound, so queue is 5x
// that size, or 1/2 second

MXINT32
SGIConverter::bestQueueSize() {
    return round(sampleRate() / 2.0);
}

MXINT32
SGIConverter::writeSamps() {
    return channels() * (bestQueueSize() / 5);
}

MXINT32
SGIConverter::readSamps() {
    return channels() * (bestQueueSize() / 5);
}

inline MXINT32 min(INT32 x, long y) { return (x < y) ? x : y; }

int
SGIConverter::doConversion(StatusAction* askedToStop) {
    BUG("SGIConverter::doConversion()");
    int sampsize = sampleSize();
    int chans = channels();
    int nframes = dataSize() / (sampsize * chans); // total frames to write
    int frames = writeSamps() / chans;         // size of each write in frames
    frames = min(frames, nframes);
    int bufsize = frames * chans * sampsize;  // size of each write in bytes
#ifdef DEBUG_SGI
    fprintf(stderr, "Playing %ld frames = %ld bytes\n", nframes,
            nframes * chans * sampsize);
    fprintf(stderr, "Buffer is %ld frames = %ld bytes\n", frames, bufsize);
    fprintf(stderr, "Starting conversion...\n");
#endif
    lockMemory();
    addr databuffer = addr(pointerToData());
    Application::inform("Playing...");
    boolean stopped = false;
    while(nframes > 0) {
        if((*askedToStop)()) {
            stopped = true;
            break;
        }
        if(alGetFillable(audio_port) < frames)
            continue;
#ifdef DEBUG_SGI
        fprintf(stderr, "\twriting %d frames...\n", frames);
#endif
        if(alWriteFrames(audio_port, databuffer, frames) < 0)
            return displayError("Error in alWriteFrames():");
#ifdef DEBUG_SGI
        fprintf(stderr, "\t%d frames written\n", frames);
#endif
        databuffer += bufsize;
        nframes -= frames;
        frames = min(frames, nframes);
#ifdef DEBUG_SGI
        fprintf(stderr, "\tnframes now %d, frames now %d\n", nframes, frames);
#endif
    }
    if(!stopped) waitForStop(askedToStop);
    return stop();
}

int
SGIConverter::doRecording(StatusAction* askedToStop) {
    int sampsize = sampleSize();
    int chans = channels();
    int nframes = dataSize() / (sampsize * chans); // total frames to read
    int frames = readSamps() / chans;         // size of each read in frames
    frames = min(frames, nframes);
    int bufsize = frames * chans * sampsize;  // size of each read in bytes
#ifdef DEBUG_SGI
    fprintf(stderr, "Recording %ld frames = %ld bytes\n",
            nframes, nframes * chans * sampsize);
    fprintf(stderr, "Buffer is %ld frames = %ld bytes\n", frames, bufsize);
#endif
    lockMemory();
    char *databuffer = (char *) pointerToData();
    Application::inform("Recording...");
    while(nframes > 0) {
        if((*askedToStop)())
            return stop();
        if(alReadFrames(audio_port, databuffer, frames) < 0)
            return displayError("Error in alReadFrames():");
        databuffer += bufsize;
        nframes -= frames;
#ifdef DEBUG_SGI
        fprintf(stderr, "\t%d frames read, nframes= %d\n", frames, nframes);
#endif
        frames = min(frames, nframes);
    }
#ifdef DEBUG_SGI
        fprintf(stderr, "At bottom, calling stop()\n");
#endif
    stop();
    return true;
}

int
SGIConverter::setInputDevice(int device) {
    inputDevice = device;
#ifdef DEBUG_SGI
    fprintf(stderr, "setInputDevice: device = %d\n", device);
#endif
    return (alSetDevice(inPortConfig, device) == 0);
}

int
SGIConverter::setOutputDevice(int device) {
    outputDevice = device;
#ifdef DEBUG_SGI
    fprintf(stderr, "setOutputDevice: device = %d\n", device);
#endif
    return alSetDevice(outPortConfig, device);
}

int SGIConverter::currentDevice() const {
    return willRecord() ? currentInputDevice() : currentOutputDevice();
}

ALconfig SGIConverter::currentConfig() const {
    return willRecord() ? inPortConfig : outPortConfig;
}

int
SGIConverter::currentPlayLevel() const {
    ALfixed gain[8];
    ALpv x[4];
    /* Now get the current value of gain */
    x[0].param = AL_GAIN;
    x[0].value.ptr = gain;
    x[0].sizeIn = 8;           // we've provided an 8-channel vector
    x[1].param = AL_CHANNELS;
    if (alGetParams(currentOutputDevice(), x, 2) < 0) {
#ifdef DEBUG_SGI
	fprintf(stderr, "alGetParams: %s\n", alGetErrorString(oserror()));
#endif
        return 0;
    }
    double decibels = alFixedToDouble(gain[0]);
#ifdef DEBUG_SGI
    fprintf(stderr, "currentPlayLevel was %g decibels\n", decibels);
#endif
    double mindB =
        outputDevices[outputDeviceToIndex(currentOutputDevice())].minGain;
    double maxdB =
        outputDevices[outputDeviceToIndex(currentOutputDevice())].maxGain;
    if (maxdB > mindB) {
	double frac = (decibels - mindB) / (maxdB - mindB);
	return (100 * frac);
    }
    else
        return (100);
}

int
SGIConverter::currentRecordLevel() const {
    ALfixed gain[8];
    ALpv x[4];
    /* Now get the current value of gain */
    x[0].param = AL_GAIN;
    x[0].value.ptr = gain;
    x[0].sizeIn = 8;           // we've provided an 8-channel vector
    x[1].param = AL_CHANNELS;
    if (alGetParams(currentInputDevice(), x, 2) < 0) {
#ifdef DEBUG_SGI
	fprintf(stderr, "alGetParams: %s\n", alGetErrorString(oserror()));
#endif
        return 0;
    }
    double decibels = alFixedToDouble(gain[0]);
#ifdef DEBUG_SGI
    fprintf(stderr, "currentRecordLevel was %g decibels\n", decibels);
#endif
    double mindB =
        inputDevices[inputDeviceToIndex(currentInputDevice())].minGain;
    double maxdB =
        inputDevices[inputDeviceToIndex(currentInputDevice())].maxGain;
    if (maxdB > mindB) {
	double frac = (decibels - mindB) / (maxdB - mindB);
	return (100 * frac);
    }
    else
        return (100);
}

boolean
SGIConverter::setPlayLevel(int volume){
    double mindB =
        outputDevices[outputDeviceToIndex(currentOutputDevice())].minGain;
    double maxdB =
        outputDevices[outputDeviceToIndex(currentOutputDevice())].maxGain;
    if (maxdB <= mindB)
        return 0;
    double targetdB = volume/100.0 * (maxdB - mindB) + mindB;
    ALfixed gain[8];
    ALpv x;
    for(int i=0; i < 8; i++) gain[i] = alDoubleToFixed(targetdB);
    x.param = AL_GAIN;
    x.value.ptr = gain;
    x.sizeIn = 8;           // we've provided an 8-channel vector
    if (alSetParams(currentOutputDevice(), &x, 1) < 0) {
#ifdef DEBUG_SGI
	fprintf(stderr, "setPlayLevel: alSetParams: %s\n",
		alGetErrorString(oserror()));
#endif
        return 0;
    }
    return 1;
}

boolean
SGIConverter::setRecordLevel(int volume){
    double mindB =
        inputDevices[inputDeviceToIndex(currentInputDevice())].minGain;
    double maxdB =
        inputDevices[inputDeviceToIndex(currentInputDevice())].maxGain;
    if (maxdB <= mindB)
        return 0;

    double targetdB = volume/100.0 * (maxdB - mindB) + mindB;
    ALfixed gain[8];
    ALpv x;
    for(int i=0; i < 8; i++) gain[i] = alDoubleToFixed(targetdB);
    x.param = AL_GAIN;
    x.value.ptr = gain;
    x.sizeIn = 8;           // we've provided an 8-channel vector
    if (alSetParams(currentInputDevice(), &x, 1) < 0) {
#ifdef DEBUG_SGI
	fprintf(stderr, "setRecordLevel: alSetParams: %s\n",
		alGetErrorString(oserror()));
#endif
        return 0;
    }
    return 1;
}

boolean
SGIConverter::setSpeakerOutput(boolean isOn) {
    return setParameter(AL_MUTE, !isOn);
}

int
SGIConverter::pause() {
    return false;
}

int
SGIConverter::resume() {
    return false;
}

int
SGIConverter::stop() {
    BUG("SGIConverter::stop()");
    int status = true;
    if(running()) {
#ifdef DEBUG_SGI
        MXINT32 buf[10];
        buf[0] = AL_ERROR_NUMBER;
        buf[2] = AL_ERROR_TYPE;
        buf[4] = AL_ERROR_LENGTH;
        buf[6] = AL_ERROR_LOCATION_LSP;
        buf[8] = AL_ERROR_LOCATION_MSP;
        if(audio_port) {
            ALgetstatus(audio_port, buf, 10);
            if(buf[1] > 0)
                fprintf(stderr, "Last audio port error:  %d at frame %d: %s\n",
                        buf[3], buf[5], alGetErrorString(buf[3]));
        }
#endif
        if(audio_port) alClosePort(audio_port);
        unlockMemory();
        resetDefaults();
        status = Super::stop();
    }
    return status;
}

int
SGIConverter::lockMemory() {
    // fix memory in RAM to avoid swapping
    return (mpin((void *) pointerToData(), unsigned(dataSize())) == 0);    
}

int
SGIConverter::unlockMemory() {
    // let it swap
    return (munpin((void *) pointerToData(), unsigned(dataSize())) == 0);    
}

int
SGIConverter::waitForStop(StatusAction* askedToStop) {
    BUG("SGIConverter::waitForStop()");
    while(alGetFilled(audio_port) > 0) {    
        if((*askedToStop)())
            break;
        sginap(1);        // allow audio buffer to drain
    }
    return true;
}

int
SGIConverter::sampleSize() {
    return type_to_sampsize(dataType());
}

#ifdef __GNUG__
typedef void (*SignalFun)(...);
#elif defined(sgi) 
#if defined(__sigargs)
typedef void (*SignalFun)(__sigargs);    // since this varies with OS version
#else
typedef void (*SignalFun)(...);
#endif /* sgi */
#else
typedef void (*SignalFun)(int);
#endif

void
SGIConverter::catchSignals(boolean flag) {
    if(flag) {
        signal(SIGHUP, SignalFun(signalExit));
        signal(SIGINT, SignalFun(signalExit));
        signal(SIGQUIT, SignalFun(signalQuit));
        signal(SIGBUS, SignalFun(signalQuit));
        signal(SIGSEGV, SignalFun(signalQuit));
        signal(SIGTERM, SignalFun(signalExit));
    }
    else {
        signal(SIGHUP, SIG_DFL);
        signal(SIGINT, SIG_DFL);
        signal(SIGQUIT, SIG_DFL);
        signal(SIGBUS, SIG_DFL);
        signal(SIGSEGV, SIG_DFL);
        signal(SIGTERM, SIG_DFL);
    }
}

void
SGIConverter::ignoreSignals(boolean flag) {
    if(flag) {
#ifdef SIGTSTP
        signal(SIGTSTP, SignalFun(signalIgnore));
#endif
    }
    else {
#ifdef SIGTSTP
        signal(SIGTSTP, SIG_DFL);
#endif
    }
}

// these are declared static to allow passing to signal()

void
SGIConverter::signalExit(...) {
    fprintf(stderr, "\nCaught signal.  Exiting...\n");
    fflush(stderr);
    Converter::destroyInstance();
    exit(0);
}

void
SGIConverter::signalIgnore(...) {
    fprintf(stderr, "\nCaught signal during play or record...ignoring.\n");
}

void
SGIConverter::signalQuit(...) {
    fprintf(stderr, "\nCaught deadly signal.  Aborting...\n");
    fflush(stderr);
    abort();
}