billthefarmer/mididriver

Dead locks using MidiDriver after a while

akdmia opened this issue · 2 comments

I got dead locks after play sequences with a lot of midi events

I think the native method "write" shouldn't be accessible outside of the midi driver thread

I fixed it by adding a queued list, so i call a "queueEvent" method instead of "write":

public void queueEvent(byte[] event) {
    synchronized (this.mutex) {
        this.queuedEvents.add(event);
    }
}

and then i write events in the main thread just before render method:

    while( this.thread != null ) {

        // Write the midi events
        synchronized (this.mutex) {
            for(byte[] queuedEvent : this.queuedEvents) {
                this.write(queuedEvent);
            }
            this.queuedEvents.clear();
        }

        // Render the audio
        if (this.render(buffer) == 0) {
            break;
        }
        // Write audio to audiotrack
        status = this.audioTrack.write(buffer, 0, buffer.length);

        if (status < 0) {
            break;
        }
    }

This way it's thread safe.

I would like to add your code to this project but I am unable to work out exactly which class you have used for the queue. There doesn't appear to be a queued list in the android or java docs.

I used an ArrayList. It seems to work ok. I hate java collections, the nomenclature is crazy.