-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup.js
396 lines (335 loc) · 11.4 KB
/
setup.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
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
/* function Song(){
this.title = ''
this.artist = ''
this.author = ''
this.content = [
{
this.sectionname = 'Verse',
this.objects = [
{word: 'Hello', chord:'A'}
,{word: 'World', chord:'Em'}
,{word:'how', chord:'G'}
,{word: 'are', chord:'C'}
]
},
{
this.sectionname = 'Chorus',
this.objects = [
{word:'Chorus', chord:'G'}
{word:'here.', chord:'Am'}
]
}
]
}
function Section(name){
this.sectionname = name;
this.objects = [];
}
*/
/* */
function Song(){
this.chords = [];
this.tempchords = {};
this.content = [];
this.sections = [];
};
/*
/*
Helper classes
*
*/
function Chord(name){
this.name = name;
};
function LyriChord(object){
for(var prop in object){
this[prop] = object[prop];
};
};
function Section(sectionobject){
if(sectionobject.name){ this.name = sectionobject.name }
this.begin = sectionobject.begin;
this.end = sectionobject.end;
this.chordsUnique = sectionobject.chordsUnique;
this.chordsInOrder = sectionobject.chordsInOrder;
};
/*
/*
Internal functions
*
*/
Song.prototype.addLyriChord = function(lyrichord){
var lyric = lyrichord.lyric || '';
var lastInLine = lyrichord.lastInLine || false;
var object = {};
for(var prop in lyrichord){
if(prop!='chord') { object[prop] = lyrichord[prop] };
};
if (!lyrichord.chord){ this.content.push(new LyriChord(object)); return this.content.length-1; }
else {
var chordIndex = this.addChord(lyrichord.chord);
object.chordIndex = chordIndex;
this.content.push(new LyriChord(object));
return this.content.length-1;
}
};
Song.prototype.updateLyriChord = function(objectupdate){
var index = objectupdate.index;
//update only if specified
if(objectupdate.lyric){ this.content[index].lyric = objectupdate.lyric };
if(objectupdate.chord){ this.content[index].chordIndex = this.addChord(objectupdate.chord); }
// console.log('updated', this);
}
Song.prototype.addChord = function(chord){
var found = false;
for(var c=0;c<=this.chords.length;c++){
if (c===this.chords.length && found===false){
chord = chord.charAt(0).toUpperCase() + chord.slice(1);
this.chords.push(new Chord(chord));
// console.log('new chord at ' + c, this.chords)
return c;
}
else if (this.chords[c].name.toLowerCase() === chord.toLowerCase()){
found = true;
// console.log('found chord at ' +c);
return c;
}
}
}
Song.prototype.addSection = function(sectionobject){
var object = sectionobject;
var chordsUniqueArray = [];
var chordsInOrder = [];
for(var c=sectionobject.begin;c<=sectionobject.end;c++){
var hasChord = false;
if(this.content[c].chordIndex===0){ hasChord = true }
if(this.content[c].chordIndex || hasChord){
if(chordsUniqueArray.indexOf(this.content[c].chordIndex) === -1){ chordsUniqueArray.push(this.content[c].chordIndex) };
chordsInOrder.push(this.content[c].chordIndex);
}
}
object.chordsUnique = chordsUniqueArray; //dynamically create the chordArray from the given sections constructor w/ beginning and end
object.chordsInOrder = chordsInOrder;
this.sections.push(new Section(object));
// console.log(this);
};
Song.prototype.lookupSection = function(sectionname){
var found = false;
for(var s=0;s<this.sections.length;s++){
if (this.sections[s].name === sectionname){ return this.sections[s]; found = true; }
}
if(!found){ return false };
};
Song.prototype.sectionObjects = function(sectionname){
var section = this.lookupSection(sectionname);
if(section){
var arrayObjects = [];
for(var i=section.begin;i<=section.end;i++){
arrayObjects.push(this.content[i]);
}
return arrayObjects;
}
else{ return false };
}
Song.prototype.sectionChordsInOrder = function(sectionname){
var section = this.lookupSection(sectionname);
if(section){
var arrayChords = [];
for(var c=0;c<section.chordsInOrder.length;c++){
arrayChords.push(this.chords[section.chordsInOrder[c]].name);
}
return arrayChords;
}
else{ return false };
}
Song.prototype.sectionChordsUnique = function(sectionname){
var section = this.lookupSection(sectionname);
if(section){
var arrayChords = [];
for(var c=0;c<section.chordsUnique.length;c++){
arrayChords.push(this.chords[section.chordsUnique[c]].name);
}
return arrayChords;
}
else{ return false };
}
Song.prototype.transposeChords = function(halfsteps){
console.log('original: ', this.chords);
var newChords = [];
for(var t=0;t<this.chords.length;t++){
newChords.push(new Chord(this.transpose(this.chords[t].name,halfsteps)));
}
var temp = {};
temp['tempchords'] = newChords;
temp['transposed'] = halfsteps;
this.tempchords = temp;
return this.tempchords;
}
Song.prototype.transpose = function(chordname,halfsteps){ // should replace this with a linked list
var steps = halfsteps;
if(steps<0){ steps = 12-Math.abs(steps) }; //account for if steps are negative
var chord = chordname[0].toUpperCase() + chordname.slice(1);
var result = ''
while(steps > 0){
if(chord[1]==='#'){
chord = chord.slice(0,1) + chord.slice(2);
switch(chord[0]){
case 'C': chord = 'D'+chord.slice(1); break;
case 'D': chord = 'E'+chord.slice(1); break;
case 'F': chord = 'G'+chord.slice(1); break;
case 'G': chord = 'A'+chord.slice(1); break;
case 'A': chord = 'B'+chord.slice(1); break;
}
}
else if(chord[1]!='#'){
switch(chord[0]){
case 'C':
case 'D':
case 'F':
case 'G':
case 'A':
chord = chord[0]+'#'+chord.slice(1);
break;
case 'E':
chord = 'F'+chord.slice(1);
break;
case 'B':
chord = 'C'+chord.slice(1);
break;
}
}
steps--;
}
return chord;
}
/*
/*
Conversion functions
*
*/
String.prototype.contains = function(val){ //helper function
if(this.indexOf(val) >= 0){return true; }
else{return false;};
};
Song.prototype.generateFromMarkdown = function(markdown){
this.markdown = markdown;
var sections = markdown.split(/(\*[a-z|A-Z|\s|\d|-]+\*)/); //split by asterisk headers, and store them in the array
sections = sections.filter(function(n){return n;}); //filter out empty, how this works i do not know
// console.log('THE SECTIONS',sections);
var sectionName='';
for(var s=0;s<sections.length;s++){
if(sections[s].contains('*')){ sectionName = sections[s].replace("*","").replace("*","") }
else{
this.generateSectionFromMarkdown(sections[s],sectionName);
};
};
return this;
};
Song.prototype.generateSectionFromMarkdown = function(section,sectionName){
var sectionobject = {};
sectionobject.name = sectionName;
sectionobject.begin = this.content.length;
var subsections = section.split(/\n\n|\r\r|\r\n\r\n/g);
for(var b=0;b<subsections.length;b++){
var lines = subsections[b].split(/\r\n|\r|\n/g); //should also split by two \n , signifying a new subsection similar to <p>
lines = lines.filter(function(n){return n;});
for(var i=0;i<lines.length;i++){
var line = lines[i].split(' ').filter(function(n){return n;});
for(var k=0;k<line.length;k++){
var lyric = null;
var chord = null;
var object = {};
var splits = line[k].split('[').filter(function(n){return n;}); //split if chord exists, and if it does, it will have a '[' and need the ']' to filter out later, and filter out empty
if(splits[0].contains(']')){ chord = splits[0].replace(']','')} // chord is only element
else {lyric = splits[0]; if(splits[1]){ chord = splits[1].replace(']','')} };
if(lyric){ object.lyric = lyric };
if(chord){ object.chord = chord };
if(k===line.length-1){ object.lastInLine = true };
if(k===0 && i===0){ object.firstInSection = true };
if(i===lines.length-1 && k===line.length-1){ object.lastInSection = true };
this.addLyriChord(object);
}
}
}
sectionobject.end = this.content.length-1;
this.addSection(sectionobject);
}
Song.prototype.generateToMarkdown = function(){
var returnString = '';
var curCount = 1;
var objCount = this.content.length;
for(var s=0;s<this.sections.length;s++){
if(this.sections[s].name){ returnString+= '*'+this.sections[s].name+'*'+'\n'};
for(var k=this.sections[s].begin;k<=this.sections[s].end;k++){
var lyric = this.content[k].lyric;
var chordIndex = this.content[k].chordIndex;
var chord = null;
if (chordIndex >= 0) { chord = this.chords[chordIndex].name };
if(lyric){ returnString+=lyric };
if(chord){ returnString+='['+chord+']' };
if(this.content[k].lastInLine && curCount!=objCount){ if(this.content[k].lastInSection){returnString+='\n'}; returnString+='\n' }
else if(curCount!=objCount){ returnString+=' ' };
curCount++;
}
}
this.markdownGenerated = returnString; //currently set to markdownGenerated in order to compare
return returnString;
}
Song.prototype.generateToHTML = function(){
var returnHTML = '';
var curCount = 0;
for(var s=0;s<this.sections.length;s++){
if(this.sections[s].name){ returnHTML+= '<div class="section"><p class="lead">'+this.sections[s].name+'</p>'};
for(var k=this.sections[s].begin;k<=this.sections[s].end;k++){
var lyric = this.content[k].lyric || ' ';
var chordExists = false; //needed since 0 returns a null
if(this.content[k].chordIndex === 0){ chordExists = true };
var chord = ' ';
if(this.content[k].chordIndex || chordExists){ chord = this.chords[this.content[k].chordIndex].name };
if(this.content[k].firstInSection){ returnHTML+='<div class="subsection">' };
// if(k==this.sections[s].begin){ console.log('first', this.content[k], chord)}
returnHTML+='<div class="object" data-index="'+curCount+'"><div class="chord">'+chord+'</div><div class="lyric">'+lyric+'</div></div>';
if(this.content[k].lastInLine && this.content[k].lastInSection){ returnHTML+='</div>' }
else if(this.content[k].lastInLine){ returnHTML+='<br/>' };
curCount++;
}
returnHTML +='</div>';
}
this.generatedHTML = returnHTML;
return returnHTML;
}
Song.prototype.testSplit = function(text){
var arr = text.split(/(\*[a-z|A-Z|\s|\d|-]+\*)/);
console.log(arr);
}
/*
/*
Testing
*
*/
var mySong = new Song();
var text = "*verse 1*\nfdsa fdsafdsa fdsa[c]\nfdsafdsafds[emaj7] [f] [em7] fdsafdsafdsa[b]\n\n*verse 2*\nqwerqwer qwer\nqwerqwerqwerqwer";
// mySong.testSplit(text);
// console.log(mySong.generateFromMarkdown(text));
/*
mySong.addLyriChord({lyric:'Hello',chord:'Amaj7'});
mySong.addLyriChord({lyric:'World',chord:'amaJ7'});
mySong.addLyriChord({lyric:'whadddup'});
mySong.addLyriChord({lyric:'whadddup doe'});
mySong.addLyriChord({chord:'Cmin7'});
mySong.addLyriChord({lyric:'test',chord:'cmin7', lastInLine:true});
mySong.addChord('amaj7');
mySong.addChord('Amaj7');
mySong.addChord('Cmin7');
mySong.addChord('F');
mySong.addLyriChord({lyric:'testing',chord:'F'});
mySong.updateLyriChord({index:0,chord:'Em'});
mySong.addSection({name: 'Verse1', begin:0, end:3});
// console.log(mySong.lookupSection('Verse1'));
// console.log(mySong.sectionObjects('Verse1'));
// console.log(mySong.transpose('A#min7',3));
console.log(mySong.transposeChords(-2));
console.log(mySong.tempchords);
console.log(mySong);
// console.log(mySong.content[0]);
*/