I created a custom scene node for cAudio aswell:
SoundSceneNode.h
Code: Select all
/*
* SoundSceneNode.h
*
* Tank @ War Project
* December 2011
*/
#ifndef SOUNDSCENENODE_H_
#define SOUNDSCENENODE_H_
#include <irrlicht.h>
#include <cAudio.h>
namespace irr {
namespace scene {
#ifdef MAKE_IRR_ID
const int SOUND_SCENE_NODE_ID = MAKE_IRR_ID('s','n','d','n');
#else
const int SOUND_SCENE_NODE_ID = 'sndn';
#endif // MAKE_IRR_ID
class SoundSceneNode: public scene::ISceneNode {
private:
core::aabbox3df box;
cAudio::IAudioSource* sound;
cAudio::IAudioManager* soundManager;
bool deleteWhenFinished;
f32 soundVolume;
public:
/**
* Constructor of SoundSceneNode
* @param sound
* @param soundManager
* @param parent
* @param smgr
*/
SoundSceneNode(cAudio::IAudioSource* sound, cAudio::IAudioManager* soundManager, scene::ISceneNode* parent, scene::ISceneManager* smgr);
/**
* Destructor
*/
virtual ~SoundSceneNode();
/**
* Set delete when finished
* @param deleteWhenFinished
*/
void setDeleteWhenFinished(bool deleteWhenFinished);
/**
* On register scene node
*/
virtual void OnRegisterSceneNode();
/**
* Render
*/
virtual void render();
/**
* On Animate
* @param timeMs
*/
virtual void OnAnimate(u32 timeMs);
/**
* Get AA bounding box
*/
virtual const core::aabbox3d<f32>& getBoundingBox() const;
/**
* Get type
* @return type
*/
virtual scene::ESCENE_NODE_TYPE getType() const;
/**
* Get sound source
* @return sound
*/
cAudio::IAudioSource* getSound() const;
/**
* Set sound volume
* @param volume
*/
void setVolume(f32 volume);
/**
* Convert irrlicht to cAudio vector
* @param v
* @return vector
*/
static cAudio::cVector3 irrlichtToCAudioVector(core::vector3df v);
/**
* Convert cAudio to irrlicht vector
* @param v
* @return vector
*/
static core::vector3df cAudioToIrrlichtVector(cAudio::cVector3 v);
};
} // end namespace scene
} // end namespace irr
#endif /* SOUNDSCENENODE_H_ */
SoundSceneNode.cpp
Code: Select all
// (c) Tank @ War
#include "SoundSceneNode.h"
using namespace irr;
using namespace scene;
SoundSceneNode::SoundSceneNode(cAudio::IAudioSource* sound, cAudio::IAudioManager* soundManager, scene::ISceneNode* parent, scene::ISceneManager* smgr) :
scene::ISceneNode(parent, smgr, -1) {
this->sound = sound;
this->soundManager = soundManager;
deleteWhenFinished = true;
if (sound) {
// Play sound
cAudio::cVector3 position = irrlichtToCAudioVector(getAbsolutePosition());
sound->setPosition(position);
soundVolume = sound->getVolume();
sound->play3d(position, sound->getStrength(), sound->isLooping());
}
}
SoundSceneNode::~SoundSceneNode() {
if (sound) {
if (sound->isPlaying()) {
sound->stop();
}
soundManager->release(sound);
}
}
void SoundSceneNode::setDeleteWhenFinished(bool deleteWhenFinished) {
this->deleteWhenFinished = deleteWhenFinished;
}
void SoundSceneNode::OnRegisterSceneNode() {
ISceneNode::OnRegisterSceneNode();
}
void SoundSceneNode::render() {
}
void SoundSceneNode::OnAnimate(u32 timeMs) {
if (sound) {
// Move to new position
sound->move(irrlichtToCAudioVector(getAbsolutePosition()));
// Set volume
f32 distance = cAudioToIrrlichtVector(soundManager->getListener()->getPosition()).getDistanceFrom(getAbsolutePosition());
if (distance <= sound->getMinDistance()) {
sound->setVolume(soundVolume);
} else if (distance > sound->getMaxDistance()) {
sound->setVolume(0);
} else {
f32 gain = (sound->getRolloffFactor() * (distance - sound->getMinDistance()) / (sound->getMaxDistance() - sound->getMinDistance()));
sound->setVolume(soundVolume - (soundVolume * gain));
}
// Delete sound when not looping and finished playing
if (deleteWhenFinished && !sound->isLooping() && sound->isStopped()) {
SceneManager->addToDeletionQueue(this);
}
} else {
SceneManager->addToDeletionQueue(this);
}
ISceneNode::OnAnimate(timeMs);
}
void SoundSceneNode::setVolume(f32 volume) {
soundVolume = volume;
if (soundVolume == 0) {
sound->setVolume(0);
}
}
const core::aabbox3d<f32>& SoundSceneNode::getBoundingBox() const {
return box;
}
scene::ESCENE_NODE_TYPE SoundSceneNode::getType() const {
return (scene::ESCENE_NODE_TYPE) SOUND_SCENE_NODE_ID;
}
cAudio::IAudioSource* SoundSceneNode::getSound() const {
return sound;
}
cAudio::cVector3 SoundSceneNode::irrlichtToCAudioVector(core::vector3df v) {
return cAudio::cVector3(v.X, v.Y, v.Z);
}
core::vector3df SoundSceneNode::cAudioToIrrlichtVector(cAudio::cVector3 v) {
return core::vector3df(v.x, v.y, v.z);
}
Code: Select all
cAudio::IAudioSource* sound = soundManager->create(filename, filename);
if (sound) {
sound->setVolume(1);
sound->setMinDistance(30.f);
sound->setMaxDistance(200.f);
// etc
scene::SoundSceneNode* soundNode = new scene::SoundSceneNode(sound, soundManager, parent, smgr);
soundNode->drop();
return soundNode;
}