patest_mono.c (5350B)
1 /** @file patest_mono.c 2 @ingroup test_src 3 @brief Play a monophonic sine wave using the Portable Audio api for several seconds. 4 @author Phil Burk http://www.softsynth.com 5 */ 6 /* 7 * $Id$ 8 * 9 * Authors: 10 * Ross Bencina <rossb@audiomulch.com> 11 * Phil Burk <philburk@softsynth.com> 12 * 13 * This program uses the PortAudio Portable Audio Library. 14 * For more information see: http://www.portaudio.com 15 * Copyright (c) 1999-2000 Ross Bencina and Phil Burk 16 * 17 * Permission is hereby granted, free of charge, to any person obtaining 18 * a copy of this software and associated documentation files 19 * (the "Software"), to deal in the Software without restriction, 20 * including without limitation the rights to use, copy, modify, merge, 21 * publish, distribute, sublicense, and/or sell copies of the Software, 22 * and to permit persons to whom the Software is furnished to do so, 23 * subject to the following conditions: 24 * 25 * The above copyright notice and this permission notice shall be 26 * included in all copies or substantial portions of the Software. 27 * 28 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 29 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 30 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 31 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR 32 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 33 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 34 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 35 */ 36 37 /* 38 * The text above constitutes the entire PortAudio license; however, 39 * the PortAudio community also makes the following non-binding requests: 40 * 41 * Any person wishing to distribute modifications to the Software is 42 * requested to send the modifications to the original developer so that 43 * they can be incorporated into the canonical version. It is also 44 * requested that these non-binding requests be included along with the 45 * license above. 46 */ 47 48 #include <stdio.h> 49 #include <math.h> 50 #include "portaudio.h" 51 52 #define NUM_SECONDS (10) 53 #define SAMPLE_RATE (44100) 54 #define AMPLITUDE (0.8) 55 #define FRAMES_PER_BUFFER (64) 56 #define OUTPUT_DEVICE Pa_GetDefaultOutputDevice() 57 58 #ifndef M_PI 59 #define M_PI (3.14159265) 60 #endif 61 62 #define TABLE_SIZE (200) 63 typedef struct 64 { 65 float sine[TABLE_SIZE]; 66 int phase; 67 } 68 paTestData; 69 70 /* This routine will be called by the PortAudio engine when audio is needed. 71 ** It may called at interrupt level on some machines so don't do anything 72 ** that could mess up the system like calling malloc() or free(). 73 */ 74 static int patestCallback( const void *inputBuffer, void *outputBuffer, 75 unsigned long framesPerBuffer, 76 const PaStreamCallbackTimeInfo* timeInfo, 77 PaStreamCallbackFlags statusFlags, 78 void *userData ) 79 { 80 paTestData *data = (paTestData*)userData; 81 float *out = (float*)outputBuffer; 82 unsigned long i; 83 int finished = 0; 84 /* avoid unused variable warnings */ 85 (void) inputBuffer; 86 (void) timeInfo; 87 (void) statusFlags; 88 for( i=0; i<framesPerBuffer; i++ ) 89 { 90 *out++ = data->sine[data->phase]; /* left */ 91 data->phase += 1; 92 if( data->phase >= TABLE_SIZE ) data->phase -= TABLE_SIZE; 93 } 94 return finished; 95 } 96 97 /*******************************************************************/ 98 int main(void); 99 int main(void) 100 { 101 PaStreamParameters outputParameters; 102 PaStream *stream; 103 PaError err; 104 paTestData data; 105 int i; 106 printf("PortAudio Test: output MONO sine wave. SR = %d, BufSize = %d\n", SAMPLE_RATE, FRAMES_PER_BUFFER); 107 /* initialise sinusoidal wavetable */ 108 for( i=0; i<TABLE_SIZE; i++ ) 109 { 110 data.sine[i] = (float) (AMPLITUDE * sin( ((double)i/(double)TABLE_SIZE) * M_PI * 2. )); 111 } 112 data.phase = 0; 113 114 err = Pa_Initialize(); 115 if( err != paNoError ) goto error; 116 117 outputParameters.device = OUTPUT_DEVICE; 118 outputParameters.channelCount = 1; /* MONO output */ 119 outputParameters.sampleFormat = paFloat32; /* 32 bit floating point output */ 120 outputParameters.suggestedLatency = Pa_GetDeviceInfo( outputParameters.device )->defaultLowOutputLatency; 121 outputParameters.hostApiSpecificStreamInfo = NULL; 122 123 err = Pa_OpenStream( 124 &stream, 125 NULL, /* no input */ 126 &outputParameters, 127 SAMPLE_RATE, 128 FRAMES_PER_BUFFER, 129 paClipOff, /* we won't output out of range samples so don't bother clipping them */ 130 patestCallback, 131 &data ); 132 if( err != paNoError ) goto error; 133 134 err = Pa_StartStream( stream ); 135 if( err != paNoError ) goto error; 136 137 printf("Play for %d seconds.\n", NUM_SECONDS ); fflush(stdout); 138 Pa_Sleep( NUM_SECONDS * 1000 ); 139 140 err = Pa_StopStream( stream ); 141 if( err != paNoError ) goto error; 142 143 err = Pa_CloseStream( stream ); 144 if( err != paNoError ) goto error; 145 146 Pa_Terminate(); 147 printf("Test finished.\n"); 148 return err; 149 error: 150 Pa_Terminate(); 151 fprintf( stderr, "An error occured while using the portaudio stream\n" ); 152 fprintf( stderr, "Error number: %d\n", err ); 153 fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) ); 154 return err; 155 } 156