Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 22e78c65 authored by Mike Lockwood's avatar Mike Lockwood Committed by Android (Google) Code Review
Browse files

Merge "Add MidiReceiver.send() and MidiReceiver.getMaxMessageSize()"

parents 38bcb8c9 0c7342f0
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -75,9 +75,9 @@ public final class MidiDispatcher extends MidiReceiver {
    public void receive(byte[] msg, int offset, int count, long timestamp) throws IOException {
       for (MidiReceiver receiver : mReceivers) {
            try {
                receiver.receive(msg, offset, count, timestamp);
                receiver.send(msg, offset, count, timestamp);
            } catch (IOException e) {
                // if the receiver fails we remove the receiver but do not propogate the exception
                // if the receiver fails we remove the receiver but do not propagate the exception
                mReceivers.remove(receiver);
            }
        }
+13 −10
Original line number Diff line number Diff line
@@ -81,19 +81,22 @@ public final class MidiInputPort extends MidiReceiver implements Closeable {
     *                  {@link java.lang.System#nanoTime}
     */
    public void receive(byte[] msg, int offset, int count, long timestamp) throws IOException {
        assert(offset >= 0 && count >= 0 && offset + count <= msg.length);
        if (offset < 0 || count < 0 || offset + count > msg.length) {
            throw new IllegalArgumentException("offset or count out of range");
        }
        if (count > MidiPortImpl.MAX_PACKET_DATA_SIZE) {
            throw new IllegalArgumentException("count exceeds max message size");
        }

        synchronized (mBuffer) {
            while (count > 0) {
            int length = MidiPortImpl.packMessage(msg, offset, count, timestamp, mBuffer);
            mOutputStream.write(mBuffer, 0, length);
                int sent = MidiPortImpl.getMessageSize(mBuffer, length);
                assert(sent >= 0 && sent <= length);

                offset += sent;
                count -= sent;
        }
    }

    @Override
    public int getMaxMessageSize() {
        return MidiPortImpl.MAX_PACKET_DATA_SIZE;
    }

    @Override
+1 −1
Original line number Diff line number Diff line
@@ -68,7 +68,7 @@ public final class MidiOutputPort extends MidiSender implements Closeable {
                    long timestamp = MidiPortImpl.getMessageTimeStamp(buffer, count);

                    // dispatch to all our receivers
                    mDispatcher.receive(buffer, offset, size, timestamp);
                    mDispatcher.send(buffer, offset, size, timestamp);
                }
            } catch (IOException e) {
                // FIXME report I/O failure?
+31 −0
Original line number Diff line number Diff line
@@ -27,6 +27,7 @@ import java.io.IOException;
abstract public class MidiReceiver {
    /**
     * Called to pass MIDI data to the receiver.
     * May fail if count exceeds {@link getMaxMessageSize}.
     *
     * NOTE: the msg array parameter is only valid within the context of this call.
     * The msg bytes should be copied by the receiver rather than retaining a reference
@@ -42,4 +43,34 @@ abstract public class MidiReceiver {
     */
    abstract public void receive(byte[] msg, int offset, int count, long timestamp)
            throws IOException;

    /**
     * Returns the maximum size of a message this receiver can receive.
     * Defaults to {@link java.lang.Integer#MAX_VALUE} unless overridden.
     * @return maximum message size
     */
    public int getMaxMessageSize() {
        return Integer.MAX_VALUE;
    }

    /**
     * Called to send MIDI data to the receiver
     * Data will get split into multiple calls to {@link receive} if count exceeds
     * {@link getMaxMessageSize}.
     *
     * @param msg a byte array containing the MIDI data
     * @param offset the offset of the first byte of the data in the byte array
     * @param count the number of bytes of MIDI data in the array
     * @param timestamp the timestamp of the message (based on {@link java.lang.System#nanoTime}
     * @throws IOException
     */
    public void send(byte[] msg, int offset, int count, long timestamp) throws IOException {
        int messageSize = getMaxMessageSize();
        while (count > 0) {
            int length = (count > messageSize ? messageSize : count);
            receive(msg, offset, length, timestamp);
            offset += length;
            count -= length;
        }
    }
}
+1 −1
Original line number Diff line number Diff line
@@ -144,7 +144,7 @@ public final class UsbMidiDevice implements Closeable {

                                int count = mInputStreams[index].read(buffer);
                                long timestamp = System.nanoTime();
                                outputReceivers[index].receive(buffer, 0, count, timestamp);
                                outputReceivers[index].send(buffer, 0, count, timestamp);
                            } else if ((pfd.revents & (OsConstants.POLLERR
                                                        | OsConstants.POLLHUP)) != 0) {
                                done = true;