javax.sound.midi.Track Java Examples

The following examples show how to use javax.sound.midi.Track. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: StandardMidiFileWriter.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Obtains the file types that this provider can write from the
 * sequence specified.
 * @param sequence the sequence for which midi file type support
 * is queried
 * @return array of file types.  If no file types are supported,
 * returns an array of length 0.
 */
public int[] getMidiFileTypes(Sequence sequence){
    int typesArray[];
    Track tracks[] = sequence.getTracks();

    if( tracks.length==1 ) {
        typesArray = new int[2];
        typesArray[0] = MIDI_TYPE_0;
        typesArray[1] = MIDI_TYPE_1;
    } else {
        typesArray = new int[1];
        typesArray[0] = MIDI_TYPE_1;
    }

    return typesArray;
}
 
Example #2
Source File: MusReader.java    From mochadoom with GNU General Public License v3.0 6 votes vote down vote up
/** Create a sequence from an InputStream.
 *  This is the counterpart of {@link MidiSystem#getSequence(InputStream)}
 *  for MUS format.
 *
 * @param is MUS data (this method does not try to auto-detect the format.)
 */
public static Sequence getSequence(InputStream is)
throws IOException, InvalidMidiDataException {
    DataInputStream dis = new DataInputStream(is);
    dis.skip(6);
    int rus = dis.readUnsignedShort();
    short scoreStart = Swap.SHORT((char) rus);
    dis.skip(scoreStart - 8);
    Sequence sequence = new Sequence(Sequence.SMPTE_30, 14, 1);
    Track track = sequence.getTracks()[0];
    int[] chanVelocity = new int[16];
    Arrays.fill(chanVelocity, 100);
    EventGroup eg;
    long tick = 0;
    while ((eg = nextEventGroup(dis, chanVelocity)) != null) {
        tick = eg.appendTo(track, tick);
    }
    MetaMessage endOfSequence = new MetaMessage();
    endOfSequence.setMessage(47, new byte[] {0}, 1);
    track.add(new MidiEvent(endOfSequence, tick));
    return sequence;
}
 
Example #3
Source File: StandardMidiFileWriter.java    From jdk8u-dev-jdk with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Obtains the file types that this provider can write from the
 * sequence specified.
 * @param sequence the sequence for which midi file type support
 * is queried
 * @return array of file types.  If no file types are supported,
 * returns an array of length 0.
 */
public int[] getMidiFileTypes(Sequence sequence){
    int typesArray[];
    Track tracks[] = sequence.getTracks();

    if( tracks.length==1 ) {
        typesArray = new int[2];
        typesArray[0] = MIDI_TYPE_0;
        typesArray[1] = MIDI_TYPE_1;
    } else {
        typesArray = new int[1];
        typesArray[0] = MIDI_TYPE_1;
    }

    return typesArray;
}
 
Example #4
Source File: StandardMidiFileWriter.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Obtains the file types that this provider can write from the
 * sequence specified.
 * @param sequence the sequence for which midi file type support
 * is queried
 * @return array of file types.  If no file types are supported,
 * returns an array of length 0.
 */
public int[] getMidiFileTypes(Sequence sequence){
    int typesArray[];
    Track tracks[] = sequence.getTracks();

    if( tracks.length==1 ) {
        typesArray = new int[2];
        typesArray[0] = MIDI_TYPE_0;
        typesArray[1] = MIDI_TYPE_1;
    } else {
        typesArray = new int[1];
        typesArray[0] = MIDI_TYPE_1;
    }

    return typesArray;
}
 
Example #5
Source File: Markers.java    From mpcmaid with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Exports the location of each slice into a MIDI file
 */
public Sequence exportMidiSequence(final File file, final int ppq) throws IOException {
	final double tempoBps = (double) getTempo() / 60.0;
	final MidiSequenceBuilder builder = new MidiSequenceBuilder(ppq);
	final Track track = builder.getTrack();
	final int n = markers.size();
	int startTick = 0;
	int key = 35;
	for (int i = 0; i < n; i++) {
		final int location = getLocation(i);
		startTick = (int) Math.round(ppq * tempoBps * location / samplingRate);
		int tickLength = 32;
		builder.addNote(track, startTick, tickLength, key++);
	}
	if (file != null) {
		builder.save(file);
	}
	return builder.getSequence();
}
 
Example #6
Source File: MidiSequencerImpl.java    From tuxguitar with GNU Lesser General Public License v2.1 6 votes vote down vote up
public void resetTracks(){
	try {
		Sequence sequence = this.getSequencer().getSequence();
		if(sequence != null){
			Track[] tracks = sequence.getTracks();
			if( tracks != null ){
				int count = tracks.length;
				for( int i = 0 ; i < count; i++ ){
					this.setSolo( i , false );
					this.setMute( i , false );
				}
			}
		}
	} catch (Throwable throwable) {
		throwable.printStackTrace();
	}
}
 
Example #7
Source File: StandardMidiFileWriter.java    From jdk8u_jdk with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Obtains the file types that this provider can write from the
 * sequence specified.
 * @param sequence the sequence for which midi file type support
 * is queried
 * @return array of file types.  If no file types are supported,
 * returns an array of length 0.
 */
public int[] getMidiFileTypes(Sequence sequence){
    int typesArray[];
    Track tracks[] = sequence.getTracks();

    if( tracks.length==1 ) {
        typesArray = new int[2];
        typesArray[0] = MIDI_TYPE_0;
        typesArray[1] = MIDI_TYPE_1;
    } else {
        typesArray = new int[1];
        typesArray[0] = MIDI_TYPE_1;
    }

    return typesArray;
}
 
Example #8
Source File: StandardMidiFileWriter.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Obtains the file types that this provider can write from the
 * sequence specified.
 * @param sequence the sequence for which midi file type support
 * is queried
 * @return array of file types.  If no file types are supported,
 * returns an array of length 0.
 */
public int[] getMidiFileTypes(Sequence sequence){
    int typesArray[];
    Track tracks[] = sequence.getTracks();

    if( tracks.length==1 ) {
        typesArray = new int[2];
        typesArray[0] = MIDI_TYPE_0;
        typesArray[1] = MIDI_TYPE_1;
    } else {
        typesArray = new int[1];
        typesArray[0] = MIDI_TYPE_1;
    }

    return typesArray;
}
 
Example #9
Source File: StandardMidiFileWriter.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Obtains the file types that this provider can write from the
 * sequence specified.
 * @param sequence the sequence for which midi file type support
 * is queried
 * @return array of file types.  If no file types are supported,
 * returns an array of length 0.
 */
public int[] getMidiFileTypes(Sequence sequence){
    int typesArray[];
    Track tracks[] = sequence.getTracks();

    if( tracks.length==1 ) {
        typesArray = new int[2];
        typesArray[0] = MIDI_TYPE_0;
        typesArray[1] = MIDI_TYPE_1;
    } else {
        typesArray = new int[1];
        typesArray[0] = MIDI_TYPE_1;
    }

    return typesArray;
}
 
Example #10
Source File: StandardMidiFileWriter.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Obtains the file types that this provider can write from the
 * sequence specified.
 * @param sequence the sequence for which midi file type support
 * is queried
 * @return array of file types.  If no file types are supported,
 * returns an array of length 0.
 */
public int[] getMidiFileTypes(Sequence sequence){
    int typesArray[];
    Track tracks[] = sequence.getTracks();

    if( tracks.length==1 ) {
        typesArray = new int[2];
        typesArray[0] = MIDI_TYPE_0;
        typesArray[1] = MIDI_TYPE_1;
    } else {
        typesArray = new int[1];
        typesArray[0] = MIDI_TYPE_1;
    }

    return typesArray;
}
 
Example #11
Source File: StandardMidiFileWriter.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Obtains the file types that this provider can write from the
 * sequence specified.
 * @param sequence the sequence for which midi file type support
 * is queried
 * @return array of file types.  If no file types are supported,
 * returns an array of length 0.
 */
public int[] getMidiFileTypes(Sequence sequence){
    int typesArray[];
    Track tracks[] = sequence.getTracks();

    if( tracks.length==1 ) {
        typesArray = new int[2];
        typesArray[0] = MIDI_TYPE_0;
        typesArray[1] = MIDI_TYPE_1;
    } else {
        typesArray = new int[1];
        typesArray[0] = MIDI_TYPE_1;
    }

    return typesArray;
}
 
Example #12
Source File: StandardMidiFileWriter.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Obtains the file types that this provider can write from the
 * sequence specified.
 * @param sequence the sequence for which midi file type support
 * is queried
 * @return array of file types.  If no file types are supported,
 * returns an array of length 0.
 */
@Override
public int[] getMidiFileTypes(Sequence sequence){
    int typesArray[];
    Track tracks[] = sequence.getTracks();

    if( tracks.length==1 ) {
        typesArray = new int[2];
        typesArray[0] = MIDI_TYPE_0;
        typesArray[1] = MIDI_TYPE_1;
    } else {
        typesArray = new int[1];
        typesArray[0] = MIDI_TYPE_1;
    }

    return typesArray;
}
 
Example #13
Source File: RealTimeSequencer.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void recordEnable(Track track, int channel) {
    if (!findTrack(track)) {
        throw new IllegalArgumentException("Track does not exist in the current sequence");
    }

    synchronized(recordingTracks) {
        RecordingTrack rc = RecordingTrack.get(recordingTracks, track);
        if (rc != null) {
            rc.channel = channel;
        } else {
            recordingTracks.add(new RecordingTrack(track, channel));
        }
    }

}
 
Example #14
Source File: StandardMidiFileWriter.java    From Bytecoder with Apache License 2.0 6 votes vote down vote up
/**
 * Obtains the file types that this provider can write from the
 * sequence specified.
 * @param sequence the sequence for which midi file type support
 * is queried
 * @return array of file types.  If no file types are supported,
 * returns an array of length 0.
 */
@Override
public int[] getMidiFileTypes(Sequence sequence){
    int[] typesArray;
    Track[] tracks = sequence.getTracks();

    if( tracks.length==1 ) {
        typesArray = new int[2];
        typesArray[0] = MIDI_TYPE_0;
        typesArray[1] = MIDI_TYPE_1;
    } else {
        typesArray = new int[1];
        typesArray[0] = MIDI_TYPE_1;
    }

    return typesArray;
}
 
Example #15
Source File: StandardMidiFileWriter.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Obtains the file types that this provider can write from the
 * sequence specified.
 * @param sequence the sequence for which midi file type support
 * is queried
 * @return array of file types.  If no file types are supported,
 * returns an array of length 0.
 */
public int[] getMidiFileTypes(Sequence sequence){
    int typesArray[];
    Track tracks[] = sequence.getTracks();

    if( tracks.length==1 ) {
        typesArray = new int[2];
        typesArray[0] = MIDI_TYPE_0;
        typesArray[1] = MIDI_TYPE_1;
    } else {
        typesArray = new int[1];
        typesArray[0] = MIDI_TYPE_1;
    }

    return typesArray;
}
 
Example #16
Source File: MidiWaveformSynthesizer.java    From chart-fx with Apache License 2.0 6 votes vote down vote up
public static final void addNotesToTrack(final Track track, final Track trk) throws InvalidMidiDataException {
    for (int ii = 0; ii < track.size(); ii++) {
        final MidiEvent me = track.get(ii);
        final MidiMessage mm = me.getMessage();
        if (mm instanceof ShortMessage) {
            final ShortMessage sm = (ShortMessage) mm;
            final int command = sm.getCommand();
            int com = -1;
            if (command == ShortMessage.NOTE_ON) {
                com = LOCAL_NOTE_ON;
            } else if (command == ShortMessage.NOTE_OFF) {
                com = LOCAL_NOTE_OFF;
            }
            if (com > 0) {
                final byte[] b = sm.getMessage();
                final int l = (b == null ? 0 : b.length);
                final MetaMessage metaMessage = new MetaMessage(com, b, l);
                final MidiEvent me2 = new MidiEvent(metaMessage, me.getTick());
                trk.add(me2);
            }
        }
    }
}
 
Example #17
Source File: StandardMidiFileWriter.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Obtains the file types that this provider can write from the
 * sequence specified.
 * @param sequence the sequence for which midi file type support
 * is queried
 * @return array of file types.  If no file types are supported,
 * returns an array of length 0.
 */
public int[] getMidiFileTypes(Sequence sequence){
    int typesArray[];
    Track tracks[] = sequence.getTracks();

    if( tracks.length==1 ) {
        typesArray = new int[2];
        typesArray[0] = MIDI_TYPE_0;
        typesArray[1] = MIDI_TYPE_1;
    } else {
        typesArray = new int[1];
        typesArray[0] = MIDI_TYPE_1;
    }

    return typesArray;
}
 
Example #18
Source File: StandardMidiFileWriter.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Obtains the file types that this provider can write from the
 * sequence specified.
 * @param sequence the sequence for which midi file type support
 * is queried
 * @return array of file types.  If no file types are supported,
 * returns an array of length 0.
 */
