简体   繁体   中英

How do you separate each channel of a two channel wav file into two different files using wavio? or another library?

The following script plays the original file ok. I try to separate each channel in the obvious way, but it does not work.

import os
import wavio
import numpy
import pyglet

file_name = "guitarup_full.wav"

# I get the file !
File = wavio.read(file_name)
rate = File.rate
# it looks good
print File.data.shape
print rate
# and then the channels:
channel_1 = File.data[:,0]
channel_2 = File.data[:,1]
wavio.write("guitar_channel_1.wav", channel_1, rate )
wavio.write("guitar_channel_2.wav", channel_2, rate )

# now we try to play:
source =  pyglet.resource.media(file_name, streaming=False)
source_ch1 =  pyglet.resource.media("guitar_channel_1.wav", streaming=False)
source_ch2 =  pyglet.resource.media("guitar_channel_2.wav", streaming=False)

#uncomment the one you want to listen.
source.play()
#source_ch1.play()
#source_ch2.play()

pyglet.app.run()

The first sounds like a guitar, the second and third like Gaussian noise. Can someone tell me what is wrong with it?

The audio file I used is: https://www.freesounds.info/global/wav.php?fileid=11

The shape of the data is: (88471, 2) rate is: 44100

Also, if I play the file in another player, I get the same: Gaussian Noise.

Note: The use of pyglet is superfluous for the problem. If you use it to investigate this issue, make sure the files are in a folder registered in the resources folder. To do that:

pyglet.resource.path.append("your_sounds_location")
pyglet.resource.reindex()

I don't know what is wrong with the wavio code, but here's how you can separate WAV channel using standard Python module wave (which is also used by wavio) and numpy:

import wave
import numpy as np

def save_wav_channel(fn, wav, channel):
    '''
    Take Wave_read object as an input and save one of its
    channels into a separate .wav file.
    '''
    # Read data
    nch   = wav.getnchannels()
    depth = wav.getsampwidth()
    wav.setpos(0)
    sdata = wav.readframes(wav.getnframes())

    # Extract channel data (24-bit data not supported)
    typ = { 1: np.uint8, 2: np.uint16, 4: np.uint32 }.get(depth)
    if not typ:
        raise ValueError("sample width {} not supported".format(depth))
    if channel >= nch:
        raise ValueError("cannot extract channel {} out of {}".format(channel+1, nch))
    print ("Extracting channel {} out of {} channels, {}-bit depth".format(channel+1, nch, depth*8))
    data = np.fromstring(sdata, dtype=typ)
    ch_data = data[channel::nch]

    # Save channel to a separate file
    outwav = wave.open(fn, 'w')
    outwav.setparams(wav.getparams())
    outwav.setnchannels(1)
    outwav.writeframes(ch_data.tostring())
    outwav.close()

wav = wave.open(WAV_FILENAME)
save_wav_channel('ch1.wav', wav, 0)
save_wav_channel('ch2.wav', wav, 1)

Try ffmpeg on the command line:

Output each channel in stereo input to individual mono files:

ffmpeg -i stereo.wav -map_channel 0.0.0 left.wav -map_channel 0.0.1 right.wav

I tried it on your file and it seems to work.

If you need it to be in a python program just use os.system(cmd)

Method 1 (Using ffmpeg)

You can use ffmpeg to split a n-channeled .wav like this:

ffmpeg -i input.wav -map 0:1 1.wav -map 0:2 2.wav -map 0:3 3.wav........

You can run this is python using:

import os
os.system(ffmpeg -i 14ch.mov -map 0:1 1.wav -map 0:2 2.wav -map 0:3 3.wav........)

Method 2 (Using sox)

Install sox from here

Then try this:

sox infile.wav outfile_left.wav remix 1
sox infile.wav outfile_right.wav remix 2

Again you can run this in python using os.system

I think that you are trying to make it much more complicated than it needs to be. If you don't mind using scipy than separating the channels comes down to:

from scipy.io import wavfile

fs, data = wavfile.read('guitarup_full.wav')            # reading the file

wavfile.write('guitar_channel_1.wav', fs, data[:, 0])   # saving first column which corresponds to channel 1
wavfile.write('guitar_channel_2.wav', fs, data[:, 1])   # saving second column which corresponds to channel 2

Thanks to everyone for taking the time to read and run my scripts. I finally found a solution. Follow the steps in this order:

First, uninstall wavio by:

pip uninstall wavio

Second, uninstall numpy too:

pip uninstall numpy

Finally, install wavio again. numpy will be installed as a dependency:

pip install wavio

numpy will be installed as a dependency.

Installing collected packages: numpy, wavio
Successfully installed numpy-1.14.5 wavio-0.0.4

Then my original script will deliver the solution needed.

The use of ffmpeg as @Marichyasana suggested is a good workaround. However python libraries (such as pyglet, python audio tools, etc) have problems reading these output files back.

I hope people find this solution helpful !

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM