-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLJS.js
333 lines (298 loc) · 9.27 KB
/
LJS.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
define(
'LJS',
[
'modules/Comments/src/CommentsController',
'modules/ChordEdition/src/ChordEdition',
'modules/chordSequence/src/SongView_chordSequence',
"modules/converters/MusicCSLJson/src/main",
"modules/converters/MusicXML/src/main",
"modules/core/src/main", // most important module
'modules/Cursor/src/Cursor',
'modules/Edition/src/Edition',
'modules/FileEdition/src/FileEdition',
'modules/History/src/HistoryC',
'modules/LSViewer/src/LSViewer',
'modules/LSViewer/src/OnWindowResizer',
'modules/MainMenu/src/MainMenu',
'modules/MidiCSL/src/main',
'modules/NoteEdition/src/NoteEdition',
'modules/NoteEdition/src/NoteSpaceManager',
'modules/PlayerView/src/PlayerView',
'modules/core/src/SongModel',
'modules/converters/MusicCSLJson/src/SongModel_CSLJson',
'modules/StructureEdition/src/StructureEdition',
'modules/Tag/src/TagManager',
'utils/main',
'modules/Audio/src/AudioModule',
'modules/Edition/src/KeyboardManager',
'jquery',
'pubsub',
'modules/Edition/src/ClipboardManager'
], function(
Comments,
ChordEdition,
chordSequence,
convertersMusicCSLJson,
convertersMusicXML,
core,
Cursor,
Edition,
FileEdition,
HistoryC,
LSViewer,
OnWindowResizer,
MainMenu,
MidiCSL,
NoteEdition,
NoteSpaceManager,
PlayerView,
SongModel,
SongModel_CSLJson,
StructureEdition,
TagManager,
utils,
AudioModule,
KeyboardManager,
$,
pubsub,
ClipboardManager
) {
function singleton(constructorFn){
var instance;
return {
getInstance: function(){
if (!instance) {
instance = constructorFn(arguments);
}
return instance;
}
};
}
//constructors for different singletons:
//
var notesCursorConstructor = function(args){
var songModel = args[0];
return cursorConstructor(songModel, 'notes');
};
var playerCursorConstructor = function(args){
var songModel = args[0];
return cursorConstructor(songModel, 'player');
};
var cursorConstructor = function(songModel, id){
return new Cursor(songModel.getComponent('notes'), id, 'arrow').model;
};
//singletons used
var snglNotesCursor = (singleton)(notesCursorConstructor);
var snglPlayerCursor = (singleton)(playerCursorConstructor);
var snglNotesManager = (singleton)(function(args){
var songModel = args[0];
var viewer = args[1];
return new NoteSpaceManager(snglNotesCursor.getInstance(songModel), viewer, 'NotesCursor', null, true, false);
});
var snglKeyBoardManager = (singleton)(function(){
return new KeyboardManager(false);
});
//functions loading different modules
function loadViewer(htmlElem, options, song){
options = options || {};
var viewer = new LSViewer(htmlElem, options);
var clipBoardManager = new ClipboardManager(htmlElem);
OnWindowResizer(song);
return viewer;
}
function loadPlayer(options, songModel){
function loadMidiPlayer(playerView, soundfontUrl){
var player = new MidiCSL.PlayerModel_MidiCSL(songModel, soundfontUrl, {
cursorModel: snglPlayerCursor.getInstance(songModel),
cursorNoteModel: snglNotesCursor.getInstance(songModel)
});
new MidiCSL.PlayerController(player, playerView);
return player;
}
var players = {};
var useAudio = !!options.audio;
var useMidi = options.midi && options.midi.soundfontUrl;
var playerViewOptions = options.viewOptions;
playerViewOptions.displaySwitch = useAudio && useMidi && options.audio.audioFile; //if audioFile is not defined, we do not load displayTypeSwitch
playerViewOptions.tempo = !isNaN(songModel.getTempo()) ? songModel.getTempo() : null;
var playerView = new PlayerView(options.HTMLElement, options.imgUrl, playerViewOptions);
new NoteSpaceManager(snglPlayerCursor.getInstance(songModel), viewer, 'PlayerCursor', "#0AA000", false, false);
if (options.interactive) {
snglNotesManager.getInstance(songModel, viewer);
}
snglKeyBoardManager.getInstance();
if (useMidi) {
players.midiPlayer = loadMidiPlayer(playerView, options.midi.soundfontUrl);
}
if (useAudio) {
var audioModule = new AudioModule(songModel,
{
viewer: viewer,
notesCursor: snglPlayerCursor.getInstance(),
audioAnimation: playerViewOptions.audioAnimation,
audioDrawer: playerViewOptions.audioDrawer,
playerView: playerView
});
if (options.audio.audioFile) {
audioModule.load(options.audio.audioFile, options.audio.tempo);
}
players.audioPlayer = audioModule;
}
return players;
}
function loadEdition(viewer, songModel, menuHTML, params){
snglKeyBoardManager.getInstance();
var menu, menuModel;
if (menuHTML && (params.notes || params.chords || params.structure)){
menu = new MainMenu(menuHTML);
menuModel = menu.model;
menuModel.options = {};
if (params.notes) {
menuModel.options.notes = {
active: true,
menu: {
title: 'Notes',
order: 3
},
imgPath: params.imgUrl.notes
};
}
if (params.chords) {
menuModel.options.chords = {
active: true,
/// menu: {
// title: 'Chords',
// order: 4
//},
imgPath: params.imgUrl.chords,
menu: false // if we don't want menu
};
}
if (params.structure) {
menuModel.options.structure = {
active: true,
menu: {
title: 'Structure',
order: 5
},
imgPath: params.imgUrl.structure
};
}
}
if (params.notes){
params.snglNotesManager = snglNotesManager;
params.snglNotesCursor = snglNotesCursor;
}
var returnObj = {
edition: new Edition(viewer, songModel, menuModel, params),
};
if (menu) returnObj.menuController = menu.controller;
if (menuModel) returnObj.menuModel = menuModel;
return returnObj;
}
//LJS object:
//returns modules to be used by client
var LJS = {
Comments: Comments,
//"chordSequence": chordSequence,
converters: {
MusicCSLJson: convertersMusicCSLJson,
MusicXML: convertersMusicXML
},
core: core,
LSViewer: LSViewer,
Tag: TagManager,
OnWindowResizer: OnWindowResizer,
utils: utils,
Audio: AudioModule
};
/**
* initializes modules asked by 'params'
* @param {Object} MusicCSLJSON Json object representing a lead sheet
* @param {Object} params parameters that determine which modules to load and their options
* @return {Object} contains objects from loaded modules
*/
LJS.init = function(MusicCSLJSON, params) {
if (MusicCSLJSON === undefined) {
throw "missing MusicCLJSON song";
}
LJS.utils.AjaxUtils.setDefaultDomain(params.defaultDomain);
var useViewer = params.viewer !== undefined;
var usePlayer = params.player !== undefined;
var useEdition = params.edition !== undefined;
var modules = {songModel: SongModel_CSLJson.importFromMusicCSLJSON(MusicCSLJSON)};
// Viewer
if (useViewer) {
if (params.viewer.HTMLElement === undefined) {
throw "Missing HTMLElement for viewer";
}
viewer = loadViewer(params.viewer.HTMLElement, params.viewer.viewOptions, modules.songModel);
}
// Player
if (usePlayer) {
if (params.player.HTMLElement === undefined) {
throw "Missing HTMLElement for player";
}
var players = loadPlayer(params.player, modules.songModel);
modules.audioPlayer = players.audioPlayer;
modules.midiPlayer = players.midiPlayer;
}
if (useEdition) {
var menuHTML = params.edition.menu !== undefined ? params.edition.menu.HTMLElement : false;
var editionModule = loadEdition(viewer, modules.songModel, menuHTML, params.edition);
//history
if (params.edition.history){
if (!params.edition.history.HTMLElement){
throw "Missing HTMLElement for history";
}
var options = $.extend({displayHistory:true, displayTime:false}, params.edition.history);
new HistoryC(modules.songModel, params.edition.history.HTMLElement, snglNotesCursor.getInstance(modules.songModel), options);
$.publish('ToHistory-add', 'Open song - ' + modules.songModel.getTitle());
}
if (editionModule.menuModel){
var fileEdition = new FileEdition(modules.songModel, viewer, {extraElementsForMenu: params.edition.extraElementsForMenu});
editionModule.menuModel.addMenu({
title: 'File',
view: fileEdition.view,
order: 1
});
}
// menu
if (editionModule.menuController){
editionModule.menuController.loadStateTab();
}
modules.edition = editionModule.edition;
modules.menuModel = editionModule.menuModel;
modules.menuController = editionModule.menuController;
}
//tags
if (params.tags){
// TagManager take as argument your array of tags here call analysis, an array of color (here undefined because we use built in colors)
new TagManager(modules.songModel, {notes: snglNotesManager.getInstance(modules.songModel,viewer)}, params.tags, undefined, true, false);
}
if (useViewer){
viewer.draw(modules.songModel);
modules.viewer = viewer;
}
if (usePlayer || useEdition || params.tags){
modules.notesCursor = snglNotesCursor.getInstance(modules.songModel);
modules.noteSpaceManager = snglNotesManager.getInstance(modules.songModel,viewer);
}
return modules;
};
/*LJS._loadChordSequence = function() {
var optionChediak = {
displayTitle: true,
displayComposer: true,
displaySection: true,
displayBar: true,
delimiterBar: "",
delimiterBeat: "/",
unfoldSong: false, //TODO unfoldSong is not working yet
fillEmptyBar: false,
fillEmptyBarCharacter: "%",
};
new chordSequence($('#chordSequence1')[0], modules.songModel, optionChediak);
};*/
return LJS;
});