Skip to content
Merged
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
5 changes: 5 additions & 0 deletions docs/source/functional.rst
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,11 @@ Functions to perform common audio operations.

.. autofunction:: treble_biquad

:hidden:`bass_biquad`
~~~~~~~~~~~~~~~~~~~~~~~~~~~~

.. autofunction:: bass_biquad

:hidden:`deemph_biquad`
~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Expand Down
22 changes: 22 additions & 0 deletions test/test_sox_compatibility.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,28 @@ def test_treble(self):

self.assertEqual(output_waveform, sox_output_waveform, atol=1e-4, rtol=1e-5)

@unittest.skipIf("sox" not in BACKENDS, "sox not available")
@AudioBackendScope("sox")
def test_bass(self):
"""
Test biquad bass filter, compare to SoX implementation
"""

central_freq = 1000
q = 0.707
gain = 40

noise_filepath = common_utils.get_asset_path('whitenoise.wav')
E = torchaudio.sox_effects.SoxEffectsChain()
E.set_input_file(noise_filepath)
E.append_effect_to_chain("bass", [gain, central_freq, str(q) + 'q'])
sox_output_waveform, sr = E.sox_build_flow_effects()

waveform, sample_rate = torchaudio.load(noise_filepath, normalization=True)
output_waveform = F.bass_biquad(waveform, sample_rate, gain, central_freq, q)

self.assertEqual(output_waveform, sox_output_waveform, atol=1.5e-4, rtol=1e-5)

@unittest.skipIf("sox" not in BACKENDS, "sox not available")
@AudioBackendScope("sox")
def test_deemph(self):
Expand Down
15 changes: 15 additions & 0 deletions test/torchscript_consistency_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,21 @@ def func(tensor):

self._assert_consistency(func, waveform)

def test_bass(self):
if self.dtype == torch.float64:
raise unittest.SkipTest("This test is known to fail for float64")

waveform = common_utils.get_whitenoise(sample_rate=44100)

def func(tensor):
sample_rate = 44100
gain = 40.
central_freq = 1000.
q = 0.707
return F.bass_biquad(tensor, sample_rate, gain, central_freq, q)

self._assert_consistency(func, waveform)

def test_deemph(self):
if self.dtype == torch.float64:
raise unittest.SkipTest("This test is known to fail for float64")
Expand Down
42 changes: 42 additions & 0 deletions torchaudio/functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"equalizer_biquad",
"band_biquad",
"treble_biquad",
"bass_biquad",
"deemph_biquad",
"riaa_biquad",
"biquad",
Expand Down Expand Up @@ -983,6 +984,47 @@ def treble_biquad(
return biquad(waveform, b0, b1, b2, a0, a1, a2)


def bass_biquad(
waveform: Tensor,
sample_rate: int,
gain: float,
central_freq: float = 100,
Q: float = 0.707
) -> Tensor:
r"""Design a bass tone-control effect. Similar to SoX implementation.

Args:
waveform (Tensor): audio waveform of dimension of `(..., time)`
sample_rate (int): sampling rate of the waveform, e.g. 44100 (Hz)
gain (float): desired gain at the boost (or attenuation) in dB.
central_freq (float, optional): central frequency (in Hz). (Default: ``100``)
Q (float, optional): https://en.wikipedia.org/wiki/Q_factor (Default: ``0.707``).

Returns:
Tensor: Waveform of dimension of `(..., time)`

References:
http://sox.sourceforge.net/sox.html
https://www.w3.org/2011/audio/audio-eq-cookbook.html#APF
"""
w0 = 2 * math.pi * central_freq / sample_rate
alpha = math.sin(w0) / 2 / Q
A = math.exp(gain / 40 * math.log(10))

temp1 = 2 * math.sqrt(A) * alpha
temp2 = (A - 1) * math.cos(w0)
temp3 = (A + 1) * math.cos(w0)

b0 = A * ((A + 1) - temp2 + temp1)
b1 = 2 * A * ((A - 1) - temp3)
b2 = A * ((A + 1) - temp2 - temp1)
a0 = (A + 1) + temp2 + temp1
a1 = -2 * ((A - 1) + temp3)
a2 = (A + 1) + temp2 - temp1

return biquad(waveform, b0 / a0, b1 / a0, b2 / a0, a0 / a0, a1 / a0, a2 / a0)


def deemph_biquad(
waveform: Tensor,
sample_rate: int
Expand Down