private void loadNotes() { int program = 0; HashMap<Integer, Float> lastTimeNote = new HashMap<Integer, Float>(); HashMap<Float, Integer> secondsNotes = new HashMap<Float, Integer>(); this.notas = new ArrayList<MIDINote>(); for (Track track : sequencia.getTracks()) { for (int c = 0; c < track.size(); ++c) { MidiEvent event = track.get(c); MidiMessage msg = event.getMessage(); if (msg instanceof ShortMessage) { ShortMessage shortmsg = (ShortMessage) msg; if (shortmsg.getCommand() == ShortMessage.PROGRAM_CHANGE) { program = shortmsg.getData1(); } else { // }else if(program>=25 && program <= 40){ // else if(program== 30){ if (shortmsg.getCommand() == ShortMessage.NOTE_ON) { MIDINote midiNote = new MIDINote(event, sequencia, tempoProcessor, program); // tocador.start(); int noteChord = midiNote.getChord(); float noteSecond = midiNote.getSecond(); if (!lastTimeNote.containsKey(noteChord)) { lastTimeNote.put(noteChord, 0.0f); } if (noteSecond - lastTimeNote.get(noteChord).floatValue() <= this.interval) { continue; } lastTimeNote.put(noteChord, noteSecond); // System.out.println("Play chord "+noteChord+" in "+noteSecond+" seconds"); notas.add(midiNote); if (!secondsNotes.containsKey(noteSecond)) { secondsNotes.put(noteSecond, 1); } secondsNotes.put(noteSecond, secondsNotes.get(noteSecond).intValue() + 1); } } } } } // System.out.println("tamanho da pista "+notas.size()+" e track "+maxNote); for (float second : secondsNotes.keySet()) { int repeated = secondsNotes.get(second).intValue(); if (repeated > maxNote) { this.maxNote = repeated; } } this.notesLength = secondsNotes.size(); // GameEngine.getInstance().setFramesPerSecond((int)(((tocador.getMicrosecondLength()/1000000)/(notas.size()*1.0))*4000)); // System.out.println("(int)(("+sequencia.getMicrosecondLength()+"/1000000)/"+notas.size()+"="+(int)((sequencia.getMicrosecondLength()/1000000)/notas.size())) }
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 = 60000000 / 100; 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; }
private void init(Track trkcand) { Points = new double[trkcand.size()][3]; // CorrPoints = new double[trkcand.size()][3] ; cosEntAnglesPlanes = new double[trkcand.size()]; Track trkcandcopy = new Track(); trkcandcopy.addAll(trkcand); trkcandcopy.set_HelicalTrack(trkcand.get_Helix()); trkcandcopy.set_Helix(trkcand.get_Helix()); this.set_UpdatedTrack(trkcandcopy); for (int m = 0; m < trkcand.size(); m++) { Points[m][0] = trkcand.get(m).get_Point().x(); Points[m][1] = trkcand.get(m).get_Point().y(); Points[m][2] = trkcand.get(m).get_Point().z(); double x = trkcand .get_Helix() .getPointAtRadius( Math.sqrt(Points[m][0] * Points[m][0] + Points[m][1] * Points[m][1])) .x(); double ux = trkcand .get_Helix() .getTrackDirectionAtRadius( Math.sqrt(Points[m][0] * Points[m][0] + Points[m][1] * Points[m][1])) .x(); double y = trkcand .get_Helix() .getPointAtRadius( Math.sqrt(Points[m][0] * Points[m][0] + Points[m][1] * Points[m][1])) .y(); double uy = trkcand .get_Helix() .getTrackDirectionAtRadius( Math.sqrt(Points[m][0] * Points[m][0] + Points[m][1] * Points[m][1])) .y(); double z = trkcand .get_Helix() .getPointAtRadius( Math.sqrt(Points[m][0] * Points[m][0] + Points[m][1] * Points[m][1])) .z(); double uz = trkcand .get_Helix() .getTrackDirectionAtRadius( Math.sqrt(Points[m][0] * Points[m][0] + Points[m][1] * Points[m][1])) .z(); double cosEntranceAngle = Math.abs((x * ux + y * uy + z * uz) / Math.sqrt(x * x + y * y + z * z)); cosEntAnglesPlanes[m] = cosEntranceAngle; } }
/** * Obtains the duration of this sequence, expressed in microseconds. * * @return this sequence's duration in microseconds. */ public long getMicrosecondLength() { long ticks = getTickLength(); double seconds; // now convert ticks to time if (divisionType != PPQ) { seconds = ((double) getTickLength() / (double) (divisionType * resolution)); // $$fb 2002-10-30: fix for 4702328: Wrong time in sequence for SMPTE based types return (long) (1000000 * seconds); } else { Track tempos = new Track(); Track tmpTrack = null; MidiEvent tmpEvent = null; MidiMessage tmpMessage = null; MetaMessage tmpMeta = null; byte[] data; // find all tempo events synchronized (tracks) { for (int i = 0; i < tracks.size(); i++) { tmpTrack = (Track) tracks.elementAt(i); for (int j = 0; j < tmpTrack.size(); j++) { tmpEvent = (MidiEvent) tmpTrack.get(j); tmpMessage = tmpEvent.getMessage(); if (tmpMessage instanceof MetaMessage) { if (((MetaMessage) tmpMessage).getType() == 0x51) { tempos.add(tmpEvent); } } } } } // now add up chunks of time int tempo = (60 * 1000000) / 120; // default 120 bpm, converted to uSec/beat long microseconds = 0; long runningTick = 0; long tmpTick = 0; // loop through the tempo changes, but don't // include the last event, which is track end for (int i = 0; i < (tempos.size() - 1); i++) { tmpEvent = (MidiEvent) tempos.get(i); tmpTick = tmpEvent.getTick(); if (tmpTick >= runningTick) { microseconds += ((tmpTick - runningTick) * tempo / resolution); runningTick = tmpTick; data = ((MetaMessage) (tmpEvent.getMessage())).getMessage(); // data[0] => status, 0xFF // data[1] => type, 0x51 // data[2] => length, 0x03 // data[3] -> data[5] => tempo data tempo = (int) 0xff & data[5]; tempo = tempo | ((0xff & data[4]) << 8); tempo = tempo | ((0xff & data[3]) << 16); } } tmpTick = getTickLength(); if (tmpTick > runningTick) { microseconds += ((tmpTick - runningTick) * tempo / resolution); } // return in microseconds return (microseconds); } }