~mhall119/ubuntu-app-reviews/qtranscribe

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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/*
 *  Copyright © 2012 Matt Pharoah (mr.exuberant@gmail.com)
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#ifndef AUDIOFILEREADER_HH_
#define AUDIOFILEREADER_HH_

#include <cstring>
#include <thread>
#include <mutex>
#include <condition_variable>
#include <sonic.h>
#include <sox.h>

using namespace std;

/*
 * Stores the audio in a wrapping buffer, allowing for buffering without lots
 * of memory copies or moves. It also allows quick skip backs if max_remembered
 * is sufficiently large.
 *
 * This was written ad hoc - it is meant to be efficient, not safe. The data
 * returned is a pointer to a position in the buffer, not a copy. This data is
 * only valid until the next time data is requested, at which point the
 * previous data is no longer necessarily valid. It also assumes that you will
 * only attempt to access at most REQUEST_SIZE * sizeof(int) bytes of the
 * returned data and that you will not modify it.
 *
 */
class AudioFileReader {
  private:
	unsigned int MAX_PRE;
	unsigned int MAX_POST;
	unsigned int BUFFER_SIZE;
	unsigned int REQUEST_SIZE;
	unsigned int FILE_SIZE;
	unsigned int pre_valid;
	unsigned int post_valid;
	unsigned int position; //predicted next request (last request position + REQUEST_SIZE)

	float *circleBuffer;
	int *toConvert;
	float *nil;
	sox_format_t *audioFile;
	const char *filename;
	volatile bool *SoXError;

	thread *preloader;
	mutex transaction;
	volatile bool alive;

	condition_variable bufferMoved;
	condition_variable readRequest;
	condition_variable resetRequest;
	unsigned int requestingReset;
	static const unsigned int NO_REQUEST = 0xffffffff;

	static void callPreloaderLoop(AudioFileReader *me, unsigned int CHANNELS) {
		me->preloaderLoop(CHANNELS); //preloaderLoop is inline so it will be expaded here. I just did this so I don't have to prefix every variable with "me->"
	}

	inline void readInto(unsigned int index, unsigned int CHANNELS, unsigned int &fileHead) { //helper function
		bool retry = false;
try_again:
		if (fileHead != post_valid) {
			sox_seek(audioFile, post_valid, SOX_SEEK_SET);
			fileHead = post_valid;
		}
		unsigned int numRead = sox_read(audioFile, toConvert, REQUEST_SIZE);
		for (unsigned int i = 0; i < numRead; i++) circleBuffer[index + i] = (float) (((double) toConvert[i]) / (double) 2147483648);
		fileHead += numRead;

		if (numRead == 0 && post_valid < FILE_SIZE) { //good job, SoX! <_<
			if (retry) { //already tried to recover and failed. Give up. (This shouldn't happen)
				*SoXError = true;
				alive = false;
				memset((void *) &circleBuffer[index], 0, REQUEST_SIZE * sizeof(float));
				return;
			}

			sox_close(audioFile);
			audioFile = sox_open_read(filename, NULL, NULL, NULL);
			sox_seek(audioFile, post_valid, SOX_SEEK_SET);
			fileHead = post_valid;
			retry = true;
			goto try_again;
		}

		if (numRead < REQUEST_SIZE) memset((void *) &circleBuffer[index + numRead], 0, (REQUEST_SIZE - numRead) * sizeof(float));
		if (index + REQUEST_SIZE > BUFFER_SIZE) {
			memcpy((void *) circleBuffer, (void *) &circleBuffer[BUFFER_SIZE], (index + REQUEST_SIZE - BUFFER_SIZE) * sizeof(float));
		} else if (index < REQUEST_SIZE) {
			memcpy((void *) &circleBuffer[BUFFER_SIZE + index], (void *) &circleBuffer[index], (REQUEST_SIZE - index) * sizeof(float));
		}
	}

	inline void preloaderLoop(unsigned int CHANNELS) {
		unsigned int fileHead = 0;
		while (alive) {
			unique_lock<mutex> myTransaction(transaction);
			//wait until we can safely read more data into the circle buffer or until a reset is requested
			while (alive && requestingReset == NO_REQUEST && post_valid + REQUEST_SIZE > position + MAX_POST && post_valid != FILE_SIZE) {
				bufferMoved.wait(myTransaction);
			}
			if (!alive) break;

			//handle reset requests
			if (requestingReset != NO_REQUEST) {
				pre_valid = requestingReset;
				post_valid = requestingReset;
				position = requestingReset;
				readInto(requestingReset % BUFFER_SIZE, CHANNELS, fileHead);
				post_valid += REQUEST_SIZE;
				requestingReset = NO_REQUEST;
				resetRequest.notify_all();
				myTransaction.unlock();
				continue;
			}

			//read data into the buffer
			if (post_valid + REQUEST_SIZE > pre_valid + BUFFER_SIZE) {
				pre_valid = post_valid + REQUEST_SIZE - BUFFER_SIZE;
			}
			myTransaction.unlock();

			unsigned int index = post_valid % BUFFER_SIZE;
			if (post_valid >= FILE_SIZE) {
				memset((void *) &circleBuffer[index], 0, REQUEST_SIZE * sizeof(float));
			} else {
				readInto(index, CHANNELS, fileHead);
			}

			myTransaction.lock();
			post_valid += REQUEST_SIZE;
			if (post_valid > FILE_SIZE) post_valid = FILE_SIZE;
			readRequest.notify_all();
			myTransaction.unlock();
		}
	}

