aboutsummaryrefslogtreecommitdiff
path: root/desiredata/src/s_audio_sgi.c
blob: 878cf2558400e5815e5201192d8a7027e02704f0 (plain)
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
/* ----------------------- Experimental routines for SGI -------------- */
/* written by Olaf Matthes <olaf.matthes@gmx.de> */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <m_pd.h>
#include <s_stuff.h>

#include <dmedia/audio.h>
#include <dmedia/midi.h>
#include <sys/fpu.h>
#include <errno.h>

#define SGI_MAXDEV 4    /* it's just 3, but default counts as well... */
#define SGI_MAXCH 12    /* max. number of channels - is this enough? */

static ALport sgi_iport[SGI_MAXDEV];
static ALport sgi_oport[SGI_MAXDEV];
static ALconfig sgi_inconfig;
static ALconfig sgi_outconfig;
static int sgi_nindevs, sgi_noutdevs;
static int sgi_inchannels, sgi_outchannels;
static int sgi_ninchans[SGI_MAXDEV], sgi_noutchans[SGI_MAXDEV];

/* set the special "flush zero" but (FS, bit 24) in the Control Status Register of the FPU of R4k and beyond
    so that the result of any underflowing operation will be clamped to zero, and no exception of any kind will
    be generated on the CPU. thanks to cpirazzi@cp.esd.sgi.com (Chris Pirazzi). */
static void sgi_flush_all_underflows_to_zero () {
    union fpc_csr f;
    f.fc_word = get_fpc_csr();
    f.fc_struct.flush = 1;
    set_fpc_csr(f.fc_word);
}

/* convert the most common errors into readable strings */
static char *sgi_get_error_message(int err) {
        switch (err) {
                case AL_BAD_CONFIG:        return "Invalid config";
                case AL_BAD_DIRECTION:     return "Invalid direction (neither \"r\" nor \"w\")";
                case AL_BAD_OUT_OF_MEM:    return "Not enough memory";
                case AL_BAD_DEVICE_ACCESS: return "Audio hardware is not available or is improperly configured";
                case AL_BAD_DEVICE:        return "Invalid device";
                case AL_BAD_NO_PORTS:      return "No audio ports available";
                case AL_BAD_QSIZE:         return "Invalid fifo size";
                case AL_BAD_SAMPFMT:       return "Invalid sample format";
                case AL_BAD_FLOATMAX:      return "Invalid float maximum";
                case AL_BAD_WIDTH:         return "Invalid sample width";
                default:                   return "Unknown error";
        }
}

