mirror of
https://github.com/shadowfacts/lwjgl2-arm64.git
synced 2026-04-07 23:44:06 +00:00
moved WaveData to util
This commit is contained in:
parent
a5469e6ff3
commit
8bf163d7a8
7 changed files with 7 additions and 1 deletions
|
|
@ -37,6 +37,7 @@ import org.lwjgl.BufferUtils;
|
|||
import org.lwjgl.input.Keyboard;
|
||||
import org.lwjgl.openal.AL10;
|
||||
import org.lwjgl.opengl.Display;
|
||||
import org.lwjgl.util.WaveData;
|
||||
import org.lwjgl.util.vector.Vector3f;
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -37,6 +37,7 @@ import java.nio.IntBuffer;
|
|||
|
||||
import org.lwjgl.openal.AL;
|
||||
import org.lwjgl.openal.AL10;
|
||||
import org.lwjgl.util.WaveData;
|
||||
|
||||
/**
|
||||
* $Id$
|
||||
|
|
|
|||
|
|
@ -39,6 +39,7 @@ import java.nio.IntBuffer;
|
|||
|
||||
import org.lwjgl.BufferUtils;
|
||||
import org.lwjgl.openal.AL10;
|
||||
import org.lwjgl.util.WaveData;
|
||||
|
||||
/**
|
||||
* $Id$
|
||||
|
|
|
|||
|
|
@ -39,6 +39,7 @@ import java.nio.IntBuffer;
|
|||
|
||||
import org.lwjgl.BufferUtils;
|
||||
import org.lwjgl.openal.AL10;
|
||||
import org.lwjgl.util.WaveData;
|
||||
|
||||
/**
|
||||
* $Id$
|
||||
|
|
|
|||
|
|
@ -43,6 +43,7 @@ import org.lwjgl.openal.AL10;
|
|||
import org.lwjgl.opengl.Display;
|
||||
import org.lwjgl.opengl.GL11;
|
||||
import org.lwjgl.opengl.glu.GLU;
|
||||
import org.lwjgl.util.WaveData;
|
||||
|
||||
/**
|
||||
* $Id$
|
||||
|
|
|
|||
|
|
@ -35,6 +35,7 @@ import java.nio.IntBuffer;
|
|||
|
||||
import org.lwjgl.BufferUtils;
|
||||
import org.lwjgl.openal.AL10;
|
||||
import org.lwjgl.util.WaveData;
|
||||
|
||||
/**
|
||||
* $Id$
|
||||
|
|
|
|||
|
|
@ -1,204 +0,0 @@
|
|||
/*
|
||||
* Copyright (c) 2002-2004 LWJGL Project
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are
|
||||
* met:
|
||||
*
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* * Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* * Neither the name of 'LWJGL' nor the names of
|
||||
* its contributors may be used to endorse or promote products derived
|
||||
* from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
||||
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
package org.lwjgl.test.openal;
|
||||
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.ByteOrder;
|
||||
import java.nio.ShortBuffer;
|
||||
|
||||
import javax.sound.sampled.AudioFormat;
|
||||
import javax.sound.sampled.AudioInputStream;
|
||||
import javax.sound.sampled.AudioSystem;
|
||||
|
||||
import org.lwjgl.openal.AL10;
|
||||
|
||||
/**
|
||||
* $Id$
|
||||
*
|
||||
* Utitlity class for loading wavefiles.
|
||||
*
|
||||
* @author Brian Matzon <brian@matzon.dk>
|
||||
* @version $Revision$
|
||||
*/
|
||||
public class WaveData {
|
||||
/** actual wave data */
|
||||
public final ByteBuffer data;
|
||||
|
||||
/** format type of data */
|
||||
public final int format;
|
||||
|
||||
/** sample rate of data */
|
||||
public final int samplerate;
|
||||
|
||||
/**
|
||||
* Creates a new WaveData
|
||||
*
|
||||
* @param data actual wavedata
|
||||
* @param format format of wave data
|
||||
* @param samplerate sample rate of data
|
||||
*/
|
||||
private WaveData(ByteBuffer data, int format, int samplerate) {
|
||||
this.data = data;
|
||||
this.format = format;
|
||||
this.samplerate = samplerate;
|
||||
}
|
||||
|
||||
/**
|
||||
* Disposes the wavedata
|
||||
*/
|
||||
public void dispose() {
|
||||
data.clear();
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a WaveData container from the specified filename
|
||||
*
|
||||
* @param filepath path to file (relative, and in classpath)
|
||||
* @return WaveData containing data, or null if a failure occured
|
||||
*/
|
||||
public static WaveData create(String filepath) {
|
||||
try {
|
||||
return create(
|
||||
AudioSystem.getAudioInputStream(
|
||||
new BufferedInputStream(WaveData.class.getClassLoader().getResourceAsStream(filepath))));
|
||||
} catch (Exception e) {
|
||||
org.lwjgl.Sys.log("Unable to load file: " + filepath);
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a WaveData container from the specified bytes
|
||||
*
|
||||
* @param buffer array of bytes containing the complete wave file
|
||||
* @return WaveData containing data, or null if a failure occured
|
||||
*/
|
||||
public static WaveData create(byte[] buffer) {
|
||||
try {
|
||||
return create(
|
||||
AudioSystem.getAudioInputStream(
|
||||
new BufferedInputStream(new ByteArrayInputStream(buffer))));
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a WaveData container from the specified stream
|
||||
*
|
||||
* @param ais AudioInputStream to read from
|
||||
* @return WaveData containing data, or null if a failure occured
|
||||
*/
|
||||
public static WaveData create(AudioInputStream ais) {
|
||||
//get format of data
|
||||
AudioFormat audioformat = ais.getFormat();
|
||||
|
||||
// get channels
|
||||
int channels = 0;
|
||||
if (audioformat.getChannels() == 1) {
|
||||
if (audioformat.getSampleSizeInBits() == 8) {
|
||||
channels = AL10.AL_FORMAT_MONO8;
|
||||
} else if (audioformat.getSampleSizeInBits() == 16) {
|
||||
channels = AL10.AL_FORMAT_MONO16;
|
||||
} else {
|
||||
assert false : "Illegal sample size";
|
||||
}
|
||||
} else if (audioformat.getChannels() == 2) {
|
||||
if (audioformat.getSampleSizeInBits() == 8) {
|
||||
channels = AL10.AL_FORMAT_STEREO8;
|
||||
} else if (audioformat.getSampleSizeInBits() == 16) {
|
||||
channels = AL10.AL_FORMAT_STEREO16;
|
||||
} else {
|
||||
assert false : "Illegal sample size";
|
||||
}
|
||||
} else {
|
||||
assert false : "Only mono or stereo is supported";
|
||||
}
|
||||
|
||||
//read data into buffer
|
||||
byte[] buf =
|
||||
new byte[audioformat.getChannels()
|
||||
* (int) ais.getFrameLength()
|
||||
* audioformat.getSampleSizeInBits()
|
||||
/ 8];
|
||||
int read = 0, total = 0;
|
||||
try {
|
||||
while ((read = ais.read(buf, total, buf.length - total)) != -1
|
||||
&& total < buf.length) {
|
||||
total += read;
|
||||
}
|
||||
} catch (IOException ioe) {
|
||||
return null;
|
||||
}
|
||||
|
||||
//insert data into bytebuffer
|
||||
ByteBuffer buffer = convertAudioBytes(buf, audioformat.getSampleSizeInBits() == 16);
|
||||
/* ByteBuffer buffer = ByteBuffer.allocateDirect(buf.length);
|
||||
buffer.put(buf);
|
||||
buffer.rewind();*/
|
||||
|
||||
//create our result
|
||||
WaveData wavedata =
|
||||
new WaveData(buffer, channels, (int) audioformat.getSampleRate());
|
||||
|
||||
//close stream
|
||||
try {
|
||||
ais.close();
|
||||
} catch (IOException ioe) {
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue