diff options
| author | Sven Eisenhauer <sven@sven-eisenhauer.net> | 2023-11-10 15:11:48 +0100 |
|---|---|---|
| committer | Sven Eisenhauer <sven@sven-eisenhauer.net> | 2023-11-10 15:11:48 +0100 |
| commit | 33613a85afc4b1481367fbe92a17ee59c240250b (patch) | |
| tree | 670b842326116b376b505ec2263878912fca97e2 /Master/Reference Architectures and Patterns/hjp5/examples/Listing4901.java | |
| download | Studium-33613a85afc4b1481367fbe92a17ee59c240250b.tar.gz Studium-33613a85afc4b1481367fbe92a17ee59c240250b.tar.bz2 | |
Diffstat (limited to 'Master/Reference Architectures and Patterns/hjp5/examples/Listing4901.java')
| -rw-r--r-- | Master/Reference Architectures and Patterns/hjp5/examples/Listing4901.java | 80 |
1 files changed, 80 insertions, 0 deletions
diff --git a/Master/Reference Architectures and Patterns/hjp5/examples/Listing4901.java b/Master/Reference Architectures and Patterns/hjp5/examples/Listing4901.java new file mode 100644 index 0000000..59c0cdd --- /dev/null +++ b/Master/Reference Architectures and Patterns/hjp5/examples/Listing4901.java @@ -0,0 +1,80 @@ +/* Listing4901.java */
+
+import java.io.*;
+import javax.sound.sampled.*;
+
+public class Listing4901
+{
+ private static void playSampleFile(String name, float pan, float gain)
+ throws Exception
+ {
+ //AudioInputStream öffnen
+ AudioInputStream ais = AudioSystem.getAudioInputStream(
+ new File(name)
+ );
+ AudioFormat format = ais.getFormat();
+ //ALAW/ULAW samples in PCM konvertieren
+ if ((format.getEncoding() == AudioFormat.Encoding.ULAW) ||
+ (format.getEncoding() == AudioFormat.Encoding.ALAW))
+ {
+ AudioFormat tmp = new AudioFormat(
+ AudioFormat.Encoding.PCM_SIGNED,
+ format.getSampleRate(),
+ format.getSampleSizeInBits() * 2,
+ format.getChannels(),
+ format.getFrameSize() * 2,
+ format.getFrameRate(),
+ true
+ );
+ ais = AudioSystem.getAudioInputStream(tmp, ais);
+ format = tmp;
+ }
+ //Clip erzeugen und öffnen
+ DataLine.Info info = new DataLine.Info(
+ Clip.class,
+ format,
+ ((int) ais.getFrameLength() * format.getFrameSize())
+ );
+ Clip clip = (Clip)AudioSystem.getLine(info);
+ clip.open(ais);
+ //PAN einstellen
+ FloatControl panControl = (FloatControl)clip.getControl(
+ FloatControl.Type.PAN
+ );
+ panControl.setValue(pan);
+ //MASTER_GAIN einstellen
+ FloatControl gainControl = (FloatControl)clip.getControl(
+ FloatControl.Type.MASTER_GAIN
+ );
+ gainControl.setValue(gain);
+ //Clip abspielen
+ clip.start();
+ while (true) {
+ try {
+ Thread.sleep(100);
+ } catch (Exception e) {
+ //nothing
+ }
+ if (!clip.isRunning()) {
+ break;
+ }
+ }
+ clip.stop();
+ clip.close();
+ }
+
+ public static void main(String[] args)
+ {
+ try {
+ playSampleFile(
+ args[0],
+ Float.parseFloat(args[1]),
+ Float.parseFloat(args[2])
+ );
+ } catch (Exception e) {
+ e.printStackTrace();
+ System.exit(1);
+ }
+ System.exit(0);
+ }
+}
\ No newline at end of file |
