-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathaudio_module_playing.js
More file actions
109 lines (89 loc) · 3.4 KB
/
audio_module_playing.js
File metadata and controls
109 lines (89 loc) · 3.4 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
/*******************************************************************************************
*
* raylib [audio] example - Module playing (streaming)
*
* This example has been created using raylib 1.5 (www.raylib.com)
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
*
* Copyright (c) 2016 Ramon Santamaria (@raysan5)
* Ported to javascript 2022 by David Konsumer (@konsumer)
*
********************************************************************************************/
const r = require('../../index.js')
const MAX_CIRCLES = 64
const screenWidth = 800
const screenHeight = 450
r.SetConfigFlags(r.FLAG_MSAA_4X_HINT) // NOTE: Try to enable MSAA 4X
r.InitWindow(screenWidth, screenHeight, 'raylib [audio] example - module playing (streaming)')
r.InitAudioDevice() // Initialize audio device
const colors = [r.ORANGE, r.RED, r.GOLD, r.LIME, r.BLUE, r.VIOLET, r.BROWN, r.LIGHTGRAY, r.PINK, r.YELLOW, r.GREEN, r.SKYBLUE, r.PURPLE, r.BEIGE]
const music = r.LoadMusicStream('resources/mini1111.xm')
r.PlayMusicStream(music)
music.looping = false
let pitch = 1
let pause = false
let timePlayed = 0
const circles = [...Array(MAX_CIRCLES)].map(() => {
const radius = r.GetRandomValue(10, 40)
return {
alpha: 0,
radius,
position: {
x: r.GetRandomValue(radius, screenWidth - radius),
y: r.GetRandomValue(radius, screenHeight - radius)
},
speed: r.GetRandomValue(1, 100) / 2000,
color: colors[r.GetRandomValue(0, colors.length - 1)]
}
})
r.SetTargetFPS(60)
while (!r.WindowShouldClose()) {
r.UpdateMusicStream(music) // Update music buffer with new stream data
// Restart music playing (stop and play)
if (r.IsKeyPressed(r.KEY_SPACE)) {
r.StopMusicStream(music)
r.PlayMusicStream(music)
}
// Pause/Resume music playing
if (r.IsKeyPressed(r.KEY_P)) {
pause = !pause
if (pause) r.PauseMusicStream(music)
else r.ResumeMusicStream(music)
}
if (r.IsKeyDown(r.KEY_DOWN)) pitch -= 0.01
else if (r.IsKeyDown(r.KEY_UP)) pitch += 0.01
r.SetMusicPitch(music, pitch)
// Get timePlayed scaled to bar dimensions
timePlayed = r.GetMusicTimePlayed(music) / r.GetMusicTimeLength(music) * (screenWidth - 40)
// Color circles animation
if (!pause) {
for (const circle of circles) {
circle.alpha += circle.speed
circle.radius += circle.speed * 10.0
if (circle.alpha > 1) circle.speed *= -1
if (circle.alpha <= 0) {
circle.alpha = 0
circle.radius = r.GetRandomValue(10, 40)
circle.position.x = r.GetRandomValue(circle.radius, (screenWidth - circle.radius))
circle.position.y = r.GetRandomValue(circle.radius, (screenHeight - circle.radius))
circle.color = colors[r.GetRandomValue(0, colors.length - 1)]
circle.speed = r.GetRandomValue(1, 100) / 2000.0
}
}
}
// Draw
// ----------------------------------------------------------------------------------
r.BeginDrawing()
r.ClearBackground(r.RAYWHITE)
for (const circle of circles) {
r.DrawCircleV(circle.position, circle.radius, r.Fade(circle.color, circle.alpha))
}
// Draw time bar
r.DrawRectangle(20, screenHeight - 20 - 12, screenWidth - 40, 12, r.LIGHTGRAY)
r.DrawRectangle(20, screenHeight - 20 - 12, timePlayed, 12, r.MAROON)
r.DrawRectangleLines(20, screenHeight - 20 - 12, screenWidth - 40, 12, r.GRAY)
r.EndDrawing()
}
r.UnloadMusicStream(music)
r.CloseAudioDevice()
r.CloseWindow()