在这里编写newb尝试首次使用音频(想要制作一个以特定分贝级别切割波形文件的程序)
wf = wave.open('test.wav', 'r')
wfArraydB = []
for i in range(0, wf.getnframes()): # For every frame/sample, this loop is performed.
waveData = wf.readframes(1) # Can only do 1 frame because struct.unpack can only do 1 frame
data = struct.unpack("<h", waveData) # Converts wave data from hexadecimal
a = wf.tell() # Returns the frame that the file pointer is at
cf = copysign(data[0], 1) # Converts wave data
#print("data[0]: %d, cf: %d, cf/32767: %d" % (data[0], cf, cf/32767))
if data[0] != 0: # Eliminates a math module error if the wave sample value is 0
b = 20 * log10(cf/32767) # Converts non-hexadecimal wave data to dB format
else:
b = -90
#print("frame %d: %d dB" % (a, b))
wfArraydB.append(int(b))
acceptableLvl = []
acceptableLvlPre = []
threshold = -40
for i in range(0, len(wfArraydB)):
if wfArraydB[i] > threshold:
acceptableLvlPre.append(i)
if wfArraydB[i] < threshold:
if len(acceptableLvlPre) > 4410: # If sequence of frames over -40 is longer than 4410 samples or 0.1s
acceptableLvl.append(acceptableLvlPre) # Stores frames in which there is sufficiently loud sound
acceptableLvlPre[:] = [] # Remove all contents from pre-list (using slice assignment)
print(acceptableLvl)
当我进行打印(acceptedLvl)时,我希望看到一个列表列表,它们有两个要求:1。所有值都超过-40(dB,使用音频文件),和2所有列表的长度都超过4410个样本(0.1秒)。
相反,返回的列表包含13个相同列表的重复,只包含12个索引值(这些值都接近~2秒声音文件@ 44,100 hz的末尾)
[[85368, 87027, 87028, 87029, 87030, 87031, 87032, 87035, 87036, 87037, 87038, 87039], [85368, 87027, 87028, 87029, 87030, 87031, 87032, 87035, 87036, 87037, 87038, 87039], [85368, 87027, 87028, 87029, 87030, 87031, 87032, 87035, 87036, 87037, 87038, 87039], [85368, 87027, 87028, 87029, 87030, 87031, 87032, 87035, 87036, 87037, 87038, 87039], [85368, 87027, 87028, 87029, 87030, 87031, 87032, 87035, 87036, 87037, 87038, 87039], [85368, 87027, 87028, 87029, 87030, 87031, 87032, 87035, 87036, 87037, 87038, 87039], [85368, 87027, 87028, 87029, 87030, 87031, 87032, 87035, 87036, 87037, 87038, 87039], [85368, 87027, 87028, 87029, 87030, 87031, 87032, 87035, 87036, 87037, 87038, 87039], [85368, 87027, 87028, 87029, 87030, 87031, 87032, 87035, 87036, 87037, 87038, 87039], [85368, 87027, 87028, 87029, 87030, 87031, 87032, 87035, 87036, 87037, 87038, 87039], [85368, 87027, 87028, 87029, 87030, 87031, 87032, 87035, 87036, 87037, 87038, 87039], [85368, 87027, 87028, 87029, 87030, 87031, 87032, 87035, 87036, 87037, 87038, 87039], [85368, 87027, 87028, 87029, 87030, 87031, 87032, 87035, 87036, 87037, 87038, 87039]]
不太明白为什么因为它应该在80,000个样本之前更多地返回值,因为音频文件在开头和中间也有响度(10,000到50,000个样本)
任何有关我的语法或其他任何帮助的帮助,谢谢
答案 0 :(得分:1)
Python中的列表是可变的。您的acceptableLvl
有13个指向同一列表实例acceptableLvlPre
的链接。您修改acceptableLvlPre
,因此修改了所有副本。
解决方案很简单:将acceptableLvlPre[:] = []
替换为acceptableLvlPre = []
。然后会创建一个新列表,一切都会好的。