annotate src/portaudio_20161030_catalina_patch/test/patest_timing.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_timing.c
Chris@55 2 @ingroup test_src
Chris@55 3 @brief Play a sine wave for several seconds, and spits out a ton of timing info while it's at it. Based on patest_sine.c
Chris@55 4 @author Bjorn Roche
Chris@55 5 @author Ross Bencina <rossb@audiomulch.com>
Chris@55 6 @author Phil Burk <philburk@softsynth.com>
Chris@55 7 */
Chris@55 8 /*
Chris@55 9 * $Id: patest_timing.c 578 2003-09-02 04:17:38Z 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 <math.h>
Chris@55 48 #include "portaudio.h"
Chris@55 49
Chris@55 50 #define NUM_SECONDS (5)
Chris@55 51 #define SAMPLE_RATE (44100)
Chris@55 52 #define FRAMES_PER_BUFFER (64)
Chris@55 53
Chris@55 54 #ifndef M_PI
Chris@55 55 #define M_PI (3.14159265)
Chris@55 56 #endif
Chris@55 57
Chris@55 58 #define TABLE_SIZE (200)
Chris@55 59 typedef struct
Chris@55 60 {
Chris@55 61 PaStream *stream;
Chris@55 62 PaTime start;
Chris@55 63 float sine[TABLE_SIZE];
Chris@55 64 int left_phase;
Chris@55 65 int right_phase;
Chris@55 66 }
Chris@55 67 paTestData;
Chris@55 68
Chris@55 69 /* This routine will be called by the PortAudio engine when audio is needed.
Chris@55 70 ** It may called at interrupt level on some machines so don't do anything
Chris@55 71 ** that could mess up the system like calling malloc() or free().
Chris@55 72 */
Chris@55 73 static int patestCallback( const void *inputBuffer, void *outputBuffer,
Chris@55 74 unsigned long framesPerBuffer,
Chris@55 75 const PaStreamCallbackTimeInfo* timeInfo,
Chris@55 76 PaStreamCallbackFlags statusFlags,
Chris@55 77 void *userData )
Chris@55 78 {
Chris@55 79 paTestData *data = (paTestData*)userData;
Chris@55 80 float *out = (float*)outputBuffer;
Chris@55 81 unsigned long i;
Chris@55 82
Chris@55 83 (void) timeInfo; /* Prevent unused variable warnings. */
Chris@55 84 (void) statusFlags;
Chris@55 85 (void) inputBuffer;
Chris@55 86
Chris@55 87 printf( "Timing info given to callback: Adc: %g, Current: %g, Dac: %g\n",
Chris@55 88 timeInfo->inputBufferAdcTime,
Chris@55 89 timeInfo->currentTime,
Chris@55 90 timeInfo->outputBufferDacTime );
Chris@55 91
Chris@55 92 printf( "getStreamTime() returns: %g\n", Pa_GetStreamTime(data->stream) - data->start );
Chris@55 93
Chris@55 94 for( i=0; i<framesPerBuffer; i++ )
Chris@55 95 {
Chris@55 96 *out++ = data->sine[data->left_phase]; /* left */
Chris@55 97 *out++ = data->sine[data->right_phase]; /* right */
Chris@55 98 data->left_phase += 1;
Chris@55 99 if( data->left_phase >= TABLE_SIZE ) data->left_phase -= TABLE_SIZE;
Chris@55 100 data->right_phase += 3; /* higher pitch so we can distinguish left and right. */
Chris@55 101 if( data->right_phase >= TABLE_SIZE ) data->right_phase -= TABLE_SIZE;
Chris@55 102 }
Chris@55 103
Chris@55 104 return paContinue;
Chris@55 105 }
Chris@55 106
Chris@55 107 /*******************************************************************/
Chris@55 108 int main(void);
Chris@55 109 int main(void)
Chris@55 110 {
Chris@55 111 PaStreamParameters outputParameters;
Chris@55 112 PaStream *stream;
Chris@55 113 PaError err;
Chris@55 114 paTestData data;
Chris@55 115 int i;
Chris@55 116
Chris@55 117
Chris@55 118 printf("PortAudio Test: output sine wave. SR = %d, BufSize = %d\n", SAMPLE_RATE, FRAMES_PER_BUFFER);
Chris@55 119
Chris@55 120 /* initialise sinusoidal wavetable */
Chris@55 121 for( i=0; i<TABLE_SIZE; i++ )
Chris@55 122 {
Chris@55 123 data.sine[i] = (float) sin( ((double)i/(double)TABLE_SIZE) * M_PI * 2. );
Chris@55 124 }
Chris@55 125 data.left_phase = data.right_phase = 0;
Chris@55 126
Chris@55 127 err = Pa_Initialize();
Chris@55 128 if( err != paNoError ) goto error;
Chris@55 129
Chris@55 130 outputParameters.device = Pa_GetDefaultOutputDevice(); /* default output device */
Chris@55 131 outputParameters.channelCount = 2; /* stereo output */
Chris@55 132 outputParameters.sampleFormat = paFloat32; /* 32 bit floating point output */
Chris@55 133 outputParameters.suggestedLatency = Pa_GetDeviceInfo( outputParameters.device )->defaultLowOutputLatency;
Chris@55 134 outputParameters.hostApiSpecificStreamInfo = NULL;
Chris@55 135
Chris@55 136 err = Pa_OpenStream(
Chris@55 137 &stream,
Chris@55 138 NULL, /* no input */
Chris@55 139 &outputParameters,
Chris@55 140 SAMPLE_RATE,
Chris@55 141 FRAMES_PER_BUFFER,
Chris@55 142 paClipOff, /* we won't output out of range samples so don't bother clipping them */
Chris@55 143 patestCallback,
Chris@55 144 &data );
Chris@55 145 data.stream = stream;
Chris@55 146 data.start = Pa_GetStreamTime(stream);
Chris@55 147 if( err != paNoError ) goto error;
Chris@55 148
Chris@55 149 err = Pa_StartStream( stream );
Chris@55 150 data.start = Pa_GetStreamTime(stream);
Chris@55 151 if( err != paNoError ) goto error;
Chris@55 152
Chris@55 153 printf("Play for %d seconds.\n", NUM_SECONDS );
Chris@55 154 Pa_Sleep( NUM_SECONDS * 1000 );
Chris@55 155
Chris@55 156 err = Pa_StopStream( stream );
Chris@55 157 if( err != paNoError ) goto error;
Chris@55 158
Chris@55 159 err = Pa_CloseStream( stream );
Chris@55 160 if( err != paNoError ) goto error;
Chris@55 161
Chris@55 162 Pa_Terminate();
Chris@55 163 printf("Test finished.\n");
Chris@55 164 printf("The tone should have been heard for about 5 seconds and all the timing info above should report that about 5 seconds elapsed (except Adc, which is undefined since there was no input device opened).\n");
Chris@55 165
Chris@55 166 return err;
Chris@55 167 error:
Chris@55 168 Pa_Terminate();
Chris@55 169 fprintf( stderr, "An error occured while using the portaudio stream\n" );
Chris@55 170 fprintf( stderr, "Error number: %d\n", err );
Chris@55 171 fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) );
Chris@55 172 return err;
Chris@55 173 }