| 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 |
|
| 38 |
#include <stdio.h> |
| 39 |
#include <math.h> |
| 40 |
#include "portaudio.h" |
| 41 |
|
| 42 |
#define OUTPUT_DEVICE (Pa_GetDefaultOutputDevice()) |
| 43 |
#define SAMPLE_RATE (44100) |
| 44 |
#define FRAMES_PER_BUFFER (64) |
| 45 |
|
| 46 |
#define MIN_FREQ (100.0f) |
| 47 |
#define CalcPhaseIncrement(freq) ((freq)/SAMPLE_RATE) |
| 48 |
#ifndef M_PI |
| 49 |
#define M_PI (3.14159265) |
| 50 |
#endif |
| 51 |
#define TABLE_SIZE (400) |
| 52 |
|
| 53 |
typedef struct |
| 54 |
{ |
| 55 |
float sine[TABLE_SIZE + 1]; |
| 56 |
float phase_increment; |
| 57 |
float left_phase; |
| 58 |
float right_phase; |
| 59 |
} |
| 60 |
paTestData; |
| 61 |
|
| 62 |
float LookupSine( paTestData *data, float phase ); |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
float LookupSine( paTestData *data, float phase ) |
| 67 |
{ |
| 68 |
float fIndex = phase*TABLE_SIZE; |
| 69 |
int index = (int) fIndex; |
| 70 |
float fract = fIndex - index; |
| 71 |
float lo = data->sine[index]; |
| 72 |
float hi = data->sine[index+1]; |
| 73 |
float val = lo + fract*(hi-lo); |
| 74 |
return val; |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
static int patestCallback( const void *inputBuffer, void *outputBuffer, |
| 82 |
unsigned long framesPerBuffer, |
| 83 |
const PaStreamCallbackTimeInfo* timeInfo, |
| 84 |
PaStreamCallbackFlags statusFlags, |
| 85 |
void *userData ) |
| 86 |
{ |
| 87 |
paTestData *data = (paTestData*)userData; |
| 88 |
float *out = (float*)outputBuffer; |
| 89 |
int i; |
| 90 |
|
| 91 |
(void) inputBuffer; |
| 92 |
|
| 93 |
for( i=0; i<framesPerBuffer; i++ ) |
| 94 |
{ |
| 95 |
*out++ = LookupSine(data, data->left_phase); |
| 96 |
*out++ = LookupSine(data, data->right_phase); |
| 97 |
data->left_phase += data->phase_increment; |
| 98 |
if( data->left_phase >= 1.0f ) data->left_phase -= 1.0f; |
| 99 |
data->right_phase += (data->phase_increment * 1.5f); |
| 100 |
if( data->right_phase >= 1.0f ) data->right_phase -= 1.0f; |
| 101 |
} |
| 102 |
return 0; |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
int main(void); |
| 107 |
int main(void) |
| 108 |
{ |
| 109 |
PaStream *stream; |
| 110 |
PaStreamParameters outputParameters; |
| 111 |
PaError err; |
| 112 |
paTestData data; |
| 113 |
int i; |
| 114 |
int done = 0; |
| 115 |
|
| 116 |
printf("PortAudio Test: enter letter then hit ENTER.\n" ); |
| 117 |
|
| 118 |
for( i=0; i<TABLE_SIZE; i++ ) |
| 119 |
{ |
| 120 |
data.sine[i] = 0.90f * (float) sin( ((double)i/(double)TABLE_SIZE) * M_PI * 2. ); |
| 121 |
} |
| 122 |
data.sine[TABLE_SIZE] = data.sine[0]; |
| 123 |
data.left_phase = data.right_phase = 0.0; |
| 124 |
data.phase_increment = CalcPhaseIncrement(MIN_FREQ); |
| 125 |
|
| 126 |
err = Pa_Initialize(); |
| 127 |
if( err != paNoError ) goto error; |
| 128 |
printf("PortAudio Test: output device = %d\n", OUTPUT_DEVICE ); |
| 129 |
|
| 130 |
outputParameters.device = OUTPUT_DEVICE; |
| 131 |
outputParameters.channelCount = 2; |
| 132 |
outputParameters.sampleFormat = paFloat32; |
| 133 |
outputParameters.suggestedLatency = Pa_GetDeviceInfo( outputParameters.device )->defaultLowOutputLatency; |
| 134 |
outputParameters.hostApiSpecificStreamInfo = NULL; |
| 135 |
|
| 136 |
printf("Requested output latency = %.4f seconds.\n", outputParameters.suggestedLatency ); |
| 137 |
printf("%d frames per buffer.\n.", FRAMES_PER_BUFFER ); |
| 138 |
|
| 139 |
err = Pa_OpenStream( |
| 140 |
&stream, |
| 141 |
NULL, |
| 142 |
&outputParameters, |
| 143 |
SAMPLE_RATE, |
| 144 |
FRAMES_PER_BUFFER, |
| 145 |
paClipOff|paDitherOff, |
| 146 |
patestCallback, |
| 147 |
&data ); |
| 148 |
if( err != paNoError ) goto error; |
| 149 |
|
| 150 |
err = Pa_StartStream( stream ); |
| 151 |
if( err != paNoError ) goto error; |
| 152 |
printf("Play ASCII keyboard. Hit 'q' to stop. (Use RETURN key on Mac)\n"); |
| 153 |
fflush(stdout); |
| 154 |
while ( !done ) |
| 155 |
{ |
| 156 |
float freq; |
| 157 |
int index; |
| 158 |
char c; |
| 159 |
do |
| 160 |
{ |
| 161 |
c = getchar(); |
| 162 |
} |
| 163 |
while( c < ' '); |
| 164 |
|
| 165 |
if( c == 'q' ) done = 1; |
| 166 |
index = c % 26; |
| 167 |
freq = MIN_FREQ + (index * 40.0); |
| 168 |
data.phase_increment = CalcPhaseIncrement(freq); |
| 169 |
} |
| 170 |
printf("Call Pa_StopStream()\n"); |
| 171 |
err = Pa_StopStream( stream ); |
| 172 |
if( err != paNoError ) goto error; |
| 173 |
Pa_Terminate(); |
| 174 |
printf("Test finished.\n"); |
| 175 |
return err; |
| 176 |
error: |
| 177 |
Pa_Terminate(); |
| •178 |
fprintf( stderr, "An error occurred while using the portaudio stream\n" ); |
| 179 |
fprintf( stderr, "Error number: %d\n", err ); |
| 180 |
fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) ); |
| 181 |
return err; |
| 182 |
} |