简体   繁体   English

在python中分割wav文件

[英]splitting wav file in python

I'm trying to split a wav file programmatically in Python. 我正在尝试在Python中以编程方式拆分wav文件。 Based on hints from stackoverflow as well as the documentation from the Python wave module I'm doing the following 基于stackoverflow的提示以及Python wave模块的文档,我正在执行以下操作

import wave

origAudio = wave.open('inputFile.wav','r')
frameRate = origAudio.getframerate()
nChannels = origAudio.getnchannels()
sampWidth = origAudio.getsampwidth()

start = float(someStartVal)
end = float(someEndVal)

origAudio.setpos(start*frameRate)
chunkData = origAudio.readframes(int((end-start)*frameRate))

chunkAudio = wave.open('outputFile.wav','w')
chunkAudio.setnchannels(nChannels)
chunkAudio.setsampwidth(sampWidth)
chunkAudio.setframerate(frameRate)
chunkAudio.writeframes(chunkData)
chunkAudio.close()

I iterate through a number of different start and end values, and extract chunks of audio from the original file in this manner. 我遍历许多不同的开始和结束值,并以这种方式从原始文件中提取音频块。 What's weird is that the technique works perfectly fine for some chunks, and produces garbage white noise for others. 奇怪的是,该技术对某些块效果很好,而对其他块则产生垃圾白噪声。 Also there's no obvious pattern of which start and end positions produce white noise, just that it happens consistently for an input file. 同样,也没有明显的模式表明开始位置和结束位置会产生白噪声,只是在输入文件中始终会发生白噪声。

Anyone experienced this sort of behaviour before? 有人曾经经历过这种行为吗? Or know what I'm doing wrong? 还是知道我在做什么错? Suggestions on better ways of splitting an audio file programmatically are welcome. 欢迎以编程方式分割音频文件的更好方法的建议。

Thanks in advance. 提前致谢。

This may have to do with start*frameRate being a float when calling setpos . 调用setpos时,这可能与start*frameRate为浮点数有关 Perhaps after readframes you should use tell to find the current location of the file pointer instead.. 也许在阅读框架之后,您应该使用tell来查找文件指针的当前位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM