From 321a0cd118368be266664f91d13c9a475484d672 Mon Sep 17 00:00:00 2001 From: Borin Ouch Date: Thu, 25 Apr 2024 09:17:03 -0700 Subject: [PATCH] Plot generated samples --- src/sound.cpp | 14 ++++++++------ src/sound.h | 1 + 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/src/sound.cpp b/src/sound.cpp index c353552..3576de2 100644 --- a/src/sound.cpp +++ b/src/sound.cpp @@ -1,6 +1,7 @@ #include "sound.h" #include +#include #include #include #include @@ -47,11 +48,6 @@ void WaveGeneratorSource::render() { } ImGui::EndCombo(); } - - std::array samples; - for (int n = 0; n < 100; n += 1) { - samples[n] = sinf(n * 0.2f + ImGui::GetTime() * 5.f); - } ImGui::PlotLines("Sound", samples.data(), samples.size(), 0, nullptr, -1.5f, 1.5f, ImVec2(0, 80.0f)); ImGui::SliderFloat("Volume", &volume, 0.0f, 100.0f); ImGui::SliderFloat("Frequency", &frequency, 10.0f, 2048.0f); @@ -88,6 +84,7 @@ static inline float sawtooth_wave(float idx) { void WaveGeneratorSource::gen_sound_data(bool play_sound, short buffer[], size_t buffer_size) { if (!play_sound && !force_play) { memset(buffer, 0, sizeof(short) * buffer_size); + memset(&samples, 0, sizeof(float) * samples.size()); return; } @@ -104,7 +101,12 @@ void WaveGeneratorSource::gen_sound_data(bool play_sound, short buffer[], size_t })(); for (int i = 0; i < buffer_size; i++) { - buffer[i] = static_cast(((1 << 15) - 1) * wave_func(wave_idx) * (volume / 100.0f)); + float sample = wave_func(wave_idx) * (volume / 100.0f); + buffer[i] = static_cast(((1 << 15) - 1) * sample); + + if (i < samples.size()) { + samples[i] = sample; + } wave_idx += incr; if (wave_idx > 1.0f) diff --git a/src/sound.h b/src/sound.h index 7803387..4c86039 100644 --- a/src/sound.h +++ b/src/sound.h @@ -28,6 +28,7 @@ class WaveGeneratorSource : public SoundSource { virtual void gen_sound_data(bool play_sound, short buffer[], size_t buffer_size) override; private: + std::array samples; bool force_play = false; float frequency = 440.0f; float volume = 100.0f;