/
MainWindow.py
279 lines (205 loc) · 10 KB
/
MainWindow.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
# -*- coding: utf-8 -*-
"""
FlyFi - Floppy-Fidelity
@author: Ricardo (XeN) Band <xen@c-base.org>,
Stephan (coon) Thiele <coon@c-base.org>
This file is part of FlyFi.
FlyFi is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
FlyFi is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with FlyFi. If not, see <http://www.gnu.org/licenses/>.
Diese Datei ist Teil von FlyFi.
FlyFi ist Freie Software: Sie können es unter den Bedingungen
der GNU General Public License, wie von der Free Software Foundation,
Version 3 der Lizenz oder (nach Ihrer Option) jeder späteren
veröffentlichten Version, weiterverbreiten und/oder modifizieren.
FlyFi wird in der Hoffnung, dass es nützlich sein wird, aber
OHNE JEDE GEWÄHELEISTUNG, bereitgestellt; sogar ohne die implizite
Gewährleistung der MARKTFÄHIGKEIT oder EIGNUNG FÜR EINEN BESTIMMTEN ZWECK.
Siehe die GNU General Public License für weitere Details.
Sie sollten eine Kopie der GNU General Public License zusammen mit diesem
Programm erhalten haben. Wenn nicht, siehe <http://www.gnu.org/licenses/>.
FlyFi is using tango icons: <http://tango.freedesktop.org/>.
"""
from PySide import QtGui, QtCore
from SettingsWindow import SettingsWindow
from MidiIn import MidiIn
from MidiFileIn import MidiFileIn
from FloppyOut import FloppyOut
import pygame.midi
import profile
class MainWindow(QtGui.QMainWindow):
"""
The mainwindow containing options to fine tune things
and start everything up.
"""
def cb_midi_event_list(self, event_list):
# parsing the events
# ==================
# only note on, note off and pitch wheel range are
# important for us, so the other midi events are just ignored.
# status, data1, data2, tick
event_str = None
channel = None
for event in event_list:
status = event.statusmsg
tick = event.tick
if event.name == "Note Off":
status += event.channel # due to buggy python-midi lib
channel = status - 0x80 + 1
midi_note = event.data[0]
velocity = event.data[1]
event_str = "Chan %s Note off" % channel
self.fout.stop_note(channel)
self.mout.note_off(midi_note, velocity, channel - 1) # only for debugging. remove later!!!
elif event.name == "Note On":
status += event.channel # due to buggy python-midi lib
channel = status - 0x90 + 1
midi_note = event.data[0]
velocity = event.data[1]
event_str = "Chan %s Note on" % channel
if velocity > 0:
self.mout.note_on(midi_note, velocity, channel - 1) # only for debugging. remove later!!!
self.fout.play_note(channel, midi_note)
else:
self.mout.note_on(midi_note, velocity, channel - 1) # only for debugging. remove later!!!
self.fout.stop_note(channel) # a volume of 0 is the same as note off
elif event.name == "Set Tempo":
self.midi_fin.set_bpm(event.bpm)
elif event.name == "Program Change": # Chan Program change (change instrument)
if self.mout is not None:
self.mout.set_instrument(event.data[0], event.channel)
# elif status >= 0xE0 and status <= 0xEF: # pitch bend (TODO: don't ignore!)
# channel = status - 0xE0 + 1
# velocity = event.data[1]
# pitch_value = 128 * velocity
# event_str = "Chan %s pitch bend with value %s and" % (channel, pitch_value)
else:
event_str = "unknown event (0x%0X)" % (status)
if event_str != None:
pass
#print "%s with note %s and velocity %s @ %s" % (event_str, midi_note, velocity, tick)
# used for live midi
def cb_midi_event(self, status, data1, data2, tick):
# parsing the events
# ==================
# only note on, note off and pitch wheel range are
# important for us, so the other midi events are just ignored.
event_str = None
channel = None
if status >= 0x80 and status <= 0x8F: # Note Off
channel = status - 0x80 + 1
midi_note = data1
velocity = data2
event_str = "Chan %s Note off" % channel
self.fout.stop_note(channel)
#self.mout.note_off(midi_note, velocity, channel - 1) # only for debugging. remove later!!!
elif status >= 0x90 and status <= 0x9F: # Note On
channel = status - 0x90 + 1
midi_note = data1
velocity = data2
event_str = "Chan %s Note on" % channel
if velocity > 0:
#self.mout.note_on(midi_note, velocity, channel - 1) # only for debugging. remove later!!!
self.fout.play_note(channel, midi_note)
else:
#self.mout.note_on(midi_note, velocity, channel - 1) # only for debugging. remove later!!!
self.fout.stop_note(channel) # a volume of 0 is the same as note off
elif status >= 0xA0 and status <= 0xAF: # Polyphonic Aftertouch (ignore)
return
elif status >= 0xB0 and status <= 0xBF: # Chan Control mode change (ignore)
return
elif status >= 0xC0 and status <= 0xCF: # Chan Program change (ignore)
return #self.mout.set_instrument(event.data, event.channel)
elif status >= 0xD0 and status <= 0xDF: # Channel Aftertouch (ignore)
return
elif status >= 0xE0 and status <= 0xEF: # pitch bend (TODO: don't ignore!)
channel = status - 0xE0 + 1
velocity = data2
pitch_value = 128 * velocity
event_str = "Chan %s pitch bend with value %s and" % (channel, pitch_value)
else:
event_str = "unknown event (0x%0X)" % (status)
print "%s with note %s and velocity %s @ %s" % (event_str, data1, data2, tick)
return
if event_str != None:
pass
#print "%s with note %s and velocity %s @ %s" % (event_str, midi_note, velocity, tick)
def __init__(self):
"""
generate other windows, floppy output control and create the gui
"""
super(MainWindow, self).__init__()
#debug
pygame.midi.init() #debug
self.mout = pygame.midi.Output(pygame.midi.get_default_output_id())
self.midi_in = MidiIn(self.cb_midi_event)
self.midi_fin = MidiFileIn(self.cb_midi_event_list)
self.fout = FloppyOut()
self.settingswindow = SettingsWindow(self.midi_in, self.fout)
self.init_ui()
self.midi_in.start_midi_polling()
def setFloatNum(self, float_num):
self.lab_freq.setText( "%.2f" % (float_num / 100.0) )
def init_ui(self):
"""
create the gui and connect actions
"""
self.resize(480, 320)
self.setWindowTitle('FlyFi - Floppy-Fidelity')
self.setWindowIcon(QtGui.QIcon('images/flyfi-logo.png'))
self.center()
self.statusBar().showMessage('Ready')
act_exit = QtGui.QAction(QtGui.QIcon('images/exit.png'), '&Exit', self)
act_exit.setShortcut('Ctrl+Q')
act_exit.setStatusTip('Exit application')
act_exit.triggered.connect(QtCore.QCoreApplication.instance().quit)
act_settings = QtGui.QAction(QtGui.QIcon('images/settings.png'),
'&Settings', self)
act_settings.setShortcut('Ctrl+S')
act_settings.setStatusTip('Configure FlyFi')
act_settings.triggered.connect(self.cb_open_settings_window)
menubar = self.menuBar()
file_menu = menubar.addMenu('&File')
file_menu.addAction(act_settings)
file_menu.addAction(act_exit)
toolbar = self.addToolBar('Toolbar')
toolbar.addAction(act_settings)
toolbar.addAction(act_exit)
# midi player
openMidiButton = QtGui.QPushButton("Open Midi")
openMidiButton.clicked.connect(self.showFileDialog)
playButton = QtGui.QPushButton("Play")
playButton.clicked.connect(self.playMidiFile)
hbox = QtGui.QHBoxLayout()
hbox.addWidget(openMidiButton)
hbox.addWidget(playButton)
hbox.addStretch(1)
vbox = QtGui.QVBoxLayout()
vbox.addLayout(hbox)
vbox.addStretch(1)
centralwidget = QtGui.QWidget()
centralwidget.setLayout(vbox)
self.setCentralWidget(centralwidget)
def showFileDialog(self):
fname, _ = QtGui.QFileDialog.getOpenFileName(self, 'Open file')
self.midi_fin.open_midi_file(fname)
def playMidiFile(self):
self.midi_fin.play()
def center(self):
"""
center the window on the screen
"""
frame_geo = self.frameGeometry()
desktop_center = QtGui.QDesktopWidget().availableGeometry().center()
frame_geo.moveCenter(desktop_center)
self.move(frame_geo.topLeft())
def cb_open_settings_window(self):
self.settingswindow.show()
self.settingswindow.cb_update_serial_ports()