Skip to content

Commit 8e85d28

Browse files
add audio examples
1 parent 8d5d810 commit 8e85d28

File tree

6 files changed

+265
-0
lines changed

6 files changed

+265
-0
lines changed

examples/audio/audio_sound_loading.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
"""checked with raylib-python-cffi 5.5.0.2
2+
raylib [audio] example - Sound loading and playing
3+
Example complexity rating: [★☆☆☆] 1/4
4+
Example originally created with raylib 1.1, last time updated with raylib 3.5
5+
Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
6+
BSD-like license that allows static linking with closed source software
7+
Copyright (c) 2014-2025 Ramon Santamaria (@raysan5)
8+
9+
This source has been converted from C raylib examples to Python.
10+
"""
11+
12+
import pyray as rl
13+
from pathlib import Path
14+
15+
# Get the directory where this script is located
16+
THIS_DIR = Path(__file__).resolve().parent
17+
18+
# Initialization
19+
# --------------------------------------------------------------------------------------
20+
screen_width = 800
21+
screen_height = 450
22+
23+
rl.init_window(
24+
screen_width, screen_height, "raylib [audio] example - sound loading and playing"
25+
)
26+
27+
rl.init_audio_device() # Initialize audio device
28+
29+
# Load WAV audio file using proper path resolution
30+
fx_wav = rl.load_sound(str(THIS_DIR / "resources/sound.wav"))
31+
# Load OGG audio file using proper path resolution
32+
fx_ogg = rl.load_sound(str(THIS_DIR / "resources/target.ogg"))
33+
34+
rl.set_target_fps(60) # Set our game to run at 60 frames-per-second
35+
# --------------------------------------------------------------------------------------
36+
37+
# Main game loop
38+
while not rl.window_should_close(): # Detect window close button or ESC key
39+
# Update
40+
# ----------------------------------------------------------------------------------
41+
if rl.is_key_pressed(rl.KeyboardKey.KEY_SPACE):
42+
rl.play_sound(fx_wav) # Play WAV sound
43+
if rl.is_key_pressed(rl.KeyboardKey.KEY_ENTER):
44+
rl.play_sound(fx_ogg) # Play OGG sound
45+
# ----------------------------------------------------------------------------------
46+
47+
# Draw
48+
# ----------------------------------------------------------------------------------
49+
rl.begin_drawing()
50+
51+
rl.clear_background(rl.RAYWHITE)
52+
53+
rl.draw_text("Press SPACE to PLAY the WAV sound!", 200, 180, 20, rl.LIGHTGRAY)
54+
rl.draw_text("Press ENTER to PLAY the OGG sound!", 200, 220, 20, rl.LIGHTGRAY)
55+
56+
rl.end_drawing()
57+
# ----------------------------------------------------------------------------------
58+
59+
# De-Initialization
60+
# --------------------------------------------------------------------------------------
61+
rl.unload_sound(fx_wav) # Unload sound data
62+
rl.unload_sound(fx_ogg) # Unload sound data
63+
64+
rl.close_audio_device() # Close audio device
65+
66+
rl.close_window() # Close window and OpenGL context
67+
# --------------------------------------------------------------------------------------

