aboutsummaryrefslogtreecommitdiffstats
path: root/src/java/com/jogamp/openal/util/WAVLoader.java
diff options
context:
space:
mode:
authorSven Gothel <[email protected]>2013-01-31 23:21:05 +0100
committerSven Gothel <[email protected]>2013-01-31 23:21:05 +0100
commitb827223da34ae4d2970a7b27f9bc0efa96bcac5a (patch)
tree416a33b3ab14b8a556191fed374a768a2a247e48 /src/java/com/jogamp/openal/util/WAVLoader.java
parent034a6d264385e89e289713cb7f43a7020d6d3c46 (diff)
Android Build & Test ; WavLoader/Data javax.audio separation (part-1)
Diffstat (limited to 'src/java/com/jogamp/openal/util/WAVLoader.java')
-rw-r--r--src/java/com/jogamp/openal/util/WAVLoader.java86
1 files changed, 30 insertions, 56 deletions
diff --git a/src/java/com/jogamp/openal/util/WAVLoader.java b/src/java/com/jogamp/openal/util/WAVLoader.java
index 4ee2305..40ad99e 100644
--- a/src/java/com/jogamp/openal/util/WAVLoader.java
+++ b/src/java/com/jogamp/openal/util/WAVLoader.java
@@ -1,5 +1,6 @@
/**
* Copyright (c) 2003 Sun Microsystems, Inc. All Rights Reserved.
+* Copyright (c) 2011 JogAmp Community. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
@@ -33,21 +34,24 @@
package com.jogamp.openal.util;
-import java.io.*;
-import java.nio.*;
-import java.nio.channels.*;
-import javax.sound.sampled.*;
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
-import com.jogamp.openal.*;
+import javax.sound.sampled.AudioFormat;
+import javax.sound.sampled.AudioInputStream;
+import javax.sound.sampled.AudioSystem;
+
+import com.jogamp.openal.UnsupportedAudioFileException;
/**
* A Loader utility for (.wav) files. Creates a WAVData object containing the
* data used by the AL.alBufferData method.
*
- * @author Athomas Goldberg
+ * @author Athomas Goldberg, et.al
*/
-public class WAVLoader implements ALConstants {
- private static final int BUFFER_SIZE = 128000;
+public class WAVLoader {
+ // private static final int BUFFER_SIZE = 128000;
/**
* This method loads a (.wav) file into a WAVData object.
@@ -63,16 +67,19 @@ public class WAVLoader implements ALConstants {
*/
public static WAVData loadFromFile(String filename)
throws UnsupportedAudioFileException, IOException {
- WAVData result = null;
File soundFile = new File(filename);
- AudioInputStream aIn = AudioSystem.getAudioInputStream(soundFile);
- return readFromStream(aIn);
+ try {
+ AudioInputStream aIn = AudioSystem.getAudioInputStream(soundFile);
+ return loadFromStreamImpl(aIn);
+ } catch (javax.sound.sampled.UnsupportedAudioFileException e) {
+ throw new UnsupportedAudioFileException(e);
+ }
}
/**
* This method loads a (.wav) file into a WAVData object.
*
- * @param stream An InputStream for the .WAV file.
+ * @param stream An InputStream for the .WAV stream.
*
* @return a WAVData object containing the audio data
*
@@ -83,53 +90,20 @@ public class WAVLoader implements ALConstants {
*/
public static WAVData loadFromStream(InputStream stream)
throws UnsupportedAudioFileException, IOException {
- WAVData result = null;
- AudioInputStream aIn = AudioSystem.getAudioInputStream(stream);
- return readFromStream(aIn);
+ AudioInputStream aIn;
+ try {
+ aIn = AudioSystem.getAudioInputStream(stream);
+ return loadFromStreamImpl(aIn);
+ } catch (javax.sound.sampled.UnsupportedAudioFileException e) {
+ throw new UnsupportedAudioFileException(e);
+ }
}
- private static WAVData readFromStream(AudioInputStream aIn)
+ private static WAVData loadFromStreamImpl(AudioInputStream aIn)
throws UnsupportedAudioFileException, IOException {
- ReadableByteChannel aChannel = Channels.newChannel(aIn);
- AudioFormat fmt = aIn.getFormat();
- int numChannels = fmt.getChannels();
- int bits = fmt.getSampleSizeInBits();
- int format = AL_FORMAT_MONO8;
-
- if ((bits == 8) && (numChannels == 1)) {
- format = AL_FORMAT_MONO8;
- } else if ((bits == 16) && (numChannels == 1)) {
- format = AL_FORMAT_MONO16;
- } else if ((bits == 8) && (numChannels == 2)) {
- format = AL_FORMAT_STEREO8;
- } else if ((bits == 16) && (numChannels == 2)) {
- format = AL_FORMAT_STEREO16;
- }
-
- int freq = Math.round(fmt.getSampleRate());
- int size = aIn.available();
- ByteBuffer buffer = ByteBuffer.allocateDirect(size);
- while (buffer.remaining() > 0) {
- aChannel.read(buffer);
- }
- buffer.rewind();
-
- // Must byte swap on big endian platforms
- // Thanks to swpalmer on javagaming.org forums for hint at fix
- if ((bits == 16) && (ByteOrder.nativeOrder() == ByteOrder.BIG_ENDIAN)) {
- int len = buffer.remaining();
- for (int i = 0; i < len; i += 2) {
- byte a = buffer.get(i);
- byte b = buffer.get(i+1);
- buffer.put(i, b);
- buffer.put(i+1, a);
- }
- }
-
- WAVData result = new WAVData(buffer, format, size, freq, false);
- aIn.close();
-
- return result;
+ final AudioFormat fmt = aIn.getFormat();
+ return WAVData.loadFromStream(aIn, fmt.getChannels(), fmt.getSampleSizeInBits(), Math.round(fmt.getSampleRate()));
}
+
}