> ## Documentation Index
> Fetch the complete documentation index at: https://torqpy.itsdote.tech/llms.txt
> Use this file to discover all available pages before exploring further.

# Audio

> AudioEngine, UGens, STK instruments/effects, offline render, and live PortAudio.

Requires built `torq._audio` (PyPI binary wheel, or `TORQ_NATIVE=1 pip install 'torqpy[native]'`).

```python theme={null}
from torq.audio import AudioEngine, write_wav, has_output_device
```

## `AudioEngine`

`AudioEngine(sample_rate=48000, channels=1|2)`

| Member | Meaning |
| - | - |
| `.dac`, `.blackhole` | `AudioSink` terminals (`sink << source` ≡ `source >> sink`) |
| `.sample_rate`, `.channels`, `.now_samples` | Engine state |
| Factories | `SinOsc`, `TriOsc`, `SawOsc`, `SqrOsc`, `Noise`, `Gain`, `ADSR`, `LPF`, `HPF`, `Delay`, `Pan`, `Clarinet`, `BlowBotl`, `Plucked`, `Brass`, `Saxofony`, `Rhodey`, `BeeThree`, `ModalBar`, `NRev`, `JCRev`, `PRCRev`, `Chorus` |
| `.connect(src, dst)` | Same as `src >> dst` |
| `.render(n)` / `.render_dur(dur)` | Offline buffers — mono `(N,)` or stereo `(N, 2)` |
| `.render_while(clock, until=Time)` | Interleave shreds + offline render |
| `.play_while(clock, until=Time)` | Same with PortAudio stream |
| `.start()` / `.stop()` / `.play(n)` | Live device I/O |
| `.has_output_device()` | Default output present? |

## UGen conventions

* Connect: `a >> b >> engine.dac` or `a.to(b)`; fan-out `a.send(b, c)`; bind `a @ engine` / `a.on(clock)`
* Audio sugar: `node * g` inserts `Gain(g)` (AudioNode × number only; `Dur * n` unchanged)
* `dac << src` / `blackhole << src` reverse-patch into an `AudioSink`
* Oscillators / filters / STK: `.freq`
* `SqrOsc.width`; `Gain.gain`; `Pan.pan`
* `ADSR.keyOn` / `keyOff` (and snake\_case aliases)
* `Delay.delay`, `.mix`, `.feedback`
* STK instruments: `noteOn(amp=0.5)` / `noteOff(amp=0.5)`; `controlChange(number, value)` / `control_change` (`value` in `[0, 128]`)
* `NRev.t60`, `.mix`; `JCRev.t60`, `.mix`; `PRCRev.t60`, `.mix`; `Chorus.depth`, `.frequency`, `.mix`

Sources (oscs, Noise, STK instruments) do not accept audio inputs. STK effects (`NRev`, `JCRev`, `PRCRev`, `Chorus`) do. `AudioNode` is an alias of `UGen`.

Torque owns the audio clock: STK is embedded in the native graph. Do not use scsynth/pyo as an audio server.

## Helpers

| Function | Meaning |
| - | - |
| `write_wav(path, samples, sample_rate=48000)` | Mono or stereo float → 16-bit PCM WAV |
| `has_output_device()` | Process-wide PortAudio default output check |

## Example

```python theme={null}
from torq import Clock, Time, ms, spork
from torq.audio import AudioEngine, write_wav

engine = AudioEngine(48_000)
osc = engine.SinOsc(440)
env = engine.ADSR(0.01, 0.1, 0.3, 0.2)
osc >> env >> engine.Gain(0.35) >> engine.dac

clock = Clock(sample_rate=48_000)

def laddu_meow():
    env.keyOn()
    yield 300 * ms
    env.keyOff()
    yield 300 * ms

with clock:
    spork(laddu_meow)
buf = engine.render_while(clock, until=Time(0) + 600 * ms)
write_wav("beep.wav", buf, engine.sample_rate)
```

> [!NOTE]
>
> Headless VMs often have no output device — check `has_output_device()` before `play_while` / live `start`.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.