-
Notifications
You must be signed in to change notification settings - Fork 0
/
tonic-decoder.js
229 lines (177 loc) · 5.27 KB
/
tonic-decoder.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
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
var string = "( ( S(x, A-4 Minor) * 4 ) / ( (4,4,2,2)*4 ) ) for x in ((0,2,4,0),(2,3,1,-3),(1,2,3,-1),(2,1,0,-4))";
var target = "0,4 3,4 7,2 0,2 0,4 3,4 7,2 0,2 0,4 3,4 7,2 0,2 0,4 3,4 7,2 0,2 3,4 5,4 2,2 -5,2 3,4 5,4 2,2 -5,2 3,4 5,4 2,2 -5,2 3,4 5,4 2,2 -5,2 2,4 3,4 5,2 -2,2 2,4 3,4 5,2 -2,2 2,4 3,4 5,2 -2,2 2,4 3,4 5,2 -2,2 3,4 2,4 0,2 -7,2 3,4 2,4 0,2 -7,2 3,4 2,4 0,2 -7,2 3,4 2,4 0,2 -7,2";
function decode(string) {
}
var decoded_string = decode(string);
// console.log(decoded_string == target);
// console.log(decoded_string);
//------------------------------------------------------------------------------
var _ND = function(n) {
this.value = n;
return this;
}
var ND = function(n) {
return new _ND(n);
}
function _NG() {
this.notes = [];
for (var i = 0; i < arguments.length; i++) {
var arg = arguments[i];
if (arg instanceof _NG) {
for (var j in arg.notes) {
this.notes.push(arg.notes[j]);
}
} else if (arg instanceof _ND) {
var v = [null, ND(arg.value)];
this.notes.push(v);
} else {
var v = [arg, ND(null)];
this.notes.push(v);
}
}
return this;
}
_NG.prototype.print = function() {
var s = '';
for (var i in this.notes) {
var n = this.notes[i];
s += (n[0] === null) ? 'N' : n[0];
s += ',';
s += (n[1].value === null) ? 'N' : n[1].value;
s += ' ';
}
s = s.trim();
return s;
}
_NG.prototype.toString = _NG.prototype.print;
_NG.prototype.add_note = function(n) {
this.notes.push(n);
}
_NG.prototype._DIVIDE = function(x) {
var ret_NG = new _NG();
if (!(x instanceof _NG) && !(x instanceof _ND)) {
return this;
} else {
if (x instanceof _NG) {
for (var i in this.notes) {
var n = this.notes[i];
var x_n = x.notes[clamp(i, 0, x.length-1)];
ret_NG.add_note([n[0], x_n[1]]);
}
} else if (x instanceof _ND) {
for (var i in this.notes) {
var n = this.notes[i];
ret_NG.add_note([n[0], x]);
}
}
}
return ret_NG;
}
_NG.prototype._ADD = function(x) {
var ret_NG = new _NG();
for (var i in this.notes) {
ret_NG.add_note(this.notes[i]);
}
if (x instanceof _NG) {
for (var i in x.notes) {
ret_NG.add_note(x.notes[i]);
}
}
return ret_NG;
}
_NG.prototype._MULTIPLY = function(x) {
var ret_NG = new _NG();
for (var i = 0; i < x; i++) {
for (var j in this.notes) {
ret_NG.add_note(this.notes[j]);
}
}
return ret_NG;
}
_ND.prototype._MULTIPLY = function(x) {
var ret_NG = new _NG();
for (var i = 0; i < x; i++) {
ret_NG.add_note([null, ND(this.value)]);
}
return ret_NG;
}
function NG() {
var args = [this];
args.push.apply(args, arguments);
return new (Function.prototype.bind.apply(_NG, args));
}
function DIVIDE(a, b) {
return a._DIVIDE(b);
}
function MULTIPLY(a, b) {
try {
return a._MULTIPLY(b);
}
catch (e) {
if (e instanceof TypeError) {
throw new Error(a.constructor.name + " has no _MULTIPLY() method");
}
else {
throw e;
}
}
}
function ADD(a, b) {
return a._ADD(b);
}
function Scale(g, scale) {
var ret_NG = new _NG();
for (var i in g.notes) {
var pitch = g.notes[i][0].toString();
if (pitch === null) { continue; }
// count up pitch modifiers
var offset = ((pitch.match(/#/g) || []).length) - ((pitch.match(/b/g) || []).length);
pitch = pitch.replace(/#/g, '');
pitch = pitch.replace(/b/g, '');
pitch = +pitch;
// calculate pitch from given note in scale
if (pitch >= 0) {
var index = pitch % scale.length;
} else {
var index = (scale.length - 1) + ((pitch + 1) % scale.length);
}
var scale_pitch = scale[index]; // get pitch from scale
scale_pitch += (Math.floor(pitch / scale.length)) * 12; // add octave
var final_pitch = scale_pitch + offset;
ret_NG.add_note([final_pitch, ND(g.notes[i][1].value)]);
}
return ret_NG;
}
//------------------------------------------------------------------------------
function myTonicFunc(s) {
var RET;
// console.log('s','"' + s + '"');
// var operators = ['+','/','(',')','*'];
// var op_positions = {};
// for (var i in operators) {
// var operator = operators[i];
// var positions = [];
// var start = 0;
// while (start < s.length) {
// var index = s.indexOf(operator, start);
// if (index != -1) {
// positions.push(index);
// start = index + 1;
// } else {
// start = s.length;
// }
// }
// op_positions[operator] = positions;
// }
// console.log('positions', op_positions);
eval(s);
console.log(RET.print());
return RET;
}
var a = ND(1);
var b = MULTIPLY( ND(2), 4 );
var c = MULTIPLY( ND(3), 4 );
var d = NG(0, "2#", 4, 0);
console.log(d.print());
//var z = myTonicFunc();
//console.log('z', z.print());