Skip to content

Commit fe47e7f

Browse files
committed
Implement Sound class
See #15
1 parent 90d0853 commit fe47e7f

File tree

1 file changed

+92
-0
lines changed

1 file changed

+92
-0
lines changed

ev3dev.py

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
from os.path import abspath
3939
from PIL import Image, ImageDraw
4040
from struct import pack, unpack
41+
from subprocess import Popen
4142

4243
#------------------------------------------------------------------------------
4344
# Guess platform we are running on
@@ -2416,3 +2417,94 @@ def update(self):
24162417
else:
24172418
raise Exception("Not supported")
24182419

2420+
2421+
class Sound:
2422+
"""
2423+
Sound-related functions. The class has only static methods and is not
2424+
intended for instantiation. It can beep, play wav files, or convert text to
2425+
speach.
2426+
2427+
Note that all methods of the class spawn system processes and return
2428+
subprocess.Popen objects. The methods are asynchronous (they return
2429+
immediately after child process was spawned, without waiting for its
2430+
completion, but you can call wait() on the returned result.
2431+
2432+
Examples:
2433+
2434+
# Play 'bark.wav', return immediately:
2435+
Sound.play('bark.wav')
2436+
2437+
# Introduce yourself, wait for completion:
2438+
Sound.speak('Hello, I am Robot').wait()
2439+
"""
2440+
2441+
@staticmethod
2442+
def beep(args=''):
2443+
"""
2444+
Call beep command with the provided arguments (if any).
2445+
See beep man page [1] and google 'linux beep music' for inspiration.
2446+
2447+
[1]: http://linux.die.net/man/1/beep
2448+
"""
2449+
with open(os.devnull, 'w') as n:
2450+
return Popen('/usr/bin/beep %s' % args, stdout=n, shell=True)
2451+
2452+
2453+
@staticmethod
2454+
def tone(tone_sequence):
2455+
"""
2456+
Play tone sequence. The tone_sequence parameter is a list of tuples,
2457+
where each tuple contains up to three numbers. The first number is
2458+
frequency in Hz, the second is duration in milliseconds, and the third
2459+
is delay in milliseconds between this and the next tone in the
2460+
sequence.
2461+
2462+
Here is a cheerful example:
2463+
Sound.tone([
2464+
(392, 350, 100), (392, 350, 100), (392, 350, 100), (311.1, 250, 100),
2465+
(466.2, 25, 100), (392, 350, 100), (311.1, 250, 100), (466.2, 25, 100),
2466+
(392, 700, 100), (587.32, 350, 100), (587.32, 350, 100),
2467+
(587.32, 350, 100), (622.26, 250, 100), (466.2, 25, 100),
2468+
(369.99, 350, 100), (311.1, 250, 100), (466.2, 25, 100), (392, 700, 100),
2469+
(784, 350, 100), (392, 250, 100), (392, 25, 100), (784, 350, 100),
2470+
(739.98, 250, 100), (698.46, 25, 100), (659.26, 25, 100),
2471+
(622.26, 25, 100), (659.26, 50, 400), (415.3, 25, 200), (554.36, 350, 100),
2472+
(523.25, 250, 100), (493.88, 25, 100), (466.16, 25, 100), (440, 25, 100),
2473+
(466.16, 50, 400), (311.13, 25, 200), (369.99, 350, 100),
2474+
(311.13, 250, 100), (392, 25, 100), (466.16, 350, 100), (392, 250, 100),
2475+
(466.16, 25, 100), (587.32, 700, 100), (784, 350, 100), (392, 250, 100),
2476+
(392, 25, 100), (784, 350, 100), (739.98, 250, 100), (698.46, 25, 100),
2477+
(659.26, 25, 100), (622.26, 25, 100), (659.26, 50, 400), (415.3, 25, 200),
2478+
(554.36, 350, 100), (523.25, 250, 100), (493.88, 25, 100),
2479+
(466.16, 25, 100), (440, 25, 100), (466.16, 50, 400), (311.13, 25, 200),
2480+
(392, 350, 100), (311.13, 250, 100), (466.16, 25, 100),
2481+
(392.00, 300, 150), (311.13, 250, 100), (466.16, 25, 100), (392, 700)
2482+
]).wait()
2483+
"""
2484+
def beep_args(frequency=None, duration=None, delay=None):
2485+
args = '-n '
2486+
if frequency is not None: args += '-f %s ' % frequency
2487+
if duration is not None: args += '-l %s ' % duration
2488+
if delay is not None: args += '-d %s ' % delay
2489+
2490+
return args
2491+
2492+
return Sound.beep(' '.join([beep_args(*t) for t in tone_sequence]))
2493+
2494+
2495+
@staticmethod
2496+
def play(wav_file):
2497+
"""
2498+
Play wav file.
2499+
"""
2500+
with open(os.devnull, 'w') as n:
2501+
return Popen('/usr/bin/aplay -q "%s"' % wav_file, stdout=n, shell=True)
2502+
2503+
2504+
@staticmethod
2505+
def speak(text):
2506+
"""
2507+
Speak the given text aloud.
2508+
"""
2509+
with open(os.devnull, 'w') as n:
2510+
return Popen('/usr/bin/espeak -a 200 --stdout "%s" | /usr/bin/aplay -q' % text, stdout=n, shell=True)

0 commit comments

Comments
 (0)