Skip to content

[raudio] Added 3D Audio System #4934

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed

Conversation

paganswrath
Copy link
Contributor

This change adds a handful of functions to raudio that allow raylib to play sound in 3D spaces with ease. The 3D audio system doesn't make any changes to raudio, it builds on top of preexisting functions for sanity's sake. I plan on adding music streams too at a later date.

I have verified that this change doesn't break any builds.

Added functions

void InitAudioSystem3D();                                       // Initialize the 3D Functionality 
void UnloadAudioSystem3D();                                     // Clears all the allocated 3D Sounds
void UpdateAudioSystem3D();                                     // Used to update playback on 3D Sounds
void BindListenerToCamera(Camera3D camera);                     // Sets playback to camera position, Listener must be bound to a camera for playback to be accurate. 
Sound3D LoadSound3D(const char *path);                          // Load from a file 
void UnloadSound3D(Sound3D sound3D);                            // Free up memory 
void PlaySound3D(Sound3D sound3D, Vector3 position, float maxVolume, float minDistance, float maxDistance);  // Play sound at position and parameters 
void StopSound3D(Sound3D sound3D);                              // Stop sound 
bool IsSound3DPlaying(Sound3D sound3D);                         // Check if sound is playing. 

Basic Example Code

#include <raylib.h>

int main(){
    InitWindow(1280, 720, "3D Sound Test");

    InitAudioDevice();
    InitAudioSystem3D(); // Must Init

    SetTargetFPS(60);


    Camera3D camera = { 0 }; // Basic camera setup

    camera.position = (Vector3){ 10.0f, 2.0f, 10.0f };
    camera.target = (Vector3){ 0.0f, 0.0f, 0.0f };      
    camera.up = (Vector3){ 0.0f, 1.0f, 0.0f };        
    camera.fovy = 45.0f;                     
    camera.projection = CAMERA_PERSPECTIVE;          

    Vector3 FirePosition = { 0.0f, 0.0f, 0.0f };

    Sound3D FireSound = LoadSound3D("Data/Fire.mp3"); // Loading

    Model Campfire = LoadModel("Data/Campfire.glb");

    PlaySound3D(FireSound, FirePosition, 100.0f, 0.0f, 40.f);

    while (!WindowShouldClose()){
        BeginDrawing();
            ClearBackground(RAYWHITE);

            UpdateCamera(&camera, CAMERA_FIRST_PERSON);

            UpdateAudioSystem3D(); // Must update 
            BindListenerToCamera(camera); // Bind listener 3D to Camera 
            
            if (!IsSound3DPlaying(FireSound)){ // Loop sound
                PlaySound3D(FireSound, FirePosition, 100.0f, 0.0f, 40.f);
            }
            
            BeginMode3D(camera);

                DrawPoint3D(FirePosition, RED);

                DrawModel(Campfire, FirePosition, 1.0f, WHITE);

                DrawGrid(10, 1.0f);

            EndMode3D();

        EndDrawing();
    }

    CloseWindow();
    
}

@paganswrath paganswrath changed the title Added 3D Audio System [raudio] Added 3D Audio System May 10, 2025
@raysan5
Copy link
Owner

raysan5 commented May 10, 2025

@paganswrath I'm afraid this change is bigger than expected, I'm not adding so many alternative 3d functions, including additional initialization and additional structures to raudio at this moment.

@raysan5 raysan5 closed this May 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants