Technology Demo

3D Audio Demo

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

Spot for images

Intro

This demo will show you how to implement 3D sound in Luster. This demo builds off of the information found in the 2D Audio demo. For more information about the basics of setting up sound, see the 2D demo.

Implementing 3D Sound

Follow the instructions in the 2D demo to get basic sound set up. Implementing 3D sound requires adding only a few more lines to the code provided in the 2D demo.

There is change in arguments for the creation of the resource sound. We store our sounds in a folder called sounds, and use a file called "dream.mp3". This can also be changed to whatever song that you would prefer. The biggest change, however, is changing the boolean from false to true.

This boolean represents the "positional" property of the sound, and we want the sound to react based on position. The code looks like this:

-- Create a sound resource
self.sound = self.system:createSound("Sounds/dream.mp3", true)

We play the sound as stated in the 2D demo, and then we need to add the code that will attach sound to an object and a listener to another object. To prepare for this, we need to make a test object - in this demo, just a sphere, and implement controls to move it around the stage.

Add the following event listener, and then the method after it. This will allow you to use the WASD keys or the directional arrows on your keyboard to move the sphere around the stage.

stage:addEventListener(luster.display.Stage.ENTER_FRAME, Function(self, self.onEnterFrame))

function Root:onEnterFrame()
   if stage:getKeyPressed(luster.KeyCodes.A) then
      self.sphere.x = self.sphere.x - .5
   elseif stage:getKeyPressed(luster.KeyCodes.D) then
      self.sphere.x = self.sphere.x + .5
   end
   if stage:getKeyPressed(luster.KeyCodes.W) then
      self.sphere.z = self.sphere.z - .5
   elseif stage:getKeyPressed(luster.KeyCodes.S) then
      self.sphere.z = self.sphere.z + .5
   end
end

Listeners and Attaching Sound

In order to create 3D sound, you need to have a listener that represents the location of what is "hearing" the sound, and we need to attach the sound to some kind of object on the stage. For this example, we will attach the listener to the camera and the sound to a sphere.

The sound system that we created earlier has a listener attached to it. We store this in self.listener and then call its attach method. The attach method takes the object that will be "hearing" - in our case, that is the camera.

self.listener = self.system.listener
self.listener:attach(self.camera)

To attach a sound to an object, we need to have an object on the stage. For this demo, we use a sphere and call the attach method from soundInst, or the instance of sound we'll be playing.

self.soundInst:attach(self.sphere)

Now we can run our program to see how moving the sphere around affects how we hear the song.