forked from dhilowitz/SamplerBox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
samplerbox.py
611 lines (501 loc) · 19.6 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
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
#
# SamplerBox
#
# author: Joseph Ernest (twitter: @JosephErnest, mail: [email protected])
# url: http://www.samplerbox.org/
# license: Creative Commons ShareAlike 3.0 (http://creativecommons.org/licenses/by-sa/3.0/)
#
# samplerbox.py: Main file
#
#########################################
# LOCAL
# CONFIG
#########################################
AUDIO_DEVICE_ID = 1 # change this number to use another soundcard
SAMPLES_DIR = "." # The root directory containing the sample-sets. Example: "/media/" to look for samples on a USB stick / SD card
USE_SERIALPORT_MIDI = False # Set to True to enable MIDI IN via SerialPort (e.g. RaspberryPi's GPIO UART pins)
USE_I2C_7SEGMENTDISPLAY = False # Set to True to use a 7-segment display via I2C
USE_BUTTONS = False # Set to True to use momentary buttons (connected to RaspberryPi's GPIO pins) to change preset
MAX_POLYPHONY = 80 # This can be set higher, but 80 is a safe value
USE_I2C_16X2DISPLAY = False # Set to True to use a 16x2 display via I2C
# Define some device parameters
I2C_16x2DISPLAY_ADDR = 0x3f # I2C device address
I2C_16x2DISPLAY_LCD_WIDTH = 16 # Maximum characters per line
#########################################
# IMPORT
# MODULES
#########################################
import wave
import time
import numpy
import os
import re
import sounddevice
import threading
from chunk import Chunk
import struct
import rtmidi_python as rtmidi
import samplerbox_audio
import random
#########################################
# SLIGHT MODIFICATION OF PYTHON'S WAVE MODULE
# TO READ CUE MARKERS & LOOP MARKERS
#########################################
class waveread(wave.Wave_read):
def initfp(self, file):
self._convert = None
self._soundpos = 0
self._cue = []
self._loops = []
self._ieee = False
self._file = Chunk(file, bigendian=0)
if self._file.getname() != 'RIFF':
raise Error, 'file does not start with RIFF id'
if self._file.read(4) != 'WAVE':
raise Error, 'not a WAVE file'
self._fmt_chunk_read = 0
self._data_chunk = None
while 1:
self._data_seek_needed = 1
try:
chunk = Chunk(self._file, bigendian=0)
except EOFError:
break
chunkname = chunk.getname()
if chunkname == 'fmt ':
self._read_fmt_chunk(chunk)
self._fmt_chunk_read = 1
elif chunkname == 'data':
if not self._fmt_chunk_read:
raise Error, 'data chunk before fmt chunk'
self._data_chunk = chunk
self._nframes = chunk.chunksize // self._framesize
self._data_seek_needed = 0
elif chunkname == 'cue ':
numcue = struct.unpack('<i', chunk.read(4))[0]
for i in range(numcue):
id, position, datachunkid, chunkstart, blockstart, sampleoffset = struct.unpack('<iiiiii', chunk.read(24))
self._cue.append(sampleoffset)
elif chunkname == 'smpl':
manuf, prod, sampleperiod, midiunitynote, midipitchfraction, smptefmt, smpteoffs, numsampleloops, samplerdata = struct.unpack(
'<iiiiiiiii', chunk.read(36))
for i in range(numsampleloops):
cuepointid, type, start, end, fraction, playcount = struct.unpack('<iiiiii', chunk.read(24))
self._loops.append([start, end])
chunk.skip()
if not self._fmt_chunk_read or not self._data_chunk:
raise Error, 'fmt chunk and/or data chunk missing'
def getmarkers(self):
return self._cue
def getloops(self):
return self._loops
#########################################
# MIXER CLASSES
#
#########################################
class PlayingSound:
def __init__(self, sound, note):
self.sound = sound
self.pos = 0
self.fadeoutpos = 0
self.isfadeout = False
self.note = note
def fadeout(self, i):
self.isfadeout = True
def stop(self):
try:
playingsounds.remove(self)
except:
pass
class Sound:
def __init__(self, filename, midinote, velocity, seq):
wf = waveread(filename)
self.fname = filename
self.midinote = midinote
self.velocity = velocity
self.seq = seq
if wf.getloops():
self.loop = wf.getloops()[0][0]
self.nframes = wf.getloops()[0][1] + 2
else:
self.loop = -1
self.nframes = wf.getnframes()
self.data = self.frames2array(wf.readframes(self.nframes), wf.getsampwidth(), wf.getnchannels())
wf.close()
def play(self, note):
snd = PlayingSound(self, note)
playingsounds.append(snd)
return snd
def frames2array(self, data, sampwidth, numchan):
if sampwidth == 2:
npdata = numpy.fromstring(data, dtype=numpy.int16)
elif sampwidth == 3:
npdata = samplerbox_audio.binary24_to_int16(data, len(data)/3)
if numchan == 1:
npdata = numpy.repeat(npdata, 2)
return npdata
FADEOUTLENGTH = 30000
FADEOUT = numpy.linspace(1., 0., FADEOUTLENGTH) # by default, float64
FADEOUT = numpy.power(FADEOUT, 6)
FADEOUT = numpy.append(FADEOUT, numpy.zeros(FADEOUTLENGTH, numpy.float32)).astype(numpy.float32)
SPEED = numpy.power(2, numpy.arange(0.0, 84.0)/12).astype(numpy.float32)
samples = {}
playingnotes = {}
lastplayedseq = {}
sustainplayingnotes = []
sustain = False
playingsounds = []
globalvolume = 10 ** (-12.0/20) # -12dB default global volume
globaltranspose = 0
#########################################
# AUDIO AND MIDI CALLBACKS
#
#########################################
def AudioCallback(outdata, frame_count, time_info, status):
global playingsounds
rmlist = []
playingsounds = playingsounds[-MAX_POLYPHONY:]
b = samplerbox_audio.mixaudiobuffers(playingsounds, rmlist, frame_count, FADEOUT, FADEOUTLENGTH, SPEED)
for e in rmlist:
try:
playingsounds.remove(e)
except:
pass
b *= globalvolume
outdata[:] = b.reshape(outdata.shape)
def MidiCallback(message, time_stamp):
global playingnotes, sustain, sustainplayingnotes, lastplayedseq
global preset
messagetype = message[0] >> 4
messagechannel = (message[0] & 15) + 1
note = message[1] if len(message) > 1 else None
midinote = note
velocity = message[2] if len(message) > 2 else None
if messagetype == 9 and velocity == 0:
messagetype = 8
if messagetype == 9: # Note on
midinote += globaltranspose
try:
# Get the list of available samples for this note and velocity
notesamples = samples[midinote, velocity]
# Choose a sample from the list
sample = random.choice (notesamples)
# If we have no value for lastplayedseq, set it to 0
lastplayedseq.setdefault(midinote, 0)
# If we have more than 2 samples to work with, reject duplicates
if len(notesamples) >= 3:
while sample.seq == lastplayedseq[midinote]:
sample = random.choice (notesamples)
# print "About to play midinote: %s, seq: %s" % (midinote, sample.seq)
playingnotes.setdefault(midinote, []).append(sample.play(midinote))
# Recorded the last played note
lastplayedseq[midinote] = sample.seq
except:
pass
elif messagetype == 8: # Note off
midinote += globaltranspose
if midinote in playingnotes:
for n in playingnotes[midinote]:
if sustain:
sustainplayingnotes.append(n)
else:
n.fadeout(50)
playingnotes[midinote] = []
elif messagetype == 12: # Program change
print 'Program change ' + str(note)
preset = note
LoadSamples()
elif (messagetype == 11) and (note == 64) and (velocity < 64): # sustain pedal off
for n in sustainplayingnotes:
n.fadeout(50)
sustainplayingnotes = []
sustain = False
elif (messagetype == 11) and (note == 64) and (velocity >= 64): # sustain pedal on
sustain = True
#########################################
# LOAD SAMPLES
#
#########################################
LoadingThread = None
LoadingInterrupt = False
def LoadSamples():
global LoadingThread
global LoadingInterrupt
if LoadingThread:
LoadingInterrupt = True
LoadingThread.join()
LoadingThread = None
LoadingInterrupt = False
LoadingThread = threading.Thread(target=ActuallyLoad)
LoadingThread.daemon = True
LoadingThread.start()
NOTES = ["c", "c#", "d", "d#", "e", "f", "f#", "g", "g#", "a", "a#", "b"]
def ActuallyLoad():
global preset
global samples
global playingsounds
global globalvolume, globaltranspose
playingsounds = []
samples = {}
globalvolume = 10 ** (-12.0/20) # -12dB default global volume
globaltranspose = 0
samplesdir = SAMPLES_DIR if os.listdir(SAMPLES_DIR) else '.' # use current folder (containing 0 Saw) if no user media containing samples has been found
basename = next((f for f in os.listdir(samplesdir) if f.startswith("%d " % preset)), None) # or next(glob.iglob("blah*"), None)
if basename:
dirname = os.path.join(samplesdir, basename)
if not basename:
print 'Preset empty: %s' % preset
display("E%03d" % preset)
lcd_string('%s Preset Empty' % preset, 1)
return
print 'Preset loading: %s (%s)' % (preset, basename)
display("L%03d" % preset)
lcd_string('%s' % (basename), 1)
lcd_string('Loading...', 2)
definitionfname = os.path.join(dirname, "definition.txt")
if os.path.isfile(definitionfname):
with open(definitionfname, 'r') as definitionfile:
for i, pattern in enumerate(definitionfile):
try:
if r'%%volume' in pattern: # %%paramaters are global parameters
globalvolume *= 10 ** (float(pattern.split('=')[1].strip()) / 20)
continue
if r'%%transpose' in pattern:
globaltranspose = int(pattern.split('=')[1].strip())
continue
defaultparams = {'midinote': '0', 'velocity': '127', 'notename': '', 'seq': 1}
if len(pattern.split(',')) > 1:
defaultparams.update(dict([item.split('=') for item in pattern.split(',', 1)[1].replace(' ', '').replace('%', '').split(',')]))
pattern = pattern.split(',')[0]
pattern = re.escape(pattern.strip())
pattern = pattern.replace(r"\%midinote", r"(?P<midinote>\d+)").replace(r"\%velocity", r"(?P<velocity>\d+)")\
.replace(r"\%seq", r"(?P<seq>\d+)")\
.replace(r"\%notename", r"(?P<notename>[A-Ga-g]#?[0-9])").replace(r"\*", r".*?").strip() # .*? => non greedy
for fname in os.listdir(dirname):
if LoadingInterrupt:
return
m = re.match(pattern, fname)
if m:
info = m.groupdict()
midinote = int(info.get('midinote', defaultparams['midinote']))
velocity = int(info.get('velocity', defaultparams['velocity']))
seq = int(info.get('seq', defaultparams['seq']))
notename = info.get('notename', defaultparams['notename'])
if notename:
midinote = NOTES.index(notename[:-1].lower()) + (int(notename[-1])+2) * 12
# print "Loaded note %s, velocity %s, seq %s." % (midinote, velocity, seq)
if (midinote, velocity) in samples:
samples[midinote, velocity].append(Sound(os.path.join(dirname, fname), midinote, velocity, seq))
else:
samples[midinote, velocity] = [Sound(os.path.join(dirname, fname), midinote, velocity, seq)]
except:
print "Error in definition file, skipping line %s." % (i+1)
else:
for midinote in range(0, 127):
if LoadingInterrupt:
return
file = os.path.join(dirname, "%d.wav" % midinote)
if os.path.isfile(file):
samples[midinote, 127] = [ Sound(file, midinote, 127, 1) ]
initial_keys = set(samples.keys())
for midinote in xrange(128):
lastvelocity = None
for velocity in xrange(128):
if (midinote, velocity) not in initial_keys:
samples[midinote, velocity] = lastvelocity
else:
if not lastvelocity:
for v in xrange(velocity):
samples[midinote, v] = samples[midinote, velocity]
lastvelocity = samples[midinote, velocity]
if not lastvelocity:
for velocity in xrange(128):
try:
samples[midinote, velocity] = samples[midinote-1, velocity]
except:
pass
if len(initial_keys) > 0:
print 'Preset loaded: ' + str(preset)
display("%04d" % preset)
lcd_string('%s' % (basename), 1)
lcd_string('', 2)
else:
print 'Preset empty: ' + str(preset)
display("E%03d" % preset)
lcd_string('%s Preset Empty' % (preset), 1)
#########################################
# OPEN AUDIO DEVICE
#
#########################################
try:
sd = sounddevice.OutputStream(device=AUDIO_DEVICE_ID, blocksize=512, samplerate=44100, channels=2, dtype='int16', callback=AudioCallback)
sd.start()
print 'Opened audio device #%i' % AUDIO_DEVICE_ID
except:
print 'Invalid audio device #%i' % AUDIO_DEVICE_ID
exit(1)
#########################################
# BUTTONS THREAD (RASPBERRY PI GPIO)
#
#########################################
if USE_BUTTONS:
import RPi.GPIO as GPIO
lastbuttontime = 0
def Buttons():
GPIO.setmode(GPIO.BCM)
GPIO.setup(18, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(17, GPIO.IN, pull_up_down=GPIO.PUD_UP)
global preset, lastbuttontime
while True:
now = time.time()
if not GPIO.input(18) and (now - lastbuttontime) > 0.2:
lastbuttontime = now
preset -= 1
if preset < 0:
preset = 127
LoadSamples()
elif not GPIO.input(17) and (now - lastbuttontime) > 0.2:
lastbuttontime = now
preset += 1
if preset > 127:
preset = 0
LoadSamples()
time.sleep(0.020)
ButtonsThread = threading.Thread(target=Buttons)
ButtonsThread.daemon = True
ButtonsThread.start()
#########################################
# 7-SEGMENT DISPLAY
#
#########################################
if USE_I2C_7SEGMENTDISPLAY:
import smbus
bus = smbus.SMBus(1) # using I2C
def display(s):
for k in '\x76\x79\x00' + s: # position cursor at 0
try:
bus.write_byte(0x71, ord(k))
except:
try:
bus.write_byte(0x71, ord(k))
except:
pass
time.sleep(0.002)
def lcd_string(s, line):
pass
display('----')
time.sleep(0.5)
elif USE_I2C_16X2DISPLAY:
import smbus
# Define some device constants
LCD_CHR = 1 # Mode - Sending data
LCD_CMD = 0 # Mode - Sending command
LCD_LINE_1 = 0x80 # LCD RAM address for the 1st line
LCD_LINE_2 = 0xC0 # LCD RAM address for the 2nd line
LCD_LINE_3 = 0x94 # LCD RAM address for the 3rd line
LCD_LINE_4 = 0xD4 # LCD RAM address for the 4th line
LCD_BACKLIGHT = 0x08 # On
#LCD_BACKLIGHT = 0x00 # Off
ENABLE = 0b00000100 # Enable bit
# Timing constants
E_PULSE = 0.0005
E_DELAY = 0.0005
bus = smbus.SMBus(1) # using I2C
def lcd_init():
# Initialise display
lcd_byte(0x33,LCD_CMD) # 110011 Initialise
lcd_byte(0x32,LCD_CMD) # 110010 Initialise
lcd_byte(0x06,LCD_CMD) # 000110 Cursor move direction
lcd_byte(0x0C,LCD_CMD) # 001100 Display On,Cursor Off, Blink Off
lcd_byte(0x28,LCD_CMD) # 101000 Data length, number of lines, font size
lcd_byte(0x01,LCD_CMD) # 000001 Clear display
time.sleep(E_DELAY)
def lcd_byte(bits, mode):
# Send byte to data pins
# bits = the data
# mode = 1 for data
# 0 for command
bits_high = mode | (bits & 0xF0) | LCD_BACKLIGHT
bits_low = mode | ((bits<<4) & 0xF0) | LCD_BACKLIGHT
# High bits
bus.write_byte(I2C_16x2DISPLAY_ADDR, bits_high)
lcd_toggle_enable(bits_high)
# Low bits
bus.write_byte(I2C_16x2DISPLAY_ADDR, bits_low)
lcd_toggle_enable(bits_low)
def lcd_toggle_enable(bits):
# Toggle enable
time.sleep(E_DELAY)
bus.write_byte(I2C_16x2DISPLAY_ADDR, (bits | ENABLE))
time.sleep(E_PULSE)
bus.write_byte(I2C_16x2DISPLAY_ADDR,(bits & ~ENABLE))
time.sleep(E_DELAY)
def lcd_string(message,line):
if line == 1:
line_address = LCD_LINE_1
elif line == 2:
line_address = LCD_LINE_2
elif line == 3:
line_address = LCD_LINE_3
elif line == 4:
line_address = LCD_LINE_4
# Send string to display
message = message.ljust(I2C_16x2DISPLAY_LCD_WIDTH," ")
lcd_byte(line_address, LCD_CMD)
for i in range(I2C_16x2DISPLAY_LCD_WIDTH):
lcd_byte(ord(message[i]),LCD_CHR)
def display(s):
pass
lcd_init()
display('----')
time.sleep(0.5)
else:
def display(s):
pass
def lcd_string(s, line):
pass
#########################################
# MIDI IN via SERIAL PORT
#
#########################################
if USE_SERIALPORT_MIDI:
import serial
ser = serial.Serial('/dev/ttyAMA0', baudrate=38400) # see hack in /boot/cmline.txt : 38400 is 31250 baud for MIDI!
def MidiSerialCallback():
message = [0, 0, 0]
while True:
i = 0
while i < 3:
data = ord(ser.read(1)) # read a byte
if data >> 7 != 0:
i = 0 # status byte! this is the beginning of a midi message: http://www.midi.org/techspecs/midimessages.php
message[i] = data
i += 1
if i == 2 and message[0] >> 4 == 12: # program change: don't wait for a third byte: it has only 2 bytes
message[2] = 0
i = 3
MidiCallback(message, None)
MidiThread = threading.Thread(target=MidiSerialCallback)
MidiThread.daemon = True
MidiThread.start()
#########################################
# LOAD FIRST SOUNDBANK
#
#########################################
preset = 0
LoadSamples()
#########################################
# MIDI DEVICES DETECTION
# MAIN LOOP
#########################################
midi_in = [rtmidi.MidiIn()]
previous = []
while True:
for port in midi_in[0].ports:
if port not in previous and 'Midi Through' not in port:
midi_in.append(rtmidi.MidiIn())
midi_in[-1].callback = MidiCallback
midi_in[-1].open_port(port)
print 'Opened MIDI: ' + port
previous = midi_in[0].ports
time.sleep(2)