This repository was archived by the owner on Mar 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspectrogramHash.py
More file actions
61 lines (45 loc) · 1.77 KB
/
Copy pathspectrogramHash.py
File metadata and controls
61 lines (45 loc) · 1.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import numpy as np
import matplotlib.pyplot as plt
import soundfile as sf
import hashlib
import os
def toDatabase(folder: str) -> list[[dict[str, list[int]]]]:
files = [file for file in os.listdir(folder) if file.endswith('.wav')]
database: list[[dict[str, list[int]]]] = []
for file in files:
file_path = os.path.join(folder, file)
truncated_Hash, spectrogram = spectrogramHash(file_path)
entry = {file: {'hash': truncated_Hash, 'spectrogram': spectrogram.tolist()}}
database.append(entry)
return database
def spectrogramHash(audio_file: str, keyLength: int = 10) -> dict[str, list[int]]:
data, samplerate = sf.read(audio_file)
if len(data.shape) > 1:
data = data.mean(axis=1)
window_size = 1024
hop_size = 512
n_samples = len(data)
n_overlap = window_size - hop_size
n_windows = (n_samples - n_overlap) // hop_size
spectrogram = []
for i in range(n_windows):
start = i * hop_size
end = start + window_size
segment = data[start:end]
windowed_segment = segment * np.hamming(window_size)
spectrum = np.abs(np.fft.rfft(windowed_segment, window_size))
spectrogram.append(spectrum)
spectrogram_array = np.array(spectrogram)
concatenated_hash = b''
for row in spectrogram_array:
row_bytes = row.tobytes()
sha256_hash = hashlib.sha256(row_bytes).hexdigest().encode('utf-8')
concatenated_hash += sha256_hash
unique_hash = concatenated_hash.hex()
truncated_Hash = unique_hash[:keyLength]
return truncated_Hash, spectrogram_array
hashAudio = spectrogramHash("./data/1.wav")
print(f"Hash audio is {hashAudio}")
data = './data'
database = toDatabase(data)
print(database[1])