sintonia/library/ecasound/examples/normalize.py
Paul Baranowski 6a39e4f5f5 Removed the Debian directory.
Renamed directory "dev" to "dev_tools".

Replaced the ecasound-2.7.2 with a new download of ecasound.  The reason is
that the previous ecasound directory had all the Makefiles checked in with
hardcoded paths from Naomi's computer.  This prevented anyone else from
being able to build.  I copied over the modified version of ecacontrol.py.
2011-03-09 18:20:34 -05:00

67 lines
1.9 KiB
Python

#!/usr/local/bin/python
# -----------------------------------------------------------------------
# Example of an audio file volume normalizer (peak amplitude).
#
# 1. Takes the filename to be processed from the command line.
# 2. Runs the audio file through ecasound's volume analyzer.
# 3. If the signal isn't normalized (gain factor > 1), it is amplified
# (gain factor from the analyzer) and the original file is replaced.
# 4. Removes the temp file and exits.
#
# Known "bugs":
# - if ecasound's internal sample rate doesn't match audio file's
# sample rate, signal will get resampled twice
# - might change the audio format of the processed files
# -----------------------------------------------------------------------
import os
import sys
from tempfile import mktemp
from pyeca import *
# check arguments
if len(sys.argv) < 2: sys.exit(-1)
filename = sys.argv[1]
if not os.path.isfile(filename): sys.exit(-1)
tmpfile = mktemp(".wav")
# create and configure the 'analyze' chainsetup
e = ECA_CONTROL_INTERFACE()
e.command("cs-add analyze")
e.command("c-add 1")
print "Normalizing file " + filename
print "Using tempfile " + tmpfile
e.command("ai-add " + filename)
e.command("ao-add " + tmpfile)
e.command("cop-add -ev")
print "Analyzing sample data."
e.command("cs-connect")
e.command("run")
e.command("cop-select 1")
e.command("copp-select 2")
e.command("copp-get")
gain_factor = e.last_float()
e.command("cs-disconnect")
if gain_factor <= 1:
print "File already normalized!"
sys.exit(0)
# create and config the 'apply' chainsetup
a = ECA_CONTROL_INTERFACE()
a.command("cs-add apply")
a.command("c-add 1")
a.command("ai-add " + tmpfile)
a.command("ao-add " + filename)
print "Applying gain factor: ", gain_factor
a.command("cop-add -ea:100")
a.command("cop-select 1")
a.command("copp-select 1")
a.command_float_arg("copp-set", gain_factor * 100)
a.command("cs-connect")
a.command("run")
# remove the tempfile and exit
os.remove(tmpfile)
print "Done!"
sys.exit(0)