]> git.mxchange.org Git - flightgear.git/blob - 3rdparty/iaxclient/lib/portaudio/test/patest_mono.c
Move IAXClient library into 3rdparty directory
[flightgear.git] / 3rdparty / iaxclient / lib / portaudio / test / patest_mono.c
1 /*
2  * $Id: patest_mono.c,v 1.1 2006/06/10 21:30:56 dmazzoni Exp $
3  * patest_sine.c
4  * Play a monophonic sine wave using the Portable Audio api for several seconds.
5  *
6  * Authors:
7  *    Ross Bencina <rossb@audiomulch.com>
8  *    Phil Burk <philburk@softsynth.com>
9  *
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
13  *
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:
21  *
22  * The above copyright notice and this permission notice shall be
23  * included in all copies or substantial portions of the Software.
24  *
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.
28  *
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.
36  *
37  */
38 #include <stdio.h>
39 #include <math.h>
40 #include "portaudio.h"
41
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()
47
48 #ifndef M_PI
49 #define M_PI  (3.14159265)
50 #endif
51
52 #define TABLE_SIZE   (200)
53 typedef struct
54 {
55     float sine[TABLE_SIZE];
56     int phase;
57 }
58 paTestData;
59
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().
63 */
64 static int patestCallback( const void *inputBuffer, void *outputBuffer,
65                             unsigned long framesPerBuffer,
66                             const PaStreamCallbackTimeInfo* timeInfo,
67                             PaStreamCallbackFlags statusFlags,
68                             void *userData )
69 {
70     paTestData *data = (paTestData*)userData;
71     float *out = (float*)outputBuffer;
72     unsigned long i;
73     int finished = 0;
74     /* avoid unused variable warnings */
75     (void) inputBuffer;
76     (void) timeInfo;
77     (void) statusFlags;
78     for( i=0; i<framesPerBuffer; i++ )
79     {
80         *out++ = data->sine[data->phase];  /* left */
81         data->phase += 1;
82         if( data->phase >= TABLE_SIZE ) data->phase -= TABLE_SIZE;
83     }
84     return finished;
85 }
86
87 /*******************************************************************/
88 int main(void);
89 int main(void)
90 {
91     PaStreamParameters outputParameters;
92     PaStream *stream;
93     PaError err;
94     paTestData data;
95     int i;
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++ )
99     {
100         data.sine[i] = (float) (AMPLITUDE * sin( ((double)i/(double)TABLE_SIZE) * M_PI * 2. ));
101     }
102     data.phase = 0;
103     
104     err = Pa_Initialize();
105     if( err != paNoError ) goto error;
106
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;
112
113     err = Pa_OpenStream(
114               &stream,
115               NULL, /* no input */
116               &outputParameters,
117               SAMPLE_RATE,
118               FRAMES_PER_BUFFER,
119               paClipOff,      /* we won't output out of range samples so don't bother clipping them */
120               patestCallback,
121               &data );
122     if( err != paNoError ) goto error;
123
124     err = Pa_StartStream( stream );
125     if( err != paNoError ) goto error;
126     
127     printf("Play for %d seconds.\n", NUM_SECONDS ); fflush(stdout);
128     Pa_Sleep( NUM_SECONDS * 1000 );
129
130     err = Pa_StopStream( stream );
131     if( err != paNoError ) goto error;
132     
133     err = Pa_CloseStream( stream );
134     if( err != paNoError ) goto error;
135     
136     Pa_Terminate();
137     printf("Test finished.\n");
138     return err;
139 error:
140     Pa_Terminate();
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 ) );
144     return err;
145 }