From: <dqu...@us...> - 2003-07-26 07:25:13
|
Update of /cvsroot/jsynthlib/JSynthLib/core In directory sc8-pr-cvs1:/tmp/cvs-serv29526 Modified Files: MacOSXMidiWrapper.java Log Message: Driver update for 10.2, ActionExecutor thread deletion Index: MacOSXMidiWrapper.java =================================================================== RCS file: /cvsroot/jsynthlib/JSynthLib/core/MacOSXMidiWrapper.java,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** MacOSXMidiWrapper.java 30 Sep 2002 23:58:42 -0000 1.2 --- MacOSXMidiWrapper.java 25 Jul 2003 18:30:30 -0000 1.3 *************** *** 17,22 **** /** ! * Midi wrapper for MacOS X ! * @author Denis Queffeulou dqu...@fr... */ public class MacOSXMidiWrapper extends MidiWrapper --- 17,23 ---- /** ! * Midi wrapper for MacOS X.2 ! * This version is not X.1 compatible. Get the older version. ! * @author Denis Queffeulou mailto:dqu...@fr... */ public class MacOSXMidiWrapper extends MidiWrapper *************** *** 40,46 **** private Map mReceivedDataMap = new Hashtable(); - /** thread used to call CA api method */ - private ActionExecutor mActionExecutor; - /** when init done... */ private boolean mInitDone = false; --- 41,44 ---- *************** *** 48,57 **** private static boolean mLoadDone = false; - /** object to wait on until current sysex send completes */ - private Object mLockSysexSend = new Object(); - - /** sysex request counter */ - private int mSysexCount = 0; - /** MIDI paquet for channel messages , it seems important for handling the controller device data smoothy to create only one and reuse it --- 46,49 ---- *************** *** 64,72 **** public MacOSXMidiWrapper(int inport, int outport) throws Exception { ! // creates executor thread ! mActionExecutor = new ActionExecutor(); ! mActionExecutor.start(); ! // initialisation ! mActionExecutor.setAction(new Init(inport, outport)); } --- 56,76 ---- public MacOSXMidiWrapper(int inport, int outport) throws Exception { ! mClient = new MIDIClient(new CAFString("JSynthLib"), null); ! if (!mLoadDone) ! { ! loadDevicesNames(); ! mLoadDone = true; ! } ! mInputs = new MIDIInputPort[mNumInputs]; ! ! // device input ! mInputs[inport] = mClient.inputPortCreate(new CAFString(getInputDeviceName(inport)), new ReadProcImpl()); ! MIDIEndpoint oIn = MIDISetup.getSource(inport); ! mInputs[inport].connectSource(oIn); ! ! // device output ! mOutput = mClient.outputPortCreate(new CAFString(getInputDeviceName(outport))); ! ! mInitDone = true; } *************** *** 77,150 **** /** - * Initialization method (in a separate thread) - */ - class Init implements Runnable - { - private int mInportNumber; - private int mOutportNumber; - Init(int inport, int outport) - { - mInportNumber = inport; - mOutportNumber = outport; - } - public void run() - { - try - { - mClient = new MIDIClient(new CAFString("JSynthLib"), null); - if (!mLoadDone) - { - loadDevicesNames(); - mLoadDone = true; - } - mInputs = new MIDIInputPort[mNumInputs]; - - // device input - mInputs[mInportNumber] = mClient.inputPortCreate(new CAFString(getInputDeviceName(mInportNumber)), new ReadProcImpl()); - MIDIEndpoint oIn = MIDISetup.getSource(mInportNumber); - mInputs[mInportNumber].connectSource(oIn); - - // device output - mOutput = mClient.outputPortCreate(new CAFString(getInputDeviceName(mOutportNumber))); - - mInitDone = true; - } - catch(Exception cae) - { - cae.printStackTrace(); - } - } - } - - /** Init for input read proc. Puts MIDIInputPort in mInputs. */ ! class InputInit implements Runnable { ! private int mInportNumber; ! InputInit(int inport) ! { ! mInportNumber = inport; ! } ! public void run() ! { ! try ! { ! // controller device ! mInputs[mInportNumber] = mClient.inputPortCreate(new CAFString(getInputDeviceName(mInportNumber)), new ReadProcImpl()); ! MIDIEndpoint oInContr = MIDISetup.getSource(mInportNumber); ! mInputs[mInportNumber].connectSource(oInContr); ! } ! catch(Exception cae) ! { ! cae.printStackTrace(); ! } ! } } - - //FIXME: Never call this even though its public, I need to call it from prefsDialog - //to work around a JavaMIDI bug though. public void setInputDeviceNum (int port) throws Exception { --- 81,95 ---- /** Init for input read proc. Puts MIDIInputPort in mInputs. */ ! void inputInit(int inport) throws Exception { ! // controller device ! mInputs[inport] = mClient.inputPortCreate(new CAFString(getInputDeviceName(inport)), new ReadProcImpl()); ! MIDIEndpoint oInContr = MIDISetup.getSource(inport); ! mInputs[inport].connectSource(oInContr); } public void setInputDeviceNum (int port) throws Exception { *************** *** 175,245 **** else { ! synchronized(mLockSysexSend) ! { ! try { ! // wait until last sysex completes ! // System.out.println("mSysexCount = "+mSysexCount); ! while(mSysexCount > 0) ! { ! // System.out.println("wait for write"); ! mLockSysexSend.wait(10/*100*/); ! } ! mSysexCount++; ! mActionExecutor.setAction(new WriteLongMessage(port, sysex, length)); ! } ! catch(Exception e) { ! e.printStackTrace(); ! } ! } ! } ! } ! ! ! /** ! * Separate thread is due to bug in Java CoreAudio that throw a IllegalMonitorStateException ! */ ! class WriteLongMessage implements Runnable ! { ! private int mPort; ! private byte[] mSysex; ! private int mLength; ! ! /** ! Send sysex message ! */ ! WriteLongMessage(int aport,byte []asysex,int alength) ! { ! mPort = aport; ! mSysex = asysex; ! mLength = alength; ! } ! public void run() ! { ! try ! { ! // send sysex ! MIDIEndpoint oOut = MIDISetup.getDestination(mPort); // MIDIData oData = MIDIData.newMIDIPacketData(length); ! MIDIData oData = MIDIData.newMIDIRawData(mLength); ! ! /* la fonction a l'air de marcher mais l'objet cree ne doit pas etre correct ! oData.copyFromArray(4, mSysex, 0, mLength); */ ! int oTab[] = new int[mLength]; ! for (int i = 0; i < mLength; i++) ! { ! oTab[i] = (int)mSysex[i]; ! } ! oData.addRawData(oTab); ! MIDISysexSendRequest oSysex = new MIDISysexSendRequest(oOut, oData); ! oSysex.send(MacOSXMidiWrapper.this); ! } ! catch(Exception cae) { ! cae.printStackTrace(); } } } public void writeLongMessage (int port,byte []sysex)throws Exception { --- 120,143 ---- else { ! // send sysex ! MIDIEndpoint oOut = MIDISetup.getDestination(port); // MIDIData oData = MIDIData.newMIDIPacketData(length); ! MIDIData oData = MIDIData.newMIDIRawData(length); ! ! /* la fonction a l'air de marcher mais l'objet cree ne doit pas etre correct ! oData.copyFromArray(4, mSysex, 0, mLength); */ ! int oTab[] = new int[length]; ! for (int i = 0; i < length; i++) { ! oTab[i] = (int)sysex[i]; } + oData.addRawData(oTab); + MIDISysexSendRequest oSysex = new MIDISysexSendRequest(oOut, oData); + oSysex.send(MacOSXMidiWrapper.this); } } + public void writeLongMessage (int port,byte []sysex)throws Exception { *************** *** 250,296 **** public void writeShortMessage (int port, byte b1, byte b2) throws Exception { ! mActionExecutor.setAction(new WriteShortMessage(port, b1, b2, (byte)0)); } ! class WriteShortMessage implements Runnable { ! private int mPort; ! private int mData[] = new int[3]; ! WriteShortMessage (int port,byte b1, byte b2,byte b3) { ! mPort = port; ! mData[0] = (int)b1; ! mData[1] = (int)b2; ! mData[2] = (int)b3; } ! public void run() ! { ! try ! { ! if (mShortMessagePaquetList == null) ! { ! mShortMessagePaquetList = new MIDIPacketList(); ! } ! else ! { // reuse the paquet ! mShortMessagePaquetList.init(); ! } ! MIDIData oData = MIDIData.newMIDIChannelMessage(mData[0], mData[1], mData[2]); ! mShortMessagePaquetList.add(0, oData); ! MIDIEndpoint oOut = MIDISetup.getDestination(mPort); ! mOutput.send(oOut, mShortMessagePaquetList); ! } ! catch(Exception e) ! { ! e.printStackTrace(); ! } } } - public void writeShortMessage (int port,byte b1, byte b2,byte b3) throws Exception - { - // System.out.println("writeShortMessage port = "+port+" b1="+b1+" b2="+b2+" b3= "+b3); - mActionExecutor.setAction(new WriteShortMessage(port, b1, b2, b3)); - } public int getNumInputDevices () throws Exception --- 148,171 ---- public void writeShortMessage (int port, byte b1, byte b2) throws Exception { ! writeShortMessage(port, b1, b2, (byte)0); } ! public void writeShortMessage (int port,byte b1, byte b2,byte b3) throws Exception { ! // System.out.println("writeShortMessage port = "+port+" b1="+b1+" b2="+b2+" b3= "+b3); ! if (mShortMessagePaquetList == null) { ! mShortMessagePaquetList = new MIDIPacketList(); } ! else ! { // reuse the paquet ! mShortMessagePaquetList.init(); } + MIDIData oData = MIDIData.newMIDIChannelMessage(b1, b2, b3); + mShortMessagePaquetList.add(0, oData); + MIDIEndpoint oOut = MIDISetup.getDestination(port); + mOutput.send(oOut, mShortMessagePaquetList); } public int getNumInputDevices () throws Exception *************** *** 298,309 **** if (!mInitDone) { ! mActionExecutor.setAction(new Runnable() ! { public void run() { ! try { ! mNumInputs = MIDISetup.getNumberOfSources(); ! } catch(Exception e) { ! e.printStackTrace(); ! }} ! }); } return mNumInputs; --- 173,177 ---- if (!mInitDone) { ! mNumInputs = MIDISetup.getNumberOfSources(); } return mNumInputs; *************** *** 314,325 **** if (!mInitDone) { ! mActionExecutor.setAction(new Runnable() ! { public void run() { ! try { ! mNumInputs = MIDISetup.getNumberOfDestinations(); ! } catch(Exception e) { ! e.printStackTrace(); ! }} ! }); } return mNumOutputs; --- 182,186 ---- if (!mInitDone) { ! mNumInputs = MIDISetup.getNumberOfDestinations(); } return mNumOutputs; *************** *** 347,351 **** { // creates new input ! mActionExecutor.setAction(new InputInit(port)); } else --- 208,212 ---- { // creates new input ! inputInit(port); } else *************** *** 386,390 **** { // System.out.println("close"); - mActionExecutor.kill(); } --- 247,250 ---- *************** *** 392,434 **** Load devices names into static variables */ ! private static void loadDevicesNames() { int oEntNum = 0; ! try { ! mNumInputs = 0; ! mNumOutputs = 0; ! for (int k = 0; k < MIDIDevice.getNumberOfDevices(); k++) ! { ! MIDIDevice oDev = MIDIDevice.getDevice(k); // String oS2 = oDev.getStringProperty(MIDIConstants.kMIDIPropertyName).asString(); // System.out.println("device = "+oS2); ! for (int entity = 0; entity < oDev.getNumberOfEntities(); entity++) ! { ! MIDIEntity oEnt = oDev.getEntity(entity); ! String oSEntity = oDev.getStringProperty(MIDIConstants.kMIDIPropertyName).asString(); // System.out.println("entity = "+oSEntity); ! mNumInputs += oEnt.getNumberOfSources(); ! mNumOutputs += oEnt.getNumberOfDestinations(); ! for (int i = 0; i < oEnt.getNumberOfSources(); i++) ! { ! MIDIEndpoint oIn = oEnt.getSource(i); ! String oSIn = oIn.getStringProperty(MIDIConstants.kMIDIPropertyName).asString(); // System.out.println("input = "+oSIn); ! mInputNames.add(oSEntity+" "+oSIn); ! } ! for (int i = 0; i < oEnt.getNumberOfDestinations(); i++) ! { ! MIDIEndpoint oIn = oEnt.getDestination(i); ! String oSOut = oIn.getStringProperty(MIDIConstants.kMIDIPropertyName).asString(); // System.out.println("output = "+oSOut); ! mOutputNames.add(oSEntity+" "+oSOut); ! } } } } - catch (Exception e) - { - e.printStackTrace(); - } } --- 252,288 ---- Load devices names into static variables */ ! private static void loadDevicesNames() throws Exception { int oEntNum = 0; ! mNumInputs = 0; ! mNumOutputs = 0; ! for (int k = 0; k < MIDIDevice.getNumberOfDevices(); k++) ! { ! MIDIDevice oDev = MIDIDevice.getDevice(k); // String oS2 = oDev.getStringProperty(MIDIConstants.kMIDIPropertyName).asString(); // System.out.println("device = "+oS2); ! for (int entity = 0; entity < oDev.getNumberOfEntities(); entity++) ! { ! MIDIEntity oEnt = oDev.getEntity(entity); ! String oSEntity = oDev.getStringProperty(MIDIConstants.kMIDIPropertyName).asString(); // System.out.println("entity = "+oSEntity); ! mNumInputs += oEnt.getNumberOfSources(); ! mNumOutputs += oEnt.getNumberOfDestinations(); ! for (int i = 0; i < oEnt.getNumberOfSources(); i++) ! { ! MIDIEndpoint oIn = oEnt.getSource(i); ! String oSIn = oIn.getStringProperty(MIDIConstants.kMIDIPropertyName).asString(); // System.out.println("input = "+oSIn); ! mInputNames.add(oSEntity+" "+oSIn); ! } ! for (int i = 0; i < oEnt.getNumberOfDestinations(); i++) ! { ! MIDIEndpoint oIn = oEnt.getDestination(i); ! String oSOut = oIn.getStringProperty(MIDIConstants.kMIDIPropertyName).asString(); // System.out.println("output = "+oSOut); ! mOutputNames.add(oSEntity+" "+oSOut); } } } } *************** *** 441,449 **** { // System.out.println("SYSEX sent IN"); - synchronized(mLockSysexSend) - { - mSysexCount--; - mLockSysexSend.notifyAll(); - } // System.out.println("SYSEX sent OUT"); } --- 295,298 ---- *************** *** 488,548 **** } - /** - Thread used to execute action with CA calls - */ - class ActionExecutor extends Thread - { - private List mAction = new Vector(); - private boolean mStop = false; - /** execute the Runnable within the thread */ - synchronized void setAction(Runnable aAction) - { - // System.out.println("setAction "+aAction); - mAction.add(aAction); - notify(); - } - /** stops the thread */ - synchronized void kill() - { - mStop = true; - notify(); - } - /** - loop until action set, then run it. - */ - public void run() - { - // System.out.println("ActionExecutor start"); - while(true) - { - try - { - synchronized(this) - { - // System.out.println("Wait for action "+mAction.size()); - if (mAction.size() == 0) - { - wait(); - } - if (mStop) - { - break; - } - while(mAction.size() > 0) - { - Runnable oRun = (Runnable)mAction.remove(0); - // System.out.println("Run "+oRun); - oRun.run(); - } - } - } - catch(Exception e) - { - e.printStackTrace(); - } - } - // System.out.println("ActionExecutor exit"); - } - } } --- 337,340 ---- |