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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
|
package main
import "core:fmt"
import "core:os"
import "core:os/os2"
import "core:path/filepath"
import "core:sys/windows"
import "core:strings"
import "core:math"
import "wav"
/*
TODO: Simplify pre-allocation. Just allocate a bunch. It's probably fine.
Maybe do it by just counting how many lines are longer than 2 characters.
TODO: Drag-n-drop window if no files are specified
*/
VERBOSE :: false
INCLUDE_DATE :: false // By default I delete the retarded date field that says what day the report was generated.
PART_ONE :: #load("parts/start.html", string)
PART_TWO :: #load("parts/start2.html", string)
PART_END :: #load("parts/end.html", string)
HEADER_TEMPLATE :: #load("header_template.txt", string)
HEADER_FIELDS_PATH :: "info.txt"
header_fields_file : string
Device :: enum {
UNSET,
ZOOM,
SD6,
SD8, // Tested with 888
}
Stages :: enum {
TITLE,
INFO,
HEADER,
BODY,
}
Info_Line :: struct {
field : string,
entry : string,
}
Report :: struct {
// Content
title : string,
info_lines : []Info_Line,
header : []string,
table : [][]string,
column_count : int,
row_count : int,
info_line_count : int,
tc_column_index : int,
// Meta
path : string,
}
CSV :: string
Directory :: [dynamic]string
Job :: union {CSV, Directory}
job_list : [dynamic]Job
// TODO: Changing file_list to job_list, so the Directory jobs can contain a list of all the relevant .wav files before being sent to parse_folder()
main :: proc() {
when ODIN_OS == .Windows {
windows.SetConsoleOutputCP(windows.CODEPAGE.UTF8)
}
input_file_name : string
if len(os.args) < 2 {
fmt.println("No paths submitted.")
if os.is_file(HEADER_FIELDS_PATH) {
fmt.printfln("\"%v\" already exists.", HEADER_FIELDS_PATH)
} else {
os.write_entire_file(HEADER_FIELDS_PATH, transmute([]u8)HEADER_TEMPLATE)
fmt.printfln("Created \"%v\".", HEADER_FIELDS_PATH)
}
return
}
fmt.printf("Input path: {}\n", os.args[1])
input_file_name = os.args[1]
path_info, error := os.stat(input_file_name)
file_count := 1
files_done := 0
if error == os.ERROR_NONE {
if(path_info.is_dir) {
fmt.printf("Directory submitted! Walking directory...\n\n")
fmt.printf("š {}\n", path_info.name)
try_os2 := walk_directory(path_info.fullpath, &file_count, 1)
if len(job_list) == 0 && try_os2 {
fmt.printf("\nNot_Dir error encountered. Trying os2 version...\n\n")
fmt.printf("š {}\n", path_info.name)
walk_directory_os2(path_info.fullpath, &file_count, 1)
}
} else {
fmt.println("File submitted! Processing file...")
append(&job_list, CSV(strings.clone(path_info.fullpath)))
}
for job, i in job_list {
parsed : Report
parse_ok : bool
switch file in job {
case CSV:
file_info, _ := os.stat(file)
fmt.printf("\nš File {}: {}\n", i+1, file_info.name)
parsed, parse_ok = parse_file(file_info.fullpath)
if !parse_ok {
fmt.printf("Parse failed: {}\n", file_info.fullpath)
continue
}
case Directory:
fmt.printf("\nš Folder {}: ", i+1)
parsed, parse_ok = parse_folder(file)
fmt.printf("{}", parsed.title)
if parse_ok {
fmt.printf("\nParsed %d WAV(s).\n", parsed.row_count)
} else {
file_info, _ := os.stat(file[0])
fmt.printf("\nParse failed: {}\n", file_info.fullpath)
continue
}
}
render(parsed)
free_all(context.temp_allocator)
files_done += 1
}
fmt.printf("\nCompleted {}/{} job(s).\n\n", files_done, len(job_list))
} else {
fmt.printf("ERROR could not get path info for: {}\n", input_file_name)
}
}
parse_folder :: proc(paths : Directory) -> (Report, bool) {
// 888 888 888 8888b. 888 888
// 888 888 888 "88b 888 888
// 888 888 888 .d888888 Y88 88P
// d8b Y88b 888 d88P 888 888 Y8bd8P
// Y8P "Y8888888P" "Y888888 Y88P
output : Report = {}
wavs : [dynamic]wav.Wav
max_channels := 0
for path, i in paths {
w, ok := wav.read(path)
if ok {
append(&wavs, w)
max_channels = max(max_channels, w.channels)
}
}
header_build : [dynamic]string
append(&header_build, "Circled")
append(&header_build, "File Name")
append(&header_build, "Scene")
append(&header_build, "Take")
append(&header_build, "Duration")
append(&header_build, "Timecode")
append(&header_build, "TC FPS")
append(&header_build, "User Bits")
append(&header_build, "Tape")
append(&header_build, "Date")
append(&header_build, "Project")
append(&header_build, "Sample Rate")
append(&header_build, "Format") // Bit depth and int vs float
first_channel_index := len(header_build)
last_channel_index := -1
for i in 0..<max_channels {
track_title := fmt.aprintf("Track %d", i+1)
last_channel_index = len(header_build)
append(&header_build, track_title)
}
append(&header_build, "Note")
output.header = header_build[:]
output.column_count = len(header_build)
output.row_count = len(wavs)
output.table = make([][]string, output.row_count, context.temp_allocator)
for &row in output.table {
row = make([]string, output.column_count, context.temp_allocator)
}
output.info_lines = make([]Info_Line, 64, context.temp_allocator)
info_txt, info_txt_ok := os.read_entire_file(HEADER_FIELDS_PATH, context.temp_allocator)
if info_txt_ok {
it := string(info_txt)
line_index := 0
for line in strings.split_lines_iterator(&it) {
if strings.starts_with(line, "#") {
continue
}
if len(line)<2 {
continue
}
colon := strings.index_rune(line, ':')
if colon==-1 {
continue
}
CUTSET :: " "
output.info_lines[line_index].field = strings.trim(line[:colon+1], CUTSET)
output.info_lines[line_index].entry = strings.trim(line[colon+1:], CUTSET)
line_index += 1
}
output.info_lines[line_index].field = " "
output.info_lines[line_index].entry = "- - - - -"
line_index += 1
output.info_line_count = line_index
}
// Populating the table with data
for w, i in wavs {
row := output.table[i]
stat, _ := os.stat(w.path, allocator=context.temp_allocator)
for name, i in w.channel_names {
row[first_channel_index + i] = name
}
for title, i in output.header {
switch title {
case "File Name":
row[i] = stat.name
case "Scene":
row[i] = w.scene
case "Take":
if w.take >= 0 {
row[i] = fmt.tprintf("%02d", w.take)
}
case "Duration":
row[i] = wav.tprint_duration(w)
case "Timecode":
if w.tc_framerate > 0 {
row[i] = wav.tprint_timecode(w)
}
case "TC FPS":
if w.tc_framerate > 0 {
if w.tc_dropframe { // TC FPS
row[i] = fmt.tprintf("%.03f DF", w.tc_framerate)
} else {
row[i] = fmt.tprintf("%.03f ND", w.tc_framerate)
}
}
case "User Bits":
if w.ubits != {0,0,0,0,0,0,0,0,} {
row[i] = fmt.tprint(expand_values(w.ubits), sep="")
}
case "Tape":
row[i] = w.tape
case "Date":
row[i] = fmt.tprintf("%04d-%02d-%02d", expand_values(w.date))
case "Project":
row[i] = w.project
case "Sample Rate":
row[i] = fmt.tprintf("%d Hz", w.sample_rate)
case "Format":
switch w.format { // "Format", aka bit depth + int vs float
case .INT:
row[i] = fmt.tprintf("%d-bit int", w.bit_depth)
case .FLOAT:
row[i] = fmt.tprintf("%d-bit float", w.bit_depth)
case .ADPCM:
row[i] = fmt.tprintf("%d-bit ADPCM", w.bit_depth)
}
case "Circled":
if w.circled do row[i] = "O"
case "Note":
row[i] = w.note
}
}
}
// Cleanup!
when VERBOSE do fmt.printf("Struct before cleanup:\n%#v\n", output)
// Stacking tracks to the left
for &line, l in output.table {
stacking_index := first_channel_index
for &field, f in line[first_channel_index:last_channel_index+1] {
if field != "" {
line[stacking_index] = field
stacking_index += 1
}
}
for &field, f in line[stacking_index:last_channel_index+1] {
field = ""
}
}
// Cleaning out unused columns
touched := make([]bool, output.column_count, context.temp_allocator)
// Finding them
for line, l in output.table {
for field, f in line {
if touched[f] do continue
if field != "" {
touched[f] = true
}
}
}
// Turning unchanging columns into info line
changed := make([]bool, output.column_count, context.temp_allocator)
prev_line : []string = nil
for line, l in output.table {
if l>0 {
prev_line = output.table[l - 1]
for field, f in line {
if (prev_line[f] != field) ||
(first_channel_index <= f && f <= last_channel_index) ||
(f == output.tc_column_index) {
changed[f] = true
}
}
}
}
for did_change, i in changed {
if (!did_change) && touched[i] {
field := fmt.aprintf("{}: ", output.header[i], allocator=context.temp_allocator)
entry := prev_line[i]
output.info_lines[output.info_line_count] = {field=field, entry=entry}
output.info_line_count += 1
}
}
// Removing unused and static
for &line, l in output.table {
stacking_index := 0
for &field, f in line {
if touched[f] && changed[f] {
line[stacking_index] = field
stacking_index += 1
}
}
for &field, f in line[stacking_index:] {
field = ""
}
}
stacking_index := 0
for &field, f in output.header {
if touched[f] && changed[f] {
output.header[stacking_index] = field
stacking_index += 1
}
}
for &field, f in output.header[stacking_index:] {
field = ""
}
output.column_count = stacking_index
// Setting title for report
output.title = strings.trim(filepath.base(filepath.dir(paths[0])), "/\\")
for item in output.info_lines {
if item.field == "Tape" {
output.title = item.entry
}
}
// Setting column to sort by
for title, i in output.header {
if title == "Timecode" {
output.tc_column_index = i
break
}
}
when VERBOSE do fmt.printf("Struct before output:\n%#v\n", output)
output.path = fmt.tprintf("{}/{}_Knekt_Lydrapport.html", filepath.dir(paths[0]), output.title)
return output, true
}
parse_file :: proc(path : CSV, device : Device = .UNSET) -> (Report, bool) {
device := device
output : Report = {}
data, ok := os.read_entire_file(path, context.temp_allocator)
if !ok {
fmt.printf("ERROR: Could not read file: {}\n", path)
return {}, false
}
file_info, _ := os.lstat(path, context.temp_allocator)
lines := strings.split_lines(string(data), allocator=context.temp_allocator)
// STAGE 1 --------------------------------------------------------------
// First, we detect what kind of sound report this is
for line, line_number in lines[:3] {
if (device!=.UNSET) { break }
if line == "\"SOUND REPORT\"," {
device = .ZOOM
when VERBOSE do fmt.printf("Detected ZOOM from quotes and comma on line index {}\n", line_number)
}
if line == "\"ZOOM F8\"," {
device = .ZOOM
when VERBOSE do fmt.printf("Detected ZOOM from \"ZOOM F8\" on line index {}\n", line_number)
}
if line == "SOUND REPORT" {
device = .SD6
when VERBOSE do fmt.printf("Detected SOUND_DEVICES from unquoted SOUND REPORT line index {}\n", line_number)
}
if len(line)<15 do continue
if line[:13] == "SOUND REPORT," {
device = .SD8
when VERBOSE do fmt.printf("Detected SOUND_DEVICES 8-series from SOUND REPORT with missing newline on line index {}\n", line_number)
}
}
if device == .UNSET {
fmt.printf("ERROR: Unable to detect sound report type!\n")
return {}, false
}
// STAGE 2 --------------------------------------------------------------
// Measuring content for allocation
switch device {
case .ZOOM:
output.column_count = 21 // Ugly magic number, could be fucked by firmware update
// Padded for expanding info lines from unchanging columns
output.info_lines = make([]Info_Line, 2+output.column_count, context.temp_allocator)
output.info_line_count = 2
output.row_count = strings.count(string(data), "\n") - 7 // Ugly magic number, could be fucked by firmware update
output.table = make([][]string, output.row_count, context.temp_allocator)
output.header = make([]string, output.column_count, context.temp_allocator)
for &row in output.table {
row = make([]string, output.column_count, context.temp_allocator)
}
case .SD6:
second_to_last_line := lines[len(lines)-2]
output.column_count = strings.count(second_to_last_line, ",")
count_stage : Stages = .TITLE
for line, l in lines {
switch count_stage {
case .TITLE:
if l == 1 { // Ugly magic number, could be fucked by firmware update
count_stage = .INFO
}
case .INFO:
if line == "," {
count_stage = .HEADER
continue
} else if len(line) > 2 {
output.info_line_count += 1
}
case .HEADER:
if line == "" {
count_stage = .BODY
}
case .BODY:
if len(line)>2 {
output.row_count += 1
}
}
}
output.info_lines = make([]Info_Line, output.info_line_count+output.column_count, context.temp_allocator)
output.header = make([]string, output.column_count, context.temp_allocator)
output.table = make([][]string, output.row_count, context.temp_allocator)
for &row in output.table {
row = make([]string, output.column_count, context.temp_allocator)
}
case .SD8:
count_stage : Stages = .INFO
for line, l in lines {
#partial switch count_stage {
case .INFO:
if line == "," {
count_stage = .HEADER
continue
} else if len(line) > 2 {
output.info_line_count += 1
}
case .HEADER:
if len(line) > 2 {
// Missing comma at the en in 8-series report, v therefore + 1
output.column_count = strings.count(line, ",") + 1
count_stage = .BODY
}
case .BODY:
if len(line)>2 {
output.row_count += 1
}
}
}
output.info_lines = make([]Info_Line, output.info_line_count+output.column_count, context.temp_allocator)
output.header = make([]string, output.column_count, context.temp_allocator)
output.table = make([][]string, output.row_count, context.temp_allocator)
for &row in output.table {
row = make([]string, output.column_count, context.temp_allocator)
}
case .UNSET:
unreachable()
}
// STAGE 3 --------------------------------------------------------------
// Filling with data
when VERBOSE do fmt.printf("Struct before main parse:\n%#v\n", output)
first_channel_index := -1
last_channel_index := -1
stage : Stages = .TITLE
switch device {
case .UNSET:
fmt.eprintln("Uh-oh. This shouldn't happen. No device set when trying to fill in data.")
unreachable()
case .SD8:
// .d8888b. 8888888b. .d8888b.
// d88P Y88b 888 "Y88b d88P Y88b
// Y88b. 888 888 Y88b. d88P
// "Y888b. 888 888 "Y88888"
// "Y88b. 888 888 .d8P""Y8b.
// "888 888 888 888 888
// Y88b d88P 888 .d88P Y88b d88P
// "Y8888P" 8888888P" "Y8888P"
fmt.printf("Parsing [{}] as Sound Devices 8XX report, ", file_info.name)
if strings.contains(file_info.name, "_1.CSV") || strings.contains(file_info.name, "_2.CSV") {
output.title = file_info.name[7:len(file_info.name)-6]
} else {
output.title = file_info.name
}
fmt.printf("titled \"{}\".\n", output.title)
info_line_index := 0
body_line_index := 0
for line, line_index in lines {
switch stage {
case .TITLE:
// Missing newline on 8-series means we get info on the title line
stage = .INFO
line_elements := strings.split(line, ",")
when VERBOSE do fmt.printf(".INFO {}: {}\n", line_index, line_elements)
field := fmt.aprintf("{}:", line_elements[1], allocator=context.temp_allocator)
entry := line_elements[2]
output.info_lines[info_line_index].field = field
output.info_lines[info_line_index].entry = entry
info_line_index += 1
case .INFO:
if line == "," {
stage = .HEADER
continue
}
line_elements := strings.split(line, ",")
when VERBOSE do fmt.printf(".INFO {}: {}\n", line_index, line_elements)
if line_elements[0] == "Date" {
when VERBOSE do fmt.printf("Skipping line {}, because it's the retarded date field on an 8-series\n", line_index)
output.info_line_count -= 1
continue
}
field := fmt.aprintf("{}:", line_elements[0], allocator=context.temp_allocator)
entry := line_elements[1]
output.info_lines[info_line_index].field = field
output.info_lines[info_line_index].entry = entry
info_line_index += 1
case .HEADER:
if line == "," {
continue // This is here because there are a bunch of lines that are just commas before the header
} else if len(line)>3 {
when VERBOSE do fmt.printf(".HEADER {}:", line_index)
// No trailing comma in the header??
for element, e in strings.split(line, ",") {
when VERBOSE do fmt.printf(" {}", element)
output.header[e] = element
if element[:3] == "Trk" {
if first_channel_index == -1 do first_channel_index = e
last_channel_index = e
output.header[e] = fmt.aprintf("Trk {}", e-first_channel_index+1, allocator=context.temp_allocator)
}
if element == "Start TC" {
output.tc_column_index = e
}
}
when VERBOSE do fmt.printf("\n")
when VERBOSE do fmt.printf("first_channel_index: {}\n", first_channel_index)
when VERBOSE do fmt.printf("last_channel_index: {}\n", last_channel_index)
stage = .BODY
}
case .BODY:
if len(line) > 2 {
when VERBOSE do fmt.printf(".BODY {}:", line_index)
for element, e in strings.split(line, ",") {
when VERBOSE do fmt.printf(" {}", element)
entry : string = element
output.table[body_line_index][e] = entry
}
when VERBOSE do fmt.printf("\n")
body_line_index += 1
}
}
}
case .SD6:
// .d8888b. 8888888b. .d8888b.
// d88P Y88b 888 "Y88b d88P Y88b
// Y88b. 888 888 888
// "Y888b. 888 888 888d888b.
// "Y88b. 888 888 888P "Y88b
// "888 888 888 888 888
// Y88b d88P 888 .d88P Y88b d88P
// "Y8888P" 8888888P" "Y8888P"
fmt.printf("Parsing [{}] as Sound Devices 6XX report, ", file_info.name)
if file_info.name[len(file_info.name)-11:len(file_info.name)-3] == "_Report." {
output.title = file_info.name[:len(file_info.name)-11]
} else {
output.title = file_info.name
}
fmt.printf("titled \"{}\".\n", output.title)
info_line_index := 0
body_line_index := 0
for line, line_index in lines {
switch stage {
case .TITLE:
if line_index == 1 { // Ugly magic number, could be fucked by firmware update
stage = .INFO
}
case .INFO:
if line == "," {
stage = .HEADER
continue
}
line_elements := strings.split(line, ",")
when VERBOSE do fmt.printf(".INFO {}: {}\n", line_index, line_elements)
field := line_elements[0]
entry_raw := line_elements[1]
entry := line_elements[1][1:len(entry_raw)-1]
output.info_lines[info_line_index].field = field
output.info_lines[info_line_index].entry = entry
info_line_index += 1
case .HEADER:
if line == "," {
// This is here because there are a bunch of lines that are just commas before the header
} else if len(line)>3 {
when VERBOSE do fmt.printf(".HEADER {}:", line_index)
// No trailing comma in the header??
for element, e in strings.split(line, ",") {
when VERBOSE do fmt.printf(" {}", element)
output.header[e] = element
if element[:4] == "Trk " {
if first_channel_index == -1 do first_channel_index = e
last_channel_index = e
}
if element == "Start TC" {
output.tc_column_index = e
}
}
when VERBOSE do fmt.printf("\n")
} else if line == "" {
stage = .BODY
when VERBOSE do fmt.printf("first_channel_index: {}\n", first_channel_index)
when VERBOSE do fmt.printf("last_channel_index: {}\n", last_channel_index)
}
case .BODY:
if len(line) > 2 {
when VERBOSE do fmt.printf(".BODY {}:", line_index)
// to skip empty entry after trailing comma we do a silly slice
for element, e in strings.split(line, ",")[:output.column_count] {
when VERBOSE do fmt.printf(" {}", element)
entry : string = element
// Stripping quotes if after tracks begin
if e >= first_channel_index && (len(element)>0) {
entry = element[1:len(element)-1]
}
output.table[body_line_index][e] = entry
}
when VERBOSE do fmt.printf("\n")
body_line_index += 1
}
}
}
case .ZOOM:
// 8888888888 .d8888b.
// 888 d88P Y88b
// 888 Y88b. d88P
// 8888888 "Y88888"
// 888 .d8P""Y8b.
// 888 888 888
// 888 Y88b d88P
// 888 "Y8888P"
fmt.printf("Parsing [{}] as ZOOM report, ", file_info.name)
// Getting title
if file_info.name[:8] == "F8n Pro_" {
output.title = file_info.name[8:len(file_info.name)-4]
} else if file_info.name[:4] == "F8n_" { // I don't own one, so I don't know if this is what the F8n does
output.title = file_info.name[4:len(file_info.name)-4]
} else if file_info.name[:3] == "F8_" { // TODO: Verify this is what the original F8 does
output.title = file_info.name[4:len(file_info.name)-4]
} else {
output.title = file_info.name
}
fmt.printf("titled \"{}\".\n", output.title)
info_line_index := 0
body_line_index := 0
for line, line_index in lines {
switch stage {
case .TITLE:
if line_index == 1 { // Ugly magic number, could be fucked by firmware update
stage = .INFO
}
case .INFO:
if line == "" {
stage = .HEADER
continue
}
line_elements := strings.split(line, ",")
when VERBOSE do fmt.printf(".INFO {}: {}\n", line_index, line_elements)
field_raw := line_elements[0]
entry_raw := line_elements[1]
field := line_elements[0][1:len(field_raw)-1]
entry := line_elements[1][1:len(entry_raw)-1]
output.info_lines[info_line_index].field = field
output.info_lines[info_line_index].entry = entry
info_line_index += 1
case .HEADER:
when VERBOSE do fmt.printf(".HEADER {}:", line_index)
// to skip empty entry after trailing comma we do a silly slice
for element, e in strings.split(line, ",")[:output.column_count] {
when VERBOSE do fmt.printf(" {}", element)
output.header[e] = element[1:len(element)-1]
if element[:4] == "\"Tr " {
if first_channel_index==-1 do first_channel_index = e
last_channel_index = e
output.header[e] = fmt.aprintf("Trk {}", e-first_channel_index+1, allocator=context.temp_allocator)
}
if element == "\"Start TC\"" {
output.tc_column_index = e
}
}
when VERBOSE do fmt.printf("\n")
stage = .BODY
when VERBOSE do fmt.printf("first_channel_index: {}\n", first_channel_index)
when VERBOSE do fmt.printf("last_channel_index: {}\n", last_channel_index)
case .BODY:
if line == "" do break
when VERBOSE do fmt.printf(".BODY {}:", line_index)
// to skip empty entry after trailing comma we do a silly slice
for element, e in strings.split(line, ",")[:output.column_count] {
when VERBOSE do fmt.printf(" {}", element)
output.table[body_line_index][e] = element[1:len(element)-1]
}
when VERBOSE do fmt.printf("\n")
body_line_index += 1
}
}
}
// STAGE 4 --------------------------------------------------------------
// Cleanup!
when VERBOSE do fmt.printf("Struct before cleanup:\n%#v\n", output)
// Stacking tracks to the left
for &line, l in output.table {
stacking_index := first_channel_index
for &field, f in line[first_channel_index:last_channel_index+1] {
if field != "" {
line[stacking_index] = field
stacking_index += 1
}
}
for &field, f in line[stacking_index:last_channel_index+1] {
field = ""
}
}
// Cleaning out unused columns
touched := make([]bool, output.column_count, context.temp_allocator)
// Finding them
for line, l in output.table {
for field, f in line {
if touched[f] do continue
if field != "" {
touched[f] = true
}
}
}
// Turning unchanging columns into info line
changed := make([]bool, output.column_count, context.temp_allocator)
prev_line : []string = nil
for line, l in output.table {
if l>0 {
prev_line = output.table[l - 1]
for field, f in line {
if (prev_line[f] != field) ||
(first_channel_index <= f && f <= last_channel_index) ||
(f == output.tc_column_index) {
changed[f] = true
}
}
}
}
for did_change, i in changed {
if (!did_change) && touched[i] {
field := fmt.aprintf("{}: ", output.header[i], allocator=context.temp_allocator)
entry := prev_line[i]
output.info_lines[output.info_line_count] = {field=field, entry=entry}
output.info_line_count += 1
}
}
// Removing unused and static
for &line, l in output.table {
stacking_index := 0
for &field, f in line {
if touched[f] && changed[f] {
line[stacking_index] = field
stacking_index += 1
}
}
for &field, f in line[stacking_index:] {
field = ""
}
}
stacking_index := 0
for &field, f in output.header {
if touched[f] && changed[f] {
output.header[stacking_index] = field
stacking_index += 1
}
}
for &field, f in output.header[stacking_index:] {
field = ""
}
output.column_count = stacking_index
when VERBOSE do fmt.printf("Struct before output:\n%#v\n", output)
output.path = fmt.tprintf("{}/{}_Knekt_Lydrapport.html", filepath.dir(path), output.title)
return output, true
}
render :: proc(report : Report) {
// Now we output the HTML.
builder := strings.builder_make(context.temp_allocator)
strings.write_string(&builder, PART_ONE)
strings.write_string(&builder, report.title)
strings.write_string(&builder, " - Lydrapport")
strings.write_string(&builder, PART_TWO)
for line, l in report.info_lines[:report.info_line_count] {
strings.write_string(&builder, " <p><b>")
strings.write_string(&builder, line.field)
strings.write_string(&builder, "</b> ")
strings.write_string(&builder, line.entry)
strings.write_string(&builder, "</p>\n")
}
strings.write_string(&builder, " </div>\n </div>\n <table>\n <thead>\n <tr class=\"header-tr\">\n")
for field, f in report.header[:report.column_count] {
if f != report.tc_column_index {
strings.write_string(&builder, " <th>")
} else {
strings.write_string(&builder, " <th class=\"current-sort\">")
}
strings.write_string(&builder, field)
strings.write_string(&builder, "</th>\n")
}
strings.write_string(&builder, " </tr>\n </thead>\n <tbody>\n")
for line, l in report.table {
strings.write_string(&builder, " <tr>\n")
for field, f in line[:report.column_count] {
strings.write_string(&builder, " <td>")
strings.write_string(&builder, field)
strings.write_string(&builder, "</td>\n")
}
strings.write_string(&builder, " </tr>\n")
}
strings.write_string(&builder, PART_END)
output_text := strings.to_string(builder)
os.write_entire_file(report.path, transmute([]u8)output_text)
fmt.printf("Output: {}\n", report.path)
}
indent_by :: proc(i : int) {
for x in 0..<i {
fmt.printf(" ")
}
}
walk_directory :: proc(path : string, file_number : ^int, depth : int = 0) -> bool {
handle, ok := os.open(path)
if ok != os.ERROR_NONE {
indent_by(depth)
fmt.printf("ERROR opening dir: %s\n", path)
return false
}
defer os.close(handle)
files, okr := os.read_dir(handle, -1, context.temp_allocator)
if okr != os.ERROR_NONE {
indent_by(depth)
fmt.printf("ERROR [{}] reading dir: %s\n", okr, path)
if okr == os.ERROR_FILE_IS_NOT_DIR do return true
return true
}
wav_files : [dynamic]string
has_csv := false
for file in files {
full_path := file.fullpath
if file.is_dir {
indent_by(depth)
fmt.printf("š %s\n", file.name)
walk_directory(full_path, file_number, depth+1) // Recurse
} else { // If file is actually a file
extension := strings.to_lower(filepath.ext(file.name))
defer delete(extension)
if extension == ".csv" {
indent_by(depth)
fmt.printf("š [#%d] %s\n", file_number^, file.name)
append(&job_list, strings.clone(file.fullpath))
file_number^ += 1
has_csv = true
}
if extension == ".wav" {
append(&wav_files, strings.clone(full_path))
}
}
}
wav_count := len(wav_files)
if wav_count>0 && !has_csv {
indent_by(depth)
if wav_count == 1 {
fmt.printf("š½ [#%d] A WAV file.\n", file_number^)
} else {
fmt.printf("š½ [#%d] %d WAV files.\n", file_number^, wav_count)
}
append(&job_list, wav_files)
file_number^ += 1
}
return false
}
walk_directory_os2 :: proc(path : string, file_number : ^int, depth : int = 0) {
handle, ok := os2.open(path)
if ok != os2.ERROR_NONE {
indent_by(depth)
fmt.printf("ERROR opening dir: %s\n", path)
return
}
defer os2.close(handle)
files, okr := os2.read_dir(handle, -1, context.temp_allocator)
if okr != os2.ERROR_NONE {
indent_by(depth)
fmt.printf("ERROR [{}] reading dir: %s\n", okr, path)
return
}
wav_files : [dynamic]string
has_csv := false
for file in files {
full_path := file.fullpath
if os.is_dir(full_path) {
indent_by(depth)
fmt.printf("š %s\n", file.name)
walk_directory_os2(full_path, file_number, depth+1) // Recurse
} else { // If file is actually a file
extension := strings.to_lower(filepath.ext(file.name))
defer delete(extension)
if extension == ".csv" {
indent_by(depth)
fmt.printf("š [#%d] %s\n", file_number^, file.name)
append(&job_list, strings.clone(file.fullpath))
file_number^ += 1
has_csv = true
}
if extension == ".wav" {
append(&wav_files, strings.clone(full_path))
}
}
}
wav_count := len(wav_files)
if wav_count>0 && !has_csv {
indent_by(depth+1)
if wav_count == 1 {
fmt.printf("š½ [#%d] A WAV file.\n", file_number^)
} else {
fmt.printf("š½ [#%d] %d WAV files.\n", file_number^, wav_count)
}
append(&job_list, wav_files)
file_number^ += 1
}
}
|