annotate src/portaudio/test/patest_prime.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 patest_prime.c
Chris@4 2 @ingroup test_src
Chris@4 3 @brief Test stream priming mode.
Chris@4 4 @author Ross Bencina http://www.audiomulch.com/~rossb
Chris@4 5 */
Chris@4 6
Chris@4 7 /*
Chris@4 8 * $Id: patest_prime.c 1371 2008-03-11 14:27:26Z bjornroche $
Chris@4 9 *
Chris@4 10 * This program uses the PortAudio Portable Audio Library.
Chris@4 11 * For more information see: http://www.portaudio.com
Chris@4 12 * Copyright (c) 1999-2000 Ross Bencina and Phil Burk
Chris@4 13 *
Chris@4 14 * Permission is hereby granted, free of charge, to any person obtaining
Chris@4 15 * a copy of this software and associated documentation files
Chris@4 16 * (the "Software"), to deal in the Software without restriction,
Chris@4 17 * including without limitation the rights to use, copy, modify, merge,
Chris@4 18 * publish, distribute, sublicense, and/or sell copies of the Software,
Chris@4 19 * and to permit persons to whom the Software is furnished to do so,
Chris@4 20 * subject to the following conditions:
Chris@4 21 *
Chris@4 22 * The above copyright notice and this permission notice shall be
Chris@4 23 * included in all copies or substantial portions of the Software.
Chris@4 24 *
Chris@4 25 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
Chris@4 26 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
Chris@4 27 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
Chris@4 28 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
Chris@4 29 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
Chris@4 30 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
Chris@4 31 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Chris@4 32 */
Chris@4 33
Chris@4 34 /*
Chris@4 35 * The text above constitutes the entire PortAudio license; however,
Chris@4 36 * the PortAudio community also makes the following non-binding requests:
Chris@4 37 *
Chris@4 38 * Any person wishing to distribute modifications to the Software is
Chris@4 39 * requested to send the modifications to the original developer so that
Chris@4 40 * they can be incorporated into the canonical version. It is also
Chris@4 41 * requested that these non-binding requests be included along with the
Chris@4 42 * license above.
Chris@4 43 */
Chris@4 44
Chris@4 45 #include <stdio.h>
Chris@4 46 #include <math.h>
Chris@4 47 #include "portaudio.h"
Chris@4 48 #include "pa_util.h"
Chris@4 49
Chris@4 50 #define NUM_BEEPS (3)
Chris@4 51 #define SAMPLE_RATE (44100)
Chris@4 52 #define SAMPLE_PERIOD (1.0/44100.0)
Chris@4 53 #define FRAMES_PER_BUFFER (256)
Chris@4 54 #define BEEP_DURATION (400)
Chris@4 55 #define IDLE_DURATION (SAMPLE_RATE*2) /* 2 seconds */
Chris@4 56 #define SLEEP_MSEC (50)
Chris@4 57
Chris@4 58 #define STATE_BKG_IDLE (0)
Chris@4 59 #define STATE_BKG_BEEPING (1)
Chris@4 60
Chris@4 61 typedef struct
Chris@4 62 {
Chris@4 63 float leftPhase;
Chris@4 64 float rightPhase;
Chris@4 65 int state;
Chris@4 66 int beepCountdown;
Chris@4 67 int idleCountdown;
Chris@4 68 }
Chris@4 69 paTestData;
Chris@4 70
Chris@4 71 static void InitializeTestData( paTestData *testData )
Chris@4 72 {
Chris@4 73 testData->leftPhase = 0;
Chris@4 74 testData->rightPhase = 0;
Chris@4 75 testData->state = STATE_BKG_BEEPING;
Chris@4 76 testData->beepCountdown = BEEP_DURATION;
Chris@4 77 testData->idleCountdown = IDLE_DURATION;
Chris@4 78 }
Chris@4 79
Chris@4 80 /* This routine will be called by the PortAudio engine when audio is needed.
Chris@4 81 ** It may called at interrupt level on some machines so don't do anything
Chris@4 82 ** that could mess up the system like calling malloc() or free().
Chris@4 83 */
Chris@4 84 static int patestCallback( const void *inputBuffer, void *outputBuffer,
Chris@4 85 unsigned long framesPerBuffer,
Chris@4 86 const PaStreamCallbackTimeInfo *timeInfo,
Chris@4 87 PaStreamCallbackFlags statusFlags, void *userData )
Chris@4 88 {
Chris@4 89 /* Cast data passed through stream to our structure. */
Chris@4 90 paTestData *data = (paTestData*)userData;
Chris@4 91 float *out = (float*)outputBuffer;
Chris@4 92 unsigned int i;
Chris@4 93 int result = paContinue;
Chris@4 94
Chris@4 95 /* supress unused parameter warnings */
Chris@4 96 (void) inputBuffer;
Chris@4 97 (void) timeInfo;
Chris@4 98 (void) statusFlags;
Chris@4 99
Chris@4 100 for( i=0; i<framesPerBuffer; i++ )
Chris@4 101 {
Chris@4 102 switch( data->state )
Chris@4 103 {
Chris@4 104 case STATE_BKG_IDLE:
Chris@4 105 *out++ = 0.0; /* left */
Chris@4 106 *out++ = 0.0; /* right */
Chris@4 107 --data->idleCountdown;
Chris@4 108
Chris@4 109 if( data->idleCountdown <= 0 ) result = paComplete;
Chris@4 110 break;
Chris@4 111
Chris@4 112 case STATE_BKG_BEEPING:
Chris@4 113 if( data->beepCountdown <= 0 )
Chris@4 114 {
Chris@4 115 data->state = STATE_BKG_IDLE;
Chris@4 116 *out++ = 0.0; /* left */
Chris@4 117 *out++ = 0.0; /* right */
Chris@4 118 }
Chris@4 119 else
Chris@4 120 {
Chris@4 121 /* Play sawtooth wave. */
Chris@4 122 *out++ = data->leftPhase; /* left */
Chris@4 123 *out++ = data->rightPhase; /* right */
Chris@4 124 /* Generate simple sawtooth phaser that ranges between -1.0 and 1.0. */
Chris@4 125 data->leftPhase += 0.01f;
Chris@4 126 /* When signal reaches top, drop back down. */
Chris@4 127 if( data->leftPhase >= 1.0f ) data->leftPhase -= 2.0f;
Chris@4 128 /* higher pitch so we can distinguish left and right. */
Chris@4 129 data->rightPhase += 0.03f;
Chris@4 130 if( data->rightPhase >= 1.0f ) data->rightPhase -= 2.0f;
Chris@4 131 }
Chris@4 132 --data->beepCountdown;
Chris@4 133 break;
Chris@4 134 }
Chris@4 135 }
Chris@4 136
Chris@4 137 return result;
Chris@4 138 }
Chris@4 139
Chris@4 140 /*******************************************************************/
Chris@4 141 static PaError DoTest( int flags )
Chris@4 142 {
Chris@4 143 PaStream *stream;
Chris@4 144 PaError err = paNoError;
Chris@4 145 paTestData data;
Chris@4 146 PaStreamParameters outputParameters;
Chris@4 147
Chris@4 148 InitializeTestData( &data );
Chris@4 149
Chris@4 150 outputParameters.device = Pa_GetDefaultOutputDevice();
Chris@4 151 if (outputParameters.device == paNoDevice) {
Chris@4 152 fprintf(stderr,"Error: No default output device.\n");
Chris@4 153 goto error;
Chris@4 154 }
Chris@4 155 outputParameters.channelCount = 2;
Chris@4 156 outputParameters.hostApiSpecificStreamInfo = NULL;
Chris@4 157 outputParameters.sampleFormat = paFloat32;
Chris@4 158 outputParameters.suggestedLatency = Pa_GetDeviceInfo( outputParameters.device )->defaultHighOutputLatency;
Chris@4 159
Chris@4 160 /* Open an audio I/O stream. */
Chris@4 161 err = Pa_OpenStream(
Chris@4 162 &stream,
Chris@4 163 NULL, /* no input */
Chris@4 164 &outputParameters,
Chris@4 165 SAMPLE_RATE,
Chris@4 166 FRAMES_PER_BUFFER, /* frames per buffer */
Chris@4 167 paClipOff | flags, /* we won't output out of range samples so don't bother clipping them */
Chris@4 168 patestCallback,
Chris@4 169 &data );
Chris@4 170 if( err != paNoError ) goto error;
Chris@4 171
Chris@4 172
Chris@4 173 err = Pa_StartStream( stream );
Chris@4 174 if( err != paNoError ) goto error;
Chris@4 175
Chris@4 176 printf("hear \"BEEP\"\n" );
Chris@4 177 fflush(stdout);
Chris@4 178
Chris@4 179 while( ( err = Pa_IsStreamActive( stream ) ) == 1 ) Pa_Sleep(SLEEP_MSEC);
Chris@4 180 if( err < 0 ) goto error;
Chris@4 181
Chris@4 182 err = Pa_StopStream( stream );
Chris@4 183 if( err != paNoError ) goto error;
Chris@4 184
Chris@4 185 err = Pa_CloseStream( stream );
Chris@4 186 if( err != paNoError ) goto error;
Chris@4 187
Chris@4 188 return err;
Chris@4 189 error:
Chris@4 190 return err;
Chris@4 191 }
Chris@4 192
Chris@4 193 /*******************************************************************/
Chris@4 194 int main(void);
Chris@4 195 int main(void)
Chris@4 196 {
Chris@4 197 PaError err = paNoError;
Chris@4 198 int i;
Chris@4 199
Chris@4 200 /* Initialize library before making any other calls. */
Chris@4 201 err = Pa_Initialize();
Chris@4 202 if( err != paNoError ) goto error;
Chris@4 203
Chris@4 204 printf("PortAudio Test: Testing stream playback with no priming.\n");
Chris@4 205 printf("PortAudio Test: you should see BEEP before you hear it.\n");
Chris@4 206 printf("BEEP %d times.\n", NUM_BEEPS );
Chris@4 207
Chris@4 208 for( i=0; i< NUM_BEEPS; ++i )
Chris@4 209 {
Chris@4 210 err = DoTest( 0 );
Chris@4 211 if( err != paNoError )
Chris@4 212 goto error;
Chris@4 213 }
Chris@4 214
Chris@4 215 printf("PortAudio Test: Testing stream playback with priming.\n");
Chris@4 216 printf("PortAudio Test: you should see BEEP around the same time you hear it.\n");
Chris@4 217 for( i=0; i< NUM_BEEPS; ++i )
Chris@4 218 {
Chris@4 219 err = DoTest( paPrimeOutputBuffersUsingStreamCallback );
Chris@4 220 if( err != paNoError )
Chris@4 221 goto error;
Chris@4 222 }
Chris@4 223
Chris@4 224 printf("Test finished.\n");
Chris@4 225
Chris@4 226 Pa_Terminate();
Chris@4 227 return err;
Chris@4 228 error:
Chris@4 229 Pa_Terminate();
Chris@4 230 fprintf( stderr, "An error occured while using the portaudio stream\n" );
Chris@4 231 fprintf( stderr, "Error number: %d\n", err );
Chris@4 232 fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) );
Chris@4 233 return err;
Chris@4 234 }