-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpaTest.py
More file actions
47 lines (36 loc) · 1.08 KB
/
paTest.py
File metadata and controls
47 lines (36 loc) · 1.08 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
47
"""
PyAudio Example: Make a wire between input and output (i.e., record a
few samples and play them back immediately).
This is the callback (non-blocking) version.
"""
import pyaudio
import time
import wave
WIDTH = 2
CHANNELS = 2
RATE = 44100
CHUNK = 1024
wf = wave.open('440Hz.wav', 'rb')
print("Sample width [bytes]", wf.getsampwidth())
print("Num of channels", wf.getnchannels())
print("Frame rate", wf.getframerate())
p = pyaudio.PyAudio()
def callback(in_data, frame_count, time_info, status):
data = wf.readframes(frame_count)
print(frame_count, time_info, status)
if len(data) < frame_count*wf.getsampwidth():
wf.rewind()
data = wf.readframes(frame_count)
return (data, pyaudio.paContinue)
stream = p.open(format=p.get_format_from_width(wf.getsampwidth()),
channels=wf.getnchannels(),
rate=wf.getframerate(),
input=False,
output=True,
stream_callback=callback)
stream.start_stream()
while stream.is_active():
time.sleep(0.1)
stream.stop_stream()
stream.close()
p.terminate()