annotate src/portaudio_20140130/test/patest_wire.c @ 167:bd3cc4d1df30

Add FFTW 3.3.8 source, and a Linux build
author Chris Cannam <cannam@all-day-breakfast.com>
date Tue, 19 Nov 2019 14:52:55 +0000
parents e3d5853d5918
children
rev   line source
cannam@124 1 /** @file patest_wire.c
cannam@124 2 @ingroup test_src
cannam@124 3 @brief Pass input directly to output.
cannam@124 4
cannam@124 5 Note that some HW devices, for example many ISA audio cards
cannam@124 6 on PCs, do NOT support full duplex! For a PC, you normally need
cannam@124 7 a PCI based audio card such as the SBLive.
cannam@124 8
cannam@124 9 @author Phil Burk http://www.softsynth.com
cannam@124 10
cannam@124 11 While adapting to V19-API, I excluded configs with framesPerCallback=0
cannam@124 12 because of an assert in file pa_common/pa_process.c. Pieter, Oct 9, 2003.
cannam@124 13
cannam@124 14 */
cannam@124 15 /*
cannam@124 16 * $Id: patest_wire.c 1843 2012-06-22 21:58:10Z philburk $
cannam@124 17 *
cannam@124 18 * This program uses the PortAudio Portable Audio Library.
cannam@124 19 * For more information see: http://www.portaudio.com
cannam@124 20 * Copyright (c) 1999-2000 Ross Bencina and Phil Burk
cannam@124 21 *
cannam@124 22 * Permission is hereby granted, free of charge, to any person obtaining
cannam@124 23 * a copy of this software and associated documentation files
cannam@124 24 * (the "Software"), to deal in the Software without restriction,
cannam@124 25 * including without limitation the rights to use, copy, modify, merge,
cannam@124 26 * publish, distribute, sublicense, and/or sell copies of the Software,
cannam@124 27 * and to permit persons to whom the Software is furnished to do so,
cannam@124 28 * subject to the following conditions:
cannam@124 29 *
cannam@124 30 * The above copyright notice and this permission notice shall be
cannam@124 31 * included in all copies or substantial portions of the Software.
cannam@124 32 *
cannam@124 33 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
cannam@124 34 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
cannam@124 35 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
cannam@124 36 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
cannam@124 37 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
cannam@124 38 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
cannam@124 39 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
cannam@124 40 */
cannam@124 41
cannam@124 42 /*
cannam@124 43 * The text above constitutes the entire PortAudio license; however,
cannam@124 44 * the PortAudio community also makes the following non-binding requests:
cannam@124 45 *
cannam@124 46 * Any person wishing to distribute modifications to the Software is
cannam@124 47 * requested to send the modifications to the original developer so that
cannam@124 48 * they can be incorporated into the canonical version. It is also
cannam@124 49 * requested that these non-binding requests be included along with the
cannam@124 50 * license above.
cannam@124 51 */
cannam@124 52
cannam@124 53 #include <stdio.h>
cannam@124 54 #include <math.h>
cannam@124 55 #include "portaudio.h"
cannam@124 56
cannam@124 57 #define SAMPLE_RATE (44100)
cannam@124 58
cannam@124 59 typedef struct WireConfig_s
cannam@124 60 {
cannam@124 61 int isInputInterleaved;
cannam@124 62 int isOutputInterleaved;
cannam@124 63 int numInputChannels;
cannam@124 64 int numOutputChannels;
cannam@124 65 int framesPerCallback;
cannam@124 66 /* count status flags */
cannam@124 67 int numInputUnderflows;
cannam@124 68 int numInputOverflows;
cannam@124 69 int numOutputUnderflows;
cannam@124 70 int numOutputOverflows;
cannam@124 71 int numPrimingOutputs;
cannam@124 72 int numCallbacks;
cannam@124 73 } WireConfig_t;
cannam@124 74
cannam@124 75 #define USE_FLOAT_INPUT (1)
cannam@124 76 #define USE_FLOAT_OUTPUT (1)
cannam@124 77
cannam@124 78 /* Latencies set to defaults. */
cannam@124 79
cannam@124 80 #if USE_FLOAT_INPUT
cannam@124 81 #define INPUT_FORMAT paFloat32
cannam@124 82 typedef float INPUT_SAMPLE;
cannam@124 83 #else
cannam@124 84 #define INPUT_FORMAT paInt16
cannam@124 85 typedef short INPUT_SAMPLE;
cannam@124 86 #endif
cannam@124 87
cannam@124 88 #if USE_FLOAT_OUTPUT
cannam@124 89 #define OUTPUT_FORMAT paFloat32
cannam@124 90 typedef float OUTPUT_SAMPLE;
cannam@124 91 #else
cannam@124 92 #define OUTPUT_FORMAT paInt16
cannam@124 93 typedef short OUTPUT_SAMPLE;
cannam@124 94 #endif
cannam@124 95
cannam@124 96 double gInOutScaler = 1.0;
cannam@124 97 #define CONVERT_IN_TO_OUT(in) ((OUTPUT_SAMPLE) ((in) * gInOutScaler))
cannam@124 98
cannam@124 99 #define INPUT_DEVICE (Pa_GetDefaultInputDevice())
cannam@124 100 #define OUTPUT_DEVICE (Pa_GetDefaultOutputDevice())
cannam@124 101
cannam@124 102 static PaError TestConfiguration( WireConfig_t *config );
cannam@124 103
cannam@124 104 static int wireCallback( const void *inputBuffer, void *outputBuffer,
cannam@124 105 unsigned long framesPerBuffer,
cannam@124 106 const PaStreamCallbackTimeInfo* timeInfo,
cannam@124 107 PaStreamCallbackFlags statusFlags,
cannam@124 108 void *userData );
cannam@124 109
cannam@124 110 /* This routine will be called by the PortAudio engine when audio is needed.
cannam@124 111 ** It may be called at interrupt level on some machines so don't do anything
cannam@124 112 ** that could mess up the system like calling malloc() or free().
cannam@124 113 */
cannam@124 114
cannam@124 115 static int wireCallback( const void *inputBuffer, void *outputBuffer,
cannam@124 116 unsigned long framesPerBuffer,
cannam@124 117 const PaStreamCallbackTimeInfo* timeInfo,
cannam@124 118 PaStreamCallbackFlags statusFlags,
cannam@124 119 void *userData )
cannam@124 120 {
cannam@124 121 INPUT_SAMPLE *in;
cannam@124 122 OUTPUT_SAMPLE *out;
cannam@124 123 int inStride;
cannam@124 124 int outStride;
cannam@124 125 int inDone = 0;
cannam@124 126 int outDone = 0;
cannam@124 127 WireConfig_t *config = (WireConfig_t *) userData;
cannam@124 128 unsigned int i;
cannam@124 129 int inChannel, outChannel;
cannam@124 130
cannam@124 131 /* This may get called with NULL inputBuffer during initial setup. */
cannam@124 132 if( inputBuffer == NULL) return 0;
cannam@124 133
cannam@124 134 /* Count flags */
cannam@124 135 if( (statusFlags & paInputUnderflow) != 0 ) config->numInputUnderflows += 1;
cannam@124 136 if( (statusFlags & paInputOverflow) != 0 ) config->numInputOverflows += 1;
cannam@124 137 if( (statusFlags & paOutputUnderflow) != 0 ) config->numOutputUnderflows += 1;
cannam@124 138 if( (statusFlags & paOutputOverflow) != 0 ) config->numOutputOverflows += 1;
cannam@124 139 if( (statusFlags & paPrimingOutput) != 0 ) config->numPrimingOutputs += 1;
cannam@124 140 config->numCallbacks += 1;
cannam@124 141
cannam@124 142 inChannel=0, outChannel=0;
cannam@124 143 while( !(inDone && outDone) )
cannam@124 144 {
cannam@124 145 if( config->isInputInterleaved )
cannam@124 146 {
cannam@124 147 in = ((INPUT_SAMPLE*)inputBuffer) + inChannel;
cannam@124 148 inStride = config->numInputChannels;
cannam@124 149 }
cannam@124 150 else
cannam@124 151 {
cannam@124 152 in = ((INPUT_SAMPLE**)inputBuffer)[inChannel];
cannam@124 153 inStride = 1;
cannam@124 154 }
cannam@124 155
cannam@124 156 if( config->isOutputInterleaved )
cannam@124 157 {
cannam@124 158 out = ((OUTPUT_SAMPLE*)outputBuffer) + outChannel;
cannam@124 159 outStride = config->numOutputChannels;
cannam@124 160 }
cannam@124 161 else
cannam@124 162 {
cannam@124 163 out = ((OUTPUT_SAMPLE**)outputBuffer)[outChannel];
cannam@124 164 outStride = 1;
cannam@124 165 }
cannam@124 166
cannam@124 167 for( i=0; i<framesPerBuffer; i++ )
cannam@124 168 {
cannam@124 169 *out = CONVERT_IN_TO_OUT( *in );
cannam@124 170 out += outStride;
cannam@124 171 in += inStride;
cannam@124 172 }
cannam@124 173
cannam@124 174 if(inChannel < (config->numInputChannels - 1)) inChannel++;
cannam@124 175 else inDone = 1;
cannam@124 176 if(outChannel < (config->numOutputChannels - 1)) outChannel++;
cannam@124 177 else outDone = 1;
cannam@124 178 }
cannam@124 179 return 0;
cannam@124 180 }
cannam@124 181
cannam@124 182 /*******************************************************************/
cannam@124 183 int main(void);
cannam@124 184 int main(void)
cannam@124 185 {
cannam@124 186 PaError err = paNoError;
cannam@124 187 WireConfig_t CONFIG;
cannam@124 188 WireConfig_t *config = &CONFIG;
cannam@124 189 int configIndex = 0;;
cannam@124 190
cannam@124 191 err = Pa_Initialize();
cannam@124 192 if( err != paNoError ) goto error;
cannam@124 193
cannam@124 194 printf("Please connect audio signal to input and listen for it on output!\n");
cannam@124 195 printf("input format = %lu\n", INPUT_FORMAT );
cannam@124 196 printf("output format = %lu\n", OUTPUT_FORMAT );
cannam@124 197 printf("input device ID = %d\n", INPUT_DEVICE );
cannam@124 198 printf("output device ID = %d\n", OUTPUT_DEVICE );
cannam@124 199
cannam@124 200 if( INPUT_FORMAT == OUTPUT_FORMAT )
cannam@124 201 {
cannam@124 202 gInOutScaler = 1.0;
cannam@124 203 }
cannam@124 204 else if( (INPUT_FORMAT == paInt16) && (OUTPUT_FORMAT == paFloat32) )
cannam@124 205 {
cannam@124 206 gInOutScaler = 1.0/32768.0;
cannam@124 207 }
cannam@124 208 else if( (INPUT_FORMAT == paFloat32) && (OUTPUT_FORMAT == paInt16) )
cannam@124 209 {
cannam@124 210 gInOutScaler = 32768.0;
cannam@124 211 }
cannam@124 212
cannam@124 213 for( config->isInputInterleaved = 0; config->isInputInterleaved < 2; config->isInputInterleaved++ )
cannam@124 214 {
cannam@124 215 for( config->isOutputInterleaved = 0; config->isOutputInterleaved < 2; config->isOutputInterleaved++ )
cannam@124 216 {
cannam@124 217 for( config->numInputChannels = 1; config->numInputChannels < 3; config->numInputChannels++ )
cannam@124 218 {
cannam@124 219 for( config->numOutputChannels = 1; config->numOutputChannels < 3; config->numOutputChannels++ )
cannam@124 220 {
cannam@124 221 /* If framesPerCallback = 0, assertion fails in file pa_common/pa_process.c, line 1413: EX. */
cannam@124 222 for( config->framesPerCallback = 64; config->framesPerCallback < 129; config->framesPerCallback += 64 )
cannam@124 223 {
cannam@124 224 printf("-----------------------------------------------\n" );
cannam@124 225 printf("Configuration #%d\n", configIndex++ );
cannam@124 226 err = TestConfiguration( config );
cannam@124 227 /* Give user a chance to bail out. */
cannam@124 228 if( err == 1 )
cannam@124 229 {
cannam@124 230 err = paNoError;
cannam@124 231 goto done;
cannam@124 232 }
cannam@124 233 else if( err != paNoError ) goto error;
cannam@124 234 }
cannam@124 235 }
cannam@124 236 }
cannam@124 237 }
cannam@124 238 }
cannam@124 239
cannam@124 240 done:
cannam@124 241 Pa_Terminate();
cannam@124 242 printf("Full duplex sound test complete.\n"); fflush(stdout);
cannam@124 243 printf("Hit ENTER to quit.\n"); fflush(stdout);
cannam@124 244 getchar();
cannam@124 245 return 0;
cannam@124 246
cannam@124 247 error:
cannam@124 248 Pa_Terminate();
cannam@124 249 fprintf( stderr, "An error occured while using the portaudio stream\n" );
cannam@124 250 fprintf( stderr, "Error number: %d\n", err );
cannam@124 251 fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) );
cannam@124 252 printf("Hit ENTER to quit.\n"); fflush(stdout);
cannam@124 253 getchar();
cannam@124 254 return -1;
cannam@124 255 }
cannam@124 256
cannam@124 257 static PaError TestConfiguration( WireConfig_t *config )
cannam@124 258 {
cannam@124 259 int c;
cannam@124 260 PaError err = paNoError;
cannam@124 261 PaStream *stream;
cannam@124 262 PaStreamParameters inputParameters, outputParameters;
cannam@124 263
cannam@124 264 printf("input %sinterleaved!\n", (config->isInputInterleaved ? " " : "NOT ") );
cannam@124 265 printf("output %sinterleaved!\n", (config->isOutputInterleaved ? " " : "NOT ") );
cannam@124 266 printf("input channels = %d\n", config->numInputChannels );
cannam@124 267 printf("output channels = %d\n", config->numOutputChannels );
cannam@124 268 printf("framesPerCallback = %d\n", config->framesPerCallback );
cannam@124 269
cannam@124 270 inputParameters.device = INPUT_DEVICE; /* default input device */
cannam@124 271 if (inputParameters.device == paNoDevice) {
cannam@124 272 fprintf(stderr,"Error: No default input device.\n");
cannam@124 273 goto error;
cannam@124 274 }
cannam@124 275 inputParameters.channelCount = config->numInputChannels;
cannam@124 276 inputParameters.sampleFormat = INPUT_FORMAT | (config->isInputInterleaved ? 0 : paNonInterleaved);
cannam@124 277 inputParameters.suggestedLatency = Pa_GetDeviceInfo( inputParameters.device )->defaultLowInputLatency;
cannam@124 278 inputParameters.hostApiSpecificStreamInfo = NULL;
cannam@124 279
cannam@124 280 outputParameters.device = OUTPUT_DEVICE; /* default output device */
cannam@124 281 if (outputParameters.device == paNoDevice) {
cannam@124 282 fprintf(stderr,"Error: No default output device.\n");
cannam@124 283 goto error;
cannam@124 284 }
cannam@124 285 outputParameters.channelCount = config->numOutputChannels;
cannam@124 286 outputParameters.sampleFormat = OUTPUT_FORMAT | (config->isOutputInterleaved ? 0 : paNonInterleaved);
cannam@124 287 outputParameters.suggestedLatency = Pa_GetDeviceInfo( outputParameters.device )->defaultLowOutputLatency;
cannam@124 288 outputParameters.hostApiSpecificStreamInfo = NULL;
cannam@124 289
cannam@124 290 config->numInputUnderflows = 0;
cannam@124 291 config->numInputOverflows = 0;
cannam@124 292 config->numOutputUnderflows = 0;
cannam@124 293 config->numOutputOverflows = 0;
cannam@124 294 config->numPrimingOutputs = 0;
cannam@124 295 config->numCallbacks = 0;
cannam@124 296
cannam@124 297 err = Pa_OpenStream(
cannam@124 298 &stream,
cannam@124 299 &inputParameters,
cannam@124 300 &outputParameters,
cannam@124 301 SAMPLE_RATE,
cannam@124 302 config->framesPerCallback, /* frames per buffer */
cannam@124 303 paClipOff, /* we won't output out of range samples so don't bother clipping them */
cannam@124 304 wireCallback,
cannam@124 305 config );
cannam@124 306 if( err != paNoError ) goto error;
cannam@124 307
cannam@124 308 err = Pa_StartStream( stream );
cannam@124 309 if( err != paNoError ) goto error;
cannam@124 310
cannam@124 311 printf("Now recording and playing. - Hit ENTER for next configuration, or 'q' to quit.\n"); fflush(stdout);
cannam@124 312 c = getchar();
cannam@124 313
cannam@124 314 printf("Closing stream.\n");
cannam@124 315 err = Pa_CloseStream( stream );
cannam@124 316 if( err != paNoError ) goto error;
cannam@124 317
cannam@124 318 #define CHECK_FLAG_COUNT(member) \
cannam@124 319 if( config->member > 0 ) printf("FLAGS SET: " #member " = %d\n", config->member );
cannam@124 320 CHECK_FLAG_COUNT( numInputUnderflows );
cannam@124 321 CHECK_FLAG_COUNT( numInputOverflows );
cannam@124 322 CHECK_FLAG_COUNT( numOutputUnderflows );
cannam@124 323 CHECK_FLAG_COUNT( numOutputOverflows );
cannam@124 324 CHECK_FLAG_COUNT( numPrimingOutputs );
cannam@124 325 printf("number of callbacks = %d\n", config->numCallbacks );
cannam@124 326
cannam@124 327 if( c == 'q' ) return 1;
cannam@124 328
cannam@124 329 error:
cannam@124 330 return err;
cannam@124 331 }