-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinstrument_settings.lua
506 lines (304 loc) · 11.1 KB
/
instrument_settings.lua
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
require "ini_handler"
require "instrument_settings_gui"
MIN_SAMPLE_RATE = 8363
MAX_SAMPLE_RATE = 96000
DEFAULT_VOLUME = 64
DEFAULT_FREQUENCY = 'Low'
DEFAULT_SINC = 2
SAMPLE_FREQUENCY_LOW = 1
SAMPLE_FREQUENCY_HIGH = 2
SAMPLE_FREQUENCY_MAXIMUM = 3
SAMPLE_FREQUENCY_ORIGINAL = 4
SAMPLE_FREQUENCY_CUSTOM = 5
SAMPLE_FREQUENCY_BASENOTE = 6
instrument_settings = {}
base_notes = {}
frequency_settings_alias = {"Low", "High", "Maximum", "Original", "Custom", "Base note"}
local function get_ini_filename()
local output = ''
if renoise.song().file_name ~= '' then
local separator = get_separator()
local app_path_folder = get_path(app_path)
local song_filename = string.match(renoise.song().file_name, "[^".. separator .. "]*$")
local ini_file = string.gsub(song_filename, ".xrns", ".ini")
local arr_path = {app_path_folder, 'ini', ini_file}
output = concatenate_path(arr_path)
end
return output
end
function open_settings_folder()
local ini_file = get_ini_filename()
if io.exists(ini_file) then
renoise.app():open_path(ini_file);
else
local app_path = get_path(app_path)
local ini_folder = concatenate_path({app_path, 'ini'})
renoise.app():open_path(ini_file);
end
end
function set_instrument_volume(ki, ks, value)
if instrument_settings[ki][ks] ~= nil then
instrument_settings[ki][ks]['default_volume'] = value
end
end
function set_instrument_sinc(ki, ks, value)
if instrument_settings[ki][ks] ~= nil then
instrument_settings[ki][ks]['sinc'] = (value-1)
end
end
function set_instrument_basenote(ki, ks, index)
if instrument_settings[ki][ks] ~= nil then
print('set_instrument_basenote: ' .. base_notes[index])
instrument_settings[ki][ks]['frequency'] = base_notes[index]
print ('instrument settings saved: ' .. instrument_settings[ki][ks]['frequency'])
end
end
function set_instrument_frequency_alias(ki, ks, index)
if instrument_settings[ki][ks] ~= nil then
print('set_instrument_frequency_alias: ' .. frequency_settings_alias[index])
instrument_settings[ki][ks]['frequency'] = frequency_settings_alias[index]
print ('instrument settings saved: ' .. instrument_settings[ki][ks]['frequency'])
end
end
function set_instrument_frequency(ki, ks, value)
if instrument_settings[ki][ks] ~= nil then
print('set_instrument_frequency: ' .. value)
instrument_settings[ki][ks]['frequency'] = value
print ('instrument settings saved: ' .. instrument_settings[ki][ks]['frequency'])
end
end
function save_ini_settings()
local conf = { }
conf['volume'] = { }
conf['frequency'] = { }
conf['sinc'] = { }
for ki, vi in ipairs(renoise.song().instruments) do
for ks, vs in ipairs(renoise.song().instruments[ki].samples) do
if renoise.song().instruments[ki].samples[ks].sample_buffer.has_sample_data then
local vol = instrument_settings[ki][ks]['default_volume']
local freq = instrument_settings[ki][ks]['frequency']
local sinc = instrument_settings[ki][ks]['sinc']
local key = tostring( (ki - 1) .. '/' .. (ks - 1))
if vol ~= DEFAULT_VOLUME then
conf['volume'][key] = vol
end
if freq ~= DEFAULT_FREQUENCY then
conf['frequency'][key] = freq
end
if sinc ~= DEFAULT_SINC then
conf['sinc'][key] = sinc
end
end
end
end
rprint(conf)
local ini_file = get_ini_filename()
if ( (table.is_empty( conf['volume']) and
table.is_empty(conf['frequency']) and
table.is_empty(conf['sinc'])) == false ) then
renoise.app():show_status('Settings saved to ' .. ini_file)
save_configuration(ini_file, conf)
else
os.remove(ini_file)
renoise.app():show_status("No settings to save, file " .. ini_file .. " was removed")
end
end
function exists_sample_frequency_key( instr_index, sample_index)
return instrument_settings[instr_index] ~= nil and instrument_settings[instr_index][sample_index] ~= nil
end
function exists_sample_frequency_key( instr_index, sample_index)
return instrument_settings[instr_index] ~= nil and instrument_settings[instr_index][sample_index] ~= nil
end
function load_instrument_sinc( instr_index, sample_index)
local default_value = DEFAULT_SINC-1
if instrument_settings[instr_index][sample_index] == nil then
return DEFAULT_SINC
end
local value = instrument_settings[instr_index][sample_index]['sinc']
return (tonumber( value ) + 1)
end
function load_instrument_volume( instr_index, sample_index)
if instrument_settings[instr_index][sample_index] == nil then
return DEFAULT_VOLUME
end
local value = instrument_settings[instr_index][sample_index]['default_volume']
return tonumber( value )
end
function load_instrument_basenote( instr_index, sample_index)
print('load_instrument_basenote')
local default_value = table.find(base_notes, 'C-2')
if instrument_settings[instr_index][sample_index] == nil then
return default_value
end
local value = instrument_settings[instr_index][sample_index]['frequency']
local strvalue = tostring( value )
print('cur_val:' .. strvalue)
if strvalue ~= nil and strvalue:match('^[A-Ga-g][-#][2-3]$') then
return table.find(base_notes, strvalue)
elseif strvalue == 'Low' then
return default_value
elseif strvalue == 'High' then
return table.find(base_notes, 'C-3')
elseif strvalue == 'Maximum' then
return table.find(base_notes, 'B-3')
end
return default_value
end
function load_instrument_frequency( instr_index, sample_index)
print('load_instrument_frequency')
local default_value = SAMPLE_FREQUENCY_LOW
if instrument_settings[instr_index][sample_index] == nil then
return default_value
end
local value = instrument_settings[instr_index][sample_index]['frequency']
local strvalue = tostring(value)
print("cur value: " .. strvalue)
if strvalue:match('^[0-9]') then
return SAMPLE_FREQUENCY_CUSTOM
elseif strvalue:match('^[A-Ga-g][-#][2-5]$') then
return SAMPLE_FREQUENCY_BASENOTE
elseif strvalue == 'Low' then
return SAMPLE_FREQUENCY_LOW
elseif strvalue == 'High' then
return SAMPLE_FREQUENCY_HIGH
elseif strvalue == 'Maximum' then
return SAMPLE_FREQUENCY_MAXIMUM
elseif strvalue == 'Original' then
return SAMPLE_FREQUENCY_ORIGINAL
end
return default_value
end
function load_instrument_frequency_rate( instr_index, sample_index)
print('load_instrument_frequency_rate')
if instrument_settings[instr_index][sample_index] == nil then
return MIN_SAMPLE_RATE
end
local default_value = renoise.song().instruments[instr_index].samples[sample_index].sample_buffer.sample_rate
local value = instrument_settings[instr_index][sample_index]['frequency']
local output = tonumber(value)
print("cur value: " .. value)
if output ~= nil then
return output
else -- not a number
return default_value
end
end
function get_samples(instr_index)
local output = { }
for k, v in ipairs(renoise.song().instruments[instr_index].samples) do
output[k] = v.name
end
return output
end
function get_sample_frequency_settings()
return frequency_settings_alias
end
function get_sinc_values()
return { 'Linear', '8 pt sinc', '16 pt sinc', '32 pt sinc' }
end
function get_base_note()
local output_notes = {}
for i=2,3 do
for k, v in ipairs(NOTES_TABLE) do
table.insert(output_notes, ("%s%s"):format(v,i))
end
end
base_notes = output_notes
return output_notes
end
function get_instrument_names()
local output = { }
for k, v in ipairs(renoise.song().instruments) do
output[k] = v.name
end
return output
end
local function init_instrument_settings(conf)
if conf['volume'] == nil then
conf['volume'] = { }
end
if conf['frequency'] == nil then
conf['frequency'] = { }
end
if conf['sinc'] == nil then
conf['sinc'] = { }
end
for ki, vi in ipairs(renoise.song().instruments) do
instrument_settings[ki] = { }
for ks, vs in ipairs(renoise.song().instruments[ki].samples) do
if renoise.song().instruments[ki].samples[ks].sample_buffer.has_sample_data then
instrument_settings[ki][ks] = { }
instrument_settings[ki][ks]['default_volume'] = DEFAULT_VOLUME
--instrument_settings[ki][ks]['frequency'] = renoise.song().instruments[ki].samples[ks].sample_buffer.sample_rate
instrument_settings[ki][ks]['frequency'] = DEFAULT_FREQUENCY
instrument_settings[ki][ks]['sinc'] = DEFAULT_SINC
local c_vol = conf['volume']
local c_freq = conf['frequency']
local c_sinc = conf['sinc']
local key = tostring( (ki - 1) .. '/' .. (ks - 1))
if c_vol[key] ~= nil then
instrument_settings[ki][ks]['default_volume'] = c_vol[key]
end
if c_freq[key] ~= nil then
instrument_settings[ki][ks]['frequency'] = c_freq[key]
end
if c_sinc[key] ~= nil then
instrument_settings[ki][ks]['sinc'] = c_sinc[key]
end
end
end
end
--rprint(instrument_settings)
end
function reset_instrument_values(ki, ks)
if instrument_settings[ki][ks] ~= nil then
instrument_settings[ki][ks]['default_volume'] = DEFAULT_VOLUME
instrument_settings[ki][ks]['frequency'] = DEFAULT_FREQUENCY
instrument_settings[ki][ks]['sinc'] = DEFAULT_SINC
end
renoise.app():show_status('Sample settings reset to its default value ')
end
function reset_instrument_settings()
local conf = { }
init_instrument_settings(conf)
renoise.app():show_status('All Samples were reset to default values')
end
function load_ini_settings(show_warn)
local ini_file = get_ini_filename()
local conf = { }
if (io.exists(ini_file)) then
conf = load_configuration(ini_file)
renoise.app():show_status('Settings loaded from ' .. ini_file)
else
if show_warn then
renoise.app():show_status("Warning: settings not found")
end
end
init_instrument_settings(conf)
end
function init_instrument_settings_dialog()
if converter_dialog and converter_dialog.visible then
converter_dialog:close()
end
if helper_dialog and helper_dialog.visible then
helper_dialog:close()
end
if downgrade_dialog and downgrade_dialog.visible then
downgrade_dialog:close()
end
if controlpanel_dialog and controlpanel_dialog.visible then
controlpanel_dialog:close()
end
if renoise.song().file_name ~= '' then
local ini_file = get_ini_filename()
local conf = { }
if (io.exists(ini_file)) then
conf = load_configuration(ini_file)
end
init_instrument_settings(conf)
show_instrument_settings_dialog()
else
renoise.app():show_error(MSG_SONG_NOT_SAVED);
return
end
end