Added support for Ogg clips, which are decompressed when bound.
git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@4110 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
@@ -21,6 +21,8 @@
|
|||||||
|
|
||||||
package com.threerings.openal;
|
package com.threerings.openal;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
import java.nio.ByteBuffer;
|
import java.nio.ByteBuffer;
|
||||||
|
|
||||||
import org.lwjgl.openal.AL10;
|
import org.lwjgl.openal.AL10;
|
||||||
@@ -28,7 +30,7 @@ import org.lwjgl.openal.AL10;
|
|||||||
/**
|
/**
|
||||||
* Contains data for a single sampled sound.
|
* Contains data for a single sampled sound.
|
||||||
*/
|
*/
|
||||||
public class Clip
|
public abstract class Clip
|
||||||
{
|
{
|
||||||
/** The OpenAL format of this clip: {@link AL10#AL_FORMAT_MONO8}, etc. */
|
/** The OpenAL format of this clip: {@link AL10#AL_FORMAT_MONO8}, etc. */
|
||||||
public int format;
|
public int format;
|
||||||
@@ -36,6 +38,9 @@ public class Clip
|
|||||||
/** The frequency of this clip in samples per second. */
|
/** The frequency of this clip in samples per second. */
|
||||||
public int frequency;
|
public int frequency;
|
||||||
|
|
||||||
/** The audio data. */
|
/**
|
||||||
public ByteBuffer data;
|
* Returns a buffer containing the clip's audio data.
|
||||||
|
*/
|
||||||
|
public abstract ByteBuffer getData ()
|
||||||
|
throws IOException;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -194,8 +194,15 @@ public class ClipBuffer
|
|||||||
*/
|
*/
|
||||||
protected boolean bind (Clip clip)
|
protected boolean bind (Clip clip)
|
||||||
{
|
{
|
||||||
AL10.alBufferData(
|
try {
|
||||||
_bufferId.get(0), clip.format, clip.data, clip.frequency);
|
AL10.alBufferData(
|
||||||
|
_bufferId.get(0), clip.format, clip.getData(), clip.frequency);
|
||||||
|
} catch (IOException e) {
|
||||||
|
Log.warning("Error decoding clip [key=" + getKey() +
|
||||||
|
", error=" + e + "].");
|
||||||
|
failed();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
int errno = AL10.alGetError();
|
int errno = AL10.alGetError();
|
||||||
if (errno != AL10.AL_NO_ERROR) {
|
if (errno != AL10.AL_NO_ERROR) {
|
||||||
Log.warning("Failed to bind clip [key=" + getKey() +
|
Log.warning("Failed to bind clip [key=" + getKey() +
|
||||||
|
|||||||
@@ -0,0 +1,92 @@
|
|||||||
|
//
|
||||||
|
// $Id$
|
||||||
|
//
|
||||||
|
// Narya library - tools for developing networked games
|
||||||
|
// Copyright (C) 2002-2005 Three Rings Design, Inc., All Rights Reserved
|
||||||
|
// http://www.threerings.net/code/narya/
|
||||||
|
//
|
||||||
|
// This library is free software; you can redistribute it and/or modify it
|
||||||
|
// under the terms of the GNU Lesser General Public License as published
|
||||||
|
// by the Free Software Foundation; either version 2.1 of the License, or
|
||||||
|
// (at your option) any later version.
|
||||||
|
//
|
||||||
|
// This library is distributed in the hope that it will be useful,
|
||||||
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||||
|
// Lesser General Public License for more details.
|
||||||
|
//
|
||||||
|
// You should have received a copy of the GNU Lesser General Public
|
||||||
|
// License along with this library; if not, write to the Free Software
|
||||||
|
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||||
|
|
||||||
|
package com.threerings.openal;
|
||||||
|
|
||||||
|
import java.io.BufferedInputStream;
|
||||||
|
import java.io.ByteArrayOutputStream;
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileInputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import java.nio.ByteBuffer;
|
||||||
|
|
||||||
|
import org.lwjgl.openal.AL10;
|
||||||
|
|
||||||
|
import com.samskivert.io.ByteArrayOutInputStream;
|
||||||
|
|
||||||
|
import com.jmex.sound.openAL.objects.util.OggInputStream;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A sound clip backed by Ogg Vorbis data loaded from a file.
|
||||||
|
*/
|
||||||
|
public class OggFileClip extends Clip
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Loads the clip from the given file.
|
||||||
|
*/
|
||||||
|
public OggFileClip (File file)
|
||||||
|
throws IOException
|
||||||
|
{
|
||||||
|
// read the file in completely
|
||||||
|
FileInputStream in = new FileInputStream(file);
|
||||||
|
_data = new ByteArrayOutInputStream((int)file.length());
|
||||||
|
byte[] buffer = new byte[1024];
|
||||||
|
int read;
|
||||||
|
while ((read = in.read(buffer)) > 0) {
|
||||||
|
_data.write(buffer, 0, read);
|
||||||
|
}
|
||||||
|
_istream = new OggInputStream(_data.getInputStream());
|
||||||
|
format = (_istream.getFormat() == OggInputStream.FORMAT_MONO16) ?
|
||||||
|
AL10.AL_FORMAT_MONO16 : AL10.AL_FORMAT_STEREO16;
|
||||||
|
frequency = _istream.getRate();
|
||||||
|
}
|
||||||
|
|
||||||
|
// documentation inherited
|
||||||
|
public ByteBuffer getData ()
|
||||||
|
throws IOException
|
||||||
|
{
|
||||||
|
// we keep the Ogg stream from initialization, but reset it when we
|
||||||
|
// re-read the data
|
||||||
|
if (_istream == null) {
|
||||||
|
_istream = new OggInputStream(_data.getInputStream());
|
||||||
|
}
|
||||||
|
// decompress the entire stream
|
||||||
|
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||||
|
byte[] buffer = new byte[1024];
|
||||||
|
int read;
|
||||||
|
while ((read = _istream.read(buffer, 0, buffer.length)) > 0) {
|
||||||
|
out.write(buffer, 0, read);
|
||||||
|
}
|
||||||
|
byte[] bytes = out.toByteArray();
|
||||||
|
ByteBuffer data = ByteBuffer.allocateDirect(bytes.length);
|
||||||
|
data.put(bytes);
|
||||||
|
data.rewind();
|
||||||
|
_istream = null;
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** The entire encoded file. */
|
||||||
|
protected ByteArrayOutInputStream _data;
|
||||||
|
|
||||||
|
/** The Ogg input stream for decoding the data. */
|
||||||
|
protected OggInputStream _istream;
|
||||||
|
}
|
||||||
@@ -0,0 +1,39 @@
|
|||||||
|
//
|
||||||
|
// $Id$
|
||||||
|
//
|
||||||
|
// Narya library - tools for developing networked games
|
||||||
|
// Copyright (C) 2002-2005 Three Rings Design, Inc., All Rights Reserved
|
||||||
|
// http://www.threerings.net/code/narya/
|
||||||
|
//
|
||||||
|
// This library is free software; you can redistribute it and/or modify it
|
||||||
|
// under the terms of the GNU Lesser General Public License as published
|
||||||
|
// by the Free Software Foundation; either version 2.1 of the License, or
|
||||||
|
// (at your option) any later version.
|
||||||
|
//
|
||||||
|
// This library is distributed in the hope that it will be useful,
|
||||||
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||||
|
// Lesser General Public License for more details.
|
||||||
|
//
|
||||||
|
// You should have received a copy of the GNU Lesser General Public
|
||||||
|
// License along with this library; if not, write to the Free Software
|
||||||
|
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||||
|
|
||||||
|
package com.threerings.openal;
|
||||||
|
|
||||||
|
import java.nio.ByteBuffer;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A clip that contains uncompressed wave data.
|
||||||
|
*/
|
||||||
|
public class WaveDataClip extends Clip
|
||||||
|
{
|
||||||
|
/** The audio data. */
|
||||||
|
public ByteBuffer data;
|
||||||
|
|
||||||
|
// documentation inherited
|
||||||
|
public ByteBuffer getData ()
|
||||||
|
{
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -23,6 +23,8 @@ package com.threerings.openal;
|
|||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import java.nio.ByteBuffer;
|
||||||
|
|
||||||
import org.lwjgl.util.WaveData;
|
import org.lwjgl.util.WaveData;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -33,7 +35,7 @@ public class WaveDataClipProvider implements ClipProvider
|
|||||||
{
|
{
|
||||||
public Clip loadClip (String path) throws IOException
|
public Clip loadClip (String path) throws IOException
|
||||||
{
|
{
|
||||||
Clip clip = new Clip();
|
WaveDataClip clip = new WaveDataClip();
|
||||||
WaveData file = WaveData.create(path);
|
WaveData file = WaveData.create(path);
|
||||||
if (file == null) {
|
if (file == null) {
|
||||||
throw new IOException("Error loading " + path);
|
throw new IOException("Error loading " + path);
|
||||||
|
|||||||
Reference in New Issue
Block a user