Debian Codegraph @

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

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 /** @file patest_record.c
2 @brief Record input into an array; Save array to a file; Playback recorded data.
3 @author Phil Burk http://www.softsynth.com
4 */
5 /*
6 * $Id: patest_record.c,v 1.1 2006/06/10 21:30:56 dmazzoni Exp $
7 *
8 * This program uses the PortAudio Portable Audio Library.
9 * For more information see: http://www.portaudio.com
10 * Copyright (c) 1999-2000 Ross Bencina and Phil Burk
11 *
12 * Permission is hereby granted, free of charge, to any person obtaining
13 * a copy of this software and associated documentation files
14 * (the "Software"), to deal in the Software without restriction,
15 * including without limitation the rights to use, copy, modify, merge,
16 * publish, distribute, sublicense, and/or sell copies of the Software,
17 * and to permit persons to whom the Software is furnished to do so,
18 * subject to the following conditions:
19 *
20 * The above copyright notice and this permission notice shall be
21 * included in all copies or substantial portions of the Software.
22 *
23 * Any person wishing to distribute modifications to the Software is
24 * requested to send the modifications to the original developer so that
25 * they can be incorporated into the canonical version.
26 *
27 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
28 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
29 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
30 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
31 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
32 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
33 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 *
35 */
36
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include "portaudio.h"
40
41 /* #define SAMPLE_RATE (17932) // Test failure to open with this value. */
42 #define SAMPLE_RATE (44100)
43 #define FRAMES_PER_BUFFER (1024)
44 #define NUM_SECONDS (5)
45 #define NUM_CHANNELS (2)
46 /* #define DITHER_FLAG (paDitherOff) */
47 #define DITHER_FLAG (0) /**/
48
49 /* Select sample format. */
50 #if 1
51 #define PA_SAMPLE_TYPE paFloat32
52 typedef float SAMPLE;
53 #define SAMPLE_SILENCE (0.0f)
54 #define PRINTF_S_FORMAT "%.8f"
55 #elif 1
56 #define PA_SAMPLE_TYPE paInt16
57 typedef short SAMPLE;
58 #define SAMPLE_SILENCE (0)
59 #define PRINTF_S_FORMAT "%d"
60 #elif 0
61 #define PA_SAMPLE_TYPE paInt8
62 typedef char SAMPLE;
63 #define SAMPLE_SILENCE (0)
64 #define PRINTF_S_FORMAT "%d"
65 #else
66 #define PA_SAMPLE_TYPE paUInt8
67 typedef unsigned char SAMPLE;
68 #define SAMPLE_SILENCE (128)
69 #define PRINTF_S_FORMAT "%d"
70 #endif
71
72 typedef struct
73 {
74 int frameIndex; /* Index into sample array. */
75 int maxFrameIndex;
76 SAMPLE *recordedSamples;
77 }
78 paTestData;
79
80 /* This routine will be called by the PortAudio engine when audio is needed.
81 ** It may be 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 recordCallback( const void *inputBuffer, void *outputBuffer,
85 unsigned long framesPerBuffer,
86 const PaStreamCallbackTimeInfo* timeInfo,
87 PaStreamCallbackFlags statusFlags,
88 void *userData )
89 {
90 paTestData *data = (paTestData*)userData;
91 const SAMPLE *rptr = (const SAMPLE*)inputBuffer;
92 SAMPLE *wptr = &data->recordedSamples[data->frameIndex * NUM_CHANNELS];
93 long framesToCalc;
94 long i;
95 int finished;
96 unsigned long framesLeft = data->maxFrameIndex - data->frameIndex;
97
98 (void) outputBuffer; /* Prevent unused variable warnings. */
99 (void) timeInfo;
100 (void) statusFlags;
101 (void) userData;
102
103 if( framesLeft < framesPerBuffer )
104 {
105 framesToCalc = framesLeft;
106 finished = paComplete;
107 }
108 else
109 {
110 framesToCalc = framesPerBuffer;
111 finished = paContinue;
112 }
113
114 if( inputBuffer == NULL )
115 {
116 for( i=0; i<framesToCalc; i++ )
117 {
118 *wptr++ = SAMPLE_SILENCE; /* left */
119 if( NUM_CHANNELS == 2 ) *wptr++ = SAMPLE_SILENCE; /* right */
120 }
121 }
122 else
123 {
124 for( i=0; i<framesToCalc; i++ )
125 {
126 *wptr++ = *rptr++; /* left */
127 if( NUM_CHANNELS == 2 ) *wptr++ = *rptr++; /* right */
128 }
129 }
130 data->frameIndex += framesToCalc;
131 return finished;
132 }
133
134 /* This routine will be called by the PortAudio engine when audio is needed.
135 ** It may be called at interrupt level on some machines so don't do anything
136 ** that could mess up the system like calling malloc() or free().
137 */
138 static int playCallback( const void *inputBuffer, void *outputBuffer,
139 unsigned long framesPerBuffer,
140 const PaStreamCallbackTimeInfo* timeInfo,
141 PaStreamCallbackFlags statusFlags,
142 void *userData )
143 {
144 paTestData *data = (paTestData*)userData;
145 SAMPLE *rptr = &data->recordedSamples[data->frameIndex * NUM_CHANNELS];
146 SAMPLE *wptr = (SAMPLE*)outputBuffer;
147 unsigned int i;
148 int finished;
149 unsigned int framesLeft = data->maxFrameIndex - data->frameIndex;
150
151 (void) inputBuffer; /* Prevent unused variable warnings. */
152 (void) timeInfo;
153 (void) statusFlags;
154 (void) userData;
155
156 if( framesLeft < framesPerBuffer )
157 {
158 /* final buffer... */
159 for( i=0; i<framesLeft; i++ )
160 {
161 *wptr++ = *rptr++; /* left */
162 if( NUM_CHANNELS == 2 ) *wptr++ = *rptr++; /* right */
163 }
164 for( ; i<framesPerBuffer; i++ )
165 {
166 *wptr++ = 0; /* left */
167 if( NUM_CHANNELS == 2 ) *wptr++ = 0; /* right */
168 }
169 data->frameIndex += framesLeft;
170 finished = paComplete;
171 }
172 else
173 {
174 for( i=0; i<framesPerBuffer; i++ )
175 {
176 *wptr++ = *rptr++; /* left */
177 if( NUM_CHANNELS == 2 ) *wptr++ = *rptr++; /* right */
178 }
179 data->frameIndex += framesPerBuffer;
180 finished = paContinue;
181 }
182 return finished;
183 }
184
185 /*******************************************************************/
186 int main(void);
187 int main(void)
188 {
189 PaStreamParameters inputParameters,
190 outputParameters;
191 PaStream* stream;
192 PaError err = paNoError;
193 paTestData data;
194 int i;
195 int totalFrames;
196 int numSamples;
197 int numBytes;
198 SAMPLE max, val;
199 double average;
200
201 printf("patest_record.c\n"); fflush(stdout);
202
203 data.maxFrameIndex = totalFrames = NUM_SECONDS * SAMPLE_RATE; /* Record for a few seconds. */
204 data.frameIndex = 0;
205 numSamples = totalFrames * NUM_CHANNELS;
206 numBytes = numSamples * sizeof(SAMPLE);
207 data.recordedSamples = (SAMPLE *) malloc( numBytes ); /* From now on, recordedSamples is initialised. */
208 if( data.recordedSamples == NULL )
209 {
210 printf("Could not allocate record array.\n");
211 goto done;
212 }
213 for( i=0; i<numSamples; i++ ) data.recordedSamples[i] = 0;
214
215 err = Pa_Initialize();
216 if( err != paNoError ) goto done;
217
218 inputParameters.device = Pa_GetDefaultInputDevice(); /* default input device */
219 inputParameters.channelCount = 2; /* stereo input */
220 inputParameters.sampleFormat = PA_SAMPLE_TYPE;
221 inputParameters.suggestedLatency = Pa_GetDeviceInfo( inputParameters.device )->defaultLowInputLatency;
222 inputParameters.hostApiSpecificStreamInfo = NULL;
223
224 /* Record some audio. -------------------------------------------- */
225 err = Pa_OpenStream(
226 &stream,
227 &inputParameters,
228 NULL, /* &outputParameters, */
229 SAMPLE_RATE,
230 FRAMES_PER_BUFFER,
231 paClipOff, /* we won't output out of range samples so don't bother clipping them */
232 recordCallback,
233 &data );
234 if( err != paNoError ) goto done;
235
236 err = Pa_StartStream( stream );
237 if( err != paNoError ) goto done;
238 printf("Now recording!!\n"); fflush(stdout);
239
240 while( ( err = Pa_IsStreamActive( stream ) ) == 1 )
241 {
242 Pa_Sleep(1000);
243 printf("index = %d\n", data.frameIndex ); fflush(stdout);
244 }
245 if( err < 0 ) goto done;
246
247 err = Pa_CloseStream( stream );
248 if( err != paNoError ) goto done;
249
250 /* Measure maximum peak amplitude. */
251 max = 0;
252 average = 0.0;
253 for( i=0; i<numSamples; i++ )
254 {
255 val = data.recordedSamples[i];
256 if( val < 0 ) val = -val; /* ABS */
257 if( val > max )
258 {
259 max = val;
260 }
261 average += val;
262 }
263
264 average = average / (double)numSamples;
265
266 printf("sample max amplitude = "PRINTF_S_FORMAT"\n", max );
267 printf("sample average = %lf\n", average );
268
269 /* Write recorded data to a file. */
270 #if 0
271 {
272 FILE *fid;
273 fid = fopen("recorded.raw", "wb");
274 if( fid == NULL )
275 {
276 printf("Could not open file.");
277 }
278 else
279 {
280 fwrite( data.recordedSamples, NUM_CHANNELS * sizeof(SAMPLE), totalFrames, fid );
281 fclose( fid );
282 printf("Wrote data to 'recorded.raw'\n");
283 }
284 }
285 #endif
286
287 /* Playback recorded data. -------------------------------------------- */
288 data.frameIndex = 0;
289
290 outputParameters.device = Pa_GetDefaultOutputDevice(); /* default output device */
291 outputParameters.channelCount = 2; /* stereo output */
292 outputParameters.sampleFormat = PA_SAMPLE_TYPE;
293 outputParameters.suggestedLatency = Pa_GetDeviceInfo( outputParameters.device )->defaultLowOutputLatency;
294 outputParameters.hostApiSpecificStreamInfo = NULL;
295
296 printf("Begin playback.\n"); fflush(stdout);
297 err = Pa_OpenStream(
298 &stream,
299 NULL, /* no input */
300 &outputParameters,
301 SAMPLE_RATE,
302 FRAMES_PER_BUFFER,
303 paClipOff, /* we won't output out of range samples so don't bother clipping them */
304 playCallback,
305 &data );
306 if( err != paNoError ) goto done;
307
308 if( stream )
309 {
310 err = Pa_StartStream( stream );
311 if( err != paNoError ) goto done;
312
313 printf("Waiting for playback to finish.\n"); fflush(stdout);
314
315 while( ( err = Pa_IsStreamActive( stream ) ) == 1 ) Pa_Sleep(100);
316 if( err < 0 ) goto done;
317
318 err = Pa_CloseStream( stream );
319 if( err != paNoError ) goto done;
320
321 printf("Done.\n"); fflush(stdout);
322 }
323
324 done:
325 Pa_Terminate();
326 if( data.recordedSamples ) /* Sure it is NULL or valid. */
327 free( data.recordedSamples );
328 if( err != paNoError )
329 {
•330 fprintf( stderr, "An error occurred while using the portaudio stream\n" );
331 fprintf( stderr, "Error number: %d\n", err );
332 fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) );
333 err = 1; /* Always return 0 or 1, but no other return codes. */
334 }
335 return err;
336 }
337