下面的代码是音频文件上的功能之一。由于我将一个音频文件分割为4096个样本,因此我必须调用boxcounting函数4096次才能获得每个分割文件的输出。我为一个分段文件编写了此代码,并从主python文件中调用了此代码。如果完整音频较短,则大约需要10秒/分段文件,如果完整音频大约3-4分钟,则需要30秒/分段文件。我的问题是运行一个音频文件需要很长时间。
import numpy as np
from pydub import AudioSegment
from collections import OrderedDict
def difference(a, b):
if (a > 0) and (b > 0):
return (abs(a - b))
elif (a > 0) and (b < 0):
return abs(a + abs(b))
elif (a < 0) and (b < 0):
return (abs(a) - abs(b))
def boxcounting(left_channel, right_channel, scale):
ratioX = difference(max(left_channel), min(left_channel))/scale
ratioY = difference(max(right_channel), min(right_channel))/scale
startX = min(left_channel)
count_per_scale = []
countbox = 0
pair = list(OrderedDict.fromkeys(list(zip(left_channel, right_channel))))
for x in range(scale):
print('startX',startX)
startY = min(right_channel)
endX = startX + ratioX
if x == (scale-1):
endX = max(left_channel)
print('endX',endX)
for y in range(scale):
print('-----------------------')
print('startY',startY)
endY = startY + ratioY
if y == (scale-1):
endY = max(right_channel)
print('endY',endY)
count = 0 # reset
for l,r in pair:
if (startX < l <= endX):
if (startY < r <= endY):
count+=1
print('0',l,r)
print('count',count)
elif (min(right_channel) == r and r == startY):
count+=1
print('1',l,r)
print('count',count)
elif (min(left_channel) == l and l == startX):
if (startY < r <= endY):
count+=1
print('2',l,r)
print('count',count)
elif (min(right_channel) == r and r == startY):
count+=1
print('3',l,r)
print('count',count)
count_per_scale.append(count)
if count != 0:
countbox += 1
startY = endY
startX = endX
print('===============================')
print(count_per_scale)
countbox = 0
for i in count_per_scale:
if(i > 0):
countbox += 1
countbox = np.count_nonzero(count_per_scale)
print('No. of box that has value =', countbox)
return countbox
sound = AudioSegment.from_file('Alpharock - Pump This Party.mp3')
split_sound = sound.split_to_mono()
left_channel = np.array(split_sound[0].get_array_of_samples())
right_channel = np.array(split_sound[1].get_array_of_samples())
scale = 10 #norm and scale up
scaleupL = np.round((left_channel/np.abs(left_channel).max())* scale,1)
scaleupR = np.round((right_channel/np.abs(right_channel).max())* scale,1)
有人可以帮我加快速度吗?非常感谢。