paex_write_sine_nonint.c

Go to the documentation of this file.
00001 
00007 /*
00008  * $Id: patest_write_sine.c 1368 2008-03-01 00:38:27Z rossb $
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     
00067         float leftBuffer[FRAMES_PER_BUFFER];
00068     float rightBuffer[FRAMES_PER_BUFFER];
00069     void *buffers[2]; /* points to both non-interleaved buffers. */
00070         
00071     float sine[TABLE_SIZE]; /* sine wavetable */
00072     int left_phase = 0;
00073     int right_phase = 0;
00074     int left_inc = 1;
00075     int right_inc = 3; /* higher pitch so we can distinguish left and right. */
00076     int i, j, k;
00077     int bufferCount;
00078 
00079     
00080     printf("PortAudio Test: output sine wave NON-INTERLEAVED. SR = %d, BufSize = %d\n", SAMPLE_RATE, FRAMES_PER_BUFFER);
00081     
00082     /* initialise sinusoidal wavetable */
00083     for( i=0; i<TABLE_SIZE; i++ )
00084     {
00085         sine[i] = (float) sin( ((double)i/(double)TABLE_SIZE) * M_PI * 2. );
00086     }
00087 
00088     
00089     err = Pa_Initialize();
00090     if( err != paNoError ) goto error;
00091 
00092     outputParameters.device = Pa_GetDefaultOutputDevice(); /* default output device */
00093     if (outputParameters.device == paNoDevice) {
00094       fprintf(stderr,"Error: No default output device.\n");
00095       goto error;
00096     }
00097     outputParameters.channelCount = 2;       /* stereo output */
00098     outputParameters.sampleFormat = paFloat32 | paNonInterleaved; /* 32 bit floating point output NON-INTERLEAVED */
00099     outputParameters.suggestedLatency = Pa_GetDeviceInfo( outputParameters.device )->defaultLowOutputLatency;
00100     outputParameters.hostApiSpecificStreamInfo = NULL;
00101 
00102     err = Pa_OpenStream(
00103               &stream,
00104               NULL, /* no input */
00105               &outputParameters,
00106               SAMPLE_RATE,
00107               FRAMES_PER_BUFFER,
00108               paClipOff,      /* we won't output out of range samples so don't bother clipping them */
00109               NULL, /* no callback, use blocking API */
00110               NULL ); /* no callback, so no callback userData */
00111     if( err != paNoError ) goto error;
00112 
00113 
00114     printf( "Play 3 times, higher each time.\n" );
00115     
00116         /* Set up array of buffer pointers for Pa_WriteStream */
00117         buffers[0] = leftBuffer;
00118         buffers[1] = rightBuffer;
00119         
00120     for( k=0; k < 3; ++k )
00121     {
00122         err = Pa_StartStream( stream );
00123         if( err != paNoError ) goto error;
00124 
00125         printf("Play for %d seconds.\n", NUM_SECONDS );
00126 
00127         bufferCount = ((NUM_SECONDS * SAMPLE_RATE) / FRAMES_PER_BUFFER);
00128 
00129         for( i=0; i < bufferCount; i++ )
00130         {
00131             for( j=0; j < FRAMES_PER_BUFFER; j++ )
00132             {
00133                 leftBuffer[j] = sine[left_phase];  /* left */
00134                 rightBuffer[j] = sine[right_phase];  /* right */
00135                 left_phase += left_inc;
00136                 if( left_phase >= TABLE_SIZE ) left_phase -= TABLE_SIZE;
00137                 right_phase += right_inc;
00138                 if( right_phase >= TABLE_SIZE ) right_phase -= TABLE_SIZE;
00139             }
00140 
00141             err = Pa_WriteStream( stream, buffers, FRAMES_PER_BUFFER );
00142             if( err != paNoError ) goto error;
00143         }   
00144 
00145         err = Pa_StopStream( stream );
00146         if( err != paNoError ) goto error;
00147 
00148         ++left_inc;
00149         ++right_inc;
00150 
00151         Pa_Sleep( 1000 );
00152     }
00153 
00154     err = Pa_CloseStream( stream );
00155     if( err != paNoError ) goto error;
00156 
00157     Pa_Terminate();
00158     printf("Test finished.\n");
00159     
00160     return err;
00161 error:
00162     Pa_Terminate();
00163     fprintf( stderr, "An error occured while using the portaudio stream\n" );
00164     fprintf( stderr, "Error number: %d\n", err );
00165     fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) );
00166     return err;
00167 }

Generated on Sat Aug 6 19:33:25 2016 for PortAudio by  doxygen 1.5.6