diff --git a/examples/audio/audio_sound_loading.py b/examples/audio/audio_sound_loading.py new file mode 100644 index 0000000..3854293 --- /dev/null +++ b/examples/audio/audio_sound_loading.py @@ -0,0 +1,67 @@ +"""checked with raylib-python-cffi 5.5.0.2 +raylib [audio] example - Sound loading and playing +Example complexity rating: [★☆☆☆] 1/4 +Example originally created with raylib 1.1, last time updated with raylib 3.5 +Example licensed under an unmodified zlib/libpng license, which is an OSI-certified, +BSD-like license that allows static linking with closed source software +Copyright (c) 2014-2025 Ramon Santamaria (@raysan5) + +This source has been converted from C raylib examples to Python. +""" + +import pyray as rl +from pathlib import Path + +# Get the directory where this script is located +THIS_DIR = Path(__file__).resolve().parent + +# Initialization +# -------------------------------------------------------------------------------------- +screen_width = 800 +screen_height = 450 + +rl.init_window( + screen_width, screen_height, "raylib [audio] example - sound loading and playing" +) + +rl.init_audio_device() # Initialize audio device + +# Load WAV audio file using proper path resolution +fx_wav = rl.load_sound(str(THIS_DIR / "resources/sound.wav")) +# Load OGG audio file using proper path resolution +fx_ogg = rl.load_sound(str(THIS_DIR / "resources/target.ogg")) + +rl.set_target_fps(60) # Set our game to run at 60 frames-per-second +# -------------------------------------------------------------------------------------- + +# Main game loop +while not rl.window_should_close(): # Detect window close button or ESC key + # Update + # ---------------------------------------------------------------------------------- + if rl.is_key_pressed(rl.KeyboardKey.KEY_SPACE): + rl.play_sound(fx_wav) # Play WAV sound + if rl.is_key_pressed(rl.KeyboardKey.KEY_ENTER): + rl.play_sound(fx_ogg) # Play OGG sound + # ---------------------------------------------------------------------------------- + + # Draw + # ---------------------------------------------------------------------------------- + rl.begin_drawing() + + rl.clear_background(rl.RAYWHITE) + + rl.draw_text("Press SPACE to PLAY the WAV sound!", 200, 180, 20, rl.LIGHTGRAY) + rl.draw_text("Press ENTER to PLAY the OGG sound!", 200, 220, 20, rl.LIGHTGRAY) + + rl.end_drawing() + # ---------------------------------------------------------------------------------- + +# De-Initialization +# -------------------------------------------------------------------------------------- +rl.unload_sound(fx_wav) # Unload sound data +rl.unload_sound(fx_ogg) # Unload sound data + +rl.close_audio_device() # Close audio device + +rl.close_window() # Close window and OpenGL context +# -------------------------------------------------------------------------------------- diff --git a/examples/audio/audio_sound_multi.py b/examples/audio/audio_sound_multi.py new file mode 100644 index 0000000..0cc6c82 --- /dev/null +++ b/examples/audio/audio_sound_multi.py @@ -0,0 +1,86 @@ +"""checked with raylib-python-cffi 5.5.0.2 +raylib [audio] example - Playing sound multiple times +Example complexity rating: [★★☆☆] 2/4 +Example originally created with raylib 4.6, last time updated with raylib 4.6 +Example contributed by Jeffery Myers (@JeffM2501) and reviewed by Ramon Santamaria (@raysan5) +Example licensed under an unmodified zlib/libpng license, which is an OSI-certified, +BSD-like license that allows static linking with closed source software +Copyright (c) 2023-2025 Jeffery Myers (@JeffM2501) + +This source has been converted from C raylib examples to Python. +""" + +from typing import List + +import pyray as rl +from pathlib import Path + +# Get the directory where this script is located +THIS_DIR = Path(__file__).resolve().parent + +MAX_SOUNDS = 10 +sound_array: List[rl.Sound] = [] +current_sound = 0 + +# Initialization +# -------------------------------------------------------------------------------------- +screen_width = 800 +screen_height = 450 + +rl.init_window( + screen_width, screen_height, "raylib [audio] example - playing sound multiple times" +) + +rl.init_audio_device() # Initialize audio device + +# Load the sound list +sound_array.append( + rl.load_sound(str(THIS_DIR / "resources/sound.wav")) +) # Load WAV audio file into the first slot as the 'source' sound +# this sound owns the sample data +for i in range(1, MAX_SOUNDS): + sound_array.append( + rl.load_sound_alias(sound_array[0]) + ) # Load an alias of the sound into slots 1-9 + # These do not own the sound data, but can be played +current_sound = 0 # Set the sound list to the start + +rl.set_target_fps(60) # Set our game to run at 60 frames-per-second +# -------------------------------------------------------------------------------------- + +# Main game loop +while not rl.window_should_close(): # Detect window close button or ESC key + # Update + # ---------------------------------------------------------------------------------- + if rl.is_key_pressed(rl.KeyboardKey.KEY_SPACE): + rl.play_sound(sound_array[current_sound]) # Play the next open sound slot + current_sound += 1 # Increment the sound slot + if ( + current_sound >= MAX_SOUNDS + ): # If the sound slot is out of bounds, go back to 0 + current_sound = 0 + + # Note: a better way would be to look at the list for the first sound that is not playing and use that slot + # ---------------------------------------------------------------------------------- + + # Draw + # ---------------------------------------------------------------------------------- + rl.begin_drawing() + + rl.clear_background(rl.RAYWHITE) + + rl.draw_text("Press SPACE to PLAY a WAV sound!", 200, 180, 20, rl.LIGHTGRAY) + + rl.end_drawing() + # ---------------------------------------------------------------------------------- + +# De-Initialization +# -------------------------------------------------------------------------------------- +for i in range(1, MAX_SOUNDS): + rl.unload_sound_alias(sound_array[i]) # Unload sound aliases +rl.unload_sound(sound_array[0]) # Unload source sound data + +rl.close_audio_device() # Close audio device + +rl.close_window() # Close window and OpenGL context +# -------------------------------------------------------------------------------------- diff --git a/examples/audio/audio_sound_positioning.py b/examples/audio/audio_sound_positioning.py new file mode 100644 index 0000000..f3e77cf --- /dev/null +++ b/examples/audio/audio_sound_positioning.py @@ -0,0 +1,112 @@ +"""checked with raylib-python-cffi 5.5.0.2 +raylib [audio] example - Playing spatialized 3D sound +Example complexity rating: [★★☆☆] 2/4 +Example originally created with raylib 5.5, last time updated with raylib 5.5 +Example contributed by Le Juez Victor (@Bigfoot71) and reviewed by Ramon Santamaria (@raysan5) +Example licensed under an unmodified zlib/libpng license, which is an OSI-certified, +BSD-like license that allows static linking with closed source software +Copyright (c) 2025 Le Juez Victor (@Bigfoot71) + +This source has been converted from C raylib examples to Python. +""" + +import pyray as rl +import math +from pathlib import Path + +# Get the directory where this script is located +THIS_DIR = Path(__file__).resolve().parent + + +# Sound positioning function +def set_sound_position(listener, sound, position, max_dist): + # Calculate direction vector and distance between listener and sound source + direction = rl.vector3_subtract(position, listener.position) + distance = rl.vector3_length(direction) + + # Apply logarithmic distance attenuation and clamp between 0-1 + attenuation = 1.0 / (1.0 + (distance / max_dist)) + attenuation = rl.clamp(attenuation, 0.0, 1.0) + + # Calculate normalized vectors for spatial positioning + normalized_direction = rl.vector3_normalize(direction) + forward = rl.vector3_normalize( + rl.vector3_subtract(listener.target, listener.position) + ) + right = rl.vector3_normalize(rl.vector3_cross_product(listener.up, forward)) + + # Reduce volume for sounds behind the listener + dot_product = rl.vector3_dot_product(forward, normalized_direction) + if dot_product < 0.0: + attenuation *= 1.0 + dot_product * 0.5 + + # Set stereo panning based on sound position relative to listener + pan = 0.5 + 0.5 * rl.vector3_dot_product(normalized_direction, right) + + # Apply final sound properties + rl.set_sound_volume(sound, attenuation) + rl.set_sound_pan(sound, pan) + + +# Initialization +# -------------------------------------------------------------------------------------- +screen_width = 800 +screen_height = 450 + +rl.init_window( + screen_width, screen_height, "raylib [audio] example - Playing spatialized 3D sound" +) + +rl.init_audio_device() + +sound = rl.load_sound(str(THIS_DIR / "resources/coin.wav")) + +camera = rl.Camera3D( + (0, 5, 5), + (0, 0, 0), + (0, 1, 0), + 60.0, + rl.CameraProjection.CAMERA_PERSPECTIVE, +) + +rl.disable_cursor() + +rl.set_target_fps(60) +# -------------------------------------------------------------------------------------- + +# Main game loop +while not rl.window_should_close(): + # Update + # ---------------------------------------------------------------------------------- + rl.update_camera(camera, rl.CameraMode.CAMERA_FREE) + + th = rl.get_time() + + sphere_pos = rl.Vector3(5.0 * math.cos(th), 0.0, 5.0 * math.sin(th)) + + set_sound_position(camera, sound, sphere_pos, 20.0) + if not rl.is_sound_playing(sound): + rl.play_sound(sound) + # ---------------------------------------------------------------------------------- + + # Draw + # ---------------------------------------------------------------------------------- + rl.begin_drawing() + + rl.clear_background(rl.RAYWHITE) + + rl.begin_mode_3d(camera) + rl.draw_grid(10, 2) + rl.draw_sphere(sphere_pos, 0.5, rl.RED) + rl.end_mode_3d() + + rl.end_drawing() + # ---------------------------------------------------------------------------------- + +# De-Initialization +# -------------------------------------------------------------------------------------- +rl.unload_sound(sound) +rl.close_audio_device() # Close audio device + +rl.close_window() # Close window and OpenGL context +# -------------------------------------------------------------------------------------- diff --git a/examples/audio/resources/coin.wav b/examples/audio/resources/coin.wav new file mode 100644 index 0000000..ad95bfb Binary files /dev/null and b/examples/audio/resources/coin.wav differ diff --git a/examples/audio/resources/sound.wav b/examples/audio/resources/sound.wav new file mode 100644 index 0000000..b5d01c9 Binary files /dev/null and b/examples/audio/resources/sound.wav differ diff --git a/examples/audio/resources/target.ogg b/examples/audio/resources/target.ogg new file mode 100644 index 0000000..2b73e1c Binary files /dev/null and b/examples/audio/resources/target.ogg differ