📄 ch6p1_midiplayback.cpp
字号:
// Ch6p1_MIDIPlayback.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"
using namespace std;
using namespace AudioEngine;
CAudioManager g_AudioMgr;
CAudioManager *AudioEngine::GetAudioManager() { return(&g_AudioMgr); }
class CCh6p1StupidNotificationHandler : public CNotificationHandler
{
virtual void OnBeat(DMUS_NOTIFICATION_PMSG &msg) { cout << "..Beat!.."; }
virtual void OnMeasure(DMUS_NOTIFICATION_PMSG &msg) { cout << endl << "Measure!.."; }
};
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);
}
void main(int argc, char* argv[])
{
try {
// initialize audio manager
cout << "Initializing Audio Manager..." << endl;
g_AudioMgr.Init(GetConsoleWindowHandle());
// load a WAV file
cout << "Loading MIDI File..." << endl;
CSoundPtr snd = g_AudioMgr.LoadMIDI("test.mid");
// full volume on performance
GetAudioManager()->SetVolume(CVolume(0));
cout << "Playing sound..." << endl;
snd->SetVolume(0);
snd->Play();
// set up the notification handler
CCh6p1StupidNotificationHandler stupidhandler;
GetAudioManager()->SetNotificationHandler(&stupidhandler);
CMIDIMusic *music = dynamic_cast<CMIDIMusic *>(snd.Get());
if (music) {
int originaltempo = music->GetTempo();
cout << "Tempo is currently " << originaltempo << " beats per minute..." << endl;
cout << "Press a key to change it to 175 BPM..." << endl;
getch();
music->SetTempo(175);
cout << "Tempo is currently " << music->GetTempo() << " beats per minute..." << endl;
cout << "Press a key to change it back to " << originaltempo << " BPM..." << endl;
getch();
music->SetTempo(originaltempo);
}
// play with master tempo
cout << "Press a key to change the master tempo to 2..." << endl;
getch();
GetAudioManager()->SetMasterTempoScaleFactor(2.0f);
cout << "Press a key to change the master tempo back to 1..." << endl;
getch();
GetAudioManager()->SetMasterTempoScaleFactor(1.0f);
cout << "Keep listening to the song or press a key to stop." << endl;
// pretend this is your main game loop
while (music->IsPlaying() && !kbhit()) {
GetAudioManager()->DispatchNotificationMessages();
Sleep(100);
}
if (kbhit()) getch(); // remove key that kbhit detected
// uninit audio manager
cout << endl << "UnIniting audio manager..." << endl;
g_AudioMgr.UnInit();
}
catch(CError &e) {
MessageBox(GetConsoleWindowHandle(), e.GetMessageBoxString().c_str(), "Ch6p1_MIDIPlayback", MB_ICONSTOP);
Exit(-1);
}
catch(...) {
MessageBox(GetConsoleWindowHandle(), "This sample program has encountered an error and must close.", "Ch6p1_MIDIPlayback", MB_ICONSTOP);
Exit(-1);
}
Exit(0);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -