/** * Write the Track Chunk * * @param DataOutputStream dos * @param Track track - track to write * @exception IOException */ private void writeTrackChunk(DataOutputStream odos, Track track) throws IOException { if (VERBOSE) System.out.println("Writing MIDI Track"); // Write to temporary stream to buffer disk writes and // calculate the number of bytes written to the stream ByteArrayOutputStream baos = new ByteArrayOutputStream(); DataOutputStream dos = new DataOutputStream(baos); int header = 0x4D54726B; Enumeration aEnum = track.getEvtList().elements(); aEnum = track.getEvtList().elements(); // At this stage Except that all events are NoteOn events while (aEnum.hasMoreElements()) { Event evt = (Event) aEnum.nextElement(); evt.write(dos); if (DEBUG) evt.print(); } // Write to the real stream odos.writeInt(header); odos.writeInt(baos.size()); odos.write(baos.toByteArray(), 0, baos.size()); }
/** * Write the Track Chunk * * @param DataOutputStream dos * @param Track track - track to write * @exception IOException */ @SuppressWarnings("rawtypes") private void writeTrackChunk(DataOutputStream odos, Track track) throws IOException { // Write to temporary stream to buffer disk writes and // calculate the number of bytes written to the stream ByteArrayOutputStream baos = new ByteArrayOutputStream(); DataOutputStream dos = new DataOutputStream(baos); int header = 0x4D54726B; Enumeration en = track.getEvtList().elements(); en = track.getEvtList().elements(); // At this stage Except that all events are NoteOn events while (en.hasMoreElements()) { Event evt = (Event) en.nextElement(); evt.write(dos); if (DEBUG) evt.print(); } // Write to the real stream odos.writeInt(header); odos.writeInt(baos.size()); odos.write(baos.toByteArray(), 0, baos.size()); }
@SuppressWarnings("rawtypes") private static void sortEvents(Vector evtList, Vector phrVct, SMFTools smf, Part part) { double startTime = 0.0; double[] currentLength = new double[100]; Note[] curNote = new Note[100]; int phrIndex = 0; // Go through evts for (int i = 0; i < evtList.size(); i++) { Event evt = (Event) evtList.elementAt(i); startTime += (double) evt.getTime() / (double) smf.getPPQN(); if (evt.getID() == 007) { PChange pchg = (PChange) evt; part.setInstrument(pchg.getValue()); // if this event is a NoteOn event go on } else if (evt.getID() == 005) { NoteOn noteOn = (NoteOn) evt; part.setChannel(noteOn.getMidiChannel()); short pitch = noteOn.getPitch(); int dynamic = noteOn.getVelocity(); short midiChannel = noteOn.getMidiChannel(); // if you're a true NoteOn if (dynamic > 0) { noteOn( phrIndex, curNote, smf, i, currentLength, startTime, phrVct, midiChannel, pitch, dynamic, evtList); } } } }
/** * Reads a MIDI track chunk * * @param DataInputStream dis - the input stream to read from * @exception IOException */ private void readTrackChunk(DataInputStream dis) throws IOException { // local variables for Track class Track track = new Track(); // Insert new Track into a list of tracks this.trackList.addElement(track); int deltaTime = 0; if (VERBOSE) System.out.println("Reading Track .........."); // Read track header if (dis.readInt() != 0x4D54726B) { // If MTrk read is wrong throw new IOException("Track started in wrong place!!!! ABORTING"); } else { // If MTrk read ok get bytesRemaining dis.readInt(); } // loop variables int status, oldStatus = 0, eventLength = 0; // Start gathering event data Event event = null; while (true) { try { // get variable length timestamp deltaTime = MidiUtil.readVarLength(dis); // mark stream so we can return if we need running status dis.mark(2); status = dis.readUnsignedByte(); // decide on running status if (status < 0x80) { // set running status status = oldStatus; // return stream to before status read dis.reset(); } // create default event of correct type if (status >= 0xFF) { // Meta Event int type = dis.readUnsignedByte(); eventLength = MidiUtil.readVarLength(dis); event = MidiUtil.createMetaEvent(type); } else if (status >= 0xF0) { // System Exclusive --- NOT SUPPORTED System.out.println("SysEX---"); eventLength = MidiUtil.readVarLength(dis); } else if (status >= 0x80) { // MIDI voice event short selection = (short) (status / 0x10); short midiChannel = (short) (status - (selection * 0x10)); VoiceEvt evt = (VoiceEvt) MidiUtil.createVoiceEvent(selection); evt.setMidiChannel(midiChannel); event = evt; if (event == null) { throw new IOException("MIDI file read error: invalid voice event type!"); } } oldStatus = status; } catch (Exception e) { e.printStackTrace(); System.exit(1); } if (event != null) { // read data into the new event and // add the new event to the Track object event.setTime(deltaTime); event.read(dis); // if (VERBOSE) event.print(); track.addEvent(event); // event.print(); if (event instanceof EndTrack) break; } else { // skip the stream ahead to next valid event dis.skipBytes(eventLength); } } }