I have written a code with librosa module, to generate spectrogram of an audio file and other works. Apparently it works fine with Spyder where the output is generated after taking a while, but for PyCharm, it just gets executed. No output spectrogram.
I am fairly new to PyCharm environment please help.
My code is:
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
audio_name = 'C:\\Users\\USER\\Desktop\\Songs\\abc.wav'
hop_length = 512
window_size = 2048
import librosa
y, sr = librosa.load(audio_name) #y=time series(one-dimensional NumPy floating point array), sr= sampling rate of y, that is, the number of samples per second of audio. By default, all audio is mixed to mono and resampled to 22050 Hz at load time
window = np.hanning(window_size)
out = librosa.core.spectrum.stft(y, n_fft = window_size, hop_length = hop_length, window=window)
out = 2 * np.abs(out) / np.sum(window)
import librosa.display
librosa.display.specshow(librosa.amplitude_to_db(out, ref=np.max), y_axis='log', x_axis='time')
''' To store spectrogram
from matplotlib.backends.backend_agg import FigureCanvasAgg
fig = plt.figure()
canvas = FigureCanvasAgg(fig)
ax = fig.add_subplot(111)
p = librosa.display.specshow(librosa.amplitude_to_db(out, ref=np.max), ax=ax, y_axis='log', x_axis='time')
fig.savefig('C:\\Users\\USER\\Desktop\\Songs\\spec.png')
'''
onset_env = librosa.onset.onset_strength(y=y, sr=sr, aggregate=np.median)
tempo, beats = librosa.beat.beat_track(onset_envelope=onset_env, sr=sr)
fig, ax = plt.subplots(nrows=2, sharex=True)
times = librosa.times_like(onset_env, sr=sr, hop_length=hop_length)
M = librosa.feature.melspectrogram(y=y, sr=sr, hop_length=hop_length)
librosa.display.specshow(librosa.power_to_db(M, ref=np.max), y_axis='mel', x_axis='time', hop_length=hop_length, ax=ax[0])
ax[0].label_outer()
ax[0].set(title='Mel spectrogram')
ax[1].plot(times, librosa.util.normalize(onset_env), label='Onset strength')
ax[1].vlines(times[beats], 0, 1, alpha=0.5, color='r', linestyle='--', label='Beats')
ax[1].legend()
The output in Spyder (After taking a bit of time):
The output of PyCharm after execution:
Update: Apparently it works when I give a plt.show() at the end as referred by @Mandera. But can someone tell me why it works in Spyder even without this?