audio_sound_positioning.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*******************************************************************************************
  2. *
  3. * raylib [audio] example - sound positioning
  4. *
  5. * Example complexity rating: [★★☆☆] 2/4
  6. *
  7. * Example originally created with raylib 5.5, last time updated with raylib 5.5
  8. *
  9. * Example contributed by Le Juez Victor (@Bigfoot71) and reviewed by Ramon Santamaria (@raysan5)
  10. *
  11. * Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
  12. * BSD-like license that allows static linking with closed source software
  13. *
  14. * Copyright (c) 2025 Le Juez Victor (@Bigfoot71)
  15. *
  16. ********************************************************************************************/
  17. #include "raylib.h"
  18. #include "raymath.h"
  19. //------------------------------------------------------------------------------------
  20. // Module Functions Declaration
  21. //------------------------------------------------------------------------------------
  22. static void SetSoundPosition(Camera listener, Sound sound, Vector3 position, float maxDist);
  23. //------------------------------------------------------------------------------------
  24. // Program main entry point
  25. //------------------------------------------------------------------------------------
  26. int main(void)
  27. {
  28. // Initialization
  29. //--------------------------------------------------------------------------------------
  30. const int screenWidth = 800;
  31. const int screenHeight = 450;
  32. InitWindow(screenWidth, screenHeight, "raylib [audio] example - sound positioning");
  33. InitAudioDevice();
  34. Sound sound = LoadSound("resources/coin.wav");
  35. Camera camera = {
  36. .position = (Vector3) { 0, 5, 5 },
  37. .target = (Vector3) { 0, 0, 0 },
  38. .up = (Vector3) { 0, 1, 0 },
  39. .fovy = 60,
  40. .projection = CAMERA_PERSPECTIVE
  41. };
  42. DisableCursor();
  43. SetTargetFPS(60);
  44. //--------------------------------------------------------------------------------------
  45. // Main game loop
  46. while (!WindowShouldClose())
  47. {
  48. // Update
  49. //----------------------------------------------------------------------------------
  50. UpdateCamera(&camera, CAMERA_FREE);
  51. float th = (float)GetTime();
  52. Vector3 spherePos = {
  53. .x = 5.0f*cosf(th),
  54. .y = 0.0f,
  55. .z = 5.0f*sinf(th)
  56. };
  57. SetSoundPosition(camera, sound, spherePos, 20.0f);
  58. if (!IsSoundPlaying(sound)) PlaySound(sound);
  59. //----------------------------------------------------------------------------------
  60. // Draw
  61. //----------------------------------------------------------------------------------
  62. BeginDrawing();
  63. ClearBackground(RAYWHITE);
  64. BeginMode3D(camera);
  65. DrawGrid(10, 2);
  66. DrawSphere(spherePos, 0.5f, RED);
  67. EndMode3D();
  68. EndDrawing();
  69. //----------------------------------------------------------------------------------
  70. }
  71. // De-Initialization
  72. //--------------------------------------------------------------------------------------
  73. UnloadSound(sound);
  74. CloseAudioDevice(); // Close audio device
  75. CloseWindow(); // Close window and OpenGL context
  76. //--------------------------------------------------------------------------------------
  77. return 0;
  78. }
  79. //------------------------------------------------------------------------------------
  80. // Module Functions Definition
  81. //------------------------------------------------------------------------------------
  82. // Set sound 3d position
  83. static void SetSoundPosition(Camera listener, Sound sound, Vector3 position, float maxDist)
  84. {
  85. // Calculate direction vector and distance between listener and sound source
  86. Vector3 direction = Vector3Subtract(position, listener.position);
  87. float distance = Vector3Length(direction);
  88. // Apply logarithmic distance attenuation and clamp between 0-1
  89. float attenuation = 1.0f/(1.0f + (distance/maxDist));
  90. attenuation = Clamp(attenuation, 0.0f, 1.0f);
  91. // Calculate normalized vectors for spatial positioning
  92. Vector3 normalizedDirection = Vector3Normalize(direction);
  93. Vector3 forward = Vector3Normalize(Vector3Subtract(listener.target, listener.position));
  94. Vector3 right = Vector3Normalize(Vector3CrossProduct(listener.up, forward));
  95. // Reduce volume for sounds behind the listener
  96. float dotProduct = Vector3DotProduct(forward, normalizedDirection);
  97. if (dotProduct < 0.0f) attenuation *= (1.0f + dotProduct*0.5f);
  98. // Set stereo panning based on sound position relative to listener
  99. float pan = 0.5f + 0.5f*Vector3DotProduct(normalizedDirection, right);
  100. // Apply final sound properties
  101. SetSoundVolume(sound, attenuation);
  102. SetSoundPan(sound, pan);
  103. }