2 * $Id: patest_mono.c,v 1.1 2006/06/10 21:30:56 dmazzoni Exp $
4 * Play a monophonic sine wave using the Portable Audio api for several seconds.
7 * Ross Bencina <rossb@audiomulch.com>
8 * Phil Burk <philburk@softsynth.com>
10 * This program uses the PortAudio Portable Audio Library.
11 * For more information see: http://www.audiomulch.com/portaudio/
12 * Copyright (c) 1999-2000 Ross Bencina and Phil Burk
14 * Permission is hereby granted, free of charge, to any person obtaining
15 * a copy of this software and associated documentation files
16 * (the "Software"), to deal in the Software without restriction,
17 * including without limitation the rights to use, copy, modify, merge,
18 * publish, distribute, sublicense, and/or sell copies of the Software,
19 * and to permit persons to whom the Software is furnished to do so,
20 * subject to the following conditions:
22 * The above copyright notice and this permission notice shall be
23 * included in all copies or substantial portions of the Software.
25 * Any person wishing to distribute modifications to the Software is
26 * requested to send the modifications to the original developer so that
27 * they can be incorporated into the canonical version.
29 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
30 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
31 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
32 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
33 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
34 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
35 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
40 #include "portaudio.h"
42 #define NUM_SECONDS (10)
43 #define SAMPLE_RATE (44100)
44 #define AMPLITUDE (0.8)
45 #define FRAMES_PER_BUFFER (64)
46 #define OUTPUT_DEVICE Pa_GetDefaultOutputDevice()
49 #define M_PI (3.14159265)
52 #define TABLE_SIZE (200)
55 float sine[TABLE_SIZE];
60 /* This routine will be called by the PortAudio engine when audio is needed.
61 ** It may called at interrupt level on some machines so don't do anything
62 ** that could mess up the system like calling malloc() or free().
64 static int patestCallback( const void *inputBuffer, void *outputBuffer,
65 unsigned long framesPerBuffer,
66 const PaStreamCallbackTimeInfo* timeInfo,
67 PaStreamCallbackFlags statusFlags,
70 paTestData *data = (paTestData*)userData;
71 float *out = (float*)outputBuffer;
74 /* avoid unused variable warnings */
78 for( i=0; i<framesPerBuffer; i++ )
80 *out++ = data->sine[data->phase]; /* left */
82 if( data->phase >= TABLE_SIZE ) data->phase -= TABLE_SIZE;
87 /*******************************************************************/
91 PaStreamParameters outputParameters;
96 printf("PortAudio Test: output MONO sine wave. SR = %d, BufSize = %d\n", SAMPLE_RATE, FRAMES_PER_BUFFER);
97 /* initialise sinusoidal wavetable */
98 for( i=0; i<TABLE_SIZE; i++ )
100 data.sine[i] = (float) (AMPLITUDE * sin( ((double)i/(double)TABLE_SIZE) * M_PI * 2. ));
104 err = Pa_Initialize();
105 if( err != paNoError ) goto error;
107 outputParameters.device = OUTPUT_DEVICE;
108 outputParameters.channelCount = 1; /* MONO output */
109 outputParameters.sampleFormat = paFloat32; /* 32 bit floating point output */
110 outputParameters.suggestedLatency = Pa_GetDeviceInfo( outputParameters.device )->defaultLowOutputLatency;
111 outputParameters.hostApiSpecificStreamInfo = NULL;
119 paClipOff, /* we won't output out of range samples so don't bother clipping them */
122 if( err != paNoError ) goto error;
124 err = Pa_StartStream( stream );
125 if( err != paNoError ) goto error;
127 printf("Play for %d seconds.\n", NUM_SECONDS ); fflush(stdout);
128 Pa_Sleep( NUM_SECONDS * 1000 );
130 err = Pa_StopStream( stream );
131 if( err != paNoError ) goto error;
133 err = Pa_CloseStream( stream );
134 if( err != paNoError ) goto error;
137 printf("Test finished.\n");
141 fprintf( stderr, "An error occured while using the portaudio stream\n" );
142 fprintf( stderr, "Error number: %d\n", err );
143 fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) );