diff options
Diffstat (limited to 'intern/SoundSystem/intern')
-rw-r--r-- | intern/SoundSystem/intern/Makefile | 50 | ||||
-rw-r--r-- | intern/SoundSystem/intern/SND_AudioDevice.cpp | 247 | ||||
-rw-r--r-- | intern/SoundSystem/intern/SND_AudioDevice.h | 118 | ||||
-rw-r--r-- | intern/SoundSystem/intern/SND_C-api.cpp | 391 | ||||
-rw-r--r-- | intern/SoundSystem/intern/SND_CDObject.cpp | 182 | ||||
-rw-r--r-- | intern/SoundSystem/intern/SND_DeviceManager.cpp | 137 | ||||
-rw-r--r-- | intern/SoundSystem/intern/SND_IdObject.cpp | 75 | ||||
-rw-r--r-- | intern/SoundSystem/intern/SND_IdObject.h | 59 | ||||
-rw-r--r-- | intern/SoundSystem/intern/SND_Scene.cpp | 569 | ||||
-rw-r--r-- | intern/SoundSystem/intern/SND_SoundListener.cpp | 184 | ||||
-rw-r--r-- | intern/SoundSystem/intern/SND_SoundObject.cpp | 506 | ||||
-rw-r--r-- | intern/SoundSystem/intern/SND_Utils.cpp | 395 | ||||
-rw-r--r-- | intern/SoundSystem/intern/SND_WaveCache.cpp | 134 | ||||
-rw-r--r-- | intern/SoundSystem/intern/SND_WaveSlot.cpp | 182 |
14 files changed, 3229 insertions, 0 deletions
diff --git a/intern/SoundSystem/intern/Makefile b/intern/SoundSystem/intern/Makefile new file mode 100644 index 00000000000..ae5805ad5bc --- /dev/null +++ b/intern/SoundSystem/intern/Makefile @@ -0,0 +1,50 @@ +# +# $Id$ +# +# ***** BEGIN GPL/BL DUAL LICENSE BLOCK ***** +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. The Blender +# Foundation also sells licenses for use in proprietary software under +# the Blender License. See http://www.blender.org/BL/ for information +# about this. +# +# This program 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 this program; if not, write to the Free Software Foundation, +# Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +# +# The Original Code is Copyright (C) 2001-2002 by NaN Holding BV. +# All rights reserved. +# +# The Original Code is: all of this file. +# +# Contributor(s): none yet. +# +# ***** END GPL/BL DUAL LICENSE BLOCK ***** +# +# + +LIBNAME = SoundSystem +DIR = $(OCGDIR)/gameengine/$(LIBNAME) + +include nan_compile.mk + +CCFLAGS += $(LEVEL_1_CPP_WARNINGS) + +CPPFLAGS += -I$(NAN_FMOD)/include +CPPFLAGS += -I$(NAN_STRING)/include +CPPFLAGS += -I$(NAN_MOTO)/include +CPPFLAGS += -I../../../kernel/gen_system +CPPFLAGS += -I../../../blender/include +CPPFLAGS += -I../dummy +CPPFLAGS += -I../fmod +CPPFLAGS += -I../openal +CPPFLAGS += -I.. +CPPFLAGS += -I. diff --git a/intern/SoundSystem/intern/SND_AudioDevice.cpp b/intern/SoundSystem/intern/SND_AudioDevice.cpp new file mode 100644 index 00000000000..20800153128 --- /dev/null +++ b/intern/SoundSystem/intern/SND_AudioDevice.cpp @@ -0,0 +1,247 @@ +/** + * $Id$ + * ***** BEGIN GPL/BL DUAL LICENSE BLOCK ***** + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. The Blender + * Foundation also sells licenses for use in proprietary software under + * the Blender License. See http://www.blender.org/BL/ for information + * about this. + * + * This program 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 this program; if not, write to the Free Software Foundation, + * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV. + * All rights reserved. + * + * The Original Code is: all of this file. + * + * Contributor(s): none yet. + * + * ***** END GPL/BL DUAL LICENSE BLOCK ***** + */ + +#include "SND_AudioDevice.h" + +#include "SND_SoundObject.h" + +#ifdef WIN32 +// This warning tells us about truncation of __long__ stl-generated names. +// It can occasionally cause DevStudio to have internal compiler warnings. +#pragma warning( disable : 4786 ) +#endif + + +SND_AudioDevice::SND_AudioDevice() +{ + m_wavecache = NULL; + m_audio = false; + + for (int i = 0; i < NUM_SOURCES; i++) + { + m_idObjectArray[i] = new SND_IdObject(); + m_idObjectArray[i]->SetId(i); + m_idObjectArray[i]->SetSoundObject(NULL); + m_idObjectList.addTail(m_idObjectArray[i]); + } +} + + + +SND_AudioDevice::~SND_AudioDevice() +{ + for (int i = 0; i < NUM_SOURCES; i++) + { + delete m_idObjectArray[i]; + m_idObjectArray[i] = NULL; + } + + if (m_wavecache) + { + delete m_wavecache; + m_wavecache = NULL; + } +} + + + +bool SND_AudioDevice::IsInitialized() +{ + return m_audio; +} + + + +SND_WaveCache* SND_AudioDevice::GetWaveCache() const +{ + return m_wavecache; +} + + + +/* seeks an unused id and returns it */ +bool SND_AudioDevice::GetNewId(SND_SoundObject* pObject) +{ +#ifdef ONTKEVER + printf("SND_AudioDevice::GetNewId\n"); +#endif + + bool result; + + // first, get the oldest (the first) idobject + SND_IdObject* pIdObject = (SND_IdObject*)m_idObjectList.getHead(); + + if (pIdObject->isTail()) + { + result = false; + } + else + { + // find the first id object which doesn't have a high priority soundobject + bool ThisSoundMustStay = false; + bool OutOfIds = false; + + do + { + // if no soundobject present, it's seat may be taken + if (pIdObject->GetSoundObject()) + { + // and also if it ain't highprio + if (pIdObject->GetSoundObject()->IsHighPriority()) + { + ThisSoundMustStay = true; + pIdObject = (SND_IdObject*)pIdObject->getNext(); + + // if the last one is a priority sound too, then there are no id's left + // and we won't add any new sounds + if (pIdObject->isTail()) + OutOfIds = true; + } + else + { + ThisSoundMustStay = false; + } + } + else + { + ThisSoundMustStay = false; + } + + } while (ThisSoundMustStay && !OutOfIds); + + if (!OutOfIds) + { + SND_SoundObject* oldobject = oldobject = pIdObject->GetSoundObject(); + + // revoke the old object if present + if (oldobject) + { +#ifdef ONTKEVER + printf("oldobject: %x\n", oldobject); +#endif + RevokeSoundObject(oldobject); + } + + // set the new soundobject into the idobject + pIdObject->SetSoundObject(pObject); + + // set the id into the soundobject + int id = pIdObject->GetId(); + pObject->SetId(id); + + // connect the new id to the buffer the sample is stored in + SetObjectBuffer(id, pObject->GetBuffer()); + + // remove the idobject from the list and add it in the back again + pIdObject->remove(); + m_idObjectList.addTail(pIdObject); + + result = true; + } + } + + return result; +} + + + +void SND_AudioDevice::ClearId(SND_SoundObject* pObject) +{ +#ifdef ONTKEVER + printf("SND_AudioDevice::ClearId\n"); +#endif + + if (pObject) + { + int id = pObject->GetId(); + + if (id != -1) + { + // lets get the idobject belonging to the soundobject + SND_IdObject* pIdObject = m_idObjectArray[id]; + SND_SoundObject* oldobject = pIdObject->GetSoundObject(); + + if (oldobject) + { + RevokeSoundObject(oldobject); + + // clear the idobject from the soundobject + pIdObject->SetSoundObject(NULL); + } + + // remove the idobject and place it in front + pIdObject->remove(); + m_idObjectList.addHead(pIdObject); + } + } +} + + + +void SND_AudioDevice::RevokeSoundObject(SND_SoundObject* pObject) +{ +#ifdef ONTKEVER + printf("SND_AudioDevice::RevokeSoundObject\n"); +#endif + + // stop the soundobject + int id = pObject->GetId(); + + if (id >= 0 && id < NUM_SOURCES) + { + StopObject(id); + + // remove the object from the 'activelist' + pObject->SetActive(false); + +#ifdef ONTKEVER + printf("pObject->remove();\n"); +#endif + } + + // make sure its id is invalid + pObject->SetId(-1); +} + +/* +void SND_AudioDevice::RemoveSample(const char* filename) +{ + if (m_wavecache) + m_wavecache->RemoveSample(filename); +} +*/ + +void SND_AudioDevice::RemoveAllSamples() +{ + if (m_wavecache) + m_wavecache->RemoveAllSamples(); +} + diff --git a/intern/SoundSystem/intern/SND_AudioDevice.h b/intern/SoundSystem/intern/SND_AudioDevice.h new file mode 100644 index 00000000000..54a8738e43f --- /dev/null +++ b/intern/SoundSystem/intern/SND_AudioDevice.h @@ -0,0 +1,118 @@ +/** + * $Id$ + * + * ***** BEGIN GPL/BL DUAL LICENSE BLOCK ***** + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. The Blender + * Foundation also sells licenses for use in proprietary software under + * the Blender License. See http://www.blender.org/BL/ for information + * about this. + * + * This program 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 this program; if not, write to the Free Software Foundation, + * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV. + * All rights reserved. + * + * The Original Code is: all of this file. + * + * Contributor(s): none yet. + * + * ***** END GPL/BL DUAL LICENSE BLOCK ***** + */ +#ifndef SND_AUDIODEVICE +#define SND_AUDIODEVICE + +#include "SND_IAudioDevice.h" +#include "SoundDefines.h" +#include "SND_IdObject.h" + + +class SND_AudioDevice : public SND_IAudioDevice +{ +public: + SND_AudioDevice(); + virtual ~SND_AudioDevice(); + + virtual bool IsInitialized(); + + SND_WaveCache* GetWaveCache() const; + + bool GetNewId(SND_SoundObject* pObject); + void ClearId(SND_SoundObject* pObject); + + void UseCD() const {}; + + /* to be implemented in derived class + + virtual SND_WaveSlot* LoadSample(const STR_String& samplename, + void* memlocation, + int size) =0; + */ +// void RemoveSample(const char* filename); + void RemoveAllSamples(); + + /* to be implemented in derived class + + virtual void InitListener()=0; + virtual void SetListenerGain(float gain) const =0; + virtual void SetDopplerVelocity(MT_Scalar dopplervelocity) const =0; + virtual void SetDopplerFactor(MT_Scalar dopplerfactor) const =0; + virtual void SetListenerRollOffFactor(MT_Scalar rollofffactor) const =0; + + virtual void MakeCurrent() const =0; + + virtual void UpdateDevice() const =0; + + virtual void SetObjectBuffer(int id, unsigned int buffer)=0; + virtual int GetPlayState(int id)=0; + virtual void PlayObject(int id)=0; + virtual void StopObject(int id) const =0; + virtual void StopAllObjects()=0; + virtual void PauseObject(int id) const =0; + + virtual void SetObjectLoop(int id, bool loop) const =0; + virtual void SetObjectLoopPoints(int id, unsigned int loopstart, unsigned int loopend) const =0; + virtual void SetObjectPitch(int id, MT_Scalar pitch) const =0; + virtual void SetObjectGain(int id, MT_Scalar gain) const =0; + virtual void SetObjectRollOffFactor(int id, MT_Scalar rolloff) const =0; + virtual void SetObjectMinGain(int id, MT_Scalar mingain) const =0; + virtual void SetObjectMaxGain(int id, MT_Scalar maxgain) const =0; + virtual void SetObjectReferenceDistance(int id, MT_Scalar referencedistance) const =0; + + virtual void SetObjectTransform(int id, + const MT_Vector3& position, + const MT_Vector3& velocity, + const MT_Matrix3x3& orientation, + const MT_Vector3& lisposition, + const MT_Scalar& rollofffactor) const =0; + virtual void ObjectIs2D(int id) const =0; + + virtual void PlayCD(int track) const =0; + virtual void PauseCD(bool pause) const =0; + virtual void StopCD() const =0; + virtual void SetCDPlaymode(int playmode) const =0; + virtual void SetCDGain(MT_Scalar gain) const =0; + virtual float* GetSpectrum() =0; + */ + +protected: + bool m_audio; + GEN_List m_idObjectList; + SND_IdObject* m_idObjectArray[NUM_SOURCES]; + SND_WaveCache* m_wavecache; + +private: + void RevokeSoundObject(SND_SoundObject* pObject); +}; + +#endif //SND_AUDIODEVICE diff --git a/intern/SoundSystem/intern/SND_C-api.cpp b/intern/SoundSystem/intern/SND_C-api.cpp new file mode 100644 index 00000000000..8a4fc8f31a6 --- /dev/null +++ b/intern/SoundSystem/intern/SND_C-api.cpp @@ -0,0 +1,391 @@ +/* + * SND_C-Api.cpp + * + * C Api for soundmodule + * + * $Id$ + * + * ***** BEGIN GPL/BL DUAL LICENSE BLOCK ***** + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. The Blender + * Foundation also sells licenses for use in proprietary software under + * the Blender License. See http://www.blender.org/BL/ for information + * about this. + * + * This program 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 this program; if not, write to the Free Software Foundation, + * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV. + * All rights reserved. + * + * The Original Code is: all of this file. + * + * Contributor(s): none yet. + * + * ***** END GPL/BL DUAL LICENSE BLOCK ***** + */ + +#include "SND_C-api.h" +#include "SND_DeviceManager.h" +#include "SND_Scene.h" + +#ifdef WIN32 +#pragma warning (disable:4786) // get rid of stupid stl-visual compiler debug warning +#endif //WIN32 + + + +void SND_SetDeviceType(int device_type) +{ + SND_DeviceManager::SetDeviceType(device_type); +} + + + +SND_AudioDeviceInterfaceHandle SND_GetAudioDevice() +{ + SND_IAudioDevice* audiodevice = NULL; + + SND_DeviceManager::Subscribe(); + audiodevice = SND_DeviceManager::Instance(); + + if (!audiodevice->IsInitialized()) + { + SND_DeviceManager::SetDeviceType(snd_e_dummydevice); + audiodevice = SND_DeviceManager::Instance(); + } + + return (SND_AudioDeviceInterfaceHandle)audiodevice; +} + + + +void SND_ReleaseDevice() +{ + SND_DeviceManager::Unsubscribe(); +} + + + +int SND_IsPlaybackWanted(SND_SceneHandle scene) +{ + assert(scene); + bool result = ((SND_Scene*)scene)->IsPlaybackWanted(); + + return (int)result; +} + + + +// create a scene +SND_SceneHandle SND_CreateScene(SND_AudioDeviceInterfaceHandle audiodevice) +{ + // initialize sound scene and object + SND_Scene* scene = new SND_Scene((SND_IAudioDevice*)audiodevice); + + return (SND_SceneHandle)scene; +} + + + +void SND_DeleteScene(SND_SceneHandle scene) +{ + assert(scene); + delete (SND_Scene*)scene; +} + + + +int SND_AddSample(SND_SceneHandle scene, + const char* filename, + void* memlocation, + int size) +{ + assert(scene); + assert(memlocation); + int buffer = ((SND_Scene*)scene)->LoadSample(filename, memlocation, size); + + return buffer; +} + + + +void SND_RemoveAllSamples(SND_SceneHandle scene) +{ + assert(scene); + ((SND_Scene*)scene)->RemoveAllSamples(); +} + + + +int SND_CheckBuffer(SND_SceneHandle scene, SND_ObjectHandle object) +{ + assert(scene); + assert(object); + int result = (int)((SND_Scene*)scene)->CheckBuffer((SND_SoundObject*)object); + + return result; +} + + + +void SND_AddSound(SND_SceneHandle scene, SND_ObjectHandle object) +{ + assert(scene); + assert(object); + ((SND_Scene*)scene)->AddObject((SND_SoundObject *)object); +} + + + +void SND_RemoveSound(SND_SceneHandle scene, SND_ObjectHandle object) +{ + assert(scene); + assert(object); + ((SND_Scene*)scene)->DeleteObject((SND_SoundObject *)object); +} + + + +void SND_RemoveAllSounds(SND_SceneHandle scene) +{ + assert(scene); + ((SND_Scene*)scene)->RemoveAllObjects(); +} + + + +void SND_StopAllSounds(SND_SceneHandle scene) +{ + assert(scene); + ((SND_Scene*)scene)->StopAllObjects(); +} + + + +void SND_Proceed(SND_AudioDeviceInterfaceHandle audiodevice, SND_SceneHandle scene) +{ + assert(scene); + ((SND_Scene*)scene)->Proceed(); + ((SND_IAudioDevice*)audiodevice)->NextFrame(); +} + + + +SND_ListenerHandle SND_GetListener(SND_SceneHandle scene) +{ + assert(scene); + return (SND_ListenerHandle)((SND_Scene*)scene)->GetListener(); +} + + + +void SND_SetListenerGain(SND_SceneHandle scene, double gain) +{ + assert(scene); + SND_SoundListener* listener = ((SND_Scene*)scene)->GetListener(); + listener->SetGain((MT_Scalar)gain); +} + + + +void SND_SetDopplerFactor(SND_SceneHandle scene, double dopplerfactor) +{ + assert(scene); + SND_SoundListener* listener = ((SND_Scene*)scene)->GetListener(); + listener->SetDopplerFactor(dopplerfactor); +} + + + +void SND_SetDopplerVelocity(SND_SceneHandle scene, double dopplervelocity) +{ + assert(scene); + SND_SoundListener* listener = ((SND_Scene*)scene)->GetListener(); + listener->SetDopplerVelocity(dopplervelocity); +} + + + +// Object instantiation +SND_ObjectHandle SND_CreateSound() +{ + return (SND_ObjectHandle)new SND_SoundObject(); +} + + + +void SND_DeleteSound(SND_ObjectHandle object) +{ + assert(object); + delete (SND_SoundObject*)object; +} + + + +// Object control +void SND_StartSound(SND_SceneHandle scene, SND_ObjectHandle object) +{ + assert(scene); + assert(object); + ((SND_Scene*)scene)->AddActiveObject((SND_SoundObject*)object, 0); +} + + + +void SND_StopSound(SND_SceneHandle scene, SND_ObjectHandle object) +{ + assert(scene); + assert(object); + ((SND_Scene*)scene)->RemoveActiveObject((SND_SoundObject*)object); +} + + + +void SND_PauseSound(SND_SceneHandle scene, SND_ObjectHandle object) +{ + assert(scene); + assert(object); + ((SND_Scene*)scene)->RemoveActiveObject((SND_SoundObject*)object); +} + + + +void SND_SetSampleName(SND_ObjectHandle object, char* samplename) +{ + assert(object); + STR_String name = samplename; + ((SND_SoundObject*)object)->SetSampleName(name); +} + + + +void SND_SetGain(SND_ObjectHandle object, double gain) +{ + assert(object); + ((SND_SoundObject*)object)->SetGain(gain); +} + + + +void SND_SetMinimumGain(SND_ObjectHandle object, double minimumgain) +{ + assert(object); + ((SND_SoundObject*)object)->SetMinGain(minimumgain); +} + + + +void SND_SetMaximumGain(SND_ObjectHandle object, double maximumgain) +{ + assert(object); + ((SND_SoundObject*)object)->SetMaxGain(maximumgain); +} + + + +void SND_SetRollOffFactor(SND_ObjectHandle object, double rollofffactor) +{ + assert(object); + ((SND_SoundObject*)object)->SetRollOffFactor(rollofffactor); +} + + + +void SND_SetReferenceDistance(SND_ObjectHandle object, double referencedistance) +{ + assert(object); + ((SND_SoundObject*)object)->SetReferenceDistance(referencedistance); +} + + + +void SND_SetPitch(SND_ObjectHandle object, double pitch) +{ + assert(object); + ((SND_SoundObject*)object)->SetPitch(pitch); +} + + + +void SND_SetPosition(SND_ObjectHandle object, double* position) +{ + assert(object); + ((SND_SoundObject*)object)->SetPosition(position); +} + + + +void SND_SetVelocity(SND_ObjectHandle object, double* velocity) +{ + assert(object); + ((SND_SoundObject*)object)->SetVelocity(velocity); +} + + + +void SND_SetOrientation(SND_ObjectHandle object, double* orientation) +{ + assert(object); + ((SND_SoundObject*)object)->SetOrientation(orientation); +} + + + +void SND_SetLoopMode(SND_ObjectHandle object, int loopmode) +{ + assert(object); + ((SND_SoundObject*)object)->SetLoopMode(loopmode); +} + + + +void SND_SetLoopPoints(SND_ObjectHandle object, unsigned int loopstart, unsigned int loopend) +{ + assert(object); + ((SND_SoundObject*)object)->SetLoopStart(loopstart); + ((SND_SoundObject*)object)->SetLoopEnd(loopend); +} + + + +float SND_GetGain(SND_ObjectHandle object) +{ + assert(object); + MT_Scalar gain = ((SND_SoundObject*)object)->GetGain(); + return (float) gain; +} + + + +float SND_GetPitch(SND_ObjectHandle object) +{ + assert(object); + MT_Scalar pitch = ((SND_SoundObject*)object)->GetPitch(); + return (float) pitch; +} + + + +int SND_GetLoopMode(SND_ObjectHandle object) +{ + assert(object); + return ((SND_SoundObject*)object)->GetLoopMode(); +} + + + +int SND_GetPlaystate(SND_ObjectHandle object) +{ + assert(object); + return ((SND_SoundObject*)object)->GetPlaystate(); +} diff --git a/intern/SoundSystem/intern/SND_CDObject.cpp b/intern/SoundSystem/intern/SND_CDObject.cpp new file mode 100644 index 00000000000..97b502970b7 --- /dev/null +++ b/intern/SoundSystem/intern/SND_CDObject.cpp @@ -0,0 +1,182 @@ +/* + * SND_CDObject.cpp + * + * Implementation for CD playback + * + * $Id$ + * + * ***** BEGIN GPL/BL DUAL LICENSE BLOCK ***** + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. The Blender + * Foundation also sells licenses for use in proprietary software under + * the Blender License. See http://www.blender.org/BL/ for information + * about this. + * + * This program 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 this program; if not, write to the Free Software Foundation, + * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV. + * All rights reserved. + * + * The Original Code is: all of this file. + * + * Contributor(s): none yet. + * + * ***** END GPL/BL DUAL LICENSE BLOCK ***** + */ + +#include "SND_CDObject.h" + + +SND_CDObject* SND_CDObject::m_instance = NULL; + +bool SND_CDObject::CreateSystem() +{ + bool result = false; + + if (!m_instance) + { + m_instance = new SND_CDObject(); + result = true; + } + + return result; +} + + + +bool SND_CDObject::DisposeSystem() +{ + bool result = false; + + if (m_instance) + { + delete m_instance; + m_instance = NULL; + result = true; + } + + return result; +} + + + +SND_CDObject* SND_CDObject::Instance() +{ + return m_instance; +} + + + +SND_CDObject::SND_CDObject() +{ + m_gain = 1; + m_playmode = SND_CD_ALL; + m_track = 1; + m_playstate = SND_STOPPED; + m_used = false; + + // don't set the cd standard on modified: + // if not used, we don't wanna touch it (performance) + m_modified = false; +} + + + +SND_CDObject::~SND_CDObject() +{ +} + + + +void SND_CDObject::SetGain(MT_Scalar gain) +{ + m_gain = gain; + m_modified = true; +} + + + +void SND_CDObject::SetPlaymode(int playmode) +{ + m_playmode = playmode; +} + + + +void SND_CDObject::SetPlaystate(int playstate) +{ + m_playstate = playstate; +} + + + +void SND_CDObject::SetTrack(int track) +{ + m_track = track; +} + + + +int SND_CDObject::GetTrack() const +{ + return m_track; +} + + + +MT_Scalar SND_CDObject::GetGain() const +{ + return m_gain; +} + + +int SND_CDObject::GetPlaystate() const +{ + return m_playstate; +} + + + +bool SND_CDObject::IsModified() const +{ + return m_modified; +} + + + +void SND_CDObject::SetModified(bool modified) +{ + m_modified = modified; +} + + + +int SND_CDObject::GetPlaymode() const +{ + return m_playmode; +} + + + +void SND_CDObject::SetUsed() +{ + m_used = true; +} + + + +bool SND_CDObject::GetUsed() +{ + return m_used; +} + diff --git a/intern/SoundSystem/intern/SND_DeviceManager.cpp b/intern/SoundSystem/intern/SND_DeviceManager.cpp new file mode 100644 index 00000000000..a3e72f8e9f6 --- /dev/null +++ b/intern/SoundSystem/intern/SND_DeviceManager.cpp @@ -0,0 +1,137 @@ +/* + * SND_DeviceManager.h + * + * singleton for creating, switching and deleting audiodevices + * + * $Id$ + * + * ***** BEGIN GPL/BL DUAL LICENSE BLOCK ***** + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. The Blender + * Foundation also sells licenses for use in proprietary software under + * the Blender License. See http://www.blender.org/BL/ for information + * about this. + * + * This program 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 this program; if not, write to the Free Software Foundation, + * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV. + * All rights reserved. + * + * The Original Code is: all of this file. + * + * Contributor(s): none yet. + * + * ***** END GPL/BL DUAL LICENSE BLOCK ***** + */ + +#include "SND_DeviceManager.h" +#include "SND_DependKludge.h" +#include "SND_DummyDevice.h" +#ifdef USE_FMOD +#include "SND_FmodDevice.h" +#endif +#ifdef USE_OPENAL +#include "SND_OpenALDevice.h" +#endif + +SND_IAudioDevice* SND_DeviceManager::m_instance = NULL; +int SND_DeviceManager::m_subscriptions = 0; + +#ifdef USE_OPENAL +int SND_DeviceManager::m_device_type = snd_e_openaldevice; +#else +# ifdef USE_FMOD +int SND_DeviceManager::m_device_type = snd_e_fmoddevice; +# else +int SND_DeviceManager::m_device_type = snd_e_dummydevice; +# endif +#endif + +void SND_DeviceManager::Subscribe() +{ + ++m_subscriptions; +} + + + +void SND_DeviceManager::Unsubscribe() +{ + --m_subscriptions; + + // only release memory if there is a m_instance but no subscriptions left + if (m_subscriptions == 0 && m_instance) + { + delete m_instance; + m_instance = NULL; + } +} + + + +SND_IAudioDevice* SND_DeviceManager::Instance() +{ + // only give away an instance if there are subscriptions + if (m_subscriptions) + { + // if there's no instance yet, set and create a new one + if (m_instance == NULL) + { + SetDeviceType(m_device_type); + } + + return m_instance; + } + else + { + return NULL; + } +} + + + +void SND_DeviceManager::SetDeviceType(int device_type) +{ + // if we want to change devicetype, first delete the old one + if (m_instance) + { + delete m_instance; + m_instance = NULL; + } + + // let's create the chosen device + switch (device_type) + { +#ifdef USE_FMOD + case snd_e_fmoddevice: + { + m_instance = new SND_FmodDevice(); + m_device_type = device_type; + break; + } +#endif +#ifdef USE_OPENAL + case snd_e_openaldevice: + { + m_instance = new SND_OpenALDevice(); + m_device_type = device_type; + break; + } +#endif + default: + { + m_instance = new SND_DummyDevice(); + m_device_type = device_type; + break; + } + } +} diff --git a/intern/SoundSystem/intern/SND_IdObject.cpp b/intern/SoundSystem/intern/SND_IdObject.cpp new file mode 100644 index 00000000000..b7e13cacbe6 --- /dev/null +++ b/intern/SoundSystem/intern/SND_IdObject.cpp @@ -0,0 +1,75 @@ +/* + * SND_IdObject.cpp + * + * Object for storing runtime data, like id's, soundobjects etc + * + * $Id$ + * + * ***** BEGIN GPL/BL DUAL LICENSE BLOCK ***** + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. The Blender + * Foundation also sells licenses for use in proprietary software under + * the Blender License. See http://www.blender.org/BL/ for information + * about this. + * + * This program 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 this program; if not, write to the Free Software Foundation, + * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV. + * All rights reserved. + * + * The Original Code is: all of this file. + * + * Contributor(s): none yet. + * + * ***** END GPL/BL DUAL LICENSE BLOCK ***** + */ + +#include "SND_IdObject.h" + +SND_IdObject::SND_IdObject() +{ +} + + + +SND_IdObject::~SND_IdObject() +{ +} + + + +SND_SoundObject* SND_IdObject::GetSoundObject() +{ + return m_soundObject; +} + + + +void SND_IdObject::SetSoundObject(SND_SoundObject* pObject) +{ + m_soundObject = pObject; +} + + + +int SND_IdObject::GetId() +{ + return m_id; +} + + + +void SND_IdObject::SetId(int id) +{ + m_id = id; +} diff --git a/intern/SoundSystem/intern/SND_IdObject.h b/intern/SoundSystem/intern/SND_IdObject.h new file mode 100644 index 00000000000..116479eb69a --- /dev/null +++ b/intern/SoundSystem/intern/SND_IdObject.h @@ -0,0 +1,59 @@ +/* + * SND_IdObject.h + * + * Object for storing runtime data, like id's, soundobjects etc + * + * $Id$ + * + * ***** BEGIN GPL/BL DUAL LICENSE BLOCK ***** + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. The Blender + * Foundation also sells licenses for use in proprietary software under + * the Blender License. See http://www.blender.org/BL/ for information + * about this. + * + * This program 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 this program; if not, write to the Free Software Foundation, + * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV. + * All rights reserved. + * + * The Original Code is: all of this file. + * + * Contributor(s): none yet. + * + * ***** END GPL/BL DUAL LICENSE BLOCK ***** + */ + +#ifndef __SND_IDOBJECT_H +#define __SND_IDOBJECT_H + +#include "SND_SoundObject.h" +#include "GEN_List.h" +#include "SoundDefines.h" + +class SND_IdObject : public GEN_Link +{ + SND_SoundObject* m_soundObject; + int m_id; + +public: + SND_IdObject(); + virtual ~SND_IdObject(); + + SND_SoundObject* GetSoundObject(); + void SetSoundObject(SND_SoundObject* pObject); + + int GetId(); + void SetId(int id); +}; +#endif //__SND_OBJECT_H diff --git a/intern/SoundSystem/intern/SND_Scene.cpp b/intern/SoundSystem/intern/SND_Scene.cpp new file mode 100644 index 00000000000..5bc65f02359 --- /dev/null +++ b/intern/SoundSystem/intern/SND_Scene.cpp @@ -0,0 +1,569 @@ +/* +* SND_Scene.cpp +* +* The scene for sounds. +* +* $Id$ +* + * ***** BEGIN GPL/BL DUAL LICENSE BLOCK ***** + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. The Blender + * Foundation also sells licenses for use in proprietary software under + * the Blender License. See http://www.blender.org/BL/ for information + * about this. + * + * This program 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 this program; if not, write to the Free Software Foundation, + * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV. + * All rights reserved. + * + * The Original Code is: all of this file. + * + * Contributor(s): none yet. + * + * ***** END GPL/BL DUAL LICENSE BLOCK ***** +*/ + +#ifdef WIN32 +#pragma warning (disable:4786) // Get rid of stupid stl-visual compiler debug warning +#endif //WIN32 + +#include "SND_Scene.h" +#include "SND_DependKludge.h" +#include "SYS_System.h" +#include "SND_IAudioDevice.h" + +#include <stdlib.h> +#include <iostream> + +//static unsigned int tijd = 0; + +SND_Scene::SND_Scene(SND_IAudioDevice* audiodevice) + : m_audiodevice(audiodevice) +{ + if (m_audiodevice) + m_wavecache = m_audiodevice->GetWaveCache(); + + if (!m_wavecache || !audiodevice) + { + m_audio = false; + } + else + { + //if so, go ahead! + m_audio = true; +#ifdef ONTKEVER + printf("SND_Scene::SND_Scene() m_audio == true\n"); +#endif + m_audiodevice->InitListener(); + } + + IsPlaybackWanted(); +} + + + +SND_Scene::~SND_Scene() +{ + StopAllObjects(); +} + + + +// check if audioplayback is wanted +bool SND_Scene::IsPlaybackWanted() +{ + SYS_SystemHandle syshandle = SYS_GetSystem(); + int audio = SYS_GetCommandLineInt(syshandle,"noaudio",0); + + if ((audio == 0) && m_audiodevice && m_wavecache) + { + m_audioplayback = true; + } + else + { + StopAllObjects(); + m_audioplayback = false; + } + + return m_audioplayback; +} + + + +int SND_Scene::LoadSample(const STR_String& samplename, + void* memlocation, + int size) +{ + int result = -1; + + if (m_audiodevice) + { + SND_WaveSlot* waveslot = m_audiodevice->LoadSample(samplename, memlocation, size); + + if (waveslot) + result = waveslot->GetBuffer(); + } + + return result; +} + + + +void SND_Scene::RemoveAllSamples() +{ + if (m_audio && m_audiodevice) + m_audiodevice->RemoveAllSamples(); +} + + + +bool SND_Scene::CheckBuffer(SND_SoundObject* pObject) +{ + bool result = false; + + if (pObject && m_wavecache) + { + SND_WaveSlot* waveslot = m_wavecache->GetWaveSlot(pObject->GetSampleName()); + + if (waveslot) + { + pObject->SetBuffer(waveslot->GetBuffer()); + + result = true; + } + } + + return result; +} + + + +bool SND_Scene::IsSampleLoaded(STR_String& samplename) +{ + bool result = false; + + if (samplename && m_wavecache) + { + SND_WaveSlot* waveslot = m_wavecache->GetWaveSlot(samplename); + + if (waveslot && waveslot->IsLoaded()) + result = true; + } + + return result; +} + + + +void SND_Scene::AddObject(SND_SoundObject* pObject) +{ + if (m_audio) + { + STR_String samplename = pObject->GetSampleName(); + SND_WaveSlot* slot = NULL; + + // don't add the object if no valid sample is referenced + if (samplename != "") + { + // check if the sample is already loaded + slot = m_wavecache->GetWaveSlot(samplename); + } + + if (slot) + { + pObject->SetBuffer(slot->GetBuffer()); + + // needed for expected lifespan of the sample, but ain't necesary anymore i think + MT_Scalar samplelength = slot->GetNumberOfSamples(); + MT_Scalar samplerate = slot->GetSampleRate(); + MT_Scalar soundlength = samplelength/samplerate; + pObject->SetLength(soundlength); + + // add the object to the list + m_soundobjects.insert((SND_SoundObject*)pObject); + } + } +} + + + +void SND_Scene::SetListenerTransform(const MT_Vector3& pos, + const MT_Vector3& vel, + const MT_Matrix3x3& ori) +{ + if (m_audio) + { + GetListener()->SetPosition(pos); + GetListener()->SetVelocity(vel); + GetListener()->SetOrientation(ori); + } +} + + + +void SND_Scene::UpdateListener() +{ + // process the listener if modified + if (m_listener.IsModified()) + { + m_audiodevice->SetListenerGain(m_listener.GetGain()); + + // fmod doesn't support dopplervelocity, so just use the dopplerfactor instead +#ifdef USE_FMOD + m_audiodevice->SetDopplerFactor(m_listener.GetDopplerVelocity()); +#else + m_audiodevice->SetDopplerVelocity(m_listener.GetDopplerVelocity()); + m_audiodevice->SetDopplerFactor(m_listener.GetDopplerFactor()); +#endif + m_listener.SetModified(false); + } +} + + + +void SND_Scene::AddActiveObject(SND_SoundObject* pObject, MT_Scalar curtime) +{ + if (m_audio) + { + if (pObject) + { +#ifdef ONTKEVER + printf("SND_Scene::AddActiveObject\n"); +#endif + + // first check if the object is already on the list + if (pObject->IsActive()) + { + pObject->SetTimeStamp(curtime); + pObject->StartSound(); + } + else + { + pObject->SetTimeStamp(curtime); + + // compute the expected lifespan + pObject->SetLifeSpan(); + + // lets give the new active-to-be object an id + if (m_audiodevice->GetNewId(pObject)) + { + // and add the object + m_activeobjects.addTail(pObject); + pObject->StartSound(); + pObject->SetActive(true); + } + } + } + } +} + + + +void SND_Scene::RemoveActiveObject(SND_SoundObject* pObject) +{ + if (m_audio) + { + if (pObject) + { +#ifdef ONTKEVER + printf("SND_Scene::RemoveActiveObject\n"); +#endif + // if inactive, remove it from the list + if (pObject->IsActive()) + { + // first make sure it is stopped + m_audiodevice->ClearId(pObject); + } + } + } +} + + + +void SND_Scene::UpdateActiveObects() +{ +// ++tijd; + + SND_SoundObject* pObject; + // update only the objects that need to be updated + for (pObject = (SND_SoundObject*)m_activeobjects.getHead(); + !pObject->isTail(); + pObject = (SND_SoundObject*)pObject->getNext()) + { + int id = pObject->GetId(); + + if (id >= 0) + { + bool juststartedplaying = false; +#ifdef USE_FMOD + // fmod wants these set before playing the sample + if (pObject->IsModified()) + { + m_audiodevice->SetObjectLoop(id, pObject->GetLoopMode()); + m_audiodevice->SetObjectLoopPoints(id, pObject->GetLoopStart(), pObject->GetLoopEnd()); + } + + // ok, properties Set. now see if it must play + if (pObject->GetPlaystate() == SND_MUST_PLAY) + { + m_audiodevice->PlayObject(id); + pObject->SetPlaystate(SND_PLAYING); + pObject->InitRunning(); +// printf("start play: %d\n", tijd); + juststartedplaying = true; + } +#endif + if (pObject->Is3D()) + { + // Get the global positions and velocity vectors + // of the listener and soundobject + MT_Vector3 op = pObject->GetPosition(); + MT_Vector3 lp = m_listener.GetPosition(); + MT_Vector3 position = op - lp; + + // Calculate relative velocity in global coordinates + // of the sound with respect to the listener. + MT_Vector3 ov = pObject->GetVelocity(); + MT_Vector3 lv = m_listener.GetVelocity(); + MT_Vector3 velocity = ov - lv; + + // Now map the object position and velocity into + // the local coordinates of the listener. + MT_Matrix3x3 lo = m_listener.GetOrientation(); + + MT_Vector3 local_sound_pos = position * lo; + MT_Vector3 local_sound_vel = velocity * lo; + + m_audiodevice->SetObjectTransform( + id, + local_sound_pos, + local_sound_vel, + pObject->GetOrientation(), // make relative to listener! + lp, + pObject->GetRollOffFactor()); + } + else + { + m_audiodevice->ObjectIs2D(id); + } + + // update the situation + if (pObject->IsModified()) + { + m_audiodevice->SetObjectPitch(id, pObject->GetPitch()); + m_audiodevice->SetObjectGain(id, pObject->GetGain()); + m_audiodevice->SetObjectMinGain(id, pObject->GetMinGain()); + m_audiodevice->SetObjectMaxGain(id, pObject->GetMaxGain()); + m_audiodevice->SetObjectReferenceDistance(id, pObject->GetReferenceDistance()); + m_audiodevice->SetObjectRollOffFactor(id, pObject->GetRollOffFactor()); + m_audiodevice->SetObjectLoop(id, pObject->GetLoopMode()); + m_audiodevice->SetObjectLoopPoints(id, pObject->GetLoopStart(), pObject->GetLoopEnd()); + pObject->SetModified(false); + } + + pObject->AddRunning(); + +#ifdef ONTKEVER + STR_String naam = pObject->GetObjectName(); + STR_String sample = pObject->GetSampleName(); + + int id = pObject->GetId(); + int buffer = pObject->GetBuffer(); + + float gain = pObject->GetGain(); + float pitch = pObject->GetPitch(); + float timestamp = pObject->GetTimestamp(); + + printf("naam: %s, sample: %s \n", naam.Ptr(), sample.Ptr()); + printf("id: %d, buffer: %d \n", id, buffer); + printf("gain: %f, pitch: %f, ts: %f \n\n", gain, pitch, timestamp); +#endif +#ifdef USE_OPENAL + // ok, properties Set. now see if it must play + if (pObject->GetPlaystate() == SND_MUST_PLAY) + { + m_audiodevice->PlayObject(id); + pObject->SetPlaystate(SND_PLAYING); + //break; + } +#endif + + // check to see if the sound is still playing + // if not: release its id + int playstate = m_audiodevice->GetPlayState(id); +#ifdef ONTKEVER + if (playstate != 2) + printf("%d - ",playstate); +#endif + +// if ((playstate == SND_STOPPED && (!juststartedplaying) && !pObject->GetLoopMode() && pObject->IsRunning()) +#ifdef WIN32 + if ((playstate == SND_STOPPED) && !pObject->GetLoopMode()) +#else + if (!pObject->GetLoopMode()) +#endif + { +// printf("remove: %d\n", tijd); + RemoveActiveObject(pObject); + } + } + } +} + + + +void SND_Scene::UpdateCD() +{ + if (m_audiodevice) + { + SND_CDObject* pCD = SND_CDObject::Instance(); + + if (pCD) + { + int playstate = pCD->GetPlaystate(); + + switch (playstate) + { + case SND_MUST_PLAY: + { + // initialize the cd only when you need it + m_audiodevice->SetCDGain(pCD->GetGain()); + m_audiodevice->SetCDPlaymode(pCD->GetPlaymode()); + m_audiodevice->PlayCD(pCD->GetTrack()); + pCD->SetPlaystate(SND_PLAYING); + pCD->SetUsed(); + break; + } + case SND_MUST_PAUSE: + { + m_audiodevice->PauseCD(true); + pCD->SetPlaystate(SND_PAUSED); + break; + } + case SND_MUST_RESUME: + { + m_audiodevice->PauseCD(false); + pCD->SetPlaystate(SND_PLAYING); + break; + } + case SND_MUST_STOP: + { + m_audiodevice->StopCD(); + pCD->SetPlaystate(SND_STOPPED); + break; + } + default: + { + } + } + + // this one is only for realtime modifying settings + if (pCD->IsModified()) + { + m_audiodevice->SetCDGain(pCD->GetGain()); + pCD->SetModified(false); + } + } + } +} + + + +void SND_Scene::Proceed() +{ + if (m_audio && m_audioplayback) + { + m_audiodevice->MakeCurrent(); + + UpdateListener(); + UpdateActiveObects(); + UpdateCD(); + +// m_audiodevice->UpdateDevice(); + } +} + + +void SND_Scene::DeleteObject(SND_SoundObject* pObject) +{ +#ifdef ONTKEVER + printf("SND_Scene::DeleteObject\n"); +#endif + + if (pObject) + { + if (m_audiodevice) + m_audiodevice->ClearId(pObject); + + // must remove object from m_activeList + std::set<SND_SoundObject*>::iterator set_it; + set_it = m_soundobjects.find(pObject); + + if (set_it != m_soundobjects.end()) + m_soundobjects.erase(set_it); + + // release the memory + delete pObject; + pObject = NULL; + } +} + + + +void SND_Scene::RemoveAllObjects() +{ +#ifdef ONTKEVER + printf("SND_Scene::RemoveAllObjects\n"); +#endif + + StopAllObjects(); + + std::set<SND_SoundObject*>::iterator it = m_soundobjects.begin(); + + while (it != m_soundobjects.end()) + { + delete (*it); + it++; + } + + m_soundobjects.clear(); +} + + + +void SND_Scene::StopAllObjects() +{ + if (m_audio) + { +#ifdef ONTKEVER + printf("SND_Scene::StopAllObjects\n"); +#endif + + SND_SoundObject* pObject; + + for (pObject = (SND_SoundObject*)m_activeobjects.getHead(); + !pObject->isTail(); + pObject = (SND_SoundObject*)pObject->getNext()) + { + m_audiodevice->ClearId(pObject); + } + } +} + + + +SND_SoundListener* SND_Scene::GetListener() +{ + return &m_listener; +} diff --git a/intern/SoundSystem/intern/SND_SoundListener.cpp b/intern/SoundSystem/intern/SND_SoundListener.cpp new file mode 100644 index 00000000000..281da2aa7fb --- /dev/null +++ b/intern/SoundSystem/intern/SND_SoundListener.cpp @@ -0,0 +1,184 @@ +/* + * SND_SoundListener.cpp + * + * A SoundListener is for sound what a camera is for vision. + * + * $Id$ + * + * ***** BEGIN GPL/BL DUAL LICENSE BLOCK ***** + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. The Blender + * Foundation also sells licenses for use in proprietary software under + * the Blender License. See http://www.blender.org/BL/ for information + * about this. + * + * This program 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 this program; if not, write to the Free Software Foundation, + * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV. + * All rights reserved. + * + * The Original Code is: all of this file. + * + * Contributor(s): none yet. + * + * ***** END GPL/BL DUAL LICENSE BLOCK ***** + */ + +#include "SND_SoundListener.h" + +SND_SoundListener::SND_SoundListener() +{ + m_modified = true; + m_gain = 1.0; + m_dopplerfactor = 1.0; + m_dopplervelocity = 1.0; + m_scale = 1.0; + m_position[0] = 0.0; + m_position[1] = 0.0; + m_position[2] = 0.0; + m_velocity[0] = 0.0; + m_velocity[1] = 0.0; + m_velocity[2] = 0.0; + m_orientation[0][0] = 1.0; + m_orientation[0][1] = 0.0; + m_orientation[0][2] = 0.0; + m_orientation[1][0] = 0.0; + m_orientation[1][1] = 1.0; + m_orientation[1][2] = 0.0; + m_orientation[2][0] = 0.0; + m_orientation[2][1] = 0.0; + m_orientation[2][2] = 1.0; +} + + +SND_SoundListener::~SND_SoundListener() +{ + ; /* intentionally empty */ + +} + + + +void SND_SoundListener::SetGain(MT_Scalar gain) +{ + m_gain = gain; + m_modified = true; +} + + + +void SND_SoundListener::SetPosition (const MT_Vector3& pos) +{ + m_position = pos; +} + + + +void SND_SoundListener::SetVelocity(const MT_Vector3& vel) +{ + m_velocity = vel; +} + + + +void SND_SoundListener::SetOrientation(const MT_Matrix3x3& ori) +{ + m_orientation = ori; +} + + + +void SND_SoundListener::SetDopplerFactor(MT_Scalar dopplerfactor) +{ + m_dopplerfactor = dopplerfactor; + m_modified = true; +} + + + +void SND_SoundListener::SetDopplerVelocity(MT_Scalar dopplervelocity) +{ + m_dopplervelocity = dopplervelocity; + m_modified = true; +} + + + +void SND_SoundListener::SetScale(MT_Scalar scale) +{ + m_scale = scale; + m_modified = true; +} + + + +MT_Scalar SND_SoundListener::GetGain() const +{ + return m_gain; +} + + + +MT_Vector3 SND_SoundListener::GetPosition() const +{ + return m_position; +} + + + +MT_Vector3 SND_SoundListener::GetVelocity() const +{ + return m_velocity; +} + + + +MT_Matrix3x3 SND_SoundListener::GetOrientation() +{ + return m_orientation; +} + + + +MT_Scalar SND_SoundListener::GetDopplerFactor() const +{ + return m_dopplerfactor; +} + + + +MT_Scalar SND_SoundListener::GetDopplerVelocity() const +{ + return m_dopplervelocity; +} + + + +MT_Scalar SND_SoundListener::GetScale() const +{ + return m_scale; +} + + + +bool SND_SoundListener::IsModified() const +{ + return m_modified; +} + + + +void SND_SoundListener::SetModified(bool modified) +{ + m_modified = modified; +} diff --git a/intern/SoundSystem/intern/SND_SoundObject.cpp b/intern/SoundSystem/intern/SND_SoundObject.cpp new file mode 100644 index 00000000000..69066d0a2dd --- /dev/null +++ b/intern/SoundSystem/intern/SND_SoundObject.cpp @@ -0,0 +1,506 @@ +/* + * SND_SoundObject.cpp + * + * Implementation of the abstract sound object + * + * $Id$ + * + * ***** BEGIN GPL/BL DUAL LICENSE BLOCK ***** + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. The Blender + * Foundation also sells licenses for use in proprietary software under + * the Blender License. See http://www.blender.org/BL/ for information + * about this. + * + * This program 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 this program; if not, write to the Free Software Foundation, + * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV. + * All rights reserved. + * + * The Original Code is: all of this file. + * + * Contributor(s): none yet. + * + * ***** END GPL/BL DUAL LICENSE BLOCK ***** + */ + +#include "SND_SoundObject.h" + + + +SND_SoundObject::SND_SoundObject()// : m_modified(true) +{ + m_samplename = ""; + m_length = 0; + m_buffer = 0; + + m_gain = 0.0; + m_pitch = 1.0; + + m_mingain = 0.0; + m_maxgain = 1.0; + m_rollofffactor = 1.0; + m_referencedistance = 1.0; + + m_position[0] = 0.0; + m_position[1] = 0.0; + m_position[2] = 0.0; + m_velocity[0] = 0.0; + m_velocity[1] = 0.0; + m_velocity[2] = 0.0; + m_orientation[0][0] = 1.0; + m_orientation[0][1] = 0.0; + m_orientation[0][2] = 0.0; + m_orientation[1][0] = 0.0; + m_orientation[1][1] = 1.0; + m_orientation[1][2] = 0.0; + m_orientation[2][0] = 0.0; + m_orientation[2][1] = 0.0; + m_orientation[2][2] = 1.0; + + m_loopstart = 0; + m_loopend = 0; + m_loopmode = SND_LOOP_NORMAL; + m_is3d = true; + m_playstate = SND_INITIAL; + m_active = false; + m_id = -1; + m_lifespan = 0; + m_timestamp = 0; + m_modified = true; + m_running = 0; + m_highpriority = false; +} + + + +SND_SoundObject::~SND_SoundObject() +{ +} + + + +void SND_SoundObject::StartSound() +{ + m_playstate = SND_MUST_PLAY; +} + + + +void SND_SoundObject::StopSound() +{ + m_playstate = SND_MUST_STOP; +} + + + +void SND_SoundObject::PauseSound() +{ + m_playstate = SND_MUST_PAUSE; +} + + + +void SND_SoundObject::DeleteWhenFinished() +{ + m_playstate = SND_MUST_BE_DELETED; +} + + + +void SND_SoundObject::SetGain(MT_Scalar gain) +{ + m_gain = gain; + m_modified = true; +} + + + +void SND_SoundObject::SetMinGain(MT_Scalar mingain) +{ + m_mingain = mingain; + m_modified = true; +} + + + +void SND_SoundObject::SetMaxGain(MT_Scalar maxgain) +{ + m_maxgain = maxgain; + m_modified = true; +} + + + +void SND_SoundObject::SetRollOffFactor(MT_Scalar rollofffactor) +{ + m_rollofffactor = rollofffactor; + m_modified = true; +} + + + +void SND_SoundObject::SetReferenceDistance(MT_Scalar referencedistance) +{ + m_referencedistance = referencedistance; + m_modified = true; +} + + + +void SND_SoundObject::SetPitch(MT_Scalar pitch) +{ + m_pitch = pitch; + m_modified = true; +} + + + +void SND_SoundObject::SetLoopMode(unsigned int loopmode) +{ + m_loopmode = loopmode; + m_modified = true; +} + + + +void SND_SoundObject::SetLoopStart(unsigned int loopstart) +{ + m_loopstart = loopstart; + m_modified = true; +} + + + +void SND_SoundObject::SetLoopEnd(unsigned int loopend) +{ + m_loopend = loopend; + m_modified = true; +} + + + +void SND_SoundObject::Set3D(bool threedee) +{ + m_is3d = threedee; +} + + + +void SND_SoundObject::SetLifeSpan() +{ + m_lifespan = m_length / m_pitch; +} + + + +bool SND_SoundObject::IsLifeSpanOver(MT_Scalar curtime) const +{ + bool result = false; + + if ((curtime - m_timestamp) > m_lifespan) + result = true; + + return result; +} + + + +void SND_SoundObject::SetActive(bool active) +{ + m_active = active; + + if (!active) + { + m_playstate = SND_STOPPED; + (this)->remove(); + } +} + + + +void SND_SoundObject::SetBuffer(unsigned int buffer) +{ + m_buffer = buffer; +} + + + +void SND_SoundObject::SetObjectName(STR_String objectname) +{ + m_objectname = objectname; +} + + + +void SND_SoundObject::SetSampleName(STR_String samplename) +{ + m_samplename = samplename; +} + + + +void SND_SoundObject::SetLength(MT_Scalar length) +{ + m_length = length; +} + + + +void SND_SoundObject::SetPosition(const MT_Vector3& pos) +{ + m_position = pos; +} + + + +void SND_SoundObject::SetVelocity(const MT_Vector3& vel) +{ + m_velocity = vel; +} + + + +void SND_SoundObject::SetOrientation(const MT_Matrix3x3& orient) +{ + m_orientation = orient; +} + + + +void SND_SoundObject::SetPlaystate(int playstate) +{ + m_playstate = playstate; +} + + + +void SND_SoundObject::SetId(int id) +{ + m_id = id; +} + + + +void SND_SoundObject::SetTimeStamp(MT_Scalar timestamp) +{ + m_timestamp = timestamp; +} + + + +void SND_SoundObject::SetHighPriority(bool priority) +{ + m_highpriority = priority; +} + + + +bool SND_SoundObject::IsHighPriority() const +{ + return m_highpriority; +} + + + +bool SND_SoundObject::IsActive()const +{ + return m_active; +} + + + +int SND_SoundObject::GetId()const +{ + return m_id; +} + + + +MT_Scalar SND_SoundObject::GetLifeSpan()const +{ + return m_lifespan; +} + + + +MT_Scalar SND_SoundObject::GetTimestamp()const +{ + return m_timestamp; +} + + + +unsigned int SND_SoundObject::GetBuffer() +{ + return m_buffer; +} + + + +const STR_String& SND_SoundObject::GetSampleName() +{ + return m_samplename; +} + + + +const STR_String& SND_SoundObject::GetObjectName() +{ + return m_objectname; +} + + + +MT_Scalar SND_SoundObject::GetLength() const +{ + return m_length; +} + + + +MT_Scalar SND_SoundObject::GetGain() const +{ + return m_gain; +} + + + +MT_Scalar SND_SoundObject::GetPitch() const +{ + return m_pitch; +} + + + +MT_Scalar SND_SoundObject::GetMinGain() const +{ + return m_mingain; +} + + + +MT_Scalar SND_SoundObject::GetMaxGain() const +{ + return m_maxgain; +} + + + +MT_Scalar SND_SoundObject::GetRollOffFactor() const +{ + return m_rollofffactor; +} + + + +MT_Scalar SND_SoundObject::GetReferenceDistance() const +{ + return m_referencedistance; +} + + + +MT_Vector3 SND_SoundObject::GetPosition() const +{ + return m_position; +} + + + +MT_Vector3 SND_SoundObject::GetVelocity() const +{ + return m_velocity; +} + + + +MT_Matrix3x3 SND_SoundObject::GetOrientation() const +{ + return m_orientation; +} + + + +unsigned int SND_SoundObject::GetLoopMode() const +{ + return m_loopmode; +} + + + +unsigned int SND_SoundObject::GetLoopStart() const +{ + return m_loopstart; +} + + + +unsigned int SND_SoundObject::GetLoopEnd() const +{ + return m_loopend; +} + + + +bool SND_SoundObject::Is3D() const +{ + return m_is3d; +} + + + +int SND_SoundObject::GetPlaystate() const +{ + return m_playstate; +} + + + +bool SND_SoundObject::IsModified() const +{ + return m_modified; +} + + + +void SND_SoundObject::SetModified(bool modified) +{ + m_modified = modified; +} + + + +void SND_SoundObject::InitRunning() +{ + m_running = 0; +} + + + +bool SND_SoundObject::IsRunning() const +{ + bool result = false; + + if (m_running > 100) + result = true; + + return result; +} + + + +void SND_SoundObject::AddRunning() +{ + ++m_running; +} diff --git a/intern/SoundSystem/intern/SND_Utils.cpp b/intern/SoundSystem/intern/SND_Utils.cpp new file mode 100644 index 00000000000..3965c534cd3 --- /dev/null +++ b/intern/SoundSystem/intern/SND_Utils.cpp @@ -0,0 +1,395 @@ +/* + * SND_Utils.cpp + * + * Util functions for soundthingies + * + * $Id$ + * + * ***** BEGIN GPL/BL DUAL LICENSE BLOCK ***** + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. The Blender + * Foundation also sells licenses for use in proprietary software under + * the Blender License. See http://www.blender.org/BL/ for information + * about this. + * + * This program 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 this program; if not, write to the Free Software Foundation, + * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV. + * All rights reserved. + * + * The Original Code is: all of this file. + * + * Contributor(s): none yet. + * + * ***** END GPL/BL DUAL LICENSE BLOCK ***** + */ + +#include "SND_Utils.h" +#include "SoundDefines.h" +#include "SND_DependKludge.h" +/* +extern "C" { +#include "license_key.h" +} +*/ +#include <stdio.h> +#include <stdlib.h> +#include <fcntl.h> +#include <math.h> +#include <string.h> + +#if defined(WIN32) +#include <io.h> +#else +#include <unistd.h> +#endif + +//extern int LICENSE_KEY_VALID; +#define LICENSE_KEY_VALID true + +#define BUFFERSIZE 32 + +/* loads a file */ +void* SND_LoadSample(char *filename) +{ + int file, filelen, buffersize = BUFFERSIZE; + void* data = NULL; + +#if defined(WIN32) + file = open(filename, O_BINARY|O_RDONLY); +#else + file = open(filename, 0|O_RDONLY); +#endif + + if (file == -1) + { + //printf("can't open file.\n"); + //printf("press q for quit.\n"); + } + else + { + filelen = lseek(file, 0, SEEK_END); + lseek(file, 0, SEEK_SET); + + if (filelen != 0) + { + data = malloc(buffersize); + + if (read(file, data, buffersize) != buffersize) + { + free(data); + data = NULL; + } + } + close(file); + + } + return (data); +} + + + +bool SND_IsSampleValid(const STR_String& name, void* memlocation) +{ + bool result = false; + bool loadedsample = false; + char buffer[BUFFERSIZE]; + + if (!memlocation) + { + STR_String samplename = name; + memlocation = SND_LoadSample(samplename.Ptr()); + + if (memlocation) + loadedsample = true; + } + + if (memlocation) + { + memcpy(&buffer, memlocation, BUFFERSIZE); + + if(!(memcmp(buffer, "RIFF", 4) && memcmp(&(buffer[8]), "WAVEfmt ", 8))) + { + int shortbuf = * ((short *) &buffer[20]); + if (shortbuf == SND_WAVE_FORMAT_PCM) + result = true; + + /* only fmod supports compressed wav */ +#ifdef USE_FMOD + /* and only valid publishers may use compressed wav */ + if (LICENSE_KEY_VALID) + { + switch (shortbuf) + { + case SND_WAVE_FORMAT_ADPCM: + case SND_WAVE_FORMAT_ALAW: + case SND_WAVE_FORMAT_MULAW: + case SND_WAVE_FORMAT_DIALOGIC_OKI_ADPCM: + case SND_WAVE_FORMAT_CONTROL_RES_VQLPC: + case SND_WAVE_FORMAT_GSM_610: + case SND_WAVE_FORMAT_MPEG3: + result = true; + break; + default: + { + break; + } + } + } +#endif + } +#ifdef USE_FMOD + /* only valid publishers may use ogg vorbis */ + else if (!memcmp(buffer, "OggS", 4) && LICENSE_KEY_VALID) + { + result = true; + } + /* only valid publishers may use mp3 */ + else if (((!memcmp(buffer, "ID3", 3)) || (!memcmp(buffer, "ÿû", 2))) && LICENSE_KEY_VALID) + { + result = true; + } +#endif + } + if (loadedsample) + { + free(memlocation); + memlocation = NULL; + } + + return result; +} + + + +/* checks if the passed pointer is a valid sample */ +bool CheckSample(void* sample) +{ + bool valid = false; + char buffer[32]; + + memcpy(buffer, sample, 16); + + if(!(memcmp(buffer, "RIFF", 4) && memcmp(&(buffer[8]), "WAVEfmt ", 8))) + { + valid = true; + } + + return valid; +} + + + +/* gets the type of the sample (0 == unknown, 1 == PCM etc */ +unsigned int SND_GetSampleFormat(void* sample) +{ + short sampletype = 0; + + if (CheckSample(sample)) + { + memcpy(&sampletype, ((char*)sample) + 20, 2); + } + + return (unsigned int)sampletype; +} + + + +/* gets the number of channels in a sample */ +unsigned int SND_GetNumberOfChannels(void* sample) +{ + short numberofchannels = 0; + + if (CheckSample(sample)) + { + memcpy(&numberofchannels, ((char*)sample) + 22, 2); + } + + return (unsigned int)numberofchannels; +} + + + +/* gets the samplerate of a sample */ +unsigned int SND_GetSampleRate(void* sample) +{ + unsigned int samplerate = 0; + + if (CheckSample(sample)) + { + memcpy(&samplerate, ((char*)sample) + 24, 4); + } + + return samplerate; +} + + + +/* gets the bitrate of a sample */ +unsigned int SND_GetBitRate(void* sample) +{ + short bitrate = 0; + + if (CheckSample(sample)) + { + memcpy(&bitrate, ((char*)sample) + 34, 2); + } + + return (unsigned int)bitrate; +} + + + +/* gets the length of the actual sample data (without the header) */ +unsigned int SND_GetNumberOfSamples(void* sample) +{ + unsigned int chunklength, length = 0, offset = 16; + char data[4]; + + if (CheckSample(sample)) + { + memcpy(&chunklength, ((char*)sample) + offset, 4); + offset = offset + chunklength + 4; + memcpy(data, ((char*)sample) + offset, 4); + + // lets find "data" + while (memcmp(data, "data", 4)) + { + offset += 4; + memcpy(data, ((char*)sample) + offset, 4); + } + offset += 4; + memcpy(&length, ((char*)sample) + offset, 4); + } + + return length; +} + + + +/* gets the size of the entire header (file - sampledata) */ +unsigned int SND_GetHeaderSize(void* sample) +{ + unsigned int chunklength, headersize = 0, offset = 16; + char data[4]; + + if (CheckSample(sample)) + { + memcpy(&chunklength, ((char*)sample) + offset, 4); + offset = offset + chunklength + 4; + memcpy(data, ((char*)sample) + offset, 4); + + // lets find "data" + while (memcmp(data, "data", 4)) + { + offset += 4; + memcpy(data, ((char*)sample) + offset, 4); + } + headersize = offset + 8; + } + + + return headersize; +} + + + +unsigned int SND_GetExtraChunk(void* sample) +{ + unsigned int extrachunk = 0, chunklength, offset = 16; + char data[4]; + + if (CheckSample(sample)) + { + memcpy(&chunklength, ((char*)sample) + offset, 4); + offset = offset + chunklength + 4; + memcpy(data, ((char*)sample) + offset, 4); + + // lets find "cue" + while (memcmp(data, "cue", 3)) + { + offset += 4; + memcpy(data, ((char*)sample) + offset, 4); + } + } + + return extrachunk; +} + + + +void SND_GetSampleInfo(signed char* sample, SND_WaveSlot* waveslot) +{ + WavFileHeader fileheader; + WavFmtHeader fmtheader; + WavFmtExHeader fmtexheader; + WavSampleHeader sampleheader; + WavChunkHeader chunkheader; + + if (CheckSample(sample)) + { + memcpy(&fileheader, sample, sizeof(WavFileHeader)); + sample += sizeof(WavFileHeader); + fileheader.size = ((fileheader.size+1) & ~1) - 4; + + while ((fileheader.size != 0) && (memcpy(&chunkheader, sample, sizeof(WavChunkHeader)))) + { + sample += sizeof(WavChunkHeader); + if (!memcmp(chunkheader.id, "fmt ", 4)) + { + memcpy(&fmtheader, sample, sizeof(WavFmtHeader)); + waveslot->SetSampleFormat(fmtheader.format); + + if (fmtheader.format == 0x0001) + { + waveslot->SetNumberOfChannels(fmtheader.numberofchannels); + waveslot->SetBitRate(fmtheader.bitrate); + waveslot->SetSampleRate(fmtheader.samplerate); + sample += chunkheader.size; + } + else + { + memcpy(&fmtexheader, sample, sizeof(WavFmtExHeader)); + sample += chunkheader.size; + } + } + else if (!memcmp(chunkheader.id, "data", 4)) + { + if (fmtheader.format == 0x0001) + { + waveslot->SetNumberOfSamples(chunkheader.size); + sample += chunkheader.size; + } + else if (fmtheader.format == 0x0011) + { + //IMA ADPCM + } + else if (fmtheader.format == 0x0055) + { + //MP3 WAVE + } + } + else if (!memcmp(chunkheader.id, "smpl", 4)) + { + memcpy(&sampleheader, sample, sizeof(WavSampleHeader)); + //loop = sampleheader.loops; + sample += chunkheader.size; + } + else + sample += chunkheader.size; + + sample += chunkheader.size & 1; + fileheader.size -= (((chunkheader.size + 1) & ~1) + 8); + } + } +} diff --git a/intern/SoundSystem/intern/SND_WaveCache.cpp b/intern/SoundSystem/intern/SND_WaveCache.cpp new file mode 100644 index 00000000000..279bc29aa83 --- /dev/null +++ b/intern/SoundSystem/intern/SND_WaveCache.cpp @@ -0,0 +1,134 @@ +/* + * SND_WaveCache.cpp + * + * abstract wavecache, a way to organize samples + * + * $Id$ + * + * ***** BEGIN GPL/BL DUAL LICENSE BLOCK ***** + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. The Blender + * Foundation also sells licenses for use in proprietary software under + * the Blender License. See http://www.blender.org/BL/ for information + * about this. + * + * This program 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 this program; if not, write to the Free Software Foundation, + * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV. + * All rights reserved. + * + * The Original Code is: all of this file. + * + * Contributor(s): none yet. + * + * ***** END GPL/BL DUAL LICENSE BLOCK ***** + */ + +#ifdef WIN32 +#pragma warning (disable:4786) // Get rid of stupid stl-visual compiler debug warning +#endif //WIN32 + +#include "SND_WaveCache.h" +#include <stdio.h> + +#ifndef __APPLE__ +#include <malloc.h> +#else // __APPLE__ +#include <sys/malloc.h> +#endif // __APPLE__ + + +SND_WaveCache::SND_WaveCache() +{ + // do the buffer administration + for (int i = 0; i < NUM_BUFFERS; i++) + m_bufferList[i] = NULL; +} + + + +SND_WaveCache::~SND_WaveCache() +{ + // clean up the mess + FreeSamples(); + RemoveAllSamples(); +} + + + +SND_WaveSlot* SND_WaveCache::GetWaveSlot(const STR_String& samplename) +{ + SND_WaveSlot* waveslot = NULL; + + std::map<STR_String, SND_WaveSlot*>::iterator find_result = m_samplecache.find(samplename); + + // let's see if we have already loaded this sample + if (find_result != m_samplecache.end()) + { + waveslot = (*find_result).second; + } + else + { + // so the sample wasn't loaded, so do it here + for (int bufnum = 0; bufnum < NUM_BUFFERS; bufnum++) + { + // find an empty buffer + if (m_bufferList[bufnum] == NULL) + { + waveslot = new SND_WaveSlot(); + waveslot->SetSampleName(samplename); + waveslot->SetBuffer(bufnum); + m_bufferList[bufnum] = waveslot; + break; + } + } + m_samplecache.insert(std::pair<STR_String, SND_WaveSlot*>(samplename, waveslot)); + } + + return waveslot; +} + + + +void SND_WaveCache::RemoveAllSamples() +{ + // remove all samples + m_samplecache.clear(); + + // reset the list of buffers + for (int i = 0; i < NUM_BUFFERS; i++) + m_bufferList[i] = NULL; +} + + + +void SND_WaveCache::RemoveSample(const STR_String& samplename, int buffer) +{ + m_samplecache.erase(samplename); + m_bufferList[buffer] = NULL; +} + + + +void SND_WaveCache::FreeSamples() +{ + // iterate through the bufferlist and delete the waveslot if present + for (int i = 0; i < NUM_BUFFERS; i++) + { + if (m_bufferList[i]) + { + delete m_bufferList[i]; + m_bufferList[i] = NULL; + } + } +} diff --git a/intern/SoundSystem/intern/SND_WaveSlot.cpp b/intern/SoundSystem/intern/SND_WaveSlot.cpp new file mode 100644 index 00000000000..51356bba4bf --- /dev/null +++ b/intern/SoundSystem/intern/SND_WaveSlot.cpp @@ -0,0 +1,182 @@ +/** + * $Id$ + * ***** BEGIN GPL/BL DUAL LICENSE BLOCK ***** + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. The Blender + * Foundation also sells licenses for use in proprietary software under + * the Blender License. See http://www.blender.org/BL/ for information + * about this. + * + * This program 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 this program; if not, write to the Free Software Foundation, + * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV. + * All rights reserved. + * + * The Original Code is: all of this file. + * + * Contributor(s): none yet. + * + * ***** END GPL/BL DUAL LICENSE BLOCK ***** + */ + + +#include "SND_WaveSlot.h" + + + +SND_WaveSlot::~SND_WaveSlot() +{ +#ifdef ONTKEVER + printf("neeeeeee...\n"); +#endif +} + + + +void SND_WaveSlot::SetSampleName(STR_String samplename) +{ + m_samplename = samplename; +} + + + +void SND_WaveSlot::SetLoaded(bool loaded) +{ + m_loaded = loaded; +} + + + +void SND_WaveSlot::SetData(void* data) +{ + m_data = data; +} + + + +void SND_WaveSlot::SetBuffer(unsigned int buffer) +{ + m_buffer = buffer; +} + + + +void SND_WaveSlot::SetSampleFormat(unsigned int sampleformat) +{ + m_sampleformat = sampleformat; +} + + + +void SND_WaveSlot::SetNumberOfChannels(unsigned int numberofchannels) +{ + m_numberofchannels = numberofchannels; +} + + + +void SND_WaveSlot::SetSampleRate(unsigned int samplerate) +{ + m_samplerate = samplerate; +} + + + +void SND_WaveSlot::SetBitRate(unsigned int bitrate) +{ + m_bitrate = bitrate; +} + + + +void SND_WaveSlot::SetNumberOfSamples(unsigned int numberofsamples) +{ + m_numberofsamples = numberofsamples; +} + + + +void SND_WaveSlot::SetFileSize(unsigned int filesize) +{ + m_filesize = filesize; +} + + + +const STR_String& SND_WaveSlot::GetSampleName() +{ + return m_samplename; +} + + + +bool SND_WaveSlot::IsLoaded() const +{ + return m_loaded; +} + + + +void* SND_WaveSlot::GetData() +{ + return m_data; +} + + + +unsigned int SND_WaveSlot::GetBuffer() const +{ + return m_buffer; +} + + + +unsigned int SND_WaveSlot::GetSampleFormat() const +{ + return m_sampleformat; +} + + + +unsigned int SND_WaveSlot::GetNumberOfChannels() const +{ + return m_numberofchannels; +} + + + +unsigned int SND_WaveSlot::GetSampleRate() const +{ + return m_samplerate; +} + + + +unsigned int SND_WaveSlot::GetBitRate() const +{ + return m_bitrate; +} + + + +unsigned int SND_WaveSlot::GetNumberOfSamples() const +{ + return m_numberofsamples; +} + + + +unsigned int SND_WaveSlot::GetFileSize() const +{ + return m_filesize; +} |