Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
# Everyone want's their own Eclipse setup
.project
.classpath
.settings/
.externalToolBuilders/
.recommenders
.metadata

# Skip the build directories
/target/
Expand All @@ -24,4 +28,4 @@
# Intellij
.idea/
*.iml
*.iws
*.iws
23 changes: 22 additions & 1 deletion core/src/main/java/org/gagravarr/flac/FlacFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,28 @@ public abstract class FlacFile implements Closeable {

/**
* Opens the given file for reading
*
* @param f file to open
* @return FlacFile
* @throws IOException
* @throws FileNotFoundException
*/
public static FlacFile open(File f) throws IOException, FileNotFoundException {
// Open, in a way that we can skip backwards a few bytes
InputStream inp = new BufferedInputStream(new FileInputStream(f), 8);
FlacFile file = open(inp);
return file;
}

/**
* Opens the given file for reading.
* @param inp The InputStrem to read from, which must support mark/reset
*
* @param inp input source
* @return FlacFile
* @throws IOException
* @throws FileNotFoundException
*/
@SuppressWarnings("resource")
public static FlacFile open(InputStream inp) throws IOException, FileNotFoundException {
inp.mark(4);
byte[] header = new byte[4];
Expand All @@ -63,8 +74,13 @@ public static FlacFile open(InputStream inp) throws IOException, FileNotFoundExc
}
throw new IllegalArgumentException("File type not recognised");
}

