Browse Source

* Fixed bug with OGG audio having incorrect length

git-svn-id: https://jmonkeyengine.googlecode.com/svn/trunk@7198 75d07b2b-3a1a-0410-a2c5-0572b91ccdca
sha..rd 14 years ago
parent
commit
bb238c3c99

+ 2 - 1
engine/src/jogg/com/jme3/audio/plugins/CachedOggStream.java

@@ -41,6 +41,7 @@ import java.io.InputStream;
 import java.util.ArrayList;
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.HashMap;
 import java.util.Collection;
 import java.util.Collection;
+import java.util.logging.Level;
 import java.util.logging.Logger;
 import java.util.logging.Logger;
 
 
 /**
 /**
@@ -72,7 +73,7 @@ public class CachedOggStream implements PhysicalOggStream {
      *  file is not automatically deleted when this class is disposed.
      *  file is not automatically deleted when this class is disposed.
      */
      */
     public CachedOggStream(InputStream stream, int length, int numPages) throws IOException {
     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];
         memoryCache = new byte[length];
         sourceStream = stream;
         sourceStream = stream;

+ 18 - 5
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.asset.AssetLoader;
 import com.jme3.audio.AudioKey;
 import com.jme3.audio.AudioKey;
 import com.jme3.util.BufferUtils;
 import com.jme3.util.BufferUtils;
-import com.jme3.util.TempVars;
 import de.jarnbjo.ogg.EndOfOggStreamException;
 import de.jarnbjo.ogg.EndOfOggStreamException;
 import de.jarnbjo.ogg.LogicalOggStream;
 import de.jarnbjo.ogg.LogicalOggStream;
-import de.jarnbjo.ogg.PhysicalOggStream;
+import de.jarnbjo.ogg.OggPage;
 import de.jarnbjo.vorbis.IdentificationHeader;
 import de.jarnbjo.vorbis.IdentificationHeader;
 import de.jarnbjo.vorbis.VorbisStream;
 import de.jarnbjo.vorbis.VorbisStream;
 import java.io.ByteArrayOutputStream;
 import java.io.ByteArrayOutputStream;
@@ -54,7 +53,7 @@ public class OGGLoader implements AssetLoader {
 
 
 //    private static int BLOCK_SIZE = 4096*64;
 //    private static int BLOCK_SIZE = 4096*64;
 
 
-    private PhysicalOggStream oggStream;
+    private UncachedOggStream oggStream;
     private LogicalOggStream loStream;
     private LogicalOggStream loStream;
     private VorbisStream vorbisStream;
     private VorbisStream vorbisStream;
 
 
@@ -121,10 +120,24 @@ public class OGGLoader implements AssetLoader {
         } catch (EndOfOggStreamException ex){
         } catch (EndOfOggStreamException ex){
         }
         }
 
 
+       
         byte[] dataBytes = baos.toByteArray();
         byte[] dataBytes = baos.toByteArray();
         swapBytes(dataBytes, 0, dataBytes.length);
         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();
         vorbisStream.close();
         loStream.close();
         loStream.close();

+ 6 - 0
engine/src/jogg/com/jme3/audio/plugins/UncachedOggStream.java

@@ -55,6 +55,7 @@ public class UncachedOggStream implements PhysicalOggStream {
     private InputStream sourceStream;
     private InputStream sourceStream;
     private LinkedList<OggPage> pageCache = new LinkedList<OggPage>();
     private LinkedList<OggPage> pageCache = new LinkedList<OggPage>();
     private HashMap<Integer, LogicalOggStream> logicalStreams = new HashMap();
     private HashMap<Integer, LogicalOggStream> logicalStreams = new HashMap();
+    private OggPage lastPage = null;
 
 
     public UncachedOggStream(InputStream in) throws OggFormatException, IOException {
     public UncachedOggStream(InputStream in) throws OggFormatException, IOException {
         this.sourceStream = in;
         this.sourceStream = in;
@@ -65,6 +66,10 @@ public class UncachedOggStream implements PhysicalOggStream {
         }
         }
     }
     }
 
 
+    public OggPage getLastOggPage() {
+        return lastPage;
+    }
+
     private void readNextOggPage() throws IOException {
     private void readNextOggPage() throws IOException {
         OggPage op = OggPage.create(sourceStream);
         OggPage op = OggPage.create(sourceStream);
         if (!op.isBos()){
         if (!op.isBos()){
@@ -72,6 +77,7 @@ public class UncachedOggStream implements PhysicalOggStream {
         }
         }
         if (op.isEos()){
         if (op.isEos()){
             eos = true;
             eos = true;
+            lastPage = op;
         }
         }
 
 
         LogicalOggStreamImpl los = (LogicalOggStreamImpl) getLogicalStream(op.getStreamSerialNumber());
         LogicalOggStreamImpl los = (LogicalOggStreamImpl) getLogicalStream(op.getStreamSerialNumber());