Skip to content

Oscillator formulas 2026 10 27

odashi edited this page Nov 16, 2026 · 1 revision

Oscillatorの条件分岐を計算式へ置き換える(2026-10-27)

過去の調査・実験ログです。本文の版・条件・結果は記録当時のものです。分類索引 · 技術資料

PR #30。比較元はf63c9195167514eb8a74decefee7020fec5b2c48、変更後はff183ea5f3002a71c81133aa1b3f66ed0f0bea42。

変更

Oscillator本体のifは11箇所から5箇所へ減少。本体23行追加・36行削除、回帰テスト22行追加。

  • 正弦波: 象限番号q=floor(4p)、象限内位置f=4p-qから、四半周期テーブルの位置をabs((q & 1)-f)、符号を1-(q & 2)で求める。象限番号は0〜3に合わせてu32とする。
  • 三角波: 4 * abs(abs(p-0.25)-0.5) - 1。周期境界での小数部抽出を絶対値の折り返しへ変更する。
  • 矩形波: -copysign(1, p-0.5)。位相0.5の+0も後半周期の−1にする。
  • 発音数: 1 + (N-1) * [detune != 0](角括弧内は条件成立時1、それ以外0)。
  • デチューン: d=N-1としてdetune * (2k/max(d,1) - min(d,1))。N=1でも同じ式から0 centを得る。
  • 発音中を確認した呼び出し元の後にあった、同じ状態判定を除去する。

測定条件

Linux x86_64、Intel Core i9-14900K、CPU2固定(同時に複数のベンチマークを動かさない)。governorはpowersave、周波数は固定しない。Rust 1.99.1 / LLVM 23.1.1。48,000 Hzと96,000 Hz。

既存のbenchmarks/dsp.rsを両版で同じ条件でビルド・実行する。4096回ウォームアップ後、1バッチ20ms以上となる反復数を選び、7バッチの最小・中央値・最大を収集する。前→後、後→前、前→後の3ラウンドを逐次実行し、下表は各ラウンドの中央値の中央値。短縮率は(1 - 後/前) × 100で、負数は処理時間の増加を表す。小さな差には測定揺れ・周波数変動も含まれる。

rustc --edition=2024 -D warnings -C opt-level=3 -C panic=abort -C codegen-units=16 benchmarks/dsp.rs -o target/bench-dsp
taskset -c 2 target/bench-dsp 48000 oscillator/ --scoped-ftz
taskset -c 2 target/bench-dsp 48000 synth/p32/u7/ --scoped-ftz
# 同じ測定を96000 Hzでも実施する。

--scoped-ftzにより256操作ごとにFTZ/DAZを設定・復元する。Oscillatorは440 Hz、既定AHDSR。u1/d0・u4/d30・u7/d30・u7/d0を全4波形で測定する。連続Detune変更は毎サンプル5〜79.925 centを0.075 cent刻みで更新する。シンセ全体は32音×7ユニゾン、LP2。render/unchanged/automationはSaw、sine/detuneはSine。automationではQを毎サンプル変更する。これらはDSPの処理時間であり、DAWのオーディオコールバック全体とは測定範囲が異なる。

結果

7ユニゾンの正弦波は7.26〜10.23%、三角波は19.93〜20.49%短縮した。32音×7ユニゾンの正弦波は9.02〜13.16%短縮した。一方、SawでQを毎サンプル変更するシンセ全体の条件では0.81〜1.30%増加し、release完了後は0.48〜0.54%増加した。分岐削減が全ケースの高速化になるわけではない。

48,000 Hz

