Debian Codegraph @

3rdparty/iaxclient/lib/portaudio/test/patest_sine_formats.c

raw symbols ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 /** @file patest_sine_formats.c
2 @brief Play a sine wave for several seconds. Test various data formats.
3 @author Phil Burk
4 */
5 /*
6 * $Id: patest_sine_formats.c,v 1.1 2006/06/10 21:30:57 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 #include <stdio.h>
37 #include <math.h>
38 #include "portaudio.h"
39
40 #define NUM_SECONDS (10)
41 #define SAMPLE_RATE (44100)
42 #define FRAMES_PER_BUFFER (512)
43 #define LEFT_FREQ (SAMPLE_RATE/256.0) /* So we hit 1.0 */
44 #define RIGHT_FREQ (500.0)
45 #define AMPLITUDE (1.0)
46
47 /* Select ONE format for testing. */
48 #define TEST_UINT8 (0)
49 #define TEST_INT8 (0)
50 #define TEST_INT16 (1)
51 #define TEST_FLOAT32 (0)
52
53 #if TEST_UINT8
54 #define TEST_FORMAT paUInt8
55 typedef unsigned char SAMPLE_t;
56 #define SAMPLE_ZERO (0x80)
57 #define DOUBLE_TO_SAMPLE(x) (SAMPLE_ZERO + (SAMPLE_t)(127.0 * (x)))
58 #define FORMAT_NAME "Unsigned 8 Bit"
59
60 #elif TEST_INT8
61 #define TEST_FORMAT paInt8
62 typedef char SAMPLE_t;
63 #define SAMPLE_ZERO (0)
64 #define DOUBLE_TO_SAMPLE(x) (SAMPLE_ZERO + (SAMPLE_t)(127.0 * (x)))
65 #define FORMAT_NAME "Signed 8 Bit"
66
67 #elif TEST_INT16
68 #define TEST_FORMAT paInt16
69 typedef short SAMPLE_t;
70 #define SAMPLE_ZERO (0)
71 #define DOUBLE_TO_SAMPLE(x) (SAMPLE_ZERO + (SAMPLE_t)(32767 * (x)))
72 #define FORMAT_NAME "Signed 16 Bit"
73
74 #elif TEST_FLOAT32
75 #define TEST_FORMAT paFloat32
76 typedef float SAMPLE_t;
77 #define SAMPLE_ZERO (0.0)
78 #define DOUBLE_TO_SAMPLE(x) ((SAMPLE_t)(x))
79 #define FORMAT_NAME "Float 32 Bit"
80 #endif
81
82 #ifndef M_PI
83 #define M_PI (3.14159265)
84 #endif
85
86
87 typedef struct
88 {
89 double left_phase;
90 double right_phase;
91 unsigned int framesToGo;
92 }
93 paTestData;
94 /* This routine will be called by the PortAudio engine when audio is needed.
95 ** It may called at interrupt level on some machines so don't do anything
96 ** that could mess up the system like calling malloc() or free().
97 */
98 static int patestCallback( const void *inputBuffer,
99 void *outputBuffer,
100 unsigned long framesPerBuffer,
101 const PaStreamCallbackTimeInfo* timeInfo,
102 PaStreamCallbackFlags statusFlags,
103 void *userData )
104 {
105 paTestData *data = (paTestData*)userData;
106 SAMPLE_t *out = (SAMPLE_t *)outputBuffer;
107 int i;
108 int framesToCalc;
109 int finished = 0;
110 (void) inputBuffer; /* Prevent unused variable warnings. */
111
112 if( data->framesToGo < framesPerBuffer )
113 {
114 framesToCalc = data->framesToGo;
115 data->framesToGo = 0;
116 finished = 1;
117 }
118 else
119 {
120 framesToCalc = framesPerBuffer;
121 data->framesToGo -= framesPerBuffer;
122 }
123
124 for( i=0; i<framesToCalc; i++ )
125 {
126 data->left_phase += (LEFT_FREQ / SAMPLE_RATE);
127 if( data->left_phase > 1.0) data->left_phase -= 1.0;
128 *out++ = DOUBLE_TO_SAMPLE( AMPLITUDE * sin( (data->left_phase * M_PI * 2. )));
129
130 data->right_phase += (RIGHT_FREQ / SAMPLE_RATE);
131 if( data->right_phase > 1.0) data->right_phase -= 1.0;
132 *out++ = DOUBLE_TO_SAMPLE( AMPLITUDE * sin( (data->right_phase * M_PI * 2. )));
133 }
134 /* zero remainder of final buffer */
135 for( ; i<(int)framesPerBuffer; i++ )
136 {
137 *out++ = SAMPLE_ZERO; /* left */
138 *out++ = SAMPLE_ZERO; /* right */
139 }
140 return finished;
141 }
142 /*******************************************************************/
143 int main(void);
144 int main(void)
145 {
146 PaStream *stream;
147 PaStreamParameters outputParameters;
148 PaError err;
149 paTestData data;
150 int totalSamps;
151
152 printf("PortAudio Test: output " FORMAT_NAME "\n");
153
154 data.left_phase = data.right_phase = 0.0;
155 data.framesToGo = totalSamps = NUM_SECONDS * SAMPLE_RATE; /* Play for a few seconds. */
156 err = Pa_Initialize();
157 if( err != paNoError ) goto error;
158
159
160 outputParameters.device = Pa_GetDefaultOutputDevice(); /* Default output device. */
161 outputParameters.channelCount = 2; /* Stereo output */
162 outputParameters.sampleFormat = TEST_FORMAT; /* Selected above. */
163 outputParameters.suggestedLatency = Pa_GetDeviceInfo(outputParameters.device)->defaultLowOutputLatency;
164 outputParameters.hostApiSpecificStreamInfo = NULL;
165 err = Pa_OpenStream( &stream,
166 NULL, /* No input. */
167 &outputParameters, /* As above. */
168 SAMPLE_RATE,
169 FRAMES_PER_BUFFER,
170 paClipOff, /* we won't output out of range samples so don't bother clipping them */
171 patestCallback,
172 &data );
173 if( err != paNoError ) goto error;
174
175 err = Pa_StartStream( stream );
176 if( err != paNoError ) goto error;
177
178 printf("Waiting %d seconds for sound to finish.\n", NUM_SECONDS );
179
180 while( ( err = Pa_IsStreamActive( stream ) ) == 1 ) Pa_Sleep(100);
181 if( err < 0 ) goto error;
182
183 err = Pa_CloseStream( stream );
184 if( err != paNoError ) goto error;
185 Pa_Terminate();
186
187 printf("PortAudio Test Finished: " FORMAT_NAME "\n");
188
189 return err;
190 error:
191 Pa_Terminate();
192 fprintf( stderr, "An error occured while using the portaudio stream\n" );
193 fprintf( stderr, "Error number: %d\n", err );
194 fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) );
195 return err;
196 }