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
|
/* 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.
*/
#include "m_pd.h"
#include "s_stuff.h"
#include <stdio.h>
#ifdef UNIX
#include <unistd.h>
#include <sys/time.h>
#include <sys/resource.h>
#endif
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#define SYS_DEFAULTCH 2
#define SYS_MAXCH 100
#define SYS_DEFAULTSRATE 44100
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 MAXAUDIODEV 4
int sys_inchannels;
int sys_outchannels;
int sys_advance_samples; /* scheduler advance in samples */
int sys_blocksize = 256; /* audio I/O block size in sample frames */
int sys_audioapi = API_DEFAULT;
static int sys_meters; /* true if we're metering */
static float sys_inmax; /* max input amplitude */
static float sys_outmax; /* max output amplitude */
/* exported variables */
#ifdef MSW
#define DEFAULTADVANCE 70000
#else
#define DEFAULTADVANCE 50000
#endif
int sys_schedadvance = DEFAULTADVANCE; /* scheduler advance in microseconds */
float sys_dacsr;
int sys_hipriority = 0;
t_sample *sys_soundout;
t_sample *sys_soundin;
/* set channels and sample rate. */
static void sys_setchsr(int chin, int chout, int sr)
{
int nblk;
int inbytes = chin * (DEFDACBLKSIZE*sizeof(float));
int outbytes = chout * (DEFDACBLKSIZE*sizeof(float));
sys_inchannels = chin;
sys_outchannels = chout;
sys_dacsr = sr;
sys_advance_samples = (sys_schedadvance * sys_dacsr) / (1000000.);
if (sys_advance_samples < 3 * DEFDACBLKSIZE)
sys_advance_samples = 3 * DEFDACBLKSIZE;
if (sys_soundin)
free(sys_soundin);
sys_soundin = (t_float *)malloc(inbytes);
memset(sys_soundin, 0, inbytes);
if (sys_soundout)
free(sys_soundout);
sys_soundout = (t_float *)malloc(outbytes);
memset(sys_soundout, 0, outbytes);
if (sys_verbose)
post("input channels = %d, output channels = %d",
sys_inchannels, sys_outchannels);
}
/* ----------------------- public routines ----------------------- */
#if 0
void sys_open_audio(int naudioindev, int *audioindev, int nchindev,
int *chindev, int naudiooutdev, int *audiooutdev, int nchoutdev,
int *choutdev, int rate) /* IOhannes */
{
int inchans=
(nchindev > 0 ? chindev[0] : (nchindev == 0 ? 0 : SYS_DEFAULTCH));
int outchans=
(nchoutdev > 0 ? choutdev[0] : (nchoutdev == 0 ? 0 : SYS_DEFAULTCH));
int soundindev = (naudioindev <= 0 ? -1 : (audioindev[0]-1));
int soundoutdev = (naudiooutdev <= 0 ? -1 : (audiooutdev[0]-1));
int sounddev = (inchans > 0 ? soundindev : soundoutdev);
if (naudioindev > 1 || nchindev > 1 || naudiooutdev > 1 || nchoutdev > 1)
post("sorry, only one portaudio device can be open at once.\n");
/* post("nindev %d, noutdev %d", naudioindev, naudiooutdev);
post("soundindev %d, soundoutdev %d", soundindev, soundoutdev); */
if (sys_verbose)
post("channels in %d, out %d", inchans, outchans);
if (rate < 1)
rate = SYS_DEFAULTSRATE;
sys_setchsr(inchans, outchans, rate);
pa_open_audio(inchans, outchans, rate, sys_soundin, sys_soundout,
sys_blocksize, sys_advance_samples/sys_blocksize,
soundindev, soundoutdev);
}
#endif
void sys_open_audio(int naudioindev, int *audioindev, int nchindev,
int *chindev, int naudiooutdev, int *audiooutdev, int nchoutdev,
int *choutdev, int rate) /* IOhannes */
{
int i, *ip;
int defaultchannels = SYS_DEFAULTCH;
int inchans, outchans;
if (rate < 1)
rate = SYS_DEFAULTSRATE;
if (naudioindev == -1)
{ /* not set */
if (nchindev == -1)
{
nchindev=1;
chindev[0] = defaultchannels;
naudioindev = 1;
audioindev[0] = DEFAULTAUDIODEV;
}
else
{
for (i = 0; i < MAXAUDIODEV; i++)
audioindev[i] = i+1;
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)
{
nchoutdev=1;
choutdev[0]=defaultchannels;
naudiooutdev=1;
audiooutdev[0] = DEFAULTAUDIODEV;
}
else
{
for (i = 0; i < MAXAUDIODEV; i++)
audiooutdev[i] = i+1;
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;
}
}
for (i = inchans = 0; i < naudioindev; i++)
inchans += chindev[i];
for (i = outchans = 0; i < naudiooutdev; i++)
outchans += choutdev[i];
sys_setchsr(inchans, outchans, rate);
#ifdef USEAPI_OSS
if (sys_audioapi == API_OSS)
oss_open_audio(naudioindev, audioindev, nchindev, chindev,
naudiooutdev, audiooutdev, nchoutdev, choutdev, rate);
else
#endif
#ifdef USEAPI_ALSA
/* for alsa, the "device numbers" are ignored; they are sent
straight to the alsa code via linux_alsa_devname(). Only one
device is supported for each of input, output. */
if (sys_audioapi == API_ALSA)
alsa_open_audio((naudioindev > 0 ? chindev[0] : 0),
(naudiooutdev > 0 ? choutdev[0] : 0), rate);
else
#endif
#ifdef USEAPI_PORTAUDIO
if (sys_audioapi == API_PORTAUDIO)
pa_open_audio(inchans, outchans, rate, sys_soundin, sys_soundout,
sys_blocksize, sys_advance_samples/sys_blocksize,
(naudiooutdev > 0 ? audioindev[0] : 0),
(naudiooutdev > 0 ? audiooutdev[0] : 0));
else
#endif
#ifdef USEAPI_MMIO
if (sys_audioapi == API_MMIO)
mmio_open_audio(naudioindev, audioindev, nchindev, chindev,
naudiooutdev, audiooutdev, nchoutdev, choutdev, rate);
else
#endif
post("unknown audio API specified");
}
void sys_close_audio(void)
{
#ifdef USEAPI_PORTAUDIO
if (sys_audioapi == API_PORTAUDIO)
pa_close_audio();
else
#endif
#ifdef USEAPI_OSS
if (sys_audioapi == API_OSS)
oss_close_audio();
else
#endif
#ifdef USEAPI_ALSA
if (sys_audioapi == API_ALSA)
alsa_close_audio();
else
#ifdef USEAPI_MMIO
if (sys_audioapi == API_MMIO)
mmio_close_audio();
else
#endif
#endif
post("unknown API");
}
int sys_send_dacs(void)
{
if (sys_meters)
{
int i, n;
float maxsamp;
for (i = 0, n = sys_inchannels * DEFDACBLKSIZE, maxsamp = sys_inmax;
i < n; i++)
{
float f = sys_soundin[i];
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++)
{
float f = sys_soundout[i];
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_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);
}
float sys_getsr(void)
{
return (sys_dacsr);
}
int sys_get_outchannels(void)
{
return (sys_outchannels);
}
int sys_get_inchannels(void)
{
return (sys_inchannels);
}
void sys_audiobuf(int n)
{
/* set the size, in milliseconds, of the audio FIFO */
if (n < 5) n = 5;
else if (n > 5000) n = 5000;
sys_schedadvance = n * 1000;
}
void sys_getmeters(float *inmax, float *outmax)
{
if (inmax)
{
sys_meters = 1;
*inmax = sys_inmax;
*outmax = sys_outmax;
}
else
sys_meters = 0;
sys_inmax = sys_outmax = 0;
}
void sys_reportidle(void)
{
}
void sys_listdevs(void )
{
#ifdef USEAPI_PORTAUDIO
if (sys_audioapi == API_PORTAUDIO)
pa_listdevs();
else
#endif
#ifdef USEAPI_OSS
if (sys_audioapi == API_OSS)
oss_listdevs();
else
#endif
#ifdef USEAPI_MMIO
if (sys_audioapi == API_MMIO)
mmio_listdevs();
else
#endif
#ifdef USEAPI_ALSA
if (sys_audioapi == API_ALSA)
alsa_listdevs();
else
#endif
post("unknown API");
sys_listmididevs();
}
void sys_setblocksize(int n)
{
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;
}
void sys_set_sound_api(int which)
{
sys_audioapi = which;
if (sys_verbose)
post("sys_audioapi %d", sys_audioapi);
}
|