Python Librosa: How to create a stereo wav from mono?

435 Views Asked by At

I've tried a few suggestions from stackoverflow but unfortunately I can't get them to save as a valid wav file. When i try to open them they don't load properly or I get the following error. Any help will be greatly appreciated!

"Error opening 'test.wav': Format not recognised."

import librosa
import numpy as np
import soundfile as sf 

y, sr = librosa.load('mono_sound.wav', sr=44100, mono=True)
y1 = np.column_stack((y, y))
y2 = np.array([y, y])

sf.write('test1.wav', y1, sr, subtype='PCM_16')
sf.write('test2.wav', y2, sr, subtype='PCM_16')
/var/folders/5w/99fn3vp546z10xj_7_48w1nh0000gn/T/ipykernel_32921/2458352271.py in <module>
----> 1 sf.write('test2.wav', y2, sr, subtype='PCM_16')

~/opt/anaconda3/lib/python3.9/site-packages/soundfile.py in write(file, data, samplerate, subtype, endian, format, closefd)
    428     else:
    429         channels = data.shape[1]
--> 430     with SoundFile(file, 'w', samplerate, channels,
    431                    subtype, endian, format, closefd) as f:
    432         f.write(data)

~/opt/anaconda3/lib/python3.9/site-packages/soundfile.py in __init__(self, file, mode, samplerate, channels, subtype, endian, format, closefd)
    738         self._info = _create_info_struct(file, mode, samplerate, channels,
    739                                          format, subtype, endian)
--> 740         self._file = self._open(file, mode_int, closefd)
    741         if set(mode).issuperset('r+') and self.seekable():
    742             # Move write position to 0 (like in Python file objects)

~/opt/anaconda3/lib/python3.9/site-packages/soundfile.py in _open(self, file, mode_int, closefd)
   1262         else:
   1263             raise TypeError("Invalid file: {0!r}".format(self.name))
-> 1264         _error_check(_snd.sf_error(file_ptr),
   1265                      "Error opening {0!r}: ".format(self.name))
   1266         if mode_int == _snd.SFM_WRITE:

~/opt/anaconda3/lib/python3.9/site-packages/soundfile.py in _error_check(err, prefix)
   1453     if err != 0:
   1454         err_str = _snd.sf_error_number(err)
-> 1455         raise RuntimeError(prefix + _ffi.string(err_str).decode('utf-8', 'replace'))
   1456 
   1457 

RuntimeError: Error opening 'test2.wav': Format not recognised.

0

There are 0 best solutions below