annotate game/SoundManager.java @ 201:bd3cde4bc15c

Add a comment.
author Matti Hamalainen <ccr@tnsp.org>
date Thu, 25 Apr 2019 12:58:17 +0300
parents 8dbaa093c562
children
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
151
d6d92845d6a2 ISO-8859-1 -> UTF-8.
Matti Hamalainen <ccr@tnsp.org>
parents: 134
diff changeset
3 * (C) Copyright 2011 Matti 'ccr' Hämäläinen <ccr@tnsp.org>
61
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));
134
4c0dec72e2f0 Whitespace cosmetic cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 110
diff changeset
38
192
8dbaa093c562 Improve debug message handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
39 G.debug("SMGR.SoundManager() initializing with " + numThreads +" max sounds\n");
134
4c0dec72e2f0 Whitespace cosmetic cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 110
diff changeset
40
61
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 playbackFormat = format;
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
45 localLine = new ThreadLocal();
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
46 localBuffer = new ThreadLocal();
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
47 pausedLock = new Object();
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
48
93
e1d657e6c25b Work on audio code.
Matti Hamalainen <ccr@tnsp.org>
parents: 63
diff changeset
49 queue = new LinkedList();
e1d657e6c25b Work on audio code.
Matti Hamalainen <ccr@tnsp.org>
parents: 63
diff changeset
50 for (int i = 0; i < numThreads; i++)
e1d657e6c25b Work on audio code.
Matti Hamalainen <ccr@tnsp.org>
parents: 63
diff changeset
51 new PooledThread().start();
e1d657e6c25b Work on audio code.
Matti Hamalainen <ccr@tnsp.org>
parents: 63
diff changeset
52
61
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();
192
8dbaa093c562 Improve debug message handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
65 G.debug("SMGR.getMaxSimultaneousSounds() mixer information:\n");
61
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 {
192
8dbaa093c562 Improve debug message handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
69 G.debug(" - '"+i.getName()+"'\n");
61
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
201
bd3cde4bc15c Add a comment.
Matti Hamalainen <ccr@tnsp.org>
parents: 192
diff changeset
74 // if 'select' == null, uses default
61
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
75 Mixer mixer = AudioSystem.getMixer(select);
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
76
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
77 Mixer.Info i = mixer.getMixerInfo();
192
8dbaa093c562 Improve debug message handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
78 G.debug(" * selected='"+i.getName()+"'\n");
61
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
79
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
80 int maxLines = mixer.getMaxLines(lineInfo);
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
81 if (maxLines == AudioSystem.NOT_SPECIFIED)
63
1c7a97d80120 Some work on the sound code ...
Matti Hamalainen <ccr@tnsp.org>
parents: 61
diff changeset
82 maxLines = 8;
61
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
83
192
8dbaa093c562 Improve debug message handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
84 G.debug(" * maxLines="+maxLines+"\n");
61
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
85
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
86 return maxLines;
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
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
90 protected void cleanUp()
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
91 {
192
8dbaa093c562 Improve debug message handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
92 G.debug("SMGR.cleanUp()\n");
61
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
93 // signal to unpause
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
94 setPaused(false);
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
95
192
8dbaa093c562 Improve debug message handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
96 G.debug("SMGR.cleanUp(): closing mixer\n");
61
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
97
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
98 // close the mixer (stops any running sounds)
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
99 Mixer mixer = AudioSystem.getMixer(null);
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
192
8dbaa093c562 Improve debug message handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
103 G.debug("SMGR.cleanUp(): leaving\n");
61
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 {
192
8dbaa093c562 Improve debug message handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
172 G.debug("Could not load audio resource '"+ filename +"'.\n");
61
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 {
192
8dbaa093c562 Improve debug message handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
180 G.debug("Could not get AudioInputStream for '"+ filename +"'\n");
61
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 {
192
8dbaa093c562 Improve debug message handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
227 G.debug("SMGR.play(is="+is+")\n");
61
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
228 if (is != null)
63
1c7a97d80120 Some work on the sound code ...
Matti Hamalainen <ccr@tnsp.org>
parents: 61
diff changeset
229 {
61
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
230 runTask(new SoundPlayer(is));
63
1c7a97d80120 Some work on the sound code ...
Matti Hamalainen <ccr@tnsp.org>
parents: 61
diff changeset
231 }
61
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
232 return is;
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
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
236 protected void threadStarted()
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
237 {
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
238 synchronized (this)
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
239 {
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
240 try {
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
241 wait();
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
242 }
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
243 catch (InterruptedException ex) { }
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
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
110
3551b61b3c0b Increase buffer size a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 107
diff changeset
249 int bufferSize = playbackFormat.getFrameSize() * Math.round(playbackFormat.getSampleRate() / 10);
61
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
250
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
251 // create, open, and start the line
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
252 SourceDataLine line;
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
253 DataLine.Info lineInfo = new DataLine.Info(SourceDataLine.class, playbackFormat);
134
4c0dec72e2f0 Whitespace cosmetic cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 110
diff changeset
254
192
8dbaa093c562 Improve debug message handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
255 G.debug("SMGR.threadStarted(): "+lineInfo.toString()+"\n");
61
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
256
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
257 try {
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
258 line = (SourceDataLine) AudioSystem.getLine(lineInfo);
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
259 line.open(playbackFormat, bufferSize);
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
260 }
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
261 catch (LineUnavailableException ex)
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
262 {
192
8dbaa093c562 Improve debug message handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
263 G.debug("SMGR.threadStarted() line unavailable!\n");
61
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
264 // the line is unavailable - signal to end this thread
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
265 Thread.currentThread().interrupt();
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
266 return;
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
267 }
134
4c0dec72e2f0 Whitespace cosmetic cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 110
diff changeset
268
63
1c7a97d80120 Some work on the sound code ...
Matti Hamalainen <ccr@tnsp.org>
parents: 61
diff changeset
269 // Change volume
61
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
270 Control.Type ct = FloatControl.Type.MASTER_GAIN;
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
271 if (line.isControlSupported(ct))
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 FloatControl c = (FloatControl) line.getControl(ct);
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
274 c.setValue(-20f);
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
275 }
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 line.start();
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
278
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
279 // create the buffer
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
280 byte[] buffer = new byte[bufferSize];
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
281
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
282 // set this thread's locals
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
283 localLine.set(line);
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
284 localBuffer.set(buffer);
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
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
287
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
288 protected void threadStopped()
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
289 {
192
8dbaa093c562 Improve debug message handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
290 G.debug("SMGR.threadStopped()\n");
61
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
291 SourceDataLine line = (SourceDataLine) localLine.get();
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
292 if (line != null)
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 line.drain();
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
295 line.close();
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
296 }
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
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
299
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
300 protected class SoundPlayer implements Runnable
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 private InputStream source;
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 public SoundPlayer(InputStream source)
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 this.source = 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
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
309 public void run()
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
310 {
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
311 // get line and buffer from ThreadLocals
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
312 SourceDataLine line = (SourceDataLine) localLine.get();
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
313 byte[] buffer = (byte[])localBuffer.get();
134
4c0dec72e2f0 Whitespace cosmetic cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 110
diff changeset
314
61
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
315 if (line == null || buffer == null)
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
316 return;
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
317
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
318 // copy data to the line
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
319 try {
107
dd896bc7352b Fix sound streaming code to cope with partial buffer reads.
Matti Hamalainen <ccr@tnsp.org>
parents: 93
diff changeset
320 boolean playing = true;
dd896bc7352b Fix sound streaming code to cope with partial buffer reads.
Matti Hamalainen <ccr@tnsp.org>
parents: 93
diff changeset
321 while (playing) {
61
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
322 // if paused, wait until unpaused
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
323 synchronized (pausedLock)
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
324 {
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
325 if (paused) {
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
326 try {
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
327 pausedLock.wait();
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
328 }
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
329 catch (InterruptedException ex) {
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
330 return;
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 }
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
333 }
134
4c0dec72e2f0 Whitespace cosmetic cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 110
diff changeset
334
61
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
335 // copy data
107
dd896bc7352b Fix sound streaming code to cope with partial buffer reads.
Matti Hamalainen <ccr@tnsp.org>
parents: 93
diff changeset
336 int bufPos = 0;
dd896bc7352b Fix sound streaming code to cope with partial buffer reads.
Matti Hamalainen <ccr@tnsp.org>
parents: 93
diff changeset
337 while (bufPos < buffer.length && playing)
dd896bc7352b Fix sound streaming code to cope with partial buffer reads.
Matti Hamalainen <ccr@tnsp.org>
parents: 93
diff changeset
338 {
dd896bc7352b Fix sound streaming code to cope with partial buffer reads.
Matti Hamalainen <ccr@tnsp.org>
parents: 93
diff changeset
339 int res = source.read(buffer, bufPos, buffer.length - bufPos);
dd896bc7352b Fix sound streaming code to cope with partial buffer reads.
Matti Hamalainen <ccr@tnsp.org>
parents: 93
diff changeset
340 if (res != -1)
dd896bc7352b Fix sound streaming code to cope with partial buffer reads.
Matti Hamalainen <ccr@tnsp.org>
parents: 93
diff changeset
341 bufPos += res;
dd896bc7352b Fix sound streaming code to cope with partial buffer reads.
Matti Hamalainen <ccr@tnsp.org>
parents: 93
diff changeset
342 else
dd896bc7352b Fix sound streaming code to cope with partial buffer reads.
Matti Hamalainen <ccr@tnsp.org>
parents: 93
diff changeset
343 playing = false;
dd896bc7352b Fix sound streaming code to cope with partial buffer reads.
Matti Hamalainen <ccr@tnsp.org>
parents: 93
diff changeset
344 }
dd896bc7352b Fix sound streaming code to cope with partial buffer reads.
Matti Hamalainen <ccr@tnsp.org>
parents: 93
diff changeset
345 if (playing)
dd896bc7352b Fix sound streaming code to cope with partial buffer reads.
Matti Hamalainen <ccr@tnsp.org>
parents: 93
diff changeset
346 line.write(buffer, 0, bufPos);
61
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
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
350 catch (IOException ex) {
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
351 ex.printStackTrace();
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
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
357 public synchronized void runTask(Runnable task)
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
358 {
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
359 if (!alive)
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
360 {
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
361 throw new IllegalStateException();
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
362 }
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
363 if (task != null)
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
364 {
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
365 queue.add(task);
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
366 notify();
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
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
371 protected synchronized Runnable getTask() throws InterruptedException
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
372 {
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
373 while (queue.size() == 0)
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
374 {
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
375 if (!alive)
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
376 return null;
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
377
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
378 wait();
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
379 }
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
380 return (Runnable) queue.removeFirst();
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
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
384 public synchronized void close()
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
385 {
192
8dbaa093c562 Improve debug message handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
386 G.debug("SMGR.close()\n");
61
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
387
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
388 if (alive)
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
389 {
192
8dbaa093c562 Improve debug message handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
390 G.debug("SMGR.close(): alive queue clear\n");
61
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
391 // Clear queue
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
392 alive = false;
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
393 queue.clear();
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
394 interrupt();
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
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
397 cleanUp();
192
8dbaa093c562 Improve debug message handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
398 G.debug("SMGR.close(): leaving\n");
61
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
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
402 public void join()
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
403 {
192
8dbaa093c562 Improve debug message handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
404 G.debug("SMGR.join()\n");
61
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
405 cleanUp();
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
406
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
407 synchronized (this)
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
408 {
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
409 alive = false;
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
410 notifyAll();
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
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
413 Thread[] threads = new Thread[activeCount()];
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
414 int count = enumerate(threads);
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
415 for (int i = 0; i < count; i++)
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
416 {
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
417 try {
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
418 threads[i].join();
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
419 }
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
420 catch (InterruptedException ex)
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
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
427 private class PooledThread extends Thread
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
428 {
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
429 public PooledThread()
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
430 {
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
431 super(SoundManager.this, "SoundManagerPool-" + (id++));
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
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
434 public void run()
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
435 {
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
436 threadStarted();
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
437
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
438 while (!isInterrupted()) {
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
439 Runnable task = null;
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
440 try {
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
441 task = getTask();
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
442 }
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
443 catch (InterruptedException ex)
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
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
447 if (task == null)
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
448 break;
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
449
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
450 try {
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
451 task.run();
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
452 }
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
453 catch (Throwable t) {
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
454 uncaughtException(this, t);
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 }
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
457 threadStopped();
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 }
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
460 }