comparison src/portaudio/test/patest_prime.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_prime.c
2 @ingroup test_src
3 @brief Test stream priming mode.
4 @author Ross Bencina http://www.audiomulch.com/~rossb
5 */
6
7 /*
8 * $Id: patest_prime.c 1371 2008-03-11 14:27:26Z bjornroche $
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 #include "pa_util.h"
49
50 #define NUM_BEEPS (3)
51 #define SAMPLE_RATE (44100)
52 #define SAMPLE_PERIOD (1.0/44100.0)
53 #define FRAMES_PER_BUFFER (256)
54 #define BEEP_DURATION (400)
55 #define IDLE_DURATION (SAMPLE_RATE*2) /* 2 seconds */
56 #define SLEEP_MSEC (50)
57
58 #define STATE_BKG_IDLE (0)
59 #define STATE_BKG_BEEPING (1)
60
61 typedef struct
62 {
63 float leftPhase;
64 float rightPhase;
65 int state;
66 int beepCountdown;
67 int idleCountdown;
68 }
69 paTestData;
70
71 static void InitializeTestData( paTestData *testData )
72 {
73 testData->leftPhase = 0;
74 testData->rightPhase = 0;
75 testData->state = STATE_BKG_BEEPING;
76 testData->beepCountdown = BEEP_DURATION;
77 testData->idleCountdown = IDLE_DURATION;
78 }
79
80 /* This routine will be called by the PortAudio engine when audio is needed.
81 ** It may called at interrupt level on some machines so don't do anything
82 ** that could mess up the system like calling malloc() or free().
83 */
84 static int patestCallback( const void *inputBuffer, void *outputBuffer,
85 unsigned long framesPerBuffer,
86 const PaStreamCallbackTimeInfo *timeInfo,
87 PaStreamCallbackFlags statusFlags, void *userData )
88 {
89 /* Cast data passed through stream to our structure. */
90 paTestData *data = (paTestData*)userData;
91 float *out = (float*)outputBuffer;
92 unsigned int i;
93 int result = paContinue;
94
95 /* supress unused parameter warnings */
96 (void) inputBuffer;
97 (void) timeInfo;
98 (void) statusFlags;
99
100 for( i=0; i<framesPerBuffer; i++ )
101 {
102 switch( data->state )
103 {
104 case STATE_BKG_IDLE:
105 *out++ = 0.0; /* left */
106 *out++ = 0.0; /* right */
107 --data->idleCountdown;
108
109 if( data->idleCountdown <= 0 ) result = paComplete;
110 break;
111
112 case STATE_BKG_BEEPING:
113 if( data->beepCountdown <= 0 )
114 {
115 data->state = STATE_BKG_IDLE;
116 *out++ = 0.0; /* left */
117 *out++ = 0.0; /* right */
118 }
119 else
120 {
121 /* Play sawtooth wave. */
122 *out++ = data->leftPhase; /* left */
123 *out++ = data->rightPhase; /* right */
124 /* Generate simple sawtooth phaser that ranges between -1.0 and 1.0. */
125 data->leftPhase += 0.01f;
126 /* When signal reaches top, drop back down. */
127 if( data->leftPhase >= 1.0f ) data->leftPhase -= 2.0f;
128 /* higher pitch so we can distinguish left and right. */
129 data->rightPhase += 0.03f;
130 if( data->rightPhase >= 1.0f ) data->rightPhase -= 2.0f;
131 }
132 --data->beepCountdown;
133 break;
134 }
135 }
136
137 return result;
138 }
139
140 /*******************************************************************/
141 static PaError DoTest( int flags )
142 {
143 PaStream *stream;
144 PaError err = paNoError;
145 paTestData data;
146 PaStreamParameters outputParameters;
147
148 InitializeTestData( &data );
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;
156 outputParameters.hostApiSpecificStreamInfo = NULL;
157 outputParameters.sampleFormat = paFloat32;
158 outputParameters.suggestedLatency = Pa_GetDeviceInfo( outputParameters.device )->defaultHighOutputLatency;
159
160 /* Open an audio I/O stream. */
161 err = Pa_OpenStream(
162 &stream,
163 NULL, /* no input */
164 &outputParameters,
165 SAMPLE_RATE,
166 FRAMES_PER_BUFFER, /* frames per buffer */
167 paClipOff | flags, /* we won't output out of range samples so don't bother clipping them */
168 patestCallback,
169 &data );
170 if( err != paNoError ) goto error;
171
172
173 err = Pa_StartStream( stream );
174 if( err != paNoError ) goto error;
175
176 printf("hear \"BEEP\"\n" );
177 fflush(stdout);
178
179 while( ( err = Pa_IsStreamActive( stream ) ) == 1 ) Pa_Sleep(SLEEP_MSEC);
180 if( err < 0 ) goto error;
181
182 err = Pa_StopStream( stream );
183 if( err != paNoError ) goto error;
184
185 err = Pa_CloseStream( stream );
186 if( err != paNoError ) goto error;
187
188 return err;
189 error:
190 return err;
191 }
192
193 /*******************************************************************/
194 int main(void);
195 int main(void)
196 {
197 PaError err = paNoError;
198 int i;
199
200 /* Initialize library before making any other calls. */
201 err = Pa_Initialize();
202 if( err != paNoError ) goto error;
203
204 printf("PortAudio Test: Testing stream playback with no priming.\n");
205 printf("PortAudio Test: you should see BEEP before you hear it.\n");
206 printf("BEEP %d times.\n", NUM_BEEPS );
207
208 for( i=0; i< NUM_BEEPS; ++i )
209 {
210 err = DoTest( 0 );
211 if( err != paNoError )
212 goto error;
213 }
214
215 printf("PortAudio Test: Testing stream playback with priming.\n");
216 printf("PortAudio Test: you should see BEEP around the same time you hear it.\n");
217 for( i=0; i< NUM_BEEPS; ++i )
218 {
219 err = DoTest( paPrimeOutputBuffersUsingStreamCallback );
220 if( err != paNoError )
221 goto error;
222 }
223
224 printf("Test finished.\n");
225
226 Pa_Terminate();
227 return err;
228 error:
229 Pa_Terminate();
230 fprintf( stderr, "An error occured while using the portaudio stream\n" );
231 fprintf( stderr, "Error number: %d\n", err );
232 fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) );
233 return err;
234 }