-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathradio.js
100 lines (79 loc) · 2.05 KB
/
radio.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
let img;
let slider;
let static;
let audio = [];
let slider_x = 80;
let slider_y = 21;
// ======================= PRELOADING =======================
function preloadAudio(prefix, suffix, staticPath, paths) {
let fullPath;
fullPath = prefix + staticPath + suffix;
static = loadSound(fullPath);
for (i in paths) {
fullPath = prefix + paths[i] + suffix;
audio.push(loadSound(fullPath));
}
}
function preload() {
img = loadImage('assets/radio/img/radio_plate.png');
slider = loadImage('assets/radio/img/radio_needle_red.png')
let paths = ['subterfuge', 'chatter', 'homewrecker', 's03', 'lly', 's06', 'e11', 'soothsayer', 'v07', 'block_me', 'whine', 'whistle', 'e07'];
preloadAudio('assets/radio/mp3/', '.mp3', 'static', paths);
window.onfocus = playAudio;
}
// ======================= AUDIO OPS =======================
function changeChannel(index) {
let zones = [220 /*subterfuge*/, 257 /*chatter*/, 296 /*homewrecker*/,
329 /*s03*/, 363 /*love like you*/, 501 /*s06*/, 436 /*e11*/,
475 /*soothsayer*/, 521 /*v07*/, 554 /*block me*/, 590 /*whine*/,
625 /*whistle*/, 661 /*e07*/];
// Default: static
let target = -1;
for (i in zones) {
if (slider_x >= zones[i]) {
target = i;
}
}
if (target == -1) {
muteExcept();
static.setVolume(1);
} else {
muteExcept(target);
static.setVolume(0.2);
if (!audio[target].isPlaying()) {
audio[target].play();
}
}
}
function playAudio()
{
// trigger sound
static.play();
}
function muteExcept(index) {
for (i in audio) {
if (i != index && !audio[i].isPaused()) {
audio[i].pause();
}
}
}
// ======================= SETUP =======================
function setup() {
createCanvas(850, 250);
static.loop()
}
// ======================= EVENT HNDL =======================
function updateSlider() {
if (keyIsDown(LEFT_ARROW) && slider_x > 80) {
slider_x -= 1;
}
else if (keyIsDown(RIGHT_ARROW) && slider_x < 735) {
slider_x += 1;
}
image(slider, slider_x, slider_y);
}
function draw() {
image(img, 0, 0);
updateSlider();
changeChannel();
}