-
Notifications
You must be signed in to change notification settings - Fork 0
/
audio.py
37 lines (32 loc) · 1016 Bytes
/
audio.py
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
#conda install -c anaconda pyaudio
import pyaudio, wave, sys
import numpy as np
class AudioFile:
chunk = 1024
def __init__(self, file):
""" Init audio stream """
self.wf = wave.open(file, 'rb')
self.p = pyaudio.PyAudio()
self.stream = self.p.open(
format = self.p.get_format_from_width(self.wf.getsampwidth()),
channels = self.wf.getnchannels(),
rate = self.wf.getframerate(),
output = True
)
def play(self):
""" Play entire file """
frames = self.wf.readframes(self.chunk)
while frames != '':
amp = np.fromstring(frames, np.int16)
amp = amp / 32768.0
print(np.average(amp))
self.stream.write(frames)
frames = self.wf.readframes(self.chunk)
def close(self):
""" Graceful shutdown """
self.stream.close()
self.p.terminate()
# Usage example for pyaudio
a = AudioFile("taunt.wav")
a.play()
a.close()