I recently ran into a situation in Csound in which I needed to know if a signal was positive or negative, and it had to work at the audio rate. I could have used if-else conditional statements, though these only work at k-rate, and would have required implementing a User-Defined Opcode utilizing the setksmps opcode. Perhaps I was lazy at the time, or in the mood for some simple hacking fun, but I decided to forgo this obvious solution for something else. And I wanted to do it without using conditionals.
The first thing I tried was using the > (greater than) opcode. In some other languages, an expression such as
(0.8 > 0) will return a value of 1, while a
(-0.8 > 0) will yield a 0. Unfortunately, this opcode is not designed this way in Csound. So I kept looking.
I eventually came up with a two opcode solution utilizing limit and ceil(). First, the signal passes through the limiter, clipping the waveform between the value range of 0.0 and 1.0. As the signal continues through the ceil opcode, any non-zero value becomes a 1.0, while a zero value remains 0.0.
For example, processing a sine wave through this configuration creates a unipolar square wave.
Now what? That’ll have to wait for another post.