?
SD_WavReader.cpp@0:cacfc9e25452, 2019-06-04 (annotated)
- Committer:
- phungductung
- Date:
- Tue Jun 04 21:58:08 2019 +0000
- Revision:
- 0:cacfc9e25452
?;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
phungductung | 0:cacfc9e25452 | 1 | //-------------------------------------------------------------- |
phungductung | 0:cacfc9e25452 | 2 | // SD_WavReader class |
phungductung | 0:cacfc9e25452 | 3 | // SD カードの *.wav ファイルの内容を読み出す |
phungductung | 0:cacfc9e25452 | 4 | // 以下のフォーマット以外は扱わない |
phungductung | 0:cacfc9e25452 | 5 | // PCM,16 ビットステレオ,標本化周波数 44.1 kHz |
phungductung | 0:cacfc9e25452 | 6 | // |
phungductung | 0:cacfc9e25452 | 7 | // 2016/06/17, Copyright (c) 2016 MIKAMI, Naoki |
phungductung | 0:cacfc9e25452 | 8 | //-------------------------------------------------------------- |
phungductung | 0:cacfc9e25452 | 9 | |
phungductung | 0:cacfc9e25452 | 10 | #include "SD_WavReader.hpp" |
phungductung | 0:cacfc9e25452 | 11 | |
phungductung | 0:cacfc9e25452 | 12 | namespace Mikami |
phungductung | 0:cacfc9e25452 | 13 | { |
phungductung | 0:cacfc9e25452 | 14 | SD_WavReader::SD_WavReader(int32_t bufferSize) |
phungductung | 0:cacfc9e25452 | 15 | : STR_("sd"), ok_(false) |
phungductung | 0:cacfc9e25452 | 16 | { |
phungductung | 0:cacfc9e25452 | 17 | sd_ = new SDFileSystem(STR_.c_str()); |
phungductung | 0:cacfc9e25452 | 18 | sd_->mount(); |
phungductung | 0:cacfc9e25452 | 19 | buffer.SetSize(bufferSize*2); |
phungductung | 0:cacfc9e25452 | 20 | } |
phungductung | 0:cacfc9e25452 | 21 | |
phungductung | 0:cacfc9e25452 | 22 | SD_WavReader::~SD_WavReader() |
phungductung | 0:cacfc9e25452 | 23 | { |
phungductung | 0:cacfc9e25452 | 24 | sd_->unmount(); |
phungductung | 0:cacfc9e25452 | 25 | delete sd_; |
phungductung | 0:cacfc9e25452 | 26 | } |
phungductung | 0:cacfc9e25452 | 27 | |
phungductung | 0:cacfc9e25452 | 28 | void SD_WavReader::Open(const string fileName) |
phungductung | 0:cacfc9e25452 | 29 | { |
phungductung | 0:cacfc9e25452 | 30 | string name = (string)"/" + STR_ + "/" + fileName; |
phungductung | 0:cacfc9e25452 | 31 | fp_ = fopen(name.c_str(), "rb"); |
phungductung | 0:cacfc9e25452 | 32 | if (fp_ == NULL) ErrorMsg("Khong the mo!!"); |
phungductung | 0:cacfc9e25452 | 33 | } |
phungductung | 0:cacfc9e25452 | 34 | |
phungductung | 0:cacfc9e25452 | 35 | |
phungductung | 0:cacfc9e25452 | 36 | // ファイルのヘッダ (RIFFxxxxWAVEfm ) 読み込み |
phungductung | 0:cacfc9e25452 | 37 | // 戻り値: *.wav で,16 ビットステレオ, |
phungductung | 0:cacfc9e25452 | 38 | // 標本化周波数:44.1 kHz の場合 true |
phungductung | 0:cacfc9e25452 | 39 | bool SD_WavReader::IsWavFile() |
phungductung | 0:cacfc9e25452 | 40 | { |
phungductung | 0:cacfc9e25452 | 41 | char data[17]; |
phungductung | 0:cacfc9e25452 | 42 | fread(data, 1, 16, fp_); |
phungductung | 0:cacfc9e25452 | 43 | string strRead = ""; |
phungductung | 0:cacfc9e25452 | 44 | for (int n=0; n<4; n++) strRead += data[n]; |
phungductung | 0:cacfc9e25452 | 45 | for (int n=8; n<16; n++) strRead += data[n]; |
phungductung | 0:cacfc9e25452 | 46 | |
phungductung | 0:cacfc9e25452 | 47 | // "RIFF", "WAVE", "fmt " が存在することを確認 |
phungductung | 0:cacfc9e25452 | 48 | if (strRead != "RIFFWAVEfmt ") return false; |
phungductung | 0:cacfc9e25452 | 49 | |
phungductung | 0:cacfc9e25452 | 50 | // fmt chunck のサイズを取得 |
phungductung | 0:cacfc9e25452 | 51 | uint32_t fmtChunkSize; |
phungductung | 0:cacfc9e25452 | 52 | fread(&fmtChunkSize, sizeof(uint32_t), 1, fp_); |
phungductung | 0:cacfc9e25452 | 53 | |
phungductung | 0:cacfc9e25452 | 54 | // PCM, Stereo, 44.1 kHz, 16 bit であることを確認 |
phungductung | 0:cacfc9e25452 | 55 | WaveFormatEx fmtData; |
phungductung | 0:cacfc9e25452 | 56 | fread(&fmtData, fmtChunkSize, 1, fp_); |
phungductung | 0:cacfc9e25452 | 57 | if ((fmtData.wFormatTag != 1) || |
phungductung | 0:cacfc9e25452 | 58 | (fmtData.nChannels != 2) || |
phungductung | 0:cacfc9e25452 | 59 | (fmtData.nSamplesPerSec != 44100) || |
phungductung | 0:cacfc9e25452 | 60 | (fmtData.wBitsPerSample != 16) |
phungductung | 0:cacfc9e25452 | 61 | ) return false; |
phungductung | 0:cacfc9e25452 | 62 | |
phungductung | 0:cacfc9e25452 | 63 | // data chunk を探す |
phungductung | 0:cacfc9e25452 | 64 | char dataId[5]; |
phungductung | 0:cacfc9e25452 | 65 | dataId[4] = 0; |
phungductung | 0:cacfc9e25452 | 66 | fread(dataId, 1, 4, fp_); |
phungductung | 0:cacfc9e25452 | 67 | if ("data" != (string)dataId) |
phungductung | 0:cacfc9e25452 | 68 | for (int n=0; n<100; n++) |
phungductung | 0:cacfc9e25452 | 69 | { |
phungductung | 0:cacfc9e25452 | 70 | char oneByte; |
phungductung | 0:cacfc9e25452 | 71 | fread(&oneByte, 1, 1, fp_); |
phungductung | 0:cacfc9e25452 | 72 | for (int k=0; k<3; k++) |
phungductung | 0:cacfc9e25452 | 73 | dataId[k] = dataId[k+1]; |
phungductung | 0:cacfc9e25452 | 74 | dataId[3] = oneByte; |
phungductung | 0:cacfc9e25452 | 75 | if ("data" == (string)dataId) break; |
phungductung | 0:cacfc9e25452 | 76 | |
phungductung | 0:cacfc9e25452 | 77 | if (n == 99) return false; |
phungductung | 0:cacfc9e25452 | 78 | } |
phungductung | 0:cacfc9e25452 | 79 | |
phungductung | 0:cacfc9e25452 | 80 | // データサイズ (byte) を取得 |
phungductung | 0:cacfc9e25452 | 81 | int32_t sizeByte; |
phungductung | 0:cacfc9e25452 | 82 | fread(&sizeByte, sizeof(int32_t), 1, fp_); |
phungductung | 0:cacfc9e25452 | 83 | size_ = sizeByte/4; |
phungductung | 0:cacfc9e25452 | 84 | |
phungductung | 0:cacfc9e25452 | 85 | ok_ = true; |
phungductung | 0:cacfc9e25452 | 86 | return true; |
phungductung | 0:cacfc9e25452 | 87 | } |
phungductung | 0:cacfc9e25452 | 88 | |
phungductung | 0:cacfc9e25452 | 89 | // ファイルからステレオデータの取得 |
phungductung | 0:cacfc9e25452 | 90 | void SD_WavReader::ReadStereo(Array<int16_t>& dataL, |
phungductung | 0:cacfc9e25452 | 91 | Array<int16_t>& dataR) |
phungductung | 0:cacfc9e25452 | 92 | { |
phungductung | 0:cacfc9e25452 | 93 | if (!ok_) ErrorMsg("Lay du lieu that bai"); |
phungductung | 0:cacfc9e25452 | 94 | uint32_t size = dataL.Length(); |
phungductung | 0:cacfc9e25452 | 95 | fread(buffer, sizeof(int16_t), size*2, fp_); |
phungductung | 0:cacfc9e25452 | 96 | for (int n=0; n<size; n++) |
phungductung | 0:cacfc9e25452 | 97 | { |
phungductung | 0:cacfc9e25452 | 98 | dataL[n] = buffer[2*n]; |
phungductung | 0:cacfc9e25452 | 99 | dataR[n] = buffer[2*n+1]; |
phungductung | 0:cacfc9e25452 | 100 | } |
phungductung | 0:cacfc9e25452 | 101 | } |
phungductung | 0:cacfc9e25452 | 102 | |
phungductung | 0:cacfc9e25452 | 103 | // ファイルからデータをモノラルに変換しての取得 |
phungductung | 0:cacfc9e25452 | 104 | void SD_WavReader::ReadAndToMono(Array<int16_t>& data) |
phungductung | 0:cacfc9e25452 | 105 | { |
phungductung | 0:cacfc9e25452 | 106 | if (!ok_) ErrorMsg("Lay du lieu that bai"); |
phungductung | 0:cacfc9e25452 | 107 | uint32_t size = data.Length(); |
phungductung | 0:cacfc9e25452 | 108 | fread(buffer, sizeof(int16_t), size*2, fp_); |
phungductung | 0:cacfc9e25452 | 109 | for (int n=0; n<size; n++) |
phungductung | 0:cacfc9e25452 | 110 | data[n] = (buffer[2*n] + buffer[2*n+1])/2; |
phungductung | 0:cacfc9e25452 | 111 | } |
phungductung | 0:cacfc9e25452 | 112 | |
phungductung | 0:cacfc9e25452 | 113 | // データサイズ(標本化点の数)の取得 |
phungductung | 0:cacfc9e25452 | 114 | int32_t SD_WavReader::GetSize() |
phungductung | 0:cacfc9e25452 | 115 | { |
phungductung | 0:cacfc9e25452 | 116 | if (!ok_) ErrorMsg("Loi dung luong du lieu"); |
phungductung | 0:cacfc9e25452 | 117 | return size_; |
phungductung | 0:cacfc9e25452 | 118 | } |
phungductung | 0:cacfc9e25452 | 119 | } |