Skip to content

Commit

Permalink
feat: add helper method to check if a track is silence (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronabebe authored Mar 4, 2024
1 parent 2159f91 commit 020d0ce
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 14 deletions.
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

0 comments on commit 020d0ce

Please sign in to comment.