Debian Codegraph @

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

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 /*
2 * $Id: debug_record.c,v 1.1 2006/06/10 21:30:56 dmazzoni Exp $
3 * debug_record.c
4 * Record input into an array.
5 * Save array to a file.
6 * Based on patest_record.c but with various ugly debug hacks thrown in.
7 *
8 * Author: Phil Burk http://www.softsynth.com
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 * Any person wishing to distribute modifications to the Software is
26 * requested to send the modifications to the original developer so that
27 * they can be incorporated into the canonical version.
28 *
29 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
30 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
31 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
32 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
33 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
34 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
35 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
36 *
37 */
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <memory.h>
41 #include "portaudio.h"
42 #define SAMPLE_RATE (22050)
43 #define NUM_SECONDS (10)
44 #define SLEEP_DUR_MSEC (200)
45 #define FRAMES_PER_BUFFER (1<<10)
46 #define NUM_REC_BUFS (0)
47
48 #if 1
49 #define PA_SAMPLE_TYPE paFloat32
50 typedef float SAMPLE;
51 #else
52 #define PA_SAMPLE_TYPE paInt16
53 typedef short SAMPLE;
54 #endif
55
56 typedef struct
57 {
58 long frameIndex; /* Index into sample array. */
59 long maxFrameIndex;
60 long samplesPerFrame;
61 long numSamples;
62 SAMPLE *recordedSamples;
63 }
64 paTestData;
65 /* This routine will be called by the PortAudio engine when audio is needed.
66 ** It may be called at interrupt level on some machines so don't do anything
67 ** that could mess up the system like calling malloc() or free().
68 */
69 static int recordCallback( void *inputBuffer, void *outputBuffer,
70 unsigned long framesPerBuffer,
71 PaTimestamp outTime, void *userData )
72 {
73 paTestData *data = (paTestData*)userData;
74 SAMPLE *rptr = (SAMPLE*)inputBuffer;
75 SAMPLE *wptr = &data->recordedSamples[data->frameIndex * data->samplesPerFrame];
76 long framesToCalc;
77 unsigned long i;
78 int finished;
79 unsigned long framesLeft = data->maxFrameIndex - data->frameIndex;
80
81 (void) outputBuffer; /* Prevent unused variable warnings. */
82 (void) outTime;
83
84 if( framesLeft < framesPerBuffer )
85 {
86 framesToCalc = framesLeft;
87 finished = 1;
88 }
89 else
90 {
91 framesToCalc = framesPerBuffer;
92 finished = 0;
93 }
94 if( inputBuffer == NULL )
95 {
96 for( i=0; i<framesToCalc; i++ )
97 {
98 *wptr++ = 0; /* left */
99 *wptr++ = 0; /* right */
100 }
101 }
102 else
103 {
104 for( i=0; i<framesToCalc; i++ )
105 {
106 *wptr++ = *rptr++; /* left */
107 *wptr++ = *rptr++; /* right */
108 }
109 }
110 data->frameIndex += framesToCalc;
111 return finished;
112 }
113 /* This routine will be called by the PortAudio engine when audio is needed.
114 ** It may be called at interrupt level on some machines so don't do anything
115 ** that could mess up the system like calling malloc() or free().
116 */
117 static int playCallback( void *inputBuffer, void *outputBuffer,
118 unsigned long framesPerBuffer,
119 PaTimestamp outTime, void *userData )
120 {
121 paTestData *data = (paTestData*)userData;
122 SAMPLE *rptr = &data->recordedSamples[data->frameIndex * data->samplesPerFrame];
123 SAMPLE *wptr = (SAMPLE*)outputBuffer;
124 unsigned long i;
125 int finished;
126 unsigned int framesLeft = data->maxFrameIndex - data->frameIndex;
127 if( outputBuffer == NULL ) return 0;
128 (void) inputBuffer; /* Prevent unused variable warnings. */
129 (void) outTime;
130
131 if( framesLeft < framesPerBuffer )
132 {
133 /* final buffer... */
134 for( i=0; i<framesLeft; i++ )
135 {
136 *wptr++ = *rptr++; /* left */
137 *wptr++ = *rptr++; /* right */
138 }
139 for( ; i<framesPerBuffer; i++ )
140 {
141 *wptr++ = 0; /* left */
142 *wptr++ = 0; /* right */
143 }
144 data->frameIndex += framesLeft;
145 finished = 1;
146 }
147 else
148 {
149 for( i=0; i<framesPerBuffer; i++ )
150 {
151 *wptr++ = *rptr++; /* left */
152 *wptr++ = *rptr++; /* right */
153 }
154 data->frameIndex += framesPerBuffer;
155 finished = 0;
156 }
157 return finished;
158 }
159
160 /****************************************************************/
161 PaError TestRecording( paTestData *dataPtr )
162 {
163 PortAudioStream *stream;
164 PaError err;
165 int i;
166
167 /* Record some audio. */
168 err = Pa_OpenStream(
169 &stream,
170 Pa_GetDefaultInputDeviceID(),
171 dataPtr->samplesPerFrame, /* stereo input */
172 PA_SAMPLE_TYPE,
173 NULL,
174 paNoDevice,
175 0,
176 PA_SAMPLE_TYPE,
177 NULL,
178 SAMPLE_RATE,
179 FRAMES_PER_BUFFER, /* frames per buffer */
180 NUM_REC_BUFS, /* number of buffers, if zero then use default minimum */
181 paClipOff, /* we won't output out of range samples so don't bother clipping them */
182 recordCallback,
183 dataPtr );
184 if( err != paNoError ) goto error;
185 err = Pa_StartStream( stream );
186 if( err != paNoError ) goto error;
187
188 printf("Now recording!\n"); fflush(stdout);
189 for( i=0; i<(NUM_SECONDS*1000/SLEEP_DUR_MSEC); i++ )
190 {
191 if( Pa_StreamActive( stream ) <= 0)
192 {
193 printf("Stream inactive!\n");
194 break;
195 }
196 if( dataPtr->maxFrameIndex <= dataPtr->frameIndex )
197 {
198 printf("Buffer recording complete.\n");
199 break;
200 }
201 Pa_Sleep(100);
202 printf("index = %d\n", dataPtr->frameIndex ); fflush(stdout);
203 }
204
205 printf("Finished loop. Close stream.\n"); fflush(stdout);
206
207 err = Pa_CloseStream( stream );
208 if( err != paNoError ) goto error;
209
210 printf("Done.\n"); fflush(stdout);
211 {
212 SAMPLE max = 0;
213 SAMPLE posVal;
214 int i;
215 for( i=0; i<dataPtr->numSamples; i++ )
216 {
217 posVal = dataPtr->recordedSamples[i];
218 if( posVal < 0 ) posVal = -posVal;
219 if( posVal > max ) max = posVal;
220 }
221 printf("Largest recorded sample = %d\n", max );
222 }
223 /* Write recorded data to a file. */
224 #if 0
225 {
226 FILE *fid;
227 fid = fopen("recorded.raw", "wb");
228 if( fid == NULL )
229 {
230 printf("Could not open file.");
231 }
232 else
233 {
234 fwrite( dataPtr->recordedSamples, dataPtr->samplesPerFrame * sizeof(SAMPLE), totalFrames, fid );
235 fclose( fid );
236 printf("Wrote data to 'recorded.raw'\n");
237 }
238 }
239 #endif
240
241 error:
242 return err;
243 }
244
245 /****************************************************************/
246 PaError TestPlayback( paTestData *dataPtr )
247 {
248 PortAudioStream *stream;
249 PaError err;
250 int i;
251
252 /* Playback recorded data. */
253 dataPtr->frameIndex = 0;
254 printf("Begin playback.\n"); fflush(stdout);
255 err = Pa_OpenStream(
256 &stream,
257 paNoDevice,
258 0, /* NO input */
259 PA_SAMPLE_TYPE,
260 NULL,
261 Pa_GetDefaultOutputDeviceID(),
262 dataPtr->samplesPerFrame, /* stereo output */
263 PA_SAMPLE_TYPE,
264 NULL,
265 SAMPLE_RATE,
266 FRAMES_PER_BUFFER, /* frames per buffer */
267 0, /* number of buffers, if zero then use default minimum */
268 paClipOff, /* we won't output out of range samples so don't bother clipping them */
269 playCallback,
270 dataPtr );
271 if( err != paNoError ) goto error;
272 err = Pa_StartStream( stream );
273 if( err != paNoError ) goto error;
274 printf("Waiting for playback to finish.\n"); fflush(stdout);
275 for( i=0; i<(NUM_SECONDS*1000/SLEEP_DUR_MSEC); i++ )
276 {
277 Pa_Sleep(100);
278 printf("index = %d\n", dataPtr->frameIndex );
279 }
280 err = Pa_CloseStream( stream );
281 if( err != paNoError ) goto error;
282
283 error:
284 return err;
285 }
286 /*******************************************************************/
287 int main(void);
288 int main(void)
289 {
290 PaError err;
291 paTestData data;
292 long totalFrames;
293 long numBytes;
294 long i;
295 printf("patest_record.c\n"); fflush(stdout);
296
297 data.frameIndex = 0;
298 data.samplesPerFrame = 2;
299 data.maxFrameIndex = totalFrames = NUM_SECONDS*SAMPLE_RATE;
300
301 printf("totalFrames = %d\n", totalFrames ); fflush(stdout);
302 data.numSamples = totalFrames * data.samplesPerFrame;
303
304 numBytes = data.numSamples * sizeof(SAMPLE);
305 data.recordedSamples = (SAMPLE *) malloc( numBytes );
306 if( data.recordedSamples == NULL )
307 {
308 printf("Could not allocate record array.\n");
309 exit(1);
310 }
311 for( i=0; i<data.numSamples; i++ ) data.recordedSamples[i] = 0;
312
313 err = Pa_Initialize();
314 if( err != paNoError ) goto error;
315
316 for( i=0; i<2; i++ )
317 {
318 err = TestRecording( &data );
319 if( err != paNoError ) goto error;
320
321 err = TestPlayback( &data );
322 if( err != paNoError ) goto error;
323 }
324
325 free( data.recordedSamples );
326 Pa_Terminate();
327 return 0;
328
329 error:
330 Pa_Terminate();
•331 fprintf( stderr, "An error occurred while using the portaudio stream\n" );
332 fprintf( stderr, "Error number: %d\n", err );
333 fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) );
334 if( err == paHostError )
335 {
336 fprintf( stderr, "Host Error number: %d\n", Pa_GetHostError() );
337 }
338 return -1;
339 }