OSDN Git Service

[Feature] Windows版の効果音再生waveOut APIへ移行
[hengbandforosx/hengbandosx.git] / src / main-win / wav-reader.cpp
1 /*!
2  * @file wav-reader.cpp
3  * @brief Windows版固有実装(WAVファイル読込)
4  */
5
6 #include "main-win/wav-reader.h"
7 #include "main-win/main-win-utils.h"
8
9 bool wav_reader::open(char *filename)
10 {
11     close();
12
13     this->hmmio = ::mmioOpenW(to_wchar(filename).wc_str(), NULL, MMIO_READ);
14     if (this->hmmio == NULL)
15         return false;
16
17     MMRESULT mmresult;
18     LONG read_size;
19     LONG readed_size;
20
21     this->riff_chunk.fccType = mmioFOURCC('W', 'A', 'V', 'E');
22     mmresult = ::mmioDescend(this->hmmio, &this->riff_chunk, NULL, MMIO_FINDRIFF);
23     if (mmresult != MMSYSERR_NOERROR)
24         return false;
25
26     this->fmt_chunk.ckid = mmioFOURCC('f', 'm', 't', ' ');
27     mmresult = ::mmioDescend(this->hmmio, &this->fmt_chunk, &this->riff_chunk, MMIO_FINDCHUNK);
28     if (mmresult != MMSYSERR_NOERROR)
29         return false;
30
31     if (this->fmt_chunk.cksize > sizeof(this->waveformatex))
32         return false;
33     read_size = this->fmt_chunk.cksize;
34     readed_size = ::mmioRead(this->hmmio, (HPSTR) & this->waveformatex, read_size);
35     if (readed_size != read_size)
36         return false;
37     if (this->waveformatex.wFormatTag != WAVE_FORMAT_PCM)
38         return false;
39     mmresult = ::mmioAscend(this->hmmio, &this->fmt_chunk, 0);
40     if (mmresult != MMSYSERR_NOERROR)
41         return false;
42
43     this->data_chunk.ckid = mmioFOURCC('d', 'a', 't', 'a');
44     mmresult = ::mmioDescend(this->hmmio, &this->data_chunk, &riff_chunk, MMIO_FINDCHUNK);
45     if (mmresult != MMSYSERR_NOERROR)
46         return false;
47
48     this->buffer.reset(new BYTE[data_chunk.cksize]);
49     read_size = this->data_chunk.cksize;
50     readed_size = ::mmioRead(this->hmmio, (HPSTR)this->buffer.get(), read_size);
51     if (readed_size != read_size) {
52         return false;
53     }
54
55     return true;
56 }
57
58 BYTE *wav_reader::read_data()
59 {
60     return this->buffer.release();
61 }
62
63 void wav_reader::close()
64 {
65     if (this->hmmio != NULL) {
66         ::mmioClose(this->hmmio, 0);
67         this->hmmio = NULL;
68     }
69 }