s_audio.c 35 KB
Newer Older
Miller Puckette's avatar
Miller Puckette committed
1
2
3
4
5
6
7
8
/* Copyright (c) 2003, Miller Puckette and others.
* For information on usage and redistribution, and for a DISCLAIMER OF ALL
* WARRANTIES, see the file, "LICENSE.txt," in this distribution.  */

/*  machine-independent (well, mostly!) audio layer.  Stores and recalls
    audio settings from argparse routine and from dialog window. 
*/

9
10
#include "config.h"

Miller Puckette's avatar
Miller Puckette committed
11
12
13
#include "m_pd.h"
#include "s_stuff.h"
#include <stdio.h>
14
#ifdef HAVE_UNISTD_H
Miller Puckette's avatar
Miller Puckette committed
15
#include <unistd.h>
16
/* XXX Hack!!! These should be checked for independently of unistd.h ... */
Miller Puckette's avatar
Miller Puckette committed
17
18
#include <sys/time.h>
#include <sys/resource.h>
19
#endif
Miller Puckette's avatar
Miller Puckette committed
20
21
22
23
24
#include <stdlib.h>
#include <string.h>
#include <errno.h>

#define SYS_DEFAULTCH 2
25
#define SYS_MAXCH 100
Miller Puckette's avatar
Miller Puckette committed
26
27
28
29
30
31
32
33
34
typedef long t_pa_sample;
#define SYS_SAMPLEWIDTH sizeof(t_pa_sample)
#define SYS_BYTESPERCHAN (DEFDACBLKSIZE * SYS_SAMPLEWIDTH) 
#define SYS_XFERSAMPS (SYS_DEFAULTCH*DEFDACBLKSIZE)
#define SYS_XFERSIZE (SYS_SAMPLEWIDTH * SYS_XFERSAMPS)
#define MAXNDEV 20
#define DEVDESCSIZE 80

static void audio_getdevs(char *indevlist, int *nindevs,
Miller Puckette's avatar
Miller Puckette committed
35
    char *outdevlist, int *noutdevs, int *canmulti, int *cancallback, 
Miller Puckette's avatar
Miller Puckette committed
36
37
38
39
40
41
42
        int maxndev, int devdescsize);

    /* these are set in this file when opening audio, but then may be reduced,
    even to zero, in the system dependent open_audio routines. */
int sys_inchannels;
int sys_outchannels;
int sys_advance_samples;        /* scheduler advance in samples */
43
int sys_blocksize = 0;          /* audio I/O block size in sample frames */
Miller Puckette's avatar
Miller Puckette committed
44
int sys_audioapi = API_DEFAULT;
Miller Puckette's avatar
Miller Puckette committed
45
int sys_audioapiopened = -1;    /* save last API opened for later closing */
Miller Puckette's avatar
Miller Puckette committed
46
static int sys_meters;          /* true if we're metering */
47
48
static t_sample sys_inmax;         /* max input amplitude */
static t_sample sys_outmax;        /* max output amplitude */
Miller Puckette's avatar
Miller Puckette committed
49
50
51

    /* exported variables */
int sys_schedadvance;   /* scheduler advance in microseconds */
52
t_float sys_dacsr;
Miller Puckette's avatar
Miller Puckette committed
53
54
55
56
57
58
59
60
61
62
63
64
65

t_sample *sys_soundout;
t_sample *sys_soundin;

    /* the "state" is normally one if we're open and zero otherwise; 
    but if the state is one, we still haven't necessarily opened the
    audio hardware; see audio_isopen() below. */
static int audio_state;

    /* last requested parameters */
static int audio_naudioindev = -1;
static int audio_audioindev[MAXAUDIOINDEV];
static int audio_audiochindev[MAXAUDIOINDEV];
66
static char audio_indevnames[MAXMIDIINDEV * DEVDESCSIZE];
Miller Puckette's avatar
Miller Puckette committed
67
68
69
static int audio_naudiooutdev = -1;
static int audio_audiooutdev[MAXAUDIOOUTDEV];
static int audio_audiochoutdev[MAXAUDIOOUTDEV];
70
static char audio_outdevnames[MAXMIDIINDEV * DEVDESCSIZE];
Miller Puckette's avatar
Miller Puckette committed
71
static int audio_rate;
72
static int audio_advance = -1;
Miller Puckette's avatar
Miller Puckette committed
73
static int audio_callback;
74
static int audio_blocksize;
Miller Puckette's avatar
Miller Puckette committed
75

Miller Puckette's avatar
Miller Puckette committed
76
77
static int audio_callback_is_open;  /* reflects true actual state */
static int audio_nextinchans, audio_nextoutchans;
78
void sched_audio_callbackfn(void);
79
void sched_reopenmeplease(void);
Miller Puckette's avatar
Miller Puckette committed
80

81
#ifdef USEAPI_JACK
82
    /* needed to fix srate when using jack, inclded in s_audio_jack.c */
83
84
85
extern int jack_get_srate(void);
#endif /* JACK */

86
int audio_isopen(void)
Miller Puckette's avatar
Miller Puckette committed
87
88
89
90
91
92
{
    return (audio_state &&
        ((audio_naudioindev > 0 && audio_audiochindev[0] > 0) 
            || (audio_naudiooutdev > 0 && audio_audiochoutdev[0] > 0)));
}

93
94
95
96
97
int sys_audio_get_blocksize(void)
{
    return (audio_blocksize);
}

Miller Puckette's avatar
Miller Puckette committed
98
99
100
void sys_get_audio_params(
    int *pnaudioindev, int *paudioindev, int *chindev,
    int *pnaudiooutdev, int *paudiooutdev, int *choutdev,
101
    int *prate, int *padvance, int *pcallback, int *pblocksize)
Miller Puckette's avatar
Miller Puckette committed
102
{
103
    int i, devn;
Miller Puckette's avatar
Miller Puckette committed
104
    *pnaudioindev = audio_naudioindev;
105
106
107
108
109
110
111
112
    for (i = 0; i < audio_naudioindev; i++)
    {
        if ((devn = sys_audiodevnametonumber(0,
            &audio_indevnames[i * DEVDESCSIZE])) >= 0)
                paudioindev[i] = devn;
        else paudioindev[i] = audio_audioindev[i];
        chindev[i] = audio_audiochindev[i];
    }
Miller Puckette's avatar
Miller Puckette committed
113
    *pnaudiooutdev = audio_naudiooutdev;
114
    for (i = 0; i < audio_naudiooutdev; i++)
115
    {
116
117
118
119
120
        if ((devn = sys_audiodevnametonumber(1,
            &audio_outdevnames[i * DEVDESCSIZE])) >= 0)
                paudiooutdev[i] = devn;
        else paudiooutdev[i] = audio_audiooutdev[i];
        choutdev[i] = audio_audiochoutdev[i]; 
121
    }
122
123
    if (audio_rate != sys_dacsr && sys_dacsr != 0)
        audio_rate = sys_dacsr;
Miller Puckette's avatar
Miller Puckette committed
124
125
    *prate = audio_rate;
    *padvance = audio_advance;
Miller Puckette's avatar
Miller Puckette committed
126
    *pcallback = audio_callback;
127
    *pblocksize = audio_blocksize;
Miller Puckette's avatar
Miller Puckette committed
128
129
130
131
132
}

