| 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 <stdlib.h> |
| 40 |
#include <math.h> |
| 41 |
#include <string.h> |
| 42 |
#include "portaudio.h" |
| 43 |
|
| 44 |
#ifndef M_PI |
| 45 |
#define M_PI (3.14159265) |
| 46 |
#endif |
| 47 |
#define TWOPI (M_PI * 2.0) |
| 48 |
|
| 49 |
#define DEFAULT_BUFFER_SIZE (32) |
| 50 |
|
| 51 |
typedef struct |
| 52 |
{ |
| 53 |
double left_phase; |
| 54 |
double right_phase; |
| 55 |
} |
| 56 |
paTestData; |
| 57 |
|
| 58 |
|
| 59 |
static int paminlatCallback( const void *inputBuffer, void *outputBuffer, |
| 60 |
unsigned long framesPerBuffer, |
| 61 |
const PaStreamCallbackTimeInfo* timeInfo, |
| 62 |
PaStreamCallbackFlags statusFlags, |
| 63 |
void *userData ) |
| 64 |
{ |
| 65 |
paTestData *data = (paTestData*)userData; |
| 66 |
float *out = (float*)outputBuffer; |
| 67 |
unsigned int i; |
| 68 |
double left_phaseInc = 0.02; |
| 69 |
double right_phaseInc = 0.06; |
| 70 |
|
| 71 |
double left_phase = data->left_phase; |
| 72 |
double right_phase = data->right_phase; |
| 73 |
|
| 74 |
for( i=0; i<framesPerBuffer; i++ ) |
| 75 |
{ |
| 76 |
left_phase += left_phaseInc; |
| 77 |
if( left_phase > TWOPI ) left_phase -= TWOPI; |
| 78 |
*out++ = (float) sin( left_phase ); |
| 79 |
|
| 80 |
right_phase += right_phaseInc; |
| 81 |
if( right_phase > TWOPI ) right_phase -= TWOPI; |
| 82 |
*out++ = (float) sin( right_phase ); |
| 83 |
} |
| 84 |
|
| 85 |
data->left_phase = left_phase; |
| 86 |
data->right_phase = right_phase; |
| 87 |
return 0; |
| 88 |
} |
| 89 |
|
| 90 |
int main( int argc, char **argv ); |
| 91 |
int main( int argc, char **argv ) |
| 92 |
{ |
| 93 |
PaStreamParameters outputParameters; |
| 94 |
PaStream *stream; |
| 95 |
PaError err; |
| 96 |
paTestData data; |
| 97 |
int go; |
| 98 |
int outLatency = 0; |
| 99 |
int minLatency = DEFAULT_BUFFER_SIZE * 2; |
| 100 |
int framesPerBuffer; |
| 101 |
double sampleRate = 44100.0; |
| 102 |
char str[256]; |
| 103 |
|
| 104 |
printf("pa_minlat - Determine minimum latency for your computer.\n"); |
| 105 |
printf(" usage: pa_minlat {userBufferSize}\n"); |
| 106 |
printf(" for example: pa_minlat 64\n"); |
| 107 |
printf("Adjust your stereo until you hear a smooth tone in each speaker.\n"); |
| 108 |
printf("Then try to find the smallest number of frames that still sounds smooth.\n"); |
| 109 |
printf("Note that the sound will stop momentarily when you change the number of buffers.\n"); |
| 110 |
|
| 111 |
|
| 112 |
framesPerBuffer = ( argc > 1 ) ? atol( argv[1] ) : DEFAULT_BUFFER_SIZE; |
| 113 |
printf("Frames per buffer = %d\n", framesPerBuffer ); |
| 114 |
|
| 115 |
data.left_phase = data.right_phase = 0.0; |
| 116 |
|
| 117 |
err = Pa_Initialize(); |
| 118 |
if( err != paNoError ) goto error; |
| 119 |
|
| 120 |
outLatency = sampleRate * 200.0 / 1000.0; |
| 121 |
|
| 122 |
|
| 123 |
go = 1; |
| 124 |
while( go ) |
| 125 |
{ |
| 126 |
outputParameters.device = Pa_GetDefaultOutputDevice(); |
| 127 |
outputParameters.channelCount = 2; |
| 128 |
outputParameters.sampleFormat = paFloat32; |
| 129 |
outputParameters.suggestedLatency = (double)outLatency / sampleRate; |
| 130 |
outputParameters.hostApiSpecificStreamInfo = NULL; |
| 131 |
|
| 132 |
printf("Latency = %d frames = %6.1f msec.\n", outLatency, outputParameters.suggestedLatency * 1000.0 ); |
| 133 |
|
| 134 |
err = Pa_OpenStream( |
| 135 |
&stream, |
| 136 |
NULL, |
| 137 |
&outputParameters, |
| 138 |
sampleRate, |
| 139 |
framesPerBuffer, |
| 140 |
paClipOff, |
| 141 |
paminlatCallback, |
| 142 |
&data ); |
| 143 |
if( err != paNoError ) goto error; |
| 144 |
if( stream == NULL ) goto error; |
| 145 |
|
| 146 |
|
| 147 |
err = Pa_StartStream( stream ); |
| 148 |
if( err != paNoError ) goto error; |
| 149 |
|
| 150 |
|
| 151 |
printf("\nMove windows around to see if the sound glitches.\n"); |
| 152 |
printf("Latency now %d, enter new number of frames, or 'q' to quit: ", outLatency ); |
| 153 |
fgets( str, 256, stdin ); |
| 154 |
{ |
| 155 |
|
| 156 |
size_t l = strlen( str ) - 1; |
| 157 |
if( str[ l ] == '\n') |
| 158 |
str[ l ] = '\0'; |
| 159 |
} |
| 160 |
if( str[0] == 'q' ) go = 0; |
| 161 |
else |
| 162 |
{ |
| 163 |
outLatency = atol( str ); |
| 164 |
if( outLatency < minLatency ) |
| 165 |
{ |
| 166 |
printf( "Latency below minimum of %d! Set to minimum!!!\n", minLatency ); |
| 167 |
outLatency = minLatency; |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
err = Pa_StopStream( stream ); |
| 172 |
if( err != paNoError ) goto error; |
| 173 |
err = Pa_CloseStream( stream ); |
| 174 |
if( err != paNoError ) goto error; |
| 175 |
} |
| 176 |
printf("A good setting for latency would be somewhat higher than\n"); |
| 177 |
printf("the minimum latency that worked.\n"); |
| 178 |
printf("PortAudio: Test finished.\n"); |
| 179 |
Pa_Terminate(); |
| 180 |
return 0; |
| 181 |
error: |
| 182 |
Pa_Terminate(); |
| •183 |
fprintf( stderr, "An error occurred while using the portaudio stream\n" ); |
| 184 |
fprintf( stderr, "Error number: %d\n", err ); |
| 185 |
fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) ); |
| 186 |
return 1; |
| 187 |
} |