⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 directmusicsegment.cpp

📁 游戏音频程序设计-Beginning.Game.Audio.Programming
💻 CPP
字号:
// DirectMusicSegment.cpp: implementation of the CDirectMusicSegment class.
//
//////////////////////////////////////////////////////////////////////

#include "DirectMusicSegment.h"
#include "SoundInstance.h"
#include "AudioManager.h"

namespace AudioEngine {

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

CDirectMusicSegment::CDirectMusicSegment(CAudioManager *mgr) : CSound(mgr)
{
  m_Segment = NULL;
  m_OriginalData = NULL;
}

CDirectMusicSegment::~CDirectMusicSegment()
{
  if (NULL != m_Segment && m_Manager->GetPerformance()) {
    m_Segment->Unload(m_Manager->GetPerformance());
  }

  SAFE_RELEASE(m_Segment);

  SAFE_DELETE(m_OriginalData);
}

void CDirectMusicSegment::SetRepeats(int repeats)
{
  m_Segment->SetRepeats(repeats);
}
/****************************************************************************
 *
 * CDirectMusicSegment::IsPlaying: returns true if the sound is playing, 
 * false otherwise.
 *
 ****************************************************************************/
bool CDirectMusicSegment::IsPlaying()
{
  if (NULL == m_Segment) return(false);
  if (NULL == m_Manager) return(false);

  return(m_Manager->GetPerformance()->IsPlaying(m_Segment, NULL) == S_OK);
}

/****************************************************************************
 *
 * CDirectMusicSegment::Play: Plays a sound.  Returns true if the sound 
 * plays.
 *
 ****************************************************************************/
bool CDirectMusicSegment::Play(CSoundInstance *newinst, bool control, IDirectMusicAudioPath8 *path)
{
  HRESULT hr;
  if (NULL == m_Segment || NULL == m_Manager) { 
    if (newinst) newinst->UnInit(); 
    return(false); 
  }

  static CSoundInstance dummy;
  if (NULL == newinst) newinst = &dummy;
  
  IDirectMusicPerformance8 *perf = m_Manager->GetPerformance();

  IDirectMusicAudioPath8 * pPath = path;
  IUnknown *pConfig;
 
  if (NULL == pPath) { // no audio path supplied, make one!
    if (SUCCEEDED(m_Segment->GetAudioPathConfig(&pConfig)))
    {
      // this has an embedded audio path configuration
      hr = perf->CreateAudioPath(pConfig, TRUE, &pPath);
      pConfig->Release();
      ThrowIfFailed(hr, "CDirectMusicSegment::Play: Couldn't create audiopath using embedded config.");
    } 
    else {
      // no config... make our own!
      hr = perf->CreateStandardAudioPath(DMUS_APATH_DYNAMIC_STEREO, CAudioManager::NUMCHANNELS, TRUE, &pPath);
      ThrowIfFailed(hr, "CDirectMusicSegment::Play: Couldn't create standard audiopath.");
    }
  }
    
  m_Segment->Download(perf);

  // if no path was passed in, this instance should release the audio path we created.
  newinst->SetShouldReleaseAudioPath(path == NULL); 

  // initialize the sound instance (set volume, etc.)
  // this will also set it playing.
  newinst->Init(this, pPath, control);
  return(true);
}

/****************************************************************************
 *
 * CDirectMusicSegment::Stops: Stops a sound.  Returns true if the sound 
 * stops playing.
 *
 ****************************************************************************/
bool CDirectMusicSegment::Stop(MUSIC_TIME time, DWORD flags)
{
  return(SUCCEEDED(m_Manager->GetPerformance()->Stop(m_Segment, NULL, time, flags)));
}

} // namespace

⌨️ 快捷键说明

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