Skip to content

Commit 599e525

Browse files
add audio example from @ashleysommer
1 parent 15a4ee2 commit 599e525

File tree

2 files changed

+172
-0
lines changed

2 files changed

+172
-0
lines changed
Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
"""
2+
3+
raylib [audio] example - playing
4+
5+
"""
6+
import dataclasses
7+
import pyray
8+
import raylib as rl
9+
from raylib.colors import (
10+
RAYWHITE,
11+
ORANGE,
12+
RED,
13+
GOLD,
14+
LIME,
15+
BLUE,
16+
VIOLET,
17+
BROWN,
18+
LIGHTGRAY,
19+
PINK,
20+
YELLOW,
21+
GREEN,
22+
SKYBLUE,
23+
PURPLE,
24+
BEIGE,
25+
MAROON,
26+
GRAY,
27+
BLACK
28+
)
29+
30+
31+
MAX_CIRCLES=64
32+
33+
34+
@dataclasses.dataclass
35+
class CircleWave:
36+
position: 'rl.Vector2'
37+
radius: float
38+
alpha: float
39+
speed: float
40+
color: 'rl.Color'
41+
42+
43+
screenWidth = 800
44+
screenHeight = 450
45+
46+
rl.SetConfigFlags(rl.FLAG_MSAA_4X_HINT) # NOTE: Try to enable MSAA 4X
47+
48+
rl.InitWindow(screenWidth, screenHeight, b"raylib [audio] example - module playing (streaming)")
49+
50+
rl.InitAudioDevice() # Initialize audio device
51+
52+
colors = [ ORANGE, RED, GOLD, LIME, BLUE, VIOLET, BROWN, LIGHTGRAY, PINK,
53+
YELLOW, GREEN, SKYBLUE, PURPLE, BEIGE ]
54+
55+
# Creates some circles for visual effect
56+
circles = []
57+
58+
for i in range(MAX_CIRCLES):
59+
rad = rl.GetRandomValue(10, 40)
60+
pos = pyray.Vector2(
61+
float(rl.GetRandomValue(rad, int(screenWidth) - rad)),
62+
float(rl.GetRandomValue(rad, int(screenHeight) - rad))
63+
)
64+
c = CircleWave(
65+
alpha=0.0,
66+
radius=float(rad),
67+
position=pos,
68+
speed=float(rl.GetRandomValue(1, 100))/2000.0,
69+
color=colors[rl.GetRandomValue(0, 13)]
70+
)
71+
circles.append(c)
72+
73+
music = rl.LoadMusicStream(b"resources/mini1111.xm")
74+
music.looping = False
75+
pitch = 1.0
76+
77+
rl.PlayMusicStream(music)
78+
timePlayed = 0.0
79+
pause = False
80+
81+
rl.SetTargetFPS(60) # Set our game to run at 60 frames-per-second
82+
83+
84+
# Main game loop
85+
while not rl.WindowShouldClose(): # Detect window close button or ESC key
86+
# Update
87+
#----------------------------------------------------------------------------------
88+
rl.UpdateMusicStream(music) # Update music buffer with new stream data
89+
90+
# Restart music playing (stop and play)
91+
if rl.IsKeyPressed(rl.KEY_SPACE):
92+
rl.StopMusicStream(music)
93+
rl.PlayMusicStream(music)
94+
pause = False
95+
96+
# Pause/Resume music playing
97+
if rl.IsKeyPressed(rl.KEY_P):
98+
pause = not pause
99+
if pause:
100+
rl.PauseMusicStream(music)
101+
else:
102+
rl.ResumeMusicStream(music)
103+
104+
105+
if rl.IsKeyDown(rl.KEY_DOWN):
106+
pitch -= 0.01
107+
elif rl.IsKeyDown(rl.KEY_UP):
108+
pitch += 0.01
109+
110+
rl.SetMusicPitch(music, pitch)
111+
112+
# Get timePlayed scaled to bar dimensions
113+
timePlayed = (rl.GetMusicTimePlayed(music) / rl.GetMusicTimeLength(music))*(screenWidth - 40)
114+
115+
# Color circles animation
116+
for i in range(MAX_CIRCLES):
117+
if pause:
118+
break
119+
120+
circles[i].alpha += circles[i].speed
121+
circles[i].radius += circles[i].speed*10.0
122+
123+
if circles[i].alpha > 1.0:
124+
circles[i].speed *= -1
125+
126+
if circles[i].alpha <= 0.0:
127+
circles[i].alpha = 0.0
128+
rad = rl.GetRandomValue(10, 40)
129+
pos = pyray.Vector2(
130+
float(rl.GetRandomValue(rad, int(screenWidth) - rad)),
131+
float(rl.GetRandomValue(rad, int(screenHeight) - rad))
132+
)
133+
circles[i].position = pos
134+
circles[i].radius = float(rad)
135+
circles[i].speed = float(rl.GetRandomValue(1, 100)) / 2000.0
136+
circles[i].color = colors[rl.GetRandomValue(0, 13)]
137+
138+
#----------------------------------------------------------------------------------
139+
140+
# Draw
141+
#----------------------------------------------------------------------------------
142+
pyray.begin_drawing()
143+
144+
pyray.clear_background(RAYWHITE)
145+
146+
for i in range(MAX_CIRCLES):
147+
pyray.draw_circle_v(circles[i].position, circles[i].radius, rl.Fade(circles[i].color, circles[i].alpha))
148+
149+
# Draw time bar
150+
pyray.draw_rectangle(20, screenHeight - 20 - 12, screenWidth - 40, 12, LIGHTGRAY)
151+
pyray.draw_rectangle(20, screenHeight - 20 - 12, int(timePlayed), 12, MAROON)
152+
pyray.draw_rectangle_lines(20, screenHeight - 20 - 12, screenWidth - 40, 12, GRAY)
153+
154+
# Draw help instructions
155+
pyray.draw_rectangle(20, 20, 425, 145, RAYWHITE)
156+
pyray.draw_rectangle_lines(20, 20, 425, 145, GRAY)
157+
pyray.draw_text("PRESS SPACE TO RESTART MUSIC", 40, 40, 20, BLACK)
158+
pyray.draw_text("PRESS P TO PAUSE/RESUME", 40, 70, 20, BLACK)
159+
pyray.draw_text("PRESS UP/DOWN TO CHANGE SPEED", 40, 100, 20, BLACK)
160+
pyray.draw_text(f"SPEED: {pitch}", 40, 130, 20, MAROON)
161+
162+
pyray.end_drawing()
163+
#----------------------------------------------------------------------------------
164+
165+
166+
# De-Initialization
167+
#--------------------------------------------------------------------------------------
168+
rl.UnloadMusicStream(music) # Unload music stream buffers from RAM
169+
170+
rl.CloseAudioDevice() # Close audio device (music streaming is automatically stopped)
171+
172+
rl.CloseWindow() # Close window and OpenGL context

examples/audio/resources/mini1111.xm

25.1 KB
Binary file not shown.

0 commit comments

Comments
 (0)