JDK-4715722 : Sequencer setTickPosition and getTickPosition; positions don't match
  • Type: Bug
  • Component: client-libs
  • Sub-Component: javax.sound
  • Affected Version: 2.1
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: windows_xp
  • CPU: x86
  • Submitted: 2002-07-16
  • Updated: 2002-08-08
  • Resolved: 2002-08-08
Related Reports
Duplicate :  
Description

Name: jk109818			Date: 07/16/2002


FULL PRODUCT VERSION :
java version "1.4.0"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0-b92)
Java HotSpot(TM) Client VM (build 1.4.0-b92, mixed mode)

FULL OPERATING SYSTEM VERSION :
Microsoft Windows XP [Version 5.1.2600]


A DESCRIPTION OF THE PROBLEM :
If you record a sequencer's position with a call to
Sequencer.getTickPosition() and later try to reset the
sequencer to that same position it doesn't work.

You have to actually divide the recorded position by 64 to
achieve the desired result. e.g.

sequencer.setTickPosition(loopStart/64);

A related issue may be that sequencer.getTickLength()
returns a value much longer than the sequence will report
if you call getTickPosition when the sequence is at the
end.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
1.Play a midi sequence.
2.At some point record the position with
sequencer.getTickPosition();
3.Attempt to restore the sequencer to that position with
sequencer.setTickPosition();

EXPECTED VERSUS ACTUAL BEHAVIOR :
What I expected was for the sequencer to reset to the
start of a loop.
What happened was that the sequencer went to the end of
the sequence.

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
import java.io.File;
import java.util.Vector;
import javax.sound.midi.*;

public class Playback{


	public static void main(String[] args) throws Exception {

		if (args.length != 1){
			System.out.println("usage java Playback midifile");
			System.exit(0);
		}

		Sequencer sequencer = MidiSystem.getSequencer();
		File myMidiFile = new File(args[0]);
		sequencer.open();
		Sequence mySeq = MidiSystem.getSequence(myMidiFile);
		sequencer.setSequence(mySeq);

		Synthesizer synth = (Synthesizer) sequencer; //
MidiSystem.getSynthesizer();

		Instrument[] instruments = null; //
synth.getAvailableInstruments();
		int instrumentIndex = 40;
		synth.open();
		Soundbank sb = synth.getDefaultSoundbank();
		if (sb != null) {
			instruments = sb.getInstruments();
			synth.loadInstrument(instruments[instrumentIndex]);
		}

		MidiChannel[] channels = synth.getChannels();

		for(int i = 0; i < channels.length; i++){
			channels[i].programChange(instruments
[instrumentIndex].getPatch().getProgram());
		}

		sequencer.start();
		Thread.sleep(2000);
		long position = sequencer.getTickPosition();
		System.out.println("Saved Position: " + String.valueOf
(position));
		Thread.sleep(2000);

		sequencer.stop();

		while(sequencer.isRunning()){;} //wait for it to stop.

		sequencer.setTickPosition(position);
		// Workaround: divide by 64
		//sequencer.setTickPosition(position/64);  

		sequencer.start();
		System.out.println("Reset Position: " + String.valueOf
(sequencer.getTickPosition()));

		long endPosn = 0;
		while(sequencer.isRunning()) endPosn =
sequencer.getTickPosition();
		System.out.println("End Position: " + String.valueOf(endPosn)
+ " of " + String.valueOf(sequencer.getTickLength()));
		System.exit(0);
	}

}
---------- END SOURCE ----------

CUSTOMER WORKAROUND :
divide the desired position by 64 before setting it.
(Review ID: 159259) 
======================================================================