Search in sources :

Example 1 with MidiReceiver

use of android.media.midi.MidiReceiver in project platform_frameworks_base by android.

the class UsbMidiDevice method openLocked.

private boolean openLocked() {
    // FIXME - support devices with different number of input and output ports
    FileDescriptor[] fileDescriptors = nativeOpen(mAlsaCard, mAlsaDevice, mSubdeviceCount);
    if (fileDescriptors == null) {
        Log.e(TAG, "nativeOpen failed");
        return false;
    }
    mFileDescriptors = fileDescriptors;
    int inputStreamCount = fileDescriptors.length;
    // last file descriptor returned from nativeOpen() is only used for unblocking Os.poll()
    // in our input thread
    int outputStreamCount = fileDescriptors.length - 1;
    mPollFDs = new StructPollfd[inputStreamCount];
    mInputStreams = new FileInputStream[inputStreamCount];
    for (int i = 0; i < inputStreamCount; i++) {
        FileDescriptor fd = fileDescriptors[i];
        StructPollfd pollfd = new StructPollfd();
        pollfd.fd = fd;
        pollfd.events = (short) OsConstants.POLLIN;
        mPollFDs[i] = pollfd;
        mInputStreams[i] = new FileInputStream(fd);
    }
    mOutputStreams = new FileOutputStream[outputStreamCount];
    mEventSchedulers = new MidiEventScheduler[outputStreamCount];
    for (int i = 0; i < outputStreamCount; i++) {
        mOutputStreams[i] = new FileOutputStream(fileDescriptors[i]);
        MidiEventScheduler scheduler = new MidiEventScheduler();
        mEventSchedulers[i] = scheduler;
        mInputPortReceivers[i].setReceiver(scheduler.getReceiver());
    }
    final MidiReceiver[] outputReceivers = mServer.getOutputPortReceivers();
    // Create input thread which will read from all output ports of the physical device
    new Thread("UsbMidiDevice input thread") {

        @Override
        public void run() {
            byte[] buffer = new byte[BUFFER_SIZE];
            try {
                while (true) {
                    // Record time of event immediately after waking.
                    long timestamp = System.nanoTime();
                    synchronized (mLock) {
                        if (!mIsOpen)
                            break;
                        // look for a readable FileDescriptor
                        for (int index = 0; index < mPollFDs.length; index++) {
                            StructPollfd pfd = mPollFDs[index];
                            if ((pfd.revents & (OsConstants.POLLERR | OsConstants.POLLHUP)) != 0) {
                                break;
                            } else if ((pfd.revents & OsConstants.POLLIN) != 0) {
                                // clear readable flag
                                pfd.revents = 0;
                                if (index == mInputStreams.length - 1) {
                                    // last file descriptor is used only for unblocking Os.poll()
                                    break;
                                }
                                int count = mInputStreams[index].read(buffer);
                                outputReceivers[index].send(buffer, 0, count, timestamp);
                            }
                        }
                    }
                    // wait until we have a readable port or we are signalled to close
                    Os.poll(mPollFDs, -1);
                }
            } catch (IOException e) {
                Log.d(TAG, "reader thread exiting");
            } catch (ErrnoException e) {
                Log.d(TAG, "reader thread exiting");
            }
            Log.d(TAG, "input thread exit");
        }
    }.start();
    // Create output thread for each input port of the physical device
    for (int port = 0; port < outputStreamCount; port++) {
        final MidiEventScheduler eventSchedulerF = mEventSchedulers[port];
        final FileOutputStream outputStreamF = mOutputStreams[port];
        final int portF = port;
        new Thread("UsbMidiDevice output thread " + port) {

            @Override
            public void run() {
                while (true) {
                    MidiEvent event;
                    try {
                        event = (MidiEvent) eventSchedulerF.waitNextEvent();
                    } catch (InterruptedException e) {
                        // try again
                        continue;
                    }
                    if (event == null) {
                        break;
                    }
                    try {
                        outputStreamF.write(event.data, 0, event.count);
                    } catch (IOException e) {
                        Log.e(TAG, "write failed for port " + portF);
                    }
                    eventSchedulerF.addEventToPool(event);
                }
                Log.d(TAG, "output thread exit");
            }
        }.start();
    }
    mIsOpen = true;
    return true;
}
Also used : MidiEvent(com.android.internal.midi.MidiEventScheduler.MidiEvent) IOException(java.io.IOException) FileDescriptor(java.io.FileDescriptor) FileInputStream(java.io.FileInputStream) MidiEventScheduler(com.android.internal.midi.MidiEventScheduler) ErrnoException(android.system.ErrnoException) MidiReceiver(android.media.midi.MidiReceiver) FileOutputStream(java.io.FileOutputStream) StructPollfd(android.system.StructPollfd)

Example 2 with MidiReceiver

