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