Debian Codegraph @

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

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 /** @file patest_read_record.c
2 @brief Record input into an array; Save array to a file; Playback recorded
3 data. Implemented using the blocking API (Pa_ReadStream(), Pa_WriteStream() )
4 @author Phil Burk http://www.softsynth.com
5 @author Ross Bencina [email protected]
6 */
7 /*
8 * $Id: patest_read_record.c,v 1.1 2006/06/10 21:30:56 dmazzoni Exp $
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
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include "portaudio.h"
42
43 /* #define SAMPLE_RATE (17932) // Test failure to open with this value. */
44 #define SAMPLE_RATE (44100)
45 #define FRAMES_PER_BUFFER (1024)
46 #define NUM_SECONDS (5)
47 #define NUM_CHANNELS (2)
48 /* #define DITHER_FLAG (paDitherOff) */
49 #define DITHER_FLAG (0) /**/
50
51 /* Select sample format. */
52 #if 1
53 #define PA_SAMPLE_TYPE paFloat32
54 typedef float SAMPLE;
55 #define SAMPLE_SILENCE (0.0f)
56 #define PRINTF_S_FORMAT "%.8f"
57 #elif 1
58 #define PA_SAMPLE_TYPE paInt16
59 typedef short SAMPLE;
60 #define SAMPLE_SILENCE (0)
61 #define PRINTF_S_FORMAT "%d"
62 #elif 0
63 #define PA_SAMPLE_TYPE paInt8
64 typedef char SAMPLE;
65 #define SAMPLE_SILENCE (0)
66 #define PRINTF_S_FORMAT "%d"
67 #else
68 #define PA_SAMPLE_TYPE paUInt8
69 typedef unsigned char SAMPLE;
70 #define SAMPLE_SILENCE (128)
71 #define PRINTF_S_FORMAT "%d"
72 #endif
73
74
75 /*******************************************************************/
76 int main(void);
77 int main(void)
78 {
79 PaStreamParameters inputParameters, outputParameters;
80 PaStream *stream;
81 PaError err;
82 SAMPLE *recordedSamples;
83 int i;
84 int totalFrames;
85 int numSamples;
86 int numBytes;
87 SAMPLE max, average, val;
88
89
90 printf("patest_read_record.c\n"); fflush(stdout);
91
92 totalFrames = NUM_SECONDS * SAMPLE_RATE; /* Record for a few seconds. */
93 numSamples = totalFrames * NUM_CHANNELS;
94
95 numBytes = numSamples * sizeof(SAMPLE);
96 recordedSamples = (SAMPLE *) malloc( numBytes );
97 if( recordedSamples == NULL )
98 {
99 printf("Could not allocate record array.\n");
100 exit(1);
101 }
102 for( i=0; i<numSamples; i++ ) recordedSamples[i] = 0;
103
104 err = Pa_Initialize();
105 if( err != paNoError ) goto error;
106
107 inputParameters.device = Pa_GetDefaultInputDevice(); /* default input device */
108 inputParameters.channelCount = NUM_CHANNELS;
109 inputParameters.sampleFormat = PA_SAMPLE_TYPE;
110 inputParameters.suggestedLatency = Pa_GetDeviceInfo( inputParameters.device )->defaultLowInputLatency;
111 inputParameters.hostApiSpecificStreamInfo = NULL;
112
113 /* Record some audio. -------------------------------------------- */
114 err = Pa_OpenStream(
115 &stream,
116 &inputParameters,
117 NULL, /* &outputParameters, */
118 SAMPLE_RATE,
119 FRAMES_PER_BUFFER,
120 paClipOff, /* we won't output out of range samples so don't bother clipping them */
121 NULL, /* no callback, use blocking API */
122 NULL ); /* no callback, so no callback userData */
123 if( err != paNoError ) goto error;
124
125 err = Pa_StartStream( stream );
126 if( err != paNoError ) goto error;
127 printf("Now recording!!\n"); fflush(stdout);
128
129 err = Pa_ReadStream( stream, recordedSamples, totalFrames );
130 if( err != paNoError ) goto error;
131
132 err = Pa_CloseStream( stream );
133 if( err != paNoError ) goto error;
134
135 /* Measure maximum peak amplitude. */
136 max = 0;
137 average = 0;
138 for( i=0; i<numSamples; i++ )
139 {
140 val = recordedSamples[i];
141 if( val < 0 ) val = -val; /* ABS */
142 if( val > max )
143 {
144 max = val;
145 }
146 average += val;
147 }
148
149 average = average / numSamples;
150
151 printf("Sample max amplitude = "PRINTF_S_FORMAT"\n", max );
152 printf("Sample average = "PRINTF_S_FORMAT"\n", average );
153 /* Was as below. Better choose at compile time because this
154 keeps generating compiler-warnings:
155 if( PA_SAMPLE_TYPE == paFloat32 )
156 {
157 printf("sample max amplitude = %f\n", max );
158 printf("sample average = %f\n", average );
159 }
160 else
161 {
162 printf("sample max amplitude = %d\n", max );
163 printf("sample average = %d\n", average );
164 }
165 */
166 /* Write recorded data to a file. */
167 #if 0
168 {
169 FILE *fid;
170 fid = fopen("recorded.raw", "wb");
171 if( fid == NULL )
172 {
173 printf("Could not open file.");
174 }
175 else
176 {
177 fwrite( recordedSamples, NUM_CHANNELS * sizeof(SAMPLE), totalFrames, fid );
178 fclose( fid );
179 printf("Wrote data to 'recorded.raw'\n");
180 }
181 }
182 #endif
183
184 /* Playback recorded data. -------------------------------------------- */
185
186 outputParameters.device = Pa_GetDefaultOutputDevice(); /* default output device */
187 outputParameters.channelCount = NUM_CHANNELS;
188 outputParameters.sampleFormat = PA_SAMPLE_TYPE;
189 outputParameters.suggestedLatency = Pa_GetDeviceInfo( outputParameters.device )->defaultLowOutputLatency;
190 outputParameters.hostApiSpecificStreamInfo = NULL;
191
192 printf("Begin playback.\n"); fflush(stdout);
193 err = Pa_OpenStream(
194 &stream,
195 NULL, /* no input */
196 &outputParameters,
197 SAMPLE_RATE,
198 FRAMES_PER_BUFFER,
199 paClipOff, /* we won't output out of range samples so don't bother clipping them */
200 NULL, /* no callback, use blocking API */
201 NULL ); /* no callback, so no callback userData */
202 if( err != paNoError ) goto error;
203
204 if( stream )
205 {
206 err = Pa_StartStream( stream );
207 if( err != paNoError ) goto error;
208 printf("Waiting for playback to finish.\n"); fflush(stdout);
209
210 err = Pa_WriteStream( stream, recordedSamples, totalFrames );
211 if( err != paNoError ) goto error;
212
213 err = Pa_CloseStream( stream );
214 if( err != paNoError ) goto error;
215 printf("Done.\n"); fflush(stdout);
216 }
217 free( recordedSamples );
218
219 Pa_Terminate();
220 return 0;
221
222 error:
223 Pa_Terminate();
•224 fprintf( stderr, "An error occurred while using the portaudio stream\n" );
225 fprintf( stderr, "Error number: %d\n", err );
226 fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) );
227 return -1;
228 }
229