summaryrefslogtreecommitdiff
path: root/apps/plugins/pdbox/PDa/src/s_audio_pa.c
diff options
context:
space:
mode:
Diffstat (limited to 'apps/plugins/pdbox/PDa/src/s_audio_pa.c')
-rw-r--r--apps/plugins/pdbox/PDa/src/s_audio_pa.c291
1 files changed, 0 insertions, 291 deletions
diff --git a/apps/plugins/pdbox/PDa/src/s_audio_pa.c b/apps/plugins/pdbox/PDa/src/s_audio_pa.c
index ebae9679dc..943b57e212 100644
--- a/apps/plugins/pdbox/PDa/src/s_audio_pa.c
+++ b/apps/plugins/pdbox/PDa/src/s_audio_pa.c
@@ -290,295 +290,4 @@ void pa_getdevs(char *indevlist, int *nindevs,
290 *nindevs = nin; 290 *nindevs = nin;
291 *noutdevs = nout; 291 *noutdevs = nout;
292} 292}
293/* Copyright (c) 2001 Miller Puckette and others.
294* For information on usage and redistribution, and for a DISCLAIMER OF ALL
295* WARRANTIES, see the file, "LICENSE.txt," in this distribution. */
296
297/* this file calls Ross Bencina's and Phil Burk's Portaudio package. It's
298 the main way in for Mac OS and, with Michael Casey's help, also into
299 ASIO in Windows. */
300
301
302#include "m_pd.h"
303#include "s_stuff.h"
304#include <stdio.h>
305#include <stdlib.h>
306#include "portaudio.h"
307#include "pablio_pd.h"
308
309 /* LATER try to figure out how to handle default devices in portaudio;
310 the way s_audio.c handles them isn't going to work here. */
311
312#if defined(MACOSX) || defined(MSW)
313#define Pa_GetDefaultInputDevice Pa_GetDefaultInputDeviceID
314#define Pa_GetDefaultOutputDevice Pa_GetDefaultOutputDeviceID
315#endif
316
317 /* public interface declared in m_imp.h */
318
319 /* implementation */
320static PABLIO_Stream *pa_stream;
321static int pa_inchans, pa_outchans;
322static float *pa_soundin, *pa_soundout;
323
324#define MAX_PA_CHANS 32
325#define MAX_SAMPLES_PER_FRAME MAX_PA_CHANS * DEFDACBLKSIZE
326
327int pa_open_audio(int inchans, int outchans, int rate, t_sample *soundin,
328 t_sample *soundout, int framesperbuf, int nbuffers,
329 int indeviceno, int outdeviceno)
330{
331 PaError err;
332 static int initialized;
333 int j, devno, pa_indev = 0, pa_outdev = 0;
334
335 if (!initialized)
336 {
337 /* Initialize PortAudio */
338 int err = Pa_Initialize();
339 if ( err != paNoError )
340 {
341 fprintf( stderr,
342 "Error number %d occured initializing portaudio\n",
343 err);
344 fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) );
345 return (1);
346 }
347 initialized = 1;
348 }
349 /* post("in %d out %d rate %d device %d", inchans, outchans, rate, deviceno); */
350 if (inchans != 0 && outchans != 0 && inchans != outchans)
351 error("portaudio: number of input and output channels must match");
352 if (inchans > MAX_PA_CHANS)
353 {
354 post("input channels reduced to maximum %d", MAX_PA_CHANS);
355 inchans = MAX_PA_CHANS;
356 }
357 if (outchans > MAX_PA_CHANS)
358 {
359 post("output channels reduced to maximum %d", MAX_PA_CHANS);
360 outchans = MAX_PA_CHANS;
361 }
362
363 if (inchans > 0)
364 {
365 for (j = 0, devno = 0; j < Pa_CountDevices(); j++)
366 {
367 const PaDeviceInfo *info = Pa_GetDeviceInfo(j);
368 if (info->maxInputChannels > 0)
369 {
370 if (devno == indeviceno)
371 {
372 pa_indev = j;
373 break;
374 }
375 devno++;
376 }
377 }
378 }
379
380 if (outchans > 0)
381 {
382 for (j = 0, devno = 0; j < Pa_CountDevices(); j++)
383 {
384 const PaDeviceInfo *info = Pa_GetDeviceInfo(j);
385 if (info->maxOutputChannels > 0)
386 {
387 if (devno == outdeviceno)
388 {
389 pa_outdev = j;
390 break;
391 }
392 devno++;
393 }
394 }
395 }
396 293
397 if (sys_verbose)
398 {
399 post("input device %d, channels %d", pa_indev, inchans);
400 post("output device %d, channels %d", pa_outdev, outchans);
401 post("framesperbuf %d, nbufs %d", framesperbuf, nbuffers);
402 }
403 if (inchans && outchans)
404 err = OpenAudioStream( &pa_stream, rate, paFloat32,
405 PABLIO_READ_WRITE, inchans, framesperbuf, nbuffers,
406 pa_indev, pa_outdev);
407 else if (inchans)
408 err = OpenAudioStream( &pa_stream, rate, paFloat32,
409 PABLIO_READ, inchans, framesperbuf, nbuffers,
410 pa_indev, pa_outdev);
411 else if (outchans)
412 err = OpenAudioStream( &pa_stream, rate, paFloat32,
413 PABLIO_WRITE, outchans, framesperbuf, nbuffers,
414 pa_indev, pa_outdev);
415 else err = 0;
416 if ( err != paNoError )
417 {
418 fprintf( stderr, "Error number %d occured opening portaudio stream\n",
419 err);
420 fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) );
421 Pa_Terminate();
422 sys_inchannels = sys_outchannels = 0;
423 return (1);
424 }
425 else if (sys_verbose)
426 post("... opened OK.");
427 pa_inchans = inchans;
428 pa_outchans = outchans;
429 pa_soundin = soundin;
430 pa_soundout = soundout;
431 return (0);
432}
433
434void pa_close_audio( void)
435{
436 if (pa_inchans || pa_outchans)
437 CloseAudioStream( pa_stream );
438 pa_inchans = pa_outchans = 0;
439}
440
441int pa_send_dacs(void)
442{
443 float samples[MAX_SAMPLES_PER_FRAME], *fp1, *fp2;
444 int i, j;
445 double timebefore;
446
447 timebefore = sys_getrealtime();
448 if ((pa_inchans && GetAudioStreamReadable(pa_stream) < DEFDACBLKSIZE) ||
449 (pa_outchans && GetAudioStreamWriteable(pa_stream) < DEFDACBLKSIZE))
450 {
451 if (pa_inchans && pa_outchans)
452 {
453 int synced = 0;
454 while (GetAudioStreamWriteable(pa_stream) > 2*DEFDACBLKSIZE)
455 {
456 for (j = 0; j < pa_outchans; j++)
457 for (i = 0, fp2 = samples + j; i < DEFDACBLKSIZE; i++,
458 fp2 += pa_outchans)
459 {
460 *fp2 = 0;
461 }
462 synced = 1;
463 WriteAudioStream(pa_stream, samples, DEFDACBLKSIZE);
464 }
465 while (GetAudioStreamReadable(pa_stream) > 2*DEFDACBLKSIZE)
466 {
467 synced = 1;
468 ReadAudioStream(pa_stream, samples, DEFDACBLKSIZE);
469 }
470 /* if (synced)
471 post("sync"); */
472 }
473 return (SENDDACS_NO);
474 }
475 if (pa_inchans)
476 {
477 ReadAudioStream(pa_stream, samples, DEFDACBLKSIZE);
478 for (j = 0, fp1 = pa_soundin; j < pa_inchans; j++, fp1 += DEFDACBLKSIZE)
479 for (i = 0, fp2 = samples + j; i < DEFDACBLKSIZE; i++,
480 fp2 += pa_inchans)
481 {
482 fp1[i] = *fp2;
483 }
484 }
485#if 0
486 {
487 static int nread;
488 if (nread == 0)
489 {
490 post("it's %f %f %f %f",
491 pa_soundin[0], pa_soundin[1], pa_soundin[2], pa_soundin[3]);
492 nread = 1000;
493 }
494 nread--;
495 }
496#endif
497 if (pa_outchans)
498 {
499 for (j = 0, fp1 = pa_soundout; j < pa_outchans; j++,
500 fp1 += DEFDACBLKSIZE)
501 for (i = 0, fp2 = samples + j; i < DEFDACBLKSIZE; i++,
502 fp2 += pa_outchans)
503 {
504 *fp2 = fp1[i];
505 fp1[i] = 0;
506 }
507 WriteAudioStream(pa_stream, samples, DEFDACBLKSIZE);
508 }
509
510 if (sys_getrealtime() > timebefore + 0.002)
511 {
512 /* post("slept"); */
513 return (SENDDACS_SLEPT);
514 }
515 else return (SENDDACS_YES);
516}
517
518
519void pa_listdevs(void) /* lifted from pa_devs.c in portaudio */
520{
521 int i,j;
522 int numDevices;
523 const PaDeviceInfo *pdi;
524 PaError err;
525 Pa_Initialize();
526 numDevices = Pa_CountDevices();
527 if( numDevices < 0 )
528 {
529 fprintf(stderr, "ERROR: Pa_CountDevices returned 0x%x\n", numDevices );
530 err = numDevices;
531 goto error;
532 }
533 fprintf(stderr, "Audio Devices:\n");
534 for( i=0; i<numDevices; i++ )
535 {
536 pdi = Pa_GetDeviceInfo( i );
537 fprintf(stderr, "device %d:", i+1 );
538 fprintf(stderr, " %s;", pdi->name );
539 fprintf(stderr, "%d inputs, ", pdi->maxInputChannels );
540 fprintf(stderr, "%d outputs", pdi->maxOutputChannels );
541 if ( i == Pa_GetDefaultInputDevice() )
542 fprintf(stderr, " (Default Input)");
543 if ( i == Pa_GetDefaultOutputDevice() )
544 fprintf(stderr, " (Default Output)");
545 fprintf(stderr, "\n");
546 }
547
548 fprintf(stderr, "\n");
549 return;
550
551error:
552 fprintf( stderr, "An error occured while using the portaudio stream\n" );
553 fprintf( stderr, "Error number: %d\n", err );
554 fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) );
555
556}
557
558 /* scanning for devices */
559void pa_getdevs(char *indevlist, int *nindevs,
560 char *outdevlist, int *noutdevs, int *canmulti,
561 int maxndev, int devdescsize)
562{
563 int i, nin = 0, nout = 0, ndev;
564 *canmulti = 1; /* one dev each for input and output */
565
566 Pa_Initialize();
567 ndev = Pa_CountDevices();
568 for (i = 0; i < ndev; i++)
569 {
570 const PaDeviceInfo *pdi = Pa_GetDeviceInfo(i);
571 if (pdi->maxInputChannels > 0 && nin < maxndev)
572 {
573 strcpy(indevlist + nin * devdescsize, pdi->name);
574 nin++;
575 }
576 if (pdi->maxOutputChannels > 0 && nout < maxndev)
577 {
578 strcpy(outdevlist + nout * devdescsize, pdi->name);
579 nout++;
580 }
581 }
582 *nindevs = nin;
583 *noutdevs = nout;
584}