-
Notifications
You must be signed in to change notification settings - Fork 1
/
database.py
executable file
·46 lines (38 loc) · 1.53 KB
/
database.py
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
import os
import json
from Sound import Sound
from Spectrogram import Spectrogram
class Database ():
@staticmethod
def write(data, filename):
with open(filename, "w") as file:
file.write(json.dumps(data,indent=4, separators=(",", ":")))
@staticmethod
def read(path):
with open(path) as jsonFile:
data = json.load(jsonFile)
for song in data:
yield song, data[song]
@staticmethod
def GenerateDatabase():
# iterate over songs paths
songs = {}
Components = ["Full", "Music", "Vocals"]
Groups=[]
for i in range(2,26):
if i not in (4,6,16,19,22): Groups.append(i)
for Group_No in Groups:
for i in range(1,5):
for component in Components:
song_name = "Group" + str(Group_No)+"_Song" + str(i) + "_" + component
path = os.path.join(r"D:\eng mariam\3rd\2nd\2nd semester\DSP\Tasks\Task_Dsp4\Mp3", song_name + ".mp3")
data, samplingRate = Sound.ReadFile(path)
features = Spectrogram.Features(data,samplingRate)
Hashes = []
for j in range(4):
Hashes.append(Spectrogram.Hash(features[j]))
song = Spectrogram.create_dict(song_name,Hashes)
songs.update(song)
print(Group_No)
Database.write(songs, "DataBase.json")
# Database.GenerateDatabase()