Skip to content

Commit 36019b6

Browse files
author
xuming06
committed
add pyaudio
1 parent feb6ae7 commit 36019b6

7 files changed

Lines changed: 205 additions & 1 deletion

25speech/data/16k.wav

127 KB
Binary file not shown.
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
@author:XuMing([email protected])
4+
@description:
5+
"""
6+
7+
"""
8+
PyAudio Example: Make a wire between input and output (i.e., record a
9+
few samples and play them back immediately).
10+
11+
This is the callback (non-blocking) version.
12+
"""
13+
14+
import pyaudio
15+
import time
16+
17+
WIDTH = 2
18+
CHANNELS = 2
19+
RATE = 44100
20+
21+
p = pyaudio.PyAudio()
22+
23+
def callback(in_data, frame_count, time_info, status):
24+
return (in_data, pyaudio.paContinue)
25+
26+
stream = p.open(format=p.get_format_from_width(WIDTH),
27+
channels=CHANNELS,
28+
rate=RATE,
29+
input=True,
30+
output=True,
31+
stream_callback=callback)
32+
33+
stream.start_stream()
34+
35+
while stream.is_active():
36+
time.sleep(1.0)
37+
38+
stream.stop_stream()
39+
stream.close()
40+
41+
p.terminate()

25speech/pyaudio_make_play_demo.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
@author:XuMing([email protected])
4+
@description:
5+
"""
6+
7+
"""
8+
PyAudio Example: Make a wire between input and output (i.e., record a
9+
few samples and play them back immediately).
10+
"""
11+
12+
import pyaudio
13+
14+
CHUNK = 1024
15+
WIDTH = 2
16+
CHANNELS = 2
17+
RATE = 44100
18+
RECORD_SECONDS = 5
19+
20+
p = pyaudio.PyAudio()
21+
22+
stream = p.open(format=p.get_format_from_width(WIDTH),
23+
channels=CHANNELS,
24+
rate=RATE,
25+
input=True,
26+
output=True,
27+
frames_per_buffer=CHUNK)
28+
29+
print("* recording")
30+
31+
for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
32+
data = stream.read(CHUNK)
33+
stream.write(data, CHUNK)
34+
35+
print("* done")
36+
37+
stream.stop_stream()
38+
stream.close()
39+
40+
p.terminate()
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
@author:XuMing([email protected])
4+
@description:
5+
"""
6+
7+
"""PyAudio Example: Play a wave file (callback version)"""
8+
9+
import pyaudio
10+
import wave
11+
import time
12+
import sys
13+
14+
file_path = './data/16k.wav'
15+
wf = wave.open(file_path, 'rb')
16+
17+
p = pyaudio.PyAudio()
18+
19+
def callback(in_data, frame_count, time_info, status):
20+
data = wf.readframes(frame_count)
21+
return (data, pyaudio.paContinue)
22+
23+
stream = p.open(format=p.get_format_from_width(wf.getsampwidth()),
24+
channels=wf.getnchannels(),
25+
rate=wf.getframerate(),
26+
output=True,
27+
stream_callback=callback)
28+
29+
stream.start_stream()
30+
31+
while stream.is_active():
32+
time.sleep(0.1)
33+
34+
stream.stop_stream()
35+
stream.close()
36+
wf.close()
37+
38+
p.terminate()

25speech/pyaudio_play_demo.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
@author:XuMing([email protected])
4+
@description:
5+
"""
6+
7+
"""PyAudio Example: Play a WAVE file."""
8+
9+
import pyaudio
10+
import wave
11+
import sys
12+
13+
CHUNK = 1024
14+
15+
if len(sys.argv) < 2:
16+
print("Plays a wave file.\n\nUsage: %s filename.wav" % sys.argv[0])
17+
sys.exit(-1)
18+
19+
wf = wave.open(sys.argv[1], 'rb')
20+
21+
p = pyaudio.PyAudio()
22+
23+
stream = p.open(format=p.get_format_from_width(wf.getsampwidth()),
24+
channels=wf.getnchannels(),
25+
rate=wf.getframerate(),
26+
output=True)
27+
28+
data = wf.readframes(CHUNK)
29+
30+
while data != '':
31+
stream.write(data)
32+
data = wf.readframes(CHUNK)
33+
34+
stream.stop_stream()
35+
stream.close()
36+
37+
p.terminate()

25speech/pyaudio_record_demo.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
@author:XuMing([email protected])
4+
@description:
5+
"""
6+
7+
"""PyAudio example: Record a few seconds of audio and save to a WAVE file."""
8+
9+
import pyaudio
10+
import wave
11+
12+
CHUNK = 1024
13+
FORMAT = pyaudio.paInt16
14+
CHANNELS = 2
15+
RATE = 44100
16+
RECORD_SECONDS = 5
17+
WAVE_OUTPUT_FILENAME = "output.wav"
18+
19+
p = pyaudio.PyAudio()
20+
21+
stream = p.open(format=FORMAT,
22+
channels=CHANNELS,
23+
rate=RATE,
24+
input=True,
25+
frames_per_buffer=CHUNK)
26+
27+
print("* recording")
28+
29+
frames = []
30+
31+
for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
32+
data = stream.read(CHUNK)
33+
frames.append(data)
34+
35+
print("* done recording")
36+
37+
stream.stop_stream()
38+
stream.close()
39+
p.terminate()
40+
41+
wf = wave.open(WAVE_OUTPUT_FILENAME, 'wb')
42+
wf.setnchannels(CHANNELS)
43+
wf.setsampwidth(p.get_sample_size(FORMAT))
44+
wf.setframerate(RATE)
45+
wf.writeframes(b''.join(frames))
46+
wf.close()

requirements.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,6 @@ scikit-learn
77
pypinyin
88
tensorflow
99
click
10-
xlearn
10+
xlearn
11+
wave
12+
pyaudio

0 commit comments

Comments
 (0)