Skip to content

Commit

Permalink
Trying to figure out chn input/output problems.
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobjoaquin committed Aug 27, 2010
1 parent 55fb090 commit a27f8aa
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 64 deletions.
37 changes: 28 additions & 9 deletions examples/protectedTable/protectedTable.pde
Expand Up @@ -15,6 +15,7 @@
import csoundo.*;

Csoundo cs;
int bugs = 0;

void setup() {
size(740, 480);
Expand All @@ -31,22 +32,40 @@ void draw() {
noStroke();
fill(0, 8);
rect(0, 0, width, height);


// Create empty table
float size = pow(2, 12);
cs.event("f 20 0 " + size + " 10 0");
int length = cs.tableLength(20);
float phase = (float) (frameCount % 60) / 60 * TWO_PI;


// Increment phase of sine table input
int inc = 60;
float phase = (float) (frameCount % inc) / inc * TWO_PI;

// Create, write, read and draw sine waveform
stroke(255, 255);
beginShape();
for (int i = 0; i < length; i++) {

// Intentionally read backwards
cs.tableSet(20, length - i - 1, sin(2 * PI * (float) i *
(random(0.0001) + 2.0) / (float) length + phase));
for (int i = length - 1; i >= 0; i--) {

// Generate sine data point and input into table
float input = sin(TWO_PI * (float) i / (float) length + phase);
cs.tableSet(20, length, input);

// Read the from the table at the same index
float output = cs.tableGet(20, length);

// Print message if input != output
if (output != input) {
println(output + " != " + input + " @index " + i);
bugs++;
println("fail rate = " + ((float) bugs / (float) frameCount));
}

float v = cs.tableGet(20, length - i - 1);
vertex((float) i / (float) length * (float) width, height / 2.0 + height / 2.0 * v);
// Draw wave form
vertex((float) i / (float) (length - 1) * (float) width,
height / 2.0 + height / 2.0 * output);

}

endShape();
Expand Down
101 changes: 48 additions & 53 deletions src/csoundo/Csoundo.java
Expand Up @@ -33,7 +33,7 @@
*/


public class Csoundo{
public class Csoundo extends Thread{
PApplet myParent;
public final static String VERSION = "##version##";

Expand All @@ -57,42 +57,35 @@ public class Csoundo{
* @param theParent The PApplet. Usually pass 'this'
* @param f The Csound file to run. Requires full absolute path.
*/
public Csoundo(PApplet theParent, String _csd) {
myParent = theParent;
welcome();
myParent.registerDispose(this);
myParent.registerPost(this);
public Csoundo(PApplet theParent, String _csd) {
myParent = theParent;
welcome();
myParent.registerDispose(this);
myParent.registerPost(this);

path = myParent.dataPath("");
csd = myParent.dataPath(path + _csd);
engine = new Engine(csd, path);
}
csd = myParent.dataPath(path + _csd);
engine = new Engine(csd, path);

}

public void dispose() {
System.out.println("Csound dispose");
// 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");
// NOTE:

/*
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");
perfThread.Stop();
System.out.println("perfThread waiting");
perfThread.Join();
System.out.println("perfThread finished");
csound.Cleanup();
csound.delete();
System.out.println("Csound dispose complete");
}

public void pre() { }

public void post() {
mutex.cleanup(); // Closes any mutex locks left open from lock()
// mutex.cleanup(); // Closes any mutex locks left open from lock()
}

private void welcome() {
Expand All @@ -114,12 +107,12 @@ public static String version() {
* @param s The score event. ie "i 1 0 1 0.707 440"
*/
public void event(String s) {
if (!isRunning) return;
// if (!isRunning) return;
// TODO: Is mutex needed if csoundPerformanceThread.InputMessage()
// is already thread safe? Does it use mutex?
mutex.lock();
// mutex.lock();
perfThread.InputMessage(s);
mutex.unlock();
// mutex.unlock();
}

/**
Expand All @@ -128,7 +121,7 @@ public void event(String s) {
* @return 0dbfs
*/
public float get0dBFS() {
if (!isRunning) return 0;
// if (!isRunning) return 0;
return csound.Get0dBFS();
}

Expand All @@ -138,10 +131,10 @@ public float get0dBFS() {
* @return chn bus value
*/
public float getChn(String chn) {
if (!isRunning) return 0;
mutex.lock();
// if (!isRunning) return 0;
// mutex.lock();
float value = csound.GetChannel(chn);
mutex.unlock();
// mutex.unlock();
return value;
}

Expand All @@ -164,7 +157,7 @@ public String getOptions() {
*/
public int getPerfStatus() {
// TODO: Make ENUMS, not magic numbers
if (!isRunning) return -99999;
// if (!isRunning) return -99999;
return perfThread.GetStatus();
}

Expand All @@ -174,7 +167,7 @@ public int getPerfStatus() {
* @return krate
*/
public float kr() {
if (!isRunning) return 0;
// if (!isRunning) return 0;
return csound.GetKr();
}

Expand All @@ -185,7 +178,7 @@ public float kr() {
*/
public float ksmps() {
// TODO: Throw exceptions, not magic numbers.
if (!isRunning) return 0;
// if (!isRunning) return 0;
return csound.GetKsmps();
}

Expand All @@ -202,14 +195,16 @@ public void lock() {
* @return number of output channels
*/
public float nchnls() {
if (!isRunning) return 0;
// if (!isRunning) return 0;
return csound.GetNchnls();
}

/**
* starts the Csound engine.
*/
public void run() {
super.run();
System.out.println(getName());
engine.start();
perfThread = engine.perfThread;
while(perfThread.GetStatus() != 0) {
Expand All @@ -224,21 +219,21 @@ public void run() {
* Sets the value of the specified chn bus.
*/
public void setChn(String chn, float value) {
if (!isRunning) return;
mutex.lock();
// if (!isRunning) return;
// mutex.lock();
csound.SetChannel(chn, value);
mutex.unlock();
// mutex.unlock();
}

/**
* Sets the string of the specified chn bus.
*/
public void setChn(String chn, String sValue) {
// TODO: Untested. Need to figure out how to get a string.
if (!isRunning) return;
mutex.lock();
// if (!isRunning) return;
// mutex.lock();
csound.SetChannel(chn, sValue);
mutex.unlock();
// mutex.unlock();
}

/* TODO: Try to get this working, even if unnecessary.
Expand Down Expand Up @@ -275,7 +270,7 @@ public void setOptions(String s) {
* @return samplerate
*/
public float sr() {
if (!isRunning) return 0;
// if (!isRunning) return 0;
return csound.GetSr();
}

Expand All @@ -288,10 +283,10 @@ public float sr() {
*/
public float tableGet(int t, int i) {
// TODO: If sets are locked, do gets need to be?
if (!isRunning) return 0;
mutex.lock();
// if (!isRunning) return 0;
// mutex.lock();
float value = csound.TableGet(t, i);
mutex.unlock();
// mutex.unlock();
return value;
}

Expand All @@ -302,10 +297,10 @@ public float tableGet(int t, int i) {
* @return Csound table length
*/
public int tableLength(int t) {
if (!isRunning) return 0;
mutex.lock();
// if (!isRunning) return 0;
// mutex.lock();
int value = csound.TableLength(t);
mutex.unlock();
// mutex.unlock();
return value;
}

Expand All @@ -317,10 +312,10 @@ public int tableLength(int t) {
* @param v Value
*/
public void tableSet(int t, int i, float v) {
if (!isRunning) return;
mutex.lock();
// if (!isRunning) return;
// mutex.lock();
csound.TableSet(t, i, v);
mutex.unlock();
// mutex.unlock();
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/csoundo/Engine.java
Expand Up @@ -86,7 +86,7 @@ private void csoundPerf() {
isRunning = true;
perfThread = new CsoundPerformanceThread(csound_p);
perfThread.Play();
mutex = new Mutex();
//mutex = new Mutex();
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/csoundo/Mutex.java
Expand Up @@ -36,7 +36,7 @@ public class Mutex {

SWIGTYPE_p_void m; // Mutex
private int locks = 0; // Number of active locks
private boolean useLocks = true; // Use locks?
private boolean useLocks = false; // Use locks?

public Mutex() {
m = csnd.csoundCreateMutex(1);
Expand Down

0 comments on commit a27f8aa

Please sign in to comment.