Sophie

Sophie

distrib > Mageia > 5 > x86_64 > media > core-release > by-pkgid > b707d9a4ee443103660a75ccb6e51334 > files > 2163

csound-doc-5.19.01-10.mga5.noarch.rpm

<?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>lua_opdef</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="OpcodesTop.html" title="Orchestra Opcodes and Operators" />
    <link rel="prev" href="lua_exec.html" title="lua_exec" />
    <link rel="next" href="lua_opcall.html" title="lua_opcall" />
  </head>
  <body>
    <div class="navheader">
      <table width="100%" summary="Navigation header">
        <tr>
          <th colspan="3" align="center">
            lua_opdef
        </th>
        </tr>
        <tr>
          <td width="20%" align="left"><a accesskey="p" href="lua_exec.html">Prev</a> </td>
          <th width="60%" align="center">Orchestra Opcodes and Operators</th>
          <td width="20%" align="right"> <a accesskey="n" href="lua_opcall.html">Next</a></td>
        </tr>
      </table>
      <hr />
    </div>
    <div class="refentry" title="lua_opdef">
      <a id="lua_opdef"></a>
      <div class="titlepage"></div>
      <a id="Indexlua_opdef" class="indexterm"></a>
      <div class="refnamediv">
        <h2>
          <span class="refentrytitle">
            lua_opdef
        </span>
        </h2>
        <p>
            lua_opdef
         — 
             Define an opcode in Lua at i-rate. The opcode can take any 
             number of output and/or input arguments of any type. 
        </p>
      </div>
      <div class="refsect1" title="Description">
        <a id="idp29997304"></a>
        <h2>
            Description
        </h2>
        <p>
            Define an opcode in Lua at i-rate. The opcode can take any 
            number of output and/or input arguments of any type. 
            The code is executed at initialization time, typically from the 
            orchestra header. Global and local variables, functions, 
            tables, and classes may be declared and defined. Objects defined 
            at global Lua scope remain in scope throughout the performance, 
            and are visible to any other Lua code in the same Csound thread.
        </p>
        <div class="note" title="Note" style="margin-left: 0.5in; margin-right: 0.5in;">
          <table border="0" summary="Note">
            <tr>
              <td rowspan="2" align="center" valign="top" width="25">
                <img alt="[Note]" src="images/note.png" />
              </td>
              <th align="left">Note</th>
            </tr>
            <tr>
              <td align="left" valign="top">
                <p>
               By default, all objects defined in Lua are defined at global 
               scope. In order to ensure that objects are confined to their 
               own block of code, that is to ensure that the object is visible 
               only in lexical scope, the object must be declared as local. 
               This is the feature of Lua that beginners tend to find the most 
               troublesome.
            </p>
                <p>
               Another thing to look out for is that Lua arrays use 1-based 
               indexing, not the 0-based indexing used in C and many other 
               programming languages.
            </p>
              </td>
            </tr>
          </table>
        </div>
      </div>
      <div class="refsect1" title="Syntax">
        <a id="idp30038152"></a>
        <h2>
            Syntax
        </h2>
        <pre class="synopsis"><span class="command"><strong>lua_opdef</strong></span> Sname, Sluacode</pre>
      </div>
      <div class="refsect1" title="Initialization">
        <a id="idp30039096"></a>
        <h2>
            Initialization
        </h2>
        <p>
            <span class="emphasis"><em>Sname</em></span> -- The name of the opcode.
        </p>
        <p>
            <span class="emphasis"><em>Sluacode</em></span> -- A block of Lua code, of any 
            length. Multi-line blocks may be enclosed in double braces
            (i.e. <code class="literal">{{ }}</code>). This code is 
            evaluated once at initialization time.  
        </p>
        <p>
            The Lua code must define all functions that will be called from Csound,
            using the following naming convention, where opcodename
            stands for the actual opcode name:
        </p>
        <div class="itemizedlist">
          <ul class="itemizedlist" type="disc">
            <li class="listitem">
                <code class="literal">opcodename_init</code> for the i-rate opcode subroutine.
            </li>
            <li class="listitem">
                <code class="literal">opcodename_kontrol</code> for the k-rate opcode subroutine.
            </li>
            <li class="listitem">
                <code class="literal">opcodename_audio</code> for the a-rate opcode subroutine.
            </li>
            <li class="listitem">
                <code class="literal">opcodename_noteoff</code> for the note-off subroutine.
            </li>
          </ul>
        </div>
        <p>
            Each of these Lua functions will receive three lightuserdata
            (i.e. pointer) arguments: the CSOUND object, the opcode instance,
            and a pointer to the opcode arguments, which the Lua code must be type cast
            to a LuaJIT FFI ctype structure containing the opcode output arguments,
            input arguments, and state variables. Using LuaJIT FFI, the elements of
            this structure will be accessible as though they were Lua types.
        </p>
        <p>
            Each of these Lua functions must return 0 for success
            or 1 for failure.
        </p>
        <p>
            The Lua functions may do absolutely anything, although of
            course if real-time performance is expected, care must be
            taken to disable Lua garbage collection and observe other
            recommendations for real-time code.
        </p>
      </div>
      <div class="refsect1" title="Example">
        <a id="idp30045656"></a>
        <h2>
            Example
        </h2>
        <p>
            Here is an example of a Lua opcode, implementing a Moog 
            ladder filter. For purposes of comparison, a user-defined 
            opcode and the native Csound opcode that compute the same 
            sound using the same algorithm also are shown, and timed. 
            The example uses the file <a class="ulink" href="examples/luamoog.csd" target="_top">
            <em class="citetitle">luamoog.csd</em></a>.
            </p>
        <div class="example">
          <a id="idp30047608"></a>
          <p class="title">
            <strong>Example 430. 
                    Example of a Lua opcode.
                </strong>
          </p>
          <div class="example-contents">
            <pre class="programlisting">
