-
Notifications
You must be signed in to change notification settings - Fork 10
/
RecordingIO.cpp
243 lines (189 loc) · 6.75 KB
/
RecordingIO.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
//
// Created by asalehin on 7/11/20.
//
#include <cstdint>
#include <cstdio>
#include <string>
#include <unistd.h>
#include "RecordingIO.h"
#include "../logging_macros.h"
#include "../utils/Utils.h"
#include <mutex>
#include <condition_variable>
#include <sys/stat.h>
#include "RecordingEngine.h"
mutex RecordingIO::flushMtx;
condition_variable RecordingIO::flushed;
bool RecordingIO::ongoing_flush_completed = true;
bool RecordingIO::check_if_flush_completed() {
return ongoing_flush_completed;
}
mutex RecordingIO::livePlaybackMtx;
condition_variable RecordingIO::livePlaybackRead;
bool RecordingIO::live_playback_read_completed = true;
bool RecordingIO::check_if_live_playback_read_completed() {
return live_playback_read_completed;
}
RecordingIO::RecordingIO() {
Player* player = new Player();
mPlayer.reset(move(player));
taskQueue = move(new TaskQueue());
}
void RecordingIO::reserveRecordingBuffer() {
mData.reserve(kMaxSamples);
}
void RecordingIO::clearRecordingBuffer() {
mData.resize(0);
}
FileDataSource* RecordingIO::setup_audio_source(int fd) {
if (!validate_audio_file()) {
return nullptr;
}
AudioProperties targetProperties{
.channelCount = RecordingStreamConstants::mOutputChannelCount,
.sampleRate = RecordingStreamConstants::mSampleRate
};
return FileDataSource::newFromCompressedFile(mRecordingFilePath.c_str(), fd, targetProperties);
}
void RecordingIO::add_source_to_player(shared_ptr<DataSource> fileDataSource) {
map<string, shared_ptr<DataSource>> sourceMap;
sourceMap.insert(pair<string, shared_ptr<DataSource>>(mRecordingFilePath, fileDataSource));
mPlayer->addSourceMap(sourceMap);
mPlayer->setPlaying(true);
int32_t playHead = mPlayer->getPlayHead();
if (playHead >= mPlayer->getTotalSampleFrames()) {
playHead = 0;
mPlayer->setPlayHead(playHead);
}
}
bool RecordingIO::validate_audio_file() {
return !(mRecordingFilePath.empty() || (access(mRecordingFilePath.c_str(), F_OK) == -1));
}
void RecordingIO::read_playback(float *targetData, int32_t numFrames) {
if (targetData && mPlayer) {
mPlayer->renderAudio(targetData, numFrames);
}
}
void RecordingIO::flush_to_file(int16_t* data, int32_t length, const string& recordingFilePath, shared_ptr<SndfileHandle>& recordingFile) {
if (!recordingFile) {
int format = SF_FORMAT_WAV | SF_FORMAT_PCM_16;
SndfileHandle file = SndfileHandle(recordingFilePath, SFM_WRITE, format, RecordingStreamConstants::mInputChannelCount, RecordingStreamConstants::mSampleRate);
recordingFile = make_shared<SndfileHandle>(file);
}
recordingFile->write(data, length);
}
int32_t RecordingIO::write(const int16_t *sourceData, int32_t numSamples) {
// Wait if a flush action is already in progress
// Known Issue: If live playback is on, this will blink in the live playback for a while
unique_lock<mutex> lck(flushMtx);
flushed.wait(lck, check_if_flush_completed);
lck.unlock();
if (mData.size() + numSamples >= kMaxSamples) {
flush_buffer();
}
for(int i = 0; i < numSamples; i++) {
mData.push_back(sourceData[i] * gain_factor);
if (currentMax < abs(sourceData[i])) {
currentMax = abs(sourceData[i]);
}
}
mTotalSamples += numSamples;
return numSamples;
}
void RecordingIO::flush_buffer() {
if (mData.size() > 0) {
ongoing_flush_completed = false;
int numItems = mData.size();
copy(mData.begin(), mData.begin() + numItems, mBuff);
taskQueue->enqueue(move([&]() {
flush_to_file(mBuff, numItems, mRecordingFilePath, mRecordingFile);
}));
// Wait if a live playback read action is already in progress
unique_lock<mutex> lck(livePlaybackMtx);
livePlaybackRead.wait(lck, check_if_live_playback_read_completed);
lck.unlock();
mData.clear();
mLivePlaybackReadIndex = 0;
ongoing_flush_completed = true;
flushed.notify_all();
}
}
int32_t RecordingIO::read_live_playback(int16_t *targetData, int32_t numSamples) {
if (mLivePlaybackReadIndex > mData.size()) {
return 0;
}
int32_t framesRead = 0;
auto framesToCopy = numSamples;
if (mLivePlaybackReadIndex + numSamples >= mData.size()) {
framesToCopy = mData.size() - mLivePlaybackReadIndex;
}
if (framesToCopy > 0) {
// Set the flag so that any flush action waits
live_playback_read_completed = false;
copy(mData.begin() + mLivePlaybackReadIndex, mData.begin() + mLivePlaybackReadIndex + framesToCopy, targetData);
mLivePlaybackReadIndex += framesToCopy;
live_playback_read_completed = true;
livePlaybackRead.notify_all();
}
return framesRead;
}
void RecordingIO::sync_live_playback() {
mLivePlaybackReadIndex = mData.size();
}
void RecordingIO::setLivePlaybackEnabled(bool livePlaybackEnabled) {
mLivePlaybackEnabled = livePlaybackEnabled;
}
int RecordingIO::getCurrentMax() {
int temp = currentMax;
currentMax = 0;
return temp;
}
int64_t RecordingIO::getPlayerMaxTotalSourceFrames() {
return mPlayer->getMaxTotalSourceFrames();
}
void RecordingIO::resetCurrentMax() {
currentMax = 0;
}
void RecordingIO::setStopPlaybackCallback(function<void()> stopPlaybackCallback) {
mStopPlaybackCallback = stopPlaybackCallback;
mPlayer->setPlaybackCallback(mStopPlaybackCallback);
}
int RecordingIO::getTotalSampleFrames() {
if (mPlayer) {
return mPlayer->getTotalSampleFrames();
}
return 0;
}
int32_t RecordingIO::getCurrentPlaybackProgress() {
return mPlayer->getPlayHead();
}
void RecordingIO::setPlayHead(int position) {
mPlayer->setPlayHead(position);
}
int RecordingIO::getDurationInSeconds() {
return (int) mTotalSamples / (RecordingStreamConstants::mInputChannelCount * RecordingStreamConstants::mSampleRate);
}
void RecordingIO::clearPlayerSources() {
mPlayer->clearSources();
}
void RecordingIO::setPlaybackPlaying(bool playing) {
mPlayer->setPlaying(playing);
}
void RecordingIO::addSourceMap(map<string, shared_ptr<DataSource>> playMap) {
mPlayer->addSourceMap(playMap);
}
void RecordingIO::addSourceMapWithRecordedSource(map<string, shared_ptr<DataSource>> playMap, shared_ptr<DataSource> recordedSource) {
playMap.insert(pair<string, shared_ptr<DataSource>>(mRecordingFilePath, recordedSource));
mPlayer->addSourceMap(playMap);
}
bool RecordingIO::checkPlayerSources(map<string, shared_ptr<DataSource>> playMap) {
return mPlayer->checkPlayerSources(playMap);
}
void RecordingIO::resetProperties() {
taskQueue->clear_queue();
mPlayer->setPlayHead(0);
mRecordingFile.reset();
mTotalSamples = 0;
mLivePlaybackReadIndex = 0;
unlink(mRecordingFilePath.c_str());
}