comparison src/portaudio/test/patest_latency.c @ 89:8a15ff55d9af

Add bzip2, zlib, liblo, portaudio sources
author Chris Cannam <cannam@all-day-breakfast.com>
date Wed, 20 Mar 2013 13:59:52 +0000
parents
children
comparison
equal deleted inserted replaced
88:fe7c3a0b0259 89:8a15ff55d9af
1 /** @file
2 @ingroup test_src
3 @brief Hear the latency caused by big buffers.
4 Play a sine wave and change frequency based on letter input.
5 @author Phil Burk <philburk@softsynth.com>, and Darren Gibbs
6 */
7 /*
8 * $Id: patest_latency.c 1368 2008-03-01 00:38:27Z rossb $
9 *
10 * This program uses the PortAudio Portable Audio Library.
11 * For more information see: http://www.portaudio.com
12 * Copyright (c) 1999-2000 Ross Bencina and Phil Burk
13 *
14 * Permission is hereby granted, free of charge, to any person obtaining
15 * a copy of this software and associated documentation files
16 * (the "Software"), to deal in the Software without restriction,
17 * including without limitation the rights to use, copy, modify, merge,
18 * publish, distribute, sublicense, and/or sell copies of the Software,
19 * and to permit persons to whom the Software is furnished to do so,
20 * subject to the following conditions:
21 *
22 * The above copyright notice and this permission notice shall be
23 * included in all copies or substantial portions of the Software.
24 *
25 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
28 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
29 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
30 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 */
33
34 /*
35 * The text above constitutes the entire PortAudio license; however,
36 * the PortAudio community also makes the following non-binding requests:
37 *
38 * Any person wishing to distribute modifications to the Software is
39 * requested to send the modifications to the original developer so that
40 * they can be incorporated into the canonical version. It is also
41 * requested that these non-binding requests be included along with the
42 * license above.
43 */
44
45 #include <stdio.h>
46 #include <math.h>
47 #include "portaudio.h"
48
49 #define OUTPUT_DEVICE (Pa_GetDefaultOutputDevice())
50 #define SAMPLE_RATE (44100)
51 #define FRAMES_PER_BUFFER (64)
52
53 #define MIN_FREQ (100.0f)
54 #define CalcPhaseIncrement(freq) ((freq)/SAMPLE_RATE)
55 #ifndef M_PI
56 #define M_PI (3.14159265)
57 #endif
58 #define TABLE_SIZE (400)
59
60 typedef struct
61 {
62 float sine[TABLE_SIZE + 1]; /* add one for guard point for interpolation */
63 float phase_increment;
64 float left_phase;
65 float right_phase;
66 }
67 paTestData;
68
69 float LookupSine( paTestData *data, float phase );
70 /* Convert phase between and 1.0 to sine value
71 * using linear interpolation.
72 */
73 float LookupSine( paTestData *data, float phase )
74 {
75 float fIndex = phase*TABLE_SIZE;
76 int index = (int) fIndex;
77 float fract = fIndex - index;
78 float lo = data->sine[index];
79 float hi = data->sine[index+1];
80 float val = lo + fract*(hi-lo);
81 return val;
82 }
83
84 /* This routine will be called by the PortAudio engine when audio is needed.
85 ** It may called at interrupt level on some machines so don't do anything
86 ** that could mess up the system like calling malloc() or free().
87 */
88 static int patestCallback( const void *inputBuffer, void *outputBuffer,
89 unsigned long framesPerBuffer,
90 const PaStreamCallbackTimeInfo* timeInfo,
91 PaStreamCallbackFlags statusFlags,
92 void *userData )
93 {
94 paTestData *data = (paTestData*)userData;
95 float *out = (float*)outputBuffer;
96 int i;
97
98 (void) inputBuffer; /* Prevent unused variable warning. */
99
100 for( i=0; i<framesPerBuffer; i++ )
101 {
102 *out++ = LookupSine(data, data->left_phase); /* left */
103 *out++ = LookupSine(data, data->right_phase); /* right */
104 data->left_phase += data->phase_increment;
105 if( data->left_phase >= 1.0f ) data->left_phase -= 1.0f;
106 data->right_phase += (data->phase_increment * 1.5f); /* fifth above */
107 if( data->right_phase >= 1.0f ) data->right_phase -= 1.0f;
108 }
109 return 0;
110 }
111
112 /*******************************************************************/
113 int main(void);
114 int main(void)
115 {
116 PaStream *stream;
117 PaStreamParameters outputParameters;
118 PaError err;
119 paTestData data;
120 int i;
121 int done = 0;
122
123 printf("PortAudio Test: enter letter then hit ENTER.\n" );
124 /* initialise sinusoidal wavetable */
125 for( i=0; i<TABLE_SIZE; i++ )
126 {
127 data.sine[i] = 0.90f * (float) sin( ((double)i/(double)TABLE_SIZE) * M_PI * 2. );
128 }
129 data.sine[TABLE_SIZE] = data.sine[0]; /* set guard point. */
130 data.left_phase = data.right_phase = 0.0;
131 data.phase_increment = CalcPhaseIncrement(MIN_FREQ);
132
133 err = Pa_Initialize();
134 if( err != paNoError ) goto error;
135 printf("PortAudio Test: output device = %d\n", OUTPUT_DEVICE );
136
137 outputParameters.device = OUTPUT_DEVICE;
138 if (outputParameters.device == paNoDevice) {
139 fprintf(stderr,"Error: No default output device.\n");
140 goto error;
141 }
142 outputParameters.channelCount = 2; /* stereo output */
143 outputParameters.sampleFormat = paFloat32; /* 32 bit floating point output */
144 outputParameters.suggestedLatency = Pa_GetDeviceInfo( outputParameters.device )->defaultLowOutputLatency;
145 outputParameters.hostApiSpecificStreamInfo = NULL;
146
147 printf("Requested output latency = %.4f seconds.\n", outputParameters.suggestedLatency );
148 printf("%d frames per buffer.\n.", FRAMES_PER_BUFFER );
149
150 err = Pa_OpenStream(
151 &stream,
152 NULL, /* no input */
153 &outputParameters,
154 SAMPLE_RATE,
155 FRAMES_PER_BUFFER,
156 paClipOff|paDitherOff, /* we won't output out of range samples so don't bother clipping them */
157 patestCallback,
158 &data );
159 if( err != paNoError ) goto error;
160
161 err = Pa_StartStream( stream );
162 if( err != paNoError ) goto error;
163 printf("Play ASCII keyboard. Hit 'q' to stop. (Use RETURN key on Mac)\n");
164 fflush(stdout);
165 while ( !done )
166 {
167 float freq;
168 int index;
169 char c;
170 do
171 {
172 c = getchar();
173 }
174 while( c < ' '); /* Strip white space and control chars. */
175
176 if( c == 'q' ) done = 1;
177 index = c % 26;
178 freq = MIN_FREQ + (index * 40.0);
179 data.phase_increment = CalcPhaseIncrement(freq);
180 }
181 printf("Call Pa_StopStream()\n");
182 err = Pa_StopStream( stream );
183 if( err != paNoError ) goto error;
184 Pa_Terminate();
185 printf("Test finished.\n");
186 return err;
187 error:
188 Pa_Terminate();
189 fprintf( stderr, "An error occured while using the portaudio stream\n" );
190 fprintf( stderr, "Error number: %d\n", err );
191 fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) );
192 return err;
193 }