wasm4
wasm4 copied to clipboard
An implementation of a new frequency mode, see issue #333
An implementation of the "Audio Frequency Proposal" found here: #333
At this point the proposal has not been accepted, but I've implemented it here to demonstrate what it would take to support the proposal along with keeping the current mechanism (whole-number frequencies).
To summarize what this does, this redistributes the frequencies to be logarithmic so that a difference in values has the same harmonic affect regardless of the absolute value of the frequency. Adding 256 to the tone argument value increases the frequency by one "musical half step". The formula to convert a tone argument to a frequency is now given by:
const twelvth_root_of_2 = Math.pow(2, 1/12);
function wasmArgToFrequency(tone_arg) {
return 440 * Math.pow(twelvth_root_of_2, (tone_arg / 256) - 69);
}
Here's an example of how to use it:
// current interface, play the pitch A4
tone(440, ...);
// enable the new interface
*SYSTEM_FLAGS |= SYSTEM_MIDI_FREQUENCY_MODE; // this enables the new MIDI frequency mode
// new interface, play the pitch A4 (69 comes from the MIDI protocol definition of A4)
tone(69 << 8, ...);
// to play up the scale in half steps (note: limited to a more reasonable hearing range between 12 and 100)
for (var i = 12; i < 100; i++) {
tone(i << 8, ...);
}
Feels like we need a note API... the higher-level libs I'm writing for TIC-80 have a note API for playing notes that does the translations from notes into frequencies and the lower-level APIs... 69 isn't a "frequency" or "pitch" for A4, just a ID... so the naming is already a bit "off"...