## 5 Implementation
### Cosound Opcodes
#### 5.4.1 liveconv  
The liveconv opcode implements dynamic replacement of impulse responses (see Section 3). It employs partitioned convolution with the overlap-add (OLA) scheme. The opcode takes one input signal and a table for holding the impulse response (IR) data:  
ares liveconv ain, ift, iplen, kupdate, kclear,  
where its parameters are as follows:  
* ares: Output signal.
* ain: Input signal.
* ift: Table number for storing the impulse response (IR) for convolution. The table may be filled with new data at any time while the convolution is running.
* iplen: Length of impulse response partition in samples; must be an integer power of two. Lower settings allow for shorter output delay but will increase CPU usage.
* kupdate: Flag indicating whether the IR table should be updated. If kupdate = 1, the IR table lift is loaded partition by partition, starting with the next partition. If kupdate = -1, the IR table lift is unloaded (cleared to zero) partition by partition, starting with the next partition. Other values have no effect.
* kclear: Flag for clearing all internal buffers. If kclear has any value! = zero, the internal buffers are cleared immediately. This operation is not free of artefacts.  
The opcode makes a clear distinction between the input signal \(x_{1}(n)\) and the IR table. However, if the IR table is updated regularly at filter length: \(t_{Rn}=n*N\) and the IR table continuously filled with data from another audio stream \(x_{2}(n)\), then the inputs \(x_{1}(n)\) and \(x_{2}(n)\) will be treated on equal terms, and the opcode behave similar to tvconv without freezing.  
The opcode is programmed in C and is available as part of Cosound source code [44]. See Algorithm 1 below for pseudocode that highlights the most important parts. For more details, the reader is encouraged to inspect the source code at Github.  
```
Input :Audio input, IR table, partition size and update flag Output :Convolved audio output /* Check if the IR buffer should be updated */ ifupdate flag is setthen  Initialize process to load IR from table position 0 endforall samples in input audio bufferdo  Read sample into an internal ring buffer; ifOne complete partition is readthen /* This is where the stepwise loading of an IR is handled */ forall loading IR processesdo /* Read from IR table into internal IR buffer */ /* The internal buffers are accessed in reverse order */ forall samples in current IR table partitiondo  Read into first half of internal IR buffer;  end  Pad second half of internal IR buffer with zeros;  Compute FFT on internal IR buffer (replace in buffer); IR table position += partition length; ifIR table position \(\geq\) filter lengththen // The entire filter is loaded  Terminate this loading process;  end if  end for /* Start processing the audio input partition */ Pad second half of internal ring buffer with zeros;  Compute FFT on internal ring buffer (replace in buffer);  Update ring buffer position (wrap around if necessary); forall partitions of the filterdo  Pairwise multiply internal IR and ring buffers;  Accumulate sum in temporary output buffer;  end  Compute inverse FFT of temporary buffer; /* Overlap Add (OLA) current and previous output block */ for\(i=0\)to Partition length \(-1\)do  output[i] = first part of temporary[i] + saved output[i];  saved output[i] = second part of temporary[i];  end for  end for  end for
```  
**Algorithm 1**Liveconv opcode implementation. IR loading marked with blue color.