int sgi_open_audio(int nindev,  int *indev,  int nchin,  int *chin,
int noutdev, int *outdev, int nchout, int *chout, int rate/*, int dummy*/) {
    ALpv pvbuf[2];
    int num_devs = 0;
    int in_dev = 0;
    int out_dev = 0;
    int inchans = 0;
    int outchans = 0;
    int err, n, gotchannels;
    char *indevnames[4] = {"DefaultIn", "AnalogIn", "AESIn", "ADATIn"};
    char *outdevnames[4] = {"DefaultOut", "AnalogOut", "AESOut", "ADATOut"};
    sgi_flush_all_underflows_to_zero();
    if (sys_verbose) post("opening sound input...");
    for (n = 0; n < nindev; n++) {
        int gotchannels = 0;
        if (indev[n] >= 0 && indev[n] < SGI_MAXDEV) {
            if (sys_verbose) post("opening %s", indevnames[indev[n]]);
            in_dev = alGetResourceByName(AL_SYSTEM, indevnames[indev[n]], AL_DEVICE_TYPE);
        } else {
            if(sys_verbose)post("opening %s", indevnames[0]);
            in_dev = AL_DEFAULT_INPUT;
        }
        if (!in_dev) {
            error("%s", sgi_get_error_message(in_dev));
            continue;       /* try next device, if any */
        }
        sgi_inconfig = alNewConfig();
        alSetSampFmt(sgi_inconfig, AL_SAMPFMT_FLOAT);
        alSetFloatMax(sgi_outconfig, 1.1f);
        alSetChannels(sgi_outconfig, chin[n]);
        alSetQueueSize(sgi_inconfig, sys_advance_samples * chin[n]);
        alSetDevice(sgi_inconfig, in_dev);
        sgi_iport[n] = alOpenPort("Pd input port", "r", sgi_inconfig);
        if (!sgi_iport[n]) error("failed to open audio read port");
        /* try to set samplerate */
        pvbuf[0].param = AL_RATE;
        pvbuf[0].value.ll = alDoubleToFixed(rate);
        if ((err = alSetParams(in_dev, pvbuf, 1)) < 0) {
            post("could not set specified sample rate for input (%s)", sgi_get_error_message(err));
            if(pvbuf[0].sizeOut < 0) post("rate was invalid");
        }
        /* check how many channels we actually got */
        pvbuf[0].param = AL_CHANNELS;
        if (alGetParams(in_dev, pvbuf, 1) < 0) {
            post("could not figure out how many input channels we got");
            gotchannels = chin[n];  /* assume we got them all */
        } else {
            gotchannels = pvbuf[0].value.i;
        }
        inchans += gotchannels; /* count total number of channels */
        sgi_ninchans[n] = gotchannels;  /* remember channels for this device */
    }
    if (sys_verbose) post("opening sound output...");
    for (n = 0; n < noutdev; n++) {
        if (outdev[n] >= 0 && outdev[n] < SGI_MAXDEV) {
            if(sys_verbose)post("opening %s", outdevnames[outdev[n]]);
            out_dev = alGetResourceByName(AL_SYSTEM, outdevnames[outdev[n]], AL_DEVICE_TYPE);
        } else {
            if (sys_verbose) post("opening %s", outdevnames[0]);
            out_dev = AL_DEFAULT_OUTPUT;
        }
        if (!out_dev) {
            error("%s", sgi_get_error_message(out_dev));
            continue; /* try next device, if any */
        }
        /* configure the port before opening it */
        sgi_outconfig = alNewConfig();
        alSetSampFmt(sgi_outconfig, AL_SAMPFMT_FLOAT);
        alSetFloatMax(sgi_outconfig, 1.1f);
        alSetChannels(sgi_outconfig, chout[n]);
        alSetQueueSize(sgi_outconfig, sys_advance_samples * chout[n]);
        alSetDevice(sgi_outconfig, out_dev);
        /* open the port */
        sgi_oport[n] = alOpenPort("Pd ouput port", "w", sgi_outconfig);
        if (!sgi_oport[n]) error("failed to open audio write port");
        /* now try to set sample rate */
        pvbuf[0].param = AL_RATE;
        pvbuf[0].value.ll = alDoubleToFixed(rate);
        if ((err = alSetParams(out_dev, pvbuf, 1)) < 0) {
            post("could not set specified sample rate for output (%s)", sgi_get_error_message(err));
            if(pvbuf[0].sizeOut < 0) post("rate was invalid");
        }
        /* check how many channels we actually got */
        pvbuf[0].param = AL_CHANNELS;
        if (alGetParams(out_dev, pvbuf, 1) < 0) {
            post("could not figure out how many output channels we got");
            gotchannels = chout[n];
        } else {
            gotchannels = pvbuf[0].value.i;
        }
        outchans += gotchannels;
        sgi_noutchans[n] = gotchannels;
    }
    sgi_noutdevs = noutdev;
    sgi_nindevs = nindev;
    sgi_inchannels = inchans;
    sgi_outchannels = outchans;
    return !(inchans || outchans);
}

void sgi_close_audio() {
    int err, n;
    for (n = 0; n < sgi_nindevs; n++) {
        if (sgi_iport[n]) {
            err = alClosePort(sgi_iport[n]);
            if (err < 0) error("closing input %d: %s (%d)", n + 1, alGetErrorString(oserror()), err);
        }
    }
    for (n = 0; n < sgi_noutdevs; n++) {
        if (sgi_oport[n]) {
            err = alClosePort(sgi_oport[n]);
            if (err < 0) error("closing output %d: %s (%d)", n + 1, alGetErrorString(oserror()), err);
        }
    }
}

/* call this only if both input and output are open */
static void sgi_checkiosync() {
//    int i, result, checkit = 1, giveup = 1000, alreadylogged = 0;
//    long indelay, outdelay, defect;
//    if (!(sgi_outchannels && sgi_inchannels)) return;
}

