Mercurial > hg > sv-dependency-builds
comparison src/portaudio_20140130/examples/paex_sine_c++.cpp @ 124:e3d5853d5918
Current stable PortAudio source
author | Chris Cannam <cannam@all-day-breakfast.com> |
---|---|
date | Tue, 18 Oct 2016 13:11:05 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
123:0cef3a1bd1ae | 124:e3d5853d5918 |
---|---|
1 /** @file paex_sine.c | |
2 @ingroup examples_src | |
3 @brief Play a sine wave for several seconds. | |
4 @author Ross Bencina <rossb@audiomulch.com> | |
5 @author Phil Burk <philburk@softsynth.com> | |
6 */ | |
7 /* | |
8 * $Id: paex_sine.c 1752 2011-09-08 03:21:55Z philburk $ | |
9 * | |
10 * This program uses the PortAudio Portable Audio Library. | |
11 * For more information see: http://www.portaudio.com/ | |
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 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |
26 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
27 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | |
28 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR | |
29 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF | |
30 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | |
31 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
32 */ | |
33 | |
34 /* | |
35 * The text above constitutes the entire PortAudio license; however, | |
36 * the PortAudio community also makes the following non-binding requests: | |
37 * | |
38 * Any person wishing to distribute modifications to the Software is | |
39 * requested to send the modifications to the original developer so that | |
40 * they can be incorporated into the canonical version. It is also | |
41 * requested that these non-binding requests be included along with the | |
42 * license above. | |
43 */ | |
44 #include <stdio.h> | |
45 #include <math.h> | |
46 #include "portaudio.h" | |
47 | |
48 #define NUM_SECONDS (5) | |
49 #define SAMPLE_RATE (44100) | |
50 #define FRAMES_PER_BUFFER (64) | |
51 | |
52 #ifndef M_PI | |
53 #define M_PI (3.14159265) | |
54 #endif | |
55 | |
56 #define TABLE_SIZE (200) | |
57 | |
58 class Sine | |
59 { | |
60 public: | |
61 Sine() : stream(0), left_phase(0), right_phase(0) | |
62 { | |
63 /* initialise sinusoidal wavetable */ | |
64 for( int i=0; i<TABLE_SIZE; i++ ) | |
65 { | |
66 sine[i] = (float) sin( ((double)i/(double)TABLE_SIZE) * M_PI * 2. ); | |
67 } | |
68 | |
69 sprintf( message, "No Message" ); | |
70 } | |
71 | |
72 bool open(PaDeviceIndex index) | |
73 { | |
74 PaStreamParameters outputParameters; | |
75 | |
76 outputParameters.device = index; | |
77 if (outputParameters.device == paNoDevice) { | |
78 return false; | |
79 } | |
80 | |
81 outputParameters.channelCount = 2; /* stereo output */ | |
82 outputParameters.sampleFormat = paFloat32; /* 32 bit floating point output */ | |
83 outputParameters.suggestedLatency = Pa_GetDeviceInfo( outputParameters.device )->defaultLowOutputLatency; | |
84 outputParameters.hostApiSpecificStreamInfo = NULL; | |
85 | |
86 PaError err = Pa_OpenStream( | |
87 &stream, | |
88 NULL, /* no input */ | |
89 &outputParameters, | |
90 SAMPLE_RATE, | |
91 FRAMES_PER_BUFFER, | |
92 paClipOff, /* we won't output out of range samples so don't bother clipping them */ | |
93 &Sine::paCallback, | |
94 this /* Using 'this' for userData so we can cast to Sine* in paCallback method */ | |
95 ); | |
96 | |
97 if (err != paNoError) | |
98 { | |
99 /* Failed to open stream to device !!! */ | |
100 return false; | |
101 } | |
102 | |
103 err = Pa_SetStreamFinishedCallback( stream, &Sine::paStreamFinished ); | |
104 | |
105 if (err != paNoError) | |
106 { | |
107 Pa_CloseStream( stream ); | |
108 stream = 0; | |
109 | |
110 return false; | |
111 } | |
112 | |
113 return true; | |
114 } | |
115 | |
116 bool close() | |
117 { | |
118 if (stream == 0) | |
119 return false; | |
120 | |
121 PaError err = Pa_CloseStream( stream ); | |
122 stream = 0; | |
123 | |
124 return (err == paNoError); | |
125 } | |
126 | |
127 | |
128 bool start() | |
129 { | |
130 if (stream == 0) | |
131 return false; | |
132 | |
133 PaError err = Pa_StartStream( stream ); | |
134 | |
135 return (err == paNoError); | |
136 } | |
137 | |
138 bool stop() | |
139 { | |
140 if (stream == 0) | |
141 return false; | |
142 | |
143 PaError err = Pa_StopStream( stream ); | |
144 | |
145 return (err == paNoError); | |
146 } | |
147 | |
148 private: | |
149 /* The instance callback, where we have access to every method/variable in object of class Sine */ | |
150 int paCallbackMethod(const void *inputBuffer, void *outputBuffer, | |
151 unsigned long framesPerBuffer, | |
152 const PaStreamCallbackTimeInfo* timeInfo, | |
153 PaStreamCallbackFlags statusFlags) | |
154 { | |
155 float *out = (float*)outputBuffer; | |
156 unsigned long i; | |
157 | |
158 (void) timeInfo; /* Prevent unused variable warnings. */ | |
159 (void) statusFlags; | |
160 (void) inputBuffer; | |
161 | |
162 for( i=0; i<framesPerBuffer; i++ ) | |
163 { | |
164 *out++ = sine[left_phase]; /* left */ | |
165 *out++ = sine[right_phase]; /* right */ | |
166 left_phase += 1; | |
167 if( left_phase >= TABLE_SIZE ) left_phase -= TABLE_SIZE; | |
168 right_phase += 3; /* higher pitch so we can distinguish left and right. */ | |
169 if( right_phase >= TABLE_SIZE ) right_phase -= TABLE_SIZE; | |
170 } | |
171 | |
172 return paContinue; | |
173 | |
174 } | |
175 | |
176 /* This routine will be called by the PortAudio engine when audio is needed. | |
177 ** It may called at interrupt level on some machines so don't do anything | |
178 ** that could mess up the system like calling malloc() or free(). | |
179 */ | |
180 static int paCallback( const void *inputBuffer, void *outputBuffer, | |
181 unsigned long framesPerBuffer, | |
182 const PaStreamCallbackTimeInfo* timeInfo, | |
183 PaStreamCallbackFlags statusFlags, | |
184 void *userData ) | |
185 { | |
186 /* Here we cast userData to Sine* type so we can call the instance method paCallbackMethod, we can do that since | |
187 we called Pa_OpenStream with 'this' for userData */ | |
188 return ((Sine*)userData)->paCallbackMethod(inputBuffer, outputBuffer, | |
189 framesPerBuffer, | |
190 timeInfo, | |
191 statusFlags); | |
192 } | |
193 | |
194 | |
195 void paStreamFinishedMethod() | |
196 { | |
197 printf( "Stream Completed: %s\n", message ); | |
198 } | |
199 | |
200 /* | |
201 * This routine is called by portaudio when playback is done. | |
202 */ | |
203 static void paStreamFinished(void* userData) | |
204 { | |
205 return ((Sine*)userData)->paStreamFinishedMethod(); | |
206 } | |
207 | |
208 PaStream *stream; | |
209 float sine[TABLE_SIZE]; | |
210 int left_phase; | |
211 int right_phase; | |
212 char message[20]; | |
213 }; | |
214 | |
215 | |
216 /*******************************************************************/ | |
217 int main(void); | |
218 int main(void) | |
219 { | |
220 PaError err; | |
221 Sine sine; | |
222 | |
223 printf("PortAudio Test: output sine wave. SR = %d, BufSize = %d\n", SAMPLE_RATE, FRAMES_PER_BUFFER); | |
224 | |
225 err = Pa_Initialize(); | |
226 if( err != paNoError ) goto error; | |
227 | |
228 if (sine.open(Pa_GetDefaultOutputDevice())) | |
229 { | |
230 if (sine.start()) | |
231 { | |
232 printf("Play for %d seconds.\n", NUM_SECONDS ); | |
233 Pa_Sleep( NUM_SECONDS * 1000 ); | |
234 | |
235 sine.stop(); | |
236 } | |
237 | |
238 sine.close(); | |
239 } | |
240 | |
241 Pa_Terminate(); | |
242 printf("Test finished.\n"); | |
243 | |
244 return err; | |
245 | |
246 error: | |
247 Pa_Terminate(); | |
248 fprintf( stderr, "An error occured while using the portaudio stream\n" ); | |
249 fprintf( stderr, "Error number: %d\n", err ); | |
250 fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) ); | |
251 return err; | |
252 } |