1 /** @file patest_sine8.c
2 @brief Test 8 bit data: play a sine wave for several seconds.
3 @author Ross Bencina <rossb@audiomulch.com>
6 * $Id: patest_sine8.c,v 1.1 2006/06/10 21:30:56 dmazzoni Exp $
8 * This program uses the PortAudio Portable Audio Library.
9 * For more information see: http://www.audiomulch.com/portaudio/
10 * Copyright (c) 1999-2000 Ross Bencina and Phil Burk
12 * Permission is hereby granted, free of charge, to any person obtaining
13 * a copy of this software and associated documentation files
14 * (the "Software"), to deal in the Software without restriction,
15 * including without limitation the rights to use, copy, modify, merge,
16 * publish, distribute, sublicense, and/or sell copies of the Software,
17 * and to permit persons to whom the Software is furnished to do so,
18 * subject to the following conditions:
20 * The above copyright notice and this permission notice shall be
21 * included in all copies or substantial portions of the Software.
23 * Any person wishing to distribute modifications to the Software is
24 * requested to send the modifications to the original developer so that
25 * they can be incorporated into the canonical version.
27 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
28 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
29 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
30 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
31 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
32 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
33 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
39 #include "portaudio.h"
41 #define NUM_SECONDS (8)
42 #define SAMPLE_RATE (44100)
43 #define TABLE_SIZE (200)
44 #define TEST_UNSIGNED (0)
47 #define TEST_FORMAT paUInt8
49 #define TEST_FORMAT paInt8
53 #define M_PI (3.14159265)
59 unsigned char sine[TABLE_SIZE];
61 char sine[TABLE_SIZE];
65 unsigned int framesToGo;
69 /* This routine will be called by the PortAudio engine when audio is needed.
70 ** It may called at interrupt level on some machines so don't do anything
71 ** that could mess up the system like calling malloc() or free().
73 static int patestCallback( const void *inputBuffer, void *outputBuffer,
74 unsigned long framesPerBuffer,
75 const PaStreamCallbackTimeInfo* timeInfo,
76 PaStreamCallbackFlags statusFlags,
79 paTestData *data = (paTestData*)userData;
80 char *out = (char*)outputBuffer;
84 (void) inputBuffer; /* Prevent unused variable warnings. */
86 if( data->framesToGo < framesPerBuffer )
88 framesToCalc = data->framesToGo;
94 framesToCalc = framesPerBuffer;
95 data->framesToGo -= framesPerBuffer;
98 for( i=0; i<framesToCalc; i++ )
100 *out++ = data->sine[data->left_phase]; /* left */
101 *out++ = data->sine[data->right_phase]; /* right */
102 data->left_phase += 1;
103 if( data->left_phase >= TABLE_SIZE ) data->left_phase -= TABLE_SIZE;
104 data->right_phase += 3; /* higher pitch so we can distinguish left and right. */
105 if( data->right_phase >= TABLE_SIZE ) data->right_phase -= TABLE_SIZE;
107 /* zero remainder of final buffer */
108 for( ; i<(int)framesPerBuffer; i++ )
111 *out++ = (unsigned char) 0x80; /* left */
112 *out++ = (unsigned char) 0x80; /* right */
114 *out++ = 0; /* left */
115 *out++ = 0; /* right */
122 /*******************************************************************/
126 PaStreamParameters outputParameters;
134 printf("PortAudio Test: output UNsigned 8 bit sine wave.\n");
136 printf("PortAudio Test: output signed 8 bit sine wave.\n");
138 /* initialise sinusoidal wavetable */
139 for( i=0; i<TABLE_SIZE; i++ )
141 data.sine[i] = (char) (127.0 * sin( ((double)i/(double)TABLE_SIZE) * M_PI * 2. ));
143 data.sine[i] += (unsigned char) 0x80;
146 data.left_phase = data.right_phase = 0;
147 data.framesToGo = totalSamps = NUM_SECONDS * SAMPLE_RATE; /* Play for a few seconds. */
149 err = Pa_Initialize();
150 if( err != paNoError )
153 outputParameters.device = Pa_GetDefaultOutputDevice(); /* Default output device. */
154 outputParameters.channelCount = 2; /* Stereo output. */
155 outputParameters.sampleFormat = TEST_FORMAT;
156 outputParameters.suggestedLatency = Pa_GetDeviceInfo( outputParameters.device )->defaultLowOutputLatency;
157 outputParameters.hostApiSpecificStreamInfo = NULL;
158 err = Pa_OpenStream( &stream,
159 NULL, /* No input. */
162 256, /* Frames per buffer. */
163 paClipOff, /* We won't output out of range samples so don't bother clipping them. */
166 if( err != paNoError )
169 streamOpened = Pa_GetStreamTime( stream ); /* Time in seconds when stream was opened (approx). */
171 err = Pa_StartStream( stream );
172 if( err != paNoError )
175 /* Watch until sound is halfway finished. */
176 /* (Was ( Pa_StreamTime( stream ) < (totalSamps/2) ) in V18. */
177 while( (Pa_GetStreamTime( stream ) - streamOpened) < (PaTime)NUM_SECONDS / 2.0 )
180 /* Stop sound until ENTER hit. (Hu? don't see any keyboard-input here.) */
181 err = Pa_StopStream( stream );
182 if( err != paNoError )
185 printf("Pause for 2 seconds.\n");
188 err = Pa_StartStream( stream );
189 if( err != paNoError )
192 printf("Waiting for sound to finish.\n");
194 while( ( err = Pa_IsStreamActive( stream ) ) == 1 )
199 err = Pa_CloseStream( stream );
200 if( err != paNoError )
204 printf("Test finished.\n");
208 fprintf( stderr, "An error occured while using the portaudio stream\n" );
209 fprintf( stderr, "Error number: %d\n", err );
210 fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) );