-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdictation.hpp
More file actions
254 lines (212 loc) · 5.63 KB
/
dictation.hpp
File metadata and controls
254 lines (212 loc) · 5.63 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
#ifndef DICTATION_HPP_
#define DICTATION_HPP_
#include "audioFileReader.hpp"
#include "config.hpp"
#include <mutex>
extern "C" {
#include <pulse/stream.h>
#include <pulse/sample.h>
#include <pulse/def.h>
#include <pulse/mainloop.h>
}
class Dictation {
private:
AudioFileReader *reader;
void (*onReaderError)(int);
std::mutex errorLock;
unsigned short RWD_SPEED;
unsigned short FFWD_SPEED;
bool SFX;
unsigned BUFFER_FRAMES;
float *SFX_RWD;
float *SFX_FFWD;
std::thread *loopThread;
pa_stream *audioStream;
pa_mainloop *paLoop;
pa_context *paContext;
unsigned position;
float slowSpeed;
bool paused;
bool slowed;
char *fileName;
mutable std::mutex readLock;
std::mutex writeLock;
std::condition_variable pauseWait;
enum PACKED {
REWIND,
NORMAL,
FAST_FORWARD
} mode;
HOT static void fetchAudioData(pa_stream *stream, size_t bytes, void *myself);
void genFX();
HOT void mainloop();
public:
INLINE Dictation() :
reader(NULL), RWD_SPEED(8), FFWD_SPEED(8), SFX(true), SFX_RWD(NULL), SFX_FFWD(NULL),
loopThread(NULL), position(0), slowSpeed(0.5f), paused(true), slowed(false), fileName(NULL), mode(NORMAL) { onReaderError = NULL; }
INLINE ~Dictation() { closeFile(); }
INLINE void connectErrorHandler(void (*errorHandler)(int)) {
errorLock.lock();
onReaderError = errorHandler;
errorLock.unlock();
}
void openFile(const char *fname, const Options &opt);
void closeFile();
USERET INLINE bool isFileOpen() const { return(reader != NULL); }
void getFilename(char **dest) const;
/*
* I could modify AudioFileReader to allow for latency, historySize, and preloadSize
* to be changed without restarting the dictation, but this function is only called
* when the user closes the options window, so I'll take the easy route and just close
* and re-open the file
*/
INLINE void setOptions(const Options &opt) {
readLock.lock();
if (fileName == NULL) {
readLock.unlock();
return;
}
const size_t fnl = std::strlen(fileName)+1;
char prevName[fnl];
std::memcpy(prevName, fileName, fnl);
unsigned bookmark = position;
bool wasPaused = paused;
readLock.unlock();
closeFile();
openFile(prevName, opt);
writeLock.lock();
readLock.lock();
position = bookmark;
paused = wasPaused;
readLock.unlock();
writeLock.unlock();
}
void setSlowSpeed(float v);
float increaseSlowSpeed(float dv); // increases/decreases slow speed and returns the new speed
void setPositionMilliseconds(unsigned ms);
void setPositionPercentage(double p);
void skipForward(int ms);
INLINE void skipBack(int ms) { skipForward(-ms); }
INLINE void play() {
writeLock.lock();
readLock.lock();
paused = false;
readLock.unlock();
writeLock.unlock();
pauseWait.notify_all();
}
INLINE void pause() {
writeLock.lock();
readLock.lock();
paused = true;
readLock.unlock();
writeLock.unlock();
}
INLINE void togglePlay() {
writeLock.lock();
readLock.lock();
paused = !paused;
readLock.unlock();
writeLock.unlock();
pauseWait.notify_all();
}
INLINE void slow() {
writeLock.lock();
readLock.lock();
slowed = true;
readLock.unlock();
writeLock.unlock();
}
INLINE void unslow() {
writeLock.lock();
readLock.lock();
slowed = false;
readLock.unlock();
writeLock.unlock();
}
INLINE void toggleSlow() {
writeLock.lock();
readLock.lock();
slowed = !slowed;
readLock.unlock();
writeLock.unlock();
}
INLINE void startRewind() {
writeLock.lock();
readLock.lock();
mode = REWIND;
readLock.unlock();
writeLock.unlock();
pauseWait.notify_all();
}
INLINE void stopRewind() {
writeLock.lock();
readLock.lock();
if (mode == REWIND) mode = NORMAL;
readLock.unlock();
writeLock.unlock();
}
INLINE void toggleRewind() {
writeLock.lock();
readLock.lock();
mode = (mode == REWIND) ? NORMAL : REWIND;
readLock.unlock();
writeLock.unlock();
pauseWait.notify_all();
}
INLINE void startFastForward() {
writeLock.lock();
readLock.lock();
mode = FAST_FORWARD;
readLock.unlock();
writeLock.unlock();
pauseWait.notify_all();
}
INLINE void stopFastForward() {
writeLock.lock();
readLock.lock();
if (mode == FAST_FORWARD) mode = NORMAL;
readLock.unlock();
writeLock.unlock();
}
INLINE void toggleFastForward() {
writeLock.lock();
readLock.lock();
mode = (mode == FAST_FORWARD) ? NORMAL : FAST_FORWARD;
readLock.unlock();
writeLock.unlock();
pauseWait.notify_all();
}
USERET INLINE bool isPaused() const {
std::unique_lock<std::mutex> rLock(readLock);
return paused;
}
USERET INLINE bool isSlowed() const {
std::unique_lock<std::mutex> rLock(readLock);
return slowed;
}
USERET INLINE bool isRewinding() const {
std::unique_lock<std::mutex> rLock(readLock);
return (mode == REWIND);
}
USERET INLINE bool isFastForwarding() const {
std::unique_lock<std::mutex> rLock(readLock);
return (mode == FAST_FORWARD);
}
USERET INLINE unsigned getPositionMilliseconds() const {
std::unique_lock<std::mutex> rLock(readLock);
if (reader == NULL) return 0;
return ((unsigned) ((double) 1000 * ((double) position / (double) (reader->getFileInfo().sampleRate * reader->getFileInfo().numChannels))));
}
USERET INLINE unsigned getLengthMilliseconds() const {
std::unique_lock<std::mutex> rLock(readLock);
if (reader == NULL) return 0;
return((unsigned)(1000.0 * ((double)reader->getFileInfo().numSamples / (double)(reader->getFileInfo().sampleRate * reader->getFileInfo().numChannels))));
}
USERET INLINE double getPositionPercentage() const {
std::unique_lock<std::mutex> rLock(readLock);
if (reader == NULL) return 0;
return (((double) position) / (double) reader->getFileInfo().numSamples);
}
};
#endif /* DICTATION_HPP_ */