<span class="csdtag">&lt;CsoundSynthesizer&gt;</span>
<span class="csdtag">&lt;CsInstruments&gt;</span>
<span class="ohdr">sr</span> <span class="op">=</span>    48000
<span class="ohdr">ksmps</span> <span class="op">=</span>   100
<span class="ohdr">nchnls</span> <span class="op">=</span>    1

    gibegan     <span class="opc">rtclock</span>

    <span class="opc">lua_opdef</span>   "moogladder", {{
local ffi = require("ffi")
local math = require("math")
local string = require("string")
local csoundApi = ffi.load('csound64.dll.5.2')
ffi.cdef[[
    int csoundGetKsmps(void *);
    double csoundGetSr(void *);
    struct moogladder_t {
      double *out;
      double *inp;
      double *freq;
      double *res;
      double *istor;
      double sr;
      double ksmps&gt;;
      double thermal;
      double f;
      double fc;
      double fc2;
      double fc3;
      double fcr;
      double acr;
      double tune;
      double res4;
      double input;
      double i;
      double j;
      double k;
      double kk;
      double stg[6];
      double delay[6];
      double tanhstg[6];
    };
]]

local moogladder_ct = ffi.typeof('struct moogladder_t *')

function moogladder_init(csound, opcode, carguments)
    local p = ffi.cast(moogladder_ct, carguments)
    p.sr = csoundApi.csoundGetSr(csound)
    p.ksmps = csoundApi.csoundGetKsmps(csound)
    if p.istor[0] == 0 then
        for i = 0, 5 do
            p.delay[i] = 0.0
        end
        for i = 0, 3 do
            p.tanhstg[i] = 0.0
        end
    end
    return 0
end

function moogladder_kontrol(csound, opcode, carguments)
    local p = ffi.cast(moogladder_ct, carguments)
    -- transistor thermal voltage
    p.thermal = 1.0 / 40000.0
    if p.res[0] &lt; 0.0 then
        p.res[0] = 0.0
    end
    -- sr is half the actual filter sampling rate
    p.fc = p.freq[0] / p.sr
    p.f = p.fc / 2.0
    p.fc2 = p.fc * p.fc
    p.fc3 = p.fc2 * p.fc
    -- frequency &amp; amplitude correction
    p.fcr = 1.873 * p.fc3 + 0.4955 * p.fc2 - 0.6490 * p.fc + 0.9988
    p.acr = -3.9364 * p.fc2 + 1.8409 * p.fc + 0.9968
    -- filter tuning
    p.tune = (1.0 - math.exp(-(2.0 * math.pi * p.f * p.fcr))) / p.thermal
    p.res4 = 4.0 * p.res[0] * p.acr
    -- Nested 'for' loops crash, not sure why.
    -- Local loop variables also are problematic.
    -- Lower-level loop constructs don't crash.
    p.i = 0
    while p.i &lt; p.ksmps do
        p.j = 0
        while p.j &lt; 2 do
            p.k = 0
            while p.k &lt; 4 do
                if p.k == 0 then
                    p.input = p.inp[p.i] - p.res4 * p.delay[5]
                    p.stg[p.k] = p.delay[p.k] + p.tune * (math.tanh(p.input * p.thermal) - p.tanhstg[p.k])
                else
                    p.input = p.stg[p.k - 1]
                    p.tanhstg[p.k - 1] = math.tanh(p.input * p.thermal)
                    if p.k &lt; 3 then
                        p.kk = p.tanhstg[p.k]
                    else
                        p.kk = math.tanh(p.delay[p.k] * p.thermal)
                    end
                    p.stg[p.k] = p.delay[p.k] + p.tune * (p.tanhstg[p.k - 1] - p.kk)
                end
                p.delay[p.k] = p.stg[p.k]
                p.k = p.k + 1
            end
            -- 1/2-sample delay for phase compensation
            p.delay[5] = (p.stg[3] + p.delay[4]) * 0.5
            p.delay[4] = p.stg[3]
            p.j = p.j + 1
        end
        p.out[p.i] = p.delay[5]
        p.i = p.i + 1
    end
    return 0
end
}}

