📄 fmodsoundmodule.cpp
字号:
#include "OgrePrerequisites.h"
#include "CEGUI.h"
#include "FmodSoundModule.h"
#include "OgreTimer.h"
#include "OgreLogManager.h"
#include "OgreStringConverter.h"
#include "OgrePlatformManager.h"
namespace Ogre
{
AudioClip::AudioClip() : mStream(0), mPaused(false), mChannel(0)
{
pauseTimeAdjustmentStop = 0;
pauseTimeAdjustmentTotal = 0;
mTimer = mTimer = PlatformManager::getSingleton().createTimer();
if( !mTimer )
throw;
}
AudioClip::~AudioClip()
{
if(mTimer)
PlatformManager::getSingleton().destroyTimer( mTimer );
mTimer = 0;
}
void AudioClip::setAudioStreamPause( bool pause )
{
if( pause == mPaused )
return;
FSOUND_SetPaused( mChannel, pause );
if( pause == true )
pauseTimeAdjustmentStop = getAudioStreamTime();
else
pauseTimeAdjustmentTotal += getAudioStreamTime() - pauseTimeAdjustmentStop;
mPaused = pause;
}
float AudioClip::getAudioStreamTime()
{
//xxx: fix timer imprecision. ie, acount for blank audio,
//and buffer lag
return mTimer->getMilliseconds() - pauseTimeAdjustmentTotal;
}
//--------------------------------------------------------------------------//
bool AudioClip::openAudioStream( unsigned int sampleRate,
eAudioSampleFormat sf,
unsigned int bufferSize,
unsigned int flags )
{
unsigned int mode = FSOUND_SIGNED;
if( mStream )
return false;
if( sf == SF_16Bit )
mode |= FSOUND_16BITS;
if( flags & THEORA_AUDIOSTREAM_STEREO )
mode |= FSOUND_STEREO;
else
mode |= FSOUND_MONO;
mStream = FSOUND_Stream_Create( CustomStreamCallBack,
MY_FMOD_BUFF_SIZE,
mode,
sampleRate,
this);
if( mStream )
return true;
else
return false;
}
//--------------------------------------------------------------------------//
bool AudioClip::closeAudioStream()
{
if( mStream )
{
FSOUND_Stream_Stop( mStream );
FSOUND_Stream_Close( mStream );
}
mChannel = 0;
mPaused = true;
mStream = 0;
return true;
}
//--------------------------------------------------------------------------//
bool AudioClip::startAudioStream()
{
mChannel = FSOUND_Stream_Play( FSOUND_FREE, mStream );
mPaused = false;
mTimer->reset();
if( mChannel > 0 )
return true;
else
return false;
}
//--------------------------------------------------------------------------//
signed char F_CALLBACKAPI AudioClip::CustomStreamCallBack(
FSOUND_STREAM *stream, void *buff, int len, void * param )
{
AudioClip *pAudio = (AudioClip*)param;
int numRead = pAudio->outFIFO.read( buff, len );
// Zero out remainder of buffer if we run out of data
if( numRead < len )
{
int temp = len - numRead;
memset((char *)buff+numRead, 0, temp );
}
return 1;
}
//---------------------------------------------------------------------------//
SoundManager::SoundManager()
{
if ( FSOUND_GetVersion() < FMOD_VERSION )
throw;
FSOUND_SetOutput(FSOUND_OUTPUT_DSOUND);
FSOUND_SetDriver(0); //Primary
FSOUND_SetSpeakerMode( FSOUND_SPEAKERMODE_SURROUND );
FSOUND_SetMixer(FSOUND_MIXER_AUTODETECT);
if (!FSOUND_Init(44100, 32, 0))
throw;
mAudioClip = 0;
createAudioControlMenu();
}
//---------------------------------------------------------------------------//
SoundManager::~SoundManager()
{
delete mAudioClip;
FSOUND_Close();
}
AudioClip* SoundManager::getAudioClip()
{
assert( !mAudioClip );
mAudioClip = new AudioClip;
return mAudioClip;
}
void SoundManager::destroyAudioClip()
{
delete mAudioClip;
mAudioClip = 0;
}
void SoundManager::createAudioControlMenu()
{
using namespace CEGUI;
WindowManager& winMgr = WindowManager::getSingleton();
Window* root = winMgr.getWindow((utf8*)"root_wnd");
FrameWindow* fwnd1 = (FrameWindow*)winMgr.createWindow((utf8*)"Taharez Frame Window", (utf8*)"Audio/VolumeControl");
root->addChildWindow(fwnd1);
fwnd1->setMinimumSize(Size(0.01f, 0.01f));
fwnd1->setMaximumSize(Size(1.0f, 1.0f));
fwnd1->setPosition(Point(0.0f, 0.5f));
fwnd1->setSize(Size(0.2f, 0.2f));
fwnd1->setText((utf8*)"Volume Mixer");
fwnd1->setCloseButtonEnabled(false);
Slider* sldr;
for( int i = 1; i < 5; i++ )
{
CEGUI::String temp = "Audio/VolumeControl/Slider1" + (i);
sldr = (Slider*)winMgr.createWindow((utf8*)"Taharez Slider", temp);
fwnd1->addChildWindow(sldr);
sldr->setMinimumSize(Size(0.01f, 0.1f));
sldr->setMaximumSize(Size(0.03f, 1.0f));
sldr->setPosition(Point(0.05f + (0.15f * i), 0.25));
sldr->setSize(Size(0.1f, 0.28f));
sldr->setCurrentValue(1.0f);
}
}
} //end namespace OGRE
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -