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