/**
* Opens the given file for reading
*
* @param ogg file to open
* @return FlacFile
* @throws IOException
*/
public static FlacFile open(OggFile ogg) throws IOException {
return new FlacOggFile(ogg);
Expand All @@ -76,6 +92,9 @@ public static FlacFile open(OggFile ogg) throws IOException {
* Skips the audio data to the next packet with a granule
* of at least the given granule position.
* Note that skipping backwards is not currently supported!
*
* @param granulePosition position to skip to
* @throws IOException
*/
public abstract void skipToGranule(long granulePosition) throws IOException;

Expand All @@ -91,6 +110,8 @@ public FlacTags getTags() {
* file and free its resources.
* In Writing mode, will write out the Info and
* Comments objects, and then the audio data.
*
* @throws IOException
*/
public abstract void close() throws IOException;
}
8 changes: 8 additions & 0 deletions core/src/main/java/org/gagravarr/flac/FlacFirstOggPacket.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ public OggPacket write() {

/**
* Returns the Major Version number
*
* @return major version
*/
public int getMajorVersion() {
return majorVersion;
Expand All @@ -101,6 +103,8 @@ public FlacOggInfo getInfo() {
/**
* Returns the Minor Version number. Decoders should be able to
* handle anything at a given major number, no matter the minor one
*
* @return minor version
*/
public int getMinorVersion() {
return minorVersion;
Expand All @@ -116,6 +120,8 @@ public void setMinorVersion(int minorVersion) {
/**
* Gets the number of header blocks, excluding this one, or
* zero if not known
*
* @return number of header blocks
*/
public int getNumberOfHeaderBlocks() {
return numberOfHeaderBlocks;
Expand All @@ -129,6 +135,8 @@ public void setNumberOfHeaderBlocks(int numberOfHeaderBlocks) {
* Does this packet (the first in the stream) contain
* the magic string indicating that it's a FLAC
* one?
*
* @return true if flac and false otherwise
*/
public static boolean isFlacStream(OggPacket firstPacket) {
if(! firstPacket.isBeginningOfStream()) {
Expand Down
7 changes: 7 additions & 0 deletions core/src/main/java/org/gagravarr/flac/FlacInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ public FlacInfo() {

/**
* Reads the Info from the specified data
*
* @param data flac info bytes
* @param offset data offset
*/
public FlacInfo(byte[] data, int offset) {
super(STREAMINFO);
Expand Down Expand Up @@ -138,6 +141,8 @@ protected void write(OutputStream out) throws IOException {

/**
* The minimum block size (in samples) used in the stream.
*
* @return minimum block size
*/
public int getMinimumBlockSize() {
return minimumBlockSize;
Expand All @@ -149,6 +154,8 @@ public void setMinimumBlockSize(int minimumBlockSize) {
/**
* The maximum block size (in samples) used in the stream.
* (Minimum blocksize == maximum blocksize) implies a fixed-blocksize stream.
*
* @return maximum block size
*/
public int getMaximumBlockSize() {
return maximumBlockSize;
Expand Down
47 changes: 45 additions & 2 deletions core/src/main/java/org/gagravarr/flac/FlacOggFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,19 +46,31 @@ public class FlacOggFile extends FlacFile implements OggAudioHeaders {

/**
* Opens the given file for reading
*
* @param f file to use
* @throws IOException
* @throws FileNotFoundException
*/
public FlacOggFile(File f) throws IOException, FileNotFoundException {
this(new OggFile(new FileInputStream(f)));
}

/**
* Opens the given file for reading
*
* @param ogg file to use
* @throws IOException
*/
public FlacOggFile(OggFile ogg) throws IOException {
this(ogg.getPacketReader());
this.ogg = ogg;
}

/**
* Loads a Vorbis File from the given packet reader.
*
* @param r ogg packet reader
* @throws IOException
*/
public FlacOggFile(OggPacketReader r) throws IOException {
this.r = r;
Expand Down Expand Up @@ -95,23 +107,36 @@ public FlacOggFile(OggPacketReader r) throws IOException {

/**
* Opens for writing.
*
* @param out stream to output to
*/
public FlacOggFile(OutputStream out) {
this(out, new FlacOggInfo(), new FlacTags());
}

/**
* Opens for writing, based on the settings
* from a pre-read file. The Steam ID (SID) is
* automatically allocated for you.
*
* @param out stream to output to
* @param info flac info
* @param tags flac tags
*/
public FlacOggFile(OutputStream out, FlacOggInfo info, FlacTags tags) {
this(out, -1, info, tags);
}

/**
* Opens for writing, based on the settings
* from a pre-read file, with a specific
* Steam ID (SID). You should only set the SID
* Stream ID (SID). You should only set the SID
* when copying one file to another!
*
* @param out stream to output to
* @param sid stream id
* @param info flac info
* @param tags flac tags
*/
public FlacOggFile(OutputStream out, int sid, FlacOggInfo info, FlacTags tags) {
ogg = new OggFile(out);
Expand All @@ -133,6 +158,8 @@ public FlacOggFile(OutputStream out, int sid, FlacOggInfo info, FlacTags tags) {

/**
* Returns the first Ogg Packet, which has some metadata in it
*
* @return FlacFirstOggPacket
*/
public FlacFirstOggPacket getFirstPacket() {
return firstPacket;
Expand All @@ -150,20 +177,26 @@ public FlacAudioFrame getNextAudioPacket() throws IOException {
* Skips the audio data to the next packet with a granule
* of at least the given granule position.
* Note that skipping backwards is not currently supported!
*
* @param granulePosition position to skip to
*/
public void skipToGranule(long granulePosition) throws IOException {
r.skipToGranulePosition(sid, granulePosition);
}

/**
* Returns the Ogg Stream ID
*
* @return stream id
*/
public int getSid() {
return sid;
}

/**
* This is a Flac-in-Ogg file
*
* @return OggStreamType
*/
public OggStreamType getType() {
return OggStreamIdentifier.OGG_FLAC;
Expand All @@ -175,6 +208,8 @@ public OggStreamType getType() {
* need to call {@link #close()} to do that,
* because we assume you'll still be populating
* the Info/Comment/Setup objects
*
* @param data flac audio frame
*/
public void writeAudioData(FlacAudioFrame data) {
writtenAudio.add(data);
Expand All @@ -185,6 +220,8 @@ public void writeAudioData(FlacAudioFrame data) {
* file and free its resources.
* In Writing mode, will write out the Info, Comments
* and Setup objects, and then the audio data.
*
* @throws IOException
*/
public void close() throws IOException {
if(r != null) {
Expand All @@ -198,6 +235,7 @@ public void close() throws IOException {
// TODO Write the others
//w.bufferPacket(setup.write(), true);

@SuppressWarnings("unused")
long lastGranule = 0;
for(FlacAudioFrame fa : writtenAudio) {
// Update the granule position as we go
Expand Down Expand Up @@ -225,6 +263,8 @@ public void close() throws IOException {

/**
* Return the Ogg-specific version of the Flac Info
*
* @return FlacOggInfo
*/
@Override
public FlacOggInfo getInfo() {
Expand All @@ -233,14 +273,17 @@ public FlacOggInfo getInfo() {

/**
* Flac doesn't have setup packets per-se, so return null
*
* @return OggAudioSetupHeader
*/
public OggAudioSetupHeader getSetup() {
return null;
}

/**
* Returns the underlying Ogg File instance
* @return
*
* @return OggFile
*/
public OggFile getOggFile() {
return ogg;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@
*/
package org.gagravarr.ogg;

import org.gagravarr.ogg.HighLevelOggStreamPacket;
import org.gagravarr.ogg.OggPacket;

/**
* Raw, compressed audio and video data, which keeps track of a
* granule position
Expand All @@ -27,6 +24,7 @@ public OggStreamAudioVisualData(OggPacket pkt) {
super(pkt);
granulePosition = pkt.getGranulePosition();
}

public OggStreamAudioVisualData(byte[] data) {
super();
setData(data);
Expand All @@ -36,10 +34,12 @@ public OggStreamAudioVisualData(byte[] data) {
/**
* Returns the granule position, or -1 if
* this wasn't read from a file
* @return granule position
*/
public long getGranulePosition() {
return granulePosition;
}

public void setGranulePosition(long granulePosition) {
this.granulePosition = granulePosition;
}
Expand Down
Loading