Python

PyAudio库使用

2021-08-25  本文已影响0人  忘了呼吸的那只猫

PyAudio使用这个库可以进行录音,播放音频文件等等。
以下代码是PyAudio常用功能的实现

1.实现录音

import pyaudio
import wave
from tqdm import tqdm
def record_audio(wave_out_path,record_second):
  CHUNK = 1024
  FORMAT = pyaudio.paInt16
  CHANNELS = 2
  RATE = 44100
  p = pyaudio.PyAudio()
  stream = p.open(format=FORMAT,
          channels=CHANNELS,
          rate=RATE,
          input=True,
          frames_per_buffer=CHUNK)
  wf = wave.open(wave_out_path, 'wb')
  wf.setnchannels(CHANNELS)
  wf.setsampwidth(p.get_sample_size(FORMAT))
  wf.setframerate(RATE)
  print("* recording")
  for i in tqdm(range(0, int(RATE / CHUNK * record_second))):
    data = stream.read(CHUNK)
    wf.writeframes(data)
  print("* done recording")
  stream.stop_stream()
  stream.close()
  p.terminate()
  wf.close()
record_audio("output.wav",record_second=4)

2.实现播放音频文件

import pyaudio
import wave
from tqdm import tqdm
def play_audio(wave_path):
    CHUNK = 1024
    wf = wave.open(wave_path, 'rb')
    # instantiate PyAudio (1)
    p = pyaudio.PyAudio()
    # open stream (2)
    stream = p.open(format=p.get_format_from_width(wf.getsampwidth()),
          channels=wf.getnchannels(),
          rate=wf.getframerate(),
          output=True)
    # read data
    data = wf.readframes(CHUNK)
    # play stream (3)
    datas = []
    while len(data) > 0:
    data = wf.readframes(CHUNK)
    datas.append(data)
    for d in tqdm(datas):
    stream.write(d)
    # stop stream (4)
    stream.stop_stream()
    stream.close()
    # close PyAudio (5)
    p.terminate()
play_audio("test.wav")

pyaudio只能播放wav格式的音频,若是需要播放多格式的可以使用pydub库,可以支持多种格式的播放

3.实现回放

import pyaudio

CHUNK = 1024
WIDTH = 2
CHANNELS = 2
RATE = 44100
RECORD_SECONDS = 5

p = pyaudio.PyAudio()

stream = p.open(format=p.get_format_from_width(WIDTH),
                channels=CHANNELS,
                rate=RATE,
                input=True,
                output=True,
                frames_per_buffer=CHUNK)

print("* recording")

# for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
while True:
    data = stream.read(CHUNK)
    stream.write(data, CHUNK)

print("* done")

stream.stop_stream()
stream.close()

p.terminate()

4.以回调方法的方式播放一个音频文件

import pyaudio
import wave
from tqdm import tqdm
import time
def play_audio_callback(wave_path):
    CHUNK = 1024
    wf = wave.open(wave_path, 'rb')
    # instantiate PyAudio (1)
    p = pyaudio.PyAudio()
    def callback(in_data, frame_count, time_info, status):
        data = wf.readframes(frame_count)
        return (data, pyaudio.paContinue)
    # open stream (2)
    stream = p.open(format=p.get_format_from_width(wf.getsampwidth()),
                    channels=wf.getnchannels(),
                    rate=wf.getframerate(),
                    output=True,
                    stream_callback=callback)
    # read data
    stream.start_stream()
    while stream.is_active():
        time.sleep(2)
    # stop stream (4)
    stream.stop_stream()
    stream.close()
    # close PyAudio (5)
    p.terminate()
play_audio_callback("test.wav")

5.非阻塞回调

import pyaudio
import time

WIDTH = 2
CHANNELS = 2
RATE = 44100

p = pyaudio.PyAudio()

def callback(in_data, frame_count, time_info, status):
    return (in_data, pyaudio.paContinue)

stream = p.open(format=p.get_format_from_width(WIDTH),
                channels=CHANNELS,
                rate=RATE,
                input=True,
                output=True,
                stream_callback=callback)
print(pyaudio.paInt16)

stream.start_stream()

while stream.is_active():
    time.sleep(0.1)

stream.stop_stream()
stream.close()

p.terminate()
上一篇 下一篇

猜你喜欢

热点阅读