Created
September 24, 2024 18:43
-
-
Save wd5gnr/24e0caf3f14f64f0d1a89e4d4d6dfaa6 to your computer and use it in GitHub Desktop.
Klipper Aplay Extra
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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