Update gitignore

This commit is contained in:
Vulpovile
2025-04-09 21:55:29 -07:00
parent 0648a0178c
commit 4786cb7bd7
12 changed files with 769 additions and 0 deletions

View File

@@ -0,0 +1,286 @@
package com.flaremicro.audio;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Random;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.FloatControl;
import javax.sound.sampled.SourceDataLine;
import javazoom.jl.decoder.JavaLayerException;
import javazoom.jl.player.Player;
import com.flaremicro.util.Util;
public class AgnosticAudioSystem implements GameAudioSystem {
private Collection<AudioFinishedListener> listeners = new ArrayList<AudioFinishedListener>();
private HashMap<String, AudioSystemThread> threads = new HashMap<String, AudioSystemThread>();
private Random random = new Random();
public void init() {
}
public void destroy() {
}
public void addAudioFinishedListener(AudioFinishedListener listener) {
this.listeners.add(listener);
}
public void removeAudioFinishedListener(AudioFinishedListener listener) {
this.listeners.remove(listener);
}
public void removeAudioFinishedListeners() {
this.listeners.clear();
}
public String play(URL res) {
if (res.getPath().contains("."))
{
String key = String.valueOf(random.nextLong());
String ext = res.getPath().substring(res.getPath().lastIndexOf(".") + 1).toLowerCase();
AudioSystemThread audioSystemThread = null;
if (ext.equalsIgnoreCase("mp3"))
audioSystemThread = new AgnosticMp3AudioSystemThread(res, key, this, false);
if (ext.equalsIgnoreCase("wav"))
audioSystemThread = new AgnosticWavAudioSystemThread(res, key, this, false);
if (audioSystemThread != null)
{
synchronized (threads)
{
threads.put(key, audioSystemThread);
}
new Thread(audioSystemThread).start();
return key;
}
}
return null;
}
public void playbg(URL res) {
stopbg();
if (res.getPath().contains("."))
{
String ext = res.getPath().substring(res.getPath().lastIndexOf(".") + 1).toLowerCase();
AudioSystemThread audioSystemThread = null;
if (ext.equalsIgnoreCase("mp3"))
audioSystemThread = new AgnosticMp3AudioSystemThread(res, "bg", this, true);
if (ext.equalsIgnoreCase("wav"))
audioSystemThread = new AgnosticWavAudioSystemThread(res, "bg", this, true);
if (audioSystemThread != null)
{
synchronized (threads)
{
threads.put("bg", audioSystemThread);
}
new Thread(audioSystemThread).start();
}
}
}
public void stopbg() {
if (threads.containsKey("bg"))
threads.get("bg").stopMusic();
stopped("bg", threads.get("bg"));
}
public void stopped(String threadName, AudioSystemThread audioSystemThread) {
synchronized (threads)
{
if (audioSystemThread != null && audioSystemThread == threads.get(threadName))
{
threads.remove(threadName);
}
}
for (AudioFinishedListener listener : listeners)
{
listener.audioFinished(threadName, audioSystemThread.resource);
}
}
public void setVolume(float percent, String key) {
AudioSystemThread ast = threads.get(key);
if (ast != null)
{
ast.setVolume(percent);
}
}
public void stop(String currentKey) {
AudioSystemThread ast = threads.get(currentKey);
if (ast != null)
{
ast.stopMusic();
}
}
}
abstract class AudioSystemThread implements Runnable {
protected final boolean looping;
protected final AgnosticAudioSystem system;
protected final URL resource;
protected final String threadName;
public AudioSystemThread(URL resource, String threadName, AgnosticAudioSystem system, boolean looping) {
this.resource = resource;
this.looping = looping;
this.threadName = threadName;
this.system = system;
}
public abstract void setVolume(float percent);
public abstract void stopMusic();
public final void onStop() {
this.system.stopped(threadName, this);
}
}
class AgnosticMp3AudioSystemThread extends AudioSystemThread {
private boolean running = true;
private Player player = null;
private static final int FRAME_BUFFER = 2;
private float volume = 1F;
public AgnosticMp3AudioSystemThread(URL resource, String threadName, AgnosticAudioSystem system, boolean looping) {
super(resource, threadName, system, looping);
}
public void run() {
do
{
try
{
player = new Player(new BufferedInputStream(resource.openStream()));
player.setVolume(volume);
while (running && player.play(FRAME_BUFFER));
}
catch (JavaLayerException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
}
while (running && looping);
super.onStop();
}
public void stopMusic() {
this.running = false;
if (player != null)
player.close();
}
@Override
public void setVolume(float percent) {
volume = percent;
if (player != null)
{
player.setVolume(volume);
}
}
}
class AgnosticWavAudioSystemThread extends AudioSystemThread {
private boolean running = true;
private SourceDataLine sourceLine = null;
private float volume = 1.0F;
public AgnosticWavAudioSystemThread(URL resource, String threadName, AgnosticAudioSystem system, boolean looping) {
super(resource, threadName, system, looping);
}
public void run() {
AudioInputStream audioStream = null;
do
{
try
{
audioStream = AudioSystem.getAudioInputStream(resource);
AudioFormat audioFormat = audioStream.getFormat();
DataLine.Info info = new DataLine.Info(SourceDataLine.class, audioFormat);
sourceLine = (SourceDataLine) AudioSystem.getLine(info);
sourceLine.open();
sourceLine.start();
FloatControl volume = (FloatControl) sourceLine.getControl(FloatControl.Type.MASTER_GAIN);
volume.setValue(this.volume);
int nBytesRead = 0;
byte[] abData = new byte[4096];
while (nBytesRead != -1 && running)
{
try
{
nBytesRead = audioStream.read(abData, 0, abData.length);
}
catch (IOException e)
{
e.printStackTrace();
}
if (nBytesRead >= 0)
{
@SuppressWarnings("unused")
int nBytesWritten = sourceLine.write(abData, 0, nBytesRead);
}
}
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
Util.cleanClose(audioStream);
if (sourceLine != null)
{
sourceLine.drain();
sourceLine.close();
}
}
}
while (running && looping);
onStop();
}
public void stopMusic() {
this.running = false;
if (sourceLine != null)
{
sourceLine.drain();
sourceLine.close();
}
}
@Override
public void setVolume(float percent) {
if (sourceLine != null)
{
this.volume = percent;
FloatControl volume = (FloatControl) sourceLine.getControl(FloatControl.Type.MASTER_GAIN);
volume.setValue(percent);
}
}
}

View File

@@ -0,0 +1,7 @@
package com.flaremicro.audio;
import java.net.URL;
public interface AudioFinishedListener {
public void audioFinished(String key, URL resource);
}

View File

@@ -0,0 +1,83 @@
package com.flaremicro.audio;
import java.net.URL;
import java.util.Random;
public class AudioPlaylist implements AudioFinishedListener{
private final AgnosticAudioSystem aas;
public AudioPlaylist(AgnosticAudioSystem aas)
{
this.aas = aas;
}
String currentKey = null;
int currentPlaylistIndex = 0;
URL[] playlist = null;
float currVol = 1F;
private boolean stopped = true;
public void setPlaylist(URL[] url)
{
this.playlist = url;
this.currentPlaylistIndex = 0;
}
public void shuffle(){
Random rand = new Random();
for (int i = 0; i < playlist.length; i++) {
int randomIndexToSwap = rand.nextInt(playlist.length);
URL temp = playlist[randomIndexToSwap];
playlist[randomIndexToSwap] = playlist[i];
playlist[i] = temp;
}
}
public void start()
{
stopped = false;
currentPlaylistIndex = 0;
aas.addAudioFinishedListener(this);
if(currentKey == null && playlist != null && playlist.length > 0)
{
currentKey = aas.play(playlist[0]);
aas.setVolume(currVol, currentKey);
}
}
public void next()
{
if(playlist != null && playlist.length > 0)
{
currentPlaylistIndex = (currentPlaylistIndex + 1 ) % playlist.length;
currentKey = aas.play(playlist[currentPlaylistIndex]);
aas.setVolume(currVol, currentKey);
}
}
public void audioFinished(String key, URL resource) {
if(!stopped && key == currentKey)
{
next();
}
}
public void setVolume(float volume) {
currVol = volume;
aas.setVolume(currVol, currentKey);
}
public void stop() {
stopped = true;
aas.stop(currentKey);
}
public float getVolume() {
return currVol;
}
}

View File

@@ -0,0 +1,16 @@
package com.flaremicro.audio;
import java.net.URL;
public interface GameAudioSystem {
public void init();
public void destroy();
public void playbg(URL res);
public String play(URL res);
public void stopbg();
}