«parser for (simplified) abc score notation» by grirgz

on 02 Jul'12 19:12 in parser abc score notation

A simple parser for the (simplified) abc score notation. http://abcnotation.com/wiki/abc:standard:v2.0#the_tune_body

Features: - 4 octaves : C, C c c' - note dur: c2 c/ c/2 - rest: can use _ in addition to z and x - chords: [ceg] - spaces and others symbols are ignored

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
(
~notedef = Dictionary.newFrom(("CDEFGABcdefgab".as(Array).collect { arg ch, idx; [ch, idx-7] }).flat);
~notedef[$_] = \r;
~notedef[$x] = \r;
~notedef[$z] = \r;
~digitdef = Dictionary.newFrom(("123456789".as(Array).collect { arg ch, idx; [ch, idx+1] }).flat);

~is_terminal = { arg char;
	~notedef[char].notNil or: { [ $[, $] ].includes(char) }
};

~strip_spaces = { arg str;
	str.replace(" ", "")
};

~split_string = { arg str;
	var res = List.new;
	var accu = "";
	str.do { arg char;
		if( ~is_terminal.(char) ) {
			if(accu != "") {
				res.add(accu);
			};
			accu = "" ++ char;
		} {
			accu = accu ++ char;
		}
	};
	res.add(accu);
	res;
};

~extract_chords = { arg token_list;
	var res = List.new;
	var accu = List.new;
	var chord = false;
	token_list.do { arg token;
		if( chord ) {
			if( token == "]") {
				chord = false;
				res.add(accu);
				accu = List.new;
			} {
				accu.add(token)
			};
		} {
			if( token == "[") {
				chord = true;
			} {
				res.add(token)
			};
		};
	};
	//res.add(token);
	res;
};

~parse_note = { arg token;
	var pitch;
	var len = 1;
	var char;
	var divide = false;
	token = token.as(List).reverse;
	pitch = token.pop;
	pitch = ~notedef[pitch];
	while({ token.isEmpty.not }) {
		char = token.pop;	
		case
			{ char == $' } {
				pitch = pitch + 14;
			}
			{ char == $, } {
				pitch = pitch - 14;
			}
			{ char == $/ } {
				len = 1/2;
				divide = true;
			}
			{ ~digitdef[char].notNil } {
				if(divide) {
					len = 1 / ~digitdef[char];
				} {
					len = ~digitdef[char];
				}
			}
		
	};
	[pitch, len]
};

~parse_sentence = { arg token_list;
	var res_pitch = List.new, res_len = List.new;
	var pitch, len;
	token_list.do { arg token;
		if(token.isString) {
			#pitch, len = ~parse_note.(token);
			res_pitch.add(pitch);
			res_len.add(len);
		} {
			#pitch, len = ~parse_sentence.(token);
			//len = len.maxItem; // workaround to the multichannel expand bug
			res_pitch.add(pitch);
			res_len.add(len);
		}
	};
	[res_pitch, res_len]
};
		
~parseabc = { arg str;
	var res;
	res = ~strip_spaces.(str);
	res = ~split_string.(res);
	res = ~extract_chords.(res);
	res = ~parse_sentence.(res);
	res;
};

~add_legato = { arg dur;
	var legato = List.new;
	var maxi;
	dur = dur.collect { arg du;
		if(du.isSequenceableCollection) {
			maxi = du.maxItem;
			legato.add( du.collect { arg leg;
				leg / maxi
			});
			maxi;
		} {
			legato.add(1);
			du;
		}
	};
	[dur, legato]
};

~abc_to_pbind = { arg str, repeat=inf;
	var res, dur, legato;
	res = ~parseabc.(str);
	#dur, legato = ~add_legato.(res[1]);
	Pbind(
		\degree, Pseq(res[0],repeat),
		\legato, Pkey(\legato) * Pseq(legato,repeat),
		\dur, Pseq(dur,repeat)
	)
};
)

// usage example

(Pbind(\amp, 1, \stretch, 1/8) <> ~abc_to_pbind.("[c4e4g4][d4f4h4]cdef_2c_[c4e4g4][d4fh]cdec",1)).play;
raw 2939 chars (focus & ctrl+a+c to copy)
comments