* flac加载修复

This commit is contained in:
2024-01-03 20:57:57 +08:00
parent 3b9aa99534
commit da0e52ac1d
+21 -26
View File
@@ -209,50 +209,46 @@ bool AudioLoader::ReadOgg(std::string_view path_name, PCM& pcm)
return true;
}
FLAC__StreamDecoderWriteStatus write_callback(const FLAC__StreamDecoder* decoder, const FLAC__Frame* frame, const FLAC__int32* const buffer[], void* client_data)
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);
(void)decoder;
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)
{
fprintf(stderr, "ERROR: this example only works for FLAC files that have a total_samples count in STREAMINFO\n");
log_err("采样率为0!");
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");
log_err("通道数不为2,或采样数不为16!");
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);
log_err("frame通道数不为2!");
return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
}
if (buffer[0] == NULL)
{
fprintf(stderr, "ERROR: buffer [0] is NULL\n");
log_err("buffer[0]为空!");
return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
}
if (buffer[1] == NULL)
{
fprintf(stderr, "ERROR: buffer [1] is NULL\n");
log_err("buffer[1]为空!");
return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
}
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);
@@ -298,18 +294,17 @@ bool AudioLoader::ReadFlac(std::string_view path_name, PCM& 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;
}
else
FLAC__bool ok = FLAC__stream_decoder_process_until_end_of_stream(decoder);
if (!ok)
{
FLAC__bool ok = FLAC__stream_decoder_process_until_end_of_stream(decoder);
if (!ok)
{
log_msg("解码失败!{}",
FLAC__StreamDecoderStateString[FLAC__stream_decoder_get_state(decoder)]);
}
log_msg("解码失败!{}",
FLAC__StreamDecoderStateString[FLAC__stream_decoder_get_state(decoder)]);
return false;
}
FLAC__stream_decoder_delete(decoder);
return true;
}