-
Notifications
You must be signed in to change notification settings - Fork 1
/
sfzCreatorClass.py
543 lines (467 loc) · 24 KB
/
sfzCreatorClass.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Nov 17 23:33:36 2020
@author: luiz
"""
import copy
import music21 as mus # install this. With pip: pip install music21 / with anaconda: conda install -c iainsgillis music21
import re # regexp
from os import listdir
from os.path import isfile, join, split, relpath, dirname
import numpy as np
from audio_db import getPeakAmpDB
class sample:
# opcodes:
Pkeycenter = None
Lokey = None
Hikey = None
Lovel = None
Hivel = None
Ampeg_start = None
Ampeg_attack = None # https://sfzformat.com/opcodes/ampeg_attack/
Ampeg_release = None
Ampeg_hold = None
Ampeg_sustain = None
Ampeg_decay = None
Amp_veltrack = None
Cutoff = None
Fil_veltrack = None
Volume = None
Hirand = None
Lorand = None
Trigger = None # when to trigger the sample
Offset = None # number of samples to skip at the beginning (start to play at)
Locc64 = None # sustain pedal
Hicc64 = None # sustain pedal
# non-opcodes:
Fname = None
FullFilePath = None
Vel = None
# grouping
Group = None
# methods:
def get_pkeycenter(self):
return self.Pkeycenter
def get_vel(self):
return self.Vel
# def peakAmpDB(wave_file_path):
# import wave
# import struct
# # Open the audio file
# with wave.open(wave_file_path, 'r') as audio:
# # Extract the raw audio data
# raw_data = audio.readframes(audio.getnframes())
# # Convert the raw audio data to a list of integers
# samples = struct.unpack('{n}h'.format(n=audio.getnframes()), raw_data)
# # Find the peak sample
# peak = max(samples)
# # Calculate the reference value based on the bit depth of the audio file
# reference_value = 2**(audio.getsampwidth() * 8 - 1)
# # Calculate the peak value in dBFS, using the maximum possible sample value as the reference value
# peak_dB = 20 * np.log10(peak / reference_value)
# print(peak_dB)
def doBinaryOperation(val1, val2, binaryOp):
if binaryOp == '+':
retVal = val1 + val2
elif binaryOp == '*':
retVal = val1 * val2
return retVal
class sfz_creator:
Samples = None # list of dictionaries with sample name, file name and properties
SfzStrL = [] # list of strings, one per line
OutFile = None # output file path
VelMap = None # dict mapping velocities
PkcList = []
def __init__(self, samples, outFile):
self.Samples = samples
self.OutFile = outFile
def __add__(self, other): # merging two sfz file objects
self.Samples = self.Samples + other.Samples
self.SfzStrL = self.SfzStrL + other.SfzStrL
self.VelMap = self.VelMap | other.VelMap
self.PkcList = self.PkcList + other.PkcList
return self
def genSfz(self): # generates sfz file from property "samples" (list of samples)
self.SfzStrL = [] # clear list
self.writeHeader()
for sample in self.Samples:
self.writeSampleBlock(sample)
with open(self.OutFile, 'w') as ofile:
ofile.write('\n'.join(self.SfzStrL))
print('SFZ file successfully generated')
def writeHeader(self):
self.SfzStrL.append('// Header lines')
self.SfzStrL.append('') # empty line
def writeSampleBlock(self, sample): # writes a region from a sample
self.SfzStrL.append('<region>')
self.SfzStrL.append('sample=' + sample.Fname)
self.SfzStrL.append('pitch_keycenter=' + str(sample.Pkeycenter))
self.SfzStrL.append('lokey=' + str(sample.Lokey))
self.SfzStrL.append('hikey=' + str(sample.Hikey))
if sample.Lovel != None:
self.SfzStrL.append('lovel=' + str(sample.Lovel))
if sample.Hivel != None:
self.SfzStrL.append('hivel=' + str(sample.Hivel))
if sample.Ampeg_start != None:
self.SfzStrL.append('ampeg_start=' + str(sample.Ampeg_start))
if sample.Ampeg_attack != None:
self.SfzStrL.append('ampeg_attack=' + str(sample.Ampeg_attack))
if sample.Ampeg_release != None:
self.SfzStrL.append('ampeg_release=' + str(sample.Ampeg_release))
if sample.Ampeg_hold != None:
self.SfzStrL.append('ampeg_hold=' + str(sample.Ampeg_hold))
if sample.Ampeg_sustain != None:
self.SfzStrL.append('ampeg_sustain=' + str(sample.Ampeg_sustain))
if sample.Ampeg_decay != None:
self.SfzStrL.append('ampeg_decay=' + str(sample.Ampeg_decay))
if sample.Amp_veltrack != None:
self.SfzStrL.append('amp_veltrack=' + str(sample.Amp_veltrack))
if sample.Cutoff != None:
self.SfzStrL.append('cutoff=' + str(sample.Cutoff))
if sample.Fil_veltrack != None:
self.SfzStrL.append('fil_veltrack=' + str(sample.Fil_veltrack))
if sample.Volume != None:
self.SfzStrL.append('volume=' + str(sample.Volume))
if sample.Hirand != None:
self.SfzStrL.append('hirand=' + str(sample.Hirand))
if sample.Lorand != None:
self.SfzStrL.append('lorand=' + str(sample.Lorand))
if sample.Trigger != None:
self.SfzStrL.append('trigger=' + str(sample.Trigger))
if sample.Offset != None:
self.SfzStrL.append('offset=' + str(sample.Offset))
if sample.Locc64 != None:
self.SfzStrL.append('locc64=' + str(sample.Locc64))
if sample.Hicc64 != None:
self.SfzStrL.append('hicc64=' + str(sample.Hicc64))
self.SfzStrL.append('') # empty line
def getSamplesFromFolder(self, folderPath, **kwargs):
"""
Parameters
----------
folderPath : string
This should be the directory path in which the samples are to be
found.
**kwargs : key-value pairs
Each key is a sample property that shall be checked in the file
names. The corresponding value is a regexp template to find the
property. Examples:
pmidi='C(\d+)' # will look for a midi pitch that matches this
template.
pitch='_([A-G]#?\d)_' # will look for pitches like
A#4 matching this.
vel='v(\d+)' # will look for velocities in a sequence (1,2,3)
velval = 'v(\d+)' # will look for velocity values
(e.g. 64, 90, 120)
Returns
-------
None.
"""
samFiles = listdir(folderPath)
for samFile in samFiles:
if samFile[-4:] != '.wav':
continue # skip non wave files
sam = sample()
if self.OutFile == None:
sam.Fname = join(folderPath, samFile)
else:
sam.Fname = join(relpath(folderPath, dirname(self.OutFile)), samFile)
sam.FullFilePath = join(folderPath, samFile)
# create a pitch object:
p = mus.pitch.Pitch()
for key, value in kwargs.items(): # value is a regex template and key is a property
if key == 'pitch':
pitchNameWithOctave = re.findall(value, samFile)
if len(pitchNameWithOctave) == 0:
print('Pitch not found for: ' + samFile)
elif len(pitchNameWithOctave) > 1:
print('More than one pitch match for: ' + samFile)
else:
p.nameWithOctave = pitchNameWithOctave[0]
sam.Pkeycenter = p.midi # assign midi pitch value to property
if key == 'pmidi':
pitchMidiValue = re.findall(value, samFile)
if len(pitchMidiValue) == 0:
print('Pitch not found for: ' + samFile)
elif len(pitchMidiValue) > 1:
print('More than one pitch match for: ' + samFile)
else:
p.midi = int(pitchMidiValue[0])
sam.Pkeycenter = p.midi # assign midi pitch value to property
if key == 'velval':
vel = re.findall(value, samFile)
if len(vel) == 0:
print('Velocity not found for: ' + samFile)
elif len(vel) > 1:
print('More than one velocity match for: ' + samFile)
else:
sam.Vel = int(vel[0]) # assign note velocity to property
if key == 'vel':
vel = re.findall(value, samFile)
if len(vel) == 0:
print('Velocity not found for: ' + samFile)
elif len(vel) > 1:
print('More than one velocity match for: ' + samFile)
else:
sam.Vel = self.VelMap[vel[0]] # assign mapped note velocity to property
if key == 'group':
sam.Group = value
self.Samples.append(sam)
self.PkcList.append(p.midi)
def autoSpreadKeys(self, spreadDirection, group = None): # lower keys, closest keys, higher keys
self.Samples.sort(key=sample.get_pkeycenter) # sorting samples according to pitch keycenter
self.PkcList.sort() # pitch keycenter list
for iSam in range(len(self.Samples)): # run through list
currP = self.Samples[iSam].Pkeycenter # currP = current pitch
if group == None or group == self.Samples[iSam].Group:
if spreadDirection == 'lower':
if self.PkcList.index(currP) == 0: # if it's the lowest pitch keycentre
self.Samples[iSam].Lokey = 0
self.Samples[iSam].Hikey = currP
else:
self.Samples[iSam].Lokey = self.PkcList[self.PkcList.index(currP)-1] + 1 # find first occurrence and go back one position. Add 1 to start next region without superposition.
self.Samples[iSam].Hikey = currP
elif spreadDirection == 'higher':
if self.PkcList[::-1].index(currP) == 0:
self.Samples[iSam].Lokey = currP
self.Samples[iSam].Hikey = 127
else:
self.Samples[iSam].Lokey = currP
self.Samples[iSam].Hikey = self.PkcList[::-1][self.PkcList[::-1].index(currP)-1] - 1 # find last occurrence and go back one position (reversed list). Subtract 1 to finish before starting next region, without superposition.
elif spreadDirection == 'closest':
raise NotImplementedError('To be implemented.')
def autoSpreadVelocities(self, spreadDirection, group = None): # lower vel, closest vel, higher vel
self.Samples.sort(key=sample.get_vel)
self.Samples.sort(key=sample.get_pkeycenter) # sorting samples according to pitch keycenter
self.PkcList.sort() # pitch keycenter list
pkc_array = np.array(self.PkcList)
for iSam, currSam in enumerate(self.Samples): # run through list
if group == None or group == currSam.Group:
currP = self.Samples[iSam].Pkeycenter # currP = current pitch
sameP = np.where(pkc_array == currP)
samples_same_p = np.array(self.Samples)[sameP]
vels = [x.Vel for x in samples_same_p]
vels.sort()
vels.insert(0, 0)
# samples_same_p.tolist.sort(key=sample.Vel)
try:
currIdx = samples_same_p.tolist().index(currSam)
except ValueError as e:
print(f'Current sample keycentre: {currSam.Pkeycenter}')
print(f'Same pitch at: {sameP}')
print(f'Pitch keycentre array: {pkc_array}')
print(f'length of pitch keycentre list: {len(self.PkcList)}')
raise(e)
if spreadDirection == 'lower':
self.Samples[iSam].Lovel = vels[currIdx] + 1
self.Samples[iSam].Hivel = vels[currIdx+1]
elif spreadDirection == 'higher':
raise NotImplementedError('To be implemented.')
elif spreadDirection == 'closest':
raise NotImplementedError('To be implemented.')
# TODO: correct this, which should do for the group separately:
# def autoSpreadVelocities(self, spreadDirection, group = None): # lower vel, closest vel, higher vel
# self.Samples.sort(key=sample.get_pkeycenter) # sorting samples according to pitch keycenter
# self.PkcList.sort() # pitch keycenter list
# # because we want to do only for the group, let us separate the group
# # first. If group is None, it will be the same as operating the whole
# # list of samples.
# sampleListGroup = [] # subset of samples of the given group
# PkcListGroup = [] # subset of pitch keycentres of the given group
# for idx, currSam in enumerate(self.Samples): # run through list
# if group is None or group == currSam.Group: # copy all samples from group to this subset
# sampleListGroup.append(self.Samples[idx])
# PkcListGroup.append(self.PkcList[idx])
# sampleListGroup.sort(key=sample.get_vel)
# sampleListGroup.sort(key=sample.get_pkeycenter) # sorting samples according to pitch keycenter
# PkcListGroup.sort() # pitch keycenter list
# pkc_array = np.array(PkcListGroup)
# for idxG, currSamG in enumerate(sampleListGroup): # run through list
# currP = sampleListGroup[idxG].Pkeycenter # currP = current pitch
# sameP = np.where(pkc_array == currP)
# samples_same_p = np.array(sampleListGroup)[sameP] # same pitch
# vels = [x.Vel for x in samples_same_p]
# vels.sort()
# vels.insert(0, 0)
# # samples_same_p.tolist.sort(key=sample.Vel)
# currIdx = samples_same_p.tolist().index(currSamG)
# if spreadDirection == 'lower':
# sampleListGroup[idxG].Lovel = vels[currIdx] + 1
# sampleListGroup[idxG].Hivel = vels[currIdx+1]
# elif spreadDirection == 'higher':
# raise NotImplementedError('To be implemented.')
# elif spreadDirection == 'closest':
# raise NotImplementedError('To be implemented.')
# # todo - better approach: run thorough all pitches (0 to 127) and for every sub-region, do an independent spread. Connect them!
# # now going from the group list back to the global list self.Samples
# for idx in range(len(self.Samples)-1, -1, -1): # run it backwards because we will pop elements out
# if self.Samples[idx].Group == group:
# self.Samples.pop(idx) # delete all samples of this group
# self.PkcList.pop(idx) # delete equally here
# self.Samples = self.Samples + sampleListGroup # insert them back, but now with velocities spreaded in the group
# self.PkcList = self.PkcList + PkcListGroup # insert back here too
def setForAll(self, attribute: str, val, group = None):
"""
Parameters
----------
attribute : str
attribute to set for all samples. E.g. 'Ampeg_release'
val : int OR float
value to set for attribute in all samples.
Returns
-------
None.
"""
for iSam in range(len(self.Samples)):
if group == None or group == self.Samples[iSam].Group:
setattr(self.Samples[iSam], attribute, val)
def setForAllIf(self, attribute: str, val, comparison_attr: str, comparison_sign: str, comparison_value, group = None):
"""
Parameters
----------
attribute : str
attribute to set for all samples that matches the condition.
E.g. 'Cutoff'.
val : TYPE
Value to set for attribute in all samples that matches the
condition.
comparison_attr : str
Attribute whose value is to be checked.
comparison_sign : str
'==', '!=', '<', '>', '<=' or '>='.
comparison_value : TYPE
Value to check in the comparison attribute.
Raises
------
AttributeError
If comparison_sign is not valid.
Returns
-------
None.
"""
# Todo: make it more flexible with any possible set of conditions. Like [if a < b and c != d or e == f] -> this will need an interpreter for this. eval, exec?
if comparison_sign == '==':
for iSam in range(len(self.Samples)):
if group == None or group == self.Samples[iSam].Group:
if getattr(self.Samples[iSam], comparison_attr) == comparison_value:
setattr(self.Samples[iSam], attribute, val)
elif comparison_sign == '!=':
for iSam in range(len(self.Samples)):
if group == None or group == self.Samples[iSam].Group:
if getattr(self.Samples[iSam], comparison_attr) != comparison_value:
setattr(self.Samples[iSam], attribute, val)
elif comparison_sign == '>=':
for iSam in range(len(self.Samples)):
if group == None or group == self.Samples[iSam].Group:
if getattr(self.Samples[iSam], comparison_attr) >= comparison_value:
setattr(self.Samples[iSam], attribute, val)
elif comparison_sign == '<=':
for iSam in range(len(self.Samples)):
if group == None or group == self.Samples[iSam].Group:
if getattr(self.Samples[iSam], comparison_attr) <= comparison_value:
setattr(self.Samples[iSam], attribute, val)
elif comparison_sign == '>':
for iSam in range(len(self.Samples)):
if group == None or group == self.Samples[iSam].Group:
if getattr(self.Samples[iSam], comparison_attr) > comparison_value:
setattr(self.Samples[iSam], attribute, val)
elif comparison_sign == '<':
for iSam in range(len(self.Samples)):
if group == None or group == self.Samples[iSam].Group:
if getattr(self.Samples[iSam], comparison_attr) < comparison_value:
setattr(self.Samples[iSam], attribute, val)
else:
raise AttributeError('Invalid comparison sign: ' + comparison_sign)
def setForAllRegexpFileName(self, attribute: str, val, re_template: str, group = None):
for idx, sample in enumerate(self.Samples):
match = re.findall(re_template, split(sample.Fname)[1]) # compare file name only, not path
if len(match) > 0:
if group == None or group == sample.Group:
setattr(self.Samples[idx], attribute, val)
def operateOnAll(self, attribute: str, val, binaryOp: str, group = None):
"""
Operate on all samples. If a group is given, it will restrict to that
group. Usage:
sfz.operateOnAllOpcodes("Volume", 2, "+") will increase the volume
of all samples by 2.
Parameters
----------
attribute : str
attribute to modified for all samples. E.g. 'Ampeg_release'
val : int OR float
value to operate with attribute in all samples.
binaryOp: str
Operator to use between current value of the attribute and val,
resulting in the new value for the attribute.
Returns
-------
None.
"""
for iSam in range(len(self.Samples)):
if group == None or group == self.Samples[iSam].Group:
currVal = getattr(self.Samples[iSam], attribute)
newVal = doBinaryOperation(currVal, val, binaryOp)
setattr(self.Samples[iSam], attribute, newVal)
# TODO: operateOnAllIf
def operateOnAllRegexpFileName(self, attribute: str, val, binaryOp: str, re_template: str, group = None):
for idx, sample in enumerate(self.Samples):
match = re.findall(re_template, split(sample.Fname)[1]) # compare file name only, not path
if len(match) > 0:
if group == None or group == sample.Group:
currVal = getattr(self.Samples[idx], attribute)
newVal = doBinaryOperation(currVal, val, binaryOp)
setattr(self.Samples[idx], attribute, newVal)
def setGroupRegexpFileName(self, re_template: str, newGroup: int, group = None):
for idx, sample in enumerate(self.Samples):
match = re.findall(re_template, split(sample.Fname)[1]) # compare file name only, not path
if len(match) > 0:
if group == None or group == sample.Group:
self.Samples[idx].Group = newGroup
def changeGroup(self, currentGroup: int, newGroup: int):
for idx, sample in enumerate(self.Samples):
if currentGroup == sample.Group:
self.Samples[idx].Group = newGroup
def transpose(self, key_offset: int, group = None):
for idx, sample in enumerate(self.Samples):
if group == None or group == sample.Group:
sample.Pkeycenter = sample.Pkeycenter + key_offset
sample.Lokey = sample.Lokey + key_offset
sample.Hikey = sample.Hikey + key_offset
self.Samples[idx] = sample
def changeHiVelIfVels(self, newHivel, ifHivel=None, ifLovel=None, group=None): # useful when autospread velocities cannot do the job right because of multiple rr samples concurring for the same region
for idx, sample in enumerate(self.Samples):
if group == None or group == sample.Group:
condHivel = False # matches the condition ifHivel
condLovel = False # matches the condition ifLovel
if ifHivel is None:
condHivel = True
elif sample.Hivel == ifHivel:
condHivel = True
if ifLovel is None:
condLovel = True
elif sample.Lovel == ifLovel:
condLovel = True
if condHivel and condLovel:
self.Samples[idx].Hivel = newHivel
def changeLoVelIfVels(self, newLovel, ifHivel=None, ifLovel=None, group=None):
for idx, sample in enumerate(self.Samples):
if group == None or group == sample.Group:
condHivel = False # matches the condition ifHivel
condLovel = False # matches the condition ifLovel
if ifHivel is None:
condHivel = True
elif sample.Hivel == ifHivel:
condHivel = True
if ifLovel is None:
condLovel = True
elif sample.Lovel == ifLovel:
condLovel = True
if condHivel and condLovel:
self.Samples[idx].Lovel = newLovel
# Methods that access the wav files
def setVolumeToNormalize(self, multiplier: float = 1, group = None):
for idx, sample in enumerate(self.Samples):
if group == None or group == sample.Group:
peakDB = getPeakAmpDB(sample.FullFilePath)
self.Samples[idx].Volume = multiplier * (-peakDB)