1 #define DR_FLAC_IMPLEMENTATION
2 #include "../../dr_flac.h"
5 #if defined(__GNUC__) || defined(__clang__)
6 #pragma GCC diagnostic push
7 #pragma GCC diagnostic ignored "-Wpedantic"
9 #pragma GCC diagnostic ignored "-Wc99-extensions"
12 #include <FLAC/stream_decoder.h>
14 #pragma GCC diagnostic pop
17 #include "../common/dr_common.c"
33 double decodeTimeInSeconds;
35 size_t fileSizeInBytes;
39 static FLAC__StreamDecoderReadStatus
libflac__read_callback(
const FLAC__StreamDecoder *pStreamDecoder, FLAC__byte buffer[],
size_t *bytes,
void *client_data)
42 size_t bytesToRead = *bytes;
47 if (bytesToRead > bytesRemaining) {
48 bytesToRead = bytesRemaining;
51 if (bytesToRead > 0) {
57 return (bytesToRead == 0 && bytesRemaining == 0) ? FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM : FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
60 static FLAC__StreamDecoderWriteStatus
libflac__write_callback(
const FLAC__StreamDecoder *pStreamDecoder,
const FLAC__Frame *frame,
const FLAC__int32 *
const buffer[],
void *client_data)
69 pcmFramesInFLACFrame = frame->header.blocksize;
81 if (pNewPCMFrames ==
NULL) {
82 return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
90 for (i = 0; i < pcmFramesInFLACFrame; i += 1) {
91 for (j = 0; j < pDecoder->
channels; ++j) {
92 *pNextSampleInNewFrame++ = buffer[j][i] << (32 - frame->header.bits_per_sample);
98 return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
101 static FLAC__StreamDecoderLengthStatus
libflac__length_callback(
const FLAC__StreamDecoder *pStreamDecoder, FLAC__uint64 *stream_length,
void *client_data)
105 (void)pStreamDecoder;
108 return FLAC__STREAM_DECODER_LENGTH_STATUS_OK;
115 (void)pStreamDecoder;
118 if (metadata->type == FLAC__METADATA_TYPE_STREAMINFO) {
120 pDecoder->
channels = metadata->data.stream_info.channels;
121 pDecoder->
sampleRate = metadata->data.stream_info.sample_rate;
124 if (metadata->data.stream_info.total_samples > 0) {
125 pDecoder->
pcmFrameCap = metadata->data.stream_info.total_samples;
134 static void libflac__error_callback(
const FLAC__StreamDecoder *pStreamDecoder, FLAC__StreamDecoderErrorStatus status,
void *client_data)
136 (void)pStreamDecoder;
150 FLAC__StreamDecoder* pStreamDecoder;
151 FLAC__bool libflacResult;
152 FLAC__StreamDecoderInitStatus libflacStatus;
153 double decodeTimeBeg;
154 double decodeTimeEnd;
165 pStreamDecoder = FLAC__stream_decoder_new();
166 if (pDecoder ==
NULL) {
176 if (libflacStatus != FLAC__STREAM_DECODER_INIT_STATUS_OK) {
177 FLAC__stream_decoder_delete(pStreamDecoder);
183 libflacResult = FLAC__stream_decoder_process_until_end_of_metadata(pStreamDecoder);
185 FLAC__stream_decoder_delete(pStreamDecoder);
192 libflacResult = FLAC__stream_decoder_process_until_end_of_stream(pStreamDecoder);
198 FLAC__stream_decoder_delete(pStreamDecoder);
199 pStreamDecoder =
NULL;
217 if (pDecoder ==
NULL) {
228 if (pDecoder ==
NULL) {
233 if (framesToRead > pcmFramesRemaining) {
234 framesToRead = pcmFramesRemaining;
237 if (framesToRead == 0) {
251 if (pDecoder ==
NULL) {
256 if (framesToRead > pcmFramesRemaining) {
257 framesToRead = pcmFramesRemaining;
260 if (framesToRead == 0) {
275 if (pDecoder ==
NULL) {
280 if (framesToRead > pcmFramesRemaining) {
281 framesToRead = pcmFramesRemaining;
284 if (framesToRead == 0) {
297 if (pDecoder ==
NULL) {
314 #if defined(DRFLAC_64BIT)
322 #if defined(DRFLAC_X86) || defined(DRFLAC_X64)
323 #if defined(DRFLAC_X64)
324 printf(
"Architecture: x64\n");
326 printf(
"Architecture: x86\n");
330 printf(
"Has LZCNT: %s\n", drflac__is_lzcnt_supported() ?
"YES" :
"NO");