Task

Output the 8 notes of the C major diatonic scale to the default musical sound device on the system. Specifically, pitch must be tuned to 12-tone equal temperament (12TET) with the modern standard A=440Hz.

These are the notes "C, D, E, F, G, A, B, C(1 octave higher)", or "Do, Re, Mi, Fa, Sol, La, Si/Ti, Do(1 octave higher)" on Fixed do Solfège.

For the purpose of this task, Middle C (in the case of the above tuning, around 261.63 Hz) should be used as the starting note, and any note duration is allowed.

For languages that cannot utilize a sound device, it is permissible to output to a musical score sheet (or midi file), or the task can be omitted.

var sampleRate = 44100.0;
var duration = 8.0;
var dataLength = round(sampleRate * duration);
var dataLength_b0 = dataLength >> 0  & 0xff;
var dataLength_b1 = dataLength >> 8  & 0xff;
var dataLength_b2 = dataLength >> 16 & 0xff;
var dataLength_b3 = dataLength >> 24 & 0xff;
 
const adjustedHdrSize = 36;
 
var len = dataLength - adjustedHdrSize;
var len_b0 = len >> 0  & 0xff;
var len_b1 = len >> 8  & 0xff;
var len_b2 = len >> 16 & 0xff;
var len_b3 = len >> 24 & 0xff;
 
// WAV header
var wavhdr   = "RIFF";
    wavhdr ..= fmtstr("%c%c%c%c", len_b0, len_b1, len_b2, len_b3);
    wavhdr ..= "WAVE";
    wavhdr ..= "fmt ";
    wavhdr ..= "\x10\x00\x00\x00";
    wavhdr ..= "\x01\x00";
    wavhdr ..= "\x01\x00";
    wavhdr ..= "\x44\xac\x00\x00";
    wavhdr ..= "\x44\xac\x00\x00";
    wavhdr ..= "\x01\x00";
    wavhdr ..= "\x08\x00";
    wavhdr ..= "data";
    wavhdr ..= fmtstr("%c%c%c%c", dataLength_b0, dataLength_b1, dataLength_b2, dataLength_b3);
 
// write wav header
var f = fopen("notes.wav", "w");
fwrite(f, wavhdr);
 
 
// compute and write actual data
var frequs = { 261.6, 293.6, 329.6, 349.2, 392.0, 440.0, 493.9, 523.3 };
 
for var j = 0; j < duration; j++ {
	var frequ = frequs[j];
	var omega = 2 * M_PI * frequ;
	for var i = 0; i < dataLength / 8; i++ {
		var y = 32 * sin(omega * i / sampleRate);
		var byte = fmtstr("%c", round(y));
		fwrite(f, byte);
	}
}
 
fclose(f);