-
Notifications
You must be signed in to change notification settings - Fork 27
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
migperfer
wants to merge
2
commits into
shichao-an:master
Choose a base branch
from
migperfer:ponderation-curves
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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 | ||
|
@@ -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 | ||
|
@@ -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 | ||
|
@@ -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() | ||
|
@@ -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, | ||
-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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shall we pull the config for frame_rate from There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
@@ -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() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.