Permalink
Browse files

Make simpleaudio an optional dependency

The audio plugin now handles the absence of the simpleaudio package
gracefully. The package is moved to an additional requirements file so
that it doesn't become a roadblock to getting the server up and running.
  • Loading branch information...
lovett committed Dec 25, 2018
1 parent 65fa993 commit f9ffb7e644e29a12689e70f562dc5b579903fd61
Showing with 30 additions and 4 deletions.
  1. +29 −3 plugins/audio.py
  2. +1 −0 requirements-audio.txt
  3. +0 −1 requirements.txt
@@ -1,11 +1,26 @@
"""Play audio files
see https://github.com/hamiltron/py-simple-audio
Playback is handled by the simpleaudio package. Getting it to install
can be a challenge since headers for both Python and Alsa are
needed. Dealing with this just to get the medley server to run doesn't
make sense--audio playback is not a critical function, just nice
to have.
When simpleaudio is not present, the plugin will log an error message
if asked to play a file but otherwise behave normally.
See https://github.com/hamiltron/py-simple-audio
"""

import cherrypy
from cherrypy.process import plugins
import simpleaudio as sa

# Failure to import simpleaudio is allowed.
try:
import simpleaudio as SIMPLE_AUDIO # pylint: disable=import-error
except ModuleNotFoundError:
SIMPLE_AUDIO = None


class Plugin(plugins.SimplePlugin):
@@ -19,19 +34,30 @@ def start(self):
This plugin owns the audio prefix.
"""
if not SIMPLE_AUDIO:
cherrypy.log("Audio playback is disabled")

self.bus.subscribe('audio:wav:play', self.play)

@staticmethod
def play(path):
"""Play a file. So far only WAVE is supported."""

if not SIMPLE_AUDIO:
message = "Ignoring request to play {}".format(
path
)

cherrypy.log(message)
return

cherrypy.engine.publish(
"applog:add",
"audio",
"play",
"playing {}".format(path)
)

wave_obj = sa.WaveObject.from_wave_file(path)
wave_obj = SIMPLE_AUDIO.WaveObject.from_wave_file(path)
play_obj = wave_obj.play()
play_obj.wait_done()
@@ -0,0 +1 @@
simpleaudio==1.0.2
@@ -8,4 +8,3 @@ pyparsing==2.3.0
pytz==2018.4
pytzdata==2018.7
requests==2.21.0
simpleaudio==1.0.2

0 comments on commit f9ffb7e

Please sign in to comment.