Can't seem to get it working.
Code:
java.lang.ArrayIndexOutOfBoundsException
at java.lang.System.arraycopy(Native Method)
at src.sounds.emu.SpcEmu.startTrack(SpcEmu.java:147)
at src.sounds.EmuPlayer.startTrack(VGMPlayer.java:44)
at src.sounds.SoundManager.play(SoundManager.java:42)
at src.Client.<init>(Client.java:30)
at src.Client.main(Client.java:15)
sounds.zip is zip archive holding 62 spc files. I load all of them to a byte array..
However I get an error when I call the play method.
The only file I have modified is VGMPlayer
Code:
package src.sounds;
import java.io.*;
import java.util.zip.*;
import javax.sound.sampled.*;
import src.sounds.emu.*;
public class SoundManager {
private static final SoundManager instance = new SoundManager();
public static final SoundManager getInstance() {
return instance;
}
private SoundManager() {
try {
player = new VGMPlayer(44100);
ZipInputStream in = new ZipInputStream(new FileInputStream("data/sounds.zip"));
ZipEntry clip = null;
int i = 0;
byte[] buf;
while ((clip = in.getNextEntry()) != null) {
buf = new byte[(int) clip.getCompressedSize()];
in.read(buf);
sound[i++] = buf;
}
in.close();
} catch (Exception e) {
System.exit(1);
}
}
private byte[][] sound = new byte[62][];
private VGMPlayer player;
public void play(int id, int time) {
try {
player.loadFile(sound[id]);
player.startTrack(0, time);
} catch (Exception e) {
e.printStackTrace();
}
}
}
Code:
package src.sounds;
import javax.sound.sampled.*;
import java.io.*;
import src.sounds.emu.*;
/* Copyright (C) 2007-2008 Shay Green. This module 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
module 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 module; if not, write to the Free Software Foundation,
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */
class EmuPlayer implements Runnable {
public int getTrackCount() {
return emu.trackCount();
}
public void startTrack(int track, int time) throws Exception {
pause();
if (line != null)
line.flush();
emu.startTrack(track);
emu.setFade(time, 6);
play();
}
public int getCurrentTrack() {
return emu.currentTrack();
}
public int getCurrentTime() {
return (emu == null ? 0 : emu.currentTime());
}
public void setVolume(double v) {
volume_ = v;
if (line != null) {
FloatControl mg = (FloatControl) line.getControl(FloatControl.Type.MASTER_GAIN);
if (mg != null)
mg.setValue((float) (Math.log( v ) / Math.log( 10.0 ) * 20.0));
}
}
public double getVolume() {
return volume_;
}
public void pause() throws Exception {
if (thread != null) {
playing_ = false;
thread.join();
thread = null;
}
}
public boolean isPlaying() {
return playing_;
}
public void play() throws Exception {
if (line == null) {
line = (SourceDataLine) AudioSystem.getLine(lineInfo);
line.open(audioFormat);
setVolume(volume_);
}
thread = new Thread(this);
playing_ = true;
thread.start();
}
public void stop() throws Exception {
pause();
if (line != null) {
line.close();
line = null;
}
}
protected void idle() { }
private int sampleRate = 0;
AudioFormat audioFormat;
DataLine.Info lineInfo;
MusicEmu emu = new SpcEmu();
Thread thread;
volatile boolean playing_;
SourceDataLine line;
double volume_ = 1.0;
public void run() {
line.start();
byte [] buf = new byte [66092];
while (playing_ && !emu.trackEnded()) {
int count = emu.play(buf, buf.length / 2);
line.write(buf, 0, count * 2);
idle();
}
playing_ = false;
line.stop();
}
}
class VGMPlayer extends EmuPlayer {
int sampleRate;
public VGMPlayer(int sampleRate) {
this.sampleRate = sampleRate;
}
public void loadFile(byte[] data) throws Exception {
stop();
int actualSampleRate = emu.setSampleRate( sampleRate );
emu.loadFile(data);
}
void closeFile() throws Exception {
stop();
}
}