add auto-release based on actual sample length and envelope applied during playback instead of after and instead of specifying the DPCM length just use the wav length

This commit is contained in:
Maksymilian Wilczyński committed 2025-04-08 20:04:34 +02:00
1 parent ac3f16155f
commit 2e1fc43cb4
2 files changed
+16 -6

No files matched your search

+15 -5
View File
@@ -37,17 +37,21 @@ struct Wave {
std::vector<int16_t> sampleData; std::vector<int16_t> sampleData;
}; };
bool loadWavFile(const std::string& path, std::vector<int16_t>& outSamples) { bool loadWavFile(const std::string& path, std::vector<int16_t>& outSamples, int& outDurationMs) {
std::ifstream file(path, std::ios::binary); std::ifstream file(path, std::ios::binary);
if (!file.is_open()) return false; if (!file.is_open()) return false;
file.seekg(44); // skip header file.seekg(44); // skip header (basic .wav, no format parsing)
outSamples.clear(); outSamples.clear();
int16_t sample; int16_t sample;
while (file.read(reinterpret_cast<char*>(&sample), sizeof(sample))) { while (file.read(reinterpret_cast<char*>(&sample), sizeof(sample))) {
outSamples.push_back(sample); outSamples.push_back(sample);
} }
int sampleRate = 44100; // hardcoded for now
outDurationMs = static_cast<int>((outSamples.size() * 1000.0) / sampleRate);
return true; return true;
} }
@@ -126,9 +130,8 @@ bool loadWaveParams(const std::string& filename, std::vector<Wave>& waves, int&
} }
int start_ms, duration_ms; int start_ms, duration_ms;
ss >> start_ms >> duration_ms; ss >> start_ms;
wave.start_ms = start_ms; wave.start_ms = start_ms;
wave.duration_ms = duration_ms;
if (wave.type == DPCM) { if (wave.type == DPCM) {
std::string token; std::string token;
@@ -160,14 +163,21 @@ bool loadWaveParams(const std::string& filename, std::vector<Wave>& waves, int&
wave.sweepDepth = 0.0; wave.sweepDepth = 0.0;
wave.sweepRate = 0.0; wave.sweepRate = 0.0;
if (!loadWavFile(wave.samplePath, wave.sampleData)) { int actualLength = 0;
if (!loadWavFile(wave.samplePath, wave.sampleData, actualLength)) {
std::cout << "Failed to load sample: " << wave.samplePath << std::endl; std::cout << "Failed to load sample: " << wave.samplePath << std::endl;
} }
wave.duration_ms = actualLength;
wave.start_ms = start_ms;
waves.push_back(wave); waves.push_back(wave);
continue; continue;
} }
ss >> duration_ms;
wave.duration_ms = duration_ms;
// Standard wave parameters // Standard wave parameters
double volume = 1.0; double volume = 1.0;
double duty = 0.5; double duty = 0.5;
+1 -1
View File
@@ -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 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 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 NOISE 3000 300 0 0.8 0 0 0 0.02 0.1 // noise burst
DPCM 3900 500 snare.wav 0.8 0.01 0.1 0.7 0.2 // with volume + ADSR DPCM 3900 snare.wav 0.8 0.01 0.1 0.7 0.2 // with volume + ADSR