-
Notifications
You must be signed in to change notification settings - Fork 1
/
Spectrogram.py
executable file
·40 lines (32 loc) · 1.23 KB
/
Spectrogram.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
import librosa
from PIL import Image
from scipy import signal
from imagehash import hex_to_hash ,phash
class Spectrogram():
@staticmethod
def Features(data, SamplingRate):
f, d, colorMesh = signal.spectrogram(data, fs=SamplingRate, window='hann')
melspectro = librosa.feature.melspectrogram(data, sr=SamplingRate,S=colorMesh)
mfccs = librosa.feature.mfcc(data.astype('float64'), sr=SamplingRate, S=colorMesh)
chroma_stft = librosa.feature.chroma_stft(data, sr=SamplingRate, S=colorMesh)
return [colorMesh,melspectro, mfccs, chroma_stft]
@staticmethod
def Hash(array):
arr = Image.fromarray(array)
hash = phash(arr, hash_size=16).__str__()
return hash
@staticmethod
def getSimilarity(song1,song2):
similarity = 1 - (hex_to_hash(song1) - hex_to_hash(song2))/256.0
return similarity
@staticmethod
def create_dict(song_name, Hashes):
song = {
song_name: {
"spectrogram_hash": Hashes[0],
"melspectrogram": Hashes[1],
"mfcc": Hashes[2],
"chroma_stft": Hashes[3]
}
}
return song