| 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 MAX_SINES (500) |
| 42 |
#define MAX_USAGE (0.8) |
| 43 |
#define SAMPLE_RATE (44100) |
| 44 |
#define FREQ_TO_PHASE_INC(freq) (freq/(float)SAMPLE_RATE) |
| 45 |
|
| 46 |
#define MIN_PHASE_INC FREQ_TO_PHASE_INC(200.0f) |
| 47 |
#define MAX_PHASE_INC (MIN_PHASE_INC * (1 << 5)) |
| 48 |
|
| 49 |
#define FRAMES_PER_BUFFER (512) |
| 50 |
#ifndef M_PI |
| 51 |
#define M_PI (3.14159265) |
| 52 |
#endif |
| 53 |
#define TWOPI (M_PI * 2.0) |
| 54 |
|
| 55 |
#define TABLE_SIZE (512) |
| 56 |
|
| 57 |
typedef struct paTestData |
| 58 |
{ |
| 59 |
int numSines; |
| 60 |
float sine[TABLE_SIZE + 1]; |
| 61 |
float phases[MAX_SINES]; |
| 62 |
} |
| 63 |
paTestData; |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
float LookupSine( paTestData *data, float phase ); |
| 69 |
float LookupSine( paTestData *data, float phase ) |
| 70 |
{ |
| 71 |
float fIndex = phase*TABLE_SIZE; |
| 72 |
int index = (int) fIndex; |
| 73 |
float fract = fIndex - index; |
| 74 |
float lo = data->sine[index]; |
| 75 |
float hi = data->sine[index+1]; |
| 76 |
float val = lo + fract*(hi-lo); |
| 77 |
return val; |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
static int patestCallback(const void* inputBuffer, |
| 85 |
void* outputBuffer, |
| 86 |
unsigned long framesPerBuffer, |
| 87 |
const PaStreamCallbackTimeInfo* timeInfo, |
| 88 |
PaStreamCallbackFlags statusFlags, |
| 89 |
void* userData ) |
| 90 |
{ |
| 91 |
paTestData *data = (paTestData*)userData; |
| 92 |
float *out = (float*)outputBuffer; |
| 93 |
float outSample; |
| 94 |
float scaler; |
| 95 |
int numForScale; |
| 96 |
unsigned long i; |
| 97 |
int j; |
| 98 |
int finished = 0; |
| 99 |
(void) inputBuffer; |
| 100 |
|
| 101 |
|
| 102 |
numForScale = data->numSines; |
| 103 |
if( numForScale < 8 ) numForScale = 8; |
| 104 |
scaler = 1.0f / numForScale; |
| 105 |
|
| 106 |
for( i=0; i<framesPerBuffer; i++ ) |
| 107 |
{ |
| 108 |
float output = 0.0; |
| 109 |
float phaseInc = MIN_PHASE_INC; |
| 110 |
float phase; |
| 111 |
for( j=0; j<data->numSines; j++ ) |
| 112 |
{ |
| 113 |
|
| 114 |
phase = data->phases[j]; |
| 115 |
phase += phaseInc; |
| 116 |
if( phase >= 1.0 ) phase -= 1.0; |
| 117 |
|
| 118 |
output += LookupSine(data, phase); |
| 119 |
data->phases[j] = phase; |
| 120 |
|
| 121 |
phaseInc *= 1.02f; |
| 122 |
if( phaseInc > MAX_PHASE_INC ) phaseInc = MIN_PHASE_INC; |
| 123 |
} |
| 124 |
|
| 125 |
outSample = (float) (output * scaler); |
| 126 |
*out++ = outSample; |
| 127 |
*out++ = outSample; |
| 128 |
} |
| 129 |
return finished; |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
int main(void); |
| 134 |
int main(void) |
| 135 |
{ |
| 136 |
int i; |
| 137 |
PaStream* stream; |
| 138 |
PaStreamParameters outputParameters; |
| 139 |
PaError err; |
| 140 |
paTestData data = {0}; |
| 141 |
double load; |
| 142 |
|
| 143 |
printf("PortAudio Test: output sine wave. SR = %d, BufSize = %d\n", SAMPLE_RATE, FRAMES_PER_BUFFER); |
| 144 |
|
| 145 |
|
| 146 |
for( i=0; i<TABLE_SIZE; i++ ) |
| 147 |
{ |
| 148 |
data.sine[i] = (float) sin( ((double)i/(double)TABLE_SIZE) * M_PI * 2. ); |
| 149 |
} |
| 150 |
data.sine[TABLE_SIZE] = data.sine[0]; |
| 151 |
|
| 152 |
err = Pa_Initialize(); |
| 153 |
if( err != paNoError ) |
| 154 |
goto error; |
| 155 |
outputParameters.device = Pa_GetDefaultOutputDevice(); |
| 156 |
outputParameters.channelCount = 2; |
| 157 |
outputParameters.sampleFormat = paFloat32; |
| 158 |
outputParameters.hostApiSpecificStreamInfo = NULL; |
| 159 |
outputParameters.suggestedLatency = Pa_GetDeviceInfo(outputParameters.device) |
| 160 |
->defaultHighOutputLatency; |
| 161 |
err = Pa_OpenStream(&stream, |
| 162 |
NULL, |
| 163 |
&outputParameters, |
| 164 |
SAMPLE_RATE, |
| 165 |
FRAMES_PER_BUFFER, |
| 166 |
paClipOff, |
| 167 |
patestCallback, |
| 168 |
&data); |
| 169 |
if( err != paNoError ) |
| 170 |
goto error; |
| 171 |
|
| 172 |
err = Pa_StartStream( stream ); |
| 173 |
if( err != paNoError ) |
| 174 |
goto error; |
| 175 |
|
| 176 |
|
| 177 |
do { |
| 178 |
data.numSines++; |
| 179 |
Pa_Sleep(200); |
| 180 |
load = Pa_GetStreamCpuLoad(stream); |
| 181 |
printf("numSines = %d, CPU load = %f\n", data.numSines, load ); |
| 182 |
fflush(stdout); |
| 183 |
} while((load < MAX_USAGE) && (data.numSines < MAX_SINES)); |
| 184 |
|
| 185 |
Pa_Sleep(2000); |
| 186 |
|
| 187 |
err = Pa_StopStream( stream ); |
| 188 |
if( err != paNoError ) |
| 189 |
goto error; |
| 190 |
|
| 191 |
err = Pa_CloseStream( stream ); |
| 192 |
if( err != paNoError ) |
| 193 |
goto error; |
| 194 |
|
| 195 |
Pa_Terminate(); |
| 196 |
printf("Test finished.\n"); |
| 197 |
return err; |
| 198 |
error: |
| 199 |
Pa_Terminate(); |
| •200 |
fprintf( stderr, "An error occurred while using the portaudio stream\n" ); |
| 201 |
fprintf( stderr, "Error number: %d\n", err ); |
| 202 |
fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) ); |
| 203 |
return err; |
| 204 |
} |