| 1 | |
| 2 | <refentry id="convolve"> |
| 3 | <indexterm id="IndexConvolve"><primary>convolve</primary></indexterm> |
| 4 | <refentryinfo><title>Signal Modifiers:Convolution and Morphing</title></refentryinfo> |
| 5 | <refmeta> |
| 6 | <refentrytitle>convolve</refentrytitle> |
| 7 | </refmeta> |
| 8 | |
| 9 | <refnamediv> |
| 10 | <refname>convolve</refname> |
| 11 | <refpurpose> |
| 12 | Convolves a signal and an impulse response. |
| 13 | </refpurpose> |
| 14 | </refnamediv> |
| 15 | |
| 16 | <refsect1> |
| 17 | <title>Description</title> |
| 18 | <para> |
| 19 | Output is the convolution of signal <emphasis>ain</emphasis> and the impulse response contained in <emphasis>ifilcod</emphasis>. If more than one output signal is supplied, each will be convolved with the same impulse response. Note that it is considerably more efficient to use one instance of the operator when processing a mono input to create stereo, or quad, outputs. |
| 20 | </para> |
| 21 | |
| 22 | <para> |
| 23 | Note: this opcode can also be written as <link linkend="convle"><citetitle>convle</citetitle></link>. |
| 24 | </para> |
| 25 | </refsect1> |
| 26 | |
| 27 | <refsect1> |
| 28 | <title>Syntax</title> |
| 29 | <synopsis>ar1 [, ar2] [, ar3] [, ar4] <command>convolve</command> ain, ifilcod [, ichannel]</synopsis> |
| 30 | </refsect1> |
| 31 | |
| 32 | <refsect1> |
| 33 | <title>Initialization</title> |
| 34 | <para> |
| 35 | <emphasis>ifilcod</emphasis> -- integer or character-string denoting an impulse response data file. An integer denotes the suffix of a file <emphasis>convolve.m</emphasis>; a character string (in double quotes) gives a filename, optionally a full pathname. If not a fullpath, the file is sought first in the current directory, then in the one given by the environment variable SADIR (if defined). The data file contains the Fourier transform of an impulse response. Memory usage depends on the size of the data file, which is read and held entirely in memory during computation, but which is shared by multiple calls. |
| 36 | </para> |
| 37 | |
| 38 | <para> |
| 39 | <emphasis>ichannel</emphasis> (optional) -- which channel to use from the impulse response data file. |
| 40 | </para> |
| 41 | </refsect1> |
| 42 | |
| 43 | <refsect1> |
| 44 | <title>Performance</title> |
| 45 | <para> |
| 46 | <emphasis>ain</emphasis> -- input audio signal. |
| 47 | </para> |
| 48 | |
| 49 | <para> |
| 50 | <emphasis>convolve</emphasis> implements Fast Convolution. The output of this operator is delayed with respect to the input. The following formulas should be used to calculate the delay: |
| 51 | <informalexample> |
| 52 | <programlisting> |
| 53 | For (1/kr) <= IRdur: |
| 54 | Delay = ceil(IRdur * kr) / kr |
| 55 | For (1/kr) > IRdur: |
| 56 | Delay = IRdur * ceil(1/(kr*IRdur)) |
| 57 | Where: |
| 58 | kr = Csound control rate |
| 59 | IRdur = duration, in seconds, of impulse response |
| 60 | ceil(n) = smallest integer not smaller than n</programlisting> |
| 61 | </informalexample> |
| 62 | </para> |
| 63 | |
| 64 | <para> |
| 65 | One should be careful to also take into account the initial delay, if any, of the impulse response. For example, if an impulse response is created from a recording, the soundfile may not have the initial delay included. Thus, one should either ensure that the soundfile has the correct amount of zero padding at the start, or, preferably, compensate for this delay in the orchestra (the latter method is more efficient). To compensate for the delay in the orchestra, subtract the initial delay from the result calculated using the above formula(s), when calculating the required delay to introduce into the 'dry' audio path. |
| 66 | </para> |
| 67 | |
| 68 | <para> |
| 69 | For typical applications, such as reverb, the delay will be in the order of 0.5 to 1.5 seconds, or even longer. This renders the current implementation unsuitable for real time applications. It could conceivably be used for real time filtering however, if the number of taps is small enough. |
| 70 | </para> |
| 71 | |
| 72 | <para> |
| 73 | The author intends to create a higher-level operator at some stage, that would mix the wet & dry signals, using the correct amount of delay automatically. |
| 74 | </para> |
| 75 | </refsect1> |
| 76 | |
| 77 | <refsect1> |
| 78 | <title>Examples</title> |
| 79 | <para> |
| 80 | Create frequency domain impulse response file using the <link linkend="cvanal"><citetitle>cvanal utility</citetitle></link>: |
| 81 | <screen> |
| 82 | csound -Ucvanal l1_44.wav l1_44.cv</screen> |
| 83 | </para> |
| 84 | |
| 85 | <para> |
| 86 | Determine duration of impulse response. For high accuracy, determine the number of sample frames in the impulse response soundfile, and then compute the duration with: |
| 87 | <literallayout> |
| 88 | duration = (sample frames)/(sample rate of soundfile)</literallayout> |
| 89 | </para> |
| 90 | |
| 91 | <para> |
| 92 | This is due to the fact that the <link linkend="sndinfo"><citetitle>sndinfo utility</citetitle></link> only reports the duration to the nearest 10ms. If you have a utility that reports the duration to the required accuracy, then you can simply use the reported value directly. |
| 93 | <screen> |
| 94 | sndinfo l1_44.wav</screen> |
| 95 | |
| 96 | <literallayout> |
| 97 | length = 60822 samples, sample rate = 44100 |
| 98 | |
| 99 | Duration = 60822/44100 = 1.379s.</literallayout> |
| 100 | </para> |
| 101 | |
| 102 | <para> |
| 103 | Determine initial delay, if any, of impulse response. If the |
| 104 | impulse response has not had the initial delay removed, then you |
| 105 | can skip this step. If it has been removed, then the only way |
| 106 | you will know the initial delay is if the information has been |
| 107 | provided separately. For this example, let us assume that the initial delay is 60ms (0.06s) |
| 108 | </para> |
| 109 | |
| 110 | <para> |
| 111 | Determine the required delay to apply to the dry signal, to align it with the convolved signal: |
| 112 | |
| 113 | <literallayout> |
| 114 | If kr = 441: |
| 115 | 1/kr = 0.0023, which is <= IRdur (1.379s), so: |
| 116 | Delay1 = ceil(IRdur * kr) / kr |
| 117 | = ceil(608.14) / 441 |
| 118 | = 609/441 |
| 119 | = 1.38s</literallayout> |
| 120 | |
| 121 | <literallayout> |
| 122 | Accounting for the initial delay: |
| 123 | Delay2 = 0.06s |
| 124 | Total delay = delay1 - delay2 |
| 125 | = 1.38 - 0.06 |
| 126 | = 1.32s</literallayout> |
| 127 | </para> |
| 128 | |
| 129 | <para> |
| 130 | Here is similar example of the convolve opcode. It uses the file <ulink url="examples/convolve.csd"><citetitle>convolve.csd</citetitle></ulink>. |
| 131 | |
| 132 | <example> |
| 133 | <title>Example of the convolve opcode.</title> |
| 134 | |
| 135 | |
| 136 | |
| 137 | <para>See the sections <link linkend="UsingRealTime"><citetitle>Real-time Audio</citetitle></link> and <link linkend="CommandFlags"><citetitle>Command Line Flags</citetitle></link> for more information on using command line flags.</para> |
| 138 | <xi:include href="examples-xml/convolve.csd.xml" xmlns:xi="http://www.w3.org/2001/XInclude"/> |
| 139 | </example> |
| 140 | </para> |
| 141 | </refsect1> |
| 142 | |
| 143 | <refsect1> |
| 144 | <title>See also</title> |
| 145 | <para><link linkend="SigProcConmorph"><citetitle>Convolution and Morphing</citetitle></link></para> |
| 146 | </refsect1> |
| 147 | |
| 148 | <refsect1> |
| 149 | <title>Credits</title> |
| 150 | <para>Author: Greg Sullivan</para> |
| 151 | <para>1996</para> |
| 152 | <para>New in version 3.28</para> |
| 153 | </refsect1> |
| 154 | </refentry> |