-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
566 lines (504 loc) · 17.6 KB
/
main.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
# for recording audio
import sounddevice as sd
from scipy.io.wavfile import write
# for normalizing recordings
from pydub import AudioSegment, effects
# for identifying currently playing song
from ShazamAPI import Shazam
# for writing metadata to mp3
from mutagen.mp3 import MP3
from mutagen.easyid3 import EasyID3
# for playing and pausing media
from pynput.keyboard import Key, Controller
# for arguments
import sys
# for identifying song in the background while recording the song
import multiprocessing
# for creating date based folders
import datetime
# for sleeping
import time
# for creating folders
import os
# for adding the option of running the program in single threaded mode
import SingleThreadedRecord
# used to pause the program
import tkinter
from tkinter import ttk
# used to kill the thread
import threading
# Global Constants ------------------------------
UNIDENTIFIED_INDEX = 0
INDEX_FILENAME = 'counter.txt'
DEBUG = 0
"""
PyRecording usage:
pass in duration of song in minutes and seconds in the following format
3 56 = 3 minutes 56 seconds
the song will record, identify if possible, and then output as mp3
if the song cannot be identified then it will be labelled as
unidentified[index].mp3
"""
def get_song_length(minutes, seconds):
"""
minutes: Str
seconds: Str
gets a song's length in seconds based on command line arguments
returns total duration in seconds(float)
"""
# convert minutes and seconds from strings to integers/float
try:
minutes = int(minutes)
except Exception:
print("Error, minutes is not an integer")
try:
seconds = float(seconds)
except Exception:
print("Error: seconds could not be converted to float")
# convert minutes to seconds
minutes *= 60
total = minutes + seconds
return total
def read_unidentified_index():
"""
reads the index file and returns the value
"""
index = 0 # initialize index
try:
f = open(INDEX_FILENAME, 'r')
# read index and convert it to int
index = int(f.readline())
f.close()
except Exception as e:
print(e)
# create new file and start index
write_unidentified_index(index)
return index
def write_unidentified_index(index):
"""
Writes an index number to the index file.
takes one argument: index (int)
"""
try:
f = open(INDEX_FILENAME, 'w')
f.write(str(index))
f.close()
except Exception as e:
print(e)
print('error writing unidentified index')
exit()
def get_unidentified_index():
""" THIS FUNCTION IS DEPRECATED
if a song is unidentified this function will store
the mp3 as unidentified[index].mp3 for manual revision later.
This index comes from a file called counter.txt which is created
if it does not exist and is read from and updated if it does exist.
the counter should always be unique to prevent conflictsa and overwrites.
"""
try:
# open index file to get unique index
file_object = open(INDEX_FILENAME, 'r')
content = file_object.readline()
print(content)
# close file
file_object.close()
int_content = int(content)
# increment counter
int_content += 1
print(int_content)
# write back into index file
file_object = open(INDEX_FILENAME, 'w')
str_content = str(int_content)
file_object.write(str_content)
# # close file
file_object.close()
except Exception as ex:
print(ex)
# create new file and start index
file_object = open(INDEX_FILENAME, 'w')
content = "0"
int_content = 0
file_object.write(content)
# close file
file_object.close()
return int_content
def identify_song(queue):
"""
This function is meant to identify a song in the background in time to save the mp3 file
"""
# recording paramaters
sample_frequency = 44100
duration = 10 # duration of recording in seconds -- increased from 5 to 10
# recording of song
recording = sd.rec(int(duration * sample_frequency), samplerate = sample_frequency, channels = 2)
# wait for recording to finish
sd.wait()
# write out the sound bite
write("Temps/identification.wav", sample_frequency, recording)
audio_file_to_recognize = open("Temps/identification.wav", 'rb').read()
shazam = Shazam(audio_file_to_recognize)
recognize_generator = shazam.recognizeSong()
song_info = None
try:
info = next(recognize_generator)
track_info = info[1]
if 'track' in track_info:
title = track_info['track']['title']
artist = track_info['track']['subtitle']
song_info = (title, artist)
queue.put_nowait(song_info)
return
except Exception as ex:
print(ex)
queue.put(song_info)
def recording(seconds, queue):
"""
Records a song for the duration specified in seconds (integer)
returns nothing
"""
# recording parameters
# sampling freq
sample_frequency = 96000
duration = seconds
# setup multi-process
Q = multiprocessing.Queue()
# setup process, no need for queue since no return
prc = multiprocessing.Process(target=print_elapsed_time, args=(duration, Q))
print("Starting Recording...")
# start recording
song_recording = sd.rec(int(duration * sample_frequency),
samplerate = sample_frequency, channels = 2)
# start process
prc.start()
# start the song
play_pause()
print("Recording in progress...")
# wait for recording to finish
sd.wait()
# pause song and skip to next
play_pause()
skip_to_next()
# wait for process to end
prc.join()
print("Writing recording to disk...")
# write out the recording
write("Temps/recording.wav", sample_frequency, song_recording)
print("Finished Recording and writing to Temps/recording.wav")
queue.put(None)
def normalize(filename, format):
"""
Normalizes the audio to a consistent level for the given
filename of type format.
"""
print("Normalizing Audio...")
rawsound = AudioSegment.from_file(filename, format)
normalizedsound = effects.normalize(rawsound)
normalizedsound.export(filename, format=format)
def convert_to_mp3(song_info):
"""
Converts the recording in Temps folder to mp3 format
with the given song_info tuple.
"""
print("Converting to mp3...")
# normalize before conversion
normalize("Temps/recording.wav", "wav")
# converstion to mp3
sound = AudioSegment.from_wav('Temps/recording.wav')
title = song_info[0]
artist = str(song_info[1])
# Clean up song identification data
title = title.replace('/', '-')
artist = artist.replace('/', '-')
# prepare to save the file
date = datetime.date.today().isoformat()
time = datetime.datetime.now()
filename = f"Recordings/{date}/{title} - {artist}.mp3"
if check_date_dir() != True:
print(f"""{date} directory doesn't exist (likely due to day change)
Current Time: {time}
Creating {date} directory""")
# actually making the directory
os.mkdir(f"Recordings/{date}")
# save the file
sound.export(filename, format='mp3', bitrate="320k")
song = filename
mp3file = MP3(song, ID3=EasyID3)
mp3file['title'] = [title]
mp3file['artist'] = [artist]
print("Saving...")
mp3file.save()
print("Saved")
def play_pause():
"""
automates pressing the play/pause media key when recording.
"""
print("Play/Pause")
keyboard = Controller()
keyboard.press(Key.media_play_pause)
keyboard.release(Key.media_play_pause)
def print_elapsed_time(total_time, Q):
"""
Prints the time elapsed out of a given total, total_time (float in seconds).
Meant to help give real time status of the recording.
"""
current_time = 0
minutes = total_time // 60 # integer divison by seconds
seconds = total_time - (minutes * 60) # subtract minutes in form of seconds
print(">>> Total time calculated: %d:%02d" % (minutes, seconds))
while current_time <= total_time:
print(">>> Elapsed Time in seconds: %d/%02d"% (current_time, total_time), flush=True, end="\r")
time.sleep(1) # sleep for one second to reflect elapsed time
current_time += 1
# print the empty line so the next print statement doesn't overwrite.
print('')
def multi_process(record_duration):
"""
Sets up everything for running parallel processes for recording
and idenitifying the song. It's basically the main userside recording function.
Requires the record duration in seconds.
Returns the song's info (title and artist) in a tuple.
"""
print("Setting up multi_process...")
# create Queue to get return values
Q = multiprocessing.Queue()
print("Queue init'd...")
# creating processes for each of the functions
prc1 = multiprocessing.Process(target=recording, args=(record_duration,Q))
prc2 = multiprocessing.Process(target=identify_song, args=(Q,))
rets = []
# starting the first process
prc1.start()
# start second process
prc2.start()
print("Processes started")
rets.append(Q.get())
rets.append(Q.get())
# wait until first process is done
prc1.join()
# wait until second process is done
prc2.join()
# when both processes are finished
print("Processes complete!")
return rets[0] # only return the first value cuz that's the meta data
def record_song(minutes, seconds):
# calculate song length
song_length = get_song_length(minutes, seconds)
# start the recording process
song_info = multi_process(song_length)
# check if song was identified.
if song_info != None:
convert_to_mp3(song_info)
else:
global UNIDENTIFIED_INDEX # required to access global variable
song_info = ("unidentified", UNIDENTIFIED_INDEX)
UNIDENTIFIED_INDEX += 1 # increment unidentified index
convert_to_mp3(song_info)
# write the unidentified index back to the file
write_unidentified_index(UNIDENTIFIED_INDEX)
return song_info
def makedirs():
"""
Creates necessary base directories for recordings.
It doesn't create date labelled directories.
"""
# list the contents of the current directory
dir = os.listdir()
# if recordings doens't exist, create the directory
if "Recordings" not in dir:
print("making directory to hold recordings")
os.mkdir("Recordings")
else:
print("Recordings directory exists")
# if temps not in dir
if "Temps" not in dir:
print("making directory for temporary files")
os.mkdir("Temps")
else:
print("Temps directory exists")
# Ensure directory for current date exists.
date = datetime.date.today().isoformat()
if check_date_dir() == False:
print(f"making directory {date}")
os.mkdir(f"Recordings/{date}")
else:
print(f"directory {date} already exists")
def check_date_dir():
"""
Checks if the current date directory exists or not.
"""
date = datetime.date.today().isoformat()
if date not in os.listdir("Recordings"):
return 0
return 1
class interrupt_obj:
interrupt_val = 0
def trigger_interrupt(self):
"""
Sets the interrupt flag to 1 (True)
So that the reader knows to interrupt the program
"""
self.interrupt_val = 1
print("\n\rPause clicked! Program will end at end of next song")
if DEBUG:
print(f"self.interrupt_val: {self.interrupt_val}")
def get_interrupt_val(self):
"""
Sets reads the interrupt value and returns it
so that those wanting to read the interrupt value
aren't reading it directly
"""
if DEBUG:
print("interrupt_val = %d" % self.interrupt_val)
return self.interrupt_val
def onPress(q, int_var):
"""
The function which takes multiprocessing Queue
and an interrupt_obj and handles the steps to be
taken on pressing the pause button.
"""
int_var.trigger_interrupt()
q.put(int_var)
def tkinter_process(queue, int_var):
"""
This function sets up and operates the tkinter window which
allows the user to interrupt the program.
"""
root = tkinter.Tk()
frame = ttk.Frame(root, padding=10)
root.title("PyRecording")
frame.grid()
label_text = "Interrupt exits the program after the current song finishes"
ttk.Label(frame, text=label_text).grid(column=0, row=0)
ttk.Button(frame, text="Interrupt", command=lambda: onPress(queue, int_var)).grid(column=0, row=1)
root.mainloop()
def batch(filename):
"""
The function houses the funcitonality of iterating over a given
file and recording multiple songs sequentially and in an automated
fashion.
"""
print("Running in batch mode...")
song_lengths = []
with open(filename, "r") as f:
# split by spaces
for line in f:
song_lengths.append(line.split())
# ensure lengths were entered
if song_lengths == []:
raise Exception("Error, no lengths in file")
# read the undentified index regardless before starting the record loop
UNIDENTIFIED_INDEX = read_unidentified_index()
# make dirs
makedirs()
print ("*" * 20)
# set an object for maintaining interrupt state
interrupt = interrupt_obj()
Q = multiprocessing.Queue()
prc = multiprocessing.Process(target=tkinter_process, args=[Q,interrupt])
# start process for tkinter window so it doesn't block
prc.start()
# convert all entries to int
for array in song_lengths:
minutes = int(array[0])
seconds = float(array[1])
info = record_song(minutes, seconds)
print(f"Recorded \"{info[0]} - {info[1]}.mp3\"")
print("*" * 20)
if DEBUG:
print(f"interrupt_val = {interrupt.get_interrupt_val()}")
if not Q.empty():
interrupt = Q.get()
if DEBUG:
print(interrupt)
if interrupt.get_interrupt_val():
if DEBUG:
print("\rInterrupt Called")
prc.terminate()
break
prc.terminate()
prc.join()
def skip_to_next():
"""
Presses the skip media key
"""
print("skipping to next...")
keyboard = Controller()
keyboard.press(Key.media_next)
keyboard.release(Key.media_next)
if __name__ == '__main__':
HELP = """
USAGE:
python3 main.py -f [filename] -- see readme for formatting information
python3 main.py [minutes] [seconds]
python3 main.py -sf [filename] # runs the program in single threaded mode on a batch of songs
python3 main.py -s [minutes] [seconds] # runs the program in single threaded mode on a single song
i.e. python3 main.py 5 43 would be 5 minutes 43 seconds duration of song
python3 main.py -- shows this help screen
"""
# deal with commandline arguments
n = len(sys.argv)
# read the index before everything else.
UNIDENTIFIED_INDEX = read_unidentified_index()
# batch argument
if n == 2:
if sys.argv[1] == '-f' or sys.argv[1] == '-sf':
print("You'll need to enter a filename")
f = input("enter a filename: ")
if f != "" and sys.argv[1] == '-f':
batch(f)
elif f != "" and sys.argv[1] == '-sf':
SingleThreadedRecord.single_threaded_batch(f)
else:
print("Error invalid argument")
print(HELP)
sys.exit()
elif n == 3:
# handle case where filename is being passed
if sys.argv[1] == '-f':
filename = sys.argv[2]
batch(filename)
elif sys.argv[1] == '-sf':
filename = sys.argv[2]
SingleThreadedRecord.single_threaded_batch(filename)
elif sys.argv[1] == '-c':
filename = sys.argv[2]
# identify song
song_info = SingleThreadedRecord.single_threaded_identify_song(filename)
if song_info != None:
convert_to_mp3(song_info)
else:
song_info = ("unidentified", UNIDENTIFIED_INDEX)
UNIDENTIFIED_INDEX += 1 # increment unidentified index
convert_to_mp3(song_info)
# write the index back to the file
write_unidentified_index(UNIDENTIFIED_INDEX)
else:
minutes = sys.argv[1]
seconds = sys.argv[2]
# handle weird input combination
try:
verify_minutes = int(minutes)
verify_seconds = float(seconds)
except (ValueError) as exc:
print("Error, either minutes was not an integer or seconds could not be converted to float")
sys.exit()
# record song
makedirs()
record_song(minutes, seconds)
elif n == 4:
if sys.argv[1] == '-s':
minutes = sys.argv[2]
seconds = sys.argv[3]
# handle weird input combination
try:
verify_minutes = int(minutes)
verify_seconds = float(seconds)
except (ValueError) as e:
print("Error, either minutes was not an integer or seconds could not be converted to float")
sys.exit()
makedirs()
# record song in single threaded mode
SingleThreadedRecord.single_thread_record_song(minutes, seconds)
# if no argument has been given, show the help screen
else:
print(HELP)