Mehtab Ur Rahman
Mehtab Ur Rahman

Reputation: 11

TypeError: 'pad_width' must be of integral type

from scipy import signal
import matplotlib.pyplot as plt
import librosa
import librosa.display
import numpy as np
from playsound import playsound

overlap = 1024
frame_length = 2048

from scipy.io import wavfile

def readAudio(audio):
    fs, amp = wavfile.read(audio)
    dt = 1 / fs
    n = len(amp)
    t = dt * n

    if t > 1.0:
        amp = amp[int((t / 2 - 0.5) / dt):int((t / 2 + 0.5) / dt)]
        n = len(amp)
        t = dt * n
  
    return (amp, fs, n, t)

amp, fs, n ,t = readAudio(r'C:\Users\mehta\Desktop\SVDwav\2510-phrase.wav')
#playsound(r'C:\Users\mehta\Desktop\SVDwav\2510-phrase.wav')

S = librosa.feature.melspectrogram(y=amp*1, sr=fs, n_fft=frame_length, hop_length=overlap, power=1)
fig = plt.figure(figsize=(10,4))

librosa.display.specshow(librosa.power_to_db(S,ref=np.max), y_axis='mel', fmax=8000, x_axis='time')
plt.colorbar(format='%+2.0f dB')
plt.title('Mel spectrogram')
plt.tight_layout()

I am getting the following error. How I can fix it?

File "C:\Users\mehta\PycharmProjects\pythonProject9\main.py", line 40, in S = librosa.feature.melspectrogram(y=amp*1, sr=fs, n_fft=frame_length, hop_length=overlap, power=1) File "C:\Users\mehta\AppData\Local\Programs\Python\Python39\lib\site-packages\librosa\feature.py", line 461, in melspectrogram S = np.abs(librosa.core.stft(y,
File "C:\Users\mehta\AppData\Local\Programs\Python\Python39\lib\site-packages\librosa\core.py", line 237, in stft fft_window = np.pad(fft_window, (lpad, n_fft - win_length - lpad), mode='constant') File "<array_function internals>", line 180, in pad File "C:\Users\mehta\AppData\Local\Programs\Python\Python39\lib\site-packages\numpy\lib\arraypad.py", line 740, in pad raise TypeError('pad_width must be of integral type.') TypeError: pad_width must be of integral type.

Upvotes: 1

Views: 1119

Answers (1)

Oivalf
Oivalf

Reputation: 154

I think the Problem is the "y" parameter in librosa.feature.melspectrogram(), returned from your custom import method:

y, sr = librosa.load("./5114148.wav")

The y ndarray becomes much larger than the y returned by your import method. By using this import method the code works.

Upvotes: 0

Related Questions