<span class="comment">/*
Moogladder - An improved implementation of the Moog ladder filter

DESCRIPTION
This is an new digital implementation of the Moog ladder filter based on the work of Antti Huovilainen,
described in the paper \"Non-Linear Digital Implementation of the Moog Ladder Filter\" (Proceedings of DaFX04, Univ of Napoli).
This implementation is probably a more accurate digital representation of the original analogue filter.
This is version 2 (revised 14/DEC/04), with improved amplitude/resonance scaling and frequency correction using a couple of polynomials,as suggested by Antti.

SYNTAX
ar  Moogladder  asig, kcf, kres

PERFORMANCE
asig - input signal
kcf - cutoff frequency (Hz)
kres - resonance (0 - 1).

CREDITS
Victor Lazzarini
*/</span>

                    <span class="oblock">opcode</span>  moogladderu, a, akk
asig, kcf, kres     <span class="opc">xin</span>
                    <span class="opc">setksmps</span>    1
ipi                 <span class="op">=</span>           4 <span class="op">*</span> <span class="opc">taninv</span>(1)
<span class="op">/</span><span class="op">*</span> filter delays <span class="op">*</span><span class="op">/</span>
az1                 <span class="opc">init</span>        0
az2                 <span class="opc">init</span>        0
az3                 <span class="opc">init</span>        0
az4                 <span class="opc">init</span>        0
az5                 <span class="opc">init</span>        0
ay4                 <span class="opc">init</span>        0
amf                 <span class="opc">init</span>        0
                    <span class="octrl">if</span>          kres <span class="op">&gt;</span> 1 then
kres                <span class="op">=</span>           1
                    <span class="octrl">elseif</span>      kres <span class="op">&lt;</span> 0 then
