# Compare

> Turn any signal into a gate or trigger by comparing it to a threshold or to another signal, lane by lane, with hysteresis and tolerance.

Source: https://docs.tune.toys/nodes/std.compare/

Compare turns a signal into 1 or 0 by comparing A with B, lane by lane. It works on audio and on control lanes.

## What it does

Operation chooses the test: Greater, Less, Greater or equal, Less or equal, Equal or Not equal. B is the threshold: either the signal on the B input or, when B is not connected, B when unconnected on every lane.

The ordering tests use hysteresis so a signal hovering at the threshold does not flicker: Greater switches on when A rises above B and off only when A falls below B minus Hysteresis. Equal treats A and B as equal when they are within Tolerance of each other.

Mode chooses the output. Gate stays at 1 while the test is true. Trigger is 1 only on each rising edge: for one sample when the output is audio, for one block when it is control. Each lane keeps its own state, so every channel or lane switches independently.

Rate and width follow the cables the same way as in [Math](https://docs.tune.toys/nodes/std.math/): audio on A or B makes an audio output, otherwise the output is control.

## When to use it

Use it at control rate to turn a smooth control into an event: open an envelope when an LFO passes a level, step a [Counter](https://docs.tune.toys/nodes/ctl.counter/) or fire a [Sample and hold](https://docs.tune.toys/nodes/ctl.hold/). At audio rate it is a comparator: a crude square shaper, or a gate that shows when a signal goes over a level.

## Ports

<!-- ports:start -->

| Port  | Direction | Signal                               | Lanes                   |
| ----- | --------- | ------------------------------------ | ----------------------- |
| `a`   | Input     | Audio or control (follows the cable) | Inferred from the cable |
| `b`   | Input     | Audio or control (follows the cable) | Inferred from the cable |
| `out` | Output    | Audio or control (follows the cable) | Inferred from the cable |

<!-- ports:end -->

## Parameters

<!-- parameters:start -->

| Parameter    | Label              | Range   | Default | Unit | Scale  | Notes                                                                                                                                                                   |
| ------------ | ------------------ | ------- | ------- | ---- | ------ | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `op`         | Operation          | 0 to 5  | 0       |      | linear | How A is tested against B. The output is 1 while the test holds and 0 otherwise. Choices: 0 Greater, 1 Less, 2 Greater or equal, 3 Less or equal, 4 Equal, 5 Not equal. |
| `b_default`  | B when unconnected | -8 to 8 | 0.5     |      | linear | Threshold used for every lane of B when nothing is connected to it.                                                                                                     |
| `hysteresis` | Hysteresis         | 0 to 1  | 0.05    |      | linear | How far A must cross back past B before an ordering test switches off again, so noisy signals do not chatter. Shown when `op` is 0 or 1 or 2 or 3.                      |
| `epsilon`    | Tolerance          | 0 to 1  | 0.001   |      | linear | Largest difference between A and B that still counts as equal. Shown when `op` is 4 or 5.                                                                               |
| `mode`       | Mode               | 0 to 1  | 0       |      | linear | Gate stays at 1 while the test holds; Trigger is 1 for one block (control) or one sample (audio) on each rising edge. Choices: 0 Gate, 1 Trigger.                       |

<!-- parameters:end -->

### Operation (`op`)

The test, a discrete choice: Greater, Less, Greater or equal, Less or equal, Equal or Not equal.

### B when unconnected (`b_default`)

The threshold used on every lane when nothing is connected to B, from -8 to 8. Audio swings between -1 and 1 and control lanes usually between 0 and 1, so useful thresholds sit in those ranges. Changes are smoothed over 5 ms.

### Hysteresis (`hysteresis`)

For the ordering tests, how far A must move back past the threshold before the output switches off, from 0 to 1. Larger values ignore more noise but react later. Shown for Greater, Less, Greater or equal and Less or equal.

### Tolerance (`epsilon`)

For Equal and Not equal, how close A and B must be, from 0 to 1. A tolerance of 0 asks for exact equality, which continuous signals almost never reach.

### Mode (`mode`)

Gate or Trigger, a discrete choice. A trigger is short: one sample at audio rate, one block at control rate, so it needs a destination that reacts to rising edges.

## Tips

- To turn a threshold into a gate for an envelope, compare a control lane with Greater and connect the output to the envelope's Gate socket.
- Start with a Hysteresis of about 0.05 and raise it if the output chatters.
- A gate at audio rate is a 0 to 1 signal at full scale. Put a [Gain](https://docs.tune.toys/nodes/std.gain/) after it before you listen.
- Compare an audio signal with a threshold near 1 to make a gate that is on while the signal clips.

## See also

[Math](https://docs.tune.toys/nodes/std.math/), [Counter](https://docs.tune.toys/nodes/ctl.counter/), [Sample and hold](https://docs.tune.toys/nodes/ctl.hold/), [Envelope](https://docs.tune.toys/nodes/std.envelope/), [Control lanes](https://docs.tune.toys/concepts/control-lanes/).
