-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscript.js
More file actions
218 lines (176 loc) · 7 KB
/
script.js
File metadata and controls
218 lines (176 loc) · 7 KB
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
// Global Constants
const OCTAVE_DISPLACEMENT = 2;
const BREAK_EVERY_X_NOTES = 16;
const BEAM_EVERY_X_NOTES = 4;
const NODE_START_OFFSET = 48;
/**
* convertToAbcString - Converts an array of notes into the ABC string format
*
* @param {[]} data - array of note numbers
* @returns {string} - abcjs string format
*/
function convertToAbcString(data) {
// Headers (4/4 in semi quavers)
let compiled = "M: 4/4\n" + "L: 1/16\n";
let noteCount = 1;
// Process each note number in the data
for(let i=0; i < data.length; i++) {
// Convert into the ABC string format
compiled = compiled + convertNumberToNote(data[i])
// Beam every X notes
if (noteCount % BEAM_EVERY_X_NOTES === 0) compiled = compiled + " ";
// Draw a barline every X notes
if (noteCount % BREAK_EVERY_X_NOTES == 0) compiled = compiled + '|\n'
// Note counter
noteCount++;
}
return compiled;
}
/**
* convertNumberToNote - Converts midi note number to readable note name
*
* @param {*} number - midi note number
* @returns
*/
function convertNumberToNote(number) {
if (number < 0 || number > 127) return;
let noteNames = []
let octave = ""
if (number < 72) noteNames = ['C', '^C', 'D', '^D', 'E', 'F', '^F', 'G', '^G', 'A', '^A', 'B'];
if (number >= 72) noteNames = ['c', '^c', 'd', '^d', 'e', 'f', '^f', 'g', '^g', 'a', '^a', 'b'];
if (number < 60 && number >= 48) octave = ','
if (number < 48 && number >= 36) octave = ',,'
if (number < 36 && number >= 24) octave = ',,,'
if (number < 24 && number >= 12) octave = ',,,,'
if (number < 84 && number >= 72) octave = ''
if (number < 96 && number >= 84) octave = '\''
if (number < 108 && number >= 96) octave = '\'\''
if (number < 110 && number >= 108) octave = '\'\'\''
//octave = (4 - Math.floor(number / 12)) * ',';
return noteNames[number % 12] + octave.toString() +'';
}
/**
* generateScale - Generates the scales!
*
* @param divisions - the base interval between each node
* @param {int} start - which note number should we start on?
* @param {int} nodes - the number of nodes to ass
* @param {int} interpolation - the number of interpolations to add
* @param {array} interpolationIntervals - An array that contains each interval from the node
* @returns {array} of notes to be drawn
*/
function generateScale(divisions, start, nodes, interpolation, interpolationInterval) {
if (divisions <= 0) return;
if (nodes <= 0) return;
let scaleArray = [];
let startingNote = start + NODE_START_OFFSET;
// add the nodes base note to the scale array
for (i = 0; i < nodes; i++) {
scaleArray.push(startingNote + (i * divisions));
// add each of the interpolations to the scale array
for (x = 0; x < interpolation; x++) {
scaleArray.push(startingNote + (i * divisions) + interpolationInterval[x]);
}
}
return scaleArray;
}
/**
* compileAbcString - Collects all the user input, generate the scale and the abc string
*
* @returns ABC string to be passed to the visualiser or playback
*/
function compileAbcString() {
// Collect all the values from the DOM
const divisions = parseInt(divisionInput.value);
const starting = parseInt(startingNote.value)
const notes = parseInt(numberOfNotes.value)
const interpolation = parseInt(interpolationInput.value)
const interpolationInterval = [parseInt(interpolationIntervalInput1.value), parseInt(interpolationIntervalInput2.value), parseInt(interpolationIntervalInput3.value), parseInt(interpolationIntervalInput4.value)]
// Control which interpolation interval inputs the user can edit
disableInterpolationInputs(interpolation)
// Generate the scale and if reverse is checked, reverse the array
let noteArray = generateScale(divisions, starting, notes, interpolation, interpolationInterval);
if (descending.checked===true) noteArray.reverse();
// Convert to ABC and return
return convertToAbcString(noteArray);
}
/**
* disableInterpolationInputs - controls how many of the interval inputs are editable
*
* @param {int} number
*/
function disableInterpolationInputs(number) {
// Disable them all
interpolationIntervalInput1.disabled = true;
interpolationIntervalInput2.disabled = true;
interpolationIntervalInput3.disabled = true;
interpolationIntervalInput4.disabled = true;
// Enable them one by one
if(number >= 4) interpolationIntervalInput4.disabled = false;
if(number >= 3) interpolationIntervalInput3.disabled = false;
if(number >= 2) interpolationIntervalInput2.disabled = false;
if(number >= 1) interpolationIntervalInput1.disabled = false;
}
/**
* drawNotation is called when a change is made to the user inputs, updates the notation
*
*/
function drawNotation() {
const abcString = compileAbcString();
var visualOptions = {
//responsive: 'resize',
staffwidth: 768,
wrap: {
minSpacing: 2.5,
maxSpacing: 4,
preferredMeasuresPerLine: 1
},
scale: 1.8
};
var visualObj = ABCJS.renderAbc("paper", abcString, visualOptions);
}
/**
* play - generates the ABC string and starts playback
*
* from basic playback example in abcjs lib
*/
function play() {
if (ABCJS.synth.supportsAudio()) {
let abc = compileAbcString();
let visualObj = ABCJS.renderAbc("*", abc)[0];
let midiBuffer = new ABCJS.synth.CreateSynth();
midiBuffer.init({
//audioContext: new AudioContext(),
visualObj: visualObj,
// sequence: [],
millisecondsPerMeasure: 2000,
// debugCallback: function(message) { console.log(message) },
options: {
// soundFontUrl: "https://paulrosen.github.io/midi-js-soundfonts/FluidR3_GM/" ,
// sequenceCallback: function(noteMapTracks, callbackContext) { return noteMapTracks; },
// callbackContext: this,
// onEnded: function(callbackContext),
// pan: [ -0.5, 0.5 ]
}
}).then(function (response) {
console.log(response);
midiBuffer.prime().then(function (response) {
midiBuffer.start();
});
}).catch(function (error) {
console.warn("Audio problem:", error);
});
} else {
document.querySelector(".error").innerHTML = "<div class='audio-error'>Audio is not supported in this browser.</div>";
}
}
// DOM variable declarations
const divisionInput = document.getElementById("divisions-input");
const startingNote = document.getElementById("starting-input");
const numberOfNotes = document.getElementById("notes-input");
const interpolationInput = document.getElementById("interpolation-input");
const interpolationIntervalInput1 = document.getElementById("interpolation-interval-input1");
const interpolationIntervalInput2 = document.getElementById("interpolation-interval-input2");
const interpolationIntervalInput3 = document.getElementById("interpolation-interval-input3");
const interpolationIntervalInput4 = document.getElementById("interpolation-interval-input4");
const descending = document.getElementById("descending");