Advertisement
Daemonion

what bullshit math is this

Aug 24th, 2012
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.53 KB | None | 0 0
  1. The function below uses teh maths to rotate a pair of sound files (sound_r and sound_l) along the Y axis, relative to the player's "ears" in an attempt to mimic playing a stereo audio file. However, rotating sound along the Y axis doesn't do anything to the audio; rotation along the X axis, however, will create a stereo effect.
  2.  
  3. Imagine using a pair of stereo headphones, with only a left and right channel. A value in the -X will favor the left channel, and a value in the +X will favor the right channel. Moving a theoretical sound source into +Y or -Y will have no effect on the left or right channel in a stereo setup. So, setting sound_l to a -X will play it in the left speaker, and setting sound_r to a +X will play it in the right speaker. Keeping each at 0 or neutral will play them as if they were mono files, ie dead center.
  4.  
  5. The function vector_rotate_y uses theory from a very advanced, but very niche way to handle audio called Ambisonics. https://en.wikipedia.org/wiki/Ambisonics
  6.  
  7. Here is a related .pdf with a bunch of math in it I don't understand :D
  8. http://raffayhamid.com/ICIP_2004.pdf
  9.  
  10.  
  11.  
  12. I essentially need something like this (shown below) that will have an effect on the X axis instead of the Y axis. Trouble is, I have no idea where this math came from and I have no idea how to relate it to the X axis.
  13.  
  14. for Y axis (current function):
  15. function vector_rotate_y (v, angle)
  16. angle = angle * 0.017453292519943295769236907684886 (where did they get this shit from?)
  17. local c = math.cos (angle)
  18. local s = math.sin (angle)
  19. return vector ():set (v.x * c - v.z * s, v.y, v.x * s + v.z * c)
  20. end
  21.  
  22.  
  23.  
  24. for X axis (new function I need help coding):
  25. function vector_rotate_x (v, angle)
  26. angle = angle * (some number)
  27. local c = (sin? cos?)
  28. local s = (sin? cos?)
  29. return vector ():set (formula goes here)
  30. end
  31.  
  32.  
  33.  
  34. in the game, I will then use this code to tell the game how many degrees to rotate the two opposing sound sources (I'll choose +90 for the left channel, and -90 for the right):
  35.  
  36. function action_sound2d:get_source_positions ()
  37. local actor = db.actor
  38. local dir = actor:direction ()
  39. dir.y = 0.0
  40. local dir_left = vector_rotate_y (dir, 90)
  41. local dir_right = vector_rotate_y (dir, -90)
  42. dir_left:normalize ()
  43. dir_right:normalize ()
  44. --ðàññ÷èòàåì âûñîòó
  45. local pos = actor:position ()
  46. pos.y = pos.y + 1.5 --íó íèçêîðîñëûé ó íàñ àêòåð...
  47. local pos_l = pos;
  48. return pos_l:add (dir_left:mul (1.5)), pos:add (dir_right:mul (1.5))
  49. end
  50.  
  51. ------------------------------------------------------------------------------------
  52. ------------------------------------------------------------------------------------
  53. NEW CODE WITH NEW MATHS
  54. ------------------------------------------------------------------------------------
  55. ------------------------------------------------------------------------------------
  56. function action_sound2d:get_source_positions ()
  57. local actor = db.actor
  58. local dir = actor:direction ()
  59. dir.x = 0.0 --wtf does this mean
  60. local dir_left = vector_rotate_x (dir, -90) --this should rotate the sources by 90 degrees from center, respectively ...
  61. local dir_right = vector_rotate_x (dir, 90)
  62. dir_left:normalize ()
  63. dir_right:normalize ()
  64. local pos = actor:position ()
  65. pos.x = pos.x + 1.5 --wtf does this do
  66. local pos_l = pos;
  67. return pos_l:add (dir_left:mul (1.5)), pos:add (dir_right:mul (1.5))
  68. -I think pos_l = position of the left channel audio source and pos = right channel
  69. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement