use of org.jaudiotagger.audio.ogg.util.OggPageHeader in project MusicDNA by harjot-oberai.
the class OggFileReader method readOggPageHeader.
/**
* Return count Ogg Page header, count starts from zero
*
* count=0; should return PageHeader that contains Vorbis Identification Header
* count=1; should return Pageheader that contains VorbisComment and possibly SetupHeader
* count>=2; should return PageHeader containing remaining VorbisComment,SetupHeader and/or Audio
*
* @param raf
* @param count
* @return
* @throws CannotReadException
* @throws IOException
*/
public OggPageHeader readOggPageHeader(RandomAccessFile raf, int count) throws CannotReadException, IOException {
OggPageHeader pageHeader = OggPageHeader.read(raf);
while (count > 0) {
raf.seek(raf.getFilePointer() + pageHeader.getPageLength());
pageHeader = OggPageHeader.read(raf);
count--;
}
return pageHeader;
}
use of org.jaudiotagger.audio.ogg.util.OggPageHeader in project MusicDNA by harjot-oberai.
the class OggVorbisTagWriter method writeRemainingPagesOld.
public void writeRemainingPagesOld(int pageSequence, RandomAccessFile raf, RandomAccessFile rafTemp) throws IOException, CannotReadException, CannotWriteException {
//Now the Page Sequence Number for all the subsequent pages (containing audio frames) are out because there are
//less pages before then there used to be, so need to adjust
long startAudio = raf.getFilePointer();
long startAudioWritten = rafTemp.getFilePointer();
logger.fine("Writing audio, audio starts in original file at :" + startAudio + ":Written to:" + startAudioWritten);
while (raf.getFilePointer() < raf.length()) {
logger.fine("Reading Ogg Page");
OggPageHeader nextPage = OggPageHeader.read(raf);
//Create buffer large enough for next page (header and data) and set byte order to LE so we can use
//putInt method
ByteBuffer nextPageHeaderBuffer = ByteBuffer.allocate(nextPage.getRawHeaderData().length + nextPage.getPageLength());
nextPageHeaderBuffer.order(ByteOrder.LITTLE_ENDIAN);
nextPageHeaderBuffer.put(nextPage.getRawHeaderData());
raf.getChannel().read(nextPageHeaderBuffer);
//Recalculate Page Sequence Number
nextPageHeaderBuffer.putInt(OggPageHeader.FIELD_PAGE_SEQUENCE_NO_POS, ++pageSequence);
//Calculate Checksum
calculateChecksumOverPage(nextPageHeaderBuffer);
rafTemp.getChannel().write(nextPageHeaderBuffer);
}
if ((raf.length() - startAudio) != (rafTemp.length() - startAudioWritten)) {
throw new CannotWriteException("File written counts don'timer match, file not written");
}
}
use of org.jaudiotagger.audio.ogg.util.OggPageHeader in project MusicDNA by harjot-oberai.
the class OggVorbisTagReader method convertToVorbisSetupHeaderPacketAndAdditionalPackets.
/**
* The Vorbis Setup Header may span multiple(2) pages, athough it doesnt normally. We pass the start of the
* file offset of the OggPage it belongs on, it probably won'timer be first packet, also returns any addditional
* packets that immediately follow the setup header in original file
* @param fileOffsetOfStartingOggPage
* @param raf
* @throws org.jaudiotagger.audio.exceptions.CannotReadException
* @throws java.io.IOException
* @return
*/
public byte[] convertToVorbisSetupHeaderPacketAndAdditionalPackets(long fileOffsetOfStartingOggPage, RandomAccessFile raf) throws IOException, CannotReadException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
//Seek to specified offset
raf.seek(fileOffsetOfStartingOggPage);
//Read Page
OggPageHeader setupPageHeader = OggPageHeader.read(raf);
//is setupheader
if (setupPageHeader.getPacketList().size() > 1) {
raf.skipBytes(setupPageHeader.getPacketList().get(0).getLength());
}
//Now should be at start of next packet, check this is the vorbis setup header
byte[] b = new byte[VorbisHeader.FIELD_PACKET_TYPE_LENGTH + VorbisHeader.FIELD_CAPTURE_PATTERN_LENGTH];
raf.read(b);
if (!isVorbisSetupHeader(b)) {
throw new CannotReadException("Unable to find setup header(2), unable to write ogg file");
}
//Go back to start of setupheader data
raf.seek(raf.getFilePointer() - (VorbisHeader.FIELD_PACKET_TYPE_LENGTH + VorbisHeader.FIELD_CAPTURE_PATTERN_LENGTH));
//Read data
if (setupPageHeader.getPacketList().size() > 1) {
b = new byte[setupPageHeader.getPacketList().get(1).getLength()];
raf.read(b);
baos.write(b);
} else {
b = new byte[setupPageHeader.getPacketList().get(0).getLength()];
raf.read(b);
baos.write(b);
}
//Return Data
if (!setupPageHeader.isLastPacketIncomplete() || setupPageHeader.getPacketList().size() > 2) {
logger.config("Setupheader finishes on this page");
if (setupPageHeader.getPacketList().size() > 2) {
for (int i = 2; i < setupPageHeader.getPacketList().size(); i++) {
b = new byte[setupPageHeader.getPacketList().get(i).getLength()];
raf.read(b);
baos.write(b);
}
}
return baos.toByteArray();
}
//so carry on reading pages until we get to the end of comment
while (true) {
logger.config("Reading another page");
OggPageHeader nextPageHeader = OggPageHeader.read(raf);
b = new byte[nextPageHeader.getPacketList().get(0).getLength()];
raf.read(b);
baos.write(b);
//on this page so thats all we need and we can return
if (nextPageHeader.getPacketList().size() > 1) {
logger.config("Setupheader finishes on this page");
return baos.toByteArray();
}
//There is only the Setupheader packet on page if it has completed on this page we can return
if (!nextPageHeader.isLastPacketIncomplete()) {
logger.config("Setupheader finish on Page because this packet is complete");
return baos.toByteArray();
}
}
}
use of org.jaudiotagger.audio.ogg.util.OggPageHeader in project MusicDNA by harjot-oberai.
the class OggVorbisTagReader method convertToVorbisSetupHeaderPacket.
/**
* The Vorbis Setup Header may span multiple(2) pages, athough it doesnt normally. We pass the start of the
* file offset of the OggPage it belongs on, it probably won'timer be first packet.
* @param fileOffsetOfStartingOggPage
* @param raf
* @throws org.jaudiotagger.audio.exceptions.CannotReadException
* @throws java.io.IOException
* @return
*/
public byte[] convertToVorbisSetupHeaderPacket(long fileOffsetOfStartingOggPage, RandomAccessFile raf) throws IOException, CannotReadException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
//Seek to specified offset
raf.seek(fileOffsetOfStartingOggPage);
//Read Page
OggPageHeader setupPageHeader = OggPageHeader.read(raf);
//is setupheader
if (setupPageHeader.getPacketList().size() > 1) {
raf.skipBytes(setupPageHeader.getPacketList().get(0).getLength());
}
//Now should be at start of next packet, check this is the vorbis setup header
byte[] b = new byte[VorbisHeader.FIELD_PACKET_TYPE_LENGTH + VorbisHeader.FIELD_CAPTURE_PATTERN_LENGTH];
raf.read(b);
if (!isVorbisSetupHeader(b)) {
throw new CannotReadException("Unable to find setup header(2), unable to write ogg file");
}
//Go back to start of setupheader data
raf.seek(raf.getFilePointer() - (VorbisHeader.FIELD_PACKET_TYPE_LENGTH + VorbisHeader.FIELD_CAPTURE_PATTERN_LENGTH));
//Read data
if (setupPageHeader.getPacketList().size() > 1) {
b = new byte[setupPageHeader.getPacketList().get(1).getLength()];
raf.read(b);
baos.write(b);
} else {
b = new byte[setupPageHeader.getPacketList().get(0).getLength()];
raf.read(b);
baos.write(b);
}
//Return Data
if (!setupPageHeader.isLastPacketIncomplete() || setupPageHeader.getPacketList().size() > 2) {
logger.config("Setupheader finishes on this page");
return baos.toByteArray();
}
//so carry on reading pages until we get to the end of comment
while (true) {
logger.config("Reading another page");
OggPageHeader nextPageHeader = OggPageHeader.read(raf);
b = new byte[nextPageHeader.getPacketList().get(0).getLength()];
raf.read(b);
baos.write(b);
//on this page so thats all we need and we can return
if (nextPageHeader.getPacketList().size() > 1) {
logger.config("Setupheader finishes on this page");
return baos.toByteArray();
}
//There is only the Setupheader packet on page if it has completed on this page we can return
if (!nextPageHeader.isLastPacketIncomplete()) {
logger.config("Setupheader finish on Page because this packet is complete");
return baos.toByteArray();
}
}
}
use of org.jaudiotagger.audio.ogg.util.OggPageHeader in project MusicDNA by harjot-oberai.
the class OggVorbisTagWriter method writeRemainingPages.
/**
* Write all the remaining pages as they are except that the page sequence needs to be modified.
*
* @param pageSequence
* @param raf
* @param rafTemp
* @throws IOException
* @throws CannotReadException
* @throws CannotWriteException
*/
public void writeRemainingPages(int pageSequence, RandomAccessFile raf, RandomAccessFile rafTemp) throws IOException, CannotReadException, CannotWriteException {
long startAudio = raf.getFilePointer();
long startAudioWritten = rafTemp.getFilePointer();
//TODO there is a risk we wont have enough memory to create these buffers
ByteBuffer bb = ByteBuffer.allocate((int) (raf.length() - raf.getFilePointer()));
ByteBuffer bbTemp = ByteBuffer.allocate((int) (raf.length() - raf.getFilePointer()));
//Read in the rest of the data into bytebuffer and rewind it to start
raf.getChannel().read(bb);
bb.rewind();
while (bb.hasRemaining()) {
OggPageHeader nextPage = OggPageHeader.read(bb);
//Create buffer large enough for next page (header and data) and set byte order to LE so we can use
//putInt method
ByteBuffer nextPageHeaderBuffer = ByteBuffer.allocate(nextPage.getRawHeaderData().length + nextPage.getPageLength());
nextPageHeaderBuffer.order(ByteOrder.LITTLE_ENDIAN);
nextPageHeaderBuffer.put(nextPage.getRawHeaderData());
ByteBuffer data = bb.slice();
data.limit(nextPage.getPageLength());
nextPageHeaderBuffer.put(data);
nextPageHeaderBuffer.putInt(OggPageHeader.FIELD_PAGE_SEQUENCE_NO_POS, ++pageSequence);
calculateChecksumOverPage(nextPageHeaderBuffer);
bb.position(bb.position() + nextPage.getPageLength());
nextPageHeaderBuffer.rewind();
bbTemp.put(nextPageHeaderBuffer);
}
//Now just write as a single IO operation
bbTemp.rewind();
rafTemp.getChannel().write(bbTemp);
//TODO could we do any other checks to check data written correctly ?
if ((raf.length() - startAudio) != (rafTemp.length() - startAudioWritten)) {
throw new CannotWriteException("File written counts don'timer match, file not written");
}
}
Aggregations