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

📄 ch12p2_effects.cpp

📁 游戏音频程序设计-Beginning.Game.Audio.Programming
💻 CPP
字号:
// Ch12p2_Effects.cpp : Defines the entry point for the console application.
//

#include <iostream>
#include <sstream>
#include <string>
#include <math.h>
#include <conio.h>
#include <windows.h>
#include <mmreg.h>

#include "dsound.h"
#include "dxerr8.h"
#include "AudioEngine/dxutil.h"
#include "AudioEngine/AudioEngine.h"
#include "AudioEngine/SoundInstance.h"

using namespace std;
using namespace AudioEngine;

CAudioManager g_AudioMgr;
CAudioManager *AudioEngine::GetAudioManager() { return(&g_AudioMgr); }

void Exit(int errorcode)
{
  #ifdef _DEBUG
  cout << endl << endl << "(Program ended, press a key to exit.)";
  // so that the console window doesn't disappear before we have
  // a chance to look at it.
  getch(); 
#endif

  exit(errorcode);
}

HWND GetConsoleWindowHandle()
{
  char title[512];
  HWND hWnd;
 
  GetConsoleTitle(title, sizeof(title));
  hWnd = FindWindow(NULL, title);
  return(hWnd);
}

int main(int argc, char* argv[])
{
  
  try {

    // initialize audio manager
    cout << "Initializing Audio Manager..." << endl;
    g_AudioMgr.Init(GetConsoleWindowHandle());

    // load a WAV file
    cout << "Loading WAV File..." << endl;
    CSoundPtr snd = g_AudioMgr.LoadSound("test.wav");
    
    // full volume on performance
    GetAudioManager()->SetVolume(CVolume(0));

    bool done=false;

    CDirectMusicSegment *seg = dynamic_cast<CDirectMusicSegment *>(snd.Get());
    if (NULL == seg) Throw("can't dynamic cast to segment!");
    
    while (!done) {
      CSoundInstance *inst = new CSoundInstance();
      cout << endl;
      cout << "Your wish is my command: " << endl;
      cout << "1) Play normally" << endl;
      cout << "2) Play with echo" << endl;
      cout << "3) Play with chorus" << endl;
      cout << "4) Play with flange" << endl;

      cout << "x) Exit!" << endl;

      char choice = getch();
      switch(choice) {
      
      case '1': // normal
        cout << "Normal playback..." << endl;
      
        seg->Play(inst, false);
        // play sound
        Sleep(500);
        while (inst->IsPlaying()) Sleep(500);
        break;

      case '2': // with echo
        {
          cout << "With echo..." << endl;

          // create echo
          CEchoEffect *echofx = new CEchoEffect;
          echofx->m_Params.fWetDryMix = 50; // 50% echoed sound (wet), 50% original wav (dry)
          echofx->m_Params.fFeedback = 50; // 50% of output fed back into input
          echofx->m_Params.fLeftDelay = 750; // wait 750ms, the echo out left speaker
          echofx->m_Params.fRightDelay = 500; // wait 500ms, then echo out right speaker
          echofx->m_Params.lPanDelay = 0; // don't swap echo

          inst->AddEffect(echofx);

          // play sound
          seg->Play(inst, false);

          Sleep(500);
          while (inst->IsPlaying()) Sleep(500);
        }
        break;

      case '3': // with chorus
        {
          cout << "With chorus..." << endl;

          // create echo
          CChorusEffect *chorusfx = new CChorusEffect;
          chorusfx->m_Params.fWetDryMix = 50; // 50% chorused sound (wet), 50% original wav (dry)
          chorusfx->m_Params.fDepth = DSFXCHORUS_DEPTH_MAX; // depth of chorus
          chorusfx->m_Params.fFeedback = 50; // 50% of output fed back into input
          chorusfx->m_Params.fFrequency = 1.1f; // chorus frequency
          chorusfx->m_Params.lWaveform = DSFXCHORUS_WAVE_SIN; // sine wave chorus
          chorusfx->m_Params.fDelay = 16; // chorus delay
          chorusfx->m_Params.lPhase = DSFXCHORUS_PHASE_90; 

          inst->AddEffect(chorusfx);

          // play sound
          seg->Play(inst, false);

          Sleep(500);
          while (inst->IsPlaying()) Sleep(500);
        }
        break;
      
      case '4': // with flange
        {
          cout << "With flange..." << endl;

          // create echo
          CFlangeEffect *flangefx = new CFlangeEffect;
          flangefx->m_Params.fWetDryMix = 50; // 50% flangeed sound (wet), 50% original wav (dry)
          flangefx->m_Params.fDepth = DSFXFLANGER_DEPTH_MAX; // depth of flange
          flangefx->m_Params.fFeedback = 50; // 50% of output fed back into input
          flangefx->m_Params.fFrequency = 1.1f; // flange frequency
          flangefx->m_Params.lWaveform = DSFXFLANGER_WAVE_SIN; // sine wave flange
          flangefx->m_Params.fDelay = 16; // flange delay
          flangefx->m_Params.lPhase = DSFXFLANGER_PHASE_ZERO; 

          inst->AddEffect(flangefx);

          // play sound
          seg->Play(inst, false);

          Sleep(500);
          while (inst->IsPlaying()) Sleep(500);
        }
        break;

      case 'x': // exit
        done=true;
        break;
      }

      if (kbhit()) getch();
      delete inst;
    } 

    // uninit audio manager
    cout << endl << "UnIniting audio manager..." << endl;
    g_AudioMgr.UnInit();
  } 
  catch(CError &e) {
    MessageBox(GetConsoleWindowHandle(), e.GetMessageBoxString().c_str(), "Ch12p2_Effects", MB_ICONSTOP);
    Exit(-1);
  }
  catch(...) {
    MessageBox(GetConsoleWindowHandle(), "This sample program has encountered an error and must close.", "Ch12p2_Effects", MB_ICONSTOP);
    Exit(-1);
  }
  Exit(0);
  return(0); // to avoid warning C4508
}

⌨️ 快捷键说明

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