Python 播放音频与录音 !

作者: 14e61d025165 | 来源:发表于2019-07-03 15:59 被阅读0次

音频预处理

这一讲主要介绍些音频基本处理方式,为接下来的语音识别打基础。

三种播放音频的方式

使用 python 播放音频有以下几种方式:

os.system()

os.system(file) 调用系统应用来打开文件,file 可为图片或者音频文件。

缺点:要打开具体的应用,不能在后台播放音频。

pyaudio

Python学习交流群:1004391443

安装:pip install pyaudio

官方提供了播放音频与录音的 api ,使用十分方便,只要把Filename更改为你的音频文件的文字,就可以播放音频了。

<pre spellcheck="false" style="box-sizing: border-box; margin: 5px 0px; padding: 5px 10px; border: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-weight: 400; font-stretch: inherit; font-size: 16px; line-height: inherit; font-family: inherit; vertical-align: baseline; cursor: text; counter-reset: list-1 0 list-2 0 list-3 0 list-4 0 list-5 0 list-6 0 list-7 0 list-8 0 list-9 0; background-color: rgb(240, 240, 240); border-radius: 3px; white-space: pre-wrap; color: rgb(34, 34, 34); letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">"""PyAudio Example: Play a WAVE file."""
import pyaudio
import wave
CHUNK = 1024
FILENAME = '你的音频文件'
def play(filename = FILENAME):
wf = wave.open(filename, 'rb')
p = pyaudio.PyAudio()
stream = p.open(format=p.get_format_from_width(wf.getsampwidth()),
channels=wf.getnchannels(),
rate=wf.getframerate(),
output=True)
data = wf.readframes(CHUNK)
while data != b'':
stream.write(data)
data = wf.readframes(CHUNK)
stream.stop_stream()
stream.close()
p.terminate()
</pre>

jupyter notebook

在 jupyer notebook 中播放音频可以使用以下函数:

<pre spellcheck="false" style="box-sizing: border-box; margin: 5px 0px; padding: 5px 10px; border: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-weight: 400; font-stretch: inherit; font-size: 16px; line-height: inherit; font-family: inherit; vertical-align: baseline; cursor: text; counter-reset: list-1 0 list-2 0 list-3 0 list-4 0 list-5 0 list-6 0 list-7 0 list-8 0 list-9 0; background-color: rgb(240, 240, 240); border-radius: 3px; white-space: pre-wrap; color: rgb(34, 34, 34); letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">import IPython.display as ipd
ipd.Audio(文件名)
</pre>

几种读取音频的方式

python 有很多读取音频文件的方法,内置的库 wave ,科学计算库 scipy, 和方便易用的语音处理库 librosa。

下面将介绍分别使用这几种库读取音频文件:

安装:

wave 是内置库直接导入即可。

scipy: pip install scipy

librosa: pip install librosa

使用:

wave.open:

参数 path 为文件名,mode 为打开方式

以'rb'方式打开文件返回一个 Wave_read 对象,而以'wb'方式打开文件返回一个 Wave_write 对象。

scipy.io.wavfile:

参数 path 为文件名

返回 rate : 采样率(每秒采样点的个数),data : 音频数据

librosa.load:

参数 path 为文件名

返回 y 为音频数据,sr 为采样率

<pre spellcheck="false" style="box-sizing: border-box; margin: 5px 0px; padding: 5px 10px; border: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-weight: 400; font-stretch: inherit; font-size: 16px; line-height: inherit; font-family: inherit; vertical-align: baseline; cursor: text; counter-reset: list-1 0 list-2 0 list-3 0 list-4 0 list-5 0 list-6 0 list-7 0 list-8 0 list-9 0; background-color: rgb(240, 240, 240); border-radius: 3px; white-space: pre-wrap; color: rgb(34, 34, 34); letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"># read wav file from path
from scipy.io import wavfile
import librosa
import pyaudio

wave

file = wave.open(path,'rb')

wavfile

rate, data = wavfile.read(path)

librosa

y, sr = librosa.load(path)
</pre>

下面演示一个使用 wavfile 读取音频文件并且画出波形的例子:

首先要计算音频到底持续了多长时间,wave 的 shape 就是总的采样点个数,除以采样频率可以得到持续的总时间(秒),乘1000得到总持续时间(毫秒)。接着通过 np.linsapce 产生时间的序列,最后使用 matplotlib 画出图像。

<pre spellcheck="false" style="box-sizing: border-box; margin: 5px 0px; padding: 5px 10px; border: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-weight: 400; font-stretch: inherit; font-size: 16px; line-height: inherit; font-family: inherit; vertical-align: baseline; cursor: text; counter-reset: list-1 0 list-2 0 list-3 0 list-4 0 list-5 0 list-6 0 list-7 0 list-8 0 list-9 0; background-color: rgb(240, 240, 240); border-radius: 3px; white-space: pre-wrap; color: rgb(34, 34, 34); letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">from scipy.io import wavfile
import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline

