| 1 |
/** @file patest_read_write_wire.c |
| 2 |
@brief Tests full duplex blocking I/O by passing input straight to output. |
| 3 |
@author Bjorn Roche. XO Audio LLC for Z-Systems Engineering. |
| 4 |
@author based on code by: Phil Burk http://www.softsynth.com |
| 5 |
@author based on code by: Ross Bencina [email protected] |
| 6 |
*/ |
| 7 |
/* |
| 8 |
* $Id: patest_read_write_wire.c,v 1.1 2006/06/10 21:30:56 dmazzoni Exp $ |
| 9 |
* |
| 10 |
* This program uses the PortAudio Portable Audio Library. |
| 11 |
* For more information see: http://www.portaudio.com |
| 12 |
* Copyright (c) 1999-2000 Ross Bencina and Phil Burk |
| 13 |
* |
| 14 |
* Permission is hereby granted, free of charge, to any person obtaining |
| 15 |
* a copy of this software and associated documentation files |
| 16 |
* (the "Software"), to deal in the Software without restriction, |
| 17 |
* including without limitation the rights to use, copy, modify, merge, |
| 18 |
* publish, distribute, sublicense, and/or sell copies of the Software, |
| 19 |
* and to permit persons to whom the Software is furnished to do so, |
| 20 |
* subject to the following conditions: |
| 21 |
* |
| 22 |
* The above copyright notice and this permission notice shall be |
| 23 |
* included in all copies or substantial portions of the Software. |
| 24 |
* |
| 25 |
* Any person wishing to distribute modifications to the Software is |
| 26 |
* requested to send the modifications to the original developer so that |
| 27 |
* they can be incorporated into the canonical version. |
| 28 |
* |
| 29 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 30 |
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 31 |
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 32 |
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR |
| 33 |
* ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF |
| 34 |
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
| 35 |
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 36 |
* |
| 37 |
*/ |
| 38 |
|
| 39 |
#include <stdio.h> |
| 40 |
#include <stdlib.h> |
| 41 |
#include "portaudio.h" |
| 42 |
|
| 43 |
/* #define SAMPLE_RATE (17932) // Test failure to open with this value. */ |
| 44 |
#define SAMPLE_RATE (44100) |
| 45 |
#define FRAMES_PER_BUFFER (1024) |
| 46 |
#define NUM_CHANNELS (2) |
| 47 |
/* #define DITHER_FLAG (paDitherOff) */ |
| 48 |
#define DITHER_FLAG (0) /**/ |
| 49 |
|
| 50 |
/* Select sample format. */ |
| 51 |
#if 1 |
| 52 |
#define PA_SAMPLE_TYPE paFloat32 |
| 53 |
typedef float SAMPLE; |
| 54 |
#define SAMPLE_SILENCE (0.0f) |
| 55 |
#define PRINTF_S_FORMAT "%.8f" |
| 56 |
#elif 1 |
| 57 |
#define PA_SAMPLE_TYPE paInt16 |
| 58 |
typedef short SAMPLE; |
| 59 |
#define SAMPLE_SILENCE (0) |
| 60 |
#define PRINTF_S_FORMAT "%d" |
| 61 |
#elif 0 |
| 62 |
#define PA_SAMPLE_TYPE paInt8 |
| 63 |
typedef char SAMPLE; |
| 64 |
#define SAMPLE_SILENCE (0) |
| 65 |
#define PRINTF_S_FORMAT "%d" |
| 66 |
#else |
| 67 |
#define PA_SAMPLE_TYPE paUInt8 |
| 68 |
typedef unsigned char SAMPLE; |
| 69 |
#define SAMPLE_SILENCE (128) |
| 70 |
#define PRINTF_S_FORMAT "%d" |
| 71 |
#endif |
| 72 |
|
| 73 |
|
| 74 |
/*******************************************************************/ |
| 75 |
int main(void); |
| 76 |
int main(void) |
| 77 |
{ |
| 78 |
PaStreamParameters inputParameters, outputParameters; |
| 79 |
PaStream *stream; |
| 80 |
PaError err; |
| 81 |
SAMPLE *sampleBlock; |
| 82 |
int i; |
| 83 |
int numBytes; |
| 84 |
|
| 85 |
|
| 86 |
printf("patest_read_write_wire.c\n"); fflush(stdout); |
| 87 |
|
| 88 |
numBytes = FRAMES_PER_BUFFER * NUM_CHANNELS * sizeof(SAMPLE); |
| 89 |
sampleBlock = (SAMPLE *) malloc( numBytes ); |
| 90 |
if( sampleBlock == NULL ) |
| 91 |
{ |
| 92 |
printf("Could not allocate record array.\n"); |
| 93 |
exit(1); |
| 94 |
} |
| 95 |
for( i=0; i<FRAMES_PER_BUFFER*NUM_CHANNELS; i++ ) |
| 96 |
sampleBlock[i] = (SAMPLE_SILENCE); |
| 97 |
|
| 98 |
err = Pa_Initialize(); |
| 99 |
if( err != paNoError ) goto error; |
| 100 |
|
| 101 |
inputParameters.device = Pa_GetDefaultInputDevice(); /* default input device */ |
| 102 |
printf( "Input device # %d.\n", inputParameters.device ); |
| 103 |
printf( "Input LL: %g s\n", Pa_GetDeviceInfo( inputParameters.device )->defaultLowInputLatency ); |
| 104 |
printf( "Input HL: %g s\n", Pa_GetDeviceInfo( inputParameters.device )->defaultHighInputLatency ); |
| 105 |
inputParameters.channelCount = NUM_CHANNELS; |
| 106 |
inputParameters.sampleFormat = PA_SAMPLE_TYPE; |
| 107 |
inputParameters.suggestedLatency = Pa_GetDeviceInfo( inputParameters.device )->defaultHighInputLatency ; |
| 108 |
inputParameters.hostApiSpecificStreamInfo = NULL; |
| 109 |
|
| 110 |
outputParameters.device = Pa_GetDefaultOutputDevice(); /* default output device */ |
| 111 |
printf( "Output device # %d.\n", outputParameters.device ); |
| 112 |
printf( "Output LL: %g s\n", Pa_GetDeviceInfo( outputParameters.device )->defaultLowOutputLatency ); |
| 113 |
printf( "Output HL: %g s\n", Pa_GetDeviceInfo( outputParameters.device )->defaultHighOutputLatency ); |
| 114 |
outputParameters.channelCount = NUM_CHANNELS; |
| 115 |
outputParameters.sampleFormat = PA_SAMPLE_TYPE; |
| 116 |
outputParameters.suggestedLatency = Pa_GetDeviceInfo( outputParameters.device )->defaultHighOutputLatency; |
| 117 |
outputParameters.hostApiSpecificStreamInfo = NULL; |
| 118 |
|
| 119 |
/* -- setup -- */ |
| 120 |
|
| 121 |
err = Pa_OpenStream( |
| 122 |
&stream, |
| 123 |
&inputParameters, |
| 124 |
&outputParameters, |
| 125 |
SAMPLE_RATE, |
| 126 |
FRAMES_PER_BUFFER, |
| 127 |
paClipOff, /* we won't output out of range samples so don't bother clipping them */ |
| 128 |
NULL, /* no callback, use blocking API */ |
| 129 |
NULL ); /* no callback, so no callback userData */ |
| 130 |
if( err != paNoError ) goto error; |
| 131 |
|
| 132 |
err = Pa_StartStream( stream ); |
| 133 |
if( err != paNoError ) goto error; |
| 134 |
printf("Wire on. Will run one minute.\n"); fflush(stdout); |
| 135 |
|
| 136 |
for( i=0; i<(60*SAMPLE_RATE)/FRAMES_PER_BUFFER; ++i ) |
| 137 |
{ |
| 138 |
err = Pa_WriteStream( stream, sampleBlock, FRAMES_PER_BUFFER ); |
| 139 |
if( err ) goto xrun; |
| 140 |
err = Pa_ReadStream( stream, sampleBlock, FRAMES_PER_BUFFER ); |
| 141 |
if( err ) goto xrun; |
| 142 |
} |
| 143 |
err = Pa_StopStream( stream ); |
| 144 |
if( err != paNoError ) goto error; |
| 145 |
|
| 146 |
for( i=0; i<FRAMES_PER_BUFFER*NUM_CHANNELS; i++ ) |
| 147 |
sampleBlock[i] = (SAMPLE_SILENCE); |
| 148 |
|
| 149 |
err = Pa_StartStream( stream ); |
| 150 |
if( err != paNoError ) goto error; |
| 151 |
printf("Wire on. Interrupt to stop.\n"); fflush(stdout); |
| 152 |
|
| 153 |
while( 1 ) |
| 154 |
{ |
| 155 |
err = Pa_WriteStream( stream, sampleBlock, FRAMES_PER_BUFFER ); |
| 156 |
if( err ) goto xrun; |
| 157 |
err = Pa_ReadStream( stream, sampleBlock, FRAMES_PER_BUFFER ); |
| 158 |
if( err ) goto xrun; |
| 159 |
} |
| 160 |
err = Pa_StopStream( stream ); |
| 161 |
if( err != paNoError ) goto error; |
| 162 |
|
| 163 |
free( sampleBlock ); |
| 164 |
|
| 165 |
Pa_Terminate(); |
| 166 |
return 0; |
| 167 |
|
| 168 |
xrun: |
| 169 |
Pa_Terminate(); |
| 170 |
if( err & paInputOverflow ) |
| 171 |
fprintf( stderr, "Input Overflow.\n" ); |
| 172 |
if( err & paOutputUnderflow ) |
| 173 |
fprintf( stderr, "Output Underflow.\n" ); |
| 174 |
return -2; |
| 175 |
|
| 176 |
error: |
| 177 |
Pa_Terminate(); |
| 178 |
fprintf( stderr, "An error occured while using the portaudio stream\n" ); |
| 179 |
fprintf( stderr, "Error number: %d\n", err ); |
| 180 |
fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) ); |
| 181 |
return -1; |
| 182 |
} |
| 183 |
|