wave 模块让用户读写、分析及创建 WAVE(.wav)文件。可以使用 wave 模块的 open() 方法打开旧文件或创建新文件。其语法格式如下:
open(file [, mode])
其中,file 是 WAVE 文件名称;mode 可以是 r 或 rb,表示只读模式,返回一个 Wave_read 对象;也可以是 w 或 wb,表示只写模式,返回一个 Wave_write 对象。
- 只读模式Wave_read对象方法
下表 1 是 Wave_read 对象的方法列表:
表1:Wave_read对象的方法列表
方法
说明
getnchannels()
返回声道数目。1是单声道,2是双声道
getsampwidth()
返回样本字节宽度(以字节为单位
[En]
Returns the sample byte width in bytes
getframerate()
返回取样频率.
getnframes()
返回帧的数目
getcomptype()
返回压缩类型。返回None表示线性样本
getcompname()
返回可读的压缩类型
getparams()
返回-一个元组: (nchannels, sampwidth, framerate, nframes, comptype, compname)
getmarkers()
返回None。 此方法用来与aifc模块兼容
getmark(id)
抛出一个异常,因为此mark不存在。此方法用来与aifc模块兼容
readframes(n)
返回n个帧的语音数据
rewind()
倒转至语音串流的开头
setpos(pos)
移到pos位置
tell()
返回目前的位置
close()
关闭语音串流
- 只写模式WAVE_write对象方法
下表 2 是Wave_write对象的方法列表:
表2:Wave_write对象的方法列表
方法
说明
setnchannels()
设置声道的数目
setsampwidth(n)
设置样本宽度
setframerate(n)
设置取样频率
setnframes(n)
设置帧的数目
setcomptype(type, name)
设置压缩类型和可读性压缩类型
[En]
Set compression type and readable compression type
setparams()
设置一 个元组: (nchannels, sampwidth, framerate, nframes, comptype, compname )
tell()
返回目前的位置
writeframesraw( data)
写入语音帧,但不写入文件头
[En]
Write voice frame, but no file header
writeframes( data)
写入语音帧及文件表头
close()
写入文件头并关闭语音流
[En]
Write to the file header and turn off voice streaming
- wave模块实例应用
下面的示例创建一个按钮来打开语音文件,并显示该语音文件的格式。
[En]
The following example creates a button to open a voice file and displays the format of the voice file.
使用wave模块
from tkinter import *
import tkinter. filedialog, wave
创建应用程序的类
class App:
def init (self, master) :
创建一个Label控件
self. label = Label (master, text=”语音文件: “)
self. label .pack (anchor=W)
创建一个Button控件
self .button = Button (master, text=”打开语音文件”, command=self.openSoundFile)
self. button. pack (anchor=CENTER)
设置对话框打开的文件类型
self.myFileTypes = [ (‘WAVE format’, ‘*.wav’) ]
创建一个[打开旧文件]对话框
self .myDialog = tkinter.filedialog.open(master, filetypes=self .myFileTypes)
打开语音文件
def openSoundFile(self) :
返回打开的语音文件名
infile = self.myDialog.show ()
显示该语音文件的格式
self.getWaveFormat(infile)
def getwaveFormat (self, infile):
读取语音文件的格式
audio = wave.open (infile, “r”)
txt = “语音文件: ” + infile + “\n”+ “Channels: ” + str (audio. getnchannels()) + “\n”+ \
“Sample width: ” + str (audio. getsampwidth()) + “\n”+ “Frame rate: ” + str (audio. getframerate()) + “\n” + \
“Compression type: ” + str (audio. getcomptype()) + “\n”+ “Compression name: ” + str (audio. getcompname () )
self. label . config(text = txt)
创建主窗口
win = Tk()
win. title(string = “处理声音”)
创建应用程序类的实例变量
app1= App(win)
开始程序循环
win.mainloop ()
保存并运行程序 1.py,结果如图 1 所示,单击”打开语音文件”按钮,在打开的对话框中选择wav格式的文件,即可查看文件的信息。
图1:程序运行结果
- aifc模块介绍
aifc(Audio Interchange File Format)模块用于存取 AIFF 与 AIFC 格式的语音文件。aifc 模块的函数与 wave 模块的函数大致相同。
下面的示例是使用 aifc 模块创建一个新的 AIFC 语音文件。
import aifc
创建-一个新语音文件
stream = aifc. open (“D: \test.aifc”, “w”)
声道数为2
stream. setnchannels (2)
样本宽度为2
stream. setsampwidth (2)
每一秒22050个帧
stream. setframerate (22050)
写入表头以及语音串流
stream. writeframes (b”1 43456787654321″,20000)
关闭文件
stream. close ()
Original: https://blog.csdn.net/weixin_42188512/article/details/111902667
Author: 黄宇韬
Title: python wave_Python wave模块:处理WAVE音频文件
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/525099/
转载文章受原作者版权保护。转载请注明原作者出处!