Chris@4
|
1 /** @file pa_minlat.c
|
Chris@4
|
2 @ingroup test_src
|
Chris@4
|
3 @brief Experiment with different numbers of buffers to determine the
|
Chris@4
|
4 minimum latency for a computer.
|
Chris@4
|
5 @author Phil Burk http://www.softsynth.com
|
Chris@4
|
6 */
|
Chris@4
|
7 /*
|
Chris@4
|
8 * $Id: pa_minlat.c 1612 2011-02-28 23:56:48Z philburk $
|
Chris@4
|
9 *
|
Chris@4
|
10 * This program uses the PortAudio Portable Audio Library.
|
Chris@4
|
11 * For more information see: http://www.portaudio.com
|
Chris@4
|
12 * Copyright (c) 1999-2000 Ross Bencina and Phil Burk
|
Chris@4
|
13 *
|
Chris@4
|
14 * Permission is hereby granted, free of charge, to any person obtaining
|
Chris@4
|
15 * a copy of this software and associated documentation files
|
Chris@4
|
16 * (the "Software"), to deal in the Software without restriction,
|
Chris@4
|
17 * including without limitation the rights to use, copy, modify, merge,
|
Chris@4
|
18 * publish, distribute, sublicense, and/or sell copies of the Software,
|
Chris@4
|
19 * and to permit persons to whom the Software is furnished to do so,
|
Chris@4
|
20 * subject to the following conditions:
|
Chris@4
|
21 *
|
Chris@4
|
22 * The above copyright notice and this permission notice shall be
|
Chris@4
|
23 * included in all copies or substantial portions of the Software.
|
Chris@4
|
24 *
|
Chris@4
|
25 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
Chris@4
|
26 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
Chris@4
|
27 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
Chris@4
|
28 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
|
Chris@4
|
29 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
|
Chris@4
|
30 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
Chris@4
|
31 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
Chris@4
|
32 */
|
Chris@4
|
33
|
Chris@4
|
34 /*
|
Chris@4
|
35 * The text above constitutes the entire PortAudio license; however,
|
Chris@4
|
36 * the PortAudio community also makes the following non-binding requests:
|
Chris@4
|
37 *
|
Chris@4
|
38 * Any person wishing to distribute modifications to the Software is
|
Chris@4
|
39 * requested to send the modifications to the original developer so that
|
Chris@4
|
40 * they can be incorporated into the canonical version. It is also
|
Chris@4
|
41 * requested that these non-binding requests be included along with the
|
Chris@4
|
42 * license above.
|
Chris@4
|
43 */
|
Chris@4
|
44
|
Chris@4
|
45 #include <stdio.h>
|
Chris@4
|
46 #include <stdlib.h>
|
Chris@4
|
47 #include <math.h>
|
Chris@4
|
48 #include <string.h>
|
Chris@4
|
49 #include "portaudio.h"
|
Chris@4
|
50
|
Chris@4
|
51 #ifndef M_PI
|
Chris@4
|
52 #define M_PI (3.14159265)
|
Chris@4
|
53 #endif
|
Chris@4
|
54 #define TWOPI (M_PI * 2.0)
|
Chris@4
|
55
|
Chris@4
|
56 #define DEFAULT_BUFFER_SIZE (32)
|
Chris@4
|
57
|
Chris@4
|
58 typedef struct
|
Chris@4
|
59 {
|
Chris@4
|
60 double left_phase;
|
Chris@4
|
61 double right_phase;
|
Chris@4
|
62 }
|
Chris@4
|
63 paTestData;
|
Chris@4
|
64
|
Chris@4
|
65 /* Very simple synthesis routine to generate two sine waves. */
|
Chris@4
|
66 static int paminlatCallback( const void *inputBuffer, void *outputBuffer,
|
Chris@4
|
67 unsigned long framesPerBuffer,
|
Chris@4
|
68 const PaStreamCallbackTimeInfo* timeInfo,
|
Chris@4
|
69 PaStreamCallbackFlags statusFlags,
|
Chris@4
|
70 void *userData )
|
Chris@4
|
71 {
|
Chris@4
|
72 paTestData *data = (paTestData*)userData;
|
Chris@4
|
73 float *out = (float*)outputBuffer;
|
Chris@4
|
74 unsigned int i;
|
Chris@4
|
75 double left_phaseInc = 0.02;
|
Chris@4
|
76 double right_phaseInc = 0.06;
|
Chris@4
|
77
|
Chris@4
|
78 double left_phase = data->left_phase;
|
Chris@4
|
79 double right_phase = data->right_phase;
|
Chris@4
|
80
|
Chris@4
|
81 for( i=0; i<framesPerBuffer; i++ )
|
Chris@4
|
82 {
|
Chris@4
|
83 left_phase += left_phaseInc;
|
Chris@4
|
84 if( left_phase > TWOPI ) left_phase -= TWOPI;
|
Chris@4
|
85 *out++ = (float) sin( left_phase );
|
Chris@4
|
86
|
Chris@4
|
87 right_phase += right_phaseInc;
|
Chris@4
|
88 if( right_phase > TWOPI ) right_phase -= TWOPI;
|
Chris@4
|
89 *out++ = (float) sin( right_phase );
|
Chris@4
|
90 }
|
Chris@4
|
91
|
Chris@4
|
92 data->left_phase = left_phase;
|
Chris@4
|
93 data->right_phase = right_phase;
|
Chris@4
|
94 return 0;
|
Chris@4
|
95 }
|
Chris@4
|
96
|
Chris@4
|
97 int main( int argc, char **argv );
|
Chris@4
|
98 int main( int argc, char **argv )
|
Chris@4
|
99 {
|
Chris@4
|
100 PaStreamParameters outputParameters;
|
Chris@4
|
101 PaStream *stream;
|
Chris@4
|
102 PaError err;
|
Chris@4
|
103 paTestData data;
|
Chris@4
|
104 int go;
|
Chris@4
|
105 int outLatency = 0;
|
Chris@4
|
106 int minLatency = DEFAULT_BUFFER_SIZE * 2;
|
Chris@4
|
107 int framesPerBuffer;
|
Chris@4
|
108 double sampleRate = 44100.0;
|
Chris@4
|
109 char str[256];
|
Chris@4
|
110 char *line;
|
Chris@4
|
111
|
Chris@4
|
112 printf("pa_minlat - Determine minimum latency for your computer.\n");
|
Chris@4
|
113 printf(" usage: pa_minlat {userBufferSize}\n");
|
Chris@4
|
114 printf(" for example: pa_minlat 64\n");
|
Chris@4
|
115 printf("Adjust your stereo until you hear a smooth tone in each speaker.\n");
|
Chris@4
|
116 printf("Then try to find the smallest number of frames that still sounds smooth.\n");
|
Chris@4
|
117 printf("Note that the sound will stop momentarily when you change the number of buffers.\n");
|
Chris@4
|
118
|
Chris@4
|
119 /* Get bufferSize from command line. */
|
Chris@4
|
120 framesPerBuffer = ( argc > 1 ) ? atol( argv[1] ) : DEFAULT_BUFFER_SIZE;
|
Chris@4
|
121 printf("Frames per buffer = %d\n", framesPerBuffer );
|
Chris@4
|
122
|
Chris@4
|
123 data.left_phase = data.right_phase = 0.0;
|
Chris@4
|
124
|
Chris@4
|
125 err = Pa_Initialize();
|
Chris@4
|
126 if( err != paNoError ) goto error;
|
Chris@4
|
127
|
Chris@4
|
128 outLatency = sampleRate * 200.0 / 1000.0; /* 200 msec. */
|
Chris@4
|
129
|
Chris@4
|
130 /* Try different numBuffers in a loop. */
|
Chris@4
|
131 go = 1;
|
Chris@4
|
132 while( go )
|
Chris@4
|
133 {
|
Chris@4
|
134 outputParameters.device = Pa_GetDefaultOutputDevice(); /* Default output device. */
|
Chris@4
|
135 outputParameters.channelCount = 2; /* Stereo output */
|
Chris@4
|
136 outputParameters.sampleFormat = paFloat32; /* 32 bit floating point output. */
|
Chris@4
|
137 outputParameters.suggestedLatency = (double)outLatency / sampleRate; /* In seconds. */
|
Chris@4
|
138 outputParameters.hostApiSpecificStreamInfo = NULL;
|
Chris@4
|
139
|
Chris@4
|
140 printf("Latency = %d frames = %6.1f msec.\n", outLatency, outputParameters.suggestedLatency * 1000.0 );
|
Chris@4
|
141
|
Chris@4
|
142 err = Pa_OpenStream(
|
Chris@4
|
143 &stream,
|
Chris@4
|
144 NULL, /* no input */
|
Chris@4
|
145 &outputParameters,
|
Chris@4
|
146 sampleRate,
|
Chris@4
|
147 framesPerBuffer,
|
Chris@4
|
148 paClipOff, /* we won't output out of range samples so don't bother clipping them */
|
Chris@4
|
149 paminlatCallback,
|
Chris@4
|
150 &data );
|
Chris@4
|
151 if( err != paNoError ) goto error;
|
Chris@4
|
152 if( stream == NULL ) goto error;
|
Chris@4
|
153
|
Chris@4
|
154 /* Start audio. */
|
Chris@4
|
155 err = Pa_StartStream( stream );
|
Chris@4
|
156 if( err != paNoError ) goto error;
|
Chris@4
|
157
|
Chris@4
|
158 /* Ask user for a new nlatency. */
|
Chris@4
|
159 printf("\nMove windows around to see if the sound glitches.\n");
|
Chris@4
|
160 printf("Latency now %d, enter new number of frames, or 'q' to quit: ", outLatency );
|
Chris@4
|
161 line = fgets( str, 256, stdin );
|
Chris@4
|
162 if( line == NULL )
|
Chris@4
|
163 {
|
Chris@4
|
164 go = 0;
|
Chris@4
|
165 }
|
Chris@4
|
166 else
|
Chris@4
|
167 {
|
Chris@4
|
168 {
|
Chris@4
|
169 /* Get rid of newline */
|
Chris@4
|
170 size_t l = strlen( str ) - 1;
|
Chris@4
|
171 if( str[ l ] == '\n')
|
Chris@4
|
172 str[ l ] = '\0';
|
Chris@4
|
173 }
|
Chris@4
|
174
|
Chris@4
|
175
|
Chris@4
|
176 if( str[0] == 'q' ) go = 0;
|
Chris@4
|
177 else
|
Chris@4
|
178 {
|
Chris@4
|
179 outLatency = atol( str );
|
Chris@4
|
180 if( outLatency < minLatency )
|
Chris@4
|
181 {
|
Chris@4
|
182 printf( "Latency below minimum of %d! Set to minimum!!!\n", minLatency );
|
Chris@4
|
183 outLatency = minLatency;
|
Chris@4
|
184 }
|
Chris@4
|
185 }
|
Chris@4
|
186
|
Chris@4
|
187 }
|
Chris@4
|
188 /* Stop sound until ENTER hit. */
|
Chris@4
|
189 err = Pa_StopStream( stream );
|
Chris@4
|
190 if( err != paNoError ) goto error;
|
Chris@4
|
191 err = Pa_CloseStream( stream );
|
Chris@4
|
192 if( err != paNoError ) goto error;
|
Chris@4
|
193 }
|
Chris@4
|
194 printf("A good setting for latency would be somewhat higher than\n");
|
Chris@4
|
195 printf("the minimum latency that worked.\n");
|
Chris@4
|
196 printf("PortAudio: Test finished.\n");
|
Chris@4
|
197 Pa_Terminate();
|
Chris@4
|
198 return 0;
|
Chris@4
|
199 error:
|
Chris@4
|
200 Pa_Terminate();
|
Chris@4
|
201 fprintf( stderr, "An error occured while using the portaudio stream\n" );
|
Chris@4
|
202 fprintf( stderr, "Error number: %d\n", err );
|
Chris@4
|
203 fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) );
|
Chris@4
|
204 return 1;
|
Chris@4
|
205 }
|