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