If stereo WAV file's both channels are identical, change format to mono for all files recursively

  • Thread starter Raseliarison nirinA
  • Start date
R

Raseliarison nirinA

hi all,
i found an unanswered question at
http://www.faqts.com/knowledge_base/index.phtml/fid/538
with possible response below. i've tried to send it at
faqt.python but can't figure out how to edit the page. so i put it here.
i want to kwon if this can convert all wave file. is there other
encodage than 8 or 16 bits for .wav files?
any bug and comment are welcome

--
nirinA

#--------------stereo2mono-R.py---------------
'''stereo2mono-R.py
convert stereo to mono recursively
usage : python stereo2mono-R.py path-directory
'''

import os, sys

from stereo2mono import *

directory = sys.argv[1]
os.chdir(directory)

for name in os.listdir('.'):
if name.endswith('.wav'):
w = Stereo2Mono(name)
if w.isStereo() == 1:
print '%s is in stereo'%name
print 'Check for %s samples in the audio frame'%SAMPLING
w.CompareSampling()
print 'Both channels seem to be identical'
print 'Check all data frames and save to mono file'
w.CompareAndSave()
print 'Done'
else:
print '%s is already in mono'%name

else:
pass


#-----stereo2mono.py-----------
'''stereo2mono.py
convert a stereo wave to mono
if both channels are identical

http://www.faqts.com/knowledge_base/view.phtml/aid/12121/fid/538
'''

import wave, sys

FORMAT = {'11':'1b','12':'1h','21':'2b','22':'2h'}
# format for wave files encoded in 8 and 16 bits

SAMPLING = 128

class Stereo2Mono:
'''open the wave file and get its parameters
compare the channels
it will be done in two steps.
samples are first compared,
then if the samples are identical
further comparison is performed
and the mono wave file created
'''
def __init__(self, name):
self.name = name
self.w = wave.open(self.name)

def isStereo(self):
if self.w.getnchannels() == 2:
return 1
else:
return 0

def format_in(self):
self.fmt = ''.join((str(self.w.getnchannels()),
str(self.w.getsampwidth())))
return FORMAT.get(self.fmt)

def format_out(self):
self.fmt = ''.join(('1',
str(self.w.getsampwidth())))
return FORMAT.get(self.fmt)

def Parameters(self):
return self.w.getparams()

def Compare(self, amplitude):
if amplitude[0] == amplitude[1]:
return 1
else:
return 0

def CompareSampling(self):
for s in range(0, self.Parameters()[3],SAMPLING):
if self.Compare(wave.struct.unpack(
self.format_in(),self.w.readframes(1))) == 1:
pass
else:
print 'channels at %s are not identical,abort!'%s
#sys.exit()
print 'Samples pass test'

def CompareAndSave(self):
'''Compare all and save to mono'''
self.w.rewind()
self.chars = '/-\\|'
self.Save = wave.open(self.name.split('.')[0]+
'-mono'+'.wav','w')
self.newparams = (1,
self.Parameters()[1],
self.Parameters()[2],
self.Parameters()[3],
self.Parameters()[4],
self.Parameters()[5])

self.Save.setparams(self.newparams)

for i in range(1, self.Parameters()[3]+1):
self.UnPack = wave.struct.unpack(
self.format_in(), self.w.readframes(1))
if self.Compare(self.UnPack) == 1:
self.Save.writeframes(wave.struct.pack(
self.format_out(), self.UnPack[0]))
sys.stdout.write(chr(13))
sys.stdout.write('%s %i/%i ' % (
self.chars[i % 4], i, self.Parameters()[3]))
sys.stdout.flush()
else:
print 'Data at index %s are not the same, abort!'%i
self.w.close()
self.Save.close()

def main():
try:
name = sys.argv[1]

w = Stereo2Mono(name)
if w.isStereo() == 1:
print '%s is in stereo'%name
print 'Check for %s samples in the audio frame'%SAMPLING
w.CompareSampling()
print 'Both channels seem to be identical'
print 'Check all data frames and save to mono file'
w.CompareAndSave()
print 'Done'
else:
print '%s is already in mono'%name

except:
print '''usage : python stereo2mono.py the-stereo-wavefile.wav\n
the wave file must be encoded in 8 or 16 bits'''