void sys_save_audio_params(
    int naudioindev, int *audioindev, int *chindev,
    int naudiooutdev, int *audiooutdev, int *choutdev,
133
    int rate, int advance, int callback, int blocksize)
Miller Puckette's avatar
Miller Puckette committed
134
135
136
{
    int i;
    audio_naudioindev = naudioindev;
137
138
    for (i = 0; i < naudioindev; i++)
    {
Miller Puckette's avatar
Miller Puckette committed
139
        audio_audioindev[i] = audioindev[i],
140
141
142
143
        audio_audiochindev[i] = chindev[i];
        sys_audiodevnumbertoname(0, audioindev[i],
            &audio_indevnames[i * DEVDESCSIZE], DEVDESCSIZE);
    }
Miller Puckette's avatar
Miller Puckette committed
144
    audio_naudiooutdev = naudiooutdev;
145
146
    for (i = 0; i < naudiooutdev; i++)
    {
Miller Puckette's avatar
Miller Puckette committed
147
        audio_audiooutdev[i] = audiooutdev[i],
148
149
150
151
        audio_audiochoutdev[i] = choutdev[i];
        sys_audiodevnumbertoname(1, audiooutdev[i],
            &audio_outdevnames[i * DEVDESCSIZE], DEVDESCSIZE);
    }
152
    sys_dacsr = rate;
Miller Puckette's avatar
Miller Puckette committed
153
154
    audio_rate = rate;
    audio_advance = advance;
Miller Puckette's avatar
Miller Puckette committed
155
    audio_callback = callback;
156
    audio_blocksize = blocksize;
Miller Puckette's avatar
Miller Puckette committed
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
}

    /* init routines for any API which needs to set stuff up before
    any other API gets used.  This is only true of OSS so far. */
#ifdef USEAPI_OSS
void oss_init(void);
#endif

static void audio_init( void)
{
    static int initted = 0;
    if (initted)
        return;
    initted = 1;
#ifdef USEAPI_OSS
    oss_init();
#endif
} 

    /* set channels and sample rate.  */

void sys_setchsr(int chin, int chout, int sr)
{
    int inbytes = (chin ? chin : 2) *
181
                (DEFDACBLKSIZE*sizeof(t_sample));
Miller Puckette's avatar
Miller Puckette committed
182
    int outbytes = (chout ? chout : 2) *
183
                (DEFDACBLKSIZE*sizeof(t_sample));
Miller Puckette's avatar
Miller Puckette committed
184
185
186
187

    if (sys_soundin)
        freebytes(sys_soundin, 
            (sys_inchannels? sys_inchannels : 2) *
188
                (DEFDACBLKSIZE*sizeof(t_sample)));
Miller Puckette's avatar
Miller Puckette committed
189
190
191
    if (sys_soundout)
        freebytes(sys_soundout, 
            (sys_outchannels? sys_outchannels : 2) *
192
                (DEFDACBLKSIZE*sizeof(t_sample)));
Miller Puckette's avatar
Miller Puckette committed
193
194
195
    sys_inchannels = chin;
    sys_outchannels = chout;
    sys_dacsr = sr;
196
    audio_rate = sr;
Miller Puckette's avatar
Miller Puckette committed
197
    sys_advance_samples = (sys_schedadvance * sys_dacsr) / (1000000.);
198
199
    if (sys_advance_samples < 3 * DEFDACBLKSIZE)
        sys_advance_samples = 3 * DEFDACBLKSIZE;
Miller Puckette's avatar
Miller Puckette committed
200

201
    sys_soundin = (t_sample *)getbytes(inbytes);
Miller Puckette's avatar
Miller Puckette committed
202
203
    memset(sys_soundin, 0, inbytes);

204
    sys_soundout = (t_sample *)getbytes(outbytes);
Miller Puckette's avatar
Miller Puckette committed
205
206
207
208
209
210
211
212
213
214
    memset(sys_soundout, 0, outbytes);

    if (sys_verbose)
        post("input channels = %d, output channels = %d",
            sys_inchannels, sys_outchannels);
    canvas_resume_dsp(canvas_suspend_dsp());
}

/* ----------------------- public routines ----------------------- */

215
216
    /* set audio device settings (after cleaning up the specified device and
    channel vectors).  The audio devices are "zero based" (i.e. "0" means the
Miller Puckette's avatar
Miller Puckette committed
217
    first one.)  We can later re-open audio and/or show the settings on a
218
    dialog window. */
Miller Puckette's avatar
Miller Puckette committed
219

220
void sys_set_audio_settings(int naudioindev, int *audioindev, int nchindev,
Miller Puckette's avatar
Miller Puckette committed
221
    int *chindev, int naudiooutdev, int *audiooutdev, int nchoutdev,
222
    int *choutdev, int rate, int advance, int callback, int blocksize)
