2012-05-29 13:31:11 +00:00
|
|
|
// NeL - MMORPG Framework <http://dev.ryzom.com/projects/nel/>
|
|
|
|
// Copyright (C) 2010 Winch Gate Property Limited
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as
|
|
|
|
// published by the Free Software Foundation, either version 3 of the
|
|
|
|
// License, or (at your option) any later version.
|
|
|
|
//
|
|
|
|
// 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 Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
#include "stdsound.h"
|
|
|
|
|
|
|
|
#include "nel/sound/sound_bank.h"
|
|
|
|
#include "nel/sound/simple_sound.h"
|
|
|
|
#include "nel/sound/complex_sound.h"
|
|
|
|
#include "nel/sound/context_sound.h"
|
|
|
|
#include "nel/sound/background_sound.h"
|
|
|
|
#include "nel/sound/music_sound.h"
|
|
|
|
#include "nel/sound/stream_sound.h"
|
|
|
|
#include "nel/sound/stream_file_sound.h"
|
|
|
|
|
|
|
|
#include "nel/georges/u_form_loader.h"
|
|
|
|
#include "nel/georges/u_form_elm.h"
|
|
|
|
#include "nel/georges/u_form.h"
|
|
|
|
#include "nel/misc/path.h"
|
|
|
|
#include "nel/sound/driver/buffer.h"
|
|
|
|
|
|
|
|
#include "nel/georges/load_form.h"
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
using namespace NLMISC;
|
|
|
|
using namespace NLGEORGES;
|
|
|
|
|
|
|
|
|
|
|
|
namespace NLSOUND {
|
|
|
|
|
|
|
|
void CSoundBank::bufferUnloaded(const NLMISC::TStringId &bufferName)
|
|
|
|
{
|
|
|
|
TBufferAssocContainer::iterator it(_BufferAssoc.find(bufferName));
|
|
|
|
|
|
|
|
if (it != _BufferAssoc.end())
|
|
|
|
{
|
|
|
|
// ok, found some sound associated with this buffer.
|
|
|
|
// update all sounds.
|
|
|
|
TSimpleSoundContainer::iterator first(it->second.begin()), last(it->second.end());
|
|
|
|
for (; first != last; ++first)
|
|
|
|
{
|
|
|
|
// remove the associated buffer.
|
|
|
|
CSimpleSound *ss = const_cast<CSimpleSound*>(*(first));
|
|
|
|
ss->setBuffer(NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//void CSoundBank::bufferLoaded(const std::string bufferName, IBuffer *buffer)
|
|
|
|
void CSoundBank::bufferLoaded(const NLMISC::TStringId &/* bufferName */, IBuffer *buffer)
|
|
|
|
{
|
|
|
|
// std::map<std::string, std::vector<TBufferAssoc> >::iterator it(_BufferAssoc.find(buffer->getName()));
|
|
|
|
TBufferAssocContainer::iterator it(_BufferAssoc.find(buffer->getName()));
|
|
|
|
|
|
|
|
if (it != _BufferAssoc.end())
|
|
|
|
{
|
|
|
|
// ok, found some sound associated with this buffer.
|
|
|
|
// update all sounds.
|
|
|
|
TSimpleSoundContainer::iterator first(it->second.begin()), last(it->second.end());
|
|
|
|
for (; first != last; ++first)
|
|
|
|
{
|
|
|
|
CSimpleSound *ss = const_cast<CSimpleSound*>(*(it->second.begin()));
|
|
|
|
// restore the associated buffer.
|
|
|
|
ss->setBuffer(buffer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void CSoundBank::registerBufferAssoc(CSimpleSound *sound, IBuffer *buffer)
|
|
|
|
{
|
|
|
|
if (buffer != NULL)
|
|
|
|
{
|
|
|
|
const NLMISC::TStringId &bufferName = buffer->getName();
|
|
|
|
_BufferAssoc[bufferName].insert(sound);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CSoundBank::unregisterBufferAssoc(CSimpleSound *sound, IBuffer * buffer)
|
|
|
|
{
|
|
|
|
if (buffer != NULL)
|
|
|
|
{
|
|
|
|
const TStringId &bufferName = buffer->getName();
|
|
|
|
TBufferAssocContainer::iterator it(_BufferAssoc.find(bufferName));
|
|
|
|
|
|
|
|
if (it != _BufferAssoc.end())
|
|
|
|
{
|
|
|
|
TSimpleSoundContainer::iterator it2(it->second.find(sound));
|
|
|
|
|
|
|
|
nlassert(it2 != it->second.end());
|
|
|
|
it->second.erase(it2);
|
|
|
|
|
|
|
|
if (it->second.empty())
|
|
|
|
{
|
|
|
|
// last sound refenrecing this buffer
|
|
|
|
_BufferAssoc.erase(it);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Destructor
|
|
|
|
*/
|
|
|
|
CSoundBank::~CSoundBank()
|
|
|
|
{
|
|
|
|
unload();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CSoundBank::addSound(CSound *sound)
|
|
|
|
{
|
2012-06-02 14:06:10 +00:00
|
|
|
// nlassert(_Sounds.size() > sound->getName().getShortId());
|
2012-06-03 14:00:43 +00:00
|
|
|
// nldebug("SOUNDBANK: Add %s", sound->getName().toString().c_str());
|
2012-06-02 14:06:10 +00:00
|
|
|
if (_Sounds.size() <= sound->getName().getShortId())
|
|
|
|
_Sounds.resize(sound->getName().getShortId() + 1);
|
2012-06-02 12:54:39 +00:00
|
|
|
_Sounds[sound->getName().getShortId()] = sound;
|
2012-05-29 13:31:11 +00:00
|
|
|
}
|
|
|
|
|
2012-06-02 12:54:39 +00:00
|
|
|
void CSoundBank::removeSound(const NLMISC::CSheetId &sheetId)
|
2012-05-29 13:31:11 +00:00
|
|
|
{
|
2012-06-02 12:54:39 +00:00
|
|
|
_Sounds[sheetId.getShortId()] = NULL;
|
2012-05-29 13:31:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** Pseudo serializer for packed sheet loading/saving.
|
|
|
|
* This class act as a wrapper to create sound from
|
|
|
|
* xml document being read, call the serialisation method
|
|
|
|
* to read/write packed sheet and to delete sounds that are
|
|
|
|
* no more needed.
|
|
|
|
*/
|
|
|
|
class CSoundSerializer
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
/// The sound beeing managed by this serializer.
|
|
|
|
CSound *Sound;
|
|
|
|
|
|
|
|
/// Default constructor.
|
|
|
|
CSoundSerializer()
|
|
|
|
: Sound(0)
|
|
|
|
{}
|
|
|
|
|
|
|
|
// load the values using the george sheet (called by GEORGE::loadForm)
|
2012-06-02 16:12:38 +00:00
|
|
|
void readGeorges (const NLMISC::CSmartPtr<NLGEORGES::UForm> &form, const std::string &name)
|
2012-05-29 13:31:11 +00:00
|
|
|
{
|
|
|
|
// just call the sound creation method with the xml form.
|
2012-06-02 12:54:39 +00:00
|
|
|
Sound = CSound::createSound(name, form->getRootNode());
|
2012-05-29 13:31:11 +00:00
|
|
|
|
|
|
|
// success ?
|
|
|
|
// if (_Sound != 0)
|
|
|
|
// CSoundBank::instance()->addSound(_Sound);
|
|
|
|
}
|
|
|
|
|
|
|
|
// load/save the values using the serial system (called by GEORGE::loadForm)
|
|
|
|
void serial (NLMISC::IStream &s)
|
|
|
|
{
|
|
|
|
if (s.isReading())
|
|
|
|
{
|
|
|
|
// read the first item to find the type
|
|
|
|
CSound::TSOUND_TYPE type = CSound::SOUND_SIMPLE;
|
|
|
|
s.serialEnum(type);
|
|
|
|
// read the sound name
|
|
|
|
// std::string name;
|
|
|
|
// s.serial(name);
|
|
|
|
|
|
|
|
// Instantiate the corresponding sound.
|
|
|
|
switch(CSound::TSOUND_TYPE(type))
|
|
|
|
{
|
|
|
|
case CSound::SOUND_SIMPLE:
|
|
|
|
Sound = new CSimpleSound();
|
|
|
|
break;
|
|
|
|
case CSound::SOUND_COMPLEX:
|
|
|
|
Sound = new CComplexSound();
|
|
|
|
break;
|
|
|
|
case CSound::SOUND_CONTEXT:
|
|
|
|
Sound = new CContextSound();
|
|
|
|
break;
|
|
|
|
case CSound::SOUND_BACKGROUND:
|
|
|
|
Sound = new CBackgroundSound();
|
|
|
|
break;
|
|
|
|
case CSound::SOUND_MUSIC:
|
|
|
|
Sound = new CMusicSound();
|
|
|
|
break;
|
|
|
|
case CSound::SOUND_STREAM:
|
|
|
|
Sound = new CStreamSound();
|
|
|
|
break;
|
|
|
|
case CSound::SOUND_STREAM_FILE:
|
|
|
|
Sound = new CStreamFileSound();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
Sound = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// nlassert(_Sound != 0);
|
|
|
|
if (Sound)
|
|
|
|
{
|
|
|
|
// read the sound data
|
|
|
|
Sound->serial(s);
|
|
|
|
// CSoundBank::instance()->addSound(_Sound);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (Sound == 0)
|
|
|
|
{
|
|
|
|
// the sound doesn't exist
|
|
|
|
uint32 i = std::numeric_limits<uint32>::max();
|
|
|
|
s.serialEnum(i);
|
|
|
|
// s.serial(std::string("bad sound"));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// write the sound type.
|
|
|
|
CSound::TSOUND_TYPE type = Sound->getSoundType();
|
|
|
|
s.serialEnum(type);
|
|
|
|
// write the sound name
|
|
|
|
// s.serial(const_cast<std::string&>(_Sound->getName()));
|
|
|
|
|
|
|
|
// and write the sound data
|
|
|
|
Sound->serial(s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** called by GEORGE::loadForm when a sheet read from the packed sheet is no more in
|
|
|
|
* the directories.
|
|
|
|
*/
|
|
|
|
void removed()
|
|
|
|
{
|
|
|
|
if (Sound != 0)
|
|
|
|
{
|
|
|
|
// we remove the sound from the bank and delete it.
|
|
|
|
// CSoundBank::instance()->removeSound(_Sound->getName());
|
|
|
|
delete Sound;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// return the version of this class, increments this value when the content of this class changed
|
|
|
|
static uint getVersion () { return 3; }
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/** Load all the sound samples.
|
|
|
|
*
|
|
|
|
* Can throw EPathNotFound or ESoundFileNotFound (check Exception)
|
|
|
|
*/
|
|
|
|
void CSoundBank::load(const std::string &packedSheetDir, bool packedSheetUpdate)
|
|
|
|
{
|
|
|
|
// this structure is fill by the loadForm() function and will contain all you need
|
2012-06-02 16:12:38 +00:00
|
|
|
std::map<std::string, CSoundSerializer> container; // load the old way for compatibility
|
2012-05-29 13:31:11 +00:00
|
|
|
nlassert(!_Loaded);
|
|
|
|
// Just call the GEORGE::loadFrom method to read all available sounds
|
2012-06-02 12:54:39 +00:00
|
|
|
::loadForm("sound", packedSheetDir + "sounds.packed_sheets", container, packedSheetUpdate, false);
|
2012-05-29 13:31:11 +00:00
|
|
|
_Loaded = true;
|
|
|
|
|
2012-06-02 12:54:39 +00:00
|
|
|
// get the largest sheet id needed and init the sound bank
|
|
|
|
uint32 maxShortId = 0;
|
|
|
|
{
|
2012-06-02 16:12:38 +00:00
|
|
|
std::map<std::string, CSoundSerializer>::iterator first(container.begin()), last(container.end());
|
2012-06-02 12:54:39 +00:00
|
|
|
for (; first != last; ++first)
|
|
|
|
{
|
2012-06-02 16:12:38 +00:00
|
|
|
if (first->second.Sound != 0)
|
|
|
|
if (first->second.Sound->getName().getShortId() > maxShortId)
|
|
|
|
maxShortId = first->second.Sound->getName().getShortId();
|
2012-06-02 12:54:39 +00:00
|
|
|
}
|
|
|
|
++maxShortId; // inc for size = last idx + 1
|
2012-06-02 14:06:10 +00:00
|
|
|
if (container.size() == 0)
|
|
|
|
{
|
|
|
|
nlwarning("NLSOUND: No sound sheets have been loaded, missing sound sheet directory or packed sound sheets file");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
nlassert(maxShortId < (container.size() * 8)); // ensure no ridiculous sheet id values
|
|
|
|
if (maxShortId > _Sounds.size())
|
|
|
|
_Sounds.resize(maxShortId);
|
|
|
|
}
|
2012-06-02 12:54:39 +00:00
|
|
|
}
|
|
|
|
|
2012-05-29 13:31:11 +00:00
|
|
|
// add all the loaded sound in the sound banks
|
|
|
|
{
|
2012-06-02 16:12:38 +00:00
|
|
|
std::map<std::string, CSoundSerializer>::iterator first(container.begin()), last(container.end());
|
2012-06-02 12:54:39 +00:00
|
|
|
for (; first != last; ++first)
|
|
|
|
{
|
|
|
|
if (first->second.Sound != 0)
|
|
|
|
addSound(first->second.Sound);
|
|
|
|
}
|
2012-05-29 13:31:11 +00:00
|
|
|
}
|
|
|
|
|
2012-06-02 12:54:39 +00:00
|
|
|
container.clear();
|
2012-05-29 13:31:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Unload all the sound samples in this bank.
|
|
|
|
*/
|
2012-06-02 12:54:39 +00:00
|
|
|
void CSoundBank::unload()
|
2012-05-29 13:31:11 +00:00
|
|
|
{
|
|
|
|
nlassert(_Loaded);
|
|
|
|
|
2012-06-02 12:54:39 +00:00
|
|
|
for (TSoundTable::size_type i = 0; i < _Sounds.size(); ++i)
|
2012-05-29 13:31:11 +00:00
|
|
|
{
|
2012-06-02 12:54:39 +00:00
|
|
|
delete _Sounds[i];
|
2012-05-29 13:31:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_Sounds.clear();
|
|
|
|
_Loaded = false;
|
|
|
|
|
|
|
|
/* vector<CSound*> vec;
|
|
|
|
|
|
|
|
|
|
|
|
TSoundTable::iterator map_iter;
|
|
|
|
|
|
|
|
for (map_iter = _Sounds.begin(); map_iter != _Sounds.end(); ++map_iter)
|
|
|
|
{
|
|
|
|
// We can't delete directly second because the map is based on second->getName()
|
|
|
|
vec.push_back( (*map_iter).second );
|
|
|
|
}
|
|
|
|
|
|
|
|
_Sounds.clear();
|
|
|
|
|
|
|
|
vector<CSound*>::iterator vec_iter;
|
|
|
|
|
|
|
|
for (vec_iter = vec.begin(); vec_iter != vec.end(); ++vec_iter)
|
|
|
|
{
|
|
|
|
CSound *sound = *vec_iter;
|
|
|
|
delete sound;
|
|
|
|
}
|
|
|
|
|
|
|
|
_Loaded = false;
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Returns true if the samples in this bank have been loaded.
|
|
|
|
*/
|
2012-06-02 12:54:39 +00:00
|
|
|
bool CSoundBank::isLoaded()
|
2012-05-29 13:31:11 +00:00
|
|
|
{
|
|
|
|
return _Loaded;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Return a sound sample corresponding to a name.
|
|
|
|
*/
|
2012-06-02 12:54:39 +00:00
|
|
|
CSound* CSoundBank::getSound(const NLMISC::CSheetId &sheetId)
|
2012-05-29 13:31:11 +00:00
|
|
|
{
|
2012-06-02 12:54:39 +00:00
|
|
|
if (sheetId == NLMISC::CSheetId::Unknown)
|
|
|
|
return NULL;
|
2012-06-02 14:06:10 +00:00
|
|
|
|
|
|
|
// nlassert(sheetId.getShortId() < _Sounds.size());
|
|
|
|
if (sheetId.getShortId() >= _Sounds.size())
|
|
|
|
{
|
|
|
|
std::string sheetName = sheetId.toString();
|
2016-05-08 17:23:27 +00:00
|
|
|
nldebug("NLSOUND: Sound sheet id '%s' exceeds loaded sound sheets", sheetName.c_str());
|
2012-06-02 14:06:10 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-06-02 12:54:39 +00:00
|
|
|
return _Sounds[sheetId.getShortId()];
|
2012-05-29 13:31:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the names of the sounds
|
|
|
|
*/
|
2012-06-02 12:54:39 +00:00
|
|
|
void CSoundBank::getNames( std::vector<NLMISC::CSheetId> &sheetIds )
|
2012-05-29 13:31:11 +00:00
|
|
|
{
|
2012-06-02 12:54:39 +00:00
|
|
|
for (TSoundTable::size_type i = 0; i < _Sounds.size(); ++i)
|
2012-05-29 13:31:11 +00:00
|
|
|
{
|
2012-06-02 12:54:39 +00:00
|
|
|
if (_Sounds[i])
|
|
|
|
sheetIds.push_back(_Sounds[i]->getName());
|
2012-05-29 13:31:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Return the number of buffers in this bank.
|
|
|
|
*/
|
2012-06-02 12:54:39 +00:00
|
|
|
uint CSoundBank::countSounds()
|
2012-05-29 13:31:11 +00:00
|
|
|
{
|
|
|
|
return (uint)_Sounds.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
} // namespace NLSOUND
|
|
|
|
|