From 03bef8ff6fdd07977d54e2140d7a7587fc16eade Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Michael=20Bla=C3=9F?= <michael.blass@uni-hamburg.de>
Date: Tue, 8 Sep 2020 11:36:37 +0200
Subject: [PATCH] Added ``hash`` property to ``AudioFile``.

---
 src/apollon/audio.py | 7 +++++++
 tests/test_audio.py  | 5 +++++
 2 files changed, 12 insertions(+)

diff --git a/src/apollon/audio.py b/src/apollon/audio.py
index 0028fd3..1500501 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 8121765..c8f794c 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')
-- 
GitLab