use of android.media.midi.MidiReceiver in project android_frameworks_base by crdroidandroid.

the class UsbMidiDevice method openLocked.

private boolean openLocked() {
    // FIXME - support devices with different number of input and output ports
    FileDescriptor[] fileDescriptors = nativeOpen(mAlsaCard, mAlsaDevice, mSubdeviceCount);
    if (fileDescriptors == null) {
        Log.e(TAG, "nativeOpen failed");
        return false;
    }
    mFileDescriptors = fileDescriptors;
    int inputCount = fileDescriptors.length;
    // last file descriptor returned from nativeOpen() is only used for unblocking Os.poll()
    // in our input thread
    int outputCount = fileDescriptors.length - 1;
    mPollFDs = new StructPollfd[inputCount];
    mInputStreams = new FileInputStream[inputCount];
    for (int i = 0; i < inputCount; i++) {
        FileDescriptor fd = fileDescriptors[i];
        StructPollfd pollfd = new StructPollfd();
        pollfd.fd = fd;
        pollfd.events = (short) OsConstants.POLLIN;
        mPollFDs[i] = pollfd;
        mInputStreams[i] = new FileInputStream(fd);
    }
    mOutputStreams = new FileOutputStream[outputCount];
    mEventSchedulers = new MidiEventScheduler[outputCount];
    for (int i = 0; i < outputCount; i++) {
        mOutputStreams[i] = new FileOutputStream(fileDescriptors[i]);
        MidiEventScheduler scheduler = new MidiEventScheduler();
        mEventSchedulers[i] = scheduler;
        mInputPortReceivers[i].setReceiver(scheduler.getReceiver());
    }
    final MidiReceiver[] outputReceivers = mServer.getOutputPortReceivers();
    // Create input thread which will read from all input ports
    new Thread("UsbMidiDevice input thread") {

        @Override
        public void run() {
            byte[] buffer = new byte[BUFFER_SIZE];
            try {
                while (true) {
                    // Record time of event immediately after waking.
                    long timestamp = System.nanoTime();
                    synchronized (mLock) {
                        if (!mIsOpen)
                            break;
                        // look for a readable FileDescriptor
                        for (int index = 0; index < mPollFDs.length; index++) {
                            StructPollfd pfd = mPollFDs[index];
                            if ((pfd.revents & (OsConstants.POLLERR | OsConstants.POLLHUP)) != 0) {
                                break;
                            } else if ((pfd.revents & OsConstants.POLLIN) != 0) {
                                // clear readable flag
                                pfd.revents = 0;
                                if (index == mInputStreams.length - 1) {
                                    // last file descriptor is used only for unblocking Os.poll()
                                    break;
                                }
                                int count = mInputStreams[index].read(buffer);
                                outputReceivers[index].send(buffer, 0, count, timestamp);
                            }
                        }
                    }
                    // wait until we have a readable port or we are signalled to close
                    Os.poll(mPollFDs, -1);
                }
            } catch (IOException e) {
                Log.d(TAG, "reader thread exiting");
            } catch (ErrnoException e) {
                Log.d(TAG, "reader thread exiting");
            }
            Log.d(TAG, "input thread exit");
        }
    }.start();
    // Create output thread for each output port
    for (int port = 0; port < outputCount; port++) {
        final MidiEventScheduler eventSchedulerF = mEventSchedulers[port];
        final FileOutputStream outputStreamF = mOutputStreams[port];
        final int portF = port;
        new Thread("UsbMidiDevice output thread " + port) {

            @Override
            public void run() {
                while (true) {
                    MidiEvent event;
                    try {
                        event = (MidiEvent) eventSchedulerF.waitNextEvent();
                    } catch (InterruptedException e) {
                        // try again
                        continue;
                    }
                    if (event == null) {
                        break;
                    }
                    try {
                        outputStreamF.write(event.data, 0, event.count);
                    } catch (IOException e) {
                        Log.e(TAG, "write failed for port " + portF);
                    }
                    eventSchedulerF.addEventToPool(event);
                }
                Log.d(TAG, "output thread exit");
            }
        }.start();
    }
    mIsOpen = true;
    return true;
}
Also used : MidiEvent(com.android.internal.midi.MidiEventScheduler.MidiEvent) IOException(java.io.IOException) FileDescriptor(java.io.FileDescriptor) FileInputStream(java.io.FileInputStream) MidiEventScheduler(com.android.internal.midi.MidiEventScheduler) ErrnoException(android.system.ErrnoException) MidiReceiver(android.media.midi.MidiReceiver) FileOutputStream(java.io.FileOutputStream) StructPollfd(android.system.StructPollfd)

Example 3 with MidiReceiver

use of android.media.midi.MidiReceiver in project android_frameworks_base by DirtyUnicorns.

the class UsbMidiDevice method openLocked.

