-
Notifications
You must be signed in to change notification settings - Fork 1
/
freezam.py
145 lines (128 loc) · 5.33 KB
/
freezam.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
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
import argparse
from io_manager import setuplogger, getListOfFiles
import os
import json
from song_manager import Song
from database_manager import Database
import sounddevice as sd
import numpy as np
def add(args):
"""
compute the song's siganature and add the song's info and
signature to the database.
----------
args : Namespace
the namespace that was pasrsed from the commind line input
"""
filename = args.filename
if filename.startswith("http"):
filePath = filename
else:
filePath = os.path.join("./Library", filename)
libPath = './Library/'
if args.filename == 'all':
# adding all songs in the Library folder
audio_paths = getListOfFiles(libPath)
db = Database("localhost", "postgres", "postgres", "*********")
songs = []
for audio_path in audio_paths:
title = os.path.basename(audio_path)
artist = "various"
song = Song(title, artist, audio_path, libPath, True)
songs.append(song)
db = Database("localhost", "postgres", "postgres", "*********")
db.save_to_database([song])
print("{} added".format(title))
else:
if args.title is None:
title = os.path.basename(filePath)
else:
title = args.title
song = Song(title, args.artist, filePath, libPath, True)
db = Database("localhost", "postgres", "postgres", "Ivan@1995")
db.save_to_database([song])
def identify(args):
"""
compute the snippet's siganature and compare it with the
signatures of songs in the database. Return the closest match.
----------
args : Namespace
the namespace that was pasrsed from the commind line input
"""
logger = setuplogger(args.verbose, "./log/identify_log")
filename = args.filename
if filename is None:
# record the audio, set duration in s and fs in Hz
(dur, fs) = (30, 44100)
print("*recording")
myrecording = sd.rec(int(dur * fs), fs, 1, blocking=True)
print("*end")
print("*playing")
sd.play(myrecording, fs, blocking=True)
print("*end")
#mono_signal = np.mean(myrecording, axis=1)
mono_signal = np.reshape(myrecording, (int(dur*fs),))
snippet = Song("recording", "user", "from recording", "None", True, mono_signal, fs)
else:
if filename.startswith("http"):
filePath = filename
else:
filePath = os.path.join("./snippets", filename)
libPath = './Library/'
snippet = Song("recording", "user", filePath, libPath, True)
db = Database("localhost", "postgres", "postgres", "Ivan@1995")
matched_result = db.search(snippet)
logger.info("find the matched song {}".format(matched_result))
def listSongs(args):
"""
List a useful summary of the library contents
----------
args : Namespace
the namespace that was pasrsed from the commind line input
"""
logger = setuplogger(args.verbose, "./log/list_log")
logger.info("list a summary of the songs in the library")
database = "./Database"
search_space = [f for f in os.listdir(database) if f.endswith('.json')]
# load all the songs' information
for fname in search_space:
f = open(os.path.join(database, fname))
song = json.loads(f.read())
f.close()
info = "The song has title {}, aritst {}, sampling rate {}".format(song['title'], \
song['artist'], song['sample_rate'])
logger.info(info)
def main():
# create the top-level parser
parser = argparse.ArgumentParser(prog='freezam')
parser.add_argument('--version', action="store_true",
help='current version of freezam')
subparsers = parser.add_subparsers(dest='subparser_name',\
help='sub-command help')
# create the parser for the "add" command
parser_add = subparsers.add_parser('add', help='add a song to the library')
parser_add.add_argument('-t', "--title", help='title of the song')
parser_add.add_argument('-a', "--artist", help='artist of the song')
parser_add.add_argument("filename", help='audio file name in /Data directory or \
its url')
parser_add.add_argument('-v', "--verbose", help="verbose mode", action='store_true')
parser_add.set_defaults(func=add)
# creat the parser for the "identiy" command
parser_idfy = subparsers.add_parser('identify', help='identify a song in \
the library')
parser_idfy.add_argument("filename", nargs='?', help='file name in /Search directory or \
its url')
parser_idfy.add_argument('-v', "--verbose", help="verbose mode", action='store_true')
parser_idfy.set_defaults(func=identify)
# creat the parser for the "list" command
parser_list = subparsers.add_parser('list', help='list all the songs in \
the library')
parser_list.add_argument('-v', "--verbose", help="verbose mode", action='store_true')
#TODO: implement listSongs function
parser_list.set_defaults(func=listSongs)
args = parser.parse_args()
if args.version:
print("freezam 1.0")
args.func(args)
if __name__ == '__main__':
main()