【发布时间】:2020-10-28 20:05:00
【问题描述】:
说明
我希望能够根据声音的音色(音调)对列表中的一组声音进行排序。这是一个玩具示例,我手动对我创建的 12 个声音文件和uploaded to this repo 的频谱图进行排序。我知道这些排序正确,因为每个文件产生的声音与之前文件中的声音完全相同,但添加了一个效果或过滤器。
例如,正确排序声音 x、y 和 z where
- 听起来 x 和 y 是一样的,但是 y 有失真效果
- y 和 z 的发音相同,但 z 会滤除高频
- 听起来x和z是一样的,但是z有失真效果,z过滤掉高频
应该是x, y, z
仅通过查看频谱图,我可以看到一些提示声音应该如何排序的视觉指标,但我希望通过让计算机识别这些指标来自动化排序过程。
上图中声音的声音文件
- 长度都一样
- 所有相同的音符/音高
- 一切都在同一时间开始。
- 幅度相同(响度级别)
即使所有这些条件不为真,我也希望我的排序能够正常工作(但即使不能解决这个问题,我也会接受最佳答案)
比如下图
- 与第一张图片中的 MFCC_8 相比,MFCC_8 的开头发生了偏移
- MFCC_9 与第一张图片中的 MFCC_9 相同,但有重复(因此长度是原来的两倍)
如果将第一张图片中的 MFCC_8 和 MFCC_9 替换为下图中的 MFCC_8 和 MFCC_9,我希望声音的排序保持完全相同。
对于我的真实程序,我打算通过声音更改来分解一个 mp3 文件like this
到目前为止我的程序
这是产生first image in this post 的程序。我需要将函数sort_sound_files 中的代码替换为一些实际根据音色对声音文件进行排序的代码。需要做的部分是靠近底部和sound files on on this repo。我在jupyter notebook 中也有这段代码,其中还包括第二个示例,它更类似于我实际希望该程序执行的操作
import librosa
import librosa.display
import matplotlib.pyplot as plt
import numpy as np
import math
from os import path
from typing import List
class Spec:
name: str = ''
sr: int = 44100
class MFCC(Spec):
mfcc: np.ndarray # Mel-frequency cepstral coefficient
delta_mfcc: np.ndarray # delta Mel-frequency cepstral coefficient
delta2_mfcc: np.ndarray # delta2 Mel-frequency cepstral coefficient
n_mfcc: int = 13
def __init__(self, soundFile: str):
self.name = path.basename(soundFile)
y, sr = librosa.load(soundFile, sr=self.sr)
self.mfcc = librosa.feature.mfcc(y, n_mfcc=self.n_mfcc, sr=sr)
self.delta_mfcc = librosa.feature.delta(self.mfcc, mode="nearest")
self.delta2_mfcc = librosa.feature.delta(self.mfcc, mode="nearest", order=2)
def get_mfccs(sound_files: List[str]) -> List[MFCC]:
'''
:param sound_files: Each item is a path to a sound file (wav, mp3, ...)
'''
mfccs = [MFCC(sound_file) for sound_file in sound_files]
return mfccs
def draw_specs(specList: List[Spec], attribute: str, title: str):
'''
Takes a list of same type audio features, and draws a spectrogram for each one
'''
def draw_spec(spec: Spec, attribute: str, fig: plt.Figure, ax: plt.Axes):
img = librosa.display.specshow(
librosa.amplitude_to_db(getattr(spec, attribute), ref=np.max),
y_axis='log',
x_axis='time',
ax=ax
)
ax.set_title(title + str(spec.name))
fig.colorbar(img, ax=ax, format="%+2.0f dB")
specLen = len(specList)
fig, axs = plt.subplots(math.ceil(specLen/3), 3, figsize=(30, specLen * 2))
for spec in range(0, len(specList), 3):
draw_spec(specList[spec], attribute, fig, axs.flat[spec])
if (spec+1 < len(specList)):
draw_spec(specList[spec+1], attribute, fig, axs.flat[spec+1])
if (spec+2 < len(specList)):
draw_spec(specList[spec+2], attribute, fig, axs.flat[spec+2])
sound_files_1 = [
'../assets/transients_1/4.wav',
'../assets/transients_1/6.wav',
'../assets/transients_1/1.wav',
'../assets/transients_1/11.wav',
'../assets/transients_1/13.wav',
'../assets/transients_1/9.wav',
'../assets/transients_1/3.wav',
'../assets/transients_1/7.wav',
'../assets/transients_1/12.wav',
'../assets/transients_1/2.wav',
'../assets/transients_1/5.wav',
'../assets/transients_1/10.wav',
'../assets/transients_1/8.wav'
]
mfccs_1 = get_mfccs(sound_files_1)
##################################################################
def sort_sound_files(sound_files: List[str]):
# TODO: Complete this function. The soundfiles must be sorted based on the content in the file, do not use the name of the file
# This is the correct order that the sounds should be sorted in
return [f"../assets/transients_1/{num}.wav" for num in range(1, 14)] # TODO: remove(or comment) once method is completed
##################################################################
sorted_sound_files_1 = sort_sound_files(sound_files_1)
mfccs_1 = get_mfccs(sorted_sound_files_1)
draw_specs(mfccs_1, 'mfcc', "Transients_1 Sorted MFCC-")
plt.savefig('sorted_sound_spectrograms.png')
编辑
直到后来我才意识到这一点,但另一件非常重要的事情是会有很多属性在振荡。例如,第一组声音 5 和声音 6 之间的区别在于声音 6 是声音 5,但在音量上有振荡(LFO),这种类型的振荡可以放在频率滤波器上,效果(如失真)甚至音高。我意识到这使问题变得更加棘手,并且超出了我所问的范围。你有什么建议吗?我什至可以使用几种不同的类型,并且一次只查看一个属性。
【问题讨论】:
-
如果你想根据不是音符的声音的特征来建立相似性,通常称为音色
-
@jonnor 是的。我想知道如何将音色录制成我可以使用的格式。比如数组什么的,数组中的行/列代表什么
-
音色是一个相当复杂的感知概念,一般来说 - 并且有点难以与音频的音符分离。最好的办法是使用一些学习模型/嵌入,将其映射到仅音色的低维空间
-
@jonnor 当您说“将其映射到较低维度的空间”时,您到底是什么意思。我已经阅读了更多关于此的内容,看起来 MFCC 功能非常适合比较音色。你对我可以用什么模型来映射这个有什么建议吗?
-
输出是几个数字,可能是 2-10,仅代表音色 - 将它分开/独立于音乐而不是本身。在 MFCC 中,这两件事仍然纠缠不清。不幸的是,目前我不知道有什么模型。
标签: python audio signal-processing numpy-ndarray librosa