-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathSoundRecorder.cpp
More file actions
267 lines (210 loc) · 6.29 KB
/
Copy pathSoundRecorder.cpp
File metadata and controls
267 lines (210 loc) · 6.29 KB
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
////////////////////////////////////////////////////////////
//
// SFML - Simple and Fast Multimedia Library
// Copyright (C) 2007-2015 Laurent Gomila (laurent@sfml-dev.org)
//
// This software is provided 'as-is', without any express or implied warranty.
// In no event will the authors be held liable for any damages arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it freely,
// subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented;
// you must not claim that you wrote the original software.
// If you use this software in a product, an acknowledgment
// in the product documentation would be appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such,
// and must not be misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source distribution.
//
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include <cpp3ds/Audio/SoundRecorder.hpp>
#include <cpp3ds/System/Sleep.hpp>
#include <cpp3ds/System/Err.hpp>
#include <cstring>
#include <malloc.h>
#include <cpp3ds/System/Service.hpp>
namespace cpp3ds
{
static bool isInitialized = false;
////////////////////////////////////////////////////////////
SoundRecorder::SoundRecorder() :
m_thread (&SoundRecorder::record, this),
m_sampleRate (0),
m_processingInterval(milliseconds(100)),
m_isCapturing (false)
{
// Set the device name to the default device
m_deviceName = getDefaultDevice();
if (!isInitialized){
isInitialized = true;
Service::enable(Microphone);
}
m_bufferSize = micGetSampleDataSize();
}
////////////////////////////////////////////////////////////
SoundRecorder::~SoundRecorder()
{
}
////////////////////////////////////////////////////////////
bool SoundRecorder::start(SampleRate sampleRate)
{
// Check if the device can do audio capture
if (!isAvailable())
{
err() << "Failed to start capture: your system cannot capture audio data (call SoundRecorder::isAvailable to check it)" << std::endl;
return false;
}
// Check that another capture is not already running
if (m_isCapturing)
{
err() << "Trying to start audio capture, but another capture is already running" << std::endl;
return false;
}
// Clear the array of samples
m_samples.clear();
// Store the sample rate
if (sampleRate == SampleRate_32730)
m_sampleRate = 32730;
else if (sampleRate == SampleRate_16360)
m_sampleRate = 16360;
else if (sampleRate == SampleRate_10910)
m_sampleRate = 10910;
else
m_sampleRate = 8180;
// Notify derived class
if (onStart())
{
// Start the capture
m_bufferPos = 0;
if (R_FAILED(MICU_StartSampling(MICU_ENCODING_PCM16_SIGNED, static_cast<MICU_SampleRate>(sampleRate), 0, m_bufferSize, true)))
return false;
// memset(&m_ndspWaveBuf, 0, sizeof(ndspWaveBuf));
// m_ndspWaveBuf.data_vaddr = &m_samples[0];
// m_ndspWaveBuf.nsamples = m_samples.size() / 2;
// m_ndspWaveBuf.looping = false;
// m_ndspWaveBuf.status = NDSP_WBUF_FREE;
//
// ndspSetCapture(&m_ndspWaveBuf);
// Start the capture in a new thread, to avoid blocking the main thread
m_isCapturing = true;
m_thread.launch();
return true;
}
return false;
}
////////////////////////////////////////////////////////////
void SoundRecorder::stop()
{
MICU_StopSampling();
// Stop the capturing thread
m_isCapturing = false;
m_thread.wait();
// Notify derived class
onStop();
}
////////////////////////////////////////////////////////////
unsigned int SoundRecorder::getSampleRate() const
{
return m_sampleRate;
}
////////////////////////////////////////////////////////////
std::vector<std::string> SoundRecorder::getAvailableDevices()
{
std::vector<std::string> deviceNameList;
return deviceNameList;
}
////////////////////////////////////////////////////////////
std::string SoundRecorder::getDefaultDevice()
{
return "3ds mic";
}
////////////////////////////////////////////////////////////
bool SoundRecorder::setDevice(const std::string& name)
{
// Store the device name
if (name.empty())
m_deviceName = getDefaultDevice();
else
m_deviceName = name;
if (m_isCapturing)
{
// Stop the capturing thread
m_isCapturing = false;
m_thread.wait();
// Start the capture in a new thread, to avoid blocking the main thread
m_isCapturing = true;
m_thread.launch();
}
return true;
}
////////////////////////////////////////////////////////////
const std::string& SoundRecorder::getDevice() const
{
return m_deviceName;
}
////////////////////////////////////////////////////////////
bool SoundRecorder::isAvailable()
{
return Service::isEnabled(Microphone);
}
////////////////////////////////////////////////////////////
void SoundRecorder::setProcessingInterval(cpp3ds::Time interval)
{
m_processingInterval = interval;
}
////////////////////////////////////////////////////////////
bool SoundRecorder::onStart()
{
// Nothing to do
return true;
}
////////////////////////////////////////////////////////////
void SoundRecorder::onStop()
{
// Nothing to do
}
////////////////////////////////////////////////////////////
void SoundRecorder::record()
{
while (m_isCapturing)
{
// Process available samples
processCapturedSamples();
// Don't bother the CPU while waiting for more captured data
sleep(m_processingInterval);
}
// Capture is finished: clean up everything
cleanup();
}
////////////////////////////////////////////////////////////
void SoundRecorder::processCapturedSamples()
{
u32 lastOffset = micGetLastSampleOffset();
if (lastOffset > m_bufferPos)
{
u32 samplesAvailable = lastOffset - m_bufferPos;
// Forward them to the derived class
if (!onProcessSamples(reinterpret_cast<Int16*>(&Service::m_micBuffer[m_bufferPos]), samplesAvailable / sizeof(Int16)))
{
// The user wants to stop the capture
m_isCapturing = false;
}
m_bufferPos += samplesAvailable;
}
else
m_bufferPos = 0;
}
////////////////////////////////////////////////////////////
void SoundRecorder::cleanup()
{
// Get the samples left in the buffer
processCapturedSamples();
}
} // namespace cpp3ds