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 pathspectrogramFinal.py
More file actions
Latest commit
157 lines (120 loc) · 4.76 KB
/
Copy pathspectrogramFinal.py
File metadata and controls
157 lines (120 loc) · 4.76 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
importnumpyasnp
importsoundfileassf
importmatplotlib.pyplotasplt
importhashlib
importos
importpygame
importtime
defplayAudio(file_path: str):
pygame.init()
pygame.mixer.init()
screen=pygame.display.set_mode((300, 200))
pygame.display.set_caption('Music Player')
try:
pygame.mixer.music.load(file_path)
pygame.mixer.music.play()
running: bool=True
whilerunning:
foreventinpygame.event.get():
ifevent.type==pygame.QUIT:
running=False
ifnotpygame.mixer.music.get_busy():
running=False
screen.fill((255, 255, 255))
font=pygame.font.Font(None, 36)
filename=os.path.basename(file_path)
text=font.render(filename, True, (50, 50, 0))
screen.blit(text, (10, 10))
pygame.display.update()
pygame.time.Clock().tick(60)
exceptpygame.errorase:
print(f"Unable to play music: {e}")
finally:
pygame.quit()
defcompareToDatabase(file: str, folderPath: str, databaseGlobal: list[dict[str, str]]) ->bool:
checker_hash, fileValue=spectrogramHash(file)
isFound: bool=False
ifdatabaseGlobalisnotNone:
foritemindatabaseGlobal:
ifitem['key'] ==checker_hash:
playAudio(item['file_name'])
playAudio(fileValue)
print("Found a matching entry in the database:", item['file_name'])
isFound=True
returnTrue
database: list[dict[str, str]] =toDatabase(folderPath)
saveDatabase(databaseGlobal, database)
forentryindatabase:
ifentry['key'] ==checker_hash:
playAudio(entry['file_name'])
playAudio(fileValue)
print("Found a matching entry in the database:", entry['file_name'])
returnTrue
print("404 not found")
returnFalse
deftoDatabase(folder: str) ->list[dict[str, str]]:
files= [fileforfileinos.listdir(folder) if (file.endswith('.wav') orfile.endswith('.mp3'))]
database: list[dict[str, str]] = []
forfileinfiles:
file_path: str=os.path.join(folder, file)
truncated_Hash, filename=spectrogramHash(file_path)
entry: dict[str, str] = {'key': truncated_Hash, 'file_name': filename }
database.append(entry)
returndatabase
defsaveDatabase(databaseGlobal: list[dict[str, str]], database: list[dict[str, str]]):
databaseGlobal.extend(database)
writeToDatabase(databaseGlobal)
defwriteToDatabase(database: list[dict[str, str]]):
withopen('./database/data.config', 'r+') asfile:
foritemindatabase:
file.write(f"key: {item['key']}, file_name: {item['file_name']}\n")
defreadDatabase(path: str) ->list[dict[str, str]]:
read_data= []
withopen(path, 'r+') asfile:
lines=file.readlines()
forlineinlines:
parts=line.strip().split(', ')
data_dict= {}
forpartinparts:
key, value=part.split(': ')
data_dict[key.strip()] =value.strip()
read_data.append(data_dict)
returnread_data
defspectrogramHash(audio_file: str, keyLength: int=100) ->dict[str, str]:
data, _=sf.read(audio_file)
iflen(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= []
foriinrange(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''
forrowinspectrogram_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]
returntruncated_Hash, audio_file
checker: str='./sample2.wav'
data_folder: str='./data/bbc'
# database: list[dict[str, str]] = toDatabase(data_folder)
# for data in database:
# print(data)
globalDatabase=readDatabase('./database/data.config')
if__name__=="__main__":
start_time=time.time()
result=compareToDatabase(checker, data_folder, globalDatabase)
end_time=time.time()
execution_time=end_time-start_time
print(f"Execution time: {execution_time} seconds")