| 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 |
#include <stdio.h> |
| 42 |
#include <math.h> |
| 43 |
#include "portaudio.h" |
| 44 |
|
| 45 |
#define NUM_SECONDS (8) |
| 46 |
#define SAMPLE_RATE (44100) |
| 47 |
#define FRAMES_PER_BUFFER (512) |
| 48 |
#ifndef M_PI |
| 49 |
#define M_PI (3.14159265) |
| 50 |
#endif |
| 51 |
#define TABLE_SIZE (200) |
| 52 |
typedef struct |
| 53 |
{ |
| 54 |
float sine[TABLE_SIZE]; |
| 55 |
int left_phase; |
| 56 |
int right_phase; |
| 57 |
int toggle; |
| 58 |
int countDown; |
| 59 |
} |
| 60 |
paTestData; |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
static int patestCallback( const void *inputBuffer, |
| 66 |
void *outputBuffer, |
| 67 |
unsigned long framesPerBuffer, |
| 68 |
const PaStreamCallbackTimeInfo* timeInfo, |
| 69 |
PaStreamCallbackFlags statusFlags, |
| 70 |
void *userData ) |
| 71 |
{ |
| 72 |
paTestData *data = (paTestData*)userData; |
| 73 |
float *out = (float*)outputBuffer; |
| 74 |
unsigned long i; |
| 75 |
int finished = 0; |
| 76 |
|
| 77 |
(void) inputBuffer; |
| 78 |
|
| 79 |
for( i=0; i<framesPerBuffer; i++ ) |
| 80 |
{ |
| 81 |
if( data->toggle ) |
| 82 |
{ |
| 83 |
*out++ = data->sine[data->left_phase]; |
| 84 |
*out++ = 0; |
| 85 |
} |
| 86 |
else |
| 87 |
{ |
| 88 |
*out++ = 0; |
| 89 |
*out++ = data->sine[data->right_phase]; |
| 90 |
} |
| 91 |
|
| 92 |
data->left_phase += 1; |
| 93 |
if( data->left_phase >= TABLE_SIZE ) data->left_phase -= TABLE_SIZE; |
| 94 |
data->right_phase += 3; |
| 95 |
if( data->right_phase >= TABLE_SIZE ) data->right_phase -= TABLE_SIZE; |
| 96 |
} |
| 97 |
|
| 98 |
if( data->countDown < 0 ) |
| 99 |
{ |
| 100 |
data->countDown = SAMPLE_RATE; |
| 101 |
data->toggle = !data->toggle; |
| 102 |
} |
| 103 |
data->countDown -= framesPerBuffer; |
| 104 |
|
| 105 |
return finished; |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
int main(void); |
| 110 |
int main(void) |
| 111 |
{ |
| 112 |
PaStream *stream; |
| 113 |
PaStreamParameters outputParameters; |
| 114 |
PaError err; |
| 115 |
paTestData data; |
| 116 |
int i; |
| 117 |
int timeout; |
| 118 |
|
| 119 |
printf("Play different tone sine waves that alternate between left and right channel.\n"); |
| 120 |
printf("The low tone should be on the left channel.\n"); |
| 121 |
|
| 122 |
|
| 123 |
for( i=0; i<TABLE_SIZE; i++ ) |
| 124 |
{ |
| 125 |
data.sine[i] = (float) sin( ((double)i/(double)TABLE_SIZE) * M_PI * 2. ); |
| 126 |
} |
| 127 |
data.left_phase = data.right_phase = data.toggle = 0; |
| 128 |
data.countDown = SAMPLE_RATE; |
| 129 |
|
| 130 |
err = Pa_Initialize(); |
| 131 |
if( err != paNoError ) goto error; |
| 132 |
|
| 133 |
outputParameters.device = Pa_GetDefaultOutputDevice(); |
| 134 |
outputParameters.channelCount = 2; |
| 135 |
outputParameters.sampleFormat = paFloat32; |
| 136 |
outputParameters.suggestedLatency = Pa_GetDeviceInfo( outputParameters.device )->defaultLowOutputLatency; |
| 137 |
outputParameters.hostApiSpecificStreamInfo = NULL; |
| 138 |
|
| 139 |
err = Pa_OpenStream( &stream, |
| 140 |
NULL, |
| 141 |
&outputParameters, |
| 142 |
SAMPLE_RATE, |
| 143 |
FRAMES_PER_BUFFER, |
| 144 |
paClipOff, |
| 145 |
patestCallback, |
| 146 |
&data ); |
| 147 |
if( err != paNoError ) goto error; |
| 148 |
|
| 149 |
err = Pa_StartStream( stream ); |
| 150 |
if( err != paNoError ) goto error; |
| 151 |
|
| 152 |
printf("Play for several seconds.\n"); |
| 153 |
timeout = NUM_SECONDS * 4; |
| 154 |
while( timeout > 0 ) |
| 155 |
{ |
| 156 |
Pa_Sleep( 300 ); |
| 157 |
timeout -= 1; |
| 158 |
} |
| 159 |
|
| 160 |
err = Pa_StopStream( stream ); |
| 161 |
if( err != paNoError ) goto error; |
| 162 |
err = Pa_CloseStream( stream ); |
| 163 |
if( err != paNoError ) goto error; |
| 164 |
Pa_Terminate(); |
| 165 |
printf("Test finished.\n"); |
| 166 |
return err; |
| 167 |
error: |
| 168 |
Pa_Terminate(); |
| •169 |
fprintf( stderr, "An error occurred while using the portaudio stream\n" ); |
| 170 |
fprintf( stderr, "Error number: %d\n", err ); |
| 171 |
fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) ); |
| 172 |
return err; |
| 173 |
} |