forked from ospreyelm/AnalyticPiano
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmusic_controls.js
408 lines (371 loc) · 11.4 KB
/
music_controls.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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
define([
'jquery',
'lodash',
'app/config',
'app/components/events',
'app/components/component',
'app/components/ui/modal',
'app/utils/instruments',
'app/widgets/key_signature',
'app/widgets/analyze',
'app/widgets/highlight'
], function(
$,
_,
Config,
EVENTS,
Component,
ModalComponent,
Instruments,
KeySignatureWidget,
AnalyzeWidget,
HighlightWidget
) {
/**
* Defines the title of the app info modal.
* @type {string}
* @const
*/
var APP_INFO_TITLE = Config.get('helpText.appInfo.title');
/**
* Defines the content of the app info modal.
* @type {string}
* @const
*/
var APP_INFO_CONTENT = Config.get('helpText.appInfo.content');
/**
* Defines whether the shortcuts are enabled by default or not.
* @type {boolean}
* @const
*/
var KEYBOARD_SHORTCUTS_ENABLED = Config.get('general.keyboardShortcutsEnabled');
/**
* Defines the default keyboard size.
* @type {number}
* @const
*/
var DEFAULT_KEYBOARD_SIZE = Config.get('general.defaultKeyboardSize');
/**
* Defines a namespace for settings.
* canvas.
*
* @namespace
*/
var MusicControlsComponent = function(settings) {
this.settings = settings || {};
if(!("keySignature" in settings)) {
throw new Error("missing keySignature setting");
}
if(!("midiDevice" in settings)) {
throw new Error("missing midiDevice setting");
}
this.keySignature = settings.keySignature;
this.midiDevice = settings.midiDevice;
if(settings.exerciseContext) {
this.exerciseContext = settings.exerciseContext;
} else {
this.exerciseContext = false;
}
this.addComponent(new ModalComponent());
this.headerEl = $(settings.headerEl);
this.containerEl = $(settings.containerEl);
_.bindAll(this, ['onClickInfo']);
};
MusicControlsComponent.prototype = new Component();
_.extend(MusicControlsComponent.prototype, {
/**
* Initializes the component.
*
* @return undefined
*/
initComponent: function() {
$('.js-btn-help', this.headerEl).on('click', this.onClickInfo);
$('.js-btn-screenshot').on('mousedown', this.onClickScreenshot);
$('.js-btn-upload-json').on('mousedown', this.onClickUploadJSON);
$('.js-btn-download-json').on('mousedown', this.onClickDownloadJSON);
$('.js-btn-pristine').on('mousedown', () => this.onClickPristine());
this.initControlsLayout();
this.initKeySignatureTab();
this.initNotationTab();
this.renderInstrumentSelect();
this.renderKeyboardSizeSelect();
this.renderOctaveAdjustment();
this.renderKeyboardShortcuts();
this.initMidiTab();
},
/**
* Initializes the controls layout.
*
* @return undefined
*/
initControlsLayout: function() {
this.containerEl.children(".accordion").accordion({
active: false,
collapsible: true,
heightStyle: "content"
});
},
/**
* Initializes the content of the midi.
*
* @return undefined
*/
initMidiTab: function() {
var containerEl = this.containerEl;
var renderDevices = function(midiDevice) {
var inputs = midiDevice.getInputs();
var outputs = midiDevice.getOutputs();
var tpl = _.template('<option value="<%= id %>"><%= name %></option>');
var makeOptions = function(device, idx) {
return tpl({ id: idx, name: device.name });
};
var devices = {
'input': {
'selector': $('.js-select-midi-input', containerEl),
'options': _.map(inputs, makeOptions)
},
'output': {
'selector': $('.js-select-midi-output', containerEl),
'options': _.map(outputs, makeOptions) }
};
_.each(devices, function(device, type) {
if(device.options.length > 0) {
$(device.selector).html(device.options.join(''));
} else {
$(device.selector).html('<option>--</option>');
}
if(device.readonly) {
$(device.selector).attr('disabled', 'disabled');
} else {
$(device.selector).on('change', function() {
var index = parseInt($(this).val(), 10);
var inputs = this.length; /* this is the number of available devices */
midiDevice[type=='input'?'selectInput':'selectOutput'](index, inputs);
});
}
});
};
$('.js-refresh-midi-devices', containerEl).on('click', this.midiDevice.update);
this.midiDevice.bind("updated", renderDevices);
renderDevices(this.midiDevice);
},
/**
* Initializes the content of the key signature.
*
* @return undefined
*/
initKeySignatureTab: function() {
var containerEl = this.headerEl;
var el = $('.js-keysignature-widget', containerEl);
var widget = new KeySignatureWidget(this.keySignature);
widget.render();
el.append(widget.el);
},
/**
* Initializes the content of the notation containerEl.
*
* @return undefined
*/
initNotationTab: function() {
var that = this;
var containerEl = this.containerEl;
var el = $('.js-analyze-widget', containerEl);
var analysisSettings = {};
var highlightSettings = {};
var staffDistribution = {};
if(this.exerciseContext) {
/* TO DO: grab additional settings */
analysisSettings = this.exerciseContext.getDefinition().getAnalysisSettings();
highlightSettings = this.exerciseContext.getDefinition().getHighlightSettings();
staffDistribution = this.exerciseContext.getDefinition().getStaffDistribution();
}
var analyze_widget = new AnalyzeWidget(analysisSettings);
var highlight_widget = new HighlightWidget(highlightSettings);
var event_for = {
'highlight': EVENTS.BROADCAST.HIGHLIGHT_NOTES,
'analyze': EVENTS.BROADCAST.ANALYZE_NOTES
};
var onChangeCategory = function(category, enabled) {
if(event_for[category]) {
that.broadcast(event_for[category], {key: "enabled", value: enabled});
}
};
var onChangeOption = function(category, mode, enabled) {
var value = {};
if(event_for[category]) {
value[mode] = enabled;
that.broadcast(event_for[category], {key: "mode", value: value});
}
};
highlight_widget.bind('changeCategory', onChangeCategory);
highlight_widget.bind('changeOption', onChangeOption);
analyze_widget.bind('changeCategory', onChangeCategory);
analyze_widget.bind('changeOption', onChangeOption);
analyze_widget.render();
highlight_widget.render();
el.append(analyze_widget.el, highlight_widget.el);
},
/**
* Renders the instrument selector.
*
* @return undefined
*/
renderInstrumentSelect: function() {
var that = this;
var containerEl = this.containerEl;
var el = $('.js-instrument', containerEl);
var selectEl = $("<select/>");
var tpl = _.template('<% _.forEach(instruments, function(inst) { %><option value="<%= inst.num %>"><%- inst.name %></option><% }); %>');
var options = tpl({ instruments: Instruments.getEnabled() });
selectEl.append(options);
selectEl.on('change', function() {
var instrument_num = $(this).val();
that.broadcast(EVENTS.BROADCAST.INSTRUMENT, instrument_num);
});
el.append(selectEl);
},
/**
* Renders the keyboard size selector.
*
* @return undefined
*/
renderKeyboardSizeSelect: function() {
var that = this;
var containerEl = this.containerEl;
var el = $('.js-keyboardsize', containerEl);
var selectEl = $("<select/>");
var tpl = _.template('<% _.forEach(sizes, function(size) { %><option value="<%= size %>"><%- size %></option><% }); %>');
var options = tpl({sizes: [25,32,37,49,88]})
var selected = DEFAULT_KEYBOARD_SIZE;
selectEl.append(options);
selectEl.find("[value="+selected+"]").attr("selected", "selected");
selectEl.on('change', function() {
var size = parseInt($(this).val(), 10);
that.broadcast(EVENTS.BROADCAST.KEYBOARD_SIZE, size);
});
el.append(selectEl).wrapInner("<label>Piano keys </label>");
},
/**
* Renders the octave adjustment selector.
*
* @return undefined
*/
renderOctaveAdjustment: function() {
var that = this;
var containerEl = this.containerEl;
var el = $('.js-octaveadjustment', containerEl);
var selectEl = $("<select/>");
var tpl = _.template('<% _.forEach(adjustments, function(adj) { %><option value="<%= adj %>"><%- adj %></option><% }); %>');
var options = tpl({adjustments: [-2,-1,0,1,2]})
var selected = 0;
selectEl.append(options);
selectEl.find("[value="+selected+"]").attr("selected", "selected");
selectEl.on('change', function() {
var adj = parseInt($(this).val(), 10);
that.broadcast(EVENTS.BROADCAST.OCTAVE_ADJUSTMENT, adj);
});
el.append(selectEl).wrapInner("<label>Octave adjustment </label>");
},
/**
* Renders the keyboard shorcuts.
*
* @return undefined
*/
renderKeyboardShortcuts: function() {
var that = this;
var containerEl = this.containerEl;
var el = $('.js-keyboardshortcuts', containerEl);
var inputEl = $('<input type="checkbox" name="keyboard_shortcuts" value="on" />');
el.append("Computer keyboard as piano ").append(inputEl).wrap("<label/>");
// toggle shortcuts on/off via gui control
inputEl.attr('checked', KEYBOARD_SHORTCUTS_ENABLED);
inputEl.on('change', function() {
var toggle = $(this).is(':checked') ? true : false;
that.broadcast(EVENTS.BROADCAST.TOGGLE_SHORTCUTS, toggle);
$(this).blur(); // trigger blur so it loses focus
});
// update gui control when toggled via ESC key
this.subscribe(EVENTS.BROADCAST.TOGGLE_SHORTCUTS, function(enabled) {
inputEl[0].checked = enabled;
});
},
/**
* Handler to generate a screenshot/image of the staff area.
*
* @param {object} evt
* @return {boolean} true
*/
onClickScreenshot: function(evt) {
var $canvas = $('#staff-area canvas');
var $target = $(evt.target);
var data_url = $canvas[0].toDataURL();
$target[0].href = data_url;
$target[0].target = '_blank';
return true;
},
/**
* Handler to upload JSON data for the current notation.
*
* @param {object} evt
* @return {boolean} true
*/
onClickUploadJSON: function(evt) {
const json_data = sessionStorage.getItem('current_state')
|| false;
console.log("upload", json_data);
if (!json_data /* || json_data["chords"].length < 1 */) return false;
/*
// enable later
$.ajax({
type: "POST",
url: 'exercises/add',
data: {'data': json_data},
dataType: 'json',
});
*/
return true;
},
/**
* Handler to download JSON data for the current notation.
*
* @param {object} evt
* @return {boolean} true
*/
onClickDownloadJSON: function(evt) {
const json_data = sessionStorage.getItem('current_state')
|| false;
console.log("download", json_data);
let intro_text = prompt("Enter the Intro Text");
var file_name = intro_text.split(' ').join('_');
let json_parsed = JSON.parse(json_data);
console.log(json_parsed);
json_parsed.introText = intro_text;
const new_json_data = JSON.stringify(json_parsed);
if (!json_data /* || json_data["chords"].length < 1 */) return false;
var blob = new Blob([new_json_data], {type: "application/json;charset=utf-8"});
saveAs(blob, file_name + ".json");
return true;
},
/**
* Handler to broadcast request for pristine sheet music div.
*
* @param {object} evt
* @return {boolean} true
*/
onClickPristine: function() {
this.broadcast(EVENTS.BROADCAST.PRISTINE);
return true;
},
/**
* Handler to shows the info modal.
*
* @param {object} evt
* @return {boolean} false
*/
onClickInfo: function(evt) {
this.trigger("modal", {title: APP_INFO_TITLE, content: APP_INFO_CONTENT});
return false;
}
});
return MusicControlsComponent;
});