diff --git a/synthetizer.cpp b/synthetizer.cpp index e840732..0215f8e 100644 --- a/synthetizer.cpp +++ b/synthetizer.cpp @@ -37,17 +37,21 @@ struct Wave { std::vector sampleData; }; -bool loadWavFile(const std::string& path, std::vector& outSamples) { +bool loadWavFile(const std::string& path, std::vector& outSamples, int& outDurationMs) { std::ifstream file(path, std::ios::binary); if (!file.is_open()) return false; - file.seekg(44); // skip header + file.seekg(44); // skip header (basic .wav, no format parsing) outSamples.clear(); int16_t sample; while (file.read(reinterpret_cast(&sample), sizeof(sample))) { outSamples.push_back(sample); } + + int sampleRate = 44100; // hardcoded for now + outDurationMs = static_cast((outSamples.size() * 1000.0) / sampleRate); + return true; } @@ -126,9 +130,8 @@ bool loadWaveParams(const std::string& filename, std::vector& waves, int& } int start_ms, duration_ms; - ss >> start_ms >> duration_ms; + ss >> start_ms; wave.start_ms = start_ms; - wave.duration_ms = duration_ms; if (wave.type == DPCM) { std::string token; @@ -160,14 +163,21 @@ bool loadWaveParams(const std::string& filename, std::vector& waves, int& wave.sweepDepth = 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; } + wave.duration_ms = actualLength; + wave.start_ms = start_ms; + waves.push_back(wave); continue; } + ss >> duration_ms; + wave.duration_ms = duration_ms; + // Standard wave parameters double volume = 1.0; double duty = 0.5; diff --git a/waves.txt b/waves.txt index ce9a135..d9cc3ba 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 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