Skip to content

Commit

Permalink
Plot generated samples
Browse files Browse the repository at this point in the history
  • Loading branch information
aceiii committed Apr 25, 2024
1 parent a4ebed9 commit 321a0cd
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
14 changes: 8 additions & 6 deletions src/sound.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "sound.h"

#include <cmath>
#include <cstring>
#include <spdlog/spdlog.h>
#include <raylib.h>
#include <imgui.h>
Expand Down Expand Up @@ -47,11 +48,6 @@ void WaveGeneratorSource::render() {
}
ImGui::EndCombo();
}

std::array<float, 100> 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);
Expand Down Expand Up @@ -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;
}

Expand All @@ -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<short>(((1 << 15) - 1) * wave_func(wave_idx) * (volume / 100.0f));
float sample = wave_func(wave_idx) * (volume / 100.0f);
buffer[i] = static_cast<short>(((1 << 15) - 1) * sample);

if (i < samples.size()) {
samples[i] = sample;
}

wave_idx += incr;
if (wave_idx > 1.0f)
Expand Down
1 change: 1 addition & 0 deletions src/sound.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<float, 1000> samples;
bool force_play = false;
float frequency = 440.0f;
float volume = 100.0f;
Expand Down

0 comments on commit 321a0cd

Please sign in to comment.