javax.sound.midi.MidiEvent Java Examples
The following examples show how to use
javax.sound.midi.MidiEvent.
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: MidiRealTime.java From haxademic with MIT License | 6 votes |
public static MidiEvent getNextEvent() { ArrayList<MidiEvent> nextEvent = new ArrayList<MidiEvent>(); ArrayList<Integer> trackOfNextEvent = new ArrayList<Integer>(); for (int track = 0; track < seq.getTracks().length; track ++) { if (seq.getTracks()[track].size()-1 > (nextMessageOf.get(track))) { nextEvent.add(seq.getTracks()[track].get(nextMessageOf.get(track))); trackOfNextEvent.add(track); } } if (nextEvent.size() == 0) return null; int closestMessage = 0; int smallestTick = (int)nextEvent.get(0).getTick(); for (int trialMessage = 1; trialMessage < nextEvent.size(); trialMessage ++) { if ((int)nextEvent.get(trialMessage).getTick() < smallestTick) { smallestTick = (int)nextEvent.get(trialMessage).getTick(); closestMessage = trialMessage; } } currentTrack = trackOfNextEvent.get(closestMessage); nextMessageOf.set(currentTrack,(nextMessageOf.get(currentTrack)+1)); return nextEvent.get(closestMessage); }
Example #3
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 #4
Source File: MidiToAudioWriter.java From tuxguitar with GNU Lesser General Public License v2.1 | 5 votes |
private static void sort(List<MidiEvent> events){ Collections.sort(events, new Comparator<MidiEvent>() { public int compare(MidiEvent e1, MidiEvent e2) { if( e1 != null && e2 != null ){ if(e1.getTick() > e2.getTick()){ return 1; } else if(e1.getTick() < e2.getTick()){ return -1; } } return 0; } }); }
Example #5
Source File: MidiRealTime.java From haxademic with MIT License | 5 votes |
public static boolean changeTemp(MidiEvent event) { if ((int)Integer.valueOf((""+Integer.toString((int)event.getMessage().getStatus(), 16).toUpperCase().charAt(0)), 16) == 15 && (int)Integer.valueOf((""+((String)(Integer.toString((int)event.getMessage().getStatus(), 16).toUpperCase())).charAt(1)), 16) == 15 && Integer.toString((int)event.getMessage().getMessage()[1],16).toUpperCase().length() == 2 && Integer.toString((int)event.getMessage().getMessage()[1],16).toUpperCase().equals("51") && Integer.toString((int)event.getMessage().getMessage()[2],16).toUpperCase().equals("3")) return true; return false; }
Example #6
Source File: MidiSynth.java From jmg with GNU General Public License v2.0 | 5 votes |
/** * Create a Control Change event * @param int channel is the channel to use * @param int controlNum is the control change number to use * @param int value is the value of the control change */ protected static MidiEvent createCChangeEvent(int channel, int controlNum, int value, long tick) throws InvalidMidiDataException{ ShortMessage msg = new ShortMessage(); msg.setMessage(0xB0 + channel, controlNum, value); MidiEvent evt = new MidiEvent(msg, tick); return evt; }
Example #7
Source File: MidiSynth.java From jmg with GNU General Public License v2.0 | 5 votes |
/** * Create a Program Change Event * @param int channel is the channel to change * @param int value is the new value to use * @param long tick is the time this event occurs */ protected static MidiEvent createProgramChangeEvent(int channel, int value, long tick) throws InvalidMidiDataException { ShortMessage msg = new ShortMessage(); msg.setMessage(0xC0 + channel, value, 0); MidiEvent evt = new MidiEvent(msg, tick); return evt; }
Example #8
Source File: MidiSynth.java From jmg with GNU General Public License v2.0 | 5 votes |
/** * Create a Note Off Event * @param int channel is the channel to change * @param int pitch is the pitch of the note * @param int velocity is the velocity of the note * @param long tick is the time this event occurs */ protected static MidiEvent createNoteOffEvent(int channel, int pitch, int velocity, long tick) throws InvalidMidiDataException { ShortMessage msg = new ShortMessage(); msg.setMessage(0x80 + channel, pitch, velocity); MidiEvent evt = new MidiEvent(msg, tick); return evt; }
Example #9
Source File: MidiSynth.java From jmg with GNU General Public License v2.0 | 5 votes |
/** * Create a Note On Event * @param int channel is the channel to change * @param int pitch is the pitch of the note * @param int velocity is the velocity of the note * @param long tick is the time this event occurs */ protected static MidiEvent createNoteOnEvent(int channel, int pitch, int velocity, long tick) throws InvalidMidiDataException { ShortMessage msg = new ShortMessage(); msg.setMessage(0x90 + channel, pitch, velocity); MidiEvent evt = new MidiEvent(msg, tick); return evt; }
Example #10
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 #11
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 #12
Source File: MidiToAudioWriter.java From tuxguitar with GNU Lesser General Public License v2.1 | 5 votes |
public static void write(OutputStream out, List<MidiEvent> events, MidiToAudioSettings settings) throws Throwable { MidiToAudioSynth.instance().openSynth(); MidiToAudioSynth.instance().loadSoundbank(getPatchs(events), settings.getSoundbankPath()); int usqTempo = 60000000 / 120; long previousTick = 0; long timePosition = 0; MidiToAudioWriter.sort(events); Receiver receiver = MidiToAudioSynth.instance().getReceiver(); AudioInputStream stream = MidiToAudioSynth.instance().getStream(); Iterator<MidiEvent> it = events.iterator(); while(it.hasNext()){ MidiEvent event = (MidiEvent)it.next(); MidiMessage msg = event.getMessage(); timePosition += ( (event.getTick() - previousTick) * usqTempo) / TGDuration.QUARTER_TIME; if (msg instanceof MetaMessage) { if (((MetaMessage) msg).getType() == 0x51) { byte[] data = ((MetaMessage) msg).getData(); usqTempo = ((data[0] & 0xff) << 16) | ((data[1] & 0xff) << 8) | (data[2] & 0xff); } } else { receiver.send(msg, timePosition); } previousTick = event.getTick(); } long duration = (long) (stream.getFormat().getFrameRate() * ( (timePosition / 1000000.0) )); AudioInputStream srcStream = new AudioInputStream(stream, stream.getFormat(), duration ); AudioInputStream dstStream = AudioSystem.getAudioInputStream(settings.getFormat(), srcStream ); AudioSystem.write(new AudioInputStream(dstStream, dstStream.getFormat(), duration ), settings.getType(), out); dstStream.close(); srcStream.close(); MidiToAudioSynth.instance().closeSynth(); }
Example #13
Source File: MidiSequenceHandlerImpl.java From tuxguitar with GNU Lesser General Public License v2.1 | 5 votes |
public void addProgramChange(long tick,int track,int channelId, int instrument) { GMChannelRoute gmChannel = this.router.getRoute(channelId); if( gmChannel != null ){ this.events.add(new MidiEvent(MidiMessageUtils.programChange(gmChannel.getChannel1(), instrument), tick )); if( gmChannel.getChannel1() != gmChannel.getChannel2() ){ this.events.add(new MidiEvent(MidiMessageUtils.programChange(gmChannel.getChannel2(), instrument), tick )); } } }
Example #14
Source File: MidiSequenceHandlerImpl.java From tuxguitar with GNU Lesser General Public License v2.1 | 5 votes |
public void addControlChange(long tick,int track,int channelId, int controller, int value) { GMChannelRoute gmChannel = this.router.getRoute(channelId); if( gmChannel != null ){ this.events.add(new MidiEvent(MidiMessageUtils.controlChange(gmChannel.getChannel1(), controller, value), tick )); if( gmChannel.getChannel1() != gmChannel.getChannel2() ){ this.events.add(new MidiEvent(MidiMessageUtils.controlChange(gmChannel.getChannel2(), controller, value), tick )); } } }
Example #15
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 #16
Source File: MetaCallback.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
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+"!"); } }
Example #17
Source File: SeqRecordDoesNotCopy.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
public static void printEvent(MidiEvent event) { MidiMessage message = event.getMessage(); long tick = event.getTick(); byte[] data = message.getMessage(); StringBuffer sb = new StringBuffer((data.length * 3) - 1); for (int i = 0; i < data.length; i++) { sb.append(toHexByteString(data[i])); if (i < data.length - 1) sb.append(' '); } System.out.printf("%5d: %s%n", tick, sb); }
Example #18
Source File: SeqRecordsRealTimeEvents.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
public static void printEvent(MidiEvent event) { MidiMessage message = event.getMessage(); long tick = event.getTick(); byte[] data = message.getMessage(); StringBuffer sb = new StringBuffer((data.length * 3) - 1); for (int i = 0; i < data.length; i++) { sb.append(toHexByteString(data[i])); if (i < data.length - 1) sb.append(' '); } System.out.printf("%5d: %s%n", tick, sb); }
Example #19
Source File: TrackAddSameTick.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
public static MidiEvent createEvent(long tick, int expectedPos) throws Exception { ShortMessage msg = new ShortMessage(); msg.setMessage(0x90, (int) expectedPos, 00); MidiEvent ev = new MidiEvent(msg, tick); return ev; }
Example #20
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 #21
Source File: RealTimeSequencer.java From openjdk-jdk9 with GNU General Public License v2.0 | 4 votes |
/** * chase all events from beginning of Track * and send note off for those events that are active * in noteOnCache array. * It is possible, of course, to catch notes from other tracks, * but better than more complicated logic to detect * which notes are really from this track */ private void sendNoteOffIfOn(Track track, long endTick) { int size = track.size(); int done = 0; try { for (int i = 0; i < size; i++) { MidiEvent event = track.get(i); if (event.getTick() > endTick) break; MidiMessage msg = event.getMessage(); int status = msg.getStatus(); int len = msg.getLength(); if (len == 3 && ((status & 0xF0) == ShortMessage.NOTE_ON)) { int note = -1; if (msg instanceof ShortMessage) { ShortMessage smsg = (ShortMessage) msg; if (smsg.getData2() > 0) { // only consider Note On with velocity > 0 note = smsg.getData1(); } } else { byte[] data = msg.getMessage(); if ((data[2] & 0x7F) > 0) { // only consider Note On with velocity > 0 note = data[1] & 0x7F; } } if (note >= 0) { int bit = 1<<(status & 0x0F); if ((noteOnCache[note] & bit) != 0) { // the bit is set. Send Note Off getTransmitterList().sendMessage(status | (note<<8), -1); // clear the bit noteOnCache[note] &= (0xFFFF ^ bit); done++; } } } } } catch (ArrayIndexOutOfBoundsException aioobe) { // this happens when messages are removed // from the track while this method executes } if (DEBUG_PUMP) Printer.println(" sendNoteOffIfOn: sent "+done+" messages."); }
Example #22
Source File: MIDISequenceRenderer.java From haxademic with MIT License | 4 votes |
public MidiSequenceEvent( MidiEvent event, ShortMessage sm, float tickTime ) { _sm = sm; _event = event; _tickTimeSeconds = tickTime; }
Example #23
Source File: RealTimeSequencer.java From Bytecoder with Apache License 2.0 | 4 votes |
/** * chase all events from beginning of Track * and send note off for those events that are active * in noteOnCache array. * It is possible, of course, to catch notes from other tracks, * but better than more complicated logic to detect * which notes are really from this track */ private void sendNoteOffIfOn(Track track, long endTick) { int size = track.size(); int done = 0; try { for (int i = 0; i < size; i++) { MidiEvent event = track.get(i); if (event.getTick() > endTick) break; MidiMessage msg = event.getMessage(); int status = msg.getStatus(); int len = msg.getLength(); if (len == 3 && ((status & 0xF0) == ShortMessage.NOTE_ON)) { int note = -1; if (msg instanceof ShortMessage) { ShortMessage smsg = (ShortMessage) msg; if (smsg.getData2() > 0) { // only consider Note On with velocity > 0 note = smsg.getData1(); } } else { byte[] data = msg.getMessage(); if ((data[2] & 0x7F) > 0) { // only consider Note On with velocity > 0 note = data[1] & 0x7F; } } if (note >= 0) { int bit = 1<<(status & 0x0F); if ((noteOnCache[note] & bit) != 0) { // the bit is set. Send Note Off getTransmitterList().sendMessage(status | (note<<8), -1); // clear the bit noteOnCache[note] &= (0xFFFF ^ bit); done++; } } } } } catch (ArrayIndexOutOfBoundsException aioobe) { // this happens when messages are removed // from the track while this method executes } }
Example #24
Source File: MidiRealTime.java From haxademic with MIT License | 4 votes |
public static boolean noteIsOff(MidiEvent event) { if (Integer.toString((int)event.getMessage().getStatus(), 16).toUpperCase().charAt(0) == '8' || (noteIsOn(event) && event.getMessage().getLength() >= 3 && ((int)event.getMessage().getMessage()[2] & 0xFF) == 0)) return true; return false; }
Example #25
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 #26
Source File: Midi2WavRenderer.java From computoser with GNU Affero General Public License v3.0 | 4 votes |
public static double send(Sequence seq, Receiver recv) { float divtype = seq.getDivisionType(); assert (seq.getDivisionType() == Sequence.PPQ); Track[] tracks = seq.getTracks(); int[] trackspos = new int[tracks.length]; int mpq = 500000; int seqres = seq.getResolution(); long lasttick = 0; long curtime = 0; while (true) { MidiEvent selevent = null; int seltrack = -1; for (int i = 0; i < tracks.length; i++) { int trackpos = trackspos[i]; Track track = tracks[i]; if (trackpos < track.size()) { MidiEvent event = track.get(trackpos); if (selevent == null || event.getTick() < selevent.getTick()) { selevent = event; seltrack = i; } } } if (seltrack == -1) break; trackspos[seltrack]++; long tick = selevent.getTick(); if (divtype == Sequence.PPQ) curtime += ((tick - lasttick) * mpq) / seqres; else curtime = (long) ((tick * 1000000.0 * divtype) / seqres); lasttick = tick; MidiMessage msg = selevent.getMessage(); if (msg instanceof MetaMessage) { if (divtype == Sequence.PPQ) if (((MetaMessage) msg).getType() == 0x51) { byte[] data = ((MetaMessage) msg).getData(); mpq = ((data[0] & 0xff) << 16) | ((data[1] & 0xff) << 8) | (data[2] & 0xff); } } else { if (recv != null) recv.send(msg, curtime); } } return curtime / 1000000.0; }
Example #27
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; }
Example #28
Source File: MidiRealTime.java From haxademic with MIT License | 4 votes |
public static boolean noteIsOn(MidiEvent event) { if (Integer.toString(event.getMessage().getStatus(), 16).toUpperCase().charAt(0) == '9') return true; return false; }
Example #29
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 #30
Source File: MidiSequenceHandlerImpl.java From tuxguitar with GNU Lesser General Public License v2.1 | 4 votes |
public void addProgramChange(long tick,int track,int channel, int instrument) { addEvent(track,new MidiEvent(MidiMessageFactory.programChange(channel, instrument), tick )); }