private boolean openLocked() {
    // FIXME - support devices with different number of input and output ports
    FileDescriptor[] fileDescriptors = nativeOpen(mAlsaCard, mAlsaDevice, mSubdeviceCount);
    if (fileDescriptors == null) {
        Log.e(TAG, "nativeOpen failed");
        return false;
    }
    mFileDescriptors = fileDescriptors;
    int inputCount = fileDescriptors.length;
    // last file descriptor returned from nativeOpen() is only used for unblocking Os.poll()
    // in our input thread
    int outputCount = fileDescriptors.length - 1;
    mPollFDs = new StructPollfd[inputCount];
    mInputStreams = new FileInputStream[inputCount];
    for (int i = 0; i < inputCount; i++) {
        FileDescriptor fd = fileDescriptors[i];
        StructPollfd pollfd = new StructPollfd();
        pollfd.fd = fd;
        pollfd.events = (short) OsConstants.POLLIN;
        mPollFDs[i] = pollfd;
        mInputStreams[i] = new FileInputStream(fd);
    }
    mOutputStreams = new FileOutputStream[outputCount];
    mEventSchedulers = new MidiEventScheduler[outputCount];
    for (int i = 0; i < outputCount; i++) {
        mOutputStreams[i] = new FileOutputStream(fileDescriptors[i]);
        MidiEventScheduler scheduler = new MidiEventScheduler();
        mEventSchedulers[i] = scheduler;
        mInputPortReceivers[i].setReceiver(scheduler.getReceiver());
    }
    final MidiReceiver[] outputReceivers = mServer.getOutputPortReceivers();
    // Create input thread which will read from all input ports
    new Thread("UsbMidiDevice input thread") {

        @Override
        public void run() {
            byte[] buffer = new byte[BUFFER_SIZE];
            try {
                while (true) {
                    // Record time of event immediately after waking.
                    long timestamp = System.nanoTime();
                    synchronized (mLock) {
                        if (!mIsOpen)
                            break;
                        // look for a readable FileDescriptor
                        for (int index = 0; index < mPollFDs.length; index++) {
                            StructPollfd pfd = mPollFDs[index];
                            if ((pfd.revents & (OsConstants.POLLERR | OsConstants.POLLHUP)) != 0) {
                                break;
                            } else if ((pfd.revents & OsConstants.POLLIN) != 0) {
                                // clear readable flag
                                pfd.revents = 0;
                                if (index == mInputStreams.length - 1) {
                                    // last file descriptor is used only for unblocking Os.poll()
                                    break;
                                }
                                int count = mInputStreams[index].read(buffer);
                                outputReceivers[index].send(buffer, 0, count, timestamp);
                            }
                        }
                    }
                    // wait until we have a readable port or we are signalled to close
                    Os.poll(mPollFDs, -1);
                }
            } catch (IOException e) {
                Log.d(TAG, "reader thread exiting");
            } catch (ErrnoException e) {
                Log.d(TAG, "reader thread exiting");
            }
            Log.d(TAG, "input thread exit");
        }
    }.start();
    // Create output thread for each output port
    for (int port = 0; port < outputCount; port++) {
        final MidiEventScheduler eventSchedulerF = mEventSchedulers[port];
        final FileOutputStream outputStreamF = mOutputStreams[port];
        final int portF = port;
        new Thread("UsbMidiDevice output thread " + port) {

            @Override
            public void run() {
                while (true) {
                    MidiEvent event;
                    try {
                        event = (MidiEvent) eventSchedulerF.waitNextEvent();
                    } catch (InterruptedException e) {
                        // try again
                        continue;
                    }
                    if (event == null) {
                        break;
                    }
                    try {
                        outputStreamF.write(event.data, 0, event.count);
                    } catch (IOException e) {
                        Log.e(TAG, "write failed for port " + portF);
                    }
                    eventSchedulerF.addEventToPool(event);
                }
                Log.d(TAG, "output thread exit");
            }
        }.start();
    }
    mIsOpen = true;
    return true;
}
Also used : MidiEvent(com.android.internal.midi.MidiEventScheduler.MidiEvent) IOException(java.io.IOException) FileDescriptor(java.io.FileDescriptor) FileInputStream(java.io.FileInputStream) MidiEventScheduler(com.android.internal.midi.MidiEventScheduler) ErrnoException(android.system.ErrnoException) MidiReceiver(android.media.midi.MidiReceiver) FileOutputStream(java.io.FileOutputStream) StructPollfd(android.system.StructPollfd)

Example 4 with MidiReceiver

use of android.media.midi.MidiReceiver in project android_frameworks_base by ResurrectionRemix.

the class UsbMidiDevice method openLocked.

