File size: 7,661 Bytes
af6912c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
const { noteToMidi } = require('../../synth/note-to-midi');
var TabNote = require('./tab-note');
var tabNotes = require('./tab-notes');


function buildCapo(self) {
	var capoTuning = null;
	var tuning = self.tuning;
	if (self.capo > 0) {
		capoTuning = [];
		for (var iii = 0; iii < tuning.length; iii++) {
			var curNote = new TabNote(tuning[iii]);
			for (var jjj = 0; jjj < self.capo; jjj++) {
				curNote = curNote.nextNote();
			}
			capoTuning[iii] = curNote.emit();
		}
	}
	return capoTuning;
}

function buildPatterns(self) {
	var strings = [];
	var tuning = self.tuning;
	if (self.capo > 0) {
		tuning = self.capoTuning;
	}
	var pos = tuning.length - 1;
	for (var iii = 0; iii < tuning.length; iii++) {
		var nextNote = self.highestNote; // highest handled note
		if (iii != tuning.length - 1) {
			nextNote = tuning[iii + 1];
		}
		var stringNotes = tabNotes(tuning[iii], nextNote);
		if (stringNotes.error) {
			return stringNotes;
		}
		strings[pos--] = stringNotes;
	}
	return strings;
}


function buildSecond(first) {
	var seconds = [];
	seconds[0] = [];
	var strings = first.strings;
	for (var iii = 1; iii < strings.length; iii++) {
		seconds[iii] = strings[iii - 1];
	}
	return seconds;
}

function sameString(self, chord) {
	for (var jjjj = 0; jjjj < chord.length - 1; jjjj++) {
		var curPos = chord[jjjj];
		var nextPos = chord[jjjj + 1];
		if (curPos.str == nextPos.str) {
			// same String
			// => change lower pos 
			if (curPos.str == self.strings.length - 1) {
				// Invalid tab Chord position for instrument
				curPos.num = "?";
				nextPos.num = "?";
				return;
			}
			// change lower pitch on lowest string
			if (nextPos.num < curPos.num) {
				nextPos.str++;
				nextPos = noteToNumber(self,
					nextPos.note,
					nextPos.str,
					self.secondPos,
					self.strings[nextPos.str].length
				);
			} else {
				curPos.str++;
				curPos = noteToNumber(self,
					curPos.note,
					curPos.str,
					self.secondPos,
					self.strings[curPos.str].length
				);
			}
			// update table
			chord[jjjj] = curPos;
			chord[jjjj + 1] = nextPos;
		}
	}
	return null;
}

function handleChordNotes(self, notes) {
	var retNotes = [];
	for (var iiii = 0; iiii < notes.length; iiii++) {
		if (notes[iiii].endTie)
			continue;
		var note = new TabNote(notes[iiii].name, self.clefTranspose);
		note.checkKeyAccidentals(self.accidentals, self.measureAccidentals)
		var curPos = toNumber(self, note);
		retNotes.push(curPos);
	}
	sameString(self, retNotes);
	return retNotes;
}

function noteToNumber(self, note, stringNumber, secondPosition, firstSize) {
	var strings = self.strings;
	note.checkKeyAccidentals(self.accidentals, self.measureAccidentals);
	if (secondPosition) {
		strings = secondPosition;
	}
	var noteName = note.emitNoAccidentals();
	var num = strings[stringNumber].indexOf(noteName);
	var acc = note.acc;
	if (num != -1) {
		if (secondPosition) {
			num += firstSize;
		}
		if ((note.isFlat || note.acc == -1) && (num == 0)) {
			// flat on 0 pos => previous string 7th position
			var noteEquiv = note.getAccidentalEquiv();
			stringNumber++;
			num = strings[stringNumber].indexOf(noteEquiv.emit());
			acc = 0;
		}
		return {
			num: (num + acc),
			str: stringNumber,
			note: note
		};
	}
	return null;
}

function toNumber(self, note) {
	if (note.isAltered || note.natural) {
		var acc;
		if (note.isFlat) {
			if (note.isDouble)
				acc = "__"
			else
				acc = "_"
		} else if (note.isSharp) {
			if (note.isDouble)
				acc = "^^"
			else
				acc = "^"
		} else if (note.natural)
			acc = "="
		self.measureAccidentals[note.name.toUpperCase()] = acc
	}
	for (var i = self.stringPitches.length - 1; i >= 0; i--) {
		if (note.pitch + note.pitchAltered >= self.stringPitches[i]) {
			var num = note.pitch + note.pitchAltered - self.stringPitches[i]
			if (note.quarter === '^') num -= 0.5
			else if (note.quarter === "v") num += 0.5
			return {
				num: Math.round(num),
				str: self.stringPitches.length - 1 - i, // reverse the strings because string 0 is on the bottom
				note: note
			}
		}
	}
	return {
		num: "?",
		str: self.stringPitches.length - 1,
		note: note,
	};
}

