mirror of
https://github.com/jokerx3/HeatUp
synced 2026-07-27 10:39:44 +02:00
final audio engine commit, now threaded and comfortable to use as enum CLASS
This commit is contained in:
@@ -3,9 +3,7 @@ package de.heatup.audio;
|
|||||||
public class Main {
|
public class Main {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
//new WavePlayer("<filetpath>").start();
|
|
||||||
//new WavePlayer("audiosample.wav").start();
|
|
||||||
|
|
||||||
Sound.SAMPLE.play();
|
Sound.BOMB.play();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,10 +1,58 @@
|
|||||||
package de.heatup.audio;
|
package de.heatup.audio;
|
||||||
|
|
||||||
import java.applet.Applet;
|
import java.io.File;
|
||||||
import java.applet.AudioClip;
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import javax.sound.sampled.AudioInputStream;
|
||||||
|
import javax.sound.sampled.AudioSystem;
|
||||||
|
import javax.sound.sampled.Clip;
|
||||||
|
import javax.sound.sampled.Line;
|
||||||
|
import javax.sound.sampled.LineUnavailableException;
|
||||||
|
import javax.sound.sampled.UnsupportedAudioFileException;
|
||||||
|
|
||||||
|
public enum Sound {
|
||||||
|
BOMB("src/de/heatup/resources/audio/" + "bomb.wav");
|
||||||
|
|
||||||
public class Sound {
|
private Clip clip;
|
||||||
public static final AudioClip SAMPLE = Applet.newAudioClip(Sound.class.getResource("audiosample.wav"));
|
|
||||||
|
Sound(String soundFileName) {
|
||||||
|
|
||||||
|
File f = new File(soundFileName);
|
||||||
|
if(!f.exists()) {
|
||||||
|
System.err.println("File not found : " + f.getAbsolutePath());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
|
||||||
|
Line.Info linfo = new Line.Info(Clip.class);
|
||||||
|
Line line = AudioSystem.getLine(linfo);
|
||||||
|
|
||||||
|
clip = (Clip) line;
|
||||||
|
AudioInputStream inputStream = AudioSystem.getAudioInputStream(f);
|
||||||
|
|
||||||
|
clip.open(inputStream);
|
||||||
|
|
||||||
|
} catch(UnsupportedAudioFileException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
} catch(IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
} catch(LineUnavailableException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
} catch(Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void play() {
|
||||||
|
try {
|
||||||
|
new Thread() {
|
||||||
|
public void run() {
|
||||||
|
clip.start();
|
||||||
|
}
|
||||||
|
}.start();
|
||||||
|
} catch(Throwable e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -1,114 +0,0 @@
|
|||||||
package de.heatup.audio;
|
|
||||||
|
|
||||||
import java.io.BufferedInputStream;
|
|
||||||
import java.io.File;
|
|
||||||
import java.io.IOException;
|
|
||||||
|
|
||||||
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.LineUnavailableException;
|
|
||||||
import javax.sound.sampled.SourceDataLine;
|
|
||||||
import javax.sound.sampled.UnsupportedAudioFileException;
|
|
||||||
|
|
||||||
public class WavePlayer extends Thread {
|
|
||||||
|
|
||||||
private String filename;
|
|
||||||
private Position curPosition;
|
|
||||||
//private File soundFile;
|
|
||||||
|
|
||||||
private final int EXTERNAL_BUFFER_SIZE = 524288; // 128Kb
|
|
||||||
|
|
||||||
enum Position {
|
|
||||||
LEFT, RIGHT, NORMAL
|
|
||||||
};
|
|
||||||
|
|
||||||
public WavePlayer(String wavfile) {
|
|
||||||
filename = wavfile;
|
|
||||||
curPosition = Position.NORMAL;
|
|
||||||
}
|
|
||||||
|
|
||||||
public WavePlayer(String wavfile, Position p) {
|
|
||||||
filename = wavfile;
|
|
||||||
curPosition = p;
|
|
||||||
}
|
|
||||||
|
|
||||||
//public WavePlayer(File soundFile) {
|
|
||||||
//this.soundFile = soundFile;
|
|
||||||
//}
|
|
||||||
|
|
||||||
public void run() {
|
|
||||||
|
|
||||||
/**
|
|
||||||
File soundFile = new File(filename);
|
|
||||||
if (!soundFile.exists()) {
|
|
||||||
System.err.println("Wave file not found: " + filename);
|
|
||||||
return;
|
|
||||||
} */
|
|
||||||
|
|
||||||
AudioInputStream audioInputStream = null;
|
|
||||||
try {
|
|
||||||
//audioInputStream = AudioSystem.getAudioInputStream(soundFile);
|
|
||||||
String path = "/resources/audio/" + filename;
|
|
||||||
File f = new File(path);
|
|
||||||
if(!f.exists()) {
|
|
||||||
System.err.println("File not found: " + path);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
audioInputStream = AudioSystem.getAudioInputStream(new BufferedInputStream(getClass().getResourceAsStream(path)));
|
|
||||||
} catch (UnsupportedAudioFileException e1) {
|
|
||||||
e1.printStackTrace();
|
|
||||||
return;
|
|
||||||
} catch (IOException e1) {
|
|
||||||
e1.printStackTrace();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
AudioFormat format = audioInputStream.getFormat();
|
|
||||||
SourceDataLine auline = null;
|
|
||||||
DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);
|
|
||||||
|
|
||||||
try {
|
|
||||||
auline = (SourceDataLine) AudioSystem.getLine(info);
|
|
||||||
auline.open(format);
|
|
||||||
} catch (LineUnavailableException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
return;
|
|
||||||
} catch (Exception e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (auline.isControlSupported(FloatControl.Type.PAN)) {
|
|
||||||
FloatControl pan = (FloatControl) auline
|
|
||||||
.getControl(FloatControl.Type.PAN);
|
|
||||||
if (curPosition == Position.RIGHT)
|
|
||||||
pan.setValue(1.0f);
|
|
||||||
else if (curPosition == Position.LEFT)
|
|
||||||
pan.setValue(-1.0f);
|
|
||||||
}
|
|
||||||
|
|
||||||
auline.start();
|
|
||||||
int nBytesRead = 0;
|
|
||||||
byte[] abData = new byte[EXTERNAL_BUFFER_SIZE];
|
|
||||||
|
|
||||||
try {
|
|
||||||
while (nBytesRead != -1) {
|
|
||||||
nBytesRead = audioInputStream.read(abData, 0, abData.length);
|
|
||||||
if (nBytesRead >= 0)
|
|
||||||
auline.write(abData, 0, nBytesRead);
|
|
||||||
}
|
|
||||||
} catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
return;
|
|
||||||
} finally {
|
|
||||||
auline.drain();
|
|
||||||
auline.close();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Binary file not shown.
Binary file not shown.
Reference in New Issue
Block a user