Fixes loading mp3 multiclip since AudioInputStream.availble didn't work.

This commit is contained in:
fd 2015-02-27 19:22:29 -05:00
parent 8a8024aadb
commit 8512f7c3c5
3 changed files with 36 additions and 21 deletions

View File

@ -20,8 +20,6 @@ package itdelatrisu.opsu.audio;
import java.util.HashMap; import java.util.HashMap;
import javax.sound.sampled.Clip;
/** /**
* Hit sounds. * Hit sounds.
*/ */

View File

@ -1,7 +1,5 @@
package itdelatrisu.opsu.audio; package itdelatrisu.opsu.audio;
import itdelatrisu.opsu.ErrorHandler;
import java.io.IOException; import java.io.IOException;
import java.util.LinkedList; import java.util.LinkedList;
@ -21,28 +19,49 @@ public class MultiClip {
AudioFormat format; AudioFormat format;
/** The data for this audio sample */ /** The data for this audio sample */
byte[] buffer; byte[] audioData;
/** The name given to this clip */ /** The name given to this clip */
String name; String name;
/** Constructor /** Size of a single buffer */
* @param name final int BUFFER_SIZE = 0x1000;
* @throws LineUnavailableException */
/** Constructor */
public MultiClip(String name, AudioInputStream audioIn) throws IOException, LineUnavailableException { public MultiClip(String name, AudioInputStream audioIn) throws IOException, LineUnavailableException {
this.name = name; this.name = name;
if(audioIn != null){ if(audioIn != null){
buffer = new byte[audioIn.available()];
int readed= 0;
while(readed < buffer.length) {
int read = audioIn.read(buffer, readed, buffer.length-readed);
if(read < 0 )
break;
readed += read;
}
format = audioIn.getFormat(); format = audioIn.getFormat();
} else {
System.out.println("Null multiclip"); LinkedList<byte[]> allBufs = new LinkedList<byte[]>();
int readed = 0;
boolean hasData = true;
while (hasData) {
readed = 0;
byte[] tbuf = new byte[BUFFER_SIZE];
while (readed < tbuf.length) {
int read = audioIn.read(tbuf, readed, tbuf.length - readed);
if (read < 0) {
hasData = false;
break;
}
readed += read;
}
allBufs.add(tbuf);
}
audioData = new byte[(allBufs.size() - 1) * BUFFER_SIZE + readed];
int cnt = 0;
for (byte[] tbuf : allBufs) {
int size = BUFFER_SIZE;
if (cnt == allBufs.size() - 1) {
size = readed;
}
System.arraycopy(tbuf, 0, audioData, BUFFER_SIZE * cnt, size);
cnt++;
}
} }
getClip(); getClip();
} }
@ -88,7 +107,7 @@ public class MultiClip {
} }
Clip t = AudioSystem.getClip(); Clip t = AudioSystem.getClip();
if (format != null) if (format != null)
t.open(format, buffer, 0, buffer.length); t.open(format, audioData, 0, audioData.length);
clips.add(t); clips.add(t);
return t; return t;
} }

View File

@ -18,8 +18,6 @@
package itdelatrisu.opsu.audio; package itdelatrisu.opsu.audio;
import javax.sound.sampled.Clip;
/** /**
* Sound effects. * Sound effects.
*/ */