Initial commit of newLISP.
[newlisp.git] / guiserver / java / SoundSystem.java
blobf3ac216de3fc8fcaf439289460250005ce6b522b
1 //
2 // Dispatcher.java
3 // guiserver
4 //
5 // Created by Lutz Mueller on 8/13/07.
6 //
7 //
8 // Copyright (C) 2007 Lutz Mueller
9 //
10 // This program is free software: you can redistribute it and/or modify
11 // it under the terms of the GNU General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
15 // This program is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU General Public License for more details.
20 // You should have received a copy of the GNU General Public License
21 // along with this program. If not, see <http://www.gnu.org/licenses/>.
24 import java.io.File;
25 import java.io.IOException;
27 import javax.sound.sampled.AudioFormat;
28 import javax.sound.sampled.AudioInputStream;
29 import javax.sound.sampled.AudioSystem;
30 import javax.sound.sampled.DataLine;
31 import javax.sound.sampled.LineUnavailableException;
32 import javax.sound.sampled.SourceDataLine;
34 public class SoundSystem
36 private static final int EXTERNAL_BUFFER_SIZE = 64000;
38 public static void playSound(String filePath)
40 File soundFile = new File(filePath);
41 AudioInputStream audioInputStream = null;
43 try { audioInputStream = AudioSystem.getAudioInputStream(soundFile); }
44 catch (Exception e) {
45 ErrorDialog.show("play-sound","Unsupported audio file");
46 return;
49 AudioFormat audioFormat = audioInputStream.getFormat();
51 SourceDataLine line = null;
53 DataLine.Info info = new DataLine.Info(SourceDataLine.class, audioFormat);
54 try {
55 line = (SourceDataLine) AudioSystem.getLine(info);
56 line.open(audioFormat);
58 catch (LineUnavailableException e) {
59 System.out.println("Sound data line not available");
60 return;
62 catch (Exception e) {
63 ErrorDialog.show("play-sound","Could nor open sound data line");
64 return;
67 line.start();
69 int nBytesRead = 0;
70 byte[] abData = new byte[EXTERNAL_BUFFER_SIZE];
72 while (nBytesRead != -1)
74 try { nBytesRead = audioInputStream.read(abData, 0, abData.length); }
75 catch (IOException e) {
76 ErrorDialog.show("play-sound","Could not read audio file");
78 if (nBytesRead >= 0)
80 int nBytesWritten = line.write(abData, 0, nBytesRead);
84 line.drain();
85 line.close();
86 line = null;
91 // eof