File size: 5,437 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
var { noteToMidi, midiToNote } = require('../../synth/note-to-midi');

/**
 * 
 * Note structure for Tabs
 * 
 */


function TabNote(note, clefTranspose) {
	//console.log("INIT/RENDER TabNote constructor")
	var pitch = noteToMidi(note)
	if (clefTranspose)
		pitch += clefTranspose
	var newNote = midiToNote(pitch);
	var isFlat = false;
	var isSharp = false;
	var isAltered = false;
	var natural = null;
	var quarter = null;
	var isDouble = false;
	var acc = 0;

	if (note.startsWith('_')) {
		isFlat = true;
		acc = -1;
		// check quarter flat
		if (note[1] == '/') {
			isFlat = false;
			quarter = "v";
			acc = 0;
		} else if (note[1] == '_') {
			// double flat
			isDouble = true;
			acc -= 1;
		}
	} else if (note.startsWith('^')) {
		isSharp = true;
		acc = +1;
		// check quarter sharp
		if (note[1] == '/') {
			isSharp = false;
			quarter = "^";
			acc = 0;
		} else if (note[1] == '^') {
			// double sharp
			isDouble = true;
			acc += 1;
		}
	} else if (note.startsWith('=')) {
		natural = true;
		acc = 0;
	}
	isAltered = isFlat || isSharp || (quarter != null);
	if (isAltered || natural) {
		if ((quarter != null) || (isDouble)) {
			newNote = note.slice(2);
		} else {
			newNote = note.slice(1);
		}
	}
	var hasComma = (newNote.match(/,/g) || []).length;
	var hasQuote = (newNote.match(/'/g) || []).length;

	this.pitch = pitch
	this.pitchAltered = 0
	this.name = newNote;
	this.acc = acc;
	this.isSharp = isSharp;
	this.isKeySharp = false;
	this.isDouble = isDouble;
	this.isAltered = isAltered;
	this.isFlat = isFlat;
	this.isKeyFlat = false;
	this.natural = natural;
	this.quarter = quarter;
	this.isLower = (this.name == this.name.toLowerCase());
	this.name = this.name[0].toUpperCase();
	this.hasComma = hasComma;
	this.isQuoted = hasQuote;
}

function cloneNote(self) {
	var newNote = self.name;
	var newTabNote = new TabNote(newNote);
	newTabNote.pitch = self.pitch;
	newTabNote.hasComma = self.hasComma;
	newTabNote.isLower = self.isLower;
	newTabNote.isQuoted = self.isQuoted;
	newTabNote.isSharp = self.isSharp;
	newTabNote.isKeySharp = self.isKeySharp;
	newTabNote.isFlat = self.isFlat;
	newTabNote.isKeyFlat = self.isKeyFlat;
	return newTabNote;
}
TabNote.prototype.sameNoteAs = function (note) {
	//console.log("INIT TabNote sameNoteAs")
	return note.pitch === this.pitch
};

TabNote.prototype.isLowerThan = function (note) {
	//console.log("INIT TabNote isLowerThan")
	return note.pitch > this.pitch
};

TabNote.prototype.checkKeyAccidentals = function (accidentals, measureAccidentals) {
	//console.log("RENDER TabNote checkKeyAccidentals")
	if (this.isAltered || this.natural)
		return
	if (measureAccidentals[this.name.toUpperCase()]) {
		switch (measureAccidentals[this.name.toUpperCase()]) {
			case "__": this.acc = -2; this.pitchAltered = -2; return;
			case "_": this.acc = -1; this.pitchAltered = -1; return;
			case "=": this.acc = 0; this.pitchAltered = 0; return;
			case "^": this.acc = 1; this.pitchAltered = 1; return;
			case "^^": this.acc = 2; this.pitchAltered = 2; return;
		}
	} else if (accidentals) {
		var curNote = this.name;
		for (var iii = 0; iii < accidentals.length; iii++) {
			var curAccidentals = accidentals[iii];
			if (curNote == curAccidentals.note.toUpperCase()) {
				if (curAccidentals.acc == 'flat') {
					this.acc = -1;
					this.isKeyFlat = true;
					this.pitchAltered = -1
				}
				if (curAccidentals.acc == 'sharp') {
					this.acc = +1;
					this.isKeySharp = true;
					this.pitchAltered = 1
				}
			}
		}
	}
};

TabNote.prototype.getAccidentalEquiv = function () {
	//console.log("TabNote getAccidentalEquiv")
	var cloned = cloneNote(this);
	if (cloned.isSharp || cloned.isKeySharp) {
		cloned = cloned.nextNote();
		cloned.isFlat = true;
		cloned.isSharp = false;
		cloned.isKeySharp = false;
	} else if (cloned.isFlat || cloned.isKeyFlat) {
		cloned = cloned.prevNote();
		cloned.isSharp = true;
		cloned.isFlat = false;
		cloned.isKeyFlat = false;
	}
	return cloned;
};


TabNote.prototype.nextNote = function () {
	//console.log("INIT TabNote nextNote")
	var note = midiToNote(this.pitch + 1 + this.pitchAltered)
	return new TabNote(note)
};

TabNote.prototype.prevNote = function () {
	//console.log("TabNote prevNote")
	var note = midiToNote(this.pitch - 1 + this.pitchAltered)
	return new TabNote(note)
};

TabNote.prototype.emitNoAccidentals = function () {
	//console.log("TabNote emitNoAccidentals")
	var returned = this.name;
	if (this.isLower) {
		returned = returned.toLowerCase();
	}
	for (var ii = 0; ii < this.isQuoted; ii++) {
		returned += "'";
	}
	for (var jj = 0; jj < this.hasComma; jj++) {
		returned += ",";
	}
	return returned;
};

TabNote.prototype.emit = function () {
	//console.log("INIT/RENDER TabNote emit")
	var returned = this.name;
	if (this.isSharp || this.isKeySharp) {
		returned = '^' + returned;
		if (this.isDouble) {
			returned = '^' + returned;
		}
	}
	if (this.isFlat || this.isKeyFlat) {
		returned = '_' + returned;
		if (this.isDouble) {
			returned = '_' + returned;
		}
	}
	if (this.quarter) {
		if (this.quarter == "^") {
			returned = "^/" + returned;
		} else {
			returned = "_/" + returned;
		}
	}
	if (this.natural) {
		returned = '=' + returned;
	}
	for (var ii = 1; ii <= this.hasComma; ii++) {
		returned += ',';
	}

	if (this.isLower) {
		returned = returned.toLowerCase();
		for (var jj = 1; jj <= this.isQuoted; jj++) {
			returned += "'";
		}
	}
	return returned;
};

module.exports = TabNote