| 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 NUM_SECONDS (4) |
| 43 |
#define SAMPLE_RATE (44100) |
| 44 |
#ifndef M_PI |
| 45 |
#define M_PI (3.14159265) |
| 46 |
#endif |
| 47 |
#define TABLE_SIZE (200) |
| 48 |
|
| 49 |
typedef struct paTestData |
| 50 |
{ |
| 51 |
float sine[TABLE_SIZE]; |
| 52 |
float amplitude; |
| 53 |
int left_phase; |
| 54 |
int right_phase; |
| 55 |
} |
| 56 |
paTestData; |
| 57 |
|
| 58 |
PaError PlaySine( paTestData *data, unsigned long flags, float amplitude ); |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
static int sineCallback( const void *inputBuffer, void *outputBuffer, |
| 65 |
unsigned long framesPerBuffer, |
| 66 |
const PaStreamCallbackTimeInfo* timeInfo, |
| 67 |
PaStreamCallbackFlags statusFlags, |
| 68 |
void *userData ) |
| 69 |
{ |
| 70 |
paTestData *data = (paTestData*)userData; |
| 71 |
float *out = (float*)outputBuffer; |
| 72 |
float amplitude = data->amplitude; |
| 73 |
unsigned int i; |
| 74 |
(void) inputBuffer; |
| 75 |
(void) timeInfo; |
| 76 |
(void) statusFlags; |
| 77 |
|
| 78 |
for( i=0; i<framesPerBuffer; i++ ) |
| 79 |
{ |
| 80 |
*out++ = amplitude * data->sine[data->left_phase]; |
| 81 |
*out++ = amplitude * data->sine[data->right_phase]; |
| 82 |
data->left_phase += 1; |
| 83 |
if( data->left_phase >= TABLE_SIZE ) data->left_phase -= TABLE_SIZE; |
| 84 |
data->right_phase += 3; |
| 85 |
if( data->right_phase >= TABLE_SIZE ) data->right_phase -= TABLE_SIZE; |
| 86 |
} |
| 87 |
return 0; |
| 88 |
} |
| 89 |
|
| 90 |
int main(void); |
| 91 |
int main(void) |
| 92 |
{ |
| 93 |
PaError err; |
| 94 |
paTestData data; |
| 95 |
int i; |
| 96 |
|
| 97 |
printf("PortAudio Test: output sine wave with and without clipping.\n"); |
| 98 |
|
| 99 |
for( i=0; i<TABLE_SIZE; i++ ) |
| 100 |
{ |
| 101 |
data.sine[i] = (float) sin( ((double)i/(double)TABLE_SIZE) * M_PI * 2. ); |
| 102 |
} |
| 103 |
|
| 104 |
printf("\nHalf amplitude. Should sound like sine wave.\n"); fflush(stdout); |
| 105 |
err = PlaySine( &data, paClipOff | paDitherOff, 0.5f ); |
| 106 |
if( err < 0 ) goto error; |
| 107 |
|
| 108 |
printf("\nFull amplitude. Should sound like sine wave.\n"); fflush(stdout); |
| 109 |
err = PlaySine( &data, paClipOff | paDitherOff, 0.999f ); |
| 110 |
if( err < 0 ) goto error; |
| 111 |
|
| 112 |
printf("\nOver range with clipping and dithering turned OFF. Should sound very nasty.\n"); |
| 113 |
fflush(stdout); |
| 114 |
err = PlaySine( &data, paClipOff | paDitherOff, 1.1f ); |
| 115 |
if( err < 0 ) goto error; |
| 116 |
|
| 117 |
printf("\nOver range with clipping and dithering turned ON. Should sound smoother than previous.\n"); |
| 118 |
fflush(stdout); |
| 119 |
err = PlaySine( &data, paNoFlag, 1.1f ); |
| 120 |
if( err < 0 ) goto error; |
| 121 |
|
| 122 |
printf("\nOver range with paClipOff but dithering ON.\n" |
| 123 |
"That forces clipping ON so it should sound the same as previous.\n"); |
| 124 |
fflush(stdout); |
| 125 |
err = PlaySine( &data, paClipOff, 1.1f ); |
| 126 |
if( err < 0 ) goto error; |
| 127 |
|
| 128 |
return 0; |
| 129 |
error: |
| •130 |
fprintf( stderr, "An error occurred while using the portaudio stream\n" ); |
| 131 |
fprintf( stderr, "Error number: %d\n", err ); |
| 132 |
fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) ); |
| 133 |
return 1; |
| 134 |
} |
| 135 |
|
| 136 |
PaError PlaySine( paTestData *data, unsigned long flags, float amplitude ) |
| 137 |
{ |
| 138 |
PaStreamParameters outputParameters; |
| 139 |
PaStream *stream; |
| 140 |
PaError err; |
| 141 |
|
| 142 |
data->left_phase = data->right_phase = 0; |
| 143 |
data->amplitude = amplitude; |
| 144 |
|
| 145 |
err = Pa_Initialize(); |
| 146 |
if( err != paNoError ) goto error; |
| 147 |
|
| 148 |
outputParameters.device = Pa_GetDefaultOutputDevice(); |
| 149 |
outputParameters.channelCount = 2; |
| 150 |
outputParameters.sampleFormat = paFloat32; |
| 151 |
outputParameters.suggestedLatency = Pa_GetDeviceInfo( outputParameters.device )->defaultLowOutputLatency; |
| 152 |
outputParameters.hostApiSpecificStreamInfo = NULL; |
| 153 |
|
| 154 |
err = Pa_OpenStream( |
| 155 |
&stream, |
| 156 |
NULL, |
| 157 |
&outputParameters, |
| 158 |
SAMPLE_RATE, |
| 159 |
1024, |
| 160 |
flags, |
| 161 |
sineCallback, |
| 162 |
data ); |
| 163 |
if( err != paNoError ) goto error; |
| 164 |
|
| 165 |
err = Pa_StartStream( stream ); |
| 166 |
if( err != paNoError ) goto error; |
| 167 |
|
| 168 |
Pa_Sleep( NUM_SECONDS * 1000 ); |
| 169 |
printf("CPULoad = %8.6f\n", Pa_GetStreamCpuLoad( stream ) ); |
| 170 |
|
| 171 |
err = Pa_CloseStream( stream ); |
| 172 |
if( err != paNoError ) goto error; |
| 173 |
|
| 174 |
Pa_Terminate(); |
| 175 |
return paNoError; |
| 176 |
error: |
| 177 |
return err; |
| 178 |
} |