/
loudness.py
183 lines (150 loc) · 6.11 KB
/
loudness.py
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
"""
from __future__ import division
__author__ = 'Maarten Versteegh'
__date__ = 'Fri Mar 2 15:34:27 2012'
from scikits.audiolab import wavread, wavwrite
import numpy as np
from scipy.signal.filter_design import bilinear
from scipy.signal import lfilter
import os
import glob
import argparse
def rms(a):
return np.sqrt(np.sum(a**2)/len(a))
def A_weighting_filter(fs):
"""construct an a-weighting filter at the specified samplerate
from here: http://www.mathworks.com/matlabcentral/fileexchange/69
"""
f1, f2, f3, f4, A1000 = 20.598997, 107.65265, 737.86223, 12194.217, 1.9997
NUMs = [(2 * np.pi * f4) ** 2 * (10 ** (A1000/20)), 0, 0, 0, 0]
DENs = np.convolve([1, 4 * np.pi * f4, (2 * np.pi * f4) ** 2],
[1, 4 * np.pi * f1, (2 * np.pi * f1) ** 2], mode='full')
DENs = np.convolve(np.convolve(DENs, [1, 2 * np.pi * f3], mode='full'),
[1, 2 * np.pi * f2], mode='full')
return bilinear(NUMs, DENs, fs)
def A_weight(sig, fs):
B, A = A_weighting_filter(fs)
return lfilter(B, A, sig)
def analyze_dir(dirname):
props = get_props_dir(dirname)
for f in props:
display_properties(f, props[f])
def display_props(props):
for f in props:
display_properties(f, props[f])
def get_props_dir(dirname):
props = {}
for f in glob.glob(os.path.join(dirname, '*.wav')):
props[os.path.basename(f)] = analyze(*wavread(f))
return props
def remove_DC_offset(sig):
"""DC offset fucks with the filters"""
return sig - np.mean(sig)
def dB(level):
return 20 * np.log10(level)
def analyze(sig, fs, enc):
props = {}
props['sig'] = sig
props['fs'] = fs
props['enc'] = enc
props['dc'] = np.mean(sig)
props['peak'] = np.max(np.abs(sig))
props['rms'] = rms(sig)
props['crest'] = props['peak']/props['rms']
weighted_sig = A_weight(sig, fs)
props['weighted'] = rms(weighted_sig)
return props
def display_properties(name, props):
print '-' * 20
print 'Properties of {0:s}'.format(name)
print 'Length:\t\t\t{0:.3f}s'.format(len(props['sig']) / props['fs'])
print 'Sampling rate:\t\t{0:d}kHz'.format(props['fs'])
print 'Encoding:\t\t{0:s}'.format(props['enc'])
print 'Peak level:\t\t{0:.3f} ({1:.3f}dBFS)'.format(props['peak'],
dB(props['peak']))
print 'RMS (unweighted):\t{0:.3f} ({1:.3f}dBFS)'.format(props['rms'],
dB(props['rms']))
print 'RMS (A-weighted):\t{0:.3f} ({1:.3f}dBFS, {2:.3f}dB)'.format(
props['weighted'],
dB(props['weighted']),
dB(props['weighted']/props['rms']))
print 'Crest factor:\t\t{0:.3f} ({1:.3f}dB)'.format(props['crest'],
dB(props['crest']))
print 'DC offset:\t\t{0:.3f}'.format(props['dc'])
print '-' * 20
def match_dir(props, prop, outdir, ext):
ref_prop = min(props.keys(), key=lambda x: props[x][prop])
ref_peak = max(props.keys(),
key=lambda x: (props[x]['peak'] *
props[ref_prop][prop] / props[x][prop]))
for f in props:
a = props[f]['sig'] * (props[ref_prop][prop] /
(props[f][prop] *
props[ref_peak]['peak']))
bname = os.path.basename(f)
wavwrite(a,
os.path.join(outdir,
os.path.splitext(bname)[0] + ext + '.wav'),
props[f]['fs'],
props[f]['enc'])
def run():
parser = argparse.ArgumentParser(
prog='loudnessmatcher.py',
formatter_class=argparse.RawDescriptionHelpFormatter,
description="""Match .wav files by loudness.
By default only displays loudness properties of files in directory.""",
epilog="""Example usage:
$ ./loudness.py -s -m rms -o my_results
matches all the .wav files in the current directory
and stores the output in "my_results/"
""")
parser.add_argument('-s', '--silent',
action='store_true',
dest='silent',
help="don't display loudness properties of .wav files")
parser.add_argument('-i',
nargs=1,
action='store',
default='.',
dest='indir',
help=('directory containing the .wav'
' files to be analyzed'))
parser.add_argument('-o',
nargs=1,
action='store',
default=argparse.SUPPRESS,
dest='outdir',
help='destination of matched .wav files')
parser.add_argument('-m', '--match',
nargs=1,
action='store',
choices=['peak', 'rms', 'weighted'],
dest='match',
default=argparse.SUPPRESS,
help=('match .wav files by a property. '
'valid choices are "peak" (peak audio '
'level), "rms" (root mean square in '
'time domain) and "weighted" (a-weighted '
'rms) [default]'))
parser.add_argument('-e', '--ext',
nargs=1,
action='store',
default='_matched',
dest='ext',
help='extension to add to matched filenames')
options = vars(parser.parse_args())
props = get_props_dir(options['indir'][0])
if not 'outdir' in options:
options['outdir'] = options['indir']
if not os.path.exists(options['outdir'][0]):
os.mkdir(options['outdir'][0])
if not options['silent']:
display_props(props)
if 'match' in options:
match_dir(props, options['match'][0],
options['outdir'][0], options['ext'][0])
if __name__ == '__main__':
run()