annotate src/portaudio/test/patest_read_record.c @ 20:ab7c38c4c577

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