Miller Puckette's avatar
Miller Puckette committed
223
{
224
    int i, *ip;
Miller Puckette's avatar
Miller Puckette committed
225
226
227
228
229
230
    int defaultchannels = SYS_DEFAULTCH;
    int inchans, outchans, nrealindev, nrealoutdev;
    int realindev[MAXAUDIOINDEV], realoutdev[MAXAUDIOOUTDEV];
    int realinchans[MAXAUDIOINDEV], realoutchans[MAXAUDIOOUTDEV];

    char indevlist[MAXNDEV*DEVDESCSIZE], outdevlist[MAXNDEV*DEVDESCSIZE];
Miller Puckette's avatar
Miller Puckette committed
231
    int indevs = 0, outdevs = 0, canmulti = 0, cancallback = 0;
Miller Puckette's avatar
Miller Puckette committed
232
    audio_getdevs(indevlist, &indevs, outdevlist, &outdevs, &canmulti,
Miller Puckette's avatar
Miller Puckette committed
233
        &cancallback, MAXNDEV, DEVDESCSIZE);
Miller Puckette's avatar
Miller Puckette committed
234
235
236

    if (rate < 1)
        rate = DEFAULTSRATE;
237
    if (advance < 0)
Miller Puckette's avatar
Miller Puckette committed
238
        advance = DEFAULTADVANCE;
239
240
    if (blocksize != (1 << ilog2(blocksize)) || blocksize < DEFDACBLKSIZE)
        blocksize = DEFDACBLKSIZE;
Miller Puckette's avatar
Miller Puckette committed
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
     audio_init();
        /* Since the channel vector might be longer than the
        audio device vector, or vice versa, we fill the shorter one
        in to match the longer one.  Also, if both are empty, we fill in
        one device (the default) and two channels. */ 
    if (naudioindev == -1)
    {           /* no input audio devices specified */
        if (nchindev == -1)
        {
            if (indevs >= 1)
            {
                nchindev=1;
                chindev[0] = defaultchannels;
                naudioindev = 1;
                audioindev[0] = DEFAULTAUDIODEV;
            }
            else naudioindev = nchindev = 0;
        }
        else
        {
            for (i = 0; i < MAXAUDIOINDEV; i++)
                audioindev[i] = i;
            naudioindev = nchindev;
        }
    }
    else
    {
        if (nchindev == -1)
        {
            nchindev = naudioindev;
            for (i = 0; i < naudioindev; i++)
                chindev[i] = defaultchannels;
        }
        else if (nchindev > naudioindev)
        {
            for (i = naudioindev; i < nchindev; i++)
            {
                if (i == 0)
                    audioindev[0] = DEFAULTAUDIODEV;
                else audioindev[i] = audioindev[i-1] + 1;
            }
            naudioindev = nchindev;
        }
        else if (nchindev < naudioindev)
        {
            for (i = nchindev; i < naudioindev; i++)
            {
                if (i == 0)
                    chindev[0] = defaultchannels;
                else chindev[i] = chindev[i-1];
            }
            naudioindev = nchindev;
        }
    }

    if (naudiooutdev == -1)
    {           /* not set */
        if (nchoutdev == -1)
        {
            if (outdevs >= 1)
            {
                nchoutdev=1;
                choutdev[0]=defaultchannels;
                naudiooutdev=1;
                audiooutdev[0] = DEFAULTAUDIODEV;
            }
            else nchoutdev = naudiooutdev = 0;
        }
        else
        {
            for (i = 0; i < MAXAUDIOOUTDEV; i++)
                audiooutdev[i] = i;
            naudiooutdev = nchoutdev;
        }
    }
    else
    {
        if (nchoutdev == -1)
        {
            nchoutdev = naudiooutdev;
            for (i = 0; i < naudiooutdev; i++)
                choutdev[i] = defaultchannels;
        }
        else if (nchoutdev > naudiooutdev)
        {
            for (i = naudiooutdev; i < nchoutdev; i++)
            {
                if (i == 0)
                    audiooutdev[0] = DEFAULTAUDIODEV;
                else audiooutdev[i] = audiooutdev[i-1] + 1;
            }
            naudiooutdev = nchoutdev;
        }
        else if (nchoutdev < naudiooutdev)
        {
            for (i = nchoutdev; i < naudiooutdev; i++)
            {
                if (i == 0)
                    choutdev[0] = defaultchannels;
                else choutdev[i] = choutdev[i-1];
            }
            naudiooutdev = nchoutdev;
        }
    }
    
        /* count total number of input and output channels */
    for (i = nrealindev = inchans = 0; i < naudioindev; i++)
        if (chindev[i] > 0)
    {
        realinchans[nrealindev] = chindev[i];
        realindev[nrealindev] = audioindev[i];
        inchans += chindev[i];
        nrealindev++;
    }
    for (i = nrealoutdev = outchans = 0; i < naudiooutdev; i++)
        if (choutdev[i] > 0)
    {
        realoutchans[nrealoutdev] = choutdev[i];
        realoutdev[nrealoutdev] = audiooutdev[i];
        outchans += choutdev[i];
        nrealoutdev++;
    }
    sys_schedadvance = advance * 1000;
    sys_log_error(ERR_NOTHING);
Miller Puckette's avatar
Miller Puckette committed
365
366
    audio_nextinchans = inchans;
    audio_nextoutchans = outchans;
367
    sys_setchsr(audio_nextinchans, audio_nextoutchans, rate);
368
    sys_save_audio_params(nrealindev, realindev, realinchans,
369
370
        nrealoutdev, realoutdev, realoutchans, rate, advance, callback,
            blocksize);
Miller Puckette's avatar
Miller Puckette committed
371
372
373
374
375
376
377
378
379
380
381
}

void sys_close_audio(void)
{
    if (sys_externalschedlib)
    {
        return;
    }
    if (!audio_isopen())
        return;
#ifdef USEAPI_PORTAUDIO
Miller Puckette's avatar
Miller Puckette committed
382
    if (sys_audioapiopened == API_PORTAUDIO)
Miller Puckette's avatar
Miller Puckette committed
383
384
385
386
        pa_close_audio();
    else 
#endif
#ifdef USEAPI_JACK
Miller Puckette's avatar
Miller Puckette committed
387
    if (sys_audioapiopened == API_JACK)
Miller Puckette's avatar
Miller Puckette committed
388
389
390
391
        jack_close_audio();
    else
#endif
#ifdef USEAPI_OSS
Miller Puckette's avatar
Miller Puckette committed
392
    if (sys_audioapiopened == API_OSS)
Miller Puckette's avatar
Miller Puckette committed
393
394
395
396
        oss_close_audio();
    else
#endif
#ifdef USEAPI_ALSA
Miller Puckette's avatar
Miller Puckette committed
397
    if (sys_audioapiopened == API_ALSA)
Miller Puckette's avatar
Miller Puckette committed
398
399
400
401
        alsa_close_audio();
    else
#endif
#ifdef USEAPI_MMIO
Miller Puckette's avatar
Miller Puckette committed
402
    if (sys_audioapiopened == API_MMIO)
Miller Puckette's avatar
Miller Puckette committed
403
404
405
        mmio_close_audio();
    else
#endif
406
        post("sys_close_audio: unknown API %d", sys_audioapiopened);
Miller Puckette's avatar
Miller Puckette committed
407
    sys_inchannels = sys_outchannels = 0;
Miller Puckette's avatar
Miller Puckette committed
408
    sys_audioapiopened = -1;
409
    sched_set_using_audio(SCHED_AUDIO_NONE);
410
    audio_state = 0;
Miller Puckette's avatar
Miller Puckette committed
411
    audio_callback_is_open = 0;
Miller Puckette's avatar
Miller Puckette committed
412
413
414
415
416
417
418
}

    /* open audio using whatever parameters were last used */
