This discussion is archived
1 Reply Latest reply: Jul 12, 2011 12:28 PM by captfoss RSS

Sync and reliability problems with audio sequencer

874819 Newbie
Currently Being Moderated
I am currently working on a rhythm box that handles both midi instruments and audio samples, i.e. each track is created as a midi track, but when a track needs to play audio samples instead of midi instruments, I add Control Change Messages to the midi track instead of note-on, note-off messages.

I create each clip in the standard way:
public Clip CreateClip(File audioFile)
{
   Clip clip;
   AudioInputStream ais;
   ais = AudioSystem.getAudioInputStream(audioFile);
   AudioFormat format = ais.getFormat();
   DataLine.Info info = new DataLine.Info(Clip.class, ais.getFormat(), ((int) ais.getFrameLength() * format.getFrameSize()));
   clip = (Clip) AudioSystem.getLine(info);
   clip.open(ais);     
   return clip;
}
Once a ControlChange event is thrown, I do the following:
   clip.stop();
   clip.setFramePosition(0);
   clip.start();
I tried working on one clip per track and also with having a clip for each beat in the track. And the latter seems to work more reliably, but in both cases I run into the following problems:

1 - The control Change event (which activates my clip) seems to get fired before the midi note-on events, resulting in both the midi click track and the clip being out of sync.
2 - the clip does not always fire, i.e. once every 12 notes or so, it skips a beat.

Does anyone have any ideas regarding this?

thanks

Edited by: 871816 on 12-Jul-2011 00:51
  • 1. Re: Sync and reliability problems with audio sequencer
    captfoss Pro
    Currently Being Moderated
    871816 wrote:
    1 - The control Change event (which activates my clip) seems to get fired before the midi note-on events, resulting in both the midi click track and the clip being out of sync.
    Control change events are, as I recall, higher priority messages and they need to be finished before the next note plays. You wouldn't want the first note after an instrument change to be the old instrument, or the first note to be really loud after you've decreased the volume.
    2 - the clip does not always fire, i.e. once every 12 notes or so, it skips a beat.
    I believe that clips will refuse to fire if you're lacking system resources.

    For your application, I believe you'd be better off loading the various audio samples into a series of byte[] and playing them manually.

    http://www.jsresources.org/examples/SimpleAudioPlayer.java.html

    You'd simply want to call "line.write" with the appropriate byte[] for the desired sample everytime you want to play anything and it should work more reliably than starting a clip.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points