Library for Skeleton of SD card player. SD カードプレーヤのための骨組みとして使うためのライブラリ.このライブラリを登録した際のプログラム:「F746_SD_Player_Demo」

Dependencies:   SDFileSystem_Warning_Fixed

Dependents:   F746_SD_Player_Demo F746_SD_VarableFilter F746_SD_GraphicEqualizer F746_MySoundMachine ... more

Committer:
MikamiUitOpen
Date:
Mon Aug 15 04:38:45 2016 +0000
Revision:
0:d310bb78455d
Child:
11:c33c711a5712
1

Who changed what in which revision?

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