Global max clips
reuse clips close extraclips in ranking
This commit is contained in:
parent
8512f7c3c5
commit
955a184d2c
|
@ -1,11 +1,11 @@
|
||||||
package itdelatrisu.opsu.audio;
|
package itdelatrisu.opsu.audio;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.util.Iterator;
|
||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
|
|
||||||
import javax.sound.sampled.AudioFormat;
|
import javax.sound.sampled.AudioFormat;
|
||||||
import javax.sound.sampled.AudioInputStream;
|
import javax.sound.sampled.AudioInputStream;
|
||||||
import javax.sound.sampled.AudioSystem;
|
|
||||||
import javax.sound.sampled.Clip;
|
import javax.sound.sampled.Clip;
|
||||||
import javax.sound.sampled.FloatControl;
|
import javax.sound.sampled.FloatControl;
|
||||||
import javax.sound.sampled.LineUnavailableException;
|
import javax.sound.sampled.LineUnavailableException;
|
||||||
|
@ -27,6 +27,8 @@ public class MultiClip {
|
||||||
/** Size of a single buffer */
|
/** Size of a single buffer */
|
||||||
final int BUFFER_SIZE = 0x1000;
|
final int BUFFER_SIZE = 0x1000;
|
||||||
|
|
||||||
|
static LinkedList<MultiClip> allMultiClips = new LinkedList<MultiClip>();
|
||||||
|
|
||||||
/** Constructor */
|
/** 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;
|
||||||
|
@ -64,6 +66,7 @@ public class MultiClip {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
getClip();
|
getClip();
|
||||||
|
allMultiClips.add(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -83,6 +86,9 @@ public class MultiClip {
|
||||||
public void start(float volume) throws LineUnavailableException, IOException {
|
public void start(float volume) throws LineUnavailableException, IOException {
|
||||||
Clip clip = getClip();
|
Clip clip = getClip();
|
||||||
|
|
||||||
|
if(clip == null)
|
||||||
|
return;
|
||||||
|
|
||||||
// PulseAudio does not support Master Gain
|
// PulseAudio does not support Master Gain
|
||||||
if (clip.isControlSupported(FloatControl.Type.MASTER_GAIN)) {
|
if (clip.isControlSupported(FloatControl.Type.MASTER_GAIN)) {
|
||||||
// set volume
|
// set volume
|
||||||
|
@ -96,19 +102,55 @@ public class MultiClip {
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* Returns a Clip that is not playing from the list
|
* Returns a Clip that is not playing from the list
|
||||||
* if one is not available a new one is created
|
* if one is not available a new one is created if able
|
||||||
* @return the Clip
|
* @return the Clip
|
||||||
*/
|
*/
|
||||||
private Clip getClip() throws LineUnavailableException, IOException{
|
private Clip getClip() throws LineUnavailableException, IOException{
|
||||||
for(Clip c : clips){
|
for(Iterator<Clip> ita = clips.listIterator(); ita.hasNext(); ) {
|
||||||
|
Clip c = ita.next();
|
||||||
if(!c.isRunning()){
|
if(!c.isRunning()){
|
||||||
|
ita.remove();
|
||||||
|
clips.add(c);
|
||||||
return c;
|
return c;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Clip t = AudioSystem.getClip();
|
|
||||||
if (format != null)
|
Clip t = SoundController.newClip();
|
||||||
t.open(format, audioData, 0, audioData.length);
|
if(t == null){
|
||||||
clips.add(t);
|
if(clips.isEmpty()){
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
t = clips.removeFirst();
|
||||||
|
t.stop();
|
||||||
|
clips.add(t);
|
||||||
|
} else {
|
||||||
|
if (format != null)
|
||||||
|
t.open(format, audioData, 0, audioData.length);
|
||||||
|
clips.add(t);
|
||||||
|
}
|
||||||
return t;
|
return t;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Destroys all but one clip
|
||||||
|
*/
|
||||||
|
protected void destroyAllButOne(){
|
||||||
|
for(Iterator<Clip> ita = clips.listIterator(); ita.hasNext(); ) {
|
||||||
|
Clip c = ita.next();
|
||||||
|
if(clips.size()>1){
|
||||||
|
ita.remove();
|
||||||
|
SoundController.destroyClip(c);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Destroys all but one clip for all MultiClips
|
||||||
|
*/
|
||||||
|
protected static void destroyExtraClips() {
|
||||||
|
for(MultiClip mc : MultiClip.allMultiClips){
|
||||||
|
mc.destroyAllButOne();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -27,6 +27,9 @@ import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.LinkedList;
|
||||||
|
import java.util.ListIterator;
|
||||||
|
|
||||||
import javax.sound.sampled.AudioFormat;
|
import javax.sound.sampled.AudioFormat;
|
||||||
import javax.sound.sampled.AudioInputStream;
|
import javax.sound.sampled.AudioInputStream;
|
||||||
|
@ -298,4 +301,53 @@ public class SoundController {
|
||||||
|
|
||||||
return currentFileIndex * 100 / (SoundEffect.SIZE + (HitSound.SIZE * SampleSet.SIZE));
|
return currentFileIndex * 100 / (SoundEffect.SIZE + (HitSound.SIZE * SampleSet.SIZE));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/** Max number of clips that can be created */
|
||||||
|
static int MAX_CLIP = 100;
|
||||||
|
|
||||||
|
/** List of clips that has been created */
|
||||||
|
static HashSet<Clip> clipList = new HashSet<Clip>();
|
||||||
|
|
||||||
|
/** List of clips to be closed */
|
||||||
|
static LinkedList<Clip> clipsToClose = new LinkedList<Clip>();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a new clip if it still below the max number of clips
|
||||||
|
*/
|
||||||
|
protected static Clip newClip() throws LineUnavailableException{
|
||||||
|
if(clipList.size() < MAX_CLIP) {
|
||||||
|
Clip c = AudioSystem.getClip();
|
||||||
|
clipList.add(c);
|
||||||
|
return c;
|
||||||
|
} else {
|
||||||
|
System.out.println("Can't newClip");
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds a clip to be closed
|
||||||
|
*/
|
||||||
|
protected static void destroyClip(Clip c) {
|
||||||
|
if(clipList.remove(c)){
|
||||||
|
clipsToClose.add(c);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Destroys all extra Clips
|
||||||
|
*/
|
||||||
|
public static void destroyExtraClips() {
|
||||||
|
MultiClip.destroyExtraClips();
|
||||||
|
new Thread(){
|
||||||
|
public void run(){
|
||||||
|
for(ListIterator<Clip> ita = clipsToClose.listIterator(); ita.hasNext(); ){
|
||||||
|
Clip c = ita.next();
|
||||||
|
c.close();
|
||||||
|
ita.remove();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}.start();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -185,6 +185,7 @@ public class GameRanking extends BasicGameState {
|
||||||
retryButton.resetHover();
|
retryButton.resetHover();
|
||||||
exitButton.resetHover();
|
exitButton.resetHover();
|
||||||
}
|
}
|
||||||
|
SoundController.destroyExtraClips();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
Loading…
Reference in New Issue
Block a user