Skip to content

Conversation

@marler8997
Copy link

@marler8997 marler8997 commented Jan 17, 2022

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, ...);
}

@joshgoebel
Copy link
Contributor

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"...

@marler8997 marler8997 force-pushed the midiMode branch 7 times, most recently from 3deaf52 to 5321f17 Compare January 22, 2022 01:55
@JerwuQu JerwuQu mentioned this pull request Dec 29, 2023
aduros added a commit that referenced this pull request Apr 18, 2024
APU Note Mode (alternative version of #334)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants