| 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 |
|
| 40 |
#include "portaudio.h" |
| 41 |
|
| 42 |
#define SAMPLE_RATE (44100) |
| 43 |
#define FRAMES_PER_BUFFER (256) |
| 44 |
#define FREQ_INCR (300.0 / SAMPLE_RATE) |
| 45 |
#define MAX_CHANNELS (64) |
| 46 |
|
| 47 |
#ifndef M_PI |
| 48 |
#define M_PI (3.14159265) |
| 49 |
#endif |
| 50 |
|
| 51 |
typedef struct |
| 52 |
{ |
| 53 |
short interleaved; |
| 54 |
int numChannels; |
| 55 |
double phases[MAX_CHANNELS]; |
| 56 |
} |
| 57 |
paTestData; |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
static int patestCallback(const void* inputBuffer, |
| 64 |
void* outputBuffer, |
| 65 |
unsigned long framesPerBuffer, |
| 66 |
const PaStreamCallbackTimeInfo* timeInfo, |
| 67 |
PaStreamCallbackFlags statusFlags, |
| 68 |
void* userData) |
| 69 |
{ |
| 70 |
int frameIndex, channelIndex; |
| 71 |
float** outputs = (float**)outputBuffer; |
| 72 |
paTestData* data = (paTestData*)userData; |
| 73 |
|
| 74 |
(void) inputBuffer; |
| 75 |
if (data->interleaved) |
| 76 |
{ |
| 77 |
float *out = (float*)outputBuffer; |
| 78 |
for( frameIndex=0; frameIndex<(int)framesPerBuffer; frameIndex++ ) |
| 79 |
{ |
| 80 |
for( channelIndex=0; channelIndex<data->numChannels; channelIndex++ ) |
| 81 |
{ |
| 82 |
|
| 83 |
*out++ = (float) sin(data->phases[channelIndex]); |
| 84 |
|
| 85 |
|
| 86 |
data->phases[channelIndex] += FREQ_INCR * (4 + channelIndex); |
| 87 |
if( data->phases[channelIndex] >= (2.0 * M_PI) ) data->phases[channelIndex] -= (2.0 * M_PI); |
| 88 |
} |
| 89 |
} |
| 90 |
} |
| 91 |
else |
| 92 |
{ |
| 93 |
for( frameIndex=0; frameIndex<(int)framesPerBuffer; frameIndex++ ) |
| 94 |
{ |
| 95 |
for( channelIndex=0; channelIndex<data->numChannels; channelIndex++ ) |
| 96 |
{ |
| 97 |
|
| 98 |
outputs[channelIndex][frameIndex] = (float) sin(data->phases[channelIndex]); |
| 99 |
|
| 100 |
|
| 101 |
data->phases[channelIndex] += FREQ_INCR * (4 + channelIndex); |
| 102 |
if( data->phases[channelIndex] >= (2.0 * M_PI) ) data->phases[channelIndex] -= (2.0 * M_PI); |
| 103 |
} |
| 104 |
} |
| 105 |
} |
| 106 |
return 0; |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
int test(short interleaved) |
| 111 |
{ |
| 112 |
PaStream* stream; |
| 113 |
PaStreamParameters outputParameters; |
| 114 |
PaError err; |
| 115 |
const PaDeviceInfo* pdi; |
| 116 |
paTestData data; |
| 117 |
short n; |
| 118 |
|
| 119 |
outputParameters.device = Pa_GetDefaultOutputDevice(); |
| 120 |
pdi = Pa_GetDeviceInfo(outputParameters.device); |
| 121 |
outputParameters.channelCount = pdi->maxOutputChannels; |
| 122 |
if (outputParameters.channelCount > MAX_CHANNELS) |
| 123 |
outputParameters.channelCount = MAX_CHANNELS; |
| 124 |
outputParameters.sampleFormat = paFloat32; |
| 125 |
outputParameters.suggestedLatency = pdi->defaultLowOutputLatency; |
| 126 |
outputParameters.hostApiSpecificStreamInfo = NULL; |
| 127 |
|
| 128 |
data.interleaved = interleaved; |
| 129 |
data.numChannels = outputParameters.channelCount; |
| 130 |
for (n = 0; n < data.numChannels; n++) |
| 131 |
data.phases[n] = 0.0; |
| 132 |
printf("%d ", data.numChannels); |
| 133 |
if (interleaved) |
| 134 |
printf("interleaved "); |
| 135 |
else |
| 136 |
{ |
| 137 |
printf(" non-interleaved "); |
| 138 |
outputParameters.sampleFormat |= paNonInterleaved; |
| 139 |
} |
| 140 |
printf("channels.\n"); |
| 141 |
|
| 142 |
err = Pa_OpenStream(&stream, |
| 143 |
NULL, |
| 144 |
&outputParameters, |
| 145 |
SAMPLE_RATE, |
| 146 |
FRAMES_PER_BUFFER, |
| 147 |
paClipOff, |
| 148 |
patestCallback, |
| 149 |
&data); |
| 150 |
if (err == paNoError) |
| 151 |
{ |
| 152 |
err = Pa_StartStream(stream); |
| 153 |
if (err == paNoError) |
| 154 |
{ |
| 155 |
printf("Hit ENTER to stop this test.\n"); |
| 156 |
getchar(); |
| 157 |
err = Pa_StopStream(stream); |
| 158 |
} |
| 159 |
Pa_CloseStream( stream ); |
| 160 |
} |
| 161 |
return err; |
| 162 |
} |
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
int main(void) |
| 167 |
{ |
| 168 |
PaError err; |
| 169 |
|
| 170 |
printf("PortAudio Test: output sine wave on each channel.\n" ); |
| 171 |
|
| 172 |
err = Pa_Initialize(); |
| 173 |
if (err != paNoError) |
| 174 |
goto done; |
| 175 |
|
| 176 |
err = test(1); |
| 177 |
if (err != paNoError) |
| 178 |
goto done; |
| 179 |
|
| 180 |
err = test(0); |
| 181 |
if (err != paNoError) |
| 182 |
goto done; |
| 183 |
|
| 184 |
printf("Test finished.\n"); |
| 185 |
done: |
| 186 |
if (err) |
| 187 |
{ |
| •188 |
fprintf(stderr, "An error occurred while using the portaudio stream\n"); |
| 189 |
fprintf(stderr, "Error number: %d\n", err ); |
| 190 |
fprintf(stderr, "Error message: %s\n", Pa_GetErrorText(err)); |
| 191 |
} |
| 192 |
Pa_Terminate(); |
| 193 |
return 0; |
| 194 |
} |