【问题标题】:Librosa Display Waveplot, why are they totally blue and flat?Librosa Display Waveplot,为什么它们完全是蓝色和平坦的?
【发布时间】:2019-12-07 11:07:48
【问题描述】:

我关注了this example of Music Synchronization with Dynamic Time Warping

但是,当我这样做时:

import matplolib.pyplot as plt
import librosa
import librosa.display

x_1, fs = librosa.load('musicdata/slow_melody.wav')
plt.figure(figsize=(16, 4))
librosa.display.waveplot(x_1, sr=fs)
plt.title('Slower Version $X_1$')
plt.tight_layout()

对于更快的版本也是如此,我得到了这个结果:

我可以在色度表示中正确地达到 wav 文件的音高等级,并且 wav 文件中没有问题。

我创建了这样的 wav 文件的快速和慢速版本:

    # Tone-duration sequence
    melody = [('E', 0.3), ('E', 0.3), ('F', 0.3), ('G', 0.3)]
    slow_melody = [('E', 0.6), ('E', 0.6), ('F', 0.6), ('G', 0.6)]

    melody_output = np.array([])
    # Construct the audio signal based on the chord sequence
    for item in melody:
        input_tone = item[0]
        duration = item[1]
        synthesized_tone = synthesizer(tone_freq_map[input_tone], duration, amplitude, sampling_freq)
        melody_output = np.append(melody_output, synthesized_tone, axis=0)

    # Write to the output file
    name = 'melody' + '.wav'
    write(name, sampling_freq, melody_output)

    slow_melody_output = np.array([])
    # Construct the audio signal based on the chord sequence
    for item in slow_melody:
        input_tone = item[0]
        duration = item[1]
        synthesized_tone = synthesizer(tone_freq_map[input_tone], duration, amplitude, sampling_freq)
        slow_melody_output = np.append(slow_melody_output, synthesized_tone, axis=0)

    # Write to the output file
    name = 'slow_melody' + '.wav'
    write(name, sampling_freq, slow_melody_output)

我从以下位置获取音调频率:

{ “一”:440, “锐利”:466, “B”:494, “C”:523, “Csharp”:554, “D”:587, “锐利”:622, “E”:659, “F”:698, “锐化”:740, “G”:784, “Gsharp”:831 }

合成器是:

    def synthesizer(freq, duration, amp=1.0, sampling_freq=44100):
        # Build the time axis
        t = np.linspace(0, duration, duration * sampling_freq)

        # Construct the audio signal
        audio = amp * np.sin(2 * np.pi * freq * t)

        return audio.astype(np.int16) 

另外,输入参数是:

duration = 2
amplitude = 10000
sampling_freq = 44100

那么,为什么我不能正确地可视化波形图?它们看起来像这样的原因可能是什么?

【问题讨论】:

  • 既然你用的是matplotlib,你能不能试试plt.plot(x_1); plot.show() 看看会发生什么?

标签: wav librosa


【解决方案1】:

我认为您所遵循的教程有问题。 librosa.display.waveplot() 本身不会绘制任何东西,你仍然需要调用 plt.show() 来可视化它。从官方文档中,这是一个使用示例:

y, sr = librosa.load(librosa.util.example_audio_file(), duration=10)
librosa.display.waveplot(y_harm, sr=sr, alpha=0.25)
plt.tight_layout()
plt.show()

你可以在这里找到更多信息https://librosa.github.io/librosa/generated/librosa.display.waveplot.html

【讨论】:

  • 感谢您的回复。正如你所说,我在 plt.tight_layout() 之后添加了 plt.show() 。然而,一切都没有改变。我在上一张图片中得到了相同的情节。但是,我也尝试了其他一些 mp3 文件,它们没有问题。所以,我认为问题可能与我使用的 wav 文件有关,也许?
  • 啊,我认为问题在于您尝试可视化的示例太长了。采样率为 48kHz 的一秒音频是每秒 48000 个采样点。这可能就是为什么它都卡住了。尝试使用较低的采样率或更短的音频文件。
  • 对于我尝试的第一个 wav 文件,没有。实际上,它们是2-3秒。正如我提到的,其他 mp3 文件没有问题,它们是 6-8 秒。所以,有趣的是,我对较短的有问题。但是我用 python 脚本构建了第一个有问题的 wav 文件。这会导致这样的问题吗?
  • 很难在没有看到您的代码的情况下判断。你如何创造它们?您能否在问题的编辑中添加该代码?
猜你喜欢
  • 1970-01-01
  • 2019-06-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-07
  • 2016-10-24
  • 1970-01-01
  • 2023-03-04
相关资源
最近更新 更多