annotate src/portaudio_20161030/examples/paex_read_write_wire.c @ 56:af97cad61ff0

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