Csound Csound-dev Csound-tekno Search About

Re: [Cs-dev] Java Wrapper Now Working

Date2005-12-11 23:26
FromMichael Gogins
SubjectRe: [Cs-dev] Java Wrapper Now Working
Thanks for the example. The host data issue is an argument for including a separate host data pointer for each callback, as some of the callbacks currently do, but not all.

Before you started working on this, I was experimenting with different approaches. I started making Csound itself a director class, then I started an approach similar to yours.

The most elegant approach is making Csound (in Csound.hpp) a director class. This however would require 2 pointers for each callback. At the bottom of the stack, Csound::onCallback (C++/director virtual method) would be called. By default, it in turn would call the original default Csound (in csound.c) callback. Anyone overriding the virtual method would intercept the default callback. The other callback pointer is required to call into Csound::callback from Csound (in csound.c). 

It would also be necessary to keep track of which instance of CSOUND is firing the callback in order to call into the correct instance of Csound::callback.

There's no getting around the 2 layer callback, it's just a question which is the most elegant and most efficient solution.

Regards,
Mike

-----Original Message-----
From: Istvan Varga 
Sent: Dec 11, 2005 5:30 PM
To: csound-devel@lists.sourceforge.net
Subject: Re: [Cs-dev] Java Wrapper Now Working

On Sunday 11 December 2005 20:12, Victor Lazzarini wrote:

> Could you post an example?

Here is a simple Java example that works with the current CVS
sources (note that this is not implemented for other languages
yet, and may be changed or removed later). The C++ class it
uses is also included below.

// CallbackExample.java

import csnd.*;

public class CallbackExample {

  // JavaCallbacks is a subclass of CsoundCallbackWrapper that
  // can override the methods MessageCallback(), InputValueCallback(),
  // OutputValueCallback(), and YieldCallback() for defining callbacks

  static class JavaCallbacks extends CsoundCallbackWrapper {
    JavaCallbacks(Csound cs)
    {
      super(cs);
    }
    public int YieldCallback()
    {
      java.lang.System.err.println("Yield called for " + GetCsound() + " !");
      return 1;
    }
    public void MessageCallback(int attr, String msg)
    {
      if (msg.endsWith("\n"))
        java.lang.System.err.print("ATTR: " + attr + "  MSG: " + msg);
      else
        java.lang.System.err.println("ATTR: " + attr + "  MSG: " + msg);
    }
  }

  public static void main(String argv[])
  {
    Csound cs = new Csound();
    // create a callback wrapper object for this instance of Csound
    // note that this will use the host data pointer of 'cs', so the
    // host data cannot be used for other purposes
    JavaCallbacks cb = new JavaCallbacks(cs);
    // enable the callbacks for which there are definitions above
    cb.SetYieldCallback();
    cb.SetMessageCallback();
    // build an argument list from the command line options
    CsoundArgVList args = new CsoundArgVList();
    args.Append("csound");
    for (int i = 0; i < argv.length; i++)
      args.Append(argv[i]);
    // call Compile() to see if the callbacks work
    cs.Compile(args.argc(), args.argv());
    // Reset() must be called before 'cs' or 'cb' is possibly
    // garbage collected, otherwise a crash may occur
    cs.Reset();
    // free memory used by the argument list; this is optional, and
    // would be done automatically when 'args' is garbage collected
    args.Clear();
  }
};

// end of CallbackExample.java

---------------------------------------------------------------------

// callback wrapper class declared in interfaces/cs_glue.hpp

class CsoundCallbackWrapper {
 private:
    CSOUND  *csound_;
 public:
    virtual void MessageCallback(int attr, char *msg)
    {
    }
    virtual MYFLT InputValueCallback(const char *chnName)
    {
      return (MYFLT) 0;
    }
    virtual void OutputValueCallback(const char *chnName, MYFLT value)
    {
    }
    virtual int YieldCallback()
    {
      return 1;
    }
    void SetMessageCallback();
    void SetInputValueCallback();
    void SetOutputValueCallback();
    void SetYieldCallback();
    CSOUND *GetCsound()
    {
      return csound_;
    }
    // --------
    CsoundCallbackWrapper(Csound *csound);
    CsoundCallbackWrapper(CSOUND *csound);
    virtual ~CsoundCallbackWrapper()
    {
    }
};


-------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc. Do you grep through log files
for problems?  Stop!  Download the new AJAX search engine that makes
searching your log files as easy as surfing the  web.  DOWNLOAD SPLUNK!
http://ads.osdn.com/?ad_id=7637&alloc_id=16865&op=click
_______________________________________________
Csound-devel mailing list
Csound-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/csound-devel





-------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc. Do you grep through log files
for problems?  Stop!  Download the new AJAX search engine that makes
searching your log files as easy as surfing the  web.  DOWNLOAD SPLUNK!
http://ads.osdn.com/?ad_id=7637&alloc_id=16865&op=click
_______________________________________________
Csound-devel mailing list
Csound-devel@lists.sourceforge.net