<?xml version="1.0" encoding="UTF-8" standalone="no"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>A Csound server</title> <link rel="stylesheet" type="text/css" href="csound.css" /> <meta name="generator" content="DocBook XSL Stylesheets V1.76.1" /> <link rel="home" href="index.html" title="The Canonical Csound Reference Manual" /> <link rel="up" href="tclcsound.html" title="TclCsound" /> <link rel="prev" href="cswish.html" title="Cswish: the windowing shell" /> <link rel="next" href="tclscripting.html" title="A Scripting Environment" /> </head> <body> <div class="navheader"> <table width="100%" summary="Navigation header"> <tr> <th colspan="3" align="center">A Csound server </th> </tr> <tr> <td width="20%" align="left"><a accesskey="p" href="cswish.html">Prev</a> </td> <th width="60%" align="center">TclCsound</th> <td width="20%" align="right"> <a accesskey="n" href="tclscripting.html">Next</a></td> </tr> </table> <hr /> </div> <div class="section" title="A Csound server"> <div class="titlepage"> <div> <div> <h2 class="title" style="clear: both"><a id="csserver"></a>A Csound server </h2> </div> </div> </div> <p>In Tcl, setting up TCP network connections is very simple. With a few lines of code a csound server can be built. This can accept connections from the local machine or from remote clients. Not only Tcl/Tk clients can send commands to it, but TCP connections can be made from other sofware, such as, for instance, Pure Data (PD). A Tcl script that can be run under the standard tclsh interpreter is shown below. It uses the Tclcsound module, a dynamic library that adds the Csound API commands to Tcl. </p> <pre class="programlisting"> # load tclcsound.so #(OSX: tclcsound.dylib, Windows: tclcsound.dll) load tclcsound.so Tclcsound set forever 0 # This arranges for commands to be evaluated proc ChanEval { chan client } { if { [catch { set rtn [eval [gets $chan]]} err] } { puts "Error: $err" } else { puts $client $rtn flush $client } } </pre> <pre class="programlisting"> # this arranges for connections to be made </pre> <pre class="programlisting"> proc NewChan { chan host port } { puts "Csound server: connected to $host on port $port ($chan)" fileevent $chan readable [list ChanEval $chan $host] } </pre> <pre class="programlisting"> # this sets up a server to listen for # connections </pre> <pre class="programlisting"> set server [socket -server NewChan 40001] set sinfo [fconfigure $server -sockname] puts "Csound server: ready for connections on port [lindex $sinfo 2]" vwait forever </pre> <p>With the server running, it is then possible to set up clients to control the Csound server. Such clients can be run from standard Tcl/Tk interpreters, as they do not evaluate the Csound commands themselves. Here is an example of client connections to a Csound server, using Tcl: </p> <pre class="programlisting"> # connect to server set sock [socket localhost 40001] # compile Csound code puts $sock "csCompile -odac <span class="emphasis"><em>orchestra</em></span> <span class="emphasis"><em>score</em></span>" flush $sock </pre> <pre class="programlisting"> # start performance puts $sock "csPlay" flush $sock </pre> <pre class="programlisting"> # stop performance puts $sock "csStop" flush $sock </pre> <p>As mentioned before, it is possible to set up clients using other software systems, such as PD. Such clients need only to connect to the server (using a netsend object) and send messages to it. The first item of each message is taken to be a command. Further items can optionally be added to it as arguments to that command. </p> </div> <div class="navfooter"> <hr /> <table width="100%" summary="Navigation footer"> <tr> <td width="40%" align="left"><a accesskey="p" href="cswish.html">Prev</a> </td> <td width="20%" align="center"> <a accesskey="u" href="tclcsound.html">Up</a> </td> <td width="40%" align="right"> <a accesskey="n" href="tclscripting.html">Next</a></td> </tr> <tr> <td width="40%" align="left" valign="top">Cswish: the windowing shell </td> <td width="20%" align="center"> <a accesskey="h" href="index.html">Home</a> </td> <td width="40%" align="right" valign="top"> A Scripting Environment </td> </tr> </table> </div> </body> </html>