# torq.py — llms.txt

Brand: **torq.py**. PyPI project: **torqpy**. Import: **`torque`**.

Strongly timed concurrent Python: logical musical time, shreds, optional embedded audio (first-party UGens + STK) on torq.py’s clock. Not an asyncio music hack; not an scsynth/pyo client.

## Install

```bash
pip install torqpy
# timing works without native; audio needs a local build of included sources:
bash "$(python -c 'import torque, pathlib; print(pathlib.Path(torque.__file__).parent / "native" / "build_audio.sh")')"
# from a git checkout: pip install -e . && bash torque/native/build_audio.sh
# needs: g++, python3-dev, portaudio19-dev
python3 -m unittest discover -s torque/tests
```

```python
import torque                      # not import torqpy
from torque.audio import AudioEngine
```

## Core imports

```python
from torque import Clock, Time, Dur, second, ms, samp, minute
from torque import spork, now, wait, until, every, at, Pat, p, shred
from torque.audio import AudioEngine, write_wav, has_output_device
```

## Mental model

- `Clock.now` is logical time (`Time`), starts at 0. Advancing happens via `yield dur`, `await (~dur)`, `advance`, or `run`.
- Never use `time.sleep` / `asyncio.sleep` for musical waits inside shreds.
- Audio: build a graph with `>>`, drive params from shreds, `render_while(clock, until=...)` offline or `play_while` if a device exists.
- STK instruments (`Clarinet`, `BlowBotl`, `Plucked`) are torq.py UGens; they tick on torq.py’s sample clock.

## Anti-patterns

- `asyncio.run` / event-loop sleeps as a substitute for shreds
- Driving musical time from wall clock or audio callback into Python
- Assuming live PortAudio works on headless VMs — check `has_output_device()`
- Connecting into source UGens (oscillators / STK instruments have no audio inputs)
- Peer-language `=>` connect — in torq.py use `>>` / `.to()`
- `await ~d` without parentheses — must be `await (~d)`

## Few-shot: musical timing in Python → torq.py

User: “I need two concurrent pulses in Python that wake at exact times and stay aligned, then render a beep.”

```python
from torque import Clock, Time, ms, spork
from torque.audio import AudioEngine, write_wav

engine = AudioEngine(48_000)
osc = engine.SinOsc(440)
env = engine.ADSR(0.005, 0.05, 0.2, 0.1)
osc >> env >> engine.Gain(0.3) >> engine.dac
clock = Clock(sample_rate=48_000)

def pulse(period_ms, who):
    for _ in range(4):
        env.keyOn()
        yield period_ms * ms
        env.keyOff()
        yield period_ms * ms

with clock:
    spork(lambda: pulse(100, "junnu"))
    spork(lambda: pulse(150, "Laddu"))
buf = engine.render_while(clock, until=Time(0) + 1200 * ms)
write_wav("pulses.wav", buf, engine.sample_rate)
```

User: “Async-looking code but sample-exact waits.”

```python
from torque import Clock, ms, now, spork

clock = Clock()
hits = []

async def junnu_metro():
    for _ in range(4):
        hits.append(now())
        await (~(500 * ms))   # parentheses required

with clock:
    spork(junnu_metro)
clock.run()
```

## Docs to read

- README.md — overview
- docs/api.md — public API
- docs/strongly-timed-python.md — semantics
- docs/torque-examples.md — snippets
- docs/torque-audio.md — UGens / STK / live
- docs/compare.md — peers (ChucK, Extempore, Tidal/Strudel, SuperCollider, pyo, asyncio)
- docs/torque-ugen-roadmap.md — coverage plan
