From 7282a7d04d0b268f9b9dbf65eacaa7647d9fa0ff Mon Sep 17 00:00:00 2001 From: Jono Targett Date: Fri, 24 May 2024 18:00:07 +0930 Subject: [PATCH] Added tone samples generator (and convesion to midi key) --- python/samples.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100755 python/samples.py diff --git a/python/samples.py b/python/samples.py new file mode 100755 index 0000000..aa6b1b5 --- /dev/null +++ b/python/samples.py @@ -0,0 +1,31 @@ +#! /usr/bin/env python3 + +from tones import SQUARE_WAVE +from tones.mixer import Mixer +import csv +import math + +def frequency_to_midi(frequency): + ''' + There are only 128 midi notes. The don't map nicely + to the SELCAL frequencies :'( + + MIDI note #69 is A4 @ 440 Hz. The other notes are given by: + f = 440 * 2 ^ (n - 69)/12 + ''' + note = 12 * (math.log(frequency/220)/math.log(2)) + 57 + return round(note) + +tones = {} +with open('tones.csv', newline='') as csvfile: + reader = csv.DictReader(csvfile) + for row in reader: + tones[row['designator']] = float(row['frequency']) + +for tone in tones: + print(f"{tone} is MIDI #{frequency_to_midi(tones[tone])}") + + mixer = Mixer(44100, 1) + mixer.create_track(0, SQUARE_WAVE) + mixer.add_tone(0, frequency=tones[tone], duration=1.0) + mixer.write_wav(f'samples/{tone}.wav') \ No newline at end of file