Revision: 2393
          http://rigsofrods.svn.sourceforge.net/rigsofrods/?rev=2393&view=rev
Author:   rorthomas
Date:     2012-01-30 23:38:12 +0000 (Mon, 30 Jan 2012)
Log Message:
-----------
audio merge from branch, WIP!

Modified Paths:
--------------
    trunk/source/main/audio/SoundManager.cpp
    trunk/source/main/audio/SoundManager.h
    trunk/source/main/audio/SoundScriptManager.cpp
    trunk/source/main/audio/SoundScriptManager.h

Added Paths:
-----------
    trunk/source/main/audio/Sound.cpp
    trunk/source/main/audio/Sound.h

Property Changed:
----------------
    trunk/source/main/
    trunk/source/main/audio/


Property changes on: trunk/source/main
___________________________________________________________________
Modified: svn:ignore
   - CMakeFiles
cmake_install.cmake
Makefile


   + CMakeFiles
cmake_install.cmake
Makefile



Property changes on: trunk/source/main/audio
___________________________________________________________________
Added: svn:ignore
   + CMakeFiles
cmake_install.cmake
Makefile


Added: trunk/source/main/audio/Sound.cpp
===================================================================
--- trunk/source/main/audio/Sound.cpp                           (rev 0)
+++ trunk/source/main/audio/Sound.cpp   2012-01-30 23:38:12 UTC (rev 2393)
@@ -0,0 +1,146 @@
+/*
+This source file is part of Rigs of Rods
+Copyright 2005-2012 Pierre-Michel Ricordel
+Copyright 2007-2012 Thomas Fischer
+
+For more information, see http://www.rigsofrods.com/
+
+Rigs of Rods is free software: you can redistribute it and/or modify
+it under the terms of the GNU General Public License version 3, as 
+published by the Free Software Foundation.
+
+Rigs of Rods is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with Rigs of Rods.  If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#ifdef USE_OPENAL
+
+#include "Sound.h"
+#include "SoundManager.h"
+
+using namespace Ogre;
+
+Sound::Sound(ALuint _buffer, SoundManager *_sound_mgr, int _source_index) : 
+         buffer(_buffer)
+       , sound_mgr(_sound_mgr)
+       , source_index(_source_index)
+       , hardware_index(-1)
+       , gain(1)
+       , pitch(1)
+       , loop(false)
+       , enabled(true)
+       , should_play(false)
+       , position(Vector3::ZERO)
+       , velocity(Vector3::ZERO)
+{
+}
+
+void Sound::computeAudibility(Vector3 pos)
+{
+       // Disable sound?
+       if (!enabled)
+       {
+               audibility = 0;
+               return;
+       }
+
+       // First check if the sound is finished!
+       if (!loop && should_play && hardware_index!=-1)
+       {
+               int value;
+               
alGetSourcei((ALuint)sound_mgr->getHardwareSource(hardware_index), 
AL_SOURCE_STATE, &value);
+               if (value != AL_PLAYING)
+                       should_play = false;
+       }
+       
+       // should it play at all?
+       if (!should_play || gain == 0.0)
+       {
+               audibility = 0.0;
+               return;
+       }
+
+       float distance = (pos - position).length();
+       
+       if (distance > sound_mgr->MAX_DISTANCE)
+       {
+               audibility = 0.0;
+       } else if (distance < sound_mgr->REFERENCE_DISTANCE)
+       {
+               audibility = gain;
+       } else
+       {
+               audibility = gain * (sound_mgr->REFERENCE_DISTANCE / 
(sound_mgr->REFERENCE_DISTANCE + (sound_mgr->ROLLOFF_FACTOR * (distance - 
sound_mgr->REFERENCE_DISTANCE))));
+       }
+}
+
+bool Sound::isPlaying()
+{
+       if (hardware_index != -1)
+       {
+               int value;
+               
alGetSourcei((ALuint)sound_mgr->getHardwareSource(hardware_index), 
AL_SOURCE_STATE, &value);
+               return (value==AL_PLAYING);
+       }
+       return false;
+}
+
+void Sound::setEnabled(bool e)
+{
+       enabled = e;
+       sound_mgr->recomputeSource(source_index, REASON_PLAY, 0, NULL);
+}
+
+bool Sound::getEnabled()
+{
+       return enabled;
+}
+
+void Sound::play()
+{
+       should_play=true;
+       sound_mgr->recomputeSource(source_index, REASON_PLAY, 0, NULL);
+}
+
+void Sound::stop()
+{
+       should_play=false;
+       sound_mgr->recomputeSource(source_index, REASON_STOP, 0, NULL);
+}
+
+void Sound::setGain(float gain)
+{
+       this->gain=gain;
+       sound_mgr->recomputeSource(source_index, REASON_GAIN, gain, NULL);
+}
+
+void Sound::setLoop(bool loop)
+{
+       this->loop=loop;
+       sound_mgr->recomputeSource(source_index, REASON_LOOP, (loop)?1.0:0.0, 
NULL);
+}
+
+void Sound::setPitch(float pitch)
+{
+       this->pitch=pitch;
+       sound_mgr->recomputeSource(source_index, REASON_PTCH, pitch, NULL);
+}
+
+void Sound::setPosition(Ogre::Vector3 pos)
+{
+       this->position=pos;
+       sound_mgr->recomputeSource(source_index, REASON_POSN, 0, &pos);
+}
+
+void Sound::setVelocity(Ogre::Vector3 vel)
+{
+       this->velocity=vel;
+       sound_mgr->recomputeSource(source_index, REASON_VLCT, 0, &vel);
+}
+
+#endif // USE_OPENAL


Property changes on: trunk/source/main/audio/Sound.cpp
___________________________________________________________________
Added: svn:keywords
   + Date Revision Author HeadURL Id
Added: svn:eol-style
   + native

Added: trunk/source/main/audio/Sound.h
===================================================================
--- trunk/source/main/audio/Sound.h                             (rev 0)
+++ trunk/source/main/audio/Sound.h     2012-01-30 23:38:12 UTC (rev 2393)
@@ -0,0 +1,76 @@
+/*
+This source file is part of Rigs of Rods
+Copyright 2005-2012 Pierre-Michel Ricordel
+Copyright 2007-2012 Thomas Fischer
+
+For more information, see http://www.rigsofrods.com/
+
+Rigs of Rods is free software: you can redistribute it and/or modify
+it under the terms of the GNU General Public License version 3, as 
+published by the Free Software Foundation.
+
+Rigs of Rods is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with Rigs of Rods.  If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#pragma once
+
+#ifdef USE_OPENAL
+#ifndef __Sound_H_
+#define __Sound_H_
+
+#include "RoRPrerequisites.h"
+#include "Ogre.h"
+#include <AL/al.h>
+
+
+class Sound
+{
+       friend class SoundManager;
+
+public:
+       Sound(ALuint buffer, SoundManager* sound_mgr, int source_index);
+
+       void setPitch(float pitch);
+       void setGain(float gain);
+       void setPosition(Ogre::Vector3 pos);
+       void setVelocity(Ogre::Vector3 vel);
+       void setLoop(bool loop);
+       void setEnabled(bool e);
+       void play();
+       void stop();
+
+       bool getEnabled();
+       bool isPlaying();
+
+       enum { REASON_PLAY, REASON_STOP, REASON_GAIN, REASON_LOOP, REASON_PTCH, 
REASON_POSN, REASON_VLCT }; 
+
+private:
+       void computeAudibility(Ogre::Vector3 pos);
+
+       float audibility;
+       float gain;
+       float pitch;
+       bool loop;
+       bool enabled;
+       bool should_play;
+
+       // This value is changed dynamically, depending on whether the input is 
played or not.
+       int hardware_index;
+       ALuint buffer;
+       
+       Ogre::Vector3 position;
+       Ogre::Vector3 velocity;
+
+       SoundManager* sound_mgr;
+       // Must not be changed during the lifetime of this object
+       int source_index;
+};
+
+#endif // __Sound_H_
+#endif // USE_OPENAL


Property changes on: trunk/source/main/audio/Sound.h
___________________________________________________________________
Added: svn:keywords
   + Date Revision Author HeadURL Id
Added: svn:eol-style
   + native

Modified: trunk/source/main/audio/SoundManager.cpp
===================================================================
--- trunk/source/main/audio/SoundManager.cpp    2012-01-30 22:37:09 UTC (rev 
2392)
+++ trunk/source/main/audio/SoundManager.cpp    2012-01-30 23:38:12 UTC (rev 
2393)
@@ -17,36 +17,32 @@
 You should have received a copy of the GNU General Public License
 along with Rigs of Rods.  If not, see <http://www.gnu.org/licenses/>.
 */
