freude/freude.py
2024-12-23 01:43:51 +01:00

25 lines
865 B
Python

import csv
# actually an arbitrary scaling factor
steps_per_mm = 5
# starting from C3 = 130.81 Hz
frequencies = [2 ** (i / 12) * 130.81 for i in range(24)]
bpm = 60
# duration of 1/8th
base_duration = 60 / bpm / 8
with open("FREUDE.gcode", "w") as f:
f.write("G21 ; set units to millimeters\n")
f.write("G90 ; use absolute coordinates\n")
f.write("G0 Z10 F300\n")
f.write("G0 X0 Y0 F3000\n")
f.write("G4 S1 ; wait 1 second\n")
position = 0
with open("freude.txt", "r") as notes:
for note, duration in csv.reader(notes):
feedrate = int(frequencies[int(note) + 12] * 60 / steps_per_mm)
length = int(duration) * base_duration / 60 * feedrate
position += -length if position > 100 else length
f.write(f"G0 X{position:.6f} Y{position:.6f} F{feedrate} \n")
f.write("G4\n")