fixed offset/length to use buffer properties

keep reference to buffers until free'd
added FMUSIC_LoadSongEx test
This commit is contained in:
Brian Matzon 2006-06-23 08:14:49 +00:00
parent 20663ff01b
commit d20c118cd6
11 changed files with 232 additions and 39 deletions

View File

@ -84,7 +84,7 @@ public class FMusic {
public static FMusicModule FMUSIC_LoadSong(String name) {
long result = nFMUSIC_LoadSong(name);
if(result != FMUSIC_TYPE_NONE) {
return new FMusicModule(result);
return new FMusicModule(result, null);
}
return null;
}
@ -110,16 +110,14 @@ public class FMusic {
* </p>
*
* @param data containing song to load. On PlayStation 2 data must be 16 byte aligned if loading from memory
* @param offset Optional. 0 by default. If > 0, this value is used to specify an offset in a file, so fmod will seek before opening
* @param length Optional. 0 by default. If > 0, this value is used to specify the length of a memory block when using FSOUND_LOADMEMORY, or it is the length of a file or file segment if the offset parameter is used. On PlayStation 2 this must be 16 byte aligned for memory loading
* @param mode Mode for opening song. With module files, only FSOUND_LOADMEMORY, FSOUND_NONBLOCKING, FSOUND_LOOP_NORMAL, or FSOUND_LOOP_OFF are supported. For FSB files, FSOUND_2D, FSOUND_HW3D, FSOUND_FORCEMONO also work
* @param sampleList Optional. Buffer of sample indicies to load. Leave as Null if you want all samples to be loaded (default behaviour). See Remarks for more on this
* @return On success, a FMusicModule instance is returned. On failure, Null is returned
*/
public static FMusicModule FMUSIC_LoadSongEx(ByteBuffer data, int offset, int length, int mode, IntBuffer sampleList) {
long result = nFMUSIC_LoadSongEx(data, data.position(), offset, length, mode, (sampleList != null) ? sampleList : null, (sampleList != null) ? sampleList.position() : 0, (sampleList != null) ? sampleList.remaining() : 0);
public static FMusicModule FMUSIC_LoadSongEx(ByteBuffer data, int mode, IntBuffer sampleList) {
long result = nFMUSIC_LoadSongEx(data, data.position(), data.remaining(), mode, (sampleList != null) ? sampleList : null, (sampleList != null) ? sampleList.position() : 0, (sampleList != null) ? sampleList.remaining() : 0);
if(result != FMUSIC_TYPE_NONE) {
return new FMusicModule(result);
return new FMusicModule(result, data);
}
return null;
}
@ -137,11 +135,11 @@ public class FMusic {
long result = nFMUSIC_LoadSongEx(name, offset, length, mode, (sampleList != null) ? sampleList : null, (sampleList != null) ? sampleList.position() : 0, (sampleList != null) ? sampleList.remaining() : 0);
if(result != FMUSIC_TYPE_NONE) {
return new FMusicModule(result);
return new FMusicModule(result, null);
}
return null;
}
private static native long nFMUSIC_LoadSongEx(ByteBuffer data, int dataOffset, int offset, int length, int mode, IntBuffer sampleList, int bufferOffset, int remaining);
private static native long nFMUSIC_LoadSongEx(ByteBuffer data, int offset, int length, int mode, IntBuffer sampleList, int bufferOffset, int remaining);
private static native long nFMUSIC_LoadSongEx(String name, int offset, int length, int mode, IntBuffer sampleList, int bufferOffset, int remaining);
/**
@ -165,6 +163,7 @@ public class FMusic {
FMOD.registerCallback(FMOD.FMUSIC_ORDERCALLBACK, module.moduleHandle, null, null);
FMOD.registerCallback(FMOD.FMUSIC_ROWCALLBACK, module.moduleHandle, null, null);
FMOD.registerCallback(FMOD.FMUSIC_ZXXCALLBACK, module.moduleHandle, null, null);
module.release();
return nFMUSIC_FreeSong(module.moduleHandle);
}
private static native boolean nFMUSIC_FreeSong(long module);
@ -335,6 +334,7 @@ public class FMusic {
* @return On success, true is returned. On failure, false is returned
*/
public static boolean FMUSIC_SetUserData(FMusicModule module, ByteBuffer userdata) {
module.userData = userdata;
return nFMUSIC_SetUserData(module.moduleHandle, userdata, userdata.position());
}
private static native boolean nFMUSIC_SetUserData(long module, ByteBuffer userdata, int offset);
@ -551,7 +551,7 @@ public class FMusic {
public static FSoundSample FMUSIC_GetSample(FMusicModule module, int sampno) {
long result = nFMUSIC_GetSample(module.moduleHandle, sampno);
if(result != 0) {
return new FSoundSample(result);
return new FSoundSample(result, null);
}
return null;
}

View File

@ -31,6 +31,9 @@
*/
package org.lwjgl.fmod3;
import java.nio.Buffer;
import java.nio.ByteBuffer;
/**
* This class is a representation of a Module in FMod.
* <br>
@ -42,12 +45,27 @@ public class FMusicModule {
/** Handle to module */
long moduleHandle;
/** Handle to buffer containing module data */
Buffer moduleData;
/** Handle to buffer containing user data */
Buffer userData;
/**
* Creates a new FMusicModule
*
* @param moduleHandle
*/
FMusicModule(long moduleHandle) {
this.moduleHandle = moduleHandle;
FMusicModule(long moduleHandle, Buffer moduleData) {
this.moduleHandle = moduleHandle;
this.moduleData = moduleData;
}
/**
* Releases the reference to any data contained
*/
void release() {
this.moduleData = null;
this.userData = null;
}
}

View File

@ -877,7 +877,7 @@ public class FSound {
*/
public static FSoundSample FSOUND_Sample_Alloc(int index, int length, int mode, int deffreq, int defvol, int defpan, int defpri) {
long result = nFSOUND_Sample_Alloc(index, length, mode, deffreq, defvol, defpan, defpri);
return (result != 0) ? new FSoundSample(result) : null;
return (result != 0) ? new FSoundSample(result, null) : null;
}
private static native long nFSOUND_Sample_Alloc(int index, int length, int mode, int deffreq, int defvol, int defpan, int defpri);
@ -886,6 +886,7 @@ public class FSound {
* @param sample sample definition to be freed
*/
public static void FSOUND_Sample_Free(FSoundSample sample) {
sample.release();
nFSOUND_Sample_Free(sample.sampleHandle);
}
private static native void nFSOUND_Sample_Free(long sample);
@ -902,7 +903,7 @@ public class FSound {
*/
public static FSoundSample FSOUND_Sample_Get(int sampno) {
long result = nFSOUND_Sample_Get(sampno);
return (result != 0) ? new FSoundSample(result) : null;
return (result != 0) ? new FSoundSample(result, null) : null;
}
private static native long nFSOUND_Sample_Get(int sampno);
@ -1056,14 +1057,12 @@ public class FSound {
* FSOUND_UNMANAGED - Dont have this sample managed within fsounds sample management system
* @param data ByteBuffer of memory image to load.
* @param inputmode Description of the data format, OR in the bits defined in FSOUND_MODES to describe the data being loaded.
* @param offset Optional. 0 by default. If > 0, this value is used to specify an offset in a file, so fmod will seek before opening. length must also be specified if this value is used.
* @param length Optional. 0 by default. If > 0, this value is used to specify the length of a memory block when using FSOUND_LOADMEMORY, or it is the length of a file or file segment if the offset parameter is used. On PlayStation 2 this must be 16 byte aligned for memory loading.
* @return On success, a sample is returned. On failure, NULL is returned.
*/
public static FSoundSample FSOUND_Sample_Load(int index, ByteBuffer data, int inputmode, int offset, int length) {
long result = nFSOUND_Sample_Load(index, data, data.position(), inputmode, offset, length);
public static FSoundSample FSOUND_Sample_Load(int index, ByteBuffer data, int inputmode) {
long result = nFSOUND_Sample_Load(index, data, inputmode, data.position(), data.remaining());
if(result != 0) {
return new FSoundSample(result);
return new FSoundSample(result, data);
}
return null;
}
@ -1083,11 +1082,11 @@ public class FSound {
public static FSoundSample FSOUND_Sample_Load(int index, String name, int inputmode, int offset, int length) {
long result = nFSOUND_Sample_Load(index, name, inputmode, offset, length);
if(result != 0) {
return new FSoundSample(result);
return new FSoundSample(result, null);
}
return null;
}
private static native long nFSOUND_Sample_Load(int index, ByteBuffer data, int dataOffset, int inputmode, int offset, int length);
private static native long nFSOUND_Sample_Load(int index, ByteBuffer data, int inputmode, int offset, int length);
private static native long nFSOUND_Sample_Load(int index, String name, int inputmode, int offset, int length);
/**
@ -1693,7 +1692,7 @@ public class FSound {
public static FSoundSample FSOUND_GetCurrentSample(int channel) {
long result = nFSOUND_GetCurrentSample(channel);
if(result != 0) {
return new FSoundSample(result);
return new FSoundSample(result, null);
}
return null;
}
@ -2088,7 +2087,7 @@ public class FSound {
public static FSoundStream FSOUND_Stream_Open(String name, int mode, int offset, int length) {
long result = nFSOUND_Stream_Open(name, mode, offset, length);
if(result != 0) {
return new FSoundStream(result);
return new FSoundStream(result, null);
}
return null;
}
@ -2098,18 +2097,16 @@ public class FSound {
* @param data data when FSOUND_LOADMEMORY is used.
* @param mode Simple description of how to play the file. For all formats except raw PCM,
* FSOUND_LOOP*, FSOUND_HW3D, FSOUND_HW2D, FSOUND_2D, FSOUND_LOADMEMORY, FSOUND_LOADRAW, FSOUND_MPEGACCURATE, FSOUND_NONBLOCKING flags are the only ones supported.
* @param offset Optional. 0 by default. If > 0, this value is used to specify an offset in a file, so fmod will seek before opening. length must also be specified if this value is used.
* @param length Optional. 0 by default. If > 0, this value is used to specify the length of a memory block when using FSOUND_LOADMEMORY, or it is the length of a file or file segment if the offset parameter is used. On PlayStation 2 this must be 16 byte aligned for memory loading.
* @return On success, a reference to an opened stream is returned. On failure, NULL is returned.
*/
public static FSoundStream FSOUND_Stream_Open(ByteBuffer data, int mode, int offset, int length) {
long result = nFSOUND_Stream_Open(data, data.position(), mode, offset, length);
public static FSoundStream FSOUND_Stream_Open(ByteBuffer data, int mode) {
long result = nFSOUND_Stream_Open(data, mode, data.position(), data.remaining());
if(result != 0) {
return new FSoundStream(result);
return new FSoundStream(result, data);
}
return null;
}
private static native long nFSOUND_Stream_Open(ByteBuffer data, int dataOffset, int mode, int offset, int length);
private static native long nFSOUND_Stream_Open(ByteBuffer data, int mode, int offset, int length);
private static native long nFSOUND_Stream_Open(String name, int mode, int offset, int length);
/**
@ -2170,6 +2167,7 @@ public class FSound {
* @return On success, TRUE is returned. On failure, FALSE is returned.
*/
public static boolean FSOUND_Stream_Close(FSoundStream stream) {
stream.release();
return nFSOUND_Stream_Close(stream.streamHandle);
}
private static native boolean nFSOUND_Stream_Close(long streamhandle);
@ -2243,7 +2241,7 @@ public class FSound {
FSoundStream stream = null;
long result = nFSOUND_Stream_Create(lenbytes, mode, samplerate);
if(result != 0) {
stream = new FSoundStream(result);
stream = new FSoundStream(result, null);
FMOD.registerCallback(FMOD.FSOUND_STREAMCALLBACK, stream.streamHandle, stream, callbackHandler);
}
return stream;
@ -2414,7 +2412,7 @@ public class FSound {
public static FSoundSample FSOUND_Stream_GetSample(FSoundStream stream) {
long result = nFSOUND_Stream_GetSample(stream.streamHandle);
if(result != 0) {
return new FSoundSample(result);
return new FSoundSample(result, null);
}
return null;
}

View File

@ -31,6 +31,8 @@
*/
package org.lwjgl.fmod3;
import java.nio.Buffer;
/**
* This class is a representation of a Sound Sample in FMod.
* <br>
@ -42,12 +44,23 @@ public class FSoundSample {
/** Handle to sample */
long sampleHandle;
/** Handle to buffer containing sample data */
Buffer sampleData;
/**
* Creates a new FSoundSample
*
* @param sampleHandle handle to sample
*/
FSoundSample(long sampleHandle) {
this.sampleHandle = sampleHandle;
FSoundSample(long sampleHandle, Buffer sampleData) {
this.sampleHandle = sampleHandle;
this.sampleData = sampleData;
}
/**
* Releases the reference to any data contained
*/
void release() {
this.sampleData = null;
}
}

View File

@ -31,6 +31,8 @@
*/
package org.lwjgl.fmod3;
import java.nio.Buffer;
/**
* This class is a representation of a Sound stream in FMod.
* <br>
@ -42,12 +44,23 @@ public class FSoundStream {
/** Handle to stream */
long streamHandle;
/** Handle to buffer containing sample data */
Buffer streamData;
/**
* Creates a new FSoundStream
*
* @param streamHandle handle to stream
*/
FSoundStream(long streamHandle) {
FSoundStream(long streamHandle, Buffer streamData) {
this.streamHandle = streamHandle;
this.streamData = streamData;
}
/**
* Releases the reference to any data contained
*/
void release() {
this.streamData = null;
}
}

View File

@ -60,7 +60,7 @@ public class DSPTest {
System.out.println("Usage:\n DSPTest <file>");
// default to phero.mp3
args = new String[] { "res\\phero.mp3"};
args = new String[] { "res" + File.separator + "phero.mp3"};
System.out.println("Using default: " + args[0]);
}

View File

@ -53,7 +53,7 @@ public class MusicPlayer {
System.out.println("Usage:\n MusicPlayer <file>");
// default to Missing_you.mod
args = new String[] { "res\\Missing_you.mod"};
args = new String[] { "res" + File.separator + "Missing_you.mod"};
System.out.println("Using default: " + args[0]);
}

View File

@ -0,0 +1,150 @@
/*
* 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.fmod3;
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import org.lwjgl.fmod3.FMOD;
import org.lwjgl.fmod3.FMODException;
import org.lwjgl.fmod3.FMusic;
import org.lwjgl.fmod3.FMusicModule;
import org.lwjgl.fmod3.FSound;
/**
* <br>
* @author Brian Matzon <brian@matzon.dk>
* @version $Revision: 2286 $
* $Id: MusicPlayer.java 2286 2006-03-23 19:32:21Z matzon $
*/
public class MusicPlayerMemory {
public static void main(String[] args) {
if (args.length < 1) {
System.out.println("Usage:\n MusicPlayer <file>");
// default to Missing_you.mod
args = new String[] { "res" + File.separator + "Missing_you.mod"};
System.out.println("Using default: " + args[0]);
}
File file = new File(args[0]);
if (!file.exists()) {
System.out.println("No such file: " + args[0]);
System.exit(0);
}
try {
FMOD.create();
} catch (FMODException fmode) {
fmode.printStackTrace();
System.exit(0);
}
System.out.println("Initializing FMOD");
if (!FSound.FSOUND_Init(44100, 32, 0)) {
System.out.println("Failed to initialize FMOD");
return;
}
System.out.println("Loading " + args[0]);
// using name (path)
ByteBuffer data = getData(args[0]);
FMusicModule module = FMusic.FMUSIC_LoadSongEx(data, FSound.FSOUND_LOADMEMORY, null);
if (module != null) {
System.out.println("Loaded. Playing module of type: " + FMusic.FMUSIC_GetType(module));
FMusic.FMUSIC_PlaySong(module);
System.out.println("Press enter to stop playing");
try {
System.in.read();
} catch (IOException ioe) {
}
FMusic.FMUSIC_StopSong(module);
System.out.println("Done playing. Cleaning up");
FMusic.FMUSIC_FreeSong(module);
} else {
System.out.println("Unable to play: " + args[0]);
System.out.println("Error: " + FMOD.FMOD_ErrorString(FSound.FSOUND_GetError()));
}
FSound.FSOUND_Close();
FMOD.destroy();
System.exit(0);
}
/**
* Reads the file into a ByteBuffer
*
* @param filename Name of file to load
* @return ByteBuffer containing file data
*/
static protected ByteBuffer getData(String filename) {
ByteBuffer buffer = null;
System.out.println("Attempting to load: " + filename);
try {
BufferedInputStream bis = new BufferedInputStream(StreamPlayerMemory.class.getClassLoader()
.getResourceAsStream(filename));
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int bufferLength = 4096;
byte[] readBuffer = new byte[bufferLength];
int read = -1;
while ((read = bis.read(readBuffer, 0, bufferLength)) != -1) {
baos.write(readBuffer, 0, read);
}
//done reading, close
bis.close();
// place it in a buffer
buffer = ByteBuffer.allocateDirect(baos.size());
buffer.order(ByteOrder.nativeOrder());
buffer.put(baos.toByteArray());
buffer.flip();
System.out.println("loaded " + buffer.remaining() + " bytes");
} catch (Exception ioe) {
ioe.printStackTrace();
}
return buffer;
}
}

View File

@ -52,7 +52,7 @@ public class StreamPlayer {
System.out.println("Usage:\n StreamPlayer <file>");
// default to phero.mp3
args = new String[] { "res\\phero.mp3"};
args = new String[] { "res" + File.separator + "phero.mp3"};
System.out.println("Using default: " + args[0]);
}

View File

@ -33,6 +33,7 @@ package org.lwjgl.test.fmod3;
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
@ -54,7 +55,7 @@ public class StreamPlayerMemory {
System.out.println("Usage:\n StreamPlayerMemory <file>");
// default to phero.mp3
args = new String[] { "phero2.ogg"};
args = new String[] { "res" + File.separator + "phero2.ogg"};
System.out.println("Using default: " + args[0]);
}
@ -73,7 +74,7 @@ public class StreamPlayerMemory {
}
ByteBuffer data = getData(args[0]);
FSoundStream stream = FSound.FSOUND_Stream_Open(data, FSound.FSOUND_LOADMEMORY, 0, data.capacity());
FSoundStream stream = FSound.FSOUND_Stream_Open(data, FSound.FSOUND_LOADMEMORY);
if (stream != null) {
FSound.FSOUND_Stream_Play(0, stream);

View File

@ -101,7 +101,7 @@ public class SyncTest {
System.out.println("Usage:\n SyncTest <file>");
// default to Missing_you.mod
args = new String[] { "res\\Missing_you.mod"};
args = new String[] { "res" + File.separator + "Missing_you.mod"};
System.out.println("Using default: " + args[0]);
}