-
Notifications
You must be signed in to change notification settings - Fork 4
/
libjukebox.h
302 lines (250 loc) · 7.7 KB
/
libjukebox.h
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/*
Copyright 2017 Roberto Panerai Velloso.
This file is part of libjukebox.
libjukebox 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.
libjukebox 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 libjukebox. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef _LIBJUKEBOX_H
#define _LIBJUKEBOX_H
#include <string>
#include <memory>
#include <istream>
#include <functional>
#include <map>
#include <mutex>
#include <vector>
namespace jukebox {
class MixerImpl {
public:
virtual ~MixerImpl() = default;
virtual int getVolume() = 0;
virtual void setVolume(int vol) = 0;
};
}
namespace jukebox {
class Mixer {
public:
Mixer();
int getVolume();
void setVolume(int vol);
private:
MixerImpl &impl;
};
}
namespace jukebox {
class SoundFileImpl;
class DecoderImpl {
public:
DecoderImpl(SoundFileImpl &fileImpl);
virtual ~DecoderImpl() = default;
virtual int getSamples(char *buf, int pos, int len) = 0;
virtual int getBlockSize() const;
virtual short getNumChannels() const;
virtual int getSampleRate() const;
virtual short getBitsPerSample() const;
virtual int getDataSize() const;
virtual int silenceLevel() const;
SoundFileImpl &getFileImpl() const;
virtual DecoderImpl *peel();
protected:
SoundFileImpl &fileImpl;
int blockSize;
};
}
namespace jukebox {
class SoundFileImpl;
class SoundFile;
class Decoder {
public:
Decoder(SoundFile soundFile);
Decoder(Decoder &&) = default;
Decoder &operator=(Decoder &&) = default;
int getSamples(char *buf, int pos, int len);
short getNumChannels() const;
int getSampleRate() const;
short getBitsPerSample() const;
int getDataSize() const;
int getBlockSize() const;
const std::string &getFilename() const;
double getDuration() const;
int silenceLevel() const;
Decoder *prototype();
template<typename T, typename ...Params>
Decoder &wrap(Params&&... params) {
impl.reset(new T(impl.release(), std::forward<Params>(params)...));
return *this;
}
Decoder &peel();
private:
Decoder(std::shared_ptr<SoundFileImpl> soundFileImpl);
Decoder(const Decoder &) = delete;
Decoder &operator=(const Decoder &) = delete;
std::shared_ptr<SoundFileImpl> soundFileImpl;
std::unique_ptr<DecoderImpl> impl;
};
}
namespace jukebox {
class MIDIConfigurator {
public:
MIDIConfigurator(MIDIConfigurator &) = delete;
void operator =(MIDIConfigurator &) = delete;
const std::string &getSoundFont() const;
void setSoundFont(const std::string &sfPath);
static MIDIConfigurator &getInstance();
private:
std::string soundFontPath;
static std::unique_ptr<MIDIConfigurator> instance;
MIDIConfigurator();
};
}
namespace jukebox {
extern size_t dr_libs_read_callback(void *stream, void *outBuf, size_t len);
extern uint32_t dr_libs_seek_callback(void *stream, int offset, int origin);
class SoundFileImpl {
public:
SoundFileImpl() = default;
virtual ~SoundFileImpl() = default;
virtual short getNumChannels() const = 0;
virtual int getSampleRate() const = 0;
virtual short getBitsPerSample() const = 0;
virtual const std::string &getFilename() const = 0;
virtual DecoderImpl *makeDecoder() = 0;
virtual int silenceLevel() const;
virtual void truncAt(int pos);
virtual int getDataSize() const;
protected:
int dataSize = 0;
};
}
namespace jukebox {
class Decoder;
class SoundFile {
friend class Decoder;
public:
SoundFile(SoundFileImpl *impl);
short getNumChannels() const;
int getSampleRate() const;
short getBitsPerSample() const;
int getDataSize() const;
const std::string &getFilename() const;
double getDuration() const;
void truncAt(int pos);
DecoderImpl *makeDecoder();
int silenceLevel() const;
private:
std::shared_ptr<SoundFileImpl> impl;
};
}
namespace jukebox {
class SoundImpl {
public:
SoundImpl(Decoder *);
virtual ~SoundImpl() = default;
virtual void play() = 0;
virtual void stop() = 0;
virtual void pause() = 0;
virtual int getVolume() const = 0;
virtual void setVolume(int) = 0;
virtual void loop(bool) = 0;
virtual bool playing() const = 0;
virtual int getPosition() const;
virtual void setPosition(int pos);
virtual void pushOnStopCallback(std::function<void(void)>);
virtual std::function<void(void)> popOnStopCallback();
virtual void clearOnStopStack();
virtual bool onStopStackEmpty();
virtual void addTimedEventCallback(size_t seconds, std::function<void(void)>);
virtual Decoder &getDecoder();
void processTimedEvents();
protected:
int position = 0;
std::unique_ptr<Decoder> decoder;
std::vector<std::function<void (void)>> onStopStack;
std::recursive_mutex timedEventsMutex;
std::map<size_t, std::function<void (void)> > timedEvents;
};
}
namespace jukebox {
namespace factory {
class SoundBuilder;
}
class Sound {
public:
Sound(SoundImpl *impl);
Sound(Sound &&) = default;
Sound &operator=(Sound &&) = default;
Sound &play();
Sound &restart();
Sound &pause();
Sound &stop();
int getVolume() const;
int getPosition() const;
bool playing() const;
Sound &setPosition(int pos);
Sound &pushOnStopCallback(std::function<void(void)>);
std::function<void(void)> popOnStopCallback();
Sound &clearOnStopStack();
bool onStopStackEmpty();
Sound &addTimedEventCallback(size_t seconds, std::function<void(void)>);
Sound &reverb(float delay, float decay, size_t numDelays);
Sound &distortion(float gain);
Sound &fade(int fadeInSecs, int fadeOutSecs);
Sound &resolution(int bitsPerSample);
Sound &fadeOnStop(int fadeOutSecs);
Sound &setVolume(int);
Sound &loop(bool);
Sound &jointStereo();
Sound &movingAverage(float len);
Sound &peelDecoder();
Sound prototype();
short getNumChannels() const;
int getSampleRate() const;
short getBitsPerSample() const;
int getDataSize() const;
const std::string &getFilename() const;
double getDuration() const;
private:
Sound(const Sound &) = delete;
Sound &operator=(const Sound &) = delete;
std::unique_ptr<SoundImpl> impl;
bool looping = false;
};
}
namespace jukebox {
namespace factory {
Sound makeSound(SoundFile &file);
Sound makeSound(const std::string &filename, bool onMemory = false);
Sound makeSound(std::istream &inp, const std::string &filename, bool onMemory = false);
Sound makeSoundOutputToFile(SoundFile &file, const std::string &filename);
Sound makeSoundOutputToFile(const std::string &inputFile, const std::string &filename, bool onMemory = false);
SoundImpl *makeSoundImpl(Decoder *decoder);
SoundFile loadFile(const std::string &filename, bool onMemory = false);
SoundFile loadFromStream(std::istream &inp, const std::string &filename, bool onMemory = false);
SoundFile loadWaveFile(const std::string &filename, bool onMemory = false);
SoundFile loadWaveStream(std::istream &inp, bool onMemory = false);
SoundFile loadVorbisFile(const std::string &filename, bool onMemory = false);
SoundFile loadVorbisStream(std::istream &inp, bool onMemory = false);
SoundFile loadMP3File(const std::string &filename, bool onMemory = false);
SoundFile loadMP3Stream(std::istream &inp, bool onMemory = false);
SoundFile loadFLACFile(const std::string &filename, bool onMemory = false);
SoundFile loadFLACStream(std::istream &inp, bool onMemory = false);
SoundFile loadMIDIFile(const std::string &filename);
SoundFile loadMIDIStream(std::istream &inp);
SoundFile loadModFile(const std::string &filename);
SoundFile loadModStream(std::istream &inp);
}
}
namespace jukebox {
namespace factory {
extern MixerImpl &makeMixerImpl();
}
}
#endif // #define _LIBJUKEBOX_H