-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathsoundrecorder.cpp
233 lines (196 loc) · 4.39 KB
/
soundrecorder.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
/**
* \file soundrecorder.cpp
*/
#include <mipconfig.h>
#if(defined(MIPCONFIG_SUPPORT_OSS) || defined(MIPCONFIG_SUPPORT_WINMM) || defined(MIPCONFIG_SUPPORT_PORTAUDIO))
#include <mipcomponentchain.h>
#include <mipcomponent.h>
#include <miptime.h>
#include <mipwavoutput.h>
#ifdef MIPCONFIG_SUPPORT_WINMM
#include <mipwinmminput.h>
#else
#ifdef MIPCONFIG_SUPPORT_PORTAUDIO
#include <mippainputoutput.h>
#define NEED_PA_INIT
#else
#include <mipossinputoutput.h>
#endif
#endif
#include <miprawaudiomessage.h> // Needed for MIPRAWAUDIOMESSAGE_TYPE_S16LE
#include <mipsampleencoder.h>
#include <iostream>
#include <string>
#include <stdio.h>
class SoundRecorder : public MIPErrorBase
{
public:
SoundRecorder();
virtual ~SoundRecorder();
bool init(const std::string &fname, int sampRate);
bool destroy();
protected:
virtual void onThreadExit(bool err, const std::string &compName, const std::string &errStr) { }
private:
class SoundRecorderChain : public MIPComponentChain
{
public:
SoundRecorderChain(SoundRecorder *pSndRec) : MIPComponentChain("Sound Recorder Chain") { m_pSndRec = pSndRec; }
protected:
void onThreadExit(bool err, const std::string &compName, const std::string &errStr) { m_pSndRec->onThreadExit(err, compName, errStr); }
private:
SoundRecorder *m_pSndRec;
};
void zeroAll();
void deleteAll();
bool m_init;
SoundRecorderChain *m_pChain;
#ifdef MIPCONFIG_SUPPORT_WINMM
MIPWinMMInput *m_pInput;
#else
#ifdef MIPCONFIG_SUPPORT_PORTAUDIO
MIPPAInputOutput *m_pInput;
#else
MIPOSSInputOutput *m_pInput;
#endif
#endif
MIPSampleEncoder *m_pSampEnc;
MIPWAVOutput *m_pOutput;
friend class SoundRecorderChain;
};
SoundRecorder::SoundRecorder()
{
zeroAll();
m_init = false;
}
SoundRecorder::~SoundRecorder()
{
deleteAll();
}
bool SoundRecorder::init(const std::string &fname, int sampRate)
{
if (m_init)
{
setErrorString("Already initialized");
return false;
}
MIPTime interval(0.200); // We'll use 200 ms intervals
int channels = 1;
#ifdef MIPCONFIG_SUPPORT_WINMM
m_pInput = new MIPWinMMInput();
if (!m_pInput->open(sampRate, channels, interval, MIPTime(10.0), true)) // TODO: always use high priority?
{
setErrorString(m_pInput->getErrorString());
deleteAll();
return false;
}
#else
#ifdef MIPCONFIG_SUPPORT_PORTAUDIO
m_pInput = new MIPPAInputOutput();
if (!m_pInput->open(sampRate, channels, interval, MIPPAInputOutput::ReadOnly))
{
setErrorString(m_pInput->getErrorString());
deleteAll();
return false;
}
#else
m_pInput = new MIPOSSInputOutput();
if (!m_pInput->open(sampRate, channels, interval, MIPOSSInputOutput::ReadOnly))
{
setErrorString(m_pInput->getErrorString());
deleteAll();
return false;
}
#endif
#endif
m_pSampEnc = new MIPSampleEncoder();
if (!m_pSampEnc->init(MIPRAWAUDIOMESSAGE_TYPE_U8))
{
setErrorString(m_pSampEnc->getErrorString());
deleteAll();
return false;
}
m_pOutput = new MIPWAVOutput();
if (!m_pOutput->open(fname, sampRate))
{
setErrorString(m_pOutput->getErrorString());
deleteAll();
return false;
}
m_pChain = new SoundRecorderChain(this);
m_pChain->setChainStart(m_pInput);
m_pChain->addConnection(m_pInput, m_pSampEnc);
m_pChain->addConnection(m_pSampEnc, m_pOutput);
if (!m_pChain->start())
{
setErrorString(m_pChain->getErrorString());
deleteAll();
return false;
}
m_init = true;
return true;
}
bool SoundRecorder::destroy()
{
if (!m_init)
{
setErrorString("Not initialized");
return false;
}
deleteAll();
m_init = false;
return true;
}
void SoundRecorder::zeroAll()
{
m_pChain = 0;
m_pInput = 0;
m_pSampEnc = 0;
m_pOutput = 0;
}
void SoundRecorder::deleteAll()
{
if (m_pChain)
{
m_pChain->stop();
delete m_pChain;
}
if (m_pInput)
delete m_pInput;
if (m_pSampEnc)
delete m_pSampEnc;
if (m_pOutput)
delete m_pOutput;
zeroAll();
}
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
SoundRecorder sndRec;
if (!sndRec.init("sound.wav",44100))
{
std::cout << sndRec.getErrorString() << std::endl;
return -1;
}
getc(stdin);
sndRec.destroy();
#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 //