Created
April 20, 2014 08:25
-
-
Save hirosof/11108567 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <stdio.h> | |
| #include <Windows.h> | |
| #include <tchar.h> | |
| #include <xaudio2.h> | |
| #include "HSWaveClass/CHSWAVEReader.hpp" //自前Waveファイル読み込みクラス | |
| #pragma comment(lib,"Xaudio2.lib") | |
| //プロトタイプ宣言 | |
| void PrintoutSongInfo(CHSWAVEReader *pReader); | |
| namespace myapp { | |
| IXAudio2 *pXaudio = nullptr; | |
| IXAudio2MasteringVoice *pMasterVoice = nullptr; | |
| IXAudio2SourceVoice *pSourceVoice = nullptr; | |
| HRESULT hr; | |
| void *pPlayData = nullptr; | |
| UINT32 PlayDataSize = 0; | |
| UINT32 ReadDataSamples = 0; | |
| bool Init(void) { | |
| hr = XAudio2Create(&pXaudio); | |
| if (FAILED(hr)) return false; | |
| hr = pXaudio->CreateMasteringVoice(&pMasterVoice); | |
| return (SUCCEEDED(hr)) ? true : false; | |
| } | |
| bool Play(CHSWAVEReader *pReader) { | |
| if (pReader == nullptr) return false; | |
| bool bRet = true; | |
| WAVEFORMATEX wfex; | |
| //フォーマット情報を読み込み | |
| if(pReader->GetFormat(&wfex) == false) return false; | |
| hr = pXaudio->CreateSourceVoice(&pSourceVoice , &wfex); | |
| if (FAILED(hr)) return false; | |
| try { | |
| UINT32 samples = pReader->GetPlaySamples(); | |
| PlayDataSize = pReader->CalculateMemorySize(samples); | |
| pPlayData = malloc(PlayDataSize); | |
| pReader->ReadPlayData(pPlayData , 0 , samples , &ReadDataSamples); | |
| XAUDIO2_BUFFER buffer = { 0 }; | |
| buffer.AudioBytes = pReader->CalculateMemorySize(ReadDataSamples); | |
| buffer.pAudioData = (BYTE*)pPlayData; | |
| buffer.Flags = XAUDIO2_END_OF_STREAM; | |
| hr = pSourceVoice->SubmitSourceBuffer(&buffer); | |
| if (FAILED(hr)) throw 0; | |
| hr = pSourceVoice->Start(); | |
| if (FAILED(hr)) throw 1; | |
| } catch (int erno) { | |
| if (erno >= 0){ | |
| pSourceVoice->DestroyVoice(); | |
| free(pPlayData); | |
| } | |
| bRet = false; | |
| } | |
| return bRet; | |
| } | |
| bool Stop(void) { | |
| if (pSourceVoice) { | |
| pSourceVoice->Stop(); | |
| pSourceVoice->DestroyVoice(); | |
| free(pPlayData); | |
| } | |
| return true; | |
| } | |
| void Cleanup(void) { | |
| if (pMasterVoice)pMasterVoice->DestroyVoice(); | |
| if (pXaudio)pXaudio->Release(); | |
| } | |
| }; | |
| int main(void) { | |
| //COM初期化 | |
| CoInitializeEx(NULL , COINIT_APARTMENTTHREADED); | |
| //自前WAVEファイル読み込みクラス | |
| CHSWAVEReader cReader; | |
| //WAVEファイルを開く | |
| if (cReader.Open(_T("03-Battle-b4-1411kbps.wav"))) { | |
| //曲情報を表示する | |
| PrintoutSongInfo(&cReader); | |
| //区切り線 | |
| printf("------------------------------\n"); | |
| printf("XAudio2を初期化します。\n"); | |
| bool bRet = myapp::Init(); | |
| if (bRet) { | |
| printf("成功しました。\n"); | |
| printf("1秒間だけ再生します。\n"); | |
| myapp::Play(&cReader); | |
| printf("何かキーを押すと再生を停止します。"); | |
| getchar(); | |
| myapp::Stop(); | |
| } else { | |
| printf("失敗しました。\n"); | |
| } | |
| myapp::Cleanup(); | |
| //閉じる(忘れてもデストラクタで自動で閉じられる) | |
| cReader.Close(); | |
| } else { | |
| printf("WAVEファイルの読み込みに失敗しました。\n"); | |
| } | |
| //COM解放 | |
| CoUninitialize(); | |
| return 0; | |
| } | |
| void PrintoutSongInfo(CHSWAVEReader *pReader) { | |
| char *pText = nullptr; | |
| unsigned int size = 0; | |
| printf("<< 曲情報 >>\n"); | |
| size = pReader->GetSongNameLength(); | |
| if (size) { | |
| pText = new char[size + 1]; | |
| *(pText + size) = 0; | |
| if (pReader->GetSongName(pText, size)) { | |
| printf("曲名:%s\n" , pText); | |
| } | |
| delete[]pText; | |
| } | |
| size = pReader->GetSongArtistNameLength(); | |
| if (size) { | |
| pText = new char[size + 1]; | |
| *(pText + size) = 0; | |
| if (pReader->GetSongArtistName(pText, size)) { | |
| printf("アーティスト:%s\n" , pText); | |
| } | |
| delete[]pText; | |
| } | |
| size = pReader->GetSongProductNameLength(); | |
| if (size) { | |
| pText = new char[size + 1]; | |
| *(pText + size) = 0; | |
| if (pReader->GetSongProductName(pText, size)) { | |
| printf("表品名/アルバム名:%s\n" , pText); | |
| } | |
| delete[]pText; | |
| } | |
| size = pReader->GetSongGenreTextLength(); | |
| if (size) { | |
| pText = new char[size + 1]; | |
| *(pText + size) = 0; | |
| if (pReader->GetSongGenreText(pText, size)) { | |
| printf("ジャンル:%s\n" , pText); | |
| } | |
| delete[]pText; | |
| } | |
| UINT32 length = pReader->GetPlayTimeMilliSeconds(); | |
| printf("長さ: %02d:%02d:%02d.%03d\n" , | |
| (length / 1000) / 3600 , | |
| ((length / 1000) % 3600) / 60, | |
| (length/1000) % 60, | |
| length % 1000 | |
| ); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment