本文整理汇总了Java中javax.sound.midi.Sequence.createTrack方法的典型用法代码示例。如果您正苦于以下问题:Java Sequence.createTrack方法的具体用法?Java Sequence.createTrack怎么用?Java Sequence.createTrack使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.sound.midi.Sequence
的用法示例。
在下文中一共展示了Sequence.createTrack方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: go
import javax.sound.midi.Sequence; //导入方法依赖的package包/类
public void go() {
setUpGui();
try {
Sequencer sequencer = MidiSystem.getSequencer();
sequencer.open();
sequencer.addControllerEventListener(ml, new int[] {127});
Sequence seq = new Sequence(Sequence.PPQ, 4);
Track track = seq.createTrack();
int r = 0;
for (int i = 0; i < 60; i+=4) {
r = (int) ((Math.random() * 50) + 1);
track.add(makeEvent(144, 1, r, 100, i));
track.add(makeEvent(176, 1, 127, 0, i));
track.add(makeEvent(128, 1, r, 100, i + 2));
} // end loop
sequencer.setSequence(seq);
sequencer.start();
sequencer.setTempoInBPM(120);
} catch (Exception ex) {ex.printStackTrace();}
}
示例2: go
import javax.sound.midi.Sequence; //导入方法依赖的package包/类
public void go() {
try {
Sequencer sequencer = MidiSystem.getSequencer();
sequencer.open();
int[] eventsIWant = {127};
sequencer.addControllerEventListener(this, eventsIWant);
Sequence seq = new Sequence(Sequence.PPQ, 4);
Track track = seq.createTrack();
for (int i = 5; i < 60; i+= 4) {
track.add(makeEvent(144, 1, i, 100, i));
track.add(makeEvent(176, 1, 127, 0, i));
track.add(makeEvent(128, 1, i, 100, i + 2));
} // end loop
sequencer.setSequence(seq);
sequencer.setTempoInBPM(220);
sequencer.start();
} catch (Exception ex) {ex.printStackTrace();}
}
示例3: main
import javax.sound.midi.Sequence; //导入方法依赖的package包/类
public static void main(String[] args) {
try {
// make and open a sequencer
Sequencer sequencer = MidiSystem.getSequencer();
sequencer.open();
// make a sequence and a track
Sequence seq = new Sequence(Sequence.PPQ, 4);
Track track = seq.createTrack();
// make a bunch of events to make the notes keep going up
// (from piano note 5 to piano note 61)
for (int i = 5; i < 61; i+=4) {
track.add(makeEvent(144, 1, i, 100, i));
track.add(makeEvent(128, 1, i, 100, i + 2));
} // end loop
sequencer.setSequence(seq);
sequencer.setTempoInBPM(220);
sequencer.start();
} catch (Exception ex) {ex.printStackTrace();}
}
示例4: export
import javax.sound.midi.Sequence; //导入方法依赖的package包/类
/**
* Returns a clone of this sequence suitable for Midi file export. What it does is to map the FTW channel setting to all the midi events
* for the corresponding tracks
* @return
* @throws InvalidMidiDataException
*/
public Sequence export() throws InvalidMidiDataException
{
Sequence newSeq = new Sequence(getDivisionType(),getResolution());
for(FrinikaTrackWrapper ftw : frinikaTrackWrappers)
{
Track track = newSeq.createTrack();
for(int n=0;n<ftw.size();n++)
{
MidiEvent sourceMidiEvent = ftw.get(n);
MidiMessage msg = sourceMidiEvent.getMessage();
if(msg instanceof ShortMessage)
{
ShortMessage shm = (ShortMessage)msg;
ShortMessage nshm = new ShortMessage();
nshm.setMessage(shm.getCommand(),ftw.getMidiChannel(),shm.getData1(),shm.getData2());
msg = nshm;
}
MidiEvent newEvent = new MidiEvent(msg,sourceMidiEvent.getTick());
track.add(newEvent);
}
}
return newSeq;
}
示例5: main
import javax.sound.midi.Sequence; //导入方法依赖的package包/类
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.");
}
示例6: MetaCallback
import javax.sound.midi.Sequence; //导入方法依赖的package包/类
MetaCallback() throws Exception {
sequencer=MidiSystem.getSequencer();
sequence=new Sequence(Sequence.PPQ,240);
track=sequence.createTrack();
sequencer.addMetaEventListener(this);
byte[] data = new byte[1];
track.add(new MidiEvent(MidiMsg3(ShortMessage.NOTE_ON+0,45,100),0));
track.add(new MidiEvent(MidiMsg3(ShortMessage.NOTE_ON+0,45,0),0 + 240));
int c;
for(c=0; c < TOTAL_COUNT; c++) {
data[0]=(byte)(c+1);
MetaMessage meta = new MetaMessage();
meta.setMessage(1, data, 1); // type, data, length
track.add(new MidiEvent(meta,c*20));
}
track.add(new MidiEvent(MidiMsg3(ShortMessage.NOTE_ON+9,45,100),c*20));
track.add(new MidiEvent(MidiMsg3(ShortMessage.NOTE_ON+9,45,0),c*20 + 10));
sequencer.setSlaveSyncMode(Sequencer.SyncMode.INTERNAL_CLOCK);
sequencer.setMasterSyncMode(Sequencer.SyncMode.INTERNAL_CLOCK);
sequencer.open();
sequencer.setSequence(sequence);
sequencer.setTempoInBPM(100);
System.out.println("Starting playback...");
this.start();
while (!finished && sequencer.getTickPosition() < sequencer.getTickLength()) {
System.out.println("Tick "+sequencer.getTickPosition()+"...");
Thread.sleep(1000);
}
System.out.println("Stopping playback...");
this.stop();
if (metaCount != TOTAL_COUNT) {
throw new Exception("Expected "+TOTAL_COUNT+" callbacks, but got "+metaCount+"!");
}
}
示例7: MidiWriter
import javax.sound.midi.Sequence; //导入方法依赖的package包/类
/**
* Create a new MidiWriter to write out to the given File.
*
* @param outFile {@link #outFile}
* @param tt {@link #timeTracker}
*
* @throws InvalidMidiDataException If somehow the TimeTracker has an invalid PPQ value.
*/
public MidiWriter(File outFile, TimeTracker tt) throws InvalidMidiDataException {
this.outFile = outFile;
timeTracker = tt;
sequence = new Sequence(Sequence.PPQ, (int) timeTracker.getPPQ());
sequence.createTrack();
writeTimeTracker();
}
示例8: ChangeResolution
import javax.sound.midi.Sequence; //导入方法依赖的package包/类
/**
* Change resolution (TPQN) and retiming events.
* @param seq Sequence to be processed.
* @param resolution Ticks per quarter note of new sequence.
* @return New sequence with new resolution.
* @throws InvalidMidiDataException throw if MIDI data is invalid.
*/
public static Sequence ChangeResolution(Sequence sourceSeq, int resolution) throws InvalidMidiDataException
{
// sequence must be tick-based
if (sourceSeq.getDivisionType() != Sequence.PPQ)
{
throw new UnsupportedOperationException("SMPTE is not supported.");
}
Sequence seq = new Sequence(sourceSeq.getDivisionType(), resolution);
// process all input tracks
for (int trackIndex = 0; trackIndex < sourceSeq.getTracks().length; trackIndex++)
{
Track sourceTrack = sourceSeq.getTracks()[trackIndex];
Track track = seq.createTrack();
// process all events
double timingRate = (double) resolution / sourceSeq.getResolution();
for (int eventIndex = 0; eventIndex < sourceTrack.size(); eventIndex++)
{
MidiEvent sourceEvent = sourceTrack.get(eventIndex);
MidiEvent event = new MidiEvent(sourceEvent.getMessage(), Math.round(sourceEvent.getTick() * timingRate));
track.add(event);
}
}
// if the target resolution is shorter than source resolution,
// events at different timing might be located at the same timing.
// As a result, there might be zero-length note and/or
// same control changes at the same timing.
//
// Probably, they should be removed for better conversion.
// I do not remove them anyway at the moment,
// because it does not cause any major problems.
return seq;
}
示例9: MidiSequence
import javax.sound.midi.Sequence; //导入方法依赖的package包/类
public MidiSequence(int ppqn)
{
try
{
sequence = new Sequence(Sequence.PPQ, ppqn);
trackDrums = new Track[MIDIBeast.spacelessDrumName.length];
trackChords = sequence.createTrack();
trackBass = sequence.createTrack();
trackMelody = sequence.createTrack();
}
catch( Exception e )
{
ErrorLog.log(ErrorLog.WARNING, "something wrong in MidiSequence");
}
}
示例10: setUpMidi
import javax.sound.midi.Sequence; //导入方法依赖的package包/类
public void setUpMidi() {
try {
sequencer = MidiSystem.getSequencer();
sequencer.open();
sequence = new Sequence(Sequence.PPQ, 4);
track = sequence.createTrack();
sequencer.setTempoInBPM(120);
} catch (Exception e) {e.printStackTrace();}
}
示例11: play
import javax.sound.midi.Sequence; //导入方法依赖的package包/类
public void play(int instrument, int note) {
try {
Sequencer player = MidiSystem.getSequencer();
player.open();
Sequence seq = new Sequence(Sequence.PPQ, 4);
Track track = seq.createTrack();
MidiEvent event = null;
ShortMessage first = new ShortMessage();
first.setMessage(192, 1, instrument, 0);
MidiEvent changeInstrument = new MidiEvent(first, 1);
track.add(changeInstrument);
ShortMessage a = new ShortMessage();
a.setMessage(144, 1, note, 100);
MidiEvent noteOn = new MidiEvent(a, 1);
track.add(noteOn);
ShortMessage b = new ShortMessage();
b.setMessage(128, 1, note, 100);
MidiEvent noteOff = new MidiEvent(b, 16);
track.add(noteOff);
player.setSequence(seq);
player.start();
} catch (Exception ex) {ex.printStackTrace();}
}
示例12: play
import javax.sound.midi.Sequence; //导入方法依赖的package包/类
public void play() {
try {
// Get a sequencer and open it
Sequencer player = MidiSystem.getSequencer();
player.open();
// Set up sequence
Sequence seq = new Sequence(Sequence.PPQ, 4);
// Ask the sequence for a Track
Track track = seq.createTrack();
// Put some MidiEvents into the Track
ShortMessage a = new ShortMessage();
a.setMessage(144, 1, 44, 100);
MidiEvent noteOn = new MidiEvent(a, 1);
track.add(noteOn);
ShortMessage b = new ShortMessage();
b.setMessage(128, 1, 44, 100);
MidiEvent noteOff = new MidiEvent(b, 16);
track.add(noteOff);
// Give the sequence to the sequencer
player.setSequence(seq);
// Start() the sequencer
player.start();
} catch (Exception ex) {
ex.printStackTrace();
}
}
示例13: setUpMidi
import javax.sound.midi.Sequence; //导入方法依赖的package包/类
public void setUpMidi() {
try {
sequencer = MidiSystem.getSequencer();
sequencer.open();
sequence = new Sequence(Sequence.PPQ, 4);
track = sequence.createTrack();
sequencer.setTempoInBPM(120);
} catch(Exception e) {e.printStackTrace();}
}
示例14: createFastPrototypes
import javax.sound.midi.Sequence; //导入方法依赖的package包/类
/**
* A hack to get a real FastShortMessage that can be cloned, e.g. Prototype
*/
private static void createFastPrototypes() {
try {
// create a sequence with a ShortMessage
Sequence sequence = new Sequence(Sequence.PPQ, 10);
Track track = sequence.createTrack();
track.add(new MidiEvent(new ShortMessage(), 0l));
// save the sequence
try {
File file = File.createTempFile("FSMhack", "mid");
MidiSystem.write(sequence, 0, file);
// load the sequence and get back the FastShortMessage
sequence = MidiSystem.getSequence(file);
track = sequence.getTracks()[0];
MidiMessage msg = null;
for ( int i = 0; i < track.size(); i++ ) {
msg = track.get(i).getMessage();
Class clazz = msg.getClass();
if ( fastShortPrototype == null &&
clazz.getCanonicalName().equals("com.sun.media.sound.FastShortMessage") ) {
fastShortPrototype = msg;
}
}
file.delete();
} catch ( IOException ioe ) {
}
} catch ( InvalidMidiDataException imde ) {
}
}
示例15: convertToType1
import javax.sound.midi.Sequence; //导入方法依赖的package包/类
/**
* Separates the MIDI file to have one track per channel (Type 1).
*/
public static void convertToType1(Sequence song)
{
if (song.getTracks().length == 1)
{
Track track0 = song.getTracks()[0];
Track[] tracks = new Track[CHANNEL_COUNT];
int trackNumber = 1;
int i = 0;
while (i < track0.size())
{
MidiEvent evt = track0.get(i);
if (evt.getMessage() instanceof ShortMessage)
{
int chan = ((ShortMessage) evt.getMessage()).getChannel();
if (tracks[chan] == null)
{
tracks[chan] = song.createTrack();
String trackName = (chan == DRUM_CHANNEL) ? "Drums" : ("Track " + trackNumber);
trackNumber++;
tracks[chan].add(MidiFactory.createTrackNameEvent(trackName));
}
tracks[chan].add(evt);
if (track0.remove(evt))
continue;
}
i++;
}
}
}