ケース 変更前 ns/sample 変更後 ns/sample 短縮率
oscillator/Sine/u1/d0 22.163 21.498 +3.00%
oscillator/Sine/u4/d30 35.395 33.188 +6.24%
oscillator/Sine/u7/d30 49.548 44.477 +10.23%
oscillator/Sine/u7/d0 22.157 21.522 +2.87%
oscillator/Triangle/u1/d0 20.299 19.281 +5.02%
oscillator/Triangle/u4/d30 26.305 22.309 +15.19%
oscillator/Triangle/u7/d30 31.884 25.350 +20.49%
oscillator/Triangle/u7/d0 20.278 19.280 +4.92%
oscillator/Square/u1/d0 19.837 19.307 +2.67%
oscillator/Square/u4/d30 22.287 22.178 +0.49%
oscillator/Square/u7/d30 25.227 24.859 +1.46%
oscillator/Square/u7/d0 19.833 19.296 +2.71%
oscillator/Sawtooth/u1/d0 19.454 19.390 +0.33%
oscillator/Sawtooth/u4/d30 22.158 21.982 +0.79%
oscillator/Sawtooth/u7/d30 24.985 24.761 +0.90%
oscillator/Sawtooth/u7/d0 19.443 19.400 +0.22%
oscillator/Sine/u7/detune-changing 70.972 67.559 +4.81%
oscillator/idle/u7 7.569 7.572 -0.04%
synth/p32/u7/render 1480.617 1478.792 +0.12%
synth/p32/u7/unchanged 1489.528 1488.336 +0.08%
synth/p32/u7/automation 2067.149 2094.029 -1.30%
synth/p32/u7/idle 490.631 490.670 -0.01%
synth/p32/u7/released 548.681 551.302 -0.48%
synth/p32/u7/sine 2411.071 2093.714 +13.16%
synth/p32/u7/detune 3189.641 2990.681 +6.24%

96,000 Hz

ケース 変更前 ns/sample 変更後 ns/sample 短縮率
oscillator/Sine/u1/d0 22.030 21.573 +2.07%
oscillator/Sine/u4/d30 34.870 33.134 +4.98%
oscillator/Sine/u7/d30 47.959 44.478 +7.26%
oscillator/Sine/u7/d0 22.024 21.562 +2.10%
oscillator/Triangle/u1/d0 20.292 19.289 +4.94%
oscillator/Triangle/u4/d30 26.381 22.311 +15.43%
oscillator/Triangle/u7/d30 31.848 25.500 +19.93%
oscillator/Triangle/u7/d0 20.296 19.298 +4.92%
oscillator/Square/u1/d0 19.828 19.265 +2.84%
oscillator/Square/u4/d30 22.341 22.172 +0.76%
oscillator/Square/u7/d30 25.205 24.986 +0.87%
oscillator/Square/u7/d0 19.827 19.263 +2.84%
oscillator/Sawtooth/u1/d0 19.460 19.370 +0.46%
oscillator/Sawtooth/u4/d30 22.098 22.051 +0.21%
oscillator/Sawtooth/u7/d30 24.923 24.858 +0.26%
oscillator/Sawtooth/u7/d0 19.464 19.357 +0.55%
oscillator/Sine/u7/detune-changing 69.066 67.618 +2.10%
oscillator/idle/u7 7.578 7.571 +0.09%
synth/p32/u7/render 1486.213 1478.929 +0.49%
synth/p32/u7/unchanged 1487.902 1488.073 -0.01%
synth/p32/u7/automation 2062.973 2079.702 -0.81%
synth/p32/u7/idle 490.592 490.439 +0.03%
synth/p32/u7/released 548.203 551.142 -0.54%
synth/p32/u7/sine 2299.711 2092.288 +9.02%
synth/p32/u7/detune 3084.964 2999.423 +2.77%

出力の比較

旧実装と変更後を各波形1,376,256サンプル、合計5,505,024サンプル比較した。サンプルレートは1 / 8,000 / 44,100 / 48,000 / 96,000 / 192,000 Hz。1〜7ユニゾン、定常入力および毎サンプル変更(周波数・Detune・ユニゾン数・振幅・attack)、Note On/Off・再トリガー・停止を走査した。位相モードはKeepとZero。Detuneの0/100 cent、周波数0・サンプルレート超過・f64最大値も含む。

波形 最大絶対差 差分のRMS
Sine 0 0
Triangle 1.33226762955018785e-15 1.37890505268202592e-16
Square 0 0
Sawtooth 0 0

三角波の差は折り返し式の演算順序に伴う浮動小数点の丸め差。全出力が有限であることを確認した。回帰テストでは位相0・1/4・1/2・3/4・1の境界と隣接する浮動小数点値を区間ごとの直線式で検証する。既存の位相モード・停止・再トリガー・周波数キャッシュ・全波形RMSの検証も含め、releaseの52テストが成功した。Clippy・フォーマット検査も成功。追加した境界値テストはMiriでも成功した。Linuxの公式SDKヘッダによるABI検証は100インスタンス・float32/64・ノート・位相継続・波形オートメーション・状態保存・無音・ライフサイクルを含めて成功した。

全測定の生データ

Clone this wiki locally