-
-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathaudio.js
115 lines (97 loc) · 2.45 KB
/
audio.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
// @ts-check
// Playing audio and controlling it
kaplay({
// This makes it so the audio doesn't pause when the tab is changed
backgroundAudio: true,
background: "5ba675",
});
// Loads the bell sound
loadSound("bell", "/examples/sounds/bell.mp3");
// Load the music, it makes it being streamed, so loading is faster
loadMusic("OtherworldlyFoe", "/examples/sounds/OtherworldlyFoe.mp3");
loadSprite("bag", "/sprites/bag.png");
// Adjust global volume
volume(0.5);
// We use the play() function to play audio
onKeyPress("enter", () => {
play("bell", {
volume: 1,
speed: 1,
});
});
// For our mobile friends
onTouchStart(() => {
play("bell", {
volume: 1,
speed: 1,
});
});
// We can also play music, and control it
const music = play("OtherworldlyFoe", {
loop: true,
paused: true,
});
const label = add([
text(),
pos(10, 10),
]);
// See below for the function
updateText();
// Update text every frame
onUpdate(() => {
updateText();
});
// Adjust music properties through input
onKeyPress("space", () => music.paused = !music.paused);
onKeyPressRepeat("up", () => music.volume += 0.1);
onKeyPressRepeat("down", () => music.volume -= 0.1);
onKeyPressRepeat("left", () => music.speed -= 0.1);
onKeyPressRepeat("right", () => music.speed += 0.1);
onKeyPress("m", () => music.seek(4.24));
// Piano
// We store some keys in a string
const keyboard = "awsedftgyhujk";
// Simple piano with "bell" sound and the second row of a QWERTY keyboard
for (let i = 0; i < keyboard.length; i++) {
onKeyPress(keyboard[i], () => {
play("bell", {
// The original "bell" sound is F, -500 will make it C for the first key
detune: i * 100 - 500,
});
});
}
// Draw music progress bar
onDraw(() => {
if (!music.duration()) return;
const h = 16;
drawRect({
pos: vec2(0, height() - h),
width: music.time() / music.duration() * width(),
height: h,
});
});
// The rotating bag
const bag = add([
sprite("bag"),
pos(center()),
anchor("center"),
rotate(0),
scale(2),
]);
bag.onUpdate(() => {
if (music.paused) return;
bag.angle += dt() * 100;
});
// Create text guide
function updateText() {
label.text = `
${music.paused ? "Paused" : "Playing"}
Time: ${music.time().toFixed(2)}
Volume: ${music.volume.toFixed(2)}
Speed: ${music.speed.toFixed(2)}
\\[space] play/pause
[up/down] volume
[left/right] speed
[a...k] piano
`.trim();
}