soundinstance.cpp

来自「游戏音频程序设计-Beginning.Game.Audio.Programmin」· C++ 代码 · 共 72 行

CPP
72
字号
// SoundInstance.cpp: implementation of the CSoundInstance class.
//
//////////////////////////////////////////////////////////////////////

#include "SoundInstance.h"
#include "Sound.h"
#include "AudioManager.h"

#include <assert.h>

namespace AudioEngine {

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CSoundInstance::CSoundInstance()
{
  m_SegmentState = NULL;
  m_AudioPath = NULL;
  m_Valid = false;
}

CSoundInstance::~CSoundInstance()
{
  UnInit();
}

void CSoundInstance::Init(CSound *sound, IDirectMusicAudioPath8 *path)
{
  HRESULT hr;

  m_AudioPath = path;
  m_Valid = true;

  // set volume based on sound's volume
  SetVolume(sound->GetVolume());

  // now that everything's set, go ahead and play it.
  hr = GetAudioManager()->GetPerformance()->PlaySegmentEx(sound->GetSegment(), NULL, NULL,
    DMUS_SEGF_SECONDARY, 0, 
    (IDirectMusicSegmentState **)&m_SegmentState, 
    NULL, path);
  ThrowIfFailed(hr, "CSoundInstance::Init: PlaySegmentEx failed.");
}

void CSoundInstance::UnInit()
{
  SAFE_RELEASE(m_SegmentState);
  SAFE_RELEASE(m_AudioPath);
  m_Valid = false;
}

void CSoundInstance::SetVolume(const CVolume &vol, int fadeoverms)
{
  assert(IsValid()); if (!IsValid()) { return; }
  m_AudioPath->SetVolume(vol.ToDirectMusic(), fadeoverms);
}

bool CSoundInstance::IsPlaying()
{
  assert(IsValid()); if (!IsValid()) { return(false); }
  return(GetAudioManager()->GetPerformance()->IsPlaying(NULL, m_SegmentState) == S_OK);
}

bool CSoundInstance::Stop(MUSIC_TIME time, DWORD flags)
{
  return(SUCCEEDED(GetAudioManager()->GetPerformance()->Stop(NULL, m_SegmentState, time, flags)));
}


}; // namespace

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?