Debian Codegraph @

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

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