-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgui_music_player.py
759 lines (608 loc) · 25.9 KB
/
gui_music_player.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
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
from modules.music_player.vlc_audio_player import VLC_Audio_Player
from modules.music_player.music_dataframe import Music_Dataframe
from tkinter.filedialog import *
from tkinter import *
import random
import time
from threading import Timer, Thread, Event
import threading
import os
import sys
import subprocess
from modules.MQTT.transmitSong import MQTTTransmitter
import json
from paho.mqtt import client as mqtt_client
from modules.VoiceRecognition.speechGet import Voice_Recognition
import pafy
from youtubesearchpython import VideosSearch
running_subprocesses = []
pp_indicator = 0
class FrameApp(Frame):
def __init__(self, parent):
super(FrameApp, self).__init__(parent)
# Setup window frame
root.title("Smartify Player")
root.geometry("470x320")
# Configure menu bar
smartify_menu = Menu(root)
root.config(menu=smartify_menu)
# Create menu bar items
file_menu = Menu(smartify_menu)
smartify_menu.add_cascade(label="File", menu=file_menu)
file_menu.add_command(label="Add Song Directory",
command=self.add_to_list)
file_menu.add_command(label="Clear Smartify Data",
command=self.clear_smartify_data)
emotion_menu = Menu(smartify_menu)
smartify_menu.add_cascade(label="Emotion", menu=emotion_menu)
emotion_menu.add_command(
label="Export Emotion Data", command=self.export_csv)
emotion_menu.add_command(
label="Import Emotion Data", command=self.import_csv)
emotion_menu.add_command(
label="Run Emotion Detection", command=self.thread_detect_user_emotion)
player_menu = Menu(smartify_menu)
smartify_menu.add_cascade(label="Player", menu=player_menu)
player_menu.add_command(
label="Play/Pause", command=self.play_pause_music)
player_menu.add_command(label="Previous", command=self.previous_song)
player_menu.add_command(label="Next", command=self.next_song)
player_menu.add_command(label="Stop", command=self.stop)
player_menu.add_command(label="Play Random Song",
command=self.play_random_playlist)
self.grid(padx=20, pady=20)
self.player = VLC_Audio_Player()
self.df_songs = Music_Dataframe()
self.df_songs.import_csv(".smartify.csv") # import from default path
self.voice = Voice_Recognition()
self.voice_thread = Thread(target=self.speechGet)
self.voice_on = False
# Default Topic for MQTT is "/ECE180DA/Team9/"
self.default_topic = "/ECE180DA/Team9/"
# MQTT Transmitter (from MQTT module)
self.transmitter = MQTTTransmitter()
self.transmitter_client = self.transmitter.connect_mqtt()
self.transmit_msg = False
self.transmitter_thread = Thread(target=self.transmit)
# MQTT Receiver (Inside the player itself)
# These are variables to initialize the Receiver
self.receive_msg = False
self.broker = 'broker.emqx.io'
self.receiver_topic = self.default_topic
self.client_id = 'python-mqtt'+str(random.randint(0, 1000))
self.client = self.initialize_mqtt() # connect to broker and subscribe
self.emotion = 4
self.emotion_dict = {0: "Angry", 1: "Disgusted", 2: "Fearful",
3: "Happy", 4: "Neutral", 5: "Sad", 6: "Surprised"}
# --------------
# GUI code below
# --------------
self.button_transmit = Button(
self, text="Transmitter: OFF", command=self.thread_transmit, width=12)
self.button_transmit.grid(row=1, column=0)
self.TChannel = Entry(self, width=14)
self.TChannel.grid(row=2, column=0)
self.button_TChannel = Button(
self, text="Load", command=self.transmit_channel, width=12)
self.button_TChannel.grid(row=3, column=0)
self.button_receive = Button(
self, text="Receiver: OFF", command=self.receive, width=12)
self.button_receive.grid(row=1, column=2)
self.RChannel = Entry(self, width=14)
self.RChannel.grid(row=2, column=2)
self.button_RChannel = Button(
self, text="Load", command=self.receive_channel, width=12)
self.button_RChannel.grid(row=3, column=2)
self.button_emotion_detection = Button(
self, text="Detect Emotion", command=self.thread_detect_user_emotion, width=12)
self.button_emotion_detection.grid(row=4, column=0)
# Voice recognition
# row 4, column 2
self.button_voice = Button(
self, text="Voice Command", command=self.thread_voice, width=12)
self.button_voice.grid(row=4, column=2)
self.button_add_songs = Button(
self, text="Shuffle", command=self.play_random_playlist, width=12)
self.button_add_songs.grid(row=3, column=1)
# Song name
# row 5, column 0, columnspan 3
self.button_play_pause = Button(
self, text="▶️", command=self.play_pause_music, width=12)
self.button_play_pause.grid(row=6, column=1)
self.button_previous = Button(
self, text="⏮", command=self.previous_song, width=12)
self.button_previous.grid(row=6, column=0)
self.button_next = Button(
self, text="⏭", command=self.next_song, width=12)
self.button_next.grid(row=6, column=2)
self.button_stop = Button(self, text="⏹", command=self.stop, width=12)
self.button_stop.grid(row=7, column=1)
# Volume minus
# row 7, column 0
# Volume plus
# row 7, column 2
# self.button_add_songs = Button(self, text="Add Song Directory", command=self.add_to_list, width=20)
# self.button_add_songs.grid(row=5, column=0)
# self.button_test = Button(self, text="Test Button", command=self.test, width=20)
# self.button_test.grid(row=8, column=0)
# self.button_export_csv = Button(self, text="Export Smartify Data", command=self.export_csv, width=20)
# self.button_export_csv.grid(row=11, column=0)
# self.button_import_csv = Button(self, text="Import Smartify Data", command=self.import_csv, width=20)
# self.button_import_csv.grid(row=12, column=0)
# TODO: Make progressbar, delete songs from playlist, amplify volume
"""
Following code was modified from sample code to create the progress bar
and OnTimer/scale_sel and ttkTimer class
Author: Patrick Fay
Date: 23-09-2015
"""
# Progress Bar
self.scale_var = DoubleVar()
self.timeslider_last_val = ""
self.timeslider = Scale(
self, variable=self.scale_var, from_=0, to=1000, orient=HORIZONTAL, length=410)
# Update only on Button Release
self.timeslider.bind("<ButtonRelease-1>", self.scale_sel)
self.timeslider.grid(row=15, column=0, columnspan=3)
self.label1 = Label(self, text="Time Slider")
self.label1.grid(row=16, column=1)
self.timer = ttkTimer(self.OnTimer, 1.0)
self.timer.start() # start Thread
self.volume_var = IntVar()
self.volslider = Scale(self, variable=self.volume_var, command=self.volume_sel,
from_=0, to=100, orient=HORIZONTAL, length=410)
self.volslider.grid(row=20, column=0, columnspan=3)
self.label1 = Label(self, text="Volume Slider")
self.label1.grid(row=21, column=1)
"""
MQTT COMMANDS
"""
def initialize_mqtt(self):
"""
same as connect_mqtt() and subscribe_mqtt()
returns client
"""
client = self.connect_mqtt()
self.subscribe_mqtt(client, self.receiver_topic)
return client
def connect_mqtt(self):
"""
create MQTT client instance
connect to MQTT broker:
return: client instance (MQTT)
"""
client = mqtt_client.Client(self.client_id)
client.on_connect = self.on_connect
try:
client.connect(self.broker) # default port is 1883
except Exception as error: # catch most exceptions, except few:
# for details, check https://docs.python.org/3.5/library/exceptions.html#exception-hierarchy
print('An exception occurred: {}'.format(error), file=sys.stderr)
print("WARNING: Transmit and Receive Can't be used!")
return client
def subscribe_mqtt(self, client, topic):
client.subscribe(topic)
client.on_message = self.on_message
def on_connect(self, client, userdata, flags, rc):
"""
Prints Message when player is connected to MQtt
"""
if rc == 0:
print("Connected to MQTT Broker!")
else:
print(
"Failed to connect to MQTT Broker, Transmit/Recieve will not work, return code %d\n", rc)
def on_message(self, client, userdata, msg):
"""
The function we call when we receive a message from MQTT broker
"""
print(
f"Received `{msg.payload.decode()}` from `{msg.topic}` topic")
self.parse_command(json.loads(msg.payload.decode()))
"""
END OF MQTT OOMMANDS
"""
def OnTimer(self):
"""Update the time slider according to the current movie time.
"""
if self.player == None:
return
# since the self.player.get_length can change while playing,
# re-set the timeslider to the correct range.
length = self.player.get_length()
dbl = length * 0.001
self.timeslider.config(to=dbl)
# update the time on the slider
tyme = self.player.get_time()
if tyme == -1:
tyme = 0
dbl = tyme * 0.001
self.timeslider_last_val = ("%.0f" % dbl) + ".0"
self.timeslider.set(dbl)
def scale_sel(self, evt):
if self.player == None:
return
nval = self.scale_var.get()
sval = str(nval)
if self.timeslider_last_val != sval:
mval = "%.0f" % (nval * 1000)
self.player.set_time(int(mval)) # expects milliseconds
def volume_sel(self, evt):
if self.player.listPlayer.get_media_player() == None: # nothing being played
return
volume = self.volume_var.get()
if self.player.audio_set_volume(volume) == -1:
print("Failed to set volume")
def add_to_list(self):
"""
Opens window to browse data on disk and adds selected songs (no directories) to playlist
:return: None
"""
music_directory = askdirectory()
# appends song directory on disk to playlist in memory
# adds songs into dataframe
self.df_songs.load(music_directory)
def play(self):
"""
Plays current song. Does nothing if the song is currently being played.
"""
self.player.play()
def pause(self):
"""
Pause current song. Does nothing if the song is already paused.
Note: this is different behavior from VLC-python's Pause which acts like "Toggle"
"""
self.player.pause()
def play_pause_music(self):
"""
Plays song if Paused, Pauses song if Playing.
"""
if self.player.is_playing():
self.button_play_pause.configure(text="▶️")
self.player.pause()
else:
self.button_play_pause.configure(text="⏸")
self.player.play()
def stop(self):
"""
Stops current song
:return: None
"""
self.player.stop()
def next_song(self):
"""
Plays next song
:return: None
"""
self.player.next()
def previous_song(self):
"""
Plays previous song
:return:
"""
self.player.previous()
def play_random_playlist(self):
random_playlist = self.create_random_playlist()
self.player.addPlaylist(random_playlist)
self.player.play()
def set_playlist_as_random_playlist(self):
random_playlist = self.create_random_playlist()
self.player.addPlaylist(random_playlist)
def create_random_playlist(self) -> list:
"""
Creates a randomly generated playlist with ALL songs in the dataframe:
Output - List containing paths to songs
"""
random_ints = list(range(self.df_songs.size()))
random.seed()
random.shuffle(random_ints)
random_playlist = []
for i in random_ints:
random_playlist.append(self.df_songs.Music.iloc[i]['path'])
return random_playlist
def test(self):
"""
Whatever function we want to test
"""
self.player.change_volume(-10)
def thread_transmit(self):
"""
Sets self.transmit_msg to On/Off (Switch for Transmitter, not atomic)
If transmitter is turned on, sends a message to client every interval.
"""
if self.transmit_msg == True:
self.transmit_msg = False
self.button_transmit.configure(text="Trasmitter: OFF")
print("Transmitter Turned Off")
else:
self.transmit_msg = True
if not self.transmitter_thread.is_alive():
# only start thread if thread is not alive
self.transmitter_thread = Thread(target=self.transmit)
self.transmitter_thread.start()
self.button_transmit.configure(text="Trasmitter: ON")
print("Transmitter Turned On")
def transmit_channel(self):
input = self.TChannel.get()
self.transmitter.topic = self.default_topic + str(input)
print("the transmitter channel name has been changed to: " +
self.transmitter.topic)
def transmit(self):
"""
Transmit song data via MQTT every Interval (loops forever as long as tramsitter is on, use a thread)
"""
while self.transmit_msg == True:
(song_metadata, songtime) = self.get_info_current_song()
if song_metadata is not None:
songname = song_metadata.title
artistname = song_metadata.artist
self.transmitter.setSongname(songname)
self.transmitter.setArtistname(artistname)
self.transmitter.setSongtime(songtime)
if self.player.is_playing():
self.transmitter.setCommand("INPUTSONG")
else:
self.transmitter.setCommand("PAUSE")
self.transmitter.publish(self.transmitter_client)
time.sleep(3) # Interval to sleep between each message
def receive_channel(self):
input = self.RChannel.get()
# unsubscribe from previous topic
self.client.unsubscribe(self.receiver_topic)
self.receiver_topic = self.default_topic + \
str(input) # change topic of receiver
self.client.subscribe(self.receiver_topic)
print("the receiver channel name has been changed to: " + self.receiver_topic)
def receive(self):
"""
If Receiver is off: turns receiver on, player will parse any message received via MQTT
If Recevier is on: turns receiver off
Side note: on_message() calls parse_command, so we just need the MQTTclient to be on to parse commands
"""
if self.receive_msg == False:
self.client.loop_start()
self.button_receive.configure(text="Receiver: ON")
print("Receiver Turned On!")
self.receive_msg = True
else:
self.client.loop_stop()
self.button_receive.configure(text="Receiver: OFF")
print("Receiver Turned Off!")
self.receive_msg = False
def parse_command(self, msg):
"""
Parses commands given msg (dictionary), and calls correct functions accordingly
"""
command = msg["command"]
songname = msg["songname"]
artistname = msg["artistname"]
songtime = msg["songtime"]
# Python has no switch statements, I could use a dict, but we can talk about this later
if command == "INPUTSONG":
# If song is same as current song being played
(player_song_metadata, player_songtime) = self.get_info_current_song()
if player_song_metadata is None: # edge case with no song being played
player_song_name = None
else:
player_song_name = player_song_metadata.title
if player_song_name == songname: # songname matches
# only change timestamp of song when off by more than 5 sec.
if abs(player_songtime - songtime) > 5000:
self.play_song(songname, artist=artistname,
start_time=int(songtime))
else:
self.play_song(songname, artist=artistname,
start_time=int(songtime))
elif command == "PLAY":
self.play()
elif command == "PAUSE":
self.pause()
elif command == "TOGGLE":
self.play_pause_music()
elif command == "SKIPTIME":
self.skip_time(songtime)
elif command == "NEXT":
self.next_song()
elif command == "PREV":
self.previous_song()
else: # command not recognized
print("Command not Recognized!")
def play_song(self, title, artist=None, start_time=0):
"""
Looks up song given title and artist.
If the song is not found in local directory, nothing plays (Prints a message)
Otherwise, the song is played from the current playlist (if it is on the playlist)
If the song is not on current playlist, a random playlist is generated (with the song), and is played
"""
# Don't do anything on when given null
if title is None:
return
song_path = self.df_songs.find_song(title=title, artist=artist)
# CHANGE THIS LINE LATER S0 we can let user decide:
self.enable_youtube_search = True
if song_path == None:
if self.enable_youtube_search:
song_info = {'title': title, 'artist': artist}
# search song on separate thread
try:
youtube_link = self.search_song_online(song_info)
except Exception as e:
print("Error occured searching song online")
return
try:
video = pafy.new(youtube_link)
except Exception as e:
print("Error occured playing song online; Possible copyright issues")
return
audio = video.getbestaudio()
audio_link = audio.url
self.df_songs.insert(audio_link, song_info)
# Now play the song
self.set_playlist_as_random_playlist() # random playlist of ALL songs
played = self.player.play_song_from_current_playlist(
audio_link, start_time=start_time)
else:
print("Song Not Found!")
return
else:
played = self.player.play_song_from_current_playlist(
song_path, start_time=start_time)
if not played: # song not in playlist or can't play for some reason
self.set_playlist_as_random_playlist() # random playlist of ALL songs
played = self.player.play_song_from_current_playlist(
song_path, start_time=start_time)
def thread_voice(self):
"""
Sets self.voice_msg to On/Off
If voice is turned on, gets a voice command from user until a valid one is received.
"""
if self.voice_on == False:
# do nothing if user presses button while we're currently getting the voice command
self.voice_on = True
if not self.voice_thread.is_alive():
self.voice_thread = Thread(target=self.speechGet)
self.voice_thread.start()
def speechGet(self):
print("Please enter your voice command.")
self.voice.speechGet()
voiceattempt = 0
while self.voice.getCommand() == "ERROR" and voiceattempt < 2:
print("Unrecognized input. Please enter your voice command again.")
self.voice.speechGet()
voiceattempt = voiceattempt + 1
if self.voice.getCommand() == "ERROR":
print(
"Sorry, we couldn't hear anything from your mic. Please try again later.")
else:
print("Got it. We're on it now.")
command_dict = self.voice.getDict()
print(command_dict)
self.parse_command(command_dict)
self.voice_on = False
def search_song_online(self, song_info):
"""
input: song_info - dictionary of metadata
returns: youtube_link of video (of Youtube video)
"""
search_str = str(song_info['title']) + " " + str(song_info['artist'])
song_search = VideosSearch(search_str, limit=5)
video_link = song_search.result()['result'][0]['link']
return video_link
def get_info_current_song(self):
"""
returns (title, artist, time_in_ms)
Note: If no metadata is found, title and artist will be returned as None
"""
curr_song_path, time_in_ms = self.player.get_path_and_time()
curr_song_metadata = self.df_songs.get_metadata_tag(curr_song_path)
return (curr_song_metadata, time_in_ms)
def print_current_song_info(self):
"""
Prints information from get_info_current_song()
returns nothing
"""
song_tag, curr_time = self.get_info_current_song()
curr_title = song_tag.title
curr_artist = song_tag.artist
print("Title: %s Artist: %s Time: %.2fsec" %
(curr_title, curr_artist, curr_time/1000))
def skip_time(self, time_to_skip=5000): # time to skip in ms
current_time = self.player.get_time()
self.player.set_time(current_time + time_to_skip)
def thread_detect_user_emotion(self):
"""
Same as detect_user_emotion(), but creates a new daemon thread
and runs it on a separte thread (call this in the gui)
"""
t = Thread(target=self.detect_user_emotion)
t.start()
def detect_user_emotion(self):
"""
Opens a subprocess to detect emotion from user (from a webcam)
returns: nothing
"""
# Use Threads to prevent freezing;
# Using thread.join() with this function seems to freeze GUI as well (most likely due to the subprocess, my guess)
"""
print("In the Function")
print("Main Thread:", threading.main_thread())
print("Current Thread:", threading.current_thread())
print("Current Thread Count:", threading.active_count())
"""
print("Please wait for our module to load...")
print("Please place your face near the camera.")
emotion_subprocess = subprocess.Popen(
["python", "./modules/emotionDetection/emotions.py", "--mode", "display"])
# add to list of running subprocesses
running_subprocesses.append(emotion_subprocess)
emotion_subprocess.wait()
running_subprocesses.remove(
emotion_subprocess) # remove once completed
self.emotion = emotion_subprocess.returncode
print("Your Emotion is:", self.emotion_dict[self.emotion])
print("Recommending Songs based on your Emotion!")
self.play_emotion_playlist()
def play_emotion_playlist(self, num_songs=20):
"""
Creates a random playlist of a song matching the emotions
With songs with matching emotion
If matching songs < num_songs, playlist will contain all matching songs
"""
emotion_playlist = self.df_songs.find_emotion_songs(self.emotion)
random.shuffle(emotion_playlist)
if len(emotion_playlist) > num_songs:
emotion_playlist = emotion_playlist[:num_songs]
if len(emotion_playlist) == 0:
print("No songs matching your current emotion! Try adding more songs!")
self.player.addPlaylist(emotion_playlist)
self.player.play()
def export_csv(self):
"""
Returns .csv of Dataframe
"""
self.df_songs.export_csv(file_path="./Smartify_Data.csv")
def import_csv(self):
"""
Sets Dataframe values to equal the .csv file, if the columns are valid
"""
df_file = askopenfile().name
print("Importing data from..." , df_file)
self.df_songs.import_csv(file_path=df_file)
def clear_smartify_data(self):
self.df_songs = Music_Dataframe()
class ttkTimer(Thread):
"""a class serving same function as wxTimer... but there may be better ways to do this
"""
def __init__(self, callback, tick):
Thread.__init__(self)
self.callback = callback
#print("callback= ", callback())
self.stopFlag = Event()
self.tick = tick
self.iters = 0
def run(self):
while not self.stopFlag.wait(self.tick):
self.iters += 1
self.callback()
#print("ttkTimer start")
def stop(self):
self.stopFlag.set()
def get(self):
return self.iters
def _quit():
print("Closing App...")
for subprocess in running_subprocesses:
subprocess.terminate() # kill all running subprocesses
app.df_songs.clear_all_youtube_links()
app.df_songs.export_csv(file_path=".smartify.csv")
root = Tk()
root.quit() # stops mainloop
root.destroy() # this is necessary on Windows to prevent
# Fatal Python Error: PyEval_RestoreThread: NULL tstate
os._exit(1)
if __name__ == '__main__':
root = Tk()
root.geometry("800x500")
root.protocol("WM_DELETE_WINDOW", _quit)
app = FrameApp(root)
app.mainloop()