-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaudioFileReader.cpp
More file actions
188 lines (153 loc) · 6.38 KB
/
audioFileReader.cpp
File metadata and controls
188 lines (153 loc) · 6.38 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
#include "audioFileReader.hpp"
#include <stdexcept>
static const unsigned NO_REQUEST = 0xffffffff;
AudioFileReader::AudioFileReader(const char *fname, unsigned maxRequestMilliseconds, unsigned maxRememberSeconds, unsigned maxPreloadSeconds) {
alive = true;
error = 0;
size_t chars = std::strlen(fname) + 1;
filename = new char[chars];
std::memcpy(filename, fname, chars);
audioFile = sox_open_read(fname, NULL, NULL, NULL);
if (audioFile == NULL || audioFile->encoding.encoding == SOX_ENCODING_UNKNOWN) {
throw std::invalid_argument("Error: Unable to decode audio. Either the file is corrupt or you have not installed the codecs required to play it. Install the libsox-fmt-all package, then restart OpenScribe and try again.");
} else if (!audioFile->seekable) {
throw std::invalid_argument("Error: Sox is unable to seek in this file. Aborting.");
}
fileInfo.sampleRate = (unsigned) audioFile->signal.rate;
fileInfo.numChannels = audioFile->signal.channels;
fileInfo.numSamples = audioFile->signal.length;
if (fileInfo.numSamples >= 0xffffffff) {
throw std::invalid_argument("Error: File is too big! OpenScribe cannot read audio files with more than 4GB of samples. What are you even trying to play?!?");
}
MAX_REQUEST = (unsigned) ((double) maxRequestMilliseconds * (double) fileInfo.sampleRate * (double) fileInfo.numChannels / 1000.0 + 0.5);
MAX_REQUEST -= MAX_REQUEST % fileInfo.numChannels;
if (MAX_REQUEST == 0) {
throw std::invalid_argument("Error: Sample rate is invalid or could not be determined.");
}
MAX_PRE = maxRememberSeconds * fileInfo.sampleRate * fileInfo.numChannels;
MAX_POST = MAX_REQUEST + maxPreloadSeconds * fileInfo.sampleRate * fileInfo.numChannels;
BUFFER_SIZE = MAX_PRE + MAX_POST;
circleBuffer = new float[BUFFER_SIZE + MAX_REQUEST];
toConvert = new int[MAX_REQUEST];
nil = new float[MAX_REQUEST];
std::memset(nil, 0, MAX_REQUEST * sizeof(float));
preValid = postValid = pos = 0;
requestingReset = NO_REQUEST;
readerThread = new std::thread(&AudioFileReader::preloaderLoop, this);
audioStretcher = new AudioStretcher(this);
}
AudioFileReader::~AudioFileReader() {
accessLock.lock();
alive = false;
accessLock.unlock();
bufferMoved.notify_all();
readerThread->join();
delete readerThread;
delete[] circleBuffer;
delete[] toConvert;
delete[] nil;
delete audioStretcher;
delete[] filename;
sox_close(audioFile);
}
const void *AudioFileReader::readData(unsigned at, size_t numBytes) {
assert(numBytes % sizeof(float) == 0);
register const size_t request = numBytes / sizeof(float);
if (at >= fileInfo.numSamples || !alive) return nil;
std::unique_lock<std::mutex> thisAccess(accessLock);
if (at >= preValid && (at + request <= postValid || (at + request > fileInfo.numSamples && postValid == fileInfo.numSamples))) {
//We already have the data ready in the buffer. Nothing needs to be done
} else if (at >= preValid && at <= postValid && postValid + request <= pos + MAX_POST) {
//The requested data is being read right now. Just wait for it.
readRequest.wait(thisAccess);
} else {
//We don't have the data yet, and it's not being read at this instant
requestingReset = at;
bufferMoved.notify_all();
resetRequest.wait(thisAccess);
}
pos = at + request;
thisAccess.unlock();
bufferMoved.notify_all();
return (void*) &circleBuffer[at % BUFFER_SIZE];
}
HOT void AudioFileReader::preloaderLoop() {
unsigned head = 0;
while (alive) {
std::unique_lock<std::mutex> thisAccess(accessLock);
//wait until we can read more data without overwriting what we want to keep or until a reset is requested
while (alive && requestingReset == NO_REQUEST && (postValid + MAX_REQUEST > pos + MAX_POST || postValid == fileInfo.numSamples)) bufferMoved.wait(thisAccess);
if (!alive) break;
//handle reset requests
if (requestingReset != NO_REQUEST) {
preValid = postValid = pos = requestingReset;
readInto(requestingReset % BUFFER_SIZE, head);
postValid += MAX_REQUEST;
requestingReset = NO_REQUEST;
thisAccess.unlock();
resetRequest.notify_all();
continue;
}
//read data into the buffer
if (postValid + MAX_REQUEST > preValid + BUFFER_SIZE) {
preValid = postValid + MAX_REQUEST - BUFFER_SIZE;
}
thisAccess.unlock();
unsigned i = postValid % BUFFER_SIZE;
if (postValid >= fileInfo.numSamples) {
std::memset((void*) &circleBuffer[i], 0, MAX_REQUEST * sizeof(float));
} else {
readInto(i, head);
}
thisAccess.lock();
postValid += MAX_REQUEST;
if (postValid > fileInfo.numSamples) postValid = fileInfo.numSamples;
thisAccess.unlock();
readRequest.notify_all();
}
}
HOT void AudioFileReader::readInto(unsigned index, unsigned &head) {
bool retry = false;
unsigned read = 0;
do {
if (head != postValid) {
//file head is not where we want to read- seek to it
sox_seek(audioFile, postValid, SOX_SEEK_SET);
head = postValid;
}
read = sox_read(audioFile, toConvert, MAX_REQUEST);
//SoX reads in signed 32-bit integer format, but I want floating point format. Convert it.
for (unsigned i = 0; i < read; i++) circleBuffer[index + i] = (float) ((double) toConvert[i] / (double) 0x80000000);
head += read;
if (head > fileInfo.numSamples) head = fileInfo.numSamples;
/*
* When seeking to or from the end of an audio file, SoX will stop reading data from the file.
* Until I find a better library, I'll work around this by closing the file and opening it again
* If we immediately fail again, then an actual error occurred.
*/
if (read == 0 && postValid < fileInfo.numSamples) {
if (retry) {
//Okay, something actually went wrong here
error = 1;
alive = false;
std::memset((void*) &circleBuffer[index], 0, MAX_REQUEST * sizeof(float));
return;
}
// "have you tried turning it off and on again?"
sox_close(audioFile);
audioFile = sox_open_read(filename, NULL, NULL, NULL);
sox_seek(audioFile, postValid, SOX_SEEK_SET);
head = postValid;
retry = true;
} else break;
} while (true);
if (read < MAX_REQUEST) std::memset((void*) &circleBuffer[index + read], 0, (MAX_REQUEST - read)*sizeof(float));
// wrap around
if (index + MAX_REQUEST > BUFFER_SIZE) {
// end -> start
std::memcpy((void*) circleBuffer, (void*) &circleBuffer[BUFFER_SIZE], (index + MAX_REQUEST - BUFFER_SIZE)*sizeof(float));
} else if (index < MAX_REQUEST) {
// start -> end
std::memcpy((void*) &circleBuffer[BUFFER_SIZE + index], (void*) &circleBuffer[index], (MAX_REQUEST - index)*sizeof(float));
}
}