  public:
	AudioFileReader(sox_format_t *f, const char *fname, volatile bool *errorCheck, unsigned int CHANNELS, unsigned int SIZE, unsigned int request_size, unsigned int max_preload, unsigned int max_remembered = 0) {
		SoXError = errorCheck;
		audioFile = f;
		filename = fname;
		REQUEST_SIZE = request_size;
		MAX_PRE = max_remembered; if (MAX_PRE < REQUEST_SIZE) MAX_PRE = REQUEST_SIZE;
		MAX_POST = max_preload; if (MAX_POST < REQUEST_SIZE) MAX_POST = REQUEST_SIZE;
		BUFFER_SIZE = MAX_PRE + MAX_POST;
		FILE_SIZE = SIZE;
		circleBuffer = new float[BUFFER_SIZE + REQUEST_SIZE];
		pre_valid = 0;
		post_valid = 0;
		position = 0;
		alive = true;
		requestingReset = NO_REQUEST;
		nil = new float[REQUEST_SIZE];
		memset((void *) nil, 0, REQUEST_SIZE * sizeof(float));
		toConvert = new int[REQUEST_SIZE];
		preloader = new thread(callPreloaderLoop, this, CHANNELS);
	}

	//this memory is only valid until the next time you request data, at which point the previous request may no longer be valid
	void *operator[](unsigned int x) {
		if (x >= FILE_SIZE) return nil;
		unique_lock<mutex> myTransaction(transaction);
		if (x >= pre_valid && x + REQUEST_SIZE <= post_valid) {
			//everything is fine!
		} else if (x >= pre_valid && x <= post_valid && post_valid + REQUEST_SIZE <= position + MAX_POST) {
			readRequest.wait(myTransaction);
		} else {
			requestingReset = x;
			bufferMoved.notify_all();
			resetRequest.wait(myTransaction);
		}
		void *ret = (void *) &circleBuffer[x % BUFFER_SIZE];
		position = x + REQUEST_SIZE;
		bufferMoved.notify_all();
		myTransaction.unlock();
		return ret;
	}

	inline unsigned int getRequestSize() {
		return REQUEST_SIZE;
	}

	~AudioFileReader() {
		transaction.lock();
		alive = false;
		bufferMoved.notify_all();
		transaction.unlock();
		preloader->join();
		delete preloader;
		delete[] circleBuffer;
		delete[] nil;
		delete[] toConvert;
	}
};

/*
 * A simple class to handle slowing down audio. The actual audio slowing
 * algorithms are in the sonic libraries- this is just a wrapper.
 */
class SlowBuffer {
  private:
	AudioFileReader *reader;
	sonicStream stretcher;
	unsigned int REQUEST_SIZE;
	unsigned int CHANNELS;
	unsigned int inPos;
	unsigned int outPos;
	unsigned int INPUT_BUFFER_SIZE;
	double SPEED;

	void trashStreamData() {
		sonicFlushStream(stretcher);
		unsigned int size = sonicSamplesAvailable(stretcher);
		float trash[size * CHANNELS];
		sonicReadFloatFromStream(stretcher, trash, size);
	}

  public:
	SlowBuffer(AudioFileReader *afr, unsigned int SAMPLE_RATE, unsigned int chans) {
		reader = afr;
		CHANNELS = chans;
		REQUEST_SIZE = reader->getRequestSize();
		SPEED = 1.0;
		inPos = 0xffffffff;
		outPos = 0xffffffff;
		INPUT_BUFFER_SIZE = SAMPLE_RATE * CHANNELS * 3; //3 second buffer
		stretcher = sonicCreateStream(SAMPLE_RATE, CHANNELS);
	}

	//reads REQUEST_SIZE frames into dest from position req in the file, then
	//returns the number of 100% speed frames elapsed (ie. the number of frames
	//copied times the speed)
	unsigned int fetchInto(unsigned int req, float *dest) {
		if (req != outPos) {
			//we've skipped to a new position, so flush out the buffer
			trashStreamData();
			inPos = req;
			outPos = req;
		}
		//top off the buffer
		while (inPos < outPos + INPUT_BUFFER_SIZE) {
			sonicWriteFloatToStream(stretcher, (float *) (*reader)[inPos], REQUEST_SIZE / CHANNELS);
			inPos += REQUEST_SIZE;
		}
		//if not enough samples are available, wait
		while ((unsigned int) sonicSamplesAvailable(stretcher) < REQUEST_SIZE / CHANNELS) this_thread::yield();
		sonicReadFloatFromStream(stretcher, dest, REQUEST_SIZE / CHANNELS);
		outPos += (unsigned int) (((double) REQUEST_SIZE) * SPEED);
		return (unsigned int) (((double) REQUEST_SIZE) * SPEED);
	}

	void setSpeed(float speed) {
		trashStreamData();
		inPos = 0xffffffff;
		outPos = 0xffffffff;
		sonicSetSpeed(stretcher, speed);
		SPEED = (double) speed;
	}

	~SlowBuffer() {
		sonicDestroyStream(stretcher);
	}
};


#endif /* AUDIOFILEREADER_HH_ */