cannam@89
|
1 /** @file patest_read_record.c
|
cannam@89
|
2 @ingroup test_src
|
cannam@89
|
3 @brief Record input into an array; Save array to a file; Playback recorded
|
cannam@89
|
4 data. Implemented using the blocking API (Pa_ReadStream(), Pa_WriteStream() )
|
cannam@89
|
5 @author Phil Burk http://www.softsynth.com
|
cannam@89
|
6 @author Ross Bencina rossb@audiomulch.com
|
cannam@89
|
7 */
|
cannam@89
|
8 /*
|
cannam@89
|
9 * $Id: patest_read_record.c 1368 2008-03-01 00:38:27Z rossb $
|
cannam@89
|
10 *
|
cannam@89
|
11 * This program uses the PortAudio Portable Audio Library.
|
cannam@89
|
12 * For more information see: http://www.portaudio.com
|
cannam@89
|
13 * Copyright (c) 1999-2000 Ross Bencina and Phil Burk
|
cannam@89
|
14 *
|
cannam@89
|
15 * Permission is hereby granted, free of charge, to any person obtaining
|
cannam@89
|
16 * a copy of this software and associated documentation files
|
cannam@89
|
17 * (the "Software"), to deal in the Software without restriction,
|
cannam@89
|
18 * including without limitation the rights to use, copy, modify, merge,
|
cannam@89
|
19 * publish, distribute, sublicense, and/or sell copies of the Software,
|
cannam@89
|
20 * and to permit persons to whom the Software is furnished to do so,
|
cannam@89
|
21 * subject to the following conditions:
|
cannam@89
|
22 *
|
cannam@89
|
23 * The above copyright notice and this permission notice shall be
|
cannam@89
|
24 * included in all copies or substantial portions of the Software.
|
cannam@89
|
25 *
|
cannam@89
|
26 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
cannam@89
|
27 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
cannam@89
|
28 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
cannam@89
|
29 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
|
cannam@89
|
30 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
|
cannam@89
|
31 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
cannam@89
|
32 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
cannam@89
|
33 */
|
cannam@89
|
34
|
cannam@89
|
35 /*
|
cannam@89
|
36 * The text above constitutes the entire PortAudio license; however,
|
cannam@89
|
37 * the PortAudio community also makes the following non-binding requests:
|
cannam@89
|
38 *
|
cannam@89
|
39 * Any person wishing to distribute modifications to the Software is
|
cannam@89
|
40 * requested to send the modifications to the original developer so that
|
cannam@89
|
41 * they can be incorporated into the canonical version. It is also
|
cannam@89
|
42 * requested that these non-binding requests be included along with the
|
cannam@89
|
43 * license above.
|
cannam@89
|
44 */
|
cannam@89
|
45
|
cannam@89
|
46 #include <stdio.h>
|
cannam@89
|
47 #include <stdlib.h>
|
cannam@89
|
48 #include "portaudio.h"
|
cannam@89
|
49
|
cannam@89
|
50 /* #define SAMPLE_RATE (17932) // Test failure to open with this value. */
|
cannam@89
|
51 #define SAMPLE_RATE (44100)
|
cannam@89
|
52 #define FRAMES_PER_BUFFER (1024)
|
cannam@89
|
53 #define NUM_SECONDS (5)
|
cannam@89
|
54 #define NUM_CHANNELS (2)
|
cannam@89
|
55 /* #define DITHER_FLAG (paDitherOff) */
|
cannam@89
|
56 #define DITHER_FLAG (0) /**/
|
cannam@89
|
57
|
cannam@89
|
58 /* Select sample format. */
|
cannam@89
|
59 #if 1
|
cannam@89
|
60 #define PA_SAMPLE_TYPE paFloat32
|
cannam@89
|
61 typedef float SAMPLE;
|
cannam@89
|
62 #define SAMPLE_SILENCE (0.0f)
|
cannam@89
|
63 #define PRINTF_S_FORMAT "%.8f"
|
cannam@89
|
64 #elif 1
|
cannam@89
|
65 #define PA_SAMPLE_TYPE paInt16
|
cannam@89
|
66 typedef short SAMPLE;
|
cannam@89
|
67 #define SAMPLE_SILENCE (0)
|
cannam@89
|
68 #define PRINTF_S_FORMAT "%d"
|
cannam@89
|
69 #elif 0
|
cannam@89
|
70 #define PA_SAMPLE_TYPE paInt8
|
cannam@89
|
71 typedef char SAMPLE;
|
cannam@89
|
72 #define SAMPLE_SILENCE (0)
|
cannam@89
|
73 #define PRINTF_S_FORMAT "%d"
|
cannam@89
|
74 #else
|
cannam@89
|
75 #define PA_SAMPLE_TYPE paUInt8
|
cannam@89
|
76 typedef unsigned char SAMPLE;
|
cannam@89
|
77 #define SAMPLE_SILENCE (128)
|
cannam@89
|
78 #define PRINTF_S_FORMAT "%d"
|
cannam@89
|
79 #endif
|
cannam@89
|
80
|
cannam@89
|
81
|
cannam@89
|
82 /*******************************************************************/
|
cannam@89
|
83 int main(void);
|
cannam@89
|
84 int main(void)
|
cannam@89
|
85 {
|
cannam@89
|
86 PaStreamParameters inputParameters, outputParameters;
|
cannam@89
|
87 PaStream *stream;
|
cannam@89
|
88 PaError err;
|
cannam@89
|
89 SAMPLE *recordedSamples;
|
cannam@89
|
90 int i;
|
cannam@89
|
91 int totalFrames;
|
cannam@89
|
92 int numSamples;
|
cannam@89
|
93 int numBytes;
|
cannam@89
|
94 SAMPLE max, average, val;
|
cannam@89
|
95
|
cannam@89
|
96
|
cannam@89
|
97 printf("patest_read_record.c\n"); fflush(stdout);
|
cannam@89
|
98
|
cannam@89
|
99 totalFrames = NUM_SECONDS * SAMPLE_RATE; /* Record for a few seconds. */
|
cannam@89
|
100 numSamples = totalFrames * NUM_CHANNELS;
|
cannam@89
|
101
|
cannam@89
|
102 numBytes = numSamples * sizeof(SAMPLE);
|
cannam@89
|
103 recordedSamples = (SAMPLE *) malloc( numBytes );
|
cannam@89
|
104 if( recordedSamples == NULL )
|
cannam@89
|
105 {
|
cannam@89
|
106 printf("Could not allocate record array.\n");
|
cannam@89
|
107 exit(1);
|
cannam@89
|
108 }
|
cannam@89
|
109 for( i=0; i<numSamples; i++ ) recordedSamples[i] = 0;
|
cannam@89
|
110
|
cannam@89
|
111 err = Pa_Initialize();
|
cannam@89
|
112 if( err != paNoError ) goto error;
|
cannam@89
|
113
|
cannam@89
|
114 inputParameters.device = Pa_GetDefaultInputDevice(); /* default input device */
|
cannam@89
|
115 if (inputParameters.device == paNoDevice) {
|
cannam@89
|
116 fprintf(stderr,"Error: No default input device.\n");
|
cannam@89
|
117 goto error;
|
cannam@89
|
118 }
|
cannam@89
|
119 inputParameters.channelCount = NUM_CHANNELS;
|
cannam@89
|
120 inputParameters.sampleFormat = PA_SAMPLE_TYPE;
|
cannam@89
|
121 inputParameters.suggestedLatency = Pa_GetDeviceInfo( inputParameters.device )->defaultLowInputLatency;
|
cannam@89
|
122 inputParameters.hostApiSpecificStreamInfo = NULL;
|
cannam@89
|
123
|
cannam@89
|
124 /* Record some audio. -------------------------------------------- */
|
cannam@89
|
125 err = Pa_OpenStream(
|
cannam@89
|
126 &stream,
|
cannam@89
|
127 &inputParameters,
|
cannam@89
|
128 NULL, /* &outputParameters, */
|
cannam@89
|
129 SAMPLE_RATE,
|
cannam@89
|
130 FRAMES_PER_BUFFER,
|
cannam@89
|
131 paClipOff, /* we won't output out of range samples so don't bother clipping them */
|
cannam@89
|
132 NULL, /* no callback, use blocking API */
|
cannam@89
|
133 NULL ); /* no callback, so no callback userData */
|
cannam@89
|
134 if( err != paNoError ) goto error;
|
cannam@89
|
135
|
cannam@89
|
136 err = Pa_StartStream( stream );
|
cannam@89
|
137 if( err != paNoError ) goto error;
|
cannam@89
|
138 printf("Now recording!!\n"); fflush(stdout);
|
cannam@89
|
139
|
cannam@89
|
140 err = Pa_ReadStream( stream, recordedSamples, totalFrames );
|
cannam@89
|
141 if( err != paNoError ) goto error;
|
cannam@89
|
142
|
cannam@89
|
143 err = Pa_CloseStream( stream );
|
cannam@89
|
144 if( err != paNoError ) goto error;
|
cannam@89
|
145
|
cannam@89
|
146 /* Measure maximum peak amplitude. */
|
cannam@89
|
147 max = 0;
|
cannam@89
|
148 average = 0;
|
cannam@89
|
149 for( i=0; i<numSamples; i++ )
|
cannam@89
|
150 {
|
cannam@89
|
151 val = recordedSamples[i];
|
cannam@89
|
152 if( val < 0 ) val = -val; /* ABS */
|
cannam@89
|
153 if( val > max )
|
cannam@89
|
154 {
|
cannam@89
|
155 max = val;
|
cannam@89
|
156 }
|
cannam@89
|
157 average += val;
|
cannam@89
|
158 }
|
cannam@89
|
159
|
cannam@89
|
160 average = average / numSamples;
|
cannam@89
|
161
|
cannam@89
|
162 printf("Sample max amplitude = "PRINTF_S_FORMAT"\n", max );
|
cannam@89
|
163 printf("Sample average = "PRINTF_S_FORMAT"\n", average );
|
cannam@89
|
164 /* Was as below. Better choose at compile time because this
|
cannam@89
|
165 keeps generating compiler-warnings:
|
cannam@89
|
166 if( PA_SAMPLE_TYPE == paFloat32 )
|
cannam@89
|
167 {
|
cannam@89
|
168 printf("sample max amplitude = %f\n", max );
|
cannam@89
|
169 printf("sample average = %f\n", average );
|
cannam@89
|
170 }
|
cannam@89
|
171 else
|
cannam@89
|
172 {
|
cannam@89
|
173 printf("sample max amplitude = %d\n", max );
|
cannam@89
|
174 printf("sample average = %d\n", average );
|
cannam@89
|
175 }
|
cannam@89
|
176 */
|
cannam@89
|
177 /* Write recorded data to a file. */
|
cannam@89
|
178 #if 0
|
cannam@89
|
179 {
|
cannam@89
|
180 FILE *fid;
|
cannam@89
|
181 fid = fopen("recorded.raw", "wb");
|
cannam@89
|
182 if( fid == NULL )
|
cannam@89
|
183 {
|
cannam@89
|
184 printf("Could not open file.");
|
cannam@89
|
185 }
|
cannam@89
|
186 else
|
cannam@89
|
187 {
|
cannam@89
|
188 fwrite( recordedSamples, NUM_CHANNELS * sizeof(SAMPLE), totalFrames, fid );
|
cannam@89
|
189 fclose( fid );
|
cannam@89
|
190 printf("Wrote data to 'recorded.raw'\n");
|
cannam@89
|
191 }
|
cannam@89
|
192 }
|
cannam@89
|
193 #endif
|
cannam@89
|
194
|
cannam@89
|
195 /* Playback recorded data. -------------------------------------------- */
|
cannam@89
|
196
|
cannam@89
|
197 outputParameters.device = Pa_GetDefaultOutputDevice(); /* default output device */
|
cannam@89
|
198 if (outputParameters.device == paNoDevice) {
|
cannam@89
|
199 fprintf(stderr,"Error: No default output device.\n");
|
cannam@89
|
200 goto error;
|
cannam@89
|
201 }
|
cannam@89
|
202 outputParameters.channelCount = NUM_CHANNELS;
|
cannam@89
|
203 outputParameters.sampleFormat = PA_SAMPLE_TYPE;
|
cannam@89
|
204 outputParameters.suggestedLatency = Pa_GetDeviceInfo( outputParameters.device )->defaultLowOutputLatency;
|
cannam@89
|
205 outputParameters.hostApiSpecificStreamInfo = NULL;
|
cannam@89
|
206
|
cannam@89
|
207 printf("Begin playback.\n"); fflush(stdout);
|
cannam@89
|
208 err = Pa_OpenStream(
|
cannam@89
|
209 &stream,
|
cannam@89
|
210 NULL, /* no input */
|
cannam@89
|
211 &outputParameters,
|
cannam@89
|
212 SAMPLE_RATE,
|
cannam@89
|
213 FRAMES_PER_BUFFER,
|
cannam@89
|
214 paClipOff, /* we won't output out of range samples so don't bother clipping them */
|
cannam@89
|
215 NULL, /* no callback, use blocking API */
|
cannam@89
|
216 NULL ); /* no callback, so no callback userData */
|
cannam@89
|
217 if( err != paNoError ) goto error;
|
cannam@89
|
218
|
cannam@89
|
219 if( stream )
|
cannam@89
|
220 {
|
cannam@89
|
221 err = Pa_StartStream( stream );
|
cannam@89
|
222 if( err != paNoError ) goto error;
|
cannam@89
|
223 printf("Waiting for playback to finish.\n"); fflush(stdout);
|
cannam@89
|
224
|
cannam@89
|
225 err = Pa_WriteStream( stream, recordedSamples, totalFrames );
|
cannam@89
|
226 if( err != paNoError ) goto error;
|
cannam@89
|
227
|
cannam@89
|
228 err = Pa_CloseStream( stream );
|
cannam@89
|
229 if( err != paNoError ) goto error;
|
cannam@89
|
230 printf("Done.\n"); fflush(stdout);
|
cannam@89
|
231 }
|
cannam@89
|
232 free( recordedSamples );
|
cannam@89
|
233
|
cannam@89
|
234 Pa_Terminate();
|
cannam@89
|
235 return 0;
|
cannam@89
|
236
|
cannam@89
|
237 error:
|
cannam@89
|
238 Pa_Terminate();
|
cannam@89
|
239 fprintf( stderr, "An error occured while using the portaudio stream\n" );
|
cannam@89
|
240 fprintf( stderr, "Error number: %d\n", err );
|
cannam@89
|
241 fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) );
|
cannam@89
|
242 return -1;
|
cannam@89
|
243 }
|
cannam@89
|
244
|