一秒采样数

sr, wave = wavfile.read('D://QQPCMgr/Desktop/python3/skip.wav')
sample_number = wave.shape[0]
total_time = int(sample_number / sr * 1000)
time_series = np.linspace(0,total_time,sample_number)
fig, ax = plt.subplots(1, 1)
ax.plot(time_series, wave)
ax.set_title('Time*Amplitude')
ax.set_xlabel('Time/ms')
ax.set_ylabel('Amplitude/dB')
</pre>

<tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1562140724926 ql-align-center" data-render-status="finished" data-syl-blot="image" style="box-sizing: border-box; cursor: text; text-align: left; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;"> image

<input class="pgc-img-caption-ipt" placeholder="图片描述(最多50字)" value="" style="box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;"></tt-image>

最后再借用 pyaudio 的 api 我们可以实现连续录音功能:

python 实现录音功能

其中,函数 multi_record每结束一次录音会询问 “是否进行下一次录音?”,按回车就可以进行下一次录音了。

<pre spellcheck="false" style="box-sizing: border-box; margin: 5px 0px; padding: 5px 10px; border: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-weight: 400; font-stretch: inherit; font-size: 16px; line-height: inherit; font-family: inherit; vertical-align: baseline; cursor: text; counter-reset: list-1 0 list-2 0 list-3 0 list-4 0 list-5 0 list-6 0 list-7 0 list-8 0 list-9 0; background-color: rgb(240, 240, 240); border-radius: 3px; white-space: pre-wrap; color: rgb(34, 34, 34); letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">import wave
import pyaudio
import matplotlib.pyplot as plt
import time
CHUNK = 1024
FORMAT = pyaudio.paInt16
CHANNELS = 2
RATE = 44100
RECORD_SECONDS = 5
def record(filename='output.wav'):
"""官方录音教程
"""

p = pyaudio.PyAudio()

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

print("* recording")

frames = []
for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
data = stream.read(CHUNK)
frames.append(data)

print("* done recording")

stream.stop_stream()
stream.close()
p.terminate()

wf = wave.open(filename, 'wb')
wf.setnchannels(CHANNELS)
wf.setsampwidth(p.get_sample_size(FORMAT))
wf.setframerate(RATE)
wf.writeframes(b''.join(frames))
wf.close()
def multi_record(num=3):
"""implement 多次录音"""
for i in range(1,num+1):
print('第{}次录音准备'.format(i))
filename = 'record_{}.wav'.format(i)
record(filename)
time.sleep(second)
_ = input('进行下一次录音?')
def main():
multi_record()
if name == 'main':
</pre>

相关文章

  • Python 播放音频与录音 !

    音频预处理 这一讲主要介绍些音频基本处理方式,为接下来的语音识别打基础。 三种播放音频的方式 使用 python ...

  • Python 播放音频与录音

    音频预处理 这一讲主要介绍些音频基本处理方式,为接下来的语音识别打基础。 三种播放音频的方式 使用 python ...

  • python 播放音频与录音

    音频预处理 这一讲主要介绍些音频基本处理方式,为接下来的语音识别打基础。 三种播放音频的方式 使用 python ...

  • python 播放音频与录音

    音频预处理 这一讲主要介绍些音频基本处理方式,为接下来的语音识别打基础。 三种播放音频的方式 使用 python ...

  • Android 音频录音与播放

    介绍音频的采集、编码、生成文件、转码等操作,通过 AudioRecord 采集音频,生成三种格式的文件格式(pcm...

  • 利用Python进行录音和音频分析

    pyaudio简介 Python有个很强大的处理音频的库pyqudio, 使用pyaudio库可以进行录音,播放,...

  • IOS学习(14)-多媒体

    iOS开发系列--音频播放、录音、视频播放、拍照、视频录制

  • Qt 录音播放控件

    Qt 录音播放控件 [TOC] 功能 录音(自动保存WAV文件) 播放音频文件 音频频谱显示 背景色,频谱色可调 ...

  • iOS开发系列--音频播放、录音、视频播放、拍照、视频录制

    原文 :iOS开发系列--音频播放、录音、视频播放、拍照、视频录制

  • 慢放器

    功能 打开文件架读取音频 音频播放, 视频播放 音频变速 循环播放 设置AB点(起始和结束) 开启节拍器 录音 页...

网友评论

    本文标题:Python 播放音频与录音 !

    本文链接:https://www.haomeiwen.com/subject/xbqchctx.html