Skip to main content

torq.py

PyPI version Python versions License

Strongly timed concurrent Python on one logical clock — audio/synth, MIDI/DMX cues, deterministic sim ticks, and lab protocols share the same rational Time / Dur timeline.

now advances only when shreds yield a duration (or you advance / run / SimClock.step / frame). No wall-clock sleep for logical waits. Timing, cues, sim, and lab ship pure-Python; optional in-process audio builds locally from included sources.

Status: research / alpha (0.1.x). APIs may evolve. PyPI project torqpy; import torq; brand torq.py.

Install

pip install torqpy
import torq   # package name on PyPI is torqpy; import is torq

Editable checkout:

git clone https://github.com/valid22/torq.py.git
cd torq.py
pip install -e ".[dev]"
python3 -m unittest discover -s torq/tests

Optional native audio

Timing / shreds / cues / SimClock / lab need no native build. torq.audio needs the optional extension (sources ship in the wheel):

bash "$(python -c 'import torq, pathlib; print(pathlib.Path(torq.__file__).parent / "native" / "build_audio.sh")')"
# from a git checkout: bash torq/native/build_audio.sh
# needs: g++, python3-dev, portaudio19-dev (PortAudio for live I/O)

Quickstart

junnu’s four-hit metronome — deterministic, no wall sleep:

from torq import Clock, ms, spork

clock = Clock()
hits = []

def junnu_metro():
    for _ in range(4):
        hits.append(clock.now)
        yield 500 * ms

clock.spork(junnu_metro())
clock.run()
# hits at 0, 0.5s, 1s, 1.5s

Laddu’s polite meow on the same clock (after building _audio):

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)

Features by domain

Domain Entry points Highlights
Core clock / shreds Clock, Time, Dur, spork, yield / await (~d) Exact rational time; concurrent shreds in ordinary Python
Ergonomics with clock:, now, wait, ~d, t ^ d, Pat / p["x-x-"], @shred Context-local clock; patterns; unary + spork
Audio / synth torq.audio.AudioEngine In-process UGens + embedded STK (Clarinet…Saxofony, NRev, Chorus) on torq.py’s sample clock; offline WAV; optional PortAudio
MIDI / DMX cues torq.cues.CueSchedule Note/CC/DMX stamps; deterministic events(); stub frames / mido / composite live sinks
Sim ticks SimClock(dt) Fixed-timestep step / accumulator frame + update/render hooks; replay-stable
Lab protocols torq.lab.Protocol Stimuli, DAQ windows; StimulusDriver / DAQDevice stubs for TTL + DAQ

MIDI / DMX cues

from torq import Clock, ms, second
from torq.cues import CueSchedule

clock = Clock()
cues = CueSchedule(clock)

def junnu_phrase():
    cues.note_on(0, 60, 100)
    yield 250 * ms
    cues.note_off(0, 60)
    cues.cc(0, 1, 64)
    yield 250 * ms
    cues.dmx(1, {1: 255, 2: 128})
    yield second

clock.spork(junnu_phrase())
clock.run()
events = cues.events()  # deterministic offline list

Sim ticks

from torq import SimClock, second, spork, now

sim = SimClock(second / 60)
log = []

def junnu():
    while True:
        log.append(("junnu", sim.now))
        yield sim.dt

@sim.on_step
def laddu_hook():
    log.append(("Laddu", now(), sim.tick))

with sim:
    spork(junnu)
sim.step(3)

# Accumulator frame: fixed updates + render alpha
@sim.on_update
def physics():
    pass

@sim.on_render
def draw():
    blend = sim.alpha   # Fraction in [0, 1)

sim.frame(sim.dt / 2)

Lab protocol

from torq import Clock, ms
from torq.lab import Protocol, StubDAQ, StubDriver

proto = (
    Protocol("meow-trial")
    .mark("ready")
    .window("adc", duration=100 * ms, channels=("mic",), rate=1000, advance=False)
    .stimulus(
        "meow",
        duration=20 * ms,
        onset=25 * ms,
        payload={"who": "Laddu", "line": "ttl0"},
        advance=False,
    )
    .wait(100 * ms)
    .mark("done")
)

timeline = proto.plan()  # offline, no hardware
clock = Clock()
driver, daq = StubDriver(lines=("ttl0",)), StubDAQ(sample_rate=1000)
timeline, fired = proto.run(clock, driver=driver, daq=daq)

Documentation

Doc Contents
docs/api.md Public API surface
docs/strongly-timed-python.md Timing model & type rules
docs/torq-examples.md Copy-paste timing snippets
docs/torq-cues.md MIDI / DMX cue schedules
docs/torq-lab.md Stimuli, DAQ windows, protocols
docs/torq-audio.md UGens, stereo, live, STK
docs/torq-ugen-roadmap.md Audio coverage roadmap
docs/compare.md Related systems (peers, not the headline)
llms.txt Agent-oriented install & patterns

Contributing

Issues and PRs welcome at github.com/valid22/torq.py.

pip install -e ".[dev]"
python3 -m unittest discover -s torq/tests

Optional native VM clock (used by some tests): bash torq/native/build_vm.sh.

License

Apache-2.0. Vendored STK under torq/native/stk/ carries its own MIT-style license (Cook & Scavone).

Metadata

Release files for torqpy 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for torqpy 0.2.0
File Size Uploaded Provenance Actions
torqpy-0.2.0.tar.gz 118.8 kB With provenance Details for torqpy-0.2.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for torqpy 0.2.0
File Interpreter ABI Platform Actions
torqpy-0.2.0-py3-none-any.whl Python 3 none any Details for torqpy-0.2.0-py3-none-any.whl

Total release size: 242.9 kB

Release files / torqpy-0.2.0.tar.gz

Download URL torqpy-0.2.0.tar.gz
Size 118.8 kB
Tags Source
SHA-256 checksum
How to use checksums
ba73f784acbd19eea5fd46255016d6940848c4dae247a27dc071c392a149363f
BLAKE2b-256 checksum
How to use checksums
71755ce3a68a19536591abfdeb93f9eb90fc7253933556bfb80b14b90a83b9c9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Dec 2, 2026.

Transparency log

Release files / torqpy-0.2.0-py3-none-any.whl

Download URL torqpy-0.2.0-py3-none-any.whl
Size 124.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e3ade87b13e71f7def33329d8347f4ecb8a7c7bdeaf88f5430fbce4865976e6d
BLAKE2b-256 checksum
How to use checksums
18bb9b1a291f67eb0c0eda3d443acae71a55c4e8e5fc08d1dbd61894cec06998
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Dec 2, 2026.

Transparency log

Security for torqpy 0.2.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for torqpy-0.2.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Dec 2, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page