Ticket #1604: archiveStream.diff

File archiveStream.diff, 45.6 KB (added by stwf, 12 years ago)

patch file

  • source/ps/GameSetup/Config.cpp

     
    8484    CFG_GET_USER_VAL("silhouettes", Bool, g_Silhouettes);
    8585    CFG_GET_USER_VAL("showsky", Bool, g_ShowSky);
    8686
     87 #if CONFIG2_AUDIO
    8788    float gain = 0.5f;
    8889    float musicGain = 0.5f;
    8990    float ambientGain = 0.5f;
     
    99100    CFG_GET_USER_VAL("sound.bufferCount", Int, bufferCount);
    100101    CFG_GET_USER_VAL("sound.bufferSize", UnsignedLong, bufferSize);
    101102
    102     g_SoundManager->SetMasterGain(gain);
    103     g_SoundManager->SetMusicGain(musicGain);
    104     g_SoundManager->SetAmbientGain(ambientGain);
    105     g_SoundManager->SetActionGain(actionGain);
     103    if ( g_SoundManager ) {
     104        g_SoundManager->SetMasterGain(gain);
     105        g_SoundManager->SetMusicGain(musicGain);
     106        g_SoundManager->SetAmbientGain(ambientGain);
     107        g_SoundManager->SetActionGain(actionGain);
    106108
    107     g_SoundManager->SetMemoryUsage(bufferSize, bufferCount);
     109        g_SoundManager->SetMemoryUsage(bufferSize, bufferCount);
     110    }
     111#endif
    108112}
    109113
    110114
  • source/ps/GameSetup/GameSetup.cpp

     
    190190{
    191191    PROFILE3("render");
    192192
    193     g_SoundManager->IdleTask();
    194 
     193 #if CONFIG2_AUDIO
     194    if ( g_SoundManager )
     195        g_SoundManager->IdleTask();
     196#endif
    195197    ogl_WarnIfError();
    196198
    197199    g_Profiler2.RecordGPUFrameStart();
     
    685687    // resource
    686688    // first shut down all resource owners, and then the handle manager.
    687689    TIMER_BEGIN(L"resource modules");
    688         delete g_SoundManager;
     690#if CONFIG2_AUDIO
     691        if ( g_SoundManager )
     692            delete g_SoundManager;
     693#endif
    689694
    690695        g_VFS.reset();
    691696
     
    862867    g_ScriptStatsTable = new CScriptStatsTable;
    863868    g_ProfileViewer.AddRootTable(g_ScriptStatsTable);
    864869
    865     g_SoundManager = new CSoundManager();
     870    CSoundManager::CreateSoundManager();
    866871
    867872    InitScripting();    // before GUI
    868873
     
    926931        // speed up startup by disabling all sound
    927932        // (OpenAL init will be skipped).
    928933        // must be called before first snd_open.
    929         g_SoundManager->SetEnabled(false);
     934        CSoundManager::SetEnabled(false);
    930935    }
    931936
    932937    g_GUI = new CGUIManager(g_ScriptingHost.GetScriptInterface());
  • source/ps/Game.cpp

     
    294294    if (doInterpolate)
    295295    {
    296296        m_TurnManager->Interpolate(deltaSimTime, deltaRealTime);
    297         g_SoundManager->IdleTask();
     297#if CONFIG2_AUDIO
     298        if ( g_SoundManager )
     299            g_SoundManager->IdleTask();
     300#endif
    298301    }
    299302   
    300303    // TODO: maybe we should add a CCmpParticleInterface that passes the interpolation commands
  • source/soundmanager/items/CSoundItem.h

     
    2121#include "CSoundBase.h"
    2222#include "soundmanager/data/SoundData.h"
    2323
     24#if CONFIG2_AUDIO
    2425
    2526class CSoundItem : public CSoundBase
    2627{
     
    3435
    3536};
    3637
     38#endif
    3739#endif // INCLUDED_CSOUNDITEM_H
  • source/soundmanager/items/CBufferItem.h

     
    1818#ifndef INCLUDED_CBUFFERITEM_H
    1919#define INCLUDED_CBUFFERITEM_H
    2020
     21#include "lib/config2.h"
     22
     23#if CONFIG2_AUDIO
     24
    2125#include "CSoundBase.h"
    2226
    2327class CBufferItem : public CSoundBase
     
    3539   
    3640};
    3741
    38 
     42#endif
    3943#endif // INCLUDED_CBUFFERITEM_H
  • source/soundmanager/items/CStreamItem.h

     
    2121#include "soundmanager/data/SoundData.h"
    2222#include "CSoundBase.h"
    2323
     24#if CONFIG2_AUDIO
     25
    2426class CStreamItem : public CSoundBase
    2527{
    2628public:
     
    3537
    3638};
    3739
     40#endif
    3841#endif // INCLUDED_CSTREAMITEM_H
  • source/soundmanager/items/ISoundItem.h

     
    1919#define INCLUDED_ISOUNDITEM_H
    2020
    2121#include "lib/external_libraries/openal.h"
     22#include "lib/config2.h"
    2223#include "maths/Vector3D.h"
    2324
    2425#include <string>
     26#if CONFIG2_AUDIO
    2527
    2628class ISoundItem
    2729{
     
    5658    virtual void SetRollOff(ALfloat gain) = 0;
    5759};
    5860
    59 
     61#endif
    6062#endif // INCLUDED_ISOUNDITEM_H
     63 No newline at end of file
  • source/soundmanager/items/CSoundBase.cpp

     
    2222#include "lib/timer.h"
    2323#include "soundmanager/SoundManager.h"
    2424#include "soundmanager/data/SoundData.h"
     25#include "ps/CLogger.h"
    2526
    2627#include <iostream>
     28#if CONFIG2_AUDIO
    2729
    2830CSoundBase::CSoundBase()
    2931{
     
    7375
    7476void CSoundBase::SetGain(ALfloat gain)
    7577{
     78    AL_CHECK
     79
    7680    alSourcef(m_ALSource, AL_GAIN, gain);
     81
     82    AL_CHECK
    7783}
    7884
    7985void CSoundBase::SetRollOff(ALfloat rolls)
    8086{
    8187   alSourcef(m_ALSource, AL_ROLLOFF_FACTOR, rolls);
     88    AL_CHECK
    8289}
    8390
    8491void CSoundBase::EnsurePlay()
     
    8996
    9097void CSoundBase::SetCone(ALfloat innerCone, ALfloat outerCone, ALfloat coneGain)
    9198{
    92     alSourcef(m_ALSource, innerCone, AL_CONE_INNER_ANGLE);
    93     alSourcef(m_ALSource, outerCone, AL_CONE_OUTER_ANGLE);
    94     alSourcef(m_ALSource, coneGain, AL_CONE_OUTER_GAIN);
     99    AL_CHECK   
     100    alSourcef(m_ALSource, AL_CONE_INNER_ANGLE, innerCone);
     101    AL_CHECK
     102    alSourcef(m_ALSource, AL_CONE_OUTER_ANGLE, outerCone);
     103    AL_CHECK
     104    alSourcef(m_ALSource, AL_CONE_OUTER_GAIN, coneGain);
     105    AL_CHECK
    95106}
    96107
    97108void CSoundBase::SetPitch(ALfloat pitch)
    98109{
    99110    alSourcef(m_ALSource, AL_PITCH, pitch);
     111    AL_CHECK
    100112}
    101113
    102114void CSoundBase::SetDirection(const CVector3D& direction)
    103115{
    104116    alSourcefv(m_ALSource, AL_DIRECTION, direction.GetFloatArray());
     117    AL_CHECK
    105118}
    106119
    107120bool CSoundBase::InitOpenAL()
     
    109122    alGetError(); /* clear error */
    110123    alGenSources(1, &m_ALSource);
    111124    long anErr = alGetError();
    112     if (anErr != AL_NO_ERROR)
     125
     126    AL_CHECK
     127
     128    if (anErr == AL_NO_ERROR)
    113129    {
    114         printf("- Error creating sources %ld !!\n", anErr);
    115     }
    116     else
    117     {
    118130        ALfloat source0Pos[]={ -2.0, 0.0, 0.0};
    119131        ALfloat source0Vel[]={ 0.0, 0.0, 0.0};
    120132       
     
    123135        alSourcefv(m_ALSource,AL_POSITION,source0Pos);
    124136        alSourcefv(m_ALSource,AL_VELOCITY,source0Vel);
    125137        alSourcei(m_ALSource,AL_LOOPING,AL_FALSE);
     138        AL_CHECK
     139
    126140        return true;
    127141    }
    128142    return false;
     
    132146{
    133147    int proc_state;
    134148    alGetSourceiv(m_ALSource, AL_SOURCE_STATE, &proc_state);
     149    AL_CHECK
    135150
    136151    return (proc_state == AL_PLAYING);
    137152}
     
    149164void CSoundBase::SetLocation (const CVector3D& position)
    150165{
    151166    alSourcefv(m_ALSource,AL_POSITION, position.GetFloatArray());
     167    AL_CHECK
    152168}
    153169
    154170bool CSoundBase::HandleFade()
    155171{
     172    AL_CHECK
    156173    if (m_StartFadeTime != 0)
    157174    {
    158175        double currTime = timer_Time();
     
    169186        }
    170187        else
    171188            alSourcef(m_ALSource, AL_GAIN, curGain);     
     189
     190        AL_CHECK
    172191    }
    173192    return true;
    174193}
     
    182201{
    183202    m_Looping = loops;
    184203    alSourcei(m_ALSource, AL_LOOPING, loops ? AL_TRUE : AL_FALSE);
     204    AL_CHECK
    185205}
    186206
    187207void CSoundBase::Play()
     
    189209    m_ShouldBePlaying = true;
    190210    if (m_ALSource != 0)
    191211        alSourcePlay(m_ALSource);
     212    AL_CHECK
    192213}
    193214
    194215void CSoundBase::PlayAndDelete()
     
    215236    {
    216237        SetLooping(true);
    217238        Play();
     239        AL_CHECK
    218240    }
    219241}
    220242
     
    229251        alGetSourcef(m_ALSource, AL_GAIN, &m_StartVolume);
    230252        m_EndVolume = newVolume;
    231253    }
     254    AL_CHECK
    232255}
    233256
    234257void CSoundBase::PlayAsMusic()
     
    251274        alGetSourceiv(m_ALSource, AL_SOURCE_STATE, &proc_state);
    252275        if (proc_state == AL_PLAYING)
    253276            alSourceStop(m_ALSource);
     277
     278        AL_CHECK
    254279    }
    255280}
    256281
     
    274299        m_Name->assign(anst.begin(), anst.end());
    275300}
    276301
     302#endif
     303 No newline at end of file
  • source/soundmanager/items/CSoundItem.cpp

     
    2020#include "CSoundItem.h"
    2121
    2222#include "soundmanager/data/SoundData.h"
     23#include "soundmanager/SoundManager.h"
    2324
    2425#include <iostream>
    2526
     27#if CONFIG2_AUDIO
    2628
    2729CSoundItem::CSoundItem()
    2830{
     
    3840
    3941CSoundItem::~CSoundItem()
    4042{
     43    AL_CHECK
    4144    ALuint al_buf;
    4245   
    4346    Stop();
    4447    alSourceUnqueueBuffers(m_ALSource, 1, &al_buf);
     48    AL_CHECK
    4549}
    4650
    4751bool CSoundItem::IdleTask()
     
    5256    {
    5357        int proc_state;
    5458        alGetSourceiv(m_ALSource, AL_SOURCE_STATE, &proc_state);
     59        AL_CHECK
    5560        return (proc_state != AL_STOPPED);
    5661    }
    5762    return true;
     
    6368    {
    6469        m_SoundData = itemData->IncrementCount();
    6570        alSourcei(m_ALSource, AL_BUFFER, m_SoundData->GetBuffer());
     71       
     72        AL_CHECK
    6673    }
    6774}
     75
     76#endif
     77 No newline at end of file
  • source/soundmanager/items/CBufferItem.cpp

     
    2020#include "CBufferItem.h"
    2121
    2222#include "soundmanager/data/SoundData.h"
     23#include "soundmanager/SoundManager.h"
    2324
    2425#include <iostream>
    2526
     27 #if CONFIG2_AUDIO
     28
     29
    2630CBufferItem::CBufferItem(CSoundData* sndData)
    2731{
    2832    ResetVars();
     
    3337
    3438CBufferItem::~CBufferItem()
    3539{
     40    AL_CHECK
     41
    3642    Stop();
    3743    int num_processed;
    3844    alGetSourcei(m_ALSource, AL_BUFFERS_PROCESSED, &num_processed);
     
    4147    {
    4248        ALuint* al_buf = new ALuint[num_processed];
    4349        alSourceUnqueueBuffers(m_ALSource, num_processed, al_buf);
    44 
     50       
     51        AL_CHECK
    4552        delete[] al_buf;
    4653    }
    4754}
     
    6774        {
    6875            ALuint al_buf;
    6976            alSourceUnqueueBuffers(m_ALSource, 1, &al_buf);
     77            AL_CHECK
    7078            alSourceQueueBuffers(m_ALSource, 1, &al_buf);
     79            AL_CHECK
    7180        }
    7281    }
    7382
     
    7685
    7786void CBufferItem::Attach(CSoundData* itemData)
    7887{
     88AL_CHECK
    7989    if (itemData != NULL)
    8090    {
    8191        m_SoundData = itemData->IncrementCount();
    8292        alSourceQueueBuffers(m_ALSource, m_SoundData->GetBufferCount(),(const ALuint *) m_SoundData->GetBufferPtr());
     93        AL_CHECK
    8394    }
    8495}
    8596
     
    8899    m_Looping = loops;
    89100}
    90101
     102#endif
     103 No newline at end of file
  • source/soundmanager/items/CSoundBase.h

     
    2424
    2525#include <string>
    2626
     27#if CONFIG2_AUDIO
     28
    2729class CSoundBase : public ISoundItem
    2830{
    2931protected:
     
    8688   
    8789};
    8890
     91#endif
    8992#endif // INCLUDED_CSOUNDBASE_H
  • source/soundmanager/items/CStreamItem.cpp

     
    2020#include "CStreamItem.h"
    2121
    2222#include "soundmanager/data/OggData.h"
     23#include "soundmanager/SoundManager.h"
    2324
    2425#include <iostream>
    2526
     27#if CONFIG2_AUDIO
     28
    2629CStreamItem::CStreamItem(CSoundData* sndData)
    2730{
    2831    ResetVars();
     
    4144    {
    4245        ALuint* al_buf = new ALuint[num_processed];
    4346        alSourceUnqueueBuffers(m_ALSource, num_processed, al_buf);
     47        AL_CHECK
    4448        delete[] al_buf;
    4549    }
    4650}
    4751
    4852bool CStreamItem::IdleTask()
    4953{
     54    AL_CHECK
    5055    HandleFade();
     56    AL_CHECK
    5157
    5258    int proc_state;
    5359    alGetSourceiv(m_ALSource, AL_SOURCE_STATE, &proc_state);
     60    AL_CHECK
    5461   
    5562    if (proc_state == AL_STOPPED)
    5663    {
     
    6572        {
    6673            int num_processed;
    6774            alGetSourcei(m_ALSource, AL_BUFFERS_PROCESSED, &num_processed);
     75            AL_CHECK
    6876           
    6977            if (num_processed > 0)
    7078            {
    7179                ALuint* al_buf = new ALuint[num_processed];
    7280                alSourceUnqueueBuffers(m_ALSource, num_processed, al_buf);
     81                AL_CHECK
    7382                int didWrite = tmp->FetchDataIntoBuffer(num_processed, al_buf);
    7483                alSourceQueueBuffers(m_ALSource, didWrite, al_buf);
     84                AL_CHECK
    7585                delete[] al_buf;
    7686            }
    7787        }
     
    8999    {
    90100        m_SoundData = itemData->IncrementCount();
    91101        alSourceQueueBuffers(m_ALSource, m_SoundData->GetBufferCount(), (const ALuint *)m_SoundData->GetBufferPtr());
     102        AL_CHECK
    92103    }
    93104}
    94105
     
    97108    m_Looping = loops;
    98109}
    99110
     111#endif
     112 No newline at end of file
  • source/soundmanager/SoundManager.cpp

     
    2727#include "soundmanager/js/AmbientSound.h"
    2828#include "soundmanager/js/MusicSound.h"
    2929#include "soundmanager/js/Sound.h"
     30#include "ps/CLogger.h"
    3031
    31 CSoundManager* g_SoundManager;
     32CSoundManager* g_SoundManager = NULL;
    3233
     34
     35
     36
    3337void CSoundManager::ScriptingInit()
    3438{
    3539    JAmbientSound::ScriptingInit();
     
    3842    JSoundPlayer::ScriptingInit();
    3943}
    4044
     45void CSoundManager::CreateSoundManager()
     46{
     47#if CONFIG2_AUDIO
     48    g_SoundManager = new CSoundManager();
     49#endif
     50}
     51
     52void CSoundManager::SetEnabled(bool doEnable)
     53{
     54#if CONFIG2_AUDIO
     55
     56    if ( g_SoundManager && !doEnable )
     57    {
     58        delete g_SoundManager;
     59
     60        g_SoundManager = NULL;
     61    }
     62    else if ( ! g_SoundManager && doEnable )
     63    {
     64        CSoundManager::CreateSoundManager();
     65    }
     66#endif
     67}
     68
     69#if CONFIG2_AUDIO
     70
     71void CSoundManager::al_ReportError(ALenum err, const char* caller, int line)
     72{
     73    LOGERROR(L"OpenAL error: %hs; called from %hs (line %d)\n", alGetString(err), caller, line);
     74}
     75
     76void CSoundManager::al_check(const char* caller, int line)
     77{
     78    ALenum err = alGetError();
     79    if (err != AL_NO_ERROR)
     80        al_ReportError(err, caller, line);
     81}
     82
    4183CSoundManager::CSoundManager()
    4284{
    4385    m_Items = new ItemsList;
     
    4789    m_MusicGain         = 1;
    4890    m_AmbientGain       = 1;
    4991    m_ActionGain        = 1;
    50     m_Enabled           = true;
    5192    m_BufferCount       = 50;
    5293    m_BufferSize        = 65536;
    5394    m_MusicEnabled      = true;
    54     AlcInit();
     95    m_Enabled           = AlcInit() == INFO::OK;
    5596}
    5697
    5798CSoundManager::~CSoundManager()
     
    78119{   
    79120    Status ret = INFO::OK;
    80121
     122    AL_CHECK
     123
    81124    m_Device = alcOpenDevice(NULL);
    82125    if(m_Device)
    83126    {
     
    93136    const char* dev_name = (const char*)alcGetString(m_Device, ALC_DEVICE_SPECIFIER);
    94137
    95138    if(err == ALC_NO_ERROR && m_Device && m_Context)
    96         debug_printf(L"Sound: AlcInit success, using %hs\n", dev_name);
     139        LOGERROR(L"Sound: AlcInit success, using %hs\n", dev_name);
    97140    else
    98141    {
    99         debug_printf(L"Sound: AlcInit failed, m_Device=%p m_Context=%p dev_name=%hs err=%d\n", m_Device, m_Context, dev_name, err);
     142        LOGERROR(L"Sound: AlcInit failed, m_Device=%p m_Context=%p dev_name=%hs err=%d\n", m_Device, m_Context, dev_name, err);
    100143// FIXME Hack to get around exclusive access to the sound device
    101144#if OS_UNIX
    102145        ret = INFO::OK;
     
    142185
    143186ISoundItem* CSoundManager::LoadItem(const VfsPath& itemPath)
    144187{   
     188    AL_CHECK
     189
    145190    CSoundData* itemData = CSoundData::SoundDataFromFile(itemPath);
    146191    ISoundItem* answer = NULL;
     192
     193    AL_CHECK
    147194   
    148195    if (itemData != NULL)
    149196    {
     
    198245            deadItems++;
    199246        }
    200247    }
     248    AL_CHECK
    201249    if (m_CurrentTune)
    202250        m_CurrentTune->EnsurePlay();
    203251    if (m_CurrentEnvirons)
    204252        m_CurrentEnvirons->EnsurePlay();
     253    AL_CHECK
    205254}
    206255
    207256void CSoundManager::DeleteItem(long itemNum)
     
    232281    alDistanceModel(AL_EXPONENT_DISTANCE);
    233282}
    234283
    235 void CSoundManager::SetEnabled(bool doEnable)
    236 {
    237     m_Enabled = doEnable;
    238 }
    239 
    240284void CSoundManager::PlayActionItem(ISoundItem* anItem)
    241285{
    242286    if (anItem)
     
    245289        {
    246290            anItem->SetGain(m_Gain * m_ActionGain);
    247291            anItem->Play();
     292            AL_CHECK
    248293        }
    249294    }
    250295}
     
    255300        if (m_Enabled && (m_ActionGain > 0)) {
    256301            anItem->SetGain(m_Gain * groupGain);
    257302            anItem->Play();
     303            AL_CHECK
    258304        }
    259305    }
    260306}
     
    271317
    272318void CSoundManager::SetMusicItem(ISoundItem* anItem)
    273319{
     320    AL_CHECK
    274321    if (m_CurrentTune)
    275322    {
    276323        m_CurrentTune->FadeAndDelete(3.00);
     
    291338            anItem->StopAndDelete();
    292339        }
    293340    }
     341    AL_CHECK
    294342}
    295343
    296344void CSoundManager::SetAmbientItem(ISoundItem* anItem)
     
    312360            m_CurrentEnvirons->FadeToIn(m_Gain * m_AmbientGain, 2.00);
    313361        }
    314362    }
     363    AL_CHECK
    315364}
    316365
     366#endif
  • source/soundmanager/data/ogg.h

     
     1/* Copyright (C) 2012 Wildfire Games.
     2 * This file is part of 0 A.D.
     3 *
     4 * 0 A.D. is free software: you can redistribute it and/or modify
     5 * it under the terms of the GNU General Public License as published by
     6 * the Free Software Foundation, either version 2 of the License, or
     7 * (at your option) any later version.
     8 *
     9 * 0 A.D. is distributed in the hope that it will be useful,
     10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
     11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     12 * GNU General Public License for more details.
     13 *
     14 * You should have received a copy of the GNU General Public License
     15 * along with 0 A.D.  If not, see <http://www.gnu.org/licenses/>.
     16 */
     17#ifndef INCLUDED_OGG
     18#define INCLUDED_OGG
     19
     20#include "lib/config2.h"
     21
     22#if CONFIG2_AUDIO
     23
     24#include "lib/external_libraries/openal.h"
     25#include "lib/file/vfs/vfs.h"
     26
     27class OggStream
     28{
     29public:
     30    virtual ~OggStream() { }
     31    virtual ALenum Format() = 0;
     32    virtual ALsizei SamplingRate() = 0;
     33    virtual bool atFileEOF() = 0;
     34    virtual Status ResetFile() = 0;
     35
     36    /**
     37     * @return bytes read (<= size) or a (negative) Status
     38     **/
     39    virtual Status GetNextChunk(u8* buffer, size_t size) = 0;
     40};
     41
     42typedef shared_ptr<OggStream> OggStreamPtr;
     43
     44extern Status OpenOggStream(const OsPath& pathname, OggStreamPtr& stream);
     45
     46/**
     47 * A non-streaming OggStream (reading the whole file in advance)
     48 * that can cope with archived/compressed files.
     49 */
     50extern Status OpenOggNonstream(const PIVFS& vfs, const VfsPath& pathname, OggStreamPtr& stream);
     51
     52#endif  // CONFIG2_AUDIO
     53
     54#endif // INCLUDED_OGG
  • source/soundmanager/data/SoundData.cpp

     
    2020#include "SoundData.h"
    2121
    2222#include "OggData.h"
    23 #include "lib/file/vfs/vfs_util.h"
    24 #include "ps/Filesystem.h"
    2523
    2624#include <iostream>
    2725
     26#if CONFIG2_AUDIO
     27
    2828DataMap* CSoundData::sSoundData = NULL;
    2929
    3030CSoundData::CSoundData()
     
    9696    CSoundData* answer = NULL;
    9797    COggData* oggAnswer = new COggData();
    9898
    99     OsPath realPath;
    100     Status ret = g_VFS->GetRealPath(itemPath, realPath);
    101     if (ret == INFO::OK)
    102     {
    103         if (oggAnswer->InitOggFile(realPath.string().c_str()))
    104         {
    105             answer = oggAnswer;
    106         }
    107     }   
     99    if (oggAnswer->InitOggFile(itemPath))
     100        answer = oggAnswer;
     101
    108102    return answer;
    109103}
    110104
     
    140134    return &m_ALBuffer;
    141135}
    142136
     137#endif
     138 No newline at end of file
  • source/soundmanager/data/OggData.cpp

     
    2020#include "OggData.h"
    2121
    2222#include "soundmanager/SoundManager.h"
     23#include "ps/Filesystem.h"
    2324
    24 #include <wchar.h>
    25 #include <iostream>
     25#if CONFIG2_AUDIO
    2626
    2727COggData::COggData()
    2828{
     
    3232COggData::~COggData()
    3333{
    3434    alDeleteBuffers(m_BuffersUsed, m_Buffer);
    35     ov_clear(&m_vf);
    3635}
    3736
    3837void COggData::SetFormatAndFreq(int form, ALsizei freq)
     
    4140    m_Frequency = freq;
    4241}
    4342
    44 bool COggData::InitOggFile(const wchar_t* fileLoc)
     43bool COggData::InitOggFile(const VfsPath& itemPath)
    4544{
    46     int buffersToStart = g_SoundManager->GetBufferCount();
    47     char nameH[300];
    48     sprintf(nameH, "%ls", fileLoc);
    49    
    50     FILE* f = fopen(nameH, "rb");
    51     m_current_section = 0;
    52     int err = ov_open_callbacks(f, &m_vf, NULL, 0, OV_CALLBACKS_DEFAULT);
    53     if (err < 0)
    54     {
    55         fprintf(stderr,"Input does not appear to be an Ogg bitstream :%d :%d.\n", err, ferror(f));
    56         return false;
    57     }
     45    int buffersToStart = g_SoundManager->GetBufferCount(); 
     46    OpenOggNonstream( g_VFS, itemPath, ogg);
    5847
    59     m_FileName = CStrW(fileLoc);
    60 
    6148    m_FileFinished = false;
    62     SetFormatAndFreq((m_vf.vi->channels == 1)? AL_FORMAT_MONO16 : AL_FORMAT_STEREO16 , (ALsizei)m_vf.vi->rate);
    6349
     50    SetFormatAndFreq(ogg->Format(), ogg->SamplingRate() );
     51
    6452    alGetError(); /* clear error */
    6553    alGenBuffers(buffersToStart, m_Buffer);
    6654   
     
    9684
    9785void COggData::ResetFile()
    9886{
    99     ov_time_seek(&m_vf, 0);
    100     m_current_section = 0;
     87    ogg->ResetFile();
    10188    m_FileFinished = false;
    10289}
    10390
     
    11097{
    11198    long bufferSize = g_SoundManager->GetBufferSize();
    11299   
    113     char* pcmout = new char[bufferSize + 5000];
     100    u8* pcmout = new u8[bufferSize + 5000];
    114101    int buffersWritten = 0;
    115102   
    116103    for (int i = 0; (i < count) && !m_FileFinished; i++)
    117104    {
    118         char* readDest = pcmout;
    119         long totalRet = 0;
    120         while (totalRet < bufferSize)
    121         {
    122             long ret=ov_read(&m_vf,readDest, 4096,0,2,1, &m_current_section);
    123             if (ret == 0)
    124             {
    125                 m_FileFinished=true;
    126                 break;
    127             }
    128             else if (ret < 0)
    129             {
    130                 /* error in the stream.  Not a problem, just reporting it in
    131                  case we (the app) cares.  In this case, we don't. */
    132             }
    133             else
    134             {
    135                 totalRet += ret;
    136                 readDest += ret;
    137             }
    138         }
     105        Status totalRet = ogg->GetNextChunk( pcmout, bufferSize);
     106        m_FileFinished = ogg->atFileEOF();
    139107        if (totalRet > 0)
    140108        {
    141109            buffersWritten++;
     
    157125    return m_Buffer;
    158126}
    159127
     128#endif
    160129
    161130
    162131
    163 
  • source/soundmanager/data/SoundData.h

     
    2020
    2121#include "lib/external_libraries/openal.h"
    2222#include "lib/file/vfs/vfs_path.h"
    23 #include "lib/os_path.h"
     23#include "lib/config2.h"
    2424
    2525#include <string>
    2626#include <map>
    2727
     28#if CONFIG2_AUDIO
     29
    2830class CSoundData;
    2931typedef std::map<std::wstring, CSoundData*> DataMap;
    3032
     
    6163
    6264};
    6365
     66#endif
    6467#endif // INCLUDED_SOUNDDATA_H
  • source/soundmanager/data/OggData.h

     
    2020
    2121#include "SoundData.h"
    2222#include "lib/external_libraries/openal.h"
    23 #include "vorbis/vorbisfile.h"
     23#include "ogg.h"
     24 #if CONFIG2_AUDIO
    2425
    2526class COggData : public CSoundData
    2627{
     
    3132    COggData();
    3233    virtual ~COggData();
    3334
    34     virtual bool InitOggFile(const wchar_t* fileLoc);
     35    virtual bool InitOggFile(const VfsPath& itemPath);
    3536    virtual bool IsFileFinished();
    3637    virtual bool IsOneShot();
    3738
     
    3940    virtual void ResetFile();
    4041
    4142protected:
    42     OggVorbis_File  m_vf;
    43     int m_current_section;
     43    OggStreamPtr  ogg;
     44//  int m_current_section;
    4445    bool m_FileFinished;
    4546    bool m_OneShot;
    4647    ALuint m_Buffer[100];
     
    5354    ALuint* GetBufferPtr();
    5455};
    5556
    56 
     57#endif
    5758#endif // INCLUDED_OGGDATA_H
  • source/soundmanager/data/ogg.cpp

     
     1/* Copyright (C) 2012 Wildfire Games.
     2 * This file is part of 0 A.D.
     3 *
     4 * 0 A.D. is free software: you can redistribute it and/or modify
     5 * it under the terms of the GNU General Public License as published by
     6 * the Free Software Foundation, either version 2 of the License, or
     7 * (at your option) any later version.
     8 *
     9 * 0 A.D. is distributed in the hope that it will be useful,
     10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
     11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     12 * GNU General Public License for more details.
     13 *
     14 * You should have received a copy of the GNU General Public License
     15 * along with 0 A.D.  If not, see <http://www.gnu.org/licenses/>.
     16 */
     17#include "precompiled.h"
     18#include "ogg.h"
     19
     20#if CONFIG2_AUDIO
     21
     22#include "lib/external_libraries/openal.h"
     23#include "lib/external_libraries/vorbis.h"
     24
     25#include "lib/byte_order.h"
     26#include "lib/file/io/io.h"
     27#include "lib/file/file_system.h"
     28
     29#include "lib/file/vfs/vfs_util.h"
     30#include "ps/Filesystem.h"
     31
     32
     33static Status LibErrorFromVorbis(int err)
     34{
     35    switch(err)
     36    {
     37    case 0:
     38        return INFO::OK;
     39    case OV_HOLE:
     40        return ERR::AGAIN;
     41    case OV_EREAD:
     42        return ERR::IO;
     43    case OV_EFAULT:
     44        return ERR::LOGIC;
     45    case OV_EIMPL:
     46        return ERR::NOT_SUPPORTED;
     47    case OV_EINVAL:
     48        return ERR::INVALID_PARAM;
     49    case OV_ENOTVORBIS:
     50        return ERR::NOT_SUPPORTED;
     51    case OV_EBADHEADER:
     52        return ERR::CORRUPTED;
     53    case OV_EVERSION:
     54        return ERR::INVALID_VERSION;
     55    case OV_ENOTAUDIO:
     56        return ERR::_1;
     57    case OV_EBADPACKET:
     58        return ERR::_2;
     59    case OV_EBADLINK:
     60        return ERR::_3;
     61    case OV_ENOSEEK:
     62        return ERR::_4;
     63    default:
     64        return ERR::FAIL;
     65    }
     66}
     67
     68
     69//-----------------------------------------------------------------------------
     70
     71class VorbisFileAdapter
     72{
     73public:
     74    VorbisFileAdapter(const PFile& openedFile)
     75        : file(openedFile)
     76        , size(FileSize(openedFile->Pathname()))
     77        , offset(0)
     78    {
     79    }
     80
     81    static size_t Read(void* bufferToFill, size_t itemSize, size_t numItems, void* context)
     82    {
     83        VorbisFileAdapter* adapter = (VorbisFileAdapter*)context;
     84        const off_t sizeRequested = numItems*itemSize;
     85        const off_t sizeRemaining = adapter->size - adapter->offset;
     86        const size_t sizeToRead = (size_t)std::min(sizeRequested, sizeRemaining);
     87
     88        io::Operation op(*adapter->file.get(), bufferToFill, sizeToRead, adapter->offset);
     89        if(io::Run(op) == INFO::OK)
     90        {
     91            adapter->offset += sizeToRead;
     92            return sizeToRead;
     93        }
     94
     95        errno = EIO;
     96        return 0;
     97    }
     98
     99    static int Seek(void* context, ogg_int64_t offset, int whence)
     100    {
     101        VorbisFileAdapter* adapter = (VorbisFileAdapter*)context;
     102
     103        off_t origin = 0;
     104        switch(whence)
     105        {
     106        case SEEK_SET:
     107            origin = 0;
     108            break;
     109        case SEEK_CUR:
     110            origin = adapter->offset;
     111            break;
     112        case SEEK_END:
     113            origin = adapter->size+1;
     114            break;
     115            NODEFAULT;
     116        }
     117
     118        adapter->offset = Clamp(off_t(origin+offset), off_t(0), adapter->size);
     119        return 0;
     120    }
     121
     122    static int Close(void* context)
     123    {
     124        VorbisFileAdapter* adapter = (VorbisFileAdapter*)context;
     125        adapter->file.reset();
     126        return 0;   // return value is ignored
     127    }
     128
     129    static long Tell(void* context)
     130    {
     131        VorbisFileAdapter* adapter = (VorbisFileAdapter*)context;
     132        return adapter->offset;
     133    }
     134
     135private:
     136    PFile file;
     137    off_t size;
     138    off_t offset;
     139};
     140
     141//-----------------------------------------------------------------------------
     142
     143class VorbisBufferAdapter
     144{
     145public:
     146    VorbisBufferAdapter(const shared_ptr<u8>& buffer, size_t size)
     147        : buffer(buffer)
     148        , size(size)
     149        , offset(0)
     150    {
     151    }
     152
     153    static size_t Read(void* bufferToFill, size_t itemSize, size_t numItems, void* context)
     154    {
     155        VorbisBufferAdapter* adapter = (VorbisBufferAdapter*)context;
     156        const off_t sizeRequested = numItems*itemSize;
     157        const off_t sizeRemaining = adapter->size - adapter->offset;
     158        const size_t sizeToRead = (size_t)std::min(sizeRequested, sizeRemaining);
     159
     160        memcpy(bufferToFill, adapter->buffer.get() + adapter->offset, sizeToRead);
     161
     162        adapter->offset += sizeToRead;
     163        return sizeToRead;
     164    }
     165
     166    static int Seek(void* context, ogg_int64_t offset, int whence)
     167    {
     168        VorbisBufferAdapter* adapter = (VorbisBufferAdapter*)context;
     169
     170        off_t origin = 0;
     171        switch(whence)
     172        {
     173        case SEEK_SET:
     174            origin = 0;
     175            break;
     176        case SEEK_CUR:
     177            origin = adapter->offset;
     178            break;
     179        case SEEK_END:
     180            origin = adapter->size+1;
     181            break;
     182            NODEFAULT;
     183        }
     184
     185        adapter->offset = Clamp(off_t(origin+offset), off_t(0), adapter->size);
     186        return 0;
     187    }
     188
     189    static int Close(void* context)
     190    {
     191        VorbisBufferAdapter* adapter = (VorbisBufferAdapter*)context;
     192        adapter->buffer.reset();
     193        return 0;   // return value is ignored
     194    }
     195
     196    static long Tell(void* context)
     197    {
     198        VorbisBufferAdapter* adapter = (VorbisBufferAdapter*)context;
     199        return adapter->offset;
     200    }
     201
     202private:
     203    shared_ptr<u8> buffer;
     204    off_t size;
     205    off_t offset;
     206};
     207
     208
     209//-----------------------------------------------------------------------------
     210
     211template <typename Adapter>
     212class OggStreamImpl : public OggStream
     213{
     214public:
     215    OggStreamImpl(const Adapter& adapter)
     216        : adapter(adapter)
     217    {
     218        m_fileEOF = false;
     219    }
     220
     221    Status Open()
     222    {
     223        ov_callbacks callbacks;
     224        callbacks.read_func = Adapter::Read;
     225        callbacks.close_func = Adapter::Close;
     226        callbacks.seek_func = Adapter::Seek;
     227        callbacks.tell_func = Adapter::Tell;
     228        const int ret = ov_open_callbacks(&adapter, &vf, 0, 0, callbacks);
     229        if(ret != 0)
     230            WARN_RETURN(LibErrorFromVorbis(ret));
     231
     232        const int link = -1;    // retrieve info for current bitstream
     233        info = ov_info(&vf, link);
     234        if(!info)
     235            WARN_RETURN(ERR::INVALID_HANDLE);
     236
     237        return INFO::OK;
     238    }
     239
     240    virtual ALenum Format()
     241    {
     242        return (info->channels == 1)? AL_FORMAT_MONO16 : AL_FORMAT_STEREO16;
     243    }
     244
     245    virtual ALsizei SamplingRate()
     246    {
     247        return info->rate;
     248    }
     249    virtual bool atFileEOF()
     250    {
     251        return m_fileEOF;
     252    }
     253
     254    virtual Status ResetFile()
     255    {
     256        ov_time_seek( &vf, 0 );
     257        m_fileEOF = false;
     258    }
     259
     260    virtual Status GetNextChunk(u8* buffer, size_t size)
     261    {
     262        // we may have to call ov_read multiple times because it
     263        // treats the buffer size "as a limit and not a request"
     264        size_t bytesRead = 0;
     265        for(;;)
     266        {
     267            const int isBigEndian = (BYTE_ORDER == BIG_ENDIAN);
     268            const int wordSize = sizeof(i16);
     269            const int isSigned = 1;
     270            int bitstream;  // unused
     271            const int ret = ov_read(&vf, (char*)buffer+bytesRead, int(size-bytesRead), isBigEndian, wordSize, isSigned, &bitstream);
     272            if(ret == 0) {  // EOF
     273                m_fileEOF = true;
     274                return (Status)bytesRead;
     275            }
     276            else if(ret < 0)
     277                WARN_RETURN(LibErrorFromVorbis(ret));
     278            else    // success
     279            {
     280                bytesRead += ret;
     281                if(bytesRead == size)
     282                    return (Status)bytesRead;
     283            }
     284        }
     285    }
     286
     287private:
     288    Adapter adapter;
     289    OggVorbis_File vf;
     290    vorbis_info* info;
     291    bool m_fileEOF;
     292};
     293
     294
     295//-----------------------------------------------------------------------------
     296
     297Status OpenOggStream(const OsPath& pathname, OggStreamPtr& stream)
     298{
     299    PFile file(new File);
     300    RETURN_STATUS_IF_ERR(file->Open(pathname, L'r'));
     301
     302    shared_ptr<OggStreamImpl<VorbisFileAdapter> > tmp(new OggStreamImpl<VorbisFileAdapter>(VorbisFileAdapter(file)));
     303    RETURN_STATUS_IF_ERR(tmp->Open());
     304    stream = tmp;
     305    return INFO::OK;
     306}
     307
     308Status OpenOggNonstream(const PIVFS& vfs, const VfsPath& pathname, OggStreamPtr& stream)
     309{
     310    shared_ptr<u8> contents;
     311    size_t size;
     312    RETURN_STATUS_IF_ERR(vfs->LoadFile(pathname, contents, size));
     313
     314    shared_ptr<OggStreamImpl<VorbisBufferAdapter> > tmp(new OggStreamImpl<VorbisBufferAdapter>(VorbisBufferAdapter(contents, size)));
     315    RETURN_STATUS_IF_ERR(tmp->Open());
     316    stream = tmp;
     317    return INFO::OK;
     318}
     319
     320#endif  // CONFIG2_AUDIO
  • source/soundmanager/SoundManager.h

     
    2323
    2424#include <vector>
    2525#include <map>
     26#if CONFIG2_AUDIO
    2627
     28
     29#define AL_CHECK CSoundManager::al_check(__func__, __LINE__);
     30
    2731typedef std::vector<ISoundItem*> ItemsList;
    2832
    2933
     
    4549    long m_BufferSize;
    4650    int m_BufferCount;
    4751    bool m_MusicEnabled;
     52    bool m_SoundEnabled;
    4853
    4954public:
    5055    CSoundManager();
     
    5358    ISoundItem* LoadItem(const VfsPath& itemPath);
    5459
    5560    static void ScriptingInit();
     61    static void CreateSoundManager();
     62    static void SetEnabled(bool doEnable);
     63   
     64    static void al_ReportError(ALenum err, const char* caller, int line);
     65    static void al_check(const char* caller, int line);
    5666
    5767    void SetMusicEnabled (bool isEnabled);
     68    void setSoundEnabled( bool enabled );
    5869
    5970    ISoundItem* ItemFromWAV(VfsPath& fname);
    6071    ISoundItem* ItemFromOgg(VfsPath& fname);
     
    7889    void SetAmbientGain(float gain);
    7990    void SetActionGain(float gain);
    8091   
    81     void SetEnabled(bool doEnable);
    8292protected:
    8393    void InitListener();
    8494    virtual Status AlcInit();
    8595
    8696};
    8797
     98#else
     99class CSoundManager
     100{
     101public:
     102    static void ScriptingInit();
     103    static void CreateSoundManager();
     104    static void SetEnabled(bool doEnable);
     105};
     106#endif
     107
     108
    88109extern CSoundManager*  g_SoundManager;
    89110
    90 
    91111#endif // INCLUDED_SOUNDMANAGER_H
  • source/soundmanager/js/SoundGroup.cpp

     
    4141
    4242#include <algorithm>
    4343
     44
    4445extern CGame *g_Game;
    4546
    4647#define PI 3.14126f
     
    155156
    156157void CSoundGroup::UploadPropertiesAndPlay(int theIndex, const CVector3D& position)
    157158{
    158     bool    isOnscreen;
    159     ALfloat initialRolllOff = 0.02f;
    160     ALfloat itemRollOff = initialRolllOff;
     159#if CONFIG2_AUDIO
     160    if ( g_SoundManager ) {
     161        bool    isOnscreen;
     162        ALfloat initialRolllOff = 0.02f;
     163        ALfloat itemRollOff = initialRolllOff;
    161164
    162     float   offSet = RadiansOffCenter(position, isOnscreen, itemRollOff);
     165        float   offSet = RadiansOffCenter(position, isOnscreen, itemRollOff);
    163166
    164     if (isOnscreen || TestFlag(eDistanceless) || TestFlag(eOmnipresent))
    165     {
    166         if (snd_group.size() == 0)
    167             Reload();
     167        if (isOnscreen || TestFlag(eDistanceless) || TestFlag(eOmnipresent))
     168        {
     169            if (snd_group.size() == 0)
     170                Reload();
    168171
    169         ISoundItem* hSound = snd_group[theIndex];
    170         CVector3D origin = g_Game->GetView()->GetCamera()->GetOrientation().GetTranslation();
    171         float sndDist = origin.Y;
     172            ISoundItem* hSound = snd_group[theIndex];
     173            CVector3D origin = g_Game->GetView()->GetCamera()->GetOrientation().GetTranslation();
     174            float sndDist = origin.Y;
    172175
    173         if (!TestFlag(eOmnipresent))
    174         {
    175             hSound->SetLocation(CVector3D((sndDist * sin(offSet)), 0, sndDist * cos(offSet)));
    176             if (TestFlag(eDistanceless))
    177                 hSound->SetRollOff(initialRolllOff);
     176            if (!TestFlag(eOmnipresent))
     177            {
     178                hSound->SetLocation(CVector3D((sndDist * sin(offSet)), 0, sndDist * cos(offSet)));
     179                if (TestFlag(eDistanceless))
     180                    hSound->SetRollOff(initialRolllOff);
     181                else
     182                    hSound->SetRollOff(itemRollOff);
     183            }
     184
     185            if (TestFlag(eRandPitch))
     186                hSound->SetPitch(RandFloat(m_PitchLower, m_PitchUpper));
    178187            else
    179                 hSound->SetRollOff(itemRollOff);
    180         }
     188                hSound->SetPitch(m_Pitch);
    181189
    182         if (TestFlag(eRandPitch))
    183             hSound->SetPitch(RandFloat(m_PitchLower, m_PitchUpper));
    184         else
    185             hSound->SetPitch(m_Pitch);
     190            ALfloat theGain = m_Gain;
     191            if (TestFlag(eRandGain))
     192                theGain = RandFloat(m_GainLower, m_GainUpper);
    186193
    187         ALfloat theGain = m_Gain;
    188         if (TestFlag(eRandGain))
    189             theGain = RandFloat(m_GainLower, m_GainUpper);
     194            hSound->SetCone(m_ConeInnerAngle, m_ConeOuterAngle, m_ConeOuterGain);
    190195
    191         hSound->SetCone(m_ConeInnerAngle, m_ConeOuterAngle, m_ConeOuterGain);
    192 
    193         g_SoundManager->PlayGroupItem(hSound, theGain);
     196            g_SoundManager->PlayGroupItem(hSound, theGain);
     197        }
    194198    }
     199#endif
    195200}
    196201
    197202
     
    216221{
    217222    m_index = 0; // reset our index
    218223
     224#if CONFIG2_AUDIO
    219225    snd_group.clear();
    220226
    221     for (size_t i = 0; i < filenames.size(); i++)
    222     {
    223         VfsPath  thePath =  m_filepath/filenames[i];
    224         ISoundItem* temp = g_SoundManager->LoadItem(thePath);
     227    if ( g_SoundManager ) {
     228        for (size_t i = 0; i < filenames.size(); i++)
     229        {
     230            VfsPath  thePath =  m_filepath/filenames[i];
     231            ISoundItem* temp = g_SoundManager->LoadItem(thePath);
    225232
    226         if (temp == NULL)
    227             HandleError(L"error loading sound", thePath, ERR::FAIL);
    228         else
    229             snd_group.push_back(temp);
     233            if (temp == NULL)
     234                HandleError(L"error loading sound", thePath, ERR::FAIL);
     235            else
     236                snd_group.push_back(temp);
     237        }
     238
     239        if (TestFlag(eRandOrder))
     240            random_shuffle(snd_group.begin(), snd_group.end());
    230241    }
    231 
    232     if (TestFlag(eRandOrder))
    233         random_shuffle(snd_group.begin(), snd_group.end());
     242#endif
    234243}
    235244
    236245void CSoundGroup::ReleaseGroup()
    237246{
     247#if CONFIG2_AUDIO
    238248    for (size_t i = 0; i < snd_group.size(); i++)
    239249    {
    240250        snd_group[i]->FadeAndDelete(0.2);
    241251    }
    242252    snd_group.clear();
     253#endif
    243254}
    244255
    245256void CSoundGroup::Update(float UNUSED(TimeSinceLastFrame))
  • source/soundmanager/js/SoundPlayer.cpp

     
    3434
    3535bool JSoundPlayer::StartMusic(JSContext* UNUSED(cx), uintN UNUSED(argc), jsval* UNUSED(argv))
    3636{
    37     g_SoundManager->SetMusicEnabled(true);
    38 
     37#if CONFIG2_AUDIO
     38    if ( g_SoundManager )
     39        g_SoundManager->SetMusicEnabled(true);
     40#endif
    3941    return true;
    4042}
    4143
    4244// request the sound be played until free() is called. returns immediately.
    4345bool JSoundPlayer::StopMusic(JSContext* UNUSED(cx), uintN UNUSED(argc), jsval* UNUSED(argv))
    4446{
    45     g_SoundManager->SetMusicEnabled(false);
     47#if CONFIG2_AUDIO
     48    if ( g_SoundManager )
     49        g_SoundManager->SetMusicEnabled(false);
     50#endif
    4651
    4752    return true;
    4853}
     
    6166    JSoundPlayer* newObject = new JSoundPlayer();
    6267    newObject->m_EngineOwned = false;
    6368    JS_SET_RVAL(cx, vp, OBJECT_TO_JSVAL(newObject->GetScript()));
    64    
     69
    6570    return JS_TRUE;
    6671}
    6772
  • source/soundmanager/js/AmbientSound.cpp

     
    3232// start playing the sound, all ambient sounds loop
    3333bool JAmbientSound::Play(JSContext* UNUSED(cx), uintN UNUSED(argc), jsval* UNUSED(argv))
    3434{
    35     ISoundItem* aSnd = g_SoundManager->LoadItem(m_FileName);
     35 #if CONFIG2_AUDIO
     36    if ( g_SoundManager ) {
     37        ISoundItem* aSnd = g_SoundManager->LoadItem(m_FileName);
    3638
    37     if (aSnd)
    38         aSnd->PlayAsAmbient();
    39     else
    40         debug_printf(L"sound item could not be loaded to play: %ls\n", m_FileName.string().c_str());
    41 
     39        if (aSnd)
     40            aSnd->PlayAsAmbient();
     41        else
     42            debug_printf(L"sound item could not be loaded to play: %ls\n", m_FileName.string().c_str());
     43    }
     44#endif
    4245    return true;
    4346}
    4447
    4548// start playing the sound, all ambient sounds loop
    4649bool JAmbientSound::Loop(JSContext* UNUSED(cx), uintN UNUSED(argc), jsval* UNUSED(argv))
    4750{
    48     ISoundItem* aSnd = g_SoundManager->LoadItem(m_FileName);
     51 #if CONFIG2_AUDIO
     52    if ( g_SoundManager ) {
     53        ISoundItem* aSnd = g_SoundManager->LoadItem(m_FileName);
    4954
    50     if (aSnd)
    51         aSnd->PlayAsAmbient();
    52     else
    53         debug_printf(L"sound item could not be loaded to loop: %ls\n", m_FileName.string().c_str());
    54 
     55        if (aSnd)
     56            aSnd->PlayAsAmbient();
     57        else
     58            debug_printf(L"sound item could not be loaded to loop: %ls\n", m_FileName.string().c_str());
     59    }
     60#endif
    5561    return true;
    5662}
    5763bool JAmbientSound::Free(JSContext* UNUSED(cx), uintN UNUSED(argc), jsval* UNUSED(argv))
    5864{
    59     g_SoundManager->SetAmbientItem(0L);
     65 #if CONFIG2_AUDIO
     66    if ( g_SoundManager )
     67        g_SoundManager->SetAmbientItem(0L);
     68#endif
    6069
    6170    return true;
    6271}
     
    9099    CStrW filename;
    91100    if (! ToPrimitive<CStrW>(cx, JS_ARGV(cx, vp)[0], filename))
    92101        return JS_FALSE;
    93    
     102
    94103    JAmbientSound* newObject = new JAmbientSound(filename);
    95104    newObject->m_EngineOwned = false;
    96105
    97106    JS_SET_RVAL(cx, vp, OBJECT_TO_JSVAL(newObject->GetScript()));
    98    
     107
    99108    return JS_TRUE;
    100109}
     110
  • source/soundmanager/js/Sound.cpp

     
    2727
    2828JSound::JSound(const VfsPath& pathname)
    2929{
    30     m_SndItem = g_SoundManager->LoadItem(pathname);
     30#if CONFIG2_AUDIO
     31    if ( g_SoundManager )
     32        m_SndItem = g_SoundManager->LoadItem(pathname);
     33#endif
    3134}
    3235
    3336JSound::~JSound()
    3437{
     38#if CONFIG2_AUDIO
    3539    if (m_SndItem)
    3640    {
    3741        m_SndItem->FadeAndDelete(0.2);
    3842        m_SndItem = 0;
    3943    }
     44#endif
    4045}
    4146
    4247bool JSound::ClearSoundItem()
    4348{
     49#if CONFIG2_AUDIO
    4450    m_SndItem = 0L;
     51#endif
    4552    return true;
    4653}
    4754
    4855bool JSound::SetGain(JSContext* cx, uintN UNUSED(argc), jsval* argv)
    4956{
     57#if CONFIG2_AUDIO
    5058    if (! m_SndItem)
    5159        return false;
    5260
     
    5462    if (! ToPrimitive<float>(cx, argv[0], gain))
    5563        return false;
    5664   
    57     m_SndItem->SetGain(gain);   
     65    m_SndItem->SetGain(gain);
     66#endif
    5867    return true;
    5968}
    6069
    6170bool JSound::SetPitch(JSContext* cx, uintN UNUSED(argc), jsval* argv)
    6271{
     72#if CONFIG2_AUDIO
    6373    if (! m_SndItem)
    6474        return false;
    6575
     
    6878        return false;
    6979   
    7080    m_SndItem->SetPitch(pitch);
     81#endif
    7182    return true;
    7283}
    7384
    7485bool JSound::SetPosition(JSContext* cx, uintN argc, jsval* argv)
    7586{
     87#if CONFIG2_AUDIO
    7688    if (! m_SndItem)
    7789        return false;
    7890   
     
    8496        return false;
    8597
    8698    m_SndItem->SetLocation(pos);
    87    
     99#endif
    88100    return true;
    89101}
    90102
    91103
    92104bool JSound::Fade(JSContext* cx, uintN UNUSED(argc), jsval* argv)
    93105{
     106#if CONFIG2_AUDIO
    94107    if (! m_SndItem)
    95108        return false;
    96109   
     
    104117   
    105118    m_SndItem->SetGain(initial_gain);
    106119    m_SndItem->FadeToIn(final_gain, length);
    107 
     120#endif
    108121    return true;
    109122}
    110123
    111124bool JSound::Play(JSContext* UNUSED(cx), uintN UNUSED(argc), jsval* UNUSED(argv))
    112125{
     126#if CONFIG2_AUDIO
    113127    if (! m_SndItem)
    114128        return false;
    115129
    116130    m_SndItem->Play();
    117 
     131#endif
    118132    return true;
    119133}
    120134
    121135bool JSound::Loop(JSContext* UNUSED(cx), uintN UNUSED(argc), jsval* UNUSED(argv))
    122136{
     137#if CONFIG2_AUDIO
    123138    if (! m_SndItem)
    124139        return false;
    125140
    126141    m_SndItem->PlayLoop();
    127 
     142#endif
    128143    return true;
    129144}
    130145
    131146bool JSound::Free(JSContext* UNUSED(cx), uintN UNUSED(argc), jsval* UNUSED(argv))
    132147{
     148#if CONFIG2_AUDIO
    133149    if (m_SndItem)
    134150    {
    135151        m_SndItem->FadeAndDelete(0.2);
    136152        m_SndItem = 0;
    137153    }
    138 
     154#endif
    139155    return true;
    140156}
    141157
     
    155171
    156172CStr JSound::ToString(JSContext* UNUSED(cx), uintN UNUSED(argc), jsval* UNUSED(argv))
    157173{
     174#if CONFIG2_AUDIO
    158175    return "[object Sound: " + (m_SndItem ? m_SndItem->GetName() : "(null)") + "]";
     176#else
     177    return "[object Sound: audio disabled]";
     178#endif
    159179}
    160180
    161181JSBool JSound::Construct(JSContext* cx, uintN UNUSED(argc), jsval* vp)
     
    169189    JSound* newObject = new JSound(filename);
    170190    newObject->m_EngineOwned = false;
    171191    JS_SET_RVAL(cx, vp, OBJECT_TO_JSVAL(newObject->GetScript()));
    172    
     192
    173193    return JS_TRUE;
    174194}
  • source/soundmanager/js/MusicSound.cpp

     
    3131
    3232bool JMusicSound::Play(JSContext* UNUSED(cx), uintN UNUSED(argc), jsval* UNUSED(argv))
    3333{
    34     ISoundItem* aSnd = g_SoundManager->LoadItem(m_FileName);
    35     if (aSnd != NULL)
    36         aSnd->PlayAsMusic();
    37 
     34 #if CONFIG2_AUDIO
     35    if ( g_SoundManager ) {
     36        ISoundItem* aSnd = g_SoundManager->LoadItem(m_FileName);
     37        if (aSnd != NULL)
     38            aSnd->PlayAsMusic();
     39    }
     40#endif
    3841    return true;
    3942}
    4043
    4144// request the sound be played until free() is called. returns immediately.
    4245bool JMusicSound::Loop(JSContext* UNUSED(cx), uintN UNUSED(argc), jsval* UNUSED(argv))
    4346{
    44     ISoundItem* aSnd = g_SoundManager->LoadItem(m_FileName);
    45     if (aSnd != NULL)
    46         aSnd->PlayAsMusic();
    47 
     47 #if CONFIG2_AUDIO
     48    if ( g_SoundManager ) {
     49        ISoundItem* aSnd = g_SoundManager->LoadItem(m_FileName);
     50        if (aSnd != NULL)
     51            aSnd->PlayAsMusic();
     52    }
     53#endif
    4854    return true;
    4955}
    5056
     
    7581    JMusicSound* newObject = new JMusicSound(filename);
    7682    newObject->m_EngineOwned = false;
    7783    JS_SET_RVAL(cx, vp, OBJECT_TO_JSVAL(newObject->GetScript()));
    78    
     84
    7985    return JS_TRUE;
    8086}
     87
     88
  • source/soundmanager/js/SoundGroup.h

     
    5353#define INCLUDED_SOUNDGROUP_H
    5454
    5555#include "lib/file/vfs/vfs_path.h"
     56#include "lib/config2.h"
    5657
    5758#include <vector>
    5859
    5960class CVector3D;
     61
     62
    6063class ISoundItem;
    6164
    6265enum eSndGrpFlags
     
    109112
    110113    size_t m_index;  // index of the next sound to play
    111114       
     115#if CONFIG2_AUDIO
    112116    std::vector<ISoundItem*> snd_group;  // we store the handles so we can load now and play later
     117#endif
    113118    std::vector<std::wstring> filenames; // we need the filenames so we can reload when necessary.
    114119
    115120    VfsPath m_filepath; // the file path for the list of sound file resources
  • source/soundmanager/js/Sound.h

     
    3535#include "scripting/ScriptableObject.h"
    3636#include "soundmanager/items/ISoundItem.h"
    3737
     38
    3839class JSound : public CJSObject<JSound>
    3940{
    4041public:
     
    6162    static void ScriptingInit();
    6263
    6364protected:
     65#if CONFIG2_AUDIO
    6466    ISoundItem* m_SndItem;
     67#endif
    6568};
    6669
    6770#endif  // #ifndef INCLUDED_JSOUND
  • source/simulation2/components/CCmpSoundManager.cpp

     
    3636
    3737    DEFAULT_COMPONENT_ALLOCATOR(SoundManager)
    3838
     39#if CONFIG2_AUDIO
    3940    std::map<std::wstring, CSoundGroup*> m_SoundGroups;
     41#endif
    4042
    4143    static std::string GetSchema()
    4244    {
     
    4951
    5052    virtual void Deinit()
    5153    {
     54#if CONFIG2_AUDIO
    5255        for (std::map<std::wstring, CSoundGroup*>::iterator it = m_SoundGroups.begin(); it != m_SoundGroups.end(); ++it)
    5356            delete it->second;
    5457        m_SoundGroups.clear();
     58#endif
    5559    }
    5660
    5761    virtual void Serialize(ISerializer& UNUSED(serialize))
     
    7680            // or on some other timer?
    7781            const CMessageUpdate& msgData = static_cast<const CMessageUpdate&> (msg);
    7882            float t = msgData.turnLength.ToFloat();
     83#if CONFIG2_AUDIO
    7984            for (std::map<std::wstring, CSoundGroup*>::iterator it = m_SoundGroups.begin(); it != m_SoundGroups.end(); ++it)
    8085                if (it->second)
    8186                    it->second->Update(t);
     87#endif
    8288            break;
    8389        }
    8490        }
     
    8692
    8793    virtual void PlaySoundGroup(std::wstring name, entity_id_t source)
    8894    {
     95#if CONFIG2_AUDIO
    8996        // Make sure the sound group is loaded
    9097        CSoundGroup* group;
    9198        if (m_SoundGroups.find(name) == m_SoundGroups.end())
     
    127134
    128135            group->PlayNext(sourcePos);
    129136        }
     137#endif
    130138    }
    131139};
    132140
    133141REGISTER_COMPONENT_TYPE(SoundManager)
     142
     143