public int[] getMidiFileTypes(Sequence sequence){
    int typesArray[];
    Track tracks[] = sequence.getTracks();

    if( tracks.length==1 ) {
        typesArray = new int[2];
        typesArray[0] = MIDI_TYPE_0;
        typesArray[1] = MIDI_TYPE_1;
    } else {
        typesArray = new int[1];
        typesArray[0] = MIDI_TYPE_1;
    }

    return typesArray;
}
 
Example #19
Source File: StandardMidiFileWriter.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Obtains the file types that this provider can write from the
 * sequence specified.
 * @param sequence the sequence for which midi file type support
 * is queried
 * @return array of file types.  If no file types are supported,
 * returns an array of length 0.
 */
public int[] getMidiFileTypes(Sequence sequence){
    int typesArray[];
    Track tracks[] = sequence.getTracks();

    if( tracks.length==1 ) {
        typesArray = new int[2];
        typesArray[0] = MIDI_TYPE_0;
        typesArray[1] = MIDI_TYPE_1;
    } else {
        typesArray = new int[1];
        typesArray[0] = MIDI_TYPE_1;
    }

    return typesArray;
}
 
Example #20
Source File: RealTimeSequencer.java    From Bytecoder with Apache License 2.0 6 votes vote down vote up
@Override
public void recordEnable(Track track, int channel) {
    if (!findTrack(track)) {
        throw new IllegalArgumentException("Track does not exist in the current sequence");
    }

    synchronized(recordingTracks) {
        RecordingTrack rc = RecordingTrack.get(recordingTracks, track);
        if (rc != null) {
            rc.channel = channel;
        } else {
            recordingTracks.add(new RecordingTrack(track, channel));
        }
    }

}
 
Example #21
Source File: MidiUtils.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
/**
 * Binary search for the event indexes of the track
 *
 * @param tick  tick number of index to be found in array
 * @return index in track which is on or after "tick".
 *   if no entries are found that follow after tick, track.size() is returned
 */
public static int tick2index(Track track, long tick) {
    int ret = 0;
    if (tick > 0) {
        int low = 0;
        int high = track.size() - 1;
        while (low < high) {
            // take the middle event as estimate
            ret = (low + high) >> 1;
            // tick of estimate
            long t = track.get(ret).getTick();
            if (t == tick) {
                break;
            } else if (t < tick) {
                // estimate too low
                if (low == high - 1) {
                    // "or after tick"
                    ret++;
                    break;
                }
                low = ret;
            } else { // if (t>tick)
                // estimate too high
                high = ret;
            }
        }
    }
    return ret;
}
 
Example #22
Source File: MidiSynth.java    From jmg with GNU General Public License v2.0 5 votes vote down vote up
/**
  * Plays the jMusic score data via the JavaSound MIDI synthesizer
  * @param Score score - data to change to SMF
  * @exception Exception 
  */
  public void play(Score score) throws InvalidMidiDataException {
      if (null == m_sequencer) {
          if (!initSynthesizer()) {
              return;
          }
      }
      scoreTitle = score.getTitle();
m_masterTempo = (float)score.getTempo();
if (null == m_seq) {
      	m_seq = scoreToSeq(score);
} else {
	// empty seq and refill
	Track[] tracks = m_seq.getTracks();
	int t_len = tracks.length;
	for (int i=0; i < t_len; i++) {
		m_seq.deleteTrack(tracks[i]);
	}
	m_seq = scoreToSeq(score);
}
      if (null != m_seq) {
          try {
              m_sequencer.open();
          }
          catch (MidiUnavailableException e) {
              System.err.println("MIDI System Unavailable:" + e);
              return;
          }
          m_sequencer.setSequence(m_seq);
          m_sequencer.addMetaEventListener(this);
	m_sequencer.setMicrosecondPosition(0l);
          m_sequencer.setTempoInBPM(m_masterTempo);

          //printSeqInfo(seq);
          m_sequencer.start();
	isPlaying = true;
      }
  }
 
Example #23
Source File: FinnwMusicModule.java    From mochadoom with GNU General Public License v3.0 5 votes vote down vote up
long appendTo(Sequence sequence, int trackNum, long pos) {
    Track track = sequence.getTracks()[trackNum];
    for (MidiMessage msg: messages) {
        track.add(new MidiEvent(msg, pos));
    }
    return pos + delay * 3;
}
 
