annotate src/portaudio_20161030_catalina_patch/test/patest_wire.c @ 169:223a55898ab9 tip default

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