Skip to content

Commit

Permalink
ChipTuneEngine.h:
Browse files Browse the repository at this point in the history
* Clearing variables in ChipTuneEngineParser before loading a new tune.
* Implementing the on song end listener callback.
  • Loading branch information
razterizer committed Nov 24, 2024
1 parent 6db4295 commit c6e6532
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
7 changes: 6 additions & 1 deletion ChipTuneEngine.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,25 @@
#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
class ChipTuneEngine : public ChipTuneEngineParser, public EventBroadcaster<ChipTuneEngineListener>
{
public:
ChipTuneEngine(AudioSourceHandler& audio_handler, const WaveformGeneration& waveform_gen)
Expand Down Expand Up @@ -267,6 +270,8 @@ namespace audio
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;
}

Expand Down
38 changes: 38 additions & 0 deletions ChipTuneEngine_Internals/ChipTuneEngineParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,39 @@ namespace audio

class ChipTuneEngineParser
{
void clear()
{
m_instruments_basic.clear();
m_instruments_ring_mod.clear();
m_instruments_conv.clear();
m_instruments_weight_avg.clear();
m_instruments_lib.clear();
m_envelopes.clear();
m_filter_args.clear();
m_waveform_params.clear();


m_time_step_ms.clear();
m_curr_time_step_ms = 100;

m_volume.clear();
m_curr_volume = 1.f;

m_labels.clear(); // note_idx -> Label (label, id)
m_gotos.clear(); // note_idx -> Goto (from_label, to_label, count)
m_al_fine = false;
m_al_coda = false;
m_to_coda = false;

m_print_switches.clear();

num_voices = 0;
m_voices.clear();
//std::vector<Instrument> m_instruments;
note_start_idx = 0;
num_notes_parsed = 0;
}

public:
ChipTuneEngineParser(AudioSourceHandler& audio_handler, const WaveformGeneration& waveform_gen)
: m_audio_handler(audio_handler)
Expand All @@ -35,6 +68,8 @@ namespace audio
// Load tune from a text file with a specific format
bool load_tune(const std::string& file_path, bool verbose = false)
{
clear();

if (!file_path.ends_with(".ct"))
{
std::cerr << "Wrong file ending in filepath argument. Expected *.ct" << std::endl;
Expand All @@ -47,6 +82,8 @@ namespace audio
std::cerr << "Error opening tune file: " << file_path << std::endl;
return false;
}

m_curr_file_path = file_path;

if (verbose)
std::cout << "Parsing Tune" << std::endl;
Expand Down Expand Up @@ -184,6 +221,7 @@ namespace audio

AudioSourceHandler& m_audio_handler;
const WaveformGeneration& m_waveform_gen;
std::string m_curr_file_path;
std::vector<InstrumentBasic> m_instruments_basic;
std::vector<InstrumentRingMod> m_instruments_ring_mod;
std::vector<InstrumentConv> m_instruments_conv;
Expand Down

0 comments on commit c6e6532

Please sign in to comment.