From bb238c3c9991481dd82410817cb2178be16411a4 Mon Sep 17 00:00:00 2001 From: "sha..rd" Date: Wed, 6 Apr 2011 02:06:29 +0000 Subject: [PATCH] * Fixed bug with OGG audio having incorrect length git-svn-id: https://jmonkeyengine.googlecode.com/svn/trunk@7198 75d07b2b-3a1a-0410-a2c5-0572b91ccdca --- .../jme3/audio/plugins/CachedOggStream.java | 3 ++- .../com/jme3/audio/plugins/OGGLoader.java | 23 +++++++++++++++---- .../jme3/audio/plugins/UncachedOggStream.java | 6 +++++ 3 files changed, 26 insertions(+), 6 deletions(-) diff --git a/engine/src/jogg/com/jme3/audio/plugins/CachedOggStream.java b/engine/src/jogg/com/jme3/audio/plugins/CachedOggStream.java index 4c922baca..693efb862 100644 --- a/engine/src/jogg/com/jme3/audio/plugins/CachedOggStream.java +++ b/engine/src/jogg/com/jme3/audio/plugins/CachedOggStream.java @@ -41,6 +41,7 @@ import java.io.InputStream; import java.util.ArrayList; import java.util.HashMap; import java.util.Collection; +import java.util.logging.Level; import java.util.logging.Logger; /** @@ -72,7 +73,7 @@ public class CachedOggStream implements PhysicalOggStream { * file is not automatically deleted when this class is disposed. */ public CachedOggStream(InputStream stream, int length, int numPages) throws IOException { - logger.info("Creating memory cache of size "+length); + logger.log(Level.INFO, "Creating memory cache of size {0}", length); memoryCache = new byte[length]; sourceStream = stream; diff --git a/engine/src/jogg/com/jme3/audio/plugins/OGGLoader.java b/engine/src/jogg/com/jme3/audio/plugins/OGGLoader.java index 1e79b665f..dccb881ca 100644 --- a/engine/src/jogg/com/jme3/audio/plugins/OGGLoader.java +++ b/engine/src/jogg/com/jme3/audio/plugins/OGGLoader.java @@ -38,10 +38,9 @@ import com.jme3.audio.AudioStream; import com.jme3.asset.AssetLoader; import com.jme3.audio.AudioKey; import com.jme3.util.BufferUtils; -import com.jme3.util.TempVars; import de.jarnbjo.ogg.EndOfOggStreamException; import de.jarnbjo.ogg.LogicalOggStream; -import de.jarnbjo.ogg.PhysicalOggStream; +import de.jarnbjo.ogg.OggPage; import de.jarnbjo.vorbis.IdentificationHeader; import de.jarnbjo.vorbis.VorbisStream; import java.io.ByteArrayOutputStream; @@ -54,7 +53,7 @@ public class OGGLoader implements AssetLoader { // private static int BLOCK_SIZE = 4096*64; - private PhysicalOggStream oggStream; + private UncachedOggStream oggStream; private LogicalOggStream loStream; private VorbisStream vorbisStream; @@ -121,10 +120,24 @@ public class OGGLoader implements AssetLoader { } catch (EndOfOggStreamException ex){ } + byte[] dataBytes = baos.toByteArray(); swapBytes(dataBytes, 0, dataBytes.length); - ByteBuffer data = BufferUtils.createByteBuffer(dataBytes.length); - data.put(dataBytes).flip(); + // Vorbis stream could have more samples than than the duration of the sound + // Must truncate. + int numSamples = (int) oggStream.getLastOggPage().getAbsoluteGranulePosition(); + + // Number of Samples * Number of Channels * Bytes Per Sample + int totalBytes = numSamples * streamHdr.getChannels() * 2; + +// System.out.println("Sample Rate: " + streamHdr.getSampleRate()); +// System.out.println("Channels: " + streamHdr.getChannels()); +// System.out.println("Stream Length: " + numSamples); +// System.out.println("Bytes Calculated: " + totalBytes); +// System.out.println("Bytes Available: " + dataBytes.length); + + ByteBuffer data = BufferUtils.createByteBuffer(totalBytes); + data.put(dataBytes, 0, totalBytes).flip(); vorbisStream.close(); loStream.close(); diff --git a/engine/src/jogg/com/jme3/audio/plugins/UncachedOggStream.java b/engine/src/jogg/com/jme3/audio/plugins/UncachedOggStream.java index 1b3ddc292..a588c57c9 100644 --- a/engine/src/jogg/com/jme3/audio/plugins/UncachedOggStream.java +++ b/engine/src/jogg/com/jme3/audio/plugins/UncachedOggStream.java @@ -55,6 +55,7 @@ public class UncachedOggStream implements PhysicalOggStream { private InputStream sourceStream; private LinkedList pageCache = new LinkedList(); private HashMap logicalStreams = new HashMap(); + private OggPage lastPage = null; public UncachedOggStream(InputStream in) throws OggFormatException, IOException { this.sourceStream = in; @@ -65,6 +66,10 @@ public class UncachedOggStream implements PhysicalOggStream { } } + public OggPage getLastOggPage() { + return lastPage; + } + private void readNextOggPage() throws IOException { OggPage op = OggPage.create(sourceStream); if (!op.isBos()){ @@ -72,6 +77,7 @@ public class UncachedOggStream implements PhysicalOggStream { } if (op.isEos()){ eos = true; + lastPage = op; } LogicalOggStreamImpl los = (LogicalOggStreamImpl) getLogicalStream(op.getStreamSerialNumber());