-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbeep.py
More file actions
47 lines (37 loc) · 1.28 KB
/
beep.py
File metadata and controls
47 lines (37 loc) · 1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import pyaudio, os, wave
from log import Log
class BeepHandler:
_tag = "beep_handler"
CHUNK=1024
"""
Handler to play a beep on the recording commencing.
:param str on_beep_audio_file: File name to play.
"""
def __init__(self, on_beep_audio_file):
dir = os.path.dirname(os.path.realpath(__file__))
self.filename = on_beep_audio_file
self.filepath = os.path.join(dir, self.filename)
self._pyaudio = pyaudio.PyAudio()
Log.debug(self._tag, "BeepHandler created")
def play(self):
"""
Play the beep.
"""
Log.debug(self._tag, "Play a beep")
f = wave.open(self.filepath,"rb")
stream = self._pyaudio.open(format = self._pyaudio.get_format_from_width(f.getsampwidth()),
channels = f.getnchannels(),
rate = f.getframerate(),
output = True)
data = f.readframes(self.CHUNK)
while data:
stream.write(data)
data = f.readframes(self.CHUNK)
stream.stop_stream()
stream.close()
def terminate(self):
"""
Terminate the beep handler (no beeps can be played after this).
"""
Log.debug(self._tag, "BeepHandler termianted")
self._pyaudio.terminate()