int sgi_send_dacs() {
    float buf[SGI_MAXCH * sys_dacblocksize], *fp1, *fp2, *fp3, *fp4;
    static int xferno = 0;
    static int callno = 0;
    static double timenow;
    double timelast;
    int inchannels =   min(sys_inchannels, sgi_inchannels);
    int outchannels = min(sys_outchannels,sgi_outchannels);
    long outfill[SGI_MAXDEV], infill[SGI_MAXDEV];
    int outdevchannels, indevchannels;
    int i, n, channel;
    int result;
    unsigned int outtransfersize = sys_dacblocksize;
    unsigned int intransfersize = sys_dacblocksize;
    /* no audio channels open, return */
    if (!inchannels && !outchannels) return SENDDACS_NO;
    timelast = timenow;
    timenow = sys_getrealtime();
#ifdef DEBUG_SGI_XFER
    if (timenow - timelast > 0.050) post("(%d)", (int)(1000 * (timenow - timelast)));
#endif
    callno++;
    sgi_checkiosync(); /* check I/O are in sync and data not late */
    /* check whether there is enough space in buffers */
    if (sgi_nindevs) {
        for (n = 0; n < sgi_nindevs; n++) {
            if (alGetFilled(sgi_iport[n]) < intransfersize)
            return SENDDACS_NO;
        }
    }
    if (sgi_noutdevs) {
        for(n = 0; n < sgi_noutdevs; n++) {
            if (alGetFillable(sgi_oport[n]) < outtransfersize)
            return SENDDACS_NO;
        }
    }
    /* output audio data, if we use audio out */
    if (sgi_noutdevs) {
        fp2 = sys_soundout;             /* point to current output position in buffer */
        for(n = 0; n < sgi_noutdevs; n++) {
            outdevchannels = sgi_noutchans[n];      /* channels supported by this device */
            for (channel = 0, fp1 = buf; channel < outdevchannels; channel++, fp1++, fp2 += sys_dacblocksize) {
                for (i = 0, fp3 = fp1, fp4 = fp2; i < sys_dacblocksize; i++, fp3 += outdevchannels, fp4++) *fp3 = *fp4, *fp4 = 0;
            }
            alWriteFrames(sgi_oport[n], buf, sys_dacblocksize);
        }
    }
    /* zero out the output buffer */
    memset(sys_soundout, 0, sys_dacblocksize * sizeof(*sys_soundout) * sys_outchannels);
    if (sys_getrealtime() - timenow > 0.002) {
    #ifdef DEBUG_SGI_XFER
        post("output %d took %d msec", callno, (int)(1000 * (timenow - timelast)));
    #endif
        timenow = sys_getrealtime();
        sys_log_error(ERR_DACSLEPT);
    }
    /* get audio data from input, if we use audio in */
    if (sgi_nindevs) {
        fp2 = sys_soundin;              /* point to current input position in buffer */
        for (n = 0; n < sgi_nindevs; n++) {
            indevchannels = sgi_ninchans[n];        /* channels supported by this device */
            if (alGetFilled(sgi_iport[n]) > sys_dacblocksize) {
                alReadFrames(sgi_iport[n], buf, sys_dacblocksize);
            } else /* have to read but nothing's there... */ {
                // if (sys_verbose) post("extra ADC buf");
                /* set buffer to silence */
                memset(buf, 0, intransfersize * sizeof(*sys_soundout) * sgi_ninchans[n]);
            }
            for (channel = 0, fp1 = buf; channel < indevchannels; channel++, fp1++, fp2 += sys_dacblocksize) {
                for (i = 0, fp3 = fp1, fp4 = fp2; i < sys_dacblocksize; i++, fp3 += indevchannels, fp4++) *fp4 = *fp3;
            }
        }
    }
    xferno++;
    if (sys_getrealtime() - timenow > 0.002) {
#ifdef DEBUG_SGI_XFER
        post("routine took %d msec", int(1000 * (sys_getrealtime() - timenow)));
#endif
        sys_log_error(ERR_ADCSLEPT);
    }
    return SENDDACS_YES;
}

void sgi_getdevs(char *indevlist, int *nindevs, char *outdevlist, int *noutdevs, int *canmulti, int maxndev, int devdescsize) {
#if 0
    ALpv pvs[1];
    char name[32];
    int i, ndev, err;
    *canmulti = 3;  /* supports multiple devices */
    /* get max. number of audio ports from system */
    pvs[0].param = AL_DEVICES;
    err = alGetParams(AL_SYSTEM, pvs, 1);
    if (err < 0) {
        sprintf("alGetParams failed: %s\n", alGetErrorString(oserror()));
    }
    ndev = pvs[0].value.i;
    for (i = 0; i < ndev; i++) {
        pvs[0].param = AL_NAME;  /* a string parameter */
        pvs[0].value.ptr = name; /* the string we've allocated */
        pvs[0].sizeIn = 32;      /* the array size, in characters, including space for NULL */
        if (alGetParams(i, pvs, 1) < 0) {
            sprintf("alGetParams failed: %s\n", alGetErrorString(oserror()));
        }
        sprintf(indevlist + i * devdescsize, "%d: %s", i + 1, name);
        sprintf(outdevlist + i * devdescsize, "%d: %s", i + 1, name);
    }
    *nindevs = ndev;
    *noutdevs = ndev;
#else
    sprintf( indevlist + 0 * devdescsize, "Default In");
    sprintf(outdevlist + 0 * devdescsize, "Default Out");
    sprintf( indevlist + 1 * devdescsize, "Analog In");
    sprintf(outdevlist + 1 * devdescsize, "Analog Out");
    sprintf( indevlist + 2 * devdescsize, "AES In");
    sprintf(outdevlist + 2 * devdescsize, "AES Out");
    sprintf( indevlist + 3 * devdescsize, "ADAT In");
    sprintf(outdevlist + 3 * devdescsize, "ADAT Out");
    *nindevs = 4;
    *noutdevs = 4;
    *canmulti = 3;  /* supports multiple devices */
#endif
}

/* list devices: only reflect the most common setup (Octane) */
void sgi_listaudiodevs() {
    post("common devices on SGI machines:");
    post("#-1 - Default In/Out selected in Audio Panel");
    post("#1  - Analog In/Out");
    post("#2  - AES In/Out");
    post("#3  - ADAT I/O");
}

struct t_audioapi sgi_api = {
	sgi_open_audio,
	sgi_close_audio,
	sgi_send_dacs,
	sgi_getdevs,
};