cannam@140
|
1 /** @file patest_suggested_vs_streaminfo_latency.c
|
cannam@140
|
2 @ingroup test_src
|
cannam@140
|
3 @brief Print suggested vs. PaStreamInfo reported actual latency
|
cannam@140
|
4 @author Ross Bencina <rossb@audiomulch.com>
|
cannam@140
|
5
|
cannam@140
|
6 Opens streams with a sequence of suggested latency values
|
cannam@140
|
7 from 0 to 2 seconds in .5ms intervals and gathers the resulting actual
|
cannam@140
|
8 latency values. Output a csv file and graph suggested vs. actual. Run
|
cannam@140
|
9 with framesPerBuffer unspecified, powers of 2 and multiples of 50 and
|
cannam@140
|
10 prime number buffer sizes.
|
cannam@140
|
11 */
|
cannam@140
|
12 /*
|
cannam@140
|
13 * $Id: patest_sine.c 1368 2008-03-01 00:38:27Z rossb $
|
cannam@140
|
14 *
|
cannam@140
|
15 * This program uses the PortAudio Portable Audio Library.
|
cannam@140
|
16 * For more information see: http://www.portaudio.com/
|
cannam@140
|
17 * Copyright (c) 1999-2000 Ross Bencina and Phil Burk
|
cannam@140
|
18 *
|
cannam@140
|
19 * Permission is hereby granted, free of charge, to any person obtaining
|
cannam@140
|
20 * a copy of this software and associated documentation files
|
cannam@140
|
21 * (the "Software"), to deal in the Software without restriction,
|
cannam@140
|
22 * including without limitation the rights to use, copy, modify, merge,
|
cannam@140
|
23 * publish, distribute, sublicense, and/or sell copies of the Software,
|
cannam@140
|
24 * and to permit persons to whom the Software is furnished to do so,
|
cannam@140
|
25 * subject to the following conditions:
|
cannam@140
|
26 *
|
cannam@140
|
27 * The above copyright notice and this permission notice shall be
|
cannam@140
|
28 * included in all copies or substantial portions of the Software.
|
cannam@140
|
29 *
|
cannam@140
|
30 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
cannam@140
|
31 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
cannam@140
|
32 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
cannam@140
|
33 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
|
cannam@140
|
34 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
|
cannam@140
|
35 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
cannam@140
|
36 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
cannam@140
|
37 */
|
cannam@140
|
38
|
cannam@140
|
39 /*
|
cannam@140
|
40 * The text above constitutes the entire PortAudio license; however,
|
cannam@140
|
41 * the PortAudio community also makes the following non-binding requests:
|
cannam@140
|
42 *
|
cannam@140
|
43 * Any person wishing to distribute modifications to the Software is
|
cannam@140
|
44 * requested to send the modifications to the original developer so that
|
cannam@140
|
45 * they can be incorporated into the canonical version. It is also
|
cannam@140
|
46 * requested that these non-binding requests be included along with the
|
cannam@140
|
47 * license above.
|
cannam@140
|
48 */
|
cannam@140
|
49 #include <stdio.h>
|
cannam@140
|
50 #include <stdlib.h>
|
cannam@140
|
51 #include <string.h>
|
cannam@140
|
52 #include <math.h>
|
cannam@140
|
53 #include "portaudio.h"
|
cannam@140
|
54
|
cannam@140
|
55 #define SAMPLE_RATE (44100)
|
cannam@140
|
56 #define FRAMES_PER_BUFFER 2//(128)
|
cannam@140
|
57 #define NUM_CHANNELS (2)
|
cannam@140
|
58
|
cannam@140
|
59 #define SUGGESTED_LATENCY_START_SECONDS (0.0)
|
cannam@140
|
60 #define SUGGESTED_LATENCY_END_SECONDS (2.0)
|
cannam@140
|
61 #define SUGGESTED_LATENCY_INCREMENT_SECONDS (0.0005) /* half a millisecond increments */
|
cannam@140
|
62
|
cannam@140
|
63
|
cannam@140
|
64 /* dummy callback. does nothing. never gets called */
|
cannam@140
|
65 static int patestCallback( const void *inputBuffer, void *outputBuffer,
|
cannam@140
|
66 unsigned long framesPerBuffer,
|
cannam@140
|
67 const PaStreamCallbackTimeInfo* timeInfo,
|
cannam@140
|
68 PaStreamCallbackFlags statusFlags,
|
cannam@140
|
69 void *userData )
|
cannam@140
|
70 {
|
cannam@140
|
71 return paContinue;
|
cannam@140
|
72 }
|
cannam@140
|
73
|
cannam@140
|
74 /*******************************************************************/
|
cannam@140
|
75 static void usage()
|
cannam@140
|
76 {
|
cannam@140
|
77 int i;
|
cannam@140
|
78 const PaDeviceInfo *deviceInfo;
|
cannam@140
|
79 const char *channelString;
|
cannam@140
|
80
|
cannam@140
|
81 fprintf( stderr, "PortAudio suggested (requested) vs. resulting (reported) stream latency test\n" );
|
cannam@140
|
82 fprintf( stderr, "Usage: x.exe input-device-index output-device-index sample-rate frames-per-buffer\n" );
|
cannam@140
|
83 fprintf( stderr, "Use -1 for default device index, or use one of these:\n" );
|
cannam@140
|
84 for( i=0; i < Pa_GetDeviceCount(); ++i ){
|
cannam@140
|
85 deviceInfo = Pa_GetDeviceInfo(i);
|
cannam@140
|
86 if( deviceInfo->maxInputChannels > 0 && deviceInfo->maxOutputChannels > 0 )
|
cannam@140
|
87 channelString = "full-duplex";
|
cannam@140
|
88 else if( deviceInfo->maxInputChannels > 0 )
|
cannam@140
|
89 channelString = "input only";
|
cannam@140
|
90 else
|
cannam@140
|
91 channelString = "output only";
|
cannam@140
|
92
|
cannam@140
|
93 fprintf( stderr, "%d (%s, %s, %s)\n", i, deviceInfo->name, Pa_GetHostApiInfo(deviceInfo->hostApi)->name, channelString );
|
cannam@140
|
94 }
|
cannam@140
|
95 Pa_Terminate();
|
cannam@140
|
96 exit(-1);
|
cannam@140
|
97 }
|
cannam@140
|
98
|
cannam@140
|
99 int main( int argc, const char* argv[] );
|
cannam@140
|
100 int main( int argc, const char* argv[] )
|
cannam@140
|
101 {
|
cannam@140
|
102 PaStreamParameters inputParameters, outputParameters;
|
cannam@140
|
103 PaStream *stream;
|
cannam@140
|
104 PaError err;
|
cannam@140
|
105 PaTime suggestedLatency;
|
cannam@140
|
106 const PaStreamInfo *streamInfo;
|
cannam@140
|
107 const PaDeviceInfo *deviceInfo;
|
cannam@140
|
108 float sampleRate = SAMPLE_RATE;
|
cannam@140
|
109 int framesPerBuffer = FRAMES_PER_BUFFER;
|
cannam@140
|
110 err = Pa_Initialize();
|
cannam@140
|
111 if( err != paNoError ) goto error;
|
cannam@140
|
112
|
cannam@140
|
113 if( argc > 1 && strcmp(argv[1],"-h") == 0 )
|
cannam@140
|
114 usage();
|
cannam@140
|
115
|
cannam@140
|
116 if( argc > 3 ){
|
cannam@140
|
117 sampleRate = atoi(argv[3]);
|
cannam@140
|
118 }
|
cannam@140
|
119
|
cannam@140
|
120 if( argc > 4 ){
|
cannam@140
|
121 framesPerBuffer = atoi(argv[4]);
|
cannam@140
|
122 }
|
cannam@140
|
123
|
cannam@140
|
124 printf("# sample rate=%f, frames per buffer=%d\n", (float)sampleRate, framesPerBuffer );
|
cannam@140
|
125
|
cannam@140
|
126 inputParameters.device = -1;
|
cannam@140
|
127 if( argc > 1 )
|
cannam@140
|
128 inputParameters.device = atoi(argv[1]);
|
cannam@140
|
129 if( inputParameters.device == -1 ){
|
cannam@140
|
130 inputParameters.device = Pa_GetDefaultInputDevice();
|
cannam@140
|
131 if (inputParameters.device == paNoDevice) {
|
cannam@140
|
132 fprintf(stderr,"Error: No default input device available.\n");
|
cannam@140
|
133 goto error;
|
cannam@140
|
134 }
|
cannam@140
|
135 }else{
|
cannam@140
|
136 deviceInfo = Pa_GetDeviceInfo(inputParameters.device);
|
cannam@140
|
137 if( !deviceInfo ){
|
cannam@140
|
138 fprintf(stderr,"Error: Invalid input device index.\n");
|
cannam@140
|
139 usage();
|
cannam@140
|
140 }
|
cannam@140
|
141 if( deviceInfo->maxInputChannels == 0 ){
|
cannam@140
|
142 fprintf(stderr,"Error: Specified input device has no input channels (an output only device?).\n");
|
cannam@140
|
143 usage();
|
cannam@140
|
144 }
|
cannam@140
|
145 }
|
cannam@140
|
146
|
cannam@140
|
147 inputParameters.channelCount = NUM_CHANNELS;
|
cannam@140
|
148 inputParameters.sampleFormat = paFloat32; /* 32 bit floating point output */
|
cannam@140
|
149 inputParameters.hostApiSpecificStreamInfo = NULL;
|
cannam@140
|
150
|
cannam@140
|
151 deviceInfo = Pa_GetDeviceInfo(inputParameters.device);
|
cannam@140
|
152 printf( "# using input device id %d (%s, %s)\n", inputParameters.device, deviceInfo->name, Pa_GetHostApiInfo(deviceInfo->hostApi)->name );
|
cannam@140
|
153
|
cannam@140
|
154
|
cannam@140
|
155 outputParameters.device = -1;
|
cannam@140
|
156 if( argc > 2 )
|
cannam@140
|
157 outputParameters.device = atoi(argv[2]);
|
cannam@140
|
158 if( outputParameters.device == -1 ){
|
cannam@140
|
159 outputParameters.device = Pa_GetDefaultOutputDevice();
|
cannam@140
|
160 if (outputParameters.device == paNoDevice) {
|
cannam@140
|
161 fprintf(stderr,"Error: No default output device available.\n");
|
cannam@140
|
162 goto error;
|
cannam@140
|
163 }
|
cannam@140
|
164 }else{
|
cannam@140
|
165 deviceInfo = Pa_GetDeviceInfo(outputParameters.device);
|
cannam@140
|
166 if( !deviceInfo ){
|
cannam@140
|
167 fprintf(stderr,"Error: Invalid output device index.\n");
|
cannam@140
|
168 usage();
|
cannam@140
|
169 }
|
cannam@140
|
170 if( deviceInfo->maxOutputChannels == 0 ){
|
cannam@140
|
171 fprintf(stderr,"Error: Specified output device has no output channels (an input only device?).\n");
|
cannam@140
|
172 usage();
|
cannam@140
|
173 }
|
cannam@140
|
174 }
|
cannam@140
|
175
|
cannam@140
|
176 outputParameters.channelCount = NUM_CHANNELS;
|
cannam@140
|
177 outputParameters.sampleFormat = paFloat32; /* 32 bit floating point output */
|
cannam@140
|
178 outputParameters.hostApiSpecificStreamInfo = NULL;
|
cannam@140
|
179
|
cannam@140
|
180 deviceInfo = Pa_GetDeviceInfo(outputParameters.device);
|
cannam@140
|
181 printf( "# using output device id %d (%s, %s)\n", outputParameters.device, deviceInfo->name, Pa_GetHostApiInfo(deviceInfo->hostApi)->name );
|
cannam@140
|
182
|
cannam@140
|
183
|
cannam@140
|
184 printf( "# suggested latency, half duplex PaStreamInfo::outputLatency, half duplex PaStreamInfo::inputLatency, full duplex PaStreamInfo::outputLatency, full duplex PaStreamInfo::inputLatency\n" );
|
cannam@140
|
185 suggestedLatency = SUGGESTED_LATENCY_START_SECONDS;
|
cannam@140
|
186 while( suggestedLatency <= SUGGESTED_LATENCY_END_SECONDS ){
|
cannam@140
|
187
|
cannam@140
|
188 outputParameters.suggestedLatency = suggestedLatency;
|
cannam@140
|
189 inputParameters.suggestedLatency = suggestedLatency;
|
cannam@140
|
190
|
cannam@140
|
191 printf( "%f, ", suggestedLatency );
|
cannam@140
|
192
|
cannam@140
|
193 /* ------------------------------ output ------------------------------ */
|
cannam@140
|
194
|
cannam@140
|
195 err = Pa_OpenStream(
|
cannam@140
|
196 &stream,
|
cannam@140
|
197 NULL, /* no input */
|
cannam@140
|
198 &outputParameters,
|
cannam@140
|
199 sampleRate,
|
cannam@140
|
200 framesPerBuffer,
|
cannam@140
|
201 paClipOff, /* we won't output out of range samples so don't bother clipping them */
|
cannam@140
|
202 patestCallback,
|
cannam@140
|
203 0 );
|
cannam@140
|
204 if( err != paNoError ) goto error;
|
cannam@140
|
205
|
cannam@140
|
206 streamInfo = Pa_GetStreamInfo( stream );
|
cannam@140
|
207
|
cannam@140
|
208 printf( "%f,", streamInfo->outputLatency );
|
cannam@140
|
209
|
cannam@140
|
210 err = Pa_CloseStream( stream );
|
cannam@140
|
211 if( err != paNoError ) goto error;
|
cannam@140
|
212
|
cannam@140
|
213 /* ------------------------------ input ------------------------------ */
|
cannam@140
|
214
|
cannam@140
|
215 err = Pa_OpenStream(
|
cannam@140
|
216 &stream,
|
cannam@140
|
217 &inputParameters,
|
cannam@140
|
218 NULL, /* no output */
|
cannam@140
|
219 sampleRate,
|
cannam@140
|
220 framesPerBuffer,
|
cannam@140
|
221 paClipOff, /* we won't output out of range samples so don't bother clipping them */
|
cannam@140
|
222 patestCallback,
|
cannam@140
|
223 0 );
|
cannam@140
|
224 if( err != paNoError ) goto error;
|
cannam@140
|
225
|
cannam@140
|
226 streamInfo = Pa_GetStreamInfo( stream );
|
cannam@140
|
227
|
cannam@140
|
228 printf( "%f,", streamInfo->inputLatency );
|
cannam@140
|
229
|
cannam@140
|
230 err = Pa_CloseStream( stream );
|
cannam@140
|
231 if( err != paNoError ) goto error;
|
cannam@140
|
232
|
cannam@140
|
233 /* ------------------------------ full duplex ------------------------------ */
|
cannam@140
|
234
|
cannam@140
|
235 err = Pa_OpenStream(
|
cannam@140
|
236 &stream,
|
cannam@140
|
237 &inputParameters,
|
cannam@140
|
238 &outputParameters,
|
cannam@140
|
239 sampleRate,
|
cannam@140
|
240 framesPerBuffer,
|
cannam@140
|
241 paClipOff, /* we won't output out of range samples so don't bother clipping them */
|
cannam@140
|
242 patestCallback,
|
cannam@140
|
243 0 );
|
cannam@140
|
244 if( err != paNoError ) goto error;
|
cannam@140
|
245
|
cannam@140
|
246 streamInfo = Pa_GetStreamInfo( stream );
|
cannam@140
|
247
|
cannam@140
|
248 printf( "%f,%f", streamInfo->outputLatency, streamInfo->inputLatency );
|
cannam@140
|
249
|
cannam@140
|
250 err = Pa_CloseStream( stream );
|
cannam@140
|
251 if( err != paNoError ) goto error;
|
cannam@140
|
252
|
cannam@140
|
253 /* ------------------------------------------------------------ */
|
cannam@140
|
254
|
cannam@140
|
255 printf( "\n" );
|
cannam@140
|
256 suggestedLatency += SUGGESTED_LATENCY_INCREMENT_SECONDS;
|
cannam@140
|
257 }
|
cannam@140
|
258
|
cannam@140
|
259 Pa_Terminate();
|
cannam@140
|
260 printf("# Test finished.\n");
|
cannam@140
|
261
|
cannam@140
|
262 return err;
|
cannam@140
|
263 error:
|
cannam@140
|
264 Pa_Terminate();
|
cannam@140
|
265 fprintf( stderr, "An error occured while using the portaudio stream\n" );
|
cannam@140
|
266 fprintf( stderr, "Error number: %d\n", err );
|
cannam@140
|
267 fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) );
|
cannam@140
|
268 return err;
|
cannam@140
|
269 }
|