*** empty log message ***

This commit is contained in:
Elias Naur 2003-10-22 11:33:01 +00:00
parent 561f3cff10
commit 931f15f27f
1 changed files with 61 additions and 40 deletions

View File

@ -7,15 +7,15 @@
* met: * met:
* *
* * Redistributions of source code must retain the above copyright * * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer. * notice, this list of conditions and the following disclaimer.
* *
* * Redistributions in binary form must reproduce the above copyright * * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the * notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution. * documentation and/or other materials provided with the distribution.
* *
* * Neither the name of 'Light Weight Java Game Library' nor the names of * * Neither the name of 'Light Weight Java Game Library' nor the names of
* its contributors may be used to endorse or promote products derived * its contributors may be used to endorse or promote products derived
* from this software without specific prior written permission. * from this software without specific prior written permission.
* *
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
@ -34,6 +34,8 @@ package org.lwjgl.test.openal;
import org.lwjgl.openal.AL; import org.lwjgl.openal.AL;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.ShortBuffer;
import java.nio.ByteOrder;
import javax.sound.sampled.AudioInputStream; import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem; import javax.sound.sampled.AudioSystem;
@ -74,12 +76,12 @@ public class WaveData {
this.samplerate = samplerate; this.samplerate = samplerate;
} }
/** /**
* Disposes the wavedata * Disposes the wavedata
*/ */
public void dispose() { public void dispose() {
data.clear(); data.clear();
} }
/** /**
* Creates a WaveData container from the specified filename * Creates a WaveData container from the specified filename
@ -88,32 +90,32 @@ public class WaveData {
* @return WaveData containing data, or null if a failure occured * @return WaveData containing data, or null if a failure occured
*/ */
public static WaveData create(String filepath) { public static WaveData create(String filepath) {
try { try {
return create( return create(
AudioSystem.getAudioInputStream( AudioSystem.getAudioInputStream(
new BufferedInputStream(WaveData.class.getClassLoader().getResourceAsStream(filepath)))); new BufferedInputStream(WaveData.class.getClassLoader().getResourceAsStream(filepath))));
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
return null; return null;
} }
} }
/** /**
* Creates a WaveData container from the specified bytes * Creates a WaveData container from the specified bytes
* *
* @param buffer array of bytes containing the complete wave file * @param buffer array of bytes containing the complete wave file
* @return WaveData containing data, or null if a failure occured * @return WaveData containing data, or null if a failure occured
*/ */
public static WaveData create(byte[] buffer) { public static WaveData create(byte[] buffer) {
try { try {
return create( return create(
AudioSystem.getAudioInputStream( AudioSystem.getAudioInputStream(
new BufferedInputStream(new ByteArrayInputStream(buffer)))); new BufferedInputStream(new ByteArrayInputStream(buffer))));
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
return null; return null;
} }
} }
/** /**
* Creates a WaveData container from the specified stream * Creates a WaveData container from the specified stream
@ -164,15 +166,16 @@ public class WaveData {
} }
//insert data into bytebuffer //insert data into bytebuffer
ByteBuffer buffer = ByteBuffer.allocateDirect(buf.length); ByteBuffer buffer = convertAudioBytes(buf, audioformat.getSampleSizeInBits() == 16);
/* ByteBuffer buffer = ByteBuffer.allocateDirect(buf.length);
buffer.put(buf); buffer.put(buf);
buffer.rewind(); buffer.rewind();*/
//create our result //create our result
WaveData wavedata = WaveData wavedata =
new WaveData(buffer, channels, (int) audioformat.getSampleRate()); new WaveData(buffer, channels, (int) audioformat.getSampleRate());
//close stream //close stream
try { try {
ais.close(); ais.close();
} catch (IOException ioe) { } catch (IOException ioe) {
@ -180,4 +183,22 @@ public class WaveData {
return wavedata; return wavedata;
} }
private static ByteBuffer convertAudioBytes(byte[] audio_bytes, boolean two_bytes_data) {
ByteBuffer dest = ByteBuffer.allocateDirect(audio_bytes.length);
dest.order(ByteOrder.nativeOrder());
ByteBuffer src = ByteBuffer.wrap(audio_bytes);
src.order(ByteOrder.LITTLE_ENDIAN);
if (two_bytes_data) {
ShortBuffer dest_short = dest.asShortBuffer();
ShortBuffer src_short = src.asShortBuffer();
while (src_short.hasRemaining())
dest_short.put(src_short.get());
} else {
while (src.hasRemaining())
dest.put(src.get());
}
dest.rewind();
return dest;
}
} }