comparison game/SoundManager.java @ 192:8dbaa093c562

Improve debug message handling.
author Matti Hamalainen <ccr@tnsp.org>
date Tue, 23 Apr 2019 13:00:25 +0300
parents e8eeac403e5f
children bd3cde4bc15c
comparison
equal deleted inserted replaced
191:edf4081fecd6 192:8dbaa093c562
34 { 34 {
35 super("SoundManagerPool-" + (poolID++)); 35 super("SoundManagerPool-" + (poolID++));
36 36
37 int numThreads = Math.min(maxSounds, getMaxSimultaneousSounds(playbackFormat)); 37 int numThreads = Math.min(maxSounds, getMaxSimultaneousSounds(playbackFormat));
38 38
39 System.out.print("SMGR.SoundManager() initializing with " + numThreads +" max sounds\n"); 39 G.debug("SMGR.SoundManager() initializing with " + numThreads +" max sounds\n");
40 40
41 setDaemon(true); 41 setDaemon(true);
42 alive = true; 42 alive = true;
43 43
44 playbackFormat = format; 44 playbackFormat = format;
60 public static int getMaxSimultaneousSounds(AudioFormat playbackFormat) 60 public static int getMaxSimultaneousSounds(AudioFormat playbackFormat)
61 { 61 {
62 DataLine.Info lineInfo = new DataLine.Info(SourceDataLine.class, playbackFormat); 62 DataLine.Info lineInfo = new DataLine.Info(SourceDataLine.class, playbackFormat);
63 63
64 Mixer.Info[] info = AudioSystem.getMixerInfo(); 64 Mixer.Info[] info = AudioSystem.getMixerInfo();
65 System.out.print("SMGR.getMaxSimultaneousSounds() mixer information:\n"); 65 G.debug("SMGR.getMaxSimultaneousSounds() mixer information:\n");
66 Mixer.Info select = null; 66 Mixer.Info select = null;
67 for (Mixer.Info i : info) 67 for (Mixer.Info i : info)
68 { 68 {
69 System.out.print(" - '"+i.getName()+"'\n"); 69 G.debug(" - '"+i.getName()+"'\n");
70 if (i.getName().equals("Java Sound Audio Engine")) 70 if (i.getName().equals("Java Sound Audio Engine"))
71 select = i; 71 select = i;
72 } 72 }
73 73
74 Mixer mixer = AudioSystem.getMixer(select); 74 Mixer mixer = AudioSystem.getMixer(select);
75 75
76 Mixer.Info i = mixer.getMixerInfo(); 76 Mixer.Info i = mixer.getMixerInfo();
77 System.out.print(" * selected='"+i.getName()+"'\n"); 77 G.debug(" * selected='"+i.getName()+"'\n");
78 78
79 int maxLines = mixer.getMaxLines(lineInfo); 79 int maxLines = mixer.getMaxLines(lineInfo);
80 if (maxLines == AudioSystem.NOT_SPECIFIED) 80 if (maxLines == AudioSystem.NOT_SPECIFIED)
81 maxLines = 8; 81 maxLines = 8;
82 82
83 System.out.print(" * maxLines="+maxLines+"\n"); 83 G.debug(" * maxLines="+maxLines+"\n");
84 84
85 return maxLines; 85 return maxLines;
86 } 86 }
87 87
88 88
89 protected void cleanUp() 89 protected void cleanUp()
90 { 90 {
91 System.out.print("SMGR.cleanUp()\n"); 91 G.debug("SMGR.cleanUp()\n");
92 // signal to unpause 92 // signal to unpause
93 setPaused(false); 93 setPaused(false);
94 94
95 System.out.print("SMGR.cleanUp(): closing mixer\n"); 95 G.debug("SMGR.cleanUp(): closing mixer\n");
96 96
97 // close the mixer (stops any running sounds) 97 // close the mixer (stops any running sounds)
98 Mixer mixer = AudioSystem.getMixer(null); 98 Mixer mixer = AudioSystem.getMixer(null);
99 if (mixer.isOpen()) 99 if (mixer.isOpen())
100 mixer.close(); 100 mixer.close();
101 101
102 System.out.print("SMGR.cleanUp(): leaving\n"); 102 G.debug("SMGR.cleanUp(): leaving\n");
103 } 103 }
104 104
105 105
106 public void setPaused(boolean paused) 106 public void setPaused(boolean paused)
107 { 107 {
166 public AudioInputStream getAudioInputStream(String filename) 166 public AudioInputStream getAudioInputStream(String filename)
167 { 167 {
168 ResourceLoader res = new ResourceLoader(filename); 168 ResourceLoader res = new ResourceLoader(filename);
169 if (res == null || res.getStream() == null) 169 if (res == null || res.getStream() == null)
170 { 170 {
171 System.out.print("Could not load audio resource '"+ filename +"'.\n"); 171 G.debug("Could not load audio resource '"+ filename +"'.\n");
172 return null; 172 return null;
173 } 173 }
174 try { 174 try {
175 return getAudioInputStream(res.getStream()); 175 return getAudioInputStream(res.getStream());
176 } 176 }
177 catch (Exception ex) 177 catch (Exception ex)
178 { 178 {
179 System.out.print("Could not get AudioInputStream for '"+ filename +"'\n"); 179 G.debug("Could not get AudioInputStream for '"+ filename +"'\n");
180 return null; 180 return null;
181 } 181 }
182 } 182 }
183 183
184 184
221 } 221 }
222 222
223 223
224 public InputStream play(InputStream is) 224 public InputStream play(InputStream is)
225 { 225 {
226 System.out.print("SMGR.play(is="+is+")\n"); 226 G.debug("SMGR.play(is="+is+")\n");
227 if (is != null) 227 if (is != null)
228 { 228 {
229 runTask(new SoundPlayer(is)); 229 runTask(new SoundPlayer(is));
230 } 230 }
231 return is; 231 return is;
249 249
250 // create, open, and start the line 250 // create, open, and start the line
251 SourceDataLine line; 251 SourceDataLine line;
252 DataLine.Info lineInfo = new DataLine.Info(SourceDataLine.class, playbackFormat); 252 DataLine.Info lineInfo = new DataLine.Info(SourceDataLine.class, playbackFormat);
253 253
254 System.out.print("SMGR.threadStarted(): "+lineInfo.toString()+"\n"); 254 G.debug("SMGR.threadStarted(): "+lineInfo.toString()+"\n");
255 255
256 try { 256 try {
257 line = (SourceDataLine) AudioSystem.getLine(lineInfo); 257 line = (SourceDataLine) AudioSystem.getLine(lineInfo);
258 line.open(playbackFormat, bufferSize); 258 line.open(playbackFormat, bufferSize);
259 } 259 }
260 catch (LineUnavailableException ex) 260 catch (LineUnavailableException ex)
261 { 261 {
262 System.out.print("SMGR.threadStarted() line unavailable!\n"); 262 G.debug("SMGR.threadStarted() line unavailable!\n");
263 // the line is unavailable - signal to end this thread 263 // the line is unavailable - signal to end this thread
264 Thread.currentThread().interrupt(); 264 Thread.currentThread().interrupt();
265 return; 265 return;
266 } 266 }
267 267
284 } 284 }
285 285
286 286
287 protected void threadStopped() 287 protected void threadStopped()
288 { 288 {
289 System.out.print("SMGR.threadStopped()\n"); 289 G.debug("SMGR.threadStopped()\n");
290 SourceDataLine line = (SourceDataLine) localLine.get(); 290 SourceDataLine line = (SourceDataLine) localLine.get();
291 if (line != null) 291 if (line != null)
292 { 292 {
293 line.drain(); 293 line.drain();
294 line.close(); 294 line.close();
380 } 380 }
381 381
382 382
383 public synchronized void close() 383 public synchronized void close()
384 { 384 {
385 System.out.print("SMGR.close()\n"); 385 G.debug("SMGR.close()\n");
386 386
387 if (alive) 387 if (alive)
388 { 388 {
389 System.out.print("SMGR.close(): alive queue clear\n"); 389 G.debug("SMGR.close(): alive queue clear\n");
390 // Clear queue 390 // Clear queue
391 alive = false; 391 alive = false;
392 queue.clear(); 392 queue.clear();
393 interrupt(); 393 interrupt();
394 } 394 }
395 395
396 cleanUp(); 396 cleanUp();
397 System.out.print("SMGR.close(): leaving\n"); 397 G.debug("SMGR.close(): leaving\n");
398 } 398 }
399 399
400 400
401 public void join() 401 public void join()
402 { 402 {
403 System.out.print("SMGR.join()\n"); 403 G.debug("SMGR.join()\n");
404 cleanUp(); 404 cleanUp();
405 405
406 synchronized (this) 406 synchronized (this)
407 { 407 {
408 alive = false; 408 alive = false;