Skip to content

Commit

Permalink
fix(flac): detect sample rate extension (#127)
Browse files Browse the repository at this point in the history
  • Loading branch information
viztea authored May 26, 2024
1 parent 55c6796 commit 38e7386
Showing 1 changed file with 22 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,11 @@ public class FlacFrameHeaderReader {
* @return The frame information.
* @throws IOException On read error.
*/
public static FlacFrameInfo readFrameHeader(BitStreamReader bitStreamReader, FlacStreamInfo streamInfo,
boolean variableBlock) throws IOException {
public static FlacFrameInfo readFrameHeader(
BitStreamReader bitStreamReader,
FlacStreamInfo streamInfo,
boolean variableBlock
) throws IOException {

int blockSize = blockSizeMapping[bitStreamReader.asInteger(4)];
int sampleRate = sampleRateMapping[bitStreamReader.asInteger(4)];
Expand All @@ -74,20 +77,27 @@ public static FlacFrameInfo readFrameHeader(BitStreamReader bitStreamReader, Fla

readUtf8Value(variableBlock, bitStreamReader);

if (blockSize == BLOCK_SIZE_EXPLICIT_8_BIT) {
blockSize = bitStreamReader.asInteger(8) + 1;
} else if (blockSize == BLOCK_SIZE_EXPLICIT_16_BIT) {
blockSize = bitStreamReader.asInteger(16) + 1;
switch (blockSize) {
case BLOCK_SIZE_EXPLICIT_8_BIT:
blockSize = bitStreamReader.asInteger(8) + 1;
break;
case BLOCK_SIZE_EXPLICIT_16_BIT:
blockSize = bitStreamReader.asInteger(16) + 1;
break;
}

verifyNotInvalid(blockSize, "block size");

if (blockSize == SAMPLE_RATE_EXPLICIT_8_BIT) {
sampleRate = bitStreamReader.asInteger(8);
} else if (blockSize == SAMPLE_RATE_EXPLICIT_16_BIT) {
sampleRate = bitStreamReader.asInteger(16);
} else if (blockSize == SAMPLE_RATE_EXPLICIT_10X_16_BIT) {
sampleRate = bitStreamReader.asInteger(16) * 10;
switch (sampleRate) {
case SAMPLE_RATE_EXPLICIT_8_BIT:
sampleRate = bitStreamReader.asInteger(8) * 1_000;
break;
case SAMPLE_RATE_EXPLICIT_16_BIT:
sampleRate = bitStreamReader.asInteger(16);
break;
case SAMPLE_RATE_EXPLICIT_10X_16_BIT:
sampleRate = bitStreamReader.asInteger(16) * 10;
break;
}

verifyMatchesExpected(sampleRate, streamInfo.sampleRate, "sample rate");
Expand Down

0 comments on commit 38e7386

Please sign in to comment.