0

I'm trying to use librosa to split a stereo audio file into separate channels. I have this code:

import librosa

audio, sr = librosa.load('trumpet.wav')

canal_esquerdo, canal_direito = librosa.effects.split_stereo(audio)

librosa.output.write_wav('canal_esquerdo.wav', canal_esquerdo, sr)
librosa.output.write_wav('canal_direito.wav', canal_direito, sr)

but I get an error:

Traceback (most recent call last):
  File "audio.py", line 7, in <module>
    canal_esquerdo, canal_direito = librosa.effects.split_stereo(audio)
AttributeError: module 'librosa.effects' has no attribute 'split_stereo'

What is wrong with the code? Is it possible to split the audio using librosa? If so, how?

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
  • 1
    Where did you read that you can use `librosa.effects.split_stereo`? – mkrieger1 Jan 07 '23 at 17:59
  • Does this answer your question? [How do you separate each channel of a two channel wav file into two different files using wavio? or another library?](https://stackoverflow.com/questions/51275725/how-do-you-separate-each-channel-of-a-two-channel-wav-file-into-two-different-fi) – mkrieger1 Jan 07 '23 at 18:02
  • I'm not sure if librosa is the right tool for this purpose. – mkrieger1 Jan 07 '23 at 18:02
  • I need to separate the lines from the audio, I don't know if that would be possible, I took this code example from another forum that had no more answers, the link above does not separate the lines – Rodrigo Augusto Martins Jan 07 '23 at 18:19
  • 1
    Do you mean by "separate the lines" something different than "separate left and right channel"? – mkrieger1 Jan 07 '23 at 19:28
  • Yes, let me put it in a better way here, I have an audio with two people who are talking on side A and side B. I want to separate these audios, would it be possible? – Rodrigo Augusto Martins Jan 11 '23 at 18:11
  • Does [the documentation](https://librosa.org/doc/latest/multichannel.html) answer your question? – Karl Knechtel Jan 17 '23 at 02:44
  • I edited the question to [remove noise](https://meta.stackoverflow.com/questions/343721) and ask a question clearly. – Karl Knechtel Jan 17 '23 at 02:46

2 Answers2

1

The error message means exactly what it says: .split_stereo isn't provided here, so that can't be used to split the stereo file.

To find out how to use a library for a given task, do not guess at names for functions - search for, and read, documentation.

The relevant documentation explains:

When working with multi-channel signals, we may choose to skip the default down-mixing step by specifying mono=False in the call to librosa.load, as in the following:

import librosa

# Get the "high-quality" multi-channel version of
# an example track
filename = librosa.ex('trumpet', hq=True)

# Load as multi-channel data
y_stereo, sr = librosa.load(filename, mono=False)

The resulting object now has two dimensions instead of one, with y_stereo.shape == (N_channels, N_samples). This way, we can access the first channel as y_stereo[0], the second channel as y_stereo[1], and so on if there are more than two channels.

That is to say: librosa does not treat "stereo" as a special case; as long as mono=False is specified, loading just gives a 2d array representing however many channels there are.

Adapting that to the original code:

import librosa

audio, sr = librosa.load('trumpet.wav', mono=False)

librosa.output.write_wav('canal_esquerdo.wav', audio[0], sr)
librosa.output.write_wav('canal_direito.wav', audio[1], sr)
Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
-2

Recommend using split_to_mono() per pydub.

For example:

left, right = audio.split_to_mono()
Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
AndyH
  • 1