-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmidi2muscript
More file actions
executable file
·933 lines (864 loc) · 32.5 KB
/
midi2muscript
File metadata and controls
executable file
·933 lines (864 loc) · 32.5 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
#! /usr/local/bin/lua
local MIDI= require 'MIDI'
-- require 'DataDumper'
-- 20150511 priorities:
-- note-lengths, including notes lasting several bars
-- -t option (think 5/8 7/8 3/8)
-- preference for barlines on harmony-changes
-- preference for keeping the same barlength
-- reasonable beaming
-- beaming conforming to metronome beat
local Version = '1.7' -- handles note as last event and uses table.unpack
local VersionDate = '20180220'
--------- basic infrastructure that I put in almost everything ----------
local function warn(...) -- eg: warn('i=',i,' j=',j)
local str = ''
for i,v in ipairs{...} do str = str .. tostring(v) end
io.stderr:write(str,'\n') ; io.stderr:flush()
end
local function warning(s) warn('warning: ',s) end
local function die(str) io.stderr:write(str,'\n') ; os.exit(1) end
local function round(x) return math.floor(x+0.5) end
local function dict(a)
local d = {}
if a == nil then return d end
for k,v in ipairs(a) do d[v] = true end
return d
end
local function pairsByKeys(t,f) -- Programming in Lua p.173
local a = {}
for n in pairs(t) do a[#a+1] = n end
table.sort(a,f)
local i = 0
return function()
i = i + 1
return a[i], t[a[i]]
end
end
local function copy(t)
local new_table = {}
for k, v in pairs(t) do new_table[k] = v end
return new_table
end
local function deepcopy(object) -- http://lua-users.org/wiki/CopyTable
local lookup_table = {}
local function _copy(object)
if type(object) ~= "table" then
return object
elseif lookup_table[object] then
return lookup_table[object]
end
local new_table = {}
lookup_table[object] = new_table
for index, value in pairs(object) do
new_table[_copy(index)] = _copy(value)
end
return setmetatable(new_table, getmetatable(object))
end
return _copy(object)
end
--function str(t)
--local s = string.gsub(DataDumper(t), '^%s*return%s*', '')
--s= string.gsub(s, "%s*\n%s*", ' ')
--return s
--end
function slice(l,i1,i2)
local s = {}
local i = i1
while i <= i2 and i <= #l do
table.insert(s, l[i])
i = i+1
end
return s
end
------------------------- infrastructure --------------------
function wget(url) -- 4.4
local text = {}
local function WriteMemoryCallback(a,b)
-- luacurl and Lua-cURL friendly, see http://github.com/mkottman/wdm
local s ; if type(a) == "string" then s = a else s = b end
text[#text+1] = s
return string.len(s)
end
local c
if curl.new then c = curl.new() else c = curl.easy_init() end
c:setopt(curl.OPT_URL, url)
c:setopt(curl.OPT_WRITEFUNCTION, WriteMemoryCallback)
c:setopt(curl.OPT_USERAGENT, "luacurl-agent/1.0")
assert(c:perform())
if curl.close then c:close() end
return table.concat(text,'')
end
local UsingStdinAsAFile = false
local function file2millisec(filename)
-- global UsingStdinAsAFile
if filename == '-n' then return {1000,{}} end
local midi = ""
if filename == '-' then
if UsingStdinAsAFile then die("can't read STDIN twice") end
-- (sys.stdin.fileno(), 'rb') as fh: Should disable txtmode for dos
UsingStdinAsAFile = true
return MIDI.midi2ms_score(io.read('*all'))
end
if string.find('^|s*(.+)', filename) then
local command = string.match(filename, '^|s*(.+)')
local err_fn = os.tmpname()
local pipe = io.popen(command..' 2>'..err_fn, 'r') -- rb if windows
midi = pipe.read('*all') --XXX
err_fh = assert(file.open(err_fn))
local err_msg = err_fh.read('*all')
err_fh.close()
os.remove(err_fn)
--msg = pipe.stderr.read()
pipe.close()
status = pipe.wait()
if string.len(err_msg) > 1 then
die("can't run "..command..": "..err_msg)
end
return MIDI.midi2ms_score(midi)
end
if string.find(filename, '^[a-z]+:/') then -- 3.8
pcall(function() require 'curl' end)
if not curl then pcall(function() require 'luacurl' end) end
if not curl then
die([[you need to install lua-curl or luacurl, e.g.:
aptitude install liblua5.1-curl0 (or equivalent on non-debian sytems)
or, if that doesn't work:
luarocks install luacurl]])
end
local midi = wget(filename)
return MIDI.midi2ms_score(midi)
end
fh = assert(io.open(filename, "rb"))
local midi = fh:read('*all')
fh:close()
return MIDI.midi2ms_score(midi)
end
-- --------------------------main -----------------------------
-- command-line options:
local filename = '-'
local Barlength = 1.6
local iarg=1; while arg[iarg] ~= nil do
local argument = arg[iarg]
if argument == '-v' or argument == '--version' then
io.write('midi2muscript version '..Version..' '..VersionDate.."\n")
os.exit(0)
elseif argument == '-h' or argument == '--help' then
print_help()
os.exit(0)
-- aside from [a-z], - is a synonym for * (0 or more repetitions)
elseif argument == '-b' then
iarg = iarg + 1
Barlength = tonumber(arg[iarg])
else
filename = argument
break
end
iarg = iarg + 1
end
local score = MIDI.mix_scores({file2millisec(filename),}) -- reduce to 1 track
local track = score[2]
-- go through the score evaluating the weight of each note:
-- is_metronome + lowness + duration + velocity + duration*is_newchord
-- 20150518 BUT lowness is probably wrong if it's a solo recorder or whistle!
local weights = {} -- indexed by event-number
-- The score track is returned sorted by the end-times of the notes, so if
-- you need it sorted by their start-times you have to do that yourself:
table.sort(track, function (e1,e2) return e1[2]<e2[2] end)
for ievent,event in ipairs(track) do
if event[1] == 'note' then
local weight = 0
if event[4] == 9 then -- assign lots of weight to metronome bells
if event[5] == 34 then weight = 600 + event[6]
elseif event[5] == 33 then weight = 300 + event[6]
else weight = 100 + event[6]
end
else
weight = 127-event[5] + round(0.15*event[3]) + event[6]
if event[5] < 60 then
weight = weight - event[5]
else
weight = weight - 60
end
-- should keep track of newchords and add it in here...
end
weights[ievent] = weight
elseif event[1] == 'time_signature' then
weights[ievent] = 600
end
end
-- look for the times at which the next 10mS contain the most weight
local clumped_weights = {} -- indexed by event-number
for ievent,weight in pairs(weights) do
local event = track[ievent]
local i = ievent
local clumped_weight = 0
while track[i][2] < track[ievent][2] + 10 do
if weights[i] then
clumped_weight = clumped_weight + weights[i]
end
i = i + 1
if i > #track then break end -- 20180220
end
clumped_weights[ievent] = clumped_weight
end
-- construct a dict by clumped_weight of lists of ievents
local clumped_weight2ievents = {}
for ievent,clumped_weight in pairs(clumped_weights) do
if not clumped_weight2ievents[clumped_weight] then
clumped_weight2ievents[clumped_weight] = {ievent,}
else
table.insert(clumped_weight2ievents[clumped_weight], ievent)
end
end
-- and a list of its keys in descending order
local weights_in_order = {}
for clumped_weight,l in pairs(clumped_weight2ievents) do
table.insert(weights_in_order, clumped_weight)
end
table.sort(weights_in_order, function(a,b) return(a>b) end)
-- choose the weightiest as barlines,
-- then for each interval between them choose the weightiest as a barline,
-- then recurse until the bars are about the right length
-- then go through looking for bars that are double (or triple) the
-- length of their neighbouring bars, and subdivide them.
function barlength_goodness (len)
-- the goodness-of-length could take account of the "speed" of the piece
-- zero is not permissible! work in logs...
if len <= 0 then return 0 end
local dist = 7.6 - math.log(len)
local goodness = 127000 / (1000 + 2000*dist*dist)
return goodness
end
--for k,v in ipairs {1,10,500,1000,1500,2000,2500,3000,4000,4500} do
-- warn('barlength_goodness('..v..') = '..barlength_goodness(v))
--end
--- 20150509 but this forces the barlines to be on a note, no rests allowed
local barline_ievents = {}
local barline_times = {}
local max_number_of_barlines = round(track[#track][2]/(Barlength*700))
local number_of_barlines = 0
for i, weight in ipairs(weights_in_order) do
for j, ievent in ipairs(clumped_weight2ievents[weight]) do
local time = track[ievent][2]
-- is time too close to an old, more-strongly-known, barline ?
local is_too_close = false
for old_time, one in pairs(barline_times) do
if math.abs(time - old_time) < Barlength*650 then
is_too_close = true
break
end
end
if not is_too_close then
table.insert(barline_ievents, ievent)
barline_times[time] = 1
number_of_barlines = number_of_barlines + 1
if number_of_barlines > max_number_of_barlines then break end
end
end
end
table.sort(barline_ievents)
-- 20150509 put a dummy-event onto the end of track and barline it.
table.insert(track, {'text_event', track[#track][2]+300, ' '}) -- long note?
table.insert(barline_ievents, #track) --- the dummy-event
-- 20150510 if there's an upbeat, put a startline-barline at the 1st note
for i = 1, barline_ievents[1]-1 do
if track[i][1] == 'note' then
table.insert(barline_ievents, 1, i)
barline_times[track[i][1]] = 1
break
end
end
table.sort(barline_times)
-- Work out which stave(s) each patch&channel goes on.
-- We need to know the pitch-range of each patch&channel combination
-- in order to decide whether to give it 2 staves or not.
-- score2stats isn't detailed enough, so:
local channel2reigningpatch = {}
local channel2firstpatch = {}
local stave2reigningvol = {}
local stave2reigninglegato = {} -- not yet used
local patchchannel2stave = {} -- key is (patch*16 + cha)
local stave2clefchannel = {} -- val is {clef, cha}
local patchchannel2pitchrange = {} -- val is {lowest,highest}
for ievent,event in ipairs(track) do
if event[1] == 'patch_change' then
channel2reigningpatch[event[3]] = event[4]
if not channel2firstpatch[event[3]] then
channel2firstpatch[event[3]] = event[4]
end
elseif event[1] == 'note' then
local key = 16*(channel2reigningpatch[event[4]] or 0) + event[4]
local pitch = event[5]
if not patchchannel2pitchrange[key] then
patchchannel2pitchrange[key] = {pitch, pitch}
else
local range = patchchannel2pitchrange[key]
if pitch > range[2] then
range[2] = pitch
elseif pitch < range[1] then
range[1] = pitch
end
end
end
end
local patchchannels = {}
for patchchannel,pitchrange in pairs(patchchannel2pitchrange) do
table.insert(patchchannels, patchchannel)
end
table.sort(patchchannels)
local patchchannel2staveclef = {} -- val is {lowest,highest}
local next_free_istave = 1
local voice_patch = {[52]=true,[53]=true,[54]=true,[85]=true,[91]=true,}
local tenorclef_patch = {[42]=true,[57]=true,[70]=true,}
local stave_is_part_of_two = {}
for i,patchchannel in ipairs(patchchannels) do
local needs_two_staves = false
local range = patchchannel2pitchrange[patchchannel]
local channel = patchchannel % 16
local patch = math.floor(patchchannel/16)
local clef = 'treble'
-- detect viola and try alto, nylon-gtr=>treble8bvab, bass=>bass8vab
if patch == 41 and range[1]>46 and range[2]<80 then -- viola
clef = 'alto'
elseif patch>23 and patch<31 and range[1]>38 and range[2]<83 then -- gtr
clef = 'treble8vab'
elseif patch>31 and patch<40 and range[1]>26 and range[2]<63 then -- bass
clef = 'bass8vab'
elseif patch==72 and range[1]>69 then -- piccolo
clef = 'treble8va'
elseif tenorclef_patch[patch] and range[1]>49 and range[2]>74 then
clef = 'tenor'
elseif range[1]<53 and range[2]<66 then
clef = 'bass'
elseif voice_patch[patch] and range[1]<53 and range[2]<73 then --tenorvoice
clef = 'treble8vab'
elseif range[1]>71 and range[2]>84 then
clef = 'treble8va'
elseif range[1]<36 and range[2]<49 then
clef = 'bass8vab'
elseif range[1]<54 and range[2]>67 then
needs_two_staves = true
end
patchchannel2staveclef[patchchannel] = {}
if needs_two_staves then
patchchannel2staveclef[patchchannel] = {
next_free_istave, 'treble', next_free_istave+1, 'bass'
}
stave2clefchannel[next_free_istave] = {'treble', channel}
stave_is_part_of_two[next_free_istave] = true
stave2clefchannel[next_free_istave+1] = {'bass', channel}
stave_is_part_of_two[next_free_istave+1] = true
next_free_istave = next_free_istave + 2
else
patchchannel2staveclef[patchchannel] = {next_free_istave, clef}
stave2clefchannel[next_free_istave] = {clef, channel}
next_free_istave = next_free_istave + 1
end
end
-- generate muscript's "systems" command:
local n_staves = next_free_istave - 1
local n_systems = math.floor(13 / (n_staves))
local system_command = {}
for i,patchchannel in ipairs(patchchannels) do
local staveclef = patchchannel2staveclef[patchchannel]
if #staveclef == 4 then
table.insert(system_command, ' 19-25-19')
else
table.insert(system_command, ' 19')
end
end
system_command = table.concat(system_command, ' 25') .. '/'
local systems_command = tostring(n_systems) .. ' systems /' ..
string.rep(system_command, n_systems)
print(systems_command)
print("title "..filename.."\npagenum")
print("leftfoot Score extracted by midi2muscript "..Version)
print("# See: http://www.pjb.com.au/midi/midi2muscript.html")
for cha = 0,15 do
if channel2firstpatch[cha] then
print('midi channel '..cha..' patch '..channel2firstpatch[cha])
end
end
for istave = 1,n_staves do
stave2reigningvol[istave] = 100
stave2reigninglegato[istave] = 85
end
-- should at least take account of overall notes/bar ..
print("/\n4 bars | 48 | 48 | 48 | 48 |")
-- generate the muscript for each bar
local note2letter = {'C','C','D','E','E','F','F','G','G',
'A','B','B','c','c','d','e','e','f','f','g','g','a','b','b'}
local note2acc = {'','#','','b','','','#','','#','','b',''}
local previous_accidental = {} -- must reset at the beginning of each bar
function pedal2str(event)
local s,t,cha,cc,val = table.unpack(event)
if (s == "control_change") then
if (cc == 64) then
if (val > 63) then return "-P" else return "-*" end
elseif (cc == 66) then
if (val > 63) then return "-Sos" else return "-*Sos" end
end
end
return nil
end
function note2str(clef, event)
local s,t,dt,cha,note,vol = table.unpack(event)
if (s ~= 'note') then return '' end
if string.find(clef, '^treble') then
note = note - 24
elseif clef == 'alto' then
note = note - 12
end
if string.find(clef, '8vab$') then
note = note + 12
elseif string.find(clef, '8va$') then
note = note - 12
end
local octave = ''
if (note < 36) then
local o = math.floor((47-note) / 12)
octave = string.rep('_', o)
note = note + (12 * o)
elseif (note >= 60) then
local o = math.floor((note-48) / 12)
octave = string.rep('~', o)
note = note - (12 * o)
end
note = note - 36
-- 20150508 add 'n' if there has been a previous accidental on this letter
local acc = note2acc[1+note%12]
local n2l = note2letter[1+note%12]
if acc ~= '' then
previous_accidental[n2l] = acc
elseif acc=='' and previous_accidental[n2l] then
acc = 'n'
previous_accidental[n2l] = nil
end
return(note2letter[1+note%24]..octave..acc)
end
local one_over_log2 = 1.0 / math.log(2)
function log2(x)
return (one_over_log2*math.log(x))
end
function midibar2pulse(midibar, start_time, end_time)
-- decide on the npulses_per_bar, minimum say 70 mS,
-- which minimises sigma(off_pulse^2)
-- warn(DataDumper{midibar, start_time, end_time})
local barlength_ms = start_time - end_time
local last_note_t = start_time
local shortest_gt_70ms = 1000000 -- huge
for ievent,event in ipairs(midibar) do
-- pick shortest note-to-note dt > 70mS
-- and that's your first estimate of the pulse
if event[1] == 'note' then
local gap = event[2] - last_note_t
if gap<shortest_gt_70ms and gap>70 then shortest_gt_70ms=gap end
last_note_t = event[2]
end
end
local gap = end_time - last_note_t -- and from last note to end-of-bar
if (gap<shortest_gt_70ms) and (gap>70) then shortest_gt_70ms = gap end
local pulse_ms = shortest_gt_70ms
-- while true do
-- pick the next-shortest; is it 2.0* the dt ? 1.5* ? and so on
-- for ievent,event in ipairs(midibar) do
-- end
-- break
-- end
-- then adjust it to a submultiple of the bar-length
local n_pulses = round(barlength_ms/pulse_ms)
pulse_ms = barlength_ms / n_pulses
-- and choose smq,qua,cro or min to keep the cro between 1000 and 500 mS
-- (could align pulse with neighbouring bars, to keep bordeline cases sane)
local pulse_rhythm = 2 ^ (2 + round(log2(707.0/pulse_ms)))
return pulse_ms, tostring(pulse_rhythm)
end
function pulse_mul (pulse_rhythm, n)
if n <0.5 then return '' end
if n <1.2 then
if tonumber(pulse_rhythm) < 0.8 then return 'bre' end
return pulse_rhythm
end
-- should handle '163', '83', '43'
local log2_n = log2(n)
local floor_log2_n = math.floor(log2_n + 0.001)
local base_rhythm = tostring(tonumber(pulse_rhythm) / (2^floor_log2_n))
local log_ratio_to_base = log2_n - floor_log2_n
if base_rhythm == '0.5' then base_rhythm = 'bre' end
if log_ratio_to_base < 0.2 then
return(base_rhythm)
elseif log_ratio_to_base > 0.5 and log_ratio_to_base < 0.63 then
return(base_rhythm..'.')
elseif log_ratio_to_base > 0.75 and log_ratio_to_base < 0.86 then
return(base_rhythm..'..')
else
return(base_rhythm)
end
end
local firstbar = true
function midibar2muscript (track, start_index, end_index)
local midibar = deepcopy(slice(track, start_index, end_index))
local start_time = 0
if start_index > 0 then start_time = track[start_index][2] end
local end_time = track[end_index][2]
previous_accidental = {} -- must reset at the beginning of each bar
-- 1.6 ...
local plays_at_start = {} -- output these at beginning of bar
local plays_at_end = {} -- output these at end of bar
for ievent,event in ipairs(midibar) do
if event[1] == 'sysex_f0' then -- 1.6
local s=string.match(event[3],'}!play%s([%g%s]+)\xF7')
if s then
local t = event[2]
if math.abs(t-start_time) < math.abs(t-end_time) then
table.insert(plays_at_start, "play "..s)
else
table.insert(plays_at_end, "play "..s)
end
end
end
end
if #plays_at_start>0 then print(table.concat(plays_at_start,"\n")) end
local bar_duration = end_time - start_time -- temporarily in millisec
print('| '..string.format('%g', 0.001*bar_duration))
-- first choose beats, of about the right length,
-- to minimise their average (or total?) distance-to-weight
-- then choose sub-beats, of about the right length,
-- to minimise their average (or total?) distance-to-weight
--- should pulse_rhythm be set for the whole system, not just the stave ?
local pulse_ms,pulse_rhythm = midibar2pulse(midibar,start_time,end_time)
local istave2so_far = {}
local reigning_rhythm = {}
for ievent,event in ipairs(midibar) do -- quantise everything to the pulse
if ievent>1 then
event[2] = round((event[2]-start_time) / pulse_ms)
end
if event[1]=='note' then
event[3]=round(event[3]/pulse_ms) -- duration
if event[3]<1 then event[3] = 1 end
end
end
midibar[1][2] = midibar[1][2] - start_time
midibar[#midibar] = nil -- we no longer need the start of the next bar
bar_duration = round(bar_duration/pulse_ms) -- now in pulses
for istave = 1,n_staves do
if not stave2clefchannel[istave] then
print(DataDumper(stave2clefchannel))
end
local clef,channel = table.unpack(stave2clefchannel[istave])
-- should detect majority stemdir (perhaps even plausibly beam!)
local line_words = {'='..istave} -- ..','
-- we could append "," or "'" to linewords[1] much later :-)
if firstbar then
table.insert(line_words, clef)
table.insert(line_words, 'cha'..channel)
end
-- now the notes:
istave2so_far[istave] = 0 -- should distinguish =1' =1,
reigning_rhythm[istave] = ''
local function rest_at_start(ipulse) -- a rest needed before first note
if ipulse == 0 then return nil end
local rest_text = pulse_mul(pulse_rhythm, ipulse)
if rest_text == '' then return nil end
table.insert(line_words, rest_text)
reigning_rhythm[istave] = rest_text
table.insert(line_words, 'rest')
end
-- put notes starting at same time into the same <chord>
local function its_this_stave (event)
if stave_is_part_of_two[istave] then
if event[1] == 'note' then
return ((clef=='treble' and event[5]>59) or (
clef=='bass' and event[5]<60))
elseif(pedal2str(event)) then -- Ped marks go on the bass clef
if clef=='bass' then return true else return false end
else
return true
end
else
return true
end
end
local ipulse2notes = {} -- these notes need printing
local ipulse2pedals = {} -- these pedals will be appended to a note
local num_notes_in_bar = 0
for ievent,event in ipairs(midibar) do
local channelindex = MIDI.Event2channelindex[event[1]]
if channelindex and (event[channelindex] == channel) then
if its_this_stave(event) then
if event[1] == 'note' then
if not ipulse2notes[event[2]] then
ipulse2notes[event[2]] = {}
end
table.insert(ipulse2notes[event[2]], event)
num_notes_in_bar = num_notes_in_bar + 1
elseif pedal2str(event) then
if not ipulse2pedals[event[2]] then
ipulse2pedals[event[2]] = {}
end
table.insert(ipulse2pedals[event[2]], pedal2str(event))
end
end
end
end
local note_ipulses = {}
for n in pairs(ipulse2notes) do table.insert(note_ipulses,n) end
table.sort(note_ipulses)
local pedal_ipulses = {}
for n in pairs(ipulse2pedals) do table.insert(pedal_ipulses,n) end
table.sort(pedal_ipulses)
-- we loop through each pulse in the bar
-- and test 1) if there's a note and 2) if there's a pedal
-- Problem: the duration could be several bars, needing ties...
local no_note_yet = true
local pending_pedal_option = nil -- can be -P or -*
local pending_sosped_option = nil -- can be -Sos or -*Sos
local ii = 0
for ipulse = 0, bar_duration do
-- it's only a chord if there is more than one _note_
-- so we need a local chord_notes and a local chord_ccs
if ipulse2pedals[ipulse] then
for i,v in ipairs(ipulse2pedals[ipulse]) do
if string.match(v,'Sos$') then
pending_sosped_option = v
else
pending_pedal_option = v
end
end
end
local chord_notes = ipulse2notes[ipulse]
if chord_notes and #chord_notes > 0 then
local events = ipulse2notes[ipulse]
local event = events[1] -- handle chord of unequal lengths ?
if event[1] ~= 'note' then warn('event[1]=',event[1],' but note expected') end
if no_note_yet and rest_at_start(ipulse) then
no_note_yet = false
end
if math.abs(event[6]-stave2reigningvol[istave]) > 7 then
table.insert(line_words, 'vol'..tostring(event[6]))
stave2reigningvol[istave] = event[6]
end
ii = ii + 1
local next_ipulse = note_ipulses[ii+1] or bar_duration
local written_duration = event[3]
if written_duration > next_ipulse-event[2] then
written_duration = next_ipulse - event[2]
end
local duration_text = pulse_mul(pulse_rhythm,written_duration)
-- Timing in pulses is sad here for stave2reigninglegato[istave]
-- because it'll only allow me leg100, leg200, leg300 etc :-(
local rest_pulses = next_ipulse - event[2] - written_duration
-- Later step; if event[3] is definitely too long and overlaps,
-- then =1' and =1, could be introduced, assigned according
-- to their relative pitches: event[5] and next_event[5]
if duration_text ~= reigning_rhythm[istave] then
table.insert(line_words, duration_text)
reigning_rhythm[istave] = duration_text
end
if #chord_notes == 1 then
table.insert(line_words, note2str(clef,chord_notes[1]))
no_note_yet = false
elseif #chord_notes > 1 then
table.insert(line_words, '<'..note2str(clef,chord_notes[1]))
local i = 2
while i < #chord_notes do
table.insert(line_words, note2str(clef,chord_notes[i]))
i = i + 1
end
table.insert(line_words,
note2str(clef,chord_notes[#chord_notes])..'>')
no_note_yet = false
end
if rest_pulses > 0 then -- any rest after the note?
local rest_duration=pulse_mul(pulse_rhythm,rest_pulses)
if rest_duration ~= reigning_rhythm[istave] then
table.insert(line_words, rest_duration)
reigning_rhythm[istave] = rest_duration
end
table.insert(line_words, 'rest')
end
if pending_pedal_option and not no_note_yet then
line_words[#line_words] =
line_words[#line_words]..pending_pedal_option
pending_pedal_option = nil
end
if pending_sosped_option and not no_note_yet then
line_words[#line_words] =
line_words[#line_words] .. pending_sosped_option
pending_sosped_option = nil
end
end
end
print(table.concat(line_words, ' '))
end
if #plays_at_end>0 then print(table.concat(plays_at_end,"\n")) end
if firstbar then firstbar = false end
return ''
end -- thus ends function midibar2muscript() :-)
-- then, for each bar:
for ibar, barline_ievent in ipairs(barline_ievents) do
if ibar == #barline_ievents then break end
-- barline_ievents are the INDEXES, not the TIMES
midibar2muscript(track, barline_ievent, barline_ievents[ibar+1])
end
os.exit(0)
--[[
-- PLAN A was:
-- deem any note starting less than 0.05beat before the barline and
-- lasting till longer than 0.05beat after to be starting on the barline
-- deem any note finishing within 0.05beat after the next barline to
-- finish on the barline
-- deem anything within 0.05beat of a barline to happen on the barline
-- divide the events up into an array of bars and throw away the barlines
-- for each bar, for each channel ^=9, for each beat:
-- .2 .4 .6 .8 means quintuplets. Otherwise, choose the closest out of:
-- .125 .167 .250 .333 .375 .500 .625 .667 .750 .833 .875
-- .146 .208 .291 .354 .437 .563 .646 .709 .792 .854
-- More subtly: could prefer smq's if there's a qua, else prefer qua3's
-- if there's a qua,
-- if there's a smq, we force onto dsq's
-- else we decide between dsq smq dsq and smq3 smq3 smq3
-- else if there's a smq3, we force onto smq3's
-- Enough flexibility must remain to handle grace-notes sensibly
-- lengths of notes :-( identify rests or staccato
-- construct bars, an array of arrays {'| 2.4','=1 treble 4 G A', ... }
-- must decide whether to split a channel over 2 staves
-- add in the muscript system according to the detected channels
-- print the muscript to stdout
=pod
=head1 NAME
midi2muscript - Lua script to convert midi-files to muscript
=head1 SYNOPSIS
midi2muscript t.mid >t
midi2muscript t.mid | muscript >t.ps ; gv t.ps
=head1 DESCRIPTION
Each channel gets its own stave.
Other note_on and note_off events
are rounded to the nearest plausible subdivision of a beat.
If the improvisation has been made without a metronome-track,
overdubbing an improvisation with a metronome-track is not hard,
and can be repeated until satisfactory.
Then note_offs could also be interpreted,
to assign the notes to different voices,
e.g. if it's held, say, more than twice as long as the pulse.
=head1 SUPERSEDED
The following tries were aimed at measuring the tempo:
The delta-time of each event is assigned to a Bin
if it is within +/-20% (adjustable) of the bin-average.
Then it gets put into the bin, thereby influencing the average.
If it fits no bin, it starts a new one.
At the end, the bin-averages are fitted against the integer-multiples
(low whole-numbers), starting with the "shortest" (>0.1sec) as unity.
Or: could start with narrower bins,
then at the end amalgamate them into +/-20% bins,
separating them at the minima, or at the longest sequences of empty bins.
The binning should be re-evaluated every, say, beat;
and the bin-average should favour the more recent (the pulse may have changed!)
=head1 OPTIONS
=over 3
=item I<-b 1.96>
suggests a desirable B<b>arlength (in seconds; 1.96 seconds in this example)
which I<midi2muscript> will use as a guide when choosing plausible barlines.
=item I<-t 6/8>
suggests the B<t>ime-signature which I<midi2muscript> will use
when dividing the bar into shorter notes.
=item I<-v>
Prints version number.
=back
=head1 CHANGES
20150527 1.6 secret play sysex handled
20150518 1.5 appropriate vol commands extracted
20150517 1.4 pedal stuff approximately working
20150515 1.3 real note-durations; fixed important bug in midibar2pulse()
20150510 1.2 as released
20120829 1.1 first half-working header
20110426 1.0 first working version
=head1 AUTHOR
Peter J Billam http://www.pjb.com.au/comp/contact.html
=head1 SEE ALSO
http://www.pjb.com.au/
lua(1).
=cut
]]
-- see also 75% though midichord "code from an old midi2muscript",
-- see also harmony_pl "sub midi2figbas" in Perl
-- see also harmony "def midi2figbas" in Python
-- could use NumLua, or LuaFFT to get the pulse for each channel ?
-- Seeing as it doesn't have to be processed time-wise
-- (we have the whole file!), pick out the most obvious barlines (markers,
-- loudest notes, big chords, low long notes that change the harmony)
-- and then pick the less and less obvious barlines at suitable time-gaps
-- between them (perhaps inspired by a fourier analysis?);
-- we should be able to build up a plausible set of barlines.
-- Then, some similar heuristic should be able to pick beats within each bar
-- 20120723
-- Should be easy to assign each note its metric
-- then find the say 10mS periods which contain the biggest totals of metric
-- from ~/log/work at 20150430
-- THE ADVANTAGES OF WORKING DIRECTLY FROM THE MIDI:
-- * can split into channels
-- * can get the pitches without error
-- SO, go heuristic:
-- 1) bar-length
-- * find the most periodic channel
-- * convert channels to incremental pitch and find the most periodic
-- * convert channels to [UDud0] and find the most periodic
-- This period will have a simple-whole-number relationship to the bar-length
-- often 1, else usually a low multiple or submultiple
-- 2) beat-length and pulse-length
--
-- Could also search from the pulse-length upwards:
-- perhaps use Math.Evol to seek the quantisation-time (say 100-1000ms)
-- which least changes the score (or opus)
-- a problem with which is that
-- it doesn't handle a change-of-tempo in the middle.
-- but in the case of triplets, quintuplets etc ? we need the *beat* !
-- it's that low-multiple-or-submultiple problem again...
-- LuaFFT: fft (input, inverse) but the Homepage is trash :-(
-- Calculates the Fast Fourier Transformation of the given input
-- + input: A set of points that will be transformed. At this
-- point, the input has to be a list of complex numbers,
-- according to the format in complex.lua.
-- + inverse: Boolean that controls whether a transformation or
-- inverse transformation will be carried out.
-- Returns a list of complex numbers with the same size as the input
-- list. Contains the fourier transformation of the input.
-- NumLua looks more promising:
-- cd /tmp; git clone https://github.com/carvalho/numlua
-- file:///home/pjb/lua/NumLua_doc/index.html#matrix
-- matrix.fft(m [, inverse [, wisdomonly [, inplace]]])
-- Computes the fast Fourier transform of complex matrix m. If inverse
-- is true, computes the normalized inverse FFT. wisdomonly is a
-- FFTW-specific parameter: if true, a plan to compute the transform is
-- only created if wisdom is available for the given problem, that is,
-- a FFTW_WISDOM_ONLY flag is passed when building the plan. The default
-- flag is FFTW_ESTIMATE. If inplace is true, m is updated in-place.
-- See also: matrix.fct, fft.plan
-- matrix.fct(m [, inverse [, wisdomonly [, inplace]]])
-- Computes the discrete cosine transform of real matrix m. If inverse
-- is true, computes the normalized inverse DCT. wisdomonly is a
-- FFTW-specific parameter: if true, a plan to compute the transform is
-- only created if wisdom is available for the given problem, that is,
-- a FFTW_WISDOM_ONLY flag is passed when building the plan. The default
-- flag is FFTW_ESTIMATE. If inplace is true, m is updated in-place.
-- See also: matrix.fft, fft.plan
-- a = matrix({{1, 2, 3}, {0, -1, -4}}, true)
-- b = matrix(a:shape(1, true))
-- p = fft.plan(b, false, fft.flag["measure"]) -- direct transform
-- ip = fft.plan(b, true, fft.flag["measure"]) -- inverse transform
-- b._ = a
-- p(); print(b:pretty()) -- execute direct
-- 1+0i 1-1.7320508075689i 1+1.7320508075689i
-- 11+0i -4+3.4641016151378i -4-3.4641016151378i
-- ip(); print(b:pretty()) -- execute inverse
-- 1+0i 2+0i 3+0i
-- 0+0i -1+0i -4+0i
-- 20150502 OK: A PLAN:
-- take the score in millisec ticks, scale it to the next-lower power of two,
-- put each note, regardless of channel, into a bin,
-- use NumLua to get the FFT, look for peaks meaning pulse or beat or bar,
-- and rescale them back into milliseconds.
-- could do the same per-channel (one of them's probably keeping the beat!)
-- THEN: do the same on the reigning-chord-changes to pick out the barlines,
-- again doing the same per-channel and choose the clearest signal.