Skip to content

Commit

Permalink
Flac subframe tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Gagravarr committed Aug 23, 2015
1 parent e3a9002 commit c416a63
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 11 deletions.
2 changes: 1 addition & 1 deletion core/src/main/java/org/gagravarr/flac/FlacAudioFrame.java
Expand Up @@ -183,7 +183,7 @@ public FlacAudioFrame(int first2, InputStream rawStream, FlacInfo info) throws I
+ (cn+1) + " of " + numChannels);

// Sub-Frame data
subFrames[cn] = FlacAudioSubFrame.create(type, this, br);
subFrames[cn] = FlacAudioSubFrame.create(type, wb, this, br);
}

// Skip any remaining bits, to hit the boundary
Expand Down
33 changes: 24 additions & 9 deletions core/src/main/java/org/gagravarr/flac/FlacAudioSubFrame.java
Expand Up @@ -21,33 +21,48 @@
* Per-channel, compressed audio
*/
public abstract class FlacAudioSubFrame {
public static FlacAudioSubFrame create(int type, FlacAudioFrame audioFrame,
public static FlacAudioSubFrame create(int type, int wastedBits, FlacAudioFrame audioFrame,
BitsReader data) throws IOException {
// Sanity check
if (type < 0 || type >= 64) {
throw new IllegalArgumentException("Type must be a un-signed 6 bit number, found " + type);
}

// Create the right type
FlacAudioSubFrame subFrame;
if (SubFrameConstant.matchesType(type))
return new SubFrameConstant(audioFrame, data);
if (SubFrameVerbatim.matchesType(type))
return new SubFrameVerbatim(audioFrame, data);
if (SubFrameFixed.matchesType(type))
return new SubFrameFixed(type, audioFrame, data);
if (SubFrameLPC.matchesType(type))
return new SubFrameLPC(type, audioFrame, data);
return new SubFrameReserved(audioFrame);
subFrame = new SubFrameConstant(audioFrame, data);
else if (SubFrameVerbatim.matchesType(type))
subFrame = new SubFrameVerbatim(audioFrame, data);
else if (SubFrameFixed.matchesType(type))
subFrame = new SubFrameFixed(type, audioFrame, data);
else if (SubFrameLPC.matchesType(type))
subFrame = new SubFrameLPC(type, audioFrame, data);
else subFrame = new SubFrameReserved(audioFrame);

// Record details, and return
subFrame.wastedBits = wastedBits;
return subFrame;
}

protected final FlacAudioFrame audioFrame;
protected final int predictorOrder;
protected final int sampleSizeBits;
protected final int blockSize;
private int wastedBits;

protected FlacAudioSubFrame(int predictorOrder, FlacAudioFrame audioFrame) {
this.predictorOrder = predictorOrder;
this.audioFrame = audioFrame;
this.sampleSizeBits = audioFrame.getBitsPerSample();
this.blockSize = audioFrame.getBlockSize();
}
/**
* The number of wasted bits per sample
*/
public int getWastedBits() {
return wastedBits;
}

public static class SubFrameConstant extends FlacAudioSubFrame {
protected SubFrameConstant(FlacAudioFrame audioFrame, BitsReader data) throws IOException {
Expand Down
15 changes: 14 additions & 1 deletion core/src/test/java/org/gagravarr/flac/TestFlacFileRead.java
Expand Up @@ -18,6 +18,7 @@

import junit.framework.TestCase;

import org.gagravarr.flac.FlacAudioSubFrame.SubFrameFixed;
import org.gagravarr.ogg.OggFile;
import org.gagravarr.ogg.OggPacket;
import org.gagravarr.ogg.OggPacketReader;
Expand Down Expand Up @@ -88,6 +89,12 @@ public void testReadFlacNative() throws IOException {
assertFlacContents(flac);
}

/**
* Checks that the right information is stored in the file,
* both header and audio contents
* Information for these tests generated from running "flac -a"
* against the test files.
*/
protected void assertFlacContents(FlacFile flac) throws IOException {
// Check the info
FlacInfo info = flac.getInfo();
Expand Down Expand Up @@ -128,10 +135,16 @@ protected void assertFlacContents(FlacFile flac) throws IOException {
// Should have one subframe per channel
// First should be Fixed, second LPC
assertEquals(2, audio.getSubFrames().length);

sf = audio.getSubFrames()[0];
assertEquals(FlacAudioSubFrame.SubFrameFixed.class, sf.getClass());
assertEquals(SubFrameFixed.class, sf.getClass());
assertEquals(0, sf.getWastedBits());
SubFrameFixed sff = (SubFrameFixed)sf;
assertEquals(1, sff.predictorOrder);

sf = audio.getSubFrames()[1];
assertEquals(FlacAudioSubFrame.SubFrameLPC.class, sf.getClass());
assertEquals(0, sf.getWastedBits());

// TODO Is this right? Only a single audio frame
// TODO Is this right? Different between formats?
Expand Down

0 comments on commit c416a63

Please sign in to comment.