I also wanted live midi support to look at the frequencies and see the harmony of octaves or the chaos of dissonance in real time as you press keys.

potato video but you get the point. It shows the frequency of the ideal sine wave for a given midi notes and the sum of waves when multiple notes are pressed. kinda like a reverse fft

Its doesnâ€™t makes sound, its not a synthesizer. Itâ€™s purely visual for now.

I wrote most this of this code a while ago and forgotten some bits, so I tricked chatgpt into helping me reverse engineer and document parts of this. Let me know of any errors.

There seems to be much terminology to refer to the same scale: (Western scale, 12-tone scale, 12-tet, Chromatic scale, equal-temperament).

The following calculations and formulas aim to conform to Scientific Pitch Notation (SPN) / International Pitch Notation (IPN). It adheres to these definitions and reference frequencies:

```
An octave number increases by 1 upon an ascension from B to C.
12 intervals: semitones per octave.
1 semitone = 100 cents. (1 semitone is subdivided into 100 cents)
Frequencies calculated using "twelfth root of two" rule: 2^(1/12)
1 cent = 2^(1/12) = 1.05946
1 octave = 1200 cents. (12 * 100). A frequency ratio of 2:1
1 Hz = 1 cycle / second.
Reference notes:
Middle C = C4
A4 = MIDI Note 69
A440 = 440 Hz
```

A semitone is a musical interval that corresponds to the smallest step on a standard piano keyboard. It is the interval between one key and the next adjacent key on the keyboard, whether white or black. In Western music, a semitone is equal to half of a whole tone, which is the interval between two keys.

For example, the interval between the notes C and C# is a semitone, as is the interval between B and C.

On a guitar, each fret is a semitone.

Pitch of a note is typically expressed in terms of its frequency in hertz (Hz).

`frequency = reference frequency * 2^(steps from reference note / intervals)`

The reference frequency is the pitch of the reference note, which is typically A4 (also known as A440) with a frequency of 440 Hz. There are 12 semitones in equal temperament. A semitone has 100 cents.

`1 cent = 2^(1/12)`

The steps from reference note is the number of steps or intervals the note is from the reference note on the musical scale.

- intervals = 12
- A4 = 440.0Hz
- A = 9

`f = 440 * 2^((n-9)/12)`

```
('C', 'C#', 'D', 'D#', 'E', 'F', 'F#', 'G', 'G#', 'A', 'A#', 'B').index('A')
>>> 9
```

For example, if you want to calculate the pitch of C4 (middle C), you would use the following calculation:

`pitch = 440 * (2^((0-9) / 12)) = 261.6255653005986`

Frequency of C4 = 261.62 Hz. Where reference note = A440 & C = 0:

##
Mapping MIDI key IDâ€™s to frequencies

Using A4 as a reference note. The MIDI note for A4 is 69. steps from reference note = (midi_note - a4_key_midi). Given MIDI note m:

`f = 440 * 2^((m-69)/12)`

`pitch = 440 * (2^((60-69) / 12)) = 261.6255653005986`

```
def midi_note_to_frequency(midi_note):
a4_freq = 440.0
a4_key_midi = 69
return a4_freq * math.pow(2, (midi_note - a4_key_midi) / 12)
```

##
Converting frequency to cents

A cent is a unit of measure used to describe the difference in pitch between two notes. It is equal to 1/100th of a semitone, which is the smallest interval commonly used in Western 12-tone music. For example are 100 cents between notes C4 and C#4.

To convert the frequency of a note to cents, you can use the following formula:

`cents = 1200 * log2(frequency / reference frequency)`

1200 = 12 notes * 100 cents. The reference frequency is a pitch that is considered to be the â€śzeroâ€ť point for the scale, against which all other pitches are measured. For example, in equal temperament, the reference pitch is typically the note A4 (also known as A440), which has a frequency of 440 Hz.

Therefore, to calculate the number of cents for a given frequency in equal temperament, you can use the following formula:

`cents = 1200 * log2(frequency / 440)`

For example, if you want to calculate the number of cents for a frequency of 220 Hz (which is the pitch of A3), you would use the following calculation:

`cents = 1200 * log2(220 / 440) = -1200`

This means that the pitch of A3 is 1200 cents lower than the reference pitch of A4. A3 is 12 semitones, or 1 octave less than A4.

Thereâ€™s no ryme or reason to why I chose the colors I did. but just for fun visual distinction between octaves and a clear way to visualize the same notes across the fretboard:

```
def get_color_for_octave(octave):
# colorsys.rgb_to_hls(1, 0, 0)
if octave == 1:
return '#9400D3'
elif octave == 2:
return '#4B0082'
elif octave == 3:
return '#0000FF'
elif octave == 4:
return '#00FF00'
elif octave == 5:
return '#FFFF00'
elif octave == 6:
return '#FF0000'
else:
return 'black'
```

(todo: this should probably be a switch statementâ€¦ugly)