private void readTrack(DataInputStream dataInputStream, Track track) throws InvalidMidiDataException, IOException { // search for a "MTrk" chunk while (true) { int nMagic = dataInputStream.readInt(); if (nMagic == MidiConstants.TRACK_MAGIC) { break; } int nChunkLength = dataInputStream.readInt(); if (nChunkLength % 2 != 0) { nChunkLength++; } dataInputStream.skip(nChunkLength); } int nTrackChunkLength = dataInputStream.readInt(); long lTicks = 0; long[] alRemainingBytes = new long[1]; alRemainingBytes[0] = nTrackChunkLength; int[] anRunningStatusByte = new int[1]; // indicates no running status in effect anRunningStatusByte[0] = -1; while (alRemainingBytes[0] > 0) { long lDeltaTicks = readVariableLengthQuantity(dataInputStream, alRemainingBytes); // TDebug.out("delta ticks: " + lDeltaTicks); lTicks += lDeltaTicks; MidiEvent event = readEvent(dataInputStream, alRemainingBytes, anRunningStatusByte, lTicks); track.add(event); } }
public static int readUnsignedByte(DataInputStream dataInputStream, long[] alRemainingBytes) throws IOException { int nByte = dataInputStream.readUnsignedByte(); // already done in DataInputStream.readUnsignedByte(); // if (nByte < 0) // { // throw new EOFException(); // } alRemainingBytes[0]--; return nByte; }
/** Loads a Sound from an AudioInputStream. */ public Sound getSound(AudioInputStream audioStream) { if (audioStream == null) { return null; } // get the number of bytes to read int length = (int) (audioStream.getFrameLength() * audioStream.getFormat().getFrameSize()); // read the entire stream byte[] samples = new byte[length]; DataInputStream is = new DataInputStream(audioStream); try { is.readFully(samples); is.close(); } catch (IOException ex) { ex.printStackTrace(); } // return the samples return new Sound(samples); }
public MidiFileFormat getMidiFileFormat(InputStream inputStream) throws InvalidMidiDataException, IOException { DataInputStream dataInputStream = new DataInputStream(inputStream); int nHeaderMagic = dataInputStream.readInt(); if (nHeaderMagic != MidiConstants.HEADER_MAGIC) { throw new InvalidMidiDataException("not a MIDI file: wrong header magic"); } int nHeaderLength = dataInputStream.readInt(); if (nHeaderLength < 6) { throw new InvalidMidiDataException("corrupt MIDI file: wrong header length"); } int nType = dataInputStream.readShort(); if (nType < 0 || nType > 2) { throw new InvalidMidiDataException("corrupt MIDI file: illegal type"); } if (nType == 2) { throw new InvalidMidiDataException("this implementation doesn't support type 2 MIDI files"); } int nNumTracks = dataInputStream.readShort(); if (nNumTracks <= 0) { throw new InvalidMidiDataException("corrupt MIDI file: number of tracks must be positive"); } if (nType == 0 && nNumTracks != 1) { throw new InvalidMidiDataException( "corrupt MIDI file: type 0 files must contain exactely one track"); } int nDivision = dataInputStream.readUnsignedShort(); float fDivisionType = -1.0F; int nResolution = -1; if ((nDivision & 0x8000) != 0) // frame division { int nFrameType = -((nDivision >>> 8) & 0xFF); switch (nFrameType) { case 24: fDivisionType = Sequence.SMPTE_24; break; case 25: fDivisionType = Sequence.SMPTE_25; break; case 29: fDivisionType = Sequence.SMPTE_30DROP; break; case 30: fDivisionType = Sequence.SMPTE_30; break; default: throw new InvalidMidiDataException("corrupt MIDI file: illegal frame division type"); } nResolution = nDivision & 0xff; } else // BPM division { fDivisionType = Sequence.PPQ; nResolution = nDivision & 0x7fff; } // skip additional bytes in the header dataInputStream.skip(nHeaderLength - 6); MidiFileFormat midiFileFormat = new TMidiFileFormat( nType, fDivisionType, nResolution, MidiFileFormat.UNKNOWN_LENGTH, MidiFileFormat.UNKNOWN_LENGTH, nNumTracks); return midiFileFormat; }
public void play(String name, String version, String vendor, String desc) throws Exception { Info port = null; for (Info info : MidiSystem.getMidiDeviceInfo()) { if (name == null && version == null && vendor == null && desc == null) { System.err.println( "At least one of name, version, vendor, or description must be specified."); break; } if (name != null && !info.getName().equals(name)) { continue; } if (vendor != null && !info.getVendor().equals(vendor)) { continue; } if (version != null && !info.getVersion().equals(version)) { continue; } if (desc != null && !info.getDescription().equals(desc)) { continue; } try (MidiDevice dev = MidiSystem.getMidiDevice(info)) { Receiver r = null; try { r = dev.getReceiver(); } catch (Exception e) { r = null; } if (r == null) { continue; } } if (port != null) { System.err.println( "Multiple MIDI ports match the given parameters. Please be more specific."); port = null; break; } port = info; continue; } if (port == null) { System.err.println("Unable to locate MIDI port"); System.err.println("Available ports:"); for (Info info : MidiSystem.getMidiDeviceInfo()) { try (MidiDevice dev = MidiSystem.getMidiDevice(info)) { Receiver r = null; try { r = dev.getReceiver(); } catch (Exception e) { r = null; } if (r == null) { continue; } } System.out.println( "Device [name=" + info.getName() + ", version=" + info.getVersion() + ", vendor=" + info.getVendor() + ", desc=" + info.getDescription() + "]"); } return; } System.out.println( "Using device [name=" + port.getName() + ", version=" + port.getVersion() + ", vendor=" + port.getVendor() + ", desc=" + port.getDescription() + "]"); try (MidiDevice dev = MidiSystem.getMidiDevice(port)) { int channel = CHANNEL_RAW_STEREO; dev.open(); try (Receiver rx = dev.getReceiver()) { rxCleanup = rx; try (DataInputStream dis = new DataInputStream(new FileInputStream(ymz))) { resetController(rx, channel, registers); long startTime = System.nanoTime(); // read a buffer long prevClock = 0; while (true) { long clock = dis.readLong(); byte register = dis.readByte(); byte value = dis.readByte(); if (prevClock != clock) { sendAll(rx, channel, registers); prevClock = clock; } sleepUntil(startTime, clock); System.out.println( "Clock: " + clock + " register: " + register + ", value: " + (value & 0xff)); if (register < 0 || register > 13) { System.err.println("Invalid register " + register + " found, ignoring."); continue; } registers[register] = value; } } catch (EOFException e) { System.err.println("EOF reached"); } finally { resetController(rx, channel, registers); rxCleanup = null; } } } }