annotate src/portaudio_20140130/examples/paex_read_write_wire.c @ 169:223a55898ab9 tip default

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