if __name__ == '__main__':
main()

# 030718 19:41:48 nirinA
 
E

Erik de Castro Lopo

Raseliarison said:
hi all,
i found an unanswered question at
http://www.faqts.com/knowledge_base/index.phtml/fid/538
with possible response below. i've tried to send it at
faqt.python but can't figure out how to edit the page. so i put it here.
i want to kwon if this can convert all wave file. is there other
encodage than 8 or 16 bits for .wav files?

Yes, dozens.

As well as 8 and 16 bit integer PCM files, there are also 24
and 32 bit integer PCM, 32 and 64 bit floating point PCM,
A-law, u-law, at least 6 different forms of ADPCM (adaptive
differential PCM), GSM6.10, MP3 and many, many more.

Fortunately other than the ones listed above, all the others
are pretty rare.

Erik
--
+-----------------------------------------------------------+
Erik de Castro Lopo (e-mail address removed) (Yes it's valid)
+-----------------------------------------------------------+
J. Headley: "God, root, what is difference ?"
G. Haverland: "God can change the byte order on the CPU, root can't."
 
E

Erik de Castro Lopo

klappnase said:
Hi,

I think there might be at least 24 bit wav-files, if you have a
soundcard that supports 24 bit, may be even more, for professional use
or so.

Yep, both 24 bit PCM and 32 bit floating point encoded files
are un common use in professional and semi-pro audio recording.

Erik
 
E

Erik de Castro Lopo

Raseliarison said:
hi all,
i found an unanswered question at
http://www.faqts.com/knowledge_base/index.phtml/fid/538
with possible response below. i've tried to send it at
faqt.python but can't figure out how to edit the page. so i put it here.
i want to kwon if this can convert all wave file. is there other
encodage than 8 or 16 bits for .wav files?
any bug and comment are welcome

Just in case you don't know, there is a Python wrapper
to my own libsndfile ( http://www.zip.com.au/~erikd/libsndfile )
being developed here:

http://www.arcsin.org/archive/20030520025359.shtml

libsndfile handles a large number of different file types
(AU, AIFF, WAV and many more) as well as many encodings
(8, 16, 24, and 32 bit integer PCM, 23 and 64 bit float
PCM, A-law, u-law, MS ADPCM, IMA ADPCM, G721, G723 and
so on).

Erik
--
+-----------------------------------------------------------+
Erik de Castro Lopo (e-mail address removed) (Yes it's valid)
+-----------------------------------------------------------+
"In civilian equipment, such as computers, the number
of components alone makes miniaturization essential if
the computer is to be housed in a reasonable-sized
building." Electronics Oct. 1, 1957, p. 178
 
R

Raseliarison nirinA

Erik de Castro Lopo said:
Yep, both 24 bit PCM and 32 bit floating point encoded files
are un common use in professional and semi-pro audio recording.

Erik

Erik de Castro Lopo said:
Yes, dozens.

As well as 8 and 16 bit integer PCM files, there are also 24
and 32 bit integer PCM, 32 and 64 bit floating point PCM,
A-law, u-law, at least 6 different forms of ADPCM (adaptive
differential PCM), GSM6.10, MP3 and many, many more.

Fortunately other than the ones listed above, all the others
are pretty rare.

i found in my win98box a stereo wave file encoded in ADPCM 4 bits. of
course, an unknown format for wave module.
among all those file formats, i'm wondering which are supported by wave
module or can be manipulated with chunck module. maybe a silly question
but i'm neither a professional nor a semi-pro one.
 
R

Raseliarison nirinA

Erik de Castro Lopo said:
Just in case you don't know, there is a Python wrapper
to my own libsndfile ( http://www.zip.com.au/~erikd/libsndfile )
being developed here:

http://www.arcsin.org/archive/20030520025359.shtml

libsndfile handles a large number of different file types
(AU, AIFF, WAV and many more) as well as many encodings
(8, 16, 24, and 32 bit integer PCM, 23 and 64 bit float
PCM, A-law, u-law, MS ADPCM, IMA ADPCM, G721, G723 and
so on).

Erik

many thanks for the links.
i will have a look at it

-
nirinA
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top