Magic_Game/core/tools/Music.cpp

552 lines
11 KiB
C++
Raw Normal View History

2018-09-05 13:33:39 +08:00
#include "..\e2dtool.h"
2018-09-05 13:17:07 +08:00
#include "..\e2dmodule.h"
2018-02-01 22:07:44 +08:00
#ifndef SAFE_DELETE
#define SAFE_DELETE(p) { if (p) { delete (p); (p)=nullptr; } }
2018-02-01 22:07:44 +08:00
#endif
2018-02-01 22:07:44 +08:00
#ifndef SAFE_DELETE_ARRAY
#define SAFE_DELETE_ARRAY(p) { if (p) { delete[] (p); (p)=nullptr; } }
2018-02-01 22:07:44 +08:00
#endif
inline bool TraceError(wchar_t* prompt)
2018-02-01 22:07:44 +08:00
{
WARN("Music error: %s failed!", prompt);
2018-02-01 22:07:44 +08:00
return false;
}
inline bool TraceError(wchar_t* prompt, HRESULT hr)
2018-01-30 16:45:38 +08:00
{
WARN("Music error: %s (%#X)", prompt, hr);
2018-02-01 22:07:44 +08:00
return false;
}
2018-05-17 23:53:27 +08:00
e2d::Music::Music()
2018-09-04 22:42:34 +08:00
: opened_(false)
, wfx_(nullptr)
, hmmio_(nullptr)
, buffer_(nullptr)
, wave_data_(nullptr)
, size_(0)
, voice_(nullptr)
, callback_()
{
2018-05-17 23:53:27 +08:00
}
2018-04-27 17:07:47 +08:00
2018-09-04 22:42:34 +08:00
e2d::Music::Music(const e2d::String & file_path)
: opened_(false)
, wfx_(nullptr)
, hmmio_(nullptr)
, buffer_(nullptr)
, wave_data_(nullptr)
, size_(0)
, voice_(nullptr)
, callback_()
2018-05-17 23:53:27 +08:00
{
2018-09-04 22:42:34 +08:00
this->Open(file_path);
2018-05-17 23:53:27 +08:00
}
2018-04-27 17:07:47 +08:00
e2d::Music::Music(const Resource& res)
2018-09-04 22:42:34 +08:00
: opened_(false)
, wfx_(nullptr)
, hmmio_(nullptr)
, buffer_(nullptr)
, wave_data_(nullptr)
, size_(0)
, voice_(nullptr)
, callback_()
2018-05-17 23:53:27 +08:00
{
2018-09-04 22:42:34 +08:00
this->Open(res);
2018-05-17 23:53:27 +08:00
}
2018-04-27 17:07:47 +08:00
2018-05-17 23:53:27 +08:00
e2d::Music::~Music()
{
2018-09-04 22:42:34 +08:00
Close();
}
2018-09-04 22:42:34 +08:00
bool e2d::Music::Open(const e2d::String & file_path)
{
2018-09-04 22:42:34 +08:00
if (opened_)
2018-02-01 22:07:44 +08:00
{
2018-09-04 22:42:34 +08:00
Close();
2018-08-12 12:06:06 +08:00
}
2018-09-04 22:42:34 +08:00
if (file_path.IsEmpty())
2018-08-12 12:06:06 +08:00
{
WARN("Music::Open error: Invalid file name.");
return false;
2018-02-01 22:07:44 +08:00
}
File music_file;
2018-09-07 23:47:21 +08:00
if (!music_file.Open(file_path))
{
WARN("Music::Open error: File not found.");
return false;
}
// <20>û<EFBFBD><C3BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>·<EFBFBD><C2B7><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>·<EFBFBD><C2B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD>û<EFBFBD><C3BB><EFBFBD><EFBFBD><EFBFBD>ͨ<EFBFBD><CDA8> File::AddSearchPath <20><><EFBFBD><EFBFBD>
// Ĭ<><C4AC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>·<EFBFBD><C2B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫͨ<D2AA><CDA8> File::GetPath <20><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD>·<EFBFBD><C2B7>
String music_file_path = music_file.GetPath();
// <20><>λ wave <20>ļ<EFBFBD>
wchar_t pFilePath[MAX_PATH];
if (!FindMediaFileCch(pFilePath, MAX_PATH, (const wchar_t *)music_file_path))
{
2018-09-04 22:42:34 +08:00
WARN("Failed to Find media file: %s", pFilePath);
return false;
}
2018-05-24 15:47:38 +08:00
2018-09-04 22:42:34 +08:00
hmmio_ = mmioOpen(pFilePath, nullptr, MMIO_ALLOCBUF | MMIO_READ);
2018-09-04 22:42:34 +08:00
if (nullptr == hmmio_)
{
return TraceError(L"mmioOpen");
}
2018-09-06 23:26:32 +08:00
if (!ReadMMIO())
{
// <20><>ȡ<EFBFBD><C8A1> wave <20>ļ<EFBFBD>ʱ ReadMMIO <20><><EFBFBD><EFBFBD>ʧ<EFBFBD><CAA7>
2018-09-04 22:42:34 +08:00
mmioClose(hmmio_, 0);
return TraceError(L"ReadMMIO");
}
2018-09-06 23:26:32 +08:00
if (!ResetFile())
return TraceError(L"ResetFile");
2018-09-04 22:42:34 +08:00
// <20><><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD>wave <20>ļ<EFBFBD><C4BC>Ĵ<EFBFBD>С<EFBFBD><D0A1> ck_.cksize
size_ = ck_.cksize;
2018-02-01 22:07:44 +08:00
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݶ<EFBFBD>ȡ<EFBFBD><C8A1><EFBFBD>ڴ<EFBFBD><DAB4><EFBFBD>
2018-09-04 22:42:34 +08:00
wave_data_ = new BYTE[size_];
2018-09-06 23:26:32 +08:00
if (!Read(wave_data_, size_))
{
TraceError(L"Failed to read WAV data");
2018-09-04 22:42:34 +08:00
SAFE_DELETE_ARRAY(wave_data_);
return false;
}
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Դ
2018-09-04 22:42:34 +08:00
auto xAudio2 = Audio::GetInstance()->GetXAudio2();
HRESULT hr = xAudio2->CreateSourceVoice(&voice_, wfx_, 0, XAUDIO2_DEFAULT_FREQ_RATIO, &callback_);
if (FAILED(hr))
2018-02-01 22:07:44 +08:00
{
TraceError(L"Create source voice error", hr);
2018-09-04 22:42:34 +08:00
SAFE_DELETE_ARRAY(wave_data_);
return false;
}
2018-09-04 22:42:34 +08:00
opened_ = true;
return true;
}
2018-09-04 22:42:34 +08:00
bool e2d::Music::Open(const Resource& res)
{
2018-09-04 22:42:34 +08:00
if (opened_)
{
2018-09-04 22:42:34 +08:00
Close();
2018-02-01 22:07:44 +08:00
}
HRSRC hResInfo;
HGLOBAL hResData;
DWORD dwSize;
void* pvRes;
if (nullptr == (hResInfo = FindResourceW(HINST_THISCOMPONENT, MAKEINTRESOURCE(res.id), (LPCWSTR)res.type)))
return TraceError(L"FindResource");
if (nullptr == (hResData = LoadResource(HINST_THISCOMPONENT, hResInfo)))
return TraceError(L"LoadResource");
if (0 == (dwSize = SizeofResource(HINST_THISCOMPONENT, hResInfo)))
return TraceError(L"SizeofResource");
if (nullptr == (pvRes = LockResource(hResData)))
return TraceError(L"LockResource");
2018-09-04 22:42:34 +08:00
buffer_ = new CHAR[dwSize];
memcpy(buffer_, pvRes, dwSize);
MMIOINFO mmioInfo;
ZeroMemory(&mmioInfo, sizeof(mmioInfo));
mmioInfo.fccIOProc = FOURCC_MEM;
mmioInfo.cchBuffer = dwSize;
2018-09-04 22:42:34 +08:00
mmioInfo.pchBuffer = (CHAR*)buffer_;
2018-09-04 22:42:34 +08:00
hmmio_ = mmioOpen(nullptr, &mmioInfo, MMIO_ALLOCBUF | MMIO_READ);
2018-09-04 22:42:34 +08:00
if (nullptr == hmmio_)
{
return TraceError(L"mmioOpen");
}
2018-09-06 23:26:32 +08:00
if (!ReadMMIO())
{
2018-05-17 23:53:27 +08:00
// <20><>ȡ<EFBFBD><C8A1> wave <20>ļ<EFBFBD>ʱ ReadMMIO <20><><EFBFBD><EFBFBD>ʧ<EFBFBD><CAA7>
2018-09-04 22:42:34 +08:00
mmioClose(hmmio_, 0);
return TraceError(L"ReadMMIO");
}
2018-09-06 23:26:32 +08:00
if (!ResetFile())
return TraceError(L"ResetFile");
2018-09-04 22:42:34 +08:00
// <20><><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD>wave <20>ļ<EFBFBD><C4BC>Ĵ<EFBFBD>С<EFBFBD><D0A1> ck_.cksize
size_ = ck_.cksize;
2018-05-17 23:53:27 +08:00
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݶ<EFBFBD>ȡ<EFBFBD><C8A1><EFBFBD>ڴ<EFBFBD><DAB4><EFBFBD>
2018-09-04 22:42:34 +08:00
wave_data_ = new BYTE[size_];
2018-09-06 23:26:32 +08:00
if (!Read(wave_data_, size_))
{
TraceError(L"Failed to read WAV data");
2018-09-04 22:42:34 +08:00
SAFE_DELETE_ARRAY(wave_data_);
return false;
}
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Դ
2018-09-04 22:42:34 +08:00
auto xAudio2 = Audio::GetInstance()->GetXAudio2();
HRESULT hr = xAudio2->CreateSourceVoice(&voice_, wfx_, 0, XAUDIO2_DEFAULT_FREQ_RATIO, &callback_);
if (FAILED(hr))
{
TraceError(L"Create source voice error", hr);
2018-09-04 22:42:34 +08:00
SAFE_DELETE_ARRAY(wave_data_);
return false;
}
2018-09-04 22:42:34 +08:00
opened_ = true;
return true;
}
2018-09-04 22:42:34 +08:00
bool e2d::Music::Play(int loop_count)
{
2018-09-04 22:42:34 +08:00
if (!opened_)
2018-03-02 23:49:57 +08:00
{
2018-09-04 22:42:34 +08:00
WARN("Music::Play Failed: Music must be opened first!");
2018-03-02 23:49:57 +08:00
return false;
}
2018-09-04 22:42:34 +08:00
if (voice_ == nullptr)
2018-03-02 23:49:57 +08:00
{
2018-09-04 22:42:34 +08:00
WARN("Music::Play Failed: IXAudio2SourceVoice Null pointer exception!");
2018-03-02 23:49:57 +08:00
return false;
}
2018-02-01 22:07:44 +08:00
2018-09-02 00:02:49 +08:00
XAUDIO2_VOICE_STATE state;
2018-09-04 22:42:34 +08:00
voice_->GetState(&state);
2018-09-02 00:02:49 +08:00
if (state.BuffersQueued)
{
2018-09-04 22:42:34 +08:00
Stop();
}
2018-09-04 22:42:34 +08:00
loop_count = std::min(loop_count, XAUDIO2_LOOP_INFINITE - 1);
loop_count = (loop_count < 0) ? XAUDIO2_LOOP_INFINITE : loop_count;
2018-02-06 21:11:54 +08:00
2018-02-01 22:07:44 +08:00
// <20>ύ wave <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
XAUDIO2_BUFFER buffer = { 0 };
2018-09-04 22:42:34 +08:00
buffer.pAudioData = wave_data_;
2018-02-01 22:07:44 +08:00
buffer.Flags = XAUDIO2_END_OF_STREAM;
2018-09-04 22:42:34 +08:00
buffer.AudioBytes = size_;
buffer.LoopCount = loop_count;
2018-02-01 22:07:44 +08:00
2018-03-02 23:49:57 +08:00
HRESULT hr;
2018-09-04 22:42:34 +08:00
if (FAILED(hr = voice_->SubmitSourceBuffer(&buffer)))
{
2018-04-23 00:05:57 +08:00
TraceError(L"Submitting source buffer error", hr);
2018-09-04 22:42:34 +08:00
voice_->DestroyVoice();
SAFE_DELETE_ARRAY(wave_data_);
2018-02-01 22:07:44 +08:00
return false;
}
2018-09-04 22:42:34 +08:00
hr = voice_->Start(0);
2018-05-17 23:53:27 +08:00
2018-02-01 22:07:44 +08:00
return SUCCEEDED(hr);
}
2018-09-04 22:42:34 +08:00
void e2d::Music::Pause()
{
2018-09-04 22:42:34 +08:00
if (voice_)
2018-02-01 22:07:44 +08:00
{
2018-09-04 22:42:34 +08:00
voice_->Stop();
2018-02-01 22:07:44 +08:00
}
}
2018-09-04 22:42:34 +08:00
void e2d::Music::Resume()
{
2018-09-04 22:42:34 +08:00
if (voice_)
2018-02-01 22:07:44 +08:00
{
2018-09-04 22:42:34 +08:00
voice_->Start();
2018-02-01 22:07:44 +08:00
}
}
2018-09-04 22:42:34 +08:00
void e2d::Music::Stop()
{
2018-09-04 22:42:34 +08:00
if (voice_)
2018-02-01 22:07:44 +08:00
{
2018-09-04 22:42:34 +08:00
if (SUCCEEDED(voice_->Stop()))
2018-02-01 22:07:44 +08:00
{
2018-09-04 22:42:34 +08:00
voice_->ExitLoop();
voice_->FlushSourceBuffers();
2018-02-01 22:07:44 +08:00
}
}
}
2018-09-04 22:42:34 +08:00
void e2d::Music::Close()
{
2018-09-04 22:42:34 +08:00
if (voice_)
2018-03-02 23:49:57 +08:00
{
2018-09-04 22:42:34 +08:00
voice_->Stop();
voice_->FlushSourceBuffers();
voice_->DestroyVoice();
voice_ = nullptr;
2018-03-02 23:49:57 +08:00
}
2018-09-04 22:42:34 +08:00
if (hmmio_ != nullptr)
2018-03-02 23:49:57 +08:00
{
2018-09-04 22:42:34 +08:00
mmioClose(hmmio_, 0);
hmmio_ = nullptr;
2018-03-02 23:49:57 +08:00
}
2018-09-04 22:42:34 +08:00
SAFE_DELETE_ARRAY(buffer_);
SAFE_DELETE_ARRAY(wave_data_);
SAFE_DELETE_ARRAY(wfx_);
2018-03-02 23:49:57 +08:00
2018-09-04 22:42:34 +08:00
opened_ = false;
2018-03-02 23:49:57 +08:00
}
2018-09-04 22:42:34 +08:00
bool e2d::Music::IsPlaying() const
2018-03-02 23:49:57 +08:00
{
2018-09-04 22:42:34 +08:00
if (opened_ && voice_)
{
2018-09-02 00:02:49 +08:00
XAUDIO2_VOICE_STATE state;
2018-09-04 22:42:34 +08:00
voice_->GetState(&state);
2018-09-02 00:02:49 +08:00
if (state.BuffersQueued)
return true;
2018-02-01 22:07:44 +08:00
}
2018-09-02 00:02:49 +08:00
return false;
}
2018-09-04 22:42:34 +08:00
IXAudio2SourceVoice * e2d::Music::GetSourceVoice() const
{
2018-09-04 22:42:34 +08:00
return voice_;
2018-05-17 23:53:27 +08:00
}
2018-09-04 22:42:34 +08:00
bool e2d::Music::SetVolume(float volume)
2018-05-17 23:53:27 +08:00
{
2018-09-04 22:42:34 +08:00
if (voice_)
2018-02-01 22:07:44 +08:00
{
2018-09-04 22:42:34 +08:00
return SUCCEEDED(voice_->SetVolume(volume));
2018-02-01 22:07:44 +08:00
}
return false;
}
2018-09-04 22:42:34 +08:00
void e2d::Music::SetCallbackOnEnd(const Function & func)
{
2018-09-04 22:42:34 +08:00
callback_.SetCallbackOnStreamEnd(func);
}
2018-09-04 22:42:34 +08:00
void e2d::Music::SetCallbackOnLoopEnd(const Function & func)
{
2018-09-04 22:42:34 +08:00
callback_.SetCallbackOnLoopEnd(func);
}
2018-09-06 23:26:32 +08:00
bool e2d::Music::ReadMMIO()
2018-02-01 22:07:44 +08:00
{
MMCKINFO ckIn;
PCMWAVEFORMAT pcmWaveFormat;
2018-01-30 16:45:38 +08:00
2018-02-01 22:07:44 +08:00
memset(&ckIn, 0, sizeof(ckIn));
2018-01-30 16:45:38 +08:00
2018-09-04 22:42:34 +08:00
wfx_ = nullptr;
2018-01-30 16:45:38 +08:00
2018-09-04 22:42:34 +08:00
if ((0 != mmioDescend(hmmio_, &ck_riff_, nullptr, 0)))
2018-02-01 22:07:44 +08:00
return TraceError(L"mmioDescend");
2018-01-30 16:45:38 +08:00
2018-02-01 22:07:44 +08:00
// ȷ<><C8B7><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD>һ<EFBFBD><D2BB><EFBFBD>Ϸ<EFBFBD><CFB7><EFBFBD> wave <20>ļ<EFBFBD>
2018-09-04 22:42:34 +08:00
if ((ck_riff_.ckid != FOURCC_RIFF) ||
(ck_riff_.fccType != mmioFOURCC('W', 'A', 'V', 'E')))
2018-02-01 22:07:44 +08:00
return TraceError(L"mmioFOURCC");
2018-01-30 16:45:38 +08:00
2018-02-01 22:07:44 +08:00
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><C4BC>в<EFBFBD><D0B2><EFBFBD> 'fmt' <20><>
ckIn.ckid = mmioFOURCC('f', 'm', 't', ' ');
2018-09-04 22:42:34 +08:00
if (0 != mmioDescend(hmmio_, &ckIn, &ck_riff_, MMIO_FINDCHUNK))
2018-02-01 22:07:44 +08:00
return TraceError(L"mmioDescend");
2018-01-30 16:45:38 +08:00
2018-02-01 22:07:44 +08:00
// 'fmt' <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD><D3A6> PCMWAVEFORMAT һ<><D2BB><EFBFBD><EFBFBD>
if (ckIn.cksize < (LONG)sizeof(PCMWAVEFORMAT))
return TraceError(L"sizeof(PCMWAVEFORMAT)");
// <20><> 'fmt' <20><><EFBFBD><EFBFBD>ȡ<EFBFBD><C8A1> pcmWaveFormat <20><>
2018-09-04 22:42:34 +08:00
if (mmioRead(hmmio_, (HPSTR)&pcmWaveFormat,
2018-02-01 22:07:44 +08:00
sizeof(pcmWaveFormat)) != sizeof(pcmWaveFormat))
return TraceError(L"mmioRead");
2018-01-30 16:45:38 +08:00
2018-02-01 22:07:44 +08:00
// <20><><EFBFBD><EFBFBD> WAVEFORMATEX<45><58><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> PCM <20><>ʽ<EFBFBD><CABD><EFBFBD>ٶ<EFBFBD>ȡһ<C8A1><D2BB> WORD <20><>С
// <20><><EFBFBD><EFBFBD><EFBFBD>ݣ<EFBFBD><DDA3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݾ<EFBFBD><DDBE>Ƕ<EFBFBD><C7B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ĵ<EFBFBD>С
if (pcmWaveFormat.wf.wFormatTag == WAVE_FORMAT_PCM)
2018-01-30 16:45:38 +08:00
{
2018-09-04 22:42:34 +08:00
wfx_ = (WAVEFORMATEX*) new CHAR[sizeof(WAVEFORMATEX)];
2018-02-01 22:07:44 +08:00
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-09-04 22:42:34 +08:00
memcpy(wfx_, &pcmWaveFormat, sizeof(pcmWaveFormat));
wfx_->cbSize = 0;
2018-01-30 16:45:38 +08:00
}
2018-02-01 22:07:44 +08:00
else
{
// <20><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݵĴ<DDB5>С
WORD cbExtraBytes = 0L;
2018-09-04 22:42:34 +08:00
if (mmioRead(hmmio_, (CHAR*)&cbExtraBytes, sizeof(WORD)) != sizeof(WORD))
2018-02-01 22:07:44 +08:00
return TraceError(L"mmioRead");
2018-01-30 16:45:38 +08:00
2018-09-04 22:42:34 +08:00
wfx_ = (WAVEFORMATEX*) new CHAR[sizeof(WAVEFORMATEX) + cbExtraBytes];
2018-02-01 22:07:44 +08:00
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-09-04 22:42:34 +08:00
memcpy(wfx_, &pcmWaveFormat, sizeof(pcmWaveFormat));
wfx_->cbSize = cbExtraBytes;
2018-01-30 16:45:38 +08:00
2018-02-01 22:07:44 +08:00
// <20><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-09-04 22:42:34 +08:00
if (mmioRead(hmmio_, (CHAR*)(((BYTE*)&(wfx_->cbSize)) + sizeof(WORD)),
2018-02-01 22:07:44 +08:00
cbExtraBytes) != cbExtraBytes)
{
2018-09-04 22:42:34 +08:00
SAFE_DELETE(wfx_);
2018-02-01 22:07:44 +08:00
return TraceError(L"mmioRead");
}
}
2018-09-04 22:42:34 +08:00
if (0 != mmioAscend(hmmio_, &ckIn, 0))
2018-01-30 16:45:38 +08:00
{
2018-09-04 22:42:34 +08:00
SAFE_DELETE(wfx_);
2018-02-01 22:07:44 +08:00
return TraceError(L"mmioAscend");
2018-01-30 16:45:38 +08:00
}
2018-02-01 22:07:44 +08:00
return true;
2018-01-30 16:45:38 +08:00
}
2018-09-06 23:26:32 +08:00
bool e2d::Music::ResetFile()
2018-01-30 16:45:38 +08:00
{
2018-02-01 22:07:44 +08:00
// Seek to the data
2018-09-04 22:42:34 +08:00
if (-1 == mmioSeek(hmmio_, ck_riff_.dwDataOffset + sizeof(FOURCC),
2018-02-01 22:07:44 +08:00
SEEK_SET))
return TraceError(L"mmioSeek");
2018-01-30 16:45:38 +08:00
2018-02-01 22:07:44 +08:00
// Search the input file for the 'data' chunk.
2018-09-04 22:42:34 +08:00
ck_.ckid = mmioFOURCC('d', 'a', 't', 'a');
if (0 != mmioDescend(hmmio_, &ck_, &ck_riff_, MMIO_FINDCHUNK))
2018-02-01 22:07:44 +08:00
return TraceError(L"mmioDescend");
2018-01-30 16:45:38 +08:00
2018-02-01 22:07:44 +08:00
return true;
2018-01-30 16:45:38 +08:00
}
2018-09-06 23:26:32 +08:00
bool e2d::Music::Read(BYTE* buffer, DWORD size_to_read)
2018-01-30 16:45:38 +08:00
{
2018-09-04 22:42:34 +08:00
MMIOINFO mmioinfoIn; // current status of hmmio_
2018-01-30 16:45:38 +08:00
2018-09-04 22:42:34 +08:00
if (0 != mmioGetInfo(hmmio_, &mmioinfoIn, 0))
2018-02-01 22:07:44 +08:00
return TraceError(L"mmioGetInfo");
2018-01-30 16:45:38 +08:00
2018-09-04 22:42:34 +08:00
UINT cbDataIn = size_to_read;
if (cbDataIn > ck_.cksize)
cbDataIn = ck_.cksize;
2018-01-30 16:45:38 +08:00
2018-09-04 22:42:34 +08:00
ck_.cksize -= cbDataIn;
2018-01-30 16:45:38 +08:00
2018-05-14 22:51:40 +08:00
for (DWORD cT = 0; cT < cbDataIn; ++cT)
2018-02-01 22:07:44 +08:00
{
// Copy the bytes from the io to the buffer.
if (mmioinfoIn.pchNext == mmioinfoIn.pchEndRead)
{
2018-09-04 22:42:34 +08:00
if (0 != mmioAdvance(hmmio_, &mmioinfoIn, MMIO_READ))
2018-02-01 22:07:44 +08:00
return TraceError(L"mmioAdvance");
2018-01-30 16:45:38 +08:00
2018-02-01 22:07:44 +08:00
if (mmioinfoIn.pchNext == mmioinfoIn.pchEndRead)
return TraceError(L"mmioinfoIn.pchNext");
}
2018-01-30 16:45:38 +08:00
2018-02-01 22:07:44 +08:00
// Actual copy.
2018-09-04 22:42:34 +08:00
*((BYTE*)buffer + cT) = *((BYTE*)mmioinfoIn.pchNext);
2018-05-14 22:51:40 +08:00
++mmioinfoIn.pchNext;
2018-01-30 16:45:38 +08:00
}
2018-09-04 22:42:34 +08:00
if (0 != mmioSetInfo(hmmio_, &mmioinfoIn, 0))
2018-02-01 22:07:44 +08:00
return TraceError(L"mmioSetInfo");
2018-01-30 16:45:38 +08:00
2018-02-01 22:07:44 +08:00
return true;
2018-01-30 16:45:38 +08:00
}
2018-09-06 23:26:32 +08:00
bool e2d::Music::FindMediaFileCch(wchar_t* dest_path, int cch_dest, const wchar_t * file_name)
2018-01-30 16:45:38 +08:00
{
2018-02-01 22:07:44 +08:00
bool bFound = false;
2018-01-30 16:45:38 +08:00
2018-09-04 22:42:34 +08:00
if (nullptr == file_name || nullptr == dest_path || cch_dest < 10)
2018-02-01 22:07:44 +08:00
return false;
2018-09-04 22:42:34 +08:00
// Get the exe name, and exe path
2018-02-28 19:17:15 +08:00
wchar_t strExePath[MAX_PATH] = { 0 };
wchar_t strExeName[MAX_PATH] = { 0 };
2018-02-01 22:07:44 +08:00
GetModuleFileName(HINST_THISCOMPONENT, strExePath, MAX_PATH);
strExePath[MAX_PATH - 1] = 0;
2018-07-25 16:44:22 +08:00
wchar_t* strLastSlash = wcsrchr(strExePath, TEXT('\\'));
2018-02-01 22:07:44 +08:00
if (strLastSlash)
2018-01-30 16:45:38 +08:00
{
2018-02-01 22:07:44 +08:00
wcscpy_s(strExeName, MAX_PATH, &strLastSlash[1]);
2018-01-30 16:45:38 +08:00
2018-09-04 22:42:34 +08:00
// Chop the exe name from the exe path
2018-02-01 22:07:44 +08:00
*strLastSlash = 0;
2018-01-30 16:45:38 +08:00
2018-09-04 22:42:34 +08:00
// Chop the .exe from the exe name
2018-02-01 22:07:44 +08:00
strLastSlash = wcsrchr(strExeName, TEXT('.'));
if (strLastSlash)
*strLastSlash = 0;
2018-01-30 16:45:38 +08:00
}
2018-09-04 22:42:34 +08:00
wcscpy_s(dest_path, cch_dest, file_name);
if (GetFileAttributes(dest_path) != 0xFFFFFFFF)
2018-02-01 22:07:44 +08:00
return true;
2018-01-30 16:45:38 +08:00
2018-09-04 22:42:34 +08:00
// Search all parent directories starting At .\ and using file_name as the leaf name
2018-02-28 19:17:15 +08:00
wchar_t strLeafName[MAX_PATH] = { 0 };
2018-09-04 22:42:34 +08:00
wcscpy_s(strLeafName, MAX_PATH, file_name);
2018-02-01 22:07:44 +08:00
2018-02-28 19:17:15 +08:00
wchar_t strFullPath[MAX_PATH] = { 0 };
wchar_t strFullFileName[MAX_PATH] = { 0 };
wchar_t strSearch[MAX_PATH] = { 0 };
wchar_t* strFilePart = nullptr;
2018-02-01 22:07:44 +08:00
GetFullPathName(L".", MAX_PATH, strFullPath, &strFilePart);
if (strFilePart == nullptr)
return false;
while (strFilePart != nullptr && *strFilePart != '\0')
2018-01-30 16:45:38 +08:00
{
2018-02-01 22:07:44 +08:00
swprintf_s(strFullFileName, MAX_PATH, L"%s\\%s", strFullPath, strLeafName);
if (GetFileAttributes(strFullFileName) != 0xFFFFFFFF)
{
2018-09-04 22:42:34 +08:00
wcscpy_s(dest_path, cch_dest, strFullFileName);
2018-02-01 22:07:44 +08:00
bFound = true;
break;
}
swprintf_s(strFullFileName, MAX_PATH, L"%s\\%s\\%s", strFullPath, strExeName, strLeafName);
if (GetFileAttributes(strFullFileName) != 0xFFFFFFFF)
{
2018-09-04 22:42:34 +08:00
wcscpy_s(dest_path, cch_dest, strFullFileName);
2018-02-01 22:07:44 +08:00
bFound = true;
break;
}
swprintf_s(strSearch, MAX_PATH, L"%s\\..", strFullPath);
GetFullPathName(strSearch, MAX_PATH, strFullPath, &strFilePart);
2018-01-30 16:45:38 +08:00
}
2018-02-01 22:07:44 +08:00
if (bFound)
return true;
// ʧ<><CAA7>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD>Ϊ·<CEAA><C2B7><EFBFBD><EFBFBD><EFBFBD>أ<EFBFBD>ͬʱҲ<CAB1><D2B2><EFBFBD>ش<EFBFBD><D8B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-09-04 22:42:34 +08:00
wcscpy_s(dest_path, cch_dest, file_name);
2018-02-01 22:07:44 +08:00
return false;
2018-01-30 16:45:38 +08:00
}