kres                <span class="op">=</span>           0
                    <span class="octrl">endif</span>
<span class="op">/</span><span class="op">*</span> twice the \'thermal voltage of <span class="opc">a</span> transistor\' <span class="op">*</span><span class="op">/</span>
i2v                 <span class="op">=</span>           40000
<span class="op">/</span><span class="op">*</span> <span class="ohdr">sr</span> is half the actual filter sampling rate  <span class="op">*</span><span class="op">/</span>
kfc                 <span class="op">=</span>           kcf<span class="op">/</span><span class="ohdr">sr</span>
kf                  <span class="op">=</span>           kcf<span class="op">/</span>(<span class="ohdr">sr</span><span class="op">*</span>2)
<span class="op">/</span><span class="op">*</span> frequency <span class="op">&amp;</span> amplitude correction  <span class="op">*</span><span class="op">/</span>
kfcr                <span class="op">=</span>           1.8730 <span class="op">*</span> (kfc^3) <span class="op">+</span> 0.4955 <span class="op">*</span> (kfc^2) <span class="op">-</span> 0.6490 <span class="op">*</span> kfc <span class="op">+</span> 0.9988
kacr                <span class="op">=</span>           <span class="op">-</span>3.9364 <span class="op">*</span> (kfc^2) <span class="op">+</span> 1.8409 <span class="op">*</span> kfc <span class="op">+</span> 0.9968
<span class="op">/</span><span class="op">*</span> filter tuning  <span class="op">*</span><span class="op">/</span>
k2vg                <span class="op">=</span>           i2v <span class="op">*</span> (1 <span class="op">-</span> <span class="opc">exp</span>(<span class="op">-</span>2 <span class="op">*</span> ipi <span class="op">*</span> kfcr <span class="op">*</span> kf))
<span class="op">/</span><span class="op">*</span> cascade of 4 1st order sections         <span class="op">*</span><span class="op">/</span>
ay1                 <span class="op">=</span>           az1 <span class="op">+</span> k2vg <span class="op">*</span> (<span class="opc">tanh</span>((asig <span class="op">-</span> 4 <span class="op">*</span> kres <span class="op">*</span> amf <span class="op">*</span> kacr) <span class="op">/</span> i2v) <span class="op">-</span> <span class="opc">tanh</span>(az1 <span class="op">/</span> i2v))
az1                 <span class="op">=</span>           ay1
ay2                 <span class="op">=</span>           az2 <span class="op">+</span> k2vg <span class="op">*</span> (<span class="opc">tanh</span>(ay1 <span class="op">/</span> i2v) <span class="op">-</span> <span class="opc">tanh</span>(az2 <span class="op">/</span> i2v ))
az2                 <span class="op">=</span>           ay2
ay3                 <span class="op">=</span>           az3 <span class="op">+</span> k2vg <span class="op">*</span> (<span class="opc">tanh</span>(ay2 <span class="op">/</span> i2v) <span class="op">-</span> <span class="opc">tanh</span>(az3 <span class="op">/</span> i2v))
az3                 <span class="op">=</span>           ay3
ay4                 <span class="op">=</span>           az4 <span class="op">+</span> k2vg <span class="op">*</span> (<span class="opc">tanh</span>(ay3 <span class="op">/</span> i2v) <span class="op">-</span> <span class="opc">tanh</span>(az4 <span class="op">/</span> i2v))
az4                 <span class="op">=</span>           ay4
<span class="op">/</span><span class="op">*</span> 1<span class="op">/</span>2<span class="op">-</span>sample <span class="opc">delay</span> for phase compensation  <span class="op">*</span><span class="op">/</span>
amf                 <span class="op">=</span>           (ay4 <span class="op">+</span> az5) <span class="op">*</span>0.5
az5                 <span class="op">=</span>           ay4
<span class="op">/</span><span class="op">*</span> oversampling  <span class="op">*</span><span class="op">/</span>
ay1                 <span class="op">=</span>           az1 <span class="op">+</span> k2vg <span class="op">*</span> (<span class="opc">tanh</span>((asig <span class="op">-</span> 4 <span class="op">*</span> kres <span class="op">*</span> amf <span class="op">*</span> kacr) <span class="op">/</span> i2v) <span class="op">-</span> <span class="opc">tanh</span>(az1 <span class="op">/</span> i2v))
az1                 <span class="op">=</span>           ay1
ay2                 <span class="op">=</span>           az2 <span class="op">+</span> k2vg <span class="op">*</span> (<span class="opc">tanh</span>(ay1 <span class="op">/</span> i2v) <span class="op">-</span> <span class="opc">tanh</span>(az2 <span class="op">/</span> i2v ))
az2                 <span class="op">=</span>           ay2
ay3                 <span class="op">=</span>           az3 <span class="op">+</span> k2vg <span class="op">*</span> (<span class="opc">tanh</span>(ay2 <span class="op">/</span> i2v) <span class="op">-</span> <span class="opc">tanh</span>(az3 <span class="op">/</span> i2v))
az3                 <span class="op">=</span>           ay3
ay4                 <span class="op">=</span>           az4 <span class="op">+</span> k2vg <span class="op">*</span> (<span class="opc">tanh</span>(ay3 <span class="op">/</span> i2v) <span class="op">-</span> <span class="opc">tanh</span>(az4 <span class="op">/</span> i2v))
az4                 <span class="op">=</span>           ay4
amf                 <span class="op">=</span>           (ay4 <span class="op">+</span> az5) <span class="op">*</span> 0.5
az5                 <span class="op">=</span>           ay4
                    <span class="opc">xout</span>        amf
                    <span class="oblock">endop</span>

