如何解决在 Python 中查找 wav 文件频率的持续时间
我正在尝试编写一个将 wav 文件作为输入并转换频率的代码 从这个 wav 文件转换成乐谱。 我已经编写了用于查找频率的代码,该代码有效。 但我现在正试图找到一种方法来获得每个频率的持续时间,我不确定如何解决这个问题。 我当前用于查找音符音高的输出是我映射到音符名称的 Hz 列表。理想情况下,我想要类似的东西。
这是我用于查找频率的代码:
def find_frequency(data):
# play the stream and find the frequency of each chunk
while len(data) == chunk * sample_width:
# unpack the data and multiply it times the blackman window
unpacked_data = np.array(wave.struct.unpack("%dh" % (len(data) / sample_width),data)) * window
# Take the fft and square each value
fft_data = abs(np.fft.rfft(unpacked_data)) ** 2
# find the maximum
max = fft_data[1:].argmax() + 1
# use quadratic interpolation around the max
if max != len(fft_data) - 1:
y0,y1,y2 = np.log(fft_data[max - 1:max + 2:])
x1 = (y2 - y0) * .5 / (2 * y1 - y2 - y0)
# find the frequency and output it
frequency = (max + x1) * fps / chunk
print("The freq is %f Hz." % frequency + ",which is ")
#call function to map to corresponding note
find_nearast(note_frequency,frequency)
# call function to play back the sound
play_note(note_frequency,frequency)
else:
frequency = max * fps / chunk
print("The freq is %f Hz." % frequency + ",which is")
find_nearast(note_frequency,frequency)
play_note(note_frequency,frequency)
# read some more data
data = file.readframes(chunk)
if data:
stream.write(data)
stream.close()
p.terminate()
我的 result_list(频率列表),是由以下代码生成的笔记列表:
result_list =[]
def find_nearast(note_frequency,frequency):
note_frequency = np.asarray(note_frequency)
idx = (np.abs(note_frequency - frequency)).argmin()
print(notes[idx])
global result_list
result_list.append(notes[idx])
return result_list
它获取识别的频率,将其与真实音符频率列表进行比较,找到最接近的频率,然后将其映射到相应的音符名称上。
现在我需要的是一段类似的持续时间的代码。但我真的不知道如何才能找到它。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。