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