diff --git a/src/FlacDecoder.cpp b/src/FlacDecoder.cpp index a0970a0..f2b6ea6 100644 --- a/src/FlacDecoder.cpp +++ b/src/FlacDecoder.cpp @@ -75,26 +75,6 @@ public: d->lengthSeconds = (float) numSamples / (float) d->sampleRate; auto totalSamples = numSamples * d->channelCount; - - // N.B.: "Currently the reference encoder and decoders only support up to 24 bits per sample." - - d->sourceFormat = PCMFormat::PCM_END; - - switch (d->bitDepth) - { - case 8: - internalFmt = PCMFormat::PCM_S8; - break; - case 16: - internalFmt = PCMFormat::PCM_16; - break; - case 24: - internalFmt = PCMFormat::PCM_24; - break; - default: - throw std::runtime_error("unsupported FLAC bit depth"); - break; - } // Next, process internal buffer into the user-visible samples array ConvertToFloat32(d->samples.data(), internalBuffer.data(), totalSamples, d->sourceFormat); @@ -118,12 +98,13 @@ public: void processMetadata(const FLAC__StreamMetadata_StreamInfo & info) { + // N.B.: "Currently the reference encoder and decoders only support up to 24 bits per sample." d->sampleRate = info.sample_rate; d->channelCount = info.channels; // Assert 1 to 8 - d->sourceF = info.bits_per_sample; // Assert 4 to 32 + d->sourceFormat = MakeFormatForBits(info.bits_per_sample, false, true); d->frameSize = info.channels * info.bits_per_sample; - const size_t bytesPerSample = d->bitDepth / 8; + const size_t bytesPerSample = GetFormatBitsPerSample(d->sourceFormat) / 8; numSamples = (size_t) info.total_samples; @@ -139,7 +120,7 @@ public: { FlacDecoderInternal * decoder = reinterpret_cast(userPtr); - const size_t bytesPerSample = decoder->d->bitDepth / 8; + const size_t bytesPerSample = GetFormatBitsPerSample(decoder->d->sourceFormat) / 8; auto dataPtr = decoder->internalBuffer.data(); diff --git a/src/OpusDecoder.cpp b/src/OpusDecoder.cpp index 0335222..acd5fe5 100644 --- a/src/OpusDecoder.cpp +++ b/src/OpusDecoder.cpp @@ -69,9 +69,9 @@ public: d->sampleRate = OPUS_SAMPLE_RATE; d->channelCount = (uint32_t) header->channel_count; - d->bitDepth = 32; + d->sourceFormat = MakeFormatForBits(32, true, false); d->lengthSeconds = double(getLengthInSeconds()); - d->frameSize = (uint32_t) header->channel_count * d->bitDepth; + d->frameSize = (uint32_t) header->channel_count * GetFormatBitsPerSample(d->sourceFormat); // Samples in a single channel auto totalSamples = size_t(getTotalSamples()); diff --git a/src/VorbisDecoder.cpp b/src/VorbisDecoder.cpp index fda0454..aab927b 100644 --- a/src/VorbisDecoder.cpp +++ b/src/VorbisDecoder.cpp @@ -80,9 +80,9 @@ public: d->sampleRate = int(ovInfo->rate); d->channelCount = ovInfo->channels; - d->bitDepth = 32; // float + d->sourceFormat = MakeFormatForBits(32, true, false); d->lengthSeconds = double(getLengthInSeconds()); - d->frameSize = ovInfo->channels * d->bitDepth; + d->frameSize = ovInfo->channels * GetFormatBitsPerSample(d->sourceFormat); // Samples in a single channel auto totalSamples = size_t(getTotalSamples()); diff --git a/src/WavDecoder.cpp b/src/WavDecoder.cpp index 133be4b..e1c8c2b 100644 --- a/src/WavDecoder.cpp +++ b/src/WavDecoder.cpp @@ -178,28 +178,26 @@ int WavDecoder::LoadFromBuffer(AudioData * data, const std::vector & me size_t totalSamples = (DataChunkInfo.size / wavHeader.frame_size) * wavHeader.channel_count; data->samples.resize(totalSamples); - PCMFormat internalFmt = PCMFormat::PCM_END; - switch (bit_depth) { case 8: - internalFmt = PCMFormat::PCM_U8; + data->sourceFormat = PCMFormat::PCM_U8; break; case 16: - internalFmt = PCMFormat::PCM_16; + data->sourceFormat = PCMFormat::PCM_16; break; case 24: - internalFmt = PCMFormat::PCM_24; + data->sourceFormat = PCMFormat::PCM_24; break; case 32: - internalFmt = (wavHeader.format == WaveFormatCode::FORMAT_IEEE) ? PCMFormat::PCM_FLT : PCMFormat::PCM_32; + data->sourceFormat = (wavHeader.format == WaveFormatCode::FORMAT_IEEE) ? PCMFormat::PCM_FLT : PCMFormat::PCM_32; break; case 64: - internalFmt = (wavHeader.format == WaveFormatCode::FORMAT_IEEE) ? PCMFormat::PCM_DBL : PCMFormat::PCM_64; + data->sourceFormat = (wavHeader.format == WaveFormatCode::FORMAT_IEEE) ? PCMFormat::PCM_DBL : PCMFormat::PCM_64; break; } - ConvertToFloat32(data->samples.data(), memory.data() + DataChunkInfo.offset, totalSamples, internalFmt); + ConvertToFloat32(data->samples.data(), memory.data() + DataChunkInfo.offset, totalSamples, data->sourceFormat); return IOError::NoError; } diff --git a/src/WavPackDecoder.cpp b/src/WavPackDecoder.cpp index bd40e52..63d4160 100644 --- a/src/WavPackDecoder.cpp +++ b/src/WavPackDecoder.cpp @@ -43,38 +43,23 @@ public: throw std::runtime_error("Not a WavPack file"); } + auto bitdepth = WavpackGetBitsPerSample(context); + d->sampleRate = WavpackGetSampleRate(context); d->channelCount = WavpackGetNumChannels(context); - d->bitDepth = WavpackGetBitsPerSample(context); d->lengthSeconds = double(getLengthInSeconds()); - d->frameSize = d->channelCount * d->bitDepth; + d->frameSize = d->channelCount * bitdepth; //@todo support channel masks // WavpackGetChannelMask auto totalSamples = size_t(getTotalSamples()); - PCMFormat internalFmt = PCMFormat::PCM_END; - int mode = WavpackGetMode(context); int isFloatingPoint = (MODE_FLOAT & mode); - switch (d->bitDepth) - { - case 16: - internalFmt = PCMFormat::PCM_16; - break; - case 24: - internalFmt = PCMFormat::PCM_24; - break; - case 32: - internalFmt = isFloatingPoint ? PCMFormat::PCM_FLT : PCMFormat::PCM_32; - break; - default: - throw std::runtime_error("unsupported WavPack bit depth"); - break; - } - + d->sourceFormat = MakeFormatForBits(bitdepth, isFloatingPoint, false); + /* From the docs: "... required memory at "buffer" is 4 * samples * num_channels bytes. The audio data is returned right-justified in 32-bit longs in the endian @@ -89,7 +74,7 @@ public: // Next, process internal buffer into the user-visible samples array if (!isFloatingPoint) - ConvertToFloat32(d->samples.data(), internalBuffer.data(), totalSamples * d->channelCount, internalFmt); + ConvertToFloat32(d->samples.data(), internalBuffer.data(), totalSamples * d->channelCount, d->sourceFormat); }