StringPatterns.prototype.stringToPitch = function (stringNumber) {
	var startingPitch = 5.3;
	var bottom = this.strings.length - 1;
	return startingPitch + ((bottom - stringNumber) * this.linePitch);
};

function invalidNumber(retNotes, note) {
	var number = {
		num: "?",
		str: 0,
		note: note
	};
	retNotes.push(number);
	retNotes.error = note.emit() + ': unexpected note for instrument';
}

StringPatterns.prototype.notesToNumber = function (notes, graces) {
	var note;
	var number;
	var error = null;
	var retNotes = null;
	if (notes) {
		retNotes = [];
		if (notes.length > 1) {
			retNotes = handleChordNotes(this, notes);
			if (retNotes.error) {
				error = retNotes.error;
			}
		} else {
			if (!notes[0].endTie) {
				note = new TabNote(notes[0].name, this.clefTranspose);
				note.checkKeyAccidentals(this.accidentals, this.measureAccidentals)
				number = toNumber(this, note);
				if (number) {
					retNotes.push(number);
				} else {
					invalidNumber(retNotes, note);
					error = retNotes.error;
				}
			}
		}
	}
	if (error) return retNotes;
	var retGraces = null;
	if (graces) {
		retGraces = [];
		for (var iiii = 0; iiii < graces.length; iiii++) {
			note = new TabNote(graces[iiii].name, this.clefTranspose);
			note.checkKeyAccidentals(this.accidentals, this.measureAccidentals)
			number = toNumber(this, note);
			if (number) {
				retGraces.push(number);
			} else {
				invalidNumber(retGraces, note);
				error = retNotes.error;
			}
		}
	}

	return {
		notes: retNotes,
		graces: retGraces,
		error: error
	};
};

StringPatterns.prototype.toString = function () {
	var arr = []
	for (var i = 0; i < this.tuning.length; i++) {
		var str = this.tuning[i].replaceAll(',', '').replaceAll("'", '').toUpperCase();
		if (str[0] === '_') str = str[1] + 'b '
		else if (str[0] === '^') str = str[1] + "# "
		arr.push(str)
	}
	return arr.join('');
};

StringPatterns.prototype.tabInfos = function (plugin) {
	var name = plugin.params.label;
	if (name) {
		var tunePos = name.indexOf('%T');
		var tuning = "";
		if (tunePos != -1) {
			tuning = this.toString();
			if (plugin.capo > 0) {
				tuning += ' capo:' + plugin.capo;
			}
			name = name.replace('%T', tuning);
		}
		return name;
	}
	return '';
};

// MAE 27 Nov 2023
StringPatterns.prototype.suppress = function (plugin) {
	var suppress = plugin.params.suppress;
	if (suppress) {
		return true;
	}
	return false;
};
// MAE 27 Nov 2023 End

/**
 * Common patterns for all string instruments
 * @param {} plugin
 * @param {} tuning
 * @param {*} capo
 * @param {*} highestNote 
 */
function StringPatterns(plugin) {
	//console.log("INIT StringPatterns constructor")
	var tuning = plugin.tuning;
	var capo = plugin.capo;
	var highestNote = plugin.params.highestNote;
	this.linePitch = plugin.linePitch;
	this.highestNote = "a'";
	if (highestNote) {
		// override default
		this.highestNote = highestNote;
	}
	this.measureAccidentals = {}
	this.capo = 0;
	if (capo) {
		this.capo = parseInt(capo, 10);
	}
	this.transpose = plugin.transpose ? plugin.transpose : 0
	this.tuning = tuning;
	this.stringPitches = []
	for (var i = 0; i < this.tuning.length; i++) {
		var pitch = noteToMidi(this.tuning[i]) + this.capo
		this.stringPitches.push(pitch)
	}
	if (this.capo > 0) {
		this.capoTuning = buildCapo(this);
	}
	this.strings = buildPatterns(this);
	if (this.strings.error) {
		plugin.setError(this.strings.error);
		plugin.inError = true;
		return;
	}
	// second position pattern per string
	this.secondPos = buildSecond(this);
}



module.exports = StringPatterns;