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