-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathmultiplesoundfileplayer.cpp
348 lines (282 loc) · 10.7 KB
/
multiplesoundfileplayer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/**
* \file multiplesoundfileplayer.cpp
*/
// This file illustrates the use of a MIPComponentAlias. Please also read
// the documentation of this component.
#include <mipconfig.h>
#if defined(WIN32) || defined(_WIN32_WCE) || defined(MIPCONFIG_SUPPORT_OSS) || defined(MIPCONFIG_SUPPORT_PORTAUDIO)
#include <mipcomponentchain.h>
#include <mipcomponent.h>
#include <miptime.h>
#include <mipaveragetimer.h>
#include <mipwavinput.h>
#include <mipsampleencoder.h>
#ifndef WIN32
#ifdef MIPCONFIG_SUPPORT_PORTAUDIO
#define NEED_PA_INIT
#include <mippainputoutput.h>
#else
#include <mipossinputoutput.h>
#endif
#else
#include <mipwinmmoutput.h>
#endif
#include <miprawaudiomessage.h> // Needed for MIPRAWAUDIOMESSAGE_TYPE_S16LE
#include <mipaudiomixer.h>
#include <mipsamplingrateconverter.h>
#include <mipcomponentalias.h>
#include <iostream>
#include <string>
#include <stdio.h>
#include <cstdlib>
void checkError(bool returnValue, const MIPComponent &component)
{
if (returnValue == true)
return;
std::cerr << "An error occured in component: " << component.getComponentName() << std::endl;
std::cerr << "Error description: " << component.getErrorString() << std::endl;
exit(-1);
}
void checkError(bool returnValue, const MIPComponentChain &chain)
{
if (returnValue == true)
return;
std::cerr << "An error occured in chain: " << chain.getName() << std::endl;
std::cerr << "Error description: " << chain.getErrorString() << std::endl;
exit(-1);
}
class MyChain : public MIPComponentChain
{
public:
MyChain(const std::string &chainName) : MIPComponentChain(chainName)
{
}
private:
void onThreadExit(bool error, const std::string &errorComponent, const std::string &errorDescription)
{
if (!error)
return;
std::cerr << "An error occured in the background thread." << std::endl;
std::cerr << " Component: " << errorComponent << std::endl;
std::cerr << " Error description: " << errorDescription << std::endl;
}
};
int main(void)
{
#ifdef NEED_PA_INIT
std::string errStr;
if (!MIPPAInputOutput::initializePortAudio(errStr))
{
std::cerr << "Can't initialize PortAudio: " << errStr << std::endl;
return -1;
}
#endif // NEED_PA_INIT
MIPTime interval(1.000); // one second intervals
MIPAverageTimer timer(interval);
MIPWAVInput sndFileInput1;
MIPComponentAlias alias(&sndFileInput1);
MIPWAVInput sndFileInput2;
MIPSamplingRateConverter sampConv;
MIPAudioMixer mixer;
MIPSampleEncoder sampEnc;
#ifndef WIN32
#ifdef MIPCONFIG_SUPPORT_PORTAUDIO
MIPPAInputOutput sndCardOutput;
#else
MIPOSSInputOutput sndCardOutput;
#endif
#else
MIPWinMMOutput sndCardOutput;
#endif
MyChain chain("Sound file mixer");
bool returnValue;
int choice;
std::cout << std::endl;
std::cout << std::endl;
std::cout << std::endl;
std::cout << std::endl;
std::cout << std::endl;
std::cout << "This example illustrates the MIPComponentAlias component, so please also read" << std::endl;
std::cout << "the documentation of this component." << std::endl;
std::cout << std::endl;
std::cout << "For this example, you will need two soundfiles: one named \"soundfile-16000-mono.wav\"," << std::endl;
std::cout << "and one named \"soundfile-8000-mono.wav\". As the names suggest, the first file" << std::endl;
std::cout << "should use a 16000 Hz sampling rate; the second one an 8000 Hz sampling rate." << std::endl;
std::cout << "Both files should contain mono sound, not stereo. " << std::endl;
std::cout << std::endl;
std::cout << "For the effect to be obvious, its best that one file is simply a resampled version" << std::endl;
std::cout << "of the other one." << std::endl;
std::cout << std::endl;
std::cout << "Enter chain choice: " << std::endl;
std::cout << "(1) Always use a sampling rate converter" << std::endl;
std::cout << "(2) Using a MIPComponentAlias in one subchain" << std::endl;
std::cout << "(3) Bad chain which causes strange sync problem" << std::endl;
std::cout << std::endl;
std::cout << "Note that in cases (1) and (2), you should here exactly the same thing. If you follow" << std::endl;
std::cout << "the suggestion above and use one soundfile that is a resampled version of the other" << std::endl;
std::cout << "one, you'll simply hear the sound in that file (but somewhat louder). In case (3) on" << std::endl;
std::cout << "the other hand, the two streams are not synchronized and you will hear an offset of" << std::endl;
std::cout << "one second on one of the streams." << std::endl;
std::cout << std::endl;
std::cout << "Your choice: ";
std::cin >> choice;
std::cout << std::endl;
// We'll open the file 'soundfile.wav'
returnValue = sndFileInput1.open("soundfile-16000-mono.wav", interval);
checkError(returnValue, sndFileInput1);
returnValue = sndFileInput2.open("soundfile-8000-mono.wav", interval);
checkError(returnValue, sndFileInput2);
// Get the parameters of the soundfile. We'll use these to initialize
// the soundcard output component further on.
int samplingRate = 16000;
int numChannels = 1;
// Initialize the sampling rate converter
returnValue = sampConv.init(samplingRate, numChannels);
checkError(returnValue, sampConv);
// Initialize the mixer
returnValue = mixer.init(samplingRate, numChannels, interval, false);
checkError(returnValue, mixer);
// Initialize the soundcard output
returnValue = sndCardOutput.open(samplingRate, numChannels, interval);
checkError(returnValue, sndCardOutput);
// Initialize the sample encoder
#ifndef WIN32
#ifdef MIPCONFIG_SUPPORT_PORTAUDIO
// The PortAudio output component uses signed 16 bit samples
returnValue = sampEnc.init(MIPRAWAUDIOMESSAGE_TYPE_S16);
#else
// The OSS component can use several encoding types. We'll ask
// the component to which format samples should be converted.
returnValue = sampEnc.init(sndCardOutput.getRawAudioSubtype());
#endif
#else
// The WinMM output component uses signed little endian 16 bit samples.
returnValue = sampEnc.init(MIPRAWAUDIOMESSAGE_TYPE_S16LE);
#endif
checkError(returnValue, sampEnc);
// Next, we'll create the chain
if (choice == 1)
{
// Layer 1:
// MIPAverageTimer (0xbfdbbc7c) -> MIPWAVInput (0xbfdbbc28)
// MIPAverageTimer (0xbfdbbc7c) -> MIPWAVInput (0xbfdbbbd4)
// Layer 2:
// MIPWAVInput (0xbfdbbc28) -> MIPSamplingRateConverter (0xbfdbbcd0)
// MIPWAVInput (0xbfdbbbd4) -> MIPSamplingRateConverter (0xbfdbbcd0)
// Layer 3:
// MIPSamplingRateConverter (0xbfdbbcd0) -> MIPAudioMixer (0xbfdbbb24)
// Layer 4:
// MIPAudioMixer (0xbfdbbb24) -> MIPSampleEncoder (0xbfdbbd1c)
// Layer 5:
// MIPSampleEncoder (0xbfdbbd1c) -> MIPOSSInputOutput (0xbfdbb8f8)
returnValue = chain.setChainStart(&timer);
checkError(returnValue, chain);
returnValue = chain.addConnection(&timer, &sndFileInput1);
checkError(returnValue, chain);
returnValue = chain.addConnection(&timer, &sndFileInput2);
checkError(returnValue, chain);
returnValue = chain.addConnection(&sndFileInput1, &sampConv);
checkError(returnValue, chain);
returnValue = chain.addConnection(&sndFileInput2, &sampConv);
checkError(returnValue, chain);
returnValue = chain.addConnection(&sampConv, &mixer);
checkError(returnValue, chain);
returnValue = chain.addConnection(&mixer, &sampEnc);
checkError(returnValue, chain);
returnValue = chain.addConnection(&sampEnc, &sndCardOutput);
checkError(returnValue, chain);
}
else if (choice == 2)
{
// Layer 1:
// MIPAverageTimer (0xbfbf84bc) -> MIPWAVInput (0xbfbf8468)
// MIPAverageTimer (0xbfbf84bc) -> MIPWAVInput (0xbfbf8414)
// Layer 2:
// MIPWAVInput (0xbfbf8468) -> MIPWAVInput-alias (0xbfbf85a0)
// MIPWAVInput (0xbfbf8414) -> MIPSamplingRateConverter (0xbfbf8510)
// Layer 3:
// MIPWAVInput-alias (0xbfbf85a0) -> MIPAudioMixer (0xbfbf8364)
// MIPSamplingRateConverter (0xbfbf8510) -> MIPAudioMixer (0xbfbf8364)
// Layer 4:
// MIPAudioMixer (0xbfbf8364) -> MIPSampleEncoder (0xbfbf855c)
// Layer 5:
// MIPSampleEncoder (0xbfbf855c) -> MIPOSSInputOutput (0xbfbf8138)
returnValue = chain.setChainStart(&timer);
checkError(returnValue, chain);
returnValue = chain.addConnection(&timer, &sndFileInput1);
checkError(returnValue, chain);
returnValue = chain.addConnection(&timer, &sndFileInput2);
checkError(returnValue, chain);
returnValue = chain.addConnection(&sndFileInput1, &alias, false, 0, 0);
checkError(returnValue, chain);
returnValue = chain.addConnection(&sndFileInput2, &sampConv);
checkError(returnValue, chain);
returnValue = chain.addConnection(&sampConv, &mixer);
checkError(returnValue, chain);
returnValue = chain.addConnection(&alias, &mixer);
checkError(returnValue, chain);
returnValue = chain.addConnection(&mixer, &sampEnc);
checkError(returnValue, chain);
returnValue = chain.addConnection(&sampEnc, &sndCardOutput);
checkError(returnValue, chain);
}
else if (choice == 3)
{
// Layer 1:
// MIPAverageTimer (0xbfe8133c) -> MIPWAVInput (0xbfe812e8)
// MIPAverageTimer (0xbfe8133c) -> MIPWAVInput (0xbfe81294)
// Layer 2:
// MIPWAVInput (0xbfe812e8) -> MIPAudioMixer (0xbfe811e4)
// MIPWAVInput (0xbfe81294) -> MIPSamplingRateConverter (0xbfe81390)
// Layer 3:
// MIPAudioMixer (0xbfe811e4) -> MIPSampleEncoder (0xbfe813dc)
// MIPSamplingRateConverter (0xbfe81390) -> MIPAudioMixer (0xbfe811e4)
// Layer 4:
// MIPSampleEncoder (0xbfe813dc) -> MIPOSSInputOutput (0xbfe80fb8)
returnValue = chain.setChainStart(&timer);
checkError(returnValue, chain);
returnValue = chain.addConnection(&timer, &sndFileInput1);
checkError(returnValue, chain);
returnValue = chain.addConnection(&timer, &sndFileInput2);
checkError(returnValue, chain);
returnValue = chain.addConnection(&sndFileInput1, &mixer);
checkError(returnValue, chain);
returnValue = chain.addConnection(&sndFileInput2, &sampConv);
checkError(returnValue, chain);
returnValue = chain.addConnection(&sampConv, &mixer);
checkError(returnValue, chain);
returnValue = chain.addConnection(&mixer, &sampEnc);
checkError(returnValue, chain);
returnValue = chain.addConnection(&sampEnc, &sndCardOutput);
checkError(returnValue, chain);
}
else
{
std::cerr << "Invalid choice" << std::endl;
return -1;
}
// Start the chain
returnValue = chain.start();
checkError(returnValue, chain);
// We'll wait until enter is pressed
std::string str;
std::cout << "Type something to quit" << std::endl;
std::cin >> str;
returnValue = chain.stop();
checkError(returnValue, chain);
// We'll let the destructors of the components take care
// of their de-initialization.
sndCardOutput.close(); // In case we're using PortAudio
#ifdef NEED_PA_INIT
MIPPAInputOutput::terminatePortAudio();
#endif // NEED_PA_INIT
return 0;
}
#else
#include <iostream>
int main(void)
{
std::cerr << "Not all necessary components are available to run this example." << std::endl;
return 0;
}
#endif // WIN32 || _WIN32_WCE || MIPCONFIG_SUPPORT_OSS