| 1 | /* |
|---|
| 2 | $Id$ |
|---|
| 3 | patest1.c |
|---|
| 4 | Ring modulate the audio input with a 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 |
|---|
| 16 | typedef struct |
|---|
| 17 | { |
|---|
| 18 | float sine[100]; |
|---|
| 19 | int phase; |
|---|
| 20 | int sampsToGo; |
|---|
| 21 | } |
|---|
| 22 | patest1data; |
|---|
| 23 | static 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 | /* Check to see if any input data is available. */ |
|---|
| 34 | if(inputBuffer == NULL) return 0; |
|---|
| 35 | if( data->sampsToGo < bufferFrames ) |
|---|
| 36 | { |
|---|
| 37 | framesToCalc = data->sampsToGo; |
|---|
| 38 | finished = 1; |
|---|
| 39 | } |
|---|
| 40 | for( i=0; i<framesToCalc; i++ ) |
|---|
| 41 | { |
|---|
| 42 | *out++ = *in++ * data->sine[data->phase]; /* left */ |
|---|
| 43 | *out++ = *in++ * data->sine[data->phase++]; /* right */ |
|---|
| 44 | if( data->phase >= 100 ) |
|---|
| 45 | data->phase = 0; |
|---|
| 46 | } |
|---|
| 47 | data->sampsToGo -= framesToCalc; |
|---|
| 48 | /* zero remainder of final buffer if not already done */ |
|---|
| 49 | for( ; i<bufferFrames; i++ ) |
|---|
| 50 | { |
|---|
| 51 | *out++ = 0; /* left */ |
|---|
| 52 | *out++ = 0; /* right */ |
|---|
| 53 | } |
|---|
| 54 | return finished; |
|---|
| 55 | } |
|---|
| 56 | int main(int argc, char* argv[]); |
|---|
| 57 | int main(int argc, char* argv[]) |
|---|
| 58 | { |
|---|
| 59 | PaStream *stream; |
|---|
| 60 | PaError err; |
|---|
| 61 | patest1data data; |
|---|
| 62 | int i; |
|---|
| 63 | int inputDevice = Pa_GetDefaultInputDeviceID(); |
|---|
| 64 | int outputDevice = Pa_GetDefaultOutputDeviceID(); |
|---|
| 65 | /* initialise sinusoidal wavetable */ |
|---|
| 66 | for( i=0; i<100; i++ ) |
|---|
| 67 | data.sine[i] = sin( ((double)i/100.) * M_PI * 2. ); |
|---|
| 68 | data.phase = 0; |
|---|
| 69 | data.sampsToGo = 44100 * 20; // 20 seconds |
|---|
| 70 | /* initialise portaudio subsytem */ |
|---|
| 71 | Pa_Initialize(); |
|---|
| 72 | err = Pa_OpenStream( |
|---|
| 73 | &stream, |
|---|
| 74 | inputDevice, |
|---|
| 75 | 2, /* stereo input */ |
|---|
| 76 | paFloat32, /* 32 bit floating point input */ |
|---|
| 77 | NULL, |
|---|
| 78 | outputDevice, |
|---|
| 79 | 2, /* stereo output */ |
|---|
| 80 | paFloat32, /* 32 bit floating point output */ |
|---|
| 81 | NULL, |
|---|
| 82 | 44100., |
|---|
| 83 | 512, /* small buffers */ |
|---|
| 84 | 0, /* let PA determine number of 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 | } |
|---|