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

Commit 3b766458 authored by Mike Lockwood's avatar Mike Lockwood
Browse files

Make MidiSender and MidiReceiver abstract classes, rename MidiReceiver.post() to receive()

Change-Id: I1cef3bd48ca0acf2968c9de223f78445f3434404
parent 20821ecb
Loading
Loading
Loading
Loading
+4 −4
Original line number Diff line number Diff line
@@ -85,10 +85,10 @@ public final class MidiDeviceServer implements Closeable {

                    outputPort.connect(new MidiReceiver() {
                        @Override
                        public void post(byte[] msg, int offset, int count, long timestamp)
                        public void receive(byte[] msg, int offset, int count, long timestamp)
                                throws IOException {
                            try {
                                inputPortReceviver.post(msg, offset, count, timestamp);
                                inputPortReceviver.receive(msg, offset, count, timestamp);
                            } catch (IOException e) {
                                IoUtils.closeQuietly(mInputPortOutputPorts[portNumberF]);
                                mInputPortOutputPorts[portNumberF] = null;
@@ -125,10 +125,10 @@ public final class MidiDeviceServer implements Closeable {
                final MidiSender sender = mOutputPortDispatchers[portNumber].getSender();
                sender.connect(new MidiReceiver() {
                        @Override
                        public void post(byte[] msg, int offset, int count, long timestamp)
                        public void receive(byte[] msg, int offset, int count, long timestamp)
                                throws IOException {
                            try {
                                inputPort.post(msg, offset, count, timestamp);
                                inputPort.receive(msg, offset, count, timestamp);
                            } catch (IOException e) {
                                IoUtils.closeQuietly(inputPort);
                                sender.disconnect(this);
+4 −4
Original line number Diff line number Diff line
@@ -24,12 +24,12 @@ import java.util.ArrayList;
 * This class subclasses {@link MidiReceiver} and dispatches any data it receives
 * to its receiver list. Any receivers that throw an exception upon receiving data will
 * be automatically removed from the receiver list, but no IOException will be returned
 * from the dispatcher's {@link #post} in that case.
 * from the dispatcher's {@link #receive} in that case.
 *
 * CANDIDATE FOR PUBLIC API
 * @hide
 */
public class MidiDispatcher implements MidiReceiver {
public class MidiDispatcher extends MidiReceiver {

    private final ArrayList<MidiReceiver> mReceivers = new ArrayList<MidiReceiver>();

@@ -71,12 +71,12 @@ public class MidiDispatcher implements MidiReceiver {
    }

    @Override
    public void post(byte[] msg, int offset, int count, long timestamp) throws IOException {
    public void receive(byte[] msg, int offset, int count, long timestamp) throws IOException {
        synchronized (mReceivers) {
           for (int i = 0; i < mReceivers.size(); ) {
                MidiReceiver receiver = mReceivers.get(i);
                try {
                    receiver.post(msg, offset, count, timestamp);
                    receiver.receive(msg, offset, count, timestamp);
                    i++;    // increment only on success. on failure we remove the receiver
                            // so i should not be incremented
                } catch (IOException e) {
+15 −26
Original line number Diff line number Diff line
@@ -30,12 +30,12 @@ import java.io.IOException;
 * CANDIDATE FOR PUBLIC API
 * @hide
 */
public class MidiInputPort implements MidiReceiver, Closeable {
public class MidiInputPort extends MidiReceiver implements Closeable {

    private final int mPortNumber;
    private final FileOutputStream mOutputStream;

    // buffer to use for sending messages out our output stream
    // buffer to use for sending data out our output stream
    private final byte[] mBuffer = new byte[MidiPortImpl.MAX_PACKET_SIZE];

  /* package */ MidiInputPort(ParcelFileDescriptor pfd, int portNumber) {
@@ -52,24 +52,19 @@ public class MidiInputPort implements MidiReceiver, Closeable {
        return mPortNumber;
    }

    //FIXME
    public void onIOException() {
    }

    /**
     * Writes a MIDI message to the input port
     * Writes MIDI data to the input port
     *
     * @param msg byte array containing the message
     * @param offset offset of first byte of the message in msg byte array
     * @param count size of the message in bytes
     * @param timestamp future time to post the message (based on
     * @param msg byte array containing the data
     * @param offset offset of first byte of the data in msg byte array
     * @param count size of the data in bytes
     * @param timestamp future time to post the data (based on
     *                  {@link java.lang.System#nanoTime}
     */
    public void post(byte[] msg, int offset, int count, long timestamp) throws IOException {
    public void receive(byte[] msg, int offset, int count, long timestamp) throws IOException {
        assert(offset >= 0 && count >= 0 && offset + count <= msg.length);

        synchronized (mBuffer) {
            try {
            while (count > 0) {
                int length = MidiPortImpl.packMessage(msg, offset, count, timestamp, mBuffer);
                mOutputStream.write(mBuffer, 0, length);
@@ -79,12 +74,6 @@ public class MidiInputPort implements MidiReceiver, Closeable {
                offset += sent;
                count -= sent;
            }
            } catch (IOException e) {
                IoUtils.closeQuietly(mOutputStream);
                // report I/O failure
                onIOException();
                throw e;
            }
        }
    }

+2 −2
Original line number Diff line number Diff line
@@ -31,7 +31,7 @@ import java.io.IOException;
 * CANDIDATE FOR PUBLIC API
 * @hide
 */
public class MidiOutputPort implements MidiSender, Closeable {
public class MidiOutputPort extends MidiSender implements Closeable {
    private static final String TAG = "MidiOutputPort";

    private final int mPortNumber;
@@ -59,7 +59,7 @@ public class MidiOutputPort implements MidiSender, Closeable {
                    long timestamp = MidiPortImpl.getMessageTimeStamp(buffer, count);

                    // dispatch to all our receivers
                    mDispatcher.post(buffer, offset, size, timestamp);
                    mDispatcher.receive(buffer, offset, size, timestamp);
                }
            } catch (IOException e) {
                // FIXME report I/O failure?
+4 −3
Original line number Diff line number Diff line
@@ -24,7 +24,7 @@ import java.io.IOException;
 * CANDIDATE FOR PUBLIC API
 * @hide
 */
public interface MidiReceiver {
abstract public class MidiReceiver {
    /**
     * Called to pass MIDI data to the receiver.
     *
@@ -32,7 +32,7 @@ public interface MidiReceiver {
     * The msg bytes should be copied by the receiver rather than retaining a reference
     * to this parameter.
     * Also, modifying the contents of the msg array parameter may result in other receivers
     * in the same application receiving incorrect values in their post() method.
     * in the same application receiving incorrect values in their receive() method.
     *
     * @param msg a byte array containing the MIDI data
     * @param offset the offset of the first byte of the data in the byte array
@@ -40,5 +40,6 @@ public interface MidiReceiver {
     * @param timestamp the timestamp of the message (based on {@link java.lang.System#nanoTime}
     * @throws IOException
     */
    public void post(byte[] msg, int offset, int count, long timestamp) throws IOException;
    abstract public void receive(byte[] msg, int offset, int count, long timestamp)
            throws IOException;
}
Loading