# Zero-Crossing Instrument

**URL:** <https://forum.csound.com/t/zero-crossing-instrument/240>\
**Category:** General\
**Created:** [April 7, 2021, 6:51pm UTC](https://forum.csound.com/t/zero-crossing-instrument/240 "2021-04-07T18:51:52Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![mark\_ferguson](https://forum.csound.com/user_avatar/forum.csound.com/mark_ferguson/32/479_2.png) [@mark\_ferguson](https://forum.csound.com/u/mark_ferguson)\
**Post date:** [April 7, 2021, 6:51pm UTC](https://forum.csound.com/t/zero-crossing-instrument/240/1 "2021-04-07T18:51:52Z")

</div>

Hi all,

I’m trying to think of a design for a kind of zero-crossing instrument, which detects when a recorded a-rate signal crosses its horizontal axis or 0 point. Each time that happens, I need an oscillator to switch value from 1 to 0 (or 0 to 1, if already at 1).

To achieve this, I was thinking of using the rms opcode to track input signal amplitude, and have some sort of conditional block with a built-in flagging system to get the output signal to switch when the rms value is at or close to 0; however, I would theoretically need to track at a-rate, and that doesn’t seem possible with rms.

Another design idea was to write the signal to an f-table and ‘find’ the 0 (or close to 0) values through table lookup, then apply those results to my output signal (i.e. get it to switch if a 0 or close to 0 value is detected).

Has anyone attempted this before, and if so, any advice or suggestions? Perhaps I’m missing the barn door in front of me, and there’s a brutally simple way of doing it…

Any help greatly appreciated.

Mark F.

---

<div class="post-metadata">

**Author:** ![rory](https://forum.csound.com/user_avatar/forum.csound.com/rory/32/7_2.png) [@rory](https://forum.csound.com/u/rory)\
**Post date:** [April 8, 2021, 10:57am UTC](https://forum.csound.com/t/zero-crossing-instrument/240/2 "2021-04-08T10:57:45Z")

</div>

> [@mark\_ferguson](#):
>
> I’m trying to think of a design for a kind of zero-crossing instrument, which detects when a recorded a-rate signal crosses its horizontal axis or 0 point. Each time that happens, I need an oscillator to switch value from 1 to 0 (or 0 to 1, if already at 1).

Hi Mark. Can you not write a UDO with `setksmps 1` that will check each sample, and output a 0/1 each time a sample values changes from plus to minus? Or am i over simplifying this?

> [@mark\_ferguson](#):
>
> Has anyone attempted this before

I’m sure it’s been attempted before, but not by me I’m afraid!

[some time later…]

```
opcode ZeroCross, k,a
    setksmps 1
    aSig xin
    kSwitch init 0
    kSampleValue = aSig

    if kSampleValue > 0 then
        kSwitch = 1
    else
        kSwitch = 0
    endif

    xout kSwitch
endop

instr 1
    a1, a2 diskin2 "DutchLadyTalking.aif", 1, 0, 1

    kSwitch ZeroCross a1

    if changed(kSwitch) == 1 then
        //it just crossed zero....
    endif
endin

```

---

<div class="post-metadata">

**Author:** ![mark\_ferguson](https://forum.csound.com/user_avatar/forum.csound.com/mark_ferguson/32/479_2.png) [@mark\_ferguson](https://forum.csound.com/u/mark_ferguson)\
**Post date:** [April 8, 2021, 12:02pm UTC](https://forum.csound.com/t/zero-crossing-instrument/240/3 "2021-04-08T12:02:17Z")

</div>

Hi Rory,

This is the barn door I mentioned, sitting right in front of me. Sure enough, there’s a simpler way.

Big, big thanks for your help on this; I’ll have a run through the code later on.

Kindest regards,  
Mark.

---

<div class="post-metadata">

**Author:** ![rory](https://forum.csound.com/user_avatar/forum.csound.com/rory/32/7_2.png) [@rory](https://forum.csound.com/u/rory)\
**Post date:** [April 8, 2021, 12:03pm UTC](https://forum.csound.com/t/zero-crossing-instrument/240/4 "2021-04-08T12:03:10Z")

</div>

I was getting worried I missed the barn door myself 😆

---

<div class="post-metadata">

**Author:** ![mark\_ferguson](https://forum.csound.com/user_avatar/forum.csound.com/mark_ferguson/32/479_2.png) [@mark\_ferguson](https://forum.csound.com/u/mark_ferguson)\
**Post date:** [April 8, 2021, 12:06pm UTC](https://forum.csound.com/t/zero-crossing-instrument/240/5 "2021-04-08T12:06:55Z")

</div>

Nope, that’s perfect! I tend to over-complicate these things, so just wanted to check with the community. Nice work with the forum, btw; looks great!

---

<div class="post-metadata">

**Author:** ![tjingboem](https://forum.csound.com/user_avatar/forum.csound.com/tjingboem/32/44_2.png) [@tjingboem](https://forum.csound.com/u/tjingboem)\
**Post date:** [April 8, 2021, 12:16pm UTC](https://forum.csound.com/t/zero-crossing-instrument/240/6 "2021-04-08T12:16:24Z")

</div>

i hear dutch ladies talking all day…

---

<div class="post-metadata">

**Author:** ![joachim](https://forum.csound.com/letter_avatar_proxy/v4/letter/j/a87d85/32.png) [@joachim](https://forum.csound.com/u/joachim)\
**Post date:** [April 8, 2021, 12:30pm UTC](https://forum.csound.com/t/zero-crossing-instrument/240/7 "2021-04-08T12:30:06Z")

</div>

perhaps in your udo you just have to wrap the trigger opcode with kmode=2.

---

<div class="post-metadata">

**Author:** ![mark\_ferguson](https://forum.csound.com/user_avatar/forum.csound.com/mark_ferguson/32/479_2.png) [@mark\_ferguson](https://forum.csound.com/u/mark_ferguson)\
**Post date:** [April 8, 2021, 1:40pm UTC](https://forum.csound.com/t/zero-crossing-instrument/240/8 "2021-04-08T13:40:36Z")

</div>

Thanks, Joachim: hadn’t thought of using the trigger opcode.

---

<div class="post-metadata">

**Author:** ![TetsuyaMiwa](https://forum.csound.com/user_avatar/forum.csound.com/tetsuyamiwa/32/181_2.png) [@TetsuyaMiwa](https://forum.csound.com/u/TetsuyaMiwa)\
**Post date:** [April 8, 2021, 2:12pm UTC](https://forum.csound.com/t/zero-crossing-instrument/240/9 "2021-04-08T14:12:23Z")

</div>

Hi Mark,

I’m also interested in zero-crossing because I plan to detect external trigger. I have been waiting for Expert Sleepers ES-9 over months!  
You might be interested in schmitt trigger for stable zero-crossing detection. It is available as sound-plugin.  
[https://csound-plugins.github.io/csound-plugins/opcodes/schmitt.html](https://csound-plugins.github.io/csound-plugins/opcodes/schmitt.html)

---

<div class="post-metadata">

**Author:** ![mark\_ferguson](https://forum.csound.com/user_avatar/forum.csound.com/mark_ferguson/32/479_2.png) [@mark\_ferguson](https://forum.csound.com/u/mark_ferguson)\
**Post date:** [April 8, 2021, 2:16pm UTC](https://forum.csound.com/t/zero-crossing-instrument/240/10 "2021-04-08T14:16:06Z")

</div>

That’s also very helpful, Tetsuya, and greatly appreciated!

---

<div class="post-metadata">

**Author:** ![rory](https://forum.csound.com/user_avatar/forum.csound.com/rory/32/7_2.png) [@rory](https://forum.csound.com/u/rory)\
**Post date:** [April 8, 2021, 6:20pm UTC](https://forum.csound.com/t/zero-crossing-instrument/240/11 "2021-04-08T18:20:24Z")

</div>

> [@tjingboem](#):
>
> i hear dutch ladies talking all day…

😆 I’ve been using that sample for nearly 20 years. She’s been with me a long time!

> [@mark\_ferguson](#):
>
> Thanks, Joachim: hadn’t thought of using the trigger opcode.

Yes, I hadn’t either. There are so many utility opcodes that I just never remember 🙈
