import java.applet.Applet; import java.applet.AudioClip; import java.awt.Color; public class hbeat extends Applet { private juke juke_box; public void init() { setBackground(new Color(Integer.parseInt(getParameter("back_color"), 16))); int num_clips = 0; while (getParameter ("sound_loop_" + num_clips++) != null ) {} AudioClip clips[] = new AudioClip [num_clips]; int play_time[] = new int [num_clips]; clips [0] = getAudioClip (getDocumentBase(), getParameter ("heartbeat")); play_time [0] = -1; for (int i = 0; i < num_clips - 1; i++) { clips [i + 1] = getAudioClip (getDocumentBase(), getParameter ("sound_loop_" + i)); play_time [i + 1] = 1000000; } juke_box = new juke (clips, play_time); } public void beat () { juke_box.playSound (0, - 1); } public void stop () { juke_box.killThread(); } } /* ***************************** A KIND OF SEQUENCER ******************************* */ class juke extends Thread { private AudioClip [] clips; private long play_time[]; private boolean alive; public juke ( AudioClip clips_ [], int ai[] ) { super (); clips = new AudioClip [clips_.length]; play_time = new long [clips.length]; alive = true; for (int i = 0; i < clips.length; i++) { clips [i] = clips_[i]; playSound(i, ai[i]); } start (); } /* how_long > 0 --> loop clip for how_long milliseconds how_long = 0 --> stop clip how_long < 0 --> play clip once */ public void playSound(int which, long how_long) { if(alive) { if (which < 0 || which >= clips.length) return; if (how_long > 0) { play_time[which] = System.currentTimeMillis() + how_long; clips[which].stop (); clips[which].loop (); } else if (how_long < 0) { play_time[which] = -1L; clips[which].stop (); clips[which].play (); } else { play_time[which] = 0L; clips[which].stop (); } } } public void run() { while (alive) { long time = System.currentTimeMillis(); for(int j = 0; j < clips.length; j++) if ( play_time[j] < time && play_time[j] > 0 ) { play_time[j] = 0; clips[j].stop(); } try { sleep (100L); } catch(Throwable t) { killThread(); } } for(int i = 0; i < clips.length; i++) clips[i].stop(); } public void killThread () { alive = false; } }