Ursprungsmitteilung
Thema New library version of soundplayer.py 
Autor R.Brause 
Eingangsdatum 2019-07-04 15:51:02.0 
Mitteilung The old soundplayer version used in the tutorial for programming the Raspberry pi (see www.python-exemplarisch.ch) has several drawbacks.
For Python 3.x, the library does not work anymore and had to be updated.
1) The module thread is now _thread
2) All print statements have to contain brackets (..)
3) SOme interface commands for the sox-library have to be updated to contain a "volume" parameter for volume control.

The new proposed soundplayer.py library becomes now:


# soundplayer.py
# provides an python interface to the sox run time modules
# @authors: A. Kohn
# @version: 3.0
# 6/2019 for Python 3.x R.Brause
# The volume parameter was added and the stop/pause/resume
# activities were made immediate.



from _thread import start_new_thread
import os

class SoundPlayer:
'''
Sound player based on SoX, called "the Swiss Army knife of sound processing programs" by its developper.
This simple Python wrapper is based on Linux shell commands running in extra threads.
For the Raspberry Pi the following installation are needed:
sudo apt-get install sox
sudo apt-get install mp3
'''
@staticmethod
def playTone(frequencies, duration, blocking = False, device = 0, volume=1):
'''
Plays one or several sine tones with given frequencies and duration.
@param frequencies: the frequency or a list of several frequencies in Hz
@param duration: the duration in s
@param blocking: if True, the functions blocks until playing is finished; otherwise it returns immediately (default: False)
@param device: the sound device ID (e.g. 0: standard device, 1: USB sound adapter)
'''
if not type(frequencies) == list:
frequencies = [frequencies]
if blocking:
SoundPlayer._emit(frequencies, duration, device, volume)
else:
start_new_thread(SoundPlayer._emit, (frequencies, duration, device, volume))

@staticmethod
def isPlaying():
'''
Checks if the sound is still playing.
@return: True, if the sound is playing; otherwise False
'''
info = os.popen("ps -Af").read()
process_count = info.count("play")

return process_count >= 1

@staticmethod
def _emit(frequencies, duration, device, volume):
s = " "
for f in frequencies:
s += "sin " + str(f) + " "
cmd = "AUDIODEV=hw:" + str(device) +
" play -q -n "
" synth " + str(duration) + s +
" vol "+ str(volume) +
" 2> /dev/null"
# print("cmd=",cmd)
os.system(cmd)

def __init__(self, audiofile, device = 0):
'''
Creates a sound player to play the given audio file (wav, mp3, etc.)
to be played at given device ID. Throws exception, if the sound resource is not found.
@param audiofile: the sound file to play
@param device: the sound device ID (e.g. 0: standard device, 1: USB sound adapter)
'''
if not os.path.isfile(audiofile) :
raise Exception("Audio resource " + audiofile + " not found")
self.audiofile = audiofile
self.device = device

@staticmethod
def _run(cmd):
# print("cmd=",cmd)
os.system(cmd)


def play(self, volume = 1, blocking = False, repeat = 1):
'''
Plays the sound with given volume (default: 1). The function returns immediately.
@param volume: the sound level (default: 1)
@param blocking: if True, the functions blocks until playing is finished; otherwise it returns immediately (default: False)
@param repeat: if 1, the audio file is repated one time. Using an integer n means n times.
'''
self.volume = volume # volume of audio file

cmd = "AUDIODEV=hw:" + str(self.device) +
" play -v " + str(self.volume) +
" -q " + self.audiofile +
" repeat " + str(repeat) + " 2> /dev/null"

if blocking:
self._run(cmd)
else:
start_new_thread(SoundPlayer._run, (cmd,))

@staticmethod
def stop():
'''
Stops playing and return after stopping
'''
cmd = "sudo killall play 2> /dev/null"
SoundPlayer._run(cmd)
# start_new_thread(SoundPlayer._run, (cmd,))

@staticmethod
def pause():
'''
Pauses playing momentarily.
'''
cmd = "sudo pkill -STOP play"
SoundPlayer._run(cmd)
# start_new_thread(SoundPlayer._run, (cmd,))

@staticmethod
def resume():
'''
Resumes playing (after it has been paused).
'''
cmd = "sudo pkill -CONT play"
SoundPlayer._run(cmd)
# start_new_thread(SoundPlayer._run, (cmd,))

 
 
      
Antworten
Thema New version of soundplayer.py 
Autor Aegidius Pluess 
Eingangsdatum 2019-07-11 17:41:06.0 
Mitteilung Many thanks for your contribution. It is a major improvement of my code.

A.P.