1、将pcm音频文件转为mp3、wav格式,以便在web浏览器播放音频
def pcm2mp3(pcm_bytes, save_path, channels=1, bits=16, sample_rate=16000):
"""
desc: 将pcm字节存储为mp3、wav文件
"""
import wave
import os
from pydub import AudioSegment
pcm_dir, pcm_name = os.path.split(save_path)
pcm_dir = os.path.dirname(save_path)
pcm_name = pcm_name.split('.')[0]
wave_path = os.path.join(pcm_dir, pcm_name + '.wav')
with wave.open(wave_path, 'wb') as wavfile:
# 设置声道数
wavfile.setnchannels(channels)
# 设置采样位宽
wavfile.setsampwidth(bits // 8)
# 设置采样率
wavfile.setframerate(sample_rate)
# 写入 data 部分
wavfile.writeframes(pcm_bytes)
mp3_path = os.path.join(pcm_dir, pcm_name + '.mp3')
wf = AudioSegment.from_wav(wave_path)
wf.export(mp3_path, format="mp3")
def pcm2mp3_fromfile(pcm_file, channels=1, bits=16, sample_rate=16000):
# https://www.modb.pro/db/533986
# 打开 PCM 文件
with open(pcm_file, 'rb') as f:
pcm_data = f.read()
# 打开将要写入的 WAVE 文件
if pcm_data is None:
return
pcm2mp3(pcm_data, pcm_file, channels, bits, sample_rate)
pcm2mp3_fromfile("./report/out.pcm")
2、录制音频,生成pcm文件
import pyaudio
import wave
import os
import numpy as np
def record_audio(filename, duration, sample_rate=16000, channels=1, chunk=1024):
audio_format = pyaudio.paInt16
audio = pyaudio.PyAudio()
stream = audio.open(format=audio_format,
channels=channels,
rate=sample_rate,
input=True,
frames_per_buffer=chunk)
frames = []
print("开始录制音频...")
for i in range(0, int(sample_rate / chunk * duration)):
data = stream.read(chunk)
frames.append(data)
print("录制完成!")
stream.stop_stream()
stream.close()
audio.terminate()
wf = wave.open(filename, 'wb')
wf.setnchannels(channels)
wf.setsampwidth(audio.get_sample_size(audio_format))
wf.setframerate(sample_rate)
wf.writeframes(b''.join(frames))
wf.close()
def wav2pcm(filename):
"""
desc: wav转为pcm
"""
# import soundfile as sf
pcm_file_path = filename.split('.')[0] + '.pcm'
with open(filename, 'rb') as f:
f.seek(0)
f.read(44)
data = np.fromfile(f, dtype=np.int16)
data.tofile(pcm_file_path)
def play_audio(filename):
"""
desc: 播放
"""
audio = pyaudio.PyAudio()
wf = wave.open(filename, 'rb')
stream = audio.open(format=audio.get_format_from_width(wf.getsampwidth()),
channels=wf.getnchannels(),
rate=wf.getframerate(),
output=True)
chunk = 1024
data = wf.readframes(chunk)
print("开始播放音频...")
while data:
stream.write(data)
data = wf.readframes(chunk)
print("播放完成!")
stream.stop_stream()
stream.close()
audio.terminate()
record_audio('test.wav', 5)
play_audio('test.wav')
wav2pcm('test.wav')
3、基于文本合成音频
def tts():
"""
desc: 语音合成
"""
import pyttsx3
engine = pyttsx3.init()
engine.say('今天天气怎么样')
engine.setProperty('rate', 125)
engine.setProperty('volume', 1.0)
engine.runAndWait()
engine.stop()
1179

被折叠的 条评论
为什么被折叠?



