-
Notifications
You must be signed in to change notification settings - Fork 16
/
audio.py
executable file
·38 lines (31 loc) · 878 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
38
'''
Copied from http://stackoverflow.com/a/17657304/4398908
Play a wave sound file.
'''
import pyaudio
import wave
import os
audioPath = None # Path to audio files
def play_sound(name):
#define stream chunk
chunk = 1024
#open a wav format music
f = wave.open(os.path.join(audioPath, name + '.wav'),"rb")
#instantiate PyAudio
p = pyaudio.PyAudio()
#open stream
stream = p.open(format = p.get_format_from_width(f.getsampwidth()),
channels = f.getnchannels(),
rate = f.getframerate(),
output = True)
#read data
data = f.readframes(chunk)
#play stream
while data:
stream.write(data)
data = f.readframes(chunk)
#stop stream
stream.stop_stream()
stream.close()
#close PyAudio
p.terminate()