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