void sys_reopen_audio( void)
{
    int naudioindev, audioindev[MAXAUDIOINDEV], chindev[MAXAUDIOINDEV];
    int naudiooutdev, audiooutdev[MAXAUDIOOUTDEV], choutdev[MAXAUDIOOUTDEV];
419
    int rate, advance, callback, blocksize, outcome = 0;
Miller Puckette's avatar
Miller Puckette committed
420
    sys_get_audio_params(&naudioindev, audioindev, chindev,
421
422
        &naudiooutdev, audiooutdev, choutdev, &rate, &advance, &callback,
            &blocksize);
Miller Puckette's avatar
Miller Puckette committed
423
    sys_setchsr(audio_nextinchans, audio_nextoutchans, rate);
424
425
426
427
428
429
430
431
    if (!naudioindev && !naudiooutdev)
    {
        sched_set_using_audio(SCHED_AUDIO_NONE);
        return;
    }
#ifdef USEAPI_PORTAUDIO
    if (sys_audioapi == API_PORTAUDIO)
    {
432
        int blksize = (audio_blocksize ? audio_blocksize : 64);
433
434
435
436
437
438
439
440
441
442
443
444
        outcome = pa_open_audio((naudioindev > 0 ? chindev[0] : 0),
        (naudiooutdev > 0 ? choutdev[0] : 0), rate, sys_soundin,
            sys_soundout, blksize, sys_advance_samples/blksize, 
             (naudioindev > 0 ? audioindev[0] : 0),
              (naudiooutdev > 0 ? audiooutdev[0] : 0),
               (callback ? sched_audio_callbackfn : 0));
    }
    else
#endif
#ifdef USEAPI_JACK
    if (sys_audioapi == API_JACK) 
        outcome = jack_open_audio((naudioindev > 0 ? chindev[0] : 0),
445
            (naudioindev > 0 ? choutdev[0] : 0), rate);
446
447
448
449
450
451

    else
#endif    
#ifdef USEAPI_OSS
    if (sys_audioapi == API_OSS)
        outcome = oss_open_audio(naudioindev, audioindev, naudioindev,
452
453
            chindev, naudiooutdev, audiooutdev, naudiooutdev, choutdev, rate,
                audio_blocksize);
454
455
456
457
458
459
460
    else
#endif
#ifdef USEAPI_ALSA
        /* for alsa, only one device is supported; it may
        be open for both input and output. */
    if (sys_audioapi == API_ALSA)
        outcome = alsa_open_audio(naudioindev, audioindev, naudioindev,
461
462
            chindev, naudiooutdev, audiooutdev, naudiooutdev, choutdev, rate,
                audio_blocksize);
463
464
465
466
467
    else 
#endif
#ifdef USEAPI_MMIO
    if (sys_audioapi == API_MMIO)
        outcome = mmio_open_audio(naudioindev, audioindev, naudioindev,
468
469
470
471
472
473
474
475
476
477
478
479
480
            chindev, naudiooutdev, audiooutdev, naudiooutdev, choutdev, rate,
                audio_blocksize);
    else
#endif
#ifdef USEAPI_AUDIOUNIT
    if (sys_audioapi == API_AUDIOUNIT)
        outcome = audiounit_open_audio((naudioindev > 0 ? chindev[0] : 0),
            (naudioindev > 0 ? choutdev[0] : 0), rate);
    else
#endif
#ifdef USEAPI_ESD
    if (sys_audioapi == API_ALSA)
        outcome = esd_open_audio(naudioindev, audioindev, naudioindev,
481
            chindev, naudiooutdev, audiooutdev, naudiooutdev, choutdev, rate);
482
483
484
485
486
    else 
#endif
#ifdef USEAPI_DUMMY
    if (sys_audioapi == API_DUMMY)
        outcome = dummy_open_audio(naudioindev, naudiooutdev, rate);
487
488
    else
#endif
Miller Puckette's avatar
Miller Puckette committed
489
490
491
    if (sys_audioapi == API_NONE)
        ;
    else post("unknown audio API specified");
492
493
494
495
    if (outcome)    /* failed */
    {
        audio_state = 0;
        sched_set_using_audio(SCHED_AUDIO_NONE);
Miller Puckette's avatar
Miller Puckette committed
496
        sys_audioapiopened = -1;
Miller Puckette's avatar
Miller Puckette committed
497
        audio_callback_is_open = 0;
498
499
500
    }
    else
    {
Miller Puckette's avatar
Miller Puckette committed
501
                /* fprintf(stderr, "started w/callback %d\n", callback); */
502
503
504
        audio_state = 1;
        sched_set_using_audio(
            (callback ? SCHED_AUDIO_CALLBACK : SCHED_AUDIO_POLL));
Miller Puckette's avatar
Miller Puckette committed
505
        sys_audioapiopened = sys_audioapi;
Miller Puckette's avatar
Miller Puckette committed
506
        audio_callback_is_open = callback;
507
    }
Jonathan Wilkes's avatar
Jonathan Wilkes committed
508
    gui_vmess("set_audioapi", "i",  (outcome == 0 ? sys_audioapi : 0));
Miller Puckette's avatar
Miller Puckette committed
509
510
511
512
513
514
515
}

int sys_send_dacs(void)
{
    if (sys_meters)
    {
        int i, n;
516
        t_sample maxsamp;
Miller Puckette's avatar
Miller Puckette committed
517
518
519
        for (i = 0, n = sys_inchannels * DEFDACBLKSIZE, maxsamp = sys_inmax;
            i < n; i++)
        {
520
            t_sample f = sys_soundin[i];
Miller Puckette's avatar
Miller Puckette committed
521
522
523
524
525
526
527
            if (f > maxsamp) maxsamp = f;
            else if (-f > maxsamp) maxsamp = -f;
        }
        sys_inmax = maxsamp;
        for (i = 0, n = sys_outchannels * DEFDACBLKSIZE, maxsamp = sys_outmax;
            i < n; i++)
        {
528
            t_sample f = sys_soundout[i];
Miller Puckette's avatar
Miller Puckette committed
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
            if (f > maxsamp) maxsamp = f;
            else if (-f > maxsamp) maxsamp = -f;
        }
        sys_outmax = maxsamp;
    }

#ifdef USEAPI_PORTAUDIO
    if (sys_audioapi == API_PORTAUDIO)
        return (pa_send_dacs());
    else 
#endif
#ifdef USEAPI_JACK
      if (sys_audioapi == API_JACK) 
        return (jack_send_dacs());
    else
#endif
#ifdef USEAPI_OSS
    if (sys_audioapi == API_OSS)
        return (oss_send_dacs());
    else
#endif
#ifdef USEAPI_ALSA
    if (sys_audioapi == API_ALSA)
        return (alsa_send_dacs());
    else
#endif
#ifdef USEAPI_MMIO
    if (sys_audioapi == API_MMIO)
        return (mmio_send_dacs());
    else
#endif
    post("unknown API");    
    return (0);
}

