Trong hướng dẫn này, chúng ta sẽ xem cách thực hiện ghi âm giọng nói qua micrô trong thời gian tùy ý bằng PyAudio trong Python. PyAudio cung cấp các ràng buộc Python cho PortAudio, thư viện I/O âm thanh đa nền tảng. Bạn có thể dễ dàng sử dụng Python với PyAudio để phát và ghi âm thanh trên nhiều nền tảng khác nhau
Ứng dụng này không sử dụng giao diện người dùng hoặc giao diện người dùng để bắt đầu và dừng ghi âm
Để bắt đầu ghi âm, bạn chỉ cần phát tập lệnh Python và nói qua micrô để ghi âm giọng nói của mình bao lâu tùy thích
Để dừng quay bạn chỉ cần nhấn tổ hợp phím Ctrl+C từ bàn phím trong hệ điều hành Windows. Đối với hệ điều hành khác, bạn cần kiểm tra tổ hợp phím
Đầu ra cuối cùng được ghi vào đầu ra. tập tin wav. Bạn có thể phát đầu ra. wav để nghe bản ghi âm giọng nói của bạn
Theo dõi câu trả lời của Nick Fortescue, đây là một ví dụ đầy đủ hơn về cách ghi từ micrô và xử lý dữ liệu kết quả
from sys import byteorder
from array import array
from struct import pack
import pyaudio
import wave
THRESHOLD = 500
CHUNK_SIZE = 1024
FORMAT = pyaudio.paInt16
RATE = 44100
def is_silent[snd_data]:
"Returns 'True' if below the 'silent' threshold"
return max[snd_data] < THRESHOLD
def normalize[snd_data]:
"Average the volume out"
MAXIMUM = 16384
times = float[MAXIMUM]/max[abs[i] for i in snd_data]
r = array['h']
for i in snd_data:
r.append[int[i*times]]
return r
def trim[snd_data]:
"Trim the blank spots at the start and end"
def _trim[snd_data]:
snd_started = False
r = array['h']
for i in snd_data:
if not snd_started and abs[i]>THRESHOLD:
snd_started = True
r.append[i]
elif snd_started:
r.append[i]
return r
# Trim to the left
snd_data = _trim[snd_data]
# Trim to the right
snd_data.reverse[]
snd_data = _trim[snd_data]
snd_data.reverse[]
return snd_data
def add_silence[snd_data, seconds]:
"Add silence to the start and end of 'snd_data' of length 'seconds' [float]"
silence = [0] * int[seconds * RATE]
r = array['h', silence]
r.extend[snd_data]
r.extend[silence]
return r
def record[]:
"""
Record a word or words from the microphone and
return the data as an array of signed shorts.
Normalizes the audio, trims silence from the
start and end, and pads with 0.5 seconds of
blank sound to make sure VLC et al can play
it without getting chopped off.
"""
p = pyaudio.PyAudio[]
stream = p.open[format=FORMAT, channels=1, rate=RATE,
input=True, output=True,
frames_per_buffer=CHUNK_SIZE]
num_silent = 0
snd_started = False
r = array['h']
while 1:
# little endian, signed short
snd_data = array['h', stream.read[CHUNK_SIZE]]
if byteorder == 'big':
snd_data.byteswap[]
r.extend[snd_data]
silent = is_silent[snd_data]
if silent and snd_started:
num_silent += 1
elif not silent and not snd_started:
snd_started = True
if snd_started and num_silent > 30:
break
sample_width = p.get_sample_size[FORMAT]
stream.stop_stream[]
stream.close[]
p.terminate[]
r = normalize[r]
r = trim[r]
r = add_silence[r, 0.5]
return sample_width, r
def record_to_file[path]:
"Records from the microphone and outputs the resulting data to 'path'"
sample_width, data = record[]
data = pack['