Reputation: 1369
I want to resample a mono recording recordered in 40.000 Hz to 44100 Hz.
The code below works but librosa seems to save in stereo, making the file twice the size which is not needed and I have a lot of samples to process.
So I need to save the result in mono.
Code:
# resampling a .wav file to a specific sample rate
import os
import librosa
import resampy
# this is the sample reate we want
sr_target = 44100
directory_in_str = '/home/hugo/test/'
directory = os.fsencode(directory_in_str)
for file in os.listdir(directory):
filename = os.fsdecode(file)
if filename.endswith(".wav"):
file_path = os.path.join(directory_in_str, filename)
print(file_path)
# Load in librosa's example audio file at its native sampling rate
x, sr_orig = librosa.load(file_path, mono=True, sr=None)
print("Original sample rate is : ", sr_orig)
# x is now a 1-d numpy array, with `sr_orig` audio samples per second
# We can resample this to any sampling rate we like, say 16000 Hz
y = resampy.resample(x, sr_orig, sr_target)
file_path_new = os.path.join(directory_in_str+'new/', filename)
# write it back
librosa.output.write_wav(file_path_new, y, sr_target)
continue
else:
continue
Question: I want to save the resampled file in mono, I get stereo and no option to save only mono...
Upvotes: 0
Views: 6449
Reputation: 582
The output is mono or stereo depends on y
. If y
has the shape of (n,), then the output is mono; If y
has the shape of (2,n), then the output is stereo. librosa.output.write_wav
won't automatically turn a mono signal to stereo.
From your code, your output audio seems like a stereo audio. The file having twice the size doesn't mean that it's stereo. It may be caused by the different data type of the input and output audio.
Upvotes: 1