annotate src/portaudio/test/patest_sine_formats.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_sine_formats.c
Chris@4 2 @ingroup test_src
Chris@4 3 @brief Play a sine wave for several seconds. Test various data formats.
Chris@4 4 @author Phil Burk
Chris@4 5 */
Chris@4 6 /*
Chris@4 7 * $Id: patest_sine_formats.c 1097 2006-08-26 08:27:53Z rossb $
Chris@4 8 *
Chris@4 9 * This program uses the PortAudio Portable Audio Library.
Chris@4 10 * For more information see: http://www.portaudio.com
Chris@4 11 * Copyright (c) 1999-2000 Ross Bencina and Phil Burk
Chris@4 12 *
Chris@4 13 * Permission is hereby granted, free of charge, to any person obtaining
Chris@4 14 * a copy of this software and associated documentation files
Chris@4 15 * (the "Software"), to deal in the Software without restriction,
Chris@4 16 * including without limitation the rights to use, copy, modify, merge,
Chris@4 17 * publish, distribute, sublicense, and/or sell copies of the Software,
Chris@4 18 * and to permit persons to whom the Software is furnished to do so,
Chris@4 19 * subject to the following conditions:
Chris@4 20 *
Chris@4 21 * The above copyright notice and this permission notice shall be
Chris@4 22 * included in all copies or substantial portions of the Software.
Chris@4 23 *
Chris@4 24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
Chris@4 25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
Chris@4 26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
Chris@4 27 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
Chris@4 28 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
Chris@4 29 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
Chris@4 30 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Chris@4 31 */
Chris@4 32
Chris@4 33 /*
Chris@4 34 * The text above constitutes the entire PortAudio license; however,
Chris@4 35 * the PortAudio community also makes the following non-binding requests:
Chris@4 36 *
Chris@4 37 * Any person wishing to distribute modifications to the Software is
Chris@4 38 * requested to send the modifications to the original developer so that
Chris@4 39 * they can be incorporated into the canonical version. It is also
Chris@4 40 * requested that these non-binding requests be included along with the
Chris@4 41 * license above.
Chris@4 42 */
Chris@4 43 #include <stdio.h>
Chris@4 44 #include <math.h>
Chris@4 45 #include "portaudio.h"
Chris@4 46
Chris@4 47 #define NUM_SECONDS (10)
Chris@4 48 #define SAMPLE_RATE (44100)
Chris@4 49 #define FRAMES_PER_BUFFER (512)
Chris@4 50 #define LEFT_FREQ (SAMPLE_RATE/256.0) /* So we hit 1.0 */
Chris@4 51 #define RIGHT_FREQ (500.0)
Chris@4 52 #define AMPLITUDE (1.0)
Chris@4 53
Chris@4 54 /* Select ONE format for testing. */
Chris@4 55 #define TEST_UINT8 (0)
Chris@4 56 #define TEST_INT8 (0)
Chris@4 57 #define TEST_INT16 (1)
Chris@4 58 #define TEST_FLOAT32 (0)
Chris@4 59
Chris@4 60 #if TEST_UINT8
Chris@4 61 #define TEST_FORMAT paUInt8
Chris@4 62 typedef unsigned char SAMPLE_t;
Chris@4 63 #define SAMPLE_ZERO (0x80)
Chris@4 64 #define DOUBLE_TO_SAMPLE(x) (SAMPLE_ZERO + (SAMPLE_t)(127.0 * (x)))
Chris@4 65 #define FORMAT_NAME "Unsigned 8 Bit"
Chris@4 66
Chris@4 67 #elif TEST_INT8
Chris@4 68 #define TEST_FORMAT paInt8
Chris@4 69 typedef char SAMPLE_t;
Chris@4 70 #define SAMPLE_ZERO (0)
Chris@4 71 #define DOUBLE_TO_SAMPLE(x) (SAMPLE_ZERO + (SAMPLE_t)(127.0 * (x)))
Chris@4 72 #define FORMAT_NAME "Signed 8 Bit"
Chris@4 73
Chris@4 74 #elif TEST_INT16
Chris@4 75 #define TEST_FORMAT paInt16
Chris@4 76 typedef short SAMPLE_t;
Chris@4 77 #define SAMPLE_ZERO (0)
Chris@4 78 #define DOUBLE_TO_SAMPLE(x) (SAMPLE_ZERO + (SAMPLE_t)(32767 * (x)))
Chris@4 79 #define FORMAT_NAME "Signed 16 Bit"
Chris@4 80
Chris@4 81 #elif TEST_FLOAT32
Chris@4 82 #define TEST_FORMAT paFloat32
Chris@4 83 typedef float SAMPLE_t;
Chris@4 84 #define SAMPLE_ZERO (0.0)
Chris@4 85 #define DOUBLE_TO_SAMPLE(x) ((SAMPLE_t)(x))
Chris@4 86 #define FORMAT_NAME "Float 32 Bit"
Chris@4 87 #endif
Chris@4 88
Chris@4 89 #ifndef M_PI
Chris@4 90 #define M_PI (3.14159265)
Chris@4 91 #endif
Chris@4 92
Chris@4 93
Chris@4 94 typedef struct
Chris@4 95 {
Chris@4 96 double left_phase;
Chris@4 97 double right_phase;
Chris@4 98 unsigned int framesToGo;
Chris@4 99 }
Chris@4 100 paTestData;
Chris@4 101 /* This routine will be called by the PortAudio engine when audio is needed.
Chris@4 102 ** It may called at interrupt level on some machines so don't do anything
Chris@4 103 ** that could mess up the system like calling malloc() or free().
Chris@4 104 */
Chris@4 105 static int patestCallback( const void *inputBuffer,
Chris@4 106 void *outputBuffer,
Chris@4 107 unsigned long framesPerBuffer,
Chris@4 108 const PaStreamCallbackTimeInfo* timeInfo,
Chris@4 109 PaStreamCallbackFlags statusFlags,
Chris@4 110 void *userData )
Chris@4 111 {
Chris@4 112 paTestData *data = (paTestData*)userData;
Chris@4 113 SAMPLE_t *out = (SAMPLE_t *)outputBuffer;
Chris@4 114 int i;
Chris@4 115 int framesToCalc;
Chris@4 116 int finished = 0;
Chris@4 117 (void) inputBuffer; /* Prevent unused variable warnings. */
Chris@4 118
Chris@4 119 if( data->framesToGo < framesPerBuffer )
Chris@4 120 {
Chris@4 121 framesToCalc = data->framesToGo;
Chris@4 122 data->framesToGo = 0;
Chris@4 123 finished = 1;
Chris@4 124 }
Chris@4 125 else
Chris@4 126 {
Chris@4 127 framesToCalc = framesPerBuffer;
Chris@4 128 data->framesToGo -= framesPerBuffer;
Chris@4 129 }
Chris@4 130
Chris@4 131 for( i=0; i<framesToCalc; i++ )
Chris@4 132 {
Chris@4 133 data->left_phase += (LEFT_FREQ / SAMPLE_RATE);
Chris@4 134 if( data->left_phase > 1.0) data->left_phase -= 1.0;
Chris@4 135 *out++ = DOUBLE_TO_SAMPLE( AMPLITUDE * sin( (data->left_phase * M_PI * 2. )));
Chris@4 136
Chris@4 137 data->right_phase += (RIGHT_FREQ / SAMPLE_RATE);
Chris@4 138 if( data->right_phase > 1.0) data->right_phase -= 1.0;
Chris@4 139 *out++ = DOUBLE_TO_SAMPLE( AMPLITUDE * sin( (data->right_phase * M_PI * 2. )));
Chris@4 140 }
Chris@4 141 /* zero remainder of final buffer */
Chris@4 142 for( ; i<(int)framesPerBuffer; i++ )
Chris@4 143 {
Chris@4 144 *out++ = SAMPLE_ZERO; /* left */
Chris@4 145 *out++ = SAMPLE_ZERO; /* right */
Chris@4 146 }
Chris@4 147 return finished;
Chris@4 148 }
Chris@4 149 /*******************************************************************/
Chris@4 150 int main(void);
Chris@4 151 int main(void)
Chris@4 152 {
Chris@4 153 PaStream *stream;
Chris@4 154 PaStreamParameters outputParameters;
Chris@4 155 PaError err;
Chris@4 156 paTestData data;
Chris@4 157 int totalSamps;
Chris@4 158
Chris@4 159 printf("PortAudio Test: output " FORMAT_NAME "\n");
Chris@4 160
Chris@4 161 data.left_phase = data.right_phase = 0.0;
Chris@4 162 data.framesToGo = totalSamps = NUM_SECONDS * SAMPLE_RATE; /* Play for a few seconds. */
Chris@4 163 err = Pa_Initialize();
Chris@4 164 if( err != paNoError ) goto error;
Chris@4 165
Chris@4 166
Chris@4 167 outputParameters.device = Pa_GetDefaultOutputDevice(); /* Default output device. */
Chris@4 168 outputParameters.channelCount = 2; /* Stereo output */
Chris@4 169 outputParameters.sampleFormat = TEST_FORMAT; /* Selected above. */
Chris@4 170 outputParameters.suggestedLatency = Pa_GetDeviceInfo(outputParameters.device)->defaultLowOutputLatency;
Chris@4 171 outputParameters.hostApiSpecificStreamInfo = NULL;
Chris@4 172 err = Pa_OpenStream( &stream,
Chris@4 173 NULL, /* No input. */
Chris@4 174 &outputParameters, /* As above. */
Chris@4 175 SAMPLE_RATE,
Chris@4 176 FRAMES_PER_BUFFER,
Chris@4 177 paClipOff, /* we won't output out of range samples so don't bother clipping them */
Chris@4 178 patestCallback,
Chris@4 179 &data );
Chris@4 180 if( err != paNoError ) goto error;
Chris@4 181
Chris@4 182 err = Pa_StartStream( stream );
Chris@4 183 if( err != paNoError ) goto error;
Chris@4 184
Chris@4 185 printf("Waiting %d seconds for sound to finish.\n", NUM_SECONDS );
Chris@4 186
Chris@4 187 while( ( err = Pa_IsStreamActive( stream ) ) == 1 ) Pa_Sleep(100);
Chris@4 188 if( err < 0 ) goto error;
Chris@4 189
Chris@4 190 err = Pa_CloseStream( stream );
Chris@4 191 if( err != paNoError ) goto error;
Chris@4 192 Pa_Terminate();
Chris@4 193
Chris@4 194 printf("PortAudio Test Finished: " FORMAT_NAME "\n");
Chris@4 195
Chris@4 196 return err;
Chris@4 197 error:
Chris@4 198 Pa_Terminate();
Chris@4 199 fprintf( stderr, "An error occured while using the portaudio stream\n" );
Chris@4 200 fprintf( stderr, "Error number: %d\n", err );
Chris@4 201 fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) );
Chris@4 202 return err;
Chris@4 203 }