| 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 |
#include <stdio.h> |
| 37 |
#include <stdlib.h> |
| 38 |
#include <math.h> |
| 39 |
#include "portaudio.h" |
| 40 |
#define NUM_SECONDS (1) |
| 41 |
#define SAMPLE_RATE (44100) |
| 42 |
#ifndef M_PI |
| 43 |
#define M_PI (3.14159265) |
| 44 |
#endif |
| 45 |
#define TABLE_SIZE (200) |
| 46 |
typedef struct |
| 47 |
{ |
| 48 |
short sine[TABLE_SIZE]; |
| 49 |
int left_phase; |
| 50 |
int right_phase; |
| 51 |
unsigned int sampsToGo; |
| 52 |
} |
| 53 |
paTestData; |
| 54 |
PaError TestOnce( void ); |
| 55 |
static int patest1Callback( const void *inputBuffer, void *outputBuffer, |
| 56 |
unsigned long framesPerBuffer, |
| 57 |
const PaStreamCallbackTimeInfo* timeInfo, |
| 58 |
PaStreamCallbackFlags statusFlags, |
| 59 |
void *userData ); |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
static int patest1Callback( const void *inputBuffer, void *outputBuffer, |
| 66 |
unsigned long framesPerBuffer, |
| 67 |
const PaStreamCallbackTimeInfo* timeInfo, |
| 68 |
PaStreamCallbackFlags statusFlags, |
| 69 |
void *userData ) |
| 70 |
{ |
| 71 |
paTestData *data = (paTestData*)userData; |
| 72 |
short *out = (short*)outputBuffer; |
| 73 |
unsigned int i; |
| 74 |
int finished = 0; |
| 75 |
(void) inputBuffer; |
| 76 |
|
| 77 |
if( data->sampsToGo < framesPerBuffer ) |
| 78 |
{ |
| 79 |
|
| 80 |
|
| 81 |
for( i=0; i<data->sampsToGo; i++ ) |
| 82 |
{ |
| 83 |
*out++ = data->sine[data->left_phase]; |
| 84 |
*out++ = data->sine[data->right_phase]; |
| 85 |
data->left_phase += 1; |
| 86 |
if( data->left_phase >= TABLE_SIZE ) data->left_phase -= TABLE_SIZE; |
| 87 |
data->right_phase += 3; |
| 88 |
if( data->right_phase >= TABLE_SIZE ) data->right_phase -= TABLE_SIZE; |
| 89 |
} |
| 90 |
|
| 91 |
for( ; i<framesPerBuffer; i++ ) |
| 92 |
{ |
| 93 |
*out++ = 0; |
| 94 |
*out++ = 0; |
| 95 |
} |
| 96 |
|
| 97 |
finished = 1; |
| 98 |
} |
| 99 |
else |
| 100 |
{ |
| 101 |
for( i=0; i<framesPerBuffer; i++ ) |
| 102 |
{ |
| 103 |
*out++ = data->sine[data->left_phase]; |
| 104 |
*out++ = data->sine[data->right_phase]; |
| 105 |
data->left_phase += 1; |
| 106 |
if( data->left_phase >= TABLE_SIZE ) data->left_phase -= TABLE_SIZE; |
| 107 |
data->right_phase += 3; |
| 108 |
if( data->right_phase >= TABLE_SIZE ) data->right_phase -= TABLE_SIZE; |
| 109 |
} |
| 110 |
data->sampsToGo -= framesPerBuffer; |
| 111 |
} |
| 112 |
return finished; |
| 113 |
} |
| 114 |
|
| 115 |
#ifdef MACINTOSH |
| 116 |
int main(void); |
| 117 |
int main(void) |
| 118 |
{ |
| 119 |
int i; |
| 120 |
PaError err; |
| 121 |
int numLoops = 10; |
| 122 |
printf("Loop %d times.\n", numLoops ); |
| 123 |
for( i=0; i<numLoops; i++ ) |
| 124 |
{ |
| 125 |
printf("Loop %d out of %d.\n", i+1, numLoops ); |
| 126 |
err = TestOnce(); |
| 127 |
if( err < 0 ) return 0; |
| 128 |
} |
| 129 |
} |
| 130 |
#else |
| 131 |
int main(int argc, char **argv); |
| 132 |
int main(int argc, char **argv) |
| 133 |
{ |
| 134 |
PaError err; |
| 135 |
int i, numLoops = 10; |
| 136 |
if( argc > 1 ) |
| 137 |
{ |
| 138 |
numLoops = atoi(argv[1]); |
| 139 |
} |
| 140 |
for( i=0; i<numLoops; i++ ) |
| 141 |
{ |
| 142 |
printf("Loop %d out of %d.\n", i+1, numLoops ); |
| 143 |
err = TestOnce(); |
| 144 |
if( err < 0 ) return 1; |
| 145 |
} |
| 146 |
printf("Test complete.\n"); |
| 147 |
return 0; |
| 148 |
} |
| 149 |
#endif |
| 150 |
PaError TestOnce( void ) |
| 151 |
{ |
| 152 |
PaStreamParameters outputParameters; |
| 153 |
PaStream *stream; |
| 154 |
PaError err; |
| 155 |
paTestData data; |
| 156 |
int i; |
| 157 |
int totalSamps; |
| 158 |
|
| 159 |
for( i=0; i<TABLE_SIZE; i++ ) |
| 160 |
{ |
| 161 |
data.sine[i] = (short) (32767.0 * sin( ((double)i/(double)TABLE_SIZE) * M_PI * 2. )); |
| 162 |
} |
| 163 |
data.left_phase = data.right_phase = 0; |
| 164 |
data.sampsToGo = totalSamps = NUM_SECONDS * SAMPLE_RATE; |
| 165 |
err = Pa_Initialize(); |
| 166 |
if( err != paNoError ) goto error; |
| 167 |
|
| 168 |
outputParameters.device = Pa_GetDefaultOutputDevice(); |
| 169 |
outputParameters.channelCount = 2; |
| 170 |
outputParameters.sampleFormat = paInt16; |
| 171 |
outputParameters.suggestedLatency = Pa_GetDeviceInfo( outputParameters.device )->defaultLowOutputLatency; |
| 172 |
outputParameters.hostApiSpecificStreamInfo = NULL; |
| 173 |
err = Pa_OpenStream( |
| 174 |
&stream, |
| 175 |
NULL, |
| 176 |
&outputParameters, |
| 177 |
SAMPLE_RATE, |
| 178 |
1024, |
| 179 |
paClipOff, |
| 180 |
patest1Callback, |
| 181 |
&data ); |
| 182 |
if( err != paNoError ) goto error; |
| 183 |
|
| 184 |
err = Pa_StartStream( stream ); |
| 185 |
if( err != paNoError ) goto error; |
| 186 |
printf("Waiting for sound to finish.\n"); |
| 187 |
Pa_Sleep(1000); |
| 188 |
err = Pa_CloseStream( stream ); |
| 189 |
if( err != paNoError ) goto error; |
| 190 |
Pa_Terminate(); |
| 191 |
return paNoError; |
| 192 |
error: |
| 193 |
Pa_Terminate(); |
| •194 |
fprintf( stderr, "An error occurred while using the portaudio stream\n" ); |
| 195 |
fprintf( stderr, "Error number: %d\n", err ); |
| 196 |
fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) ); |
| 197 |
return err; |
| 198 |
} |