-
Notifications
You must be signed in to change notification settings - Fork 0
/
ChipTuneEngine.h
352 lines (308 loc) · 10.4 KB
/
ChipTuneEngine.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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
//
// ChipTuneEngine.h
// 8Beat
//
// Created by Rasmus Anthin on 2024-01-21.
//
#pragma once
#include "ChipTuneEngine_Internals/ChipTuneEngineParser.h"
#include "ChipTuneEngineListener.h"
#include "AudioSourceHandler.h"
#include "Waveform.h"
#include "WaveformGeneration.h"
#include <Core/Delay.h>
#include <Core/StringHelper.h>
#include <Core/events/EventBroadcaster.h>
#include <vector>
#include <chrono>
#include <thread>
#include <atomic>
namespace audio
{
class ChipTuneEngine : public ChipTuneEngineParser, public EventBroadcaster<ChipTuneEngineListener>
{
public:
ChipTuneEngine(AudioSourceHandler& audio_handler, const WaveformGeneration& waveform_gen)
: ChipTuneEngineParser(audio_handler, waveform_gen)
{}
~ChipTuneEngine() = default;
// Play the loaded tune
bool play_tune(bool interrupt_unfinished_note = true, bool verbose = false)
{
if (m_voices.empty())
return false; // No tune loaded.
if (verbose)
std::cout << "Playing Tune" << std::endl;
auto f_find_label = [this](const auto& label)
{
return std::find_if(m_labels.begin(), m_labels.end(), [&label](const auto& lp) { return lp.second->label == label; });
};
auto f_reset_volume = [this](int goto_note_idx)
{
for (const auto& vol_pair : m_volume)
if (vol_pair.first <= goto_note_idx)
m_curr_volume = vol_pair.second;
};
auto f_reset_speed = [this](int goto_note_idx)
{
for (const auto& ts_pair : m_time_step_ms)
if (ts_pair.first <= goto_note_idx)
m_curr_time_step_ms = ts_pair.second;
};
if (auto it_ts = m_time_step_ms.find(0); it_ts != m_time_step_ms.end())
m_curr_time_step_ms = it_ts->second;
m_curr_volume = 1.f;
if (auto it_v = m_volume.find(0); it_v != m_volume.end())
m_curr_volume = it_v->second;
Delay::sleep(static_cast<int>(1e6f)); // Warm-up. #FIXME: Find a better, more robust solution.
// ### Loop over voices ###
auto num_notes = static_cast<int>(m_voices[0].notes.size());
for (int note_idx = note_start_idx; note_idx < num_notes; ++note_idx)
{
if (m_stop_audio_thread)
break;
if (auto it_p = m_print_switches.find(note_idx); it_p != m_print_switches.end())
{
if (it_p->second)
enable_print_notes();
else
disable_print_notes();
}
// Branching.
if (auto it_g = m_gotos.find(note_idx); it_g != m_gotos.end())
{
auto& goto_data = *it_g->second.get();
const auto& from_label = goto_data.from_label;
const auto& to_label = goto_data.to_label;
auto& count = goto_data.count;
if (m_enable_print_notes)
{
if (from_label == "GOTO")
std::cout << from_label << " " << to_label << std::endl;
else if (from_label == "GOTO_TIMES")
std::cout << from_label << " " << to_label << " " << count << std::endl;
else if (count == -2)
std::cout << from_label << std::endl;
}
if (from_label == "DA_CAPO_AL_FINE")
{
m_al_fine = true;
note_idx = -1;
continue;
}
else if (from_label == "DA_CAPO_AL_CODA")
{
m_al_coda = true;
note_idx = -1;
continue;
}
else if (from_label == "DAL_SEGNO_AL_FINE")
{
if (auto it = f_find_label("SEGNO"); it != m_labels.end())
{
m_al_fine = true;
note_idx = it->first - 1;
f_reset_volume(note_idx);
f_reset_speed(note_idx);
continue;
}
}
else if (from_label == "DAL_SEGNO_AL_CODA")
{
if (auto it = f_find_label("SEGNO"); it != m_labels.end())
{
m_al_coda = true;
note_idx = it->first - 1;
f_reset_volume(note_idx);
f_reset_speed(note_idx);
continue;
}
}
else if (m_al_coda && from_label == "TO_CODA")
{
m_al_coda = false;
m_to_coda = true;
auto it_l = f_find_label("CODA");
if (it_l != m_labels.end())
{
note_idx = it_l->first - 1;
f_reset_volume(note_idx);
f_reset_speed(note_idx);
continue;
}
}
// Goto label.
if (count > 0 || count == -1)
{
if (count > 0)
count--;
auto it_l = f_find_label(to_label);
if (it_l != m_labels.end())
{
note_idx = it_l->first - 1;
continue;
}
}
if (count == 0)
goto_data.reset();
}
if (m_enable_print_notes)
{
for (auto it = m_labels.begin(); it != m_labels.end(); ++it)
if (it->first == note_idx)
{
if (it->second->label == "ENDING")
std::cout << it->second->label << " " << it->second->id << std::endl;
else if (it->second->id == 0)
std::cout << "LABEL " << it->second->label << std::endl;
else if (it->second->id == -2)
std::cout << it->second->label << std::endl;
}
}
// Special labels.
if (m_al_fine)
{
if (auto it = f_find_label("FINE"); it != m_labels.end() && it->first == note_idx)
{
m_al_fine = false;
break;
}
}
else if (m_to_coda)
{
if (auto it = f_find_label("CODA"); it != m_labels.end() && it->first == note_idx)
m_to_coda = false;
}
// N:th ending.
if (auto it = stlutils::find_if(m_labels, [note_idx](const auto& lp) { return lp.first == note_idx && lp.second->label == "ENDING"; });
it != m_labels.end())
{
auto* lbl = it->second.get();
if (lbl->src_goto != nullptr)
{
// If we are standing at an ENDING for the following repetition(s).
int curr_num_repeats = lbl->src_goto->num_jumps();
if (curr_num_repeats < lbl->id)
{
note_idx = lbl->src_goto->note_idx - 1;
continue;
}
else if (curr_num_repeats > lbl->id)
{
auto it_rlp = stlutils::find_if(lbl->related_labels, [curr_num_repeats](const auto& rlp)
{
return rlp.second->id == curr_num_repeats;
});
if (it_rlp != lbl->related_labels.end())
{
int rl_note_idx = it_rlp->first;
note_idx = rl_note_idx - 1;
continue;
}
}
}
}
// Volume.
if (auto it_v = m_volume.find(note_idx); it_v != m_volume.end())
m_curr_volume = it_v->second;
// The Melody.
bool is_separator = m_voices[0].notes[note_idx].get()->separator;
for (const auto& voice : m_voices)
{
auto* note = voice.notes[note_idx].get();
if (voice.src != nullptr)
{
if (!note->pause && (interrupt_unfinished_note || !voice.src->is_playing()))
{
if (interrupt_unfinished_note)
voice.src->stop();
if (m_ir_sound != nullptr)
{
auto wd_rev = WaveformHelper::reverb_fast(note->wave, *m_ir_sound);
voice.src->update_buffer(wd_rev);
}
else
voice.src->update_buffer(note->wave);
voice.src->set_volume(m_ext_volume * m_curr_volume * note->volume);
voice.src->play(PlaybackMode::NONE);
}
}
}
if (m_enable_print_notes)
std::cout << "Note Idx: " << std::to_string(note_idx) << std::endl;
// Tempo.
if (auto it_ts = m_time_step_ms.find(note_idx); it_ts != m_time_step_ms.end())
m_curr_time_step_ms = it_ts->second;
if (!is_separator)
Delay::sleep(static_cast<int>(m_curr_time_step_ms*1e3f));
do {}
while (m_pause);
}
// Cooldown.
do {}
while (stlutils::contains_if(m_voices, [](const auto& voice) { return voice.src->is_playing(); }));
broadcast([this](auto* listener) { listener->on_tune_ended(this, m_curr_file_path); });
return true;
}
// Play the loaded tune in a separate thread
void play_tune_async(bool interrupt_unfinished_note = true)
{
// Use std::thread and std::atomic_flag to safely start and stop the thread
m_stop_audio_thread = false;
m_audio_thread = std::thread([this, interrupt_unfinished_note] { play_tune(interrupt_unfinished_note); });
// Detach the audio thread, allowing it to run independently
m_audio_thread.detach();
}
// Stop the audio playback thread
void stop_tune_async()
{
m_stop_audio_thread = true;
// Optionally, you can join the thread here if you want to wait for it to finish
if (m_audio_thread.joinable())
m_audio_thread.join();
}
// Wait for the audio playback thread to finish
void wait_for_completion()
{
if (m_audio_thread.joinable())
m_audio_thread.join();
}
void enable_print_notes()
{
m_enable_print_notes = true;
}
void disable_print_notes()
{
m_enable_print_notes = false;
}
void pause()
{
m_pause = true;
}
void resume()
{
m_pause = false;
}
void set_volume(float vol)
{
m_ext_volume = vol;
}
// #WARNING: Super-slow!!!
void set_reverb_ir(const Waveform* ir)
{
m_ir_sound = ir;
}
void reset_reverb()
{
m_ir_sound = nullptr;
}
private:
std::thread m_audio_thread;
std::atomic<bool> m_stop_audio_thread = false;
std::atomic<bool> m_pause = false;
std::atomic<bool> m_enable_print_notes = false;
std::atomic<float> m_ext_volume = 1.f;
std::atomic<Waveform const *> m_ir_sound = nullptr;
std::atomic<bool> m_use_reverb = false;
};
}