SoundManagerSonix.cpp

Go to the documentation of this file.
00001 /*************** <auto-copyright.pl BEGIN do not edit this line> **************
00002  *
00003  * VR Juggler is (C) Copyright 1998-2005 by Iowa State University
00004  *
00005  * Original Authors:
00006  *   Allen Bierbaum, Christopher Just,
00007  *   Patrick Hartling, Kevin Meinert,
00008  *   Carolina Cruz-Neira, Albert Baker
00009  *
00010  * This library is free software; you can redistribute it and/or
00011  * modify it under the terms of the GNU Library General Public
00012  * License as published by the Free Software Foundation; either
00013  * version 2 of the License, or (at your option) any later version.
00014  *
00015  * This library is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018  * Library General Public License for more details.
00019  *
00020  * You should have received a copy of the GNU Library General Public
00021  * License along with this library; if not, write to the
00022  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00023  * Boston, MA 02111-1307, USA.
00024  *
00025  * -----------------------------------------------------------------
00026  * File:          $RCSfile$
00027  * Date modified: $Date: 2005-05-05 15:09:19 -0500 (Thu, 05 May 2005) $
00028  * Version:       $Revision: 17440 $
00029  * -----------------------------------------------------------------
00030  *
00031  *************** <auto-copyright.pl END do not edit this line> ***************/
00032 
00033 #include <vrj/vrjConfig.h>
00034 #include <gmtl/Matrix.h>
00035 #include <gmtl/Generate.h>
00036 #include <snx/sonix.h>
00037 #include <vpr/Util/Debug.h>
00038 #include <vpr/Util/FileUtils.h> // for replaceEnvVars...
00039 #include <jccl/Config/ConfigElement.h>
00040 #include <vrj/Sound/SoundManagerSonix.h>
00041 
00042 namespace vrj
00043 {
00044    SoundManagerSonix::SoundManagerSonix()
00045       : vrj::SoundManager()
00046       , mLastFrameTime(0, vpr::Interval::Sec)
00047    {
00048       /* Do nothing. */ ;
00049    }
00050 
00055    bool SoundManagerSonix::configAdd(jccl::ConfigElementPtr element)
00056    {
00057       vprDEBUG(vprDBG_ALL, vprDBG_CONFIG_LVL)
00058          << "======================================\n" << vprDEBUG_FLUSH;
00059       vprDEBUG(vprDBG_ALL, vprDBG_CONFIG_LVL)
00060          << "SoundManagerSonix is being configured...\n" << vprDEBUG_FLUSH;
00061 
00062       if (!configCanHandle( element ))
00063       {
00064          std::cerr << "ERROR: Wrong element type in SoundManagerSonix\n"
00065                    << std::flush;
00066          return false;
00067       }
00068 
00069       std::string manager_name = element->getName();
00070       vprDEBUG(vprDBG_ALL, vprDBG_CONFIG_LVL) << "My Name: " << manager_name
00071                                               << "\n" << vprDEBUG_FLUSH;
00072       std::string api_to_use = element->getProperty<std::string>( "api" );
00073       vprDEBUG(vprDBG_ALL, vprDBG_CONFIG_LVL) << "Use API: " << api_to_use
00074                                               << "\n" << vprDEBUG_FLUSH;
00075 
00076       float listener_position[3];
00077       listener_position[0] = element->getProperty<float>("listener_position", 0);
00078       listener_position[1] = element->getProperty<float>("listener_position", 1);
00079       listener_position[2] = element->getProperty<float>("listener_position", 2);
00080       vprDEBUG(vprDBG_ALL, vprDBG_CONFIG_LVL)
00081          << "Listener Position: " << listener_position[0] << ","
00082          << listener_position[1] << "," << listener_position[2] << std::endl
00083          << vprDEBUG_FLUSH;
00084 
00085       std::vector<std::string> file_search_path(element->getNum("file_search_path"));
00086 
00087       for ( unsigned int i = 0; i < file_search_path.size(); ++i )
00088       {
00089          file_search_path[i] =
00090             vpr::replaceEnvVars(element->getProperty<std::string>("file_search_path", i));
00091       }
00092 
00093 //      vprDEBUG(vprDBG_ALL, vprDBG_CONFIG_LVL)
00094 //         << "Search path: " << file_search_path << "\n" << vprDEBUG_FLUSH;
00095 
00096       // configure sonix
00097       snx::sonix::instance()->changeAPI(api_to_use);
00098       gmtl::Matrix44f mat;
00099       gmtl::setTrans( mat, gmtl::Vec3f( listener_position[0], listener_position[1], listener_position[2] ) );
00100       snx::sonix::instance()->setListenerPosition(mat);
00101 
00102       // read the list of sounds
00103       int size = element->getNum( "sound" );
00104       vprDEBUG(vprDBG_ALL, vprDBG_CONFIG_LVL) << "Configuring " << size
00105                                               << " sounds.\n" << vprDEBUG_FLUSH;
00106       for (int x = 0; x < size; ++x)
00107       {
00108          jccl::ConfigElementPtr sound_element =
00109             element->getProperty<jccl::ConfigElementPtr>("sound", x);
00110          std::string alias = sound_element->getName();
00111          std::string filename = sound_element->getProperty<std::string>("filename");
00112          filename = vpr::replaceEnvVars( filename );
00113          bool ambient = sound_element->getProperty<bool>("ambient");
00114          bool retriggerable = sound_element->getProperty<bool>("retriggerable");
00115          bool streaming = sound_element->getProperty<bool>("streaming");
00116          int loop = sound_element->getProperty<int>("loop");
00117          float cutoff = sound_element->getProperty<float>("cutoff");
00118          float volume = sound_element->getProperty<float>("volume");
00119          float pitchbend = sound_element->getProperty<float>("pitch_bend");
00120          float position[3];
00121          position[0] = sound_element->getProperty<float>("position", 0);
00122          position[1] = sound_element->getProperty<float>("position", 1);
00123          position[2] = sound_element->getProperty<float>("position", 2);
00124 
00125          // configure the sound...
00126          snx::SoundInfo si;
00127          si.datasource = snx::SoundInfo::FILESYSTEM;
00128          si.filename = filename;
00129          si.cutoff = cutoff;
00130          si.volume = volume;
00131          si.pitchbend = pitchbend;
00132          si.repeat = loop;
00133          si.ambient = ambient;
00134          si.retriggerable = retriggerable;
00135          si.streaming = streaming;
00136          si.position[0] = position[0];
00137          si.position[1] = position[1];
00138          si.position[2] = position[2];
00139          vprDEBUG(vprDBG_ALL, vprDBG_CONFIG_LVL)
00140             << "- Configuring " << alias << " (" << filename << ")\n"
00141             << vprDEBUG_FLUSH;
00142          vprDEBUG(vprDBG_ALL, vprDBG_STATE_LVL)
00143             << "  + pos(" << position[0] << "," << position[1] << ","
00144             << position[2] << ")," << "loop(" << loop << "),\n"
00145             << vprDEBUG_FLUSH;
00146          vprDEBUG(vprDBG_ALL, vprDBG_STATE_LVL)
00147             << "  + amb(" << ambient << ")," << "ct(" << cutoff << "),"
00148             << "vl(" << volume << "),\n" << vprDEBUG_FLUSH;
00149          vprDEBUG(vprDBG_ALL, vprDBG_STATE_LVL)
00150             << "  + freq(" << pitchbend << ")," << "retrig(" << retriggerable
00151             << ")\n" << vprDEBUG_FLUSH;
00152 
00153          snx::sonix::instance()->configure(alias, si);
00154       }
00155       vprDEBUG(vprDBG_ALL, vprDBG_CONFIG_LVL)
00156          << "======================================\n" << vprDEBUG_FLUSH;
00157 
00158       return true;
00159    }
00160 
00165    bool SoundManagerSonix::configRemove(jccl::ConfigElementPtr element)
00166    {
00167       // remove any specified sounds...
00168       int size = element->getNum("sound");
00169       for (int x = 0; x < size; ++x)
00170       {
00171          jccl::ConfigElementPtr sound_element =
00172             element->getProperty<jccl::ConfigElementPtr>("sound", x);
00173          std::string alias(sound_element->getName());
00174          snx::sonix::instance()->remove(alias);
00175       }
00176 
00177       return true;
00178    }
00179 
00184    bool SoundManagerSonix::configCanHandle(jccl::ConfigElementPtr element)
00185    {
00186       return std::string("sound_manager_sonix") == element->getID();
00187    }
00188 
00190    void SoundManagerSonix::update()
00191    {
00192       const vpr::Interval cur_time(vpr::Interval::now());
00193       const vpr::Interval delta(cur_time - mLastFrameTime);
00194       float delta_sec(0.0f);
00195 
00196       if ( cur_time > mLastFrameTime )
00197       {
00198          delta_sec = delta.secf();
00199 
00200          if ( delta_sec > 1.0f )
00201          {
00202             delta_sec = 1.0f;
00203          }
00204       }
00205 
00206       mLastFrameTime = cur_time;
00207 
00208       snx::sonix::instance()->step(delta_sec);
00209    }
00210 
00215    void SoundManagerSonix::sync()
00216    {
00217       // whatever...
00218    }
00219 
00220 } // end namespace

Generated on Thu Jan 4 10:56:52 2007 for VR Juggler by  doxygen 1.5.1