private boolean openLocked() {
    // FIXME - support devices with different number of input and output ports
    FileDescriptor[] fileDescriptors = nativeOpen(mAlsaCard, mAlsaDevice, mSubdeviceCount);
    if (fileDescriptors == null) {
        Log.e(TAG, "nativeOpen failed");
        return false;
    }
    mFileDescriptors = fileDescriptors;
    int inputCount = fileDescriptors.length;
    // last file descriptor returned from nativeOpen() is only used for unblocking Os.poll()
    // in our input thread
    int outputCount = fileDescriptors.length - 1;
    mPollFDs = new StructPollfd[inputCount];
    mInputStreams = new FileInputStream[inputCount];
    for (int i = 0; i < inputCount; i++) {
        FileDescriptor fd = fileDescriptors[i];
        StructPollfd pollfd = new StructPollfd();
        pollfd.fd = fd;
        pollfd.events = (short) OsConstants.POLLIN;
        mPollFDs[i] = pollfd;
        mInputStreams[i] = new FileInputStream(fd);
    }
    mOutputStreams = new FileOutputStream[outputCount];
    mEventSchedulers = new MidiEventScheduler[outputCount];
    for (int i = 0; i < outputCount; i++) {
        mOutputStreams[i] = new FileOutputStream(fileDescriptors[i]);
        MidiEventScheduler scheduler = new MidiEventScheduler();
        mEventSchedulers[i] = scheduler;
        mInputPortReceivers[i].setReceiver(scheduler.getReceiver());
    }
    final MidiReceiver[] outputReceivers = mServer.getOutputPortReceivers();
    // Create input thread which will read from all input ports
    new Thread("UsbMidiDevice input thread") {

        @Override
        public void run() {
            byte[] buffer = new byte[BUFFER_SIZE];
            try {
                while (true) {
                    // Record time of event immediately after waking.
                    long timestamp = System.nanoTime();
                    synchronized (mLock) {
                        if (!mIsOpen)
                            break;
                        // look for a readable FileDescriptor
                        for (int index = 0; index < mPollFDs.length; index++) {
                            StructPollfd pfd = mPollFDs[index];
                            if ((pfd.revents & (OsConstants.POLLERR | OsConstants.POLLHUP)) != 0) {
                                break;
                            } else if ((pfd.revents & OsConstants.POLLIN) != 0) {
                                // clear readable flag
                                pfd.revents = 0;
                                if (index == mInputStreams.length - 1) {
                                    // last file descriptor is used only for unblocking Os.poll()
                                    break;
                                }
                                int count = mInputStreams[index].read(buffer);
                                outputReceivers[index].send(buffer, 0, count, timestamp);
                            }
                        }
                    }
                    // wait until we have a readable port or we are signalled to close
                    Os.poll(mPollFDs, -1);
                }
            } catch (IOException e) {
                Log.d(TAG, "reader thread exiting");
            } catch (ErrnoException e) {
                Log.d(TAG, "reader thread exiting");
            }
            Log.d(TAG, "input thread exit");
        }
    }.start();
    // Create output thread for each output port
    for (int port = 0; port < outputCount; port++) {
        final MidiEventScheduler eventSchedulerF = mEventSchedulers[port];
        final FileOutputStream outputStreamF = mOutputStreams[port];
        final int portF = port;
        new Thread("UsbMidiDevice output thread " + port) {

            @Override
            public void run() {
                while (true) {
                    MidiEvent event;
                    try {
                        event = (MidiEvent) eventSchedulerF.waitNextEvent();
                    } catch (InterruptedException e) {
                        // try again
                        continue;
                    }
                    if (event == null) {
                        break;
                    }
                    try {
                        outputStreamF.write(event.data, 0, event.count);
                    } catch (IOException e) {
                        Log.e(TAG, "write failed for port " + portF);
                    }
                    eventSchedulerF.addEventToPool(event);
                }
                Log.d(TAG, "output thread exit");
            }
        }.start();
    }
    mIsOpen = true;
    return true;
}
Also used : MidiEvent(com.android.internal.midi.MidiEventScheduler.MidiEvent) IOException(java.io.IOException) FileDescriptor(java.io.FileDescriptor) FileInputStream(java.io.FileInputStream) MidiEventScheduler(com.android.internal.midi.MidiEventScheduler) ErrnoException(android.system.ErrnoException) MidiReceiver(android.media.midi.MidiReceiver) FileOutputStream(java.io.FileOutputStream) StructPollfd(android.system.StructPollfd)

Aggregations

MidiReceiver (android.media.midi.MidiReceiver)4 ErrnoException (android.system.ErrnoException)4 StructPollfd (android.system.StructPollfd)4 MidiEventScheduler (com.android.internal.midi.MidiEventScheduler)4 MidiEvent (com.android.internal.midi.MidiEventScheduler.MidiEvent)4 FileDescriptor (java.io.FileDescriptor)4 FileInputStream (java.io.FileInputStream)4 FileOutputStream (java.io.FileOutputStream)4 IOException (java.io.IOException)4