comparison src/portaudio/test/patest_callbackstop.c @ 4:e13257ea84a4

Add bzip2, zlib, liblo, portaudio sources
author Chris Cannam
date Wed, 20 Mar 2013 13:59:52 +0000
parents
children
comparison
equal deleted inserted replaced
3:6c505a35919a 4:e13257ea84a4
1 /** @file patest_callbackstop.c
2 @ingroup test_src
3 @brief Test the paComplete callback result code.
4 @author Ross Bencina <rossb@audiomulch.com>
5 */
6 /*
7 * $Id: patest_callbackstop.c 1368 2008-03-01 00:38:27Z rossb $
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 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
27 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
28 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
29 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 */
32
33 /*
34 * The text above constitutes the entire PortAudio license; however,
35 * the PortAudio community also makes the following non-binding requests:
36 *
37 * Any person wishing to distribute modifications to the Software is
38 * requested to send the modifications to the original developer so that
39 * they can be incorporated into the canonical version. It is also
40 * requested that these non-binding requests be included along with the
41 * license above.
42 */
43
44 #include <stdio.h>
45 #include <math.h>
46 #include "portaudio.h"
47
48 #define NUM_SECONDS (5)
49 #define NUM_LOOPS (4)
50 #define SAMPLE_RATE (44100)
51 #define FRAMES_PER_BUFFER (67)
52
53 #ifndef M_PI
54 #define M_PI (3.14159265)
55 #endif
56
57 #define TABLE_SIZE (200)
58 typedef struct
59 {
60 float sine[TABLE_SIZE];
61 int phase;
62 unsigned long generatedFramesCount;
63 volatile int callbackReturnedPaComplete;
64 volatile int callbackInvokedAfterReturningPaComplete;
65 char message[100];
66 }
67 TestData;
68
69 /*
70 This routine will be called by the PortAudio stream when audio is needed.
71 It may be called at interrupt level on some machines so don't do anything
72 that could mess up the system like calling malloc() or free().
73 */
74 static int TestCallback( const void *input, void *output,
75 unsigned long frameCount,
76 const PaStreamCallbackTimeInfo* timeInfo,
77 PaStreamCallbackFlags statusFlags,
78 void *userData )
79 {
80 TestData *data = (TestData*)userData;
81 float *out = (float*)output;
82 unsigned long i;
83 float x;
84
85 (void) input; /* Prevent unused variable warnings. */
86 (void) timeInfo;
87 (void) statusFlags;
88
89
90 if( data->callbackReturnedPaComplete )
91 data->callbackInvokedAfterReturningPaComplete = 1;
92
93 for( i=0; i<frameCount; i++ )
94 {
95 /* generate tone */
96
97 x = data->sine[ data->phase++ ];
98 if( data->phase >= TABLE_SIZE )
99 data->phase -= TABLE_SIZE;
100
101 *out++ = x; /* left */
102 *out++ = x; /* right */
103 }
104
105 data->generatedFramesCount += frameCount;
106 if( data->generatedFramesCount >= (NUM_SECONDS * SAMPLE_RATE) )
107 {
108 data->callbackReturnedPaComplete = 1;
109 return paComplete;
110 }
111 else
112 {
113 return paContinue;
114 }
115 }
116
117 /*
118 * This routine is called by portaudio when playback is done.
119 */
120 static void StreamFinished( void* userData )
121 {
122 TestData *data = (TestData *) userData;
123 printf( "Stream Completed: %s\n", data->message );
124 }
125
126
127 /*----------------------------------------------------------------------------*/
128 int main(void);
129 int main(void)
130 {
131 PaStreamParameters outputParameters;
132 PaStream *stream;
133 PaError err;
134 TestData data;
135 int i, j;
136
137
138 printf( "PortAudio Test: output sine wave. SR = %d, BufSize = %d\n",
139 SAMPLE_RATE, FRAMES_PER_BUFFER );
140
141 /* initialise sinusoidal wavetable */
142 for( i=0; i<TABLE_SIZE; i++ )
143 {
144 data.sine[i] = (float) sin( ((double)i/(double)TABLE_SIZE) * M_PI * 2. );
145 }
146
147 err = Pa_Initialize();
148 if( err != paNoError ) goto error;
149
150 outputParameters.device = Pa_GetDefaultOutputDevice();
151 if (outputParameters.device == paNoDevice) {
152 fprintf(stderr,"Error: No default output device.\n");
153 goto error;
154 }
155 outputParameters.channelCount = 2; /* stereo output */
156 outputParameters.sampleFormat = paFloat32; /* 32 bit floating point output */
157 outputParameters.suggestedLatency = Pa_GetDeviceInfo( outputParameters.device )->defaultLowOutputLatency;
158 outputParameters.hostApiSpecificStreamInfo = NULL;
159
160 err = Pa_OpenStream(
161 &stream,
162 NULL, /* no input */
163 &outputParameters,
164 SAMPLE_RATE,
165 FRAMES_PER_BUFFER,
166 paClipOff, /* output will be in-range, so no need to clip */
167 TestCallback,
168 &data );
169 if( err != paNoError ) goto error;
170
171 sprintf( data.message, "Loop: XX" );
172 err = Pa_SetStreamFinishedCallback( stream, &StreamFinished );
173 if( err != paNoError ) goto error;
174
175 printf("Repeating test %d times.\n", NUM_LOOPS );
176
177 for( i=0; i < NUM_LOOPS; ++i )
178 {
179 data.phase = 0;
180 data.generatedFramesCount = 0;
181 data.callbackReturnedPaComplete = 0;
182 data.callbackInvokedAfterReturningPaComplete = 0;
183 sprintf( data.message, "Loop: %d", i );
184
185 err = Pa_StartStream( stream );
186 if( err != paNoError ) goto error;
187
188 printf("Play for %d seconds.\n", NUM_SECONDS );
189
190 /* wait for the callback to complete generating NUM_SECONDS of tone */
191
192 do
193 {
194 Pa_Sleep( 500 );
195 }
196 while( !data.callbackReturnedPaComplete );
197
198 printf( "Callback returned paComplete.\n" );
199 printf( "Waiting for buffers to finish playing...\n" );
200
201 /* wait for stream to become inactive,
202 or for a timeout of approximately NUM_SECONDS
203 */
204
205 j = 0;
206 while( (err = Pa_IsStreamActive( stream )) == 1 && j < NUM_SECONDS * 2 )
207 {
208 printf(".\n" );
209 Pa_Sleep( 500 );
210 ++j;
211 }
212
213 if( err < 0 )
214 {
215 goto error;
216 }
217 else if( err == 1 )
218 {
219 printf( "TEST FAILED: Timed out waiting for buffers to finish playing.\n" );
220 }
221 else
222 {
223 printf("Buffers finished.\n" );
224 }
225
226 if( data.callbackInvokedAfterReturningPaComplete )
227 {
228 printf( "TEST FAILED: Callback was invoked after returning paComplete.\n" );
229 }
230
231
232 err = Pa_StopStream( stream );
233 if( err != paNoError ) goto error;
234
235 printf( "sleeping for 1 second...\n" );
236 Pa_Sleep( 1000 );
237 }
238
239 err = Pa_CloseStream( stream );
240 if( err != paNoError ) goto error;
241
242 Pa_Terminate();
243 printf("Test finished.\n");
244
245 return err;
246 error:
247 Pa_Terminate();
248 fprintf( stderr, "An error occured while using the portaudio stream\n" );
249 fprintf( stderr, "Error number: %d\n", err );
250 fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) );
251 return err;
252 }