PortAudio 2.0
|
00001 00007 /* 00008 * $Id: paex_write_sine.c 1865 2012-09-01 21:16:25Z 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 printf("PortAudio Test: output sine wave. SR = %d, BufSize = %d\n", SAMPLE_RATE, FRAMES_PER_BUFFER); 00076 00077 /* initialise sinusoidal wavetable */ 00078 for( i=0; i<TABLE_SIZE; i++ ) 00079 { 00080 sine[i] = (float) sin( ((double)i/(double)TABLE_SIZE) * M_PI * 2. ); 00081 } 00082 00083 00084 err = Pa_Initialize(); 00085 if( err != paNoError ) goto error; 00086 00087 outputParameters.device = Pa_GetDefaultOutputDevice(); /* default output device */ 00088 if (outputParameters.device == paNoDevice) { 00089 fprintf(stderr,"Error: No default output device.\n"); 00090 goto error; 00091 } 00092 outputParameters.channelCount = 2; /* stereo output */ 00093 outputParameters.sampleFormat = paFloat32; /* 32 bit floating point output */ 00094 outputParameters.suggestedLatency = 0.050; // Pa_GetDeviceInfo( outputParameters.device )->defaultLowOutputLatency; 00095 outputParameters.hostApiSpecificStreamInfo = NULL; 00096 00097 err = Pa_OpenStream( 00098 &stream, 00099 NULL, /* no input */ 00100 &outputParameters, 00101 SAMPLE_RATE, 00102 FRAMES_PER_BUFFER, 00103 paClipOff, /* we won't output out of range samples so don't bother clipping them */ 00104 NULL, /* no callback, use blocking API */ 00105 NULL ); /* no callback, so no callback userData */ 00106 if( err != paNoError ) goto error; 00107 00108 00109 printf( "Play 3 times, higher each time.\n" ); 00110 00111 for( k=0; k < 3; ++k ) 00112 { 00113 err = Pa_StartStream( stream ); 00114 if( err != paNoError ) goto error; 00115 00116 printf("Play for %d seconds.\n", NUM_SECONDS ); 00117 00118 bufferCount = ((NUM_SECONDS * SAMPLE_RATE) / FRAMES_PER_BUFFER); 00119 00120 for( i=0; i < bufferCount; i++ ) 00121 { 00122 for( j=0; j < FRAMES_PER_BUFFER; j++ ) 00123 { 00124 buffer[j][0] = sine[left_phase]; /* left */ 00125 buffer[j][1] = sine[right_phase]; /* right */ 00126 left_phase += left_inc; 00127 if( left_phase >= TABLE_SIZE ) left_phase -= TABLE_SIZE; 00128 right_phase += right_inc; 00129 if( right_phase >= TABLE_SIZE ) right_phase -= TABLE_SIZE; 00130 } 00131 00132 err = Pa_WriteStream( stream, buffer, FRAMES_PER_BUFFER ); 00133 if( err != paNoError ) goto error; 00134 } 00135 00136 err = Pa_StopStream( stream ); 00137 if( err != paNoError ) goto error; 00138 00139 ++left_inc; 00140 ++right_inc; 00141 00142 Pa_Sleep( 1000 ); 00143 } 00144 00145 err = Pa_CloseStream( stream ); 00146 if( err != paNoError ) goto error; 00147 00148 Pa_Terminate(); 00149 printf("Test finished.\n"); 00150 00151 return err; 00152 00153 error: 00154 fprintf( stderr, "An error occured while using the portaudio stream\n" ); 00155 fprintf( stderr, "Error number: %d\n", err ); 00156 fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) ); 00157 // Print more information about the error. 00158 if( err == paUnanticipatedHostError ) 00159 { 00160 const PaHostErrorInfo *hostErrorInfo = Pa_GetLastHostErrorInfo(); 00161 fprintf( stderr, "Host API error = #%ld, hostApiType = %d\n", hostErrorInfo->errorCode, hostErrorInfo->hostApiType ); 00162 fprintf( stderr, "Host API error = %s\n", hostErrorInfo->errorText ); 00163 } 00164 Pa_Terminate(); 00165 return err; 00166 }