| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 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 |
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 |
|
| 47 |
for( ; i<bufferFrames; i++ ) |
| 48 |
{ |
| 49 |
*out++ = 0; |
| 50 |
*out++ = 0; |
| 51 |
} |
| 52 |
return finished; |
| 53 |
} |
| 54 |
int main(int argc, char* argv[]); |
| 55 |
int 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 |
|
| 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; |
| 68 |
|
| 69 |
Pa_Initialize(); |
| 70 |
err = Pa_OpenStream( |
| 71 |
&stream, |
| 72 |
inputDevice, |
| 73 |
2, |
| 74 |
paFloat32, |
| 75 |
NULL, |
| 76 |
outputDevice, |
| 77 |
2, |
| 78 |
paFloat32, |
| 79 |
NULL, |
| 80 |
44100., |
| 81 |
|
| 82 |
|
| 83 |
512, |
| 84 |
0, |
| 85 |
paClipOff, |
| 86 |
patest1Callback, |
| 87 |
&data ); |
| 88 |
if( err == paNoError ) |
| 89 |
{ |
| 90 |
err = Pa_StartStream( stream ); |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
printf( "Waiting for stream to complete...\n" ); |
| 96 |
|
| 97 |
while( Pa_StreamActive( stream ) ) |
| 98 |
Pa_Sleep(1000); |
| 99 |
|
| 100 |
err = Pa_CloseStream( stream ); |
| 101 |
} |
| 102 |
else |
| 103 |
{ |
| •104 |
fprintf( stderr, "An error occurred 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 |
} |