From ac3f16155fb4d0b943367b8039efedafec46f0fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maksymilian=20Wilczy=C5=84ski?= Date: Tue, 8 Apr 2025 08:15:30 +0200 Subject: [PATCH] ADSR for DPCM --- synthetizer.cpp | 22 +++++++++++++++------- waves.txt | 2 +- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/synthetizer.cpp b/synthetizer.cpp index e89c446..e840732 100644 --- a/synthetizer.cpp +++ b/synthetizer.cpp @@ -31,6 +31,8 @@ struct Wave { double sweepRate; double attack; double decay; + double sustain = 1.0; + double release = 0.0; std::string samplePath; std::vector sampleData; }; @@ -130,6 +132,8 @@ bool loadWaveParams(const std::string& filename, std::vector& waves, int& if (wave.type == DPCM) { std::string token; + + // Skip any comment block while (ss >> token && token == "/*") { while (ss >> token && token != "*/"); } @@ -139,18 +143,22 @@ bool loadWaveParams(const std::string& filename, std::vector& waves, int& continue; } - // Optional volume - if (!(ss >> wave.volume)) { - wave.volume = 1.0; - } - wave.samplePath = token; + + // Optional volume + if (!(ss >> wave.volume)) wave.volume = 1.0; + + // Optional ADSR envelope for DPCM + if (!(ss >> wave.attack)) wave.attack = 0.0; + if (!(ss >> wave.decay)) wave.decay = 0.0; + if (!(ss >> wave.sustain)) wave.sustain = 1.0; + if (!(ss >> wave.release)) wave.release = 0.0; + + // Not used for DPCM but set anyway wave.frequency = 0; wave.dutyCycle = 0.5; wave.sweepDepth = 0.0; wave.sweepRate = 0.0; - wave.attack = 0.01; - wave.decay = 0.2; if (!loadWavFile(wave.samplePath, wave.sampleData)) { std::cout << "Failed to load sample: " << wave.samplePath << std::endl; diff --git a/waves.txt b/waves.txt index 3e431bd..ce9a135 100644 --- a/waves.txt +++ b/waves.txt @@ -4,4 +4,4 @@ SINE 0 500 440 1.0 0 3 5 0.05 0.3 // sine with slow vibrato and long decay TRIANGLE 1000 500 550 0.9 0.5 2 3 0.02 0.25 // triangle with sweep SQUARE 2000 500 330 0.7 0.25 6 4 0.01 0.2 // square with duty cycle and sweep NOISE 3000 300 0 0.8 0 0 0 0.02 0.1 // noise burst -DPCM 3900 500 snare.wav 1.0 // dpcm sample 1.0 volume +DPCM 3900 500 snare.wav 0.8 0.01 0.1 0.7 0.2 // with volume + ADSR