Csound Csound-dev Csound-tekno Search About

[Csnd] Re: Re: csnd.jar and Processing-1.0.1

Date2009-01-03 10:37
Fromvictor
Subject[Csnd] Re: Re: csnd.jar and Processing-1.0.1
Jean-Pierre,
 
that would do a fine article for the Csound Journal!
 
Thanks for the example
 
Victor
----- Original Message -----
Sent: Saturday, January 03, 2009 9:53 AM
Subject: [Csnd] Re: csnd.jar and Processing-1.0.1

Here is a simple example of integrating csound with Processing. It runs fine. By default Processing is ruuning at 60 FPS, but this can be changed using framerate().

I have put the code for the Applet and CsoundThread in this document 

Best
jp

On Fri, Jan 2, 2009 at 9:50 PM, jean-pierre lemoine <lemoine.jp@gmail.com> wrote:
James,
What you want to do should work. I am using java + jogl (what Processing is using) and csound all toguether, as well as OSC.
Here are the main design points:
 - the opengl graphic loop must not grab all the CPU, I think it might be done with Processing. Search for FPS, Frame by sec
- csound is running in its own java thread (or a csound thread) 

In the graphic thread  (processing draw)
csoundInstance is a Csound object

if ( csoundInstance == null ) {
  csnd.csnd.csoundInitialize(null, null, csnd.csnd.CSOUNDINIT_NO_SIGNAL_HANDLER);
  csoundInstance = new Csound();
}
if ( CompositionGenerator.csdFile != null ) {
  if ( csoundPerformanceThread != null ) {
    if ( csoundPerformanceThread.isRunning() == true ) {
CsoundThread.stopCsound();
    }
}
int rc = csoundInstance.Compile( CompositionGenerator.csdFile );
if ( rc == 0 ) {
    csoundPerformanceThread = CsoundThread.create( csoundInstance );
    csoundPerformanceThread.startCsound(); // -> at this point a Java thread is launched and the          // Processing draw is continuing
}

and csoundThread is 
public class CsoundThread extends Thread {
private static CsoundThread singleton;
private static Csound cs;
private boolean running = false;
private boolean cleanFinished = false;


public static CsoundThread create( Csound aCs ) {
if ( singleton != null ) {
if ( singleton.isRunning() == true ) {
stopCsound();
}
}
singleton = new CsoundThread();
singleton.init( aCs );
return singleton;
}
/*
* ------------------------------------------------------------------
*/
private void init( Csound aCs ) {
cs = aCs;
}
public int compile( String aCSDFile ) {
if ( cs == null ) {
return -1;
}
else {
cs.Reset();
return cs.Compile( aCSDFile );
}
}
public static void stopCsound() {
if ( cs != null ) {
cs.Stop();
double timeOut = System.currentTimeMillis();
while( singleton.isCleanFinished() == false ) {
timeOut = System.currentTimeMillis() - timeOut;
if ( timeOut >= 5000 ) {
break;
}
Thread.yield();
}
}
}
public void startCsound() {
running = true;
start();
}

public void run() {
if ( cs != null ) {
try {
cleanFinished = false;
cs.Perform();
running = false;
cs.Cleanup();
cs.Reset();
cleanFinished = true;
}
catch( Exception ex ) {
ex.printStackTrace();
}
}
}

/**
* @return Returns the running.
*/
public synchronized boolean isRunning() {
return running;
}

/**
* @param running The running to set.
*/
public synchronized void  setRunning( boolean running ) {
this.running = running;
}

public Csound getCsound() {
return cs;
}

public boolean isCleanFinished() {
return cleanFinished;
}

}

Hope it is clear, ask for more.
Maybe there are some side effects with Processing, but again; java+jogl+OSC+csound is working well for real time applications

jp



On Fri, Jan 2, 2009 at 8:34 PM, James Hearon <j_hearon@hotmail.com> wrote:
Hi,
I've seen the Vimeo vids using OSC, with oscP5 lib for Csound/Processing, which is likely the best approach.

But I was trying to use csnd.jar as a library for Processing-1.0.1. Linux F10, Csound 5.10, Processing-1.0.1.

I had a little success getting csnd.jar to work as a Processing lib, however the Csound API performance loop seems to grab control, not letting the Processing graphics thru until after the loop has finished.  I tried once without a thread, and again using a thread, but same result where graphics window only shows up after the performance loop has finished.

Wondering if any of the Csound/Processing people might know better how to go about this, using the library approach, so graphics and sound work together?
=============
//ex. 1 no thread

import csnd.*;
void setup ()
{
    size(200, 200);
   Csound c = new Csound();
   CsoundFile d = new CsoundFile();   
   d.load("/home/JCH/sketchbook/CsndTest1/Sine.csd");
   boolean b = d.exportForPerformance();
   int rcc = c.Compile("/home/JCH/sketchbook/CsndTest1/Sine.csd");
   int rc = c.Perform("/home/JCH/sketchbook/CsndTest1/Sine.csd");
   int rccc = c.Cleanup();
}
void draw()
{
  background(0);
}
===================
//ex. 2 uses Thread

import csnd.*;
void setup ()
{
  size(200, 200);
  new MyCsound();
  start();
}
void draw()
{
  background(0);
}
class MyCsound extends Thread
{
  MyCsound()
  {
    run();
  }
  void run ()
  {
    Csound c = new Csound();
    CsoundFile d = new CsoundFile();

    d.load("/home/JCH/sketchbook/CsndTest2/Sine.csd");
    boolean b = d.exportForPerformance();
    int rcc = c.Compile("/home/JCH/sketchbook/CsndTest2/Sine.csd");
    int rc = c.Perform("/home/JCH/sketchbook/CsndTest2/Sine.csd");
    int rccc = c.Cleanup();
  }
}








Life on your PC is safer, easier, and more enjoyable with Windows Vista®. See how



Date2009-01-03 20:14
Fromjhearon
Subject[Csnd] Re: csnd.jar and Processing-1.0.1
Yes, thanks.  That's an interesting thread class, and I agree a Journal
article would be a good idea.  The technology has changed since the last Jp
article, so it would good to learn more about new integration ideas with
Csound, Java, Processing, Jogl, Eclipse, NetBeans, etc. 

I hacked a bit and got the first example you posted to compile in pure
Processing (no static etc.), but can get no sound yet.  The compiler stops
at "SECTION 1:" or just before p-fields are rendered.

I know my .csd is working for Processing.

compiler output:
...
Csound version 5.10 beta (double samples) Dec 30 2008
displays suppressed
0dBFS level = 32768.0
orch now loaded
audio buffered in 1024 sample-frame blocks
ALSA output: total buffer size: 16384, period size: 1024
writing 4096-byte blks of shorts to dac:hw:0,1
SECTION 1: [stops here]

------
I tried adding the String or path to my .csd in cs.Perform in the run method
below,
but that did not seem to allow the compiler to finish the performance.

  public void run() {
    if ( cs != null ) {
      try {
        cleanFinished = false;
        cs.Perform(aCSDFile);

Will look at the second example posted, and see if I can figure out how to
make sound.
regards,


============
//Processing sketchbook file
import csnd.*;
Csound csoundInstance = new Csound();

void setup ()
{
  size(200, 200);
  if ( csoundInstance == null ) 
  {
    csnd.csoundInitialize(null, null, csnd.CSOUNDINIT_NO_SIGNAL_HANDLER);
    csoundInstance = new Csound();
  }

  int rc = csoundInstance.Compile("/home/JCH/sketchbook/CsndTest/Sine.csd");
  if ( rc == 0 ) 
  {
    CsoundThread  csoundPerformanceThread = new CsoundThread();
    csoundPerformanceThread.startCsound(); // -> at this point a Java thread
is launched and the          
    // Processing draw is continuing
  }    

}

void draw()
{
  background(0);
}

class CsoundThread extends Thread 
{
  private CsoundThread singleton;
  private Csound cs;
  private boolean running = false;
  private boolean cleanFinished = false;
  String aCSDFile = "/home/JCH/sketchbook/CsndTest/Sine.csd";

  public CsoundThread create( Csound aCs ) {
    if ( singleton != null ) {
      if ( singleton.isRunning() == true ) {
        stopCsound();
      }
    }
    singleton = new CsoundThread();
    singleton.init( aCs );
    return singleton;
  }
  /*
* ------------------------------------------------------------------
   */
  private void init( Csound aCs ) {
    cs = aCs;
  }
  public int compile( String aCSDFile ) 
  {
    if ( cs == null ) {
      return -1;
    }
    else {
      cs.Reset();
      return cs.Compile(aCSDFile);
    }
  }
  public void stopCsound() {
    if ( cs != null ) {
      cs.Stop();
      double timeOut = System.currentTimeMillis();
      while( singleton.isCleanFinished() == false ) {
        timeOut = System.currentTimeMillis() - timeOut;
        if ( timeOut >= 5000 ) {
          break;
        }
        Thread.yield();
      }
    }
  }
  public void startCsound() {
    running = true;
    start();
  }

  public void run() {
    if ( cs != null ) {
      try {
        cleanFinished = false;
        cs.Perform();
        running = false;
        cs.Cleanup();
        cs.Reset();
        cleanFinished = true;
      }
      catch( Exception ex ) {
        ex.printStackTrace();
      }
    }
  }

  /**
   * @return Returns the running.
   */
  public synchronized boolean isRunning() {
    return running;
  }

  /**
   * @param running The running to set.
   */
  public synchronized void  setRunning( boolean running ) {
    this.running = running;
  }

  public Csound getCsound() {
    return cs;
  }

  public boolean isCleanFinished() {
    return cleanFinished;
  }
}


-- 
View this message in context: http://www.nabble.com/csnd.jar-and-Processing-1.0.1-tp21256228p21268846.html
Sent from the Csound - General mailing list archive at Nabble.com.


Date2009-01-03 21:30
Fromjhearon
Subject[Csnd] Re: csnd.jar and Processing-1.0.1
...sorry.  Answering my own reply.

Problem seems to be in run method of thread class where 'cs' is null, and
thus cs.Perform does not happen.
Not sure where cs becomes null, since it seems to make it thru the compile
method of CsoundThread class o.k.
...
public void run() {
  //if ( cs == null ) {
    //java.lang.System.out.println( "cs is NULL" );
  //}
if ( cs != null ) {
try {
cleanFinished = false;
cs.Perform();
running = false;
cs.Cleanup();
cs.Reset();
cleanFinished = true;
}
catch( Exception ex ) {
ex.printStackTrace();
}
}
}


-- 
View this message in context: http://www.nabble.com/csnd.jar-and-Processing-1.0.1-tp21256228p21269649.html
Sent from the Csound - General mailing list archive at Nabble.com.