annotate src/portaudio/test/patest_wire.c @ 23:619f715526df sv_v2.1

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