i couldnt find how to play dynamically synthesized sampled sound (creating the sound in a memory buffer) in JME… so this code subclasses AudioInputStream into DynamicAudioStream.
is there a better way to do this? if not, maybe something similar can be added to JME.
public class TestDynamicJMESound extends SimpleGame {
private AudioSystem audio;
class DynamicAudioStream extends AudioInputStream {
private int samplesPerSecond = 44100;
public DynamicAudioStream() throws IOException {
//supply a dummy URL for the superclass
super(DynamicAudioStream.class.getClassLoader().getResource("."), 10.0f);
}
@Override public int available() {
return samplesPerSecond;
}
@Override public int getBitRate() {
return getChannelCount() * getDepth()/8 * samplesPerSecond;
}
@Override public int getChannelCount() {
return 1;
}
@Override public int getDepth() {
return 16;
}
@Override public AudioInputStream makeNew() throws IOException {
return this;
}
/** synthesizes the next audio buffer */
@Override public int read(ByteBuffer b, int offset, int length) throws IOException {
//System.out.println("read: " + b+ " " + offset + " " + length);
for (int i = offset; i < length; i++) {
b.put(i, (byte)(Math.random() * 200));
}
return length;
}
}
class DynamicAudioTrack extends AudioTrack {
private StreamedAudioPlayer stream;
public DynamicAudioTrack() {
//supply dummy URL for superclass
super(DynamicAudioStream.class.getClassLoader().getResource("."), true);
try {
this.stream = new OpenALStreamedAudioPlayer(new DynamicAudioStream(), this);
stream.init();
} catch (IOException e) {
e.printStackTrace();
return;
}
setPlayer(stream);
setEnabled(true);
setType(TrackType.HEADSPACE);
setRelative(false);
setTargetVolume(0.9F);
}
}
public static void main(String[] args) {
TestDynamicJMESound app = new TestDynamicJMESound();
app.setDialogBehaviour(NEVER_SHOW_PROPS_DIALOG);
app.start();
}
@Override() protected void simpleInitGame() {
audio = AudioSystem.getSystem();
audio.getEar().trackOrientation(cam);
audio.getEar().trackPosition(cam);
AudioTrack music = getDynamic();
audio.getEnvironmentalPool().addTrack(music);
}
private AudioTrack getDynamic() {
return new DynamicAudioTrack();
}
@Override() protected void simpleUpdate() {
audio.update();
}
@Override() protected void cleanup() {
audio.cleanup();
}
}