diff options
author | Sven Gothel <sgothel@jausoft.com> | 2013-01-31 23:21:05 +0100 |
---|---|---|
committer | Sven Gothel <sgothel@jausoft.com> | 2013-01-31 23:21:05 +0100 |
commit | b827223da34ae4d2970a7b27f9bc0efa96bcac5a (patch) | |
tree | 416a33b3ab14b8a556191fed374a768a2a247e48 /src/java/com/jogamp/openal/util | |
parent | 034a6d264385e89e289713cb7f43a7020d6d3c46 (diff) |
Android Build & Test ; WavLoader/Data javax.audio separation (part-1)
Diffstat (limited to 'src/java/com/jogamp/openal/util')
-rw-r--r-- | src/java/com/jogamp/openal/util/WAVData.java | 71 | ||||
-rw-r--r-- | src/java/com/jogamp/openal/util/WAVLoader.java | 86 |
2 files changed, 98 insertions, 59 deletions
diff --git a/src/java/com/jogamp/openal/util/WAVData.java b/src/java/com/jogamp/openal/util/WAVData.java index 345c675..d470643 100644 --- a/src/java/com/jogamp/openal/util/WAVData.java +++ b/src/java/com/jogamp/openal/util/WAVData.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,13 +34,21 @@ package com.jogamp.openal.util; +import java.io.IOException; +import java.io.InputStream; import java.nio.ByteBuffer; +import java.nio.ByteOrder; +import java.nio.channels.Channels; +import java.nio.channels.ReadableByteChannel; +import com.jogamp.openal.ALConstants; +import com.jogamp.openal.UnsupportedAudioFileException; /** - * This class is a holder for WAV (.wav )file Data returned from the WavLoader + * This class is a holder for WAV (.wav ) file Data returned from the WavLoader, + * or directly via {@link #loadFromStream(InputStream, int, int, int)}. * - * @author Athomas Goldberg + * @author Athomas Goldberg, et.al. */ public final class WAVData { /** The audio data */ @@ -65,11 +74,67 @@ public final class WAVData { /** flag indicating whether or not the sound in the data should loop */ public final boolean loop; - WAVData(ByteBuffer data, int format, int size, int freq, boolean loop) { + public WAVData(ByteBuffer data, int format, int size, int freq, boolean loop) { this.data = data; this.format = format; this.size = size; this.freq = freq; this.loop = loop; } + + /** + * This method loads a (.wav) file into a WAVData object. + * + * @param stream An InputStream for the .WAV stream + * @param numChannels + * @param bits + * @param sampleRate + * + * @return a WAVData object containing the audio data + * + * @throws UnsupportedAudioFileException if the format of the audio if not + * supported. + * @throws IOException If the file can no be found or some other IO error + * occurs + */ + public static WAVData loadFromStream(InputStream aIn, int numChannels, int bits, int sampleRate) + throws IOException { + ReadableByteChannel aChannel = Channels.newChannel(aIn); + int format = ALConstants.AL_FORMAT_MONO8; + + if ((bits == 8) && (numChannels == 1)) { + format = ALConstants.AL_FORMAT_MONO8; + } else if ((bits == 16) && (numChannels == 1)) { + format = ALConstants.AL_FORMAT_MONO16; + } else if ((bits == 8) && (numChannels == 2)) { + format = ALConstants.AL_FORMAT_STEREO8; + } else if ((bits == 16) && (numChannels == 2)) { + format = ALConstants.AL_FORMAT_STEREO16; + } + + 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, sampleRate, false); + aIn.close(); + + return result; + } + } 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())); } + } |