annotate src/portaudio/examples/paex_read_write_wire.c @ 23:619f715526df sv_v2.1

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