-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.py
997 lines (893 loc) · 49.7 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
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
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
"""
Main script for BachDuet Application
Usage
-----
python main.py
"""
# pyQt5 imports
from PyQt5 import QtGui, QtCore, QtSvg
from PyQt5.QtWidgets import (QMainWindow, QApplication, QCheckBox, QComboBox, QDateTimeEdit,QMessageBox,
QDial, QDialog, QGridLayout, QGroupBox, QHBoxLayout, QLabel, QLineEdit,
QProgressBar, QPushButton, QRadioButton, QScrollBar, QSizePolicy,
QSlider, QSpinBox, QStyleFactory, QTableWidget, QTabWidget, QTextEdit, QSplashScreen,
QVBoxLayout, QWidget,QLCDNumber, QDoubleSpinBox,QGraphicsItem, QGraphicsItemGroup, QGraphicsEllipseItem, QGraphicsObject, QGraphicsLineItem,
QGraphicsScene, QGraphicsView, QStyle, QWidget, QLabel, QHBoxLayout, QMenuBar, QTextEdit, QGridLayout, QAction, QActionGroup, QToolBar, QToolBox, QToolButton)
from PyQt5.QtCore import QObject, pyqtSignal, QTimer, Qt, pyqtSlot, QThread, QPointF, QRectF, QLineF, QRect
from PyQt5.QtGui import (QPen, QTransform, QPixmap)
from PyQt5.QtSvg import QGraphicsSvgItem
import pyqtgraph as pg
from pyqtgraph.Qt import QtGui
# python imports
import logging
import logging.config
from collections import deque, OrderedDict, defaultdict
from queue import Queue
import time, threading
import sys
import argparse
import random
import rtmidi
from datetime import datetime
from functools import partial
import numpy as np
from pathlib import Path
import pickle
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.nn.utils import weight_norm
import torch.optim as optim
import sys
# custom imports
from GuiClasses.MidiReader import MidiKeyboardReaderAsync, MidiReaderSync
from GuiClasses.Timers import Clock, Metronome, TempoEstimator
from GuiClasses.NeuralNetworkIsmir import NeuralNetSync, NeuralNet
from GuiClasses.AudioRecording2 import Audio2MidiEvents, AudioRecorder
from GuiClasses.PitchEstimators import YinEstimator, CrepeEstimator
from GuiClasses.StaffItem import Staff
from GuiClasses.StaffPainter import StaffPainter
from GuiClasses.StaffView import StaffView
from GuiClasses.Mixer import *
from GuiClasses.Manager import *
from GuiClasses.MenuBar import *
from GuiClasses.ToolBar import *
from GuiClasses.ModeWindow import *
from GuiClasses.Preferences import *
from GuiClasses.Player import *
from GuiClasses.PianoRollView import PianoRollView
from GuiClasses.PianoRollPainter import PianoRollPainter
from GuiClasses.Memory import Memory
from GuiClasses.InputDialog import InputDialog
from keyMapping import ddd as KeyMappings
from ParsingClasses import RhythmTemplate
from utils import Params, TensorBuffer, rename
from ParsingClasses import Vocabulary
class ApplicationContext(object):
"""
Manages the resources. There is not
actual need for this Class. The only reason
to use it is for compatibility with the fbs
library, in order to create an exe file for the app
Methods
-------
get_resource(name) : str
returns the path of a resource by name
"""
def __init__(self):
self.path = Path.cwd() / 'resources' / 'base'
def get_resource(self, name):
return str(self.path / name)
class BachDuet(QWidget):
"""
The main module of BachDuet
It is responsible for setting up the GUI windows, and initialize
all the submodules/threads of the system.
Attributes
----------
appctxt : ApplicationContext()
an ApplicationContext class that contains the path
of the resources
logger : Logger()
the logger of the app. Currently not used
parent : None
BachDuet is the top in hierarchy QWidget, so it has
no parrent
Signals
-------
ctrlSignal() : pyqtSignal()
it emits a signal when the ctrl button is pressed. This signal
is usefull for some GUI classes. I.e ctrl+p opens the
preferences box etc.
Methods
-------
showSplashScreen() : None
creates a QSplashScreen object and displays the splash image,
for 1 second
showModeWindow() : None
show the mode window so the user can select between
HH, HM, MM modes
setupBachDuet(buttonInd) : None
setups the current BachDuet configuration, according to
the button that the user selected in ModeWindow().
getSubjectId(name): str
it returns the unique ID of a user. If the user is new
it assigns a new ID and it returns it.
initUi() : None
creates the basic windows of the app including
pianoroll, staffs, mixer, preferences box, toolbars etc
createPianoRollGroup() : None
creates the piano roll window
createStaffGroup() : None
creates the staff window
createMiscGroup() : None
currently not in used.
setupThreads() : None
initiates all the threads for every player
signalsNslots() : None
sets up the connections between the modules/threads.
certain actions in one thread, trigger signals, which connect
to slot functions in another thread.
setupDelayedThreadsAndSignals() : None
some threads/connections "A" have to wait until some
other threads "B" start. This method is a slot, which is activated
after all the "B" threads have been set up
changeAttribute(state, extra, player, attribute) : None
manages all the changes in a players preferences.
It is a slot that is connected to all the signals of the
preference box fields. It requires the players name, the
attribute name, and the new state of the attribute.
updatePortsDict() : None
scans for current midi devices, and updates the
midi ports list in preferences
openDefaultPorts() : None
opens the default midi ports for each player
connectToNewMidiInput(portInd, player) : None
connects the midi input with id portInd
to the player
connectToNewMidiOutput(portInd, player) : None
connects the midi output with id portInd
to the player
"""
ctrlSignal = pyqtSignal(str)
def __init__(self, appctxt, logger, parent = None):
super(BachDuet, self).__init__()
self.setObjectName("BachDuet")
# self.config contains system settings loaded from
# a json file using the Params() class from utils.py
self.config = Params(appctxt.get_resource("bachDuet.json"))
self.cwd = Path.cwd()
self.storagePath = self.cwd / self.config.storagePath
self.appctxt = appctxt
self.ctrlPressed = False
# notesPainterDict is used for displaying the notes on the Staves,
# and it contains all the information about each notes position in the Staff
# as well as their spellin given different key configurations.
with open(self.appctxt.get_resource('notesPainterDict.pickle'), 'rb') as handle:
self.notesDict = pickle.load(handle)
# initiates the event loop. event filter is an object that receives all
# events that are sent to this object. This function is inherited
self.installEventFilter(self)
# set the QIcon of the App
self.setWindowIcon(QtGui.QIcon(self.appctxt.get_resource('Images/bachDuetIconYellow1024.png')))
# display the splashScreen
self.showSplashScreen()
self.showModeWindow()
def showSplashScreen(self):
# load the splash screen image
splash_pix = QPixmap(self.appctxt.get_resource('Images/bachDuetSplashYellow1024.png'))
splash_pix = splash_pix.scaled(400,400)
# QSplashScreen is the actual splash screen object/window
self.splash = QSplashScreen(splash_pix, Qt.WindowStaysOnTopHint)
# Sets the background as transparent (I think)
self.splash.setMask(splash_pix.mask())
# show the window for 1 second.
# TODO use a single shot timer, instead of time.sleep, so the
# rest of the processes are not bloked.
self.splash.show()
time.sleep(1)
def showModeWindow(self):
# ModeWindow contains 3 buttons (HH,HM,MM)
self.modeWindow = ModeWindow(self.appctxt)
self.modeWindow.show()
# splash screen closes at the same time ModeWindow appears
self.splash.finish(self.modeWindow)
# Selecting one of the 3 buttons of modeWindow, triggers
# a signal that activates setupBachDuet() function.
self.modeWindow.buttonGroup.buttonClicked[int].connect(self.setupBachDuet)
@pyqtSlot(int)
def setupBachDuet(self, buttonInd):
#TODO remove the if blocks
self.modeWindow.hide()
# current time is used to time annotate the saved data
now = datetime.now()
# if user selected Human vs Machine (HM) in ModeWindow()
if buttonInd == 0:
# dialog box for the user(s) to input their names
dialog = InputDialog(humanPlayers=1)
if dialog.exec():
fields = dialog.getInputs()
# get the unique ID of the user.
humanId = self.getSubjectId(name = fields['fullName 1'])
# every user has a folder where all his interactions are saved
self.subjectPath = self.storagePath / f'{humanId}_{fields["fullName 1"]}'
self.experimentPath = self.subjectPath / now.strftime("%d_%m_%Y_%H_%M_%S")
self.experimentPath.mkdir(parents=True, exist_ok=True)
#TODO in the future, every user will have their own personalized settings
# currently for all users I use the default profile
self.params = self.config.default
# for the (HM) task we need a human a machine and a metronome player
self.player1 = Player( name = fields['fullName 1'], type = 'human', params = self.params, realTimeInput = True, inputType = 'midi', modules = {}, holdFlag = False)
self.player2 = Player( type = 'machine', params = self.params, realTimeInput = False, inputType = None, modules = {}, holdFlag = None)
self.player3 = Player( type = 'metronome', params = self.params, realTimeInput = False, inputType = None, modules = {}, holdFlag = None)
self.sessionName = 'Human Vs Machine'
# initial BPM is loaded from the params.
self.initBPM = self.params['metronome']["BPM"]
# if user selected Machine vs Machine (MM) in ModeWindow()
elif buttonInd == 1:
# similar comments with the buttonInd == 0
self.params = self.config.default
self.player1 = Player( type = 'machine', params = self.params, realTimeInput = False, inputType = None, modules = {}, holdFlag = None)
self.player2 = Player( type = 'machine2', params = self.params, realTimeInput = False, inputType = None, modules = {}, holdFlag = None)
self.player3 = Player( type = 'metronome', params = self.params, realTimeInput = False, inputType = None, modules = {}, holdFlag = None)
self.initBPM = self.params['metronome']["BPM"]
self.sessionName = 'Machine Vs Machine'
# if user selected Human vs Human (HH) in ModeWindow()
elif buttonInd == 2:
# similar comments with the buttonInd == 0
dialog = InputDialog(humanPlayers = 2)
if dialog.exec():
fields = dialog.getInputs()
humanId1 = self.getSubjectId(name = fields['fullName 1'])
humanId2 = self.getSubjectId(name = fields['fullName 2'])
self.subjectPath = [self.storagePath / f'{humanId1}_{fields["fullName 1"]}', self.storagePath / f'{humanId2}_{fields["fullName 2"]}']
self.experimentPath = [self.subjectPath[0] / now.strftime("%d_%m_%Y_%H_%M_%S"), self.subjectPath[1] / now.strftime("%d_%m_%Y_%H_%M_%S")]
self.experimentPath[0].mkdir(parents=True, exist_ok=True)
self.experimentPath[1].mkdir(parents=True, exist_ok=True)
#TODO currently I use the folder of the first player to save the
# generated data. Change it to save them in both folders.
self.experimentPath = self.subjectPath[0] / now.strftime("%d_%m_%Y_%H_%M_%S")
#TODO also here, each player should have its own parameters.
# Currently all human players use the default profile
self.params = self.config.default
self.player1 = Player( type = 'human', params = self.params, realTimeInput = True, inputType = 'midi', modules = {}, holdFlag = False)
self.player2 = Player( type = 'human2', params = self.params, realTimeInput = True, inputType = 'midi', modules = {}, holdFlag = False)
self.player3 = Player( type = 'metronome', params = self.params, realTimeInput = False, inputType = None, modules = {}, holdFlag = None)
self.sessionName = 'Human Vs Human'
self.initBPM = self.params['metronome']["BPM"]
# for each player open a new midi output port.
# only human player class has midi input.
self.players = [self.player1, self.player2, self.player3]
for player in self.players :
player.midiOut = rtmidi.MidiOut()
if 'human' in player.type :
player.midiIn = rtmidi.MidiIn()
# initialize the main UI
self.initUi()
# initialize all threads
self.setupThreads()
# define signals slots, namely the communication rules
# between threads.
self.signalsNslots()
# detect available midi input and output devices
self.updatePortsDict()
# open the default midi input and output devices
# for each user
self.openDefaultPorts()
# after everything is set up,
# show the BachDuet QWidget maximized.
self.showMaximized()
def getSubjectId(self, name):
# subjects.txt contains all users and their unique ID
subjectsFilePath = self.storagePath / 'subjects.txt'
self.storagePath.mkdir(parents=True, exist_ok=True)
if Path(subjectsFilePath).exists() == True:
readMode = 'r+'
else:
readMode = 'w'
found = False
i=-1
with open(subjectsFilePath, readMode) as f:
if readMode == 'r+':
lines = f.readlines()
for i, line in enumerate(lines) :
if name in line:
found = True
subjectId = int(line.strip().split(' - ')[0])
# if new user, add them in subjects.txt and assign a new ID
if not found :
subjectId = i+2
f.write(f'{subjectId} - {name}\n')
return subjectId
def initUi(self):
self.setWindowTitle('BachDuet')
s = QStyleFactory.create('Fusion')
self.setStyle(s)
self.createPianoRollGroup()
self.createStaffGroup()
# self.createMiscGroup()
self.mixer = Mixer(self.players, self.params, self.appctxt,self) # TODO not functional
self.preferences = Preferences(self.players, self.params,self.appctxt,self)
self.menuBar = MenuBar(self)
self.toolbar = ToolBar(appctxt = self.appctxt, parent = self, params = self.params)
mainLayout = QGridLayout(self)
mainLayout.setMenuBar(self.menuBar)
mainLayout.addWidget(self.toolbar, 0,0,3,1, Qt.AlignLeft|Qt.AlignTop)
mainLayout.addWidget(self.staffGroup, 1,0,6,1)
mainLayout.addWidget(self.pianoRollGroup, 7,0,15,1)
# mainLayout.addWidget(self.othersGroup), 3,0,1,1)
self.setLayout(mainLayout)
def createMiscGroup(self):
# TODO NOT USED FOR NOW
self.othersGroup = QGroupBox("Other")
self.timeSignatureComboBox = QComboBox(self)
self.timeSignatureComboBox.addItems(['2/4','3/4','4/4','3/8','4/8','6/8','9/8','12/8'])
timeSignatureLabel = QLabel("Time Signature")
timeSignatureLabel.setBuddy(self.timeSignatureComboBox)
layout = QGridLayout()
layout.addWidget(timeSignatureLabel, 0, 0, 1, 1)
layout.addWidget(self.timeSignatureComboBox,0,1,1,1)
self.othersGroup.setLayout(layout)
def createPianoRollGroup(self):
self.pianoRollGroup = QGroupBox("Piano Roll")
self.pianoRollView = PianoRollView(self.appctxt, None)
layout = QGridLayout()
layout.addWidget(self.pianoRollView, 0, 0)
self.pianoRollGroup.setLayout(layout)
def createStaffGroup(self):
# TODO StaffView needs to create as many staffs as the players
self.staffGroup = QGroupBox("Staff", self)
self.staffView = StaffView(appctxt = self.appctxt, parent=self)
self.humanLabel = QLabel("H")
self.humanLabel.setObjectName("Human")
self.machineLabel = QLabel("M")
self.machineLabel.setObjectName("Machine")
layout = QGridLayout()
layout.addWidget(self.staffView, 0, 1, 6,150)
layout.addWidget(self.humanLabel, 2,0, 1, 1 , Qt.AlignTop)
layout.addWidget(self.machineLabel, 4,0,1,1, Qt.AlignTop)
#layout.addWidget(self.staffView, 0, 0)
self.staffGroup.setLayout(layout)
def setupThreads(self):
self.currentAudioFrame = Queue()
self.currentAudioNote = Queue()
self.audioMidiEventsQueue = Queue()
# Each player has a sync and async module related to them.
# Sync modules operate in sync with the clock
# Async operate according to a timer (way faster than the clock signal)
# or according to signals from other modules (besides clock)
for player in self.players:
modules = defaultdict(lambda:None)
if player.type in ['human', 'human2']:
# for humans the async module is MidiKeyboardReaderAsync that reads
# the input from the midi keyboard, or the keyboard of the laptop
# the notes are stored in either the internalKeyboardQueue (laptop keyboard)
# or the asyncQueue (midi keyboard)
# TODO update doc's, humans can have two async modules (include Audio)
rate = 16000
chunk = 1024
# set up AudioRecorder module for each human
# tempAudioFramesQueue = Queue()
# tempAudioRecorder = AudioRecorder(audioFramesQueue = tempAudioFramesQueue,
# parentPlayer = player,
# chunk = chunk, # read this from json file instead
# rate = rate)
# set up pitchEstimator module for each human
# tempPitchEstimationsQueue = Queue()
# tempPitchEstimatorThread = QThread()
# tempPitchEstimator = YinEstimator(audioFramesQueue = tempAudioFramesQueue,
# pitchEstimationsQueue = tempPitchEstimationsQueue,
# parentPlayer = player, appctxt = self.appctxt,
# medianOrder=5, rate = rate)
# tempPitchEstimator = CrepeEstimator(audioFramesQueue = tempAudioFramesQueue,
# pitchEstimationsQueue = tempPitchEstimationsQueue,
# parentPlayer = player, appctxt = self.appctxt
# )
# tempPitchEstimator.moveToThread(tempPitchEstimatorThread)
# set up Audio2MidiEvents module for each human
# tempAsyncAudioThread = QThread()
tempAsyncAudioQueue = Queue()
# tempAsyncAudio = Audio2MidiEvents( pitchEstimationsQueue = tempPitchEstimationsQueue,
# audioMidiEventsQueue = tempAsyncAudioQueue,
# parentPlayer = player)
# set up MidiKeyboardReaderAsync
# there is not thread for the async module of human players
# (MidiKeyboardReaderAsync) since it uses a timer to run periodicaly
# on the background
tempAsyncThread = None
tempAsyncQueue = Queue()
tempInternalKeyboardQueue = Queue()
tempAsync = MidiKeyboardReaderAsync( keyboardMidiEventsQueue = tempAsyncQueue,
internalKeyboardAsyncQueue = tempInternalKeyboardQueue,
parentPlayer = player)
# for human players, the sync module is MidiReaderSync(), which
# reads the input from tempAsyncQueue and the tempAsyncAudioQueue
tempSyncThread = QThread()
tempSync = MidiReaderSync(keyboardMidiEventsQueue = tempAsyncQueue,
# audioMidiEventsQueue = tempAsyncAudioQueue,
parentPlayer = player)
tempSync.moveToThread(tempSyncThread)
# modules['audioFramesQueue'] = tempAudioFramesQueue
# modules['audioRecorderModule'] = tempAudioRecorder
# modules['pitchEstimationsQueue'] = tempPitchEstimationsQueue
# modules['pitchEstimatorThread'] = tempPitchEstimatorThread
# modules['pitchEstimatorModule'] = tempPitchEstimator
# modules['asyncAudioThread'] = tempAsyncAudioThread
# modules['asyncAudioQueue'] = tempAsyncAudioQueue
# modules['asyncAudioModule'] = tempAsyncAudio
modules['asyncQueue'] = tempAsyncQueue
modules["asyncModule"] = tempAsync
modules["internalKeyboardQueue"] = tempInternalKeyboardQueue
modules["syncModule"] = tempSync
modules["syncThread"] = tempSyncThread
elif player.type in ['machine', 'machine2']:
# for machine, the async module is the GeneratorDNN(), which is the neural net.
# The neural predicts a note on evey 16th note, but, in order to do so, it needs
# the last note the human played, so NeuralNet's run function is triggered by
# the signal that MidiReaderSync emits and not the Clock() signal.
tempSyncThread = QThread()
# GeneratorDNN() pushes the generated notes in the tempAsyncQueue
tempAsyncQueue = Queue()
# Similar to the MidiReaderSync(), NeuralNetSync(), operates in sync with the clock
# and reads the neural net generated notes from the tempAsyncQueue
tempSync = NeuralNetSync(tempAsyncQueue, player)
tempSync.moveToThread(tempSyncThread)
tempAsyncThread = QThread()
tempAsync = NeuralNet(tempAsyncQueue,
self.notesDict, self.appctxt,
parentPlayer = player, parent = self)
tempAsync.moveToThread(tempAsyncThread)
modules["asyncThread"] = tempAsyncThread
modules['asyncQueue'] = tempAsyncQueue
modules["asyncModule"] = tempAsync
modules["syncModule"] = tempSync
modules["syncThread"] = tempSyncThread
elif player.type == 'metronome':
# for metronome, things are simpler. There is only a sync module
# which is the Metronome
tempSyncThread = QThread()
tempSync = Metronome(appctxt = self.appctxt, parentPlayer = player)
tempSync.moveToThread(tempSyncThread)
modules["syncModule"] = tempSync
modules["syncThread"] = tempSyncThread
elif player.type == 'condition':
# In the future, there will be another type of player wich will be static
# for example, a given chord sequence, or a given melodic line
raise NotImplementedError
player.modules = modules
#print(player.__dict__)
# Manager Thread
self.threadManager = QThread()
self.manager = Manager(self.players, parent = self) # self.midiOut,
self.manager.moveToThread(self.threadManager)
# Memory Thread
self.threadMemory = QThread()
self.memory = Memory(parent = self, notesDict = self.notesDict,
experimentPath = self.experimentPath)
self.memory.moveToThread(self.threadMemory)
# Tempo Estimator
#TODO not used for now
# self.threadTempoEstimator = QThread()
# self.tempoEstimator = TempoEstimator(self.params)
# self.tempoEstimator.moveToThread(self.threadManager)
# Clock
self.threadClock = QThread()
self.clock = Clock(self.appctxt)
self.clock.moveToThread(self.threadClock)
# start all the threads of the sync and async modules of each player
for player in self.players:
if player.modules['audioRecorderModule'] is not None:
# if player input mode is Audio then call
player.modules['audioRecorderModule'].stopStartRecorder('Audio Mic')
pass
if player.modules['pitchEstimatorThread'] is not None:
# player.modules['pitchEstimatorThread'].started.connect(player.modules['pitchEstimatorModule'].process)
player.modules['audioRecorderModule'].audioRecorderSignal.connect(player.modules['pitchEstimatorModule'].process)
player.modules['pitchEstimatorThread'].start()
pass
if player.modules['asyncAudioThread'] is not None:
player.modules['pitchEstimatorModule'].pitchEstimatorSignal.connect(player.modules['asyncAudioModule'].process)
# player.modules['asyncAudioThread'].started.connect(player.modules['asyncAudioModule'].process)
player.modules['asyncAudioThread'].start()
pass
if player.modules['syncThread'] is not None:
player.modules['syncThread'].start()
if player.modules['asyncThread'] is not None:
player.modules['asyncThread'].start()
# as long as the clock thread starts, send a signal to activate
# its run method
self.threadClock.started.connect(self.clock.run1)
# start the rest of the modules
self.threadClock.start()
# self.threadTempoEstimator.start()
self.threadManager.start()
self.threadMemory.start()
def signalsNslots(self):
humanPlayers = [player for player in self.players if player.type in ['human', 'human2']]
machinePlayers = [player for player in self.players if player.type in ['machine', 'machine2']]
metronomePlayer = [player for player in self.players if player.type == 'metronome'][0]
for humanPlayer in humanPlayers:
# actions in the human player tab of preferences box, trigger signals that call
# the method self.changeAttribute, with the appropriate arguments
humanPlayer.preferencesTab.directMonBox.hit.connect(lambda state, extra, player: self.changeAttribute(state, extra, player, 'directMonFlag'))#, extra = humanPlayer.preferencesTab.directMonBox.isChecked()))
humanPlayer.preferencesTab.queueMidiEventsBox.hit.connect(lambda state, extra, player: self.changeAttribute(state, extra, player, 'queueMidiEvents'))#, extra = humanPlayer.preferencesTab.queueMidiEventsBox.isChecked()))
humanPlayer.preferencesTab.keyboardInpBox.hit.connect(lambda state, extra, player: self.changeAttribute(state, extra, player, 'internalKeyboardFlag'))#, extra = humanPlayer.preferencesTab.keyboardInpBox.isChecked()))
humanPlayer.preferencesTab.channelInBox.hit.connect(lambda state, extra, player: self.changeAttribute(state, extra, player, 'channelIn'))
humanPlayer.preferencesTab.channelOutBox.hit.connect(lambda state, extra, player: self.changeAttribute(state, extra, player, 'channelOut'))
humanPlayer.preferencesTab.midiOutBox.hit.connect(lambda state, extra, player: self.changeAttribute(state, extra, player, 'defaultMidiOut'))
humanPlayer.preferencesTab.midiInBox.hit.connect(lambda state, extra, player: self.changeAttribute(state, extra, player, 'defaultMidiIn'))
humanPlayer.preferencesTab.refreshMidiButton.pressed.connect(self.updatePortsDict)
# as I mentioned before, every human's sync module, emmits a signal (midiReaderOutputSignal)
# in order to inform the machine player(s) about the new notes the human(s) played
for machinePlayer in machinePlayers:
humanPlayer.modules['syncModule'].midiReaderOutputSignal.connect(machinePlayer.modules['asyncModule'].forwardPass)
# human's sync module also sends every new note to the Manager()'s receiver method
humanPlayer.modules['syncModule'].midiReaderOutputSignal.connect(self.manager.receiver)
# the line below is not currently used. But when we ll add real time beat tracking
# the async module of humanPlayer will measure the time between MIDI events, and will
# inform the TempoEstimator() module to calculate the current BPM speed
# humanPlayer.modules['asyncModule'].sendDurToEstimatorSignal.connect(self.tempoEstimator.dur2bpm)
# connect the signal the clock emits with all the human sync modules
self.clock.clockSignal.connect(humanPlayer.modules['syncModule'].getNewMidiEvent)
for machinePlayer in machinePlayers:
self.toolbar.reset.triggered.connect(machinePlayer.modules['asyncModule'].initHiddenStates)
# human's sync module also sends every new note to the Manager()'s receiver method
machinePlayer.modules['syncModule'].neuralNetSyncOutputSignal.connect(self.manager.receiver)
# currently not in use
# self.toolbar.condition.triggered.connect(machinePlayer.modules['asyncModule'].loadConditionFile)
# when we have more than one machine player, each note that a machine player generates
# has to sent to all the other machine players also
for machinePlayerOther in machinePlayers:
if machinePlayerOther.name != machinePlayer.name :
machinePlayer.modules['syncModule'].neuralNetSyncOutputSignal.connect(machinePlayerOther.modules['asyncModule'].forwardPass)
# connect the signal the clock emits with all the machine sync modules
self.clock.clockSignal.connect(machinePlayer.modules['syncModule'].getNewNeuralNetPrediction)
# actions in the machine player tab of preferences box, trigger signals that call
# the method self.changeAttribute, with the appropriate arguments
machinePlayer.preferencesTab.channelOutBox.hit.connect(lambda state, extra, player: self.changeAttribute(state, extra, player, 'channelOut'))
machinePlayer.preferencesTab.tempBox.hit.connect(lambda state, extra, player: self.changeAttribute(state, extra, player, 'temperature'))
machinePlayer.preferencesTab.refreshMidiButton.pressed.connect(self.updatePortsDict)
machinePlayer.preferencesTab.midiOutBox.hit.connect(lambda state, extra, player: self.changeAttribute(state, extra, player, 'defaultMidiOut'))
# TODO this is not correct for the case I have two machine players
# machinePlayer.tool.tempBox.hit.connect(lambda state, extra, name: self.changeAttribute(state, extra, name, 'temperature'))
#self.toolbar.randomnessSlider.valueChanged.connect(self.fixThat)
# self.mixer.metronomeSlider.volumeSlider.valueChanged.connect(self.updateVolumes)
# we ll have only one metronome player, so no need for for loop
metronomePlayer.modules['syncModule'].metronome2managerSignal.connect(self.manager.receiver)
metronomePlayer.preferencesTab.channelOutBox.hit.connect(lambda state, extra, player: self.changeAttribute(state, extra, player, 'channelOut'))
metronomePlayer.preferencesTab.firstPitchBox.hit.connect(lambda state, extra, player: self.changeAttribute(state, extra, player, 'pitch1'))
metronomePlayer.preferencesTab.otherPitchBox.hit.connect(lambda state, extra, player: self.changeAttribute(state, extra, player, 'pitch2'))
metronomePlayer.preferencesTab.refreshMidiButton.pressed.connect(self.updatePortsDict)
metronomePlayer.preferencesTab.midiOutBox.hit.connect(lambda state, extra, player: self.changeAttribute(state, extra, player, 'defaultMidiOut'))
# again, currently tempoEstimator is not used, but when it does, we want it to update
# the Clock() with the new BPM estimation
# self.tempoEstimator.updateTempoSignal.connect(self.sendNewBpmVal)
# connect the signal the clock emits with the metronome sync module
self.clock.clockSignal.connect(metronomePlayer.modules['syncModule'].process)
# manager's signals connect with updatePlot(pianoroll) and memory
# self.manager.updatePianoRollPainter.connect(self.pianoRollPainter.updatePlot)
self.manager.updateMemorySignal.connect(self.memory.getNewNoteEvent)
#self.threadAudio2MidiEvents.started.connect(self.audio2MidiEvents.process)
#self.threadPitchEstimator.started.connect(self.pitchEstimator.process)
# Staff signals
# ctrlSignal is emitted when I press ctrl. StaffView() needs the ctrl events
# in order to know when the user wants to zoom or slide the staves
self.ctrlSignal.connect(self.staffView.ctrlKeyReceiver)
# We want the staff related threads to begin after the staffView is ready,
# so when StaffView() is ready, it emits a signal which connects to the method
# setupDelayedThreadsAndSignals which starts the staff related threads
self.staffView.startStaffPainterSignal.connect(self.setupDelayedThreadsAndSignals)
# Toolbar signals
self.toolbar.playPause.triggered.connect(self.pauseResumeClock)
self.toolbar.reset.triggered.connect(self.reset)
self.toolbar.bpmBox.valueChanged.connect(self.sendNewBpmVal)
self.toolbar.save.triggered.connect(self.memory.saveHistory)
self.toolbar.preferences.triggered.connect(self.preferences.showWindow)
# self.toolbar.enforce.triggered.connect(self.enforceSignal)
# self.toolbar.clear.triggered.connect(self.clearScene)
# MenuBar Signal
self.menuBar.showMixerAction.triggered.connect(self.mixer.showWindow)
self.menuBar.quitAction.triggered.connect(self.quit)
#self.menuBar.preferencesAction.triggered.connect(self.preferences.showWindow)
self.menuBar.aboutAction.triggered.connect(self.about)
self.menuBar.aboutQtAction.triggered.connect(self.aboutQt)
@pyqtSlot()
def setupDelayedThreadsAndSignals(self):
#TODO staffPainter() is not a thread. If I move it to a thread
# as I do with all the other modules, I get an error. Currently
# the staffPaintin processes run in the main thread.
# self.threadStaffPainter = QThread()
self.staffPainter = StaffPainter(staffView = self.staffView, notesDict = self.notesDict,
appctxt = self.appctxt)
# self.staffPainter.moveToThread(self.threadStaffPainter)
self.staffPainter.sendNoteItemToMain.connect(self.staffView.updateStaffView)
self.manager.updateStaffPainter.connect(self.staffPainter.getNewNoteEvent)
self.staffView.updatePaintersRectSignal.connect(self.staffPainter.viewChanged)
self.toolbar.reset.triggered.connect(self.staffPainter.resetEvent)
self.threadPianoRollPainter = QThread()
self.pianoRollPainter = PianoRollPainter(self.pianoRollView, self.appctxt)
self.pianoRollPainter.moveToThread(self.threadPianoRollPainter)
self.manager.updatePianoRollPainter.connect(self.pianoRollPainter.updatePlot)
self.threadPianoRollPainter.start()
# self.threadStaffPainter.start()
@pyqtSlot(object, str, str, object)
def changeAttribute(self, state, extra, player, attribute):
print(f"name {player.name} state {state} attribute {attribute} extra {extra} enableMidiKeyb {player.enableMidiKeyb}")
if 'channel' in attribute:
state += 143
if 'defaultMidiIn' in attribute:
self.connectToNewMidiInput(state , player)
elif 'defaultMidiOut' in attribute:
self.connectToNewMidiOutput(state , player)
else:
if extra is None :
setattr(player, attribute, state)
else:
setattr(player, attribute, extra)
#player.directMonFlag = player.directMonFlag ^ True
#self.directMonitorFlag = self.directMonitorFlag ^ True
#self.directMonSignal.emit(self.directMonitorFlag)
def updateGrid(self):
#TODO
raise NotImplementedError
def updatePortsDict(self):
self.midiInDict = {"":-1}
self.midiOutDict = {"":-1}
self.usedMidiInPorts = {}
self.usedMidiOutPorts = {}
#TODO delete these
tempMidiIn = rtmidi.MidiIn()
tempMidiOut = rtmidi.MidiOut()
portsNumberInp = range(tempMidiIn.get_port_count())
portsNumberOut = range(tempMidiOut.get_port_count())
for i in portsNumberInp:
portName = tempMidiIn.get_port_name(i)
#print(self.midiIn.get_port_name(i))
self.midiInDict[portName] = i
for i in portsNumberOut:
portName = tempMidiOut.get_port_name(i)
#print(self.midiOut.get_port_name(i))
self.midiOutDict[portName] = i
#print(self.midiInDict.keys())
#print(self.midiOutDict.keys())
for player in self.players:
player.midiOut.close_port()
player.preferencesTab.midiOutBox.clear()
player.preferencesTab.midiOutBox.addItems(self.midiOutDict.keys())
if 'human' in player.type:
player.midiIn.close_port()
player.preferencesTab.midiInBox.clear()
player.preferencesTab.midiInBox.addItems(self.midiInDict.keys())
def openDefaultPorts(self):
outPorts = list(self.midiOutDict.items())
inPorts = list(self.midiInDict.items())
print(outPorts)
print(inPorts)
for player in self.players:
if player.defaultMidiOut is not None:
defaultOutInd = [port[1] for port in outPorts if player.defaultMidiOut in port[0]]
if len(defaultOutInd) > 0:
player.preferencesTab.midiOutBox.setCurrentIndex(defaultOutInd[0]+1)
self.connectToNewMidiOutput(defaultOutInd[0]+1, player)
if 'human' in player.type:
if player.defaultMidiIn is not None:
defaultInInd = [port[1] for port in inPorts if player.defaultMidiIn in port[0]]
if len(defaultInInd) > 0:
player.preferencesTab.midiInBox.setCurrentIndex(defaultInInd[0]+1)
self.connectToNewMidiInput(defaultInInd[0]+1, player)
# currently not in use
# @pyqtSlot(int)
# def fixThat(self,value):
# machinePlayers = [player for player in self.players if player.type in ['machine', 'machine2']]
# for machinePlayer in machinePlayers:
# machinePlayer.preferencesTab.tempBox.setValue(value)
def connectToNewMidiInput(self, portInd, player):
# print("___________________________________________________________")
if portInd > 0:
if portInd-1 in list(self.usedMidiInPorts.keys()):
# maybe a MIDI port is already open and in use by another
# player, so in this case opening again the same port would
# cause an error.
player.midiIn = self.usedMidiInPorts[portInd-1]
else:
# if the MIDI port is not already in use, then open it and
# assign it to the player
temp = rtmidi.MidiIn()
temp.open_port(portInd-1)
player.midiIn = temp
self.usedMidiInPorts[portInd-1] = temp
@pyqtSlot(int)
def connectToNewMidiOutput(self, portInd, player):
# print("___________________________________________________________")
if portInd > 0:
# print(f"++++++++++++{player.name} {portInd} and {list(self.usedMidiOutPorts.keys())}")
if portInd-1 in list(self.usedMidiOutPorts.keys()):
# maybe a MIDI port is already open and in use by another
# player, so in this case opening again the same port would
# cause an error.
player.midiOut = self.usedMidiOutPorts[portInd-1]
else:
# if the MIDI port is not already in use, then open it and
# assign it to the player
temp = rtmidi.MidiOut()
temp.open_port(portInd-1)
player.midiOut = temp
print(temp.is_port_open())
print(player.midiOut.is_port_open())
self.usedMidiOutPorts[portInd-1] = temp
# currently not used
@pyqtSlot(str)
def setTimeSignature(self, value):
#TODO thats not good practice, I shouldn't communicate directly with self.clock
# but I should do it through signals/slots. But since I am running a while loop
# on self.clock.run1, the event loop is blocked, so no signal/slot are received
self.clock.setTimeSignature(value)
@pyqtSlot()
def sendNewBpmVal(self):
value = self.toolbar.bpmBox.value()
print(f" bpmBox valueChanged signal activated {value}")
#TODO thats not good practice, I shouldn't communicate directly with self.clock
# but I should do it through signals/slots. But since I am running a while loop
# on self.clock.run1, the event loop is blocked, so no signal/slot are received
self.clock.changeBpm(value)
#@pyqtSlot()
def pauseResumeClock(self):
#TODO thats not good practice, I shouldn't communicate directly with self.clock
# but I should do it through signals/slots. But since I am running a while loop
# on self.clock.run1, the event loop is blocked, so no signal/slot are received
self.clock.pauseResumeClock()
if self.clock.paused:
self.sendMidiOffEvents()
self.toolbar.playPause.setIcon(QtGui.QIcon(self.appctxt.get_resource("Images/svg/play.svg")))
else :
self.toolbar.playPause.setIcon(QtGui.QIcon(self.appctxt.get_resource("Images/svg/pause.svg")))
# currently not used
@pyqtSlot()
def clearScene(self):
self.toolbar.playPause.trigger()
self.staffView.staffScene.clear()
self.staffView.horizontalScrollBar().setValue(0)
self.staffView.addStaffs()
self.plotDataDnn = []
self.plotCurve1.clear()
self.plotCurve2.clear()
self.plotCurve1.cl
#del self.staffScene
#del self.staffLines
#print(self.horizontalScrollBar().value())
# print(self.parent.staffPainter.svgTreble)
#self.staffScene.addItem(self.staffLines)
@pyqtSlot()
def reset(self):
#TODO if already paused, do not trigger
# also keep in mind that QAction.trigger() is already a slot in C++
self.toolbar.playPause.trigger()
self.toolbar.save.trigger()
#TODO thats not good practice, I shouldn't communicate directly with self.clock
# but I should do it through signals/slots. But since I am running a while loop
# on self.clock.run1, the event loop is blocked, so no signal/slot are received
self.clock.reset()
@pyqtSlot()
def about(self):
#msg = QString("We must be <b>bold</b>, very <b>bold</b>")
with open(self.appctxt.get_resource("about.html"),"r") as aboutFile:
aboutTxt = aboutFile.read()
QMessageBox.about(self, "About", aboutTxt)# "BachDuet: A Human-Machine Duet Improvisation System\n")
@pyqtSlot()
def aboutQt(self):
QMessageBox.aboutQt(self, "About Qt")
@pyqtSlot()
def quit(self):
QApplication.quit()
#sys.exit()
def closeEvent(self, event):
#TODO thats not good practice, I shouldn't communicate directly with self.clock
# but I should do it through signals/slots. But since I am running a while loop
# on self.clock.run1, the event loop is blocked, so no signal/slot are received
self.clock.stopClock()
for player in self.players:
if player.type == 'human':
player.modules['asyncModule'].stopit()
# player.modules['asyncAudioModule'].stopit()
if player.type in ['machine']:
#TODO replace this with signal and slot
player.modules['asyncModule'].hidden2pickle()
self.memory.saveHistory()
#self.logger.info("MESSAGE FROM LOGGER _ CLOSEEEED")
#self.audioRecorder.stopit()
#self.pitchEstimator.stopit()
#self.audio2MidiEvents.stopit()
#self.keyboardReaderAsync.stopit()
self.sendMidiOffEvents()
#self.deleteLater()
def sendMidiOffEvents(self):
time.sleep(1)
for i in range(128):
for player in self.players:
player.midiOut.send_message([player.channelOut, i, 0])
def keyPressEvent(self, eventQKeyEvent):
key = eventQKeyEvent.key()
if not eventQKeyEvent.isAutoRepeat():
if key == Qt.Key_Control:
self.ctrlPressed = True
self.ctrlSignal.emit("press")
try:
midiNumberPressed = KeyMappings[key]
#print(KeyMappings[key])
#print(f"line 724 in main {KeyMappings[key]} ctrl {self.ctrlPressed}")
if self.ctrlPressed is False:
# self.internalKeyboardAsyncBuffer.put([self.players[0].channelIn, aaa, 127])
for player in self.players:
if player.type in ['human','human2']:
#print(f"internalKeyb flag for {player.type} is {player.internalKeyboardFlag}")
if player.internalKeyboardFlag is True:
player.modules['internalKeyboardQueue'].put([player.channelIn, midiNumberPressed, player.volume])
# [player.modules['internalKeyboardQueue'].put([player.channelIn, aaa, 127]) for player in self.players if player.type in ['human', 'human2']]
#print(f"self.ctrlPressed {self.ctrlPressed} kai meta id ouras = {id(self.internalKeyboardAsyncBuffer)} with codent {self.internalKeyboardReaderAsync.queue}")
except Exception as e:
print(f"exception {e} and key is {key}")
pass
if self.ctrlPressed is True:
if key == Qt.Key_E:
self.enforceSignal.emit()
#print(f"patisa EEEE")
if key == Qt.Key_Right:
for player in self.players:
if player.type in ["machine", "machine2"]:
player.modules['asyncModule'].saveHiddenStates(label = 1)
if key == Qt.Key_Left:
for player in self.players:
if player.type in ["machine", "machine2"]:
player.modules['asyncModule'].saveHiddenStates(label = 0)
#print('released')
def keyReleaseEvent(self, eventQKeyEvent):
key = eventQKeyEvent.key()
if not eventQKeyEvent.isAutoRepeat():
if key == Qt.Key_Control:
self.ctrlPressed = False
self.ctrlSignal.emit("release")
try:
aaa = KeyMappings[key]
if self.ctrlPressed is False:
for player in self.players:
if player.type in ['human','human2']:
if player.internalKeyboardFlag is True:
player.modules['internalKeyboardQueue'].put([player.channelIn, aaa, 0])
# self.internalKeyboardAsyncBuffer.put([self.players[0].channelIn, aaa, 0])
# [player.modules['internalKeyboardQueue'].put([player.channelIn, aaa, 0]) for player in self.players if player.type in ['human', 'human2']]
#print(f"self.ctrlPressed {self.ctrlPressed} kai meta id ouras = {id(self.internalKeyboardAsyncBuffer)} with codent {self.internalKeyboardReaderAsync.queue}")
except:
pass
def configure_logger(name):
logging.config.dictConfig({
'version': 1,
'formatters': {
'default': {'format': '%(asctime)s.%(msecs)05d %(levelname)s %(module)s - %(funcName)s -%(threadName)s -%(lineno)s: %(message)s', 'datefmt': '%H:%M:%S'}
},
'handlers': {
'console': {
'level': 'DEBUG',
'class': 'logging.StreamHandler',
'formatter': 'default',
'stream': 'ext://sys.stdout'
},
},
'loggers': {
'default': {
'level': 'DEBUG',
'handlers': ['console']
}
},
'disable_existing_loggers': False
})
return logging.getLogger(name)
if __name__ == '__main__':
logger = configure_logger('default')
CUDA_LAUNCH_BLOCKING=1
appctxt = ApplicationContext()
styleSheet = appctxt.get_resource("styleSheet.css")
app = QApplication(sys.argv)
with open(styleSheet,"r") as fh:
app.setStyleSheet(fh.read())
bachDuet = BachDuet(appctxt, logger)
exit_code = app.exec_()
sys.exit(exit_code)