Files
dl_old/dl/system/dl_audio_loader.cpp
T
2024-01-03 20:57:57 +08:00

311 lines
7.1 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#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, &current_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;
}
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;
Buffer& buf = pcm->_buf;
if (pcm->_buf.empty())
{
FLAC__uint32 total_size = (FLAC__uint32)(
pcm->_freq * pcm->_numChannel * (pcm->_bitPerSample / 8));
buf.reserve(total_size);
}
if (pcm->_freq == 0)
{
log_err("采样率为0!");
return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
}
if (pcm->_numChannel != 2 || pcm->_bitPerSample != 16)
{
log_err("通道数不为2,或采样数不为16!");
return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
}
if (frame->header.channels != 2)
{
log_err("frame通道数不为2!");
return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
}
if (buffer[0] == NULL)
{
log_err("buffer[0]为空!");
return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
}
if (buffer[1] == NULL)
{
log_err("buffer[1]为空!");
return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
}
for (size_t i = 0; i < frame->header.blocksize; ++i)
{
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)
{
log_err("出现错误:{}", FLAC__StreamDecoderErrorStatusString[status]);
}
bool AudioLoader::ReadFlac(std::string_view path_name, PCM& pcm)
{
FLAC__StreamDecoder* decoder = FLAC__stream_decoder_new();
if (!decoder)
{
log_err("创建decoder失败!");
return false;
}
(void)FLAC__stream_decoder_set_md5_checking(decoder, true);
FLAC__StreamDecoderInitStatus 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]);
FLAC__stream_decoder_delete(decoder);
return false;
}
FLAC__bool ok = FLAC__stream_decoder_process_until_end_of_stream(decoder);
if (!ok)
{
log_msg("解码失败!{}",
FLAC__StreamDecoderStateString[FLAC__stream_decoder_get_state(decoder)]);
return false;
}
return true;
}
}