| 1 | |
| 2 | <chapter id="StringsTop"> |
| 3 | <title>Strings</title> |
| 4 | <para> |
| 5 | String variables are variables with a name starting with S or gS (for a local or global string variable, respectively). These variables can be used as input argument to any opcode that expects a quoted string constant, and can be manipulated at initialization or performance time with the opcodes listed below. |
| 6 | </para> |
| 7 | <para> |
| 8 | It is also possible to use string p-fields. The string p-field can be used by many orchestra opcodes |
| 9 | directly, or it can be copied to a string variable first: |
| 10 | |
| 11 | <programlisting> |
| 12 | a1 <emphasis role="opc">diskin2</emphasis> p5, 1</programlisting> |
| 13 | |
| 14 | <programlisting> |
| 15 | Sname <emphasis role="opc">strget</emphasis> p5 |
| 16 | a1 <emphasis role="opc">diskin2</emphasis> Sname, 1</programlisting> |
| 17 | |
| 18 | </para> |
| 19 | <para> |
| 20 | Strings within Csound can be expressed using traditional double |
| 21 | quotes (" "), and also using {{ }}. The second method is useful |
| 22 | to allow ';' and '$' characters within the string without having |
| 23 | to used ASCII codes. |
| 24 | </para> |
| 25 | <note> |
| 26 | <title>Note</title> |
| 27 | <para>String variables and related opcodes are not available in Csound versions older than 5.00.</para> |
| 28 | </note> |
| 29 | <para> |
| 30 | Strings can also be linked to a number using <link linkend="strset"><citetitle>strset</citetitle></link> and <link linkend="strget"><citetitle>strget</citetitle></link>. |
| 31 | </para> |
| 32 | <para> |
| 33 | Csound 5 also has improvements in parsing string constants. It is possible |
| 34 | to specify a multi-line string by enclosing it within {{ and }} instead of |
| 35 | the usual double quote characters, and the following escape sequences are |
| 36 | automatically converted: |
| 37 | <itemizedlist> |
| 38 | <listitem> |
| 39 | <para><emphasis>\a</emphasis> alert bell</para> |
| 40 | </listitem> |
| 41 | <listitem> |
| 42 | <para><emphasis>\b</emphasis> backspace</para> |
| 43 | </listitem> |
| 44 | <listitem> |
| 45 | <para><emphasis>\n</emphasis> new line</para> |
| 46 | </listitem> |
| 47 | <listitem> |
| 48 | <para><emphasis>\r</emphasis> carriage return</para> |
| 49 | </listitem> |
| 50 | <listitem> |
| 51 | <para><emphasis>\t</emphasis> tab</para> |
| 52 | </listitem> |
| 53 | <listitem> |
| 54 | <para><emphasis>\\</emphasis> a single '\' character</para> |
| 55 | </listitem> |
| 56 | <listitem> |
| 57 | <para><emphasis>\nnn</emphasis> the character of which the ASCII code (in octal) is nnn</para> |
| 58 | </listitem> |
| 59 | </itemizedlist> |
| 60 | <note> |
| 61 | <title>Note</title> |
| 62 | <para> |
| 63 | If the user does not want an escaped sequence to be automatically converted, she has to escape it |
| 64 | with an additional '\' character so that Csound knows it does not have to interpret the escaped sequence. |
| 65 | For example the string <code>"Not escaped\nline return"</code> will be converted to |
| 66 | <programlisting> |
| 67 | "Not escaped |
| 68 | line return" |
| 69 | </programlisting> |
| 70 | before being used, while the string <code>"Escaped\\nline return"</code> will be converted to |
| 71 | <programlisting> |
| 72 | "Escaped\nline return" |
| 73 | </programlisting> |
| 74 | before being used. |
| 75 | </para> |
| 76 | </note> |
| 77 | |
| 78 | It can be useful together with the <link linkend="system"><citetitle>system</citetitle></link> opcode: |
| 79 | <programlisting> |
| 80 | <emphasis role="oblock">instr</emphasis> 1 |
| 81 | <emphasis role="comment">; csound5 lets you make a string with line returns inside double brackets</emphasis> |
| 82 | <emphasis role="opc">system</emphasis> {{ ps |
| 83 | date |
| 84 | cd ~/Desktop |
| 85 | pwd |
| 86 | ls -l |
| 87 | whois csounds.com |
| 88 | }} |
| 89 | <emphasis role="oblock">endin</emphasis></programlisting> |
| 90 | And the <link linkend="py"><citetitle>python opcodes</citetitle></link>, among others: |
| 91 | <programlisting> |
| 92 | <emphasis role="opc">pyruni</emphasis> {{ |
| 93 | import random |
| 94 | |
| 95 | pool = [(1 + i/10.0) ** 1.2 for i in range(100)] |
| 96 | |
| 97 | def get_number_from_pool(n, p): |
| 98 | if random.random() < p: |
| 99 | i = int(random.random() * len(pool)) |
| 100 | pool[i] = n |
| 101 | return random.choice(pool) |
| 102 | }}</programlisting> |
| 103 | </para> |
| 104 | &stringsmanipulate; |
| 105 | &stringsconvert; |
| 106 | </chapter> |