| 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 OUTPUT_DEVICE (Pa_GetDefaultOutputDeviceID()) |
| 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 |
int numChannels; |
| 54 |
double phases[MAX_CHANNELS]; |
| 55 |
float amplitude; |
| 56 |
} |
| 57 |
paTestData; |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
static int patestCallback( void *inputBuffer, void *outputBuffer, |
| 64 |
unsigned long framesPerBuffer, |
| 65 |
PaTimestamp outTime, void *userData ) |
| 66 |
{ |
| 67 |
paTestData *data = (paTestData*)userData; |
| 68 |
float *out = (float*)outputBuffer; |
| 69 |
int frameIndex, channelIndex; |
| 70 |
(void) outTime; |
| 71 |
(void) inputBuffer; |
| 72 |
|
| 73 |
for( frameIndex=0; frameIndex<(int)framesPerBuffer; frameIndex++ ) |
| 74 |
{ |
| 75 |
for( channelIndex=0; channelIndex<data->numChannels; channelIndex++ ) |
| 76 |
{ |
| 77 |
|
| 78 |
*out++ = (float) ( data->amplitude * sin(data->phases[channelIndex]) ); |
| 79 |
|
| 80 |
|
| 81 |
data->phases[channelIndex] += FREQ_INCR * (4 + channelIndex); |
| 82 |
if( data->phases[channelIndex] >= (2.0 * M_PI) ) data->phases[channelIndex] -= (2.0 * M_PI); |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
return 0; |
| 87 |
} |
| 88 |
|
| 89 |
int main(void); |
| 90 |
int main(void) |
| 91 |
{ |
| 92 |
char pad[256]; |
| 93 |
PortAudioStream *stream; |
| 94 |
PaError err; |
| 95 |
const PaDeviceInfo *pdi; |
| 96 |
paTestData data = {0}; |
| 97 |
printf("PortAudio Test: output sine wave on each channel.\n" ); |
| 98 |
|
| 99 |
err = Pa_Initialize(); |
| 100 |
if( err != paNoError ) goto error; |
| 101 |
|
| 102 |
pdi = Pa_GetDeviceInfo( OUTPUT_DEVICE ); |
| 103 |
data.numChannels = pdi->maxOutputChannels; |
| 104 |
if( data.numChannels > MAX_CHANNELS ) data.numChannels = MAX_CHANNELS; |
| 105 |
printf("Number of Channels = %d\n", data.numChannels ); |
| 106 |
data.amplitude = 1.0; |
| 107 |
|
| 108 |
err = Pa_OpenStream( |
| 109 |
&stream, |
| 110 |
paNoDevice, |
| 111 |
0, |
| 112 |
paFloat32, |
| 113 |
NULL, |
| 114 |
OUTPUT_DEVICE, |
| 115 |
data.numChannels, |
| 116 |
paFloat32, |
| 117 |
NULL, |
| 118 |
SAMPLE_RATE, |
| 119 |
FRAMES_PER_BUFFER, |
| 120 |
0, |
| 121 |
paClipOff, |
| 122 |
patestCallback, |
| 123 |
&data ); |
| 124 |
if( err != paNoError ) goto error; |
| 125 |
|
| 126 |
err = Pa_StartStream( stream ); |
| 127 |
if( err != paNoError ) goto error; |
| 128 |
|
| 129 |
do |
| 130 |
{ |
| 131 |
printf("Current amplitude = %f\n", data.amplitude ); |
| 132 |
printf("Enter new amplitude or 'q' to quit.\n"); |
| 133 |
fflush(stdout); |
| 134 |
gets( pad ); |
| 135 |
if( pad[0] != 'q' ) |
| 136 |
{ |
| 137 |
|
| 138 |
float amp; |
| 139 |
sscanf( pad, "%f", & ); |
| 140 |
data.amplitude = amp; |
| 141 |
} |
| 142 |
} while( pad[0] != 'q' ); |
| 143 |
|
| 144 |
err = Pa_StopStream( stream ); |
| 145 |
if( err != paNoError ) goto error; |
| 146 |
|
| 147 |
Pa_CloseStream( stream ); |
| 148 |
Pa_Terminate(); |
| 149 |
printf("Test finished.\n"); |
| 150 |
return err; |
| 151 |
error: |
| 152 |
Pa_Terminate(); |
| •153 |
fprintf( stderr, "An error occurred while using the portaudio stream\n" ); |
| 154 |
fprintf( stderr, "Error number: %d\n", err ); |
| 155 |
fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) ); |
| 156 |
return err; |
| 157 |
} |