本文整理匯總了Java中javax.sound.midi.Track.add方法的典型用法代碼示例。如果您正苦於以下問題:Java Track.add方法的具體用法?Java Track.add怎麽用?Java Track.add使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類javax.sound.midi.Track
的用法示例。
在下文中一共展示了Track.add方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: partName
import javax.sound.midi.Track; //導入方法依賴的package包/類
/**
* scan the part attribute name for a known string to create a gm program change and instrument name event
* @param part
* @param track the track that shall correspond to the part
* @param generateProgramChanges if true, program change events are generated (useful for MIR and as a cheap kind of piano reduction)
*/
private void partName(Element part, Track track, boolean generateProgramChanges) {
short chan = Short.parseShort(part.getAttributeValue("midi.channel"));
if ((part.getAttribute("name") == null) || part.getAttributeValue("name").isEmpty()) { // if there is no name
if (generateProgramChanges)
track.add(EventMaker.createProgramChange(chan, 0, (short)0)); // add program change event for Acoustic Grand Piano
track.add(EventMaker.createInstrumentName(0, "")); // add an empty instrument name event to the track
return;
}
String name = part.getAttributeValue("name");
if (generateProgramChanges)
track.add(EventMaker.createProgramChange(chan, 0, name)); // add program change event
track.add(EventMaker.createInstrumentName(0, name)); // add an instrument name event to the track
}
示例2: parseScore
import javax.sound.midi.Track; //導入方法依賴的package包/類
/**
* parse the elements in the score map of part (part.dated.score) to midi events and add them to track
*
* @param part the msm source
* @param track the midi track
*/
private void parseScore(Element part, Track track) {
if ((part.getFirstChildElement("dated") == null)
|| (part.getFirstChildElement("dated").getFirstChildElement("score") == null)
|| (part.getAttribute("midi.channel") == null)) // if no sufficient information
return; // cancel
int chan = Integer.parseInt(part.getAttributeValue("midi.channel")); // get the midi channel number
for (Element n = part.getFirstChildElement("dated").getFirstChildElement("score").getFirstChildElement("note"); n != null; n = Helper.getNextSiblingElement("note", n)) { // go through all note elements in score
// switch (n.getLocalName()) {
// case "rest": // rests are not represented in midi
// break;
// case "note": // for note elements create note_on and note_off events
int pitch = Math.round(Float.parseFloat(n.getAttributeValue("midi.pitch"))); // Math.round(float) returns int; so far pitches are well captured by number type float
long date = Math.round(Double.parseDouble(n.getAttributeValue("midi.date"))); // Math.round(double) returns long
long dur = Math.round(Double.parseDouble(n.getAttributeValue("midi.duration")));
track.add(EventMaker.createNoteOn(chan, date, pitch, 100));
track.add(EventMaker.createNoteOff(chan, date + dur, pitch, 100));
// break;
// }
// TODO: process text (not implemented in mei-to-msm-export, yet, but planned to be added in the future)
}
}
示例3: parseMarkerMap
import javax.sound.midi.Track; //導入方法依賴的package包/類
/**
* parse the markerMap and create marker events from it
* @param part
* @param track
*/
private void parseMarkerMap(Element part, Track track) {
if ((part.getFirstChildElement("dated") == null)
|| (part.getFirstChildElement("dated").getFirstChildElement("markerMap") == null)) // if no sufficient information
return; // cancel
String message; // the marker message
for (Element e = part.getFirstChildElement("dated").getFirstChildElement("markerMap").getFirstChildElement("marker"); e != null; e = Helper.getNextSiblingElement("marker", e)) {
try {
message = e.getAttributeValue("message");
} catch (NullPointerException | NumberFormatException error) {
message = "marker";
}
track.add(EventMaker.createMarker(Math.round(Double.parseDouble(e.getAttributeValue("midi.date"))), message));
}
}
示例4: addMidiNote
import javax.sound.midi.Track; //導入方法依賴的package包/類
/**
* Add the given MidiNote into the {@link #sequence}.
*
* @param note The note to add.
*
* @throws InvalidMidiDataException If the MidiNote contains invalid Midi data.
*/
public void addMidiNote(MidiNote note) throws InvalidMidiDataException {
int correctVoice = note.getCorrectVoice();
// Pad with enough tracks
while (sequence.getTracks().length <= correctVoice) {
sequence.createTrack();
}
// Get the correct track
Track track = sequence.getTracks()[correctVoice];
ShortMessage noteOn = new ShortMessage();
noteOn.setMessage(ShortMessage.NOTE_ON | correctVoice, note.getPitch(), note.getVelocity());
MidiEvent noteOnEvent = new MidiEvent(noteOn, note.getOnsetTick());
ShortMessage noteOff = new ShortMessage();
noteOff.setMessage(ShortMessage.NOTE_OFF | correctVoice, note.getPitch(), 0);
MidiEvent noteOffEvent = new MidiEvent(noteOff, note.getOffsetTick());
track.add(noteOnEvent);
track.add(noteOffEvent);
}
示例5: go
import javax.sound.midi.Track; //導入方法依賴的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();}
}
示例6: go
import javax.sound.midi.Track; //導入方法依賴的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();}
}
示例7: main
import javax.sound.midi.Track; //導入方法依賴的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();}
}
示例8: export
import javax.sound.midi.Track; //導入方法依賴的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;
}
示例9: renderBar
import javax.sound.midi.Track; //導入方法依賴的package包/類
/**
* Render a bar of notes as MIDI to the specified Track from the
* specified start tick with the specified ticks per bar.
* @param notes the notes to render
* @param track the MIDI Track to render to
* @param startTick the tick at the start of the bar
* @param ticksPerBar the number of ticks per bar
*/
public void renderBar(int[] notes, Track track,
long startTick, int ppqn)
throws InvalidMidiDataException {
final int channel = getInstrument().getChannel();
MidiMessage msg;
for ( int i = 0; i < notes.length; i++) {
int note = notes[i];
float timeOn = swing(Note.getTime(note));
int pitch = Note.getPitch(note);
int level = Note.getLevel(note);
long onTick = ticks2MidiTicks(timeOn, ppqn);
msg = ChannelMsg.createChannel(
ChannelMsg.NOTE_ON, channel, pitch, level);
track.add(new MidiEvent(msg, startTick + onTick));
// note off
msg = ChannelMsg.createChannel(
ChannelMsg.NOTE_OFF, channel, pitch, 0);
float timeOff = swing(Note.getTime(note)+Note.getDuration(note));
long offTick = ticks2MidiTicks(timeOff, ppqn);
track.add(new MidiEvent(msg, startTick + offTick));
}
}
示例10: getSequence
import javax.sound.midi.Track; //導入方法依賴的package包/類
/** 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;
}
示例11: addMidiTempoEvents
import javax.sound.midi.Track; //導入方法依賴的package包/類
private void addMidiTempoEvents(Track track0)
{
for (QuantizedTimingInfo.TimingInfoEvent event : qtm.getTimingInfoByTick().values())
{
if (event.tick > exportEndTick)
break;
track0.add(MidiFactory.createTempoEvent(event.info.getTempoMPQ(), event.tick));
if (event.tick == 0)
{
// The Java MIDI sequencer can sometimes miss a tempo event at tick 0
// Add another tempo event at tick 1 to work around the bug
track0.add(MidiFactory.createTempoEvent(event.info.getTempoMPQ(), 1));
}
}
}
示例12: test_getPatchList
import javax.sound.midi.Track; //導入方法依賴的package包/類
/**
* test method getPatchList()
*
*/
public void test_getPatchList() throws Exception {
//TODO
/*
* I don't understand how this method works
*/
Sequence seq = new Sequence(Sequence.PPQ, 987, 2);
Track tr = seq.createTrack();
MidiEvent event1 = new MidiEvent(new MidiMessage1(new byte[] {1, 2, 3, 34, -98, -27}), -10L);
MidiEvent event2 = new MidiEvent(new MidiMessage1(new byte[] {23, -16, 4, 78, -12, 5}), 3L);
MidiEvent event3 = new MidiEvent(new MidiMessage1(new byte[] {3, -67, -1, 87, 9, 8, -2}), 6L);
tr.add(event1);
tr.add(event3);
Track tr1 = seq.createTrack();
tr1.add(event2);
assertEquals(0, seq.getPatchList().length);
}
示例13: test_getTickLength
import javax.sound.midi.Track; //導入方法依賴的package包/類
/**
* test method getTickLength()
*
*/
public void test_getTickLength() throws Exception {
Sequence seq = new Sequence(Sequence.SMPTE_24, 67, 9);
Track tr = seq.createTrack();
MidiEvent event1 = new MidiEvent(new MidiMessage1(new byte[] {1, 2, 3}), -10L);
MidiEvent event2 = new MidiEvent(new MidiMessage1(new byte[] {23, -16, 4}), 2L);
MidiEvent event3 = new MidiEvent(new MidiMessage1(new byte[] {3, -67, -1}), 6L);
tr.add(event1);
tr.add(event2);
assertEquals(2, seq.getTickLength());
Track tr1 = seq.createTrack();
tr1.add(event1);
tr1.add(event2);
tr1.add(event3);
assertEquals(6, seq.getTickLength());
seq.deleteTrack(tr1);
assertEquals(2, seq.getTickLength());
seq.deleteTrack(tr);
assertEquals(0, seq.getTickLength());
}
示例14: writeToTrack
import javax.sound.midi.Track; //導入方法依賴的package包/類
/**
* Loads and adds the effects to the given midi track
*
* @param track The track to write to
* @param channel The channel to write to
*/
@Override
public void writeToTrack(Track track, int channel) {
long lastEnd = 0;
for (String name : effects.keySet()) {
Sequence sequence = FileUtils.LoadMidiFile("effects/" + name + ".mid");
if (sequence != null) {
int start =// Position effect in track
(int) (QUARTER * tempo.averageBpm / 60.0 * //beats per second
15 * //because 15 seconds
effects.get(name) //i-th word in text
);
if (start < lastEnd)
start = (int) lastEnd;
if (sequence.getMicrosecondLength() / 1000000.0 + TicksInSecs(start, tempo.resolution) > 15.0) {
start -= SecsInTicks(TicksInSecs(start, tempo.resolution) + sequence.getMicrosecondLength() / 1000000.0 - 15, tempo.resolution);
}
float scale = tempo.resolution / (float) sequence.getResolution(); // Make the tempo fit
for (Track t : sequence.getTracks()) {
for (int i = 0; i < t.size(); i++) {
MidiEvent event = t.get(i);
byte[] data = event.getMessage().getMessage();//(command & 0xF0) | (channel & 0x0F)
data[0] += 2; // Keep channel 1 and 2 free
long tick = (long) (event.getTick() * scale) + start;
MidiEvent ev = new MidiEvent(new MidiMessage(data) {
@Override
public Object clone() {
return null;
}
}, tick);
track.add(ev);
if (tick > lastEnd)
lastEnd = tick;
}
}
}
}
}
示例15: main
import javax.sound.midi.Track; //導入方法依賴的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.");
}