Debian Codegraph @

3rdparty/iaxclient/lib/portaudio/test/pa_minlat.c

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 /** @file pa_minlat.c
2 @brief Experiment with different numbers of buffers to determine the
3 minimum latency for a computer.
4 @author Phil Burk http://www.softsynth.com
5 */
6 /*
7 * $Id: pa_minlat.c,v 1.1 2006/06/10 21:30:56 dmazzoni Exp $
8 *
9 * This program uses the PortAudio Portable Audio Library.
10 * For more information see: http://www.portaudio.com
11 * Copyright (c) 1999-2000 Ross Bencina and Phil Burk
12 *
13 * Permission is hereby granted, free of charge, to any person obtaining
14 * a copy of this software and associated documentation files
15 * (the "Software"), to deal in the Software without restriction,
16 * including without limitation the rights to use, copy, modify, merge,
17 * publish, distribute, sublicense, and/or sell copies of the Software,
18 * and to permit persons to whom the Software is furnished to do so,
19 * subject to the following conditions:
20 *
21 * The above copyright notice and this permission notice shall be
22 * included in all copies or substantial portions of the Software.
23 *
24 * Any person wishing to distribute modifications to the Software is
25 * requested to send the modifications to the original developer so that
26 * they can be incorporated into the canonical version.
27 *
28 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
29 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
30 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
31 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
32 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
33 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
34 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
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 /* Very simple synthesis routine to generate two sine waves. */
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 /* Get bufferSize from command line. */
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; /* 200 msec. */
121
122 /* Try different numBuffers in a loop. */
123 go = 1;
124 while( go )
125 {
126 outputParameters.device = Pa_GetDefaultOutputDevice(); /* Default output device. */
127 outputParameters.channelCount = 2; /* Stereo output */
128 outputParameters.sampleFormat = paFloat32; /* 32 bit floating point output. */
129 outputParameters.suggestedLatency = (double)outLatency / sampleRate; /* In seconds. */
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, /* no input */
137 &outputParameters,
138 sampleRate,
139 framesPerBuffer,
140 paClipOff, /* we won't output out of range samples so don't bother clipping them */
141 paminlatCallback,
142 &data );
143 if( err != paNoError ) goto error;
144 if( stream == NULL ) goto error;
145
146 /* Start audio. */
147 err = Pa_StartStream( stream );
148 if( err != paNoError ) goto error;
149
150 /* Ask user for a new nlatency. */
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 /* Get rid of newline */
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 /* Stop sound until ENTER hit. */
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 }