examples/audio/audio_sound_multi.py

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
"""checked with raylib-python-cffi 5.5.0.2
2+
raylib [audio] example - Playing sound multiple times
3+
Example complexity rating: [★★☆☆] 2/4
4+
Example originally created with raylib 4.6, last time updated with raylib 4.6
5+
Example contributed by Jeffery Myers (@JeffM2501) and reviewed by Ramon Santamaria (@raysan5)
6+
Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
7+
BSD-like license that allows static linking with closed source software
8+
Copyright (c) 2023-2025 Jeffery Myers (@JeffM2501)
9+
10+
This source has been converted from C raylib examples to Python.
11+
"""
12+
13+
from typing import List
14+
15+
import pyray as rl
16+
from pathlib import Path
17+
18+
# Get the directory where this script is located
19+
THIS_DIR = Path(__file__).resolve().parent
20+
21+
MAX_SOUNDS = 10
22+
sound_array: List[rl.Sound] = []
23+
current_sound = 0
24+
25+
# Initialization
26+
# --------------------------------------------------------------------------------------
27+
screen_width = 800
28+
screen_height = 450
29+
30+
rl.init_window(
31+
screen_width, screen_height, "raylib [audio] example - playing sound multiple times"
32+
)
33+
34+
rl.init_audio_device() # Initialize audio device
35+
36+
# Load the sound list
37+
sound_array.append(
38+
rl.load_sound(str(THIS_DIR / "resources/sound.wav"))
39+
) # Load WAV audio file into the first slot as the 'source' sound
40+
# this sound owns the sample data
41+
for i in range(1, MAX_SOUNDS):
42+
sound_array.append(
43+
rl.load_sound_alias(sound_array[0])
44+
) # Load an alias of the sound into slots 1-9
45+
# These do not own the sound data, but can be played
46+
current_sound = 0 # Set the sound list to the start
47+
48+
rl.set_target_fps(60) # Set our game to run at 60 frames-per-second
49+
# --------------------------------------------------------------------------------------
50+
51+
# Main game loop
52+
while not rl.window_should_close(): # Detect window close button or ESC key
53+
# Update
54+
# ----------------------------------------------------------------------------------
55+
if rl.is_key_pressed(rl.KeyboardKey.KEY_SPACE):
56+
rl.play_sound(sound_array[current_sound]) # Play the next open sound slot
57+
current_sound += 1 # Increment the sound slot
58+
if (
59+
current_sound >= MAX_SOUNDS
60+
): # If the sound slot is out of bounds, go back to 0
61+
current_sound = 0
62+
63+
# Note: a better way would be to look at the list for the first sound that is not playing and use that slot
64+
# ----------------------------------------------------------------------------------
65+
66+
# Draw
67+
# ----------------------------------------------------------------------------------
68+
rl.begin_drawing()
69+
70+
rl.clear_background(rl.RAYWHITE)
71+
72+
rl.draw_text("Press SPACE to PLAY a WAV sound!", 200, 180, 20, rl.LIGHTGRAY)
73+
74+
rl.end_drawing()
75+
# ----------------------------------------------------------------------------------
76+
77+
# De-Initialization
78+
# --------------------------------------------------------------------------------------
79+
for i in range(1, MAX_SOUNDS):
80+
rl.unload_sound_alias(sound_array[i]) # Unload sound aliases
81+
rl.unload_sound(sound_array[0]) # Unload source sound data
82+
83+
rl.close_audio_device() # Close audio device
84+
85+
rl.close_window() # Close window and OpenGL context
86+
# --------------------------------------------------------------------------------------
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
"""checked with raylib-python-cffi 5.5.0.2
2+
raylib [audio] example - Playing spatialized 3D sound
3+
Example complexity rating: [★★☆☆] 2/4
4+
Example originally created with raylib 5.5, last time updated with raylib 5.5
5+
Example contributed by Le Juez Victor (@Bigfoot71) and reviewed by Ramon Santamaria (@raysan5)
6+
Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
7+
BSD-like license that allows static linking with closed source software
8+
Copyright (c) 2025 Le Juez Victor (@Bigfoot71)
9+
10+
This source has been converted from C raylib examples to Python.
11+
"""
12+
13+
import pyray as rl
14+
import math
15+
from pathlib import Path
16+
17+
# Get the directory where this script is located
18+
THIS_DIR = Path(__file__).resolve().parent
19+
20+
21+
# Sound positioning function
22+
def set_sound_position(listener, sound, position, max_dist):
23+
# Calculate direction vector and distance between listener and sound source
24+
direction = rl.vector3_subtract(position, listener.position)
25+
distance = rl.vector3_length(direction)
26+
27+
# Apply logarithmic distance attenuation and clamp between 0-1
28+
attenuation = 1.0 / (1.0 + (distance / max_dist))
29+
attenuation = rl.clamp(attenuation, 0.0, 1.0)
30+
31+
# Calculate normalized vectors for spatial positioning
32+
normalized_direction = rl.vector3_normalize(direction)
33+
forward = rl.vector3_normalize(
34+
rl.vector3_subtract(listener.target, listener.position)
35+
)
36+
right = rl.vector3_normalize(rl.vector3_cross_product(listener.up, forward))
37+
38+
# Reduce volume for sounds behind the listener
39+
dot_product = rl.vector3_dot_product(forward, normalized_direction)
40+
if dot_product < 0.0:
41+
attenuation *= 1.0 + dot_product * 0.5
42+
43+
# Set stereo panning based on sound position relative to listener
44+
pan = 0.5 + 0.5 * rl.vector3_dot_product(normalized_direction, right)
45+
46+
# Apply final sound properties
47+
rl.set_sound_volume(sound, attenuation)
48+
rl.set_sound_pan(sound, pan)
49+
50+
51+
# Initialization
52+
# --------------------------------------------------------------------------------------
53+
screen_width = 800
54+
screen_height = 450
55+
56+
rl.init_window(
57+
screen_width, screen_height, "raylib [audio] example - Playing spatialized 3D sound"
58+
)
59+
60+
rl.init_audio_device()
61+
62+
sound = rl.load_sound(str(THIS_DIR / "resources/coin.wav"))
63+
64+
camera = rl.Camera3D(
65+
(0, 5, 5),
66+
(0, 0, 0),
67+
(0, 1, 0),
68+
60.0,
69+
rl.CameraProjection.CAMERA_PERSPECTIVE,
70+
)
71+
72+
rl.disable_cursor()
73+
74+
rl.set_target_fps(60)
75+
# --------------------------------------------------------------------------------------
76+
77+
# Main game loop
78+
while not rl.window_should_close():
79+
# Update
80+
# ----------------------------------------------------------------------------------
81+
rl.update_camera(camera, rl.CameraMode.CAMERA_FREE)
82+
83+
th = rl.get_time()
84+
85+
sphere_pos = rl.Vector3(5.0 * math.cos(th), 0.0, 5.0 * math.sin(th))
86+
87+
set_sound_position(camera, sound, sphere_pos, 20.0)
88+
if not rl.is_sound_playing(sound):
89+
rl.play_sound(sound)
90+
# ----------------------------------------------------------------------------------
91+
92+
# Draw
93+
# ----------------------------------------------------------------------------------
94+
rl.begin_drawing()
95+
96+
rl.clear_background(rl.RAYWHITE)
97+
98+
rl.begin_mode_3d(camera)
99+
rl.draw_grid(10, 2)
100+
rl.draw_sphere(sphere_pos, 0.5, rl.RED)
101+
rl.end_mode_3d()
102+
103+
rl.end_drawing()
104+
# ----------------------------------------------------------------------------------
105+
106+
# De-Initialization
107+
# --------------------------------------------------------------------------------------
108+
rl.unload_sound(sound)
109+
rl.close_audio_device() # Close audio device
110+
111+
rl.close_window() # Close window and OpenGL context
112+
# --------------------------------------------------------------------------------------

examples/audio/resources/coin.wav

17.7 KB
Binary file not shown.

examples/audio/resources/sound.wav

95.2 KB
Binary file not shown.

examples/audio/resources/target.ogg

13.1 KB
Binary file not shown.

0 commit comments

Comments
 (0)