---
title: "CEL 上下文与动作"
description: "由运行时事实生成的 when 上下文字段、枚举、策略和 then 输出动作参考。"
---

> Documentation Index
> Fetch the complete documentation index at: https://treble.harekaze.win/llms.txt
> Use this file to discover all available pages before exploring further.

# CEL 上下文与动作

## 事实来源

本页表格来自 `docs-site/src/generated/cel-facts.json`。该 JSON 由 Rust 中与规则求值和编辑器补全相同的常量、字段目录及枚举映射导出，并由默认 `cargo test` 做 freshness 检查；不是 JavaScript 解析 Rust 源码得到的副本。

`source.sample_rate` 对 DSD 使用原始每声道 bit rate，不是 DoP carrier rate。`source.dop_rate` 为 DSD bit rate ÷ 16，PCM 为 `0`。

设备能力信息可能来自缓存或当前已知摘要。即使条件通过，实际打开设备时仍会严格验证目标格式；`device.dsd_capable` 不代表 DAC 锁定已经验证。

## when 上下文

### `when` 上下文

`when` 的求值结果必须是布尔值，并可读取下列字段。字段名固定；其值描述当前曲目、输出端点、输出模式、策略和播放设置。

#### `source`

| 字段 | 类型 | 描述 |
| --- | --- | --- |
| `source.sample_rate` | `int` | source rate in Hz (DSD: raw bit rate) |
| `source.bits_per_sample` | `int` | source effective depth (DSD: 1) |
| `source.channels` | `int` | source channel count |
| `source.is_dsd` | `bool` | source is DSD |
| `source.dop_rate` | `int` | DSD rate / 16 (PCM: 0) |

#### `device`

| 字段 | 类型 | 描述 |
| --- | --- | --- |
| `device.id` | `string` | stable endpoint ID |
| `device.dsd_capable` | `bool` | unverified capability assumption |
| `device.min_sample_rate` | `int` | known minimum PCM rate; 0 unknown |
| `device.max_sample_rate` | `int` | known maximum PCM rate; 0 unknown |
| `device.max_bits` | `int` | known maximum depth; 0 unknown |
| `device.channels` | `int` | endpoint channel summary |
| `device.exclusive` | `bool` | exclusive mode requested |

#### `output`

| 字段 | 类型 | 描述 |
| --- | --- | --- |
| `output.mode` | `string` | shared, exclusive, asio, pipewire, or alsa |

#### `policy`

| 字段 | 类型 | 描述 |
| --- | --- | --- |
| `policy.min_pcm_rate` | `int` | fixed PCM policy floor |

#### `playback`

| 字段 | 类型 | 描述 |
| --- | --- | --- |
| `playback.dsd_mode` | `string` | auto, dop, direct, or pcm |
| `playback.resample_quality` | `string` | configured resampler quality |

#### 上下文枚举

这些是上下文枚举字段当前的字符串值。可与 `'exclusive'` 等字符串字面量比较。

| 字段 | 可选值 |
| --- | --- |
| `output.mode` | `'shared'`, `'exclusive'`, `'asio'`, `'pipewire'`, `'alsa'` |
| `playback.dsd_mode` | `'auto'`, `'dop'`, `'direct'`, `'pcm'` |

#### 策略值

| 键 | 值 |
| --- | --- |
| `policy.min_pcm_rate` | `22050` |

## then 动作

`then` 必须是 map。只设置需要改变的字段，省略的字段继承默认行为。

### `then` 动作

`then` 必须返回 `map`。只设置需要改变的字段；省略的字段继承默认行为。

| 字段 | 类型 | 可选值 | 描述 |
| --- | --- | --- | --- |
| `mode` | `string` | `'pcm'`, `'dop'`, `'passthrough'` | Target transport mode |
| `rate` | `int` | — | PCM target rate in Hz; named rate constants such as CD and DVD also work |
| `bits` | `int` | `16`, `24`, `32` | Target bit depth |
| `channels` | `int` | `1`, `2`, `6` | Target channel count |
| `quality` | `string` | `'Quick'`, `'Low'`, `'Medium'`, `'Generic'`, `'High'`, `'Extreme'`, `'VeryHigh'`, `'Ultra'` | Resampler quality |

仅接受上表列出的字段。非 `map` 的 `then` 值或未知动作键会在规则校验时被拒绝。

#### 重采样质量可选值

`quality` 接受以下值之一：

`'Quick'`, `'Low'`, `'Medium'`, `'Generic'`, `'High'`, `'Extreme'`, `'VeryHigh'`, `'Ultra'`

显式 `mode:'dop'` 不能同时请求 PCM 的 `rate`、`bits` 或声道变更。DoP 需要 DSD 源；设备是否接受 DoP 仍会在实际打开输出时验证。

```text
{}
{'mode':'pcm'}
{'mode':'pcm','rate':88200,'bits':24,'channels':2}
{'mode':'dop'}
{'mode':'passthrough'}
```

规则求值顺序和失败语义见[输出规则入门](https://treble.harekaze.win/guides/output-rules/)。

Source: https://treble.harekaze.win/reference/cel-context/index.mdx
