#include #include const int SAMPLE_RATE = 44100; const double PI = 3.141592653589793; void writeWavHeader(std::ofstream& file, int dataSize) { file.write("RIFF", 4); int chunkSize = 36 + dataSize; file.write((char*)&chunkSize, 4); file.write("WAVE", 4); file.write("fmt ", 4); int subChunk1Size = 16; short audioFormat = 1; short numChannels = 1; int byteRate = SAMPLE_RATE * 2; short blockAlign = 2; short bitsPerSample = 16; file.write((char*)&subChunk1Size, 4); file.write((char*)&audioFormat, 2); file.write((char*)&numChannels, 2); file.write((char*)&SAMPLE_RATE, 4); file.write((char*)&byteRate, 4); file.write((char*)&blockAlign, 2); file.write((char*)&bitsPerSample, 2); file.write("data", 4); file.write((char*)&dataSize, 4); } void playNote(std::ofstream& file, double freq, double seconds) { int samples = seconds * SAMPLE_RATE; for (int i = 0; i < samples; i++) { double t = (double)i / SAMPLE_RATE; short sample = (short)(30000 * sin(2 * PI * freq * t)); file.write((char*)&sample, 2); } } int main() { std::ofstream file("jingle_bells.wav", std::ios::binary); int totalSamples = SAMPLE_RATE * 4; // approx length writeWavHeader(file, totalSamples * 2); // Jingle Bells (simplified melody) playNote(file, 659.25, 0.4); // E playNote(file, 659.25, 0.4); playNote(file, 659.25, 0.6); playNote(file, 659.25, 0.4); playNote(file, 659.25, 0.4); playNote(file, 659.25, 0.6); playNote(file, 659.25, 0.4); playNote(file, 783.99, 0.4); // G playNote(file, 523.25, 0.4); // C playNote(file, 587.33, 0.4); // D playNote(file, 659.25, 0.8); // E file.close(); return EXIT_SUCCESS; }