annotate src/portaudio/test/patest_wire.c @ 89:8a15ff55d9af

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