annotate src/portaudio_20140130/examples/paex_read_write_wire.c @ 39:7ddb4fc30dac

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