<?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>GENfarey</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="ScoregensTop.html" title="Score Statements and GEN Routines" /> <link rel="prev" href="GENsone.html" title="GENsone" /> <link rel="next" href="GENwave.html" title="GENwave" /> </head> <body> <div class="navheader"> <table width="100%" summary="Navigation header"> <tr> <th colspan="3" align="center">GENfarey</th> </tr> <tr> <td width="20%" align="left"><a accesskey="p" href="GENsone.html">Prev</a> </td> <th width="60%" align="center">Score Statements and GEN Routines</th> <td width="20%" align="right"> <a accesskey="n" href="GENwave.html">Next</a></td> </tr> </table> <hr /> </div> <div class="refentry" title="GENfarey"> <a id="GENfarey"></a> <div class="titlepage"></div> <a id="IndexGENfarey" class="indexterm"></a> <div class="refnamediv"> <h2> <span class="refentrytitle">GENfarey</span> </h2> <p>"farey" — Fills a table with the Farey Sequence F<sub>n</sub> of the integer n. </p> </div> <div class="refsect1" title="Description"> <a id="idp78636408"></a> <h2>Description</h2> <p> A Farey Sequence F<sub>n</sub> of order n is a list of fractions in their lowest terms between 0 and 1 and in ascending order. Their denominators do not exceed n. This means a fraction a/b belongs to F<sub>n</sub> if 0 ≤ a ≤ b ≤ n. The numerator and denominator of each fraction are always coprime. 0 and 1 are included in F<sub>n</sub> as the fractions 0/1 and 1/1. For example F<sub>5</sub> = {0/1, 1/5, 1/4, 1/3, 2/5, 1/2, 3/5, 2/3, 3/4, 4/5, 1/1} Some properties of the Farey Sequence: </p> <div class="itemizedlist"> <ul class="itemizedlist" type="disc"> <li class="listitem"> If a/b and c/d are two successive terms of F<sub>n</sub>, then bc - ad = 1. </li> <li class="listitem"> If a/b, c/d, e/f are three successive terms of F<sub>n</sub>, then: c/d = (a+e) / (b+f). In this case c/d is called the mediant fraction between a/b and e/f. </li> <li class="listitem"> If n > 1, then no two successive terms ofF<sub>n</sub> have the same denominator. </li> </ul> </div> <p> </p> <p> The length of any Farey Sequence F<sub>n</sub> is determined by |F<sub>n</sub>| = 1 + SUM over n (phi(m)) where phi(m) is Euler's totient function, which gives the number of integers ≤ m that are coprime to m. </p> <p> Some values for the length of F<sub>n</sub> given n: </p> <div class="informaltable"> <table border="1"> <colgroup> <col /> <col /> </colgroup> <thead> <tr> <th>n</th> <th>F<sub>n</sub></th> </tr> </thead> <tbody> <tr> <td>1</td> <td>2</td> </tr> <tr> <td>2</td> <td>3</td> </tr> <tr> <td>3</td> <td>5</td> </tr> <tr> <td>4</td> <td>7</td> </tr> <tr> <td>5</td> <td>11</td> </tr> <tr> <td>6</td> <td>13</td> </tr> <tr> <td>7</td> <td>19</td> </tr> <tr> <td>8</td> <td>23</td> </tr> <tr> <td>9</td> <td>29</td> </tr> <tr> <td>10</td> <td>33</td> </tr> <tr> <td>11</td> <td>43</td> </tr> <tr> <td>12</td> <td>47</td> </tr> <tr> <td>13</td> <td>59</td> </tr> <tr> <td>14</td> <td>65</td> </tr> <tr> <td>15</td> <td>73</td> </tr> <tr> <td>16</td> <td>81</td> </tr> <tr> <td>17</td> <td>97</td> </tr> <tr> <td>18</td> <td>103</td> </tr> <tr> <td>19</td> <td>121</td> </tr> <tr> <td>20</td> <td>129</td> </tr> </tbody> </table> </div> <p> </p> </div> <div class="refsect1" title="Syntax"> <a id="idp78693272"></a> <h2>Syntax</h2> <pre class="synopsis"><span class="command"><strong>f</strong></span> # time size "farey" fareynum mode</pre> </div> <div class="refsect1" title="Initialization"> <a id="idp78694256"></a> <h2>Initialization</h2> <p> <span class="emphasis"><em>size</em></span> -- number of points in the table. Must be a power of 2 or power-of-2 plus 1 (see <a class="link" href="f.html" title="f Statement (or Function Table Statement)"><em class="citetitle">f statement</em></a>). </p> <p> <span class="emphasis"><em>fareynum</em></span> -- the integer n for generating Farey Sequence F<sub>n</sub> </p> <p> <span class="emphasis"><em>mode</em></span> -- integer to trigger a specific output to be written into the table: </p> <div class="itemizedlist"> <ul class="itemizedlist" type="disc"> <li class="listitem"> <p>0 -- outputs floating point numbers representing the elements of F<sub>n</sub>.</p> </li> <li class="listitem"> <p> 1 -- outputs delta values of successive elements of F<sub>n</sub>, useful for generating note durations for example.</p> </li> <li class="listitem"> <p> 2 -- outputs only the denominators of the integer ratios, useful for indexing other tables or instruments for example.</p> </li> <li class="listitem"> <p> 3 -- same as mode 2 but with normalised output.</p> </li> <li class="listitem"> <p> 4 -- same as mode 0 but with 1 added to each number, useful for generating tables for tuning opcodes, for example <span class="emphasis"><em>cps2pch</em></span>. </p> </li> </ul> </div> <p> </p> </div> <div class="refsect1" title="Examples"> <a id="idp78700072"></a> <h2>Examples</h2> <p> </p> <div class="informalexample"><pre class="programlisting"> <span class="stamnt">f</span>1 0 -23 "farey" 8 0</pre> Generates generates Farey Sequence F<sub>8</sub>. The table contains all 23 elements of F<sub>8</sub> as floating point numbers. <pre class="programlisting"> <span class="stamnt">f</span>1 0 -18 "farey" 7 1</pre> This generates Farey Sequence F<sub>7</sub>. The table contains 18 delta values of F<sub>7</sub>, i.e. the difference between r<sub>i+1</sub> - r<sub>i</sub>, where r is the ith element of F<sub>n</sub>. <pre class="programlisting"> <span class="stamnt">f</span>1 0 -43 "farey" 11 2</pre> This generates Farey Sequence F<sub>11</sub>. The table contains the denominators of all 43 fractions in F<sub>11</sub>. <pre class="programlisting"> <span class="stamnt">f</span>1 0 -43 "farey" 11 3</pre> This generates Farey Sequence F<sub>11</sub>. The table contains the denominators of all 43 fractions in F<sub>11</sub>, each of those divided by 11, i.e. normalised. <pre class="programlisting"> <span class="stamnt">f</span>1 0 -18 "farey" 7 4</pre> This generates Farey Sequence F<sub>7</sub>. The table contains all fractions of F<sub>7</sub>, same as mode 0, but this time '1' is added to each table element. </div> <p> </p> <p> </p> <div class="example"> <a id="idp78707744"></a> <p class="title"> <strong>Example 1076. A simple example of the GENfarey routine.</strong> </p> <div class="example-contents"> <p> See the sections <a class="link" href="UsingRealTime.html" title="Real-Time Audio"><em class="citetitle">Real-time Audio</em></a> and <a class="link" href="CommandFlags.html" title="Csound command line"><em class="citetitle">Command Line Flags</em></a> for more information on using command line flags. </p> <pre class="programlisting"> <span class="csdtag"><CsoundSynthesizer></span> <span class="csdtag"><CsOptions></span> <span class="csdtag"></CsOptions></span> <span class="csdtag"><CsInstruments></span> <span class="ohdr">sr</span><span class="op">=</span>44100 <span class="ohdr">ksmps</span><span class="op">=</span>10 <span class="ohdr">nchnls</span><span class="op">=</span>1 <span class="oblock">instr</span> 4 kndx <span class="opc">init</span> 0 <span class="comment">; read out elements of F_8 one by one and print to file</span> <span class="octrl">if</span> (kndx <span class="op"><</span> 23) then kelem <span class="opc">tab</span> kndx, 1 <span class="opc">fprintks</span> "farey8table.txt", "<span class="op">%</span>2.6f\\n", kelem kndx <span class="op">=</span> kndx<span class="op">+</span>1 <span class="octrl">endif</span> <span class="oblock">endin</span> <span class="csdtag"></CsInstruments></span> <span class="csdtag"><CsScore></span> <span class="comment">; initialise integer for Farey Sequence F_8</span> <span class="stamnt">f</span>1 0 -23 "<span class="stamnt">f</span>arey" 8 0 <span class="comment">; if mode=0 then the table stores all elements of the Farey Sequence</span> <span class="comment">; as fractions in the range [0,1]</span> <span class="stamnt">i</span>4 0 1 <span class="stamnt">e</span> <span class="csdtag"></CsScore></span> <span class="csdtag"></CsoundSynthesizer></span> </pre> </div> </div> <p><br class="example-break" /> </p> <p> Here is a complete example of the GENfarey routine. It uses the files <a class="ulink" href="examples/genfarey-2.csd" target="_top"><em class="citetitle">genfarey-2.csd</em></a>. </p> <div class="example"> <a id="idp78710616"></a> <p class="title"> <strong>Example 1077. Another example of the GENfarey routine.</strong> </p> <div class="example-contents"> <p>See the sections <a class="link" href="UsingRealTime.html" title="Real-Time Audio"><em class="citetitle">Real-time Audio</em></a> and <a class="link" href="CommandFlags.html" title="Csound command line"><em class="citetitle">Command Line Flags</em></a> for more information on using command line flags.</p> <pre class="programlisting"> <span class="csdtag"><CsoundSynthesizer></span> <span class="csdtag"><CsOptions></span> <span class="comment">; Select audio/midi flags here according to platform</span> -odac <span class="comment">;;;realtime audio out</span> <span class="comment">;-iadc ;;;uncomment -iadc for RT audio input as well </span> <span class="comment">; For Non-realtime ouput leave only the line below:</span> <span class="comment">; -o genfarey.wav -W ;;; for file output any platform</span> <span class="csdtag"></CsOptions></span> <span class="csdtag"><CsInstruments></span> <span class="ohdr">sr</span> <span class="op">=</span> 44100 <span class="ohdr">ksmps</span> <span class="op">=</span> 32 <span class="ohdr">nchnls</span> <span class="op">=</span> 2 <span class="ohdr">0dbfs</span> <span class="op">=</span> 1 <span class="comment">; GENfarey creates table gidelta. </span> <span class="comment">; The table contains the delta values of Farey Sequence 7 (p5=7).</span> <span class="comment">; They are used as Inter Onset Intervals (IOIs) or event durations.</span> <span class="comment">; If p6 is set to 1 for IOI output then the length of the table (p3=-18) is -(|F_7| - 1)</span> <span class="comment">; Remember that a negative sign is for non-power-of-2 table lengths.</span> <span class="comment">; The negative sign in front of the GEN number prevents post-normalisation of its values.</span> gidelta <span class="ohdr">ftgen</span> 0,0,<span class="op">-</span>18,"farey",7,1 <span class="comment">; Use GENfarey with p6 set to 2 to generate the denominators of fractions of F_7 </span> <span class="comment">; this is used in this example as factors to create a series of pitches:</span> gimult <span class="ohdr">ftgen</span> 0,0,<span class="op">-</span>18,"farey",7,2 <span class="comment">;-------- loop and trigger instrument 901 using a Farey Sequence polyrhythm</span> <span class="oblock">instr</span> 1 kindx <span class="opc">init</span> 0 kindx2 <span class="opc">init</span> 0 ktrigger <span class="opc">init</span> 0 ktime_unit <span class="opc">init</span> p6 kstart <span class="opc">init</span> p4 kloop <span class="opc">init</span> p5 kinitndx <span class="opc">init</span> 0 kfn_times <span class="opc">init</span> gidelta knote <span class="opc">init</span> 60 kbasenote <span class="opc">init</span> p8 ifundam <span class="opc">init</span> p7 ktrigger <span class="opc">seqtime</span> ktime_unit, kstart, kloop, kinitndx, kfn_times <span class="octrl">if</span> (ktrigger <span class="op">></span> 0 ) then kpitch <span class="op">=</span> <span class="opc">cpspch</span>(ifundam) kmult <span class="opc">tab</span> kindx2, gimult kpitch <span class="op">=</span> kpitch <span class="op">*</span> kmult knote <span class="op">=</span> kbasenote <span class="op">+</span> kmult <span class="opc">event</span> "i", 901, 0, .4, .10, kpitch, kpitch <span class="op">*</span> .9, 0.4, 5, .75, .8, 1.0, .15, .0, .125, .125, .25, .5, 1.0, .0, .0, .0, .0, .125, .25, .25, .25, knote kindx <span class="op">=</span> kindx <span class="op">+</span> 1 kindx <span class="op">=</span> kindx <span class="op">%</span> kloop kindx2 <span class="op">=</span> kindx2 <span class="op">+</span> 1 kindx2 <span class="op">=</span> kindx2 <span class="op">%</span> kloop <span class="octrl">endif</span> <span class="oblock">endin</span> <span class="comment">;------ basic 2 Operators FM algorithm ----------------</span> <span class="oblock">instr</span> 901 inotedur <span class="op">=</span> p3 imaxamp <span class="op">=</span> p4 <span class="comment">;ampdb(p4)</span> icarrfreq <span class="op">=</span> p5 imodfreq <span class="op">=</span> p6 ilowndx <span class="op">=</span> p7 indxdiff <span class="op">=</span> p8<span class="op">-</span>p7 knote <span class="op">=</span> p27 aampenv <span class="opc">linseg</span> p9, p14<span class="op">*</span>p3, p10, p15<span class="op">*</span>p3, p11, p16<span class="op">*</span>p3, p12, p17<span class="op">*</span>p3, p13 adevenv <span class="opc">linseg</span> p18, p23<span class="op">*</span>p3, p19, p24<span class="op">*</span>p3, p20, p25<span class="op">*</span>p3, p21, p26<span class="op">*</span>p3, p22 amodosc <span class="opc">oscili</span> (ilowndx<span class="op">+</span>indxdiff<span class="op">*</span>adevenv)<span class="op">*</span>imodfreq, imodfreq, 10 acarosc <span class="opc">oscili</span> imaxamp<span class="op">*</span>aampenv, icarrfreq<span class="op">+</span>amodosc, 10 <span class="opc">outs</span> acarosc, acarosc <span class="oblock">endin</span> <span class="csdtag"></CsInstruments></span> <span class="csdtag"><CsScore></span> <span class="stamnt">f</span>10 0 4096 10 1 <span class="comment">;sine wave </span> <span class="comment">; p4 kstart := index offset into the Farey Sequence</span> <span class="comment">; p5 kloop := end index into Farey Seq.</span> <span class="comment">; p6 timefac := time in seconds for one loop to complete</span> <span class="comment">; p7 fundam := fundamental of the FM instrument</span> <span class="comment">; p8 basenote:= root pitch of the midi voice output</span> <span class="comment">; note that pitch structures of the midi file output are not equivalent to the</span> <span class="comment">; ones used for the FM real-time synthesis.</span> <span class="comment">; start dur kstart kloop timefac fundam. basenote</span> <span class="stamnt">i</span>1 0.0 44 0 18 2 6.05 60 <span class="stamnt">i</span>1 4 30 0 18 3 7.05 72 <span class="stamnt">i</span>1 34 12 9 18 3 7.05 72 <span class="stamnt">i</span>1 10 12 0 18 1.5 8 84 <span class="stamnt">i</span>1 22 12 0 9 1.5 8 84 <span class="stamnt">i</span>1 15 16 0 18 1 5 48 <span class="stamnt">i</span>1 22 20 5 17 1.7 4 36 <span class="stamnt">i</span>1 46 20 3 11 2.5 7.04 71 <span class="stamnt">i</span>1 51 20 5 13 2.5 7.06 72 <span class="stamnt">i</span>1 73.5 1.5 11 18 1.5 5.05 48 <span class="stamnt">i</span>1 75 1 12 18 1 6.03 58 <span class="stamnt">e</span> <span class="csdtag"></CsScore></span> <span class="csdtag"></CsoundSynthesizer></span> </pre> </div> </div> <p><br class="example-break" /> <span>These are the diagrams of the waveforms of the GENfarey routines, as used in the example:</span> </p> <div class="mediaobject"> <img src="images/genfarey-2_1.png" alt="gidelta ftgen 100,0,-18,"farey",7,1 - delta values of Farey Sequence 7" /> <div class="caption"> <p>gidelta ftgen 100,0,-18,"farey",7,1 - delta values of Farey Sequence 7</p> </div> </div> <p> </p> <div class="mediaobject"> <img src="images/genfarey-2_2.png" alt="gimult ftgen 101,0,-18,"farey",7,2 - generate the denominators of fractions of F_7" /> <div class="caption"> <p>gimult ftgen 101,0,-18,"farey",7,2 - generate the denominators of fractions of F_7 </p> </div> </div> <p> </p> </div> <div class="refsect1" title="Credits"> <a id="idp78716824"></a> <h2>Credits</h2> <p> </p> <table border="0" summary="Simple list" class="simplelist"> <tr> <td>Author: Georg Boenn</td> </tr> <tr> <td>University of Glamorgan</td> </tr> <tr> <td>2010</td> </tr> </table> <p> </p> <p>New in Csound version 5.13</p> </div> </div> <div class="navfooter"> <hr /> <table width="100%" summary="Navigation footer"> <tr> <td width="40%" align="left"><a accesskey="p" href="GENsone.html">Prev</a> </td> <td width="20%" align="center"> <a accesskey="u" href="ScoregensTop.html">Up</a> </td> <td width="40%" align="right"> <a accesskey="n" href="GENwave.html">Next</a></td> </tr> <tr> <td width="40%" align="left" valign="top">GENsone </td> <td width="20%" align="center"> <a accesskey="h" href="index.html">Home</a> </td> <td width="40%" align="right" valign="top"> GENwave</td> </tr> </table> </div> </body> </html>