]> git.mxchange.org Git - simgear.git/blobdiff - simgear/sound/sample_openal.cxx
use auto_ptr instead
[simgear.git] / simgear / sound / sample_openal.cxx
index 8eb83a68d981657325a2be3dfc8eab46b2e2222e..a4efd08a16c9761d60c3ee80f94b3476cda85eb0 100644 (file)
@@ -1,8 +1,10 @@
-// sample.cxx -- Sound sample encapsulation class
+// sample_openal.cxx -- Audio sample encapsulation class
 // 
 // Written by Curtis Olson, started April 2004.
+// Modified to match the new SoundSystem by Erik Hofman, October 2009
 //
 // Copyright (C) 2004  Curtis L. Olson - http://www.flightgear.org/~curt
+// Copyright (C) 2009 Erik Hofman <erik@ehofman.com>
 //
 // This program is free software; you can redistribute it and/or
 // modify it under the terms of the GNU General Public License as
@@ -24,6 +26,8 @@
 #  include <simgear_config.h>
 #endif
 
+#include <stdlib.h>    // rand()
+
 #include <simgear/debug/logstream.hxx>
 #include <simgear/structure/exception.hxx>
 #include <simgear/misc/sg_path.hxx>
 
 // empty constructor
 SGSoundSample::SGSoundSample() :
-    _absolute_pos(SGVec3d::zeros().data()),
-    _relative_pos(SGVec3f::zeros().data()),
-    _base_pos(SGVec3d::zeros().data()),
-    _orientation(SGVec3f::zeros().data()),
-    _direction(SGVec3f::zeros().data()),
-    _velocity(SGVec3f::zeros().data()),
-    _sample_name(""),
+    _absolute_pos(SGVec3d::zeros()),
+    _relative_pos(SGVec3d::zeros()),
+    _direction(SGVec3d::zeros()),
+    _velocity(SGVec3d::zeros()),
+    _orientation(SGQuatd::zeros()),
+    _orivec(SGVec3f::zeros()),
+    _base_pos(SGGeod()),
+    _refname(random_string()),
     _data(NULL),
     _format(AL_FORMAT_MONO8),
     _size(0),
@@ -72,12 +77,13 @@ SGSoundSample::SGSoundSample() :
 
 // constructor
 SGSoundSample::SGSoundSample( const char *path, const char *file ) :
-    _absolute_pos(SGVec3d::zeros().data()),
-    _relative_pos(SGVec3f::zeros().data()),
-    _base_pos(SGVec3d::zeros().data()),
-    _orientation(SGVec3f::zeros().data()),
-    _direction(SGVec3f::zeros().data()),
-    _velocity(SGVec3f::zeros().data()),
+    _absolute_pos(SGVec3d::zeros()),
+    _relative_pos(SGVec3d::zeros()),
+    _direction(SGVec3d::zeros()),
+    _velocity(SGVec3d::zeros()),
+    _orientation(SGQuatd::zeros()),
+    _orivec(SGVec3f::zeros()),
+    _base_pos(SGGeod()),
     _format(AL_FORMAT_MONO8),
     _size(0),
     _freq(0),
@@ -103,21 +109,24 @@ SGSoundSample::SGSoundSample( const char *path, const char *file ) :
     if ( strlen(file) ) {
         samplepath.append( file );
     }
-    _sample_name = samplepath.str();
+    _refname = samplepath.str();
 
      SG_LOG( SG_GENERAL, SG_DEBUG, "From file sounds sample = "
             << samplepath.str() );
 }
 
 // constructor
-SGSoundSample::SGSoundSample( unsigned char *data, int len, int freq, int format ) :
-    _absolute_pos(SGVec3d::zeros().data()),
-    _relative_pos(SGVec3f::zeros().data()),
-    _base_pos(SGVec3d::zeros().data()),
-    _orientation(SGVec3f::zeros().data()),
-    _direction(SGVec3f::zeros().data()),
-    _velocity(SGVec3f::zeros().data()),
-    _data(data),
+SGSoundSample::SGSoundSample( std::auto_ptr<unsigned char>& data,
+                              int len, int freq, int format ) :
+    _absolute_pos(SGVec3d::zeros()),
+    _relative_pos(SGVec3d::zeros()),
+    _direction(SGVec3d::zeros()),
+    _velocity(SGVec3d::zeros()),
+    _orientation(SGQuatd::zeros()),
+    _orivec(SGVec3f::zeros()),
+    _base_pos(SGGeod()),
+    _refname(random_string()),
+    _data(data.release()),
     _format(format),
     _size(len),
     _freq(freq),
@@ -139,7 +148,6 @@ SGSoundSample::SGSoundSample( unsigned char *data, int len, int freq, int format
     _static_changed(true),
     _is_file(false)
 {
-    _sample_name = "unknown, data supplied by caller";
     SG_LOG( SG_GENERAL, SG_DEBUG, "In memory sounds sample" );
 }
 
@@ -148,46 +156,45 @@ SGSoundSample::SGSoundSample( unsigned char *data, int len, int freq, int format
 SGSoundSample::~SGSoundSample() {
 }
 
-float *SGSoundSample::get_orientation() {
-#if 0
-    SGQuatf quat = SGQuatf::fromAngleAxis(_orientation);
-    SGVec3f orient = quat.transform(_direction);
-    return orient.data();
-#else
-    return _orientation.data();
-#endif
-}
-
-void SGSoundSample::set_base_position( SGVec3d pos ) {
-    _base_pos = pos;
+void SGSoundSample::set_orientation( const SGQuatd& ori ) {
+    _orientation = ori;
     update_absolute_position();
     _changed = true;
 }
 
-void SGSoundSample::set_relative_position( SGVec3f pos ) {
-    _relative_pos = pos;
+void SGSoundSample::set_direction( const SGVec3d& dir ) {
+    _direction = dir;
     update_absolute_position();
     _changed = true;
 }
 
-void SGSoundSample::set_orientation( SGVec3f ori ) {
-    _orientation = ori;
+void SGSoundSample::set_relative_position( const SGVec3f& pos ) {
+    _relative_pos = toVec3d(pos);
     update_absolute_position();
     _changed = true;
 }
 
-void SGSoundSample::set_direction( SGVec3f dir ) {
-    _direction = dir;
+void SGSoundSample::set_position( const SGGeod& pos ) {
+    _base_pos = pos;
     update_absolute_position();
     _changed = true;
 }
 
 void SGSoundSample::update_absolute_position() {
-#if 0
-    SGQuatf orient = SGQuatf::fromAngleAxis(_orientation);
-    SGVec3f modified_relative_pos = orient.transform(_relative_pos);
-    _absolute_pos = _base_pos + toVec3d(modified_relative_pos);
-#else
-    _absolute_pos = _base_pos;
-#endif
+    SGQuatd orient = SGQuatd::fromLonLat(_base_pos) * _orientation;
+    _orivec = -toVec3f(orient.rotate(_direction));
+
+     orient = SGQuatd::fromRealImag(0, _relative_pos) * _orientation;
+    _absolute_pos = -SGVec3d::fromGeod(_base_pos); // -orient.rotate(SGVec3d::e1());
+}
+
+string SGSoundSample::random_string() {
+      static const char *r = "0123456789abcdefghijklmnopqrstuvwxyz"
+                             "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
+      string rstr;
+      for (int i=0; i<10; i++) {
+          rstr.push_back( r[rand() % strlen(r)] );
+      }
+
+      return rstr;
 }