aboutsummaryrefslogtreecommitdiff
path: root/desiredata/src/s_audio_portaudio.c
blob: 3690aba03063ea502571a56dccc96640fcdbfa32 (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
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
/* Copyright (c) 2001 Miller Puckette and others.
 * Copyright (c) 2005-2006 Tim Blechmann
 * supported by vibrez.net
 * For information on usage and redistribution, and for a DISCLAIMER OF ALL
 * WARRANTIES, see the file, "LICENSE.txt," in this distribution.  */

/* this file calls Ross Bencina's and Phil Burk's Portaudio package.  It's
   the main way in for Mac OS and, with Michael Casey's help, also into
   ASIO in Windows. */

/* tb: requires portaudio >= V19 */

#include "m_pd.h"
#include "s_stuff.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <portaudio.h>
#include <errno.h>
#include "assert.h"
#ifdef MSW
# include <malloc.h>
# include <pa_asio.h>
#else
# include <alloca.h>
#endif
#include "pthread.h"
/* for M_PI */
#if defined(_MSC_VER) && !defined(_USE_MATH_DEFINES)
#define _USE_MATH_DEFINES
#endif
#include <math.h>
#define MAX_PA_CHANS 32

static int pa_inchans, pa_outchans;
static int pa_blocksize;

static PaStream *pa_stream;
/* Initialize PortAudio  */
PaError pa_status = -1;
int pa_initialized = 0;

void pa_initialize() {
//    if (pa_initialized) return;
    pa_status = Pa_Initialize();
    if (pa_status!=paNoError) {
        error("Error number %d occured initializing portaudio: %s", pa_status, Pa_GetErrorText(pa_status));
        return;
    }
    pa_initialized = 1;
}

static float* pa_inbuffer[MAX_PA_CHANS];
static float* pa_outbuffer[MAX_PA_CHANS];
static int pa_bufferpos;
static int pddev2padev(int pdindev,int isinput);
static int padev2pddev(int padev,int isinput);
int process (const void *input, void *output, unsigned long frameCount, const PaStreamCallbackTimeInfo* timeInfo,
    PaStreamCallbackFlags statusFlags, void *userData);

static int pa_indev = -1, pa_outdev = -1;

int pa_open_audio(int inchans, int outchans, int rate, int advance,
int indeviceno, int outdeviceno, int schedmode) {
    PaError err;
    const PaDeviceInfo *pdi,*pdo;
    schedmode = 1; /* we don't support blocking io */
    pa_initialize();
    sys_setscheduler(schedmode);
    /* post("in %d out %d rate %d device %d", inchans, outchans, rate, deviceno); */
    if ( inchans > MAX_PA_CHANS) {post( "input channels reduced to maximum %d", MAX_PA_CHANS);  inchans = MAX_PA_CHANS;}
    if (outchans > MAX_PA_CHANS) {post("output channels reduced to maximum %d", MAX_PA_CHANS); outchans = MAX_PA_CHANS;}
    pdi = NULL;
    if (inchans > 0) {
        pa_indev = pddev2padev(indeviceno,1);
        if(pa_indev >= 0) {
            pdi = Pa_GetDeviceInfo(pa_indev);
            if(pdi->maxInputChannels < inchans) inchans = pdi->maxInputChannels;
        }
    }
    pdo = NULL;
    if (outchans > 0) {
        pa_outdev = pddev2padev(outdeviceno,0);
        if(pa_outdev >= 0) {
            pdo = Pa_GetDeviceInfo(pa_outdev);
            if(pdo->maxOutputChannels < outchans) outchans = pdo->maxOutputChannels;
        }
    }
    if (sys_verbose) {
        post("input device %d, channels %d", pa_indev, inchans);
        post("output device %d, channels %d", pa_outdev, outchans);
        post("latency advance %d", advance);
    }
    if (inchans || outchans) {
        int blocksize;
        PaStreamParameters iparam,oparam;
        /* initialize input */
        iparam.device = pa_indev;
        iparam.channelCount = inchans;
        iparam.sampleFormat = paFloat32 | paNonInterleaved;
        iparam.suggestedLatency = advance * 0.001;
        iparam.hostApiSpecificStreamInfo = NULL;
        /* initialize output */
        oparam.device = pa_outdev;
        oparam.channelCount = outchans;
        oparam.sampleFormat = paFloat32 | paNonInterleaved;
        oparam.suggestedLatency = advance * 0.001;
        oparam.hostApiSpecificStreamInfo = NULL;
        /* set block size */
        blocksize=64;
        while ((float)blocksize/(float)rate*1000*2 < advance && blocksize==1024) blocksize *= 2;
        pa_blocksize = blocksize;
        /* initialize io buffer */
        for (int j=0; j != MAX_PA_CHANS;++j) {
            if (pa_inbuffer[j])  freealignedbytes(pa_inbuffer[j], 0);
            if (pa_outbuffer[j]) freealignedbytes(pa_outbuffer[j], 0);
            pa_inbuffer[j]  = (float *)getalignedbytes((blocksize + sys_dacblocksize)*sizeof(float));
            pa_outbuffer[j] = (float *)getalignedbytes((blocksize + sys_dacblocksize)*sizeof(float));
        }
        pa_bufferpos = 0;
        /* report to portaudio */
        err = Pa_OpenStream(&pa_stream,
            (( pa_indev!=-1) ? &iparam : 0),
            ((pa_outdev!=-1) ? &oparam : 0),
            rate, pa_blocksize, paClipOff, /* tb: we should be faster ;-) */ process /* patestCallback */, NULL);
        if (err == paNoError) {
            const PaStreamInfo *streaminfo = Pa_GetStreamInfo(pa_stream);
            t_atom atoms[4];
            t_symbol *pd = gensym("pd");
            sys_schedadvance = int(1e-6 * streaminfo->outputLatency);

            SETFLOAT(atoms, (float)indeviceno);
            SETFLOAT(atoms+1, (float)inchans);
            SETFLOAT(atoms+2, (float)rate);
            SETFLOAT(atoms+3, (float)streaminfo->inputLatency * 1000.f);
            typedmess(pd->s_thing, gensym("audiocurrentininfo"), 4, atoms);

            SETFLOAT(atoms, (float)outdeviceno);
            SETFLOAT(atoms+1, (float)outchans);
            SETFLOAT(atoms+2, (float)rate);
            SETFLOAT(atoms+3, (float)streaminfo->outputLatency * 1000.f);
            typedmess(pd->s_thing, gensym("audiocurrentoutinfo"), 4, atoms);
        }
    } else err = 0;

    if (err != paNoError) {
        error("Error number %d occured opening portaudio stream: %s", err, Pa_GetErrorText(err));
        sys_inchannels = sys_outchannels = 0;
        pa_indev = pa_outdev = -1;
        pa_inchans = pa_outchans = 0;
        return 1;
    } else if (sys_verbose) post("... opened OK.");
    pa_inchans = inchans;
    pa_outchans = outchans;

    /* we might have adapted the channel count */
    sys_setchsr(inchans, outchans, rate, sys_dacblocksize);
    err = Pa_StartStream(pa_stream);
    if (err!=paNoError) {
        post("Error number %d occured starting portaudio stream: %s", err, Pa_GetErrorText(err));
        sys_inchannels = sys_outchannels = 0;
        return 1;
    }
    if(sys_verbose) post("successfully started");
    return 0;
}

void sys_peakmeters();
extern int sys_meters;          /* true if we're metering */

void run_all_idle_callbacks();
void sys_xrun_notification(); /* in m_sched.c */
void sys_lock_timeout_notification();

int process (const void *input, void *output, unsigned long frameCount, const PaStreamCallbackTimeInfo* timeInfo,
PaStreamCallbackFlags statusFlags, void *userData) {
    int timeout = int((float)frameCount / (float) sys_dacsr * 1e6);
    if (statusFlags) sys_xrun_notification();
    if (sys_timedlock(timeout) == ETIMEDOUT) /* we're late */ {
        sys_lock_timeout_notification();
        return 0;
    }
    for (int i=0; (unsigned)i < frameCount / sys_dacblocksize; ++i) {
        for (int j=0; j < sys_inchannels; j++) {
            t_sample  *in = ((t_sample**)input)[j] + i * sys_dacblocksize;
            copyvec(sys_soundin + j * sys_dacblocksize, in, sys_dacblocksize);
        }
        sched_tick(sys_time + sys_time_per_dsp_tick);
        for (int j=0; j < sys_outchannels; j++) {
            t_sample *out = ((t_sample**)output)[j] + i * sys_dacblocksize;
            copyvec(out, sys_soundout + j * sys_dacblocksize, sys_dacblocksize);
        }
        if (sys_meters) sys_peakmeters();
        zerovec(sys_soundout, pa_outchans * sys_dacblocksize);
    }
    run_all_idle_callbacks();
    sys_unlock();
    return 0;
}

static void pa_close_audio() {
    if(sys_verbose) post("closing portaudio");
    if (pa_inchans || pa_outchans) {
        if (pa_stream) {
            int status = Pa_StopStream(pa_stream);
            if (status) post("error closing audio: %d", status);
            Pa_CloseStream(pa_stream);
            pa_stream = NULL;
        }
    }
    sys_setscheduler(0);
    if(sys_verbose) post("portaudio closed");
    pa_inchans = pa_outchans = 0;
    pa_indev = pa_outdev = -1;
}

/* for blocked IO */
static int pa_send_dacs() {
    /* we don't support blocking i/o */
    return SENDDACS_NO;
}

/* lifted from pa_devs.c in portaudio */
static void pa_listdevs() {
    PaError err;
    pa_initialize();
    int numDevices = Pa_GetDeviceCount();
    if(numDevices < 0) {
        error("ERROR: Pa_GetDeviceCount returned %d", numDevices);
        err = numDevices;
        goto error;
    }
    post("Audio Devices:");
    for(int i=0; i<numDevices; i++) {
        const PaDeviceInfo *pdi = Pa_GetDeviceInfo(i);
        post ("device %s", pdi->name);
        post("device %d:", i+1);
        post(" %s;", pdi->name);
        post("%d inputs, ", pdi->maxInputChannels);
        post("%d outputs ", pdi->maxOutputChannels);
        if (i == Pa_GetDefaultInputDevice())  post(" (Default Input)");
        if (i == Pa_GetDefaultOutputDevice()) post(" (Default Output)");
        post("");
    }
    post("");
    return;
 error:
    error("Error #%d occurred while using the portaudio stream: %s\n", err, Pa_GetErrorText(err));
}

/* scanning for devices */
static void pa_getdevs(char *indevlist, int *nindevs, char *outdevlist, int *noutdevs, int *canmulti, int maxndev, int devdescsize) {
    int nin = 0, nout = 0, ndev;
    *canmulti = 1;  /* one dev each for input and output */
    pa_initialize();
    ndev = Pa_GetDeviceCount();
    for (int i=0; i<ndev; i++) {
        const PaDeviceInfo *pdi = Pa_GetDeviceInfo(i);
        if (pdi->maxInputChannels > 0 && nin < maxndev) {
            PaHostApiIndex api = pdi->hostApi;
            const PaHostApiInfo *info = Pa_GetHostApiInfo(api);
            const char *apiName = info->name;
            unsigned int apiNameLen = strlen(apiName);
            strcpy(indevlist + nin * devdescsize, apiName);
            indevlist[nin * devdescsize + apiNameLen] = '/';
            strcpy(indevlist + nin * devdescsize + apiNameLen + 1, pdi->name);
            nin++;
        }
        if (pdi->maxOutputChannels > 0 && nout < maxndev) {
            PaHostApiIndex api = pdi->hostApi;
            const PaHostApiInfo *info = Pa_GetHostApiInfo(api);
            const char *apiName = info->name;
            unsigned int apiNameLen = strlen(apiName);
            strcpy(outdevlist + nout * devdescsize, apiName);
            outdevlist[nout * devdescsize + apiNameLen] = '/';
            strcpy(outdevlist + nout * devdescsize + apiNameLen + 1, pdi->name);
            nout++;
        }
    }
    *nindevs = nin;
    *noutdevs = nout;
}

void pa_getaudioininfo(t_float f) {
    int i = pddev2padev((int)f,1);
    const PaDeviceInfo *pdi;
    pa_initialize();
    pdi = Pa_GetDeviceInfo(i);
    if (pdi) {
        t_symbol *selector = gensym("audioininfo");
        t_symbol *pd = gensym("pd");
        t_atom argv[4];
        SETFLOAT(argv, pdi->maxInputChannels);
        SETFLOAT(argv+1, pdi->defaultSampleRate);
        SETFLOAT(argv+2, pdi->defaultLowInputLatency*1000.f);
        SETFLOAT(argv+3, pdi->defaultHighInputLatency*1000.f);
        typedmess(pd->s_thing, selector, 4, argv);
    }
}

void pa_getaudiooutinfo(t_float f) {
    int i = pddev2padev((int)f,0);
    const PaDeviceInfo *pdi;
    pa_initialize();
    pdi = Pa_GetDeviceInfo(i);
    if (pdi) {
        t_symbol *selector = gensym("audiooutinfo");
        t_symbol *pd = gensym("pd");
        t_atom argv[4];
        SETFLOAT(argv, pdi->maxOutputChannels);
        SETFLOAT(argv+1, pdi->defaultSampleRate);
        SETFLOAT(argv+2, pdi->defaultLowOutputLatency*1000.f);
        SETFLOAT(argv+3, pdi->defaultHighOutputLatency*1000.f);
        typedmess(pd->s_thing, selector, 4, argv);
    }
}

void pa_getcurrent_devices() {
    t_symbol *pd = gensym("pd");
    t_symbol *selector = gensym("audiodevice");
    t_atom argv[2];
    SETFLOAT(argv, padev2pddev(pa_indev,1));
    SETFLOAT(argv+1, padev2pddev(pa_outdev,0));
    typedmess(pd->s_thing, selector, 2, argv);
}

void pa_test_setting (int ac, t_atom *av) {
    int indev      = atom_getintarg(0, ac, av);
    int outdev     = atom_getintarg(1, ac, av);
    int samplerate = atom_getintarg(2, ac, av);
    int inchans    = atom_getintarg(3, ac, av);
    int outchans   = atom_getintarg(4, ac, av);
    int advance    = atom_getintarg(5, ac, av);
    t_symbol *pd = gensym("pd");
    t_symbol *selector = gensym("testaudiosettingresult");
    t_atom argv[1];
    pa_initialize();
    indev = pddev2padev(indev,1);
    outdev = pddev2padev(outdev,0);
    if (pa_indev==-1 && pa_outdev==-1) {
        int ret;
        PaStreamParameters iparam, oparam;
        iparam.device = indev;
        iparam.channelCount = inchans;
        iparam.sampleFormat = paFloat32 | paNonInterleaved;
        iparam.suggestedLatency = advance * 0.001;
        iparam.hostApiSpecificStreamInfo = NULL;
        oparam.device = outdev;
        oparam.channelCount = outchans;
        oparam.sampleFormat = paFloat32 | paNonInterleaved;
        oparam.suggestedLatency = advance * 0.001;
        oparam.hostApiSpecificStreamInfo = NULL;
        ret = Pa_IsFormatSupported(&iparam, &oparam, samplerate);
        SETFLOAT(argv, ret == paNoError?1:0);
        typedmess(pd->s_thing, selector, 1, argv);
    }
}

static int pddev2padev(int pddev,int input) {
    pa_initialize();
    for (int j=0, devno=0; j < Pa_GetDeviceCount(); j++) {
        const PaDeviceInfo *info = Pa_GetDeviceInfo(j);
        int maxchans = input?info->maxInputChannels:info->maxOutputChannels;
        if (maxchans > 0) {
            if (devno == pddev) return j;
            devno++;
        }
    }
    return -1;
}

static int padev2pddev(int padev,int input) {
    int count = Pa_GetDeviceCount();
    for (int j=0, devno=0; j < count; j++) {
        const PaDeviceInfo *info = Pa_GetDeviceInfo(j);
        int chans = input?info->maxInputChannels:info->maxOutputChannels;
        if (chans > 0) {
            if(j == padev) return devno;
            devno++;
        }
    }
    return -1; // no found
}

void pa_get_asio_latencies(t_float f) {
    int index = pddev2padev((int)f,0);
    const PaDeviceInfo *pdi = Pa_GetDeviceInfo(index);
    const PaHostApiInfo *phi = Pa_GetHostApiInfo(pdi->hostApi);
    if (phi->type != paASIO) {
        post("device not an asio device");
        return;
    }
#ifdef WIN32
    else {
        long minlat, maxlat, preflat, gran;
        t_atom argv[4];
        t_symbol *selector = gensym("asiolatency");
        t_symbol *pd = gensym("pd");
        PaAsio_GetAvailableLatencyValues(index, &minlat, &maxlat, &preflat, &gran);
        SETFLOAT(argv, (float) minlat);
        SETFLOAT(argv + 1, (float) maxlat);
        SETFLOAT(argv + 2, (float) preflat);
        SETFLOAT(argv + 3, (float) gran);
        typedmess(pd->s_thing, selector, 4, argv);
    }
#endif
}

t_audioapi pa_api = {
    0 /* pa_open_audio */,
    pa_close_audio,
    pa_send_dacs,
    pa_getdevs,
};