Changeset 51 in tmcsimulator
- Timestamp:
- 07/03/2016 06:25:46 AM (10 years ago)
- Location:
- trunk
- Files:
-
- 1 added
- 1 deleted
- 4 edited
-
IDE_metadata/NetBeans/TMCSim/nbproject/project.properties (modified) (2 diffs)
-
audio/187/18701.wav (deleted)
-
lib/jaco-mp3-player-0.9.3.jar (added)
-
src/tmcsim/application.properties (modified) (1 diff)
-
src/tmcsim/cadsimulator/SoundPlayer.java (modified) (2 diffs)
-
test/tmcsim/cadsimulator/SoundPlayerTest.java (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
trunk/IDE_metadata/NetBeans/TMCSim/nbproject/project.properties
r43 r51 28 28 endorsed.classpath= 29 29 excludes= 30 file.reference.jaco-mp3-player-0.9.3.jar=../../../lib/jaco-mp3-player-0.9.3.jar 30 31 file.reference.mockito-1.10.19.jar=../../../lib/mockito-1.10.19.jar 31 file.reference.mp3plugin.jar=../../../lib/mp3plugin.jar32 32 file.reference.tmc-simulator-src=../../../src 33 33 file.reference.tmc-simulator-test=../../../test … … 39 39 ${file.reference.xercesImpl.jar}:\ 40 40 ${file.reference.uispec4j-jdk17.jar}:\ 41 ${file.reference.mp3plugin.jar}:\42 41 ${file.reference.mockito-1.10.19.jar}:\ 43 ${libs.junit_4.classpath} 42 ${libs.junit_4.classpath}:\ 43 ${file.reference.jaco-mp3-player-0.9.3.jar} 44 44 # Space-separated list of extra javac options 45 45 javac.compilerargs= -
trunk/src/tmcsim/application.properties
r50 r51 1 #S at, 02 Jul 2016 16:19:54-07001 #Sun, 03 Jul 2016 06:56:35 -0700 2 2 3 3 Application.revision=49 4 4 5 Application.buildnumber= 195 Application.buildnumber=20 -
trunk/src/tmcsim/cadsimulator/SoundPlayer.java
r16 r51 1 1 package tmcsim.cadsimulator; 2 2 3 import jaco.mp3.player.MP3Player; 4 import java.applet.*; 3 5 import java.io.File; 4 import java.io.FileInputStream;5 6 import java.util.Timer; 6 7 import java.util.TimerTask; … … 8 9 import java.util.logging.Level; 9 10 import java.util.logging.Logger; 10 11 import sun.audio.AudioPlayer;12 import sun.audio.AudioStream;13 11 import tmcsim.client.cadclientgui.data.IncidentEvent; 14 12 15 13 /** 16 * SoundPlayer is used to play audio files associated with IncidentEvents that 17 * occur during a simulation. When the SoundPlayer has been enabled audio 18 * clips are enqueued by calling the enqueueClip() method. The audio files 19 * are played in order of receipt and when finished, the IncidentEvent is 20 * notified. Audio playing may be disabled or enabled through the 21 * setAudioEnabled() method. Disabling audio will cause a currently 22 * playing audio clip to stop and be requeued at the front of the queue. 23 * Re-enabling the audio will then continue playing all queued clips. The 24 * deQueueAll() method is used to clear the list of audio clips that have 25 * been queued. 14 * SoundPlayer is used to play audio files associated with IncidentEvents that 15 * occur during a simulation. When the SoundPlayer has been enabled audio clips 16 * are enqueued by calling the enqueueClip() method. The audio files are played 17 * in order of receipt and when finished, the IncidentEvent is notified. Audio 18 * playing may be disabled or enabled through the setAudioEnabled() method. 19 * Disabling audio will cause a currently playing audio clip to stop and be 20 * requeued at the front of the queue. Re-enabling the audio will then continue 21 * playing all queued clips. The deQueueAll() method is used to clear the list 22 * of audio clips that have been queued. 26 23 * 27 24 * @author Matthew Cechini (mcechini@calpoly.edu) 28 * @version $Date: 2006/06/06 20:46:41 $ $Revision: 1.3 $ 25 * @version Date: 2006/06/06 20:46:41 26 * @author Nicolas Russo modified to play mp3 audio. 27 * @version 2016.07.03 29 28 */ 30 public class SoundPlayer extends Thread { 31 32 /** Error Logger. */ 29 public class SoundPlayer extends Thread 30 { 31 32 /** 33 * Error Logger. 34 */ 33 35 private Logger soundLogger = Logger.getLogger("tmcsim.cadsimulator"); 34 35 /** The base location that wav files are referenced from */ 36 /** 37 * The base location that wav files are referenced from 38 */ 36 39 private String baseAudioDir = ""; 37 38 /** The private vector of audioClips used during a simulation */ 40 /** 41 * The private vector of audioClips used during a simulation 42 */ 39 43 private Vector<AudioClipInfo> enqueuedClips = null; 40 41 /** The audio clip that is currently being played by the AudioPlayer. */ 44 /** 45 * The audio clip that is currently being played by the AudioPlayer. 46 */ 42 47 private AudioClipInfo currentClip = null; 43 44 /** Audio stream for playing audio files. */ 45 private AudioStream audioStream = null; 46 47 /** 48 * Flag to designate when an audio file is playing. This prevents 49 * multiple audio files from being played simultaneously. 50 * Flag is initialized to false. 51 */ 52 private boolean audioPlaying = false; 53 54 /** Flag to designate whether audio playing is enabled. If audio is disabled, 55 * then enquque events will be ignored. Flag is initialized to false. 48 /** 49 * Audio stream for playing audio files. 50 */ 51 private AudioClip clip = null; 52 private MP3Player mp3Player = null; 53 /** 54 * Flag to designate when an audio file is playing. This prevents multiple 55 * audio files from being played simultaneously. Flag is initialized to 56 * false. 57 */ 58 private boolean audioPlaying = false; 59 /** 60 * Flag to designate whether audio playing is enabled. If audio is disabled, 61 * then enquque events will be ignored. Flag is initialized to false. 56 62 */ 57 63 private boolean audioEnabled = false; 58 59 /** Timer used to pause audio player during an audio file. */ 64 /** 65 * Timer used to pause audio player during an audio file. 66 */ 60 67 private Timer timer = null; 61 62 /** Inner class used to contain the audio file's name and duration. */ 63 private class AudioClipInfo { 68 69 /** 70 * Inner class used to contain the audio file's name and duration. 71 */ 72 private class AudioClipInfo 73 { 74 64 75 String fileName; 65 76 int duration; 66 77 IncidentEvent theEvent; 67 68 AudioClipInfo(IncidentEvent ie) { 78 79 AudioClipInfo(IncidentEvent ie) 80 { 69 81 fileName = ie.waveFile; 70 duration = ie.waveLength; 82 duration = ie.waveLength; 71 83 theEvent = ie; 72 } 73 74 AudioClipInfo(String name, int dur) { 84 } 85 86 AudioClipInfo(String name, int dur) 87 { 75 88 fileName = name; 76 duration = dur; 89 duration = dur; 77 90 theEvent = null; 78 } 79 80 public void wavePlayed() { 81 if(theEvent != null) 91 } 92 93 public void wavePlayed() 94 { 95 if (theEvent != null) 96 { 82 97 theEvent.wavePlayed(); 83 } 84 } 85 86 /** 87 * Constructor. Establish the base file path for wav file referencing. 88 * 89 * @param wavFilePath Pathname for where the simulation wav files will be referenced. 90 */ 91 public SoundPlayer(String baseFilePath) { 92 93 baseAudioDir = baseFilePath; 94 audioPlaying = false; 95 audioEnabled = true; 96 timer = new Timer(); 98 } 99 } 100 } 101 102 /** 103 * Constructor. Establish the base file path for wav file referencing. 104 * 105 * @param wavFilePath Pathname for where the simulation wav files will be 106 * referenced. 107 */ 108 public SoundPlayer(String baseFilePath) 109 { 110 baseAudioDir = baseFilePath; 111 audioPlaying = false; 112 audioEnabled = true; 113 timer = new Timer(); 97 114 enqueuedClips = new Vector<AudioClipInfo>(); 98 115 } 99 116 100 117 /** 101 * Add a audioClipInfo object to the audioClips Vector. The enqueued audio file will play 102 * when all previously enqueued audio files have been played. If audio is not enabled, the 103 * clip will not be queued, and the incident event will be notified that the audio clip 104 * has finished playing to allow the simulation to continue. 118 * Add a audioClipInfo object to the audioClips Vector. The enqueued audio 119 * file will play when all previously enqueued audio files have been played. 120 * If audio is not enabled, the clip will not be queued, and the incident 121 * event will be notified that the audio clip has finished playing to allow 122 * the simulation to continue. 105 123 * 106 124 * @param ie The Incident Event to enqueue to the current list of events. 107 125 */ 108 public void enqueueClip (IncidentEvent ie) { 109 110 if(audioEnabled) { 111 synchronized(enqueuedClips) { 126 public void enqueueClip(IncidentEvent ie) 127 { 128 129 if (audioEnabled) 130 { 131 synchronized (enqueuedClips) 132 { 112 133 enqueuedClips.add(new AudioClipInfo(ie)); 113 134 } 114 135 } 115 else { 136 else 137 { 116 138 ie.wavePlayed(); 117 139 } 118 } 119 120 /** 121 * Method called when user presses reset in simulation. All queued events need 122 * to be deQueued. 123 */ 124 public void deQueueAll() { 125 synchronized(enqueuedClips) { 140 } 141 142 /** 143 * Method called when user presses reset in simulation. All queued events 144 * need to be deQueued. 145 */ 146 public void deQueueAll() 147 { 148 synchronized (enqueuedClips) 149 { 126 150 enqueuedClips.clear(); 127 } 128 } 129 130 /** 151 } 152 } 153 154 /** 131 155 * Get the current audio enabled status. 132 156 * 133 157 * @return True if enabled, false if disabled. 134 158 */ 135 public boolean getAudioEnabled() { 159 public boolean getAudioEnabled() 160 { 136 161 return audioEnabled; 137 } 138 139 /** 140 * Set the current audio enabled status. A false value will cause all141 * enqueue events to be ignored. A currently playing audio file will be142 * stopped and requeued. All other queued events will remain in the queue.162 } 163 164 /** 165 * Set the current audio enabled status. A false value will cause all 166 * enqueue events to be ignored. A currently playing audio file will be 167 * stopped and requeued. All other queued events will remain in the queue. 143 168 * 144 169 * @param enable True if enabling, false if disabling. 145 170 */ 146 public void setAudioEnabled(boolean enable) { 147 148 if(!enable) { 171 public void setAudioEnabled(boolean enable) 172 { 173 174 if (!enable) 175 { 149 176 timer.cancel(); 150 AudioPlayer.player.stop(audioStream); 151 152 if(currentClip != null) { 177 if (clip != null) 178 { 179 clip.stop(); 180 } 181 if (mp3Player != null) 182 { 183 mp3Player.stop(); 184 } 185 186 if (currentClip != null) 187 { 153 188 enqueuedClips.add(0, currentClip); 154 } 155 189 } 190 156 191 audioPlaying = false; 157 192 } 158 193 159 194 audioEnabled = enable; 160 195 } 161 162 163 /** 164 * Method declaration for the Thread.run() method. While this thread is not 165 * interrupted, check if there are enqueued audio files. If so, remove the 166 * first one(FIFO) create an AudioClip object, and play the audio file. 167 * A timer tis then started for the duration of the audio file. When the 168 * timer expires the audioPlaying flag is reset to false and the corresponding 196 197 /** 198 * Method declaration for the Thread.run() method. While this thread is not 199 * interrupted, check if there are enqueued audio files. If so, remove the 200 * first one(FIFO) create an AudioClip object, and play the audio file. A 201 * timer tis then started for the duration of the audio file. When the timer 202 * expires the audioPlaying flag is reset to false and the corresponding 169 203 * incident event is notified of the audio file's completion. 170 204 */ 171 public void run() { 205 public void run() 206 { 172 207 AudioClipInfo theClip = null; 173 174 while(!isInterrupted()) { 175 176 synchronized(enqueuedClips) { 177 if(enqueuedClips.size() > 0 && !audioPlaying && audioEnabled) 178 theClip = enqueuedClips.remove(0); 179 } 180 208 209 while (!isInterrupted()) 210 { 211 212 synchronized (enqueuedClips) 213 { 214 if (enqueuedClips.size() > 0 && !audioPlaying && audioEnabled) 215 { 216 theClip = enqueuedClips.remove(0); 217 } 218 } 219 181 220 //if nothing to play, wait a second 182 if(theClip == null) { 183 try { Thread.sleep(1000);} catch (Exception e) {} 184 } 185 else { 186 187 try{ 188 189 if(theClip.duration > 0) { 190 191 File audioFile = new File(baseAudioDir + theClip.fileName); 192 193 if(audioFile.exists()) { 194 audioStream = new AudioStream (new FileInputStream(audioFile)); 195 AudioPlayer.player.start(audioStream); 196 } 197 else { 221 if (theClip == null) 222 { 223 try 224 { 225 Thread.sleep(1000); 226 } catch (Exception e) 227 { 228 } 229 } 230 else 231 { 232 233 try 234 { 235 236 if (theClip.duration > 0) 237 { 238 String filename = baseAudioDir + theClip.fileName; 239 String fileExt = filename.substring(filename.lastIndexOf('.')); 240 File audioFile = new File(filename); 241 242 if (audioFile.exists() && fileExt.equals(".mp3")) 243 { 244 mp3Player = new MP3Player(audioFile); 245 mp3Player.play(); 246 } 247 else if (audioFile.exists() && fileExt.equals(".wav")) 248 { 249 clip = Applet.newAudioClip(audioFile.toURI().toURL()); 250 clip.play(); 251 } 252 else 253 { 198 254 throw new Exception(); 199 } 255 } 200 256 } 201 202 currentClip = theClip;257 258 currentClip = theClip; 203 259 audioPlaying = true; 204 260 205 261 timer = new Timer(); 206 timer.schedule(new TimerTask () { 207 public void run() { 262 timer.schedule(new TimerTask() 263 { 264 public void run() 265 { 208 266 clipPlayed(); 209 audioPlaying = false; 267 audioPlaying = false; 210 268 } 211 269 }, theClip.duration * 1000); 212 213 theClip = null; 214 } 215 catch (Exception e){216 soundLogger.logp(Level.WARNING, "SoundPlayer", "run", 270 271 theClip = null; 272 } catch (Exception e) 273 { 274 soundLogger.logp(Level.WARNING, "SoundPlayer", "run", 217 275 "Unable to play audio file: " + baseAudioDir + theClip.fileName, e); 218 276 219 277 theClip.theEvent.wavePlayed(); 220 278 theClip = null; 221 279 timer.cancel(); 222 } 223 } 224 } 225 } 226 227 228 /** 229 * Called when audio file finishes playing. Notifies the corresponding 280 } 281 } 282 } 283 } 284 285 /** 286 * Called when audio file finishes playing. Notifies the corresponding 230 287 * Incident Event that its audio file has finished playing. 231 288 */ 232 private void clipPlayed() { 289 private void clipPlayed() 290 { 233 291 currentClip.wavePlayed(); 234 } 292 } 235 293 } -
trunk/test/tmcsim/cadsimulator/SoundPlayerTest.java
r16 r51 36 36 System.out.println("run"); 37 37 Vector dummy = new Vector(); 38 // the frog clip is actually 3 seconds long, but we specify clip duration of 2 39 // to demonstrate that the subsequent clip will start before the first one finishes. 40 // This shows that SoundPlayer assumes correct clip durations. 38 41 IncidentEvent evt1 = new IncidentEvent(0, null, "frog-croak.mp3", 2, dummy, dummy); 39 42 IncidentEvent evt2 = new IncidentEvent(0, null, "bikehorn.mp3", 1, dummy, dummy);
Note: See TracChangeset
for help on using the changeset viewer.
