diff --git a/synthetizer.cpp b/synthetizer.cpp index d943310..b5ac2c9 100644 --- a/synthetizer.cpp +++ b/synthetizer.cpp @@ -128,10 +128,10 @@ std::vector generateAudio(const std::vector& waves, int totalDura // Generate the waveform for the specified duration for (int t = startSample; t < endSample; ++t) { + if (t >= samples.size()) break; // Prevent out-of-bounds access double time = static_cast(t) / SAMPLE_RATE; double sample = generateWave(wave.type, wave.frequency, time); - sample *= wave.volume; // Apply the volume to the waveform - + sample *= wave.volume; // Apply volume samples[t] += static_cast(sample * 32767); // Linear mixing } } @@ -193,8 +193,8 @@ int main() { std::vector samples = generateAudio(waves, totalDuration); // Write the audio to a WAV file - writeWav("output_mixed_with_volume.wav", samples); + writeWav("output.wav", samples); - std::cout << "WAV file generated: output_mixed_with_volume.wav" << std::endl; + std::cout << "WAV file generated: output.wav" << std::endl; return 0; } diff --git a/waves.txt b/waves.txt index 9e8b21e..a8ce288 100644 --- a/waves.txt +++ b/waves.txt @@ -1,3 +1,61 @@ -TOTAL_DURATION(5000) // Total duration in milliseconds -SINE 0 1000 440 1.0 // Sine wave starts at 0 ms, lasts for 1000 ms, frequency 440 Hz, full volume -SQUARE 0 1000 440 0.5 // Square wave starts at 0 ms, lasts for 1000 ms, frequency 440 Hz, 50% volume +TOTAL_DURATION(10000) + +SQUARE 0 400 392 0.7 +SAWTOOTH 0 400 392 0.4 +SINE 0 400 392 0.3 +TRIANGLE 0 400 392 0.2 + +SQUARE 450 400 392 0.7 +SAWTOOTH 450 400 392 0.4 +SINE 450 400 392 0.3 +TRIANGLE 450 400 392 0.2 + +SQUARE 900 800 440 0.7 +SAWTOOTH 900 800 440 0.4 +SINE 900 800 440 0.3 +TRIANGLE 900 800 440 0.2 + +SQUARE 1800 800 392 0.7 +SAWTOOTH 1800 800 392 0.4 +SINE 1800 800 392 0.3 +TRIANGLE 1800 800 392 0.2 + +SQUARE 2700 800 523 0.7 +SAWTOOTH 2700 800 523 0.4 +SINE 2700 800 523 0.3 +TRIANGLE 2700 800 523 0.2 + +SQUARE 3600 1600 494 0.7 +SAWTOOTH 3600 1600 494 0.4 +SINE 3600 1600 494 0.3 +TRIANGLE 3600 1600 494 0.2 + +SQUARE 5400 400 392 0.7 +SAWTOOTH 5400 400 392 0.4 +SINE 5400 400 392 0.3 +TRIANGLE 5400 400 392 0.2 + +SQUARE 5850 400 392 0.7 +SAWTOOTH 5850 400 392 0.4 +SINE 5850 400 392 0.3 +TRIANGLE 5850 400 392 0.2 + +SQUARE 6300 800 440 0.7 +SAWTOOTH 6300 800 440 0.4 +SINE 6300 800 440 0.3 +TRIANGLE 6300 800 440 0.2 + +SQUARE 7200 800 392 0.7 +SAWTOOTH 7200 800 392 0.4 +SINE 7200 800 392 0.3 +TRIANGLE 7200 800 392 0.2 + +SQUARE 8100 800 587 0.7 +SAWTOOTH 8100 800 587 0.4 +SINE 8100 800 587 0.3 +TRIANGLE 8100 800 587 0.2 + +SQUARE 9000 1600 523 0.7 +SAWTOOTH 9000 1600 523 0.4 +SINE 9000 1600 523 0.3 +TRIANGLE 9000 1600 523 0.2