#!/usr/bin/python
#!/usr/local/bin/python
#
# Stereonormgroup
# Find the lowest L & R volumes from a group of wav files,
# then normalize all of them by that volume level.
# Useful for loudening CDs from live performances.
#
# G. Forrest Cook
# April 27, 2003
#
import sys
import os
import string

#
# Return left and right volume normalization levels for one wav file.
#
def stereo_wavfile_levs (wavfile):
    cmd = "ecasound -i " + wavfile + " -o null -eac:0,2 -ev | grep clipping"
    #print "Command: " + cmd
    ecafd = os.popen (cmd)
    line = ecafd.readline ()
    ecafd.close ()
    # Break up the return values
    sline = string.split (line)

    # Get rid of the tailing . on the returned number, scale and make int.
    gain1 = (int) (100 * string.atof (sline[6][0:-1]))
    #print "gain1 change: ", gain1

    #
    # Get Channel 2 gain change info.
    #
    cmd = "ecasound -i " + wavfile + " -o null -eac:0,1 -ev | grep clipping"
    #print "Command: " + cmd
    ecafd = os.popen (cmd)
    line = ecafd.readline ()
    ecafd.close ()
    # Break up the return values
    sline = string.split (line)

    #
    # Get rid of the tailing . on the returned number, scale and make int.
    # Rounding down is the desired behavior.
    #
    gain2 = (int) (100 * string.atof (sline[6][0:-1]))
    #print "gain2 change: ", gain2

    return (gain1, gain2)


def norm1wavfile (wavfile, gain1, gain2):
    tmp1 = "tmp1.wav"
    tmp2 = "tmp2.wav"
    if (gain1 != 100):

	print "Normalizing channel 1 by: ", gain1
	#
	# Change Channel 1 gain
	#
	cmd = "ecasound -i " + wavfile + " -o " + tmp1 + " -eac:%d"  % gain1 + ",1 > /dev/null"
	#print "cmd: ", cmd

	os.system (cmd)
    else:
	print "Gain 1 at max, not changing"
	tmp1 = wavfile

    if (gain2 != 100):
	print "Normalizing channel 2 by: ", gain2
	#
	# Change Channel 2 gain
	#
	cmd = "ecasound -i " + tmp1 + " -o " + tmp2 + " -eac:%d"  % gain2 + ",2 > /dev/null"
	#print "cmd: ", cmd
	os.system (cmd)
    else:
	print "Gain 2 at max, not changing"

    cmd = "/bin/mv tmp2.wav tmp1.wav >& /dev/null"
    os.system (cmd)
    cmd = "/bin/mv tmp1.wav " +  wavfile + " >& /dev/null"
    os.system (cmd)

#
# The main code..
#
dirname = "."
dirlist = os.listdir (dirname)
dirlist.sort()
#print dirlist
g1list = []
g2list = []

cmd = "/bin/rm ./tmp1.wav ./tmp2.wav >& /dev/null"
os.system (cmd)

for dfile in dirlist:
    if (dfile[-4:] == '.wav' and not os.path.isdir (dirname+dfile)):
        print "\nFinding gain for: ", dfile
	g1,g2 = stereo_wavfile_levs (dfile)
	g1list.append (g1)
	g2list.append (g2)
	print "Gain1: ", g1, "  Gain2:", g2
	#print "g1list: ", g1list, "g2list: ", g2list

print "\nMinimum Gain 1: ", min(g1list)
print "Minimum Gain 2: ", min(g2list)

for dfile in dirlist:
    if (dfile[-4:] == '.wav' and not os.path.isdir (dirname+dfile)):
        print "\nNormalizing: ", dfile
	norm1wavfile (dfile, min(g1list), min(g2list))

#
# Clean up.
#
cmd = "/bin/rm ./tmp1.wav ./tmp2.wav >& /dev/null"
os.system (cmd)

sys.exit (1)
