4

I want to read the left and rigth channel.

 import wave
 origAudio = wave.open("6980.wav","r")
 frameRate = origAudio.getframerate()
 nChannels = origAudio.getnchannels()
 sampWidth = origAudio.getsampwidth()
 nbframe=origAudio.getnframes()
 da = np.fromstring(origAudio.readframes(48000), dtype=np.int16)
 origAudio.getparams()

the parametre

  (2, 3, 48000, 2883584, 'NONE', 'not compressed')

Now I want to separate left and right channel with wave file in 24 bit data

mouride touba
  • 55
  • 1
  • 4

2 Answers2

5

You can use wavio, a small module that I wrote to read and write WAV files using numpy arrays. In your case:

import wavio

wav = wavio.read("6980.wav")

# wav.data is the numpy array of samples.
# wav.rate is the sampling rate.
# wav.sampwidth is the sample width, in bytes.  For a 24 bit file,
# wav.sampwdith is 3.

left_channel = wav.data[:, 0]
right_channel = wav.data[:, 1]

wavio is on PyPi, and the source is on github at https://github.com/WarrenWeckesser/wavio.

Warren Weckesser
  • 110,654
  • 19
  • 194
  • 214
1

The parameters tell you that you have 2 channels of data at 3 bytes per sample, at 48kHz. So when you say readframes(48000) you get one second of frames which you should probably read into a slightly different data structure:

da = np.fromstring(origAudio.readframes(48000), dtype=np.uint8)

Now you should have 48000 * 2 * 3 bytes, i.e. len(da). To take only the first channel you'd do this:

chan1 = np.zeros(48000, np.uint32)
chan1bytes = chan1.view(np.uint8)
chan1bytes[0::4] = da[0::6]
chan1bytes[1::4] = da[1::6]
chan1bytes[2::4] = da[2::6]

That is, you make an array of integers, one per sample, and copy the appropriate bytes over from the source data (you could try copying directly from the result of readframes() and skip creating da).

John Zwinck
  • 239,568
  • 38
  • 324
  • 436
  • Thanks @John Zwing I want to take the left and right channel data – mouride touba Nov 09 '16 at 12:59
  • @mouridetouba OK well then try the above solution. If it works for the left channel you can do basically the same for the right channel, but using `da[3::6]`, `da[4::6]`, and `da[5::6]`. – John Zwinck Nov 09 '16 at 13:02