summaryrefslogtreecommitdiffstats
path: root/Master/Reference Architectures and Patterns/hjp5/examples/Listing4901.java
blob: 59c0cdd5b293bc157a7f5517488ddf6e106812ff (plain)
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
/* 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);
  }
}