-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathplayer.js
More file actions
164 lines (148 loc) · 5.92 KB
/
player.js
File metadata and controls
164 lines (148 loc) · 5.92 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
'use strict'
function Player() {
this.currentSong = {};
this.noteBuffer = [];
this.visualiser = new Visualiser(this);
this.sequencer = new Sequencer(this);
this.context = new (window.AudioContext || window.webkitAudioContext)();
this.loop = false;
this.isPlaying = false;
};
Player.prototype.loopToggle = function() {
this.loop = !this.loop
const button = document.getElementById('left-workspace-button-loop');
const toggleState = this.loop === true ? 'toggledOn' : 'toggledOff';
button.setAttribute('class', toggleState)
}
Player.prototype.toggleSongDialogue = function(typeOfDialogue) {
let dialogueBox = document.getElementById(typeOfDialogue + '-song-dialogue-box');
if (dialogueBox.classList.contains('collapsed')) {
expandSongDialogueArea(dialogueBox);
}
else {
collapseSongDialogueArea(dialogueBox);
}
};
function expandSongDialogueArea(dialogueBox) {
dialogueBox.style.marginTop = "0px";
dialogueBox.classList.remove('collapsed');
setTimeout(function() {
dialogueBox.style.transform = "scaleY(1)"
}, 250)
};
function collapseSongDialogueArea(dialogueBox) {
dialogueBox.style.transform = "scaleY(0)"
dialogueBox.classList.add('collapsed');
setTimeout(function() {
dialogueBox.style.marginTop = "-" + dialogueBox.clientHeight + "px";
}, 400)
};
Player.prototype.load = function(song) {
let loadDialogue = document.getElementById('load-song-dialogue-box');
let createDialogue = document.getElementById('create-song-dialogue-box');
collapseSongDialogueArea(loadDialogue);
collapseSongDialogueArea(createDialogue);
this.clear();
this.currentSong = song;
let sections = song.structure;
let phrases = song.phrases;
for (var section in sections) {
let phrase = phrases[sections[section]];
for (var bar in phrase.bars) {
for (var beat = 1; beat <= phrase.bars[bar].timeSig.beatCount; beat++) {
for (var semiquaver in phrase.bars[bar].beats[beat]) {
let noteData = phrase.bars[bar].beats[beat][semiquaver];
this.noteBuffer.push(noteData);
};
};
};
};
this.sequencer.renderGridArea(song, song.structure[0], song.instruments[0])
this.sequencer.showSongInfo(song);
this.visualiser.renderGridArea(song)
};
Player.prototype.refreshNoteBuffer = function() {
this.noteBuffer = [];
let sections = this.currentSong.structure;
let phrases = this.currentSong.phrases;
for (var section in sections) {
let phrase = phrases[sections[section]];
for (var bar in phrase.bars) {
for (var beat = 1; beat <= phrase.bars[bar].timeSig.beatCount; beat++) {
for (var semiquaver in phrase.bars[bar].beats[beat]) {
let noteData = phrase.bars[bar].beats[beat][semiquaver];
this.noteBuffer.push(noteData);
};
};
};
};
}
Player.prototype.clear = function() {
this.currentSong = {};
this.noteBuffer = [];
};
Player.prototype.playToggle = function() {
this.isPlaying = !this.isPlaying
if (this.isPlaying === true) {
this.visualiser.play(this.loop);
}
const button = document.getElementById('left-workspace-button');
button.innerHTML = this.isPlaying === true ? "◼︎" : "▶";
};
function generateSongName() {
const today = new Date()
return ["Sunday Song", "Monday Mix", "Tuesday Tune", "Wednesday Wiggle", "Thursday Thang", "Friday Funk", "Saturday Set"][today.getDay()]
}
Player.prototype.createSong = function(givenName = "Automatic Song", givenTempo = 120) {
if (JSON.stringify(this.currentSong) == "{}") {
let tempo = givenTempo;
let name = givenName || generateSongName();
let timeSig = [4, 4];
let song = new Song(name, tempo);
song.addPhrase(new Phrase([new Bar(timeSig[0], timeSig[1])]));
this.currentSong = song; // too magic?
this.load(song);
return song;
} else {
this.clear();
this.createSong(givenName, givenTempo);
}
};
Player.prototype.unpackEncodedSong = function(sqntfile) {
let decodedSongData = sqntfile ? atob(sqntfile) : atob(document.getElementById('load-sqnt-textarea').value);
document.getElementById('load-sqnt-textarea').value = "";
let loadedSongData = JSON.parse(decodedSongData) || {};
if (JSON.stringify(loadedSongData) == "{}" || !loadedSongData.name) {
alert("sequent did not understand the song you are trying to load. sorry.");
}
else {
let loadedSong = new Song(loadedSongData.name, loadedSongData.tempo, loadedSongData.keySignature)
loadedSongData.instruments.forEach(function(instrument) {
loadedSong.addInstrument(instrument.name, instrument.sounds, instrument.octave, instrument.duration)
});
for (var phrase in loadedSongData.phrases) {
let currentPhraseData = loadedSongData.phrases[phrase];
let currentLoadingPhrase = new Phrase();
for (var bar in currentPhraseData.bars) {
let currentBarData = currentPhraseData.bars[bar];
let currentLoadingBar = new Bar(currentBarData.timeSig.beatCount, currentBarData.timeSig.beatUnit);
for (var beat in currentBarData.beats) {
let currentBeatData = currentBarData.beats[beat];
for (var semi in currentBeatData) {
let currentSemiData = currentBeatData[semi];
for (var note in currentSemiData) {
let currentNoteData = currentSemiData[note];
let currentLoadingNote = new Note(loadedSong.getInstrumentByName(currentNoteData.instrument.name), currentNoteData.note, currentNoteData.octave, currentNoteData.duration);
currentLoadingBar.beats[beat][semi].push(currentLoadingNote);
};
};
};
currentLoadingPhrase.addBar(currentLoadingBar)
};
loadedSong.addPhrase(currentLoadingPhrase);
};
removeAllChildren(document.getElementById('sequencer-transport-bar-instrument-selector')) // to avoid instrument duplication in the visualiser
removeAllChildren(document.getElementById('playback-grid')) // to avoid instrument duplication in the visualiser
this.load(loadedSong);
};
};