379 lines
9.6 KiB
C++
379 lines
9.6 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"
|
|
|
|
#include <FLAC++/decoder.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;
|
|
}
|
|
|
|
static FLAC__StreamDecoderWriteStatus write_callback(const FLAC__StreamDecoder* decoder, const FLAC__Frame* frame, const FLAC__int32* const buffer[], void* client_data);
|
|
static void metadata_callback(const FLAC__StreamDecoder* decoder, const FLAC__StreamMetadata* metadata, void* client_data);
|
|
static void error_callback(const FLAC__StreamDecoder* decoder, FLAC__StreamDecoderErrorStatus status, void* client_data);
|
|
|
|
static FLAC__bool write_little_endian_uint16(FILE* f, FLAC__uint16 x)
|
|
{
|
|
return
|
|
fputc(x, f) != EOF &&
|
|
fputc(x >> 8, f) != EOF
|
|
;
|
|
}
|
|
|
|
static FLAC__bool write_little_endian_int16(FILE* f, FLAC__int16 x)
|
|
{
|
|
return write_little_endian_uint16(f, (FLAC__uint16)x);
|
|
}
|
|
|
|
static FLAC__bool write_little_endian_uint32(FILE* f, FLAC__uint32 x)
|
|
{
|
|
return
|
|
fputc(x, f) != EOF &&
|
|
fputc(x >> 8, f) != EOF &&
|
|
fputc(x >> 16, f) != EOF &&
|
|
fputc(x >> 24, f) != EOF
|
|
;
|
|
}
|
|
|
|
FLAC__StreamDecoderWriteStatus write_callback(const FLAC__StreamDecoder* decoder, const FLAC__Frame* frame, const FLAC__int32* const buffer[], void* client_data)
|
|
{
|
|
AudioLoader::PCM* pcm = (AudioLoader::PCM*)client_data;
|
|
const FLAC__uint32 total_size = (FLAC__uint32)(pcm->_freq * pcm->_numChannel * (pcm->_bitPerSample / 8));
|
|
|
|
Buffer& buf = pcm->_buf;
|
|
buf.resize(total_size * 10);
|
|
(void)decoder;
|
|
|
|
if (pcm->_freq == 0)
|
|
{
|
|
fprintf(stderr, "ERROR: this example only works for FLAC files that have a total_samples count in STREAMINFO\n");
|
|
return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
|
|
}
|
|
if (pcm->_numChannel != 2 || pcm->_bitPerSample != 16)
|
|
{
|
|
fprintf(stderr, "ERROR: this example only supports 16bit stereo streams\n");
|
|
return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
|
|
}
|
|
if (frame->header.channels != 2)
|
|
{
|
|
fprintf(stderr, "ERROR: This frame contains %u channels (should be 2)\n", frame->header.channels);
|
|
return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
|
|
}
|
|
if (buffer[0] == NULL)
|
|
{
|
|
fprintf(stderr, "ERROR: buffer [0] is NULL\n");
|
|
return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
|
|
}
|
|
if (buffer[1] == NULL)
|
|
{
|
|
fprintf(stderr, "ERROR: buffer [1] is NULL\n");
|
|
return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
|
|
}
|
|
|
|
/* write WAVE header before we write the first frame */
|
|
if (frame->header.number.sample_number == 0)
|
|
{
|
|
// if (
|
|
// fwrite("RIFF", 1, 4, f) < 4 ||
|
|
// !write_little_endian_uint32(f, total_size + 36) ||
|
|
// fwrite("WAVEfmt ", 1, 8, f) < 8 ||
|
|
// !write_little_endian_uint32(f, 16) ||
|
|
// !write_little_endian_uint16(f, 1) ||
|
|
// !write_little_endian_uint16(f, (FLAC__uint16)channels) ||
|
|
// !write_little_endian_uint32(f, sample_rate) ||
|
|
// !write_little_endian_uint32(f, sample_rate * channels * (bps / 8)) ||
|
|
// !write_little_endian_uint16(f, (FLAC__uint16)(channels * (bps / 8))) || /* block align */
|
|
// !write_little_endian_uint16(f, (FLAC__uint16)bps) ||
|
|
// fwrite("data", 1, 4, f) < 4 ||
|
|
// !write_little_endian_uint32(f, total_size)
|
|
// ) {
|
|
// fprintf(stderr, "ERROR: write error\n");
|
|
// return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
|
|
// }
|
|
}
|
|
/* write decoded PCM samples */
|
|
for (size_t i = 0; i < frame->header.blocksize; ++i)
|
|
{
|
|
// if (
|
|
// !write_little_endian_int16(buf, (FLAC__int16)buffer[0][i]) || /* left channel */
|
|
// !write_little_endian_int16(f, (FLAC__int16)buffer[1][i]) /* right channel */
|
|
// ) {
|
|
// fprintf(stderr, "ERROR: write error\n");
|
|
// return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
|
|
// }
|
|
std::byte* p0 = (std::byte*)(buffer[0] + i);
|
|
buf.insert(buf.end(), p0, p0 + 2);
|
|
std::byte* p1 = (std::byte*)(buffer[1] + i);
|
|
buf.insert(buf.end(), p1, p1 + 2);
|
|
}
|
|
|
|
return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
|
|
}
|
|
|
|
void metadata_callback(const FLAC__StreamDecoder* decoder, const FLAC__StreamMetadata* metadata, void* client_data)
|
|
{
|
|
AudioLoader::PCM* pcm = (AudioLoader::PCM*)client_data;
|
|
|
|
if (metadata->type != FLAC__METADATA_TYPE_STREAMINFO)
|
|
{
|
|
log_err("获取metadata类型失败");
|
|
return;
|
|
}
|
|
pcm->_bitPerSample = metadata->data.stream_info.bits_per_sample;
|
|
pcm->_numChannel = metadata->data.stream_info.channels;
|
|
pcm->_freq = metadata->data.stream_info.sample_rate;
|
|
}
|
|
|
|
void error_callback(const FLAC__StreamDecoder* decoder, FLAC__StreamDecoderErrorStatus status, void* client_data)
|
|
{
|
|
(void)decoder, (void)client_data;
|
|
|
|
fprintf(stderr, "Got error callback: %s\n", FLAC__StreamDecoderErrorStatusString[status]);
|
|
}
|
|
|
|
bool AudioLoader::ReadFlac(std::string_view path_name, PCM& pcm)
|
|
{
|
|
FLAC__bool ok = true;
|
|
FLAC__StreamDecoder* decoder = 0;
|
|
FLAC__StreamDecoderInitStatus init_status;
|
|
|
|
FILE* file = File::GetFile(path_name);
|
|
if (!file)
|
|
{
|
|
log_err("打开文件失败:{}", path_name);
|
|
return false;
|
|
}
|
|
|
|
if ((decoder = FLAC__stream_decoder_new()) == NULL)
|
|
{
|
|
log_err("创建decoder失败!");
|
|
fclose(file);
|
|
return false;
|
|
}
|
|
|
|
(void)FLAC__stream_decoder_set_md5_checking(decoder, true);
|
|
|
|
init_status = FLAC__stream_decoder_init_file(decoder, path_name.data(),
|
|
write_callback, metadata_callback, error_callback, &pcm);
|
|
if (init_status != FLAC__STREAM_DECODER_INIT_STATUS_OK)
|
|
{
|
|
log_err("初始化decoder失败: %s\n", FLAC__StreamDecoderInitStatusString[init_status]);
|
|
ok = false;
|
|
}
|
|
|
|
if (ok)
|
|
{
|
|
ok = FLAC__stream_decoder_process_until_end_of_stream(decoder);
|
|
if (!ok)
|
|
{
|
|
log_msg("解码失败!{}", FLAC__StreamDecoderStateString[FLAC__stream_decoder_get_state(decoder)]);
|
|
}
|
|
}
|
|
|
|
FLAC__stream_decoder_delete(decoder);
|
|
fclose(file);
|
|
return true;
|
|
}
|
|
|
|
} |