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

Remove 'NonPositiveEnergyError' exception when mix audio #922

Merged
merged 12 commits into from
Jan 2, 2023

Conversation

drawfish
Copy link
Contributor

Remove 'NonPositiveEnergyError' exception when mix audio, still check if energy<=0.0 but just print warning.
For more detail reason, please see this issue.

@desh2608
Copy link
Collaborator

You may need to modify the tests in accordance with the new behavior.

lhotse/audio.py Outdated
@@ -1236,8 +1236,8 @@ def __init__(
self.reference_energy = reference_energy

if self.reference_energy <= 0.0:
raise NonPositiveEnergyError(
f"To perform mix, energy must be non-zero and non-negative (got {self.reference_energy})"
logging.warning(
Copy link
Collaborator

Choose a reason for hiding this comment

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

I would avoid the warning at all -- it will just annoy the users.

lhotse/audio.py Outdated
@@ -1323,6 +1323,9 @@ def add_to_mix(
if audio.size == 0:
return # do nothing for empty arrays

if self.reference_energy <= 0.0:
Copy link
Collaborator

Choose a reason for hiding this comment

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

hmmm in that case we have audio data with all zeroes, and want to mix something in -- the result should be mixed in audio rather than all zeros. I would remove this if here

lhotse/audio.py Outdated
@@ -1331,16 +1334,16 @@ def add_to_mix(
gain = 1.0
if snr is not None:
Copy link
Collaborator

@pzelasko pzelasko Dec 14, 2022

Choose a reason for hiding this comment

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

I would change this to

Suggested change
if snr is not None:
if snr is not None and self.reference_energy > 0:

lhotse/audio.py Outdated
# we need to take a square root of the energy ratio.
gain = sqrt(target_energy / added_audio_energy)
else:
logging.warning(
Copy link
Collaborator

Choose a reason for hiding this comment

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

We don't need the warning, it will just annoy users.

), f"To perform mix, energy must be non-zero and non-negative (got {self.reference_energy})"
if self.reference_energy <= 0.0:
logging.warning(
f"To perform mix, energy must be non-zero and non-negative (got {self.reference_energy}), ignore mix operation."
Copy link
Collaborator

Choose a reason for hiding this comment

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

Same as above, I'd just remove this.

Copy link
Collaborator

Choose a reason for hiding this comment

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

The rest of the comments I made above in audio.py are also relevant for this file

Copy link
Collaborator

@pzelasko pzelasko left a comment

Choose a reason for hiding this comment

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

Thanks @drawfish, I left some comments, would you be able to address those?

@drawfish
Copy link
Contributor Author

Thanks @drawfish, I left some comments, would you be able to address those?

Done~

Copy link
Collaborator

@pzelasko pzelasko left a comment

Choose a reason for hiding this comment

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

Thanks, one last round of review and LGTM!

with pytest.raises(NonPositiveEnergyError):
mixed.load_audio()
mix_cut_samples = mixed.load_audio()
assert mix_cut_samples.shape[1] == sr
Copy link
Collaborator

@pzelasko pzelasko Dec 15, 2022

Choose a reason for hiding this comment

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

can you additionally assert that np.testing.assert_equal(rand_cut.load_audio(), mix_cut_samples)?

with pytest.raises(NonPositiveEnergyError):
mixed.load_audio()
mix_cut_samples = mixed.load_audio()
assert mix_cut_samples.shape[1] == sr

@pytest.mark.parametrize("snr", [None, 10])
def test_fault_tolerant_loading_skips_cut(self, snr):
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can you remove this test? It does not do anything right now since mixing zero cuts cannot trigger failure in audio loading.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

OK~

@pzelasko pzelasko added this to the v1.12 milestone Jan 2, 2023
@pzelasko pzelasko merged commit 1a2e821 into lhotse-speech:master Jan 2, 2023
@pzelasko
Copy link
Collaborator

pzelasko commented Jan 2, 2023

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants