I developed a Python script which can use MIDI files (music playback information) to generate G-code files (machine instructions) for a 3D printer to "play." The video above demonstrates one such G-code file played on my custom 3D printer.
Older consumer FDM 3D printers use unsophisticated stepper motor drivers which cause each motor to produce a distinctive hum when an axis is driven. Though most modern consumer FDM offerings feature "silent" stepper motor drivers to reduce this noise during operation, they can often be configured to use the more primitive waveforms to bring back the hum (as was done above).
The "pitch" of that hum correlates with how fast the motor is spinning. The faster the spin, the higher the note produced.
Knowing which frequencies produce each note, we can calculate the feed rate that each axis needs to travel at to produce the desired frequencies. The distance each axis travels determines the note length. If we need two notes to play at the same time, we do this for two different axes on the printer.
Using this information, we can generate a movement command. By chaining many movement commands together in sequence, we create a song.
The full source code can be found on my GitHub: github.com/Lukrative525/Music-Box