Skip to content

Instantly share code, notes, and snippets.

@wd5gnr
Created September 24, 2024 18:43
Show Gist options
  • Save wd5gnr/24e0caf3f14f64f0d1a89e4d4d6dfaa6 to your computer and use it in GitHub Desktop.
Save wd5gnr/24e0caf3f14f64f0d1a89e4d4d6dfaa6 to your computer and use it in GitHub Desktop.
Klipper Aplay Extra
# Play a sound from gcode
#
# Copyright (C) 2023 Al Williams
#
#
# This file may be distributed under the terms of the GNU GPLv3 license.
import os
import shlex
import subprocess
import logging
class AplayCommand:
def __init__(self, config):
self.name = config.get_name().split()[-1] # get our name
self.printer = config.get_printer()
self.gcode = self.printer.lookup_object('gcode') # get wave and path
wav = config.get('wave')
path = config.get('path',None)
if path!=None:
wav = "aplay "+path+'/'+wav
else:
wav = "aplay " + wav
self.wav = shlex.split(wav) # build command line
self.gcode.register_mux_command( # register new command for gcode_macro
"APLAY", "SOUND", self.name,
self.cmd_APLAY_COMMAND, # worker for new command
desc=self.cmd_APLAY_COMMAND_help) # help text
cmd_APLAY_COMMAND_help = "Play a sound"
def cmd_APLAY_COMMAND(self, params):
try:
proc = subprocess.run(self.wav) # run aplay
except Exception:
logging.exception(
"aplay: Wave {%s} failed" % (self.name))
raise self.gcode.error("Error playing {%s}" % (self.name))
# main entry point
def load_config_prefix(config):
return AplayCommand(config)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment