root/portaudio/branches/V18.1/pa_tests/debug_test1.c

Revision 90, 3.3 KB (checked in by phil, 8 years ago)

Initial revision

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1/*
2 * $Id$
3 patest1.c
4 Ring modulate the audio input with a 441hz sine wave for 20 seconds
5    using the Portable Audio api
6    Author: Ross Bencina <rossb@audiomulch.com>
7    Modifications:
8 April 5th, 2001 - PLB - Check for NULL inputBuffer.
9*/
10#include <stdio.h>
11#include <math.h>
12#include "portaudio.h"
13#ifndef M_PI
14#define M_PI  (3.14159265)
15#endif
16typedef struct
17{
18    float sine[100];
19    int phase;
20    int sampsToGo;
21}
22patest1data;
23static int patest1Callback( void *inputBuffer, void *outputBuffer,
24                            unsigned long bufferFrames,
25                            PaTimestamp outTime, void *userData )
26{
27    patest1data *data = (patest1data*)userData;
28    float *in = (float*)inputBuffer;
29    float *out = (float*)outputBuffer;
30    int framesToCalc = bufferFrames;
31    unsigned long i;
32    int finished = 0;
33    if(inputBuffer == NULL) return 0;
34    if( data->sampsToGo < bufferFrames )
35    {
36        finished = 1;
37    }
38    for( i=0; i<bufferFrames; i++ )
39    {
40        *out++ = *in++;
41        *out++ = *in++;
42        if( data->phase >= 100 )
43            data->phase = 0;
44    }
45    data->sampsToGo -= bufferFrames;
46    /* zero remainder of final buffer if not already done */
47    for( ; i<bufferFrames; i++ )
48    {
49        *out++ = 0; /* left */
50        *out++ = 0; /* right */
51    }
52    return finished;
53}
54int main(int argc, char* argv[]);
55int main(int argc, char* argv[])
56{
57    PaStream *stream;
58    PaError err;
59    patest1data data;
60    int i;
61    int inputDevice = Pa_GetDefaultInputDeviceID();
62    int outputDevice = Pa_GetDefaultOutputDeviceID();
63    /* initialise sinusoidal wavetable */
64    for( i=0; i<100; i++ )
65        data.sine[i] = sin( ((double)i/100.) * M_PI * 2. );
66    data.phase = 0;
67    data.sampsToGo = 44100 * 4;   // 20 seconds
68    /* initialise portaudio subsytem */
69    Pa_Initialize();
70    err = Pa_OpenStream(
71              &stream,
72              inputDevice,
73              2,              /* stereo input */
74              paFloat32,  /* 32 bit floating point input */
75              NULL,
76              outputDevice,
77              2,              /* stereo output */
78              paFloat32,      /* 32 bit floating point output */
79              NULL,
80              44100.,
81              //    22050,          /* half second buffers */
82              //    4,              /* four buffers */
83              512,          /* half second buffers */
84              0,              /* four buffers */
85              paClipOff,      /* we won't output out of range samples so don't bother clipping them */
86              patest1Callback,
87              &data );
88    if( err == paNoError )
89    {
90        err = Pa_StartStream( stream );
91        //       printf( "Press any key to end.\n" );
92        //       getc( stdin ); //wait for input before exiting
93        //       Pa_AbortStream( stream );
94
95        printf( "Waiting for stream to complete...\n" );
96
97        while( Pa_StreamActive( stream ) )
98            Pa_Sleep(1000); /* sleep until playback has finished */
99
100        err = Pa_CloseStream( stream );
101    }
102    else
103    {
104        fprintf( stderr, "An error occured while opening the portaudio stream\n" );
105        if( err == paHostError )
106            fprintf( stderr, "Host error number: %d\n", Pa_GetHostError() );
107        else
108            fprintf( stderr, "Error number: %d\n", err );
109    }
110    Pa_Terminate();
111    printf( "bye\n" );
112
113    return 0;
114}
Note: See TracBrowser for help on using the browser.