3D Sound - Audio - Music

Post those lines of code you feel like sharing or find what you require for your project here; or simply use them as tutorials.
Post Reply
jorgerosa
Competition winner
Posts: 117
Joined: Wed Jun 30, 2010 8:44 am
Location: Portugal
Contact:

3D Sound - Audio - Music

Post by jorgerosa »

Using 3D Audio (sort of) in irrlicht with audiere.

Why Audiere? Has a permissive licence, its simple to install, small and works very well with OGG files. (I had no issues with OGG files until now).

(I´m using CODE::BLOCKS)

Code: Select all

#include "audiere.h" // For Audio. Also must add "audiere" in the Project->Build Options->Linker!
using namespace audiere; // Important!

... ... ...
... ... ...
... ... ...

int main(){ // MAIN [REPLACE WITH YOURS]

    // CREATE AUDIO DEVICE:
    AudioDevicePtr deviceA(OpenDevice());
    
    OutputStreamPtr mySound(OpenSound(deviceA, "media/maze.ogg", true)); // Your sound path
    mySound->setVolume(0.5f); // 0.0 to 1.0
    mySound->setPan(0);       // 0 Left, 1 Right
    mySound->setRepeat(1);    // 1 loop, 0 don't loop
    mySound->play();
    
    ... ... ...
    ... ... ...
    ... ... ...

    while(device->run()) // MAIN LOOP [REPLACE WITH YOURS]
    {
        // Get your (camera) position and the object (node) and calculate sound volume:
        float px = camera->getPosition().X; // camera is you
        float sx = node->getPosition().X;   // node is the sound emitter object
        float pz = camera->getPosition().Z;
        float sz = node->getPosition().Z;
        float distance = sqrt(pow(abs(px-sx),2) + pow(abs(pz-sz),2));
        mySound->setVolume(abs(distance-1000)/1000.0);
    }
    
    ... ... ...
    ... ... ...
    ... ... ...
   
} 
TIP: To calculate the distance from camera and the object you can also retrieve it using (eg) core::line3d<f32> myray; and get myray lenght.

I had this script after search for lots of ideas in google and in the forums, so after try/error 1000 times i had this. Yep, could be easier if i had more irrlicht and c++ skills... but its life. Btw, this script works just fine for me. So, could fit your basic needs without expend a few bucks in other (but much better, of course) audio libraries ;)
Post Reply