annotate game/SoundManager.java @ 61:ceaf645ed930

Add (non-working) audio code.
author Matti Hamalainen <ccr@tnsp.org>
date Thu, 24 Feb 2011 03:58:33 +0200
parents
children 1c7a97d80120
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
61
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1 /*
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2 * Ristipolku Game Engine
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3 * (C) Copyright 2011 Matti 'ccr' Hämäläinen <ccr@tnsp.org>
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
4 */
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
5 package game;
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
6
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
7 import java.util.*;
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
8 import java.io.*;
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
9 import game.*;
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
10 import javax.sound.sampled.*;
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
11
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
12
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
13 public class SoundManager extends ThreadGroup
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
14 {
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
15 private boolean alive;
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
16 private LinkedList queue;
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
17 private int id;
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
18 private static int poolID;
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
19
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
20 private AudioFormat playbackFormat;
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
21 private ThreadLocal localLine;
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
22 private ThreadLocal localBuffer;
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
23 private Object pausedLock;
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
24 private boolean paused;
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
25
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
26
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
27 public SoundManager(AudioFormat format)
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
28 {
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
29 this(format, getMaxSimultaneousSounds(format));
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
30 }
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
31
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
32
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
33 public SoundManager(AudioFormat format, int maxSounds)
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
34 {
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
35 super("SoundManagerPool-" + (poolID++));
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
36
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
37 int numThreads = Math.min(maxSounds, getMaxSimultaneousSounds(playbackFormat));
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
38
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
39 System.out.print("SMGR.SoundManager() initializing with " + numThreads +" max sounds\n");
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
40
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
41 setDaemon(true);
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
42 alive = true;
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
43
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
44 queue = new LinkedList();
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
45 for (int i = 0; i < numThreads; i++)
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
46 new PooledThread().start();
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
47
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
48 playbackFormat = format;
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
49 localLine = new ThreadLocal();
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
50 localBuffer = new ThreadLocal();
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
51 pausedLock = new Object();
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
52
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
53 synchronized (this)
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
54 {
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
55 notifyAll();
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
56 }
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
57 }
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
58
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
59
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
60 public static int getMaxSimultaneousSounds(AudioFormat playbackFormat)
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
61 {
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
62 DataLine.Info lineInfo = new DataLine.Info(SourceDataLine.class, playbackFormat);
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
63
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
64 Mixer.Info[] info = AudioSystem.getMixerInfo();
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
65 System.out.print("getMaxSimultaneousSounds() mixer information:\n");
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
66 Mixer.Info select = null;
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
67 for (Mixer.Info i : info)
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
68 {
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
69 System.out.print("#"+i.getName()+"\n");
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
70 if (i.getName().equals("Java Sound Audio Engine"))
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
71 select = i;
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
72 }
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
73
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
74 Mixer mixer = AudioSystem.getMixer(select);
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
75
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
76 Mixer.Info i = mixer.getMixerInfo();
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
77 System.out.print("selected: "+i.getName()+"\n");
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
78
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
79 int maxLines = mixer.getMaxLines(lineInfo);
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
80 if (maxLines == AudioSystem.NOT_SPECIFIED)
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
81 maxLines = 16;
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
82
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
83 System.out.print("SMGR.getMaxSimultaneousSounds() maxLines="+maxLines+"\n");
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
84
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
85 return maxLines;
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
86 }
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
87
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
88
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
89 protected void cleanUp()
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
90 {
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
91 System.out.print("SMGR.cleanUp()\n");
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
92 // signal to unpause
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
93 setPaused(false);
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
94
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
95 System.out.print("SMGR.cleanUp(): closing mixer\n");
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
96
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
97 // close the mixer (stops any running sounds)
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
98 Mixer mixer = AudioSystem.getMixer(null);
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
99 System.out.print("SMGR.cleanUp(): foo\n");
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
100 if (mixer.isOpen())
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
101 mixer.close();
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
102
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
103 System.out.print("SMGR.cleanUp(): leaving\n");
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
104 }
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
105
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
106
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
107 public void setPaused(boolean paused)
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
108 {
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
109 if (this.paused != paused)
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
110 {
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
111 synchronized (pausedLock)
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
112 {
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
113 this.paused = paused;
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
114 if (!paused)
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
115 {
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
116 // restart sounds
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
117 pausedLock.notifyAll();
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
118 }
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
119 }
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
120 }
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
121 }
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
122
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
123
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
124 public boolean isPaused()
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
125 {
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
126 return paused;
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
127 }
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
128
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
129
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
130 public Sound getSound(String filename)
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
131 {
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
132 return getSound(getAudioInputStream(filename));
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
133 }
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
134
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
135
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
136 public Sound getSound(InputStream is)
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
137 {
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
138 return getSound(getAudioInputStream(is));
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
139 }
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
140
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
141
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
142 public Sound getSound(AudioInputStream audioStream)
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
143 {
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
144 if (audioStream == null)
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
145 return null;
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
146
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
147 // get the number of bytes to read
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
148 int length = (int)(audioStream.getFrameLength() * audioStream.getFormat().getFrameSize());
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
149
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
150 // read the entire stream
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
151 byte[] samples = new byte[length];
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
152 DataInputStream is = new DataInputStream(audioStream);
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
153 try {
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
154 is.readFully(samples);
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
155 is.close();
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
156 }
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
157 catch (IOException ex)
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
158 {
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
159 ex.printStackTrace();
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
160 }
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
161
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
162 // return the samples
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
163 return new Sound(samples);
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
164 }
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
165
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
166
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
167 public AudioInputStream getAudioInputStream(String filename)
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
168 {
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
169 ResourceLoader res = new ResourceLoader(filename);
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
170 if (res == null || res.getStream() == null)
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
171 {
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
172 System.out.print("Could not load audio resource '"+ filename +"'.\n");
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
173 return null;
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
174 }
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
175 try {
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
176 return getAudioInputStream(res.getStream());
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
177 }
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
178 catch (Exception ex)
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
179 {
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
180 System.out.print("Could not get AudioInputStream for '"+ filename +"'\n");
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
181 return null;
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
182 }
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
183 }
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
184
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
185
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
186 public AudioInputStream getAudioInputStream(InputStream is)
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
187 {
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
188 try {
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
189 if (!is.markSupported())
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
190 is = new BufferedInputStream(is);
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
191
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
192 // open the source stream
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
193 AudioInputStream source = AudioSystem.getAudioInputStream(is);
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
194
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
195 // convert to playback format
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
196 return AudioSystem.getAudioInputStream(playbackFormat, source);
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
197 }
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
198
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
199 catch (UnsupportedAudioFileException ex) {
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
200 ex.printStackTrace();
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
201 }
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
202 catch (IOException ex) {
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
203 ex.printStackTrace();
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
204 }
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
205 catch (IllegalArgumentException ex) {
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
206 ex.printStackTrace();
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
207 }
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
208
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
209 return null;
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
210 }
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
211
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
212
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
213 public InputStream play(Sound sound)
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
214 {
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
215 InputStream is;
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
216 if (sound != null)
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
217 {
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
218 is = new ByteArrayInputStream(sound.getSamples());
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
219 return play(is);
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
220 }
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
221 return null;
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
222 }
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
223
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
224
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
225 public InputStream play(InputStream is)
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
226 {
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
227 System.out.print("SMGR.play()\n");
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
228 if (is != null)
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
229 runTask(new SoundPlayer(is));
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
230
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
231 return is;
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
232 }
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
233
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
234
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
235 protected void threadStarted()
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
236 {
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
237 synchronized (this)
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
238 {
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
239 try {
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
240 wait();
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
241 }
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
242 catch (InterruptedException ex) { }
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
243 }
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
244
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
245 System.out.print("SMGR.threadStarted()\n");
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
246
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
247 // use a short, 100ms (1/10th sec) buffer for filters that
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
248 // change in real-time
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
249 int bufferSize = playbackFormat.getFrameSize() *
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
250 Math.round(playbackFormat.getSampleRate() / 10);
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
251
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
252 // create, open, and start the line
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
253 SourceDataLine line;
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
254 DataLine.Info lineInfo = new DataLine.Info(SourceDataLine.class, playbackFormat);
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
255
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
256 try {
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
257 line = (SourceDataLine) AudioSystem.getLine(lineInfo);
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
258 line.open(playbackFormat, bufferSize);
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
259 }
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
260 catch (LineUnavailableException ex)
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
261 {
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
262 // the line is unavailable - signal to end this thread
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
263 Thread.currentThread().interrupt();
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
264 return;
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
265 }
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
266
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
267 /*
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
268 Control[] ctrls = line.getControls();
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
269 for (Control c : ctrls)
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
270 {
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
271 System.out.print("#" + c.toString() +"\n");
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
272 }
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
273 */
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
274 Control.Type ct = FloatControl.Type.MASTER_GAIN;
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
275 if (line.isControlSupported(ct))
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
276 {
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
277 FloatControl c = (FloatControl) line.getControl(ct);
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
278 c.setValue(-20f);
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
279 }
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
280
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
281 line.start();
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
282
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
283 // create the buffer
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
284 byte[] buffer = new byte[bufferSize];
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
285
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
286 // set this thread's locals
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
287 localLine.set(line);
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
288 localBuffer.set(buffer);
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
289 }
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
290
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
291
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
292 protected void threadStopped()
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
293 {
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
294 System.out.print("SMGR.threadStopped()\n");
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
295 SourceDataLine line = (SourceDataLine) localLine.get();
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
296 if (line != null)
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
297 {
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
298 line.drain();
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
299 line.close();
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
300 }
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
301 }
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
302
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
303
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
304 protected class SoundPlayer implements Runnable
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
305 {
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
306 private InputStream source;
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
307
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
308 public SoundPlayer(InputStream source)
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
309 {
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
310 this.source = source;
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
311 }
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
312
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
313 public void run()
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
314 {
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
315 // get line and buffer from ThreadLocals
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
316 SourceDataLine line = (SourceDataLine) localLine.get();
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
317 byte[] buffer = (byte[])localBuffer.get();
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
318
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
319 if (line == null || buffer == null)
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
320 return;
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
321
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
322 System.out.print("SMGR.SoundPlayer.run()\n");
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
323
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
324 // copy data to the line
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
325 try {
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
326 int numBytesRead = 0;
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
327
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
328 while (numBytesRead != -1) {
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
329 // if paused, wait until unpaused
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
330 synchronized (pausedLock)
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
331 {
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
332 if (paused) {
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
333 try {
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
334 pausedLock.wait();
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
335 }
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
336 catch (InterruptedException ex) {
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
337 return;
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
338 }
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
339 }
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
340 }
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
341
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
342 // copy data
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
343 numBytesRead = source.read(buffer, 0, buffer.length);
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
344 if (numBytesRead != -1)
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
345 line.write(buffer, 0, numBytesRead);
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
346 }
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
347 }
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
348
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
349 catch (IOException ex) {
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
350 ex.printStackTrace();
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
351 }
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
352
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
353 }
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
354 }
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
355
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
356 public synchronized void runTask(Runnable task)
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
357 {
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
358 if (!alive)
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
359 {
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
360 throw new IllegalStateException();
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
361 }
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
362 if (task != null)
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
363 {
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
364 queue.add(task);
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
365 notify();
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
366 }
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
367
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
368 }
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
369
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
370 protected synchronized Runnable getTask() throws InterruptedException
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
371 {
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
372 while (queue.size() == 0)
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
373 {
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
374 if (!alive)
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
375 return null;
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
376
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
377 wait();
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
378 }
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
379 return (Runnable) queue.removeFirst();
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
380 }
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
381
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
382
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
383 public synchronized void close()
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
384 {
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
385 System.out.print("SMGR.close()\n");
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
386
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
387 if (alive)
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
388 {
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
389 System.out.print("SMGR.close(): alive queue clear\n");
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
390 // Clear queue
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
391 alive = false;
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
392 queue.clear();
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
393 interrupt();
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
394 }
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
395
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
396 cleanUp();
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
397 System.out.print("SMGR.close(): leaving\n");
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
398 }
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
399
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
400
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
401 public void join()
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
402 {
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
403 System.out.print("SMGR.join()\n");
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
404 cleanUp();
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
405
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
406 synchronized (this)
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
407 {
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
408 alive = false;
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
409 notifyAll();
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
410 }
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
411
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
412 Thread[] threads = new Thread[activeCount()];
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
413 int count = enumerate(threads);
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
414 for (int i = 0; i < count; i++)
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
415 {
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
416 try {
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
417 threads[i].join();
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
418 }
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
419 catch (InterruptedException ex)
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
420 {
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
421 }
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
422 }
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
423 }
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
424
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
425
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
426 private class PooledThread extends Thread
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
427 {
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
428 public PooledThread()
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
429 {
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
430 super(SoundManager.this, "SoundManagerPool-" + (id++));
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
431 }
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
432
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
433 public void run()
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
434 {
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
435 threadStarted();
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
436
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
437 while (!isInterrupted()) {
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
438 Runnable task = null;
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
439 try {
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
440 task = getTask();
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
441 }
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
442 catch (InterruptedException ex)
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
443 {
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
444 }
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
445
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
446 if (task == null)
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
447 break;
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
448
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
449 try {
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
450 task.run();
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
451 }
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
452 catch (Throwable t) {
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
453 uncaughtException(this, t);
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
454 }
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
455 }
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
456 threadStopped();
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
457 }
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
458 }
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
459 }