-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathswe_wave_engine.cpp
More file actions
251 lines (205 loc) · 5.76 KB
/
swe_wave_engine.cpp
File metadata and controls
251 lines (205 loc) · 5.76 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
#include "swe_wave_engine.h"
namespace olc::sound
{
///[OLC_HM] START WAVE_ENGINE_CPP
WaveEngine::WaveEngine()
{
m_sInputDevice = "NONE";
m_sOutputDevice = "DEFAULT";
#if defined(SOUNDWAVE_USING_WINMM)
m_driver = std::make_unique<driver::WinMM>(this);
#endif
#if defined(SOUNDWAVE_USING_WASAPI)
m_driver = std::make_unique<driver::WASAPI>(this);
#endif
#if defined(SOUNDWAVE_USING_XAUDIO)
m_driver = std::make_unique<driver::XAudio>(this);
#endif
#if defined(SOUNDWAVE_USING_OPENAL)
m_driver = std::make_unique<driver::OpenAL>(this);
#endif
#if defined(SOUNDWAVE_USING_ALSA)
m_driver = std::make_unique<driver::ALSA>(this);
#endif
#if defined(SOUNDWAVE_USING_SDLMIXER)
m_driver = std::make_unique<driver::SDLMixer>(this);
#endif
#if defined(SOUNDWAVE_USING_PULSE)
m_driver = std::make_unique<driver::PulseAudio>(this);
#endif
}
WaveEngine::~WaveEngine()
{
DestroyAudio();
}
std::vector<std::string> WaveEngine::GetOutputDevices()
{
return { "XXX" };
}
void WaveEngine::UseOutputDevice(const std::string& sDeviceOut)
{
m_sOutputDevice = sDeviceOut;
}
std::vector<std::string> WaveEngine::GetInputDevices()
{
return { "XXX" };
}
void WaveEngine::UseInputDevice(const std::string& sDeviceIn)
{
m_sInputDevice = sDeviceIn;
}
bool WaveEngine::InitialiseAudio(uint32_t nSampleRate, uint32_t nChannels, uint32_t nBlocks, uint32_t nBlockSamples)
{
m_nSampleRate = nSampleRate;
m_nChannels = nChannels;
m_nBlocks = nBlocks;
m_nBlockSamples = nBlockSamples;
m_dSamplePerTime = double(nSampleRate);
m_dTimePerSample = 1.0 / double(nSampleRate);
m_driver->Open(m_sOutputDevice, m_sInputDevice);
m_driver->Start();
return false;
}
bool WaveEngine::DestroyAudio()
{
StopAll();
m_driver->Stop();
m_driver->Close();
return false;
}
void WaveEngine::SetCallBack_NewSample(std::function<void(double)> func)
{
m_funcNewSample = func;
}
void WaveEngine::SetCallBack_SynthFunction(std::function<float(uint32_t, double)> func)
{
m_funcUserSynth = func;
}
void WaveEngine::SetCallBack_FilterFunction(std::function<float(uint32_t, double, float)> func)
{
m_funcUserFilter = func;
}
PlayingWave WaveEngine::PlayWaveform(Wave* pWave, bool bLoop, double dSpeed)
{
WaveInstance wi;
wi.bLoop = bLoop;
wi.pWave = pWave;
wi.dSpeedModifier = dSpeed * double(pWave->file.samplerate()) / m_dSamplePerTime;
wi.dDuration = pWave->file.duration() / dSpeed;
wi.dInstanceTime = m_dGlobalTime;
m_listWaves.push_back(wi);
return std::prev(m_listWaves.end());
}
void WaveEngine::PauseWaveform(const PlayingWave& w) {
w->bPaused = true;
}
void WaveEngine::ResumeWaveform(const PlayingWave& w) {
w->bPaused = false;
}
void WaveEngine::RewindWaveform(const PlayingWave& w) {
w->dInstanceTime = m_dGlobalTime;
}
void WaveEngine::StopWaveform(const PlayingWave& w)
{
w->bFlagForStop = true;
}
void WaveEngine::StopAll()
{
for (auto& wave : m_listWaves)
{
wave.bFlagForStop = true;
}
}
void WaveEngine::SetOutputVolume(const float fVolume)
{
m_fOutputVolume = std::clamp(fVolume, 0.0f, 1.0f);
}
uint32_t WaveEngine::FillOutputBuffer(std::vector<float>& vBuffer, const uint32_t nBufferOffset, const uint32_t nRequiredSamples)
{
for (uint32_t nSample = 0; nSample < nRequiredSamples; nSample++)
{
double dSampleTime = m_dGlobalTime + nSample * m_dTimePerSample;
if (m_funcNewSample)
m_funcNewSample(dSampleTime);
for (uint32_t nChannel = 0; nChannel < m_nChannels; nChannel++)
{
// Construct the sample
float fSample = 0.0f;
// 1) Sample any active waves
for (auto& wave : m_listWaves)
{
// Is wave instance flagged for stopping?
if (wave.bFlagForStop)
{
wave.bFinished = true;
}
else
{
// Calculate offset into wave instance
double dTimeOffset = dSampleTime - wave.dInstanceTime;
// If offset is larger than wave then...
if (dTimeOffset >= wave.dDuration)
{
if (wave.bLoop)
{
// ...if looping, reset the wave instance
wave.dInstanceTime = dSampleTime;
}
else
{
// ...if not looping, flag wave instance as dead
wave.bFinished = true;
}
}
else
{
// OR, if waveform is not paused, then sample the waveform from the correct channel
if (!wave.bPaused) {
fSample += float(wave.pWave->vChannelView[nChannel % wave.pWave->file.channels()].GetSample(dTimeOffset * m_dSamplePerTime * wave.dSpeedModifier));
}
else {
// Account for paused waiting time.
wave.dInstanceTime += m_dTimePerSample;
}
}
}
}
// Remove waveform instances that have finished
m_listWaves.remove_if([](const WaveInstance& wi) {return wi.bFinished; });
// 2) If user is synthesizing, request sample
if (m_funcUserSynth)
fSample += m_funcUserSynth(nChannel, dSampleTime);
// 3) Apply global filters
// 4) If user is filtering, allow manipulation of output
if (m_funcUserFilter)
fSample = m_funcUserFilter(nChannel, dSampleTime, fSample);
// Place sample in buffer
vBuffer[nBufferOffset + nSample * m_nChannels + nChannel] = fSample * m_fOutputVolume;
}
}
// UPdate global time, accounting for error (thanks scripticuk)
m_dGlobalTime += nRequiredSamples * m_dTimePerSample;
return nRequiredSamples;
}
uint32_t WaveEngine::GetSampleRate() const
{
return m_nSampleRate;
}
uint32_t WaveEngine::GetChannels() const
{
return m_nChannels;
}
uint32_t WaveEngine::GetBlocks() const
{
return m_nBlocks;
}
uint32_t WaveEngine::GetBlockSampleCount() const
{
return m_nBlockSamples;
}
double WaveEngine::GetTimePerSample() const
{
return m_dTimePerSample;
}
///[OLC_HM] END WAVE_ENGINE_CPP
} // SoundWave Interface Implementation - Driver agnostic