pythonwavlibrosamfcc

MFCC conversion returning error "TypeError: expected string or bytes-like object, got 'NoneType'"


I have a mel spectogram saved to a numpy array (in a file called spectogram.npy). In the same folder, i have a file called main.py. Essentially, this python program is supposed to take this spectogram and then reconstruct a wav file from it. I am using the librosa package and some code I found on stack overflow to test this out.

main.py

import numpy
import librosa
array = numpy.load("C:\\Users\\aweso\\meltowav\\spectogram.npy")
print(array)

spec = librosa.feature.melspectrogram(y=array,
sr=16000,
n_fft=2048,
hop_length=512,
win_length=None,
window='hann',
center=True,
pad_mode='reflect',
power=2.0,
n_mels=25)

res = librosa.feature.inverse.mel_to_audio(spec,
sr=16000,
n_fft=2048,
hop_length=512,
win_length=None,
window='hann',
center=True,
pad_mode='reflect',
power=2.0,
n_iter=32)

import soundfile as sf
sf.write("test1.wav", res, 16000)

However, I keep on getting a weird error when I run the program:

'Traceback (most recent call last):File "c:\Users\aweso\meltowav\main.py", line 6, in <module>spec = librosa.feature.melspectrogram(y=array,^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\aweso\AppData\Roaming\Python\Python311\site-packages\lazy_loader_init_.py", line 77, in getattrsubmod = importlib.import_module(submod_path)^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\aweso\AppData\Local\Programs\Python\Python311\Lib\importlib_init_.py", line 126, in import_modulereturn _bootstrap._gcd_import(name[level:], package, level)^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "<frozen importlib._bootstrap>", line 1204, in _gcd_importFile 
"<frozen importlib._bootstrap>", line 1176, in _find_and_loadFile 
"<frozen importlib._bootstrap>", line 1147, in _find_and_load_unlockedFile 
"<frozen importlib._bootstrap>", line 690, in _load_unlockedFile 
"<frozen importlib._bootstrap_external>", line 940, in exec_moduleFile 
"<frozen importlib.bootstrap>", line 241, in call_with_frames_removed
File "C:\Users\aweso\AppData\Local\Programs\Python\Python311\Lib\site-packages\librosa\feature\spectral.py", line 15, in <module>from ..core.audio import zero_crossings
File "C:\Users\aweso\AppData\Local\Programs\Python\Python311\Lib\site-packages\librosa\core\audio.py", line 1158, in <module>@guvectorize(^^^^^^^^^^^^
File "C:\Users\aweso\AppData\Local\Programs\Python\Python311\Lib\site-packages\numba\np\ufunc\decorators.py", line 203, in wrapguvec.add(fty)
File "C:\Users\aweso\AppData\Local\Programs\Python\Python311\Lib\site-packages\numba\np\ufunc\gufunc.py", line 64, in addself.gufunc_builder.add(fty)
File "C:\Users\aweso\AppData\Local\Programs\Python\Python311\Lib\site-packages\numba\np\ufunc\ufuncbuilder.py", line 258, in addcres, args, return_type = compile_element_wise_function(^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\aweso\AppData\Local\Programs\Python\Python311\Lib\site-packages\numba\np\ufunc\ufuncbuilder.py", line 176, in compile_element_wise_functioncres = nb_func.compile(sig, **targetoptions)^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\aweso\AppData\Local\Programs\Python\Python311\Lib\site-packages\numba\np\ufunc\ufuncbuilder.py", line 124, in compilereturn self.compile_core(sig, flags, locals)^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\aweso\AppData\Local\Programs\Python\Python311\Lib\site-packages\numba\np\ufunc\ufuncbuilder.py", line 151, in compile_corecres = self.cache.load_overload(sig, targetctx)^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\aweso\AppData\Local\Programs\Python\Python311\Lib\site-packages\numba\core\caching.py", line 633, in load_overloadtarget_context.refresh()
File "C:\Users\aweso\AppData\Local\Programs\Python\Python311\Lib\site-packages\numba\core\base.py", line 267, in refreshself.load_additional_registries()
File "C:\Users\aweso\AppData\Local\Programs\Python\Python311\Lib\site-packages\numba\core\cpu.py", line 99, in load_additional_registriesnumba.core.entrypoints.init_all()
File "C:\Users\aweso\AppData\Local\Programs\Python\Python311\Lib\site-packages\numba\core\entrypoints.py", line 48, in init_alleps = importlib_metadata.entry_points()^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\aweso\AppData\Local\Programs\Python\Python311\Lib\importlib\metadata_init.py", line 1040, in entry_pointsreturn SelectableGroups.load(eps).select(**params)^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\aweso\AppData\Local\Programs\Python\Python311\Lib\importlib\metadata_init.py", line 476, in loadordered = sorted(eps, key=by_group)^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\aweso\AppData\Local\Programs\Python\Python311\Lib\importlib\metadata_init.py", line 1037, in <genexpr>eps = itertools.chain.from_iterable(^
File "C:\Users\aweso\AppData\Local\Programs\Python\Python311\Lib\importlib\metadata_itertools.py", line 16, in unique_everseenk = key(element)^^^^^^^^^^^^
File "C:\Users\aweso\AppData\Local\Programs\Python\Python311\Lib\importlib\metadata_init.py", line 954, in normalized_nameor super().normalized_name^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\aweso\AppData\Local\Programs\Python\Python311\Lib\importlib\metadata_init.py", line 627, in normalized_namereturn Prepared.normalize(self.name)^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\aweso\AppData\Local\Programs\Python\Python311\Lib\importlib\metadata_init.py", line 882, in normalizereturn re.sub(r"[-.]+", "-", name).lower().replace('-', '')^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\aweso\AppData\Local\Programs\Python\Python311\Lib\re_init.py", line 185, in subreturn _compile(pattern, flags).sub(repl, string, count)^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^TypeError: expected string or bytes-like object, got 'NoneType'

I have tried rewriting my code, but I couldn't find anything wrong with it. I also printed out the contents of the spectogram and confirmed that it was not empty.


Solution

  • I think you make mel spectrogram twice. You've already have it in spectogram.npy

    import librosa
    import numpy as np
    import soundfile as sf
    
    
    audio_path = librosa.example('example.wav')
    y, sr = librosa.load(audio_path, sr=None)
    
    mel_spectrogram = librosa.feature.melspectrogram(y=y, sr=sr, n_mels=128)
    
    np.save('mel_spectrogram.npy', mel_spectrogram)
    
    loaded_mel_spectrogram = np.load('mel_spectrogram.npy')
    
    mel_to_audio = librosa.feature.inverse.mel_to_audio
    
    y_restored = mel_to_audio(loaded_mel_spectrogram, sr=sr, n_iter=32)
    
    sf.write('restored_audio.wav', y_restored, sr)
    

    this code save audio mel spectrogram and then restore it, picture below shows the result

    enter image description here