+
 #ifdef USE_OPENAL
 
-#include "Ogre.h"
+#include "Sound.h"
 #include "SoundManager.h"
 #include "Settings.h"
-//#include "pstdint.h"
 
-
 // some gcc fixes
 #if OGRE_PLATFORM == OGRE_PLATFORM_LINUX
 #pragma GCC diagnostic ignored "-Wfloat-equal"
-#endif //OGRE_PLATFORM_LINUX
+#endif // OGRE_PLATFORM_LINUX
 
 using namespace Ogre;
 
-SoundManager::SoundManager()
-{
-       device=NULL;
-       context=NULL;
-       free_input_source=0;
-       free_buffer=0;
-       num_hardware_sources=0;
-       used_hardware_sources=0;
-       maxDistance=500.0;
-       rolloffFactor=1.0;
-       referenceDistance=7.5;
-       pthread_mutex_init(&audio_mutex, NULL);
+const float SoundManager::MAX_DISTANCE       = 500.0f;
+const float SoundManager::ROLLOFF_FACTOR     = 1.0f;
+const float SoundManager::REFERENCE_DISTANCE = 7.5f;
 
-       for (int i=0; i<MAX_HARDWARE_SOURCES; i++) 
hardware_sources_input_map[i]=-1;
-
+SoundManager::SoundManager() :
+         m_sound_device(NULL)
+       , m_sound_context(NULL)
+       , m_audio_sources_in_use_count(0)
+       , m_audio_buffers_in_use_count(0)
+       , m_hardware_sources_num(0)
+       , m_hardware_sources_in_use_count(0)
+{
        if (SSETTING("3D Sound renderer") == "No sound") return;
 
     char str[256];
@@ -55,561 +51,379 @@
 
        LOG("Opening Device: '"+String(str)+"'");
 
-       //we loop alcOpenDevice() because there is a potential race condition 
with the asynchronous DSound enumeration callback
+       for (int i=0; i<MAX_HARDWARE_SOURCES; i++) m_hardware_sources_map[i]=-1;
+
+       // We loop alcOpenDevice() because there is a potential race condition 
with the asynchronous DSound enumeration callback
        for (int i=0; i<100; i++)
        {
-               device=alcOpenDevice(str);
-               if (device) break; //all right we got it
+               m_sound_device=alcOpenDevice(str);
+               if (m_sound_device)
+                       break;
                else
                {
-                       ALint error;
-                       error=alGetError();
-                       if (error!=ALC_INVALID_VALUE)
-                       {
-                               //this is unexpected error
-                               LOG("Could not create OpenAL device, error 
code: "+TOSTRING(error));
-                               return;
-                       } //else just loop
+                       ALint error=alGetError();
+                       if (error != ALC_INVALID_VALUE) {LOG("Could not create 
OpenAL device, error code: "+TOSTRING(error));return;}
                }
        }
-       if (!device) //we looped and we got nothing
+       if (!m_sound_device)
        {
-               //last ditch attempt with the default sound device, in case the 
user has a messed-up config file
-               device=alcOpenDevice("");
-               if (!device)
-               {
-                       LOG("Could not create OpenAL device after many tries.");
-                       return;
-               }
-               else
-                       LOG("Warning: invalid sound device configuration, I 
will use the default sound source. Run configurator!");
+               // Last ditch attempt with the default sound device, in case 
the user has a messed-up config file
+               m_sound_device=alcOpenDevice("");
+               if (!m_sound_device) {LOG("Could not create OpenAL device after 
many tries.");return;}
+               else LOG("Warning: invalid sound device configuration, I will 
use the default sound source. Run configurator!");
        }
-       context=alcCreateContext(device, NULL);
-       if (!context)
+       m_sound_context=alcCreateContext(m_sound_device, NULL);
+       if (!m_sound_context)
        {
                ALint error;
                error=alGetError();
                LOG("Could not create OpenAL context, error code: 
"+TOSTRING(error));
-               alcCloseDevice(device);
-               device=NULL;
+               alcCloseDevice(m_sound_device);
+               m_sound_device=NULL;
                return;
        }
-       alcMakeContextCurrent(context);
-       //generate the AL sources
-       for (num_hardware_sources=0; num_hardware_sources<MAX_HARDWARE_SOURCES; 
num_hardware_sources++)
+       alcMakeContextCurrent(m_sound_context);
+       // Generate the AL sources
+       for (m_hardware_sources_num=0; 
m_hardware_sources_num<MAX_HARDWARE_SOURCES; m_hardware_sources_num++)
        {
                alGetError();
-               alGenSources(1, &hardware_sources[num_hardware_sources]);
-               if(alGetError()!=AL_NO_ERROR)
+               alGenSources(1, &m_hardware_sources[m_hardware_sources_num]);
+               if (alGetError() != AL_NO_ERROR)
                        break;
-               alSourcef(hardware_sources[num_hardware_sources], 
AL_REFERENCE_DISTANCE, referenceDistance);
-               alSourcef(hardware_sources[num_hardware_sources], 
AL_ROLLOFF_FACTOR, rolloffFactor);
-               alSourcef(hardware_sources[num_hardware_sources], 
AL_MAX_DISTANCE, maxDistance);
+               alSourcef(m_hardware_sources[m_hardware_sources_num], 
AL_REFERENCE_DISTANCE, REFERENCE_DISTANCE);
+               alSourcef(m_hardware_sources[m_hardware_sources_num], 
AL_ROLLOFF_FACTOR, ROLLOFF_FACTOR);
+               alSourcef(m_hardware_sources[m_hardware_sources_num], 
AL_MAX_DISTANCE, MAX_DISTANCE);
        }
+       alDopplerFactor(1.0);
+       alDopplerVelocity(343.0f);
 }
 
-Sound* SoundManager::createSound(String filename)
+SoundManager::~SoundManager()
 {
-       if (!device) return NULL;
-       MUTEX_LOCK(&audio_mutex);
-       ALuint buffer=0;
-       //first, search if we need to create the buffer
-       for (int i=0; i<free_buffer; i++)
-               if (filename==buffer_filenames[i])
-               {
-                       buffer=buffers[i];
-                       break;
-               }
-       if (!buffer)
-       {
-               //load the file
-               if (free_buffer==MAX_BUFFERS)
-               {
-                       MUTEX_UNLOCK(&audio_mutex);
-                       return NULL;
-               }
-               alGenBuffers(1, &buffers[free_buffer]);
-               buffer_filenames[free_buffer]=filename;
-               if (loadWAVFile(filename, buffers[free_buffer]))
-               {
-                       //there was an error!
-                       alDeleteBuffers(1, &buffers[free_buffer]);
-                       buffer_filenames[free_buffer]=String("");
-                       MUTEX_UNLOCK(&audio_mutex);
-                       return NULL;
-               }
-               buffer=buffers[free_buffer];
-               free_buffer++;
-       }
-       if (free_input_source==MAX_INPUT_SOURCES)
-       {
-               MUTEX_UNLOCK(&audio_mutex);
-               return NULL;
-       }
-       input_sources[free_input_source]=new Sound(free_input_source, buffer, 
this);
-       free_input_source++;
-       //don't recompute, because sounds do not play automatically
-       //recomputeSource(free_input_source-1);
-       MUTEX_UNLOCK(&audio_mutex);
-       return input_sources[free_input_source-1];
+       // Delete the sources and buffers
+       alDeleteSources(MAX_HARDWARE_SOURCES, m_hardware_sources);
+    alDeleteBuffers(MAX_AUDIO_BUFFERS, m_audio_buffers);
+
+       // Destroy the sound context and device
+    m_sound_context = alcGetCurrentContext();
+    m_sound_device = alcGetContextsDevice(m_sound_context);
+    alcMakeContextCurrent(NULL);
+    alcDestroyContext(m_sound_context);
+    if (m_sound_device)
+        alcCloseDevice(m_sound_device);
+
+       LOG("SoundManager destroyed.");
 }
 
 void SoundManager::setCamera(Ogre::Vector3 position, Ogre::Vector3 direction, 
Ogre::Vector3 up, Ogre::Vector3 velocity)
 {
-       if (!device) return;
-       MUTEX_LOCK(&audio_mutex);
-       cameraPosition=position;
+       if (!m_sound_device) return;
+       camera_position = position;
        recomputeAllSources();
-       float dir[6];
-       //at
-       dir[0]=direction.x;
-       dir[1]=direction.y;
-       dir[2]=direction.z;
+       
+       float orientation[6];
+       //dir
+       orientation[0] = direction.x;
+       orientation[1] = direction.y;
+       orientation[2] = direction.z;
        //up
-       dir[3]=up.x;
-       dir[4]=up.y;
-       dir[5]=up.z;
-       alListener3f(AL_POSITION, position.x,position.y, position.z);
-       alListener3f(AL_VELOCITY, velocity.x,velocity.y,velocity.z);
-       alListenerfv(AL_ORIENTATION, dir);
-       MUTEX_UNLOCK(&audio_mutex);
-}
+       orientation[3] = up.x;
+       orientation[4] = up.y;
+       orientation[5] = up.z;
 
-void SoundManager::pauseAllSounds()
-{
-       if (!device) return;
-       //no need for mutex
-       alListenerf(AL_GAIN, 0.0);
+       alListener3f(AL_POSITION, position.x, position.y, position.z);
+       alListener3f(AL_VELOCITY, velocity.x, velocity.y, velocity.z);
+       alListenerfv(AL_ORIENTATION, orientation);
 }
 
-void SoundManager::resumeAllSounds()
-{
-       if (!device) return;
-       //no need for mutex
-       alListenerf(AL_GAIN, 1.0);
+bool compareByAudibility(std::pair<int, float> a, std::pair<int, float> b)
+{              
+       return a.second > b.second;
 }
 
-int SoundManager::maxSources()
-{
-       //no need for mutex
-       return num_hardware_sources;
-}
-
+// Called when the camera moves
 void SoundManager::recomputeAllSources()
 {
-       if (!device) return;
-       /*LOG("==========Table dump===================");
-       for (int i=0; i<num_hardware_sources; i++)
-               LOG("   "+TOSTRING(hardware_sources_input_map[i]));
-               */
-       //mutex is supposed to be already taken
-
-       //todo: use a high performance selection algorithm
-       //http://en.wikipedia.org/wiki/Selection_algorithm
-
-       //this function is called when the camera moves
-       //first, compute all audibility
-       for (int i=0; i<free_input_source; i++) 
input_sources[i]->computeAudibility(cameraPosition);
-       //next, sort sources by audibility
-       //suboptimal algorithm
-       //this source index table contains the num_hardware_sources most 
audible sources
-       // it can contain indexes -1 if not enough sources are audible
-       int most_audibles[MAX_HARDWARE_SOURCES];
-       for (int i=0; i<MAX_HARDWARE_SOURCES; i++) most_audibles[i]=-1;
-       for (int i=0; i<num_hardware_sources; i++)
+       
+       if (!m_sound_device) return;
+       // Mutex is supposed to be already taken
+       for (int i=0; i<m_audio_sources_in_use_count; i++)
        {
-               float maxa=0.0;
-               for (int j=0; j<free_input_source; j++)
-               {
-                       if (input_sources[j]->audibility>maxa)
-                       {
-                               int k;
-                               //search in the table if it was not already 
recorded
-                               for (k=0; k<i; k++) if (most_audibles[k]==j) 
break;
-                               if (k==i)
-                               {
-                                       //nope, its really a potential hit!
-                                       most_audibles[i]=j;
-                                       maxa=input_sources[j]->audibility;
-                               }
-                       }
-               }
+               m_audio_sources[i]->computeAudibility(camera_position);
+               m_audio_sources_most_audible[i].first = i;
+               m_audio_sources_most_audible[i].second = 
m_audio_sources[i]->audibility;
        }
-       //oookay
-       //check if we must retire sources first
-       for (int i=0; i<free_input_source; i++)
+       // Sort first 'num_hardware_sources' sources by audibility
+       // See: https://en.wikipedia.org/wiki/Selection_algorithm
+       if ((m_audio_sources_in_use_count-1) > m_hardware_sources_num)
+               std::nth_element(m_audio_sources_most_audible, 
m_audio_sources_most_audible+m_hardware_sources_num, 
m_audio_sources_most_audible+m_audio_sources_in_use_count-1, 
compareByAudibility);
+       // Retire out of range sources first
+       for (int i=0; i<m_audio_sources_in_use_count; i++)
        {
-               if (input_sources[i]->hardware_index!=-1)
-               {
-                       //search in the list
-                       int j;
-                       for (j=0; j<num_hardware_sources; j++) if 
(most_audibles[j]==i) break;
-                       if (j==num_hardware_sources)
-                       {
-                               //it was not found!
-                               retire(i);
-                       }
-               }
+               if 
(m_audio_sources[m_audio_sources_most_audible[i].first]->hardware_index != -1 
&& (i >= m_hardware_sources_num || m_audio_sources_most_audible[i].second == 0))
+                       retire(m_audio_sources_most_audible[i].first);
        }
-       //next, assign new sources
-       for (int i=0; i<num_hardware_sources; i++)
+       // Assign new sources
+       for (int i=0; i<std::min(m_audio_sources_in_use_count, 
m_hardware_sources_num); i++)
        {
-               if (most_audibles[i]!=-1)
+               if 
(m_audio_sources[m_audio_sources_most_audible[i].first]->hardware_index == -1 
&& m_audio_sources_most_audible[i].second > 0)
                {
-                       if (input_sources[most_audibles[i]]->hardware_index==-1)
+                       for (int j=0; j<m_hardware_sources_num; j++)
                        {
-                               //its not assigned!
-                               //find a freshly retired hardware buffer: there 
MUST be at least one!
-                               int j;
-                               for (j=0; j<num_hardware_sources; j++) if 
(hardware_sources_input_map[j]==-1) break;
-                               assign(most_audibles[i], j);
+                               if (m_hardware_sources_map[j] == -1)
+                               {
+                                       
assign(m_audio_sources_most_audible[i].first, j);
+                                       break;
+                               }
                        }
                }
        }
-       //yes, thats it!
 }
 
-void SoundManager::recomputeSource(int input_index, int reason, float vfl, 
Vector3 *vvec)
+void SoundManager::recomputeSource(int source_index, int reason, float vfl, 
Vector3 *vvec)
 {
-       if (!device) return;
-       //mutex is supposed to be already taken
+       if (!m_sound_device) return;
 
-       //something has changed in a source
-       input_sources[input_index]->computeAudibility(cameraPosition);
-       if (input_sources[input_index]->audibility==0)
+       m_audio_sources[source_index]->computeAudibility(camera_position);
+
+       if (m_audio_sources[source_index]->audibility == 0)
        {
-               //this is an extinct source, retire if currently playing
-               if (input_sources[input_index]->hardware_index!=-1) 
retire(input_index);
+               if (m_audio_sources[source_index]->hardware_index != -1) // 
Retire the source if it is currently assigned
+                       retire(source_index);
        }
        else
        {
-               //this is a potentially audible source
-               if (input_sources[input_index]->hardware_index!=-1)
+               // This is a potentially audible m_audio_sources[source_index]
+               if (m_audio_sources[source_index]->hardware_index != -1)
                {
-                       //source already playing
-                       //update the AL settings
+                       // m_audio_sources[source_index] already playing
+                       // Update the AL settings
                        switch (reason)
                        {
-                               case REASON_PLAY: 
alSourcePlay(hardware_sources[input_sources[input_index]->hardware_index]);break;
-                               case REASON_STOP: 
alSourceStop(hardware_sources[input_sources[input_index]->hardware_index]);break;
-                               case REASON_GAIN: 
alSourcef(hardware_sources[input_sources[input_index]->hardware_index], 
AL_GAIN, vfl);break;
-                               case REASON_LOOP: 
alSourcei(hardware_sources[input_sources[input_index]->hardware_index], 
AL_LOOPING, (vfl>0.5)?AL_TRUE:AL_FALSE);break;
-                               case REASON_PTCH: 
alSourcef(hardware_sources[input_sources[input_index]->hardware_index], 
AL_PITCH, vfl);break;
-                               case REASON_POSN: 
alSource3f(hardware_sources[input_sources[input_index]->hardware_index], 
AL_POSITION, vvec->x,vvec->y,vvec->z);break;
-                               case REASON_VLCT: 
alSource3f(hardware_sources[input_sources[input_index]->hardware_index], 
AL_VELOCITY, vvec->x,vvec->y,vvec->z);break;
-                               default:break;
+                               case Sound::REASON_PLAY: 
alSourcePlay(m_hardware_sources[m_audio_sources[source_index]->hardware_index]);break;
+                               case Sound::REASON_STOP: 
alSourceStop(m_hardware_sources[m_audio_sources[source_index]->hardware_index]);break;
+                               case Sound::REASON_GAIN: 
alSourcef(m_hardware_sources[m_audio_sources[source_index]->hardware_index], 
AL_GAIN, vfl);break;
+                               case Sound::REASON_LOOP: 
alSourcei(m_hardware_sources[m_audio_sources[source_index]->hardware_index], 
AL_LOOPING, (vfl>0.5)?AL_TRUE:AL_FALSE);break;
+                               case Sound::REASON_PTCH: 
alSourcef(m_hardware_sources[m_audio_sources[source_index]->hardware_index], 
AL_PITCH, vfl);break;
+                               case Sound::REASON_POSN: 
alSource3f(m_hardware_sources[m_audio_sources[source_index]->hardware_index], 
AL_POSITION, vvec->x,vvec->y,vvec->z);break;
+                               case Sound::REASON_VLCT: 
alSource3f(m_hardware_sources[m_audio_sources[source_index]->hardware_index], 
AL_VELOCITY, vvec->x,vvec->y,vvec->z);break;
+                               default: break;
                        }
                }
                else
                {
-                       //try to make it play by the hardware
-                       //check if there is one free source in the pool
-                       if (used_hardware_sources<num_hardware_sources)
+                       // Try to make it play by the hardware
+                       // Check if there is one free 
m_audio_sources[source_index] in the pool
+                       if (m_hardware_sources_in_use_count < 
m_hardware_sources_num)
                        {
-                               //yes!
-                               for (int i=0; i<num_hardware_sources; i++)
-                                       if (hardware_sources_input_map[i]==-1)
-                                       {
-                                               assign(input_index, i);
-                                               break;
-                                       }
+                               for (int i=0; i<m_hardware_sources_num; i++)
+                                       if (m_hardware_sources_map[i] == -1) 
{assign(source_index, i);break;}
                        }
                        else
                        {
-                               //no, compute who is the faintest
-                               //note: we know the table 
hardware_sources_input_map is full!
+                               // Now, compute who is the faintest
+                               // Note: we know the table 
m_hardware_sources_map is full!
                                float fv=1.0;
                                int al_faintest=0;
-                               for (int i=0; i<num_hardware_sources; i++)
-                                       if 
(input_sources[hardware_sources_input_map[i]]->audibility<fv)
+                               for (int i=0; i<m_hardware_sources_num; i++)
+                               {
+                                       if (m_hardware_sources_map[i] >= 0 && 
m_audio_sources[m_hardware_sources_map[i]]->audibility < fv)
                                        {
-                                               
fv=input_sources[hardware_sources_input_map[i]]->audibility;
+                                               
fv=m_audio_sources[m_hardware_sources_map[i]]->audibility;
                                                al_faintest=i;
                                        }
-                               //find if the the faintest source is louder or 
not
-                               if (fv<input_sources[input_index]->audibility)
+                                       }
+                               // Find if the the faintest 
m_audio_sources[source_index] is louder or not
+                               if (fv < 
m_audio_sources[source_index]->audibility)
                                {
-                                       //this new source is louder than the 
faintest!
-                                       
retire(hardware_sources_input_map[al_faintest]);
-                                       assign(input_index, al_faintest);
+                                       // This new 
m_audio_sources[source_index] is louder than the faintest!
+                                       
retire(m_hardware_sources_map[al_faintest]);
+                                       assign(source_index, al_faintest);
                                }
-                               //else this source is too faint, we don't play 
it!
+                               // Else this m_audio_sources[source_index] is 
too faint, we don't play it!
                        }
                }
        }
 }
+       
+void SoundManager::assign(int source_index, int hardware_index)
+{
+       if (!m_sound_device) return;
 
-void SoundManager::assign(int input_index, int hardware_index)
+       m_audio_sources[source_index]->hardware_index=hardware_index;
+       m_hardware_sources_map[hardware_index]=source_index;
+
+       // The hardware source is supposed to be stopped!
+       alSourcei(m_hardware_sources[hardware_index], AL_BUFFER, 
m_audio_sources[source_index]->buffer);
+       alSourcef(m_hardware_sources[hardware_index], AL_GAIN, 
m_audio_sources[source_index]->gain);
+       alSourcei(m_hardware_sources[hardware_index], AL_LOOPING, 
(m_audio_sources[source_index]->loop)?AL_TRUE:AL_FALSE);
+       alSourcef(m_hardware_sources[hardware_index], AL_PITCH, 
m_audio_sources[source_index]->pitch);
+       alSource3f(m_hardware_sources[hardware_index], AL_POSITION, 
m_audio_sources[source_index]->position.x,m_audio_sources[source_index]->position.y,m_audio_sources[source_index]->position.z);
+       alSource3f(m_hardware_sources[hardware_index], AL_VELOCITY, 
m_audio_sources[source_index]->velocity.x,m_audio_sources[source_index]->velocity.y,m_audio_sources[source_index]->velocity.z);
+       if (m_audio_sources[source_index]->should_play)
+               alSourcePlay(m_hardware_sources[hardware_index]);
+       
+       m_hardware_sources_in_use_count++;
+}
+
+void SoundManager::retire(int source_index)
 {
-       if (!device) return;
-       //mutex is supposed to be taken
-       input_sources[input_index]->hardware_index=hardware_index;
-       hardware_sources_input_map[hardware_index]=input_index;
-       used_hardware_sources++;
+       if (!m_sound_device) return;
 
-       //the hardware index is supposed to be stopped!
-       alSourcei(hardware_sources[hardware_index], AL_BUFFER, 
input_sources[input_index]->buffer);
-       alSourcef(hardware_sources[hardware_index], AL_GAIN, 
input_sources[input_index]->gain);
-       alSourcei(hardware_sources[hardware_index], AL_LOOPING, 
(input_sources[input_index]->loop)?AL_TRUE:AL_FALSE);
-       alSourcef(hardware_sources[hardware_index], AL_PITCH, 
input_sources[input_index]->pitch);
-       alSource3f(hardware_sources[hardware_index], AL_POSITION, 
input_sources[input_index]->position.x,input_sources[input_index]->position.y,input_sources[input_index]->position.z);
-       alSource3f(hardware_sources[hardware_index], AL_VELOCITY, 
input_sources[input_index]->velocity.x,input_sources[input_index]->velocity.y,input_sources[input_index]->velocity.z);
-       if (input_sources[input_index]->should_play)
-       {
-               alSourcePlay(hardware_sources[hardware_index]);
-               //suboptimal, but better than nothing <-does not work
-               //if (!input_sources[input_index]->loop) 
input_sources[input_index]->should_play=false;
-       }
+       if (m_audio_sources[source_index]->hardware_index == -1) return;
+       
alSourceStop(m_hardware_sources[m_audio_sources[source_index]->hardware_index]);
+       
m_hardware_sources_map[m_audio_sources[source_index]->hardware_index]=-1;
+       m_audio_sources[source_index]->hardware_index=-1;
+       m_hardware_sources_in_use_count--;
 }
 
-void SoundManager::retire(int input_index)
+void SoundManager::pauseAllSounds()
 {
-       if (!device) return;
-       //mutex is supposed to be taken
-       int hardware_index=input_sources[input_index]->hardware_index;
-       if (hardware_index==-1) return;
-       alSourceStop(hardware_sources[hardware_index]);
-       hardware_sources_input_map[hardware_index]=-1;
-       input_sources[input_index]->hardware_index=-1;
-       used_hardware_sources--;
+       if (!m_sound_device) return;
+       alListenerf(AL_GAIN, 0.0);
 }
 
-int SoundManager::loadWAVFile(String filename, ALuint buffer)
+void SoundManager::resumeAllSounds()
 {
-       if (!device) return -1;
+       if (!m_sound_device) return;
+       alListenerf(AL_GAIN, 1.0);
+
+}
+
+Sound* SoundManager::createSound(String filename)
+{
+       if (!m_sound_device) return NULL;
+       ALuint buffer=0;
+
+       // Is the file already loaded?
+       for (int i=0; i<m_audio_buffers_in_use_count; i++)
+       {
+               if (filename == m_audio_buffer_file_name[i])
+               {
+                       buffer=m_audio_buffers[i];
+                       break;
+               }
+       }
+       if (!buffer)
+       {
+               // Load the file
+               if (m_audio_buffers_in_use_count >= MAX_AUDIO_BUFFERS)
+                       return NULL;
+
+               alGenBuffers(1, &m_audio_buffers[m_audio_buffers_in_use_count]);
+               if (loadWAVFile(filename, 
m_audio_buffers[m_audio_buffers_in_use_count]))
+               {
+                       // There was an error!
+                       alDeleteBuffers(1, 
&m_audio_buffers[m_audio_buffers_in_use_count]);
+                       
m_audio_buffer_file_name[m_audio_buffers_in_use_count]=String("");
+                       return NULL;
+               }
+               buffer=m_audio_buffers[m_audio_buffers_in_use_count];
+               m_audio_buffer_file_name[m_audio_buffers_in_use_count]=filename;
+       }
+       if (m_audio_buffers_in_use_count >= MAX_AUDIO_SOURCES) return NULL;
+
+       m_audio_sources[m_audio_buffers_in_use_count]=new Sound(buffer, this, 
m_audio_buffers_in_use_count);
+
+       return m_audio_sources[m_audio_buffers_in_use_count++];
+}
+
+bool SoundManager::loadWAVFile(String filename, ALuint buffer)
+{
+       if (!m_sound_device) return true;
        LOG("Loading WAV file "+filename);
-       //creating Stream
+       
+       // Create the Stream
        ResourceGroupManager *rgm=ResourceGroupManager::getSingletonPtr();
        String group=rgm->findGroupContainingResource(filename);
        DataStreamPtr stream=rgm->openResource(filename, group);
-       //load RIFF/WAVE
+
+       // Load RIFF/WAVE
        char magic[5];
        magic[4]=0;
        unsigned int   lbuf; // uint32_t
        unsigned short sbuf; // uint16_t
 
-       // check magic
-       if (stream->read(magic, 4)!=4) {LOG("Could not read file 
"+filename);return -1;}
-       if (String(magic)!=String("RIFF")) {LOG("Invalid WAV file (no RIFF): 
"+filename);return -1;}
-       //skip filesize
+       // Check magic
+       if (stream->read(magic, 4) != 4) {LOG("Could not read file 
"+filename);return true;}
+       if (String(magic) != String("RIFF")) {LOG("Invalid WAV file (no RIFF): 
"+filename);return true;}
+       // Skip 4 bytes (magic)
        stream->skip(4);
-       // check file format
-       if (stream->read(magic, 4)!=4) {LOG("Could not read file 
"+filename);return -1;}
-       if (String(magic)!=String("WAVE")) {LOG("Invalid WAV file (no WAVE): 
"+filename);return -1;}
-       // check 'fmt ' sub chunk (1)
-       if (stream->read(magic, 4)!=4) {LOG("Could not read file 
"+filename);return -1;}
-       if (String(magic)!=String("fmt ")) {LOG("Invalid WAV file (no fmt): 
"+filename);return -1;}
-       // read (1)'s size
-       if (stream->read(&lbuf, 4)!=4) {LOG("Could not read file 
"+filename);return -1;}
+       // Check file format
+       if (stream->read(magic, 4) != 4) {LOG("Could not read file 
"+filename);return true;}
+       if (String(magic) != String("WAVE")) {LOG("Invalid WAV file (no WAVE): 
"+filename);return true;}
+       // Check 'fmt ' sub chunk (1)
+       if (stream->read(magic, 4) != 4) {LOG("Could not read file 
"+filename);return true;}
+       if (String(magic) != String("fmt ")) {LOG("Invalid WAV file (no fmt): 
"+filename);return true;}
+       // Read (1)'s size
+       if (stream->read(&lbuf, 4) != 4) {LOG("Could not read file 
"+filename);return true;}
        unsigned long subChunk1Size=lbuf;
-       if (subChunk1Size<16) {LOG("Invalid WAV file (invalid subChunk1Size): 
"+filename);return -1;}
-       // check PCM audio format
-       if (stream->read(&sbuf, 2)!=2) {LOG("Could not read file 
"+filename);return -1;}
+       if (subChunk1Size<16) {LOG("Invalid WAV file (invalid subChunk1Size): 
"+filename);return true;}
+       // Check PCM audio format
+       if (stream->read(&sbuf, 2) != 2) {LOG("Could not read file 
"+filename);return true;}
        unsigned short audioFormat=sbuf;
-       if (audioFormat!=1) {LOG("Invalid WAV file (invalid audioformat 
"+TOSTRING(audioFormat)+"): "+filename);return -1;}
-       // read number of channels
-       if (stream->read(&sbuf, 2)!=2) {LOG("Could not read file 
"+filename);return -1;}
+       if (audioFormat != 1) {LOG("Invalid WAV file (invalid audioformat 
"+TOSTRING(audioFormat)+"): "+filename);return true;}
+       // Read number of channels
+       if (stream->read(&sbuf, 2) != 2) {LOG("Could not read file 
"+filename);return true;}
        unsigned short channels=sbuf;
-       // read frequency (sample rate)
-       if (stream->read(&lbuf, 4)!=4) {LOG("Could not read file 
"+filename);return -1;}
+       // Read frequency (sample rate)
+       if (stream->read(&lbuf, 4) != 4) {LOG("Could not read file 
"+filename);return true;}
        unsigned long freq=lbuf;
-       // skip 6 bytes (Byte rate (4), Block align (2))
+       // Skip 6 bytes (Byte rate (4), Block align (2))
        stream->skip(6);
-       // read bits per sample
-       if (stream->read(&sbuf, 2)!=2) {LOG("Could not read file 
"+filename);return -1;}
+       // Read bits per sample
+       if (stream->read(&sbuf, 2) != 2) {LOG("Could not read file 
"+filename);return true;}
        unsigned short bps=sbuf;
-       // check 'data' sub chunk (2)
-       if (stream->read(magic, 4)!=4) {LOG("Could not read file 
"+filename);return -1;}
-       if (String(magic)!=String("data") && String(magic)!=String("fact")) 
{LOG("Invalid WAV file (no data/fact): "+filename);return -1;}
-       // fact is an option section we don't need to worry about
-       if(String(magic)==String("fact"))
+       // Check 'data' sub chunk (2)
+       if (stream->read(magic, 4) != 4) {LOG("Could not read file 
"+filename);return true;}
+       if (String(magic) != String("data") && String(magic) != String("fact")) 
{LOG("Invalid WAV file (no data/fact): "+filename);return true;}
+       // Fact is an option section we don't need to worry about
+       if (String(magic)==String("fact"))
        {
                stream->skip(8);
                // Now we shoudl hit the data chunk
-               if (stream->read(magic, 4)!=4) {LOG("Could not read file 
"+filename);return -1;}
-               if (String(magic)!=String("data")) {LOG("Invalid WAV file (no 
data): "+filename);return -1;}
+               if (stream->read(magic, 4) != 4) {LOG("Could not read file 
"+filename);return true;}
+               if (String(magic) != String("data")) {LOG("Invalid WAV file (no 
data): "+filename);return true;}
        }
        // The next four bytes are the remaining size of the file
-       if (stream->read(&lbuf, 4)!=4) {LOG("Could not read file 
"+filename);return -1;}
+       if (stream->read(&lbuf, 4) != 4) {LOG("Could not read file 
"+filename);return true;}
+       
        unsigned long dataSize=lbuf;
+       int format=0;
 
-       int format;
-       if (channels==1 && bps==8)
-       {
+       if (channels == 1 && bps == 8)
                format=AL_FORMAT_MONO8;
-       } else
-       if (channels==1 && bps==16)
-       {
+       else if (channels == 1 && bps == 16)
                format=AL_FORMAT_MONO16;
-       } else
-       if (channels==2 && bps==8)
-       {
+       else if (channels == 2 && bps == 8)
                format=AL_FORMAT_STEREO16;
-       } else
-       if (channels==2 && bps==16)
-       {
+       else if (channels == 2 && bps == 16)
                format=AL_FORMAT_STEREO16;
-       } else
+       else
        {
                LOG("Invalid WAV file (wrong channels/bps): "+filename);
-               return -1;
+               return true;
        }
-       if(channels != 1)
-       {
-               LOG("Invalid WAV file: the file needs to be mono, and nothing 
else. Will try to continue anyways ...");
-       }
+
+       if (channels != 1) LOG("Invalid WAV file: the file needs to be mono, 
and nothing else. Will try to continue anyways ...");
+
        //okay, creating buffer
        void* bdata=malloc(dataSize);
-       if (!bdata) {LOG("Memory error reading file "+filename);return -1;}
-       if (stream->read(bdata, dataSize)!=dataSize) {LOG("Could not read file 
"+filename);return -1;}
+       if (!bdata) {LOG("Memory error reading file "+filename);return true;}
+       if (stream->read(bdata, dataSize) != dataSize) {LOG("Could not read 
file "+filename);return true;}
 
-//     LOG("alBufferData: format "+TOSTRING(format)+" size 
"+TOSTRING(dataSize)+" freq "+TOSTRING(freq));
-       alGetError(); //reset errors
+       //LOG("alBufferData: format "+TOSTRING(format)+" size 
"+TOSTRING(dataSize)+" freq "+TOSTRING(freq));
+       alGetError(); // Reset errors
        ALint error;
        alBufferData(buffer, format, bdata, dataSize, freq);
        error=alGetError();
-       if(error!=AL_NO_ERROR)
-       {
-               LOG("OpenAL error while loading buffer for "+filename+" : 
"+TOSTRING(error));
-               free(bdata);
-               return -1;
-       }
 
        free(bdata);
-       //stream will be closed by itself
+       // Stream will be closed by itself
 
-       return 0;
-}
+       if (error != AL_NO_ERROR) {LOG("OpenAL error while loading buffer for 
"+filename+" : "+TOSTRING(error));return true;}
 
-//-----------------------------------------------------------------------------------
-
-Sound::Sound(int input_index, ALuint buffer, SoundManager *sm)
-{
-       //mutex is supposed to be taken
-       this->sm=sm;
-       this->buffer=buffer;
-       this->input_index=input_index;
-       hardware_index=-1;
-       should_play=false;
-       gain=1.0;
-       pitch=1.0;
-       position=Vector3::ZERO;
-       velocity=Vector3::ZERO;
-       loop=false;
-       enabled=true;
-}
-
-void Sound::computeAudibility(Vector3 from)
-{
-       // disable sound?
-       if(!enabled)
-       {
-               audibility=0;
-               return;
-       }
-
-       //mutex is supposed to be taken
-       //first check if the sound is finished!
-       if (!loop && hardware_index!=-1 && should_play)
-       {
-               int value;
-               alGetSourcei(sm->hardware_sources[hardware_index], 
AL_SOURCE_STATE, &value);
-               if (value!=AL_PLAYING) should_play=false;
-       }
-       if (!should_play) {audibility=0.0; return;}
-       if (gain==0.0) {audibility=0.0; return;}
-       
-       float distance=(from-position).length();
-       
-       if (distance > sm->maxDistance) {audibility=0.0; return;}
-       if (distance < sm->referenceDistance) {audibility=gain; return;}
-       
-       audibility=gain*(sm->referenceDistance/(sm->referenceDistance+ 
(sm->rolloffFactor*(distance-sm->referenceDistance))));
-}
-
-bool Sound::isPlaying()
-{
-       MUTEX_LOCK(&sm->audio_mutex);
-       if (hardware_index!=-1)
-       {
-               int value;
-               alGetSourcei(sm->hardware_sources[hardware_index], 
AL_SOURCE_STATE, &value);
-               MUTEX_UNLOCK(&sm->audio_mutex);
-               return (value==AL_PLAYING);
-       }
-       MUTEX_UNLOCK(&sm->audio_mutex);
        return false;
 }
 
-void Sound::setEnabled(bool e)
-{
-       MUTEX_LOCK(&sm->audio_mutex);
-       enabled = e;
-       sm->recomputeSource(input_index, REASON_PLAY, 0, NULL);
-       MUTEX_UNLOCK(&sm->audio_mutex);
-}
-
-bool Sound::getEnabled()
-{
-       MUTEX_LOCK(&sm->audio_mutex);
-       return enabled;
-       MUTEX_UNLOCK(&sm->audio_mutex);
-}
-
-void Sound::play()
-{
-       MUTEX_LOCK(&sm->audio_mutex);
-       should_play=true;
-       sm->recomputeSource(input_index, REASON_PLAY, 0, NULL);
-       MUTEX_UNLOCK(&sm->audio_mutex);
-}
-
-void Sound::stop()
-{
-       MUTEX_LOCK(&sm->audio_mutex);
-       should_play=false;
-       sm->recomputeSource(input_index, REASON_STOP, 0, NULL);
-       MUTEX_UNLOCK(&sm->audio_mutex);
-}
-
-void Sound::setGain(float gain)
-{
-       MUTEX_LOCK(&sm->audio_mutex);
-       this->gain=gain;
-       sm->recomputeSource(input_index, REASON_GAIN, gain, NULL);
-       MUTEX_UNLOCK(&sm->audio_mutex);
-}
-
-void Sound::setLoop(bool loop)
-{
-       MUTEX_LOCK(&sm->audio_mutex);
-       this->loop=loop;
-       sm->recomputeSource(input_index, REASON_LOOP, (loop)?1.0:0.0, NULL);
-       MUTEX_UNLOCK(&sm->audio_mutex);
-}
-
-void Sound::setPitch(float pitch)
-{
-       MUTEX_LOCK(&sm->audio_mutex);
-       this->pitch=pitch;
-       sm->recomputeSource(input_index, REASON_PTCH, pitch, NULL);
-       MUTEX_UNLOCK(&sm->audio_mutex);
-}
-
-void Sound::setPosition(Ogre::Vector3 pos)
-{
-       MUTEX_LOCK(&sm->audio_mutex);
-       this->position=pos;
-       sm->recomputeSource(input_index, REASON_POSN, 0, &pos);
-       MUTEX_UNLOCK(&sm->audio_mutex);
-}
-
-void Sound::setVelocity(Ogre::Vector3 vel)
-{
-       MUTEX_LOCK(&sm->audio_mutex);
-       this->velocity=vel;
-       sm->recomputeSource(input_index, REASON_VLCT, 0, &vel);
-       MUTEX_UNLOCK(&sm->audio_mutex);
-}
-
-#endif //OPENAL
+#endif // USE_OPENAL

Modified: trunk/source/main/audio/SoundManager.h
===================================================================
--- trunk/source/main/audio/SoundManager.h      2012-01-30 22:37:09 UTC (rev 
2392)
+++ trunk/source/main/audio/SoundManager.h      2012-01-30 23:38:12 UTC (rev 
2393)
@@ -17,119 +17,74 @@
 You should have received a copy of the GNU General Public License
 along with Rigs of Rods.  If not, see <http://www.gnu.org/licenses/>.
 */
+
+#pragma once
+
 #ifdef USE_OPENAL
-#ifndef __SoundManager_H__
-#define __SoundManager_H__
+#ifndef __SoundManager_H_
+#define __SoundManager_H_
 
 #include "RoRPrerequisites.h"
-
 #include "Ogre.h"
-using namespace Ogre;
+#include <AL/al.h>
+#include <AL/alc.h>
 
-#include "AL/al.h"
-#include "AL/alc.h"
-
-#include "pthread.h"
-
-
-//maximum number of really mixed sources
-#define MAX_HARDWARE_SOURCES 32
-//maximum number of potential sound sources
-#define MAX_INPUT_SOURCES 8192
-//maximum number of sound files
-#define MAX_BUFFERS 2048
-
-#define REASON_PLAY 0
-#define REASON_STOP 1
-#define REASON_GAIN 2
-#define REASON_LOOP 3
-#define REASON_PTCH 4
-#define REASON_POSN 5
-#define REASON_VLCT 6
-
-
-class Sound
+class SoundManager
 {
+       friend class Sound;
+
 public:
-       Sound(int input_index, ALuint buffer, SoundManager* sm);
-       void setPitch(float pitch);
-       void setGain(float gain);
-       void setPosition(Vector3 pos);
-       void setVelocity(Vector3 vel);
-       bool isPlaying();
-       void setLoop(bool loop);
-       void play();
-       void stop();
-       void computeAudibility(Vector3 from);
-       float audibility;
-       //the hardware index, this value is dynamically updated as this input 
is played or not
-       int hardware_index;
-       ALuint buffer;
-       bool should_play;
-       float gain;
-       float pitch;
-       bool loop;
-       bool enabled;
-       
-       void setEnabled(bool e);
-       bool getEnabled();
+       static SoundManager* mSoundManager;
+ 
+    SoundManager();
+       ~SoundManager();
 
-       Vector3 position;
-       Vector3 velocity;
-private:
-       SoundManager* sm;
-       //the input index, this value should not change in the lifetime of the 
object
-       int input_index;
-};
+       Sound* createSound(Ogre::String filename);
 
-class SoundManager
-{
-public:
-       SoundManager();
-       int maxSources();
-       void setCamera(Vector3 position, Vector3 direction, Vector3 up, Vector3 
velocity);
+       void setCamera(Ogre::Vector3 position, Ogre::Vector3 direction, 
Ogre::Vector3 up, Ogre::Vector3 velocity);
        void pauseAllSounds();
        void resumeAllSounds();
-       Sound* createSound(String filename);
 
-       void recomputeAllSources();
-       void recomputeSource(int input_index, int reason, float vfl, Vector3 
*vvec);
-       float maxDistance;
-       float rolloffFactor;
-       float referenceDistance;
-       //AL hardware sources : this buffer contains valid AL handles up to 
num_hardware_sources
-       ALuint hardware_sources[MAX_HARDWARE_SOURCES];
-       //access mutex
-       pthread_mutex_t audio_mutex;
-private:
-       void assign(int input_index, int hardware_index);
-       void retire(int input_index);
-       int loadWAVFile(String filename, ALuint buffer);
+       int getNumHardwareSources() { return m_hardware_sources_num; }
 
+       static const float MAX_DISTANCE;
+       static const float ROLLOFF_FACTOR;
+       static const float REFERENCE_DISTANCE;
+       static const unsigned int MAX_HARDWARE_SOURCES = 32;
+       static const unsigned int MAX_AUDIO_SOURCES = 8192;
+       static const unsigned int MAX_AUDIO_BUFFERS = 2048;
 
-       //input sources: array of Sound
-       int free_input_source;
-       Sound* input_sources[MAX_INPUT_SOURCES];
+private:
+       void recomputeAllSources();
+       void recomputeSource(int source_index, int reason, float vfl, 
Ogre::Vector3 *vvec);
+       ALuint getHardwareSource(int hardware_index) { return 
m_hardware_sources[hardware_index]; };
 
-       //buffers: array of AL buffers and filenames
-       int free_buffer;
-       ALuint buffers[MAX_BUFFERS];
-       String buffer_filenames[MAX_BUFFERS];
+       void assign(int source_index, int hardware_index);
+       void retire(int source_index);
 
-       //total number of hardware sources available. This number is inferior 
or equal to MAX_HARDWARE_SOURCES
-       int num_hardware_sources;
-       //number of used hardware sources
-       int used_hardware_sources;
-       //This gives the input index for each hardware index. Value -1 means 
unmapped hardware source
-       int hardware_sources_input_map[MAX_HARDWARE_SOURCES];
+       bool SoundManager::loadWAVFile(Ogre::String filename, ALuint buffer);
 
-       Vector3 cameraPosition;
-       ALCdevice *device;
-       ALCcontext *context;
+       // Active audio sources (hardware sources)
+       int             m_hardware_sources_num;                                 
         // Total number of available hardware sources < MAX_HARDWARE_SOURCES
+       int             m_hardware_sources_in_use_count;
+       int             m_hardware_sources_map[MAX_HARDWARE_SOURCES]; // Stores 
the hardware index for each source. -1 = unmapped
+       ALuint  m_hardware_sources[MAX_HARDWARE_SOURCES];        // This buffer 
contains valid AL handles up to m_hardware_sources_num
 
+       // Audio sources
+       int             m_audio_sources_in_use_count;
+       Sound*  m_audio_sources[MAX_AUDIO_SOURCES];
+       // Helper for calculating the most audible sources
+       std::pair<int, float> m_audio_sources_most_audible[MAX_AUDIO_SOURCES];
+       
+       // Audio buffers: Array of AL buffers and filenames
+       int             m_audio_buffers_in_use_count;
+       ALuint  m_audio_buffers[MAX_AUDIO_BUFFERS];
+       Ogre::String    m_audio_buffer_file_name[MAX_AUDIO_BUFFERS];
+
+       Ogre::Vector3 camera_position;
+       ALCdevice *m_sound_device;
+       ALCcontext *m_sound_context;
 };
 
-#endif
-
-#endif //OPENAL
-
+#endif // __SoundManager_H_
+#endif // USE_OPENAL

Modified: trunk/source/main/audio/SoundScriptManager.cpp
===================================================================
--- trunk/source/main/audio/SoundScriptManager.cpp      2012-01-30 22:37:09 UTC 
(rev 2392)
+++ trunk/source/main/audio/SoundScriptManager.cpp      2012-01-30 23:38:12 UTC 
(rev 2393)
@@ -19,7 +19,7 @@
 */
 #ifdef USE_OPENAL
 
-#include "Ogre.h"
+#include "Sound.h"
 #include "SoundScriptManager.h"
 #include "Settings.h"
 
@@ -48,7 +48,7 @@
        }
        for (int i=0; i<SS_MAX_TRIG*(MAX_TRUCKS+2); i++) statemap[i]=false;
        sm=new SoundManager(); //we can give a device name if we want here
-       LOG("SoundScriptManager: Sound Manager started with 
"+TOSTRING(sm->maxSources())+" sources");
+       LOG("SoundScriptManager: Sound Manager started with 
"+TOSTRING(sm->getNumHardwareSources())+" sources");
        mScriptPatterns.push_back("*.soundscript");
        ResourceGroupManager::getSingleton()._registerScriptLoader(this);
 }

Modified: trunk/source/main/audio/SoundScriptManager.h
===================================================================
--- trunk/source/main/audio/SoundScriptManager.h        2012-01-30 22:37:09 UTC 
(rev 2392)
+++ trunk/source/main/audio/SoundScriptManager.h        2012-01-30 23:38:12 UTC 
(rev 2393)
@@ -23,14 +23,13 @@
 #define __SoundScriptManager_H__
 
 #include "RoRPrerequisites.h"
-
 #include "Ogre.h"
 #include "Beam.h"
 #include "OgreScriptLoader.h"
 #include "OgreResourceGroupManager.h"
+#include "Sound.h"
 #include "SoundManager.h"
 
-
 #define MAX_SOUNDS_PER_SCRIPT 16
 #define MAX_INSTANCES_PER_GROUP 256
 
@@ -132,21 +131,18 @@
 #define SS_MOD_AOA          28
 #define SS_MAX_MOD                     29
 
-
-using namespace Ogre;
-
 class SoundScriptTemplate
 {
 public:
-       SoundScriptTemplate(String name, String groupname, String filename, 
bool baseTemplate);
+       SoundScriptTemplate(Ogre::String name, Ogre::String groupname, 
Ogre::String filename, bool baseTemplate);
        bool setParameter(StringVector vec);
        ~SoundScriptTemplate();
 
 //private:
-       int parseModulation(String str);
-       String name;
-       String groupname;
-       String filename;
+       int parseModulation(Ogre::String str);
+       Ogre::String name;
+       Ogre::String groupname;
+       Ogre::String filename;
        int trigger_source;
        int pitch_source;
        float pitch_offset;
@@ -158,24 +154,24 @@
        float gain_square;
        bool has_start_sound;
        float start_sound_pitch;
-       String start_sound_name;
+       Ogre::String start_sound_name;
        bool has_stop_sound;
        float stop_sound_pitch;
-       String stop_sound_name;
+       Ogre::String stop_sound_name;
        bool unpitchable;
        int free_sound;
        float sound_pitches[MAX_SOUNDS_PER_SCRIPT];
-       String sound_names[MAX_SOUNDS_PER_SCRIPT];
+       Ogre::String sound_names[MAX_SOUNDS_PER_SCRIPT];
        bool baseTemplate;
 };
 
 class SoundScriptInstance
 {
 public:
-       SoundScriptInstance(int truck, SoundScriptTemplate* templ, 
SoundManager* sm, String instancename);
+       SoundScriptInstance(int truck, SoundScriptTemplate* templ, 
SoundManager* sm, Ogre::String instancename);
        void setPitch(float value);
        void setGain(float value);
-       void setPosition(Vector3 pos, Vector3 velocity);
+       void setPosition(Ogre::Vector3 pos, Ogre::Vector3 velocity);
        void runOnce();
        void start();
        void stop();
@@ -205,11 +201,11 @@
 
        //ScriptLoader interface
     const StringVector& getScriptPatterns(void) const;
-    void parseScript(DataStreamPtr& stream, const String& groupName);
+    void parseScript(DataStreamPtr& stream, const Ogre::String& groupName);
     Real getLoadingOrder(void) const;
 
-       SoundScriptInstance* createInstance(String templatename, int truck, 
SceneNode *toAttach);
-       void unloadResourceGroup(String groupname);
+       SoundScriptInstance* createInstance(Ogre::String templatename, int 
truck, SceneNode *toAttach);
+       void unloadResourceGroup(Ogre::String groupname);
        void clearNonBaseTemplates();
 
        //values update
@@ -228,7 +224,7 @@
 
        void soundEnable(bool state);
 
-       void setCamera(Vector3 position, Vector3 direction, Vector3 up, Vector3 
velocity);
+       void setCamera(Ogre::Vector3 position, Ogre::Vector3 direction, 
Ogre::Vector3 up, Ogre::Vector3 velocity);
        void setLoadingBaseSounds(bool v) { loadingBase = v; };
 
        float maxDistance;
@@ -243,7 +239,7 @@
 
        void skipToNextCloseBrace(DataStreamPtr& chunk);
        void skipToNextOpenBrace(DataStreamPtr& chunk);
-       SoundScriptTemplate* createTemplate(String name, String groupname, 
String filename);
+       SoundScriptTemplate* createTemplate(Ogre::String name, Ogre::String 
groupname, Ogre::String filename);
        //instances lookup tables
        int free_trigs[SS_MAX_TRIG];
        SoundScriptInstance* trigs[SS_MAX_TRIG*MAX_INSTANCES_PER_GROUP];

This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.


------------------------------------------------------------------------------
Try before you buy = See our experts in action!
The most comprehensive online learning library for Microsoft developers
is just $99.99! Visual Studio, SharePoint, SQL - plus HTML5, CSS3, MVC3,
Metro Style Apps, more. Free future releases when you subscribe now!
http://p.sf.net/sfu/learndevnow-dev2
_______________________________________________
Rigsofrods-devel mailing list
Rigsofrods-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/rigsofrods-devel

Reply via email to