1

I followed this example of Music Synchronization with Dynamic Time Warping

However, when I do this:

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()

and same for the faster version, I get this result:

enter image description here

I can properly reach the pitch classes of the wav files in chroma representations and there are no problems in the wav files.

I created the fast and slow versions of the wav files like this:

    # 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)

I get the tone frequencies from:

{ "A": 440, "Asharp": 466, "B": 494, "C": 523, "Csharp": 554, "D": 587, "Dsharp": 622, "E": 659, "F": 698, "Fsharp": 740, "G": 784, "Gsharp": 831 }

Synthesizer is:

    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) 

Also, the input parameters are:

duration = 2
amplitude = 10000
sampling_freq = 44100

So, why couldn't I properly visualize the waveplots? What could be the reason that they appear like this?

oldboy123
  • 61
  • 2
  • 8

1 Answers1

0

I believe there is something wrong in the tutorial you are following. librosa.display.waveplot() doesn't plot anything by itself, you still have to call plt.show() to visualize it. From the official documentation, here's an example of it's usage:

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()

You can find more info here https://librosa.github.io/librosa/generated/librosa.display.waveplot.html

Ahmad Moussa
  • 876
  • 10
  • 31
  • Thanks for the reply. I added plt.show() after plt.tight_layout() as you said. However, nothing has changed. I got the same plots in the previous image. But, I also tried with some other mp3 files and there was no problem with them. So, I think that the problem may be about the wav files that I have used, maybe? – oldboy123 Dec 08 '19 at 22:03
  • ah i think the problem is that the examples you are trying to visualize are too long. One second of audio at a sample_rate of 48kHz is 48000 sample points per second. That might be why it is all jammed up. Try with a lower samplerate or a shorter audio file. – Ahmad Moussa Dec 09 '19 at 02:35
  • For the first wav files I tried, no. Actually, they were 2-3 secs. There was no issue with the other mp3 files as I mentioned, and they were 6-8 secs. So, interestingly I have a problem with the shorter ones. But I constructed that first problematic wav files with a python script. May this cause such an issue? – oldboy123 Dec 17 '19 at 14:24
  • Hard to tell without seeing your code. How do you create them? Could you add that code in an edit to your question? – Ahmad Moussa Dec 17 '19 at 15:50