Skip to content

Commit

Permalink
Fixing FFT issue with odd signals. Making state default to None in in…
Browse files Browse the repository at this point in the history
…stantiate. (#50)

* Fixing FFT issue with odd signals. Making state default to None in instantiate.

* If loudness_cutoff is None, use faster excerpt routine.

Co-authored-by: pseeth <[email protected]>
  • Loading branch information
pseeth and pseeth authored Aug 24, 2022
1 parent 911d31e commit 4f07af6
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 17 deletions.
2 changes: 1 addition & 1 deletion audiotools/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = "0.3.10"
__version__ = "0.3.11"
from .core import AudioSignal, STFTParams, Meter, util
from . import metrics
from . import data
Expand Down
18 changes: 10 additions & 8 deletions audiotools/core/audio_signal.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,16 +111,18 @@ def excerpt(cls, audio_path, offset=None, duration=None, state=None, **kwargs):
def salient_excerpt(
cls, audio_path, loudness_cutoff=None, num_tries=None, state=None, **kwargs
):
loudness_cutoff = -np.inf if loudness_cutoff is None else loudness_cutoff
state = util.random_state(state)
loudness = -np.inf
num_try = 0
while loudness <= loudness_cutoff:
if loudness_cutoff is None:
excerpt = cls.excerpt(audio_path, state=state, **kwargs)
loudness = excerpt.loudness()
num_try += 1
if num_tries is not None and num_try >= num_tries:
break
else:
loudness = -np.inf
num_try = 0
while loudness <= loudness_cutoff:
excerpt = cls.excerpt(audio_path, state=state, **kwargs)
loudness = excerpt.loudness()
num_try += 1
if num_tries is not None and num_try >= num_tries:
break
return excerpt

@classmethod
Expand Down
11 changes: 6 additions & 5 deletions audiotools/core/effects.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,16 @@ def convolve(self, other, start_at_max=True):
delta = torch.zeros_like(other.audio_data)
delta[..., 0] = 1

delta_fft = torch.fft.rfft(delta)
other_fft = torch.fft.rfft(other.audio_data)
self_fft = torch.fft.rfft(self.audio_data)
length = self.signal_length
delta_fft = torch.fft.rfft(delta, length)
other_fft = torch.fft.rfft(other.audio_data, length)
self_fft = torch.fft.rfft(self.audio_data, length)

convolved_fft = other_fft * self_fft
convolved_audio = torch.fft.irfft(convolved_fft)
convolved_audio = torch.fft.irfft(convolved_fft, length)

delta_convolved_fft = other_fft * delta_fft
delta_audio = torch.fft.irfft(delta_convolved_fft)
delta_audio = torch.fft.irfft(delta_convolved_fft, length)

# Use the delta to rescale the audio exactly as needed.
delta_max = delta_audio.abs().max(dim=-1, keepdims=True)[0]
Expand Down
4 changes: 2 additions & 2 deletions audiotools/data/transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def __call__(self, *args, **kwargs):

def instantiate(
self,
state: RandomState,
state: RandomState = None,
signal: AudioSignal = None,
):
state = util.random_state(state)
Expand Down Expand Up @@ -105,7 +105,7 @@ def instantiate(

def batch_instantiate(
self,
states: list,
states: list = None,
signal: AudioSignal = None,
):
kwargs = []
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

setup(
name="audiotools",
version="0.3.10",
version="0.3.11",
classifiers=[
"Intended Audience :: Developers",
"Intended Audience :: Education",
Expand Down
5 changes: 5 additions & 0 deletions tests/core/test_audio_signal.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,11 @@ def test_salient_excerpt(loudness_cutoff):
signal = AudioSignal.salient_excerpt(
f.name, loudness_cutoff=np.inf, duration=1, num_tries=10
)
signal = AudioSignal.salient_excerpt(
f.name,
loudness_cutoff=None,
duration=1,
)


def test_arithmetic():
Expand Down

0 comments on commit 4f07af6

Please sign in to comment.