-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.lua
More file actions
2447 lines (2169 loc) · 99.8 KB
/
Copy pathmain.lua
File metadata and controls
2447 lines (2169 loc) · 99.8 KB
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
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
-- main.lua - BreakFast Main Entry Point (Phase 2: Added Substitute Overwrite Behavior)
local vb = renoise.ViewBuilder()
local labeler = require("labeler")
local breakpoints = require("breakpoints")
local syntax = require("syntax")
local utils = require("utils")
local editor = require("editor")
local selection = require("selection")
local json = require("json")
local dialog = nil
-- Forward declarations
local commit_to_phrase
local add_composite_symbol
local current_symbol_index = 0
-- Available composite symbols
local composite_symbols = {"U", "V", "W", "X", "Y", "Z"}
-- Current dialog ViewBuilder reference for composite keybindings
local current_dialog_vb = nil
-- Store formatted labels at module level for pagination access
local current_formatted_labels = {}
-- Overflow behavior constants and state
local overflow_behavior = {
EXTEND = 1,
NEXT_PATTERN = 2,
TRUNCATE = 3,
LOOP = 4
}
local current_overflow_behavior = overflow_behavior.EXTEND
-- UPDATED: Overwrite behavior constants and state (added SUBSTITUTE, RETAIN, EXCLUDE, and INTERSECT)
local overwrite_behavior = {
SUM = 1,
REPLACE = 2,
SUBSTITUTE = 3,
RETAIN = 4,
EXCLUDE = 5,
INTERSECT = 6 -- NEW: Added intersect behavior
}
local current_overwrite_behavior = overwrite_behavior.SUM
-- Instrument source behavior constants and state
local instrument_source_behavior = {
EMBEDDED = 1, -- Use embedded instrument values (current behavior)
CURRENT_SELECTED = 2 -- Use currently selected instrument
}
local current_instrument_source_behavior = instrument_source_behavior.EMBEDDED
-- Global symbol registry for cross-instrument symbol management
local global_symbol_registry = {}
local available_symbols = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9"}
-- Set up tool preferences for global symbol registry persistence (declare early)
local preferences = renoise.Document.create("BreakFastPreferences") {
-- Use a simple string-based storage approach to avoid nested table issues
global_symbol_registry_data = ""
}
renoise.tool().preferences = preferences
-- Initialization flag
local tool_initialized = false
-- Pagination state for symbol grid
local symbol_pagination = {
current_page = 1,
symbols_per_page = 12, -- 3x4 grid (3 rows, 4 columns)
total_pages = 1
}
-- Serialize a table to a string (simple implementation)
local function serialize_table(t, indent)
indent = indent or 0
local spacing = string.rep(" ", indent)
local result = "{\n"
for k, v in pairs(t) do
local key_str = type(k) == "string" and string.format("[%q]", k) or "[" .. tostring(k) .. "]"
result = result .. spacing .. " " .. key_str .. " = "
if type(v) == "table" then
result = result .. serialize_table(v, indent + 1)
elseif type(v) == "string" then
result = result .. string.format("%q", v)
else
result = result .. tostring(v)
end
result = result .. ",\n"
end
result = result .. spacing .. "}"
return result
end
-- Global symbol registry management functions
function get_global_symbol_registry()
return global_symbol_registry
end
function set_global_symbol_registry(registry)
global_symbol_registry = registry or {}
end
function find_next_available_symbols(num_symbols_needed)
local used_symbols = {}
for symbol, data in pairs(global_symbol_registry) do
used_symbols[symbol] = true
end
local available = {}
for _, symbol in ipairs(available_symbols) do
if not used_symbols[symbol] and #available < num_symbols_needed then
table.insert(available, symbol)
end
end
return available
end
function assign_symbols_to_instrument(instrument_index, break_sets, saved_labels)
local num_symbols_needed = #break_sets
local available = find_next_available_symbols(num_symbols_needed)
if #available < num_symbols_needed then
return nil, string.format("Not enough available symbols. Need %d, only %d available.",
num_symbols_needed, #available)
end
-- Assign symbols to this instrument
for i = 1, num_symbols_needed do
local symbol = available[i]
global_symbol_registry[symbol] = {
instrument_index = instrument_index,
break_set = break_sets[i],
saved_labels = saved_labels
}
end
return available
end
function get_symbol_instrument_mapping(symbol)
local registry_entry = global_symbol_registry[symbol]
return registry_entry and registry_entry.instrument_index or nil
end
-- Clear all symbols from the global registry
function clear_all_symbols()
print("DEBUG: Clearing all symbols from global registry")
-- Clear the in-memory registry
global_symbol_registry = {}
-- Clear the persisted data
preferences.global_symbol_registry_data.value = ""
-- Update current formatted labels to reflect the cleared state
current_formatted_labels = {}
print("DEBUG: All symbols cleared from registry and preferences")
-- Refresh the main dialog if it's open to show the cleared state
if dialog and dialog.visible then
dialog:close()
show_main_dialog()
end
renoise.app():show_status("All symbols cleared from global registry")
end
-- Capture current selection as symbol
function capture_selection_as_symbol()
print("DEBUG: Capturing selection as symbol")
local success, new_symbol = selection.capture_selection_as_symbol()
if success then
-- Update current formatted labels to reflect the new symbol
current_formatted_labels = syntax.prepare_global_symbol_labels(global_symbol_registry)
-- Refresh the main dialog if it's open to show the new symbol
if dialog and dialog.visible then
dialog:close()
show_main_dialog()
end
return true, new_symbol
end
return false
end
-- Load global symbol registry from preferences on startup
function load_global_symbol_registry()
if preferences.global_symbol_registry_data and preferences.global_symbol_registry_data.value ~= "" then
-- Deserialize the registry from string format
local success, loaded_registry = pcall(loadstring("return " .. preferences.global_symbol_registry_data.value))
if success and loaded_registry then
global_symbol_registry = loaded_registry
print("DEBUG: Loaded global symbol registry with", table.count(global_symbol_registry), "symbols")
else
print("DEBUG: Failed to load global symbol registry, starting with empty registry")
global_symbol_registry = {}
end
else
print("DEBUG: No saved global symbol registry found, starting with empty registry")
global_symbol_registry = {}
end
end
-- Save global symbol registry to preferences
function save_global_symbol_registry()
-- Serialize the registry to a string
local serialized = serialize_table(global_symbol_registry)
preferences.global_symbol_registry_data.value = serialized
print("DEBUG: Saved global symbol registry with", table.count(global_symbol_registry), "symbols")
end
-- Export global symbol registry to CSV format
function export_global_alphabet_csv()
local filepath = renoise.app():prompt_for_filename_to_write("csv", "Export Global Alphabet (CSV)")
if not filepath or filepath == "" then return end
if not filepath:lower():match("%.csv$") then
filepath = filepath .. ".csv"
end
local file, err = io.open(filepath, "w")
if not file then
renoise.app():show_error("Unable to open file for writing: " .. tostring(err))
return
end
-- Write CSV header - expanded to include symbol type and range capture metadata
file:write("Symbol,SymbolType,InstrumentIndex,SliceIndex,SliceLabel,IsBreakpoint,TimingLine,TimingDelay,OriginalDistance,NoteValue,SourcePattern,SourceTrack,CaptureStartLine,CaptureEndLine\n")
-- Write data for each symbol
for symbol, symbol_data in pairs(global_symbol_registry) do
local instrument_index = symbol_data.instrument_index or 1
local break_set = symbol_data.break_set
local saved_labels = symbol_data.saved_labels or {}
local symbol_type = symbol_data.symbol_type or "breakpoint_created"
-- Extract source metadata for range-captured symbols
local source_pattern = ""
local source_track = ""
local capture_start_line = ""
local capture_end_line = ""
if symbol_type == "range_captured" and symbol_data.source_metadata then
source_pattern = tostring(symbol_data.source_metadata.pattern_index or "")
source_track = tostring(symbol_data.source_metadata.track_index or "")
if symbol_data.source_metadata.capture_info then
capture_start_line = tostring(symbol_data.source_metadata.capture_info.start_line or "")
capture_end_line = tostring(symbol_data.source_metadata.capture_info.end_line or "")
end
end
if break_set and break_set.timing then
for _, timing in ipairs(break_set.timing) do
local slice_index = timing.instrument_value or 0
local hex_key = string.format("%02X", slice_index + 1)
local label_data = saved_labels[hex_key] or {}
local slice_label = label_data.label or ""
local is_breakpoint = label_data.breakpoint or false
local note_value = timing.note_value or ""
-- Escape CSV fields - ensure all values are strings and handle nil
local function escape_csv_field(field)
-- Convert to string and handle nil values
local str_field = tostring(field or "")
if str_field:find(',') or str_field:find('"') then
return '"' .. str_field:gsub('"', '""') .. '"'
end
return str_field
end
-- For range-captured symbols, use the actual instrument value from the timing data
-- For breakpoint symbols, use the slice_index as before
local actual_instrument_value = slice_index
if symbol_type == "range_captured" and timing.source_instrument_index then
-- For range symbols, the instrument value should be the 0-based instrument from the pattern
actual_instrument_value = (timing.source_instrument_index - 1)
end
local values = {
symbol or "",
symbol_type or "",
instrument_index or "",
actual_instrument_value or "",
slice_label or "",
tostring(is_breakpoint),
timing.relative_line or "",
timing.new_delay or "",
timing.original_distance or "",
note_value or "",
source_pattern,
source_track,
capture_start_line,
capture_end_line
}
-- Ensure all values are properly escaped
for i, value in ipairs(values) do
values[i] = escape_csv_field(value)
end
file:write(table.concat(values, ",") .. "\n")
end
end
end
file:close()
renoise.app():show_status("Global alphabet exported to " .. filepath)
end
-- Export global symbol registry to JSON format
function export_global_alphabet_json()
local filepath = renoise.app():prompt_for_filename_to_write("json", "Export Global Alphabet (JSON)")
if not filepath or filepath == "" then return end
if not filepath:lower():match("%.json$") then
filepath = filepath .. ".json"
end
local file, err = io.open(filepath, "w")
if not file then
renoise.app():show_error("Unable to open file for writing: " .. tostring(err))
return
end
-- Build export structure with expanded schema
local export_data = {
version = "2.0",
symbols = {}
}
for symbol, symbol_data in pairs(global_symbol_registry) do
local instrument_index = symbol_data.instrument_index or 1
local break_set = symbol_data.break_set
local saved_labels = symbol_data.saved_labels or {}
local symbol_type = symbol_data.symbol_type or "breakpoint_created"
-- Build symbol entry with all metadata
local symbol_entry = {
symbol_type = symbol_type,
instrument_index = instrument_index,
notes = {},
timing_data = {}
}
-- Add source metadata for range-captured symbols
if symbol_type == "range_captured" and symbol_data.source_metadata then
symbol_entry.source_metadata = {
pattern_index = symbol_data.source_metadata.pattern_index,
track_index = symbol_data.source_metadata.track_index,
capture_info = symbol_data.source_metadata.capture_info
}
end
-- Add saved labels for breakpoint symbols
if symbol_type == "breakpoint_created" and saved_labels then
symbol_entry.saved_labels = saved_labels
end
if break_set and break_set.timing then
for _, timing in ipairs(break_set.timing) do
local slice_index = timing.instrument_value or 0
local hex_key = string.format("%02X", slice_index + 1)
local label_data = saved_labels[hex_key] or {}
-- For range-captured symbols, use the actual instrument value from timing data
-- For breakpoint symbols, use the slice_index as before
local actual_slice_index = slice_index
local actual_instrument_value = timing.source_instrument_index or instrument_index
if symbol_type == "range_captured" then
-- For range symbols, slice_index should represent the actual instrument value from the pattern
actual_slice_index = (timing.source_instrument_index and (timing.source_instrument_index - 1)) or slice_index
end
-- Build note entry with comprehensive data
local note_entry = {
slice_index = actual_slice_index,
label = label_data.label or "",
breakpoint = label_data.breakpoint or false,
timing_line = timing.relative_line or 1,
timing_delay = timing.new_delay or 0,
original_distance = timing.original_distance or 256,
source_instrument_index = actual_instrument_value
}
-- Add note_value for range-captured symbols
if timing.note_value then
note_entry.note_value = timing.note_value
end
-- Add additional timing properties if they exist
if timing.volume_value then
note_entry.volume_value = timing.volume_value
end
if timing.panning_value then
note_entry.panning_value = timing.panning_value
end
if timing.effect_number then
note_entry.effect_number = timing.effect_number
end
if timing.effect_amount then
note_entry.effect_amount = timing.effect_amount
end
table.insert(symbol_entry.notes, note_entry)
-- Also store raw timing data for exact reconstruction
table.insert(symbol_entry.timing_data, {
instrument_value = timing.instrument_value or 0,
relative_line = timing.relative_line or 1,
new_delay = timing.new_delay or 0,
original_distance = timing.original_distance or 256,
source_instrument_index = timing.source_instrument_index or instrument_index,
note_value = timing.note_value,
volume_value = timing.volume_value,
panning_value = timing.panning_value,
effect_number = timing.effect_number,
effect_amount = timing.effect_amount
})
end
end
export_data.symbols[symbol] = symbol_entry
end
-- Write JSON
local json_str = json.encode(export_data)
file:write(json_str)
file:close()
renoise.app():show_status("Global alphabet exported to " .. filepath)
end
-- Show format selection dialog for export
function export_global_alphabet()
local vb = renoise.ViewBuilder()
local format_dialog = nil -- Declare upfront
local dialog_content = vb:column {
margin = 10,
spacing = 10,
vb:text {
text = "Export Global Alphabet",
font = "big",
style = "strong"
},
vb:text {
text = "Choose export format:",
style = "strong"
},
vb:row {
spacing = 10,
vb:button {
text = "CSV",
width = 80,
notifier = function()
if format_dialog then format_dialog:close() end
export_global_alphabet_csv()
end
},
vb:button {
text = "JSON",
width = 80,
notifier = function()
if format_dialog then format_dialog:close() end
export_global_alphabet_json()
end
},
vb:button {
text = "Cancel",
width = 80,
notifier = function()
if format_dialog then format_dialog:close() end
end
}
}
}
format_dialog = renoise.app():show_custom_dialog("Export Format", dialog_content)
end
-- Import global symbol registry from CSV format
function import_global_alphabet_csv()
local filepath = renoise.app():prompt_for_filename_to_read({"*.csv"}, "Import Global Alphabet (CSV)")
if not filepath or filepath == "" then return end
local file, err = io.open(filepath, "r")
if not file then
renoise.app():show_error("Unable to open file: " .. tostring(err))
return
end
-- Read and validate header
local header = file:read()
if not header then
renoise.app():show_error("Invalid CSV format: No header found")
file:close()
return
end
-- Parse header to find column positions
local function parse_csv_line(line)
local fields = {}
local field = ""
local in_quotes = false
local i = 1
while i <= #line do
local char = line:sub(i,i)
if char == '"' then
if in_quotes and line:sub(i+1,i+1) == '"' then
field = field .. '"'
i = i + 2
else
in_quotes = not in_quotes
i = i + 1
end
elseif char == ',' and not in_quotes then
table.insert(fields, field)
field = ""
i = i + 1
else
field = field .. char
i = i + 1
end
end
table.insert(fields, field)
return fields
end
local function unescape_csv_field(field)
if field:sub(1,1) == '"' and field:sub(-1) == '"' then
return field:sub(2, -2):gsub('""', '"')
end
return field
end
local header_fields = parse_csv_line(header)
local column_positions = {}
-- Updated expected columns to include new fields
local expected_columns = {
"symbol", "symboltype", "instrumentindex", "sliceindex", "slicelabel",
"isbreakpoint", "timingline", "timingdelay", "originaldistance", "notevalue",
"sourcepattern", "sourcetrack", "capturestartline", "captureendline"
}
-- Map header fields to column positions (case insensitive)
for i, field in ipairs(header_fields) do
local lower_field = field:lower():gsub("%s+", "")
for _, expected in ipairs(expected_columns) do
if lower_field == expected then
column_positions[expected] = i
break
end
end
end
-- Validate required core columns exist (backwards compatibility check)
local required_columns = {"symbol", "instrumentindex", "sliceindex", "timingline", "timingdelay", "originaldistance"}
for _, required in ipairs(required_columns) do
if not column_positions[required] then
renoise.app():show_error("Invalid CSV format: Missing required '" .. required .. "' column")
file:close()
return
end
end
-- Parse data lines
local imported_symbols = {}
local line_number = 1
for line in file:lines() do
line_number = line_number + 1
local line_trimmed = line:gsub("^%s*(.-)%s*$", "%1")
if line_trimmed ~= "" then -- Only process non-empty lines
local fields = parse_csv_line(line)
if #fields >= #required_columns then -- Only process lines with sufficient core fields
-- Extract core fields
local symbol = unescape_csv_field(fields[column_positions.symbol] or ""):upper()
local symbol_type = unescape_csv_field(fields[column_positions.symboltype] or "breakpoint_created")
local instrument_index = tonumber(unescape_csv_field(fields[column_positions.instrumentindex] or "1"))
local slice_index = tonumber(unescape_csv_field(fields[column_positions.sliceindex] or "0"))
local slice_label = unescape_csv_field(fields[column_positions.slicelabel] or "")
local is_breakpoint_str = unescape_csv_field(fields[column_positions.isbreakpoint] or "false"):lower()
local timing_line = tonumber(unescape_csv_field(fields[column_positions.timingline] or "1"))
local timing_delay = tonumber(unescape_csv_field(fields[column_positions.timingdelay] or "0"))
local original_distance = tonumber(unescape_csv_field(fields[column_positions.originaldistance] or "256"))
-- Extract new fields with fallbacks
local note_value = nil
if column_positions.notevalue and fields[column_positions.notevalue] and fields[column_positions.notevalue] ~= "" then
note_value = tonumber(unescape_csv_field(fields[column_positions.notevalue]))
end
-- Extract range capture metadata
local source_pattern = nil
local source_track = nil
local capture_start_line = nil
local capture_end_line = nil
if symbol_type == "range_captured" then
if column_positions.sourcepattern and fields[column_positions.sourcepattern] and fields[column_positions.sourcepattern] ~= "" then
source_pattern = tonumber(unescape_csv_field(fields[column_positions.sourcepattern]))
end
if column_positions.sourcetrack and fields[column_positions.sourcetrack] and fields[column_positions.sourcetrack] ~= "" then
source_track = tonumber(unescape_csv_field(fields[column_positions.sourcetrack]))
end
if column_positions.capturestartline and fields[column_positions.capturestartline] and fields[column_positions.capturestartline] ~= "" then
capture_start_line = tonumber(unescape_csv_field(fields[column_positions.capturestartline]))
end
if column_positions.captureendline and fields[column_positions.captureendline] and fields[column_positions.captureendline] ~= "" then
capture_end_line = tonumber(unescape_csv_field(fields[column_positions.captureendline]))
end
end
local is_breakpoint = (is_breakpoint_str == "true")
-- Validate essential data
if symbol and symbol ~= "" and instrument_index and slice_index and timing_line and timing_delay and original_distance then
-- Initialize symbol data if not exists
if not imported_symbols[symbol] then
imported_symbols[symbol] = {
symbol_type = symbol_type,
instrument_index = instrument_index,
timing_data = {},
saved_labels = {},
source_metadata = nil
}
-- Add source metadata for range-captured symbols
if symbol_type == "range_captured" and (source_pattern or source_track or capture_start_line or capture_end_line) then
imported_symbols[symbol].source_metadata = {
pattern_index = source_pattern,
track_index = source_track,
capture_info = {}
}
if capture_start_line or capture_end_line then
imported_symbols[symbol].source_metadata.capture_info = {
start_line = capture_start_line,
end_line = capture_end_line
}
end
end
end
-- Create timing entry with all available data
-- For range-captured symbols, slice_index contains the actual instrument value (0-based)
-- For breakpoint symbols, slice_index is the slice index
local timing_entry = {
instrument_value = slice_index,
relative_line = timing_line,
new_delay = timing_delay,
original_distance = original_distance,
source_instrument_index = instrument_index
}
-- For range-captured symbols, ensure source_instrument_index reflects the actual instrument
if symbol_type == "range_captured" then
-- slice_index contains the 0-based instrument value from the pattern
-- Convert to 1-based for source_instrument_index
timing_entry.source_instrument_index = slice_index + 1
end
-- Add note_value for range-captured symbols
if note_value then
timing_entry.note_value = note_value
end
table.insert(imported_symbols[symbol].timing_data, timing_entry)
-- Add label data (for breakpoint symbols or compatibility)
if symbol_type == "breakpoint_created" or slice_label ~= "" or is_breakpoint then
local hex_key = string.format("%02X", slice_index + 1)
imported_symbols[symbol].saved_labels[hex_key] = {
label = slice_label,
breakpoint = is_breakpoint,
instrument_index = instrument_index
}
end
else
print("WARNING: Line " .. line_number .. " has invalid core data, skipping")
end
else
print("WARNING: Line " .. line_number .. " has insufficient fields, skipping")
end
end
end
file:close()
if next(imported_symbols) == nil then
renoise.app():show_warning("No valid symbol data found in file")
return
end
-- Convert imported data to proper break_set format and update global registry
for symbol, symbol_data in pairs(imported_symbols) do
-- Create break_set structure
local break_set = {
timing = symbol_data.timing_data,
notes = {},
start_line = 1,
end_line = 64 -- Default values
}
-- Create notes from timing data
for _, timing in ipairs(symbol_data.timing_data) do
local note_entry = {
line = timing.relative_line,
instrument_value = timing.instrument_value,
delay_value = timing.new_delay,
distance = timing.original_distance,
is_last = false
}
-- Set note_value based on symbol type
if symbol_data.symbol_type == "range_captured" and timing.note_value then
note_entry.note_value = timing.note_value
else
note_entry.note_value = 48 -- C-4 default for breakpoint symbols
end
table.insert(break_set.notes, note_entry)
end
-- Adjust end_line based on actual content
if #break_set.notes > 0 then
local last_note = break_set.notes[#break_set.notes]
local distance_in_lines = math.floor(last_note.distance / 256)
break_set.end_line = last_note.line + distance_in_lines + 4 -- Add buffer
end
-- Create registry entry with proper structure
local registry_entry = {
instrument_index = symbol_data.instrument_index,
break_set = break_set,
saved_labels = symbol_data.saved_labels
}
-- Add symbol type and source metadata for range-captured symbols
if symbol_data.symbol_type == "range_captured" then
registry_entry.symbol_type = "range_captured"
if symbol_data.source_metadata then
registry_entry.source_metadata = symbol_data.source_metadata
end
end
-- Update global registry
global_symbol_registry[symbol] = registry_entry
end
-- Save to preferences
save_global_symbol_registry()
local symbol_count = 0
for _ in pairs(imported_symbols) do symbol_count = symbol_count + 1 end
renoise.app():show_status(string.format("Imported %d symbols from CSV", symbol_count))
-- Refresh main dialog if open
if dialog and dialog.visible then
dialog:close()
show_main_dialog()
end
end
-- Import global symbol registry from JSON format
function import_global_alphabet_json()
local filepath = renoise.app():prompt_for_filename_to_read({"*.json"}, "Import Global Alphabet (JSON)")
if not filepath or filepath == "" then return end
local file, err = io.open(filepath, "r")
if not file then
renoise.app():show_error("Unable to open file: " .. tostring(err))
return
end
local content = file:read("*all")
file:close()
-- Parse JSON
local success, import_data = pcall(json.decode, content)
if not success then
renoise.app():show_error("Invalid JSON format: " .. tostring(import_data))
return
end
-- Validate structure
if not import_data.symbols or type(import_data.symbols) ~= "table" then
renoise.app():show_error("Invalid JSON structure: Missing 'symbols' table")
return
end
-- Check version for compatibility
local format_version = import_data.version or "1.0"
local is_legacy_format = (format_version == "1.0")
local imported_count = 0
-- Process each symbol
for symbol, symbol_data in pairs(import_data.symbols) do
if type(symbol_data) == "table" and
symbol_data.instrument_index and
((symbol_data.notes and type(symbol_data.notes) == "table") or
(symbol_data.timing_data and type(symbol_data.timing_data) == "table")) then
local instrument_index = symbol_data.instrument_index
local symbol_type = symbol_data.symbol_type or "breakpoint_created"
local timing_data = {}
local saved_labels = {}
local notes = {}
local source_metadata = nil
-- Handle different data sources based on format version
local data_source = nil
if not is_legacy_format and symbol_data.timing_data then
-- New format: use timing_data for reconstruction
data_source = symbol_data.timing_data
elseif symbol_data.notes then
-- Legacy format or fallback: use notes array
data_source = symbol_data.notes
end
if data_source then
-- Process timing/note data
for _, entry in ipairs(data_source) do
if type(entry) == "table" then
local slice_index, timing_line, timing_delay, original_distance, note_value
local source_instrument_index = instrument_index
-- Extract fields based on data source type
if not is_legacy_format and entry.instrument_value then
-- New timing_data format
slice_index = entry.instrument_value
timing_line = entry.relative_line
timing_delay = entry.new_delay
original_distance = entry.original_distance
note_value = entry.note_value
source_instrument_index = entry.source_instrument_index or instrument_index
else
-- Legacy notes format
slice_index = entry.slice_index
timing_line = entry.timing_line
timing_delay = entry.timing_delay
original_distance = entry.original_distance
note_value = entry.note_value
source_instrument_index = entry.source_instrument_index or instrument_index
end
-- Validate essential fields
if slice_index and timing_line and timing_delay and original_distance then
-- Create comprehensive timing entry
local timing_entry = {
instrument_value = slice_index,
relative_line = timing_line,
new_delay = timing_delay,
original_distance = original_distance,
source_instrument_index = source_instrument_index
}
-- For range-captured symbols, ensure proper instrument value handling
if symbol_type == "range_captured" then
-- slice_index should contain the 0-based instrument value for range symbols
-- Ensure source_instrument_index is correctly set (1-based)
if not is_legacy_format and entry.instrument_value and entry.source_instrument_index then
-- New format: instrument_value is the 0-based instrument, source_instrument_index is 1-based
timing_entry.instrument_value = entry.instrument_value
timing_entry.source_instrument_index = entry.source_instrument_index
else
-- Legacy or converted: slice_index contains 0-based instrument value
timing_entry.source_instrument_index = slice_index + 1
end
end
-- Add note_value if present
if note_value then
timing_entry.note_value = note_value
end
-- Add additional properties if they exist
if entry.volume_value then
timing_entry.volume_value = entry.volume_value
end
if entry.panning_value then
timing_entry.panning_value = entry.panning_value
end
if entry.effect_number then
timing_entry.effect_number = entry.effect_number
end
if entry.effect_amount then
timing_entry.effect_amount = entry.effect_amount
end
table.insert(timing_data, timing_entry)
-- Create note entry
local note_entry = {
line = timing_line,
instrument_value = slice_index,
delay_value = timing_delay,
distance = original_distance,
is_last = false
}
-- Set note_value based on symbol type and available data
if symbol_type == "range_captured" and note_value then
note_entry.note_value = note_value
else
note_entry.note_value = 48 -- C-4 default for breakpoint symbols
end
table.insert(notes, note_entry)
-- Add label data for breakpoint symbols
if symbol_type == "breakpoint_created" then
local hex_key = string.format("%02X", slice_index + 1)
saved_labels[hex_key] = {
label = entry.label or "",
breakpoint = entry.breakpoint or false,
instrument_index = instrument_index
}
end
end
end
end
end
-- Extract saved_labels if provided (for new format)
if not is_legacy_format and symbol_data.saved_labels and type(symbol_data.saved_labels) == "table" then
saved_labels = symbol_data.saved_labels
end
-- Extract source_metadata for range-captured symbols
if symbol_type == "range_captured" and symbol_data.source_metadata and type(symbol_data.source_metadata) == "table" then
source_metadata = symbol_data.source_metadata
end
if #timing_data > 0 then
-- Create break_set structure
local break_set = {
timing = timing_data,
notes = notes,
start_line = 1,
end_line = 64 -- Default values
}
-- Adjust end_line based on actual content
if #notes > 0 then
local last_note = notes[#notes]
local distance_in_lines = math.floor(last_note.distance / 256)
break_set.end_line = last_note.line + distance_in_lines + 4 -- Add buffer
end
-- Create registry entry with proper structure
local registry_entry = {
instrument_index = instrument_index,
break_set = break_set,
saved_labels = saved_labels
}
-- Add symbol type and source metadata for range-captured symbols
if symbol_type == "range_captured" then
registry_entry.symbol_type = "range_captured"
if source_metadata then
registry_entry.source_metadata = source_metadata
end
end
-- Update global registry
global_symbol_registry[symbol:upper()] = registry_entry
imported_count = imported_count + 1
end