Musical scale: Difference between revisions
Content deleted Content added
m J |
No edit summary |
||
Line 215: | Line 215: | ||
if @.note\==0 then call sound @.note,dur /*sound the "note".*/ |
if @.note\==0 then call sound @.note,dur /*sound the "note".*/ |
||
return</lang> |
return</lang> |
||
=={{header|Sparkling}}== |
|||
The following Sparkling program generates a WAVE audio file named "notes.wav" that can be played in order to achieve the required effect: |
|||
<lang Sparkling> |
|||
var sampleRate = 44100.0; |
|||
var duration = 8.0; |
|||
var dataLength = round(sampleRate * duration); // 1 second of data |
|||
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);</lang> |
|||
=={{header|Tcl}}== |
=={{header|Tcl}}== |