forked from josephernest/SamplerBox
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsamplerbox.py
258 lines (216 loc) · 8.13 KB
/
samplerbox.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
# SamplerBox
#
# author: Joseph Ernest (twitter: @JosephErnest, mail: contact@samplerbox.org)
# contributor: Alex MacRae (alex.finlay.macrae@gmail.com)
# url: http://www.samplerbox.org/
# license: Creative Commons ShareAlike 3.0 (http://creativecommons.org/licenses/by-sa/3.0/)
#
# samplerbox.py: Main file
#
# TODO: if we're compiling a dist (Windows/Mac), bundled files such as config.ini can't be found, ie relative paths are affected
# import os, sys
# if 'Python' in os.path.dirname(sys.executable):
# env_basename = os.path.dirname(sys.executable)
# else:
# env_basename = ''
#########################################
# IMPORT
# MODULES
#########################################
from os.path import ismount
from os.path import isfile
from os import system
import subprocess
import time
time_start = time.time()
usleep = lambda x: time.sleep(x / 1000000.0)
msleep = lambda x: time.sleep(x / 1000.0)
import threading
import rtmidi2
# from filters import FilterType, Filter, FilterChain
# from utility import byteToPCM, floatToPCM, pcmToFloat, sosfreqz
from modules import globalvars as gv
from modules import displayer
from modules import audiocontrols
from modules import buttons
from modules import systemfunctions
from modules import setlist
from modules import loadsamples
from modules import sound
from modules import midimaps
from modules import midicallback
from modules import midiserial
###########
# Fix USB #
###########
# if ismount('/media'):
# try:
# subprocess.call(["fsck", "-yvp", "/media"]) # auto-repair USB drive in case of dirty bits (if connected/mounted)
# path = gv.SAMPLES_DIR
# if 'media' in path:
# if path.endswith('/'): path = path[:-1]
# subprocess.call(['rm', '-v', path + '/FSCK*.REC'])
# else:
# subprocess.call(['rm', '-v', '/media/FSCK*.REC'])
# except:
# print 'USB repair failed'
try:
if ismount('/media'):
if 'media' in gv.SAMPLES_DIR:
subprocess.call(['rm', '-v', gv.SAMPLES_DIR.rstrip('/') + '/FSCK*.REC'])
else:
subprocess.call(['rm', '-v', '/media/FSCK*.REC'])
except:
pass
###################
# Fix MIDI Serial #
###################
try:
subprocess.call(['systemctl', 'stop', 'serial-getty@ttyAMA0.service'])
subprocess.call(['systemctl', 'disable', 'serial-getty@ttyAMA0.service'])
except:
print 'Failed to stop MIDI serial'
pass
###########
# Logging #
###########
# import sys
# log_file = open("console.log", 'w')
log_file = None
# sys.stdout = log_file
#######################
# Start Displayer #
# Load MIDI mappings #
# Start the Navigator #
# Start the GUI #
#######################
samples_fs_resize_format_script = '/boot/resize_samples_partition.sh'
gv.displayer = displayer.Displayer()
if isfile(samples_fs_resize_format_script):
gv.SYSTEM_MODE = 1
from modules import HD44780_sys_1
gv.displayer.LCD_SYS = HD44780_sys_1.LCD_SYS_1()
gv.displayer.LCD_SYS.temp_display = True
time.sleep(0.1)
gv.displayer.disp_change(str_override='ExpandingStorage', timeout=60, line=1, is_priority=True)
gv.displayer.disp_change(str_override='DO NOT TURN OFF!', timeout=60, line=2, is_priority=True)
time.sleep(0.1)
systemfunctions.mount_boot_rw()
systemfunctions.mount_root_rw()
system('sh ' + samples_fs_resize_format_script)
print 'Finished expanding. Reboot now.'
systemfunctions.SystemFunctions().reboot()
exit()
else:
print '\r\n***********\r\n/SAMPLES/ HAS BEEN GROWN AND FORMATTED - READY TO GO\r\n***********\r\n'
print '#### START SETLIST ####'
gv.setlist = setlist.Setlist()
print '#### END SETLIST ####\n'
if gv.SYSTEM_MODE == 1:
from modules import HD44780_sys_1
from modules import navigator_sys_1
gv.displayer.LCD_SYS = HD44780_sys_1.LCD_SYS_1()
gv.nav = navigator_sys_1.Navigator(navigator_sys_1.PresetNav)
elif gv.SYSTEM_MODE == 2:
from modules import HD44780_sys_2
from modules import navigator_sys_2
gv.displayer.LCD_SYS = HD44780_sys_2.LCD_SYS_2()
gv.nav = navigator_sys_2
gv.midimaps = midimaps.MidiMapping().maps
gv.autochorder = audiocontrols.AutoChorder()
gv.ac = audiocontrols.AudioControls()
gv.sound = sound.StartSound()
gv.sysfunc = systemfunctions.SystemFunctions()
gv.ls = loadsamples.LoadingSamples()
bnt = buttons.Buttons()
gv.midicallback = midicallback.Midi()
gv.midiserial = midiserial.MIDISerial(midicallback=gv.midicallback)
import modules.gui as gui
###########################################
# START GUI #
# If running on Windows/Mac. Experimental #
###########################################
if gv.USE_GUI and not gv.IS_DEBIAN: gv.gui = gui.SamplerBoxGUI() # Start the GUI
################################
# LOAD FIRST SAMPLE-SET/PRESET #
################################
gv.ls.load_preset()
################################################################
# MIDI IN via SERIAL PORT #
# this should be extended with logic for "midi running status" #
# possible solution at http://www.samplerbox.org/forum/146 #
################################################################
gv.midiserial.start()
#################################
# Test initial script load time #
#################################
time_end = time.time()
time_total = float(time_end - time_start)
print '\r\nINIT LOAD TIME: %d seconds (before sample loading)\r\n' % time_total
##########################
# MIDI DEVICES DETECTION #
# MAIN LOOP #
##########################
midi_in = rtmidi2.MidiInMulti()
curr_ports = []
prev_ports = []
first_loop = True
time.sleep(0.5)
try:
def midi_devices_loop():
global prev_ports, first_loop
while True:
no_playing_sounds = False
for channel in xrange(16):
if not gv.playingnotes[channel + 1]:
no_playing_sounds = True
if no_playing_sounds: # only check when there are no sounds
curr_ports = rtmidi2.get_in_ports()
if (len(prev_ports) != len(curr_ports)):
print '\n==== START GETTING MIDI DEVICES ===='
midi_in.close_ports()
prev_ports = []
for port in curr_ports:
if port not in prev_ports and 'Midi Through' not in port and (
len(prev_ports) != len(curr_ports) and 'LoopBe Internal' not in port):
midi_in.open_ports(port)
midi_in.callback = gv.midicallback.callback
if first_loop:
print 'Opened MIDI port: ' + port
else:
print 'Reopening MIDI port: ' + port
print '==== END GETTING MIDI DEVICES ====\n'
prev_ports = curr_ports
first_loop = False
time.sleep(0.2)
if gv.USE_GUI and not gv.IS_DEBIAN:
# MIDI device detection is threaded because Tkinter's loop will become the main loop (below)
LoadingInterrupt = False
LoadingThread = threading.Thread(target=midi_devices_loop)
LoadingThread.daemon = True
LoadingThread.start()
#########################
# START GUI / MAIN LOOP #
#########################
if not gv.IS_DEBIAN:
gv.gui.start_gui_loop() # this is the main loop
else:
midi_devices_loop() # this is the main loop
except KeyboardInterrupt:
print "\nStopped by CTRL-C\n"
gv.sysfunc.shutdown(log_file)
exit()
except:
print "\nStopped by other error\n"
gv.sysfunc.shutdown(log_file)
exit()
# TODO: returns fatal errors to the LCD screen. `try:` needs to encapsulate whole script. Buggy.
# except:
# exc_info = sys.exc_info()
# # print exc_info
# traceback_str = '%s %s' % (str(exc_info[0]), str(exc_info[1]))
# print traceback_str
# gv.displayer.disp_change('FATAL ERROR'.center(gv.LCD_COLS, ' '), line=1, is_error=True)
# gv.nav.text_scroller.set_string(string=traceback_str, line=2, is_error=True)
# while True:
# time.sleep(1)