Debian Codegraph @

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

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 /** @file
2 @brief Hear the latency caused by big buffers.
3 Play a sine wave and change frequency based on letter input.
4 @author Phil Burk <[email protected]>, and Darren Gibbs
5 */
6 /*
7 * $Id: patest_latency.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 <math.h>
40 #include "portaudio.h"
41
42 #define OUTPUT_DEVICE (Pa_GetDefaultOutputDevice())
43 #define SAMPLE_RATE (44100)
44 #define FRAMES_PER_BUFFER (64)
45
46 #define MIN_FREQ (100.0f)
47 #define CalcPhaseIncrement(freq) ((freq)/SAMPLE_RATE)
48 #ifndef M_PI
49 #define M_PI (3.14159265)
50 #endif
51 #define TABLE_SIZE (400)
52
53 typedef struct
54 {
55 float sine[TABLE_SIZE + 1]; /* add one for guard point for interpolation */
56 float phase_increment;
57 float left_phase;
58 float right_phase;
59 }
60 paTestData;
61
62 float LookupSine( paTestData *data, float phase );
63 /* Convert phase between and 1.0 to sine value
64 * using linear interpolation.
65 */
66 float LookupSine( paTestData *data, float phase )
67 {
68 float fIndex = phase*TABLE_SIZE;
69 int index = (int) fIndex;
70 float fract = fIndex - index;
71 float lo = data->sine[index];
72 float hi = data->sine[index+1];
73 float val = lo + fract*(hi-lo);
74 return val;
75 }
76
77 /* This routine will be called by the PortAudio engine when audio is needed.
78 ** It may called at interrupt level on some machines so don't do anything
79 ** that could mess up the system like calling malloc() or free().
80 */
81 static int patestCallback( const void *inputBuffer, void *outputBuffer,
82 unsigned long framesPerBuffer,
83 const PaStreamCallbackTimeInfo* timeInfo,
84 PaStreamCallbackFlags statusFlags,
85 void *userData )
86 {
87 paTestData *data = (paTestData*)userData;
88 float *out = (float*)outputBuffer;
89 int i;
90
91 (void) inputBuffer; /* Prevent unused variable warning. */
92
93 for( i=0; i<framesPerBuffer; i++ )
94 {
95 *out++ = LookupSine(data, data->left_phase); /* left */
96 *out++ = LookupSine(data, data->right_phase); /* right */
97 data->left_phase += data->phase_increment;
98 if( data->left_phase >= 1.0f ) data->left_phase -= 1.0f;
99 data->right_phase += (data->phase_increment * 1.5f); /* fifth above */
100 if( data->right_phase >= 1.0f ) data->right_phase -= 1.0f;
101 }
102 return 0;
103 }
104
105 /*******************************************************************/
106 int main(void);
107 int main(void)
108 {
109 PaStream *stream;
110 PaStreamParameters outputParameters;
111 PaError err;
112 paTestData data;
113 int i;
114 int done = 0;
115
116 printf("PortAudio Test: enter letter then hit ENTER.\n" );
117 /* initialise sinusoidal wavetable */
118 for( i=0; i<TABLE_SIZE; i++ )
119 {
120 data.sine[i] = 0.90f * (float) sin( ((double)i/(double)TABLE_SIZE) * M_PI * 2. );
121 }
122 data.sine[TABLE_SIZE] = data.sine[0]; /* set guard point. */
123 data.left_phase = data.right_phase = 0.0;
124 data.phase_increment = CalcPhaseIncrement(MIN_FREQ);
125
126 err = Pa_Initialize();
127 if( err != paNoError ) goto error;
128 printf("PortAudio Test: output device = %d\n", OUTPUT_DEVICE );
129
130 outputParameters.device = OUTPUT_DEVICE;
131 outputParameters.channelCount = 2; /* stereo output */
132 outputParameters.sampleFormat = paFloat32; /* 32 bit floating point output */
133 outputParameters.suggestedLatency = Pa_GetDeviceInfo( outputParameters.device )->defaultLowOutputLatency;
134 outputParameters.hostApiSpecificStreamInfo = NULL;
135
136 printf("Requested output latency = %.4f seconds.\n", outputParameters.suggestedLatency );
137 printf("%d frames per buffer.\n.", FRAMES_PER_BUFFER );
138
139 err = Pa_OpenStream(
140 &stream,
141 NULL, /* no input */
142 &outputParameters,
143 SAMPLE_RATE,
144 FRAMES_PER_BUFFER,
145 paClipOff|paDitherOff, /* we won't output out of range samples so don't bother clipping them */
146 patestCallback,
147 &data );
148 if( err != paNoError ) goto error;
149
150 err = Pa_StartStream( stream );
151 if( err != paNoError ) goto error;
152 printf("Play ASCII keyboard. Hit 'q' to stop. (Use RETURN key on Mac)\n");
153 fflush(stdout);
154 while ( !done )
155 {
156 float freq;
157 int index;
158 char c;
159 do
160 {
161 c = getchar();
162 }
163 while( c < ' '); /* Strip white space and control chars. */
164
165 if( c == 'q' ) done = 1;
166 index = c % 26;
167 freq = MIN_FREQ + (index * 40.0);
168 data.phase_increment = CalcPhaseIncrement(freq);
169 }
170 printf("Call Pa_StopStream()\n");
171 err = Pa_StopStream( stream );
172 if( err != paNoError ) goto error;
173 Pa_Terminate();
174 printf("Test finished.\n");
175 return err;
176 error:
177 Pa_Terminate();
•178 fprintf( stderr, "An error occurred while using the portaudio stream\n" );
179 fprintf( stderr, "Error number: %d\n", err );
180 fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) );
181 return err;
182 }