diff --git a/src/apollon/audio.py b/src/apollon/audio.py index 0028fd32453a7c95d484fe68a02381e21db2e975..1500501164f4ac3c7589d53b2e9ff753049ceb76 100644 --- a/src/apollon/audio.py +++ b/src/apollon/audio.py @@ -10,6 +10,7 @@ Functions: fti16 Cast float to int16. load_audio Load .wav file. """ +import hashlib import pathlib import matplotlib.pyplot as plt @@ -40,6 +41,12 @@ class AudioFile: """Return source file name.""" return self._path.name + @property + def hash(self) -> str: + """Compute sha256 hash.""" + obj = hashlib.sha256(self.data.tobytes()) + return obj.hexdigest() + @property def n_channels(self) -> int: """Return number of channels.""" diff --git a/tests/test_audio.py b/tests/test_audio.py index 812176597584fcc50d87bfe5e17c4e7c08490950..c8f794c06b74d3fc3345797a73993578912dc1bf 100644 --- a/tests/test_audio.py +++ b/tests/test_audio.py @@ -10,6 +10,11 @@ class TestAudioFile(unittest.TestCase): def test_path_is_sting(self): self.assertIsInstance(self.snd.file_name, str) + def test_hash(self): + snd2 = AudioFile('audio/beat.wav') + self.assertEqual(self.snd.hash, snd2.hash) + + class TestAudioFileReadMono(unittest.TestCase): def setUp(self): self.snd = AudioFile('audio/beat.wav')