1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
|
package renoria.sound;
import java.io.BufferedInputStream; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.Deque; import java.util.Iterator; import java.util.LinkedList; import java.util.List; import java.util.concurrent.atomic.AtomicInteger; import javax.sound.sampled.AudioFormat; import javax.sound.sampled.AudioInputStream; import javax.sound.sampled.AudioSystem; import javax.sound.sampled.BooleanControl; import javax.sound.sampled.DataLine; import javax.sound.sampled.FloatControl; import javax.sound.sampled.LineUnavailableException; import javax.sound.sampled.Mixer; import javax.sound.sampled.SourceDataLine; import javax.sound.sampled.UnsupportedAudioFileException; import net.idgames.archive.idx.IDXException;
public class AsyncSoundPlayer { private Deque<SoundInfo> queuedSounds = new LinkedList(); private final Object poolSync = new Object(); private final Object queueSync = new Object(); private boolean shutDown = false; private List<Player> playerPool; private int poolsize; private AudioFormat playBackFormat; private AtomicInteger playingSounds = new AtomicInteger(0); private int maxPlayingSoundsPerPool = 32; private Mixer mixer; private boolean soundAvailable = false; private float masterVolume = 1.0f;
public AsyncSoundPlayer() { this(1, null); }
public AsyncSoundPlayer(int poolsize, Mixer useMixer) { Mixer.Info[] infos = AudioSystem.getMixerInfo(); if (infos == null || infos.length < 1) { throw new SoundUnavailableException(); } if (poolsize <= 0) { throw new IllegalArgumentException(); } if (useMixer != null) { this.mixer = useMixer; } else { this.mixer = SoundUtil.getDefaultMixer(SoundUtil.PLAYBACK_MIXER); } this.poolsize = poolsize; soundAvailable = true; }
public float getMasterVolume() { return masterVolume; }
public void setMasterVolume(float masterVolume) { this.masterVolume = masterVolume; }
public void start() { if (!soundAvailable) { throw new SoundUnavailableException(); } if (playerPool == null) { playerPool = new LinkedList(); for (int i = 0; i < poolsize; i++) { Player p = new Player(); p.start(); playerPool.add(p); } try { mixer.open(); } catch (LineUnavailableException ex) { throw new IDXException(ex); } } else { throw new IllegalStateException(); } }
public void stop() { stop(0); }
public void stop(int maxwait) { if (playerPool != null) { try { synchronized (poolSync) { Iterator<Player> pItr = playerPool.iterator(); while (pItr.hasNext()) { Player p = pItr.next(); p.join(maxwait); } } } catch (InterruptedException ex) {} playerPool = null; mixer.close(); } else { throw new IllegalStateException(); } }
public void setPoolSize(int ps) throws IllegalStateException { if (playerPool != null) { throw new IllegalStateException(); } poolsize = ps; }
public int getPoolsize() { return poolsize; } public AudioInputStream getAudioInputStream(InputStream is) { try { if (!is.markSupported()) { is = new BufferedInputStream(is); }
AudioInputStream source = AudioSystem.getAudioInputStream(is);
return playBackFormat != null ? convertStream(source) : source; } catch (UnsupportedAudioFileException ex) { throw new IDXException(ex); } catch (IOException ex) { throw new IDXException(ex); } catch (IllegalArgumentException ex) { throw new IDXException(ex); } }
public void stopAllSounds(boolean instant) { Iterator<Player> pItr = playerPool.iterator(); while (pItr.hasNext()) { Player p = pItr.next(); p.stopAllSounds(instant); } playingSounds.set(0); }
static int getMaxDefaultMixerLines(AudioFormat format) { try { Mixer mixer = AudioSystem.getMixer(null); DataLine.Info info = new DataLine.Info(SourceDataLine.class, format); return mixer.getMaxLines(info); } catch (Exception e) { renoria.Core.handleException(e); return -1; } }
private AudioInputStream convertStream(AudioInputStream stream) { if (playBackFormat == null) { return stream; } try { return AudioSystem.getAudioInputStream(playBackFormat, stream); } catch (IllegalArgumentException e) { renoria.Core.handleException(e); return stream; } }
public SoundInfo playSound(InputStream input) { return playSound(input, true); }
|
NOTE: Split into 3 posts due to character limit
Have fun, and report to me any problems you may encounter.
PS: Some exception handing and a few bits of other code are reliant on some of my other code. That code can be edited if you wish