Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Clean up sound code a bit more

  • Loading branch information...
commit e0437affec7b74465f899b53efce4c02836c6d8e 1 parent dce4091
@djbe authored
View
38 src/com/davidjennes/ElectroJam/ScheduledSound.java
@@ -0,0 +1,38 @@
+package com.davidjennes.ElectroJam;
+
+/**
+ * Used for scheduling sound on next timer beat
+ */
+class ScheduledSound {
+ private int m_skipped;
+ private Sound m_sound;
+
+ /**
+ * Constructor
+ * @param aID
+ * @param aLooped
+ */
+ public ScheduledSound(Sound sound) {
+ m_sound = sound;
+ m_skipped = -1;
+ }
+
+ /**
+ * Timer beat, update skip counter and play if we are in sync
+ * @param beats The number of timer beats (which we sync to)
+ */
+ public void skipBeat(int beats) {
+ // increase skip counter only when we're in sync with the beats counter
+ // once in sync, the skip counter will increase with a modulo to the skip limit
+ if (m_skipped > -1 || (m_skipped == -1 && (beats % m_sound.skipLimit) == 0))
+ m_skipped = (m_skipped + 1) % m_sound.skipLimit;
+
+ // update progress bar
+ if (m_skipped > -1)
+ m_sound.setProgress(m_skipped);
+
+ // play on sync with beat timer
+ if (m_skipped == 0)
+ m_sound.play();
+ }
+}
View
45 src/com/davidjennes/ElectroJam/Sound.java
@@ -16,7 +16,7 @@
public final static int SAMPLE_LENGTH = 1875;
private MediaPlayer m_mp;
- public ProgressBar progressBar;
+ public ProgressBar m_progressBar;
public int id, skipLimit;
/**
@@ -28,7 +28,7 @@ public Sound(int newID, Context context, int resid) {
m_mp = create(context, resid);
id = newID;
- progressBar = null;
+ m_progressBar = null;
skipLimit = (int) m_mp.getDuration() / SAMPLE_LENGTH;
}
@@ -49,13 +49,12 @@ protected void finalize() throws Throwable {
*/
public void play() {
try {
- // start new one
- if (m_mp.isPlaying()) {
- m_mp.pause();
- m_mp.seekTo(0);
- }
+ // reset to beginning
+ m_mp.seekTo(0);
- m_mp.start();
+ // start again if needed
+ if (!m_mp.isPlaying())
+ m_mp.start();
} catch (IllegalStateException e) {
e.printStackTrace();
}
@@ -67,12 +66,13 @@ public void play() {
public void stop() {
try {
// stop player (and prepare it again)
- m_mp.pause();
+ if (m_mp.isPlaying())
+ m_mp.pause();
m_mp.seekTo(0);
// reset progress bar
- if (progressBar != null)
- progressBar.setProgress(0);
+ if (m_progressBar != null)
+ m_progressBar.setProgress(0);
} catch (IllegalStateException e) {
e.printStackTrace();
}
@@ -86,9 +86,22 @@ public boolean isPlaying() {
return m_mp.isPlaying();
}
+ /**
+ * Connect a ProgressBar to this sound
+ * @param bar The new progress bar
+ */
public void setProgressBar(ProgressBar bar) {
- progressBar = bar;
- progressBar.setSecondaryProgress(skipLimit * 25);
+ m_progressBar = bar;
+ m_progressBar.setSecondaryProgress(skipLimit * 25);
+ }
+
+ /**
+ * Show the specified progress on the progress bar associated with this sound
+ * @param progress The new progress to set to (in skips, from 0 to skip limit)
+ */
+ public void setProgress(int progress) {
+ progress = (progress < skipLimit) ? progress + 1 : skipLimit;
+ m_progressBar.setProgress(progress * 25);
}
/**
@@ -99,14 +112,12 @@ public void setProgressBar(ProgressBar bar) {
*/
private MediaPlayer create(Context context, int resid) {
try {
- AssetFileDescriptor afd = context.getResources().openRawResourceFd(
- resid);
+ AssetFileDescriptor afd = context.getResources().openRawResourceFd(resid);
if (afd == null)
return null;
MediaPlayer mp = new MediaPlayer();
- mp.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(),
- afd.getLength());
+ mp.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
afd.close();
mp.prepare();
View
73 src/com/davidjennes/ElectroJam/SoundManager.java
@@ -83,18 +83,14 @@ public void playSound(int id, boolean looped) {
if (looped)
synchronized (m_soundQueue) {
if (!m_soundQueueMap.containsKey(id)) {
- ScheduledSound schedule = new ScheduledSound(id, looped);
+ ScheduledSound schedule = new ScheduledSound(m_sounds.get(id));
m_soundQueue.add(schedule);
m_soundQueueMap.put(id, schedule);
}
}
-
- // otherwise play directly (if not already playing)
- else {
- Sound sound = m_sounds.get(id);
- if (!sound.isPlaying())
- sound.play();
- }
+ // otherwise play directly
+ else
+ m_sounds.get(id).play();
}
/**
@@ -103,15 +99,16 @@ public void playSound(int id, boolean looped) {
*/
public void stopSound(int id) {
synchronized (m_soundQueue) {
- if (m_sounds.containsKey(id))
- m_sounds.get(id).stop();
-
+ // Stop looping if it is
if (m_soundQueueMap.containsKey(id)) {
ScheduledSound schedule = m_soundQueueMap.get(id);
-
m_soundQueue.remove(schedule);
m_soundQueueMap.remove(id);
}
+
+ // Actually stop playing
+ if (m_sounds.containsKey(id))
+ m_sounds.get(id).stop();
}
}
@@ -147,56 +144,8 @@ public void run() {
}
// play each scheduled sound
- for (ScheduledSound schedule : soundQueue) {
- Sound sound = m_sounds.containsKey(schedule.id) ? m_sounds.get(schedule.id) : null;
- if (sound == null)
- continue;
-
- // keep track of skip count, connected to beats counter
- if (schedule.getSkipped() != -1 || (schedule.getSkipped() == -1 && (m_beats % sound.skipLimit) == 0))
- schedule.skip();
-
- // play only when skip limit is reached
- if (schedule.getSkipped() == 0)
- sound.play();
- }
- }
- }
-
- /**
- * Used for scheduling sound until next timer beat
- */
- class ScheduledSound {
- private int m_skipped;
- public int id;
- public boolean looped;
-
- public ScheduledSound(int i, boolean b) {
- id = i;
- looped = b;
- m_skipped = -1;
- }
-
- /**
- * Check how many times we've been skipped
- * @return The number of skip-times
- */
- public int getSkipped() {
- return m_skipped;
- }
-
- /**
- * Update the skip count
- * @param limit Limit the skip count, past which it'll be reset to 0
- */
- public void skip() {
- Sound sound = m_sounds.get(id);
-
- // update skip counter
- m_skipped = (m_skipped + 1) % sound.skipLimit;
-
- // update progress bar
- sound.progressBar.setProgress((m_skipped + 1) * 25);
+ for (ScheduledSound schedule : soundQueue)
+ schedule.skipBeat(m_beats);
}
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.