Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Moved mutex stuff into new class Mutex.

  • Loading branch information...
commit fc12d442dbd26de95ae4e925c49385b04110aa3b 1 parent 9bb4d95
@jacobjoaquin authored
View
22 examples/devScratchPad/devScratchPad.pde
@@ -5,22 +5,26 @@ Csoundo cs;
void setup() {
size(720, 480);
frameRate(30);
- background(0);
+ smooth();
cs = new Csoundo(this, "data/synth.csd");
cs.run();
-
+ println("perf status: " + cs.getPerfStatus());
cs.tableSet(1, 2048, -1.0);
}
void draw() {
- if (frameCount % 9 == 0) {
- cs.event("i 2 0 0.3 0.5 " + (random(440.0) + 440));
- }
-
- println(cs.tableLength(1));
+ background(0);
+ noStroke();
+ fill(random(255));
+ ellipseMode(RADIUS);
+ ellipse(width / 2, height / 2, 50, 50);
- if (frameCount % 5 == 0) {
- cs.setChn("amp", random(1.0));
+ cs.kLock();
+ if (frameCount % 30 == 0) {
+ cs.event("i 2 0 0.3 0.5 440");
+ cs.event("i 2 0.1 0.3 0.5 880");
}
+ cs.kUnlock();
+
}
View
2  examples/protectedTable/protectedTable.pde
@@ -24,7 +24,7 @@ void draw() {
// fill(r, r, 196);
println("before");
- float size = pow(2, 11);
+ float size = pow(2, 7);
println(size);
cs.event("f 20 0 " + size + " 10 0");
println("after");
View
199 src/csoundo/Csoundo.java
@@ -37,18 +37,16 @@
PApplet myParent;
public final static String VERSION = "##version##";
- private boolean useThreads = true;
private String csd;
private String path;
public boolean isRunning = false;
private Engine engine;
-// private CppSound csound;
private Csound csound;
private CsoundPerformanceThread perfThread;
- private SWIGTYPE_p_CSOUND_ csound_p;
- private SWIGTYPE_p_void mutex;
+// private SWIGTYPE_p_CSOUND_ csound_p;
+ private Mutex mutex;
/**
* The Csoundo onstructor, usually called in the setup() method in your
@@ -63,7 +61,6 @@ public Csoundo(PApplet theParent, String _csd) {
myParent.registerDispose(this);
myParent.registerPost(this);
-// csound = new CppSound();
csd = myParent.sketchPath(_csd);
path = myParent.sketchPath("");
engine = new Engine(csd, path);
@@ -71,12 +68,23 @@ public Csoundo(PApplet theParent, String _csd) {
public void dispose() {
System.out.println("Csound dispose");
- // TODO: Shut down Csound properly.
-
- csound.Stop();
- csnd.csoundDestroy(csound_p);
- csound.Cleanup();
-
+ // TODO: Not satisified that Csound is shutdown properly.
+
+ perfThread.Stop(); // Calls csound.Cleanup() too
+
+ System.out.println("perfThread waiting");
+ while(perfThread.GetStatus() == 0) { };
+ System.out.println("perfThread finished");
+
+/*
+ csound.Stop();
+
+ System.out.println("csnd.csoundDestroy()");
+ csnd.csoundDestroy(csound_p);
+ while(csound != null) { }
+ System.out.println("csound finished");
+*/
+ System.out.println("Csound dispose complete");
}
public void pre() { }
@@ -102,51 +110,52 @@ public static String version() {
* @param s The score event. ie "i 1 0 1 0.707 440"
*/
public void event(String s) {
- if (isRunning) {
-
- if (useThreads) {
- csnd.csoundLockMutex(mutex);
- perfThread.InputMessage(s);
-// perfThread.FlushMessageQueue();
- csnd.csoundUnlockMutex(mutex);
- } else {
- perfThread.InputMessage(s);
- }
-
- }
+ if (!isRunning) return;
+ mutex.lock();
+ perfThread.InputMessage(s);
+ mutex.unlock();
}
- /**
+ public float get0dBFS() {
+ if (!isRunning) return 0;
+ return csound.Get0dBFS();
+ }
+
+ /**
* Returns the value of the specified chn bus.
*
* @return chn bus value
*/
public float getChn(String chn) {
- if (isRunning) {
- if (useThreads) {
- csnd.csoundLockMutex(mutex);
- float value = csound.GetChannel(chn);
- csnd.csoundUnlockMutex(mutex);
- return value;
- } else {
- return csound.GetChannel(chn);
- }
- }
-
- return 0;
+ if (!isRunning) return 0;
+ mutex.lock();
+ float value = csound.GetChannel(chn);
+ mutex.unlock();
+ return value;
}
+ public int getPerfStatus() {
+ if (!isRunning) return -99999;
+ return perfThread.GetStatus();
+ }
+
+
+ public void kLock() {
+ mutex.lock();
+ }
+
+ public void kUnlock() {
+ mutex.unlock();
+ }
+
/**
* Return the control rate.
*
* @return krate
*/
public float kr() {
- if (isRunning) {
- return csound.GetKr();
- }
-
- return 0;
+ if (!isRunning) return 0;
+ return csound.GetKr();
}
/**
@@ -155,11 +164,8 @@ public float kr() {
* @return ksmps
*/
public float ksmps() {
- if (isRunning) {
- return csound.GetKsmps();
- }
-
- return 0;
+ if (!isRunning) return 0;
+ return csound.GetKsmps();
}
/**
@@ -168,11 +174,8 @@ public float ksmps() {
* @return number of output channels
*/
public float nchnls() {
- if (isRunning) {
- return csound.GetNchnls();
- }
-
- return 0;
+ if (!isRunning) return 0;
+ return csound.GetNchnls();
}
/**
@@ -181,9 +184,11 @@ public float nchnls() {
public void run() {
engine.start();
perfThread = engine.perfThread;
+ while(perfThread.GetStatus() != 0) {
+ System.out.println("Waiting for csoundPerformanceThread");
+ }
csound = engine.csound;
- csound_p = engine.csound_p;
- mutex = engine.mutex;
+ mutex = new Mutex();
isRunning = engine.isRunning;
}
@@ -191,15 +196,10 @@ public void run() {
* Sets the value of the specified chn bus.
*/
public void setChn(String chn, float value) {
- if (isRunning) {
- if (useThreads) {
- csnd.csoundLockMutex(mutex);
- csound.SetChannel(chn, value);
- csnd.csoundUnlockMutex(mutex);
- } else {
- csound.SetChannel(chn, value);
- }
- }
+ if (!isRunning) return;
+ mutex.lock();
+ csound.SetChannel(chn, value);
+ mutex.unlock();
}
/**
@@ -207,15 +207,10 @@ public void setChn(String chn, float value) {
*/
public void setChn(String chn, String sValue) {
// TODO: Untested. Need to figure out how to get a string.
- if (isRunning) {
- if (useThreads) {
- csnd.csoundLockMutex(mutex);
- csound.SetChannel(chn, sValue);
- csnd.csoundUnlockMutex(mutex);
- } else {
- csound.SetChannel(chn, sValue);
- }
- }
+ if (!isRunning) return;
+ mutex.lock();
+ csound.SetChannel(chn, sValue);
+ mutex.unlock();
}
/* TODO: Try to get this working, even if unnecessary.
@@ -239,11 +234,8 @@ public void setChnTest(String chn, float value) {
* @return samplerate
*/
public float sr() {
- if (isRunning) {
- return csound.GetSr();
- }
-
- return 0;
+ if (!isRunning) return 0;
+ return csound.GetSr();
}
/**
@@ -254,18 +246,12 @@ public float sr() {
* @return Csound table value
*/
public float tableGet(int t, int i) {
- if (isRunning) {
- if (useThreads) {
- csnd.csoundLockMutex(mutex);
- float value = csound.TableGet(t, i);
- csnd.csoundUnlockMutex(mutex);
- return value;
- } else {
- return csound.TableGet(t, i);
- }
- }
-
- return 0;
+ // TODO: If sets are locked, do gets need to be?
+ if (!isRunning) return 0;
+ mutex.lock();
+ float value = csound.TableGet(t, i);
+ mutex.unlock();
+ return value;
}
/**
@@ -275,18 +261,11 @@ public float tableGet(int t, int i) {
* @return Csound table length
*/
public int tableLength(int t) {
- if (isRunning) {
- if (useThreads) {
- csnd.csoundLockMutex(mutex);
- int value = csound.TableLength(t);
- csnd.csoundUnlockMutex(mutex);
- return value;
- } else {
- return csnd.csoundTableLength(csound_p, t);
- }
- }
-
- return 0;
+ if (!isRunning) return 0;
+ mutex.lock();
+ int value = csound.TableLength(t);
+ mutex.unlock();
+ return value;
}
/**
@@ -297,24 +276,12 @@ public int tableLength(int t) {
* @param v Value
*/
public void tableSet(int t, int i, float v) {
- if (isRunning) {
- if (useThreads) {
- csnd.csoundLockMutex(mutex);
- csound.TableSet(t, i, v);
- csnd.csoundUnlockMutex(mutex);
- } else {
- csound.TableSet(t, i, v);
- }
- }
+ if (!isRunning) return;
+ mutex.lock();
+ csound.TableSet(t, i, v);
+ mutex.unlock();
}
- public float get0dBFS() {
- if (isRunning) {
- return csound.Get0dBFS();
- }
-
- return 0;
- }
}
View
4 src/csoundo/Engine.java
@@ -41,7 +41,7 @@
public SWIGTYPE_p_void v;
public SWIGTYPE_p_CSOUND_ csound_p;
- SWIGTYPE_p_void mutex;
+// SWIGTYPE_p_void mutex;
/**
* The Engine constructor, usually called in the setup() method in your
@@ -81,7 +81,7 @@ private void csoundPerf() {
isRunning = true;
perfThread = new CsoundPerformanceThread(csound_p);
perfThread.Play();
- mutex = csnd.csoundCreateMutex(1);
+ //mutex = csnd.csoundCreateMutex(1);
}
}
}
View
67 src/csoundo/Mutex.java
@@ -0,0 +1,67 @@
+/**
+ * A Csound interface library for Processing.
+ *
+ * ##copyright##
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General
+ * Public License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
+ * Boston, MA 02111-1307 USA
+ *
+ * @author ##author##
+ * @modified ##date##
+ * @version ##version##
+ */
+
+package csoundo;
+
+import csnd.*;
+
+public class Mutex {
+
+ SWIGTYPE_p_void m;
+ int instances = 0;
+ private boolean useThreads = true;
+
+ public void cleanup() {
+ if (!useThreads) return;
+ while (instances > 0) {
+ unlock();
+ }
+ }
+
+ public Mutex() {
+ m = csnd.csoundCreateMutex(1);
+ }
+
+ public void lock() {
+ if (!useThreads) return;
+ csnd.csoundLockMutex(m);
+ instances++;
+ }
+
+ public void unlock() {
+ if (!useThreads) return;
+ if (instances > 0) {
+ instances--;
+ csnd.csoundUnlockMutex(m);
+ } else {
+ System.out.println("Warning: Mutex trying to unlock imaginary threads.");
+ }
+ }
+
+}
+
+
+
+
Please sign in to comment.
Something went wrong with that request. Please try again.