00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "soundManager.h"
00024
00025
00026 SoundManager::SoundManager(osg::ref_ptr<osg::Group> rootNode){
00027
00028
00029
00030 this->root = rootNode;
00031 }
00032
00033 SoundManager::~SoundManager()
00034 {
00035 std::cout<< "suppression du sound manager..."<<std::endl;
00036
00037 osgAL::SoundManager::instance()->shutdown();
00038
00039 }
00040 bool SoundManager::init(){
00041
00042 std::cout<< "initialisation..."<<std::endl;
00043 try{
00044 osgAL::SoundManager::instance()->init(16);
00045 osgAL::SoundManager::instance()->getEnvironment()->setDistanceModel(openalpp::InverseDistance);
00046 osgAL::SoundManager::instance()->getEnvironment()->setDopplerFactor(1);
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062 std::cout<< "Init sound : done."<<std::endl;
00063 return true;
00064 }
00065 catch(std::exception& e){
00066 std::cout<< "Impossible d'initialiser le périphérique!\n(Erreur:" << e.what()<< ")\n" << std::endl;
00067 return false;
00068 }
00069 }
00070
00071 osg::ref_ptr<osgAL::SoundNode> SoundManager::createSound(const std::string& file,float gain, float pitch,bool loop)
00072 {
00073
00074 std::cout << "Création du son :"<< file <<std::endl;
00075
00076 openalpp::Sample *sample = new openalpp::Sample(file.c_str());
00077
00078
00079 osg::ref_ptr<osgAL::SoundState> sound_state = new osgAL::SoundState("glider");
00080
00081
00082 sound_state->setSample(sample);
00083
00084
00085 sound_state->setGain(gain);
00086
00087
00088 sound_state->setPitch(pitch);
00089
00090
00091 sound_state->setPlay(true);
00092
00093
00094 sound_state->setLooping(loop);
00095
00096
00097
00098 sound_state->allocateSource(10, false);
00099
00100
00101 sound_state->setReferenceDistance(70);
00102 sound_state->setRolloffFactor(4);
00103 sound_state->apply();
00104
00105
00106 osgAL::SoundManager::instance()->addSoundState(sound_state.get());
00107
00108
00109 osg::ref_ptr<osgAL::SoundNode> sound = new osgAL::SoundNode;
00110 sound->setSoundState(sound_state.get());
00111
00112 return sound;
00113
00114 }
00115
00116
00117 osg::ref_ptr<osgAL::SoundNode> SoundManager::createSoundNode(const std::string& file, bool occlude, osg::Node *root, bool is_stream)
00118 {
00119
00120
00121
00122 bool add_to_cache = false;
00123 osg::ref_ptr<openalpp::Stream> stream;
00124 osg::ref_ptr<openalpp::Sample> sample;
00125
00126
00127 osg::ref_ptr<osgAL::SoundState> sound_state = new osgAL::SoundState(file);
00128
00129 sound_state->allocateSource(10, false);
00130
00131 if (is_stream) {
00132 stream = osgAL::SoundManager::instance()->getStream(file.c_str(), add_to_cache);
00133 std::cerr << "Loading stream: " << file << std::endl;
00134 sound_state->setStream(stream.get());
00135 }
00136 else {
00137 sample = osgAL::SoundManager::instance()->getSample(file.c_str(), add_to_cache);
00138 std::cerr << "Loading sample: " << file << std::endl;
00139 sound_state->setSample(sample.get());
00140
00141 }
00142
00143
00144 sound_state->setGain(1.0f);
00145 sound_state->setReferenceDistance(70);
00146 sound_state->setRolloffFactor(4);
00147 sound_state->setPlay(true);
00148 sound_state->setLooping(true);
00149
00150
00151
00152
00153
00154 sound_state->apply();
00155 osg::ref_ptr<osgAL::SoundNode> sound_node = new osgAL::SoundNode;
00156 sound_node->setSoundState(sound_state.get());
00157
00158 float radius = 0.5;
00159
00160 if (occlude) {
00161 osgAL::OccludeCallback *cb = new osgAL::OccludeCallback(root);
00162 cb->setNearThreshold(radius*1.1);
00163 sound_node->setOccludeCallback(cb);
00164 }
00165
00166 return sound_node;
00167 return NULL;
00168 }
00169