<span class="oblock">instr</span> 1
                <span class="opc">prints</span>      "No filter.\n"
	kfe         <span class="opc">expseg</span>      500, p3<span class="op">*</span>0.9, 1800, p3<span class="op">*</span>0.1, 3000
    kenv        <span class="opc">linen</span>       10000, 0.05, p3, 0.05
    asig        <span class="opc">buzz</span>        kenv, 100, <span class="ohdr">sr</span><span class="op">/</span>(200), 1
    <span class="comment">; afil      moogladder  asig, kfe, 1</span>
                <span class="opc">out</span>         asig
<span class="oblock">endin</span>

<span class="oblock">instr</span> 2
                <span class="opc">prints</span>      "Native moogladder.\n"
	kfe         <span class="opc">expseg</span>      500, p3<span class="op">*</span>0.9, 1800, p3<span class="op">*</span>0.1, 3000
    kenv        <span class="opc">linen</span>       10000, 0.05, p3, 0.05
    asig        <span class="opc">buzz</span>        kenv, 100, <span class="ohdr">sr</span><span class="op">/</span>(200), 1
    afil        <span class="opc">moogladder</span>  asig, kfe, 1
                <span class="opc">out</span>         afil
<span class="oblock">endin</span>

<span class="oblock">instr</span> 3
                <span class="opc">prints</span>      "UDO moogladder.\n"
	kfe         <span class="opc">expseg</span>      500, p3<span class="op">*</span>0.9, 1800, p3<span class="op">*</span>0.1, 3000
    kenv        <span class="opc">linen</span>       10000, 0.05, p3, 0.05
    asig        <span class="opc">buzz</span>        kenv, 100, <span class="ohdr">sr</span><span class="op">/</span>(200), 1
    afil        moogladderu asig, kfe, 1
                <span class="opc">out</span>         afil
<span class="oblock">endin</span>

