Skip to content

Commit

Permalink
Implement multiple sound sources
Browse files Browse the repository at this point in the history
  • Loading branch information
aceiii committed Apr 25, 2024
1 parent 35d869e commit 63a26e6
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 41 deletions.
66 changes: 30 additions & 36 deletions src/interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const char* const kSettingsFile = "settings.toml";
static bool should_close = false;
static bool init_dock = true;
static bool rom_loaded = false;
static float sine_idx = 0.0f;
static bool force_play_all_sounds = false;

static const char* const sound_source_names[] = {
"Waveform Generator",
Expand Down Expand Up @@ -149,8 +149,7 @@ void Interface::initialize() {
int monitor_height = GetMonitorHeight(monitor);
spdlog::trace("Monitor resolution: {}x{}", monitor_width, monitor_height);

sound_source = std::make_unique<WaveGeneratorSource>();
sound_source->initialize();
sounds.push_back(std::make_unique<WaveGeneratorSource>());

if (settings.lock_fps) {
SetTargetFPS(kDefaultFPS);
Expand Down Expand Up @@ -227,8 +226,9 @@ bool Interface::update() {

keyboard.update();

if (sound_source) {
sound_source->update(regs->st > 0);
bool is_playing = regs->st > 0;
for (auto &sound : sounds) {
sound->update(is_playing || force_play_all_sounds);
}

screen.update();
Expand Down Expand Up @@ -530,38 +530,36 @@ bool Interface::update() {
SetMasterVolume(settings.volume / 100.0f);
}

ImGui::Separator();
ImGui::Checkbox("Force Play All Sounds", &force_play_all_sounds);

if (ImGui::BeginCombo("Source", sound_source_names[current_source])) {
for (int n = 0; n < IM_ARRAYSIZE(sound_source_names); n += 1) {
bool is_selected = current_source == n;
if (ImGui::Selectable(sound_source_names[n], is_selected) && n != current_source) {
current_source = n;

if (sound_source) {
sound_source->cleanup();
}

if (current_source == 0) {
sound_source = std::make_unique<WaveGeneratorSource>();
sound_source->initialize();
} else {
sound_source = nullptr;
}
}
if (is_selected) {
ImGui::SetItemDefaultFocus();
}
ImGui::NewLine();

int sid_to_remove = -1;
for (int sid = 0; sid < sounds.size(); sid += 1) {
ImGui::PushID(sid);
ImGui::BeginChild("##Sound", ImVec2(0, 0), ImGuiChildFlags_Border | ImGuiChildFlags_AutoResizeY);
ImGui::Text("Sound #%d", (sid + 1));

sounds[sid]->render();

if (ImGui::Button("Remove")) {
sid_to_remove = sid;
}
ImGui::EndCombo();
}

ImGui::Separator();
ImGui::EndChild();
ImGui::PopID();
}

if (sound_source) {
sound_source->render();
if (sid_to_remove != -1) {
sounds.erase(sounds.begin() + sid_to_remove);
}
}

ImGui::NewLine();
if (ImGui::Button("Add sound")) {
sounds.push_back(std::make_unique<WaveGeneratorSource>());
}

ImGui::End();
}

Expand Down Expand Up @@ -704,11 +702,7 @@ void Interface::cleanup() {
settings.window_height = GetScreenHeight();

spdlog::info("Cleaning up interface");

if (sound_source) {
sound_source->cleanup();
}

sounds.clear();
rlImGuiShutdown();
CloseAudioDevice();
CloseWindow();
Expand Down
2 changes: 1 addition & 1 deletion src/interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,5 +78,5 @@ class Interface {
Config<interface_settings> config;
std::vector<uint8_t> rom;
std::shared_ptr<registers> regs;
std::unique_ptr<SoundSource> sound_source;
std::vector<std::unique_ptr<SoundSource>> sounds;
};
6 changes: 4 additions & 2 deletions src/sound.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@
#include <imgui.h>
#include <random>

void SoundSource::initialize() {
SoundSource::SoundSource() {
spdlog::trace("Initializing SoundSource");
SetAudioStreamBufferSizeDefault(kMaxSamplesPerUpdate);
stream = LoadAudioStream(kAudioSampleRate, kAudioSampleSize, kAudioNumChannels);
PlayAudioStream(stream);
}

void SoundSource::cleanup() {
SoundSource::~SoundSource() {
spdlog::trace("Destructing SoundSource");
StopAudioStream(stream);
UnloadAudioStream(stream);
}
Expand Down
7 changes: 5 additions & 2 deletions src/sound.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@ constexpr int kAudioNumChannels = 1;

class SoundSource {
public:
SoundSource();
virtual ~SoundSource();

virtual void render() = 0;
virtual void gen_sound_data(bool play_sound, short buffer[], size_t buffer_size) = 0;

virtual void initialize();
void update(bool play_sound);
virtual void cleanup();

private:
AudioStream stream;
Expand All @@ -24,6 +25,8 @@ class SoundSource {

class WaveGeneratorSource : public SoundSource {
public:
virtual ~WaveGeneratorSource() = default;

virtual void render() override;
virtual void gen_sound_data(bool play_sound, short buffer[], size_t buffer_size) override;

Expand Down

0 comments on commit 63a26e6

Please sign in to comment.