import javax.sound.sampled.*; import java.io.*; public class Beeper implements LineListener { boolean playing = false; // if the file actually playing or not float _interval = 1.0; // interval set by user using the slider float _real_intv = _interval; // measured interval float prev_time = millis(); // to measure interval Clip audioclip; // Java Sound API ClipLoader loader; public Beeper(String filepath){ loader = new ClipLoader(this, filepath); loader.start(); } public void beat(){ play(); // Measure Interval float now = millis(); _real_intv = now - prev_time; prev_time = now; } void speedSlider(float value){ _interval = value; println("beat interval: " + _interval); clock.setInterval(_interval); // set new interval value } private void play(){ if (audioclip != null){ println("start"); audioclip.start(); playing = true; } } void update(LineEvent event){ if (event.getType() == LineEvent.Type.STOP){ println("stop"); audioclip.stop(); playing = false; } } public void stop() { audioclip.stop(); } class ClipLoader extends Thread { Beeper delegate; String filepath; boolean loaded = false; public ClipLoader(Beeper obj, String filepath){ setDaemon(true); delegate = obj; this.filepath = filepath; } public void run(){ try{ loaded = loadClip(filepath); } catch(Exception e){ println(e.toString()); } if (!loaded) println("Couldn't load a sound file: " + filepath); } private boolean loadClip(String path){ try{ // 1. access the audio file as a stream AudioInputStream stream; if (online){ stream = AudioSystem.getAudioInputStream( getClass().getResource("data/" + path) ); } else{ stream = AudioSystem.getAudioInputStream( new File(dataPath(path))); } // stream = new AudioInputStream(openStream(filepath) );//AudioSystem.getAudioInputStream( openStream(filepath) ); if (stream == null){ println("Error in openStream()"); return false; } // 2. Get the original audio format of the file and convert it to a suitable format (if necessary) AudioFormat format = stream.getFormat(); if ( (format.getEncoding() == AudioFormat.Encoding.ULAW) || (format.getEncoding() == AudioFormat.Encoding.ALAW)) { AudioFormat newFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, format.getSampleRate(), format.getSampleSizeInBits()*2, format.getChannels(), format.getFrameSize()*2, format.getFrameRate(), true); // big endian stream = AudioSystem.getAudioInputStream(newFormat, stream); println("Convert audio fomat: " + newFormat); format = newFormat; } // 3. Gather information for line creation DataLine.Info info = new DataLine.Info(Class.forName("javax.sound.sampled.Clip"), format); // 4. Check if the DataLine format is supported or not on this system if (!AudioSystem.isLineSupported(info)){ println("Unsupported Clip File: " + path); return false; } // 5. Make Empty clip audioclip = (Clip)AudioSystem.getLine(info); // 6. Start monitoring line event audioclip.addLineListener(delegate); // 7. Open the InputStream audioclip.open(stream); stream.close(); // done with input stream // Get clip duration double duration = audioclip.getMicrosecondLength() / 1000000.0; // duration in sec println("Duration: " + duration); // Set flag return true; } catch (Exception e){ println(e.toString()); return false; } } } }