Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: SDFileSystem_Warning_Fixed
SD_WavReader.cpp
- Committer:
- phungductung
- Date:
- 2019-06-04
- Revision:
- 18:6631cd0fbbcd
- Parent:
- 16:299cc1052baa
File content as of revision 18:6631cd0fbbcd:
#include "SD_WavReader.hpp"
namespace Mikami
{
SD_WavReader::SD_WavReader(int32_t bufferSize)
: STR_("sd"), ok_(false)
{
sd_ = new SDFileSystem(STR_.c_str());
sd_->mount();
buffer.SetSize(bufferSize*2);
}
SD_WavReader::~SD_WavReader()
{
sd_->unmount();
delete sd_;
}
void SD_WavReader::Open(const string fileName)
{
string name = (string)"/" + STR_ + "/" + fileName;
fp_ = fopen(name.c_str(), "rb");
if (fp_ == NULL) ErrorMsg("Loi The SD!!!");
}
// Đọc một phần của tiêu đề tệp "RIFFxxxxWAVEfmt"
// Giá trị trả về: true nếu âm thanh nổi 16 bit, tần số lấy mẫu là 44,1 kHz
bool SD_WavReader::IsWavFile()
{
char data[17];
fread(data, 1, 16, fp_); // Đọc 16 byte
string strRead = "";
for (int n=0; n<4; n++) strRead += data[n];
// Không kiểm tra 4 ký tự giữa
for (int n=8; n<16; n++) strRead += data[n];
// Kiểm tra xem "RIFF", "WAVE", "fmt" có tồn tại không
if (strRead != "RIFFWAVEfmt ") return false;
// có được kích thước của fmt chunck
uint32_t fmtChunkSize;
fread(&fmtChunkSize, sizeof(uint32_t), 1, fp_);
// Xác nhận rằng đó là PCM, Stereo, 44.1 kHz, 16 bit
WaveFormatEx fmtData;
fread(&fmtData, fmtChunkSize, 1, fp_);
if ((fmtData.wFormatTag != 1) ||
(fmtData.nChannels != 2) ||
(fmtData.nSamplesPerSec != AUDIO_FREQUENCY_44K) ||
(fmtData.wBitsPerSample != 16)
) return false;
// Tìm dữ liệu
char dataId[5];
dataId[4] = 0;
fread(dataId, 1, 4, fp_);
if ("data" != (string)dataId)
for (int n=0; n<100; n++)
{
char oneByte;
fread(&oneByte, 1, 1, fp_);
for (int k=0; k<3; k++)
dataId[k] = dataId[k+1];
dataId[3] = oneByte;
if ("data" == (string)dataId) break;
if (n == 99) return false;
}
// Nhận kích thước dữ liệu (byte)
int32_t sizeByte;
fread(&sizeByte, sizeof(int32_t), 1, fp_);
size_ = sizeByte/4;
ok_ = true;
return true;
}
// Nhận dữ liệu âm thanh nổi từ tập tin
void SD_WavReader::ReadStereo(Array<int16_t>& dataL,
Array<int16_t>& dataR)
{
if (!ok_) ErrorMsg("Get data FAILED");
uint32_t size = dataL.Length();
fread(buffer, sizeof(int16_t), size*2, fp_);
for (int n=0; n<size; n++)
{
dataL[n] = buffer[2*n];
dataR[n] = buffer[2*n+1];
}
}
// Chuyển đổi dữ liệu từ tập tin sang đơn âm
void SD_WavReader::ReadAndToMono(Array<int16_t>& data)
{
if (!ok_) ErrorMsg("Get data FAILED");
uint32_t size = data.Length();
fread(buffer, sizeof(int16_t), size*2, fp_);
for (int n=0; n<size; n++)
data[n] = (buffer[2*n] + buffer[2*n+1])/2;
}
// Thu thập kích thước dữ liệu (số điểm lấy mẫu)
int32_t SD_WavReader::GetSize()
{
if (!ok_) ErrorMsg("Get data size FAILED");
return size_;
}
}