MIDI to G-Code Converter

Turn your 3D printer into a musical instrument using Python.

What you need before starting

🐍

1. Python Installed

Download Python from python.org. During installation, make sure to check the box Add Python to PATH.

📦

2. 'mido' Library

Open Command Prompt (CMD) and run this command:

pip install mido

Step-by-Step Guide

  1. Create a new, empty folder on your computer.
  2. Save the Python script below inside that folder as HEROS_Converter.py.
  3. Copy your MIDI file into the same folder and rename it exactly to music.mid.
  4. Open Command Prompt in that folder and run:
    python HEROS_Converter.py
  5. When the black window says "Done!", press Enter to close it.
  6. Open the generated music_print.gcode file using Notepad.
  7. Select all text, copy it, and paste it into the Machine start G-code section in your slicer (e.g. Bambu Studio).
HEROS_Converter.py
import mido
import os

def convert_midi_to_gcode(midi_path, gcode_path):
    print(f"Starting conversion: {midi_path} -> {gcode_path}")
    
    try:
        mid = mido.MidiFile(midi_path)
    except Exception as e:
        print(f"Error loading MIDI file: {e}")
        return

    gcode_lines = [
        "; Generated by MIDI -> G-code converter",
        "M17",
        "M400 S1",
        "M1006 S1"
    ]
    
    active_notes = {}
    accumulated_time = 0.0
    
    SPEED_MULTIPLIER = 100 
    
    print("Analyzing notes...")

    for msg in mid:
        accumulated_time += msg.time
        
        if msg.type not in ['note_on', 'note_off']:
            continue
            
        if accumulated_time > 0.01: 
            duration_units = int(accumulated_time * SPEED_MULTIPLIER)
            
            if duration_units > 0:
                if active_notes:
                    notes = list(active_notes.items())[:3]
                    
                    channels = ['A', 'C', 'E']
                    durs = ['B', 'D', 'F']
                    vels = ['L', 'M', 'N']
                    
                    line = "M1006"
                    for i, (note, vel) in enumerate(notes):
                        gcode_vel = int((vel / 127) * 99) 
                        line += f" {channels[i]}{note} {durs[i]}{duration_units} {vels[i]}{gcode_vel}"
                    gcode_lines.append(line)
                else:
                    gcode_lines.append(f"M1006 A0 B{duration_units} L0")
            
            accumulated_time = 0.0

        if msg.type == 'note_on' and msg.velocity > 0:
            active_notes[msg.note] = msg.velocity
        elif msg.type == 'note_off' or (msg.type == 'note_on' and msg.velocity == 0):
            if msg.note in active_notes:
                del active_notes[msg.note]
                
    gcode_lines.append("M1006 W")
    gcode_lines.append("M18")
    
    with open(gcode_path, 'w', encoding='utf-8') as f:
        f.write("\n".join(gcode_lines))
        
    print(f"Done! Created file: {gcode_path}")

if __name__ == "__main__":
    input_midi = "music.mid" 
    output_gcode = "music_print.gcode"
    
    if os.path.exists(input_midi):
        convert_midi_to_gcode(input_midi, output_gcode)
    else:
        print(f"File '{input_midi}' not found in the current folder!")
        
    input("\nPress Enter to exit...")
🎵 RHCP_Song_cant_stop.gcode

Show us your results! Tag @heros_engineering on Instagram.

COPYRIGHT @ 2026 HEROS ENGINEERING

CONTACT

contact@heros-engineering.com