Jump to content

JavasCRIPT MIDI interpretter


Ingolme

Recommended Posts

I've been developing a MIDI interpretter in Javascript. It graphically displays what's being played in each channel on a set of 16 keyboards. It works best on Firefox, but it works OK in other browsers. Opera seems to play the MIDIs slower than it should.Watch your volume settings before using it.Here's the link to it: http://fierce-wolf.com/MIDI/It has a few problems though, that I have trouble solving:

  • The songs eventually delay, due to error margin in Javascript's setInterval function
  • Some time values translate to floating point numbers in milliseconds, Javascript's setInterval doesn't read floating point numbers, so this will cause some notes to be shorter or longer than they should be.
  • Tempo changes aren't handled perfetly, because after the tempo has changed, if setInterval was running from before, it won't reduce the amount of remaining time for the next note to play.
  • The songs play slower in Opera for some reason
  • Internet Explorer displays the Java applet while the MIDI is still being processed by Javascript (CSS problems that I could solve eventually).
  • Don't reload the page without pressing "Stop" first, the Java application keeps playing the MIDI file to the end, and it can't be stopped.
  • The Java MIDI sound player sometimes won't start, just randomly, I heard it has something to do with how applets work loading init() before start() or something. I'm not an expert in Java, so if somebody would like to help me I'm posting the source code here

If anybody wants to help me improve my Java applet (I'm not a real Java programmer, I just put this together from help I got on the internet) here's the code:

import java.io.*;import java.net.*;import java.awt.Graphics;import java.awt.Button;import java.awt.Color;import java.awt.Frame;import java.awt.event.ActionListener;import java.awt.event.ActionEvent;import java.applet.Applet;import javax.sound.midi.*;public class MIDI extends Applet implements ActionListener, MetaEventListener {	private Button playBtn;  private Button stopBtn;  private Sequence sequence;  private Sequencer sequencer;		/**	* Initialize the applet. First resize it, then get the	 * "snd" attribute.	 */	public void init() {    try {    while(sequence == null) {      sequence = MidiSystem.getSequence(new URL(getParameter("file")));    }    while(sequencer == null) {      sequencer = MidiSystem.getSequencer();    }    sequencer.open();    sequencer.addMetaEventListener(this);    sequencer.setSequence(sequence);    } catch ( Exception e ) { }		playBtn = new Button("Play");		playBtn.addActionListener(this);		this.add(playBtn);		stopBtn = new Button("Stop");		stopBtn.addActionListener(this);		this.add(stopBtn);  }	/**	* When the applet is started play the next sound.	 */	public void start() {	}	/**	* Deal with the mouse click.	 */	public void actionPerformed( ActionEvent e ) {		if ( e.getSource() == playBtn) {			play();      try { getAppletContext().showDocument(new URL("java script:play();void(0)")); } catch ( Exception err ) { }		}		if ( e.getSource() == stopBtn) {      try { getAppletContext().showDocument(new URL("java script:stop();void(0)")); } catch ( Exception er ) { }			stopMyMusic();		}	}	/**    * Play the score      */	private void play() {    if(sequencer != null) {      sequencer.start();    }	}	private void stopMyMusic() {    if(sequencer != null) {      sequencer.stop();      sequencer.setTickPosition(0);    }	}  public void meta(MetaMessage event) {    if (event.getType() == 47) {      if (sequencer != null && sequencer.isOpen()) {        sequencer.stop();        sequencer.setTickPosition(0);      }    }  }	public static void main(String[] args) {		Applet theApplet = new MIDI();		Frame theFrame = new Frame();		theFrame.setSize(100,100);		theFrame.add(theApplet);;		theApplet.init();		theApplet.start();		theFrame.setVisible(true);	}}

Link to comment
Share on other sites

I don't hear anything... I'll go ask the Ubuntu gurus about it.I know Java, although I'm probably a bit rusty by now. Where did you see the thing about start and init? (I vaguely recall such an issue, but I need to read up on it to address it.)I haven't looked at your implementation yet, but could you move the setInterval management to Java? I think it should be tuned finer than JS, and it might support floating points. EDIT: See http://java.sun.com/javase/6/docs/api/java/util/Timer.html.Couldn't JS stop the MIDI onunload?

Link to comment
Share on other sites

I don't hear anything... I'll go ask the Ubuntu gurus about it.I know Java, although I'm probably a bit rusty by now. Where did you see the thing about start and init? (I vaguely recall such an issue, but I need to read up on it to address it.)I haven't looked at your implementation yet, but could you move the setInterval management to Java? I think it should be tuned finer than JS, and it might support floating points. EDIT: See http://java.sun.com/javase/6/docs/api/java/util/Timer.html.Couldn't JS stop the MIDI onunload?
I'll explain what it does:The Java applet just plays the music, and nothing else. Unfortunately, it doesn't work well.Javascript reads information from the MIDI file and interprets it all on its own, I programmed all this in Javascript, I just used Java because Javascript doesn't play music. So I get Java to play the song and call Javascript's play() method at the same time.I just updated it so that you can display the MIDI if Java doesn't work, it just won't have sound.
Link to comment
Share on other sites

  • 2 weeks later...

*slaps self* I'm very sorry I completely forgot about this. I depend on email notifications for my subscriptions, and I tend to open a ton of tabs at once. So this was one among many tabs I had open and then I swept it away as I was cleaning up to go to bed.Does the Stop button stop the music or just the animation? If both, just set window.onunload to stop. If only the animation, do you know yet how to stop the music?Are you satisfied with JS's setInterval accuracy?To reiterate...

Where did you see the thing about start and init? (I vaguely recall such an issue, but I need to read up on it to address it.)
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...