forked from lgmventura/SoundFontTools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsfzCreatorClass.py
427 lines (364 loc) · 17.8 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
#!/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
class sample:
# opcodes:
Fname = None
Pkeycenter = None
Lokey = None
Hikey = None
Lovel = None
Hivel = None
Ampeg_start = None
Ampeg_attack = None
Ampeg_release = None
Amp_veltrack = None
Cutoff = None
Fil_veltrack = None
Volume = None
Hirand = None
Lorand = None
Trigger = None
# non-opcodes:
Vel = None
FullFilePath = 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)
# Defining some functions to be used later
def getPeakAmpDB(wave_file_path):
from scipy.io.wavfile import read
a = read(wave_file_path)
data = np.array(a[1])
maxAmp = np.max(data)/(2**16/2) # considering a signed 16 bit wav!! division by 2 because it's signed
minAmp = np.min(data)/(2**16/2)
peakDB = max(20 * np.log10(maxAmp), 20 * np.log10(-minAmp))
return peakDB
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 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.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))
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)
currIdx = samples_same_p.tolist().index(currSam)
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 - better approach: run thorough all pitches (0 to 127) and for every sub-region, do an independent spread. Connect them!
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 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
# 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)