<span class="oblock">instr</span> 4
                <span class="opc">prints</span>      "Lua moogladder.\n"
    kres        <span class="opc">init</span>        1
    istor       <span class="opc">init</span>        0
	kfe         <span class="opc">expseg</span>      500, p3<span class="op">*</span>0.9, 1800, p3<span class="op">*</span>0.1, 3000
    kenv        <span class="opc">linen</span>       10000, 0.05, p3, 0.05
    asig        <span class="opc">buzz</span>        kenv, 100, <span class="ohdr">sr</span><span class="op">/</span>(200), 1
    afil        <span class="opc">init</span>        0
                <span class="opc">lua_ikopcall</span>    "moogladder", afil, asig, kfe, kres, istor
                <span class="opc">out</span>         afil
<span class="oblock">endin</span>

<span class="oblock">instr</span> 5
    giended     <span class="opc">rtclock</span>
    ielapsed    <span class="op">=</span>           giended <span class="op">-</span> gibegan
                <span class="opc">print</span>       ielapsed
    gibegan     <span class="opc">rtclock</span>
<span class="oblock">endin</span>

<span class="csdtag">&lt;/CsInstruments&gt;</span>
<span class="csdtag">&lt;CsScore&gt;</span>
<span class="stamnt">f</span> 1     0 65536 10 1
<span class="stamnt">i</span> 5.1   0   1
<span class="stamnt">i</span> 4     1   20
<span class="stamnt">i</span> 5.2   21  1
<span class="stamnt">i</span> 4     22  20
<span class="stamnt">i</span> 5.3   42  1
<span class="stamnt">i</span> 2     43  20
<span class="stamnt">i</span> 5.4   63  1
<span class="stamnt">i</span> 2     64  20
<span class="stamnt">i</span> 5.5   84  1
<span class="stamnt">i</span> 3     85  20
<span class="stamnt">i</span> 5.6   105 1
<span class="stamnt">i</span> 3     106 20
<span class="stamnt">i</span> 5.7   126 1
<span class="stamnt">i</span> 1     127 20
<span class="stamnt">i</span> 5.8   147 1
<span class="stamnt">i</span> 1     148 20
<span class="stamnt">i</span> 5.9   168 1
<span class="stamnt">i</span> 4     169 20
<span class="stamnt">i</span> 4     170 20
<span class="stamnt">i</span> 4     171 20
<span class="stamnt">e</span>
<span class="csdtag">&lt;/CsScore&gt;</span>
<span class="csdtag">&lt;/CsoundSynthesizer&gt;</span>
</pre>
          </div>
        </div>
        <p><br class="example-break" />
        </p>
      </div>
      <div class="refsect1" title="See Also">
        <a id="idp30048656"></a>
        <h2>
            See Also
        </h2>
        <p>
            <a class="link" href="lua_exec.html" title="lua_exec"><em class="citetitle">lua_exec</em></a>, 
            <a class="link" href="lua_opcall.html" title="lua_opcall"><em class="citetitle">lua_opcall</em></a>. 
        </p>
      </div>
      <div class="refsect1" title="Credits">
        <a id="idp30050200"></a>
        <h2>
            Credits
        </h2>
        <p>
            By: Michael Gogins 2011
        </p>
        <p>
            New in Csound version 5.13.2
        </p>
      </div>
    </div>
    <div class="navfooter">
      <hr />
      <table width="100%" summary="Navigation footer">
        <tr>
          <td width="40%" align="left"><a accesskey="p" href="lua_exec.html">Prev</a> </td>
          <td width="20%" align="center">
            <a accesskey="u" href="OpcodesTop.html">Up</a>
          </td>
          <td width="40%" align="right"> <a accesskey="n" href="lua_opcall.html">Next</a></td>
        </tr>
        <tr>
          <td width="40%" align="left" valign="top">
            lua_exec
         </td>
          <td width="20%" align="center">
            <a accesskey="h" href="index.html">Home</a>
          </td>
          <td width="40%" align="right" valign="top"> 
            lua_opcall
        </td>
        </tr>
      </table>
    </div>
  </body>
</html>