| 1 | |
| 2 | <refentry id="tablewa"> |
| 3 | <indexterm id="IndexTablewa"><primary>tablewa</primary></indexterm> |
| 4 | <refentryinfo><title>Table Control:Read/Write Operations</title></refentryinfo> |
| 5 | <refmeta> |
| 6 | <refentrytitle>tablewa</refentrytitle> |
| 7 | </refmeta> |
| 8 | |
| 9 | <refnamediv> |
| 10 | <refname>tablewa</refname> |
| 11 | <refpurpose> |
| 12 | Writes tables in sequential locations. |
| 13 | </refpurpose> |
| 14 | </refnamediv> |
| 15 | |
| 16 | <refsect1> |
| 17 | <title>Description</title> |
| 18 | <para> |
| 19 | This opcode writes to a table in sequential locations to and from an a-rate variable. Some thought is required before using it. It has at least two major, and quite different, applications which are discussed below. |
| 20 | </para> |
| 21 | </refsect1> |
| 22 | |
| 23 | <refsect1> |
| 24 | <title>Syntax</title> |
| 25 | <synopsis>kstart <command>tablewa</command> kfn, asig, koff</synopsis> |
| 26 | </refsect1> |
| 27 | |
| 28 | <refsect1> |
| 29 | <title>Performance</title> |
| 30 | <para> |
| 31 | <emphasis>kstart</emphasis> -- Where in table to read or write. |
| 32 | </para> |
| 33 | |
| 34 | <para> |
| 35 | <emphasis>kfn</emphasis> -- i- or k-rate number of the table to read or write. |
| 36 | </para> |
| 37 | |
| 38 | <para> |
| 39 | <emphasis>asig</emphasis> -- a-rate signal to read from when writing to the table. |
| 40 | </para> |
| 41 | |
| 42 | <para> |
| 43 | <emphasis>koff</emphasis> -- i- or k-rate offset into table. Range unlimited - see explanation at end of this section. |
| 44 | </para> |
| 45 | |
| 46 | <para> |
| 47 | In one application, it is intended to be used with one or with several <link linkend="tablera"><citetitle>tablera</citetitle></link> opcodes before a <emphasis>tablewa</emphasis> -- all sharing the same <emphasis>kstart</emphasis> variable. |
| 48 | </para> |
| 49 | |
| 50 | <para> |
| 51 | These read from and write to sequential locations in a table at audio rates, with <emphasis>ksmps</emphasis> floats being written and read each cycle. |
| 52 | </para> |
| 53 | |
| 54 | <para> |
| 55 | <link linkend="tablera"><citetitle>tablera</citetitle></link> starts reading from location <emphasis>kstart</emphasis>. <emphasis>tablewa</emphasis> starts writing to location <emphasis>kstart</emphasis>, and then writes to <emphasis>kstart</emphasis> with the number of the location one more than the one it last wrote. (Note that for <emphasis>tablewa</emphasis>, <emphasis>kstart</emphasis> is both an input and output variable.) If the writing index reaches the end of the table, then no further writing occurs and zero is written to <emphasis>kstart</emphasis>. |
| 56 | </para> |
| 57 | |
| 58 | <para> |
| 59 | For instance, if the table's length was 16 (locations 0 to 15), and <emphasis>ksmps</emphasis> was 5. Then the following steps would occur with repetitive runs of the <emphasis>tablewa</emphasis> opcode, assuming that <emphasis>kstart</emphasis> started at 0. |
| 60 | </para> |
| 61 | |
| 62 | <informaltable frame="all"> |
| 63 | <tgroup cols="4" colsep="1" rowsep="1"> |
| 64 | <thead> |
| 65 | <row> |
| 66 | <entry>Run Number</entry> |
| 67 | <entry>Initial kstart</entry> |
| 68 | <entry>Final kstart</entry> |
| 69 | <entry>Locations Written</entry> |
| 70 | </row> |
| 71 | </thead> |
| 72 | |
| 73 | <tbody> |
| 74 | <row> |
| 75 | <entry>1</entry> |
| 76 | <entry>0</entry> |
| 77 | <entry>5</entry> |
| 78 | <entry>0 1 2 3 4</entry> |
| 79 | </row> |
| 80 | |
| 81 | <row> |
| 82 | <entry>2</entry> |
| 83 | <entry>5</entry> |
| 84 | <entry>10</entry> |
| 85 | <entry>5 6 7 8 9</entry> |
| 86 | </row> |
| 87 | |
| 88 | <row> |
| 89 | <entry>3</entry> |
| 90 | <entry>10</entry> |
| 91 | <entry>15</entry> |
| 92 | <entry>10 11 12 13 14</entry> |
| 93 | </row> |
| 94 | |
| 95 | <row> |
| 96 | <entry>4</entry> |
| 97 | <entry>15</entry> |
| 98 | <entry>0</entry> |
| 99 | <entry>15</entry> |
| 100 | </row> |
| 101 | </tbody> |
| 102 | </tgroup> |
| 103 | </informaltable> |
| 104 | |
| 105 | <para> |
| 106 | This is to facilitate processing table data using standard a-rate orchestra code between the <link linkend="tablera"><citetitle>tablera</citetitle></link> and <emphasis>tablewa</emphasis> opcodes. They allow all Csound k-rate operators to be used (with caution) on a-rate variables - something that would only be possible otherwise by <emphasis>ksmps</emphasis> = 1, downsamp and upsamp. |
| 107 | </para> |
| 108 | |
| 109 | <caution> |
| 110 | <title>Several cautions</title> |
| 111 | <para> |
| 112 | <itemizedlist> |
| 113 | <listitem> |
| 114 | <para> |
| 115 | The k-rate code in the processing loop is really running at a-rate, so time dependent functions like <emphasis>port</emphasis> and <emphasis>oscil</emphasis> work faster than normal - their code is expecting to be running at k-rate. |
| 116 | </para> |
| 117 | </listitem> |
| 118 | |
| 119 | <listitem> |
| 120 | <para> |
| 121 | This system will produce undesirable results unless the <emphasis>ksmps</emphasis> fits within the table length. For instance a table of length 16 will accommodate 1 to 16 samples, so this example will work with <emphasis>ksmps</emphasis> = 1 to 16. |
| 122 | </para> |
| 123 | </listitem> |
| 124 | </itemizedlist> |
| 125 | </para> |
| 126 | </caution> |
| 127 | |
| 128 | <para> |
| 129 | Both these opcodes generate an error and deactivate the instrument if a table with length < <emphasis>ksmps</emphasis> is selected. Likewise an error occurs if <emphasis>kstart</emphasis> is below 0 or greater than the highest entry in the table - if <emphasis>kstart</emphasis> = table length. |
| 130 | </para> |
| 131 | |
| 132 | <para> |
| 133 | <itemizedlist> |
| 134 | <listitem> |
| 135 | <para> |
| 136 | <emphasis>kstart</emphasis> is intended to contain integer values between 0 and (table length - 1). Fractional values above this should not affect operation but do not achieve anything useful. |
| 137 | </para> |
| 138 | </listitem> |
| 139 | |
| 140 | <listitem> |
| 141 | <para> |
| 142 | These opcodes are not interpolating, and the <emphasis>kstart</emphasis> and <emphasis>koff</emphasis> parameters always have a range of 0 to (table length - 1) - not 0 to 1 as is available in other table read/write opcodes. <emphasis>koff</emphasis> can be outside this range but it is wrapped around by the final AND operation. |
| 143 | </para> |
| 144 | </listitem> |
| 145 | |
| 146 | <listitem> |
| 147 | <para> |
| 148 | These opcodes are permanently in wrap mode. When <emphasis>koff</emphasis> is 0, no wrapping needs to occur, since the <emphasis>kstart</emphasis>++ index will always be within the table's normal range. <emphasis>koff</emphasis> not equal to 0 can lead to wrapping. |
| 149 | </para> |
| 150 | </listitem> |
| 151 | |
| 152 | <listitem> |
| 153 | <para> |
| 154 | The offset does not affect the number of read/write cycles performed, or the value written to <emphasis>kstart</emphasis> by <emphasis>tablewa</emphasis>. |
| 155 | </para> |
| 156 | </listitem> |
| 157 | |
| 158 | <listitem> |
| 159 | <para> |
| 160 | These opcodes cannot read or write the guardpoint. Use <emphasis>tablegpw</emphasis> to write the guardpoint after manipulations have been done with <emphasis>tablewa</emphasis>. |
| 161 | </para> |
| 162 | </listitem> |
| 163 | </itemizedlist> |
| 164 | </para> |
| 165 | </refsect1> |
| 166 | |
| 167 | <refsect1> |
| 168 | <title>Examples</title> |
| 169 | <para> |
| 170 | <informalexample> |
| 171 | <programlisting> |
| 172 | kstart <emphasis role="op">=</emphasis> 0 |
| 173 | |
| 174 | <emphasis role="olabel">lab1:</emphasis> |
| 175 | atemp <emphasis role="opc">tablera</emphasis> ktabsource, kstart, 0 <emphasis role="comment">; Read 5 values from table into an</emphasis> |
| 176 | <emphasis role="comment">; a-rate variable.</emphasis> |
| 177 | |
| 178 | atemp <emphasis role="op">=</emphasis> <emphasis role="opc">log</emphasis>(atemp) <emphasis role="comment">; Process the values using a-rate</emphasis> |
| 179 | <emphasis role="comment">; code.</emphasis> |
| 180 | |
| 181 | kstart <emphasis role="opc">tablewa</emphasis> ktabdest, atemp, 0 <emphasis role="comment">; Write it back to the table</emphasis> |
| 182 | |
| 183 | <emphasis role="octrl">if</emphasis> ktemp 0 <emphasis role="octrl">goto</emphasis> <emphasis role="olabel">lab1</emphasis> <emphasis role="comment">; Loop until all table locations</emphasis> |
| 184 | <emphasis role="comment">; have been processed.</emphasis></programlisting> |
| 185 | </informalexample> |
| 186 | </para> |
| 187 | |
| 188 | <para> |
| 189 | The above example shows a processing loop, which runs every k-cycle, reading each location in the table <emphasis>ktabsource</emphasis>, and writing the log of those values into the same locations of table <emphasis>ktabdest</emphasis>. |
| 190 | </para> |
| 191 | |
| 192 | <para> |
| 193 | This enables whole tables, parts of tables (with offsets and different control loops) and data from several tables at once to be manipulated with a-rate code and written back to another (or to the same) table. This is a bit of a fudge, but it is faster than doing it with k-rate table read and write code. |
| 194 | </para> |
| 195 | |
| 196 | <para> |
| 197 | Another application is: |
| 198 | </para> |
| 199 | |
| 200 | <para> |
| 201 | <informalexample> |
| 202 | <programlisting> |
| 203 | kzero <emphasis role="op">=</emphasis> 0 |
| 204 | kloop <emphasis role="op">=</emphasis> 0 |
| 205 | |
| 206 | kzero <emphasis role="opc">tablewa</emphasis> 23, asignal, 0 <emphasis role="comment">; ksmps a-rate samples written</emphasis> |
| 207 | <emphasis role="comment">; into locations 0 to (ksmps -1) of table 23.</emphasis> |
| 208 | |
| 209 | <emphasis role="olabel">lab1:</emphasis> ktemp <emphasis role="opc">table</emphasis> kloop, 23 <emphasis role="comment">; Start a loop which runs ksmps times,</emphasis> |
| 210 | <emphasis role="comment">; in which each cycle processes one of</emphasis> |
| 211 | [ Some code to manipulate ] <emphasis role="comment">; table 23's values with k-rate orchestra</emphasis> |
| 212 | [ the value of ktemp. ] <emphasis role="comment">; code.</emphasis> |
| 213 | |
| 214 | <emphasis role="opc">tablew</emphasis> ktemp, kloop, 23 <emphasis role="comment">; Write the processed value to the table.</emphasis> |
| 215 | |
| 216 | kloop <emphasis role="op">=</emphasis> kloop <emphasis role="op">+</emphasis> 1 <emphasis role="comment">; Increment the kloop, which is both the</emphasis> |
| 217 | <emphasis role="comment">; pointer into the table and the loop</emphasis> |
| 218 | <emphasis role="octrl">if</emphasis> kloop <emphasis role="op"><</emphasis> ksmps <emphasis role="octrl">goto</emphasis> <emphasis role="olabel">lab1</emphasis> <emphasis role="comment">; counter. Keep looping until all values</emphasis> |
| 219 | <emphasis role="comment">; in the table have been processed.</emphasis> |
| 220 | |
| 221 | asignal <emphasis role="opc">tablera</emphasis> 23, 0, 0 <emphasis role="comment">; Copy the table contents back</emphasis> |
| 222 | <emphasis role="comment">; to an a-rate variable.</emphasis></programlisting> |
| 223 | </informalexample> |
| 224 | </para> |
| 225 | |
| 226 | <para> |
| 227 | <emphasis>koff</emphasis> -- This is an offset which is added to the sum of <emphasis>kstart</emphasis> and the internal index variable which steps through the table. The result is then ANDed with the lengthmask (000 0111 for a table of length 8 - or 9 with guardpoint) and that final index is used to read or write to the table. <emphasis>koff</emphasis> can be any value. It is converted into a long using the ANSI floor() function so that -4.3 becomes -5. This is what we would want when using offsets which range above and below zero. |
| 228 | </para> |
| 229 | |
| 230 | <para> |
| 231 | Ideally this would be an optional variable, defaulting to 0, however with the existing Csound orchestra read code, such default parameters must be init time only. We want k-rate here, so we cannot have a default. |
| 232 | </para> |
| 233 | </refsect1> |
| 234 | |
| 235 | <refsect1> |
| 236 | <title>See also</title> |
| 237 | <para><link linkend="TableReadwrit"><citetitle>Read/Write Operations</citetitle></link></para> |
| 238 | </refsect1> |
| 239 | |
| 240 | <refsect1> |
| 241 | <title>Credits</title> |
| 242 | <para> |
| 243 | <simplelist> |
| 244 | <member>Author: Robin Whittle</member> |
| 245 | <member>Australia</member> |
| 246 | </simplelist> |
| 247 | </para> |
| 248 | </refsect1> |
| 249 | </refentry> |