PortAudio
2.0
|
00001 00007 /* 00008 * $Id: paex_write_sine.c 1752 2011-09-08 03:21:55Z philburk $ 00009 * 00010 * This program uses the PortAudio Portable Audio Library. 00011 * For more information see: http://www.portaudio.com/ 00012 * Copyright (c) 1999-2000 Ross Bencina and Phil Burk 00013 * 00014 * Permission is hereby granted, free of charge, to any person obtaining 00015 * a copy of this software and associated documentation files 00016 * (the "Software"), to deal in the Software without restriction, 00017 * including without limitation the rights to use, copy, modify, merge, 00018 * publish, distribute, sublicense, and/or sell copies of the Software, 00019 * and to permit persons to whom the Software is furnished to do so, 00020 * subject to the following conditions: 00021 * 00022 * The above copyright notice and this permission notice shall be 00023 * included in all copies or substantial portions of the Software. 00024 * 00025 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 00026 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 00027 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 00028 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR 00029 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 00030 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 00031 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 00032 */ 00033 00034 /* 00035 * The text above constitutes the entire PortAudio license; however, 00036 * the PortAudio community also makes the following non-binding requests: 00037 * 00038 * Any person wishing to distribute modifications to the Software is 00039 * requested to send the modifications to the original developer so that 00040 * they can be incorporated into the canonical version. It is also 00041 * requested that these non-binding requests be included along with the 00042 * license above. 00043 */ 00044 00045 #include <stdio.h> 00046 #include <math.h> 00047 #include "portaudio.h" 00048 00049 #define NUM_SECONDS (5) 00050 #define SAMPLE_RATE (44100) 00051 #define FRAMES_PER_BUFFER (1024) 00052 00053 #ifndef M_PI 00054 #define M_PI (3.14159265) 00055 #endif 00056 00057 #define TABLE_SIZE (200) 00058 00059 00060 int main(void); 00061 int main(void) 00062 { 00063 PaStreamParameters outputParameters; 00064 PaStream *stream; 00065 PaError err; 00066 float buffer[FRAMES_PER_BUFFER][2]; /* stereo output buffer */ 00067 float sine[TABLE_SIZE]; /* sine wavetable */ 00068 int left_phase = 0; 00069 int right_phase = 0; 00070 int left_inc = 1; 00071 int right_inc = 3; /* higher pitch so we can distinguish left and right. */ 00072 int i, j, k; 00073 int bufferCount; 00074 00075 00076 printf("PortAudio Test: output sine wave. SR = %d, BufSize = %d\n", SAMPLE_RATE, FRAMES_PER_BUFFER); 00077 00078 /* initialise sinusoidal wavetable */ 00079 for( i=0; i<TABLE_SIZE; i++ ) 00080 { 00081 sine[i] = (float) sin( ((double)i/(double)TABLE_SIZE) * M_PI * 2. ); 00082 } 00083 00084 00085 err = Pa_Initialize(); 00086 if( err != paNoError ) goto error; 00087 00088 outputParameters.device = Pa_GetDefaultOutputDevice(); /* default output device */ 00089 if (outputParameters.device == paNoDevice) { 00090 fprintf(stderr,"Error: No default output device.\n"); 00091 goto error; 00092 } 00093 outputParameters.channelCount = 2; /* stereo output */ 00094 outputParameters.sampleFormat = paFloat32; /* 32 bit floating point output */ 00095 outputParameters.suggestedLatency = Pa_GetDeviceInfo( outputParameters.device )->defaultLowOutputLatency; 00096 outputParameters.hostApiSpecificStreamInfo = NULL; 00097 00098 err = Pa_OpenStream( 00099 &stream, 00100 NULL, /* no input */ 00101 &outputParameters, 00102 SAMPLE_RATE, 00103 FRAMES_PER_BUFFER, 00104 paClipOff, /* we won't output out of range samples so don't bother clipping them */ 00105 NULL, /* no callback, use blocking API */ 00106 NULL ); /* no callback, so no callback userData */ 00107 if( err != paNoError ) goto error; 00108 00109 00110 printf( "Play 3 times, higher each time.\n" ); 00111 00112 for( k=0; k < 3; ++k ) 00113 { 00114 err = Pa_StartStream( stream ); 00115 if( err != paNoError ) goto error; 00116 00117 printf("Play for %d seconds.\n", NUM_SECONDS ); 00118 00119 bufferCount = ((NUM_SECONDS * SAMPLE_RATE) / FRAMES_PER_BUFFER); 00120 00121 for( i=0; i < bufferCount; i++ ) 00122 { 00123 for( j=0; j < FRAMES_PER_BUFFER; j++ ) 00124 { 00125 buffer[j][0] = sine[left_phase]; /* left */ 00126 buffer[j][1] = sine[right_phase]; /* right */ 00127 left_phase += left_inc; 00128 if( left_phase >= TABLE_SIZE ) left_phase -= TABLE_SIZE; 00129 right_phase += right_inc; 00130 if( right_phase >= TABLE_SIZE ) right_phase -= TABLE_SIZE; 00131 } 00132 00133 err = Pa_WriteStream( stream, buffer, FRAMES_PER_BUFFER ); 00134 if( err != paNoError ) goto error; 00135 } 00136 00137 err = Pa_StopStream( stream ); 00138 if( err != paNoError ) goto error; 00139 00140 ++left_inc; 00141 ++right_inc; 00142 00143 Pa_Sleep( 1000 ); 00144 } 00145 00146 err = Pa_CloseStream( stream ); 00147 if( err != paNoError ) goto error; 00148 00149 Pa_Terminate(); 00150 printf("Test finished.\n"); 00151 00152 return err; 00153 error: 00154 Pa_Terminate(); 00155 fprintf( stderr, "An error occured while using the portaudio stream\n" ); 00156 fprintf( stderr, "Error number: %d\n", err ); 00157 fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) ); 00158 return err; 00159 }