annotate src/portaudio_20161030_catalina_patch/test/patest_unplug.c @ 81:7029a4916348

Merge build update
author Chris Cannam
date Thu, 31 Oct 2019 13:36:58 +0000
parents 4edcd14160a5
children
rev   line source
Chris@55 1 /** @file patest_unplug.c
Chris@55 2 @ingroup test_src
Chris@55 3 @brief Debug a crash involving unplugging a USB device.
Chris@55 4 @author Phil Burk http://www.softsynth.com
Chris@55 5 */
Chris@55 6 /*
Chris@55 7 * $Id$
Chris@55 8 *
Chris@55 9 * This program uses the PortAudio Portable Audio Library.
Chris@55 10 * For more information see: http://www.portaudio.com
Chris@55 11 * Copyright (c) 1999-2000 Ross Bencina and Phil Burk
Chris@55 12 *
Chris@55 13 * Permission is hereby granted, free of charge, to any person obtaining
Chris@55 14 * a copy of this software and associated documentation files
Chris@55 15 * (the "Software"), to deal in the Software without restriction,
Chris@55 16 * including without limitation the rights to use, copy, modify, merge,
Chris@55 17 * publish, distribute, sublicense, and/or sell copies of the Software,
Chris@55 18 * and to permit persons to whom the Software is furnished to do so,
Chris@55 19 * subject to the following conditions:
Chris@55 20 *
Chris@55 21 * The above copyright notice and this permission notice shall be
Chris@55 22 * included in all copies or substantial portions of the Software.
Chris@55 23 *
Chris@55 24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
Chris@55 25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
Chris@55 26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
Chris@55 27 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
Chris@55 28 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
Chris@55 29 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
Chris@55 30 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Chris@55 31 */
Chris@55 32
Chris@55 33 /*
Chris@55 34 * The text above constitutes the entire PortAudio license; however,
Chris@55 35 * the PortAudio community also makes the following non-binding requests:
Chris@55 36 *
Chris@55 37 * Any person wishing to distribute modifications to the Software is
Chris@55 38 * requested to send the modifications to the original developer so that
Chris@55 39 * they can be incorporated into the canonical version. It is also
Chris@55 40 * requested that these non-binding requests be included along with the
Chris@55 41 * license above.
Chris@55 42 */
Chris@55 43
Chris@55 44 #include <stdio.h>
Chris@55 45 #include <stdlib.h>
Chris@55 46 #include <memory.h>
Chris@55 47 #include <math.h>
Chris@55 48 #include "portaudio.h"
Chris@55 49
Chris@55 50 #define NUM_SECONDS (8)
Chris@55 51 #define SAMPLE_RATE (44100)
Chris@55 52 #ifndef M_PI
Chris@55 53 #define M_PI (3.14159265)
Chris@55 54 #endif
Chris@55 55 #define TABLE_SIZE (200)
Chris@55 56 #define FRAMES_PER_BUFFER (64)
Chris@55 57 #define MAX_CHANNELS (8)
Chris@55 58
Chris@55 59 typedef struct
Chris@55 60 {
Chris@55 61 short sine[TABLE_SIZE];
Chris@55 62 int32_t phases[MAX_CHANNELS];
Chris@55 63 int32_t numChannels;
Chris@55 64 int32_t sampsToGo;
Chris@55 65 }
Chris@55 66 paTestData;
Chris@55 67
Chris@55 68
Chris@55 69 static int inputCallback( const void *inputBuffer, void *outputBuffer,
Chris@55 70 unsigned long framesPerBuffer,
Chris@55 71 const PaStreamCallbackTimeInfo* timeInfo,
Chris@55 72 PaStreamCallbackFlags statusFlags,
Chris@55 73 void *userData )
Chris@55 74 {
Chris@55 75 paTestData *data = (paTestData*)userData;
Chris@55 76 int finished = 0;
Chris@55 77 (void) inputBuffer; /* Prevent "unused variable" warnings. */
Chris@55 78 (void) outputBuffer; /* Prevent "unused variable" warnings. */
Chris@55 79
Chris@55 80 data->sampsToGo -= framesPerBuffer;
Chris@55 81 if (data->sampsToGo <= 0)
Chris@55 82 {
Chris@55 83 data->sampsToGo = 0;
Chris@55 84 finished = 1;
Chris@55 85 }
Chris@55 86 return finished;
Chris@55 87 }
Chris@55 88
Chris@55 89 static int outputCallback( const void *inputBuffer, void *outputBuffer,
Chris@55 90 unsigned long framesPerBuffer,
Chris@55 91 const PaStreamCallbackTimeInfo* timeInfo,
Chris@55 92 PaStreamCallbackFlags statusFlags,
Chris@55 93 void *userData )
Chris@55 94 {
Chris@55 95 paTestData *data = (paTestData*)userData;
Chris@55 96 short *out = (short*)outputBuffer;
Chris@55 97 unsigned int i;
Chris@55 98 int finished = 0;
Chris@55 99 (void) inputBuffer; /* Prevent "unused variable" warnings. */
Chris@55 100
Chris@55 101 for( i=0; i<framesPerBuffer; i++ )
Chris@55 102 {
Chris@55 103 for (int channelIndex = 0; channelIndex < data->numChannels; channelIndex++)
Chris@55 104 {
Chris@55 105 int phase = data->phases[channelIndex];
Chris@55 106 *out++ = data->sine[phase];
Chris@55 107 phase += channelIndex + 2;
Chris@55 108 if( phase >= TABLE_SIZE ) phase -= TABLE_SIZE;
Chris@55 109 data->phases[channelIndex] = phase;
Chris@55 110 }
Chris@55 111 }
Chris@55 112 return finished;
Chris@55 113 }
Chris@55 114
Chris@55 115 /*******************************************************************/
Chris@55 116 int main(int argc, char **args);
Chris@55 117 int main(int argc, char **args)
Chris@55 118 {
Chris@55 119 PaStreamParameters inputParameters;
Chris@55 120 PaStreamParameters outputParameters;
Chris@55 121 PaStream *inputStream;
Chris@55 122 PaStream *outputStream;
Chris@55 123 const PaDeviceInfo *deviceInfo;
Chris@55 124 PaError err;
Chris@55 125 paTestData data;
Chris@55 126 int i;
Chris@55 127 int totalSamps;
Chris@55 128 int inputDevice = -1;
Chris@55 129 int outputDevice = -1;
Chris@55 130
Chris@55 131 printf("Test unplugging a USB device.\n");
Chris@55 132
Chris@55 133 if( argc > 1 ) {
Chris@55 134 inputDevice = outputDevice = atoi( args[1] );
Chris@55 135 printf("Using device number %d.\n\n", inputDevice );
Chris@55 136 } else {
Chris@55 137 printf("Using default device.\n\n" );
Chris@55 138 }
Chris@55 139
Chris@55 140 memset(&data, 0, sizeof(data));
Chris@55 141
Chris@55 142 /* initialise sinusoidal wavetable */
Chris@55 143 for( i=0; i<TABLE_SIZE; i++ )
Chris@55 144 {
Chris@55 145 data.sine[i] = (short) (32767.0 * sin( ((double)i/(double)TABLE_SIZE) * M_PI * 2. ));
Chris@55 146 }
Chris@55 147 data.numChannels = 2;
Chris@55 148 data.sampsToGo = totalSamps = NUM_SECONDS * SAMPLE_RATE; /* Play for a few seconds. */
Chris@55 149
Chris@55 150
Chris@55 151 err = Pa_Initialize();
Chris@55 152 if( err != paNoError ) goto error;
Chris@55 153
Chris@55 154 if( inputDevice == -1 )
Chris@55 155 inputParameters.device = Pa_GetDefaultInputDevice(); /* default input device */
Chris@55 156 else
Chris@55 157 inputParameters.device = inputDevice ;
Chris@55 158
Chris@55 159 if (inputParameters.device == paNoDevice) {
Chris@55 160 fprintf(stderr,"Error: No default input device.\n");
Chris@55 161 goto error;
Chris@55 162 }
Chris@55 163
Chris@55 164 if( outputDevice == -1 )
Chris@55 165 outputParameters.device = Pa_GetDefaultOutputDevice(); /* default output device */
Chris@55 166 else
Chris@55 167 outputParameters.device = outputDevice ;
Chris@55 168
Chris@55 169 if (outputParameters.device == paNoDevice) {
Chris@55 170 fprintf(stderr,"Error: No default output device.\n");
Chris@55 171 goto error;
Chris@55 172 }
Chris@55 173
Chris@55 174 inputParameters.channelCount = 2;
Chris@55 175 inputParameters.sampleFormat = paInt16;
Chris@55 176 deviceInfo = Pa_GetDeviceInfo( inputParameters.device );
Chris@55 177 if( deviceInfo == NULL )
Chris@55 178 {
Chris@55 179 fprintf( stderr, "No matching input device.\n" );
Chris@55 180 goto error;
Chris@55 181 }
Chris@55 182 inputParameters.suggestedLatency = deviceInfo->defaultLowInputLatency;
Chris@55 183 inputParameters.hostApiSpecificStreamInfo = NULL;
Chris@55 184 err = Pa_OpenStream(
Chris@55 185 &inputStream,
Chris@55 186 &inputParameters,
Chris@55 187 NULL,
Chris@55 188 SAMPLE_RATE,
Chris@55 189 FRAMES_PER_BUFFER,
Chris@55 190 0,
Chris@55 191 inputCallback,
Chris@55 192 &data );
Chris@55 193 if( err != paNoError ) goto error;
Chris@55 194
Chris@55 195 outputParameters.channelCount = 2;
Chris@55 196 outputParameters.sampleFormat = paInt16;
Chris@55 197 deviceInfo = Pa_GetDeviceInfo( outputParameters.device );
Chris@55 198 if( deviceInfo == NULL )
Chris@55 199 {
Chris@55 200 fprintf( stderr, "No matching output device.\n" );
Chris@55 201 goto error;
Chris@55 202 }
Chris@55 203 outputParameters.suggestedLatency = deviceInfo->defaultLowOutputLatency;
Chris@55 204 outputParameters.hostApiSpecificStreamInfo = NULL;
Chris@55 205 err = Pa_OpenStream(
Chris@55 206 &outputStream,
Chris@55 207 NULL,
Chris@55 208 &outputParameters,
Chris@55 209 SAMPLE_RATE,
Chris@55 210 FRAMES_PER_BUFFER,
Chris@55 211 (paClipOff | paDitherOff),
Chris@55 212 outputCallback,
Chris@55 213 &data );
Chris@55 214 if( err != paNoError ) goto error;
Chris@55 215
Chris@55 216 err = Pa_StartStream( inputStream );
Chris@55 217 if( err != paNoError ) goto error;
Chris@55 218 err = Pa_StartStream( outputStream );
Chris@55 219 if( err != paNoError ) goto error;
Chris@55 220
Chris@55 221 printf("When you hear sound, unplug the USB device.\n");
Chris@55 222 do
Chris@55 223 {
Chris@55 224 Pa_Sleep(500);
Chris@55 225 printf("Frames remaining = %d\n", data.sampsToGo);
Chris@55 226 printf("Pa_IsStreamActive(inputStream) = %d\n", Pa_IsStreamActive(inputStream));
Chris@55 227 printf("Pa_IsStreamActive(outputStream) = %d\n", Pa_IsStreamActive(outputStream));
Chris@55 228 } while( Pa_IsStreamActive(inputStream) && Pa_IsStreamActive(outputStream) );
Chris@55 229
Chris@55 230 err = Pa_CloseStream( inputStream );
Chris@55 231 if( err != paNoError ) goto error;
Chris@55 232 err = Pa_CloseStream( outputStream );
Chris@55 233 if( err != paNoError ) goto error;
Chris@55 234 Pa_Terminate();
Chris@55 235 return paNoError;
Chris@55 236 error:
Chris@55 237 Pa_Terminate();
Chris@55 238 fprintf( stderr, "An error occured while using the portaudio stream\n" );
Chris@55 239 fprintf( stderr, "Error number: %d\n", err );
Chris@55 240 fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) );
Chris@55 241 fprintf( stderr, "Host Error message: %s\n", Pa_GetLastHostErrorInfo()->errorText );
Chris@55 242 return err;
Chris@55 243 }