OSDN Git Service

Merge pull request #3569 from sikabane-works/release/3.0.0.88-alpha
[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
18     MMRESULT mmresult;
19     LONG read_size;
20     LONG readed_size;
21
22     this->riff_chunk.fccType = mmioFOURCC('W', 'A', 'V', 'E');
23     mmresult = ::mmioDescend(this->hmmio, &this->riff_chunk, NULL, MMIO_FINDRIFF);
24     if (mmresult != MMSYSERR_NOERROR) {
25         return false;
26     }
27
28     this->fmt_chunk.ckid = mmioFOURCC('f', 'm', 't', ' ');
29     mmresult = ::mmioDescend(this->hmmio, &this->fmt_chunk, &this->riff_chunk, MMIO_FINDCHUNK);
30     if (mmresult != MMSYSERR_NOERROR) {
31         return false;
32     }
33
34     if (this->fmt_chunk.cksize > sizeof(this->waveformatex)) {
35         return false;
36     }
37     read_size = this->fmt_chunk.cksize;
38     readed_size = ::mmioRead(this->hmmio, (HPSTR) & this->waveformatex, read_size);
39     if (readed_size != read_size) {
40         return false;
41     }
42     if (this->waveformatex.wFormatTag != WAVE_FORMAT_PCM) {
43         return false;
44     }
45     mmresult = ::mmioAscend(this->hmmio, &this->fmt_chunk, 0);
46     if (mmresult != MMSYSERR_NOERROR) {
47         return false;
48     }
49
50     this->data_chunk.ckid = mmioFOURCC('d', 'a', 't', 'a');
51     mmresult = ::mmioDescend(this->hmmio, &this->data_chunk, &riff_chunk, MMIO_FINDCHUNK);
52     if (mmresult != MMSYSERR_NOERROR) {
53         return false;
54     }
55
56     this->buffer.reset(new BYTE[data_chunk.cksize]);
57     read_size = this->data_chunk.cksize;
58     readed_size = ::mmioRead(this->hmmio, (HPSTR)this->buffer.get(), read_size);
59     if (readed_size != read_size) {
60         return false;
61     }
62
63     return true;
64 }
65
66 BYTE *wav_reader::read_data()
67 {
68     return this->buffer.release();
69 }
70
71 void wav_reader::close()
72 {
73     if (this->hmmio != NULL) {
74         ::mmioClose(this->hmmio, 0);
75         this->hmmio = NULL;
76     }
77 }