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

feat: add helper method to check if a track is silence #21

Merged
merged 1 commit into from
Mar 4, 2024
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
13 changes: 13 additions & 0 deletions src/nendo/schema/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,19 @@ def slice(self, end: float, start: Optional[float] = 0) -> np.ndarray:
end_frame = int(end * self.sr)
return self.signal[:, start_frame:end_frame]

def is_silent(self, threshold: float = 0.01) -> bool:
"""Check if a track is silent using RMS energy and a threshold.

Args:
threshold (float, optional): The threshold for silence.
Defaults to 0.01.

Returns:
bool: True if the track is silent, False otherwise.
"""
rms = np.sqrt(np.mean(self.signal ** 2))
return rms < threshold

def save(self) -> NendoTrack:
"""Save the track to the library.

Expand Down
21 changes: 7 additions & 14 deletions tests/test_nendo_track.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,20 +49,13 @@ def test_has_relationship(self):
self.assertTrue(track.has_relationship("stem"))
self.assertFalse(track.has_relationship("outpainting"))

# def test_add_relationship(self):
# resource = NendoResource(
# file_path="tests/assets/",
# file_name="test.wav",
# resource_type="audio",
# meta={
# "checksum": "file_checksum",
# "original_filename": os.path.basename("tests/assets/test.wav"),
# },
# )
# track = NendoTrack(id=uuid.uuid4(), user_id=uuid.uuid4(), resource=resource)

# track.add_relationship(track_id=uuid.uuid4(), relationship_type="stem")
# self.assertTrue(track.has_relationship("stem"))
def test_is_silent(self):
"""Test the `NendoTrack.is_silent()` method."""
nd.library.reset(force=True)
track = nd.library.add_track(file_path="tests/assets/test.wav")
self.assertFalse(track.is_silent())
silent_track = nd.library.add_track(file_path="tests/assets/silence.mp3")
self.assertTrue(silent_track.is_silent())

def test_signal_sr_properties_exist(self):
"""Test the `NendoTrack.signal` and `NendoTrack.sr` properties."""
Expand Down
Loading