Skip to content

Commit d1afe50

Browse files
committed
bug fix: add some comments
1 parent b57cf19 commit d1afe50

File tree

11 files changed

+149
-9
lines changed

11 files changed

+149
-9
lines changed

midicontroller/getinstallpath.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
import os.path
1919
import sys
2020
def getInstallPath():
21+
"""
22+
function that returns the directory where the script is located
23+
"""
2124
thePath = os.path.dirname(sys.argv[0])
2225
if thePath:
2326
thePath += os.sep

midicontroller/main.py

Lines changed: 50 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ def __init__(self, MainWindow):
6868
]
6969
for i,cb in enumerate(self.midiDeviceComboBoxes):
7070
cb.currentIndexChanged.connect(self.choose_midi_device_for_channel(i))
71-
71+
# reset midi channel buttons
7272
self.resetButtons = [
7373
self.resetChannel0Button,
7474
self.resetChannel1Button,
@@ -92,7 +92,6 @@ def __init__(self, MainWindow):
9292

9393
# provide storage to remember the last 512 raw eeg data points
9494
self.last_512_raw_waves = deque([0]*RAW_VAL_WIN_SIZE, RAW_VAL_WIN_SIZE)
95-
9695
self.counter = 0
9796

9897
# reserve a notequeue to remember 3 notes on each channel
@@ -111,23 +110,29 @@ def __init__(self, MainWindow):
111110
self.signal_quality = "unknown signal quality"
112111
self.update_statusbar()
113112

113+
# instantiate an object that can transform note names to midi numbers
114114
self.notelookup = notelookup.NoteLookup()
115115

116-
# more signals
116+
# more signals: what to do if a checkbox is clicked
117117
self.attentionCheckBox.clicked.connect(self.attentionCheckBoxClicked)
118118
self.meditationCheckBox.clicked.connect(self.meditationCheckBoxClicked)
119119
self.eyeBlinkCheckBox.clicked.connect(self.eyeBlinkCheckBoxClicked)
120120

121+
# make sure the menu entries do something
121122
self.actionQuit.triggered.connect(QtGui.qApp.quit)
122123
self.actionSave_state.triggered.connect(self.save_state)
123124
self.actionLoad_state.triggered.connect(self.load_state)
124125

126+
# determine which ui elements should be saved/loaded
125127
self.setup_serialization()
126128

129+
# discover the plugins and display in the ui as needed
127130
self.setup_plugins()
128131

129132
def setup_serialization(self):
130-
133+
"""
134+
function that collects all information required for saving ui state to model file
135+
"""
131136
def lineedit_getter(lineedit):
132137
return "{0}".format(lineedit.text())
133138

@@ -168,6 +173,10 @@ def combobox_setter(combobox, value):
168173
self.serializer.register("eyeBlinkPluginComboBox",self.eyeBlinkPluginComboBox, combobox_getter, combobox_setter)
169174

170175
def setup_plugins(self):
176+
"""
177+
discover all plugins and populate the plugin combo boxes
178+
setup signals so that selecting a plugin has some effect
179+
"""
171180
self.prevAttentionPluginIndex = 0
172181
self.prevMeditationPluginIndex = 0
173182
self.prevEyeBlinkPluginIndex = 0
@@ -196,6 +205,9 @@ def setup_plugins(self):
196205
self.eyeBlinkPluginSelected(0)
197206

198207
def suspend_plugin(self, plugin_object, section):
208+
"""
209+
define how to suspend a running plugin without deleting it, e.g. when the checkbox is unchecked
210+
"""
199211
plugin_object.suspend(section)
200212
midichan = plugin_object.get_midi_channel_list(section)
201213
for m in midichan:
@@ -204,6 +216,9 @@ def suspend_plugin(self, plugin_object, section):
204216
self.midiOut[m].send_message(msg)
205217

206218
def attentionCheckBoxClicked(self):
219+
"""
220+
what to do if the attention check box is clicked
221+
"""
207222
from yapsy.PluginManager import PluginManagerSingleton
208223
manager = PluginManagerSingleton.get()
209224
plugin = manager.getAllPlugins()[self.prevAttentionPluginIndex]
@@ -213,6 +228,9 @@ def attentionCheckBoxClicked(self):
213228
plugin.plugin_object.resume('attention')
214229

215230
def attentionPluginSelected(self, index):
231+
"""
232+
what to do if an attention plugin is selected
233+
"""
216234
from yapsy.PluginManager import PluginManagerSingleton
217235
manager = PluginManagerSingleton.get()
218236
plugin = manager.getAllPlugins()[self.prevAttentionPluginIndex]
@@ -225,6 +243,9 @@ def attentionPluginSelected(self, index):
225243
self.attentionCheckBoxClicked()
226244

227245
def meditationCheckBoxClicked(self):
246+
"""
247+
what to do if meditation checkbox is clicked
248+
"""
228249
from yapsy.PluginManager import PluginManagerSingleton
229250
manager = PluginManagerSingleton.get()
230251
plugin = manager.getAllPlugins()[self.prevMeditationPluginIndex]
@@ -235,6 +256,9 @@ def meditationCheckBoxClicked(self):
235256

236257

237258
def meditationPluginSelected(self, index):
259+
"""
260+
what to do if a meditation plugin is selected
261+
"""
238262
from yapsy.PluginManager import PluginManagerSingleton
239263
manager = PluginManagerSingleton.get()
240264
plugin = manager.getAllPlugins()[self.prevMeditationPluginIndex]
@@ -248,6 +272,9 @@ def meditationPluginSelected(self, index):
248272

249273

250274
def eyeBlinkCheckBoxClicked(self):
275+
"""
276+
what to do if the eye blink check box is clicked
277+
"""
251278
from yapsy.PluginManager import PluginManagerSingleton
252279
manager = PluginManagerSingleton.get()
253280
plugin = manager.getAllPlugins()[self.prevEyeBlinkPluginIndex]
@@ -257,6 +284,9 @@ def eyeBlinkCheckBoxClicked(self):
257284
plugin.plugin_object.resume('eyeBlink')
258285

259286
def eyeBlinkPluginSelected(self, index):
287+
"""
288+
what to do if an eye blink plugin is selected
289+
"""
260290
from yapsy.PluginManager import PluginManagerSingleton
261291
manager = PluginManagerSingleton.get()
262292
plugin = manager.getAllPlugins()[self.prevEyeBlinkPluginIndex]
@@ -269,6 +299,9 @@ def eyeBlinkPluginSelected(self, index):
269299
self.eyeBlinkCheckBoxClicked()
270300

271301
def save_state(self):
302+
"""
303+
get state of ui and all plugins, and save to file
304+
"""
272305
model = self.serializer.ui_to_model()
273306
plugin_model = {}
274307
from yapsy.PluginManager import PluginManagerSingleton
@@ -284,6 +317,9 @@ def save_state(self):
284317
f.write(modelstring)
285318

286319
def load_state(self):
320+
"""
321+
get state of ui and all plugins from file, and set to the ui
322+
"""
287323
with open("midicontroller-state.json", "r") as f:
288324
modelstring = f.read()
289325
import json
@@ -304,6 +340,9 @@ def load_state(self):
304340

305341

306342
def quit_gracefully(self):
343+
"""
344+
switch off all playing notes and delete all plugins
345+
"""
307346
for i,mo in enumerate(self.midiOut):
308347
bytemsg = self.notequeue.clear_notes(i)
309348
for msg in bytemsg:
@@ -316,7 +355,7 @@ def quit_gracefully(self):
316355

317356
def monitor(self):
318357
"""
319-
start/stop button
358+
start/stop button pressed
320359
"""
321360
print "monitor"
322361
if self.running:
@@ -354,10 +393,16 @@ def monitor(self):
354393
self.update_statusbar()
355394

356395
def handle_poor_signal(self, headset, value):
396+
"""
397+
react to a poor signal event
398+
"""
357399
self.signal_quality = "poor signal quality {0}%".format(value)
358400
self.MainWindow.update_statusbar_signal.emit()
359401

360402
def handle_good_signal(self, headset, value):
403+
"""
404+
handle to a good signal event
405+
"""
361406
self.signal_quality = "good signal quality"
362407
self.MainWindow.update_statusbar_signal.emit()
363408

midicontroller/notelookup.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ def __init__(self):
4444
self.lookup_table[string.lower("{0}".format(notename))] = notenumber
4545

4646
def lookup(self, note):
47+
"""
48+
transform midi number string to integer;
49+
if that fails, try to convert from note name to midi number
50+
"""
4751
try:
4852
return int(note)
4953
except ValueError:

midicontroller/notequeue.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ def play_note(self, channel, note, velocity):
4646
return msgbytes
4747

4848
def clear_notes(self, channel):
49+
"""
50+
create note off midi msgs for all notes in the channel
51+
"""
4952
msgbytes = []
5053
q = self.qs[channel]
5154
while q:
@@ -58,6 +61,9 @@ def clear_notes(self, channel):
5861
return msgbytes
5962

6063
def clear_all_notes(self):
64+
"""
65+
create note off midi msgs for all notes in all channels
66+
"""
6167
msgbytes = []
6268
for i,q in enumerate(self.qs):
6369
msgbytes.extend(self.clear_notes(i))

midicontroller/plugins/scoregenerators/simplegenerator/notelookup.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
# along with mindwave-python. If not, see <http://www.gnu.org/licenses/>. #
1616
##################################################################################
1717

18+
# NOTE: duplicated from ../../.. -> need to find out how to use code from parent folders together with yapsy
19+
1820
import string
1921

2022
class NoteLookup(object):

midicontroller/plugins/scoregenerators/simplegenerator/parseutils.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
# along with mindwave-python. If not, see <http://www.gnu.org/licenses/>. #
1616
##################################################################################
1717

18+
# NOTE: duplicated from ../../.. -> need to find out how to use code from parent folders with yapsy
19+
1820
import notelookup
1921

2022
class ParseUtil(object):

midicontroller/plugins/scoregenerators/simplegenerator/simplegenerator.py

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
##################################################################################
1717

1818
from yapsy.IPlugin import IPlugin
19-
from yapsy.PluginManager import PluginManagerSingleton
2019
import parseutils
2120

2221
from PyQt4 import QtGui
@@ -27,12 +26,19 @@ class SimpleGenerator(IPlugin):
2726
name = "Simple Generator"
2827

2928
def __init__(self):
29+
"""
30+
set up simple generator plugin
31+
"""
3032
super(SimpleGenerator, self).__init__()
3133
self.parseutil = parseutils.ParseUtil()
3234
self.instances = {}
3335
self.playing_suspended = {}
3436

3537
def get_ui(self, parent, name):
38+
"""
39+
create the ui for embedding in main ui
40+
keep a reference for later usage
41+
"""
3642
if name in self.instances:
3743
return self.instances[name][1]
3844

@@ -43,6 +49,10 @@ def get_ui(self, parent, name):
4349
return Panel
4450

4551
def get_state_as_dict(self, parent):
52+
"""
53+
return internal state of the plugin as a dictionary;
54+
used during save state to file
55+
"""
4656
result = {}
4757
for name in self.instances:
4858
if name not in self.instances:
@@ -55,6 +65,10 @@ def get_state_as_dict(self, parent):
5565
return result
5666

5767
def set_state_from_dict(self, parent, dct):
68+
"""
69+
set internal state of the plugin from a dictionary;
70+
used during load state from file
71+
"""
5872
for name in dct:
5973
if name not in self.instances:
6074
self.get_ui(parent, name)
@@ -64,6 +78,9 @@ def set_state_from_dict(self, parent, dct):
6478
ui.allowedNotesEdit.setText("{0}".format(dct[name]["allowedNotesEdit"]))
6579

6680
def trigger(self, name, midiOuts, notequeue, value):
81+
"""
82+
method called by the main program to ask the plugin to do something
83+
"""
6784
if name not in self.instances:
6885
return
6986

@@ -99,24 +116,37 @@ def trigger(self, name, midiOuts, notequeue, value):
99116
midiOuts[chan].send_message(msg)
100117

101118
def suspend(self, name):
119+
"""
120+
suspend generating music while not deleting the plugin
121+
"""
102122
self.playing_suspended[name] = True
103123

104124
def resume(self, name):
125+
"""
126+
resume generating music
127+
"""
105128
self.playing_suspended[name] = False
106129

107130
def is_suspended(self, name):
131+
"""
132+
true if plugin is currently suspended
133+
"""
108134
return name in self.playing_suspended and self.playing_suspended[name]
109135

110136
def stop(self, name):
137+
"""
138+
stop and delete plugin
139+
"""
111140
if name in self.instances:
112141
del self.instances[name]
113142

114143
def get_midi_channel_list(self, name):
144+
"""
145+
return a list of all channels this plugin can write midi msgs to
146+
"""
115147
if name in self.instances:
116148
ui = self.instances[name][0]
117149
midichan = self.parseutil.parse_midi_channel_list(ui.midiChannelEdit.text())
118150
return midichan
119151
return []
120152

121-
122-

midicontroller/plugins/scoregenerators/spraycangenerator/notelookup.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
# along with mindwave-python. If not, see <http://www.gnu.org/licenses/>. #
1616
##################################################################################
1717

18+
# NOTE: duplicated from ../../.. -> need to find out how to use code from parent folder using yapsy
19+
1820
import string
1921

2022
class NoteLookup(object):

midicontroller/plugins/scoregenerators/spraycangenerator/parseutils.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
# along with mindwave-python. If not, see <http://www.gnu.org/licenses/>. #
1616
##################################################################################
1717

18+
# NOTE: almost duplicated from ../../.. -> need to find out how to use code from parent folder with yapsy
19+
1820
import notelookup
1921

2022
class ParseUtil(object):

0 commit comments

Comments
 (0)