Skip to content

Commit c6e6532

Browse files
committed
ChipTuneEngine.h:
* Clearing variables in ChipTuneEngineParser before loading a new tune. * Implementing the on song end listener callback.
1 parent 6db4295 commit c6e6532

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

ChipTuneEngine.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,25 @@
77
#pragma once
88

99
#include "ChipTuneEngine_Internals/ChipTuneEngineParser.h"
10+
#include "ChipTuneEngineListener.h"
1011
#include "AudioSourceHandler.h"
1112
#include "Waveform.h"
1213
#include "WaveformGeneration.h"
1314

1415
#include <Core/Delay.h>
1516
#include <Core/StringHelper.h>
17+
#include <Core/events/EventBroadcaster.h>
1618

1719
#include <vector>
1820
#include <chrono>
1921
#include <thread>
2022
#include <atomic>
2123

24+
2225
namespace audio
2326
{
2427

25-
class ChipTuneEngine : public ChipTuneEngineParser
28+
class ChipTuneEngine : public ChipTuneEngineParser, public EventBroadcaster<ChipTuneEngineListener>
2629
{
2730
public:
2831
ChipTuneEngine(AudioSourceHandler& audio_handler, const WaveformGeneration& waveform_gen)
@@ -267,6 +270,8 @@ namespace audio
267270
do {}
268271
while (stlutils::contains_if(m_voices, [](const auto& voice) { return voice.src->is_playing(); }));
269272

273+
broadcast([this](auto* listener) { listener->on_tune_ended(this, m_curr_file_path); });
274+
270275
return true;
271276
}
272277

ChipTuneEngine_Internals/ChipTuneEngineParser.h

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,39 @@ namespace audio
2020

2121
class ChipTuneEngineParser
2222
{
23+
void clear()
24+
{
25+
m_instruments_basic.clear();
26+
m_instruments_ring_mod.clear();
27+
m_instruments_conv.clear();
28+
m_instruments_weight_avg.clear();
29+
m_instruments_lib.clear();
30+
m_envelopes.clear();
31+
m_filter_args.clear();
32+
m_waveform_params.clear();
33+
34+
35+
m_time_step_ms.clear();
36+
m_curr_time_step_ms = 100;
37+
38+
m_volume.clear();
39+
m_curr_volume = 1.f;
40+
41+
m_labels.clear(); // note_idx -> Label (label, id)
42+
m_gotos.clear(); // note_idx -> Goto (from_label, to_label, count)
43+
m_al_fine = false;
44+
m_al_coda = false;
45+
m_to_coda = false;
46+
47+
m_print_switches.clear();
48+
49+
num_voices = 0;
50+
m_voices.clear();
51+
//std::vector<Instrument> m_instruments;
52+
note_start_idx = 0;
53+
num_notes_parsed = 0;
54+
}
55+
2356
public:
2457
ChipTuneEngineParser(AudioSourceHandler& audio_handler, const WaveformGeneration& waveform_gen)
2558
: m_audio_handler(audio_handler)
@@ -35,6 +68,8 @@ namespace audio
3568
// Load tune from a text file with a specific format
3669
bool load_tune(const std::string& file_path, bool verbose = false)
3770
{
71+
clear();
72+
3873
if (!file_path.ends_with(".ct"))
3974
{
4075
std::cerr << "Wrong file ending in filepath argument. Expected *.ct" << std::endl;
@@ -47,6 +82,8 @@ namespace audio
4782
std::cerr << "Error opening tune file: " << file_path << std::endl;
4883
return false;
4984
}
85+
86+
m_curr_file_path = file_path;
5087

5188
if (verbose)
5289
std::cout << "Parsing Tune" << std::endl;
@@ -184,6 +221,7 @@ namespace audio
184221

185222
AudioSourceHandler& m_audio_handler;
186223
const WaveformGeneration& m_waveform_gen;
224+
std::string m_curr_file_path;
187225
std::vector<InstrumentBasic> m_instruments_basic;
188226
std::vector<InstrumentRingMod> m_instruments_ring_mod;
189227
std::vector<InstrumentConv> m_instruments_conv;

0 commit comments

Comments
 (0)