This article has several simple examples of using the csound API  to build a simple host for csound in “c” and other wrapper libraries like java and c++.  Using C# with csound6netlib is similar if more succinct.  In its simplest form:

using csound6netlib;
public static void Main(string[] args)
{
    using (Csound6Net cs = new Csound6Net())
    {
        var cancel = new CancellationTokenSource();
        CsoundStatus result = await cs.PlayAsync(new FileInfo("csdFiles\\xanadu.csd"), null, cancel.Token);
    }
}

The C# equivalent of the “c”, c++, java etc examples from the Floss Manual page linked to above might be:

using csound6netlib;
public static int Main(string[] args) {
   //Main Csound loop
   CsoundStatus result;
   using (var csound = new Csound6Net()) {
      result = csound.Compile(args);
      if (result == CsoundStatus.Success) {
      while(!csound.PerformKsmps());
      }
   }
   return (((int)result) >= 0) ? 0 : (int)result;
}

To implement a full equivalent to csound command line program, take a look at the console program supplied for testing. It implements to "--logfile" flag as C#-style event handlers. These wrap csound callbacks and are used like any other .net event handler and should be self-explanatory to most C# programmers: Csound6NetConsole

Version 0.2.0 of Csound6NetLib.dll adds access to Csound's threading model and channel support among other Csound API capabilities.  The C++ version of Csound provides a performing thread class which is emulated in classes provided in csound6NetLib.

Below is a C# version of the standard console channel/thread example used to demonstrate this capability: a frequency is entered at the console which is forwarded to a control channel to be played in real time by Csound in a separate thread.  See new Csound6RuntimeConsole for a full implementation including how the local UserData class implements ICsound6Runnable and handles channel communication.

using csound6netlib;
class Csound6RuntimeConsole 
{
    static int Main(string[] args)
    {
        //UserData implements ICsound6Runnable.
        var channels = new UserData(); 
        //args include the csd file to perform (example plays for one minute)
        CsoundStatus result = channels.Csound.Compile(args); 
        if (result == CsoundStatus.Success)
        {
        //Sets thread in motion until Hz not a positive integer or score completes.
        //  Now we can communicate with it from here via channels or callbacks
        //  and send whatever frequency is typed from the keyboard via "pitch" channel
            var t = new Csound6NetThread(channels);
            double hz = 1;
            while (hz > 0) 
            {
                Console.Out.Write("\nEnter a pitch in Hz (or 0 to exit) and press enter:");
                string s = Console.In.ReadLine();
                if (Double.TryParse(s, out hz) && (hz > 0))
                {
                   // Use new csound 6 thread-safe signatures for channel access. 
                   channels["pitch"] = hz; 
                }
            }
            channels.Done = true;//aborts performance loop
            t.Join();            //good practice for orderly shutdown.
            while (t.IsRunning) ;//normally not running when Join returns
        }

        return (((int)result) >= 0) ? 0 : (int)result;
    }

}

Last edited Oct 31, 2013 at 8:17 PM by rhenninger, version 41

Comments

No comments yet.