Lucas Seiki Oshiro

Show HN: Using Haskell to play music on 3D printer motors (2020)

Last verified:

Visit Lucas Seiki Oshiro

What is Lucas Seiki Oshiro?

music2gcode is a Haskell program created by Lucas Seiki Oshiro that converts music written in a custom text-based format into G-code commands for 3D printers. When executed by a Cartesian filament 3D printer, the G-code commands cause the stepper motors to vibrate at specific frequencies, producing musical notes through the motor sounds. The tool is designed specifically for users who want to make their 3D printers play music using only standard G-code protocol without modifying the printer's firmware.

The program works through a 5-stage pipeline: parsing the input music format, building a frequency table from musical notes using the formula freq = mult * c0 * baseExp^(fromFigure), calculating movement distances and velocities for each axis, converting relative movements to absolute positions within printer bounds, and finally generating G0 (Linear Move) commands. Each note is represented as `<nota> <oitava> <duração>` using Anglo-Saxon notation (CDEFGAB) with support for sharps (#) and flats (b), while silences are represented as `- <duração>`.

Key features include multi-channel support (channels play simultaneously), automatic axis boundary checking to prevent damage to the printer, configurable tempo in BPM, support for multiple octaves (0-3), and the ability to use X, Y, and Z axes as separate audio channels. The tool targets 3D printing enthusiasts, Haskell programmers, and hobbyists interested in unconventional uses of stepper motors. Future features planned include displaying song lyrics on the printer display, using the extruder motor as an extra channel, using the buzzer as an extra channel, and adding support for Delta and CoreXY printers.

Lucas Seiki Oshiro pricing

Pricing model: Freemium

Free and open source - available on GitHub at https://github.com/lucasoshiro/music2gcode. The tool is a hobby project with no paid tiers or commercial plans.

Lucas Seiki Oshiro pros

  • Written in Haskell with a clean embedded domain-specific language (EDSL) structure
  • Converts music to G-code without requiring firmware modifications
  • Supports multi-channel music with simultaneous playback
  • Automatic safety checking prevents movements beyond printer axis limits
  • Custom text-based input format is easy for humans to write and parse
  • Supports sharps (#) and flats (b) in musical notation
  • Configurable tempo using BPM setting
  • Supports multiple octaves from 0 to 3
  • Uses standard G0 Linear Move commands compatible with Marlin firmware
  • Calculates precise frequency-to-feedrate conversion using physics formulas
  • Handles rests/silences in the music composition
  • Works with any Cartesian filament 3D printer using G-code
  • References base frequency c0 = 16.35 Hz for accurate note calculation
  • Uses 2^(1/12) ratio for semitone frequency calculations
  • Recursive position calculation breaks movements that exceed boundaries
  • 2023 Update added GuitarPro tablature support as input format

Lucas Seiki Oshiro cons

  • Only works with Cartesian filament 3D printers, not Delta printers
  • Input format is much less flexible than MIDI, MusicXML, or professional notation software
  • Does not support double sharps or double flats
  • No extruder motor support in the original version (only X, Y, Z axes)
  • No built-in buzzer support in the original version
  • Requires manual calculation of steps-per-mm for your specific printer
  • No graphical interface - entirely text-based input and output
  • GuitarPro support added in 2023 may be buggy as it was implemented in one afternoon
  • Does not work on SLA printers since they lack stepper motors
  • No automatic home position movement before playing (must be configured manually)
  • Limited to 3 audio channels (one per Cartesian axis)
  • No lyrics or song title display functionality in original version
  • Input format requires manual transcription of music - no import from common formats initially
  • No preview or simulation of how the music will sound before generating G-code
  • Axis movement may cause physical wear on printer motors from frequent use

Frequently asked questions about Lucas Seiki Oshiro

How does the tool actually make the 3D printer produce musical notes?

The stepper motors produce sound through vibrations. The more steps a stepper motor makes per second, the higher the frequency and thus the higher (more acute) the sound. The program calculates the exact feedrate (velocity in mm/min) and movement distance for each axis to produce the desired frequency for each musical note using the formula p = Δt * f (steps = duration × frequency), then converts this to G-code movement commands.

What is the input music format and how do I write music in it?

The input format is a simplified custom text format with: TEMPO <bpm> for tempo, BEGINCH/ENDCH to define channels (channels play simultaneously), and notes written as `<nota> <oitava> <duração>` using Anglo-Saxon notation (CDEFGAB) with # for sharps and b for flats. Silences are written as `- <duração>`. For example, Brejeiro by Ernesto Nazareth can be transcribed in this format.

What G-code commands does the tool generate?

The tool generates G0 (Linear Move) commands in the format: G0 X<posição X> Y<posição Y> Z<posição Z> F<velocidade>. For example, G0 X10 Y20 Z30 F200 moves the print tip linearly to position (10mm, 20mm, 30mm) at 200mm/min speed. The frequency is controlled by the F (feedrate) parameter and the note duration by the distance parameter.

Which 3D printers does this work with?

The tool works specifically with Cartesian filament 3D printers that use G-code protocol (the most common type). It does not work on Delta printers. The tool focuses on filament printers with Cartesian movement and is compatible with Marlin firmware, which is widely used in 3D printers.

How are musical frequencies calculated from note names?

The program uses the formula: freq = mult * c0 * baseExp^(fromFigure), where c0 = 16.351597831287418 Hz (frequency of C0), baseExp = 1.0594630943592953 (2^(1/12), the semitone ratio), mult = 2^octave (octave multiplier since frequency doubles per octave), and fromFigure converts the note name (like A#) to semitons distance from C in the same octave.

How does the tool prevent the printer from moving beyond its physical limits?

The tool uses a recursive function nextSafeMovements that checks if a movement would exceed axis bounds. If there's enough space for one movement, it uses it. If not, it breaks the movement into multiple smaller movements, moving to the edge first, then recursively processing the remaining movement. This ensures all movements stay within the printer's printable area to prevent damage.

Can I use more than 3 audio channels?

In the original version, only 3 channels are available (X, Y, Z axes). The extruder motor and buzzer are not supported initially but are listed as possible future features. The 2023 Update added GuitarPro tablature support as input, but the channel limit remains at 3 for the original custom format.

What is the 2023 Update and what does it add?

The 2023 Update added support for GuitarPro tablatures as input format. This was implemented in one afternoon and may still be buggy, but it's a significant advancement since editing in GuitarPro is easier than the original custom text format. The GuitarPro parser works as the parser described in Stage 1 of the original pipeline.

How do I calculate the duration of each note in the input format?

Duration is calculated using the formula: period = 60 * beats / bpm, where bpm is the tempo setting and beats is the note value (like 1/4 for quarter note, 1/8 for eighth note). This is a simple ratio: if you make bpm beats in 60 seconds, how many seconds does it take for beats number of beats?

Is the code available and how do I get it?

Yes, the project is available on GitHub at https://github.com/lucasoshiro/music2gcode. It is written in Haskell and the complete EDSL (Embedded Domain Specific Language) is described in the implementation. The project includes the parser, frequency calculation, movement conversion, and G-code generation functions.

Categories

Use cases

Browse all AI tools on NeedAnAI