209 lines
4.3 KiB
C++
209 lines
4.3 KiB
C++
#include "dl_audio_loader.h"
|
|
|
|
#include <fstream>
|
|
|
|
#include <vorbis\codec.h>
|
|
#include <vorbis\vorbisfile.h>
|
|
|
|
#include "system/dl_platform.h"
|
|
#include "io/dl_file.h"
|
|
#include "memory/dl_stream_output.h"
|
|
|
|
namespace dl
|
|
{
|
|
|
|
#ifndef DL_ENDIAN_LITTLE
|
|
#define fourccRIFF 'RIFF'
|
|
#define fourccDATA 'data'
|
|
#define fourccFMT 'fmt '
|
|
#define fourccWAVE 'WAVE'
|
|
#define fourccXWMA 'XWMA'
|
|
#define fourccDPDS 'dpds'
|
|
#else
|
|
#define fourccRIFF 'FFIR'
|
|
#define fourccDATA 'atad'
|
|
#define fourccFMT ' tmf'
|
|
#define fourccWAVE 'EVAW'
|
|
#define fourccXWMA 'AMWX'
|
|
#define fourccDPDS 'sdpd'
|
|
#endif
|
|
|
|
bool FindChunk(std::ifstream& ifs, uint32_t fourcc, uint32_t& size, uint32_t& pos)
|
|
{
|
|
bool ret = true;
|
|
|
|
ifs.seekg(0);
|
|
if (ifs.fail())
|
|
return false;
|
|
|
|
uint32_t dwChunkType;
|
|
uint32_t dwChunkDataSize;
|
|
uint32_t dwRIFFDataSize = 0;
|
|
uint32_t dwFileType;
|
|
uint32_t bytesRead = 0;
|
|
uint32_t dwOffset = 0;
|
|
|
|
while (ret)
|
|
{
|
|
ifs.read((char*)&dwChunkType, sizeof(uint32_t));
|
|
ifs.read((char*)&dwChunkDataSize, sizeof(uint32_t));
|
|
|
|
switch (dwChunkType)
|
|
{
|
|
case fourccRIFF:
|
|
dwRIFFDataSize = dwChunkDataSize;
|
|
dwChunkDataSize = 4;
|
|
ifs.read((char*)&dwFileType, sizeof(uint32_t));
|
|
break;
|
|
|
|
default:
|
|
ifs.seekg(dwChunkDataSize, std::ios::cur);
|
|
if (ifs.fail())
|
|
return false;
|
|
break;
|
|
}
|
|
|
|
dwOffset += sizeof(uint32_t) * 2;
|
|
|
|
if (dwChunkType == fourcc)
|
|
{
|
|
size = dwChunkDataSize;
|
|
pos = dwOffset;
|
|
return true;
|
|
}
|
|
|
|
dwOffset += dwChunkDataSize;
|
|
|
|
if (bytesRead >= dwRIFFDataSize)
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
bool ReadChunkData(std::ifstream& ifs, void* buffer, uint32_t size, uint32_t pos)
|
|
{
|
|
ifs.seekg(pos);
|
|
if (ifs.fail())
|
|
return false;
|
|
ifs.read((char*)buffer, size);
|
|
return true;
|
|
}
|
|
bool AudioLoader::ReadWav(std::string_view path_name, PCM& pcm)
|
|
{
|
|
std::ifstream ifs = File::Get(path_name);
|
|
if (!ifs.is_open())
|
|
{
|
|
log_err("打开文件失败:{}", path_name);
|
|
return false;
|
|
}
|
|
|
|
uint32_t dwChunkSize;
|
|
uint32_t dwChunkPosition;
|
|
//查找riff块
|
|
FindChunk(ifs, fourccRIFF, dwChunkSize, dwChunkPosition);
|
|
uint32_t filetype;
|
|
ReadChunkData(ifs, &filetype, sizeof(uint32_t), dwChunkPosition);
|
|
|
|
if (filetype != fourccWAVE)
|
|
{
|
|
log_err("匹配标记失败(fourccWAVE):{}", path_name);
|
|
return false;
|
|
}
|
|
//查找fmt块
|
|
if (!FindChunk(ifs, fourccFMT, dwChunkSize, dwChunkPosition))
|
|
{
|
|
log_err("查找块失败(fourccFMT):{}", path_name);
|
|
return false;
|
|
}
|
|
//读wave信息
|
|
WAVEFormat wave_format;
|
|
if (!ReadChunkData(ifs, &wave_format, dwChunkSize, dwChunkPosition))
|
|
{
|
|
log_err("读取块失败(wave_format):{}", path_name);
|
|
return false;
|
|
};
|
|
//查找音频数据
|
|
if (!FindChunk(ifs, fourccDATA, dwChunkSize, dwChunkPosition))
|
|
{
|
|
log_err("查找块失败(fourccDATA):{}", path_name);
|
|
return false;
|
|
};
|
|
|
|
pcm._buf.resize(dwChunkSize);
|
|
|
|
if (!ReadChunkData(ifs, pcm._buf.data(), dwChunkSize, dwChunkPosition))
|
|
{
|
|
log_err("读取块失败(pcm数据):{}", path_name);
|
|
return false;
|
|
};
|
|
|
|
pcm._numChannel = wave_format.numChannels;
|
|
pcm._bitPerSample = wave_format.bitsPerSample;
|
|
pcm._freq = wave_format.sampleRate;
|
|
if (pcm._bitPerSample == 32)
|
|
{
|
|
log_err("不支持32位:{}", path_name);
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
bool AudioLoader::ReadOgg(std::string_view path_name, PCM& pcm)
|
|
{
|
|
FILE* file = File::GetFile(path_name);
|
|
if (!file)
|
|
{
|
|
log_err("打开文件失败:{}", path_name);
|
|
return false;
|
|
}
|
|
|
|
OggVorbis_File vf;
|
|
int eof = 0;
|
|
int current_section;
|
|
|
|
int ov_ret = ov_open_callbacks(file, &vf, NULL, 0, OV_CALLBACKS_NOCLOSE);
|
|
if (ov_ret < 0)
|
|
{
|
|
log_err("不是ogg文件:{}", path_name );
|
|
fclose(file);
|
|
return false;
|
|
}
|
|
|
|
//写入wfx
|
|
{
|
|
vorbis_info* vi = ov_info(&vf, -1);
|
|
|
|
//只需要填充Foramt这一个结构,其他被截断了
|
|
pcm._numChannel = vi->channels;
|
|
pcm._freq = vi->rate;
|
|
pcm._bitPerSample = 16;
|
|
}
|
|
|
|
|
|
//写入buffer
|
|
std::byte pcmout[4096];
|
|
Buffer& buf = pcm._buf;
|
|
|
|
while (!eof) {
|
|
long ret = ov_read(&vf, (char*)pcmout, sizeof(pcmout), 0, 2, 1, ¤t_section);
|
|
if (ret == 0) {
|
|
/* EOF */
|
|
eof = 1;
|
|
}
|
|
else if (ret < 0) {
|
|
/* error in the stream. Not a problem, just reporting it in
|
|
case we (the app) cares. In this case, we don't. */
|
|
}
|
|
else {
|
|
/* we don't bother dealing with sample rate changes, etc, but
|
|
you'll have to*/
|
|
buf.insert(buf.end(), pcmout, pcmout + ret);
|
|
}
|
|
}
|
|
|
|
ov_clear(&vf);
|
|
fclose(file);
|
|
return true;
|
|
}
|
|
|
|
} |