📄 audiomanager.cpp
字号:
if (samples > 0) {
pcmdata.reserve(channels*2*samples);
}
while(!eof){
long ret=ov_read(&vf,pcmout,sizeof(pcmout),0,2,1,¤t_section);
if (ret == 0) {
/* EOF */
eof=1;
} else if (ret < 0) {
/* error in the stream. Not a problem, just reporting it in
case we (the app) cares. In this case, we don't. */
} else {
/* we don't bother dealing with sample rate changes, etc, but
you'll have to*/
// transfer the data out of the ogg buffer and into ours.
for (int q=0; q < ret; q++) {
pcmdata.push_back(pcmout[q]);
}
}
}
/* cleanup */
ov_clear(&vf);
// now that we have the PCM data in memory, convert it to a WAV file
// and handoff to DirectMusic for loading into a segment.
CWAVFile wavfile;
wavfile.m_AudioFormat = WAVE_FORMAT_PCM;
wavfile.m_BitsPerSample = 16;
wavfile.m_BlockAlign = 2*channels; // 16 bits / 8 (to bytes) * numchannels (2)
wavfile.m_ByteRate = samplerate*channels*2;
wavfile.m_NumberOfChannels = channels;
wavfile.m_SampleRate = samplerate;
wavfile.SetData(pcmdata.begin(), pcmdata.size());
return(LoadSound(wavfile));
}
CSoundPtr CAudioManager::LoadTrackedMusic(std::string filename)
{
FILE *f = fopen(filename.c_str(), "rb");
if (f == NULL) {
Throw("CAudioManager::LoadTrackedMusic: cannot open file.");
}
fclose(f);
CTrackedMusic *music = new CTrackedMusic(this);
music->m_FileName = filename;
char charfilename[256];
strcpy(charfilename, filename.c_str());
music->m_Module = Player_Load(charfilename, 64, 0);
return(music);
}
CAudioScriptPtr CAudioManager::LoadScript(std::string filename, std::string name)
{
CAudioScript *script = new CAudioScript(this);
HRESULT hr;
// convert filename to wide-string
WCHAR widefilename[MAX_PATH];
DXUtil_ConvertGenericStringToWide( widefilename, filename.c_str());
// tell loader to load this file
hr = m_Loader->LoadObjectFromFile(
CLSID_DirectMusicScript,
IID_IDirectMusicScript8,
widefilename,
(void**) &script->m_Script);
ThrowIfFailed(hr, "CAudioManager::LoadScript(std::string filename): LoadObjectFromFile failed.");
if (!name.length()) { name = filename; }
script->Init(name);
return(CAudioScriptPtr(script));
}
CAudioScriptPtr CAudioManager::LoadScript(unsigned char *data, int datalen, std::string name)
{
CAudioScript *script = new CAudioScript(this);
HRESULT hr;
DMUS_OBJECTDESC desc;
memset(&desc, 0, sizeof(DMUS_OBJECTDESC));
desc.dwSize = sizeof(DMUS_OBJECTDESC);
desc.dwValidData = DMUS_OBJ_MEMORY | DMUS_OBJ_CLASS;
desc.guidClass = CLSID_DirectMusicScript;
desc.llMemLength = datalen;
desc.pbMemData = data;
hr = m_Loader->GetObject(&desc, IID_IDirectMusicScript8, (void **)&script->m_Script);
ThrowIfFailed(hr, "CAudioManager::LoadScript(unsigned char *data): GetObject failed.");
script->m_OriginalData = data;
script->Init(name);
return(CAudioScriptPtr(script));
}
CAudioScriptPtr CAudioManager::LoadScript(HMODULE hmod, char *type, WORD resID, std::string name)
{
CAudioScript *script = new CAudioScript(this);
HRESULT hr;
DMUS_OBJECTDESC ObjDesc;
HRSRC hFound = FindResource(hmod, MAKEINTRESOURCE(resID), type);
if (NULL == hFound) {
Throw("CAudioManager::LoadScript: couldn't find resource!");
}
HGLOBAL hRes = LoadResource(hmod, hFound);
ObjDesc.dwSize = sizeof(DMUS_OBJECTDESC);
ObjDesc.guidClass = CLSID_DirectMusicScript;
ObjDesc.dwValidData = DMUS_OBJ_CLASS | DMUS_OBJ_MEMORY;
ObjDesc.pbMemData = (BYTE *) LockResource(hRes);
ObjDesc.llMemLength = SizeofResource(hmod, hFound);
hr = m_Loader->GetObject(&ObjDesc, IID_IDirectMusicScript8,
(void**) &script->m_Script);
ThrowIfFailed(hr, "CAudioManager::LoadScript(HMODULE hmod, WORD resID): GetObject failed.");
script->Init(name);
return(CAudioScriptPtr(script));
}
CSoundPtr CAudioManager::Load3DSound(const CWAVFile &wavfile)
{
unsigned char *savedata = wavfile.Save();
// when you load a wave from memory, DirectMusic owns the memory pointer...
// do NOT delete!
return(Load3DSound(savedata, wavfile.GetTotalSize()));
}
CSoundPtr CAudioManager::Load3DSound(unsigned char *data, int datalen)
{
C3DSoundEffect *snd = new C3DSoundEffect(this);
LoadSegmentFromMemory(snd, data, datalen);
return(CSoundPtr(snd));
}
CSoundPtr CAudioManager::Load3DSound(HMODULE hmod, char *type, WORD resID)
{
C3DSoundEffect *snd = new C3DSoundEffect(this);
LoadSegmentFromResource(snd, hmod, type, resID);
return(CSoundPtr(snd));
}
CSoundPtr CAudioManager::Load3DSound(std::string filename)
{
C3DSoundEffect *snd = new C3DSoundEffect(this);
LoadSegmentFromDisk(snd, filename);
return(CSoundPtr(snd));
}
void CAudioManager::LoadSegmentFromDisk(CDirectMusicSegment *dest, std::string filename)
{
HRESULT hr;
// convert filename to wide-string
WCHAR widefilename[MAX_PATH];
DXUtil_ConvertGenericStringToWide( widefilename, filename.c_str());
// tell loader to load this file
hr = m_Loader->LoadObjectFromFile(
CLSID_DirectMusicSegment,
IID_IDirectMusicSegment8,
widefilename,
(void**) &dest->m_Segment);
ThrowIfFailed(hr, "CAudioManager::LoadSegmentFromDisk(std::string filename): LoadObjectFromFile failed.");
}
void CAudioManager::LoadSegmentFromMemory(CDirectMusicSegment *dest, unsigned char *data, int datalen)
{
HRESULT hr;
DMUS_OBJECTDESC desc;
memset(&desc, 0, sizeof(DMUS_OBJECTDESC));
desc.dwSize = sizeof(DMUS_OBJECTDESC);
desc.dwValidData = DMUS_OBJ_MEMORY | DMUS_OBJ_CLASS;
desc.guidClass = CLSID_DirectMusicSegment;
desc.llMemLength = datalen;
desc.pbMemData = data;
hr = m_Loader->GetObject(&desc, IID_IDirectMusicSegment8, (void **)&dest->m_Segment);
ThrowIfFailed(hr, "CAudioManager::LoadSegmentFromMemory(unsigned char *data): GetObject failed.");
dest->m_OriginalData = data;
}
void CAudioManager::LoadSegmentFromResource(CDirectMusicSegment *dest, HMODULE hmod, char *type, WORD resID)
{
HRESULT hr;
DMUS_OBJECTDESC ObjDesc;
HRSRC hFound = FindResource(hmod, MAKEINTRESOURCE(resID), type);
if (NULL == hFound) {
Throw("CAudioManager::LoadSegmentFromResource: couldn't find resource!");
}
HGLOBAL hRes = LoadResource(hmod, hFound);
ObjDesc.dwSize = sizeof(DMUS_OBJECTDESC);
ObjDesc.guidClass = CLSID_DirectMusicSegment;
ObjDesc.dwValidData = DMUS_OBJ_CLASS | DMUS_OBJ_MEMORY;
ObjDesc.pbMemData = (BYTE *) LockResource(hRes);
ObjDesc.llMemLength = SizeofResource(hmod, hFound);
hr = m_Loader->GetObject(&ObjDesc, IID_IDirectMusicSegment8,
(void**) &dest->m_Segment);
ThrowIfFailed(hr, "CAudioManager::LoadSegmentFromResource(HMODULE hmod, WORD resID): GetObject failed.");
}
void CAudioManager::SetSearchDirectory(std::string dirname)
{
// convert dirname to wide-string
WCHAR widedirname[MAX_PATH];
DXUtil_ConvertGenericStringToWide( widedirname, dirname.c_str());
HRESULT hr = m_Loader->SetSearchDirectory(CLSID_DirectMusicSegment, widedirname, true);
ThrowIfFailed(hr, "CAudioManager::SetSearchDirectory: SetSearchDirectory() failed.");
}
void CAudioManager::SetVolume(const CVolume &vol)
{
if (NULL == m_Performance) return;
long dmvol = vol.ToDirectMusic();
ThrowIfFailed(m_Performance->SetGlobalParam(GUID_PerfMasterVolume, &dmvol, sizeof(dmvol)),
"CAudioManager::SetVolume: SetGlobalParam failed.");
}
CVolume CAudioManager::GetVolume()
{
if (NULL == m_Performance) return(0);
long dmvol=0;
ThrowIfFailed(m_Performance->GetGlobalParam(GUID_PerfMasterVolume, &dmvol, sizeof(dmvol)),
"CAudioManager::GetVolume: GetGlobalParam failed.");
CVolume vol;
vol.FromDirectMusic(dmvol);
return(vol);
}
void CAudioManager::DispatchNotificationMessages()
{
if (NULL == m_Performance) return;
DMUS_NOTIFICATION_PMSG* pPmsg;
while (m_NotificationHandler && m_Performance->GetNotificationPMsg(&pPmsg) == S_OK) {
if (pPmsg->guidNotificationType == GUID_NOTIFICATION_MEASUREANDBEAT) {
// if it's a beat, call OnBeat... otherwise call OnMeasure.
if (pPmsg->dwField1 == 0) { m_NotificationHandler->OnMeasure(*pPmsg); }
else { m_NotificationHandler->OnBeat(*pPmsg); }
}
m_Performance->FreePMsg((DMUS_PMSG*)pPmsg);
} // while
// look for and process DirectShow notifications
CDirectShowMusic::DispatchDirectShowNotifications();
}
void CAudioManager::SetMasterTempoScaleFactor(float value)
{
if (NULL == m_Performance) return;
ThrowIfFailed(m_Performance->SetGlobalParam(GUID_PerfMasterTempo, &value, sizeof(value)),
"CAudioManager::SetMasterTempo: SetGlobalParam failed.");
}
float CAudioManager::GetMasterTempoScaleFactor()
{
if (NULL == m_Performance) return(0.0f);
float tempo=0.0f;
ThrowIfFailed(m_Performance->GetGlobalParam(GUID_PerfMasterTempo, &tempo, sizeof(tempo)),
"CAudioManager::GetMasterTempo: GetGlobalParam failed.");
return(tempo);
}
}; // namespace
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -