Ticket #1766: soundManagerEntity.patch

File soundManagerEntity.patch, 24.8 KB (added by stwf, 11 years ago)

first draft of new SoundManager code

  • source/simulation2/components/CCmpSoundManager.cpp

     
    135135                    sourcePos = CVector3D(cmpPosition->GetPosition());
    136136            }
    137137
    138             group->PlayNext(sourcePos);
     138            group->PlayNext(sourcePos, source);
    139139        }
    140140#else // !CONFIG2_AUDIO
    141141        UNUSED2(name);
  • source/soundmanager/SoundManager.cpp

     
    3434
    3535CSoundManager* g_SoundManager = NULL;
    3636
     37#define     SOURCE_NUM      128
    3738
     39#if CONFIG2_AUDIO
     40
    3841class CSoundManagerWorker
    3942{
    4043public:
     
    8386            ItemsList::iterator lstr = m_Items->begin();
    8487            while (lstr != m_Items->end())
    8588            {
    86                 (*lstr)->Stop();
    8789                delete *lstr;
    8890                lstr++;
    8991            }
     
    115117        m_DeadItems->clear();
    116118    }
    117119
    118     void DeleteItem(long itemNum)
    119     {
    120         CScopeLock lock(m_WorkerMutex);
    121         AL_CHECK
    122         ItemsList::iterator lstr = m_Items->begin();
    123         lstr += itemNum;
    124        
    125         delete *lstr;
    126         AL_CHECK
    127        
    128         m_Items->erase(lstr);
    129     }
    130 
    131 
    132120private:
    133121    static void* RunThread(void* data)
    134122    {
     
    146134        // Wait until the main thread wakes us up
    147135        while ( true )
    148136        {
     137            g_Profiler2.RecordRegionLeave("semaphore wait");
     138
    149139            // Handle shutdown requests as soon as possible
    150140            if (GetShutdown())
    151141                return;
     
    155145                continue;
    156146
    157147            int pauseTime = 1000;
     148            if ( g_SoundManager->InDistress() )
     149                pauseTime = 50;
     150
    158151            {
    159152                CScopeLock lock(m_WorkerMutex);
    160153       
     
    164157
    165158                while (lstr != m_Items->end()) {
    166159
     160                    AL_CHECK
    167161                    if ((*lstr)->IdleTask())
    168162                    {
    169                         if ( (*lstr)->IsFading() )
     163                        if ( (pauseTime == 1000) && (*lstr)->IsFading() )
    170164                            pauseTime = 100;
     165
    171166                        nextItemList->push_back(*lstr);
    172167                    }
    173168                    else
     
    217212    bool m_Enabled;
    218213    bool m_Shutdown;
    219214};
     215#endif
    220216
    221217void CSoundManager::ScriptingInit()
    222218{
     
    229225
    230226#if CONFIG2_AUDIO
    231227
     228
    232229void CSoundManager::CreateSoundManager()
    233230{
    234231    g_SoundManager = new CSoundManager();
     
    260257
    261258CSoundManager::CSoundManager()
    262259{
    263     m_CurrentEnvirons   = NULL;
    264     m_CurrentTune       = NULL;
     260    m_CurrentEnvirons   = 0;
     261    m_ALSourceBuffer    = NULL;
     262    m_CurrentTune       = 0;
    265263    m_Gain              = 1;
    266264    m_MusicGain         = 1;
    267265    m_AmbientGain       = 1;
     
    269267    m_BufferCount       = 50;
    270268    m_BufferSize        = 65536;
    271269    m_MusicEnabled      = true;
     270    m_DistressTime  = 0;
     271    m_DistressErrCount = 0;
     272
    272273    m_Enabled           = AlcInit() == INFO::OK;
    273274    InitListener();
    274275
     
    286287}
    287288
    288289
     290
    289291Status CSoundManager::AlcInit()
    290292{   
    291293    Status ret = INFO::OK;
     
    297299        m_Context = alcCreateContext(m_Device, &attribs[0]);
    298300
    299301        if(m_Context)
     302        {
    300303            alcMakeContextCurrent(m_Context);
     304            m_ALSourceBuffer = new ALSourceHolder[SOURCE_NUM];
     305            ALuint* sourceList = new ALuint[SOURCE_NUM];
     306
     307            alGenSources( SOURCE_NUM, sourceList);
     308            ALCenum err = alcGetError(m_Device);
     309            if ( err == ALC_NO_ERROR )
     310            {
     311                for ( int x=0; x<SOURCE_NUM;x++)
     312                {
     313                    m_ALSourceBuffer[x].ALSource    = sourceList[x];
     314                    m_ALSourceBuffer[x].SourceItem  = NULL;
     315
     316                }
     317            }
     318            else
     319            {
     320                LOGERROR(L"error in gensource = %d", err);
     321            }
     322            delete[] sourceList;
     323        }
    301324    }
    302325
    303326    // check if init succeeded.
     
    311334    else
    312335    {
    313336        LOGERROR(L"Sound: AlcInit failed, m_Device=%p m_Context=%p dev_name=%hs err=%d\n", m_Device, m_Context, dev_name, err);
     337
     338
     339
    314340// FIXME Hack to get around exclusive access to the sound device
    315341#if OS_UNIX
    316342        ret = INFO::OK;
     
    321347
    322348    return ret;
    323349}
     350
     351bool CSoundManager::InDistress()
     352{
     353    CScopeLock lock(m_DistressMutex);
     354
     355    if ( m_DistressTime == 0 )
     356        return false;
     357    else if ( (timer_Time() - m_DistressTime) > 10 )
     358    {
     359        m_DistressTime = 0;
     360        LOGERROR(L"Sound: Coming out of distress mode suffering %ld errors\n", m_DistressErrCount);
     361        m_DistressErrCount = 0;
     362        return false;
     363    }
     364    else
     365    {
     366//      LOGERROR(L"Sound: Still in distress mode suffering %ld errors at %ld time\n", m_DistressErrCount, m_DistressTime);
     367    }
     368
     369
     370    return true;
     371}
     372
     373void CSoundManager::SetDistressThroughShortage()
     374{
     375    CScopeLock lock(m_DistressMutex);
     376
     377    if ( m_DistressTime == 0 )
     378        LOGERROR(L"Sound: Entering distress mode through shortage\n", 0);
     379   
     380    m_DistressTime = timer_Time();
     381}
     382
     383void CSoundManager::SetDistressThroughError()
     384{
     385    CScopeLock lock(m_DistressMutex);
     386
     387    if ( m_DistressTime == 0 )
     388        LOGERROR(L"Sound: Entering distress mode through error\n", 0);
     389   
     390    m_DistressTime = timer_Time();
     391    m_DistressErrCount++;
     392}
     393
     394
     395
     396ALuint CSoundManager::GetALSource( ISoundItem* anItem)
     397{
     398    for ( int x=0; x<SOURCE_NUM;x++)
     399    {
     400        if ( ! m_ALSourceBuffer[x].SourceItem )
     401        {
     402            m_ALSourceBuffer[x].SourceItem = anItem;
     403            return m_ALSourceBuffer[x].ALSource;
     404        }
     405    }
     406    SetDistressThroughShortage();
     407    return 0;
     408}
     409
     410void CSoundManager::ReleaseALSource(ALuint theSource)
     411{
     412    for ( int x=0; x<SOURCE_NUM;x++)
     413    {
     414        if ( m_ALSourceBuffer[x].ALSource == theSource )
     415        {
     416            m_ALSourceBuffer[x].SourceItem = NULL;
     417            return;
     418        }
     419    }
     420}
     421
    324422void CSoundManager::SetMemoryUsage(long bufferSize, int bufferCount)
    325423{
    326424    m_BufferCount = bufferCount;
     
    361459    AL_CHECK
    362460
    363461    CSoundData* itemData = CSoundData::SoundDataFromFile(itemPath);
     462
     463    AL_CHECK
     464    if ( itemData )
     465        return CSoundManager::ItemForData( itemData );
     466
     467    return NULL;
     468}
     469
     470ISoundItem* CSoundManager::ItemForData(CSoundData* itemData)
     471{   
     472    AL_CHECK
    364473    ISoundItem* answer = NULL;
    365474
    366475    AL_CHECK
     
    392501    AL_CHECK
    393502    if (m_CurrentTune)
    394503        m_CurrentTune->EnsurePlay();
     504    AL_CHECK
    395505    if (m_CurrentEnvirons)
    396506        m_CurrentEnvirons->EnsurePlay();
    397507    AL_CHECK
    398508    if (m_Worker)
    399509        m_Worker->CleanupItems();
     510    AL_CHECK
    400511}
    401512
    402 void CSoundManager::DeleteItem(long itemNum)
     513ISoundItem* CSoundManager::ItemForEntity( entity_id_t source, CSoundData* sndData)
    403514{
    404     if (m_Worker)
    405         m_Worker->DeleteItem(itemNum);
     515    return ItemForData( sndData );
    406516}
    407517
    408518
     
    431541        }
    432542    }
    433543}
    434 void CSoundManager::PlayGroupItem(ISoundItem* anItem, ALfloat groupGain)
     544void CSoundManager::PlayGroupItem(ISoundItem* anItem, ALfloat groupGain )
    435545{
    436546    if (anItem)
    437547    {
    438548        if (m_Enabled && (m_ActionGain > 0)) {
    439549            anItem->SetGain(m_ActionGain * groupGain);
    440             anItem->Play();
     550            anItem->PlayAndDelete();
    441551            AL_CHECK
    442552        }
    443553    }
  • source/soundmanager/SoundManager.h

     
    2424
    2525#include "lib/file/vfs/vfs_path.h"
    2626#include "soundmanager/items/ISoundItem.h"
     27#include "simulation2/system/Entity.h"
     28#include "soundmanager/data/SoundData.h"
     29#include "ps/Profiler2.h"
    2730
    2831#include <vector>
    2932#include <map>
     
    3134#define AL_CHECK CSoundManager::al_check(__func__, __LINE__);
    3235
    3336typedef std::vector<ISoundItem*> ItemsList;
     37typedef std::map<entity_id_t, ISoundItem*> ItemsMap;
    3438
    3539class CSoundManagerWorker;
    3640
     41
     42struct ALSourceHolder
     43{
     44    /// Title of the column
     45    ALuint      ALSource;
     46    ISoundItem* SourceItem;
     47};
     48
    3749class CSoundManager
    3850{
    3951protected:
     
    4456    ISoundItem* m_CurrentTune;
    4557    ISoundItem* m_CurrentEnvirons;
    4658    CSoundManagerWorker* m_Worker;
     59    CMutex m_DistressMutex;
     60
    4761    float m_Gain;
    4862    float m_MusicGain;
    4963    float m_AmbientGain;
     
    5468    bool m_MusicEnabled;
    5569    bool m_SoundEnabled;
    5670
     71    long m_DistressErrCount;
     72    long m_DistressTime;
     73
     74    ALSourceHolder* m_ALSourceBuffer;
     75
    5776public:
    5877    CSoundManager();
    5978    virtual ~CSoundManager();
    6079
    6180    ISoundItem* LoadItem(const VfsPath& itemPath);
     81    ISoundItem* ItemForData(CSoundData* itemData);
     82    ISoundItem* ItemForEntity( entity_id_t source, CSoundData* sndData);
    6283
    6384    static void ScriptingInit();
    6485    static void CreateSoundManager();
     
    7091    void SetMusicEnabled (bool isEnabled);
    7192    void setSoundEnabled( bool enabled );
    7293
     94    ALuint GetALSource(ISoundItem* anItem);
     95    void ReleaseALSource(ALuint theSource);
     96    ISoundItem* ItemFromData(CSoundData* itemData);
     97
    7398    ISoundItem* ItemFromWAV(VfsPath& fname);
    7499    ISoundItem* ItemFromOgg(VfsPath& fname);
    75100
    76101    ISoundItem* GetSoundItem(unsigned long itemRow);
    77102    unsigned long Count();
    78103    void IdleTask();
    79     void DeleteItem(long itemNum);
    80104   
    81105    void SetMemoryUsage(long bufferSize, int bufferCount);
    82106    long GetBufferCount();
     
    91115    void SetMusicGain(float gain);
    92116    void SetAmbientGain(float gain);
    93117    void SetActionGain(float gain);
    94    
     118    bool InDistress();
     119    void SetDistressThroughShortage();
     120    void SetDistressThroughError();
     121
    95122protected:
    96123    void InitListener();
    97124    virtual Status AlcInit();
  • source/soundmanager/data/OggData.cpp

     
    3434
    3535COggData::~COggData()
    3636{
     37    ogg->Close();
     38
    3739    alDeleteBuffers(m_BuffersUsed, m_Buffer);
    3840}
    3941
  • source/soundmanager/data/SoundData.cpp

     
    3535
    3636CSoundData::~CSoundData()
    3737{
     38//  LOGERROR(L"Sound data deleted %ls\n", m_FileName->c_str() );
     39
    3840    if (m_ALBuffer != 0)
    3941        alDeleteBuffers(1, &m_ALBuffer);
     42
    4043    delete m_FileName;
    4144}
    4245
  • source/soundmanager/data/ogg.cpp

     
    2828#include "lib/file/file_system.h"
    2929
    3030#include "lib/file/vfs/vfs_util.h"
     31#include "ps/CLogger.h"
    3132#include "ps/Filesystem.h"
    3233
    3334
     
    219220        m_fileEOF = false;
    220221    }
    221222
     223    Status Close()
     224    {
     225        ov_clear( &vf );
     226        return 0;
     227    }
     228
    222229    Status Open()
    223230    {
    224231        ov_callbacks callbacks;
  • source/soundmanager/data/ogg.h

     
    3232    virtual ALsizei SamplingRate() = 0;
    3333    virtual bool atFileEOF() = 0;
    3434    virtual Status ResetFile() = 0;
     35  virtual Status Close() = 0;
    3536
    3637    /**
    3738     * @return bytes read (<= size) or a (negative) Status
  • source/soundmanager/items/CBufferItem.cpp

     
    3636
    3737CBufferItem::~CBufferItem()
    3838{
    39     AL_CHECK
     39}
    4040
    41     Stop();
    42     if (m_ALSource != 0)
     41
     42void CBufferItem::ReleaseOpenAL()
     43{
     44    int num_processed;
     45    alGetSourcei(m_ALSource, AL_BUFFERS_PROCESSED, &num_processed);
     46   
     47    if (num_processed > 0)
    4348    {
    4449        int num_processed;
    4550        alGetSourcei(m_ALSource, AL_BUFFERS_PROCESSED, &num_processed);
     
    5358            delete[] al_buf;
    5459        }
    5560    }
     61
     62    CSoundBase::ReleaseOpenAL();
    5663}
    5764
    58 
    5965bool CBufferItem::IdleTask()
    6066{
     67    if ( m_ALSource == 0 )
     68        return false;
     69
    6170    HandleFade();
    62     if (m_ALSource != 0)
     71   
     72    if (m_LastPlay)
    6373    {
    64         if (m_LastPlay)
     74        int proc_state;
     75        alGetSourceiv(m_ALSource, AL_SOURCE_STATE, &proc_state);
     76        AL_CHECK
     77        return (proc_state != AL_STOPPED);
     78    }
     79   
     80    if (GetLooping())
     81    {
     82        int num_processed;
     83        alGetSourcei(m_ALSource, AL_BUFFERS_PROCESSED, &num_processed);
     84       
     85        for (int i = 0; i < num_processed; i++)
    6586        {
    6687            int proc_state;
    6788            alGetSourceiv(m_ALSource, AL_SOURCE_STATE, &proc_state);
     
    91112
    92113void CBufferItem::Attach(CSoundData* itemData)
    93114{
     115AL_CHECK
     116    if ( m_ALSource == 0 )
     117        return;
     118   
    94119    AL_CHECK
    95     if ( (itemData != NULL) && (m_ALSource != 0) )
     120    if (m_SoundData != NULL)
    96121    {
     122        CSoundData::ReleaseSoundData(m_SoundData);
     123        m_SoundData = 0;
     124    }
     125    AL_CHECK
     126    if (itemData != NULL)
     127    {
    97128        m_SoundData = itemData->IncrementCount();
    98129        alSourceQueueBuffers(m_ALSource, m_SoundData->GetBufferCount(),(const ALuint *) m_SoundData->GetBufferPtr());
    99130        AL_CHECK
  • source/soundmanager/items/CBufferItem.h

     
    2727class CBufferItem : public CSoundBase
    2828{
    2929public:
    30     CBufferItem(CSoundData* sndData);
    31     virtual ~CBufferItem();
    32    
    33     virtual void SetLooping(bool loops);
    34     virtual bool IdleTask();
    35    
    36 protected: 
    37     virtual void Attach(CSoundData* itemData);
     30  CBufferItem(CSoundData* sndData);
     31  virtual ~CBufferItem();
     32 
     33  virtual void SetLooping(bool loops);
     34  virtual bool IdleTask();
     35  virtual void Attach(CSoundData* itemData);
    3836
    39    
     37protected: 
     38  virtual void ReleaseOpenAL();
     39 
    4040};
    4141
    4242#endif // CONFIG2_AUDIO
  • source/soundmanager/items/CSoundBase.cpp

     
    3636CSoundBase::~CSoundBase()
    3737{
    3838    Stop();
     39    ReleaseOpenAL();
     40}
     41
     42void CSoundBase::ReleaseOpenAL()
     43{
    3944    if (m_ALSource != 0)
    4045    {
    41         alDeleteSources(1, &m_ALSource);
     46        alSourcei(m_ALSource, AL_BUFFER, 0);
     47        g_SoundManager->ReleaseALSource(m_ALSource);
    4248        m_ALSource = 0;
    4349    }
    4450    if (m_SoundData != 0)
     
    4854    }
    4955}
    5056
     57void CSoundBase::Attach(CSoundData* itemData)
     58{
     59
     60}
     61
    5162void CSoundBase::ResetVars()
    5263{
    5364    m_ALSource = 0;
     
    7182    m_ShouldBePlaying = false;
    7283}
    7384
     85
     86bool CSoundBase::InitOpenAL()
     87{
     88    alGetError(); /* clear error */
     89    m_ALSource = g_SoundManager->GetALSource( this );
     90
     91    AL_CHECK
     92
     93    if ( m_ALSource )   
     94    {       
     95        alSourcef(m_ALSource,AL_PITCH,1.0f);
     96        AL_CHECK
     97        alSourcef(m_ALSource,AL_GAIN,1.0f);
     98        AL_CHECK
     99        alSourcei(m_ALSource,AL_LOOPING,AL_FALSE);
     100        AL_CHECK
     101        return true;
     102    }
     103    else
     104    {
     105//      LOGERROR(L"Source not allocated by SOundManager\n", 0);
     106    }
     107    return false;
     108}
     109
     110
    74111void CSoundBase::SetGain(ALfloat gain)
    75112{
    76113    AL_CHECK
     
    84121
    85122void CSoundBase::SetRollOff(ALfloat rolls)
    86123{
    87     if ( m_ALSource )   
    88     {
     124    if ( m_ALSource )
     125    {
    89126        alSourcef(m_ALSource, AL_REFERENCE_DISTANCE, 70.0f);
    90127        AL_CHECK
    91128        alSourcef(m_ALSource, AL_MAX_DISTANCE, 200.0);
     
    133170    }
    134171}
    135172
    136 bool CSoundBase::InitOpenAL()
    137 {
    138     alGetError(); /* clear error */
    139     alGenSources(1, &m_ALSource);
    140     ALenum anErr = alGetError();
    141173
    142     if (anErr == AL_NO_ERROR)
    143     {       
    144         alSourcef(m_ALSource,AL_PITCH,1.0f);
    145         AL_CHECK
    146         alSourcef(m_ALSource,AL_GAIN,1.0f);
    147         AL_CHECK
    148         alSourcei(m_ALSource,AL_LOOPING,AL_FALSE);
    149         AL_CHECK
    150         return true;
    151     }
    152     else
    153     {
    154         CSoundManager::al_ReportError( anErr, __func__, __LINE__);
    155     }
    156     return false;
    157 }
    158 
    159174bool CSoundBase::IsPlaying()
    160175{
    161     ALenum proc_state = AL_STOPPED;
    162     if ( m_ALSource != 0 )
     176    if ( m_ALSource )
    163177    {
     178        int proc_state;
    164179        alGetSourceiv(m_ALSource, AL_SOURCE_STATE, &proc_state);
    165180        AL_CHECK
     181
     182        return (proc_state == AL_PLAYING);
    166183    }
    167 
    168     return (proc_state == AL_PLAYING);
     184    return false;
    169185}
    170186
    171187void CSoundBase::SetLastPlay(bool last)
     
    178194    return true;
    179195}
    180196
     197
    181198void CSoundBase::SetLocation (const CVector3D& position)
    182199{
    183200    if ( m_ALSource != 0 )
     
    237254void CSoundBase::Play()
    238255{
    239256    m_ShouldBePlaying = true;
     257    AL_CHECK
    240258    if (m_ALSource != 0)
     259    {
    241260        alSourcePlay(m_ALSource);
    242     AL_CHECK
     261        ALenum err = alGetError();
     262        if (err != AL_NO_ERROR)
     263        {
     264            if (err == AL_INVALID)
     265                g_SoundManager->SetDistressThroughError();
     266            else
     267                g_SoundManager->al_ReportError(err, __func__, __LINE__);
     268        }
     269    }
    243270}
    244271
    245272void CSoundBase::PlayAndDelete()
     
    248275    Play();
    249276}
    250277
     278
    251279void CSoundBase::FadeAndDelete(double fadeTime)
    252280{
    253281    SetLastPlay(true);
  • source/soundmanager/items/CSoundBase.h

     
    2323#if CONFIG2_AUDIO
    2424
    2525#include "lib/external_libraries/openal.h"
     26#include "ps/ThreadUtil.h"
    2627#include "soundmanager/items/ISoundItem.h"
    2728#include "soundmanager/data/SoundData.h"
    2829
     
    3940   
    4041    double m_StartFadeTime;
    4142    double m_EndFadeTime;
     43
    4244    ALfloat m_StartVolume;
    4345    ALfloat m_EndVolume;
    4446
     
    5759    virtual void SetDirection(const CVector3D& direction);
    5860    virtual void SetCone(ALfloat innerCone, ALfloat outerCone, ALfloat coneGain);
    5961    virtual void SetLastPlay(bool last);
     62    virtual void ReleaseOpenAL();
     63   
    6064    virtual bool IsFading();
    6165
    6266    void Play();
     
    6670    void Stop();
    6771    void StopAndDelete();
    6872    void FadeToIn(ALfloat newVolume, double fadeDuration);
     73    void Attach(CSoundData* itemData);
    6974
    7075    void PlayAsMusic();
    7176    void PlayAsAmbient();
  • source/soundmanager/items/CSoundItem.cpp

     
    4040
    4141CSoundItem::~CSoundItem()
    4242{
    43     AL_CHECK
    44    
    45     Stop();
     43}
    4644
    47     if (m_ALSource != 0)
    48     {
    49         alSourcei(m_ALSource, AL_BUFFER, 0);
    50         AL_CHECK
    51     }   
     45void CSoundItem::ReleaseOpenAL()
     46{
     47    alSourcei(m_ALSource, AL_BUFFER, 0);
     48
     49    CSoundBase::ReleaseOpenAL();
    5250}
    5351
    5452bool CSoundItem::IdleTask()
    5553{
     54    if ( m_ALSource == 0 )
     55        return false;
     56
    5657    HandleFade();
    5758
    58     if (m_LastPlay && (m_ALSource != 0) )
     59    if (m_LastPlay && m_ALSource)
    5960    {
    6061        int proc_state;
    61         alGetSourceiv(m_ALSource, AL_SOURCE_STATE, &proc_state);
     62        alGetSourcei(m_ALSource, AL_SOURCE_STATE, &proc_state);
    6263        AL_CHECK
    6364        return (proc_state != AL_STOPPED);
    6465    }
     
    6768
    6869void CSoundItem::Attach(CSoundData* itemData)
    6970{
    70     if (itemData != NULL && (m_ALSource != 0) )
     71AL_CHECK
     72    if (m_SoundData != NULL)
    7173    {
     74        CSoundData::ReleaseSoundData(m_SoundData);
     75        m_SoundData = 0;
     76    }
     77AL_CHECK
     78    if (itemData != NULL)
     79    {
     80        alSourcei(m_ALSource, AL_BUFFER, 0);
     81        AL_CHECK
    7282        m_SoundData = itemData->IncrementCount();
    7383        alSourcei(m_ALSource, AL_BUFFER, m_SoundData->GetBuffer());
    7484       
  • source/soundmanager/items/CSoundItem.h

     
    2828class CSoundItem : public CSoundBase
    2929{
    3030public:
    31     CSoundItem();
    32     CSoundItem(CSoundData* sndData);
    33    
    34     virtual ~CSoundItem();
    35     void Attach(CSoundData* itemData);
    36     bool IdleTask();
     31  CSoundItem();
     32  CSoundItem(CSoundData* sndData);
     33 
     34  virtual ~CSoundItem();
     35  void Attach(CSoundData* itemData);
    3736
     37  bool IdleTask();
     38  void  ReleaseOpenAL();
     39
    3840};
    3941
    4042#endif // CONFIG2_AUDIO
  • source/soundmanager/items/CStreamItem.cpp

     
    3535
    3636CStreamItem::~CStreamItem()
    3737{
    38     Stop();
     38}
     39
     40void CStreamItem::ReleaseOpenAL()
     41{
     42    int num_processed;
     43    alGetSourcei(m_ALSource, AL_BUFFERS_PROCESSED, &num_processed);
    3944   
    4045    if (m_ALSource != 0)
    4146    {
     
    5055            delete[] al_buf;
    5156        }
    5257    }
     58    CSoundBase::ReleaseOpenAL();
    5359}
    5460
    5561bool CStreamItem::IdleTask()
    5662{
    5763    AL_CHECK
     64
    5865    HandleFade();
    5966    AL_CHECK
    6067
     
    101108
    102109void CStreamItem::Attach(CSoundData* itemData)
    103110{
    104     if (itemData != NULL && (m_ALSource != 0) )
     111    if (m_SoundData != NULL)
    105112    {
     113        CSoundData::ReleaseSoundData(m_SoundData);
     114        m_SoundData = 0;
     115    }
     116
     117    if (itemData != NULL)
     118    {
    106119        m_SoundData = itemData->IncrementCount();
    107120        alSourceQueueBuffers(m_ALSource, m_SoundData->GetBufferCount(), (const ALuint *)m_SoundData->GetBufferPtr());
    108121        AL_CHECK
  • source/soundmanager/items/CStreamItem.h

     
    2828class CStreamItem : public CSoundBase
    2929{
    3030public:
    31     CStreamItem(CSoundData* sndData);
    32     virtual ~CStreamItem();
    33    
    34     virtual void SetLooping(bool loops);
    35     virtual bool IdleTask();
    36    
    37 protected: 
    38     virtual void Attach(CSoundData* itemData);
     31  CStreamItem(CSoundData* sndData);
     32  virtual ~CStreamItem();
     33 
     34  virtual void SetLooping(bool loops);
     35  virtual bool IdleTask();
    3936
     37  virtual void Attach(CSoundData* itemData);
     38
     39protected: 
     40  virtual void ReleaseOpenAL();
    4041};
    4142
    4243#endif // CONFIG2_AUDIO
  • source/soundmanager/items/ISoundItem.h

     
    2525#include "lib/external_libraries/openal.h"
    2626#include "maths/Vector3D.h"
    2727#include "ps/CStr.h"
     28#include "soundmanager/data/SoundData.h"
    2829
    2930class ISoundItem
    3031{
     
    4344    virtual void Play() = 0;
    4445    virtual void Stop() = 0;
    4546
     47    virtual void Attach(CSoundData* itemData) = 0;
     48
    4649    virtual void EnsurePlay() = 0;
    4750    virtual void PlayAsMusic() = 0;
    4851    virtual void PlayAsAmbient() = 0;
  • source/soundmanager/js/SoundGroup.cpp

     
    157157    return answer;
    158158}
    159159
    160 void CSoundGroup::UploadPropertiesAndPlay(unsigned int theIndex, const CVector3D& position)
     160void CSoundGroup::UploadPropertiesAndPlay(int theIndex, const CVector3D& position, entity_id_t source)
    161161{
    162162#if CONFIG2_AUDIO
    163163    if ( g_SoundManager )
     
    175175
    176176            if ( snd_group.size() > theIndex )
    177177            {
    178                 if ( ISoundItem* hSound = snd_group[theIndex] )
     178                if ( CSoundData* sndData = snd_group[theIndex] )
    179179                {
    180180                    CVector3D origin = g_Game->GetView()->GetCamera()->GetOrientation().GetTranslation();
    181181                    float sndDist = origin.Y;
    182182
     183                    ISoundItem* hSound = g_SoundManager->ItemForEntity( source, sndData);
     184
    183185                    if (!TestFlag(eOmnipresent))
    184186                    {
    185187                        if (TestFlag(eDistanceless))
     
    208210#else // !CONFIG2_AUDIO
    209211    UNUSED2(theIndex);
    210212    UNUSED2(position);
     213    UNUSED2(source);
    211214#endif // !CONFIG2_AUDIO
    212215}
    213216
     
    219222    LOGERROR(L"%ls: pathname=%ls, error=%ls", message.c_str(), pathname.string().c_str(), ErrorString(err));
    220223}
    221224
    222 void CSoundGroup::PlayNext(const CVector3D& position)
     225void CSoundGroup::PlayNext(const CVector3D& position, entity_id_t source)
    223226{
    224227    // if no sounds, return
    225228    if (filenames.size() == 0)
    226229        return;
    227230   
    228231    m_index = (size_t)rand(0, (size_t)filenames.size());
    229     UploadPropertiesAndPlay(m_index, position);
     232    UploadPropertiesAndPlay(m_index, position, source);
    230233}
    231234
    232235void CSoundGroup::Reload()
     
    234237    m_index = 0; // reset our index
    235238
    236239#if CONFIG2_AUDIO
    237     snd_group.clear();
     240    ReleaseGroup();
    238241
    239242    if ( g_SoundManager ) {
    240243        for (size_t i = 0; i < filenames.size(); i++)
    241244        {
    242245            VfsPath  thePath =  m_filepath/filenames[i];
    243             ISoundItem* temp = g_SoundManager->LoadItem(thePath);
     246            CSoundData* itemData = CSoundData::SoundDataFromFile(thePath);
    244247
    245             if (temp == NULL)
     248            if (itemData == NULL)
    246249                HandleError(L"error loading sound", thePath, ERR::FAIL);
    247250            else
    248                 snd_group.push_back(temp);
     251                snd_group.push_back(itemData->IncrementCount());
    249252        }
    250253
    251254        if (TestFlag(eRandOrder))
     
    259262#if CONFIG2_AUDIO
    260263    for (size_t i = 0; i < snd_group.size(); i++)
    261264    {
    262         snd_group[i]->FadeAndDelete(0.2);
     265        CSoundData::ReleaseSoundData( snd_group[i] );
    263266    }
    264267    snd_group.clear();
    265268#endif // CONFIG2_AUDIO
  • source/soundmanager/js/SoundGroup.h

     
    5454
    5555#include "lib/config2.h"
    5656#include "lib/file/vfs/vfs_path.h"
     57#include "simulation2/system/Entity.h"
     58#include "soundmanager/data/SoundData.h"
    5759
    5860#include <vector>
    5961
     
    8284    // Play next sound in group
    8385    // @param position world position of the entity generating the sound
    8486    // (ignored if the eOmnipresent flag is set)
    85     void PlayNext(const CVector3D& position);
     87    void PlayNext(const CVector3D& position, entity_id_t source);
    8688
    8789    float RadiansOffCenter(const CVector3D& position, bool& onScreen, float& itemRollOff);
    8890
     
    105107   
    106108private:
    107109    void SetGain(float gain);
    108     void UploadPropertiesAndPlay(unsigned int theIndex, const CVector3D& position);
     110
     111    void UploadPropertiesAndPlay(int theIndex, const CVector3D& position, entity_id_t source);
     112
    109113    void SetDefaultValues();
    110114
    111     size_t m_index;  // index of the next sound to play
     115    int m_index;  // index of the next sound to play
    112116       
    113117#if CONFIG2_AUDIO
    114     std::vector<ISoundItem*> snd_group;  // we store the handles so we can load now and play later
     118    std::vector<CSoundData*> snd_group;  // we store the handles so we can load now and play later
    115119#endif
    116120    std::vector<std::wstring> filenames; // we need the filenames so we can reload when necessary.
    117121