-
Notifications
You must be signed in to change notification settings - Fork 1
/
stageView.js
executable file
·268 lines (251 loc) · 11.4 KB
/
stageView.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
// Filename: views/stage/stageView.js
/*
This is the StageView, that represents the entire drum kit.
*/
define([
'jquery',
'underscore',
'bbone',
'backbone/collections/beats',
'backbone/collections/measures',
'backbone/collections/representations',
'backbone/collections/stage',
'backbone/models/beat',
'backbone/models/measure',
'backbone/models/hTrack',
'backbone/models/representation',
'backbone/models/state',
'backbone/models/conductor',
'backbone/models/remainingInstrumentGenerator',
'backbone/views/button/remainingInstrumentGeneratorView',
'backbone/views/button/resetStageView',
'backbone/views/hTrack/hTrackView',
'general/lookupInstrument',
'logging',
'text!backbone/templates/hTrack/hTrack.html'
], function($, _, Backbone, BeatsCollection, MeasuresCollection, RepresentationsCollection, StageCollection, BeatModel, MeasureModel, HTrackModel, RepresentationModel, StateModel, ConductorModel, RemainingInstrumentGeneratorModel, RemainingInstrumentGeneratorView, ResetStageView, HTrackView, LookupInstrument, Logging, HTrackTemplate){
var StageView = Backbone.View.extend({
el: $('#sof-stage-area'),
initialize: function(){
console.info('stage view initialize')
this.initialSetup();
this.render();
this.addListeners();
this.secondarySetup();
window.stageView = this;
},
addListeners: function(){
// Listeners
this.listenTo(ResetStageView, 'resetStage', this.resetStage);
this.listenTo(RemainingInstrumentGeneratorModel, 'removedInstrumentFromUnused', this.addFromGeneratorModel);
this.listenTo(this.stageCollection, 'add remove', this.turnPlayingOff);
// allow the letter t to click the first transition
$(document).bind('keypress', this.manualPress);
},
initialSetup: function(){
Logging.initialize();
this.stageCollection = StageCollection;
// set the song's conductor
this.conductor = ConductorModel;
this.masterAudioContext = new AudioContext();
StateModel.set('stageCollection', this.stageCollection);
// Per SO? http://stackoverflow.com/questions/9522845/backbone-js-remove-all-sub-views
this.childViews = [];
},
secondarySetup: function(){
// This kicks off and adds the snare to the
RemainingInstrumentGeneratorModel.removeInstrumentFromUnused({type:'sn'});
// RemainingInstrumentGeneratorModel.removeInstrumentFromUnused({type:'hh'});
this.makeChildrenHTracks();
},
resetStage: function(){
Backbone.history.navigate('');
window.location.reload();
// Until I can get all the reset working correctly
// RemainingInstrumentGeneratorModel.resetInstrumentGenerator();
// debugger;
// this.close();
// this.destroyEverything();
// this.initialSetup();
// this.render();
// this.addListeners();
// this.secondarySetup();
},
destroyEverything: function(){
delete this.stageCollection;
delete this.conductor;
delete this.masterAudioContext;
delete StateModel;
delete childViews;
delete RemainingInstrumentGeneratorModel;
},
turnPlayingOff: function(){
if(ConductorModel.get('isPlaying')){
ConductorModel.stop();
Logging.logStorage('The music was playing when the HTrack was added/deleted');
}
},
render: function(){
console.info('StageView render start');
this.$el.append('<div id="instruments-collection"></div>');
},
makeChildHTrack: function(hTrack){
console.info('hTrack: ', hTrack);
//create a hTrack view.
var hTrackChildView = new HTrackView({
model: hTrack,
masterAudioContext: this.masterAudioContext
});
this.childViews.push(hTrackChildView)
},
makeChildrenHTracks: function() {
//we have to render each one of our hTrack's.
_.each(this.stageCollection.models, function(hTrack) {
this.makeChildHTrack(hTrack);
}, this);
},
makeInstrumentFromScratch: function(options) {
console.info('makeInstrumentFromScratch options: ', options)
// set up the measures, measureReps, and beats for an instruments-collection
this.setUpMMB();
// Add an instrument model to the stage Collection
var newInstrumentToAdd = {
label: LookupInstrument.getDefault(options.type, 'label'),
type: LookupInstrument.getDefault(options.type, 'type'),
img: LookupInstrument.getDefault(options.type, 'image'),
gain: LookupInstrument.getDefault(options.type, 'gain'),
sample: LookupInstrument.getDefault(options.type, 'sample'),
measures: this.manuallyCreatedMeasuresCollection,
signature: this.manuallyCreatedMeasuresCollection.models[0].get('beats').length,
active: true,
tempo: 120 //bpm
};
this.stageCollection = StageCollection.add(newInstrumentToAdd);
this.makeChildHTrack(StageCollection.last());
},
addFromGeneratorModel: function(options){
// Add an instrument model to the stage Collection
this.setUpMMB();
if(RemainingInstrumentGeneratorModel.get('unusedInstruments').length == 2){
var insertedMeasuresCollection = this.manuallyCreatedMeasuresCollection;
} else {
var insertedMeasuresCollection = this.secondManuallyCreatedMeasuresCollection;
}
var newInstrumentToAdd = {
label: LookupInstrument.getDefault(options.type, 'label'),
type: LookupInstrument.getDefault(options.type, 'type'),
img: LookupInstrument.getDefault(options.type, 'image'),
gain: LookupInstrument.getDefault(options.type, 'gain'),
sample: LookupInstrument.getDefault(options.type, 'sample'),
measures: insertedMeasuresCollection,
signature: insertedMeasuresCollection.models[0].get('beatsCollection').length,
active: true,
tempo: 120 //bpm
};
this.stageCollection = StageCollection.add(newInstrumentToAdd);
this.makeChildHTrack(StageCollection.last());
},
setUpMMB: function(){
// This is for the initial instrument to be set up
// this creates 1 measure, and adds beats and the representations to itself
this.manuallyCreatedMeasureBeatsCollection = new BeatsCollection;
//for each beat - also change signature below
for (var i = 0; i < 5; i++) {
if (i == 0 || i == 2 || i == 4 || i == 6 || i == 8 || i == 10 || i == 12 || i == 14 ){
this.manuallyCreatedMeasureBeatsCollection.add([{selected: true}]);
} else {
this.manuallyCreatedMeasureBeatsCollection.add([{selected: false}]);
// this.manuallyCreatedMeasureBeatsCollection.add();
}
}
//make a collection
this.manuallyCreatedMeasureRepresentationCollection = new RepresentationsCollection;
// add an audio rep
this.manuallyCreatedRepresentationModel = new RepresentationModel({
currentRepresentationType:'audio',
beatsCollection:this.manuallyCreatedMeasureBeatsCollection
});
this.manuallyCreatedMeasureRepresentationCollection.add(this.manuallyCreatedRepresentationModel);
// add a bead rep
// this.secondManuallyCreatedRepresentationModel = new RepresentationModel({
// currentRepresentationType: 'bead',
// beatsCollection: this.manuallyCreatedMeasureBeatsCollection
// });
// this.manuallyCreatedMeasureRepresentationCollection.add(this.secondManuallyCreatedRepresentationModel);
// // add a line rep
// this.thirdManuallyCreatedRepresentationModel = new RepresentationModel({
// currentRepresentationType: 'line',
// beatsCollection: this.manuallyCreatedMeasureBeatsCollection
// });
// this.manuallyCreatedMeasureRepresentationCollection.add(this.thirdManuallyCreatedRepresentationModel);
// // add a pie rep
// this.manuallyCreatedRepresentationModel = new RepresentationModel({currentRepresentationType:'pie', beatsCollection:this.manuallyCreatedMeasureBeatsCollection});
// this.manuallyCreatedMeasureRepresentationCollection.add(this.manuallyCreatedRepresentationModel);
// // add a bar rep
// this.manuallyCreatedRepresentationModel = new RepresentationModel({currentRepresentationType:'bar', beatsCollection:this.manuallyCreatedMeasureBeatsCollection});
// this.manuallyCreatedMeasureRepresentationCollection.add(this.manuallyCreatedRepresentationModel);
// Create a Measures Collection, and add the beats and representations
this.manuallyCreatedMeasuresCollection = new MeasuresCollection;
this.parentMeasureModel = new MeasureModel({
beatsCollection: this.manuallyCreatedMeasureBeatsCollection,
measureRepresentations: this.manuallyCreatedMeasureRepresentationCollection
});
this.manuallyCreatedMeasuresCollection.add(this.parentMeasureModel);
this.manuallyCreatedRepresentationModel.addParentMeasureModelAfter(this.parentMeasureModel);
// this.secondManuallyCreatedRepresentationModel.addParentMeasureModelAfter(this.parentMeasureModel);
// this.thirdManuallyCreatedRepresentationModel.addParentMeasureModelAfter(this.parentMeasureModel);
// This is for subsequent instruments when added
this.secondManuallyCreatedMeasureBeatsCollection = new BeatsCollection;
//for each beat - also change signature below
for (var i = 0; i < 4; i++) {
if (i == 0 || i == 2 || i == 4 || i == 6 || i == 7){
this.secondManuallyCreatedMeasureBeatsCollection.add([{selected: true}]);
} else {
this.secondManuallyCreatedMeasureBeatsCollection.add([{selected: false}]);
// this.secondManuallyCreatedMeasureBeatsCollection.add();
}
}
this.secondManuallyCreatedMeasuresCollection = new MeasuresCollection;
this.secondManuallyCreatedMeasuresCollection.add({
beatsCollection: this.secondManuallyCreatedMeasureBeatsCollection, measureRepresentations: this.manuallyCreatedMeasureRepresentationCollection});
},
// We want to delete an instrument from the view, as well as add it to the instrument generator
deleteInstrument: function(instrument) {
},
subRender: function() {
// used if we want to render a portion of this view...
$('#sample-messages').html('subre');
},
close: function(){
console.info('in stageView close function');
$(document).unbind('keypress', this.manualPress);
this.remove();
this.unbind();
this.closeChildren();
},
closeChildren: function(){
// handle other unbinding needs, here
_.each(this.childViews, function(childView){
console.log('in measureView close function, CLOSING CHILDREN');
// If the childView has a close() method, then use it, to close it's children
if (childView.close){
childView.close();
}
})
},
manualPress: function(e) {
// t = 116, d = 100, w = 119, o = 111
if(!$('.modal-content:visible').length){
if (e.keyCode === 116) {
if(!$('.measureRep:nth-child(2)').hasClass('transition-rep')){
$('.measureRep:nth-child(2)').addClass('transition-rep')
} else {
$('.measureRep:nth-child(2)').removeClass('transition-rep')
}
}
}
}
});
// A singleton
return new StageView();
});