-
Notifications
You must be signed in to change notification settings - Fork 6
/
tolead.js
103 lines (95 loc) · 2.41 KB
/
tolead.js
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
// abc2svg - tolead.js - convert ABC to lead sheet
//
// Copyright (C) 2014-2018 Jean-Francois Moine
//
// This file is part of abc2svg.
//
// abc2svg is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// abc2svg is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with abc2svg. If not, see <http://www.gnu.org/licenses/>.
var BASE_LEN = 1536
function lead(tsfirst, voice_tb, music_types, info) {
var s, beat, cur_beat, i,
line = '';
function get_beat(s) {
if (s.a_meter[0].top[0] == 'C' || !s.a_meter[0].bot) {
beat = BASE_LEN / 4
} else {
beat = BASE_LEN / s.a_meter[0].bot[0] |0
if (isNaN(beat)) {
abc2svg.print('** Cannot get the beat')
return
}
if (s.a_meter[0].bot[0] == 8
&& s.a_meter[0].top[0] % 3 == 0)
beat = BASE_LEN / 8 * 3
}
} // get_beat()
abc2svg.print('-- ' + info.T + ' --');
// get the beat
get_beat(voice_tb[0].meter)
if (beat == undefined)
return
// treat only the first voice
cur_beat = 0
for (s = voice_tb[0].sym; s; s = s.next) {
while (s.time > cur_beat) {
line += '/ ';
cur_beat += beat
}
switch (music_types[s.type]) {
case 'note':
case 'rest':
if (s.a_gch) { // search a chord symbol
for (i = 0; i < s.a_gch.length; i++) {
if (s.a_gch[i].type == 'g') {
line += s.a_gch[i].text + ' ';
cur_beat = s.time + beat
break
}
}
}
break
case 'bar':
if (s.eoln) {
if (s.bar_type == '::')
line += ':|\n|: '
else
line += s.bar_type +'\n'
} else {
if (s.bar_type == '::')
line += ':|: '
else
line += s.bar_type + ' '
}
cur_beat = s.time // re-synchronize
break
case 'meter':
get_beat(s)
break
}
}
abc2svg.print(line)
}
// -- local functions
abc2svg.abort = function(e) {
abc2svg.abc_end()
abc2svg.print(e.message + "\n*** Abort ***\n" + e.stack);
abc2svg.quit()
}
abc2svg.abc_init = function() {
user.get_abcmodel = lead
}
abc2svg.abc_end = function() {
if (user.errtxt)
abc2svg.print("Errors:\n" + user.errtxt)
}