Skip to content

Instantly share code, notes, and snippets.

@phuang1024
Created July 25, 2023 02:17
Show Gist options
  • Select an option

  • Save phuang1024/691fb8b285946c89818af9c49dc966b0 to your computer and use it in GitHub Desktop.

Select an option

Save phuang1024/691fb8b285946c89818af9c49dc966b0 to your computer and use it in GitHub Desktop.
Generate stretch tuning frequencies.
STRETCH = (
-20, -19, -18, -17, -16, -15, -14, -13, -12, -11, -10, -9,
-8, -7, -6, -6, -5, -5, -4, -4, -4, -4, -4, -3,
-3, -3, -3, -3, -3, -3, -3, -3, -3, -2.5, -2.5, -2,
-2, -2, -2, -2, -2, -2, -1.5, -1, -1, -1, -1, -1,
0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2,
2, 2, 2, 2, 3, 3, 3, 4, 4, 5, 5, 6,
6, 7, 8, 9, 10, 11, 12, 13, 15, 17, 19, 21,
23, 25, 27, 30
)
assert len(STRETCH) == 88
A4 = 48
TUNING = 443
NOTE_NAMES = ("A", "A#", "B", "C", "C#", "D", "D#", "E", "F", "F#", "G","G#")
def note_to_name(note: int):
octave = note // 12
name = NOTE_NAMES[note % 12]
return f"{name}{octave}"
def get_freq(note: int | float):
offset = note - A4
mult_per_note = 2 ** (1/12)
freq = TUNING * (mult_per_note ** offset)
return freq
def get_freq_stretch(note: int):
base = get_freq(note)
# Calculate numerical d(freq)/d(note)
h = 0.01
deriv = (get_freq(note+h) - get_freq(note-h)) / (2*h)
freq_offset = STRETCH[note] / 100 * deriv
return base + freq_offset
if __name__ == "__main__":
for i in range(88):
name = note_to_name(i)
base_freq = get_freq(i)
stretch_freq = get_freq_stretch(i)
print(f"{name:3}\tbase={base_freq:.2f}\tstretch={stretch_freq:.2f}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment