File: normalize.py

package info (click to toggle)
ecasound 2.9.3-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 6,292 kB
  • sloc: cpp: 39,475; sh: 4,335; lisp: 1,918; ansic: 1,883; makefile: 888; python: 617; ruby: 202
file content (67 lines) | stat: -rw-r--r-- 1,995 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/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)