-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathindex.js
More file actions
265 lines (223 loc) · 6.89 KB
/
index.js
File metadata and controls
265 lines (223 loc) · 6.89 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
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
import Jazz, {NoteTranslator} from './jazz';
import UTM from './utm';
const TOTAL_BARS = 8;
const MAXSTATES = 17;
const MINSTATES = 1;
const jazz = new Jazz();
document.addEventListener('DOMContentLoaded', () => {
const gobutton = document.querySelector('#begin-button');
gobutton.addEventListener('click', () => {
// Reset img and any sound
resetAll();
// Have utm play
const userOptions = getUserOptions();
const tape = makeTape(); // Empty tape 4x8
const utm = new UTM(tape, 20, userOptions.states);
jazz.oscillatorTypes = userOptions.instruments;
const staff = [];
for (let i = 0; i < TOTAL_BARS; i++) {
utm.begin();
const tape = utm.getTape();
for (let j = 0; j < tape.length; j++) {
if (staff[j]) {
staff[j] = staff[j].concat(tape[j].slice());
} else {
staff[j] = tape[j].slice();
}
}
}
const noteTranslator = new NoteTranslator(staff);
noteTranslator.translate();
const freqs = noteTranslator.getFreq();
makeImage(staff);
showInstruments(userOptions);
jazz.play(freqs);
moveBar();
const stopbutton = document.querySelector('#stop-button');
stopbutton.addEventListener('click', () => {
stop(jazz);
});
const replaybutton = document.querySelector('#replay-button');
replaybutton.addEventListener('click', () => {
jazz.stop();
removeImage();
makeImage(staff);
showInstruments(userOptions);
jazz.play(freqs);
moveBar();
});
const reset = document.querySelector('#reset-button');
reset.addEventListener('click', () => {
jazz.stop();
removeImage();
});
});
const randomizebutton = document.querySelector('#random-button');
randomizebutton.addEventListener('click', () => {
// Reset img and any sound
resetAll();
// Have utm play with randomized instruments and states
const randomizerOptions = getRandomizerOptions();
const tape = makeTape(); // Empty tape 4x8
const utm = new UTM(tape, 20, randomizerOptions.states);
jazz.oscillatorTypes = randomizerOptions.instruments;
const staff = [];
for (let i = 0; i < TOTAL_BARS; i++) {
utm.begin();
const tape = utm.getTape();
for (let j = 0; j < tape.length; j++) {
if (staff[j]) {
staff[j] = staff[j].concat(tape[j].slice());
} else {
staff[j] = tape[j].slice();
}
}
}
const noteTranslator = new NoteTranslator(staff);
noteTranslator.translate();
const freqs = noteTranslator.getFreq();
makeImage(staff);
showInstruments(randomizerOptions);
showStates(randomizerOptions);
jazz.play(freqs);
moveBar();
const stopbutton = document.querySelector('#stop-button');
stopbutton.addEventListener('click', () => {
stop(jazz);
});
const replaybutton = document.querySelector('#replay-button');
replaybutton.addEventListener('click', () => {
jazz.stop();
removeImage();
makeImage(staff);
showInstruments(randomizerOptions);
showStates(randomizerOptions);
jazz.play(freqs);
moveBar();
});
const reset = document.querySelector('#reset-button');
reset.addEventListener('click', () => {
jazz.stop();
removeImage();
});
});
});
function getUserOptions() {
const userOptions = {
instruments: [],
states: 4
};
userOptions.states = parseInt(document.querySelector('#stateval').value, 10);
for (let i = 0; i < 4; i++) {
userOptions.instruments.push(
document.querySelector(`#instrument-${i}`).value);
}
return userOptions;
}
function getRandomizerOptions() {
const randomizerOptions = {
instruments: [],
states: 4
};
randomizerOptions.states = Math.floor(Math.random() * MAXSTATES) + MINSTATES;
const instrumentsoptions = ['chiptune', 'brass', 'bass', 'organ'];
for (let i = 0; i < 4; i++) {
const index = Math.floor(Math.random() * instrumentsoptions.length);
randomizerOptions.instruments.push(instrumentsoptions[index]);
}
return randomizerOptions;
}
function makeTape() {
const tape = [];
for (let i = 0; i < 4; i++) {
tape[i] = [];
for (let j = 0; j < 8; j++) {
tape[i][j] = 0;
}
}
return tape;
}
function makeImage(staff) {
const musicsheet = document.querySelector('#music-sheets');
// For each row in the staff (each instrument) create an image of a staff
for (let y = 0; y < staff.length; y++) {
const notesheet = document.createElement('div');
notesheet.id = 'music-animation';
musicsheet.append(notesheet);
const divider = document.createElement('div');
divider.id = 'sheet-divider';
musicsheet.append(divider);
let notexvalue = 10;
const noteyvalue = 10;
// For each note for each instrument staff, create a new note and place on img
for (let x = 0; x < staff[y].length; x++) {
const newnote = document.createElement('div');
newnote.id = 'music-notes';
notesheet.append(newnote);
if (staff[y][x] === 0) {
newnote.style.bottom = noteyvalue;
newnote.style.left = notexvalue;
notexvalue += 10;
} else {
const notevalue = staff[y][x] % 8;
newnote.style.bottom = (noteyvalue + (10 * notevalue)) + 'px';
notexvalue += 10;
newnote.style.left = notexvalue + 'px';
}
}
}
}
function removeImage() {
resetAll();
for (let j = 0; j < 4; j++) {
const instname = document.querySelector(`#inst-${j}`);
instname.innerHTML = '';
}
}
function moveBar() {
const bar = document.querySelector('#music-bar');
let pos = 0;
const loc = setInterval(move, 23);
function move() {
const imgwidth = document.querySelector('#music-animation').clientWidth;
if (pos === imgwidth) {
clearInterval(loc);
} else {
pos++;
bar.style.left = pos + 'px';
}
}
}
function resetAll() {
const musicsheet = document.querySelector('#music-sheets');
while (musicsheet.firstChild) {
musicsheet.removeChild(musicsheet.firstChild);
}
const newbar = document.createElement('div');
newbar.id = 'music-bar';
musicsheet.append(newbar);
jazz.stop();
}
function stop(jazz) {
jazz.stop();
const musicbar = document.querySelector('#music-bar');
const pos = window.getComputedStyle(musicbar)
.getPropertyValue('margin-left');
const newbar = musicbar.cloneNode(true);
musicbar.parentNode.replaceChild(newbar, musicbar);
newbar.style.left = pos + 'px';
}
function showInstruments(userOptions) {
const instr = userOptions.instruments;
for (let i = 0; i < instr.length; i++) {
const element = document.querySelector(`#inst-${i}`);
element.innerHTML = instr[i];
const choseninstr = document.querySelector(`#instrument-${i}`);
choseninstr.value = instr[i];
}
}
function showStates(randomizerOptions) {
const states = randomizerOptions.states;
const element = document.querySelector('#stateval');
element.value = states;
}