Python 写入WAV文件,并为WAV文件制作副本
我有一个关于编写WAV文件的问题。 以下是一些信息: 写入名为filename的WAV文件,该文件具有两个通道,两个字节的采样宽度, 帧速率由Python 写入WAV文件,并为WAV文件制作副本,python,wav,Python,Wav,我有一个关于编写WAV文件的问题。 以下是一些信息: 写入名为filename的WAV文件,该文件具有两个通道,两个字节的采样宽度, 帧速率由framerate给定,带有len(sound)样本、压缩类型“NONE”和压缩 名称“未压缩”' 这是我的密码: import wave import struct def loadstereowav(filename): w = wave.open(filename, 'r') nchannels, sampwidth, framer
framerate
给定,带有len(sound
)样本、压缩类型“NONE
”和压缩
名称“未压缩”
'
这是我的密码:
import wave
import struct
def loadstereowav(filename):
w = wave.open(filename, 'r')
nchannels, sampwidth, framerate, nframes, comptype, compname = w.getparams()
assert nchannels == 2 and sampwidth == 2 and comptype == 'NONE'
frames = w.readframes(nframes * nchannels)
sound = struct.unpack_from((str(nframes)+'h') * nchannels, frames)
w.close()
return framerate, sound
def savestereowav(filename, framerate, sound):
w1 = wave.open(filename, "w")
for i in range(0, len(sound)):
w1.write(struct.pack('>h', framerate))
以下是一些测试用例:
>>> rate, sound = loadstereowav('love.wav')
>>> savestereowav('love_copy.wav', rate, sound)
我需要编写一个savestereowav(文件名、帧速率、声音)
函数来加载和保存文件应该会产生一个副本
例如,加载一个“love.wav”并使用“love\u copy.wav”进行复制
但是,我不知道如何处理WAV文件
有人帮我解答这个问题吗?如果您需要复制文件而不进行修改(相同的
nchannels
、相同的sampwidth
,等等),您可以复制文件而不必分析/理解其内容:shutils.copyfile()
应该可以
另一方面,如果需要转换数据(例如更改采样率、通道数等),则需要两件事:
wav
文件格式。在网上搜索“wav文件格式”(例如)。wave
模块仅提供readframes()
方法,但您必须手动管理数据由于您在
savestereowav
函数中包含了一个framerate
参数,因此我假设您想要重新采样。但是在您的测试用例中,您使用相同的速率
,因为我可以看到您已经拥有了它。。。这只是一个小小的调整。
加载WAV文件时,首先打开它并检索所有可用参数。你可以决定是否使用它们。实际上,您只需要前四个,即通道数(单声道1个,立体声2个)、采样率、采样宽度和帧数。因为这段代码中的音轨必须是立体声的,否则就属于断言的情况,所以必须读取每个声道的帧(立体声的左声道和右声道)。
声音是在你解压成字节然后关闭后合成的。
现在,您拥有克隆或复制WAV文件所需的所有信息
然后,剩下的是相反的过程,您创建一个新的WAV文件,从原始曲目设置相同的参数,并且必须通过pack函数逐帧写入
import wave, struct
def loadstereowav(filename):
w = wave.open(filename, 'r')
nchannels, sample_width, sample_rate, nframes, comptype, compname = w.getparams()
assert nchannels == 2 and sample_width == 2 and comptype == 'NONE'
frames = w.readframes(nframes * nchannels)
sound = struct.unpack_from((str(nframes)+'h') * nchannels, frames)
w.close()
return sample_rate, nchannels, sample_width, sound
def savestereowav(filename, sample_rate, n_channels, sample_width, sound):
# Add file parameters
w1 = wave.open(filename, "w")
w1.setnchannels(n_channels)
w1.setsampwidth(sample_width)
w1.setframerate(sample_rate)
for i in range(0, len(sound)):
w1.writeframesraw(struct.pack('<h', sound[i])) ## Copy each frame
srate, channels, swidth, sound = loadstereowav('Hit.wav')
savestereowav('Hit_copy.wav', srate, channels, swidth, sound)
import wave,struct
def loadstereowav(文件名):
w=wave.open(文件名'r')
nchannels,采样宽度,采样率,n帧,comptype,compname=w.getparams()
断言nchannels==2,sample_width==2,comptype==NONE
frames=w.readframes(nframes*nchannels)
声音=结构从((str(nframes)+'h')*nchannels,frames)解包
w、 关闭()
返回采样率、通道、采样宽度、声音
def SAVESTEROWAV(文件名、采样率、n个通道、采样宽度、声音):
#添加文件参数
w1=wave.open(文件名,“w”)
w1.设置通道(n_通道)
w1.设置采样宽度(采样宽度)
w1.设置帧率(采样率)
对于范围(0,len(声音))中的i:
w1.writeframesraw(struct.pack(“您好!虽然这段代码可能会解决问题,但它如何以及为什么会解决问题将真正有助于提高您的帖子质量,并可能导致更多的投票。请记住,您是在将来为读者回答问题,而不仅仅是现在提问的人。请您的回答添加解释并给出提示适用的限制和假设。