473,587 Members | 2,527 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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(direct ory)

for name in os.listdir('.') :
if name.endswith(' .wav'):
w = Stereo2Mono(nam e)
if w.isStereo() == 1:
print '%s is in stereo'%name
print 'Check for %s samples in the audio frame'%SAMPLING
w.CompareSampli ng()
print 'Both channels seem to be identical'
print 'Check all data frames and save to mono file'
w.CompareAndSav e()
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/.../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.getnchan nels() == 2:
return 1
else:
return 0

def format_in(self) :
self.fmt = ''.join((str(se lf.w.getnchanne ls()),
str(self.w.gets ampwidth())))
return FORMAT.get(self .fmt)

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

def Parameters(self ):
return self.w.getparam s()

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(wa ve.struct.unpac k(
self.format_in( ),self.w.readfr ames(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.setpa rams(self.newpa rams)

for i in range(1, self.Parameters ()[3]+1):
self.UnPack = wave.struct.unp ack(
self.format_in( ), self.w.readfram es(1))
if self.Compare(se lf.UnPack) == 1:
self.Save.write frames(wave.str uct.pack(
self.format_out (), self.UnPack[0]))
sys.stdout.writ e(chr(13))
sys.stdout.writ e('%s %i/%i ' % (
self.chars[i % 4], i, self.Parameters ()[3]))
sys.stdout.flus h()
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(nam e)
if w.isStereo() == 1:
print '%s is in stereo'%name
print 'Check for %s samples in the audio frame'%SAMPLING
w.CompareSampli ng()
print 'Both channels seem to be identical'
print 'Check all data frames and save to mono file'
w.CompareAndSav e()
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

Jul 18 '05 #1
3 7945
Raseliarison nirinA wrote:

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 no****@mega-nerd.com (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."
Jul 18 '05 #2
klappnase wrote:

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
--
+-----------------------------------------------------------+
Erik de Castro Lopo no****@mega-nerd.com (Yes it's valid)
+-----------------------------------------------------------+
Windows NT : An evolutionary dead end.
Jul 18 '05 #3
Raseliarison nirinA wrote:

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 no****@mega-nerd.com (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
Jul 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
4111
by: Grace | last post by:
Hi, How do I create/access an INI file in VB .NET? I appreciate any help you can give me... Thanks!
1
6466
by: laredotornado | last post by:
Hi, I'm using PHP 4.4.4 on Apache 2 on Fedora Core 5. PHP was installed using Apache's apxs and the php library was installed to /usr/local/php. However, when I set my "error_reporting" setting to be "E_ALL", notices are still not getting reported. The perms on my file are 664, with owner root and group root. The php.ini file is located...
9
8371
by: JimmyKoolPantz | last post by:
IDE: Visual Studio 2005 Language: VB.NET Fox Pro Driver Version: 9.0.0.3504 Problem: I currently have a problem altering a DBF file. I do not get any syntax errors when running the program. However, after I alter the table and open microsoft excel to look at any changes; I get the following error: "This file is not in a recognizable...
0
5769
by: lovecarole | last post by:
hi, i am the student who should write a program about reading wav file and do the DFT. actually i don't know how to read data of the wav song and save it into the array... if i want to read 17640 every times., and i set byte read=new byte, i have the wav format paper, but i don't know how to read the data correctly into the byte array,...
42
10282
by: Ronin | last post by:
Hello guys, I have a little bit of problem with textfile operations.I would like to some help from you smart people in the forum. I have a textfile by the name of 10117053.swd. This file contains a certain data in specific format like 010110010001ADITY3104200310450001 010110010001ADITY3104600310460001 010110010001ADITY3104800310490001...
1
14908
by: homevista | last post by:
Part II: Wave file - How to read to a buffer Wave (or Wav) is the standard format for storing audio data on the PC. As software developers, we are interested in the internal structure of the file so that we can open and read data correctly before transmitting it over the phone line. Fortunately, there are plenty of good articles on the...
6
29127
by: homevista | last post by:
PART III: Putting things together In part I we examined the modem to verify that it supported voice. If so, we took a note about the voice data format that we would use. In the second part, we prepared a wave file and implemented a piece of code in C# to be used by HomeZIX to read the wave file into a buffer. Now, it’s time to put things...
11
3798
by: itdevries | last post by:
Hi, I'm trying to convert some char data I read from a binary file (using ifstream) to a float type. I've managed to convert the int types but now I need to do the float types as well but it doesn't seem to work. The code below is what I'm trying to use. Anyone see any obvious errors? or have any hints/pointers? regards, Igor float...
0
1007
by: AM | last post by:
-Hello all, I am trying to play stereo wavefiles in python with no success. I can play mono wavefiles, using either pygames mixer library, python's winsound library or wxPython's wx.Sound library, but i cannot get stereo wavefiles to play in any of these. Normally i wouldn't care and covert the files to mono, but the phase difference...
0
7920
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8215
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8347
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7973
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8220
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5718
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5394
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
2358
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1454
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.