-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmicrophone_rec.cpp
More file actions
79 lines (58 loc) · 1.76 KB
/
microphone_rec.cpp
File metadata and controls
79 lines (58 loc) · 1.76 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
#include "microphone_rec.h"
#include <cstring>
#include <chrono>
#include <thread>
MicrophoneRec::MicrophoneRec()
{
//system initialization
FMOD::System_Create(&system);
system->setOutput(FMOD_OUTPUTTYPE_DSOUND);
//playback devices
system->setDriver(0);
//record devices
record_driver = 0;
system->setSoftwareFormat(sampleRate, FMOD_SOUND_FORMAT_PCMFLOAT, 1, 0, FMOD_DSP_RESAMPLER_LINEAR);
result = system->init(32, FMOD_INIT_NORMAL, 0);
//create sound to record to
create_sound();
}
MicrophoneRec::~MicrophoneRec()
{
sound->release();
system->release();
}
void MicrophoneRec::create_sound()
{
if (sound != NULL)
sound->release();
FMOD_CREATESOUNDEXINFO sound_info;
std::memset(&sound_info, 0, sizeof(FMOD_CREATESOUNDEXINFO));
sound_info.cbsize = sizeof(FMOD_CREATESOUNDEXINFO);
sound_info.length = sampleRate * sizeof(float) * 0.5;
sound_info.numchannels = 1;
sound_info.defaultfrequency = sampleRate;
sound_info.format = FMOD_SOUND_FORMAT_PCMFLOAT;
system->createSound(0, FMOD_2D | FMOD_SOFTWARE | FMOD_LOOP_NORMAL | FMOD_OPENUSER, &sound_info, &sound);
}
void MicrophoneRec::start_capturing()
{
system->recordStart(record_driver, sound, true);
std::this_thread::sleep_for(std::chrono::microseconds(100));
system->playSound(FMOD_CHANNEL_REUSE, sound, false, &channel);
//mute output
channel->setVolume(0);
}
void MicrophoneRec::stop_capturing()
{
channel->stop();
// Stop recording
system->recordStop(record_driver);
// Free sound recording buffer
sound->release();
sound = NULL;
}
std::array<float, bufferSize> MicrophoneRec::get_waveData()
{
channel->getWaveData(wave_data.data(), bufferSize, 0); //0 for mono
return wave_data;
}