Example #24
Source File: MidiWaveformSynthesizer.java    From chart-fx with Apache License 2.0 5 votes vote down vote up
public final Track mergeShortMessageEvent(final Track[] tracks) {
    final Track trk = sequence.createTrack();
    for (final Track track : tracks) {
        for (int i = 0; i < track.size(); i++) {
            final MidiEvent evt = track.get(i);
            final MidiMessage mm = evt.getMessage();
            if (mm instanceof ShortMessage) {
                trk.add(evt);
            }
        }
    }
    return trk;
}
 
Example #25
Source File: MidiSequenceBuilder.java    From mpcmaid with GNU Lesser General Public License v2.1 5 votes vote down vote up
public void addNote(Track track, int channel, int startTick, int tickLength, int key, int velocity) {
	try {
		final ShortMessage on = new ShortMessage();
		on.setMessage(ShortMessage.NOTE_ON, channel, key, velocity);
		final ShortMessage off = new ShortMessage();
		off.setMessage(ShortMessage.NOTE_OFF, channel, key, velocity);
		track.add(new MidiEvent(on, startTick));
		track.add(new MidiEvent(off, startTick + tickLength));
	} catch (InvalidMidiDataException e) {
		e.printStackTrace();
	}
}
 
Example #26
Source File: MidiSequenceHandlerImpl.java    From tuxguitar with GNU Lesser General Public License v2.1 5 votes vote down vote up
private void init(){
	try {
		this.sequence = new Sequence(Sequence.PPQ,(int)TGDuration.QUARTER_TIME);
		this.midiTracks = new Track[getTracks()];
		for (int i = 0; i < this.midiTracks.length; i++) {
			this.midiTracks[i] = this.sequence.createTrack();
		}
	} catch (InvalidMidiDataException e) {
		e.printStackTrace();
	}
}
 
Example #27
Source File: RealTimeSequencer.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
static Track get(List<RecordingTrack> recordingTracks, int channel) {

            synchronized(recordingTracks) {
                int size = recordingTracks.size();
                for (int i = 0; i < size; i++) {
                    RecordingTrack current = recordingTracks.get(i);
                    if ((current.channel == channel) || (current.channel == -1)) {
                        return current.track;
                    }
                }
            }
            return null;

        }
 
Example #28
Source File: RealTimeSequencer.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
@Override
public void recordDisable(Track track) {
    synchronized(recordingTracks) {
        RecordingTrack rc = RecordingTrack.get(recordingTracks, track);
        if (rc != null) {
            recordingTracks.remove(rc);
        }
    }

}
 
Example #29
Source File: TrackAddSameTick.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String argv[]) throws Exception {
    Sequence seq = new Sequence(Sequence.PPQ, 240);
    Track t = seq.createTrack();

    log("add 10 events in random order");
    t.add(createEvent(10, 5));
    t.add(createEvent(0, 0));
    t.add(createEvent(10, 6));
    t.add(createEvent(11, 8));
    t.add(createEvent(10, 7));
    t.add(createEvent(0, 1));
    t.add(createEvent(0, 2));
    t.add(createEvent(15, 9));
    t.add(createEvent(0, 3));
    t.add(createEvent(1, 4));

    // now compare the events.
    // The note param will tell us the
    // the expected position
    long lastTick = 0;
    for (int i = 0; i < t.size(); i++) {
        MidiEvent ev = t.get(i);
        if (ev.getMessage() instanceof ShortMessage) {
            ShortMessage msg = (ShortMessage) ev.getMessage();
            log(""+i+": ShortMessage at tick "+ev.getTick()
                +" with expected position "+msg.getData1());
            if (ev.getTick() < lastTick) {
                log("  FAILED: last tick is larger than this event's tick!");
                failed = true;
            }
            if (i != msg.getData1()) {
                log("  FAILED: Track did not order correctly.");
                failed = true;
            }
        }
    }

    if (failed) throw new Exception("Test FAILED!");
    log("Test passed.");
}
 
Example #30
Source File: RealTimeSequencer.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
private boolean findTrack(Track track) {
    boolean found = false;
    if (sequence != null) {
        Track[] tracks = sequence.getTracks();
        for (int i = 0; i < tracks.length; i++) {
            if (track == tracks[i]) {
                found = true;
                break;
            }
        }
    }
    return found;
}