Skip to content

Instantly share code, notes, and snippets.

@Bigfoot71
Last active April 10, 2025 00:32
Show Gist options
  • Save Bigfoot71/ac839e78659b29a9adb17d4c755b73ed to your computer and use it in GitHub Desktop.
Save Bigfoot71/ac839e78659b29a9adb17d4c755b73ed to your computer and use it in GitHub Desktop.
Sound spatialization with raylib
#include <raylib.h>
#include <raymath.h>
static void SetSoundPosition(Camera listener, Sound sound, Vector3 position, float maxDist)
{
// Calculate direction vector and distance between listener and sound source
Vector3 direction = Vector3Subtract(position, listener.position);
float distance = Vector3Length(direction);
// Apply logarithmic distance attenuation and clamp between 0-1
float attenuation = 1.0f / (1.0f + (distance / maxDist));
attenuation = Clamp(attenuation, 0.0f, 1.0f);
// Calculate normalized vectors for spatial positioning
Vector3 normalizedDirection = Vector3Normalize(direction);
Vector3 forward = Vector3Normalize(Vector3Subtract(listener.target, listener.position));
Vector3 right = Vector3Normalize(Vector3CrossProduct(forward, listener.up));
// Reduce volume for sounds behind the listener
float dotProduct = Vector3DotProduct(forward, normalizedDirection);
if (dotProduct < 0.0f) {
attenuation *= (1.0f + dotProduct * 0.5f);
}
// Set stereo panning based on sound position relative to listener
float pan = 0.5f + 0.5f * Vector3DotProduct(normalizedDirection, right);
// Apply final sound properties
SetSoundVolume(sound, attenuation);
SetSoundPan(sound, pan);
}
int main(void)
{
InitWindow(800, 600, "Quick Spatial Sound");
InitAudioDevice();
SetTargetFPS(60);
DisableCursor();
Sound sound = LoadSound("sound.wav");
PlaySound(sound);
Camera camera = {
.position = (Vector3) { 0, 5, 5 },
.target = (Vector3) { 0, 0, 0 },
.up = (Vector3) { 0, 1, 0 },
.fovy = 60,
};
while (!WindowShouldClose())
{
UpdateCamera(&camera, CAMERA_FREE);
float th = GetTime();
Vector3 spherePos = {
.x = 5.0f * cosf(th),
.y = 0.0f,
.z = 5.0f * sinf(th)
};
SetSoundPosition(camera, sound, spherePos, 20.0f);
if (!IsSoundPlaying(sound)) PlaySound(sound);
BeginDrawing();
{
ClearBackground(BLACK);
BeginMode3D(camera);
DrawGrid(10, 2);
DrawSphere(spherePos, 0.5f, RED);
EndMode3D();
}
EndDrawing();
}
CloseAudioDevice();
CloseWindow();
}

Basic Sound Spatialization with raylib

This code demonstrates how to perform basic 3D sound spatialization using raylib.

How the Code Works

  1. SetSoundPosition:

    • This function adjusts the volume and panning of the sound based on its position relative to the camera (listener).
    • The distance between the sound and the listener determines the volume attenuation.
    • If the sound is behind the listener, the volume is further reduced to simulate a more "realistic" effect.
    • Stereo panning is calculated based on the listener's orientation and the direction of the sound.
  2. Sound Movement:

    • The sound is played in a sphere that moves around the scene origin.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment