| Trees | Indices | Help |
|---|
|
|
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()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 @staticmethod10920 ''' 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 @staticmethod36 ''' 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 >= 243 44 @staticmethod46 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)5254 ''' 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 = device64 65 @staticmethod 6870 ''' 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 @staticmethod87 ''' 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 killed93 94 @staticmethod96 ''' 97 Pauses playing momentarily. 98 ''' 99 cmd = "sudo pkill -STOP play" 100 thread.start_new_thread(SoundPlayer._run, (cmd,))101 102 @staticmethod104 ''' 105 Resumes playing (after it has been stopped). 106 ''' 107 cmd = "sudo pkill -CONT play" 108 thread.start_new_thread(SoundPlayer._run, (cmd,))
| Trees | Indices | Help |
|---|
| Generated by Epydoc 3.0.1 on Sun Apr 16 12:46:36 2017 | http://epydoc.sourceforge.net |