564
t_float sys_getsr(void)
Miller Puckette's avatar
Miller Puckette committed
565
{
566
567
    // when starting with -nogui option, it is conceivable that sys_dacsr
    // is not initialized even though the audio_rate by this time has
568
    // proper sample rate entered. Hence, we check for this here and avoid
569
570
    // the -nogui nonsense where we have to do various workarounds to make
    // sure that audio objects that depend on this call get the proper sr
571
    if (sys_dacsr == 0 && audio_rate > 0)
572
573
574
575
    {
        sys_dacsr = (t_float)audio_rate;
    }
    return (sys_dacsr);
Miller Puckette's avatar
Miller Puckette committed
576
577
578
579
580
581
582
583
584
585
586
587
}

int sys_get_outchannels(void)
{
     return (sys_outchannels); 
}

int sys_get_inchannels(void) 
{
     return (sys_inchannels);
}

588
void sys_getmeters(t_sample *inmax, t_sample *outmax)
Miller Puckette's avatar
Miller Puckette committed
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
{
    if (inmax)
    {
        sys_meters = 1;
        *inmax = sys_inmax;
        *outmax = sys_outmax;
    }
    else
        sys_meters = 0;
    sys_inmax = sys_outmax = 0;
}

void sys_reportidle(void)
{
}

static void audio_getdevs(char *indevlist, int *nindevs,
Miller Puckette's avatar
Miller Puckette committed
606
    char *outdevlist, int *noutdevs, int *canmulti, int *cancallback,
Miller Puckette's avatar
Miller Puckette committed
607
608
609
        int maxndev, int devdescsize)
{
    audio_init();
Miller Puckette's avatar
Miller Puckette committed
610
    *cancallback = 0;   /* may be overridden by specific API implementation */
Miller Puckette's avatar
Miller Puckette committed
611
612
613
614
615
#ifdef USEAPI_PORTAUDIO
    if (sys_audioapi == API_PORTAUDIO)
    {
        pa_getdevs(indevlist, nindevs, outdevlist, noutdevs, canmulti,
            maxndev, devdescsize);
Miller Puckette's avatar
Miller Puckette committed
616
        *cancallback = 1;
Miller Puckette's avatar
Miller Puckette committed
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
    }
    else
#endif
#ifdef USEAPI_JACK
    if (sys_audioapi == API_JACK)
    {
        jack_getdevs(indevlist, nindevs, outdevlist, noutdevs, canmulti,
            maxndev, devdescsize);
    }
    else
#endif
#ifdef USEAPI_OSS
    if (sys_audioapi == API_OSS)
    {
        oss_getdevs(indevlist, nindevs, outdevlist, noutdevs, canmulti,
            maxndev, devdescsize);
    }
    else
#endif
#ifdef USEAPI_ALSA
    if (sys_audioapi == API_ALSA)
    {
        alsa_getdevs(indevlist, nindevs, outdevlist, noutdevs, canmulti,
            maxndev, devdescsize);
    }
    else
#endif
#ifdef USEAPI_MMIO
    if (sys_audioapi == API_MMIO)
    {
        mmio_getdevs(indevlist, nindevs, outdevlist, noutdevs, canmulti,
            maxndev, devdescsize);
    }
    else
#endif
    {
            /* this shouldn't happen once all the above get filled in. */
        int i;
        *nindevs = *noutdevs = 3;
        for (i = 0; i < 3; i++)
        {
            sprintf(indevlist + i * devdescsize, "input device #%d", i+1);
            sprintf(outdevlist + i * devdescsize, "output device #%d", i+1);
        }
        *canmulti = 0;
    }
}


static void sys_listaudiodevs(void )
{
    char indevlist[MAXNDEV*DEVDESCSIZE], outdevlist[MAXNDEV*DEVDESCSIZE];
Miller Puckette's avatar
Miller Puckette committed
669
    int nindevs = 0, noutdevs = 0, i, canmulti = 0, cancallback = 0;
Miller Puckette's avatar
Miller Puckette committed
670
671

    audio_getdevs(indevlist, &nindevs, outdevlist, &noutdevs, &canmulti,
Miller Puckette's avatar
Miller Puckette committed
672
        &cancallback, MAXNDEV, DEVDESCSIZE);
Miller Puckette's avatar
Miller Puckette committed
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

    if (!nindevs)
        post("no audio input devices found");
    else
    {
            /* To agree with command line flags, normally start at 1 */
            /* But microsoft "MMIO" device list starts at 0 (the "mapper"). */
            /* (see also sys_mmio variable in s_main.c)  */

        post("audio input devices:");
        for (i = 0; i < nindevs; i++)
            post("%d. %s", i + (sys_audioapi != API_MMIO),
                indevlist + i * DEVDESCSIZE);
    }
    if (!noutdevs)
        post("no audio output devices found");
    else
    {
        post("audio output devices:");
        for (i = 0; i < noutdevs; i++)
            post("%d. %s", i + (sys_audioapi != API_MMIO),
                outdevlist + i * DEVDESCSIZE);
    }
    post("API number %d\n", sys_audioapi);
}


    /* start an audio settings dialog window */
