Module soundplayer
[hide private]
[frames] | no frames]

Source Code for Module soundplayer

  1  # soundplayer.py 
  2   
  3  import thread 
  4  import os 
  5  import time 
  6  import sys 
  7   
  8  sys.excepthook = None  # avoid error message in playTone() 
9 10 -class SoundPlayer:
11 ''' 12 Sound player based on SoX, called "the Swiss Army knife of sound processing programs" by its developper. 13 This simple Python wrapper is based on Linux shell commands running in extra threads. 14 For the Raspberry Pi the following installation are needed: 15 sudo apt-get install sox 16 sudo apt-get install mp3 17 ''' 18 @staticmethod
19 - def playTone(frequencies, duration, blocking = False, device = 0):
20 ''' 21 Plays one or several sine tones with given frequencies and duration. 22 @param frequencies: the frequency or a list of several frequencies in Hz 23 @param duration: the duration in s 24 @param blocking: if True, the functions blocks until playing is finished; otherwise it returns immediately (default: False) 25 @param device: the sound device ID (e.g. 0: standard device, 1: USB sound adapter) 26 ''' 27 if not type(frequencies) == list: 28 frequencies = [frequencies] 29 if blocking: 30 SoundPlayer._emit(frequencies, duration, device) 31 else: 32 thread.start_new_thread(SoundPlayer._emit, (frequencies, duration, device))
33 34 @staticmethod
35 - def isPlaying():
36 ''' 37 Checks if the sound is still playing. 38 @return: True, if the sound is playing; otherwise False 39 ''' 40 info = os.popen("ps -Af").read() 41 process_count = info.count("play") 42 return process_count >= 2
43 44 @staticmethod
45 - def _emit(frequencies, duration, device):
46 s = " " 47 for f in frequencies: 48 s += "sin " + str(f) + " " 49 cmd = "AUDIODEV=hw:" + str(device) + " play -q -n synth " + str(duration) + \ 50 s + " 2> /dev/null" 51 os.system(cmd)
52
53 - def __init__(self, audiofile, device = 0):
54 ''' 55 Creates a sound player to play the given audio file (wav, mp3, etc.) 56 to be played at given device ID. Throws exception, if the sound resource is not found. 57 @param audiofile: the sound file to play 58 @param device: the sound device ID (e.g. 0: standard device, 1: USB sound adapter) 59 ''' 60 if not os.path.isfile(audiofile) : 61 raise Exception("Audio resource " + audiofile + " not found") 62 self.audiofile = audiofile 63 self.device = device
64 65 @staticmethod
66 - def _run(cmd):
67 os.system(cmd)
68
69 - def play(self, volume = 1, blocking = False):
70 ''' 71 Plays the sound with given volume (default: 1). The function returns immediately. 72 @param volume: the sound level (default: 1) 73 @param blocking: if True, the functions blocks until playing is finished; otherwise it returns immediately (default: False) 74 ''' 75 self.volume = volume 76 cmd = "AUDIODEV=hw:" + str(self.device) + \ 77 " play -v " + str(self.volume) + \ 78 " -q " + self.audiofile + " 2> /dev/null" 79 80 if blocking: 81 self._run(cmd) 82 else: 83 thread.start_new_thread(SoundPlayer._run, (cmd,))
84 85 @staticmethod
86 - def stop():
87 ''' 88 Stops playing. 89 ''' 90 cmd = "sudo killall -9 play" 91 thread.start_new_thread(SoundPlayer._run, (cmd,)) 92 time.sleep(0.5) # Wait until process is killed
93 94 @staticmethod
95 - def pause():
96 ''' 97 Pauses playing momentarily. 98 ''' 99 cmd = "sudo pkill -STOP play" 100 thread.start_new_thread(SoundPlayer._run, (cmd,))
101 102 @staticmethod
103 - def resume():
104 ''' 105 Resumes playing (after it has been stopped). 106 ''' 107 cmd = "sudo pkill -CONT play" 108 thread.start_new_thread(SoundPlayer._run, (cmd,))
109