处理音频--pyaudio,,前言安装读写音频文件


前言安装读写音频文件playrecordwiredplaycallbackWirecallback外部应用总结

前言

很久之前写过一个将文本转成语音的,借助了一个名为pyttsx的库。具体使用可以参考下面的链接。

http://blog.csdn.net/marksinoberg/article/details/52137547

今天再来分享一个处理音频的博文。接住百度的语音接口,差不多可以方便的将音频转成文字了。

安装

安装的过程比较麻烦一点,不是说安装的步骤,而是找到能用的库不是很容易。

目标库: pyaudio。

但是奈何我的Python版本是36,而pip是安装不了的。找了很多资料,最后还是在pypi上找到了兼容的版本。

Python36版本: https://pypi.python.org/pypi/PyAudio/0.2.11

Python35 Python2:http://people.csail.mit.edu/hubert/pyaudio/packages/

读写音频文件

官网上给了几个小例子,个人觉得不错。拿来分享一下。

play

"""PyAudio Example: Play a WAVE file."""import pyaudioimport waveimport sysCHUNK = 1024if len(sys.argv) < 2:    print("Plays a wave file.\n\nUsage: %s filename.wav" % sys.argv[0])    sys.exit(-1)wf = wave.open(sys.argv[1], ‘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 != ‘‘:    stream.write(data)    data = wf.readframes(CHUNK)stream.stop_stream()stream.close()p.terminate()

保存为play.py
然后可以再terminal中来尝试一下。当然了,前提是先准备一个 .wav音频文件。

Python play.py 3.12.wav

然后不出意外的话,就可以听到电脑播放的音频了。

record

有了读的,那么再来个记录的吧。

"""PyAudio example: Record a few seconds of audio and save to a WAVE file."""import pyaudioimport waveCHUNK = 1024FORMAT = pyaudio.paInt16CHANNELS = 2RATE = 44100RECORD_SECONDS = 5WAVE_OUTPUT_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(WAVE_OUTPUT_FILENAME, ‘wb‘)wf.setnchannels(CHANNELS)wf.setsampwidth(p.get_sample_size(FORMAT))wf.setframerate(RATE)wf.writeframes(b‘‘.join(frames))wf.close()

保存为record.py
然后运行下面的命令。

Python record.py

代码中声明的是5秒的记录时长,这一点可以根据自己的需要来进行动态的修改。然后程序运行结束之后,就会在同一级目录下得到一个output.wav 的音频文件。

wired

刚才那俩小例子要么一个读,要么一个记录。那么要是既想读,然后再看下结果的需求呢?可以这么来实现。

"""PyAudio Example: Make a wire between input and output (i.e., record afew samples and play them back immediately)."""import pyaudioCHUNK = 1024WIDTH = 2CHANNELS = 2RATE = 44100RECORD_SECONDS = 5p = 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)):    data = stream.read(CHUNK)    stream.write(data, CHUNK)print("* done")stream.stop_stream()stream.close()p.terminate()

保存为wire.py
然后运行下面的命令

Python wire.py

就可以记录一个5秒的实现了。

play(callback)

搞定了上面三个小例子,做出自己的东西也不是什么难事了。基本上可以满足自己的需求。但是官网上还给了更加优雅的方式,那就是使用回调函数。除此之外很重要的一点就是callback方式是noblocking的。
官网的api解释如下:

Note that in “blocking mode”, each pyaudio.Stream.write() or pyaudio.Stream.read() blocks until all the given/requested frames have been played/recorded. Alternatively, to generate audio data on the fly or immediately process recorded audio data, use the “callback mode” outlined below.
"""PyAudio Example: Make a wire between input and output (i.e., record afew samples and play them back immediately)."""import pyaudioCHUNK = 1024WIDTH = 2CHANNELS = 2RATE = 44100RECORD_SECONDS = 5p = 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)):    data = stream.read(CHUNK)    stream.write(data, CHUNK)print("* done")stream.stop_stream()stream.close()p.terminate()

其实也没啥特殊的地方了,就是代码看起来更加精简了。

Wire(callback)

"""PyAudio Example: Make a wire between input and output (i.e., record afew samples and play them back immediately).This is the callback (non-blocking) version."""import pyaudioimport timeWIDTH = 2CHANNELS = 2RATE = 44100p = 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)stream.start_stream()while stream.is_active():    time.sleep(0.1)stream.stop_stream()stream.close()p.terminate()License

外部应用

下面拿一个小例子入手,实时的测试一下音频转文字。因为本人没有百度语音服务的权限,所以在网上搜索了一个key。在这里感谢下面的这个链接。

https://github.com/luyishisi/python_yuyinduihua

话不多说, 上例子吧。

# coding: utf8# @Author: 郭 璞# @File: baiduyuyinshibie.py                                                                 # @Time: 2017/5/10                                   # @Contact: 1064319632@qq.com# @blog: http://blog.csdn.net/marksinoberg# @Description: 百度语音识别接口调用import waveimport requestsimport jsondef get_token():    apiKey = "。。。GBOtpg22ZSGAU"    secretKey = "44。。。e34936227d4a19dc2"    auth_url = "https://openapi.baidu.com/oauth/2.0/token?grant_type=client_credentials&client_id=" + apiKey + "&client_secret=" + secretKey    response = requests.get(url=auth_url)    jsondata = response.text    return json.loads(jsondata)[‘access_token‘]def use_cloud(token, wavefile):    fp = wave.open(wavefile, ‘rb‘)    # 已经录好音的音频片段内容    nframes = fp.getnframes()    filelength = nframes*2    audiodata = fp.readframes(nframes)    # 百度语音接口的产品ID    cuid = ‘71XXXX663‘    server_url = ‘http://vop.baidu.com/server_api‘ + ‘?cuid={}&token={}‘.format(cuid, token)    headers = {        ‘Content-Type‘: ‘audio/pcm; rete=8000‘,        ‘Content-Length‘: ‘{}‘.format(filelength),    }    response = requests.post(url=server_url, headers=headers, data=audiodata)    return response.text if response.status_code==200 else ‘Something Wrong!‘if __name__ == ‘__main__‘:    access_token = get_token()    print(access_token)    result = use_cloud(token=access_token, wavefile=‘./output.wav‘)    print(result)

音频文件借助了上面第二个例子中录制的音频。

我说的话是: 345# 由于在图书馆,所以不敢太大声,要是用标准的普通话,相信准确度可能会更高一点。

然后运行的结果就是下面这样的了。
技术分享

可以看出请求成功,返回的结果里面包含了相应的文本内容。虽然不是很准确,但是也还算可以吧。

总结

最后来总结一下,今天貌似净拿人家的东西了,虽然自己整合了一下。但是还远远不够啊。其实结合这个语音接口可以做出很多更加好玩的功能的。


参考链接:

聊天机器人

GitHub机器人聊天参考源码

pyaudio官方文档

pyaudio官网

PyPI宝库

处理音频--pyaudio

评论关闭