gpt4 book ai didi

python - 使用 wave 模块在 2GB WAV 文件中搜索 dropouts

转载 作者:太空宇宙 更新时间:2023-11-04 00:47:38 25 4
gpt4 key购买 nike

`使用 wave 模块分析 2GB WAV 文件(1khz 音调)音频丢失的最佳方法是什么?我尝试了下面的脚本

import wave
file1 = wave.open("testdropout.wav", "r")
file2 = open("silence.log", "w")
for i in xrange(file1.getnframes()):
frame = file1.readframes(i)

zero = True

for j in xrange(len(frame)):
# check if amplitude is greater than 0
# the ord() function converts the hex values to integers
if ord(frame[j]) > 0:
zero = False
break

if zero:
print >> file2, 'dropout at second %s' % (file1.tell()/file1.getframerate())
file1.close()
file2.close()

最佳答案

我以前没有使用过 wave 模块,但是 file1.readframes(i) 在第一帧时看起来像是在读取 1 帧,2当你在第二帧时是 10 帧,当你在第十帧时是 10 帧,而一个 2Gb CD 质量的文件可能有一百万帧 - 当你在帧 100,000 读取 100,000 帧时......越来越慢每次都通过循环吗?

根据我的评论,在 Python 2 中 range() 首先生成一个完整大小的内存数组,而 xrange() 不会,但不是完全使用 range 会更有帮助。

并使用 any() 将循环下推到较低层,以使代码更短,并可能更快:

import wave

file1 = wave.open("testdropout.wav", "r")
file2 = open("silence.log", "w")

chunksize = file1.getframerate()
chunk = file1.readframes(chunksize)

while chunk:
if not any(ord(sample) for sample in chunk):
print >> file2, 'dropout at second %s' % (file1.tell()/chunksize)

chunk = file1.readframes(chunksize)

file1.close()
file2.close()

这应该以 1 秒的 block 读取文件。

关于python - 使用 wave 模块在 2GB WAV 文件中搜索 dropouts,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38706926/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com