Hello Guest

Author Topic: 3D Audio  (Read 2988 times)

0 Members and 1 Guest are viewing this topic.

Fruckert

  • Member
  • Posts: 60
  • Total Square
    • View Profile
3D Audio
« on: January 10, 2015, 11:03:59 PM »
I made a pull request for 3D audio.

Here's an example of how to use it:
Code: (Program.cs) [Select]
using System;
using Otter;

namespace ThreeDeeAudioTest
{
    class Program
    {
        static void Main(string[] args)
        {
            var game = new Game();

            var scene = new Scene(game.Width, game.Height);
            scene.Add(new ListenerEntity());

            game.FirstScene = scene;

            game.Start();
        }
    }

    class ListenerEntity : Entity
    {
        private BasicMovement _movement;
        private Axis _axis;
        private Button _beeper;
        private Sound _beep;

        private Image _soundSource;

        public ListenerEntity(float x = 0, float y = 0)
            :base(x, y)
        {
            SetHitbox(16, 16);
            Collider.CenterOrigin();

            _axis = Axis.CreateArrowKeys();
            _movement = new BasicMovement(100, 100, 100) { Axis = _axis, Collider = Collider };
            _beeper = new Button();
            _beeper.AddKey(Key.Space);
           
            //If our Listener is pointing in weird directions, sound will be weird too!
            //I find Forward to work just fine, but you can do some different stuff if you want.
            Sound.ListenerDirection = Vector3.Forward;

            AddComponents(_axis, _movement, _beeper);
        }

        public override void Added()
        {
           //This is how you set up a sound. I'm not sure what good values for min distance and attenuation are yet, though.
            _beep = new Sound("beep.wav");
            _beep.Position = new Vector3(X, Y, 0f);
            _beep.MinimumDistance = 200f;
            _beep.Attenuation = 10f;
            _beep.RelativeToListener = false;

           //A visualizer to tell you where the sound is and what it's distance is.
            _soundSource = Image.CreateCircle((int)_beep.MinimumDistance);
            _soundSource.CenterOrigin();
            _soundSource.Relative = false;
            AddGraphic(_soundSource);

            base.Added();
        }

        public override void Update()
        {
            base.Update();

            //Make our camera follow us...
            Scene.CenterCamera(X, Y);

            //As well as the audio listener.
            Sound.CenterListener(X, Y);
           
            //Also, point the listener in the direction we're facing if we're facing a direction.
            if (!_axis.Neutral)
                Sound.PointListener(_axis.X, _axis.Y);

            if (_beeper.Pressed)
                _beep.Play();
        }

        public override void Render()
        {
            base.Render();

            Collider.Render();
        }
    }
}


Kyle

  • Administrator
  • Member
  • Posts: 258
    • View Profile
Re: 3D Audio
« Reply #1 on: January 10, 2015, 11:13:01 PM »
Awesome! I'll get this merged in soon :)

Fruckert

  • Member
  • Posts: 60
  • Total Square
    • View Profile
Re: 3D Audio
« Reply #2 on: January 10, 2015, 11:43:43 PM »
Thank you!
It was a fun little implementation project. Hope it can be of use.

Fruckert

  • Member
  • Posts: 60
  • Total Square
    • View Profile
Re: 3D Audio
« Reply #3 on: January 12, 2015, 12:39:40 AM »
Dropping this here, because it's useful for implementing this properly into games.

Fruckert

  • Member
  • Posts: 60
  • Total Square
    • View Profile
Re: 3D Audio
« Reply #4 on: February 03, 2015, 05:31:17 PM »
So, I'm trying to add this functionality to Music and the caching stuff is interfering with the positional stuff. Thinking about making a "StreamingSound" class to differentiate from Music, does that sound alright?

Kyle

  • Administrator
  • Member
  • Posts: 258
    • View Profile
Re: 3D Audio
« Reply #5 on: February 04, 2015, 05:13:54 PM »
Hmm I guess I'd have to see more of what the issue is.  Could you go into more detail as to what you're trying to accomplish and what's going wrong with the music?

Fruckert

  • Member
  • Posts: 60
  • Total Square
    • View Profile
Re: 3D Audio
« Reply #6 on: February 04, 2015, 05:36:50 PM »
I'm trying to implement positional stuff in Music as well, just for consistency, and because I wanted to make Fallout 3 style radios at some point for funsies.
But because of the caching, the position, attenuation, and the like gets saved for each Music you create...which now that I think about it, might not be such a big deal?
I don't know, I'll have to experiment.

Kyle

  • Administrator
  • Member
  • Posts: 258
    • View Profile
Re: 3D Audio
« Reply #7 on: February 05, 2015, 07:40:42 PM »
I believe the cache should be okay as it's not storing the music data itself, just references and properties of the music.  Since it's all streamed it shouldn't be taking up huge chunks of memory, or at least I don't think it will.