void glob_audio_properties(t_pd *dummy, t_floatarg flongform)
{
    char buf[1024 + 2 * MAXNDEV*(DEVDESCSIZE+4)];
        /* these are the devices you're using: */
    int naudioindev, audioindev[MAXAUDIOINDEV], chindev[MAXAUDIOINDEV];
    int naudiooutdev, audiooutdev[MAXAUDIOOUTDEV], choutdev[MAXAUDIOOUTDEV];
    int audioindev1, audioindev2, audioindev3, audioindev4,
        audioinchan1, audioinchan2, audioinchan3, audioinchan4,
        audiooutdev1, audiooutdev2, audiooutdev3, audiooutdev4,
        audiooutchan1, audiooutchan2, audiooutchan3, audiooutchan4;
711
    int rate, advance, callback, blocksize;
Miller Puckette's avatar
Miller Puckette committed
712
713
        /* these are all the devices on your system: */
    char indevlist[MAXNDEV*DEVDESCSIZE], outdevlist[MAXNDEV*DEVDESCSIZE];
Miller Puckette's avatar
Miller Puckette committed
714
    int nindevs = 0, noutdevs = 0, canmulti = 0, cancallback = 0, i;
Miller Puckette's avatar
Miller Puckette committed
715
716

    audio_getdevs(indevlist, &nindevs, outdevlist, &noutdevs, &canmulti,
Miller Puckette's avatar
Miller Puckette committed
717
         &cancallback, MAXNDEV, DEVDESCSIZE);
Miller Puckette's avatar
Miller Puckette committed
718

pokergaming's avatar
pokergaming committed
719
720
721
722
723
724
725

    gui_start_vmess("gui_audio_properties", "s",
        gfxstub_new2(&glob_pdobject, (void *)glob_audio_properties));

    //sys_gui("global audio_indevlist; set audio_indevlist {}\n");

    gui_start_array(); // input devices
Miller Puckette's avatar
Miller Puckette committed
726
    for (i = 0; i < nindevs; i++)
pokergaming's avatar
pokergaming committed
727
728
729
730
731
732
    {
        //sys_vgui("lappend audio_indevlist {%s}\n",
        //    indevlist + i * DEVDESCSIZE);
        gui_s(indevlist + i * DEVDESCSIZE);
    }
    gui_end_array();
Miller Puckette's avatar
Miller Puckette committed
733

pokergaming's avatar
pokergaming committed
734
735
//    sys_gui("global audio_outdevlist; set audio_outdevlist {}\n");
    gui_start_array(); // output devices
Miller Puckette's avatar
Miller Puckette committed
736
    for (i = 0; i < noutdevs; i++)
pokergaming's avatar
pokergaming committed
737
738
739
740
741
742
    {
        //sys_vgui("lappend audio_outdevlist {%s}\n",
        //    outdevlist + i * DEVDESCSIZE);
        gui_s(outdevlist + i * DEVDESCSIZE);
    }
    gui_end_array();
Miller Puckette's avatar
Miller Puckette committed
743
744

    sys_get_audio_params(&naudioindev, audioindev, chindev,
745
746
        &naudiooutdev, audiooutdev, choutdev, &rate, &advance, &callback,
            &blocksize);
747
    //rate = (int)sys_getsr();
748
749

#ifdef USEAPI_JACK
750
751
752
753
    if (sys_audioapiopened == API_JACK)
    {
        sys_setchsr(audio_nextinchans, audio_nextoutchans, rate);
    }
754
#endif /* JACK */
755

Miller Puckette's avatar
Miller Puckette committed
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
    /* post("naudioindev %d naudiooutdev %d longform %f",
            naudioindev, naudiooutdev, flongform); */
    if (naudioindev > 1 || naudiooutdev > 1)
        flongform = 1;

    audioindev1 = (naudioindev > 0 &&  audioindev[0]>= 0 ? audioindev[0] : 0);
    audioindev2 = (naudioindev > 1 &&  audioindev[1]>= 0 ? audioindev[1] : 0);
    audioindev3 = (naudioindev > 2 &&  audioindev[2]>= 0 ? audioindev[2] : 0);
    audioindev4 = (naudioindev > 3 &&  audioindev[3]>= 0 ? audioindev[3] : 0);
    audioinchan1 = (naudioindev > 0 ? chindev[0] : 0);
    audioinchan2 = (naudioindev > 1 ? chindev[1] : 0);
    audioinchan3 = (naudioindev > 2 ? chindev[2] : 0);
    audioinchan4 = (naudioindev > 3 ? chindev[3] : 0);
    audiooutdev1 = (naudiooutdev > 0 && audiooutdev[0]>=0 ? audiooutdev[0] : 0);  
    audiooutdev2 = (naudiooutdev > 1 && audiooutdev[1]>=0 ? audiooutdev[1] : 0);  
    audiooutdev3 = (naudiooutdev > 2 && audiooutdev[2]>=0 ? audiooutdev[2] : 0);  
    audiooutdev4 = (naudiooutdev > 3 && audiooutdev[3]>=0 ? audiooutdev[3] : 0);  
    audiooutchan1 = (naudiooutdev > 0 ? choutdev[0] : 0);
    audiooutchan2 = (naudiooutdev > 1 ? choutdev[1] : 0);
    audiooutchan3 = (naudiooutdev > 2 ? choutdev[2] : 0);
    audiooutchan4 = (naudiooutdev > 3 ? choutdev[3] : 0);
pokergaming's avatar
pokergaming committed
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816

//    sprintf(buf,
//"pdtk_audio_dialog %%s \
//%d %d %d %d %d %d %d %d \
//%d %d %d %d %d %d %d %d \
//%d %d %d %d %d %d\n",
//        audioindev1, audioindev2, audioindev3, audioindev4, 
//        audioinchan1, audioinchan2, audioinchan3, audioinchan4, 
//        audiooutdev1, audiooutdev2, audiooutdev3, audiooutdev4,
//        audiooutchan1, audiooutchan2, audiooutchan3, audiooutchan4, 
//        rate, advance, canmulti, (cancallback ? callback : -1),
//        (flongform != 0), blocksize);

    gui_start_array(); // audio input devices
    gui_i(audioindev1); gui_i(audioindev2);
    gui_i(audioindev3); gui_i(audioindev4);
    gui_end_array();

    gui_start_array(); // audio input channels
    gui_i(audioinchan1); gui_i(audioinchan2);
    gui_i(audioinchan3); gui_i(audioinchan4);
    gui_end_array();

    gui_start_array(); // audio output devices
    gui_i(audiooutdev1); gui_i(audiooutdev2);
    gui_i(audiooutdev3); gui_i(audiooutdev4);
    gui_end_array();

    gui_start_array(); // audio output channels
    gui_i(audiooutchan1); gui_i(audiooutchan2);
    gui_i(audiooutchan3); gui_i(audiooutchan4);
    gui_end_array();

    gui_start_array();
    gui_s("rate");        gui_i(rate);
    gui_s("advance");     gui_i(advance);
    gui_s("canmulti");    gui_i(canmulti);
    gui_s("cancallback"); gui_i(cancallback ? callback : -1);
    gui_s("flongform");   gui_i(flongform != 0);
    gui_s("blocksize");   gui_i(blocksize);
817
818
    gui_s("current-api"); gui_i(sys_audioapi); // cannot figure out how this
                                               // originally got set
pokergaming's avatar
pokergaming committed
819
820
821
822
823
824
825
    gui_end_array();

    gui_end_vmess();

    // not sure why we were deleting the key 0 here...
//    gfxstub_deleteforkey(0);
//    gfxstub_new(&glob_pdobject, (void *)glob_audio_properties, buf);
Miller Puckette's avatar
Miller Puckette committed
826
827
}

Miller Puckette's avatar
Miller Puckette committed
828
extern int pa_foo;
Miller Puckette's avatar
Miller Puckette committed
829
830
831
    /* new values from dialog window */
void glob_audio_dialog(t_pd *dummy, t_symbol *s, int argc, t_atom *argv)
{
832
833
834
835
    int naudioindev, audioindev[MAXAUDIOINDEV], chindev[MAXAUDIOINDEV];
    int naudiooutdev, audiooutdev[MAXAUDIOOUTDEV], choutdev[MAXAUDIOOUTDEV];
    int rate, advance, audioon, i, nindev, noutdev;
    int audioindev1, audioinchan1, audiooutdev1, audiooutchan1;
Miller Puckette's avatar
Miller Puckette committed
836
837
838
839
840
    int newaudioindev[4], newaudioinchan[4],
        newaudiooutdev[4], newaudiooutchan[4];
        /* the new values the dialog came back with: */
    int newrate = atom_getintarg(16, argc, argv);
    int newadvance = atom_getintarg(17, argc, argv);
Miller Puckette's avatar
Miller Puckette committed
841
    int newcallback = atom_getintarg(18, argc, argv);
842
    int newblocksize = atom_getintarg(19, argc, argv);
Miller Puckette's avatar
Miller Puckette committed
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873

    for (i = 0; i < 4; i++)
    {
        newaudioindev[i] = atom_getintarg(i, argc, argv);
        newaudioinchan[i] = atom_getintarg(i+4, argc, argv);
        newaudiooutdev[i] = atom_getintarg(i+8, argc, argv);
        newaudiooutchan[i] = atom_getintarg(i+12, argc, argv);
    }

    for (i = 0, nindev = 0; i < 4; i++)
    {
        if (newaudioinchan[i])
        {
            newaudioindev[nindev] = newaudioindev[i];
            newaudioinchan[nindev] = newaudioinchan[i];
            /* post("in %d %d %d", nindev,
                newaudioindev[nindev] , newaudioinchan[nindev]); */
            nindev++;
        }
    }
    for (i = 0, noutdev = 0; i < 4; i++)
    {
        if (newaudiooutchan[i])
        {
            newaudiooutdev[noutdev] = newaudiooutdev[i];
            newaudiooutchan[noutdev] = newaudiooutchan[i];
            /* post("out %d %d %d", noutdev,
                newaudiooutdev[noutdev] , newaudioinchan[noutdev]); */
            noutdev++;
        }
    }
Miller Puckette's avatar
Miller Puckette committed
874
    
875
    sys_set_audio_settings_reopen(nindev, newaudioindev, nindev, newaudioinchan,
876
        noutdev, newaudiooutdev, noutdev, newaudiooutchan,
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
        newrate, newadvance, newcallback, newblocksize);
}


void sys_set_audio_settings_reopen(int naudioindev, int *audioindev, int nchindev,
    int *chindev, int naudiooutdev, int *audiooutdev, int nchoutdev,
    int *choutdev, int rate, int advance, int callback, int newblocksize)
{
    if (callback < 0)
        callback = 0;
    if (newblocksize != (1<<ilog2(newblocksize)) ||
        newblocksize < DEFDACBLKSIZE || newblocksize > 2048)
            newblocksize = DEFDACBLKSIZE;
    
    if (!audio_callback_is_open && !callback)
        sys_close_audio();
    sys_set_audio_settings(naudioindev, audioindev, nchindev, chindev,
        naudiooutdev, audiooutdev, nchoutdev, choutdev,
        rate, advance, (callback >= 0 ? callback : 0), newblocksize);
    if (!audio_callback_is_open && !callback)
Miller Puckette's avatar
Miller Puckette committed
897
        sys_reopen_audio();
898
    else sched_reopenmeplease();
Miller Puckette's avatar
Miller Puckette committed
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
}

void sys_listdevs(void )
{
#ifdef USEAPI_PORTAUDIO
    if (sys_audioapi == API_PORTAUDIO)
        sys_listaudiodevs();
    else 
#endif
#ifdef USEAPI_JACK
    if (sys_audioapi == API_JACK)
        jack_listdevs();
    else
#endif
#ifdef USEAPI_OSS
    if (sys_audioapi == API_OSS)
        sys_listaudiodevs();
    else
#endif
#ifdef USEAPI_ALSA
    if (sys_audioapi == API_ALSA)
        sys_listaudiodevs();
    else
#endif
#ifdef USEAPI_MMIO
    if (sys_audioapi == API_MMIO)
        sys_listaudiodevs();
    else
#endif
    post("unknown API");    

    sys_listmididevs();
}

933
void sys_setblocksize(int n)
Miller Puckette's avatar
Miller Puckette committed
934
{
935
936
937
938
939
940
    if (n < 1)
        n = 1;
    if (n != (1 << ilog2(n)))
        post("warning: adjusting blocksize to power of 2: %d", 
            (n = (1 << ilog2(n))));
    sys_blocksize = n;
Miller Puckette's avatar
Miller Puckette committed
941
942
}

943
944
945
946
947
948
949
950
951
952
void sys_get_audio_devs(char *indevlist, int *nindevs,
    char *outdevlist, int *noutdevs, int *canmulti, int *cancallback, 
                        int maxndev, int devdescsize)
{
  audio_getdevs(indevlist, nindevs,
                outdevlist, noutdevs, 
                canmulti, cancallback, 
                maxndev, devdescsize);
}

Miller Puckette's avatar
Miller Puckette committed
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
void sys_set_audio_api(int which)
{
     sys_audioapi = which;
     if (sys_verbose)
        post("sys_audioapi %d", sys_audioapi);
}

void glob_audio_setapi(void *dummy, t_floatarg f)
{
    int newapi = f;
    if (newapi)
    {
        if (newapi == sys_audioapi)
        {
            if (!audio_isopen())
                sys_reopen_audio();
        }
        else
        {
            sys_close_audio();
            sys_audioapi = newapi;
                /* bash device params back to default */
            audio_naudioindev = audio_naudiooutdev = 1;
            audio_audioindev[0] = audio_audiooutdev[0] = DEFAULTAUDIODEV;
            audio_audiochindev[0] = audio_audiochoutdev[0] = SYS_DEFAULTCH;
            sys_reopen_audio();
        }
        glob_audio_properties(0, 0);
    }
    else if (audio_isopen())
    {
        sys_close_audio();
    }
}

    /* start or stop the audio hardware */
