-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
335 lines (301 loc) · 9.41 KB
/
Copy pathscript.js
File metadata and controls
335 lines (301 loc) · 9.41 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
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
// Contexto de audio (context) y nodo fuente (source)
let context, source;
// Instancia de Hydra
let hydra;
// Indice de la pista actual
let currentTrackIdx = 0;
// Estado del reproductor. Tiene que empezar detenido porque Chrome no ermite
// que una página autoreproduzca audio sin interacción del usuario.
let playing = false;
// Función módulo utilizada para implementar la playlist circular
Number.prototype.mod = function (n) {
return ((this % n) + n) % n;
};
/*
* La variable `playlist` es una lista con objetos que tienen la información de
* cada pista:
*
* - url: la URL del .mp3 que usa el reproductor, debería ser /audio/[archivo].mp3
* - artistName: Nombre del artista, como se va a mostrar en el reproductor
* - trackName: Nombre de la pista, como se va a mostrar en el reproductor
* - profileUrl: URL del artista, opcional
* - codeUrl: URL del código de la pista, opcional. Debería ser /codeScripts/[archivo].html
* - hydraFunction: debería ser el nombre de la función con el código de hydra para esta pista
*/
let playlist = [
{
url: "audio/Adriano_Duarte_-_Pajaritos.mp3",
artistName: "Adriano Duarte",
trackName: "Pajaritos",
codeUrl: "codeScripts/Adriano_Duarte_-_Pajaritos.html",
profileUrl: "https://adrianoduarte.com/",
hydraFunction: hydraIrisS,
},
{
url: "audio/Ale_Zacha_-_Atras_del_tunel_la_luz.mp3",
artistName: "Ale Zacha",
trackName: "Atrás del tunel la luz",
profileUrl: "https://alezacha.bandcamp.com/releases",
hydraFunction: hydraA,
},
{
url: "audio/Ale_Zacha_-_olfato_tacto_y_todo_lo_que_no_se_puede.mp3",
artistName: "Ale Zacha",
trackName: "olfato tacto y todo lo que no se puede",
codeUrl: "codeScripts/Ale_Zacha_-_Olfato_Tacto.html",
profileUrl: "https://alezacha.bandcamp.com/releases",
hydraFunction: hydraAngelJara03,
},
{
url: "audio/Ale_Zacha_-_todo_lo_que_si_se_puede.mp3",
artistName: "Ale Zacha",
trackName: "todo lo que si se puede",
profileUrl: "https://alezacha.bandcamp.com/releases",
hydraFunction: hydraAngelJara12,
},
{
url: "audio/Angel_Jara_-_Gesto_Tecnico_en_lo_Cotidiano.mp3",
artistName: "Ángel Jara",
trackName: "Gesto Técnico en lo Cotidiano",
codeUrl: "codeScripts/Angel_Jara_-_Gesto_Tecnico.html",
profileUrl: "https://github.com/bruveping",
hydraFunction: hydraAngelJara13,
},
{
url: "audio/Angel_Jara_-_Objeto_Tecnico_0001.mp3",
artistName: "Ángel Jara",
trackName: "Objecto Técnico 0001",
codeUrl: "codeScripts/Angel_Jara_-_Objeto_Tecnico.html",
profileUrl: "https://github.com/bruveping",
hydraFunction: hydraAngelJara04,
},
{
url: "audio/Angel_Jara_-_ruidismoRetroFuturista02.mp3",
artistName: "Ángel Jara",
trackName: "ruidismoRetroFuturista02",
codeUrl: "codeScripts/Angel_Jara_-_Ruidismo_RetroFuturista_02.html",
profileUrl: "https://github.com/bruveping",
hydraFunction: hydraAngelJara16,
},
{
url: "audio/cataHache_-_tink.mp3",
artistName: "cataHache",
trackName: "tink",
codeUrl: "codeScripts/cataHache_-_tink.html",
profileUrl: "https://www.instagram.com/sensibilidadvirtual",
hydraFunction: hydraAngelJara15,
},
{
url: "audio/Gabriela_Baldoni_-_Kaos.mp3",
artistName: "Gabriela Baldoni",
trackName: "Kaos",
codeUrl: "codeScripts/Gabriela_Baldoni_-_Kaos.html",
hydraFunction: hydraAngelJara06,
},
{
url: "audio/Gabriela_Baldoni_-_Primer_intento.mp3",
artistName: "Gabriela Baldoni",
trackName: "Primer Intento",
codeUrl: "codeScripts/Gabriela_Baldoni_-_Primer_intento.html",
hydraFunction: hydraAngelJara07,
},
{
url: "audio/Ignacio_Franco_-_Dientes.mp3",
artistName: "Ignacio Franco",
trackName: "Dientes",
codeUrl: "codeScripts/Ignacio_Franco_-_Dientes.html",
hydraFunction: hydraAngelJara08,
},
{
url: "audio/Karen_Chalco_-_7il42.mp3",
artistName: "Karen Chalco",
trackName: "7il42",
hydraFunction: hydraAngelJara09,
},
{
url: "audio/Malien_-_algx.mp3",
artistName: "Malien",
trackName: "algx",
codeUrl: "codeScripts/Malien_-_algx.html",
profileUrl: "https://www.instagram.com/malien___/",
hydraFunction: hydraAngelJara10,
},
{
url: "audio/Malien_-_crouwu.mp3",
artistName: "Malien",
trackName: "crouwu",
codeUrl: "codeScripts/Malien_-_crouwu.html",
profileUrl: "https://www.instagram.com/malien___/",
hydraFunction: hydraAngelJara11,
},
];
/*
* Reproduce pista actual, indicada por currentTrackIdx
*
* También inicializa el contexto de audio por primera vez.
*/
function play() {
initializeContext();
// Start playing
playing = true;
updateTrack();
// Update icons
const icon = document.getElementById("playIcon");
icon.classList.add("fa-stop-circle");
icon.classList.remove("fa-play-circle");
// Show next/prev buttons now
document.getElementById("prev").classList.remove("hidden");
document.getElementById("next").classList.remove("hidden");
}
/*
* Ejecuta la función de Hydra de la pista
*/
function runHydra() {
// Clear hydra first
hush();
// Run hydra code from current track
const track = playlist[currentTrackIdx];
track.hydraFunction();
}
/*
* Actualiza los datos de la pista en el reproductor, y se asegura de ejecutar
* el reproductor y actualizar el código de Hydra
*/
function updateTrack() {
const track = playlist[currentTrackIdx];
const title = `${track.artistName} - ${track.trackName}`;
console.log("Current track:", title);
const audioEl = document.getElementById("audio");
audioEl.src = track.url;
if (playing) {
audioEl.play();
runHydra();
}
// Update artist name
const artistNameEl = document.getElementById("artistName");
artistNameEl.innerText = track.artistName;
artistNameEl.setAttribute("href", track.profileUrl || "#")
// Update track name and href
const trackNameEl = document.getElementById("trackName");
trackNameEl.innerText = track.trackName;
trackNameEl.setAttribute("href", track.codeUrl || "#");
}
/*
* Reproduce o pausa dependiendo del estado
*/
function playPause() {
if (playing) {
pause()
} else {
play()
}
}
/*
* Pausa la reproducción
*/
function pause() {
const audioEl = document.getElementById("audio");
audioEl.pause();
playing = false;
// Update icons
var button = document.getElementById("playIcon");
button.classList.add("fa-play-circle");
button.classList.remove("fa-stop-circle");
}
/*
* Avanza a la siguiente pista
*
* Usa la función mod (módulo) para hacer una lista circular. Es decir, si
* está en la última pista, vuelve a la primera.
*/
function next() {
currentTrackIdx = (currentTrackIdx + 1).mod(playlist.length);
updateTrack();
}
/*
* Vuelve a la anterior pista
*
* Usa la función mod (módulo) para hacer una lista circular. Es decir, si
* está en la primer pista, va a la última.
*/
function previous() {
currentTrackIdx = (currentTrackIdx - 1).mod(playlist.length);
updateTrack();
}
/*
* Inicializa Hydra y ajusta el canvas.
*
* Se ejecuta al cargar la página (evento onload)
*/
function init() {
// Set canvas size to full screen
var canvas = document.getElementById("canvas");
canvas.width = window.innerWidth * window.devicePixelRatio;
canvas.height = window.innerHeight * window.devicePixelRatio;
// Create a new hydra-synth instance with audio disabled
hydra = new window.Hydra({
canvas: document.getElementById("canvas"),
detectAudio: false
});
// Shuffle playlist!
playlist = shuffle(playlist);
// Make container visible
const container = document.getElementById("interactionContainer");
container.classList.remove("hidden");
}
/*
* Inicializa el contexto de audio y fuente para Hydra
*
* También setea eventos generales del <audio>.
*/
function initializeContext() {
// Initialize audio context and source, if it's the first time
if (!context || !source) {
context = new AudioContext();
const audioEl = document.getElementById("audio");
source = context.createMediaElementSource(audioEl);
source.connect(context.destination);
// Go to next track when current track finishes
audioEl.addEventListener('ended', (event) => {
next();
});
audioEl.addEventListener('waiting', () => {
const icon = document.getElementById("playIcon");
icon.classList.remove("fa-play-circle", "fa-stop-circle");
icon.classList.add("fa-spinner", "fa-spin");
});
audioEl.addEventListener('playing', () => {
const icon = document.getElementById("playIcon");
icon.classList.remove("fa-spinner", "fa-spin");
if (playing) {
icon.classList.add("fa-stop-circle");
} else {
icon.classList.add("fa-play-circle");
}
});
// Initialize custom audio. This CustomAudio is similar to Audio from hydra,
// but receives an audio context and source, for audio analysis, instead of
// the microphone.
hydra.synth.a = new CustomAudio({
context: context,
source: source
});
window.a = hydra.synth.a; // set global variable for audio
hydra.detectAudio = true; // now force hydra to tick audio too
}
}
/*
* Devuelve una copia de array mezclada.
*/
function shuffle(array) {
var i, j, x;
for (i = array.length - 1; i > 0; i--) {
j = Math.floor(Math.random() * (i + 1));
x = array[i];
array[i] = array[j];
array[j] = x;
}
return array;
}
// Cuando se cargue la página, ejecuta la función `init`
window.addEventListener("load", init, false);