billthefarmer / mididriver

Android midi driver using Sonivox EAS library
176 stars 52 forks source link

Dead locks using MidiDriver after a while #4

Closed akdmia closed 9 years ago

akdmia commented 9 years ago

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.

billthefarmer commented 9 years ago

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.

billthefarmer commented 9 years ago

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