Hello, I’ve been trying to implement a couple of M300 tones for when the print starts and ends.
Basically, I take a MIDI file and run it through a converter to get the M300 values.
I can preview the result in the converter and it sounds fine. It also sounds fine when tested with Marlin firmware. But when I try to play the same tones (same gcode) using Klipper they sound completely different. The timing appears to be correct but the pitch is totally wrong. I don’t expect it to be pitch-perfect, but at least as good as the demo linked below.
I’ve set up the M300 macro according to sample_macros.cfg:
[gcode_macro M300]
gcode:
{% set S = params.S|default(1000)|int %} # Use a default 1kHz tone if S is omitted.
{% set P = params.P|default(100)|int %} # Use a 10ms duration is P is omitted.
SET_PIN PIN=BEEPER_pin VALUE=0.5 CYCLE_TIME={ 1.0/S if S > 0 else 1 }
G4 P{P}
SET_PIN PIN=BEEPER_pin VALUE=0
[output_pin BEEPER_pin]
pin: EXP1_1 # Beeper pin. This parameter must be provided.
pwm: True # A piezo beeper needs a PWM signal, a DC buzzer doesn't.
value: 0 # Silent at power on, set to 1 if active low.
shutdown_value: 0 # Disable at emergency shutdown (no PWM would be available anyway).
cycle_time: 0.001 # Default PWM frequency : 0.001 = 1ms will give a tone of 1kHz (not pitch perfect).
Should I just add hardware_pwm: False after pwm: True?
@iCach0 and @exo316 you could record the sounds produced and post it here and/or measure the signal at the beeper, if you have access to an oscilloscope.
#pwm: False
# Set if the output pin should be capable of pulse-width-modulation.
# If this is true, the value fields should be between 0 and 1; if it
# is false the value fields should be either 0 or 1. The default is
# False.
The main issue is the timing. Marlin uses 260hz at 1ms. All the macros I found are only 2khz by default. You would have to modify the freq and duration. I’m working on matching it. If I get it right I will share.
[gcode_macro M300]
gcode:
# Use a default 1kHz tone if S is omitted.
{% set S = params.S|default(260)|int %}
# Use a 10ms duration is P is omitted.
{% set P = params.P|default(100)|int %}
SET_PIN PIN=BEEPER VALUE=0.5 CYCLE_TIME={ 1.0/S if S > 0 else 1 }
G4 P{P}
SET_PIN PIN=BEEPER VALUE=0
wellerman_1.gcode (12.6 KB)
Try this one… it plays ok, on mine… the key was to add the G4 in the converter… happy music. (oh by the way, I also found with calling aplay you can play .wav files on the host) instead of using the beeper
[gcode_macro M300]
gcode:
# Use a default 1kHz tone if S is omitted.
{% set S = params.S|default(1000)|int %}
# Use a 10ms duration is P is omitted.
{% set P = params.P|default(100)|int %}
# SET_PIN PIN=Case_Light VALUE=0
# SET_PIN PIN=LCD_Light VALUE=0
SET_PIN PIN=_BEEPER_pin VALUE=0.5 CYCLE_TIME={ 1.0/S if S > 0 else 1 }
# SET_PIN PIN=Case_Light VALUE=0.5 CYCLE_TIME={ 1.0/S if S > 0 else 1 }
# SET_PIN PIN=LCD_Light VALUE=0.5 CYCLE_TIME={ 1.0/S if S > 0 else 1 }
G4 P{P}
SET_PIN PIN=_BEEPER_pin VALUE=0
# SET_PIN PIN=Case_Light VALUE=1
# SET_PIN PIN=LCD_Light VALUE=1
# End Macro M300
here is my config… fyi, I had set it to blink the lights too but I found fancier ways of doing the lights, so are commented out at them moment.
Thank you DaVinci10, I made a change to the code to take out the extra beeps I was hearing on my machine. You can’t have a cycle time of 0, which would be silent anyway, so anytime S is 0 I just have it do the dwell time for the pause.
Here is the updated code:
[gcode_macro M300]
gcode:
# Use a default 1kHz tone if S is omitted.
{% set S = params.S|default(1000)|int %}
# Use a 10ms duration is P is omitted.
{% set P = params.P|default(100)|int %}
#SET_PIN PIN=Case_Light VALUE=0
#SET_PIN PIN=LCD_Light VALUE=0
{% if S > 0 %}
SET_PIN PIN=BEEPER_Pin VALUE=0.5 CYCLE_TIME={ 1.0/S }
#SET_PIN PIN=Case_Light VALUE=0.5 CYCLE_TIME={ 1.0/S }
#SET_PIN PIN=LCD_Light VALUE=0.5 CYCLE_TIME={ 1.0/S }
G4 P{P}
{% else %}
G4 P{P}
{% endif %}
SET_PIN PIN=BEEPER_Pin VALUE=0
#SET_PIN PIN=Case_Light VALUE=1
#SET_PIN PIN=LCD_Light VALUE=1
# End Macro M300