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