Reputation: 3626
I am loading an audio file using librosa and would like to potentially convert it to stereo from mono if the file is mono.
I'm sure it does not need to be said, but the audio time series is an "np.ndarray [shape=(n,) or (2, n)]
". Essentially, I am really asking if I have an np.ndarray
of shape=(n,)
, how do I convert it to a shape=(2,n)
? Is this possible? I am unfamiliar with this terminology.
Alternatively, is there a way to just load the file, using librosa and only librosa, as a stereo file (it does the conversion for me if necessary)? The fact that there exists a librosa.to_mono()
function and not a librosa.to_stereo
function frightens me.
For context, I am trying to standardize audio files based on some parameters (number of channels, sample size and freq, etc). They would all be converted to .wav
files. Supposedly, a .wav
PMC compliant file can be converted to stereo using the wave
library, but I would rather not essentially create two wav
files (the function would require the wav
file to be reconstructed, I imagine).
Any tips?
Edit:
Okay, so I found out about the np.reshape()
function. Now, the question is, how would I reshape the array?
If I have an audio time series that is shape=(746572,)
, what should the stereo shape be? Is it going to be a 2D array that is essentially the first array twice? So
[[1, 2, 3, ..., 746572],
[1, 2, 3, ..., 746572]]
Or is it not that simple?
Upvotes: 1
Views: 1639
Reputation: 3626
Okay, I think I figured it out. For this problem specifically, I think my hypothesis that the array needed to be repeated twice is true. Therefore, a simple:
y = np.array([y, y])
Sufficed.
However!!! If trying to write to a wave file using librosa.output.write_wav()
, The np array needs to be fortran compliant. So,
y = np.asfortranarray(np.array([y, y]))
Is the complete, correct answer. Cheers
If there is a better answer please comment!
Upvotes: 2