void sys_set_audio_state(int onoff)
{
    if (onoff)  /* start */
    {
        if (!audio_isopen())
            sys_reopen_audio();    
    }
    else
    {
        if (audio_isopen())
            sys_close_audio();
    }
}

void sys_get_audio_apis(char *buf)
{
    int n = 0;
    strcpy(buf, "{ ");
#ifdef USEAPI_OSS
    sprintf(buf + strlen(buf), "{OSS %d} ", API_OSS); n++;
#endif
#ifdef USEAPI_MMIO
    sprintf(buf + strlen(buf), "{\"standard (MMIO)\" %d} ", API_MMIO); n++;
#endif
#ifdef USEAPI_ALSA
    sprintf(buf + strlen(buf), "{ALSA %d} ", API_ALSA); n++;
#endif
#ifdef USEAPI_PORTAUDIO
#ifdef MSW
    sprintf(buf + strlen(buf),
        "{\"ASIO (via portaudio)\" %d} ", API_PORTAUDIO);
#else
#ifdef OSX
    sprintf(buf + strlen(buf),
        "{\"standard (portaudio)\" %d} ", API_PORTAUDIO);
#else
    sprintf(buf + strlen(buf), "{portaudio %d} ", API_PORTAUDIO);
#endif
#endif
     n++;
#endif
#ifdef USEAPI_JACK
Ivica Bukvic's avatar
Ivica Bukvic committed
1031
    sprintf(buf + strlen(buf), "{JACK %d} ", API_JACK); n++;
Miller Puckette's avatar
Miller Puckette committed
1032
1033
1034
1035
1036
1037
1038
#endif
    strcat(buf, "}");
        /* then again, if only one API (or none) we don't offer any choice. */
    if (n < 2)
        strcpy(buf, "{}");
}

1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
void sys_get_audio_apis2(t_binbuf *buf)
{
    int n = 0;
#ifdef USEAPI_OSS
    binbuf_addv(buf, "si", gensym("OSS"), API_OSS); n++;
#endif
#ifdef USEAPI_MMIO
    binbuf_addv(buf, "si", gensym("standard (MMIO)"), API_MMIO); n++;
#endif
#ifdef USEAPI_ALSA
    binbuf_addv(buf, "si", gensym("ALSA"), API_ALSA); n++;
#endif
#ifdef USEAPI_PORTAUDIO
#ifdef MSW
    binbuf_addv(buf, "si", gensym("ASIO (via portaudio)"), API_PORTAUDIO);
#else
#ifdef OSX
    binbuf_addv(buf, "si", gensym("standard (portaudio)"), API_PORTAUDIO);
#else
    binbuf_addv(buf, "si", gensym("portaudio"), API_PORTAUDIO);
#endif
#endif
     n++;
#endif
#ifdef USEAPI_JACK
    binbuf_addv(buf, "si", gensym("JACK"), API_JACK); n++;
#endif
       /* then again, if only one API (or none) we don't offer any choice. */
//    if (n < 2)
//        strcpy(buf, "{}");
}

Miller Puckette's avatar
Miller Puckette committed
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
#ifdef USEAPI_ALSA
void alsa_putzeros(int n);
void alsa_getzeros(int n);
void alsa_printstate( void);
#endif

    /* debugging */
void glob_foo(void *dummy, t_symbol *s, int argc, t_atom *argv)
{
    t_symbol *arg = atom_getsymbolarg(0, argc, argv);
    if (arg == gensym("restart"))
        sys_reopen_audio();
#ifdef USEAPI_ALSA
    else if (arg == gensym("alsawrite"))
    {
        int n = atom_getintarg(1, argc, argv);
        alsa_putzeros(n);
    }
    else if (arg == gensym("alsaread"))
    {
        int n = atom_getintarg(1, argc, argv);
        alsa_getzeros(n);
    }
    else if (arg == gensym("print"))
    {
        alsa_printstate();
    }
#endif
}
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158

/* convert a device name to a (1-based) device number.  (Output device if
'output' parameter is true, otherwise input device).  Negative on failure. */

int sys_audiodevnametonumber(int output, const char *name)
{
    char indevlist[MAXNDEV*DEVDESCSIZE], outdevlist[MAXNDEV*DEVDESCSIZE];
    int nindevs = 0, noutdevs = 0, i, canmulti, cancallback;

    sys_get_audio_devs(indevlist, &nindevs, outdevlist, &noutdevs,
        &canmulti, &cancallback, MAXNDEV, DEVDESCSIZE);

    if (output)
    {
        for (i = 0; i < noutdevs; i++)
        {
            unsigned int comp = strlen(name);
            if (comp > strlen(outdevlist + i * DEVDESCSIZE))
                comp = strlen(outdevlist + i * DEVDESCSIZE);
            if (!strncmp(name, outdevlist + i * DEVDESCSIZE, comp))
                return (i);
        }
    }
    else
    {
        for (i = 0; i < nindevs; i++)
        {
            unsigned int comp = strlen(name);
            if (comp > strlen(indevlist + i * DEVDESCSIZE))
                comp = strlen(indevlist + i * DEVDESCSIZE);
            if (!strncmp(name, indevlist + i * DEVDESCSIZE, comp))
                return (i);
        }
    }
    return (-1);
}

/* convert a (1-based) device number to a device name.  (Output device if
'output' parameter is true, otherwise input device).  Empty string on failure.
*/

void sys_audiodevnumbertoname(int output, int devno, char *name, int namesize)
{
    char indevlist[MAXNDEV*DEVDESCSIZE], outdevlist[MAXNDEV*DEVDESCSIZE];
    int nindevs = 0, noutdevs = 0, i, canmulti, cancallback;
    if (devno < 0)
    {
        *name = 0;
        return;
    }
    sys_get_audio_devs(indevlist, &nindevs, outdevlist, &noutdevs,
        &canmulti, &cancallback, MAXNDEV, DEVDESCSIZE);
    if (output && (devno < noutdevs))
        strncpy(name, outdevlist + devno * DEVDESCSIZE, namesize);
    else if (!output && (devno < nindevs))
        strncpy(name, indevlist + devno * DEVDESCSIZE, namesize);
    else *name = 0;
    name[namesize-1] = 0;
}