Permalink
Browse files

Changed synchronization of Mixer

  • Loading branch information...
1 parent cb538a6 commit 121d5d61cd397ef9eca4be76bc35ab68e3b35060 @finnkuusisto committed Feb 29, 2012
@@ -2,7 +2,7 @@
<!--NewPage-->
<HTML>
<HEAD>
-<!-- Generated by javadoc (build 1.6.0_11) on Tue Feb 28 20:50:48 CST 2012 -->
+<!-- Generated by javadoc (build 1.6.0_11) on Tue Feb 28 21:38:16 CST 2012 -->
<TITLE>
All Classes
</TITLE>
@@ -2,7 +2,7 @@
<!--NewPage-->
<HTML>
<HEAD>
-<!-- Generated by javadoc (build 1.6.0_11) on Tue Feb 28 20:50:48 CST 2012 -->
+<!-- Generated by javadoc (build 1.6.0_11) on Tue Feb 28 21:38:16 CST 2012 -->
<TITLE>
All Classes
</TITLE>
View
@@ -2,7 +2,7 @@
<!--NewPage-->
<HTML>
<HEAD>
-<!-- Generated by javadoc (build 1.6.0_11) on Tue Feb 28 20:50:48 CST 2012 -->
+<!-- Generated by javadoc (build 1.6.0_11) on Tue Feb 28 21:38:16 CST 2012 -->
<TITLE>
Constant Field Values
</TITLE>
View
@@ -2,7 +2,7 @@
<!--NewPage-->
<HTML>
<HEAD>
-<!-- Generated by javadoc (build 1.6.0_11) on Tue Feb 28 20:50:48 CST 2012 -->
+<!-- Generated by javadoc (build 1.6.0_11) on Tue Feb 28 21:38:16 CST 2012 -->
<TITLE>
Deprecated List
</TITLE>
View
@@ -2,7 +2,7 @@
<!--NewPage-->
<HTML>
<HEAD>
-<!-- Generated by javadoc (build 1.6.0_11) on Tue Feb 28 20:50:48 CST 2012 -->
+<!-- Generated by javadoc (build 1.6.0_11) on Tue Feb 28 21:38:16 CST 2012 -->
<TITLE>
API Help
</TITLE>
View
@@ -2,7 +2,7 @@
<!--NewPage-->
<HTML>
<HEAD>
-<!-- Generated by javadoc (build 1.6.0_11) on Tue Feb 28 20:50:48 CST 2012 -->
+<!-- Generated by javadoc (build 1.6.0_11) on Tue Feb 28 21:38:16 CST 2012 -->
<TITLE>
Index
</TITLE>
View
@@ -2,7 +2,7 @@
<!--NewPage-->
<HTML>
<HEAD>
-<!-- Generated by javadoc on Tue Feb 28 20:50:48 CST 2012-->
+<!-- Generated by javadoc on Tue Feb 28 21:38:16 CST 2012-->
<TITLE>
Generated Documentation (Untitled)
</TITLE>
@@ -2,7 +2,7 @@
<!--NewPage-->
<HTML>
<HEAD>
-<!-- Generated by javadoc (build 1.6.0_11) on Tue Feb 28 20:50:48 CST 2012 -->
+<!-- Generated by javadoc (build 1.6.0_11) on Tue Feb 28 21:38:16 CST 2012 -->
<TITLE>
Music
</TITLE>
@@ -2,7 +2,7 @@
<!--NewPage-->
<HTML>
<HEAD>
-<!-- Generated by javadoc (build 1.6.0_11) on Tue Feb 28 20:50:48 CST 2012 -->
+<!-- Generated by javadoc (build 1.6.0_11) on Tue Feb 28 21:38:16 CST 2012 -->
<TITLE>
Sound
</TITLE>
@@ -2,7 +2,7 @@
<!--NewPage-->
<HTML>
<HEAD>
-<!-- Generated by javadoc (build 1.6.0_11) on Tue Feb 28 20:50:48 CST 2012 -->
+<!-- Generated by javadoc (build 1.6.0_11) on Tue Feb 28 21:38:16 CST 2012 -->
<TITLE>
TinySound
</TITLE>
@@ -2,7 +2,7 @@
<!--NewPage-->
<HTML>
<HEAD>
-<!-- Generated by javadoc (build 1.6.0_11) on Tue Feb 28 20:50:48 CST 2012 -->
+<!-- Generated by javadoc (build 1.6.0_11) on Tue Feb 28 21:38:16 CST 2012 -->
<TITLE>
Mixer
</TITLE>
@@ -2,7 +2,7 @@
<!--NewPage-->
<HTML>
<HEAD>
-<!-- Generated by javadoc (build 1.6.0_11) on Tue Feb 28 20:50:48 CST 2012 -->
+<!-- Generated by javadoc (build 1.6.0_11) on Tue Feb 28 21:38:16 CST 2012 -->
<TITLE>
MusicReference
</TITLE>
@@ -2,7 +2,7 @@
<!--NewPage-->
<HTML>
<HEAD>
-<!-- Generated by javadoc (build 1.6.0_11) on Tue Feb 28 20:50:48 CST 2012 -->
+<!-- Generated by javadoc (build 1.6.0_11) on Tue Feb 28 21:38:16 CST 2012 -->
<TITLE>
SoundReference
</TITLE>
@@ -2,7 +2,7 @@
<!--NewPage-->
<HTML>
<HEAD>
-<!-- Generated by javadoc (build 1.6.0_11) on Tue Feb 28 20:50:48 CST 2012 -->
+<!-- Generated by javadoc (build 1.6.0_11) on Tue Feb 28 21:38:16 CST 2012 -->
<TITLE>
UpdateRunner
</TITLE>
@@ -2,7 +2,7 @@
<!--NewPage-->
<HTML>
<HEAD>
-<!-- Generated by javadoc (build 1.6.0_11) on Tue Feb 28 20:50:48 CST 2012 -->
+<!-- Generated by javadoc (build 1.6.0_11) on Tue Feb 28 21:38:16 CST 2012 -->
<TITLE>
kuusisto.tinysound.internal
</TITLE>
@@ -2,7 +2,7 @@
<!--NewPage-->
<HTML>
<HEAD>
-<!-- Generated by javadoc (build 1.6.0_11) on Tue Feb 28 20:50:48 CST 2012 -->
+<!-- Generated by javadoc (build 1.6.0_11) on Tue Feb 28 21:38:16 CST 2012 -->
<TITLE>
kuusisto.tinysound.internal
</TITLE>
@@ -2,7 +2,7 @@
<!--NewPage-->
<HTML>
<HEAD>
-<!-- Generated by javadoc (build 1.6.0_11) on Tue Feb 28 20:50:48 CST 2012 -->
+<!-- Generated by javadoc (build 1.6.0_11) on Tue Feb 28 21:38:16 CST 2012 -->
<TITLE>
kuusisto.tinysound.internal Class Hierarchy
</TITLE>
@@ -2,7 +2,7 @@
<!--NewPage-->
<HTML>
<HEAD>
-<!-- Generated by javadoc (build 1.6.0_11) on Tue Feb 28 20:50:48 CST 2012 -->
+<!-- Generated by javadoc (build 1.6.0_11) on Tue Feb 28 21:38:16 CST 2012 -->
<TITLE>
kuusisto.tinysound
</TITLE>
@@ -2,7 +2,7 @@
<!--NewPage-->
<HTML>
<HEAD>
-<!-- Generated by javadoc (build 1.6.0_11) on Tue Feb 28 20:50:48 CST 2012 -->
+<!-- Generated by javadoc (build 1.6.0_11) on Tue Feb 28 21:38:16 CST 2012 -->
<TITLE>
kuusisto.tinysound
</TITLE>
@@ -2,7 +2,7 @@
<!--NewPage-->
<HTML>
<HEAD>
-<!-- Generated by javadoc (build 1.6.0_11) on Tue Feb 28 20:50:48 CST 2012 -->
+<!-- Generated by javadoc (build 1.6.0_11) on Tue Feb 28 21:38:16 CST 2012 -->
<TITLE>
kuusisto.tinysound Class Hierarchy
</TITLE>
View
@@ -2,7 +2,7 @@
<!--NewPage-->
<HTML>
<HEAD>
-<!-- Generated by javadoc (build 1.6.0_11) on Tue Feb 28 20:50:48 CST 2012 -->
+<!-- Generated by javadoc (build 1.6.0_11) on Tue Feb 28 21:38:16 CST 2012 -->
<TITLE>
Overview List
</TITLE>
@@ -2,7 +2,7 @@
<!--NewPage-->
<HTML>
<HEAD>
-<!-- Generated by javadoc (build 1.6.0_11) on Tue Feb 28 20:50:48 CST 2012 -->
+<!-- Generated by javadoc (build 1.6.0_11) on Tue Feb 28 21:38:16 CST 2012 -->
<TITLE>
Overview
</TITLE>
View
@@ -2,7 +2,7 @@
<!--NewPage-->
<HTML>
<HEAD>
-<!-- Generated by javadoc (build 1.6.0_11) on Tue Feb 28 20:50:48 CST 2012 -->
+<!-- Generated by javadoc (build 1.6.0_11) on Tue Feb 28 21:38:16 CST 2012 -->
<TITLE>
Class Hierarchy
</TITLE>
@@ -149,8 +149,10 @@ public static void init(int updateRate, boolean autoUpdate) {
TinySound.autoUpdate = autoUpdate;
if (autoUpdate) {
TinySound.autoUpdater = new UpdateRunner(updateRate);
+ Thread updateThread = new Thread(TinySound.autoUpdater);
+ updateThread.setDaemon(true);
TinySound.inited = true;
- new Thread(TinySound.autoUpdater).start();
+ updateThread.start();
}
TinySound.inited = true;
}
@@ -54,63 +54,51 @@ public Mixer() {
* Registers a MusicReference with this Mixer.
* @param music MusicReference to be registered
*/
- public void registerMusicReference(MusicReference music) {
- synchronized (this.musics) {
- this.musics.add(music);
- }
+ public synchronized void registerMusicReference(MusicReference music) {
+ this.musics.add(music);
}
/**
* Registers a SoundReference with this Mixer.
* @param sound SoundReference to be registered
*/
- public void registerSoundReference(SoundReference sound) {
- synchronized (this.sounds) {
- this.sounds.add(sound);
- }
+ public synchronized void registerSoundReference(SoundReference sound) {
+ this.sounds.add(sound);
}
/**
* Unregisters a MusicReference with this Mixer.
* @param music MusicReference to be unregistered
*/
- public void unRegisterMusicReference(MusicReference music) {
- synchronized (this.musics) {
- this.musics.remove(music);
- }
+ public synchronized void unRegisterMusicReference(MusicReference music) {
+ this.musics.remove(music);
}
/**
* Unregisters all SoundReferences with a given soundID.
* @param soundID ID of SoundReferences to be unregistered
*/
- public void unRegisterSoundReference(int soundID) {
- synchronized (this.sounds) {
- //removal working backward is easier
- for (int i = this.sounds.size() - 1; i >= 0; i--) {
- if (this.sounds.get(i).SOUND_ID == soundID) {
- this.sounds.remove(i);
- }
+ public synchronized void unRegisterSoundReference(int soundID) {
+ //removal working backward is easier
+ for (int i = this.sounds.size() - 1; i >= 0; i--) {
+ if (this.sounds.get(i).SOUND_ID == soundID) {
+ this.sounds.remove(i);
}
}
}
/**
* Unregister all Music registered with this Mixer.
*/
- public void clearMusic() {
- synchronized (this.musics) {
- this.musics.clear();
- }
+ public synchronized void clearMusic() {
+ this.musics.clear();
}
/**
* Unregister all Sounds registered with this Mixer.
*/
- public void clearSounds() {
- synchronized (this.sounds) {
- this.sounds.clear();
- }
+ public synchronized void clearSounds() {
+ this.sounds.clear();
}
/**
@@ -120,7 +108,7 @@ public void clearSounds() {
* @param length the maximum number of bytes that should be read
* @return number of bytes read into buffer
*/
- public int read(byte[] data, int offset, int length) {
+ public synchronized int read(byte[] data, int offset, int length) {
//*********************************************//
//assume big-endian, stereo, 16-bit, signed PCM//
//*********************************************//
@@ -136,43 +124,39 @@ public int read(byte[] data, int offset, int length) {
double leftValue = 0.0;
double rightValue = 0.0;
//go through all the music first
- synchronized (this.musics) {
- for (int m = 0; m < this.musics.size(); m++) {
- MusicReference music = this.musics.get(m);
- //is the music playing and are there bytes available
- if (music.getPlaying() && music.bytesAvailable() > 0) {
- //add this music to the mix by volume
- music.nextTwoBytes(this.dataBuf, true);
- double volume = music.getVolume();
- leftValue += (this.dataBuf[0] * volume);
- rightValue += (this.dataBuf[1] * volume);
- //we know we aren't done yet now
- bytesRead = true;
- }
+ for (int m = 0; m < this.musics.size(); m++) {
+ MusicReference music = this.musics.get(m);
+ //is the music playing and are there bytes available
+ if (music.getPlaying() && music.bytesAvailable() > 0) {
+ //add this music to the mix by volume
+ music.nextTwoBytes(this.dataBuf, true);
+ double volume = music.getVolume();
+ leftValue += (this.dataBuf[0] * volume);
+ rightValue += (this.dataBuf[1] * volume);
+ //we know we aren't done yet now
+ bytesRead = true;
}
}
//then go through all the sounds (backwards to remove completed)
- synchronized (this.sounds) {
- for (int s = this.sounds.size() - 1; s >= 0; s--) {
- SoundReference sound = this.sounds.get(s);
- //are there bytes available
- if (sound.bytesAvailable() > 0) {
- //add this sound to the mix by volume
- sound.nextTwoBytes(this.dataBuf, true);
- double volume = sound.getVolume();
- leftValue += (this.dataBuf[0] * volume);
- rightValue += (this.dataBuf[1] * volume);
- //we know we aren't done yet now
- bytesRead = true;
- //remove the reference if done
- if (sound.bytesAvailable() <= 0) {
- this.sounds.remove(s);
- }
- }
- else { //otherwise remove this reference
+ for (int s = this.sounds.size() - 1; s >= 0; s--) {
+ SoundReference sound = this.sounds.get(s);
+ //are there bytes available
+ if (sound.bytesAvailable() > 0) {
+ //add this sound to the mix by volume
+ sound.nextTwoBytes(this.dataBuf, true);
+ double volume = sound.getVolume();
+ leftValue += (this.dataBuf[0] * volume);
+ rightValue += (this.dataBuf[1] * volume);
+ //we know we aren't done yet now
+ bytesRead = true;
+ //remove the reference if done
+ if (sound.bytesAvailable() <= 0) {
this.sounds.remove(s);
}
}
+ else { //otherwise remove this reference
+ this.sounds.remove(s);
+ }
}
//if we actually read bytes, store in the buffer
if (bytesRead) {

0 comments on commit 121d5d6

Please sign in to comment.