|
| 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 | +# -------------------------------------------------------------------------------------- |
0 commit comments