src/soundManager.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2007 by                                                 *
00003  *         Pierre-yves JEZEQUEL, Julien MICHOT, Loic MOISAN,               *
00004  *         Julien PAPILLON, Sebastien PINEAUD, Barthelemy SERRES           *
00005  *                                                                         *
00006  *   https://sourceforge.net/projects/anidam                               *
00007  *                                                                         *
00008  *   This program is free software; you can redistribute it and/or modify  *
00009  *   it under the terms of the GNU General Public License as published by  *
00010  *   the Free Software Foundation; either version 2 of the License, or     *
00011  *   (at your option) any later version.                                   *
00012  *                                                                         *
00013  *   This program is distributed in the hope that it will be useful,       *
00014  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00015  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00016  *   GNU General Public License for more details.                          *
00017  *                                                                         *
00018  *   You should have received a copy of the GNU General Public License     *
00019  *   along with this program; if not, write to the                         *
00020  *   Free Software Foundation, Inc.,                                       *
00021  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
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      // Very important to call before end of main!
00037     osgAL::SoundManager::instance()->shutdown();
00038 
00039     }
00040 bool SoundManager::init(){
00041   //groupOfSounds = root;
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         // Add the soundstate to the sound manager, so we can find it later on if we want to
00048         //osgAL::SoundManager::instance()->addSoundState(m_sound_state.get());
00049 
00050     // Create ONE (only one, otherwise the transformation of the listener and update for SoundManager will be
00051     // called several times, which is not catastrophic, but unnecessary) 
00052     // SoundRoot that will make sure the listener is updated and
00053     // to keep the internal state of the SoundManager updated
00054     // This could also be done manually, this is just a handy way of doing it.
00055        //SoundManager::sound_root = new osgAL::SoundRoot;
00056   
00057     
00058     // The position in the scenegraph of this node is not important.
00059     // Just as long as the cull traversal should be called after any changes to the SoundManager are made.
00060     //root->addChild(sound_root.get());
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         // Create a sample, load a .wav file.
00076         openalpp::Sample *sample = new openalpp::Sample(file.c_str());
00077 
00078         // Create a named sound state.
00079         osg::ref_ptr<osgAL::SoundState> sound_state = new osgAL::SoundState("glider");
00080 
00081         // Let the soundstate use the sample we just created
00082         sound_state->setSample(sample);
00083 
00084         // Set its gain (volume) to 0.9
00085         sound_state->setGain(gain);
00086 
00087         // Set its pitch to 1 (normal speed)
00088         sound_state->setPitch(pitch);
00089 
00090         // Make it play
00091         sound_state->setPlay(true);
00092 
00093         // The sound should loop over and over again
00094         sound_state->setLooping(loop);
00095 
00096         // Allocate a hardware soundsource to this soundstate (priority 10)
00097         //
00098         sound_state->allocateSource(10, false);
00099 
00100         // At 40 the gain will be half of full!
00101         sound_state->setReferenceDistance(70);
00102         sound_state->setRolloffFactor(4);
00103         sound_state->apply();
00104 
00105         // Add the soundstate to the sound manager, so we can find it later on if we want to
00106         osgAL::SoundManager::instance()->addSoundState(sound_state.get());
00107 
00108         // Create a sound node and attach the soundstate to it.
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   // Create a sample, load a .wav file.
00122   bool add_to_cache = false;
00123   osg::ref_ptr<openalpp::Stream> stream;
00124   osg::ref_ptr<openalpp::Sample> sample;
00125 
00126   // Create a new soundstate, give it the name of the file we loaded.
00127   osg::ref_ptr<osgAL::SoundState> sound_state = new osgAL::SoundState(file);
00128   // Allocate a hardware soundsource to this soundstate (priority 10)
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   // Add the soundstate to the sound manager, so we can find it later on if we want to
00152   //osgAL::SoundManager::instance()->addSoundState(sound_state.get());
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         

Generated on Tue Jun 5 16:56:48 2007 for Anidam by  doxygen 1.5.1