Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ponderation curves #32

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ System packages
~~~~~~~~~~~~~~~
On Debian/Ubuntu::

$ sudo apt-get install portaudio19-dev python-dev alsa-utils
$ sudo apt-get install portaudio19-dev python-dev alsa-utils ffmpeg

On Fedora/RHEL::

Expand Down
7 changes: 7 additions & 0 deletions soundmeter/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ def parse_args():
help='verbose mode')
segment_help = 'audio segment length recorded in seconds (defaults to 0.5)'
parser.add_argument('--segment', metavar='SECONDS', help=segment_help)
valuetype_help = 'select dB or RMS, default RMS'
parser.add_argument('--valuetype', type=str, choices=['db', 'rms', 'dB', 'Rms', 'RMS'],
help=valuetype_help)
curve_help = 'weighting curve to use, can be A, B, C or Z'
parser.add_argument('-w', '--weightingcurve', type=str, choices=['A', 'B', 'C', 'Z', 'a', 'b', 'c', 'z'],
help=curve_help)

# Extra validation of arguments
args = parser.parse_args()
Expand All @@ -45,6 +51,7 @@ def parse_args():
msg = ('-c/--collect should not be used with -a/--action '
'or -t/--trigger')
raise parser.error(msg)

if args.segment:
try:
segment = float(args.segment)
Expand Down
70 changes: 59 additions & 11 deletions soundmeter/meter.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
import wave
import signal
import six
import numpy as np
from scipy.signal import lfilter
import subprocess
import math
import sys
import time
import warnings
Expand Down Expand Up @@ -34,7 +37,7 @@ class StopException(Exception):

def __init__(self, collect=False, seconds=None, action=None,
threshold=None, num=None, script=None, log=None,
verbose=False, segment=None, profile=None, *args, **kwargs):
verbose=False, segment=None, profile=None, valuetype="rms", weightingcurve="z", *args, **kwargs):
"""
:param bool collect: A boolean indicating whether collecting RMS values
:param float seconds: A float representing number of seconds to run the
Expand All @@ -49,6 +52,9 @@ def __init__(self, collect=False, seconds=None, action=None,
:param bool verbose: A boolean for verbose mode
:param float segment: A float representing `AUDIO_SEGMENT_LENGTH`
:param str profile: The config profile
:param str values: The type of values to use, RMS or dB. Default is RMS
:param str weightingcurve: The weighting curve to use when measuring, default is Z which is plane.
Possible values are A, B, C, Z.
"""

global _soundmeter
Expand All @@ -74,6 +80,8 @@ def __init__(self, collect=False, seconds=None, action=None,
self.verbose = verbose
self.segment = segment
self.is_running = False
self.valuestype = valuetype.lower()
self.weightingcurve = weightingcurve.lower()
self._graceful = False # Graceful stop switch
self._timeout = False
self._timer = None
Expand Down Expand Up @@ -112,7 +120,7 @@ def start(self):
if self.verbose:
self._timer = time.time()
if self.collect:
print('Collecting RMS values...')
print('Collecting values...')
if self.action:
# Interpret threshold
self.get_threshold()
Expand All @@ -124,27 +132,64 @@ def start(self):
record.send(True) # Record stream `AUDIO_SEGMENT_LENGTH' long
data = self.output.getvalue()
segment = pydub.AudioSegment(data)
rms = segment.rms
if self.valuestype == "rms":
soundvalue = segment.rms
elif self.valuestype == "db":
soundvalue = self.weightedvalue(segment, self.weightingcurve)
else:
sys.exit(1) # Value type must be either db or rms
if self.collect:
self.collect_rms(rms)
self.meter(rms)
self.collect_rms(soundvalue)
self.meter(soundvalue)
if self.action:
if self.is_triggered(rms):
self.execute(rms)
self.monitor(rms)
if self.is_triggered(soundvalue):
self.execute(soundvalue)
self.monitor(soundvalue)
self.is_running = False
self.stop()

except self.__class__.StopException:
self.is_running = False
self.stop()

def meter(self, rms):
def meter(self, value):
if not self._graceful:
sys.stdout.write('\r%10d ' % rms)
sys.stdout.write('\r%10d %s' % (value, self.valuestype))
sys.stdout.flush()
if self.log:
self.logging.info(rms)
self.logging.info(value)

def weightedvalue(self, segment, curve):
"""
This function it is used to calculate the average dB level using the weighting curves.
The zeros and poles were created by using those functions:
http://siggigue.github.io/pyfilterbank/splweighting.html
with the sample rate hardcoded to 44100Hz
More info about weighting curves:
https://www.cirrusresearch.co.uk/blog/2011/08/what-are-a-c-z-frequency-weightings/
:param segment: The segment to calculate the weighted average
:param curve: The type of curve to apply Z, A, B or C
:return: The weighted average
"""
if curve.lower() == "z":
return segment.dBFS
if curve.lower() == "a":
b = np.array([0.25574113, -0.51148225, -0.25574113, 1.0229645, -0.25574113,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jajaxdlol Are these hardcoded numbers only for the 44100 frequency?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Those are hardcoded for 44100 freq yes. I'm not good at filter design so I have no idea on how to create a function to create curves according to frequency, I wasn't sure if you wanted to add a dependency to another library.

-0.51148225, 0.25574113])
a = np.array([1.00000000e+00, -4.01957618e+00, 6.18940644e+00,
-4.45319890e+00, 1.42084295e+00, -1.41825474e-01,
4.35117723e-03])
if curve.lower() == "b":
b = np.array([0.21727294, -0.21727294, -0.43454587, 0.43454587, 0.21727294,
-0.21727294])
a = np.array([1., -3.11234468, 3.36634059, -1.40032549, 0.15112883,
-0.00479909])
if curve.lower() == "c":
b = np.array([0.21700856, 0., -0.43401712, 0., 0.21700856]),
a = np.array([1., -2.13467496, 1.27933353, -0.14955985, 0.0049087])
y = np.float32(lfilter(b, a, segment.get_array_of_samples()))
audio_segment = pydub.AudioSegment(y.tobytes(), frame_rate=44100, sample_width=y.dtype.itemsize, channels=1)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shall we pull the config for frame_rate from self.config.RATE?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right! But for that, we should have weighting curves that depends on frequency.

return audio_segment.dBFS

def graceful(self):
"""Graceful stop so that the while loop in start() will stop after the
Expand Down Expand Up @@ -335,3 +380,6 @@ def sigalrm_handler(signum, frame):
# Register signal handlers
signal.signal(signal.SIGINT, sigint_handler)
signal.signal(signal.SIGALRM, sigalrm_handler)

if __name__ == '__main__':
main()