Java Code Examples for javax.sound.midi.Track#add()
The following examples show how to use
javax.sound.midi.Track#add() .
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: MidiWaveformSynthesizer.java From chart-fx with Apache License 2.0 | 6 votes |
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 2
Source File: MusReader.java From mochadoom with GNU General Public License v3.0 | 6 votes |
/** 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: MidiWaveformSynthesizer.java From chart-fx with Apache License 2.0 | 5 votes |
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 4
Source File: TrackAddSameTick.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
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 5
Source File: MidiSequenceBuilder.java From mpcmaid with GNU Lesser General Public License v2.1 | 5 votes |
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 6
Source File: FinnwMusicModule.java From mochadoom with GNU General Public License v3.0 | 5 votes |
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 7
Source File: SeqRecordDoesNotCopy.java From openjdk-jdk9 with GNU General Public License v2.0 | 4 votes |
public static void main(String argv[]) { Sequencer s = null; try { s = MidiSystem.getSequencer(); s.open(); } catch (final MidiUnavailableException ignored) { // the test is not applicable return; } try { Sequence seq = new Sequence(Sequence.PPQ, 384, 2); s.setSequence(seq); Track t = seq.getTracks()[0]; ShortMessage msg = new ShortMessage(); msg.setMessage(0x90, 0x40, 0x7F); t.add(new MidiEvent(msg, 11000)); msg.setMessage(0x90, 0x40, 0x00); t.add(new MidiEvent(msg, 12000)); t = seq.getTracks()[1]; s.recordEnable(t, -1); System.out.println("Started recording..."); s.startRecording(); Receiver r = s.getReceiver(); Thread.sleep(100); // send a normal message System.out.println("Recording a normal NOTE ON message..."); msg.setMessage(0x90, 0x40, 0x6F); r.send(msg, -1); Thread.sleep(100); // send a normal message System.out.println("Recording a normal NOTE OFF message..."); msg.setMessage(0x90, 0x40, 0x00); r.send(msg, -1); Thread.sleep(100); s.stop(); // now see if the messages were recorded System.out.println("Recorded messages:"); int sameMessage = 0; for (int i = 0; i < t.size(); i++) { System.out.print(" "+(i+1)+". "); printEvent(t.get(i)); if (t.get(i).getMessage() == msg) { System.out.println("## Failed: Same Message reference!"); sameMessage++; } } if (sameMessage > 0) { System.out.println("## Failed: The same instance was recorded!"); throw new Exception("Test FAILED!"); } System.out.println("Did not detect any duplicate messages."); System.out.println("Test passed."); } catch (Exception e) { System.out.println("Unexpected Exception: "+e); //e.printStackTrace(); throw new RuntimeException("Test FAILED!"); } finally { s.close(); } }
Example 8
Source File: LoopIAE.java From openjdk-jdk9 with GNU General Public License v2.0 | 4 votes |
public static void main(String[] argv) throws Exception { if (!hasSequencer()) { return; } Sequencer sequencer = MidiSystem.getSequencer(); Sequence sequence = new Sequence(Sequence.PPQ, 240); Track track = sequence.createTrack(); 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)); track.add(new MidiEvent(MidiMsg3(ShortMessage.NOTE_ON+9,45,100),10*20)); track.add(new MidiEvent(MidiMsg3(ShortMessage.NOTE_ON+9,45,0),10*20 + 10)); try { sequencer.open(); sequencer.setSequence(sequence); sequencer.setTempoInBPM(100); System.out.println("Setting loop end point to 1"); sequencer.setLoopEndPoint(1); System.out.println(" -> effectively: "+sequencer.getLoopEndPoint()); System.out.println("Setting loop start point to 2 -- should throw IAE"); sequencer.setLoopStartPoint(2); System.out.println(" -> effectively: "+sequencer.getLoopStartPoint()); System.out.println("No IllegalArgumentException was thrown!"); failed = true; } catch (IllegalArgumentException iae) { System.out.println("IAE was thrown correctly."); } catch (MidiUnavailableException mue) { System.out.println("MidiUnavailableException was thrown: " + mue); System.out.println("Cannot execute test."); } catch (InvalidMidiDataException imEx) { System.out.println("InvalidMidiDataException was thrown."); imEx.printStackTrace(); System.out.println("Cannot execute test."); } finally { if (sequencer != null && sequencer.isOpen()) { sequencer.close(); } } if (failed) { throw new Exception("Test FAILED!"); } System.out.println("test passed."); }
Example 9
Source File: StandardMidiFileReader.java From openjdk-8-source with GNU General Public License v2.0 | 4 votes |
void readTrack(Track track) throws IOException, InvalidMidiDataException { try { // reset current tick to 0 long tick = 0; // reset current status byte to 0 (invalid value). // this should cause us to throw an InvalidMidiDataException if we don't // get a valid status byte from the beginning of the track. int status = 0; boolean endOfTrackFound = false; while (!trackFinished() && !endOfTrackFound) { MidiMessage message; int data1 = -1; // initialize to invalid value int data2 = 0; // each event has a tick delay and then the event data. // first read the delay (a variable-length int) and update our tick value tick += readVarInt(); // check for new status int byteValue = readUnsigned(); if (byteValue >= 0x80) { status = byteValue; } else { data1 = byteValue; } switch (status & 0xF0) { case 0x80: case 0x90: case 0xA0: case 0xB0: case 0xE0: // two data bytes if (data1 == -1) { data1 = readUnsigned(); } data2 = readUnsigned(); message = new FastShortMessage(status | (data1 << 8) | (data2 << 16)); break; case 0xC0: case 0xD0: // one data byte if (data1 == -1) { data1 = readUnsigned(); } message = new FastShortMessage(status | (data1 << 8)); break; case 0xF0: // sys-ex or meta switch(status) { case 0xF0: case 0xF7: // sys ex int sysexLength = (int) readVarInt(); byte[] sysexData = new byte[sysexLength]; read(sysexData); SysexMessage sysexMessage = new SysexMessage(); sysexMessage.setMessage(status, sysexData, sysexLength); message = sysexMessage; break; case 0xFF: // meta int metaType = readUnsigned(); int metaLength = (int) readVarInt(); byte[] metaData = new byte[metaLength]; read(metaData); MetaMessage metaMessage = new MetaMessage(); metaMessage.setMessage(metaType, metaData, metaLength); message = metaMessage; if (metaType == 0x2F) { // end of track means it! endOfTrackFound = true; } break; default: throw new InvalidMidiDataException("Invalid status byte: " + status); } // switch sys-ex or meta break; default: throw new InvalidMidiDataException("Invalid status byte: " + status); } // switch track.add(new MidiEvent(message, tick)); } // while } catch (ArrayIndexOutOfBoundsException e) { if (DEBUG) e.printStackTrace(); // fix for 4834374 throw new EOFException("invalid MIDI file"); } }
Example 10
Source File: StandardMidiFileReader.java From openjdk-8 with GNU General Public License v2.0 | 4 votes |
void readTrack(Track track) throws IOException, InvalidMidiDataException { try { // reset current tick to 0 long tick = 0; // reset current status byte to 0 (invalid value). // this should cause us to throw an InvalidMidiDataException if we don't // get a valid status byte from the beginning of the track. int status = 0; boolean endOfTrackFound = false; while (!trackFinished() && !endOfTrackFound) { MidiMessage message; int data1 = -1; // initialize to invalid value int data2 = 0; // each event has a tick delay and then the event data. // first read the delay (a variable-length int) and update our tick value tick += readVarInt(); // check for new status int byteValue = readUnsigned(); if (byteValue >= 0x80) { status = byteValue; } else { data1 = byteValue; } switch (status & 0xF0) { case 0x80: case 0x90: case 0xA0: case 0xB0: case 0xE0: // two data bytes if (data1 == -1) { data1 = readUnsigned(); } data2 = readUnsigned(); message = new FastShortMessage(status | (data1 << 8) | (data2 << 16)); break; case 0xC0: case 0xD0: // one data byte if (data1 == -1) { data1 = readUnsigned(); } message = new FastShortMessage(status | (data1 << 8)); break; case 0xF0: // sys-ex or meta switch(status) { case 0xF0: case 0xF7: // sys ex int sysexLength = (int) readVarInt(); byte[] sysexData = new byte[sysexLength]; read(sysexData); SysexMessage sysexMessage = new SysexMessage(); sysexMessage.setMessage(status, sysexData, sysexLength); message = sysexMessage; break; case 0xFF: // meta int metaType = readUnsigned(); int metaLength = (int) readVarInt(); byte[] metaData = new byte[metaLength]; read(metaData); MetaMessage metaMessage = new MetaMessage(); metaMessage.setMessage(metaType, metaData, metaLength); message = metaMessage; if (metaType == 0x2F) { // end of track means it! endOfTrackFound = true; } break; default: throw new InvalidMidiDataException("Invalid status byte: " + status); } // switch sys-ex or meta break; default: throw new InvalidMidiDataException("Invalid status byte: " + status); } // switch track.add(new MidiEvent(message, tick)); } // while } catch (ArrayIndexOutOfBoundsException e) { if (DEBUG) e.printStackTrace(); // fix for 4834374 throw new EOFException("invalid MIDI file"); } }
Example 11
Source File: MusReader.java From mochadoom with GNU General Public License v3.0 | 4 votes |
long appendTo(Track track, long tick) { for (MidiMessage msg: messages) { track.add(new MidiEvent(msg, tick)); } return tick + delay * 3; }
Example 12
Source File: FinnwMusicModule.java From mochadoom with GNU General Public License v3.0 | 4 votes |
long appendTo(Track track, long pos, int scale) { for (MidiMessage msg: messages) { track.add(new MidiEvent(msg, pos)); } return pos + delay * scale; }