Skip to content

Commit

Permalink
Simplify the requirements for PlaySound on macOS/Linux
Browse files Browse the repository at this point in the history
The PlaySound action class only requires the *sounddevice* package
on these platforms.
  • Loading branch information
drmfinlay committed Oct 27, 2022
1 parent 86ead5b commit 5ba1fbb
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 16 deletions.
50 changes: 38 additions & 12 deletions dragonfly/actions/action_playsound.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
============================================================================
This section describes the :class:`PlaySound` action object. This
type of action is used to play sounds or sound files.
type of action is used to play system sounds or wave files.
Example PlaySound uses
Expand Down Expand Up @@ -58,23 +58,25 @@
----------------------------------------------------------------------------
On macOS and Linux, the :class:`PlaySound` action object uses the
`sounddevice <https://python-sounddevice.readthedocs.io/>`__ and
`soundfile <https://python-soundfile.readthedocs.io/>`__ packages to
play specified wave files. The *numpy* package may also need to be
installed. These dependencies may be installed by running the
following command: ::
`sounddevice <https://python-sounddevice.readthedocs.io/>`__ package
to play specified wave files. This package may be installed by
running the following command: ::
$ pip install 'dragonfly2[playsound]'
The *name* parameter is an alias for *file* on these platforms.
On these platforms, an error message will be shown if an invalid file
path is specified.
Class reference
----------------------------------------------------------------------------
"""

import os
import wave

from dragonfly.actions.action_base import ActionBase

Expand All @@ -92,12 +94,36 @@ def play(name, flags):
if not name:
return

import sounddevice as sd
import soundfile as sf

data, fs = sf.read(name)
sd.play(data, fs)
sd.wait()
import sounddevice

# Open the specified file as a wave file.
wf = wave.open(name, 'rb')
try:
# Get params.
channels = wf.getnchannels()
framerate = wf.getframerate()
format = {
1: 'int8',
2: 'int16',
3: 'int24',
4: 'float32'
}[wf.getsampwidth()]

# Play the file through the default playback device.
blocksize = 1024
stream = sounddevice.RawOutputStream(
channels=channels,
samplerate=framerate,
dtype=format,
blocksize=blocksize
)
with stream:
data = wf.readframes(blocksize)
while len(data):
stream.write(data)
data = wf.readframes(blocksize)
finally:
wf.close()


#---------------------------------------------------------------------------
Expand Down
4 changes: 0 additions & 4 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,6 @@ def read(*names):
],
"playsound": [
"sounddevice == 0.3.*;os_name=='posix'",
"soundfile == 0.11.*;os_name=='posix'",

# Required for the way we use the above.
"numpy;os_name=='posix'",
],
},

Expand Down

0 comments on commit 5ba1fbb

Please sign in to comment.