annotate game/SoundManager.java @ 138:9eb791e2fa17

Optimize board updating logic, so that the old placed tiles need not to be redrawn from scratch on each screen update, as they do not change usually.
author Matti Hamalainen <ccr@tnsp.org>
date Fri, 25 Nov 2011 11:04:09 +0200
parents 4c0dec72e2f0
children d6d92845d6a2
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));
134
4c0dec72e2f0 Whitespace cosmetic cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 110
diff changeset
38
61
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");
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();
63
1c7a97d80120 Some work on the sound code ...
Matti Hamalainen <ccr@tnsp.org>
parents: 61
diff changeset
65 System.out.print("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 {
63
1c7a97d80120 Some work on the sound code ...
Matti Hamalainen <ccr@tnsp.org>
parents: 61
diff changeset
69 System.out.print(" - '"+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
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();
63
1c7a97d80120 Some work on the sound code ...
Matti Hamalainen <ccr@tnsp.org>
parents: 61
diff changeset
77 System.out.print(" * selected='"+i.getName()+"'\n");
61
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)
63
1c7a97d80120 Some work on the sound code ...
Matti Hamalainen <ccr@tnsp.org>
parents: 61
diff changeset
81 maxLines = 8;
61
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
82
63
1c7a97d80120 Some work on the sound code ...
Matti Hamalainen <ccr@tnsp.org>
parents: 61
diff changeset
83 System.out.print(" * maxLines="+maxLines+"\n");
61
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 if (mixer.isOpen())
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
100 mixer.close();
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
101
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
102 System.out.print("SMGR.cleanUp(): leaving\n");
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
103 }
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 public void setPaused(boolean paused)
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
107 {
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
108 if (this.paused != paused)
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
109 {
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
110 synchronized (pausedLock)
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
111 {
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
112 this.paused = paused;
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
113 if (!paused)
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
114 {
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
115 // restart sounds
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
116 pausedLock.notifyAll();
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
117 }
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 public boolean isPaused()
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
124 {
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
125 return paused;
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
126 }
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 public Sound getSound(String filename)
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
130 {
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
131 return getSound(getAudioInputStream(filename));
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
132 }
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 public Sound getSound(InputStream is)
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
136 {
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
137 return getSound(getAudioInputStream(is));
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
138 }
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 public Sound getSound(AudioInputStream audioStream)
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
142 {
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
143 if (audioStream == null)
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
144 return null;
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
145
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
146 // get the number of bytes to read
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
147 int length = (int)(audioStream.getFrameLength() * audioStream.getFormat().getFrameSize());
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
148
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
149 // read the entire stream
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
150 byte[] samples = new byte[length];
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
151 DataInputStream is = new DataInputStream(audioStream);
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
152 try {
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
153 is.readFully(samples);
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
154 is.close();
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
155 }
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
156 catch (IOException ex)
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
157 {
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
158 ex.printStackTrace();
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
159 }
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 // return the samples
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
162 return new Sound(samples);
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
163 }
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 public AudioInputStream getAudioInputStream(String filename)
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
167 {
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
168 ResourceLoader res = new ResourceLoader(filename);
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
169 if (res == null || res.getStream() == null)
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
170 {
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
171 System.out.print("Could not load audio resource '"+ filename +"'.\n");
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
172 return null;
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
173 }
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
174 try {
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
175 return getAudioInputStream(res.getStream());
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
176 }
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
177 catch (Exception ex)
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
178 {
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
179 System.out.print("Could not get AudioInputStream for '"+ filename +"'\n");
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
180 return null;
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
181 }
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 public AudioInputStream getAudioInputStream(InputStream is)
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
186 {
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
187 try {
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
188 if (!is.markSupported())
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
189 is = new BufferedInputStream(is);
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
190
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
191 // open the source stream
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
192 AudioInputStream source = AudioSystem.getAudioInputStream(is);
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
193
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
194 // convert to playback format
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
195 return AudioSystem.getAudioInputStream(playbackFormat, source);
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
196 }
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 catch (UnsupportedAudioFileException ex) {
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
199 ex.printStackTrace();
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
200 }
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
201 catch (IOException ex) {
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
202 ex.printStackTrace();
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
203 }
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
204 catch (IllegalArgumentException ex) {
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
205 ex.printStackTrace();
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
206 }
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 return null;
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
209 }
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 public InputStream play(Sound sound)
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
213 {
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
214 InputStream is;
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
215 if (sound != null)
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
216 {
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
217 is = new ByteArrayInputStream(sound.getSamples());
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
218 return play(is);
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
219 }
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
220 return null;
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
221 }
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 public InputStream play(InputStream is)
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
225 {
93
e1d657e6c25b Work on audio code.
Matti Hamalainen <ccr@tnsp.org>
parents: 63
diff changeset
226 System.out.print("SMGR.play(is="+is+")\n");
61
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
227 if (is != null)
63
1c7a97d80120 Some work on the sound code ...
Matti Hamalainen <ccr@tnsp.org>
parents: 61
diff changeset
228 {
61
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
229 runTask(new SoundPlayer(is));
63
1c7a97d80120 Some work on the sound code ...
Matti Hamalainen <ccr@tnsp.org>
parents: 61
diff changeset
230 }
61
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
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
246 // 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
247 // change in real-time
110
3551b61b3c0b Increase buffer size a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 107
diff changeset
248 int bufferSize = playbackFormat.getFrameSize() * Math.round(playbackFormat.getSampleRate() / 10);
61
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
249
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
250 // create, open, and start the line
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
251 SourceDataLine line;
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
252 DataLine.Info lineInfo = new DataLine.Info(SourceDataLine.class, playbackFormat);
134
4c0dec72e2f0 Whitespace cosmetic cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 110
diff changeset
253
93
e1d657e6c25b Work on audio code.
Matti Hamalainen <ccr@tnsp.org>
parents: 63
diff changeset
254 System.out.print("SMGR.threadStarted(): "+lineInfo.toString()+"\n");
61
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 {
63
1c7a97d80120 Some work on the sound code ...
Matti Hamalainen <ccr@tnsp.org>
parents: 61
diff changeset
262 System.out.print("SMGR.threadStarted() line unavailable!\n");
61
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
263 // the line is unavailable - signal to end this thread
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
264 Thread.currentThread().interrupt();
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
265 return;
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
266 }
134
4c0dec72e2f0 Whitespace cosmetic cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 110
diff changeset
267
63
1c7a97d80120 Some work on the sound code ...
Matti Hamalainen <ccr@tnsp.org>
parents: 61
diff changeset
268 // Change volume
61
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
269 Control.Type ct = FloatControl.Type.MASTER_GAIN;
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
270 if (line.isControlSupported(ct))
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
271 {
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
272 FloatControl c = (FloatControl) line.getControl(ct);
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
273 c.setValue(-20f);
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
274 }
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 line.start();
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
277
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
278 // create the buffer
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
279 byte[] buffer = new byte[bufferSize];
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 // set this thread's locals
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
282 localLine.set(line);
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
283 localBuffer.set(buffer);
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
284 }
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 protected void threadStopped()
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
288 {
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
289 System.out.print("SMGR.threadStopped()\n");
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
290 SourceDataLine line = (SourceDataLine) localLine.get();
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
291 if (line != null)
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
292 {
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
293 line.drain();
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
294 line.close();
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
295 }
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 protected class SoundPlayer implements Runnable
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 private InputStream source;
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 public SoundPlayer(InputStream source)
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
304 {
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
305 this.source = source;
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
306 }
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 void run()
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 // get line and buffer from ThreadLocals
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
311 SourceDataLine line = (SourceDataLine) localLine.get();
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
312 byte[] buffer = (byte[])localBuffer.get();
134
4c0dec72e2f0 Whitespace cosmetic cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 110
diff changeset
313
61
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
314 if (line == null || buffer == null)
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
315 return;
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
316
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
317 // copy data to the line
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
318 try {
107
dd896bc7352b Fix sound streaming code to cope with partial buffer reads.
Matti Hamalainen <ccr@tnsp.org>
parents: 93
diff changeset
319 boolean playing = true;
dd896bc7352b Fix sound streaming code to cope with partial buffer reads.
Matti Hamalainen <ccr@tnsp.org>
parents: 93
diff changeset
320 while (playing) {
61
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
321 // if paused, wait until unpaused
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
322 synchronized (pausedLock)
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 if (paused) {
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 pausedLock.wait();
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 catch (InterruptedException ex) {
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
329 return;
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
330 }
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 }
134
4c0dec72e2f0 Whitespace cosmetic cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 110
diff changeset
333
61
ceaf645ed930 Add (non-working) audio code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
334 // copy data
107
dd896bc7352b Fix sound streaming code to cope with partial buffer reads.
Matti Hamalainen <ccr@tnsp.org>
parents: 93
diff changeset
335 int bufPos = 0;
dd896bc7352b Fix sound streaming code to cope with partial buffer reads.
Matti Hamalainen <ccr@tnsp.org>
parents: 93
diff changeset
336 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
337 {
dd896bc7352b Fix sound streaming code to cope with partial buffer reads.
Matti Hamalainen <ccr@tnsp.org>
parents: 93
diff changeset
338 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
339 if (res != -1)
dd896bc7352b Fix sound streaming code to cope with partial buffer reads.
Matti Hamalainen <ccr@tnsp.org>
parents: 93
diff changeset
340 bufPos += res;
dd896bc7352b Fix sound streaming code to cope with partial buffer reads.
Matti Hamalainen <ccr@tnsp.org>
parents: 93
diff changeset
341 else
dd896bc7352b Fix sound streaming code to cope with partial buffer reads.
Matti Hamalainen <ccr@tnsp.org>
parents: 93
diff changeset
342 playing = false;
dd896bc7352b Fix sound streaming code to cope with partial buffer reads.
Matti Hamalainen <ccr@tnsp.org>
parents: 93
diff changeset
343 }
dd896bc7352b Fix sound streaming code to cope with partial buffer reads.
Matti Hamalainen <ccr@tnsp.org>
parents: 93
diff changeset
344 if (playing)
dd896bc7352b Fix sound streaming code to cope with partial buffer reads.
Matti Hamalainen <ccr@tnsp.org>
parents: 93
diff changeset
345 line.write(buffer, 0, bufPos);
61
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 }