comparison src/portaudio/test/patest_multi_sine.c @ 89:8a15ff55d9af

Add bzip2, zlib, liblo, portaudio sources
author Chris Cannam <cannam@all-day-breakfast.com>
date Wed, 20 Mar 2013 13:59:52 +0000
parents
children
comparison
equal deleted inserted replaced
88:fe7c3a0b0259 89:8a15ff55d9af
1 /** @file patest_multi_sine.c
2 @ingroup test_src
3 @brief Play a different sine wave on each channel.
4 @author Phil Burk http://www.softsynth.com
5 */
6 /*
7 * $Id: patest_multi_sine.c 1368 2008-03-01 00:38:27Z rossb $
8 *
9 * This program uses the PortAudio Portable Audio Library.
10 * For more information see: http://www.portaudio.com
11 * Copyright (c) 1999-2000 Ross Bencina and Phil Burk
12 *
13 * Permission is hereby granted, free of charge, to any person obtaining
14 * a copy of this software and associated documentation files
15 * (the "Software"), to deal in the Software without restriction,
16 * including without limitation the rights to use, copy, modify, merge,
17 * publish, distribute, sublicense, and/or sell copies of the Software,
18 * and to permit persons to whom the Software is furnished to do so,
19 * subject to the following conditions:
20 *
21 * The above copyright notice and this permission notice shall be
22 * included in all copies or substantial portions of the Software.
23 *
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
27 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
28 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
29 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 */
32
33 /*
34 * The text above constitutes the entire PortAudio license; however,
35 * the PortAudio community also makes the following non-binding requests:
36 *
37 * Any person wishing to distribute modifications to the Software is
38 * requested to send the modifications to the original developer so that
39 * they can be incorporated into the canonical version. It is also
40 * requested that these non-binding requests be included along with the
41 * license above.
42 */
43
44 #include <stdio.h>
45 #include <math.h>
46
47 #include "portaudio.h"
48
49 #define SAMPLE_RATE (44100)
50 #define FRAMES_PER_BUFFER (256)
51 #define FREQ_INCR (300.0 / SAMPLE_RATE)
52 #define MAX_CHANNELS (64)
53
54 #ifndef M_PI
55 #define M_PI (3.14159265)
56 #endif
57
58 typedef struct
59 {
60 short interleaved; /* Nonzero for interleaved / zero for non-interleaved. */
61 int numChannels; /* Actually used. */
62 double phases[MAX_CHANNELS]; /* Each channel gets its' own frequency. */
63 }
64 paTestData;
65
66 /* This routine will be called by the PortAudio engine when audio is needed.
67 ** It may called at interrupt level on some machines so don't do anything
68 ** that could mess up the system like calling malloc() or free().
69 */
70 static int patestCallback(const void* inputBuffer,
71 void* outputBuffer,
72 unsigned long framesPerBuffer,
73 const PaStreamCallbackTimeInfo* timeInfo,
74 PaStreamCallbackFlags statusFlags,
75 void* userData)
76 {
77 int frameIndex, channelIndex;
78 float** outputs = (float**)outputBuffer;
79 paTestData* data = (paTestData*)userData;
80
81 (void) inputBuffer; /* Prevent unused arg warning. */
82 if (data->interleaved)
83 {
84 float *out = (float*)outputBuffer; /* interleaved version */
85 for( frameIndex=0; frameIndex<(int)framesPerBuffer; frameIndex++ )
86 {
87 for( channelIndex=0; channelIndex<data->numChannels; channelIndex++ )
88 {
89 /* Output sine wave on every channel. */
90 *out++ = (float) sin(data->phases[channelIndex]);
91
92 /* Play each channel at a higher frequency. */
93 data->phases[channelIndex] += FREQ_INCR * (4 + channelIndex);
94 if( data->phases[channelIndex] >= (2.0 * M_PI) ) data->phases[channelIndex] -= (2.0 * M_PI);
95 }
96 }
97 }
98 else
99 {
100 for( frameIndex=0; frameIndex<(int)framesPerBuffer; frameIndex++ )
101 {
102 for( channelIndex=0; channelIndex<data->numChannels; channelIndex++ )
103 {
104 /* Output sine wave on every channel. */
105 outputs[channelIndex][frameIndex] = (float) sin(data->phases[channelIndex]);
106
107 /* Play each channel at a higher frequency. */
108 data->phases[channelIndex] += FREQ_INCR * (4 + channelIndex);
109 if( data->phases[channelIndex] >= (2.0 * M_PI) ) data->phases[channelIndex] -= (2.0 * M_PI);
110 }
111 }
112 }
113 return 0;
114 }
115
116 /*******************************************************************/
117 int test(short interleaved)
118 {
119 PaStream* stream;
120 PaStreamParameters outputParameters;
121 PaError err;
122 const PaDeviceInfo* pdi;
123 paTestData data;
124 short n;
125
126 outputParameters.device = Pa_GetDefaultOutputDevice(); /* Default output device, max channels. */
127 if (outputParameters.device == paNoDevice) {
128 fprintf(stderr,"Error: No default output device.\n");
129 return paInvalidDevice;
130 }
131 pdi = Pa_GetDeviceInfo(outputParameters.device);
132 outputParameters.channelCount = pdi->maxOutputChannels;
133 if (outputParameters.channelCount > MAX_CHANNELS)
134 outputParameters.channelCount = MAX_CHANNELS;
135 outputParameters.sampleFormat = paFloat32; /* 32 bit floating point output */
136 outputParameters.suggestedLatency = pdi->defaultLowOutputLatency;
137 outputParameters.hostApiSpecificStreamInfo = NULL;
138
139 data.interleaved = interleaved;
140 data.numChannels = outputParameters.channelCount;
141 for (n = 0; n < data.numChannels; n++)
142 data.phases[n] = 0.0; /* Phases wrap and maybe don't need initialisation. */
143 printf("%d ", data.numChannels);
144 if (interleaved)
145 printf("interleaved ");
146 else
147 {
148 printf(" non-interleaved ");
149 outputParameters.sampleFormat |= paNonInterleaved;
150 }
151 printf("channels.\n");
152
153 err = Pa_OpenStream(&stream,
154 NULL, /* No input. */
155 &outputParameters,
156 SAMPLE_RATE, /* Sample rate. */
157 FRAMES_PER_BUFFER, /* Frames per buffer. */
158 paClipOff, /* Samples never out of range, no clipping. */
159 patestCallback,
160 &data);
161 if (err == paNoError)
162 {
163 err = Pa_StartStream(stream);
164 if (err == paNoError)
165 {
166 printf("Hit ENTER to stop this test.\n");
167 getchar();
168 err = Pa_StopStream(stream);
169 }
170 Pa_CloseStream( stream );
171 }
172 return err;
173 }
174
175
176 /*******************************************************************/
177 int main(void)
178 {
179 PaError err;
180
181 printf("PortAudio Test: output sine wave on each channel.\n" );
182
183 err = Pa_Initialize();
184 if (err != paNoError)
185 goto done;
186
187 err = test(1); /* 1 means interleaved. */
188 if (err != paNoError)
189 goto done;
190
191 err = test(0); /* 0 means not interleaved. */
192 if (err != paNoError)
193 goto done;
194
195 printf("Test finished.\n");
196 done:
197 if (err)
198 {
199 fprintf(stderr, "An error occured while using the portaudio stream\n");
200 fprintf(stderr, "Error number: %d\n", err );
201 fprintf(stderr, "Error message: %s\n", Pa_GetErrorText(err));
202 }
203 Pa_Terminate();
204 return 0;
205 }