]> git.mxchange.org Git - flightgear.git/blob - 3rdparty/iaxclient/lib/portaudio/test/patest_sine8.c
Move IAXClient library into 3rdparty directory
[flightgear.git] / 3rdparty / iaxclient / lib / portaudio / test / patest_sine8.c
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>
4 */
5 /*
6  * $Id: patest_sine8.c,v 1.1 2006/06/10 21:30:56 dmazzoni Exp $
7  *
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
11  *
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:
19  *
20  * The above copyright notice and this permission notice shall be
21  * included in all copies or substantial portions of the Software.
22  *
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.
26  *
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.
34  *
35  */
36
37 #include <stdio.h>
38 #include <math.h>
39 #include "portaudio.h"
40
41 #define NUM_SECONDS   (8)
42 #define SAMPLE_RATE   (44100)
43 #define TABLE_SIZE    (200)
44 #define TEST_UNSIGNED (0)
45
46 #if TEST_UNSIGNED
47 #define TEST_FORMAT   paUInt8
48 #else
49 #define TEST_FORMAT   paInt8
50 #endif
51
52 #ifndef M_PI
53 #define M_PI (3.14159265)
54 #endif
55
56 typedef struct
57 {
58 #if TEST_UNSIGNED
59     unsigned char sine[TABLE_SIZE];
60 #else
61     char sine[TABLE_SIZE];
62 #endif
63     int left_phase;
64     int right_phase;
65     unsigned int framesToGo;
66 }
67 paTestData;
68
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().
72 */
73 static int patestCallback( const void *inputBuffer, void *outputBuffer,
74                            unsigned long framesPerBuffer,
75                            const PaStreamCallbackTimeInfo* timeInfo,
76                            PaStreamCallbackFlags statusFlags,
77                            void *userData )
78 {
79     paTestData *data = (paTestData*)userData;
80     char *out = (char*)outputBuffer;
81     int i;
82     int framesToCalc;
83     int finished = 0;
84     (void) inputBuffer; /* Prevent unused variable warnings. */
85
86     if( data->framesToGo < framesPerBuffer )
87     {
88         framesToCalc = data->framesToGo;
89         data->framesToGo = 0;
90         finished = 1;
91     }
92     else
93     {
94         framesToCalc = framesPerBuffer;
95         data->framesToGo -= framesPerBuffer;
96     }
97
98     for( i=0; i<framesToCalc; i++ )
99     {
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;
106     }
107     /* zero remainder of final buffer */
108     for( ; i<(int)framesPerBuffer; i++ )
109     {
110 #if TEST_UNSIGNED
111         *out++ = (unsigned char) 0x80; /* left */
112         *out++ = (unsigned char) 0x80; /* right */
113 #else
114         *out++ = 0; /* left */
115         *out++ = 0; /* right */
116 #endif
117
118     }
119     return finished;
120 }
121
122 /*******************************************************************/
123 int main(void);
124 int main(void)
125 {
126     PaStreamParameters  outputParameters;
127     PaStream*           stream;
128     PaError             err;
129     paTestData          data;
130     PaTime              streamOpened;
131     int                 i, totalSamps;
132
133 #if TEST_UNSIGNED
134     printf("PortAudio Test: output UNsigned 8 bit sine wave.\n");
135 #else
136     printf("PortAudio Test: output signed 8 bit sine wave.\n");
137 #endif
138     /* initialise sinusoidal wavetable */
139     for( i=0; i<TABLE_SIZE; i++ )
140     {
141         data.sine[i] = (char) (127.0 * sin( ((double)i/(double)TABLE_SIZE) * M_PI * 2. ));
142 #if TEST_UNSIGNED
143         data.sine[i] += (unsigned char) 0x80;
144 #endif
145     }
146     data.left_phase = data.right_phase = 0;
147     data.framesToGo = totalSamps =  NUM_SECONDS * SAMPLE_RATE; /* Play for a few seconds. */
148
149     err = Pa_Initialize();
150     if( err != paNoError )
151         goto error;
152
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. */
160                          &outputParameters,
161                          SAMPLE_RATE,
162                          256,       /* Frames per buffer. */
163                          paClipOff, /* We won't output out of range samples so don't bother clipping them. */
164                          patestCallback,
165                          &data );
166     if( err != paNoError )
167         goto error;
168
169     streamOpened = Pa_GetStreamTime( stream ); /* Time in seconds when stream was opened (approx). */
170
171     err = Pa_StartStream( stream );
172     if( err != paNoError )
173         goto error;
174
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 )
178         Pa_Sleep(10);
179
180     /* Stop sound until ENTER hit. (Hu? don't see any keyboard-input here.) */
181     err = Pa_StopStream( stream );
182     if( err != paNoError )
183         goto error;
184
185     printf("Pause for 2 seconds.\n");
186     Pa_Sleep( 2000 );
187
188     err = Pa_StartStream( stream );
189     if( err != paNoError )
190         goto error;
191
192     printf("Waiting for sound to finish.\n");
193
194     while( ( err = Pa_IsStreamActive( stream ) ) == 1 )
195         Pa_Sleep(100);
196     if( err < 0 )
197         goto error;
198
199     err = Pa_CloseStream( stream );
200     if( err != paNoError )
201         goto error;
202
203     Pa_Terminate();
204     printf("Test finished.\n");
205     return err;
206 error:
207     Pa_Terminate();
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 ) );
211     return err;
212 }