Compare commits

..
Author SHA1 Message Date
Dylan M. Taylor 498849745a Reuse shared Base64 for control responses 2026-11-15 13:31:48 -05:00
Zinadin Zidan 2688ccac88 io: import a MIDI file with stray bytes after its last track (#1528) (#1540)
* io: import a MIDI file with stray bytes after its last track (#1528)

Importing a MIDI file read it with craft-midi's strict read_smf, which
refuses any damage. The file in #1528 is whole, its one track ending with
End of Track at its declared length, but two line feeds follow it. The
strict reader took them for the start of another chunk and refused the
file with "unexpected end of data at byte 45281".

Read imported files with read_smf_lenient instead, as other MIDI readers
do: it reads a sound file exactly as read_smf does, ignores bytes after
the last chunk, and keeps what makes sense of a damaged track, bounded by
the same event and track limits. Its warnings are logged. Something that
is not a Standard MIDI File is still refused, and the file is still read
in full before the session changes (#911).

Test: a complete file followed by two line feeds imports its note; it
failed with the issue's error before.

* io: a MIDI file past the reader's limits stays refused (#1528)

Read leniently, a file with more events or tracks than craft-midi reads
kept what came before the limit and left the rest out with only a log
warning, where the strict reader refused it. Import now reads a file
strictly first: a sound file reads as before, a file past the event or
track limit is refused as before, and only other damage (stray bytes
after the last track) falls back to the lenient reader.

Test: a file one track past the limit, its last track holding a note and
stray bytes after it, is refused and leaves the session empty.

* io: refuse a MIDI file past the event limit after a damaged track (#1528)

The strict reader stops at the first damage. When a damaged track came
before the one that goes past MAX_EVENTS, the strict error was not a
limit error, the import fell back to the lenient read, and that read
kept what came before the limit and left the rest out with a warning:
the file was imported cut short.

The lenient read now says when a limit left part of the file out
(LenientRead::over_limit, storytold/craft-libs#1308), and the import
refuses such a file before the session changes, warnings full or not.

The six craft-libs crates pinned at 6a86adb move together to that PR's
commit, as craft-scraft-format uses craft-midi's and craft-audio's
types. They move to its merge commit once it merges.

* deps: keep only the craft-libs source changes in Cargo.lock (#1528)

Moving the craft-libs pins also re-resolved several windows-sys edges
(0.61.2 to 0.52.0) and tempfile's getrandom edge (0.4.3 to 0.3.4).
Restore main's resolution for those, so the lockfile only changes the
source of the craft-libs crates.

* deps: pin craft-libs to the merge of craft-libs#1308 (#1528)

Move the six craft-libs crates from the PR's commit 4db60c4 to its merge
into craft-libs main, 45a8e16. None of these crates, nor craft-zip which
craft-scraft-format uses, changed between the two.
2026-11-14 15:55:41 +01:00
Dylan M. Taylor ae652ab34b Merge pull request #1533 from storytold/fix/clip-name-pill-height
Fix clip name pill text fit
2026-11-13 09:03:26 -05:00
Dylan M. Taylor cd7ff05bdd Fix clip name pill text fit 2026-11-13 13:49:21 +00:00
FredApps 6d525c73d4 CI: use the shared git-sources check (#1509)
nix.yml ran its own copy of the git-sources job; it now calls
craft-common-actions git-sources.yml.
2026-11-13 14:04:32 +01:00
Dylan M. Taylor a8bbf12014 Merge pull request #1527 from storytold/codex/soundcraft-session-format
Use craft-libs for SoundCraft session files
2026-11-13 07:24:21 -05:00
Dylan M. Taylor ff66d48326 chore: pin shared session format to merged release 2026-11-13 12:12:38 +00:00
Dylan M. Taylor 48ea8ecfe4 Merge remote-tracking branch 'origin/main' into codex/soundcraft-session-format
# Conflicts:
#	Cargo.lock
#	Cargo.toml
2026-11-13 10:39:45 +00:00
Dylan M. Taylor 2aa2f66fd3 pin SoundCraft format to merged shared release 2026-11-13 10:29:15 +00:00
FredApps 1775ce5061 Merge pull request #974 from lucasegen/fix/clip-cut-seam
Edit window: butted clips share one seam, and the clip name is a small pill
2026-11-13 10:37:31 +01:00
FredApps e6b77d0314 Merge pull request #972 from lucasegen/feat/edit-io-volume-pan
Let the Edit window's I/O column set volume and pan
2026-11-13 10:33:33 +01:00
FredApps f0c7b35a4a Merge pull request #969 from lucasegen/fix/edit-mode-colors
Give each edit mode its own colour when it is active
2026-11-13 10:31:01 +01:00
Dylan M. Taylor 6b35582667 Merge pull request #1525 from storytold/codex/778-soundcraft-video
Use shared H.264 and ProRes codecs in SoundCraft
2026-11-13 04:09:41 -05:00
Dylan M. Taylor 869db7dc6a Pin shared video codecs to merged revision 2026-11-13 08:51:39 +00:00
Dylan M. Taylor ff9c9114f0 pin SoundCraft time to shared consolidation 2026-11-13 08:07:35 +00:00
Dylan M. Taylor acb547286d Move SoundCraft session format into craft-libs 2026-11-13 04:30:16 +00:00
Dylan M. Taylor 1f57bb19dc Fix test binary lookup in archived runs 2026-11-13 03:48:01 +00:00
Dylan M. Taylor 935505bb07 Move SoundCraft H.264 and ProRes codecs to craft-libs 2026-11-13 03:23:51 +00:00
Dylan M. Taylor e600ed6b0d Merge remote-tracking branch 'origin/main' into codex/778-soundcraft-video 2026-11-13 02:22:27 +00:00
FredApps 2bde8b9dbc Merge pull request #971 from lucasegen/fix/playhead-record-colour
Draw the playback cursor neutral and keep red for recording
2026-11-13 02:17:51 +01:00
MarekSpodBiedry c542fe931d Hide the input selector on the Master strip (#1271) (#1356)
* Hide the input selector on the Master strip (#1271)

* Keep the Master I/O section at full height so its fader lines up with other strips (#1271)
2026-11-13 01:52:31 +01:00
Ashok Ramandashokram-si 1110e6804a io: a missing movie is offline with its checked path (#923) (#1156)
* io: a missing movie is offline with its checked path (#923)

relink_videos resolved a stored movie path, found no file, and then left
the raw stored string in the session with offline unset. The video
worker opened that string as given, relative to the working directory,
so a session path reached the file system without going through
stored_path.

Store the resolved path in that arm and mark the movie offline, as the
refused-path arm already does; the Video track then shows
"media offline: file not found" instead of trying the path. Test: a
relative stored path that names no file.

* io: keep a missing movie's stored path as it was

Review follow-up: the first commit replaced the stored string with the
resolved absolute path. Saving then wrote this folder's path, and a
session moved together with its movie no longer found it under the same
relative path (relinking only looks beside the session and in
"Video Files").

Marking the movie offline is what keeps the Video track from opening the
stored string, so leave the string as stored. The test now saves the
session into another folder, puts the movie back there under the same
relative path and checks it opens.

* Test: every outcome of relinking a movie

Its file is there; it is gone with a copy next to the session or in Video
Files (relinked); it is gone with no copy (offline, path kept); its stored
path is refused, with or without a copy.

---------

Co-authored-by: ashokram-si <347905169+ashokram-si@users.noreply.github.com>
2026-11-13 01:51:36 +01:00
Ashok Ramandashokram-si 6a87da9cf7 io: check a MIDI file in full before the session changes (#911) (#1157)
* io: check a MIDI file in full before the session changes (#911)

import_midi_bytes wrote the file's tempo and meter changes and added a
track per SMF track as it went, then returned an error for a track
longer than a day. Through execute the session is restored on error,
but the web shell calls the function directly and only pushes its undo
step on success, so a refused file left the earlier tracks and the new
tempo map in place with no way back.

Build the tempo map apart from the session, check every track's length
with it, and only then commit the map and place the clips. Test: a file
whose second track is too long leaves the session equal to before.

* io: a refused MIDI file isn't an edit either

The checks before the import ran after session_mut, which already counts
as an edit: it bumps the revision, marks the engine unsaved and may copy
the session. They now read through session(), and session_mut is taken
only once nothing can fail. The test checks the revision and unsaved
mark too.

---------

Co-authored-by: ashokram-si <347905169+ashokram-si@users.noreply.github.com>
2026-11-13 01:51:06 +01:00
.CooCoo 9c05488a09 Read send destinations only while the menu is open (#1142) 2026-11-13 01:50:37 +01:00
MarekSpodBiedry 16110013c3 Import Audio: Browse selects several files at once (#1089) (#1206) 2026-11-13 01:50:08 +01:00
.CooCoo 1c4a8380e5 Dialogs: Enter presses the default button only when no button has the focus (#1243)
* Dialogs: Enter presses the default button only when no button has the focus

egui treats Enter on a focused button, checkbox, radio button or combo
box as a click, and the dialogs also pressed their default button on the
same key: Enter on a focused Cancel ran the dialog's OK (Strip Silence
stripped the clips, Bounce Mix started the bounce), Enter on Don't Save
saved first, and Enter on Browse... opened the picker and then pressed
OK or Bounce.

- Enter presses the default button only on a new key press, and only
  when no widget has the keyboard focus or a text field has it, so a
  held Enter's repeats do not press it.
- On the first frame a dialog is drawn, it clears the keyboard focus a
  widget in the main window may still hold, and ignores Enter: that
  Enter opened the dialog from a menu item or the command palette.
- Every way a dialog opens goes through Dialogs::set, so a dialog that
  replaces an open one, the Unsaved Changes prompt, the Spot dialog and
  Tempo Change get the same first frame.

* Dialogs: a Spot dialog that a clip click opens also ignores the Enter of its first frame

With the Grabber or Trim tool in Spot mode, a click on a clip set the
open dialog directly, so a Spot dialog that replaced an open dialog kept
the shown state and could take an Enter pressed in the same frame. Both
clicks now open it through Dialogs::set.

* Tests: Enter right after Strip Silence and Rename Selected Clips switch presses the new default button

* Tests: Enter on a focused default button, and the first frame of the remaining dialogs that replace another

A focused Create runs its command once. Clear, Auto Rename, Modify Group, the unsaved changes question and Tempo Change each ignore the Enter of their first frame when they replace an open dialog. Enter on a focused Browse in Bounce Mix starts no bounce.
2026-11-13 01:48:53 +01:00
Lorie faa38be657 Windows: Include portable.txt in portable ZIP packages (#1341) 2026-11-13 01:48:24 +01:00
Jonathan Cardoso 2aa342859a mix.volume: say what each track got and when a value was clamped (#1353)
* mix.volume: say what each track got and when a value was clamped

The fader stops at -144 and +12 dB, and mix.volume replied only with a track
count, so +15 dB played at +12 with nothing to show it. The reply now lists
each target's volume and sets clamped when a target or a group member was
held at a stop.

* mix.volume: list every requested track in the reply

A target moved as a group sibling of an earlier one was skipped before it was
added to volumes, so tracks [3, 6] in one group listed only track 3. Build
volumes after the loop from each requested track's final volume.
2026-11-13 01:47:53 +01:00
gyatskovandGennadij Yatskov d5d3846a78 vst3: Add missing scan paths (#1450)
* Add missing VST3 paths for Linux

* Add missing VST3 paths for Windows

* Add missing VST3 paths for macOS

* Make os_search_paths private

* Use hermetic search_paths in unix test and remove os_search_paths

* Rewrote the Unix test to use the hermetic search_paths with a fixed executable, a None case, and no process state.
* Replaced os_search_paths with default_search_paths_with, injecting the environment lookup and executable so tests
  now cover the COMMONPROGRAMW6432 → COMMONPROGRAMFILES fallback, COMMONPROGRAMFILES(X86), LOCALAPPDATA, app-local folders, and VST3_PATH.

* Add docstring to env_from

---------

Co-authored-by: Gennadij Yatskov <gennadij@yatskov.de>
2026-11-13 01:46:59 +01:00
NetterSilverandClaude Haiku 5.5 f1cf95b354 Support the 1/4t grid in event.quantize (#1453)
Found while testing crates/engine/src/cmd/event.rs:233-248 (grid_ticks, used by quantize)

Co-authored-by: Claude Haiku 5.5 <noreply@anthropic.com>
2026-11-13 01:46:22 +01:00
Javier Brines GarcíaandClaude Opus 5.5 a64f3998b2 test(cli): accept a connect timeout as an unreachable app (#1418) (#1421)
On Windows the client's 800 ms connect timeout can expire before a closed
loopback port is refused, so the CLI reports "connection timed out" without
an "(os error N)" suffix. Both assert_refused helpers now accept either
reason, and still check exit 1, empty stdout and the exact address prefix.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-13 01:45:49 +01:00
Javier Brines GarcíaandClaude Opus 5.5 a7030f1d2f docs(engine): say a new mix.send starts at -144 dB (#1410) (#1426)
The help for mix.send said `level_db?: 0`, but a new send has always started
at -144 dB (silent): SendSlot::new sets it, the Mix window relies on it, and
Preferences > Mixing shows "Sends Default to -inf dB". The help now gives the
range and the real default, and a test checks the help and a new send agree.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-13 01:43:48 +01:00
oracola91 21d02347e1 Control server: close the connection after a line that isn't a request (#1414)
A line that was not JSON got a "bad JSON" error reply and the server kept
reading the same connection. A JSON value that was not an object, or had no
string `method`, was forwarded to the UI as a request with an empty method.

Now a line that is not a JSON object with a string `method` gets one error
reply and the connection is closed, as in the other Craft apps' control
servers. Control clients send one request object per line, so a peer that
sends anything else is not one, and nothing it sends after that line is run.
Valid requests and blank lines are handled as before.

The protocol doc says so next to the error format.
2026-11-13 01:43:08 +01:00
.CooCoo 4a5eb1514a Plugins: Alt-click returns a parameter to its default value (#1081)
* Strip Silence: the sliders move a tenth as far while Ctrl or Command is held

While Ctrl (Command on a Mac) is held, a drag on one of the four Strip
Silence sliders moves the value a tenth as far as the same drag
without the key, on the slider and on its value box. Pressing or
releasing the key during a drag leaves the value where it is, and the
drag goes on from there. Releasing the mouse button leaves the value
too. A drag without the key works as before.

The new widgets::fine_slider uses the modifier test and the tenth of
widgets::fader. Threshold and Clip Start Pad show one decimal place,
and a drag on the slider during which the key was held rounds the
value to the places the box shows.

* Plugins: parameter sliders move a tenth as far while Ctrl or Command is held

While Ctrl (Command on a Mac) is held, a drag on a parameter slider in
the plugin window, the Clip Effects window or an AudioSuite plugin's
window moves the value a tenth as far as the same drag without the key,
as the Strip Silence sliders do. A logarithmic slider moves a tenth as
far along the slider, and a whole-number slider, such as the Sampler's
Root Note, stays on whole numbers. A drag without the key works as
before.

widgets::FineSlider is a new builder with the slider options these
windows use (no value box, logarithmic, whole numbers, a salt for
unlabeled sliders); widgets::fine_slider keeps its signature and calls
it.

* Plugins: Alt-click returns a parameter to its default value

An Alt-click (Option-click on a Mac) on a parameter in the plugin window,
the Clip Effects window or an AudioSuite plugin's window sets the
parameter's default value, as an Alt-click sets a fader to 0 dB. It works
on a slider, on a slider's value box and on a choice list. The click on a
value box does not open it for typing, and the click on a choice list does
not open the list. In the plugin window and the Clip Effects window the
reset runs the same command as a drag, so it is one undo step. While Alt
is held, a drag on one of these sliders leaves the value where it is.

widgets::FineSlider has a new default_value option for this.

* Plugins: FineSlider::default_value describes what a held Alt does

While Alt is held, a drag leaves the value where it is, and an Alt-click
on the value box does not open it for typing. An Alt-click on the rail
sets the default.

* Dialogs: Enter on a focused Cancel or Don't Save no longer runs OK

Enter on a focused button clicks it and also presses the dialog's OK.
In Strip Silence, Enter on Cancel closed the dialog and still stripped
the selected clips. Cancel and Escape now drop the command and the save
that OK asked for, in every dialog. In Unsaved Changes, Enter on a
focused Don't Save closes without saving instead of opening Save.

* Read send destinations only while the menu is open

* Tests: pressing Ctrl during a value-box drag keeps the value

The rest of the drag on the Threshold value box moves as far as a drag
with Ctrl held from the start.

* Bounce Mix: Cancel starts no bounce; fine adjust on more AudioSuite sliders

Enter on a focused Cancel also pressed Bounce, which started the bounce
before the Cancel check ran. The bounce now starts after that check.

The Normalize, Time Shift and Pitch Shift sliders in the AudioSuite
window move a tenth as far while Ctrl or Command is held.

* Tests: give the Enter-on-Cancel bounce test its own scratch folder

It shared bounce-cancel with cancel_or_escape_stops_a_bounce_and_leaves_no_file,
so the two tests removed each other's folder when run in parallel.

* Clip Effects: log-taper parameter sliders use a logarithmic rail

The EQ and Dynamics sliders in the Clip Effects window now follow the
parameter's taper, as the plug-in window does, so a frequency, Q, ratio,
attack or release slider moves along a logarithmic rail.

* Dialogs: leave Enter on a focused Cancel or Don't Save to its own pull request

The dialog changes for Enter on a focused Cancel or Don't Save, the
Bounce Mix change that started the bounce after the Cancel check, and
their tests in save_changes.rs, strip_silence_dialog.rs and bounce.rs
are removed from this branch. The fine adjustment on the sliders stays.

* Plugins: tests cover Alt reset and Ctrl fine adjustment on more slider kinds

Tests now cover Ctrl without Command, Alt pressed during a drag, one
undo step for a choice-list reset, whole-number and logarithmic sliders,
Alt-click and Ctrl-drag on the AudioSuite and Clip Effects value boxes,
and Alt on a Strip Silence slider, which behaves like a plain click or
drag.

The ROADMAP entry limits the choice-list reset to the plugin window, and
the docs/ui-parity.md fine-adjustment row names the Clip Effects and
AudioSuite sliders.

* Plugins: a short Ctrl-drag at the low end of a log slider with a value box moves the value

A fine drag rounded to at most three decimal places, so a one-point
drag near the bottom of a range such as Attack at 0.01 ms rounded back
to where it started. The rounding now allows up to six places.

* Strip Silence: the fine slider's response reports each change of a drag

The value moved by a drag that the key has touched is set before the
slider reads it, so the Response's changed() stayed false on those
passes. fine_slider now marks the response when the value changed.

Tests: a Control-only drag, and changed() on every pass of a Ctrl-drag.
The value box test releases the button once and checks the value after.

* Tests: plugin sliders with Control alone and with Ctrl toggled during a drag

The plugin window's logarithmic and whole-number sliders keep their value
when Ctrl is pressed or released during a drag, and a drag with Control
alone is fine. The tenth-as-far test names the parameters it drags and
checks the others do not change, and the low-end test checks the value
after the release.
2026-11-13 01:38:35 +01:00
Dylan M. Taylor 6a0949d9d0 Review fixes after the craft-audio switch; MIDI files from craft-midi (#1513)
* Review fixes after the craft-audio switch; pin craft-audio at craft-libs main

Follow-up to #1506, for its review comments:

- convert: a decode that concealed damaged packets is reported as a warning,
  and --in-place refuses to replace the source with it.
- convert: the decoded speaker layout goes to the encoder, so 5.1 with side
  surrounds stays side surrounds in WAV and FLAC.
- FileFormat::of names an MP4 or QuickTime file by its codec: AAC or HE-AAC
  is Aac, ALAC is Alac, anything else (Opus, AC-3, PCM) is Other.
- Bounce keeps a picked extension that already names the format (Mix.aifc,
  Mix.AIFF), so the file written is the one the save dialog confirmed.
- rust-version is 1.95: craft-audio, craft-media and craft-ui declare it, so
  1.90 was not a version the workspace builds with.

craft-audio and craft-media move to craft-libs main 7db4645d.

* MIDI: take Standard MIDI Files and the sequence model from craft-midi

craft-midi in craft-libs was moved from this crate (storytold/craft-libs#884):
smf.rs, sequence.rs, the error type and their tests. soundcraft-midi now
re-exports them, so every caller keeps its imports, and keeps what is
SoundCraft's own: the editing operations, the display names and XorShift.

The SMF and Sequence tests are deleted here; they run in craft-midi, which
also adds one for the track limit. The serde names do not change (the same
derives behind craft-midi's serde feature), so sessions read as before.
2026-11-11 19:51:41 -05:00
lucasegen 6fddda5618 Merge remote-tracking branch 'upstream/main' into fix/clip-cut-seam
# Conflicts:
#	crates/ui-egui/src/edit_window/tracks.rs
2026-11-05 13:14:25 +01:00
lucasegen 045f70d823 Merge remote-tracking branch 'upstream/main' into fix/clip-cut-seam
# Conflicts:
#	crates/ui-egui/src/edit_window/tests.rs
2026-11-03 16:06:52 +01:00
lucasegen e4f2d888ab Merge remote-tracking branch 'upstream/main' into fix/playhead-record-colour
# Conflicts:
#	crates/ui-egui/src/edit_window/tests.rs
2026-11-03 16:06:51 +01:00
lucasegenandClaude Opus 5.5 4f99e2a686 Edge the Universe's playback line so it reads over every track colour
The Universe paints clips in their raw track colours, and the neutral playback line is as light as some of them (1.1:1 over the first one in the dark theme): a dark edge on each side keeps it visible.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-28 19:06:53 +01:00
lucasegenandClaude Opus 5.5 3505cd9704 Merge upstream main into feat/edit-io-volume-pan
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-28 17:34:47 +01:00
lucasegenandClaude Opus 5.5 f3873d4e2c Merge upstream main into fix/playhead-record-colour
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-28 17:30:37 +01:00
lucasegenandClaude Opus 5.5 42cbb42c90 Merge upstream main into fix/clip-cut-seam
Upstream grouped draw_clip's arguments into `ClipLane` and split it into parts: the cut joins the clip's geometry, so the overlays draw the cut's single line, and the name bar stays a pill.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-28 17:05:49 +01:00
lucasegenandClaude Opus 5.5 1b652ea000 Merge upstream main into fix/clip-cut-seam
Upstream split edit_window.rs into modules: the clip outline, cut line and name pills move to edit_window/clips.rs, the per-playlist cuts are worked out in edit_window/tracks.rs, the fade handles follow in edit_window/input.rs, and the tests go to edit_window/tests.rs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-27 02:50:57 +01:00
lucasegenandClaude Opus 5.5 e641cbe906 Merge upstream main into fix/playhead-record-colour
Upstream split edit_window.rs into modules: the playhead colour moves to edit_window/overlay.rs and its tests to edit_window/tests.rs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-27 02:46:26 +01:00
lucasegenandClaude Opus 5.5 35db9d7323 Merge upstream main into feat/edit-io-volume-pan
Upstream split edit_window.rs and mix_window.rs into modules: the I/O column's volume and pan buttons move to edit_window/header.rs, and the readout helpers they share stay in mix_window/sends.rs, now crate-visible.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-27 02:42:44 +01:00
lucasegenandClaude Opus 5.5 348ce70991 Merge upstream main into fix/clip-cut-seam
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-26 10:15:48 +01:00
lucasegenandClaude Opus 5.5 1230881dea Merge upstream main into fix/playhead-record-colour
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-26 10:15:48 +01:00
lucasegenandClaude Opus 5.5 10b7b3cb2a Merge upstream main into feat/edit-io-volume-pan
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-26 10:15:48 +01:00
lucasegenandClaude Opus 5.5 b54d7aed76 Name the I/O view's volume and pan buttons after their track
Two tracks showing the same value had buttons with the same accessible name,
such as "Volume -3.0": a screen reader could not tell them apart.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-25 18:12:02 +01:00
lucasegenandClaude Opus 5.5 7085427570 Follow egui 0.37 in the I/O view's volume and pan controls
Ui::id is now Ui::scope_id, WidgetType is Role, and a fader takes an
accessible name: the I/O fader is named after its track, as the Mix window's is.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-25 18:03:37 +01:00
lucasegenandClaude Opus 5.5 9317106247 Merge upstream main into fix/playhead-record-colour
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-25 17:41:41 +01:00
lucasegenandClaude Opus 5.5 fda4811e29 Merge upstream main into fix/edit-mode-colors
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-25 17:41:28 +01:00
lucasegenandClaude Opus 5.5 3d279a76fb Merge upstream main into fix/clip-cut-seam
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-25 17:41:28 +01:00
lucasegenandClaude Opus 5.5 b7759bf15a Merge upstream main into feat/edit-io-volume-pan
Upstream gave editable_readout a Readout with an accessible name: the I/O
view's volume readout passes one, named after its track.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-25 17:40:47 +01:00
lucasegenandClaude Opus 5.5 6f6eda0240 Merge upstream main into fix/clip-cut-seam
Upstream now draws large sessions without per-clip work every frame. The cut
between butted clips keeps to that: on a playlist sorted by start, one pass and
two binary searches per drawn clip find where it meets its neighbours, instead
of a scan of the playlist per clip, and a name pill is not laid out where it
cannot fit.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-24 07:07:57 +02:00
lucasegenandClaude Opus 5.5 67a211fff7 Merge upstream main into fix/edit-mode-colors
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-24 04:40:26 +02:00
lucasegen 00fe7eccd7 Merge upstream main into fix/playhead-record-colour 2026-10-23 22:22:02 +02:00
lucasegen 496dd33922 Merge upstream main into fix/clip-cut-seam 2026-10-23 22:20:56 +02:00
lucasegen 141396836e Merge upstream main into feat/edit-io-volume-pan 2026-10-23 22:20:55 +02:00
lucasegen 911a72ee5a Merge upstream main into fix/edit-mode-colors 2026-10-23 22:20:55 +02:00
lucasegenandClaude Opus 5.5 114d231fab Show the rating stars whatever the name switch says
The stars used to live in the full-width name bar, so turning View > Clip > Name
off hid them too. They now sit in a pill of their own, with the lock, so the
two switches no longer depend on each other: View > Clip > Rating alone decides
whether the stars show, and turning the names off hides only the name pill.

The test rates every clip and tries the four combinations of the two switches.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-23 22:09:55 +02:00
lucasegenandClaude Opus 5.5 040f52f597 Draw only the clips on screen in the alternate playlist lanes
The main lane skips the clips outside the view, but the alternate playlist
lanes drew all of them. Each draw_clip scans its whole playlist for the seams
it shares with its neighbours, so a long alternate playlist cost a quadratic
scan every frame, however few of its clips showed. The alternate lanes now use
the same overlap test as the main one; the clips that show draw as before.

The test puts two clips on screen and two far past the view in an alternate
playlist, and counts the clip bodies drawn.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-23 22:09:50 +02:00
lucasegenandClaude Opus 5.5 9431333734 Keep the pressed Shuffle key readable in the dark theme
Review feedback: pressing the active Shuffle key darkens its face, and
in the dark theme the top of that gradient fell to 4.10:1 against the
dark label, under the 4.5:1 the active colours are held to. The dark
Shuffle red is a little lighter (238,96,86 -> 246,112,102: 6.17:1 at
rest, 4.66:1 at the top of the pressed gradient), and the contrast test
now checks both ends of the pressed gradient for every mode in both
themes. The pressed shading is now a named constant shared with the test.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-23 17:42:39 +02:00
lucasegenandClaude Opus 5.5 e383e89653 Draw the playback cursor neutral and keep red for recording
The Edit window drew the playhead red whenever the transport ran. Red now
means recording only: plain playback uses its own neutral grey, the new
`Tokens::playback` (rgb 140,140,142 on Dark, 110,113,119 on Light), which
reads over clips and the rulers. It is not the insertion colour (230 on Dark,
35,36,40 on Light): the edit cursor can stand beside the moving line, and the
two must never read as the same line. A test pins a contrast of at least 2.5:1
between them and 3:1 of the line on the lanes, in both themes.
`Tokens::playhead` stays red, for the record line and for the Sampler's
"S" marker. The Universe overview line follows the same rule.

Conflict note: open PR #111 (persistent playhead) goes the other way and
draws the red playhead at rest too. It rewrites the same lines of
`overlay()` and `universe()`, and it already conflicts with main, so it
needs a rebase and a decision on the colour before either lands.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-23 15:35:20 +02:00
lucasegenandClaude Opus 5.5 d171b0fd20 Show the clip name as a small pill over the clip instead of a full-width bar
The name is now a rounded pill at the clip's top-left, one point inside the
outline: half-transparent black with white bold text, as wide as the text plus
padding. A short clip caps the pill and cuts the text cleanly (no ellipsis); a
clip too narrow to show anything useful shows no pill. The name still floats to
the lane's left edge when the clip starts off-screen, and View > Clip > Name
still turns it off. The waveform, fades and gain line now use the clip's full
height.

The rating stars and the lock that lived in the old bar sit in a matching pill
at the top-right, and the name pill keeps clear of it; that pill is hidden on a
clip too narrow to hold it. Each clip's pills are drawn right after the clip, so
clips and overlays drawn later cover them. Video thumbnails use the full height
too, with the pill drawn over them.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-23 15:08:15 +02:00
lucasegenandClaude Opus 5.5 a05d36ba2e Draw a cut between butted clips as one shared line with square corners
Two clips that meet exactly (the later starts where the earlier ends) now
share a single 1 pt dark line, as in the incumbent, instead of two outlines
side by side. The later clip draws the line over its name bar and waveform;
the earlier one drops its right edge. Both sides of the cut are square.
Gaps, overlaps and lone clips keep today's look. Selection only recolours the
body, so a selected clip keeps its full outline.

The decision is a small pure helper (clip_cut) with unit tests.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-23 15:08:15 +02:00
lucasegenandClaude Opus 5.5 5e71d4b020 Let the Edit window's I/O column set volume and pan
The vol and pan readouts in the Edit window's I/O column were painted text.
A click on the volume now opens a vertical fader (the Mix window's widget)
over a readout that takes a typed dB value. A click on the pan opens a
slider, or an L and an R slider on a stereo track. An Alt-click resets
without a popup: 0 dB for the volume; for the pan, every panner goes back to
what a new track has (centre on mono, hard left and right on stereo).

Everything goes through mix.volume and mix.pan. A drag, a typed value or a
reset is one undo step (execute_merged, same keys as the Mix window), and the
fader sets Gesture::Fader so Touch and Latch automation write it. A VCA, or a
track on a multichannel output, keeps a text-only pan, as in the Mix window.
The readout paints nothing more until it is hovered, focused or open.

editable_readout, parse_db and parse_pan become pub(crate), and the Mix
window's "is this track surround-panned" test becomes surround_panned so both
windows share it.

Refs #724.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-23 15:08:14 +02:00
lucasegenandClaude Opus 5.5 5adb9b5964 Give each edit mode its own colour when it is active
Shuffle, Spot, Slip, Grid and Relative Grid all lit up the same green,
so the active mode could only be told apart by reading the label. Add
one active-fill token per mode, in both themes: Shuffle red, Spot
orange, Slip green, Grid blue, Relative Grid violet. Labels keep
`mode_on_text` and reach at least 4.5:1 on every fill; Light Slip is a
shade deeper than `mode_on` to get there. `mode_on` and the inactive
state are unchanged, and the Snap to Grid outline on the Grid key now
takes the Grid colour instead of the Slip green.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-23 15:08:14 +02:00
127 changed files with 3712 additions and 18947 deletions
+2 -40
View File
@@ -23,43 +23,5 @@ jobs:
uses: storytold/craft-common-actions/.github/workflows/nix.yml@main
git-sources:
name: Git dependencies come from storytold
runs-on: ubuntu-latest
timeout-minutes: 5
steps:
- name: Harden runner
uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4
with:
egress-policy: audit
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
- name: Git dependencies come from storytold
run: |
set -euo pipefail
test -f Cargo.lock || { echo "Cargo.lock is missing"; exit 1; }
# Source replacement, patches and path overrides in cargo config never show up
# in Cargo.lock. Parsed rather than grepped: TOML has many spellings of one key.
# Cargo still reads the legacy .cargo/config.
python3 - <<'PY'
import os, sys, tomllib
for path in (".cargo/config", ".cargo/config.toml"):
if not os.path.exists(path):
continue
config = tomllib.load(open(path, "rb"))
replaced = [name for name, source in config.get("source", {}).items()
if isinstance(source, dict) and "replace-with" in source]
if replaced or "patch" in config or "paths" in config:
print(f"{path} replaces, patches or overrides a source")
sys.exit(1)
PY
# Fails when Cargo.lock is out of date with the manifests.
cargo metadata --locked --format-version 1 > /dev/null
python3 - <<'PY'
import sys, tomllib
lock = tomllib.load(open("Cargo.lock", "rb"))
bad = sorted({p["source"] for p in lock.get("package", [])
if p.get("source", "").startswith("git+")
and not p["source"].startswith("git+https://github.com/storytold/")})
if bad:
print("Git dependencies outside github.com/storytold:", *bad, sep="\n")
sys.exit(1)
PY
# Tracks @main so suite-wide CI fixes reach every app without a PR in each repository.
uses: storytold/craft-common-actions/.github/workflows/git-sources.yml@main
Generated
+100 -22
View File
@@ -1064,7 +1064,7 @@ dependencies = [
[[package]]
name = "craft-audio"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=75895c0321d8cb6e175a42466cba050352b117c5#75895c0321d8cb6e175a42466cba050352b117c5"
source = "git+https://github.com/storytold/craft-libs?rev=45a8e16669e70dadb780dfbc759c882dd5b7ea1c#45a8e16669e70dadb780dfbc759c882dd5b7ea1c"
dependencies = [
"craft-media",
"log",
@@ -1073,20 +1073,25 @@ dependencies = [
"thiserror 2.0.21",
]
[[package]]
name = "craft-codepage"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=ea24422ae9859dc5da398d0462bc87f8e02e0d10#ea24422ae9859dc5da398d0462bc87f8e02e0d10"
[[package]]
name = "craft-docpath"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=6e03f65bf45181db77ba6229716e6624db9eb25b#6e03f65bf45181db77ba6229716e6624db9eb25b"
source = "git+https://github.com/storytold/craft-libs?rev=ea24422ae9859dc5da398d0462bc87f8e02e0d10#ea24422ae9859dc5da398d0462bc87f8e02e0d10"
[[package]]
name = "craft-fonts-build"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=6e03f65bf45181db77ba6229716e6624db9eb25b#6e03f65bf45181db77ba6229716e6624db9eb25b"
source = "git+https://github.com/storytold/craft-libs?rev=ea24422ae9859dc5da398d0462bc87f8e02e0d10#ea24422ae9859dc5da398d0462bc87f8e02e0d10"
[[package]]
name = "craft-i18n"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=6e03f65bf45181db77ba6229716e6624db9eb25b#6e03f65bf45181db77ba6229716e6624db9eb25b"
source = "git+https://github.com/storytold/craft-libs?rev=ea24422ae9859dc5da398d0462bc87f8e02e0d10#ea24422ae9859dc5da398d0462bc87f8e02e0d10"
dependencies = [
"fixed_decimal",
"icu_decimal",
@@ -1109,7 +1114,49 @@ dependencies = [
[[package]]
name = "craft-media"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=75895c0321d8cb6e175a42466cba050352b117c5#75895c0321d8cb6e175a42466cba050352b117c5"
source = "git+https://github.com/storytold/craft-libs?rev=45a8e16669e70dadb780dfbc759c882dd5b7ea1c#45a8e16669e70dadb780dfbc759c882dd5b7ea1c"
dependencies = [
"serde",
"thiserror 2.0.21",
]
[[package]]
name = "craft-midi"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=45a8e16669e70dadb780dfbc759c882dd5b7ea1c#45a8e16669e70dadb780dfbc759c882dd5b7ea1c"
dependencies = [
"serde",
"thiserror 2.0.21",
]
[[package]]
name = "craft-mime"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=ea24422ae9859dc5da398d0462bc87f8e02e0d10#ea24422ae9859dc5da398d0462bc87f8e02e0d10"
dependencies = [
"chrono",
"craft-codepage",
"encoding_rs",
]
[[package]]
name = "craft-scraft-format"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=45a8e16669e70dadb780dfbc759c882dd5b7ea1c#45a8e16669e70dadb780dfbc759c882dd5b7ea1c"
dependencies = [
"craft-audio",
"craft-midi",
"craft-time",
"craft-zip",
"serde",
"serde_json",
"thiserror 2.0.21",
]
[[package]]
name = "craft-time"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=45a8e16669e70dadb780dfbc759c882dd5b7ea1c#45a8e16669e70dadb780dfbc759c882dd5b7ea1c"
dependencies = [
"serde",
"thiserror 2.0.21",
@@ -1118,7 +1165,7 @@ dependencies = [
[[package]]
name = "craft-ui"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=6e03f65bf45181db77ba6229716e6624db9eb25b#6e03f65bf45181db77ba6229716e6624db9eb25b"
source = "git+https://github.com/storytold/craft-libs?rev=ea24422ae9859dc5da398d0462bc87f8e02e0d10#ea24422ae9859dc5da398d0462bc87f8e02e0d10"
dependencies = [
"craft-fonts-build",
"craft-i18n",
@@ -1139,7 +1186,7 @@ dependencies = [
[[package]]
name = "craft-update"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=6e03f65bf45181db77ba6229716e6624db9eb25b#6e03f65bf45181db77ba6229716e6624db9eb25b"
source = "git+https://github.com/storytold/craft-libs?rev=ea24422ae9859dc5da398d0462bc87f8e02e0d10#ea24422ae9859dc5da398d0462bc87f8e02e0d10"
dependencies = [
"chrono",
"dunce",
@@ -1163,6 +1210,26 @@ dependencies = [
"zip",
]
[[package]]
name = "craft-video"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=45a8e16669e70dadb780dfbc759c882dd5b7ea1c#45a8e16669e70dadb780dfbc759c882dd5b7ea1c"
dependencies = [
"craft-media",
"rayon",
"thiserror 2.0.21",
]
[[package]]
name = "craft-zip"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=45a8e16669e70dadb780dfbc759c882dd5b7ea1c#45a8e16669e70dadb780dfbc759c882dd5b7ea1c"
dependencies = [
"flate2",
"thiserror 2.0.21",
"zip",
]
[[package]]
name = "crc32fast"
version = "1.5.3"
@@ -1496,6 +1563,20 @@ dependencies = [
"bytemuck",
]
[[package]]
name = "encoding_rs"
version = "0.8.44"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "89253175e704ab869b04f9db224d38630e0211a53841a79181deae0889e169f5"
dependencies = [
"cfg-if",
"core_detect",
"multiversion_no_op",
"rustversion",
"scopeguard",
"simdutf8",
]
[[package]]
name = "endi"
version = "1.1.1"
@@ -2699,6 +2780,12 @@ dependencies = [
"windows-sys 0.61.2",
]
[[package]]
name = "multiversion_no_op"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "743fb55ba31b18fb1ecef6bdc9aa2743314978ac084044301a7eee33fb99a20d"
[[package]]
name = "naga"
version = "30.0.1"
@@ -4437,6 +4524,7 @@ version = "0.20.1"
dependencies = [
"craft-audio",
"craft-docpath",
"craft-scraft-format",
"log",
"proptest",
"serde",
@@ -4459,10 +4547,8 @@ dependencies = [
name = "soundcraft-midi"
version = "0.20.1"
dependencies = [
"craft-midi",
"proptest",
"serde",
"serde_json",
"thiserror 2.0.21",
]
[[package]]
@@ -4485,13 +4571,7 @@ dependencies = [
name = "soundcraft-model"
version = "0.20.1"
dependencies = [
"craft-audio",
"proptest",
"serde",
"serde_json",
"soundcraft-midi",
"soundcraft-time",
"thiserror 2.0.21",
"craft-scraft-format",
]
[[package]]
@@ -4513,10 +4593,7 @@ dependencies = [
name = "soundcraft-time"
version = "0.20.1"
dependencies = [
"proptest",
"serde",
"serde_json",
"thiserror 2.0.21",
"craft-time",
]
[[package]]
@@ -4524,6 +4601,7 @@ name = "soundcraft-ui-egui"
version = "0.20.1"
dependencies = [
"craft-audio",
"craft-mime",
"craft-ui",
"craft-update",
"egui",
@@ -4554,9 +4632,9 @@ name = "soundcraft-video"
version = "0.20.1"
dependencies = [
"craft-media",
"craft-video",
"image",
"proptest",
"rayon",
"serde",
"thiserror 2.0.21",
]
+13 -6
View File
@@ -7,10 +7,11 @@ default-members = ["crates/*", "apps/*", "xtask"]
version = "0.20.1"
edition = "2024"
license = "MIT OR Apache-2.0"
rust-version = "1.90"
rust-version = "1.95"
repository = "https://github.com/storytold/soundcraft"
[workspace.dependencies]
craft-mime = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "ea24422ae9859dc5da398d0462bc87f8e02e0d10" }
# internal
soundcraft-time = { path = "crates/time" }
soundcraft-midi = { path = "crates/midi" }
@@ -48,18 +49,24 @@ zip = { version = "9", default-features = false }
cpal = "0.18"
# Which file paths stored in a session may be opened.
craft-docpath = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "6e03f65bf45181db77ba6229716e6624db9eb25b" }
craft-docpath = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "ea24422ae9859dc5da398d0462bc87f8e02e0d10" }
# Video containers and bitstreams, shared with the other craft apps.
craft-media = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "75895c0321d8cb6e175a42466cba050352b117c5" }
craft-media = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "45a8e16669e70dadb780dfbc759c882dd5b7ea1c" }
# H.264 and ProRes codecs shared with the other Craft apps.
craft-video = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "45a8e16669e70dadb780dfbc759c882dd5b7ea1c", default-features = false, features = ["h264-decode", "prores"] }
# Audio file formats, codecs and waveform overviews, shared with the other craft apps. It shares
# types with craft-media, so both stay on one rev.
craft-audio = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "75895c0321d8cb6e175a42466cba050352b117c5", features = ["decode", "lossless-encode", "peaks", "serde"] }
# Standard MIDI Files and the note/controller model, shared with the other craft apps.
craft-midi = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "45a8e16669e70dadb780dfbc759c882dd5b7ea1c", features = ["serde"] }
craft-audio = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "45a8e16669e70dadb780dfbc759c882dd5b7ea1c", features = ["decode", "lossless-encode", "peaks", "serde"] }
craft-time = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "45a8e16669e70dadb780dfbc759c882dd5b7ea1c" }
craft-scraft-format = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "45a8e16669e70dadb780dfbc759c882dd5b7ea1c" }
# craft-update: app-specific pretend versions apply to startup and manual checks.
craft-update = { version = "=0.1.0", git = "https://github.com/storytold/craft-libs", rev = "6e03f65bf45181db77ba6229716e6624db9eb25b", features = ["egui"] }
craft-update = { version = "=0.1.0", git = "https://github.com/storytold/craft-libs", rev = "ea24422ae9859dc5da398d0462bc87f8e02e0d10", features = ["egui"] }
# ui
craft-ui = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "6e03f65bf45181db77ba6229716e6624db9eb25b", features = ["serde"] }
craft-ui = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "ea24422ae9859dc5da398d0462bc87f8e02e0d10", features = ["serde"] }
egui = { version = "0.37" }
# Resizable table columns (About ▸ Contributors / Models); no image loaders.
egui_extras = { version = "0.37", default-features = false }
+3 -4
View File
@@ -146,8 +146,7 @@ where things are.
## Getting started
Download a build from the [releases page](https://github.com/storytold/soundcraft/releases), or build
from source with Rust 1.95 or later for the desktop/web UI (the core workspace's declared
minimum remains Rust 1.90):
from source with Rust 1.95 or later:
```sh
git clone https://github.com/storytold/soundcraft
@@ -244,10 +243,10 @@ about egui, so the interface could be swapped for another one.
| Crate | What it does |
|---|---|
| `soundcraft-time` | Timebases, tempo and meter maps, timecode, grid |
| `soundcraft-time` | Compatibility re-export of `craft-time`'s SoundCraft sample-index API |
| `soundcraft-midi` | Standard MIDI Files and MIDI operations |
| `soundcraft-dsp` | Plugins, offline processing, meters, loudness, FFT |
| `soundcraft-model` | The session document |
| `soundcraft-model` | Compatibility re-export of `craft-scraft-format`'s schema, validation and JSON API |
| `soundcraft-mix` | The mix engine (realtime and offline share it) |
| `soundcraft-engine` | Commands, undo, editing, import/export, bounce |
| `soundcraft-playback` | Audio devices (CoreAudio, WASAPI, ALSA, WebAudio) and recording |
+7 -1
View File
@@ -2,7 +2,7 @@
**Stage: pre-alpha** · next: alpha, ~5% and ~110–180 h away (the alpha gate's three failing workflows)
> **Last reviewed:** 2026-10-24 · **Last updated:** 2026-10-25 · **Change:** trivial (Cancel Zoom Toggle, side panels in dock columns, Auto Pan stereo balance, Clip List Auto Rename, Strip Silence rectangles, Strip Silence Rename, Strip Silence Extract and Separate, Clip Looping dialog, AudioSuite Vari-Fi speed ramps, Auto Pan plugin, Subharmonic plugin, Ring Modulator plugin, Float32 file headroom) · **Target:** Avid Pro Tools Ultimate 2026.4.1
> **Last reviewed:** 2026-10-24 · **Last updated:** 2026-10-27 · **Change:** trivial (Cancel Zoom Toggle, side panels in dock columns, Auto Pan stereo balance, Alt-click returns a plugin parameter to its default, Clip List Auto Rename, Strip Silence rectangles, Strip Silence Rename, Strip Silence Extract and Separate, Clip Looping dialog, AudioSuite Vari-Fi speed ramps, Auto Pan plugin, Subharmonic plugin, Ring Modulator plugin, Float32 file headroom) · **Target:** Avid Pro Tools Ultimate 2026.4.1
SoundCraft is a clean-room, pure-Rust digital audio workstation that aims at full parity with
Avid Pro Tools, and then beyond it: faster, open, scriptable and agent-controllable. This page is
@@ -125,6 +125,9 @@ mutated and truncated movies. Field reports from the first public week are track
- **2026-10-25:** Alt+Shift+E (`view.zoom_toggle_cancel`) and an Alt-click on the Zoom Toggle button cancel Zoom Toggle and keep the current view: the zoom, the scroll and the track heights stay as they are, and the button is no longer lit. The next Zoom Toggle zooms in from that view and goes back to it. No readiness percentage change.
- **2026-10-25:** Tracks and Groups and the Clip List sit in dock columns that run the Edit window's full height at its left and right edges, with the MIDI Editor under the session view between them; Mix docks Tracks and Groups at its left edge. A panel dragged onto a dock group joins it as a tab, stacks above or below it, or makes a new column; one dragged to an empty window edge makes a dock there. A list with no saved place reopens in its dock, and makes the dock again when it is gone. Preferences and window configurations saved with the earlier split-frame layouts load with the new default layouts and keep their other settings. No readiness percentage change.
- **2026-10-25:** Auto Pan moves the balance of a stereo track: the left channel's level falls as the position moves right and the right channel's as it moves left, at constant power, and each channel stays on its own side. At the center the track passes unchanged, so a wide or out-of-phase stereo image keeps its width, and the envelope follower follows the louder channel. No readiness percentage change.
- **2026-10-24:** An Alt-click (Option-click on a Mac) on a parameter in the plugin window, the Clip Effects window or an AudioSuite plugin's window returns it to its default value, on a slider or a slider's value box, and in the plugin window on a choice list too, as an Alt-click returns a fader to 0 dB. In the plugin window and the Clip Effects window the reset is one undo step. While Alt is held, a drag on a slider leaves the value where it is. No readiness percentage change.
- **2026-10-24:** The parameter sliders of the plugin window, the Clip Effects window and AudioSuite plugins move a tenth as far for the same drag while Ctrl (Command on a Mac) is held, as the Strip Silence sliders do. Logarithmic sliders move a tenth as far along the slider, and whole-number sliders stay on whole numbers. No readiness percentage change.
- **2026-10-24:** The Strip Silence window's four sliders move a tenth as far for the same drag while Ctrl (Command on a Mac) is held, on the slider and on its value box. Pressing or releasing the key during a drag leaves the value where it is. The Threshold and Clip Start Pad boxes show one decimal place. No readiness percentage change.
- **2026-10-24:** Clip List › Auto Rename (`clip.auto_rename`) sets the names that editing gives the clips it separates from the selected clips: a name, a starting number, a number of places (zeros before the number) and a suffix. The piece after each cut takes the next name, the piece before it keeps its name, and all the pieces count up one number sequence. Pieces that Strip Silence names with its own Rename fields, or removes, take no Auto Rename name. Copied clips keep their scheme when they are pasted, saved sessions hold the schemes, and Import Session Data brings them along. No readiness percentage change.
- **2026-10-24:** While the Strip Silence window is open, the Edit window draws a rectangle around each range that Strip would keep on the selected tracks, padded by Clip Start Pad and Clip End Pad, and the rectangles follow the sliders as they move. They are removed when the window closes. `edit.strip_silence_preview` returns the same ranges without changing the session. No readiness percentage change.
- **2026-10-24:** The Strip Silence window has a Rename button. It opens Rename Selected Clips, where a name, a starting number, a count of zeros and a suffix set the names of the clips that Strip, Extract and Separate leave in the selection (for example SFX023.Reel1, SFX024.Reel1). The app keeps the scheme for later uses of Strip Silence until the app quits, or until Clear and then OK empty it. No readiness percentage change.
@@ -180,6 +183,9 @@ mutated and truncated movies. Field reports from the first public week are track
| 2026-10-25 | trivial | Progress log: Cancel Zoom Toggle; readiness estimates unchanged |
| 2026-10-25 | trivial | Progress log: side panels in dock columns; readiness estimates unchanged |
| 2026-10-25 | trivial | Progress log: Auto Pan stereo balance; readiness estimates unchanged |
| 2026-10-24 | trivial | Progress log: Alt-click returns a plugin parameter to its default; readiness estimates unchanged |
| 2026-10-24 | trivial | Progress log: fine adjustment on plugin parameter sliders; readiness estimates unchanged |
| 2026-10-24 | trivial | Progress log: Strip Silence fine adjustment; readiness estimates unchanged |
| 2026-10-24 | trivial | Progress log: Clip List Auto Rename; readiness estimates unchanged |
| 2026-10-24 | trivial | Progress log: Strip Silence rectangles; readiness estimates unchanged |
| 2026-10-24 | trivial | Progress log: Strip Silence Rename; readiness estimates unchanged |
+14
View File
@@ -15,6 +15,15 @@ pub(crate) fn convert(args: &ConvertArgs, out: &mut dyn Write) -> Result<(), Cli
let bytes = std::fs::read(input).map_err(|source| CliError::File { path: input.clone(), source })?;
let ext = input.extension().and_then(std::ffi::OsStr::to_str);
let (info, mut buf) = craft_audio::decode(&bytes, ext).map_err(|source| CliError::Audio { path: input.clone(), source })?;
// The decoder replaces packets it cannot decode with silence of their length and counts them.
// Such a file is not a copy of what was recorded: never let it replace its source, and say so
// when it is written elsewhere.
if info.damaged > 0 {
if args.in_place {
return Err(ConvertError::Damaged { input: input.clone(), packets: info.damaged }.into());
}
log::warn!("{}: {} damaged packet(s) decoded as silence", input.display(), info.damaged);
}
let source_rate = buf.sample_rate;
if let Some(rate) = rate {
buf.channels = soundcraft_dsp::offline::resample(&buf.channels, buf.sample_rate, rate);
@@ -34,6 +43,11 @@ pub(crate) fn convert(args: &ConvertArgs, out: &mut dyn Write) -> Result<(), Cli
// A BWF source keeps its `bext` (written for WAV output only). The time reference counts
// samples, so it moves to the output rate.
let mut opts = craft_audio::EncodeOptions::new(format).with_bit_depth(bit_depth).with_dither(dither);
// The source's speakers, not the usual ones for its channel count (5.1 with side surrounds
// stays that). Channels with no speaker assignment get the format's default.
if info.layout.is_ordered() {
opts = opts.with_layout(info.layout);
}
opts.bwf = info.bwf.map(|mut b| {
b.time_reference = at_rate(b.time_reference, source_rate, buf.sample_rate);
b
+2
View File
@@ -48,4 +48,6 @@ pub(crate) fn unicode(path: &Path) -> Result<&str, CliError> {
pub(crate) enum ConvertError {
#[error("{}: cannot write `.{ext}` files (supported: {})", output.display(), craft_audio::ENCODE_EXTENSIONS.join(", "))]
OutputFormat { output: PathBuf, ext: String },
#[error("{}: {packets} damaged packet(s) would be written as silence; not replacing the file (convert to another file to keep what decodes)", input.display())]
Damaged { input: PathBuf, packets: u64 },
}
+10
View File
@@ -0,0 +1,10 @@
//! Test helpers shared by the `soundcraft-cli` integration test binaries.
use std::ffi::OsString;
/// Nextest archives relocate binaries; plain Cargo keeps the compile-time path valid.
pub fn soundcraft_cli_binary() -> OsString {
std::env::var_os("NEXTEST_BIN_EXE_soundcraft_cli")
.or_else(|| std::env::var_os("NEXTEST_BIN_EXE_soundcraft-cli"))
.unwrap_or_else(|| env!("CARGO_BIN_EXE_soundcraft-cli").into())
}
+55 -1
View File
@@ -7,6 +7,8 @@ use std::process::Command;
use craft_audio::{AudioBuffer, BitDepth, BwfInfo, EncodeOptions};
mod common;
fn tmpdir(tag: &str) -> PathBuf {
let dir = std::env::temp_dir().join(format!("sc-cli-convert-{tag}-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&dir);
@@ -25,7 +27,7 @@ fn write_wav16(path: &Path, samples: &[i16]) {
}
fn convert(input: &Path, output: &Path, extra: &[&str]) {
let o = Command::new(env!("CARGO_BIN_EXE_soundcraft-cli"))
let o = Command::new(common::soundcraft_cli_binary())
.env("SOUNDCRAFT_NO_PREFS", "1")
.arg("convert")
.arg(input)
@@ -118,3 +120,55 @@ fn convert_keeps_the_bwf_time_stamp() {
assert_eq!(bext(&out), Some(bwf.with_time_reference(158_760_000)));
let _ = std::fs::remove_dir_all(&dir);
}
/// Twelve MPEG-1 Layer I frames (mono, 32 kHz, 48 bytes each) of silence. The seventh has the
/// forbidden bit allocation 15 in every subband: the decoder writes silence for it and counts it.
fn mp1_with_a_damaged_frame() -> Vec<u8> {
let header = [0xFF, 0xFF, 0x18, 0xC0];
let good = [&header[..], &[0; 44]].concat();
let bad = [&header[..], &[0xFF; 16], &[0; 28]].concat();
(0..12).flat_map(|i| if i == 6 { bad.clone() } else { good.clone() }).collect()
}
#[test]
fn convert_warns_about_damaged_packets_and_never_replaces_the_source_with_them() {
let dir = tmpdir("damaged");
let src = dir.join("take.mp1");
let bytes = mp1_with_a_damaged_frame();
std::fs::write(&src, &bytes).unwrap();
let run = |args: &[&std::ffi::OsStr]| {
Command::new(common::soundcraft_cli_binary()).env("SOUNDCRAFT_NO_PREFS", "1").arg("convert").args(args).output().unwrap()
};
// To another file: written, with a warning that says how much was lost.
let out = dir.join("take.wav");
let o = run(&[src.as_os_str(), out.as_os_str()]);
assert!(o.status.success(), "{}", String::from_utf8_lossy(&o.stderr));
let warning = format!("warning: {}: 1 damaged packet(s) decoded as silence\n", src.display());
assert_eq!(String::from_utf8_lossy(&o.stderr), warning);
assert_eq!(steps16(&out).len(), 12 * 384);
// Over the source: refused, and the source is untouched.
let o = run(&[src.as_os_str(), src.as_os_str(), "--in-place".as_ref()]);
assert_eq!(o.status.code(), Some(1));
let stderr = String::from_utf8_lossy(&o.stderr);
assert!(stderr.starts_with(&format!("error: {}: 1 damaged packet(s) would be written as silence", src.display())), "{stderr}");
assert_eq!(std::fs::read(&src).unwrap(), bytes);
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
fn convert_keeps_the_speaker_layout() {
use craft_audio::ChannelLayout;
let dir = tmpdir("layout");
let (src, out) = (dir.join("side.wav"), dir.join("out.wav"));
// 5.1 with side surrounds: not the layout a reader assumes for six channels.
let opts = EncodeOptions::default().with_layout(ChannelLayout::SURROUND_5_1_SIDE);
std::fs::write(&src, craft_audio::encode(&AudioBuffer::new(48_000, 6, 480), &opts).unwrap()).unwrap();
let layout = |p: &Path| craft_audio::probe(&std::fs::read(p).unwrap(), None).unwrap().layout;
assert_eq!(layout(&src), ChannelLayout::SURROUND_5_1_SIDE);
convert(&src, &out, &[]);
assert_eq!(layout(&out), ChannelLayout::SURROUND_5_1_SIDE);
let flac = dir.join("out.flac");
convert(&src, &flac, &[]);
assert_eq!(layout(&flac), ChannelLayout::SURROUND_5_1_SIDE);
let _ = std::fs::remove_dir_all(&dir);
}
+7 -3
View File
@@ -19,7 +19,7 @@ fn invoke(dir: &Path, args: &[&str]) -> Invoke {
}
fn invoke_env(dir: &Path, args: &[&str], control_port: Option<&str>) -> Invoke {
let mut cmd = Command::new(env!("CARGO_BIN_EXE_soundcraft-cli"));
let mut cmd = Command::new(crate::common::soundcraft_cli_binary());
cmd.env("SOUNDCRAFT_NO_PREFS", "1").env_remove("SOUNDCRAFT_CONTROL_PORT");
if let Some(port) = control_port {
cmd.env("SOUNDCRAFT_CONTROL_PORT", port);
@@ -80,12 +80,16 @@ fn not_found() -> std::io::Error {
std::io::Error::from_raw_os_error(2)
}
/// Exit 1 with the refused-connection message for `port`; the OS's text for the refusal differs by platform.
/// Exit 1 with the unreachable message for `port`. The reason is either the OS's refusal, whose text
/// differs by platform, or the client's connect timeout, which on Windows can expire before a
/// closed loopback port is refused.
#[track_caller]
fn assert_refused(got: &Invoke, port: u16) {
assert_eq!((got.code, got.stdout.as_str()), (1, ""));
let prefix = format!("error: cannot reach SoundCraft at 127.0.0.1:{port}: ");
assert!(got.stderr.starts_with(&prefix) && got.stderr.contains(" (os error ") && got.stderr.ends_with(")\n"), "{}", got.stderr);
let reason = got.stderr.strip_prefix(&prefix).unwrap_or_else(|| panic!("{}", got.stderr));
let refused = reason.contains(" (os error ") && reason.ends_with(")\n");
assert!(refused || reason == "connection timed out\n", "{}", got.stderr);
}
// ---- logging ----
+1 -1
View File
@@ -8,7 +8,7 @@ use std::process::{Command, Output, Stdio};
use serde_json::Value;
fn bin() -> Command {
let mut cmd = Command::new(env!("CARGO_BIN_EXE_soundcraft-cli"));
let mut cmd = Command::new(crate::common::soundcraft_cli_binary());
cmd.env("SOUNDCRAFT_NO_PREFS", "1");
cmd
}
@@ -3,7 +3,7 @@
use std::process::{Command, Stdio};
fn bin() -> Command {
let mut cmd = Command::new(env!("CARGO_BIN_EXE_soundcraft-cli"));
let mut cmd = Command::new(crate::common::soundcraft_cli_binary());
cmd.env("SOUNDCRAFT_NO_PREFS", "1");
cmd
}
+3
View File
@@ -1,5 +1,8 @@
//! Integration tests, built as one test binary: each file is a module.
#[path = "../common.rs"]
mod common;
#[allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]
mod behaviour;
#[allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]
+6 -2
View File
@@ -53,12 +53,16 @@ fn not_found() -> std::io::Error {
std::io::Error::from_raw_os_error(2)
}
/// Exit 1 with the refused-connection message for `port`; the OS's text for the refusal differs by platform.
/// Exit 1 with the unreachable message for `port`. The reason is either the OS's refusal, whose text
/// differs by platform, or the client's connect timeout, which on Windows can expire before a
/// closed loopback port is refused.
#[track_caller]
fn assert_refused(got: &Invoke, port: u16) {
assert_eq!((got.code, got.stdout.as_str()), (1, ""));
let prefix = format!("error: cannot reach SoundCraft at 127.0.0.1:{port}: ");
assert!(got.stderr.starts_with(&prefix) && got.stderr.contains(" (os error ") && got.stderr.ends_with(")\n"), "{}", got.stderr);
let reason = got.stderr.strip_prefix(&prefix).unwrap_or_else(|| panic!("{}", got.stderr));
let refused = reason.contains(" (os error ") && reason.ends_with(")\n");
assert!(refused || reason == "connection timed out\n", "{}", got.stderr);
}
#[test]
@@ -23,7 +23,7 @@ impl Drop for Scratch {
}
fn invoke(dir: &Path, args: &[&str]) -> Output {
Command::new(env!("CARGO_BIN_EXE_soundcraft-cli")).current_dir(dir).env("SOUNDCRAFT_NO_PREFS", "1").args(args).output().unwrap()
Command::new(crate::common::soundcraft_cli_binary()).current_dir(dir).env("SOUNDCRAFT_NO_PREFS", "1").args(args).output().unwrap()
}
#[test]
+3 -1
View File
@@ -5,8 +5,10 @@
use std::path::{Path, PathBuf};
use std::process::{Command, Output};
mod common;
fn bin() -> Command {
let mut cmd = Command::new(env!("CARGO_BIN_EXE_soundcraft-cli"));
let mut cmd = Command::new(common::soundcraft_cli_binary());
cmd.env("SOUNDCRAFT_NO_PREFS", "1");
cmd
}
+43 -7
View File
@@ -1,5 +1,7 @@
//! Loopback JSON-lines control server: one request per line, one reply per line.
//! This is the transport the MCP server (`soundcraft-cli mcp --connect PORT`) wraps.
//! A line that is not a JSON object with a string `method` gets one error reply, and the
//! connection is closed.
//!
//! Lines are bounded ([`MAX_LINE`]): a longer one is refused and the connection closed, without
//! buffering the rest. So is the number of connections served at once ([`MAX_CONNECTIONS`]).
@@ -103,7 +105,8 @@ fn read_bounded_line(reader: &mut impl BufRead, buf: &mut Vec<u8>, max: usize) -
}
/// Answer newline-delimited JSON requests for one connection: bound each line, forward it to the UI thread with a 90
/// s reply deadline, and stop at EOF, invalid UTF-8 or a closed request queue. Bad JSON still gets an error reply.
/// s reply deadline, and stop at EOF, invalid UTF-8, a closed request queue or a line that isn't a request (see
/// [`parse_request`]). That line gets one error reply first.
fn serve(stream: TcpStream, tx: SyncSender<ControlRequest>, ctx: egui::Context) {
let Ok(read) = stream.try_clone() else { return };
let mut out = stream;
@@ -123,11 +126,8 @@ fn serve(stream: TcpStream, tx: SyncSender<ControlRequest>, ctx: egui::Context)
if line.trim().is_empty() {
continue;
}
let reply = match serde_json::from_str::<Value>(line) {
Ok(msg) => {
let id = msg.get("id").cloned().unwrap_or(Value::Null);
let method = msg.get("method").and_then(Value::as_str).unwrap_or("").to_string();
let params = msg.get("params").cloned().unwrap_or(json!({}));
let reply = match parse_request(line) {
Ok((id, method, params)) => {
let (req, rrx) = ControlRequest::new(method, params);
match tx.try_send(req) {
Ok(()) => {
@@ -143,7 +143,12 @@ fn serve(stream: TcpStream, tx: SyncSender<ControlRequest>, ctx: egui::Context)
Err(TrySendError::Disconnected(_)) => break,
}
}
Err(e) => json!({"ok": false, "error": format!("bad JSON: {e}")}),
// Control clients send one request object per line. A peer that sends anything else isn't
// one: answer once and close, so nothing it sends after that line runs.
Err(e) => {
let _ = writeln!(out, "{}", json!({"ok": false, "error": format!("{e}; closing the connection")}));
break;
}
};
if writeln!(out, "{reply}").is_err() {
break;
@@ -151,6 +156,15 @@ fn serve(stream: TcpStream, tx: SyncSender<ControlRequest>, ctx: egui::Context)
}
}
/// A request line's `(id, method, params)`. The line must be a JSON object with a string `method`;
/// otherwise the error says what it is instead.
fn parse_request(line: &str) -> Result<(Value, String, Value), String> {
let msg = serde_json::from_str::<Value>(line).map_err(|e| format!("bad JSON: {e}"))?;
let Some(msg) = msg.as_object() else { return Err("not a request: expected a JSON object".into()) };
let Some(method) = msg.get("method").and_then(Value::as_str) else { return Err("not a request: no string `method`".into()) };
Ok((msg.get("id").cloned().unwrap_or(Value::Null), method.to_string(), msg.get("params").cloned().unwrap_or(json!({}))))
}
#[cfg(test)]
mod tests {
use super::*;
@@ -226,4 +240,26 @@ mod tests {
assert!(line.contains("too many control connections"), "{line}");
drop(held);
}
#[test]
fn a_line_that_is_not_a_request_ends_the_connection() {
let addr = serve_echo();
for junk in ["not json", "[1, 2]", "\"ping\"", r#"{"id": 5}"#, r#"{"id": 5, "method": 7}"#] {
let mut c = TcpStream::connect(addr).unwrap();
c.set_read_timeout(Some(Duration::from_secs(5))).unwrap();
// One write, so both lines arrive together and the request after the junk is waiting.
c.write_all(format!("{junk}\n{{\"id\":6,\"method\":\"ping\"}}\n").as_bytes()).unwrap();
let mut r = BufReader::new(c);
let mut line = String::new();
r.read_line(&mut line).unwrap();
let v: Value = serde_json::from_str(&line).unwrap();
assert_eq!(v["ok"].as_bool(), Some(false), "{junk}: {line}");
line.clear();
let closed = match r.read_line(&mut line) {
Ok(n) => n == 0,
Err(e) => e.kind() == std::io::ErrorKind::ConnectionReset,
};
assert!(closed, "{junk}: the connection stays open and the next request is answered: {line}");
}
}
}
+3
View File
@@ -156,6 +156,9 @@ fn services() -> Services {
};
d.pick_file()
})),
pick_open_many: Some(Box::new(|_purpose: &str| {
rfd::FileDialog::new().add_filter("Audio", craft_audio::DECODE_EXTENSIONS).pick_files().unwrap_or_default()
})),
pick_save: Some(Box::new(|purpose: &str, path: &std::path::Path| {
let mut d = match purpose {
"file.bounce_mix" => rfd::FileDialog::new().add_filter("Audio", craft_audio::ENCODE_EXTENSIONS),
+6 -1
View File
@@ -2,8 +2,13 @@
use std::process::{Command, Output};
/// Nextest archives relocate binaries; plain Cargo keeps the compile-time path valid.
fn soundcraft_binary() -> std::ffi::OsString {
std::env::var_os("NEXTEST_BIN_EXE_soundcraft").unwrap_or_else(|| env!("CARGO_BIN_EXE_soundcraft").into())
}
fn soundcraft(args: &[&str], control_port: &str) -> Output {
Command::new(env!("CARGO_BIN_EXE_soundcraft"))
Command::new(soundcraft_binary())
.args(args)
.env("SOUNDCRAFT_NO_PREFS", "1")
.env("SOUNDCRAFT_CONTROL_PORT", control_port)
+2 -1
View File
@@ -10,6 +10,7 @@ repository.workspace = true
[dependencies]
soundcraft-time.workspace = true
craft-audio.workspace = true
craft-scraft-format.workspace = true
soundcraft-midi.workspace = true
soundcraft-dsp.workspace = true
soundcraft-clap-host.workspace = true
@@ -22,7 +23,6 @@ serde.workspace = true
serde_json.workspace = true
thiserror.workspace = true
log.workspace = true
zip.workspace = true
craft-docpath.workspace = true
[target.'cfg(windows)'.dependencies]
@@ -30,6 +30,7 @@ winsafe = { version = "0.0.29", features = ["kernel"] }
[dev-dependencies]
proptest.workspace = true
zip.workspace = true
[lints]
workspace = true
+7
View File
@@ -240,6 +240,7 @@ fn grid_ticks(p: &Value) -> i64 {
"1/1" | "1" | "bar" => TICKS_PER_QUARTER * 4,
"1/2" => TICKS_PER_QUARTER * 2,
"1/4" => TICKS_PER_QUARTER,
"1/4t" => TICKS_PER_QUARTER * 2 / 3,
"1/8" => TICKS_PER_QUARTER / 2,
"1/8t" => TICKS_PER_QUARTER / 3,
"1/16t" => TICKS_PER_QUARTER / 6,
@@ -432,6 +433,12 @@ mod tests {
use crate::demo::demo_engine;
use serde_json::json;
#[test]
fn grid_ticks_accepts_the_quarter_triplet() {
use super::{TICKS_PER_QUARTER, grid_ticks};
assert_eq!(grid_ticks(&json!({"grid": "1/4t"})), TICKS_PER_QUARTER * 2 / 3);
}
#[test]
fn tempo_constant_with_an_empty_range_sets_the_tempo_at_its_start() {
use soundcraft_time::Range;
+16 -4
View File
@@ -114,7 +114,7 @@ pub fn specs() -> Vec<CommandSpec> {
"Assign Send",
[],
None,
"{track?, slot?: 0..9|a..j, bus: name, level_db?: 0 (kept when re-targeting), pre_fader?: false}",
"{track?, slot?: 0..9|a..j, bus: name, level_db?: -144..12, default -144 (silent; kept when re-targeting), pre_fader?: false}",
has_selection,
send
),
@@ -244,6 +244,15 @@ fn volume(e: &mut Engine, p: &Value) -> Result<Value> {
return Err(bad("mix.volume", "`db` or `delta_db` required"));
}
let s = e.session_mut();
// The fader stops at -144 and +12 dB. The reply says when a value was held there: +15 dB used
// to play at +12 with nothing to show it.
let clamped = std::cell::Cell::new(false);
let fader = |db: f32| {
if !(-144.0..=12.0).contains(&db) {
clamped.set(true);
}
db.clamp(-144.0, 12.0)
};
// Tracks already moved as a group sibling of an earlier target must not move again.
let mut done: Vec<TrackId> = Vec::new();
for t in &tracks {
@@ -257,11 +266,14 @@ fn volume(e: &mut Engine, p: &Value) -> Result<Value> {
(None, Some(d)) => cur + d as f32,
_ => cur,
};
let new = if new.is_finite() { new.clamp(-144.0, 12.0) } else { cur };
let new = if new.is_finite() { fader(new) } else { cur };
let d = new - cur;
set_grouped(s, *t, "volume", |tr| tr.mixer.volume_db = if tr.id == *t { new } else { (tr.mixer.volume_db + d).clamp(-144.0, 12.0) });
set_grouped(s, *t, "volume", |tr| tr.mixer.volume_db = if tr.id == *t { new } else { fader(tr.mixer.volume_db + d) });
}
Ok(json!({"tracks": tracks.len()}))
// Every requested track at its final volume, also one that moved as a group sibling of an
// earlier target and was skipped above.
let volumes: Vec<Value> = tracks.iter().filter_map(|t| s.track(*t)).map(|tr| json!({"track": tr.id.0, "db": tr.mixer.volume_db})).collect();
Ok(json!({"tracks": tracks.len(), "volumes": volumes, "clamped": clamped.get()}))
}
fn pan(e: &mut Engine, p: &Value) -> Result<Value> {
+34
View File
@@ -263,6 +263,18 @@ fn retargeting_a_send_keeps_its_settings() {
assert_eq!((sl.level_db, sl.pan, sl.mute, sl.pre_fader), (-9.0, 0.5, true, true));
}
#[test]
fn a_new_send_starts_at_the_documented_level() {
let mut e = Engine::default();
e.execute("track.new", &json!({"count": 1, "name": "Vox", "format": "mono"})).unwrap();
e.execute("mix.new_bus", &json!({"name": "Rev"})).unwrap();
e.execute("mix.send", &json!({"track": "Vox", "bus": "Rev"})).unwrap();
let level = e.session().tracks[0].mixer.sends[0].as_ref().unwrap().level_db;
assert_eq!(level, -144.0);
let doc = crate::cmd::command_specs().iter().find(|c| c.id == "mix.send").unwrap().params;
assert!(doc.contains("level_db?: -144..12, default -144 (silent;"), "{doc}");
}
fn grouped_demo() -> Engine {
let mut e = crate::demo::demo_engine();
e.execute("mix.volume", &json!({"tracks": [3], "db": 0.0})).unwrap();
@@ -294,6 +306,28 @@ fn volume_absolute_on_a_whole_group_keeps_offsets() {
assert_eq!(volumes(&e), [-10.0, -12.0]);
}
/// The fader stops at +12 dB; the reply says what each track got, and when a value was held at a stop.
#[test]
fn volume_held_at_the_fader_stop_is_reported() {
let mut e = grouped_demo();
let r = e.execute("mix.volume", &json!({"tracks": [3], "db": -6.0})).unwrap();
assert_eq!((r["volumes"].clone(), r["clamped"].clone()), (json!([{"track": 3, "db": -6.0}]), json!(false)));
let r = e.execute("mix.volume", &json!({"tracks": [3], "db": 15.0})).unwrap();
assert_eq!((r["volumes"].clone(), r["clamped"].clone()), (json!([{"track": 3, "db": 12.0}]), json!(true)));
assert_eq!(volumes(&e), [12.0, 12.0]);
// A group member held at the stop counts too.
let mut e = grouped_demo();
e.session_mut().track_mut(TrackId(6)).unwrap().mixer.volume_db = 6.0;
let r = e.execute("mix.volume", &json!({"tracks": [3], "db": 10.0})).unwrap();
assert_eq!(r["clamped"], json!(true));
assert_eq!(volumes(&e), [10.0, 12.0]);
// Both grouped targets are listed, each at the value it ended with.
let mut e = grouped_demo();
e.session_mut().track_mut(TrackId(6)).unwrap().mixer.volume_db = -2.0;
let r = e.execute("mix.volume", &json!({"tracks": [3, 6], "db": -10.0})).unwrap();
assert_eq!(r["volumes"], json!([{"track": 3, "db": -10.0}, {"track": 6, "db": -12.0}]));
}
#[test]
fn clear_with_unknown_param_keeps_lanes() {
let (mut e, t) = tone_session(&[(0, -6.0), (100, -3.0)]);
+17 -73
View File
@@ -182,46 +182,20 @@ impl Drop for SessionLimit {
/// Refuses the session `name` of `len` bytes when it is larger than [`session_limit`]: too large
/// to open, or, when `saving`, to save.
pub(crate) fn session_size(name: impl std::fmt::Display, len: u64, saving: bool) -> Result<()> {
let limit = session_limit();
if len <= limit {
return Ok(());
}
let most = if limit >= 1 << 20 { format!("{} MiB", limit >> 20) } else { format!("{limit} bytes") };
Err(EngineError::Io(if saving {
format!("{name} is too large to save: over {most}, more than SoundCraft opens")
} else {
format!("{name} is too large to open (over {most})")
}))
craft_scraft_format::check_session_size(&name.to_string(), len, session_limit(), saving).map_err(|error| EngineError::Io(error.to_string()))
}
/// [`read_text`] for a session file: refused, unread, past [`session_limit`].
pub(crate) fn read_session_text(path: impl AsRef<Path>) -> Result<String> {
let path = path.as_ref();
utf8(path, read_file_within(path, true)?)
craft_scraft_format::read_session_data(&path.display().to_string(), read_file_within(path, true)?, session_limit())
.map_err(|error| EngineError::Io(error.to_string()))
}
/// A bare `.scraft` handed over as bytes (the browser) as text: refused, uncopied, past
/// [`session_limit`].
pub(crate) fn session_bytes_text(name: &str, bytes: &[u8]) -> Result<String> {
session_size(name, bytes.len() as u64, false)?;
String::from_utf8(bytes.to_vec()).map_err(|_| EngineError::Io(format!("{name}: not a SoundCraft session")))
}
/// How many bytes [`Session::to_json`] makes of `s`, counted without keeping them.
fn json_len(s: &Session) -> u64 {
struct Count(u64);
impl std::io::Write for Count {
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
self.0 = self.0.saturating_add(buf.len() as u64);
Ok(buf.len())
}
fn flush(&mut self) -> std::io::Result<()> {
Ok(())
}
}
let mut n = Count(0);
// A session that cannot be written counts as nothing here: `to_json` reports that error.
serde_json::to_writer_pretty(&mut n, s).map_or(0, |()| n.0)
craft_scraft_format::read_session_bytes(name, bytes, session_limit()).map_err(|error| EngineError::Io(error.to_string()))
}
/// Is there a file at `path` the automation read root lets us see? A path outside the root is
@@ -795,11 +769,12 @@ fn write_session(session: &Session, path: &str, overwrite: bool) -> Result<(Sess
// Before any audio is written: a session too large to open again is not saved (#892). With no
// audio to write, the text checked is the text saved.
let checked = if pending.is_empty() {
let text = s.to_json().map_err(|err| EngineError::Io(err.to_string()))?;
session_size(p.display(), text.len() as u64, true)?;
let label = p.display().to_string();
let text = craft_scraft_format::write_session_named(&s, &label, session_limit()).map_err(|error| EngineError::Io(error.to_string()))?;
Some(text)
} else {
session_size(p.display(), json_len(&s), true)?;
let size = craft_scraft_format::session_json_len(&s).map_err(|error| EngineError::Io(error.to_string()))?;
session_size(p.display(), size, true)?;
None
};
let mut written = 0;
@@ -820,7 +795,10 @@ fn write_session(session: &Session, path: &str, overwrite: bool) -> Result<(Sess
}
let text = match checked {
Some(text) => text,
None => s.to_json().map_err(|err| EngineError::Io(err.to_string()))?,
None => {
let label = p.display().to_string();
craft_scraft_format::write_session_named(&s, &label, session_limit()).map_err(|error| EngineError::Io(error.to_string()))?
}
};
make_dir(&dir)?;
// Atomic-ish write: temp file then rename.
@@ -1107,45 +1085,11 @@ fn write_export_checked(path: &Path, mut bytes: &[u8]) -> std::io::Result<()> {
/// Unpack a bundle: the one `.scraft` at its top level and the files beside it, by relative path.
fn read_bundle(bytes: &[u8], limits: BundleLimits) -> Result<(String, std::collections::HashMap<String, Vec<u8>>)> {
use std::io::Read as _;
let zerr = |err: zip::result::ZipError| EngineError::Io(format!("bundle: {err}"));
let mut z = zip::ZipArchive::new(std::io::Cursor::new(bytes)).map_err(zerr)?;
if z.len() > limits.entries {
return Err(EngineError::Io("bundle: too many files".into()));
}
// Entries are stored, so the files we keep can only outgrow the bundle when several entries
// point at the same bytes: a small bundle naming one payload thousands of times would be
// copied out once per name.
if z.has_overlapping_files().map_err(zerr)? {
return Err(EngineError::Io("bundle: files share data".into()));
}
let (mut text, mut files, mut total) = (None, std::collections::HashMap::new(), 0u64);
for i in 0..z.len() {
let mut f = z.by_index(i).map_err(zerr)?;
if f.is_dir() {
continue;
}
let rel = f.name().map_err(zerr)?.to_string();
let size = f.size();
total = total.saturating_add(size);
if total > limits.bytes {
return Err(EngineError::Io("bundle: too large".into()));
}
let is_session = !rel.contains('/') && Path::new(&rel).extension().and_then(OsStr::to_str) == Some(soundcraft_model::SESSION_EXTENSION);
if is_session && text.is_none() {
session_size(format_args!("bundle: {rel}"), size, false)?;
}
let mut data = Vec::new();
// The declared size is hostile too: never read past it.
f.by_ref().take(size).read_to_end(&mut data).map_err(|err| EngineError::Io(format!("bundle: {rel}: {err}")))?;
if is_session && text.is_none() {
text = Some(String::from_utf8(data).map_err(|_| EngineError::Io(format!("bundle: {rel} is not text")))?);
} else {
files.insert(rel, data);
}
}
let text = text.ok_or_else(|| EngineError::Io("bundle: no .scraft session inside".into()))?;
Ok((text, files))
craft_scraft_format::read_bundle(
bytes,
craft_scraft_format::BundleLimits { entries: limits.entries, bytes: limits.bytes, session_bytes: session_limit() },
)
.map_err(|error| EngineError::Io(error.to_string()))
}
/// The record-enabled audio tracks, each with the input channels it records (those its input
+46 -50
View File
@@ -1,13 +1,13 @@
use super::render::sanitize_name;
use super::session::decode_source;
use super::{Arc, EncodeOptions, Engine, EngineError, FileFormat, OsStr, Path, Result, Session, SourceAudio, read_bundle, write_file};
use super::{Arc, Engine, EngineError, OsStr, Path, Result, Session, SourceAudio, read_bundle, session_limit, write_file};
// ─── Hosts without a filesystem (the browser) ─────────────────────────────────────────────────
/// Extension of a session bundle: one zip file holding the `.scraft` session and its
/// `Audio Files/`, the same layout as a saved session folder. The browser has no folders, so it
/// saves and opens sessions as bundles.
pub const BUNDLE_EXTENSION: &str = "scraftz";
pub const BUNDLE_EXTENSION: &str = craft_scraft_format::BUNDLE_EXTENSION;
/// Most entries and bytes a bundle may unpack to (bundles are hostile input). The writer keeps to
/// them too, so a bundle it saves always opens again (#433).
@@ -28,54 +28,8 @@ pub fn session_bundle(session: &Session) -> Result<(Vec<u8>, Session)> {
/// [`session_bundle`], refused when it would hold more than `limits` allows.
fn session_bundle_within(session: &Session, limits: BundleLimits) -> Result<(Vec<u8>, Session)> {
use std::io::Write as _;
let zerr = |err: zip::result::ZipError| EngineError::Io(format!("bundle: {err}"));
let ioerr = |err: std::io::Error| EngineError::Io(format!("bundle: {err}"));
let mut s = session.clone();
let mut w = zip::ZipWriter::new(std::io::Cursor::new(Vec::new()));
let opts = zip::write::SimpleFileOptions::default().compression_method(zip::CompressionMethod::Stored).large_file(true);
let mut taken = std::collections::HashSet::new();
let (mut entries, mut total) = (0usize, 0u64);
// Counted as the reader counts them: files, and the bytes they unpack to.
let mut fits = |len: usize| {
entries += 1;
total = total.saturating_add(len as u64);
if entries > limits.entries || total > limits.bytes {
return Err(EngineError::Io(format!(
"bundle: too large to save (a bundle holds at most {} files and {} MiB, or it can't be opened again)",
limits.entries,
limits.bytes >> 20
)));
}
Ok(())
};
for src in &mut s.sources {
let Some(audio) = s.pool.get(src.id) else { continue };
let base = sanitize_name(&src.name);
let mut target = format!("Audio Files/{base}.wav");
let mut k = 1;
while taken.contains(&target) && k <= 9_999 {
target = format!("Audio Files/{base}_{k:02}.wav");
k += 1;
}
let opts_wav = EncodeOptions::new(craft_audio::EncodeFormat::Wav).with_bit_depth(craft_audio::BitDepth::Float32);
let bytes = craft_audio::encode(&audio.buffer, &opts_wav).map_err(|err| EngineError::Io(err.to_string()))?;
fits(bytes.len())?;
w.start_file(target.as_str(), opts).map_err(zerr)?;
w.write_all(&bytes).map_err(ioerr)?;
taken.insert(target.clone());
src.path = target;
src.unsaved = false;
src.format = FileFormat::Wav;
}
let text = s.to_json().map_err(|err| EngineError::Io(err.to_string()))?;
// Its session too: one the reader would refuse is not saved (#892).
super::session_size("the session", text.len() as u64, true)?;
fits(text.len())?;
w.start_file(format!("{}.{}", sanitize_name(&s.name), soundcraft_model::SESSION_EXTENSION), opts).map_err(zerr)?;
w.write_all(text.as_bytes()).map_err(ioerr)?;
let bytes = w.finish().map_err(zerr)?.into_inner();
Ok((bytes, s))
let shared = craft_scraft_format::BundleLimits { entries: limits.entries, bytes: limits.bytes, session_bytes: session_limit() };
craft_scraft_format::write_bundle(session, shared).map_err(|error| EngineError::Io(error.to_string()))
}
/// Save as a bundle named after `path`'s file name, delivered through the host ([`write_file`]).
@@ -200,6 +154,48 @@ mod bundle_tests {
assert_eq!(r.path.as_deref(), Some("Midnight Groove.scraftz"));
}
#[test]
fn legacy_zip64_bundle_opens_with_shared_reader() {
use craft_audio::{AudioBuffer, EncodeFormat, EncodeOptions, encode};
use soundcraft_model::{FileFormat, Source, SourceId};
use std::io::Write as _;
let id = SourceId(7);
let samples = [0.125, -0.75, 1.25, -1.5];
let mut audio = AudioBuffer::new(48_000, 1, samples.len());
audio.channels[0].copy_from_slice(&samples);
let mut session = Session::default();
session.sources.push(Source {
id,
name: "Legacy Lead".into(),
path: "Audio Files/Legacy Lead.wav".into(),
channels: 1,
frames: samples.len() as u64,
sample_rate: 48_000,
format: FileFormat::Wav,
time_reference: 0,
unsaved: false,
});
session.pool.insert(id, Arc::new(SourceAudio::new(audio)));
let wav =
encode(&session.pool.get(id).unwrap().buffer, &EncodeOptions::new(EncodeFormat::Wav).with_bit_depth(craft_audio::BitDepth::Float32))
.unwrap();
let mut zip = zip::ZipWriter::new(std::io::Cursor::new(Vec::new()));
let opts = zip::write::SimpleFileOptions::default().compression_method(zip::CompressionMethod::Stored).large_file(true);
zip.start_file("Audio Files/Legacy Lead.wav", opts).unwrap();
zip.write_all(&wav).unwrap();
zip.start_file("Legacy.scraft", opts).unwrap();
zip.write_all(session.to_json().unwrap().as_bytes()).unwrap();
let legacy = zip.finish().unwrap().into_inner();
let mut opened = Engine::default();
let missing = open_session_bytes(&mut opened, "Legacy.scraftz", &legacy).unwrap();
assert!(missing.is_empty(), "{missing:?}");
assert_eq!(opened.session().pool.get(id).unwrap().buffer.channels[0], samples);
}
#[test]
fn sources_sharing_one_file_all_load_from_a_bundle() {
use std::io::Write as _;
+9 -3
View File
@@ -206,8 +206,8 @@ pub fn load_sampler_bytes(e: &mut Engine, track: TrackId, name: &str, bytes: &[u
/// Resolve each movie referenced by the session file `session` ([`stored_path`]): keep the
/// resolved path when its file exists, else look for a file of the same name next to the session
/// (moved projects) and relink. A movie whose stored path is refused and has no such copy is
/// marked offline. Returns the movies still missing.
/// (moved projects) and relink. A movie whose stored path is refused, or resolves to a file that
/// is not there, and has no such copy is marked offline. Returns the movies still missing.
pub fn relink_videos(s: &mut Session, session: &Path) -> Vec<String> {
let session_dir = session.parent().unwrap_or(Path::new(""));
let mut missing = Vec::new();
@@ -228,7 +228,13 @@ pub fn relink_videos(s: &mut Session, session: &Path) -> Vec<String> {
.find(|c| !name.as_os_str().is_empty() && file_found(c));
match (near, p) {
(Some(c), _) => v.path = c.to_string_lossy().into_owned(),
(None, Ok(p)) => missing.push(format!("{} (video)", p.display())),
// The movie is gone: mark it offline, so the Video track never opens the stored
// string (which the working directory would resolve). The string itself is kept as
// stored, so saving does not swap a path relative to the session for this folder's.
(None, Ok(p)) => {
missing.push(format!("{} (video)", p.display()));
v.offline = Some("file not found".into());
}
(None, Err(why)) => {
missing.push(format!("{} (video): {why}", v.path));
v.offline = Some(why);
+57 -14
View File
@@ -8,8 +8,39 @@ pub(super) const MAX_MIDI_TEMPOS: usize = 10_000;
pub(super) const MAX_MIDI_TRACKS: usize = 512;
/// Import a Standard MIDI File as MIDI tracks (one per SMF track).
///
/// The file is checked in full before the session changes, so a refused file (too many tempo or
/// meter changes or tracks, a track longer than a day) leaves the session as it was: callers that
/// keep their own undo step, like the web shell, never see a half-applied import.
///
/// A sound file is read strictly. A damaged one is read leniently, as other MIDI readers do: with
/// stray bytes after the last track (#1528), a track length its End of Track contradicts or a
/// track cut short, what can be read is imported and what could not is logged. A file past the
/// reader's event or track limit is still refused, also when other damage comes first: read
/// leniently it would lose the rest without saying so. Something that is not a Standard MIDI File
/// is refused.
pub fn import_midi_bytes(e: &mut Engine, bytes: &[u8], at: Samples, tempo_map: bool) -> Result<Vec<TrackId>> {
let smf = soundcraft_midi::read_smf(bytes).map_err(|err| EngineError::Io(err.to_string()))?;
let smf = match soundcraft_midi::read_smf(bytes) {
Ok(smf) => smf,
Err(err @ (soundcraft_midi::MidiError::TooManyEvents(_) | soundcraft_midi::MidiError::TooManyTracks(_))) => {
return Err(EngineError::Io(err.to_string()));
}
Err(_) => {
let read = soundcraft_midi::read_smf_lenient(bytes).map_err(|err| EngineError::Io(err.to_string()))?;
// The strict reader stops at the first damage, so a limit further on shows only here.
if read.over_limit {
return Err(EngineError::Io(format!(
"MIDI file: more than {} events or {} tracks",
soundcraft_midi::MAX_EVENTS,
soundcraft_midi::MAX_TRACKS
)));
}
for warning in &read.warnings {
log::warn!("MIDI import: {warning}");
}
read.smf
}
};
if tempo_map {
// Each meter is placed by walking the ones before it.
if smf.meters.len() > MAX_MIDI_METERS {
@@ -23,24 +54,43 @@ pub fn import_midi_bytes(e: &mut Engine, bytes: &[u8], at: Samples, tempo_map: b
if tracks_to_import > MAX_MIDI_TRACKS {
return Err(EngineError::Io(format!("MIDI file: more than {MAX_MIDI_TRACKS} tracks")));
}
let s = e.session_mut();
// Phase 1, read only: the tempo map the clips will be placed with, built apart from the
// session. `session_mut` would already count as an edit (revision, unsaved mark).
let s = e.session();
let mut tempo = s.tempo.clone();
if tempo_map {
for (tick, bpm) in &smf.tempos {
if s.tempo.tempo_at_tick(*tick) == *bpm {
if tempo.tempo_at_tick(*tick) == *bpm {
continue;
}
let _ = s.tempo.set_tempo(*tick, *bpm);
let _ = tempo.set_tempo(*tick, *bpm);
}
for (tick, n, d) in &smf.meters {
let _ = s.tempo.set_meter(*tick, u32::from(*n), u32::from(*d));
let _ = tempo.set_meter(*tick, u32::from(*n), u32::from(*d));
}
}
let base_tick = s.tempo.samples_to_ticks(at, s.sample_rate);
let mut out = Vec::new();
let base_tick = tempo.samples_to_ticks(at, s.sample_rate);
let mut clips = Vec::new();
for (i, tr) in smf.tracks.iter().enumerate() {
if tr.sequence.notes.is_empty() && tr.sequence.ctrls.is_empty() {
continue;
}
// A controller on the clip's last tick would not play, so the clip runs one tick past the last one.
let past_ctrls = tr.sequence.ctrls.iter().map(|c| c.tick.saturating_add(1)).max().unwrap_or(0);
let end_tick = base_tick.saturating_add(tr.sequence.end_tick().max(past_ctrls).max(1));
let len = tempo.tick_to_samples(end_tick, s.sample_rate).saturating_sub(at);
if len > s.sample_rate.samples(86_400.0) {
return Err(EngineError::Io(format!("MIDI track {}: longer than 24 hours", i + 1)));
}
clips.push((i, tr, len));
}
// Phase 2: nothing can fail from here on.
let s = e.session_mut();
if tempo_map {
s.tempo = tempo;
}
let mut out = Vec::new();
for (i, tr, len) in clips {
let mut name = if tr.name.trim().is_empty() { format!("MIDI {}", i + 1) } else { tr.name.clone() };
soundcraft_model::truncate_chars(&mut name, soundcraft_model::MAX_NAME_CHARS);
let id = s.add_track(TrackKind::Instrument, ChannelFormat::Stereo, Some(&name));
@@ -48,13 +98,6 @@ pub fn import_midi_bytes(e: &mut Engine, bytes: &[u8], at: Samples, tempo_map: b
if let Some(t) = s.track_mut(id) {
t.instrument = Some(soundcraft_model::Insert::new(if drums { "drum_synth" } else { "subtractive_synth" }));
}
// A controller on the clip's last tick would not play, so the clip runs one tick past the last one.
let past_ctrls = tr.sequence.ctrls.iter().map(|c| c.tick.saturating_add(1)).max().unwrap_or(0);
let end_tick = base_tick.saturating_add(tr.sequence.end_tick().max(past_ctrls).max(1));
let len = s.tempo.tick_to_samples(end_tick, s.sample_rate).saturating_sub(at);
if len > s.sample_rate.samples(86_400.0) {
return Err(EngineError::Io(format!("MIDI track {}: longer than 24 hours", i + 1)));
}
let cid = s.new_clip_id();
crate::edit::place_clip(s, id, Clip::midi(cid, name, at, len.max(1), tr.sequence.clone()));
out.push(id);
+92
View File
@@ -177,6 +177,71 @@ mod name_tests {
}
}
#[test]
fn a_midi_file_with_stray_bytes_after_its_last_track_is_imported() {
// #1528: a complete format-0 file followed by two line feeds, too short to be a chunk.
let note = soundcraft_midi::Note { pitch: 60, velocity: 100, release_velocity: 64, channel: 0, start: 0, length: 960 };
let sequence = soundcraft_midi::Sequence { notes: vec![note], ..Default::default() };
let smf = soundcraft_midi::Smf { format: 0, tracks: vec![soundcraft_midi::SmfTrack { name: String::new(), sequence }], ..Default::default() };
let mut bytes = soundcraft_midi::write_smf(&smf).unwrap();
bytes.extend_from_slice(b"\n\n");
let mut e = Engine::default();
let ids = import_midi_bytes(&mut e, &bytes, 0, true).unwrap();
assert_eq!(ids.len(), 1);
match &e.session().track(ids[0]).unwrap().clips()[0].content {
ClipContent::Midi { sequence } => assert_eq!(sequence.notes, vec![note]),
_ => panic!("expected a MIDI clip"),
}
// Something that is no MIDI file is still refused, and leaves the session as it was.
let mut e = Engine::default();
assert!(import_midi_bytes(&mut e, b"RIFF\0\0\0\0WAVE", 0, true).is_err());
assert!(e.session().tracks.is_empty());
}
#[test]
fn a_midi_file_past_the_track_limit_is_refused_even_with_stray_bytes() {
// Read leniently, the tracks past the limit would be left out without a word: the note in
// the last one would be lost. The file stays refused, and the session as it was.
let tracks = soundcraft_midi::MAX_TRACKS + 1;
let mut bytes = b"MThd\0\0\0\x06\0\x01".to_vec();
bytes.extend_from_slice(&u16::try_from(tracks).unwrap().to_be_bytes());
bytes.extend_from_slice(&960u16.to_be_bytes());
for _ in 1..tracks {
bytes.extend_from_slice(b"MTrk\0\0\0\x04\0\xFF\x2F\0");
}
// A note on, a quarter note later its note off, then End of Track.
bytes.extend_from_slice(b"MTrk\0\0\0\x0D\0\x90\x3C\x64\x87\x40\x80\x3C\x40\0\xFF\x2F\0");
bytes.extend_from_slice(b"\n\n");
let mut e = Engine::default();
let err = import_midi_bytes(&mut e, &bytes, 0, true).unwrap_err();
assert!(err.to_string().contains("too many tracks"), "{err}");
assert!(e.session().tracks.is_empty());
}
#[test]
fn a_midi_file_past_the_event_limit_is_refused_after_a_damaged_track() {
// The strict reader stops at track 1's damage, before track 2 reaches the event limit; read
// leniently, track 2's note after the limit (pitch 72) would be lost without a word.
let mut damaged = b"\0\x3C\x40".to_vec(); // a data byte with no status before it
damaged.extend_from_slice(b"\0\xFF\x2F\0");
let mut long = b"\0\x90\x3C\x64".to_vec();
for _ in 0..soundcraft_midi::MAX_EVENTS {
long.extend_from_slice(b"\0\x3C\0"); // running status: note off
}
long.extend_from_slice(b"\0\x90\x48\x64\x87\x40\x80\x48\x40\0\xFF\x2F\0");
let mut bytes = b"MThd\0\0\0\x06\0\x01\0\x02\x03\xC0".to_vec();
for body in [&damaged, &long] {
bytes.extend_from_slice(b"MTrk");
bytes.extend_from_slice(&u32::try_from(body.len()).unwrap().to_be_bytes());
bytes.extend_from_slice(body);
}
assert!(!matches!(soundcraft_midi::read_smf(&bytes), Err(soundcraft_midi::MidiError::TooManyEvents(_)) | Ok(_)));
let mut e = Engine::default();
let err = import_midi_bytes(&mut e, &bytes, 0, true).unwrap_err();
assert!(err.to_string().contains("events"), "{err}");
assert!(e.session().tracks.is_empty());
}
#[test]
fn midi_longer_than_a_day_is_refused() {
let held = soundcraft_midi::Note { pitch: 60, velocity: 100, release_velocity: 64, channel: 0, start: 0, length: (1 << 28) - 1 };
@@ -186,6 +251,33 @@ mod name_tests {
assert!(err.to_string().contains("longer than 24 hours"), "{err}");
}
#[test]
fn a_refused_midi_file_leaves_the_session_untouched() {
// Track 1 fits; track 2 is longer than a day. Before the import is refused, nothing of
// track 1, nor the file's tempo and meter changes, may have reached the session.
let note = |length| soundcraft_midi::Note { pitch: 60, velocity: 100, release_velocity: 64, channel: 0, start: 0, length };
let short = soundcraft_midi::Sequence { notes: vec![note(960)], ..Default::default() };
let held = soundcraft_midi::Sequence { notes: vec![note((1 << 28) - 1)], ..Default::default() };
let smf = soundcraft_midi::Smf {
tracks: vec![
soundcraft_midi::SmfTrack { name: "fits".into(), sequence: short },
soundcraft_midi::SmfTrack { name: "too long".into(), sequence: held },
],
tempos: vec![(0, 91.0)],
meters: vec![(0, 7, 8)],
..Default::default()
};
let mut e = Engine::default();
let before = e.session().clone();
let (revision, dirty) = (e.revision, e.is_dirty());
let err = import_midi_bytes(&mut e, &soundcraft_midi::write_smf(&smf).unwrap(), 0, true).unwrap_err();
assert!(err.to_string().contains("MIDI track 2: longer than 24 hours"), "{err}");
assert_eq!(e.session().tracks.len(), before.tracks.len());
assert_eq!(e.session().tempo, before.tempo);
assert_eq!(*e.session(), before);
assert_eq!((e.revision, e.is_dirty()), (revision, dirty), "a refused file is not an edit");
}
#[test]
fn midi_with_more_meter_changes_than_scores_have_is_refused() {
let note = soundcraft_midi::Note { pitch: 60, velocity: 100, release_velocity: 64, channel: 0, start: 0, length: 960 };
+93
View File
@@ -520,6 +520,99 @@ mod open_tests {
assert!(r.session().pool.get(r.session().sources[0].id).is_some());
let _ = std::fs::remove_dir_all(&dir);
}
/// Every outcome of [`relink_videos`] for a movie: its file is there; it is gone but a file of
/// the same name is next to the session or in its `Video Files` (relinked); it is gone with
/// no copy (offline, path kept); its stored path is refused, with or without such a copy.
#[test]
fn each_movie_is_kept_relinked_or_offline() {
let dir = std::env::temp_dir().join(format!("soundcraft-relink-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&dir);
std::fs::create_dir_all(dir.join("Movies")).unwrap();
std::fs::create_dir_all(dir.join("Video Files")).unwrap();
for f in ["Movies/here.mov", "near.mov", "Video Files/filed.mov", "remote_copy.mov"] {
std::fs::write(dir.join(f), b"movie").unwrap();
}
let session = dir.join("Song.scraft");
let mut s = Session::default();
let stored = [
"Movies/here.mov",
"Old/near.mov",
"Old/filed.mov",
"Old/gone.mov",
"file://192.0.2.1/s/remote.mov",
"file://192.0.2.1/s/remote_copy.mov",
];
for path in stored {
let id = SourceId(s.alloc());
s.videos.push(soundcraft_model::VideoSource {
id,
name: path.into(),
path: path.into(),
width: 0,
height: 0,
frame_rate: 0.0,
duration: 0.0,
codec: String::new(),
offline: None,
});
}
let missing = relink_videos(&mut s, &session);
let got: Vec<(String, Option<String>)> = s.videos.iter().map(|v| (v.path.clone(), v.offline.clone())).collect();
let at = |f: &str| dir.join(f).to_string_lossy().into_owned();
assert_eq!(got[0], (stored_path("Movies/here.mov", &session).unwrap().to_string_lossy().into_owned(), None));
assert_eq!(got[1], (at("near.mov"), None));
assert_eq!(got[2], (at("Video Files/filed.mov"), None));
assert_eq!(got[3], ("Old/gone.mov".into(), Some("file not found".into())));
assert_eq!(got[4].0, stored[4]);
assert!(got[4].1.as_deref().is_some_and(|why| why.starts_with("not opened:")), "{:?}", got[4]);
assert_eq!(got[5], (at("remote_copy.mov"), None));
assert_eq!(missing.len(), 2, "{missing:?}");
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
fn a_missing_movie_is_offline_and_keeps_its_stored_path() {
// The stored path is relative to the session and names no file. The movie is offline, so
// the Video track never opens the stored string (the working directory would resolve
// it), and the string is kept: saved into another folder, the session finds the movie
// once it is back there under the same relative path.
let dir = std::env::temp_dir().join(format!("soundcraft-missing-movie-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&dir);
let (first, moved) = (dir.join("a"), dir.join("b"));
std::fs::create_dir_all(&first).unwrap();
std::fs::create_dir_all(moved.join("Movies")).unwrap();
let mut s = Session::default();
let id = SourceId(s.alloc());
s.videos.push(soundcraft_model::VideoSource {
id,
name: "gone".into(),
path: "Movies/gone.mov".into(),
width: 0,
height: 0,
frame_rate: 0.0,
duration: 0.0,
codec: String::new(),
offline: None,
});
let session = first.join("Song.scraft");
std::fs::write(&session, s.to_json().unwrap()).unwrap();
let mut e = Engine::default();
let missing = open_session(&mut e, &session).unwrap();
assert_eq!(missing, vec![format!("{} (video)", stored_path("Movies/gone.mov", &session).unwrap().display())]);
let v = &e.session().videos[0];
assert_eq!((v.path.as_str(), v.offline.as_deref()), ("Movies/gone.mov", Some("file not found")));
// Saved next to the movie's new home, the session opens it.
let session = moved.join("Song.scraft");
save_session(&mut e, session.to_str().unwrap()).unwrap();
std::fs::write(moved.join("Movies").join("gone.mov"), b"movie").unwrap();
let mut r = Engine::default();
assert!(open_session(&mut r, &session).unwrap().is_empty());
let v = &r.session().videos[0];
assert_eq!(v.path, stored_path("Movies/gone.mov", &session).unwrap().to_string_lossy());
assert!(v.offline.is_none());
let _ = std::fs::remove_dir_all(&dir);
}
}
#[cfg(all(test, unix))]
+1 -3
View File
@@ -8,12 +8,10 @@ rust-version.workspace = true
repository.workspace = true
[dependencies]
serde.workspace = true
thiserror.workspace = true
craft-midi.workspace = true
[dev-dependencies]
proptest.workspace = true
serde_json.workspace = true
[lints]
workspace = true
+7 -34
View File
@@ -1,10 +1,10 @@
//! SoundCraft MIDI.
//!
//! - [`Note`], [`CtrlEvent`] and [`Sequence`]: the MIDI content of a region / track, in ticks at
//! [`PPQ`] (960) ticks per quarter note.
//! - [`read_smf`] / [`write_smf`]: Standard MIDI File import (format 0/1, format 2 best-effort) and
//! export (format 1, 960 PPQ). The reader treats its input as hostile: every length is checked,
//! variable-length quantities are bounded and event counts are capped.
//! [`PPQ`] (960) ticks per quarter note; [`read_smf`] / [`write_smf`]: Standard MIDI File import
//! and export, and [`read_smf_lenient`] for files that are slightly damaged. These come from
//! `craft-midi` in craft-libs, shared with the other Craft apps, and are re-exported so the rest
//! of SoundCraft names them from here.
//! - [`ops`]: MIDI editing operations (quantize, transpose, velocity, duration, split).
//! - [`note_name`], [`gm_drum_name`], [`gm_program_name`]: display names.
#![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)]
@@ -12,36 +12,9 @@
mod names;
pub mod ops;
mod rng;
mod sequence;
mod smf;
pub use craft_midi::{
CtrlEvent, CtrlKind, LenientRead, MAX_EVENTS, MAX_TRACKS, MidiError, Note, PPQ, Sequence, Smf, SmfTrack, read_smf, read_smf_lenient, write_smf,
};
pub use names::{gm_drum_name, gm_program_name, note_name};
pub use rng::XorShift;
pub use sequence::{CtrlEvent, CtrlKind, Note, Sequence};
pub use smf::{MAX_EVENTS, MAX_TRACKS, Smf, SmfTrack, read_smf, write_smf};
/// Ticks per quarter note used throughout the model.
pub const PPQ: u32 = 960;
/// Errors from reading or writing MIDI data.
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
pub enum MidiError {
#[error("not a Standard MIDI File (missing MThd header)")]
NotSmf,
#[error("unexpected end of data at byte {0}")]
Truncated(usize),
#[error("variable-length quantity longer than 4 bytes at byte {0}")]
VlqTooLong(usize),
#[error("chunk length {len} at byte {offset} exceeds the remaining data")]
BadChunkLength { offset: usize, len: u64 },
#[error("invalid MIDI data at byte {offset}: {reason}")]
Invalid { offset: usize, reason: &'static str },
#[error("unsupported MIDI file: {0}")]
Unsupported(&'static str),
#[error("too many events (limit {0})")]
TooManyEvents(usize),
#[error("too many tracks (limit {0})")]
TooManyTracks(usize),
#[error("cannot write MIDI file: {0}")]
Write(String),
}
-141
View File
@@ -1,141 +0,0 @@
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::collections::hash_map::Entry;
/// A MIDI note. Times are in model ticks ([`crate::PPQ`] per quarter note).
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
pub struct Note {
/// MIDI pitch 0..=127 (60 = middle C, shown as "C3").
pub pitch: u8,
/// Note-on velocity 1..=127.
pub velocity: u8,
/// Note-off (release) velocity 0..=127; 64 when the source had none.
pub release_velocity: u8,
/// MIDI channel 0..=15.
pub channel: u8,
/// Start in ticks.
pub start: i64,
/// Length in ticks; always > 0.
pub length: i64,
}
impl Note {
/// Tick at which the note ends (`start + length`, saturating).
pub fn end(&self) -> i64 {
self.start.saturating_add(self.length)
}
}
/// Kind of a continuous / channel controller event.
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
pub enum CtrlKind {
/// Control change with the controller number 0..=127. Value 0..=127.
Cc(u8),
/// Pitch bend. Value is centred: -8192..=8191 (0 = no bend).
PitchBend,
/// Channel (mono) aftertouch. Value 0..=127.
ChannelPressure,
/// Polyphonic aftertouch for the given pitch. Value 0..=127.
PolyPressure(u8),
/// Program change. Value is the program number 0..=127.
Program,
}
/// A controller / channel event at a tick.
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
pub struct CtrlEvent {
pub tick: i64,
pub channel: u8,
pub kind: CtrlKind,
pub value: i32,
}
/// The MIDI content of a region or track.
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
pub struct Sequence {
/// Notes, sorted by `(start, pitch)` after [`Sequence::sort`].
pub notes: Vec<Note>,
/// Controller events, sorted by tick after [`Sequence::sort`].
pub ctrls: Vec<CtrlEvent>,
}
fn kind_key(k: CtrlKind) -> (u8, u8) {
match k {
CtrlKind::Cc(n) => (0, n),
CtrlKind::PitchBend => (1, 0),
CtrlKind::ChannelPressure => (2, 0),
CtrlKind::PolyPressure(p) => (3, p),
CtrlKind::Program => (4, 0),
}
}
impl Sequence {
/// Sorts notes by `(start, pitch, channel, length, velocity)` and controller events by tick
/// (stable, so same-tick events keep their order).
pub fn sort(&mut self) {
self.notes.sort_by_key(|a| (a.start, a.pitch, a.channel, a.length, a.velocity));
self.ctrls.sort_by_key(|c| c.tick);
}
/// The last tick covered by any note end or controller event (0 when empty).
pub fn end_tick(&self) -> i64 {
let n = self.notes.iter().map(Note::end).max();
let c = self.ctrls.iter().map(|c| c.tick).max();
match (n, c) {
(Some(a), Some(b)) => a.max(b),
(Some(a), None) | (None, Some(a)) => a,
(None, None) => 0,
}
}
/// Notes whose start lies in the half-open range `[start, end)`.
pub fn notes_in(&self, start: i64, end: i64) -> impl Iterator<Item = &Note> + '_ {
self.notes.iter().filter(move |n| n.start >= start && n.start < end)
}
/// Notes that sound anywhere inside the half-open range `[start, end)`.
pub fn notes_overlapping(&self, start: i64, end: i64) -> impl Iterator<Item = &Note> + '_ {
self.notes.iter().filter(move |n| n.start < end && n.end() > start)
}
/// Removes duplicate notes (same channel, pitch and start; the longest one is kept) and
/// duplicate controller events (same tick, channel and kind; the last one is kept, matching
/// what a synth would end up with). Returns how many events were removed. The sequence is
/// left sorted.
pub fn remove_duplicates(&mut self) -> usize {
let before = self.notes.len() + self.ctrls.len();
// Longest first within each (start, pitch, channel) group, then keep the first.
self.notes.sort_by(|a, b| (a.start, a.pitch, a.channel, b.length).cmp(&(b.start, b.pitch, b.channel, a.length)));
self.notes.dedup_by(|later, kept| later.start == kept.start && later.pitch == kept.pitch && later.channel == kept.channel);
self.ctrls.sort_by_key(|c| c.tick);
let mut kept: Vec<CtrlEvent> = Vec::with_capacity(self.ctrls.len());
// The latest tick and `kept` index of each channel+kind, so a same-tick repeat is found
// without rescanning the tick's events (a tick can hold many thousands of distinct keys).
// An entry from an earlier tick counts as absent; keeping the tick instead of clearing the
// map per tick keeps the work per event constant after a dense tick.
let mut last_at: HashMap<(u8, (u8, u8)), (i64, usize)> = HashMap::new();
for c in self.ctrls.drain(..) {
// Within a tick, replace an earlier event of the same channel+kind.
match last_at.entry((c.channel, kind_key(c.kind))) {
Entry::Occupied(mut e) => {
let (tick, idx) = *e.get();
match kept.get_mut(idx) {
Some(slot) if tick == c.tick => *slot = c,
_ => {
e.insert((c.tick, kept.len()));
kept.push(c);
}
}
}
Entry::Vacant(e) => {
e.insert((c.tick, kept.len()));
kept.push(c);
}
}
}
self.ctrls = kept;
self.sort();
before - (self.notes.len() + self.ctrls.len())
}
}
-635
View File
@@ -1,635 +0,0 @@
//! Standard MIDI File reading and writing (from the public SMF 1.0 specification).
use std::collections::VecDeque;
use crate::{CtrlEvent, CtrlKind, MidiError, Note, PPQ, Sequence};
/// Maximum number of events (across all tracks) [`read_smf`] will parse.
pub const MAX_EVENTS: usize = 5_000_000;
/// Maximum number of track chunks [`read_smf`] will accept.
pub const MAX_TRACKS: usize = 4096;
/// Release velocity used when a note ends with a note-on of velocity 0 (per the spec, a
/// note-on with velocity 0 is a note-off with velocity 64).
const DEFAULT_RELEASE: u8 = 64;
/// One track of a Standard MIDI File.
#[derive(Debug, Clone, Default, PartialEq)]
pub struct SmfTrack {
/// Name from the first Sequence/Track Name meta event (empty if none).
pub name: String,
pub sequence: Sequence,
}
/// A Standard MIDI File in model terms. All ticks are at [`PPQ`] (960) per quarter note.
#[derive(Debug, Clone, Default, PartialEq)]
pub struct Smf {
/// Format of the source file (0, 1 or 2). [`write_smf`] always writes format 1.
pub format: u16,
pub tracks: Vec<SmfTrack>,
/// `(tick, beats per minute)`.
pub tempos: Vec<(i64, f64)>,
/// `(tick, numerator, denominator)`, e.g. `(0, 6, 8)`.
pub meters: Vec<(i64, u8, u8)>,
/// `(tick, text)` from Marker meta events.
pub markers: Vec<(i64, String)>,
/// `(tick, sharps (+) / flats (-) -7..=7, is_minor)`.
pub key_sigs: Vec<(i64, i8, bool)>,
}
// ---------------------------------------------------------------------------------------------
// Reading
// ---------------------------------------------------------------------------------------------
struct Cursor<'a> {
data: &'a [u8],
pos: usize,
/// Absolute offset of `data[0]` in the file, for error messages.
base: usize,
}
impl<'a> Cursor<'a> {
fn at(&self) -> usize {
self.base.saturating_add(self.pos)
}
fn is_empty(&self) -> bool {
self.pos >= self.data.len()
}
fn u8(&mut self) -> Result<u8, MidiError> {
let b = *self.data.get(self.pos).ok_or(MidiError::Truncated(self.at()))?;
self.pos += 1;
Ok(b)
}
fn peek(&self) -> Result<u8, MidiError> {
self.data.get(self.pos).copied().ok_or(MidiError::Truncated(self.at()))
}
fn bytes(&mut self, n: usize) -> Result<&'a [u8], MidiError> {
let end = self.pos.checked_add(n).ok_or(MidiError::Truncated(self.at()))?;
let s = self.data.get(self.pos..end).ok_or(MidiError::Truncated(self.at()))?;
self.pos = end;
Ok(s)
}
fn u16(&mut self) -> Result<u16, MidiError> {
let b = self.bytes(2)?;
Ok(u16::from_be_bytes([b.first().copied().unwrap_or(0), b.get(1).copied().unwrap_or(0)]))
}
fn u32(&mut self) -> Result<u32, MidiError> {
let b = self.bytes(4)?;
let mut a = [0u8; 4];
a.copy_from_slice(b);
Ok(u32::from_be_bytes(a))
}
/// Variable-length quantity: at most 4 bytes (28 bits) per the spec.
fn vlq(&mut self) -> Result<u32, MidiError> {
let start = self.at();
let mut v: u32 = 0;
for _ in 0..4 {
let b = self.u8()?;
v = (v << 7) | u32::from(b & 0x7F);
if b & 0x80 == 0 {
return Ok(v);
}
}
Err(MidiError::VlqTooLong(start))
}
}
/// Converts file ticks to model ticks: `model = file * num / den` (rounded).
#[derive(Clone, Copy)]
struct TickConv {
num: i128,
den: i128,
}
impl TickConv {
fn from_division(division: u16, offset: usize) -> Result<Self, MidiError> {
if division & 0x8000 == 0 {
if division == 0 {
return Err(MidiError::Invalid { offset, reason: "division of 0 ticks per quarter note" });
}
Ok(Self { num: i128::from(PPQ), den: i128::from(division) })
} else {
// SMPTE: high byte is negative frames/sec (two's complement), low byte ticks/frame.
// Approximated at 120 bpm: one quarter note = 0.5 s, so
// model = file / ticks_per_sec * 2 * PPQ.
let [hi, lo] = division.to_be_bytes();
let fps = -i128::from(hi as i8);
let tpf = i128::from(lo);
if !matches!(fps, 24 | 25 | 29 | 30) || tpf == 0 {
return Err(MidiError::Invalid { offset, reason: "invalid SMPTE division" });
}
let two_ppq = 2 * i128::from(PPQ);
if fps == 29 {
// 29.97 drop-frame: ticks/sec = 30000/1001 * tpf
Ok(Self { num: two_ppq * 1001, den: 30000 * tpf })
} else {
Ok(Self { num: two_ppq, den: fps * tpf })
}
}
}
fn convert(&self, file_tick: u64) -> i64 {
let v = (i128::from(file_tick) * self.num + self.den / 2) / self.den;
i64::try_from(v).unwrap_or(i64::MAX)
}
}
struct Globals {
tempos: Vec<(i64, f64)>,
meters: Vec<(i64, u8, u8)>,
markers: Vec<(i64, String)>,
key_sigs: Vec<(i64, i8, bool)>,
}
/// Parses a Standard MIDI File.
///
/// - Formats 0 and 1 are fully supported; format 2 is read best-effort (all tracks share one
/// timeline). Each `MTrk` chunk becomes one [`SmfTrack`]; for format 1 a leading conductor
/// track without notes or controllers is folded into the global tempo/meter/marker lists.
/// - Ticks are converted to [`PPQ`]; an SMPTE division is approximated at 120 bpm.
/// - Running status, note-on velocity 0 as note-off, unmatched note-offs (ignored), overlapping
/// same-pitch notes (matched first-in first-out) and hanging notes (closed at the end of the
/// track) are handled. Zero-length notes get a length of 1 tick.
/// - Unknown chunks and meta events are skipped. Any malformed structure (bad header, truncated
/// chunk, VLQ longer than 4 bytes, data byte without running status, ...) returns an error.
pub fn read_smf(bytes: &[u8]) -> Result<Smf, MidiError> {
let mut cur = Cursor { data: bytes, pos: 0, base: 0 };
if cur.bytes(4).map_err(|_| MidiError::NotSmf)? != b"MThd" {
return Err(MidiError::NotSmf);
}
let header_len = cur.u32()?;
if header_len < 6 {
return Err(MidiError::Invalid { offset: 4, reason: "header chunk shorter than 6 bytes" });
}
let header_len = usize::try_from(header_len).map_err(|_| MidiError::BadChunkLength { offset: 4, len: u64::from(header_len) })?;
let header = cur.bytes(header_len).map_err(|_| MidiError::BadChunkLength { offset: 4, len: u64::try_from(header_len).unwrap_or(u64::MAX) })?;
let mut hc = Cursor { data: header, pos: 0, base: 8 };
let format = hc.u16()?;
let _ntrks = hc.u16()?;
let division = hc.u16()?;
if format > 2 {
return Err(MidiError::Unsupported("SMF format greater than 2"));
}
let conv = TickConv::from_division(division, 12)?;
let mut globals = Globals { tempos: Vec::new(), meters: Vec::new(), markers: Vec::new(), key_sigs: Vec::new() };
let mut tracks = Vec::new();
let mut event_budget = MAX_EVENTS;
while !cur.is_empty() {
let chunk_at = cur.at();
let id = cur.bytes(4)?;
let len = cur.u32()?;
let remaining = bytes.len().saturating_sub(cur.pos);
let len_usize = usize::try_from(len).unwrap_or(usize::MAX);
if len_usize > remaining {
return Err(MidiError::BadChunkLength { offset: chunk_at, len: u64::from(len) });
}
let body_at = cur.at();
let body = cur.bytes(len_usize)?;
if id != b"MTrk" {
continue; // unknown chunk types must be ignored
}
if tracks.len() >= MAX_TRACKS {
return Err(MidiError::TooManyTracks(MAX_TRACKS));
}
let track = read_track(body, body_at, conv, &mut globals, &mut event_budget)?;
tracks.push(track);
}
// A lone track is only a conductor track if it carried global data and has no name; otherwise it is kept.
let globals_present = !(globals.tempos.is_empty() && globals.meters.is_empty() && globals.markers.is_empty() && globals.key_sigs.is_empty());
if format == 1
&& (tracks.len() > 1 || globals_present)
&& tracks.first().is_some_and(|t| t.sequence.notes.is_empty() && t.sequence.ctrls.is_empty() && (tracks.len() > 1 || t.name.is_empty()))
{
tracks.remove(0);
}
// Keep global lists ordered by tick (stable: file order within a tick).
globals.tempos.sort_by_key(|t| t.0);
globals.meters.sort_by_key(|t| t.0);
globals.markers.sort_by_key(|t| t.0);
globals.key_sigs.sort_by_key(|t| t.0);
Ok(Smf { format, tracks, tempos: globals.tempos, meters: globals.meters, markers: globals.markers, key_sigs: globals.key_sigs })
}
/// Open note-ons for one (channel, pitch), oldest first: `(model tick, velocity)`.
type OpenNotes = Vec<VecDeque<(i64, u8)>>;
fn close_note(open: &mut OpenNotes, notes: &mut Vec<Note>, channel: u8, pitch: u8, tick: i64, release: u8) {
let key = usize::from(channel & 0x0F) * 128 + usize::from(pitch & 0x7F);
let Some(stack) = open.get_mut(key) else { return };
if stack.is_empty() {
return; // unmatched note-off: ignored
}
let Some((start, velocity)) = stack.pop_front() else { return };
let length = tick.saturating_sub(start).max(1);
notes.push(Note { pitch, velocity, release_velocity: release, channel, start, length });
}
fn read_track(body: &[u8], base: usize, conv: TickConv, g: &mut Globals, budget: &mut usize) -> Result<SmfTrack, MidiError> {
let mut cur = Cursor { data: body, pos: 0, base };
let mut name: Option<String> = None;
let mut seq = Sequence::default();
let mut open: OpenNotes = vec![VecDeque::new(); 16 * 128];
let mut file_tick: u64 = 0;
let mut tick: i64 = 0;
let mut running: Option<u8> = None;
while !cur.is_empty() {
if *budget == 0 {
return Err(MidiError::TooManyEvents(MAX_EVENTS));
}
*budget -= 1;
let delta = cur.vlq()?;
file_tick = file_tick.saturating_add(u64::from(delta));
tick = conv.convert(file_tick);
let event_at = cur.at();
let first = cur.peek()?;
let status = if first & 0x80 != 0 {
cur.pos += 1;
first
} else {
running.ok_or(MidiError::Invalid { offset: event_at, reason: "data byte without running status" })?
};
match status {
0xFF => {
running = None;
let kind = cur.u8()?;
let len = cur.vlq()?;
let data = cur.bytes(usize::try_from(len).unwrap_or(usize::MAX))?;
match kind {
0x2F => break, // End of Track
0x03 if name.is_none() => {
name = Some(String::from_utf8_lossy(data).into_owned());
}
0x06 => g.markers.push((tick, String::from_utf8_lossy(data).into_owned())),
0x51 => {
if let [a, b, c] = data {
let us = (u32::from(*a) << 16) | (u32::from(*b) << 8) | u32::from(*c);
if us > 0 {
g.tempos.push((tick, 60_000_000.0 / f64::from(us)));
}
}
}
0x58 => {
if let [nn, dd, ..] = data
&& *nn > 0
&& *dd <= 7
{
g.meters.push((tick, *nn, 1u8 << *dd));
}
}
0x59 => {
if let [sf, mi, ..] = data {
let sf = *sf as i8;
if (-7..=7).contains(&sf) {
g.key_sigs.push((tick, sf, *mi == 1));
}
}
}
_ => {}
}
}
0xF0 | 0xF7 => {
running = None;
let len = cur.vlq()?;
cur.bytes(usize::try_from(len).unwrap_or(usize::MAX))?;
}
0x80..=0xEF => {
running = Some(status);
let channel = status & 0x0F;
let d1 = cur.u8()?;
if d1 & 0x80 != 0 {
return Err(MidiError::Invalid { offset: event_at, reason: "status byte where a data byte was expected" });
}
let needs_two = !matches!(status & 0xF0, 0xC0 | 0xD0);
let d2 = if needs_two {
let d = cur.u8()?;
if d & 0x80 != 0 {
return Err(MidiError::Invalid { offset: event_at, reason: "status byte where a data byte was expected" });
}
d
} else {
0
};
match status & 0xF0 {
0x80 => close_note(&mut open, &mut seq.notes, channel, d1, tick, d2),
0x90 if d2 == 0 => close_note(&mut open, &mut seq.notes, channel, d1, tick, DEFAULT_RELEASE),
0x90 => {
if let Some(stack) = open.get_mut(usize::from(channel) * 128 + usize::from(d1)) {
stack.push_back((tick, d2));
}
}
0xA0 => seq.ctrls.push(CtrlEvent { tick, channel, kind: CtrlKind::PolyPressure(d1), value: i32::from(d2) }),
0xB0 => seq.ctrls.push(CtrlEvent { tick, channel, kind: CtrlKind::Cc(d1), value: i32::from(d2) }),
0xC0 => seq.ctrls.push(CtrlEvent { tick, channel, kind: CtrlKind::Program, value: i32::from(d1) }),
0xD0 => seq.ctrls.push(CtrlEvent { tick, channel, kind: CtrlKind::ChannelPressure, value: i32::from(d1) }),
_ => {
// 0xE0 pitch bend: LSB then MSB, centred on 8192.
let v = (i32::from(d2) << 7 | i32::from(d1)) - 8192;
seq.ctrls.push(CtrlEvent { tick, channel, kind: CtrlKind::PitchBend, value: v });
}
}
}
_ => return Err(MidiError::Invalid { offset: event_at, reason: "system real-time/common status in a track" }),
}
}
// Close hanging notes at the end of the track.
for (key, stack) in open.iter_mut().enumerate() {
let channel = u8::try_from(key / 128).unwrap_or(0);
let pitch = u8::try_from(key % 128).unwrap_or(0);
for (start, velocity) in stack.drain(..) {
let length = tick.saturating_sub(start).max(1);
seq.notes.push(Note { pitch, velocity, release_velocity: DEFAULT_RELEASE, channel, start, length });
}
}
seq.sort();
Ok(SmfTrack { name: name.unwrap_or_default(), sequence: seq })
}
// ---------------------------------------------------------------------------------------------
// Writing
// ---------------------------------------------------------------------------------------------
const MAX_VLQ: u64 = 0x0FFF_FFFF;
fn push_vlq(out: &mut Vec<u8>, v: u64) -> Result<(), MidiError> {
if v > MAX_VLQ {
return Err(MidiError::Write(format!("value {v} does not fit in a MIDI variable-length quantity")));
}
let mut buf = [0u8; 4];
let mut n = 0;
let mut x = v;
loop {
if let Some(slot) = buf.get_mut(n) {
*slot = (x & 0x7F) as u8;
}
n += 1;
x >>= 7;
if x == 0 || n == 4 {
break;
}
}
for i in (0..n).rev() {
let b = buf.get(i).copied().unwrap_or(0);
out.push(if i > 0 { b | 0x80 } else { b });
}
Ok(())
}
/// An event ready to be serialised: `(tick, order, bytes)`. `order` breaks ties at one tick.
struct WEvent {
tick: u64,
order: u8,
bytes: Vec<u8>,
}
fn clamp_tick(t: i64) -> u64 {
u64::try_from(t.max(0)).unwrap_or(0)
}
fn meta(kind: u8, data: &[u8]) -> Result<Vec<u8>, MidiError> {
let mut b = vec![0xFF, kind];
push_vlq(&mut b, u64::try_from(data.len()).unwrap_or(u64::MAX))?;
b.extend_from_slice(data);
Ok(b)
}
fn write_track(out: &mut Vec<u8>, mut events: Vec<WEvent>) -> Result<(), MidiError> {
events.sort_by_key(|e| (e.tick, e.order));
let mut body = Vec::new();
let mut last_tick = 0u64;
let mut running: Option<u8> = None;
for e in &events {
push_vlq(&mut body, e.tick - last_tick)?;
last_tick = e.tick;
let status = e.bytes.first().copied().unwrap_or(0);
if (0x80..=0xEF).contains(&status) {
if running == Some(status) {
body.extend_from_slice(e.bytes.get(1..).unwrap_or(&[]));
} else {
body.extend_from_slice(&e.bytes);
}
running = Some(status);
} else {
running = None;
body.extend_from_slice(&e.bytes);
}
}
// End of Track.
body.extend_from_slice(&[0x00, 0xFF, 0x2F, 0x00]);
let len = u32::try_from(body.len()).map_err(|_| MidiError::Write("track larger than 4 GiB".into()))?;
out.extend_from_slice(b"MTrk");
out.extend_from_slice(&len.to_be_bytes());
out.extend_from_slice(&body);
Ok(())
}
/// The conductor track: tempos, meters, key signatures and markers.
fn conductor_events(smf: &Smf) -> Result<Vec<WEvent>, MidiError> {
let mut ev = Vec::new();
for &(t, bpm) in &smf.tempos {
if !bpm.is_finite() || bpm <= 0.0 {
return Err(MidiError::Write(format!("invalid tempo {bpm} bpm")));
}
let us = (60_000_000.0 / bpm).round().clamp(1.0, f64::from(0x00FF_FFFFu32)) as u32;
let [_, a, b, c] = us.to_be_bytes();
ev.push(WEvent { tick: clamp_tick(t), order: 0, bytes: meta(0x51, &[a, b, c])? });
}
for &(t, num, den) in &smf.meters {
if den == 0 || !den.is_power_of_two() {
return Err(MidiError::Write(format!("meter denominator {den} is not a power of two")));
}
let dd = den.trailing_zeros() as u8;
ev.push(WEvent { tick: clamp_tick(t), order: 1, bytes: meta(0x58, &[num, dd, 24, 8])? });
}
for &(t, sf, minor) in &smf.key_sigs {
let sf = sf.clamp(-7, 7);
ev.push(WEvent { tick: clamp_tick(t), order: 2, bytes: meta(0x59, &[sf as u8, u8::from(minor)])? });
}
for (t, text) in &smf.markers {
ev.push(WEvent { tick: clamp_tick(*t), order: 3, bytes: meta(0x06, text.as_bytes())? });
}
Ok(ev)
}
/// Trim notes still sounding when the next same-pitch note starts, and drop duplicates.
fn trim_overlaps(notes: &[Note], ends: &mut [u64], dropped: &mut [bool]) {
// The reader pairs offs with ons first-in first-out, so a note still sounding when the next
// same-pitch note starts would swap ends with it: trim it to that start. When two same-pitch
// notes start on the same tick they cannot be told apart, so only the longer one is written.
let mut order: Vec<usize> = (0..notes.len()).collect();
order.sort_by_key(|&i| {
notes.get(i).map_or((0, 0, 0, std::cmp::Reverse(0)), |n| {
(n.channel & 0x0F, n.pitch.min(127), clamp_tick(n.start), std::cmp::Reverse(ends.get(i).copied().unwrap_or(0)))
})
});
let mut prev: Option<usize> = None;
for &b in &order {
let Some(nb) = notes.get(b) else { continue };
if let Some(a) = prev
&& let Some(na) = notes.get(a)
&& na.channel & 0x0F == nb.channel & 0x0F
&& na.pitch.min(127) == nb.pitch.min(127)
{
let (sa, sb) = (clamp_tick(na.start), clamp_tick(nb.start));
if sa == sb {
if let Some(d) = dropped.get_mut(b) {
*d = true;
}
continue;
}
if let Some(ea) = ends.get_mut(a)
&& sb < *ea
{
*ea = sb;
}
}
prev = Some(b);
}
}
/// Writes a format-1 Standard MIDI File at [`PPQ`] (960) ticks per quarter note.
///
/// Track 1 is a conductor track holding tempos, meters, key signatures and markers; every
/// [`SmfTrack`] follows as its own track. Negative ticks are clamped to 0, data values are
/// clamped to their MIDI ranges, and note-on velocity 0 is written as 1 (0 would mean
/// note-off). At each tick note-offs are written before controllers, then note-ons.
///
/// Errors: invalid tempo (non-finite or <= 0 bpm), meter denominator that is not a power of two
/// (1..=128), more than 65535 tracks, or a gap between events beyond the 28-bit delta limit.
pub fn write_smf(smf: &Smf) -> Result<Vec<u8>, MidiError> {
// The conductor track is omitted when there is nothing to put in it and no content tracks, so an
// empty `Smf` is written as a file with zero tracks and reads back as zero tracks.
let has_conductor =
!(smf.tempos.is_empty() && smf.meters.is_empty() && smf.key_sigs.is_empty() && smf.markers.is_empty() && smf.tracks.is_empty());
let ntrks =
u16::try_from(smf.tracks.len().saturating_add(usize::from(has_conductor))).map_err(|_| MidiError::Write("more than 65535 tracks".into()))?;
let mut out = Vec::new();
out.extend_from_slice(b"MThd");
out.extend_from_slice(&6u32.to_be_bytes());
out.extend_from_slice(&1u16.to_be_bytes());
out.extend_from_slice(&ntrks.to_be_bytes());
out.extend_from_slice(&u16::try_from(PPQ).unwrap_or(960).to_be_bytes());
// Conductor track.
let ev = conductor_events(smf)?;
if has_conductor {
write_track(&mut out, ev)?;
}
for track in &smf.tracks {
let mut ev = Vec::new();
if !track.name.is_empty() {
ev.push(WEvent { tick: 0, order: 0, bytes: meta(0x03, track.name.as_bytes())? });
}
let notes = &track.sequence.notes;
let mut ends: Vec<u64> = notes.iter().map(|n| clamp_tick(n.end()).max(clamp_tick(n.start).saturating_add(1))).collect();
let mut dropped = vec![false; notes.len()];
trim_overlaps(notes, &mut ends, &mut dropped);
for (i, n) in notes.iter().enumerate() {
if dropped.get(i).copied().unwrap_or(false) {
continue;
}
let ch = n.channel & 0x0F;
let pitch = n.pitch.min(127);
let start = clamp_tick(n.start);
let end = ends.get(i).copied().unwrap_or(start.saturating_add(1));
ev.push(WEvent { tick: start, order: 3, bytes: vec![0x90 | ch, pitch, n.velocity.clamp(1, 127)] });
ev.push(WEvent { tick: end, order: 1, bytes: vec![0x80 | ch, pitch, n.release_velocity.min(127)] });
}
for c in &track.sequence.ctrls {
let ch = c.channel & 0x0F;
let v7 = c.value.clamp(0, 127) as u8;
let bytes = match c.kind {
CtrlKind::Cc(num) => vec![0xB0 | ch, num.min(127), v7],
CtrlKind::Program => vec![0xC0 | ch, v7],
CtrlKind::ChannelPressure => vec![0xD0 | ch, v7],
CtrlKind::PolyPressure(p) => vec![0xA0 | ch, p.min(127), v7],
CtrlKind::PitchBend => {
let v = (c.value.saturating_add(8192)).clamp(0, 16383);
vec![0xE0 | ch, (v & 0x7F) as u8, ((v >> 7) & 0x7F) as u8]
}
};
ev.push(WEvent { tick: clamp_tick(c.tick), order: 2, bytes });
}
write_track(&mut out, ev)?;
}
Ok(out)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn vlq_round_trip() {
for v in [0u64, 0x40, 0x7F, 0x80, 0x2000, 0x3FFF, 0x4000, 0x1F_FFFF, 0x20_0000, MAX_VLQ] {
let mut b = Vec::new();
push_vlq(&mut b, v).unwrap();
let mut c = Cursor { data: &b, pos: 0, base: 0 };
assert_eq!(u64::from(c.vlq().unwrap()), v);
assert!(c.is_empty());
}
assert!(push_vlq(&mut Vec::new(), MAX_VLQ + 1).is_err());
}
#[test]
fn empty_smf_round_trips_with_no_tracks() {
let r = read_smf(&write_smf(&Smf::default()).unwrap()).unwrap();
assert_eq!(r.tracks.len(), 0);
let one = Smf { tracks: vec![SmfTrack::default()], ..Smf::default() };
let r = read_smf(&write_smf(&one).unwrap()).unwrap();
assert_eq!(r.tracks.len(), 1);
let cond = Smf { tempos: vec![(0, 90.0)], ..Smf::default() };
let r = read_smf(&write_smf(&cond).unwrap()).unwrap();
assert_eq!(r.tracks.len(), 0);
assert_eq!(r.tempos.len(), 1);
}
#[test]
fn spec_vlq_examples() {
let mut b = Vec::new();
push_vlq(&mut b, 0x0800_0000).unwrap();
assert_eq!(b, [0xC0, 0x80, 0x80, 0x00]);
let mut b = Vec::new();
push_vlq(&mut b, 0x2000).unwrap();
assert_eq!(b, [0xC0, 0x00]);
}
#[test]
fn same_pitch_overlaps_do_not_swap_ends() {
let mk = |start, length| Note { pitch: 60, velocity: 100, release_velocity: 64, channel: 0, start, length };
let smf = Smf {
format: 1,
tracks: vec![SmfTrack {
name: String::new(),
sequence: Sequence { notes: vec![mk(0, 1000), mk(100, 200), mk(1000, 100), mk(2000, 500), mk(2000, 900)], ..Sequence::default() },
}],
tempos: vec![],
meters: vec![],
markers: vec![],
key_sigs: vec![],
};
let back = read_smf(&write_smf(&smf).unwrap()).unwrap();
let mut got: Vec<(i64, i64)> = back.tracks.iter().flat_map(|t| t.sequence.notes.iter().map(|n| (n.start, n.length))).collect();
got.sort_unstable();
// The earlier note is trimmed to the start of the next same-pitch note; later notes keep their length;
// of two notes starting together only the longer survives.
assert_eq!(got, vec![(0, 100), (100, 200), (1000, 100), (2000, 900)]);
}
}
-1
View File
@@ -1,4 +1,3 @@
//! Integration tests, built as one test binary: each file is a module.
mod ops;
mod smf;
-104
View File
@@ -187,107 +187,3 @@ fn split_by_pitch() {
assert_eq!(inside.iter().map(|n| n.pitch).collect::<Vec<_>>(), vec![60, 48]);
assert_eq!(outside.iter().map(|n| n.pitch).collect::<Vec<_>>(), vec![36, 72]);
}
#[test]
fn sequence_helpers() {
let mut seq = Sequence {
notes: vec![n(64, 100, 50), n(60, 100, 10), n(60, 100, 30), n(60, 0, 10), Note { channel: 2, ..n(60, 0, 10) }],
ctrls: vec![
CtrlEvent { tick: 500, channel: 0, kind: CtrlKind::Cc(1), value: 1 },
CtrlEvent { tick: 5, channel: 0, kind: CtrlKind::Cc(1), value: 2 },
CtrlEvent { tick: 500, channel: 0, kind: CtrlKind::Cc(1), value: 3 },
CtrlEvent { tick: 500, channel: 0, kind: CtrlKind::Cc(2), value: 4 },
],
};
assert_eq!(seq.end_tick(), 500);
seq.sort();
assert_eq!(starts(&seq.notes), vec![0, 0, 100, 100, 100]);
assert_eq!(seq.notes[2].pitch, 60);
assert_eq!(seq.notes_in(0, 100).count(), 2);
assert_eq!(seq.notes_overlapping(105, 106).count(), 3);
let removed = seq.remove_duplicates();
assert_eq!(removed, 2);
assert_eq!(seq.notes.len(), 4);
let kept = seq.notes.iter().find(|n| n.start == 100 && n.pitch == 60).unwrap();
assert_eq!(kept.length, 30);
assert_eq!(seq.ctrls.iter().map(|c| c.value).collect::<Vec<_>>(), vec![2, 3, 4]);
assert_eq!(seq.remove_duplicates(), 0);
assert_eq!(Sequence::default().end_tick(), 0);
}
#[test]
fn sequence_serde_round_trip() {
let seq = Sequence { notes: vec![n(60, 0, 1)], ctrls: vec![CtrlEvent { tick: 1, channel: 0, kind: CtrlKind::PolyPressure(60), value: 9 }] };
let json = serde_json::to_string(&seq).unwrap();
assert_eq!(serde_json::from_str::<Sequence>(&json).unwrap(), seq);
}
#[test]
fn remove_duplicates_handles_a_large_same_tick_controller_group() {
// Every channel x controller number at one tick, twice: the second copy of each wins. A
// rescan of the tick's kept events per incoming event would make this quadratic.
let keys: Vec<(u8, u8)> = (0..=255u8).flat_map(|ch| (0..=255u8).map(move |cc| (ch, cc))).collect();
let ctrls = [0, 1]
.iter()
.flat_map(|&pass| keys.iter().map(move |&(channel, cc)| CtrlEvent { tick: 7, channel, kind: CtrlKind::Cc(cc), value: pass }))
.collect();
let mut seq = Sequence { notes: Vec::new(), ctrls };
let start = std::time::Instant::now();
assert_eq!(seq.remove_duplicates(), keys.len());
assert!(start.elapsed() < std::time::Duration::from_secs(10), "{:?}", start.elapsed());
assert_eq!(seq.ctrls.len(), keys.len());
assert!(seq.ctrls.iter().all(|c| c.value == 1));
}
#[test]
fn remove_duplicates_matches_a_per_tick_rescan_on_mixed_ticks() {
// The straightforward per-tick rescan the indexed version replaced, kept as an oracle.
fn reference(mut ctrls: Vec<CtrlEvent>) -> Vec<CtrlEvent> {
ctrls.sort_by_key(|c| c.tick);
let key = |k: CtrlKind| match k {
CtrlKind::Cc(n) => (0, n),
CtrlKind::PitchBend => (1, 0),
CtrlKind::ChannelPressure => (2, 0),
CtrlKind::PolyPressure(p) => (3, p),
CtrlKind::Program => (4, 0),
};
let mut kept: Vec<CtrlEvent> = Vec::new();
for c in ctrls {
match kept.iter_mut().rev().take_while(|k| k.tick == c.tick).find(|k| k.channel == c.channel && key(k.kind) == key(c.kind)) {
Some(slot) => *slot = c,
None => kept.push(c),
}
}
kept
}
// Few ticks, channels and kinds, so the same key recurs both within a tick (interleaved with
// other keys) and across ticks; unsorted input from a fixed xorshift stream.
let mut state = 0x9E37_79B9_7F4A_7C15u64;
let mut next = move || {
state ^= state << 13;
state ^= state >> 7;
state ^= state << 17;
state
};
let kinds = |r: u64| match r % 5 {
0 => CtrlKind::Cc((r >> 8) as u8 % 4),
1 => CtrlKind::PitchBend,
2 => CtrlKind::ChannelPressure,
3 => CtrlKind::PolyPressure((r >> 8) as u8 % 4),
_ => CtrlKind::Program,
};
for round in 0..200 {
let mut ctrls = Vec::new();
for i in 0..(next() % 64) {
let r = next();
ctrls.push(CtrlEvent { tick: (r % 6) as i64 - 2, channel: (r >> 16) as u8 % 3, kind: kinds(r >> 24), value: (round * 100 + i) as i32 });
}
let mut seq = Sequence { notes: Vec::new(), ctrls: ctrls.clone() };
let removed = seq.remove_duplicates();
let expected = reference(ctrls.clone());
assert_eq!(seq.ctrls, expected, "round {round}");
assert_eq!(removed, ctrls.len() - expected.len(), "round {round}");
}
}
-375
View File
@@ -1,375 +0,0 @@
use proptest::prelude::*;
use soundcraft_midi::*;
fn vlq(mut v: u32) -> Vec<u8> {
let mut out = vec![(v & 0x7F) as u8];
v >>= 7;
while v > 0 {
out.insert(0, (v & 0x7F) as u8 | 0x80);
v >>= 7;
}
out
}
fn header(format: u16, ntrks: u16, division: u16) -> Vec<u8> {
let mut b = b"MThd".to_vec();
b.extend_from_slice(&6u32.to_be_bytes());
b.extend_from_slice(&format.to_be_bytes());
b.extend_from_slice(&ntrks.to_be_bytes());
b.extend_from_slice(&division.to_be_bytes());
b
}
fn track(body: &[u8]) -> Vec<u8> {
let mut b = b"MTrk".to_vec();
b.extend_from_slice(&(body.len() as u32).to_be_bytes());
b.extend_from_slice(body);
b
}
fn file(format: u16, division: u16, bodies: &[Vec<u8>]) -> Vec<u8> {
let mut b = header(format, bodies.len() as u16, division);
for body in bodies {
b.extend(track(body));
}
b
}
fn note(pitch: u8, start: i64, length: i64) -> Note {
Note { pitch, velocity: 100, release_velocity: 64, channel: 0, start, length }
}
#[test]
fn round_trip() {
let mut seq = Sequence {
notes: vec![
note(60, 0, 480),
Note { pitch: 64, velocity: 90, release_velocity: 30, channel: 3, start: 480, length: 960 },
note(67, 960, 1),
note(60, 1920, 240),
],
ctrls: vec![
CtrlEvent { tick: 0, channel: 0, kind: CtrlKind::Program, value: 5 },
CtrlEvent { tick: 10, channel: 0, kind: CtrlKind::Cc(7), value: 100 },
CtrlEvent { tick: 20, channel: 1, kind: CtrlKind::PitchBend, value: -8192 },
CtrlEvent { tick: 30, channel: 1, kind: CtrlKind::PitchBend, value: 8191 },
CtrlEvent { tick: 40, channel: 1, kind: CtrlKind::PitchBend, value: 0 },
CtrlEvent { tick: 50, channel: 2, kind: CtrlKind::ChannelPressure, value: 77 },
CtrlEvent { tick: 60, channel: 2, kind: CtrlKind::PolyPressure(61), value: 12 },
],
};
seq.sort();
let smf = Smf {
format: 1,
tracks: vec![
SmfTrack { name: "Piano".into(), sequence: seq.clone() },
SmfTrack { name: "Bass ♪".into(), sequence: Sequence { notes: vec![note(36, 100, 50)], ctrls: vec![] } },
],
tempos: vec![(0, 120.0), (3840, 90.0)],
meters: vec![(0, 4, 4), (3840, 6, 8)],
markers: vec![(0, "Intro".into()), (7680, "Verse".into())],
key_sigs: vec![(0, -3, true), (3840, 2, false)],
};
let bytes = write_smf(&smf).unwrap();
let back = read_smf(&bytes).unwrap();
assert_eq!(back.format, 1);
assert_eq!(back.tracks.len(), 2);
assert_eq!(back.tracks[0].name, "Piano");
assert_eq!(back.tracks[0].sequence, seq);
assert_eq!(back.tracks[1].name, "Bass ♪");
assert_eq!(back.tracks[1].sequence.notes, vec![note(36, 100, 50)]);
assert_eq!(back.tempos.len(), 2);
assert!((back.tempos[0].1 - 120.0).abs() < 1e-9);
assert!((back.tempos[1].1 - 90.0).abs() < 1e-3);
assert_eq!(back.tempos[1].0, 3840);
assert_eq!(back.meters, smf.meters);
assert_eq!(back.markers, smf.markers);
assert_eq!(back.key_sigs, smf.key_sigs);
// Writing again is byte-identical.
let mut again = back.clone();
again.tempos = smf.tempos.clone();
assert_eq!(write_smf(&again).unwrap(), bytes);
}
#[test]
fn first_track_name_wins_and_ignored_meta_preserves_following_notes() {
let meta = |kind, data: &[u8]| [vec![0, 0xFF, kind], vlq(data.len() as u32), data.to_vec()].concat();
for first in [&b"First"[..], &b""[..], &[0xFF][..]] {
let body = [
meta(0x03, first),
meta(0x03, b"Second"),
meta(0x7F, &[0x03, 0xFF, 0x2F]), // Unhandled metadata must remain ignored.
vec![0, 0x90, 60, 100, 16, 0x80, 60, 64, 0, 0xFF, 0x2F, 0],
]
.concat();
let smf = read_smf(&file(0, 960, &[body])).unwrap();
assert_eq!(smf.tracks.len(), 1);
assert_eq!(smf.tracks[0].name, String::from_utf8_lossy(first));
assert_eq!(smf.tracks[0].sequence.notes, vec![note(60, 0, 16)]);
}
}
#[test]
fn write_rejects_bad_values() {
let bad_tempo = Smf { tempos: vec![(0, 0.0)], ..Default::default() };
assert!(write_smf(&bad_tempo).is_err());
let nan_tempo = Smf { tempos: vec![(0, f64::NAN)], ..Default::default() };
assert!(write_smf(&nan_tempo).is_err());
let bad_meter = Smf { meters: vec![(0, 4, 3)], ..Default::default() };
assert!(write_smf(&bad_meter).is_err());
let huge_gap = Smf {
tracks: vec![SmfTrack { name: String::new(), sequence: Sequence { notes: vec![note(60, 0x1000_0000, 10)], ctrls: vec![] } }],
..Default::default()
};
assert!(write_smf(&huge_gap).is_err());
}
#[test]
fn division_conversion_480_to_960() {
// Note at tick 480 (one quarter at 480 PPQ), length 240.
let body = [vlq(480), vec![0x90, 60, 100], vlq(240), vec![0x80, 60, 0], vec![0, 0xFF, 0x2F, 0]].concat();
let smf = read_smf(&file(0, 480, &[body])).unwrap();
assert_eq!(smf.tracks.len(), 1);
assert_eq!(smf.tracks[0].sequence.notes, vec![Note { pitch: 60, velocity: 100, release_velocity: 0, channel: 0, start: 960, length: 480 }]);
}
#[test]
fn division_conversion_96_and_smpte() {
let body = [vlq(96), vec![0x90, 60, 100], vlq(48), vec![0x80, 60, 64]].concat();
let smf = read_smf(&file(0, 96, std::slice::from_ref(&body))).unwrap();
assert_eq!(smf.tracks[0].sequence.notes[0].start, 960);
assert_eq!(smf.tracks[0].sequence.notes[0].length, 480);
// SMPTE 25 fps, 40 ticks/frame = 1000 ticks/s; 96 ticks = 96 ms; at 120 bpm 0.5 s = 960 ticks.
let div = u16::from_be_bytes([(-25i8) as u8, 40]);
let body = [vlq(500), vec![0x90, 60, 100], vlq(250), vec![0x80, 60, 64]].concat();
let smf = read_smf(&file(0, div, &[body])).unwrap();
assert_eq!(smf.tracks[0].sequence.notes[0].start, 960);
assert_eq!(smf.tracks[0].sequence.notes[0].length, 480);
}
#[test]
fn smpte_division_rejects_undefined_frame_rates() {
let body = vec![0, 0x90, 60, 100, 40, 0x80, 60, 64, 0, 0xFF, 0x2F, 0];
for high in 0x80..=0xFF {
if [(-24i8) as u8, (-25i8) as u8, (-29i8) as u8, (-30i8) as u8].contains(&high) {
continue;
}
let division = u16::from_be_bytes([high, 40]);
assert!(matches!(read_smf(&file(0, division, std::slice::from_ref(&body))), Err(MidiError::Invalid { offset: 12, .. })), "{division:#06x}");
}
}
#[test]
fn smpte_division_keeps_all_four_standard_frame_rates() {
for (fps, file_ticks, model_ticks) in [(24i8, 960, 1920), (25, 1000, 1920), (29, 1200, 1922), (30, 1200, 1920)] {
let body = [vec![0, 0x90, 60, 100], vlq(file_ticks), vec![0x80, 60, 64, 0, 0xFF, 0x2F, 0]].concat();
let division = u16::from_be_bytes([(-fps) as u8, 40]);
let smf = read_smf(&file(0, division, std::slice::from_ref(&body))).unwrap();
assert_eq!(smf.tracks[0].sequence.notes, vec![note(60, 0, model_ticks)], "{fps} fps");
assert!(read_smf(&file(0, u16::from_be_bytes([(-fps) as u8, 0]), std::slice::from_ref(&body))).is_err());
}
}
#[test]
fn running_status_and_velocity_zero_note_off() {
// Note-on with running status for both on and off (velocity 0).
let body = [
vec![0x00, 0x91, 60, 100], // on C3 ch1
vec![0x00, 64, 90], // running: on E3
vlq(960),
vec![60, 0], // running: note-on vel 0 = off C3
vec![0x00, 64, 0],
vec![0x00, 0xB1, 7, 100], // CC7
vec![0x10, 10, 64], // running CC10
]
.concat();
let smf = read_smf(&file(0, 960, &[body])).unwrap();
let seq = &smf.tracks[0].sequence;
assert_eq!(
seq.notes,
vec![
Note { pitch: 60, velocity: 100, release_velocity: 64, channel: 1, start: 0, length: 960 },
Note { pitch: 64, velocity: 90, release_velocity: 64, channel: 1, start: 0, length: 960 },
]
);
assert_eq!(
seq.ctrls,
vec![
CtrlEvent { tick: 960, channel: 1, kind: CtrlKind::Cc(7), value: 100 },
CtrlEvent { tick: 976, channel: 1, kind: CtrlKind::Cc(10), value: 64 },
]
);
}
#[test]
fn meta_cancels_running_status() {
let body = [vec![0x00, 0x90, 60, 100], vec![0x00, 0xFF, 0x01, 0x01, b'x'], vec![0x00, 60, 0]].concat();
assert!(read_smf(&file(0, 960, &[body])).is_err());
}
#[test]
fn unmatched_note_off_ignored_and_hanging_notes_closed() {
let body = [
vec![0x00, 0x80, 50, 0], // unmatched off
vec![0x00, 0x90, 60, 100], // hanging
vlq(100),
vec![0x90, 62, 100],
vlq(100),
vec![0x80, 62, 10],
vlq(800),
vec![0xFF, 0x2F, 0x00],
]
.concat();
let smf = read_smf(&file(0, 960, &[body])).unwrap();
let notes = &smf.tracks[0].sequence.notes;
assert_eq!(notes.len(), 2);
assert_eq!(notes[0], Note { pitch: 60, velocity: 100, release_velocity: 64, channel: 0, start: 0, length: 1000 });
assert_eq!(notes[1], Note { pitch: 62, velocity: 100, release_velocity: 10, channel: 0, start: 100, length: 100 });
}
#[test]
fn overlapping_same_pitch_fifo_and_zero_length() {
let body = [
vec![0x00, 0x90, 60, 100],
vec![0x10, 0x90, 60, 80],
vec![0x10, 0x80, 60, 0],
vec![0x10, 0x80, 60, 0],
vec![0x00, 0x90, 70, 1],
vec![0x00, 0x80, 70, 0],
]
.concat();
let smf = read_smf(&file(0, 960, &[body])).unwrap();
let notes = &smf.tracks[0].sequence.notes;
assert_eq!((notes[0].start, notes[0].length, notes[0].velocity), (0, 32, 100));
assert_eq!((notes[1].start, notes[1].length, notes[1].velocity), (16, 32, 80));
assert_eq!((notes[2].pitch, notes[2].length), (70, 1));
}
#[test]
fn format1_conductor_folded_and_unknown_chunks_skipped() {
let conductor = [
vec![0x00, 0xFF, 0x51, 0x03, 0x07, 0xA1, 0x20], // 500000 us = 120 bpm
vec![0x00, 0xFF, 0x58, 0x04, 3, 2, 24, 8], // 3/4
vec![0x00, 0xFF, 0x2F, 0x00],
]
.concat();
let t1 = [vec![0x00, 0xFF, 0x03, 0x03], b"Org".to_vec(), vec![0x00, 0x90, 60, 100, 0x60, 0x80, 60, 0]].concat();
let mut bytes = header(1, 2, 960);
bytes.extend(track(&conductor));
bytes.extend(b"XFIH\x00\x00\x00\x03abc");
bytes.extend(track(&t1));
let smf = read_smf(&bytes).unwrap();
assert_eq!(smf.tracks.len(), 1);
assert_eq!(smf.tracks[0].name, "Org");
assert_eq!(smf.tempos, vec![(0, 120.0)]);
assert_eq!(smf.meters, vec![(0, 3, 4)]);
}
#[test]
fn sysex_skipped() {
let body = [vec![0x00, 0xF0, 0x03, 0x7E, 0x7F, 0xF7], vec![0x00, 0x90, 60, 100, 0x10, 0x80, 60, 0]].concat();
let smf = read_smf(&file(0, 960, &[body])).unwrap();
assert_eq!(smf.tracks[0].sequence.notes.len(), 1);
}
#[test]
fn malformed_inputs_are_errors() {
assert!(matches!(read_smf(b""), Err(MidiError::NotSmf)));
assert!(matches!(read_smf(b"RIFF\x00\x00\x00\x06"), Err(MidiError::NotSmf)));
// Header truncated.
assert!(read_smf(b"MThd\x00\x00\x00\x06\x00\x01").is_err());
// Header length < 6.
assert!(read_smf(b"MThd\x00\x00\x00\x02\x00\x01").is_err());
// Division 0.
assert!(read_smf(&header(0, 0, 0)).is_err());
// Format 3.
assert!(read_smf(&header(3, 0, 96)).is_err());
// Chunk length larger than the data.
let mut b = header(0, 1, 96);
b.extend(b"MTrk\xFF\xFF\xFF\xF0\x00\x90");
assert!(matches!(read_smf(&b), Err(MidiError::BadChunkLength { .. })));
// Truncated chunk header.
let mut b = header(0, 1, 96);
b.extend(b"MTr");
assert!(read_smf(&b).is_err());
// Huge VLQ (5 continuation bytes).
let body = vec![0xFF, 0xFF, 0xFF, 0xFF, 0x7F, 0x90, 60, 100];
assert!(matches!(read_smf(&file(0, 96, &[body])), Err(MidiError::VlqTooLong(_))));
// Meta length past the end of the track.
let body = vec![0x00, 0xFF, 0x03, 0x7F, b'a'];
assert!(matches!(read_smf(&file(0, 96, &[body])), Err(MidiError::Truncated(_))));
// Truncated channel message.
let body = vec![0x00, 0x90, 60];
assert!(read_smf(&file(0, 96, &[body])).is_err());
// Data byte with no running status.
let body = vec![0x00, 60, 100];
assert!(read_smf(&file(0, 96, &[body])).is_err());
// System common inside a track.
let body = vec![0x00, 0xF2, 0, 0];
assert!(read_smf(&file(0, 96, &[body])).is_err());
// Header only (no tracks) is fine.
assert_eq!(read_smf(&header(1, 0, 96)).unwrap().tracks.len(), 0);
}
#[test]
fn event_cap_reached_is_error() {
// MAX_EVENTS + 1 one-byte running-status program changes (2 bytes each with delta).
let mut body = vec![0x00, 0xC0, 0x00];
body.reserve(MAX_EVENTS * 2);
for _ in 0..MAX_EVENTS {
body.extend_from_slice(&[0x00, 0x01]);
}
assert!(matches!(read_smf(&file(0, 96, &[body])), Err(MidiError::TooManyEvents(_))));
}
proptest! {
#![proptest_config(ProptestConfig::with_cases(512))]
#[test]
fn random_bytes_never_panic(data in proptest::collection::vec(any::<u8>(), 0..512)) {
let _ = read_smf(&data);
}
#[test]
fn random_track_bodies_never_panic(body in proptest::collection::vec(any::<u8>(), 0..512), div in any::<u16>(), fmt in 0u16..4) {
let _ = read_smf(&file(fmt, div, &[body]));
}
#[test]
fn written_files_read_back(notes in proptest::collection::vec((0u8..128, 1u8..128, 0u8..16, 0i64..100_000, 1i64..10_000), 0..64)) {
let mut seq = Sequence {
notes: notes.iter().map(|&(pitch, velocity, channel, start, length)| Note { pitch, velocity, release_velocity: 64, channel, start, length }).collect(),
ctrls: vec![],
};
seq.remove_duplicates();
let smf = Smf { format: 1, tracks: vec![SmfTrack { name: "t".into(), sequence: seq.clone() }], ..Default::default() };
let back = read_smf(&write_smf(&smf).unwrap()).unwrap();
// Overlapping same-pitch notes may re-pair, but count and starts survive.
let got = back.tracks.first().map(|t| t.sequence.notes.clone()).unwrap_or_default();
prop_assert_eq!(got.len(), seq.notes.len());
let mut a: Vec<_> = got.iter().map(|n| (n.start, n.pitch, n.channel)).collect();
let mut b: Vec<_> = seq.notes.iter().map(|n| (n.start, n.pitch, n.channel)).collect();
a.sort();
b.sort();
prop_assert_eq!(a, b);
}
}
/// #472: out-of-range pitches, release velocities and controller numbers clamp to 127 instead of
/// wrapping into other notes and controllers (128 & 0x7F would be 0).
#[test]
fn writer_clamps_out_of_range_data_bytes() {
for raw in [127u8, 128, 255] {
let mut seq = Sequence::default();
seq.notes.push(Note { pitch: raw, velocity: 100, release_velocity: raw, channel: 0, start: 0, length: 960 });
seq.ctrls.push(CtrlEvent { tick: 0, channel: 0, kind: CtrlKind::Cc(raw), value: 64 });
seq.ctrls.push(CtrlEvent { tick: 0, channel: 0, kind: CtrlKind::PolyPressure(raw), value: 64 });
let smf = Smf { tracks: vec![SmfTrack { name: String::new(), sequence: seq }], ..Smf::default() };
let back = read_smf(&write_smf(&smf).unwrap()).unwrap();
let s = &back.tracks.last().unwrap().sequence;
assert_eq!((s.notes[0].pitch, s.notes[0].release_velocity), (127, 127), "input {raw}");
let kinds: Vec<CtrlKind> = s.ctrls.iter().map(|c| c.kind).collect();
assert!(kinds.contains(&CtrlKind::Cc(127)) && kinds.contains(&CtrlKind::PolyPressure(127)), "input {raw}: {kinds:?}");
}
}
+2 -10
View File
@@ -1,6 +1,6 @@
[package]
name = "soundcraft-model"
description = "SoundCraft session document: tracks, playlists, clips, mixer, routing, automation, markers"
description = "Compatibility facade for SoundCraft's shared native session format"
version.workspace = true
edition.workspace = true
license.workspace = true
@@ -8,15 +8,7 @@ rust-version.workspace = true
repository.workspace = true
[dependencies]
soundcraft-time.workspace = true
craft-audio.workspace = true
soundcraft-midi.workspace = true
serde.workspace = true
serde_json.workspace = true
thiserror.workspace = true
[dev-dependencies]
proptest.workspace = true
craft-scraft-format.workspace = true
[lints]
workspace = true
-394
View File
@@ -1,394 +0,0 @@
//! Automation lanes and modes.
use soundcraft_time::Samples;
/// Automation modes (per track).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default, serde::Serialize, serde::Deserialize)]
pub enum AutomationMode {
Off,
#[default]
Read,
Touch,
Latch,
TouchLatch,
Write,
Trim,
}
impl AutomationMode {
pub const ALL: [AutomationMode; 7] = [
AutomationMode::Off,
AutomationMode::Read,
AutomationMode::Touch,
AutomationMode::Latch,
AutomationMode::TouchLatch,
AutomationMode::Write,
AutomationMode::Trim,
];
pub fn label(self) -> &'static str {
match self {
AutomationMode::Off => "off",
AutomationMode::Read => "read",
AutomationMode::Touch => "touch",
AutomationMode::Latch => "latch",
AutomationMode::TouchLatch => "touch/latch",
AutomationMode::Write => "write",
AutomationMode::Trim => "trim",
}
}
pub fn from_id(s: &str) -> Option<AutomationMode> {
let s = s.to_ascii_lowercase();
AutomationMode::ALL.into_iter().find(|m| m.label() == s || m.label().replace('/', "_") == s || format!("{m:?}").to_ascii_lowercase() == s)
}
pub fn reads(self) -> bool {
!matches!(self, AutomationMode::Off)
}
}
/// Which parameter an automation lane controls.
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, serde::Serialize, serde::Deserialize)]
pub enum AutoParam {
/// Fader level, dB.
Volume,
/// Pan, -1..1 (index = which panner of a stereo track: 0 left, 1 right).
Pan(u8),
/// Mute (0/1).
Mute,
/// Send level, dB.
SendLevel(u8),
SendPan(u8),
SendMute(u8),
/// Insert slot + parameter id.
Plugin {
slot: u8,
param: String,
},
}
impl AutoParam {
pub fn label(&self) -> String {
match self {
AutoParam::Volume => "volume".into(),
AutoParam::Pan(0) => "pan".into(),
AutoParam::Pan(i) => format!("pan {}", u16::from(*i) + 1),
AutoParam::Mute => "mute".into(),
AutoParam::SendLevel(i) => format!("send {} level", slot_letter(*i)),
AutoParam::SendPan(i) => format!("send {} pan", slot_letter(*i)),
AutoParam::SendMute(i) => format!("send {} mute", slot_letter(*i)),
AutoParam::Plugin { slot, param } => format!("insert {} {param}", slot_letter(*slot)),
}
}
/// Parse ids like `volume`, `pan`, `pan2`, `mute`, `send_a_level`, `plugin:a:threshold`.
pub fn parse(s: &str) -> Option<AutoParam> {
let orig = s.trim();
let s = orig.to_ascii_lowercase();
match s.as_str() {
"volume" | "vol" => return Some(AutoParam::Volume),
"pan" | "pan1" => return Some(AutoParam::Pan(0)),
"pan2" => return Some(AutoParam::Pan(1)),
"mute" => return Some(AutoParam::Mute),
_ => {}
}
if let Some(rest) = s.strip_prefix("send_") {
let mut it = rest.splitn(2, '_');
let slot = letter_slot(it.next()?)?;
return match it.next()? {
"level" => Some(AutoParam::SendLevel(slot)),
"pan" => Some(AutoParam::SendPan(slot)),
"mute" => Some(AutoParam::SendMute(slot)),
_ => None,
};
}
if let Some(rest) = s.strip_prefix("plugin:") {
let (slot, param) = rest.split_once(':')?;
// ASCII lowercasing keeps byte offsets, so take the param from the original.
let param = orig.get(orig.len() - param.len()..)?;
return Some(AutoParam::Plugin { slot: letter_slot(slot)?, param: param.to_string() });
}
None
}
/// Value range and default.
pub fn range(&self) -> (f32, f32, f32) {
match self {
AutoParam::Volume | AutoParam::SendLevel(_) => (-144.0, 12.0, 0.0),
AutoParam::Pan(_) | AutoParam::SendPan(_) => (-1.0, 1.0, 0.0),
AutoParam::Mute | AutoParam::SendMute(_) => (0.0, 1.0, 0.0),
AutoParam::Plugin { .. } => (f32::MIN, f32::MAX, 0.0),
}
}
}
pub fn slot_letter(i: u8) -> char {
char::from(b'A'.saturating_add(i.min(25)))
}
fn letter_slot(s: &str) -> Option<u8> {
let c = s.chars().next()?.to_ascii_uppercase();
if s.len() == 1 && c.is_ascii_uppercase() { Some(c as u8 - b'A') } else { s.parse().ok() }
}
#[derive(Debug, Clone, Copy, PartialEq, serde::Serialize, serde::Deserialize)]
pub struct AutomationPoint {
pub at: Samples,
pub value: f32,
}
/// A breakpoint automation lane. Points are kept sorted by time.
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
pub struct AutomationLane {
pub param: AutoParam,
pub points: Vec<AutomationPoint>,
#[serde(default)]
pub enabled: bool,
}
impl AutomationLane {
pub fn new(param: AutoParam) -> Self {
AutomationLane { param, points: Vec::new(), enabled: true }
}
/// Value at `at` (linear interpolation; `default` when empty).
pub fn value_at(&self, at: Samples, default: f32) -> f32 {
let pts = &self.points;
let Some(first) = pts.first() else { return default };
if at <= first.at {
return first.value;
}
let idx = pts.partition_point(|p| p.at <= at);
match (pts.get(idx.wrapping_sub(1)), pts.get(idx)) {
(Some(a), Some(b)) if b.at > a.at => {
// Mute-like lanes step instead of ramping.
if matches!(self.param, AutoParam::Mute | AutoParam::SendMute(_)) {
return a.value;
}
// Widen so far-apart breakpoints cannot overflow i64.
let span = i128::from(b.at) - i128::from(a.at);
let off = i128::from(at) - i128::from(a.at);
// Interpolate in f64: the f32 difference overflows for
// opposite-sign values near f32::MAX.
let (av, bv) = (f64::from(a.value), f64::from(b.value));
(av + (bv - av) * (off as f64 / span as f64)) as f32
}
(Some(a), _) => a.value,
_ => default,
}
}
/// Insert or replace a breakpoint.
pub fn set_point(&mut self, at: Samples, value: f32) {
let (lo, hi, _) = self.param.range();
let value = if value.is_nan() { 0.0 } else { value.clamp(lo, hi) };
match self.points.binary_search_by(|p| p.at.cmp(&at)) {
Ok(i) => {
if let Some(p) = self.points.get_mut(i) {
p.value = value;
}
}
Err(i) => self.points.insert(i, AutomationPoint { at, value }),
}
}
/// Remove points in `[start, end)`.
pub fn clear_range(&mut self, start: Samples, end: Samples) -> usize {
let before = self.points.len();
self.points.retain(|p| p.at < start || p.at >= end);
before - self.points.len()
}
/// Write a constant value over a range, keeping the surrounding curve intact with anchor points.
pub fn write_range(&mut self, start: Samples, end: Samples, value: f32, default: f32) {
if end <= start {
return;
}
let before = self.value_at(start.saturating_sub(1), default);
let after = self.value_at(end, default);
self.clear_range(start, end.saturating_add(1));
if start > 0 {
self.set_point(start - 1, before);
}
self.set_point(start, value);
self.set_point(end, value);
let anchor = end.saturating_add(1);
// A real breakpoint already at `end + 1` keeps its own value.
if self.points.binary_search_by(|p| p.at.cmp(&anchor)).is_err() {
self.set_point(anchor, after);
}
}
/// Thin: remove points whose removal changes the curve by less than `tolerance`.
pub fn thin(&mut self, tolerance: f32) -> usize {
// Linear thinning would move the transitions of step lanes.
if matches!(self.param, AutoParam::Mute | AutoParam::SendMute(_)) || self.points.len() < 3 || tolerance.is_nan() {
return 0;
}
let tolerance = f64::from(tolerance);
let mut keep = vec![true; self.points.len()];
let mut last = 0usize;
// Slopes from the last kept point that keep every point removed since then within `tolerance`.
let (mut lo, mut hi) = (f64::NEG_INFINITY, f64::INFINITY);
for i in 1..self.points.len() - 1 {
let (Some(a), Some(b), Some(c)) = (self.points.get(last), self.points.get(i), self.points.get(i + 1)) else { continue };
let dt = b.at.saturating_sub(a.at) as f64;
let rise = f64::from(b.value) - f64::from(a.value);
lo = lo.max((rise - tolerance) / dt);
hi = hi.min((rise + tolerance) / dt);
let slope = (f64::from(c.value) - f64::from(a.value)) / c.at.saturating_sub(a.at) as f64;
if (lo..=hi).contains(&slope) {
if let Some(k) = keep.get_mut(i) {
*k = false;
}
} else {
last = i;
(lo, hi) = (f64::NEG_INFINITY, f64::INFINITY);
}
}
let before = self.points.len();
let mut it = keep.iter();
self.points.retain(|_| it.next().copied().unwrap_or(true));
before - self.points.len()
}
/// Shift points at or after `from` by `delta` (Shuffle edits, Insert Time).
pub fn shift_from(&mut self, from: Samples, delta: Samples) {
for p in &mut self.points {
if p.at >= from {
p.at = p.at.saturating_add(delta).max(0);
}
}
self.points.sort_by_key(|p| p.at);
self.points.dedup_by_key(|p| p.at);
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn parse_keeps_plugin_param_case() {
let p = AutoParam::Plugin { slot: 0, param: "Cutoff".into() };
assert_eq!(AutoParam::parse("plugin:a:Cutoff"), Some(p));
assert_eq!(AutoParam::parse("Plugin:B:Mix"), Some(AutoParam::Plugin { slot: 1, param: "Mix".into() }));
assert_eq!(AutoParam::parse("Volume"), Some(AutoParam::Volume));
assert_eq!(AutoParam::parse("SEND_B_LEVEL"), Some(AutoParam::SendLevel(1)));
}
#[test]
fn value_at_far_apart_points_does_not_overflow() {
let mut l = AutomationLane::new(AutoParam::Volume);
l.set_point(-1, 0.0);
l.set_point(i64::MAX, -10.0);
let v = l.value_at(5, 0.0);
assert!(v.is_finite() && (-10.0..=0.0).contains(&v));
let w = l.value_at(i64::MAX - 1, 0.0);
assert!(w.is_finite() && (-10.0..=0.0).contains(&w));
}
#[test]
fn value_at_opposite_sign_extremes_stays_finite() {
let mut l = AutomationLane::new(AutoParam::Plugin { slot: 0, param: "Cutoff".into() });
l.set_point(0, -3.0e38);
l.set_point(1000, 3.0e38);
let v = l.value_at(500, 0.0);
assert!(v.is_finite() && v.abs() < 1.0e32, "mid value {v}");
let w = l.value_at(1, 0.0);
assert!(w.is_finite() && w < -2.9e38, "early value {w}");
}
#[test]
fn pan_label_max_does_not_overflow() {
assert_eq!(AutoParam::Pan(255).label(), "pan 256");
}
#[test]
fn interpolation_and_steps() {
let mut l = AutomationLane::new(AutoParam::Volume);
assert_eq!(l.value_at(10, -3.0), -3.0);
l.set_point(0, 0.0);
l.set_point(100, -10.0);
assert!((l.value_at(50, 0.0) + 5.0).abs() < 1e-6);
assert_eq!(l.value_at(1000, 0.0), -10.0);
let mut m = AutomationLane::new(AutoParam::Mute);
m.set_point(0, 0.0);
m.set_point(100, 1.0);
assert_eq!(m.value_at(99, 0.0), 0.0);
assert_eq!(m.value_at(100, 0.0), 1.0);
}
#[test]
fn write_range_keeps_point_right_after_end() {
let mut l = AutomationLane::new(AutoParam::Volume);
l.set_point(0, 0.0);
l.set_point(11, -100.0);
l.write_range(2, 10, -20.0, 0.0);
assert_eq!(l.value_at(11, 0.0), -100.0);
}
#[test]
fn write_range_preserves_outside() {
let mut l = AutomationLane::new(AutoParam::Volume);
l.set_point(0, 0.0);
l.set_point(1000, 0.0);
l.write_range(200, 400, -20.0, 0.0);
assert_eq!(l.value_at(100, 0.0), 0.0);
assert_eq!(l.value_at(300, 0.0), -20.0);
assert_eq!(l.value_at(600, 0.0), 0.0);
}
#[test]
fn thin_removes_collinear() {
let mut l = AutomationLane::new(AutoParam::Volume);
for i in 0..=10 {
l.set_point(i * 10, -(i as f32));
}
assert_eq!(l.thin(0.01), 9);
assert_eq!(l.points.len(), 2);
}
#[test]
fn thin_keeps_removed_points_within_tolerance() {
for direction in [1.0, -1.0] {
let mut lane = AutomationLane::new(AutoParam::Volume);
for (at, value) in [(0, 0.0), (100, 0.0), (200, 0.0), (300, 0.12), (400, 0.26)] {
lane.set_point(at, value * direction);
}
let original = lane.points.clone();
let tolerance = 0.1;
assert!(lane.thin(tolerance) > 0);
for point in original {
let error = (lane.value_at(point.at, 0.0) - point.value).abs();
assert!(error <= tolerance, "at {}: error {error} exceeds {tolerance}", point.at);
}
}
}
#[test]
fn thin_keeps_mute_changes_in_place() {
for param in [AutoParam::Mute, AutoParam::SendMute(0)] {
let mut lane = AutomationLane::new(param);
for (at, value) in [(0, 0.0), (9000, 1.0), (10000, 1.0)] {
lane.set_point(at, value);
}
lane.thin(0.1);
assert_eq!(lane.value_at(8999, 0.0), 0.0);
assert_eq!(lane.value_at(9000, 0.0), 1.0);
}
}
#[test]
fn param_ids() {
assert_eq!(AutoParam::parse("send_b_level"), Some(AutoParam::SendLevel(1)));
assert_eq!(AutoParam::parse("plugin:a:threshold"), Some(AutoParam::Plugin { slot: 0, param: "threshold".into() }));
assert_eq!(AutoParam::parse("nope"), None);
assert_eq!(AutomationMode::from_id("touch/latch"), Some(AutomationMode::TouchLatch));
}
#[test]
fn set_point_clamps_nan() {
let mut l = AutomationLane::new(AutoParam::Pan(0));
l.set_point(0, f32::NAN);
l.set_point(1, 5.0);
assert_eq!(l.points[0].value, 0.0);
assert_eq!(l.points[1].value, 1.0);
}
}
-101
View File
@@ -1,101 +0,0 @@
//! Standard base64 (RFC 4648, `+/` alphabet, `=` padding), used to keep opaque plugin state
//! blobs in the JSON session file. Decoding treats its input as hostile: it rejects bad
//! characters and bad padding, ignores ASCII whitespace, and caps the output size.
/// Largest blob [`decode`] will produce (64 MiB): bigger session fields are rejected.
pub const MAX_DECODED_BYTES: usize = 64 << 20;
const ALPHABET: &[u8; 64] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
/// Encodes bytes as padded standard base64.
pub fn encode(data: &[u8]) -> String {
let mut out = String::with_capacity(data.len().div_ceil(3).saturating_mul(4));
for chunk in data.chunks(3) {
let b = [chunk.first().copied().unwrap_or(0), chunk.get(1).copied().unwrap_or(0), chunk.get(2).copied().unwrap_or(0)];
let n = (u32::from(b[0]) << 16) | (u32::from(b[1]) << 8) | u32::from(b[2]);
let sym = |shift: u32| char::from(ALPHABET.get(((n >> shift) & 63) as usize).copied().unwrap_or(b'A'));
out.push(sym(18));
out.push(sym(12));
out.push(if chunk.len() > 1 { sym(6) } else { '=' });
out.push(if chunk.len() > 2 { sym(0) } else { '=' });
}
out
}
fn value(c: u8) -> Option<u32> {
let v = match c {
b'A'..=b'Z' => c - b'A',
b'a'..=b'z' => c - b'a' + 26,
b'0'..=b'9' => c - b'0' + 52,
b'+' => 62,
b'/' => 63,
_ => return None,
};
Some(u32::from(v))
}
/// Decodes padded (or unpadded) standard base64. `None` for malformed input or a result larger
/// than [`MAX_DECODED_BYTES`].
pub fn decode(text: &str) -> Option<Vec<u8>> {
if text.bytes().filter(|c| !c.is_ascii_whitespace()).count() > MAX_DECODED_BYTES.div_ceil(3).saturating_mul(4) {
return None;
}
let syms: Vec<u8> = text.bytes().filter(|c| !c.is_ascii_whitespace()).collect();
let body = syms.strip_suffix(b"==").or_else(|| syms.strip_suffix(b"=")).unwrap_or(&syms);
if body.contains(&b'=') || (syms.len() != body.len() && !syms.len().is_multiple_of(4)) || body.len() % 4 == 1 {
return None;
}
let decoded_len = body.len() / 4 * 3 + body.len() % 4 * 3 / 4;
if decoded_len > MAX_DECODED_BYTES {
return None;
}
let mut out = Vec::with_capacity(decoded_len);
for chunk in body.chunks(4) {
let mut n = 0u32;
for (i, &c) in chunk.iter().enumerate() {
n |= value(c)? << (18 - 6 * i as u32);
}
let bytes = [(n >> 16) as u8, (n >> 8) as u8, n as u8];
out.extend_from_slice(bytes.get(..chunk.len() - 1)?);
}
Some(out)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn rfc4648_vectors_round_trip() {
for (plain, enc) in
[("", ""), ("f", "Zg=="), ("fo", "Zm8="), ("foo", "Zm9v"), ("foob", "Zm9vYg=="), ("fooba", "Zm9vYmE="), ("foobar", "Zm9vYmFy")]
{
assert_eq!(encode(plain.as_bytes()), enc);
assert_eq!(decode(enc).unwrap(), plain.as_bytes());
}
let all: Vec<u8> = (0..=255).collect();
assert_eq!(decode(&encode(&all)).unwrap(), all);
assert_eq!(decode("Zm9v\nYmE=").unwrap(), b"fooba", "whitespace is ignored");
assert_eq!(decode("Zm9vYmE").unwrap(), b"fooba", "padding is optional");
}
#[test]
fn hostile_input_is_rejected() {
for bad in ["Z", "Zm9=v", "Zm9v!", "====", "Zg=", "é", "Zg===", "Z==="] {
assert!(decode(bad).is_none(), "{bad}");
}
}
#[test]
fn size_cap_counts_the_partial_quartet() {
for (extra, ok) in [(-1, true), (0, true), (1, false), (2, false)] {
let data = vec![7u8; MAX_DECODED_BYTES.saturating_add_signed(extra)];
let padded = encode(&data);
let unpadded = padded.trim_end_matches('=');
assert_eq!(decode(&padded).is_some(), ok, "padded, {extra}");
assert_eq!(decode(unpadded).is_some(), ok, "unpadded, {extra}");
let spaced = format!("{}\n {}", &padded[..8], &padded[8..]);
assert_eq!(decode(&spaced).is_some(), ok, "whitespace, {extra}");
}
}
}
-544
View File
@@ -1,544 +0,0 @@
//! Clips: windows onto audio sources or MIDI sequences, placed on a playlist.
use crate::{ClipId, MAX_NAME_CHARS, SourceId, truncate_chars};
use soundcraft_midi::Sequence;
use soundcraft_time::{MAX_POSITION, Range, Samples};
/// Fade curve shapes (Pro Tools offers standard, equal-power, S-curve and custom presets).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default, serde::Serialize, serde::Deserialize)]
pub enum FadeShape {
Linear,
#[default]
EqualPower,
SCurve,
Exponential,
Logarithmic,
}
impl FadeShape {
pub const ALL: [FadeShape; 5] = [FadeShape::Linear, FadeShape::EqualPower, FadeShape::SCurve, FadeShape::Exponential, FadeShape::Logarithmic];
/// Fade-in gain at `t` ∈ [0,1] (fade-outs use `gain(1 - t)`).
pub fn gain(self, t: f32) -> f32 {
let t = if t.is_nan() { 0.0 } else { t.clamp(0.0, 1.0) };
match self {
FadeShape::Linear => t,
FadeShape::EqualPower => (t * std::f32::consts::FRAC_PI_2).sin(),
FadeShape::SCurve => 0.5 - 0.5 * (t * std::f32::consts::PI).cos(),
FadeShape::Exponential => t * t * t,
FadeShape::Logarithmic => 1.0 - (1.0 - t) * (1.0 - t) * (1.0 - t),
}
}
pub fn label(self) -> &'static str {
match self {
FadeShape::Linear => "Linear",
FadeShape::EqualPower => "Equal Power",
FadeShape::SCurve => "S-Curve",
FadeShape::Exponential => "Exponential",
FadeShape::Logarithmic => "Logarithmic",
}
}
pub fn from_id(s: &str) -> Option<FadeShape> {
let s = s.to_ascii_lowercase().replace(['-', '_', ' '], "");
FadeShape::ALL.into_iter().find(|f| f.label().to_ascii_lowercase().replace(['-', ' '], "") == s)
}
}
/// A fade at a clip boundary, `len` samples long.
#[derive(Debug, Clone, Copy, PartialEq, Default, serde::Serialize, serde::Deserialize)]
pub struct Fade {
pub len: Samples,
pub shape: FadeShape,
/// Continuous bend (-1..1); zero preserves the original shape exactly.
#[serde(default)]
pub curvature: f32,
}
impl Fade {
/// The envelope used by both rendering and audio, with fixed zero/unity endpoints.
pub fn gain(self, t: f32) -> f32 {
let base = self.shape.gain(t);
let curvature = if self.curvature.is_finite() { self.curvature.clamp(-1.0, 1.0) } else { 0.0 };
if curvature == 0.0 || base <= 0.0 || base >= 1.0 {
return base;
}
let weighted = base * (curvature * 4.0).exp2();
weighted / (weighted + 1.0 - base)
}
/// Invert the bend at the midpoint so a vertical drag follows the pointer.
pub fn curvature_for_midpoint_gain(self, gain: f32) -> f32 {
let gain = if gain.is_finite() { gain.clamp(0.001, 0.999) } else { self.gain(0.5) };
let base = self.shape.gain(0.5);
(((gain / (1.0 - gain)).log2() - (base / (1.0 - base)).log2()) / 4.0).clamp(-1.0, 1.0)
}
pub fn sanitize(&mut self) {
self.curvature = if self.curvature.is_finite() { self.curvature.clamp(-1.0, 1.0) } else { 0.0 };
}
}
/// What a clip plays.
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
pub enum ClipContent {
/// Audio from `source`, starting `offset` samples into the file.
Audio { source: SourceId, offset: Samples },
/// MIDI notes, with ticks relative to the clip start.
Midi { sequence: Sequence },
/// Picture from the movie `source` ([`crate::VideoSource`], `Session::videos`), starting
/// `offset` samples into the movie. Video clips carry no audio.
Video { source: SourceId, offset: Samples },
}
/// A clip on a playlist.
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
pub struct Clip {
pub id: ClipId,
pub name: String,
/// Timeline start (samples).
pub start: Samples,
/// Length (samples, > 0).
pub length: Samples,
pub content: ClipContent,
/// Static clip gain in dB.
#[serde(default)]
pub gain_db: f32,
/// Clip gain envelope breakpoints: (offset from clip start, dB). Empty = flat `gain_db`.
#[serde(default)]
pub gain_env: Vec<(Samples, f32)>,
#[serde(default)]
pub fade_in: Fade,
#[serde(default)]
pub fade_out: Fade,
#[serde(default)]
pub muted: bool,
#[serde(default)]
pub edit_locked: bool,
#[serde(default)]
pub time_locked: bool,
#[serde(default)]
pub rating: u8,
/// Sync point offset from clip start.
#[serde(default)]
pub sync_point: Samples,
/// Optional colour override (RGB).
#[serde(default)]
pub color: Option<[u8; 3]>,
/// The key in [`crate::Session::auto_names`] of the names that editing gives the clips it
/// separates from this one (Clip List › Auto Rename). `None` keeps the clip's name.
#[serde(default)]
pub auto_name: Option<u64>,
/// Elastic / Vari-speed ratio (1.0 = none) — applied by rendered processing.
#[serde(default = "one")]
pub stretch: f64,
/// Reversed playback (AudioSuite Reverse renders into a new source; this is for previews).
#[serde(default)]
pub group: Option<u64>,
/// Marks the clips made by `clip.loop` (the origin and its copies share one id), so
/// `clip.unloop` still recognises the loop after the source's content is edited.
#[serde(default)]
pub loop_id: Option<u64>,
}
fn one() -> f64 {
1.0
}
impl Clip {
pub fn audio(id: ClipId, name: impl Into<String>, source: SourceId, offset: Samples, start: Samples, length: Samples) -> Clip {
Clip {
id,
name: name.into(),
start,
length: length.max(1),
content: ClipContent::Audio { source, offset },
gain_db: 0.0,
gain_env: Vec::new(),
fade_in: Fade::default(),
fade_out: Fade::default(),
muted: false,
edit_locked: false,
time_locked: false,
rating: 0,
sync_point: 0,
color: None,
auto_name: None,
stretch: 1.0,
group: None,
loop_id: None,
}
}
pub fn midi(id: ClipId, name: impl Into<String>, start: Samples, length: Samples, sequence: Sequence) -> Clip {
let mut c = Clip::audio(id, name, SourceId(0), 0, start, length);
c.content = ClipContent::Midi { sequence };
c
}
/// A clip showing the movie `source` from `offset` samples into it.
pub fn video(id: ClipId, name: impl Into<String>, source: SourceId, offset: Samples, start: Samples, length: Samples) -> Clip {
let mut c = Clip::audio(id, name, SourceId(0), 0, start, length);
c.content = ClipContent::Video { source, offset };
c
}
pub fn end(&self) -> Samples {
self.start.saturating_add(self.length)
}
pub fn range(&self) -> Range {
Range { start: self.start, end: self.end() }
}
pub fn is_audio(&self) -> bool {
matches!(self.content, ClipContent::Audio { .. })
}
pub fn is_video(&self) -> bool {
matches!(self.content, ClipContent::Video { .. })
}
/// The audio source this clip plays (None for MIDI and video clips).
pub fn source(&self) -> Option<SourceId> {
match self.content {
ClipContent::Audio { source, .. } => Some(source),
ClipContent::Midi { .. } | ClipContent::Video { .. } => None,
}
}
/// The movie this clip shows (video clips only).
pub fn video_source(&self) -> Option<SourceId> {
match self.content {
ClipContent::Video { source, .. } => Some(source),
ClipContent::Audio { .. } | ClipContent::Midi { .. } => None,
}
}
/// Offset into the audio file or movie (0 for MIDI).
pub fn source_offset(&self) -> Samples {
match self.content {
ClipContent::Audio { offset, .. } | ClipContent::Video { offset, .. } => offset,
ClipContent::Midi { .. } => 0,
}
}
/// Gain (linear) at `rel` samples from the clip start: clip gain × envelope × fades.
pub fn gain_at(&self, rel: Samples) -> f32 {
self.gain_at_with_fades(rel, self.fade_in, self.fade_out)
}
/// Evaluate temporary fades while retaining clip gain and its envelope.
/// UI previews can use this without modifying the clip or rebuilding media.
pub fn gain_at_with_fades(&self, rel: Samples, fade_in: Fade, fade_out: Fade) -> f32 {
let mut db = self.gain_db;
if !self.gain_env.is_empty() {
db += env_value(&self.gain_env, rel);
}
let mut g = db_to_gain(db);
if fade_in.len > 0 && rel < fade_in.len {
g *= fade_in.gain(rel as f32 / fade_in.len as f32);
}
let from_end = self.length.saturating_sub(rel);
if fade_out.len > 0 && from_end <= fade_out.len {
g *= fade_out.gain(from_end as f32 / fade_out.len as f32);
}
g
}
/// Gain envelope value in dB at `rel` samples from the clip start (0 without an envelope).
pub fn env_db_at(&self, rel: Samples) -> f32 {
env_value(&self.gain_env, rel)
}
/// Move the start edge to `new_start` keeping material anchored (trim start).
pub fn trim_start_to(&mut self, new_start: Samples) {
// #907: these are public APIs, so they have to survive arguments a command would never
// pass (commands clamp to `MAX_POSITION` first). `Samples` is `i64`, so an extreme
// `new_start` against an extreme `start` wrapped in release and panicked in debug.
// Never so far back that the length would pass `MAX_POSITION`: trimming the start keeps the end.
let end = self.end();
let new_start = new_start.max(end.saturating_sub(MAX_POSITION)).clamp(-MAX_POSITION, MAX_POSITION).min(end.saturating_sub(1));
let delta = new_start.saturating_sub(self.start);
// The mixer plays a stretched audio clip from `offset + rel / stretch`.
let stretch = if self.stretch.is_finite() && self.stretch > 0.0 { self.stretch } else { 1.0 };
match &mut self.content {
ClipContent::Audio { offset, .. } if (stretch - 1.0).abs() >= 1e-9 => {
*offset = offset.saturating_add((delta as f64 / stretch).round() as Samples);
}
ClipContent::Audio { offset, .. } | ClipContent::Video { offset, .. } => {
*offset = offset.saturating_add(delta);
}
ClipContent::Midi { .. } => {}
}
// MIDI note ticks are re-anchored by the engine, which knows the tempo map.
self.length = self.length.saturating_sub(delta).clamp(1, MAX_POSITION);
self.start = new_start;
if self.sync_point != 0 {
self.sync_point = self.sync_point.saturating_sub(delta);
}
self.drop_stray_sync();
self.clamp_fades();
// Keep the gain at the cut when earlier breakpoints are dropped.
if self.gain_env.iter().any(|&(t, _)| t < delta) && self.gain_env.iter().all(|&(t, _)| t != delta) {
self.gain_env.insert(0, (delta, env_value(&self.gain_env, delta)));
}
self.gain_env.retain(|&(t, _)| t >= delta);
self.gain_env.iter_mut().for_each(|(t, _)| *t = t.saturating_sub(delta));
}
/// Move the end edge to `new_end`.
pub fn trim_end_to(&mut self, new_end: Samples) {
// #907: saturating, like `trim_start_to`.
let new_end = new_end.clamp(-MAX_POSITION, MAX_POSITION).max(self.start.saturating_add(1));
self.length = new_end.saturating_sub(self.start).clamp(1, MAX_POSITION);
self.drop_stray_sync();
self.clamp_fades();
}
/// Keep both fades within the clip.
pub fn clamp_fades(&mut self) {
self.fade_in.len = self.fade_in.len.clamp(0, self.length);
self.fade_out.len = self.fade_out.len.clamp(0, self.length);
}
/// A sync point (0 means none) that a trim left outside the clip no longer marks anything.
fn drop_stray_sync(&mut self) {
if !(0..self.length).contains(&self.sync_point) {
self.sync_point = 0;
}
}
/// Repair a clip read from a file: positions and lengths within ±[`MAX_POSITION`], as commands
/// keep theirs, fades within the clip, a name that can be drawn, and a usable gain and stretch.
pub fn sanitize(&mut self) {
bound(&mut self.start);
self.length = self.length.clamp(1, MAX_POSITION);
bound(&mut self.sync_point);
self.gain_env.iter_mut().for_each(|(at, _)| bound(at));
match &mut self.content {
ClipContent::Audio { offset, .. } | ClipContent::Video { offset, .. } => bound(offset),
ClipContent::Midi { sequence } => bound_sequence(sequence),
}
self.clamp_fades();
self.fade_in.sanitize();
self.fade_out.sanitize();
truncate_chars(&mut self.name, MAX_NAME_CHARS);
if !self.gain_db.is_finite() {
self.gain_db = 0.0;
}
if !self.stretch.is_finite() || self.stretch <= 0.0 {
self.stretch = 1.0;
}
}
}
pub(crate) fn bound(at: &mut Samples) {
*at = (*at).clamp(-MAX_POSITION, MAX_POSITION);
}
pub(crate) fn bound_sequence(s: &mut Sequence) {
for n in &mut s.notes {
bound(&mut n.start);
n.length = n.length.clamp(1, MAX_POSITION);
}
s.ctrls.iter_mut().for_each(|c| bound(&mut c.tick));
}
fn env_value(env: &[(Samples, f32)], at: Samples) -> f32 {
let mut prev: Option<(Samples, f32)> = None;
for &(t, v) in env {
if t >= at {
return match prev {
Some((pt, pv)) if t > pt => {
let elapsed = i128::from(at).saturating_sub(i128::from(pt));
let span = i128::from(t).saturating_sub(i128::from(pt));
pv + (v - pv) * (elapsed as f32 / span as f32)
}
_ => v,
};
}
prev = Some((t, v));
}
prev.map_or(0.0, |(_, v)| v)
}
pub(crate) fn db_to_gain(db: f32) -> f32 {
if db.is_nan() || db <= -144.0 { 0.0 } else { 10f32.powf(db.min(48.0) / 20.0) }
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn fade_shapes_hit_endpoints() {
for s in FadeShape::ALL {
assert!(s.gain(0.0).abs() < 1e-6, "{s:?}");
assert!((s.gain(1.0) - 1.0).abs() < 1e-6, "{s:?}");
assert_eq!(s.gain(f32::NAN), 0.0);
assert_eq!(FadeShape::from_id(s.label()), Some(s));
}
}
#[test]
fn fade_curvature_is_bounded_monotonic_and_inverts_at_the_midpoint() {
for shape in FadeShape::ALL {
let original = Fade { len: 1000, shape, curvature: 0.0 };
for i in 0..=128 {
let t = i as f32 / 128.0;
assert_eq!(original.gain(t), shape.gain(t), "legacy curves must be unchanged");
}
for curvature in [-1.0, -0.5, 0.0, 0.5, 1.0] {
let fade = Fade { curvature, ..original };
assert_eq!(fade.gain(0.0), 0.0);
assert_eq!(fade.gain(1.0), 1.0);
let mut previous = 0.0;
for i in 0..=128 {
let gain = fade.gain(i as f32 / 128.0);
assert!(gain.is_finite() && (0.0..=1.0).contains(&gain));
assert!(gain >= previous, "{shape:?}, {curvature}");
previous = gain;
}
assert!((fade.curvature_for_midpoint_gain(fade.gain(0.5)) - curvature).abs() < 1e-5);
}
assert!((Fade { curvature: 0.5, ..original }).gain(0.5) > original.gain(0.5));
assert!((Fade { curvature: -0.5, ..original }).gain(0.5) < original.gain(0.5));
}
}
#[test]
fn fade_curvature_loads_legacy_data_and_sanitizes_hostile_values() {
let fade: Fade = serde_json::from_str(r#"{"len":100,"shape":"SCurve"}"#).unwrap();
assert_eq!(fade.curvature, 0.0);
for curvature in [f32::NAN, f32::INFINITY, f32::NEG_INFINITY, -10.0, 10.0] {
let mut fade = Fade { curvature, ..fade };
assert!(fade.gain(0.5).is_finite());
fade.sanitize();
assert!(fade.curvature.is_finite() && (-1.0..=1.0).contains(&fade.curvature));
let roundtrip: Fade = serde_json::from_str(&serde_json::to_string(&fade).unwrap()).unwrap();
assert_eq!(roundtrip, fade);
}
}
#[test]
fn temporary_fades_keep_clip_gain_and_envelope_without_mutating_the_clip() {
let mut clip = Clip::audio(ClipId(1), "preview", SourceId(1), 0, 0, 1000);
clip.gain_db = -6.0;
clip.gain_env = vec![(0, 0.0), (1000, 6.0)];
clip.fade_in = Fade { len: 100, shape: FadeShape::SCurve, curvature: 0.25 };
clip.fade_out = Fade { len: 100, shape: FadeShape::Logarithmic, curvature: -0.1 };
let before = clip.clone();
let fade_in = Fade { len: 1000, shape: FadeShape::Linear, curvature: 0.5 };
let fade_out = Fade { len: 1000, shape: FadeShape::Linear, curvature: -0.5 };
assert!((clip.gain_at_with_fades(500, fade_in, fade_out) - db_to_gain(-3.0) * 0.16).abs() < 1e-6);
let mut committed = clip.clone();
committed.fade_in = fade_in;
committed.fade_out = fade_out;
for rel in [0, 50, 500, 999, 1000] {
assert_eq!(clip.gain_at_with_fades(rel, fade_in, fade_out), committed.gain_at(rel));
}
for rel in [i64::MIN, i64::MAX] {
assert!(clip.gain_at(rel).is_finite());
assert!(clip.gain_at_with_fades(rel, fade_in, fade_out).is_finite());
}
assert_eq!(clip, before);
clip.gain_env = vec![(i64::MIN, 0.0), (i64::MAX, 6.0)];
assert!((clip.gain_at(500) - db_to_gain(-3.0)).abs() < 1e-6);
assert!((clip.gain_at_with_fades(500, fade_in, fade_out) - db_to_gain(-3.0) * 0.16).abs() < 1e-6);
}
#[test]
fn gain_with_fades() {
let mut c = Clip::audio(ClipId(1), "a", SourceId(1), 0, 100, 1000);
c.fade_in = Fade { len: 100, shape: FadeShape::Linear, curvature: 0.0 };
c.fade_out = Fade { len: 100, shape: FadeShape::Linear, curvature: 0.0 };
assert!(c.gain_at(0).abs() < 1e-6);
assert!((c.gain_at(50) - 0.5).abs() < 1e-6);
assert!((c.gain_at(500) - 1.0).abs() < 1e-6);
assert!((c.gain_at(950) - 0.5).abs() < 1e-6);
}
#[test]
fn gain_envelope_interpolates() {
let mut c = Clip::audio(ClipId(1), "a", SourceId(1), 0, 0, 1000);
c.gain_env = vec![(0, 0.0), (1000, -20.0)];
assert!((c.gain_at(500) - db_to_gain(-10.0)).abs() < 1e-4);
}
#[test]
fn trim_start_keeps_gain_envelope_at_cut() {
for (env, cut, expected) in [
(vec![(0, 0.0), (1000, -20.0)], 500, vec![(0, -10.0), (500, -20.0)]),
(vec![(0, 0.0), (100, -20.0)], 500, vec![(0, -20.0)]),
(vec![(0, 0.0), (500, -10.0), (1000, -20.0)], 500, vec![(0, -10.0), (500, -20.0)]),
(vec![], 500, vec![]),
(vec![(100, -3.0)], 0, vec![(100, -3.0)]),
(vec![(100, -3.0)], -100, vec![(200, -3.0)]),
] {
let mut clip = Clip::audio(ClipId(1), "a", SourceId(1), 100, 0, 1000);
clip.gain_env = env;
let gain_at_cut = clip.gain_at(cut);
clip.trim_start_to(cut);
assert_eq!(clip.gain_env, expected);
if cut > 0 {
assert!((clip.gain_at(0) - gain_at_cut).abs() < 1e-6);
}
}
}
#[test]
fn trims_keep_material_anchored() {
let mut c = Clip::audio(ClipId(1), "a", SourceId(1), 1000, 100, 1000);
c.trim_start_to(300);
assert_eq!((c.start, c.length, c.source_offset()), (300, 800, 1200));
c.trim_end_to(500);
assert_eq!(c.length, 200);
c.trim_end_to(-50);
assert_eq!(c.length, 1);
// Regression: trimming the start must not leave a fade-out longer than the clip.
let mut c = Clip::audio(ClipId(2), "b", SourceId(1), 0, 0, 1000);
c.fade_out = Fade { len: 800, shape: FadeShape::Linear, curvature: 0.0 };
c.trim_start_to(900);
assert!(c.fade_out.len <= c.length);
}
/// #907: `trim_start_to` / `trim_end_to` are public APIs, so they have to survive arguments a
/// command would never pass. `Samples` is `i64`, so an extreme `new_start` against an extreme
/// `start` wrapped in release and panicked in debug. Every position stays within
/// `±MAX_POSITION` and every length within `1..=MAX_POSITION`.
#[test]
fn trims_saturate_on_extreme_arguments() {
for &new_start in &[Samples::MIN, -(MAX_POSITION * 2), -1, 0, MAX_POSITION, MAX_POSITION * 2, Samples::MAX] {
let mut c = Clip::audio(ClipId(1), "a", SourceId(1), 1000, MAX_POSITION / 2, 1000);
c.trim_start_to(new_start);
assert!((-(MAX_POSITION as i128)..=(MAX_POSITION as i128)).contains(&(c.start as i128)), "start {new_start}: {}", c.start);
assert!((1..=MAX_POSITION).contains(&c.length), "start {new_start}: length {}", c.length);
assert_eq!(c.end(), MAX_POSITION / 2 + 1000, "start {new_start} moved the end");
}
for &new_end in &[Samples::MIN, -(MAX_POSITION * 2), -1, 0, MAX_POSITION, MAX_POSITION * 2, Samples::MAX] {
let mut c = Clip::audio(ClipId(2), "b", SourceId(1), 1000, -(MAX_POSITION / 2), 1000);
c.trim_end_to(new_end);
assert!((1..=MAX_POSITION).contains(&c.length), "end {new_end}: length {}", c.length);
assert!(c.end() - c.start == c.length, "end {new_end}: start {} end {}", c.start, c.end());
}
}
#[test]
fn trim_start_of_stretched_audio_advances_offset_by_source_time() {
// The mixer plays source `offset + rel / stretch`, so a split at timeline 1000 of a 2x clip
// must start the right half at source sample 500.
let mut c = Clip::audio(ClipId(1), "a", SourceId(1), 0, 0, 2000);
c.stretch = 2.0;
c.trim_start_to(1000);
assert_eq!(c.source_offset(), 500);
let mut c = Clip::audio(ClipId(2), "b", SourceId(1), 0, 0, 2000);
c.stretch = 0.5;
c.trim_start_to(1000);
assert_eq!(c.source_offset(), 2000);
let mut c = Clip::audio(ClipId(3), "c", SourceId(1), 0, 0, 2000);
c.trim_start_to(1000);
assert_eq!(c.source_offset(), 1000);
}
}
-21
View File
@@ -1,21 +0,0 @@
//! Typed ids. All ids come from one session-wide counter so they never collide across kinds.
macro_rules! id_type {
($name:ident) => {
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Default, serde::Serialize, serde::Deserialize)]
#[serde(transparent)]
pub struct $name(pub u64);
impl std::fmt::Display for $name {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}
};
}
id_type!(TrackId);
id_type!(ClipId);
id_type!(SourceId);
id_type!(BusId);
id_type!(GroupId);
id_type!(MarkerId);
+4 -29
View File
@@ -1,32 +1,7 @@
//! The SoundCraft session document.
//! Compatibility facade for SoundCraft's shared session document crate.
//!
//! A [`Session`] is pure data: tracks with playlists of clips, the mixer (inserts, sends, routing,
//! automation), tempo/meter, memory locations and groups. Audio sample data lives in
//! [`SourcePool`] behind `Arc`s, so cloning a session for undo is cheap.
//! Engine and UI adapters depend on this name, while the wire schema, JSON reader/writer,
//! validation, and bundle format live in `craft-scraft-format`.
#![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)]
mod automation;
pub mod b64;
mod clip;
mod ids;
mod markers;
mod mixer;
mod session;
mod source;
mod track;
pub use automation::{AutoParam, AutomationLane, AutomationMode, AutomationPoint};
pub use clip::{Clip, ClipContent, Fade, FadeShape};
pub use ids::{BusId, ClipId, GroupId, MarkerId, SourceId, TrackId};
pub use markers::{Group, MarkerKind, MemoryLocation};
pub use mixer::{
Bus, ChannelFormat, INSERT_SLOTS, Insert, Mixer, OutputPath, Route, SEND_SLOTS, SendSlot, Speaker, SurroundPan, fader_db_to_pos, fader_pos_to_db,
};
pub use session::{
AutoName, BitDepthSetting, EditMode, EditState, FEET_FRAMES_START, MAX_COMMENT_CHARS, MAX_NAME_CHARS, SESSION_EXTENSION, Session, SessionError,
Tool, TrackView, ZoomState, ZoomToggle, truncate_chars,
};
pub use source::{FileFormat, Source, SourceAudio, SourcePool, VideoSource};
pub use track::{Playlist, TRACK_COLORS, Track, TrackHeight, TrackKind};
pub use soundcraft_time as time;
pub use craft_scraft_format::*;
-58
View File
@@ -1,58 +0,0 @@
//! Memory locations (markers and stored selections) and mix/edit groups.
use crate::{GroupId, MarkerId, TrackId};
use soundcraft_time::Samples;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default, serde::Serialize, serde::Deserialize)]
pub enum MarkerKind {
/// A point marker on a ruler.
#[default]
Marker,
/// A stored selection range.
Selection,
/// A location that only recalls view settings.
None,
}
/// A memory location (Window › Memory Locations).
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
pub struct MemoryLocation {
pub id: MarkerId,
/// User-visible number (1-based, unique).
pub number: u32,
pub name: String,
pub kind: MarkerKind,
pub start: Samples,
pub end: Samples,
#[serde(default)]
pub comments: String,
#[serde(default)]
pub color: Option<[u8; 3]>,
/// Marker ruler lane 1..5.
#[serde(default = "one")]
pub ruler: u8,
/// Track-lane marker on this track, if any.
#[serde(default)]
pub track: Option<TrackId>,
}
fn one() -> u8 {
1
}
/// A track group (Track › Group…).
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
pub struct Group {
pub id: GroupId,
pub name: String,
/// Group letter id shown in the Groups list ("a", "b", …).
pub letter: char,
pub members: Vec<TrackId>,
pub edit: bool,
pub mix: bool,
pub active: bool,
pub color: [u8; 3],
/// Shared attributes linked by the group (volume, mute, solo, rec, etc.).
#[serde(default)]
pub attributes: Vec<String>,
}
-714
View File
@@ -1,714 +0,0 @@
//! Per-track mixer state: inserts, sends, routing, fader, pan, mute/solo.
use crate::{AutomationMode, BusId, SourceId};
use std::collections::BTreeMap;
pub const INSERT_SLOTS: usize = 10;
pub const SEND_SLOTS: usize = 10;
/// Channel formats (track widths).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default, serde::Serialize, serde::Deserialize)]
pub enum ChannelFormat {
#[default]
Mono,
Stereo,
Lcr,
Quad,
Lcrs,
Surround50,
Surround51,
Surround60,
Surround61,
Sdds70,
Sdds71,
Surround70,
Surround71,
Atmos702,
Atmos712,
Atmos502,
Atmos512,
Atmos504,
Atmos514,
Atmos704,
Atmos714,
Atmos706,
Atmos716,
Atmos904,
Atmos914,
Atmos906,
Atmos916,
/// Ambisonics of order 1..=7 ((n+1)² channels).
Ambisonics(u8),
}
impl ChannelFormat {
pub const ALL: [ChannelFormat; 34] = [
ChannelFormat::Mono,
ChannelFormat::Stereo,
ChannelFormat::Lcr,
ChannelFormat::Quad,
ChannelFormat::Lcrs,
ChannelFormat::Surround50,
ChannelFormat::Surround51,
ChannelFormat::Surround60,
ChannelFormat::Surround61,
ChannelFormat::Sdds70,
ChannelFormat::Sdds71,
ChannelFormat::Surround70,
ChannelFormat::Surround71,
ChannelFormat::Atmos702,
ChannelFormat::Atmos712,
ChannelFormat::Ambisonics(1),
ChannelFormat::Ambisonics(2),
ChannelFormat::Ambisonics(3),
ChannelFormat::Ambisonics(4),
ChannelFormat::Ambisonics(5),
ChannelFormat::Ambisonics(6),
ChannelFormat::Ambisonics(7),
ChannelFormat::Atmos502,
ChannelFormat::Atmos512,
ChannelFormat::Atmos504,
ChannelFormat::Atmos514,
ChannelFormat::Atmos704,
ChannelFormat::Atmos714,
ChannelFormat::Atmos706,
ChannelFormat::Atmos716,
ChannelFormat::Atmos904,
ChannelFormat::Atmos914,
ChannelFormat::Atmos906,
ChannelFormat::Atmos916,
];
pub fn channels(self) -> usize {
match self {
ChannelFormat::Mono => 1,
ChannelFormat::Stereo => 2,
ChannelFormat::Lcr => 3,
ChannelFormat::Quad | ChannelFormat::Lcrs => 4,
ChannelFormat::Surround50 => 5,
ChannelFormat::Surround51 | ChannelFormat::Surround60 => 6,
ChannelFormat::Surround61 | ChannelFormat::Sdds70 | ChannelFormat::Surround70 | ChannelFormat::Atmos502 => 7,
ChannelFormat::Sdds71 | ChannelFormat::Surround71 | ChannelFormat::Atmos512 => 8,
ChannelFormat::Atmos702 | ChannelFormat::Atmos504 => 9,
ChannelFormat::Atmos712 | ChannelFormat::Atmos514 => 10,
ChannelFormat::Atmos704 => 11,
ChannelFormat::Atmos714 => 12,
ChannelFormat::Atmos706 | ChannelFormat::Atmos904 => 13,
ChannelFormat::Atmos716 | ChannelFormat::Atmos914 => 14,
ChannelFormat::Atmos906 => 15,
ChannelFormat::Atmos916 => 16,
ChannelFormat::Ambisonics(n) => {
let n = usize::from(n.clamp(1, 7)) + 1;
n * n
}
}
}
pub fn label(self) -> &'static str {
match self {
ChannelFormat::Mono => "Mono",
ChannelFormat::Stereo => "Stereo",
ChannelFormat::Lcr => "LCR",
ChannelFormat::Quad => "Quad",
ChannelFormat::Lcrs => "LCRS",
ChannelFormat::Surround50 => "5.0",
ChannelFormat::Surround51 => "5.1",
ChannelFormat::Surround60 => "6.0",
ChannelFormat::Surround61 => "6.1",
ChannelFormat::Sdds70 => "7.0 SDDS",
ChannelFormat::Sdds71 => "7.1 SDDS",
ChannelFormat::Surround70 => "7.0",
ChannelFormat::Surround71 => "7.1",
ChannelFormat::Atmos702 => "7.0.2",
ChannelFormat::Atmos712 => "7.1.2",
ChannelFormat::Atmos502 => "5.0.2",
ChannelFormat::Atmos512 => "5.1.2",
ChannelFormat::Atmos504 => "5.0.4",
ChannelFormat::Atmos514 => "5.1.4",
ChannelFormat::Atmos704 => "7.0.4",
ChannelFormat::Atmos714 => "7.1.4",
ChannelFormat::Atmos706 => "7.0.6",
ChannelFormat::Atmos716 => "7.1.6",
ChannelFormat::Atmos904 => "9.0.4",
ChannelFormat::Atmos914 => "9.1.4",
ChannelFormat::Atmos906 => "9.0.6",
ChannelFormat::Atmos916 => "9.1.6",
ChannelFormat::Ambisonics(1) => "1st Order Ambisonics",
ChannelFormat::Ambisonics(2) => "2nd Order Ambisonics",
ChannelFormat::Ambisonics(3) => "3rd Order Ambisonics",
ChannelFormat::Ambisonics(4) => "4th Order Ambisonics",
ChannelFormat::Ambisonics(5) => "5th Order Ambisonics",
ChannelFormat::Ambisonics(6) => "6th Order Ambisonics",
ChannelFormat::Ambisonics(_) => "7th Order Ambisonics",
}
}
pub fn from_id(s: &str) -> Option<ChannelFormat> {
ChannelFormat::ALL.into_iter().find(|f| f.label().eq_ignore_ascii_case(s.trim()))
}
/// Speakers in channel order, or empty for Ambisonics (ACN order, SN3D).
///
/// Channel order is SMPTE / WAV order: speakers with a WAVE_FORMAT_EXTENSIBLE mask bit come in
/// ascending bit order (L R C LFE Ls Rs Lc Rc Cs Lss Rss Ltf Rtf Ltr Rtr; 7.x puts the rear
/// surrounds Lrs/Rrs on the "back" bits before the sides Lss/Rss), followed by speakers WAV
/// cannot name (the top-middle pair of .6 layouts). E.g. 5.1 = L R C LFE Ls Rs,
/// 7.1 = L R C LFE Lrs Rrs Lss Rss, 7.1.4 = L R C LFE Lrs Rrs Lss Rss Ltf Rtf Ltr Rtr.
pub fn speakers(self) -> &'static [Speaker] {
use Speaker::*;
match self {
ChannelFormat::Mono => &[C],
ChannelFormat::Stereo => &[L, R],
ChannelFormat::Lcr => &[L, R, C],
ChannelFormat::Quad => &[L, R, Ls, Rs],
ChannelFormat::Lcrs => &[L, R, C, Cs],
ChannelFormat::Surround50 => &[L, R, C, Ls, Rs],
ChannelFormat::Surround51 => &[L, R, C, Lfe, Ls, Rs],
ChannelFormat::Surround60 => &[L, R, C, Ls, Rs, Cs],
ChannelFormat::Surround61 => &[L, R, C, Lfe, Ls, Rs, Cs],
ChannelFormat::Sdds70 => &[L, R, C, Ls, Rs, Lc, Rc],
ChannelFormat::Sdds71 => &[L, R, C, Lfe, Ls, Rs, Lc, Rc],
ChannelFormat::Surround70 => &[L, R, C, Lrs, Rrs, Lss, Rss],
ChannelFormat::Surround71 => &[L, R, C, Lfe, Lrs, Rrs, Lss, Rss],
ChannelFormat::Atmos702 => &[L, R, C, Lrs, Rrs, Lss, Rss, Ltm, Rtm],
ChannelFormat::Atmos712 => &[L, R, C, Lfe, Lrs, Rrs, Lss, Rss, Ltm, Rtm],
ChannelFormat::Atmos502 => &[L, R, C, Ls, Rs, Ltm, Rtm],
ChannelFormat::Atmos512 => &[L, R, C, Lfe, Ls, Rs, Ltm, Rtm],
ChannelFormat::Atmos504 => &[L, R, C, Ls, Rs, Ltf, Rtf, Ltr, Rtr],
ChannelFormat::Atmos514 => &[L, R, C, Lfe, Ls, Rs, Ltf, Rtf, Ltr, Rtr],
ChannelFormat::Atmos704 => &[L, R, C, Lrs, Rrs, Lss, Rss, Ltf, Rtf, Ltr, Rtr],
ChannelFormat::Atmos714 => &[L, R, C, Lfe, Lrs, Rrs, Lss, Rss, Ltf, Rtf, Ltr, Rtr],
ChannelFormat::Atmos706 => &[L, R, C, Lrs, Rrs, Lss, Rss, Ltf, Rtf, Ltr, Rtr, Ltm, Rtm],
ChannelFormat::Atmos716 => &[L, R, C, Lfe, Lrs, Rrs, Lss, Rss, Ltf, Rtf, Ltr, Rtr, Ltm, Rtm],
ChannelFormat::Atmos904 => &[L, R, C, Lrs, Rrs, Lw, Rw, Lss, Rss, Ltf, Rtf, Ltr, Rtr],
ChannelFormat::Atmos914 => &[L, R, C, Lfe, Lrs, Rrs, Lw, Rw, Lss, Rss, Ltf, Rtf, Ltr, Rtr],
ChannelFormat::Atmos906 => &[L, R, C, Lrs, Rrs, Lw, Rw, Lss, Rss, Ltf, Rtf, Ltr, Rtr, Ltm, Rtm],
ChannelFormat::Atmos916 => &[L, R, C, Lfe, Lrs, Rrs, Lw, Rw, Lss, Rss, Ltf, Rtf, Ltr, Rtr, Ltm, Rtm],
ChannelFormat::Ambisonics(_) => &[],
}
}
pub fn is_ambisonic(self) -> bool {
matches!(self, ChannelFormat::Ambisonics(_))
}
pub fn has_lfe(self) -> bool {
self.speakers().contains(&Speaker::Lfe)
}
pub fn has_height(self) -> bool {
self.speakers().iter().any(|s| s.is_height())
}
/// Short per-channel labels for meters (`W Y Z X 5…` for Ambisonics).
pub fn channel_label(self, ch: usize) -> String {
match self.speakers().get(ch) {
Some(s) => s.label().to_string(),
None if self.is_ambisonic() => ["W", "Y", "Z", "X"].get(ch).map_or_else(|| format!("{}", ch + 1), ToString::to_string),
None => format!("{}", ch + 1),
}
}
/// WAVE_FORMAT_EXTENSIBLE channel mask (0 = no positions, e.g. Ambisonics). Speakers that WAV
/// cannot name (top middle) borrow the top-front bits when the layout has no top fronts.
pub fn channel_mask(self) -> u32 {
let sp = self.speakers();
let has_tf = sp.contains(&Speaker::Ltf);
sp.iter().fold(0u32, |m, s| match s {
Speaker::Ltm if !has_tf => m | 0x1000,
Speaker::Rtm if !has_tf => m | 0x4000,
s => m | s.wave_bit(),
})
}
/// Best format for a channel count.
pub fn for_channels(n: usize) -> ChannelFormat {
[
ChannelFormat::Mono,
ChannelFormat::Stereo,
ChannelFormat::Lcr,
ChannelFormat::Quad,
ChannelFormat::Surround50,
ChannelFormat::Surround51,
ChannelFormat::Surround70,
ChannelFormat::Surround71,
]
.into_iter()
.find(|f| f.channels() == n)
.unwrap_or(if n <= 1 { ChannelFormat::Mono } else { ChannelFormat::Stereo })
}
}
/// A loudspeaker position in a channel format.
///
/// Angles follow ITU-R BS.775 / Dolby conventions: azimuth in degrees, 0 = front centre, positive
/// = to the right; elevation in degrees above the listener plane.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
pub enum Speaker {
L,
R,
C,
Lfe,
/// 5.x surrounds (±110°).
Ls,
Rs,
/// SDDS inner fronts (±15°).
Lc,
Rc,
/// Centre surround (180°).
Cs,
/// 7.x side surrounds (±90°).
Lss,
Rss,
/// 7.x rear surrounds (±150°).
Lrs,
Rrs,
/// 9.x front wides (±60°).
Lw,
Rw,
/// Top front (±45°, elevation 45°).
Ltf,
Rtf,
/// Top middle (±90°, elevation 45°).
Ltm,
Rtm,
/// Top rear (±135°, elevation 45°).
Ltr,
Rtr,
}
impl Speaker {
pub fn azimuth(self) -> f32 {
match self {
Speaker::L => -30.0,
Speaker::R => 30.0,
Speaker::C | Speaker::Lfe => 0.0,
Speaker::Ls => -110.0,
Speaker::Rs => 110.0,
Speaker::Lc => -15.0,
Speaker::Rc => 15.0,
Speaker::Cs => 180.0,
Speaker::Lss => -90.0,
Speaker::Rss => 90.0,
Speaker::Lrs => -150.0,
Speaker::Rrs => 150.0,
Speaker::Lw => -60.0,
Speaker::Rw => 60.0,
Speaker::Ltf => -45.0,
Speaker::Rtf => 45.0,
Speaker::Ltm => -90.0,
Speaker::Rtm => 90.0,
Speaker::Ltr => -135.0,
Speaker::Rtr => 135.0,
}
}
pub fn elevation(self) -> f32 {
if self.is_height() { 45.0 } else { 0.0 }
}
pub fn is_height(self) -> bool {
matches!(self, Speaker::Ltf | Speaker::Rtf | Speaker::Ltm | Speaker::Rtm | Speaker::Ltr | Speaker::Rtr)
}
pub fn is_lfe(self) -> bool {
self == Speaker::Lfe
}
pub fn label(self) -> &'static str {
match self {
Speaker::L => "L",
Speaker::R => "R",
Speaker::C => "C",
Speaker::Lfe => "LFE",
Speaker::Ls => "Ls",
Speaker::Rs => "Rs",
Speaker::Lc => "Lc",
Speaker::Rc => "Rc",
Speaker::Cs => "Cs",
Speaker::Lss => "Lss",
Speaker::Rss => "Rss",
Speaker::Lrs => "Lrs",
Speaker::Rrs => "Rrs",
Speaker::Lw => "Lw",
Speaker::Rw => "Rw",
Speaker::Ltf => "Ltf",
Speaker::Rtf => "Rtf",
Speaker::Ltm => "Ltm",
Speaker::Rtm => "Rtm",
Speaker::Ltr => "Ltr",
Speaker::Rtr => "Rtr",
}
}
/// WAVE_FORMAT_EXTENSIBLE `dwChannelMask` bit (0 for speakers WAV cannot name).
pub fn wave_bit(self) -> u32 {
match self {
Speaker::L => 0x1,
Speaker::R => 0x2,
Speaker::C => 0x4,
Speaker::Lfe => 0x8,
Speaker::Ls | Speaker::Lrs => 0x10,
Speaker::Rs | Speaker::Rrs => 0x20,
Speaker::Lc | Speaker::Lw => 0x40,
Speaker::Rc | Speaker::Rw => 0x80,
Speaker::Cs => 0x100,
Speaker::Lss => 0x200,
Speaker::Rss => 0x400,
Speaker::Ltf => 0x1000,
Speaker::Rtf => 0x4000,
Speaker::Ltr => 0x8000,
Speaker::Rtr => 0x2_0000,
Speaker::Ltm | Speaker::Rtm => 0,
}
}
}
/// Surround panner position (Pro Tools-style X/Y puck). `None` on a mixer means "use the stereo
/// pan", which keeps sessions made before surround mixing sounding exactly as they did.
#[derive(Debug, Clone, Copy, PartialEq, serde::Serialize, serde::Deserialize)]
#[serde(default)]
pub struct SurroundPan {
/// -1 (left) ..= 1 (right).
pub x: f32,
/// -1 (back) ..= 1 (front).
pub y: f32,
/// Elevation 0 (ear level) ..= 1 (overhead), used by height formats.
pub z: f32,
/// 0 = point source ..= 1 = spread over every speaker.
pub divergence: f32,
/// Centre speaker share of front-centre images, 0..=100 % (0 = phantom centre on L/R).
pub center: f32,
/// LFE send level, dB (-144 = off).
pub lfe_db: f32,
}
impl Default for SurroundPan {
fn default() -> Self {
SurroundPan { x: 0.0, y: 1.0, z: 0.0, divergence: 0.0, center: 100.0, lfe_db: -144.0 }
}
}
impl SurroundPan {
/// Puck at the stereo pan position (front edge).
pub fn from_stereo(pan: f32) -> Self {
SurroundPan { x: finite_or(pan, 0.0).clamp(-1.0, 1.0), ..SurroundPan::default() }
}
/// Clamp into range (NaN → defaults).
pub fn sanitize(&mut self) {
let d = SurroundPan::default();
self.x = finite_or(self.x, d.x).clamp(-1.0, 1.0);
self.y = finite_or(self.y, d.y).clamp(-1.0, 1.0);
self.z = finite_or(self.z, d.z).clamp(0.0, 1.0);
self.divergence = finite_or(self.divergence, d.divergence).clamp(0.0, 1.0);
self.center = finite_or(self.center, d.center).clamp(0.0, 100.0);
self.lfe_db = finite_or(self.lfe_db, d.lfe_db).clamp(-144.0, 12.0);
}
}
/// An internal mix bus (Pro Tools "bus" paths).
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
pub struct Bus {
pub id: BusId,
pub name: String,
pub format: ChannelFormat,
}
/// Physical output paths (from the I/O setup).
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
pub struct OutputPath {
pub name: String,
/// First hardware channel (0-based).
pub first_channel: u16,
pub format: ChannelFormat,
}
/// Where a signal goes or comes from.
#[derive(Debug, Clone, PartialEq, Eq, Hash, Default, serde::Serialize, serde::Deserialize)]
pub enum Route {
#[default]
None,
/// The main output (hardware outputs 1-2, or the bounce source).
Main,
Bus(BusId),
/// Hardware input/output by path name.
Hardware(String),
}
/// A plugin instance on an insert slot.
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
pub struct Insert {
/// Registry id from `soundcraft-dsp` (e.g. `eq7`, `comp`).
pub plugin: String,
#[serde(default, deserialize_with = "finite_params")]
pub params: BTreeMap<String, f32>,
#[serde(default)]
pub bypass: bool,
#[serde(default = "yes")]
pub active: bool,
/// Preset name shown in the plugin window header.
#[serde(default)]
pub preset: String,
/// Opaque plugin state (third-party plugins: CLAP state / VST3 component + controller
/// state), standard base64. Restored into a new instance before it first processes.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub state: Option<String>,
/// The session audio source a sample-playing instrument (the built-in Sampler) plays.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub sample: Option<SourceId>,
}
fn yes() -> bool {
true
}
impl Insert {
pub fn new(plugin: impl Into<String>) -> Self {
Insert {
plugin: plugin.into(),
params: BTreeMap::new(),
bypass: false,
active: true,
preset: String::from("<factory default>"),
state: None,
sample: None,
}
}
/// The decoded plugin state (`None` when absent or malformed).
pub fn state_bytes(&self) -> Option<Vec<u8>> {
self.state.as_deref().and_then(crate::b64::decode)
}
/// Stores a plugin state blob (base64-encoded); an empty blob clears it.
pub fn set_state_bytes(&mut self, data: &[u8]) {
self.state = (!data.is_empty()).then(|| crate::b64::encode(data));
}
}
/// A send to a bus.
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
pub struct SendSlot {
pub target: Route,
pub level_db: f32,
pub pan: f32,
pub mute: bool,
pub pre_fader: bool,
#[serde(default = "yes")]
pub follow_main_pan: bool,
/// Surround position into a multichannel bus (None = follow the track / stereo pan).
#[serde(default)]
pub surround: Option<SurroundPan>,
}
impl SendSlot {
pub fn new(target: Route) -> Self {
SendSlot { target, level_db: -144.0, pan: 0.0, mute: false, pre_fader: false, follow_main_pan: true, surround: None }
}
}
/// The channel strip.
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
pub struct Mixer {
pub volume_db: f32,
/// One panner per source channel for mono/stereo (stereo tracks: two panners, default hard L/R).
pub pan: Vec<f32>,
pub mute: bool,
pub solo: bool,
#[serde(default)]
pub solo_safe: bool,
/// Live record-enable state. Runtime-only: opening a session must never arm an input.
#[serde(skip)]
pub record_arm: bool,
/// Live input-monitor state. Runtime-only: opening a session must never enable monitoring.
#[serde(skip)]
pub input_monitor: bool,
#[serde(default)]
pub phase_invert: bool,
pub input: Route,
pub output: Route,
pub inserts: Vec<Option<Insert>>,
pub sends: Vec<Option<SendSlot>>,
pub automation_mode: AutomationMode,
/// Input gain / trim, dB.
#[serde(default)]
pub trim_db: f32,
/// VCA master that controls this track, if any (track id of the VCA).
#[serde(default)]
pub vca: Option<u64>,
/// Surround panner, used when the output is multichannel (None = the stereo pan to L/R).
#[serde(default)]
pub surround: Option<SurroundPan>,
}
impl Mixer {
pub fn new(format: ChannelFormat) -> Self {
let pan = match format {
ChannelFormat::Mono => vec![0.0],
ChannelFormat::Stereo => vec![-1.0, 1.0],
_ => Vec::new(),
};
Mixer {
volume_db: 0.0,
pan,
mute: false,
solo: false,
solo_safe: false,
record_arm: false,
input_monitor: false,
phase_invert: false,
input: Route::None,
output: Route::Main,
inserts: vec![None; INSERT_SLOTS],
sends: vec![None; SEND_SLOTS],
automation_mode: AutomationMode::Read,
trim_db: 0.0,
vca: None,
surround: None,
}
}
/// Clamp everything into range (after deserialising untrusted files).
pub fn sanitize(&mut self) {
self.volume_db = finite_or(self.volume_db, 0.0).clamp(-144.0, 12.0);
for p in &mut self.pan {
*p = finite_or(*p, 0.0).clamp(-1.0, 1.0);
}
self.inserts.resize(INSERT_SLOTS, None);
self.sends.resize(SEND_SLOTS, None);
for s in self.sends.iter_mut().flatten() {
s.level_db = finite_or(s.level_db, -144.0).clamp(-144.0, 12.0);
s.pan = finite_or(s.pan, 0.0).clamp(-1.0, 1.0);
if let Some(sp) = &mut s.surround {
sp.sanitize();
}
}
if let Some(sp) = &mut self.surround {
sp.sanitize();
}
self.trim_db = finite_or(self.trim_db, 0.0).clamp(-144.0, 24.0);
}
}
impl crate::Session {
/// The main mix format: the format of the first (main) output path, at least stereo and at
/// most 16 channels (wider formats fall back to stereo).
pub fn main_format(&self) -> ChannelFormat {
match self.outputs.first().map(|o| o.format) {
Some(f) if (2..=16).contains(&f.channels()) => f,
_ => ChannelFormat::Stereo,
}
}
}
pub(crate) fn finite_or(v: f32, d: f32) -> f32 {
if v.is_finite() { v } else { d }
}
/// Parameters saved as `null` (how JSON writes NaN and infinity) or beyond `f32` are dropped, so
/// they keep their defaults instead of failing the whole session.
fn finite_params<'de, D: serde::Deserializer<'de>>(d: D) -> Result<BTreeMap<String, f32>, D::Error> {
let params: BTreeMap<String, Option<f64>> = serde::Deserialize::deserialize(d)?;
Ok(params.into_iter().filter_map(|(k, v)| Some((k, v.map(|v| v as f32).filter(|v| v.is_finite())?))).collect())
}
/// Fader taper used by Pro Tools-style faders: position 0..1 ↔ dB (-inf..+12).
pub fn fader_pos_to_db(pos: f32) -> f32 {
let p = finite_or(pos, 0.0).clamp(0.0, 1.0);
if p <= 0.0 {
return -144.0;
}
// Piecewise: top 75 % covers -24..+12 linearly-ish, the bottom compresses to -inf.
if p >= 0.25 { -24.0 + (p - 0.25) / 0.75 * 36.0 } else { -24.0 - (1.0 - p / 0.25).powf(1.5) * 120.0 }
}
pub fn fader_db_to_pos(db: f32) -> f32 {
let db = finite_or(db, -144.0);
if db <= -144.0 {
return 0.0;
}
if db >= -24.0 {
(0.25 + (db.min(12.0) + 24.0) / 36.0 * 0.75).min(1.0)
} else {
let x = ((-24.0 - db) / 120.0).clamp(0.0, 1.0).powf(1.0 / 1.5);
((1.0 - x) * 0.25).max(0.0)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn insert_state_round_trips_through_json() {
let mut i = Insert::new("vst3:00");
assert_eq!(i.state_bytes(), None);
let blob: Vec<u8> = (0..=255u8).cycle().take(1000).collect();
i.set_state_bytes(&blob);
let json = serde_json::to_string(&i).unwrap();
let back: Insert = serde_json::from_str(&json).unwrap();
assert_eq!(back.state_bytes().unwrap(), blob);
i.set_state_bytes(&[]);
assert_eq!(i.state, None);
// Old sessions have no `state`; stateless inserts don't write one.
let old: Insert = serde_json::from_str(r#"{"plugin":"eq_7band"}"#).unwrap();
assert_eq!(old.state, None);
assert!(!serde_json::to_string(&old).unwrap().contains("state"));
// A hostile state string decodes to nothing rather than failing the load.
let bad: Insert = serde_json::from_str(r#"{"plugin":"x","state":"!!!"}"#).unwrap();
assert_eq!(bad.state_bytes(), None);
}
#[test]
fn params_saved_as_null_or_out_of_range_are_dropped() {
let mut i = Insert::new("eq_7band");
i.params.extend([("gain".to_string(), f32::NAN), ("q".to_string(), 0.7)]);
let back: Insert = serde_json::from_str(&serde_json::to_string(&i).unwrap()).unwrap();
assert_eq!(back.params, BTreeMap::from([("q".to_string(), 0.7)]));
let big: Insert = serde_json::from_str(r#"{"plugin":"x","params":{"gain":1e39,"q":0.5}}"#).unwrap();
assert_eq!(big.params, BTreeMap::from([("q".to_string(), 0.5)]));
}
#[test]
fn fader_taper_round_trips() {
for db in [-100.0f32, -60.0, -24.0, -10.0, 0.0, 6.0, 12.0] {
let p = fader_db_to_pos(db);
assert!((fader_pos_to_db(p) - db).abs() < 0.05, "{db} → {p}");
}
assert_eq!(fader_pos_to_db(0.0), -144.0);
assert_eq!(fader_db_to_pos(f32::NAN), 0.0);
}
#[test]
fn mixer_defaults_and_sanitize() {
let mut m = Mixer::new(ChannelFormat::Stereo);
assert_eq!(m.pan, vec![-1.0, 1.0]);
m.volume_db = f32::INFINITY;
m.inserts.truncate(2);
m.sanitize();
assert_eq!(m.volume_db, 0.0);
assert_eq!(m.inserts.len(), INSERT_SLOTS);
}
#[test]
fn speaker_layouts_match_channel_counts_and_masks() {
for f in ChannelFormat::ALL {
if !f.is_ambisonic() {
assert_eq!(f.speakers().len(), f.channels(), "{f:?}");
// Mask bits are unique and in ascending channel order.
let bits: Vec<u32> = f.speakers().iter().map(|s| s.wave_bit()).filter(|b| *b != 0).collect();
assert!(bits.windows(2).all(|w| w[0] < w[1]), "{f:?} not in WAV order");
}
}
assert_eq!(ChannelFormat::Surround51.channel_mask(), 0x3F);
assert_eq!(ChannelFormat::Surround71.channel_mask(), 0x63F);
assert_eq!(ChannelFormat::Atmos714.channel_mask(), 0x2_D63F);
assert_eq!(ChannelFormat::Ambisonics(1).channel_mask(), 0);
}
#[test]
fn surround_pan_defaults_and_old_sessions() {
let m = Mixer::new(ChannelFormat::Mono);
assert_eq!(m.surround, None);
// A mixer saved before surround existed still loads.
let mut v = serde_json::to_value(&m).unwrap();
v.as_object_mut().unwrap().remove("surround");
let back: Mixer = serde_json::from_value(v).unwrap();
assert_eq!(back.surround, None);
let mut sp: SurroundPan = serde_json::from_str(r#"{"x": 5.0}"#).unwrap();
sp.sanitize();
assert_eq!(sp.x, 1.0);
assert_eq!(sp.center, 100.0);
}
#[test]
fn formats() {
assert_eq!(ChannelFormat::for_channels(6), ChannelFormat::Surround51);
assert_eq!(ChannelFormat::from_id("stereo"), Some(ChannelFormat::Stereo));
}
}
File diff suppressed because it is too large Load Diff
-178
View File
@@ -1,178 +0,0 @@
//! Audio sources (files in the session's Audio Files folder) and their decoded data, and the
//! movies video clips reference.
use crate::SourceId;
use craft_audio::{AudioBuffer, AudioInfo, CodecId, Container, peaks::Peaks};
use std::collections::BTreeMap;
use std::sync::Arc;
/// The kind of file a source came from, as session files store it in [`Source::format`]. The
/// variant names are part of the session format: craft-audio's `Container` and `CodecId` name
/// more formats under other names, so this stays here and maps from them.
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub enum FileFormat {
Wav,
Aiff,
Flac,
Mp3,
Ogg,
Aac,
Alac,
Caf,
/// Anything else that decodes: Matroska, WebM, raw AC-3.
Other,
}
impl FileFormat {
/// The kind of the file `info` describes.
pub fn of(info: &AudioInfo) -> FileFormat {
match info.container {
Container::Wav => FileFormat::Wav,
Container::Aiff => FileFormat::Aiff,
Container::Flac => FileFormat::Flac,
Container::MpegAudio => FileFormat::Mp3,
Container::Ogg => FileFormat::Ogg,
Container::Caf => FileFormat::Caf,
Container::Mp4 | Container::QuickTime if info.codec == CodecId::Alac => FileFormat::Alac,
Container::Mp4 | Container::QuickTime | Container::Adts => FileFormat::Aac,
_ => FileFormat::Other,
}
}
}
/// Metadata for an audio file used by the session (serialised).
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
pub struct Source {
pub id: SourceId,
pub name: String,
/// Path relative to the session folder (or absolute for referenced media).
pub path: String,
pub channels: u16,
pub frames: u64,
pub sample_rate: u32,
pub format: FileFormat,
/// Original BWF time stamp (samples), if any.
#[serde(default)]
pub time_reference: u64,
/// True when the file has not been written to disk yet (recorded/rendered this session).
#[serde(default)]
pub unsaved: bool,
}
/// A movie used by Video track clips (serialised). The pictures themselves are never stored in
/// the session: the UI opens the movie from `path` and decodes frames on demand.
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
pub struct VideoSource {
pub id: SourceId,
pub name: String,
/// Absolute path of the movie (referenced, never copied into the session folder).
pub path: String,
#[serde(default)]
pub width: u32,
#[serde(default)]
pub height: u32,
/// Frames per second (0 = unknown).
#[serde(default)]
pub frame_rate: f64,
/// Picture length in seconds.
#[serde(default)]
pub duration: f64,
/// Codec id (`h264`, `prores`, `mjpeg`, `hevc`, …).
#[serde(default)]
pub codec: String,
/// Why the movie is not opened, when its stored path was refused on opening the session.
#[serde(skip)]
pub offline: Option<String>,
}
impl VideoSource {
/// Display aspect ratio (width / height), 16:9 when unknown.
pub fn aspect(&self) -> f64 {
if self.width == 0 || self.height == 0 { 16.0 / 9.0 } else { f64::from(self.width) / f64::from(self.height) }
}
}
/// Decoded audio plus waveform overviews (not serialised).
#[derive(Debug)]
pub struct SourceAudio {
pub buffer: AudioBuffer,
pub peaks: Vec<Peaks>,
}
impl SourceAudio {
pub fn new(buffer: AudioBuffer) -> Self {
let peaks = buffer.channels.iter().map(|c| Peaks::build(c)).collect();
SourceAudio { buffer, peaks }
}
pub fn frames(&self) -> usize {
self.buffer.frames()
}
pub fn channel(&self, ch: usize) -> Option<&[f32]> {
self.buffer.channels.get(ch).map(Vec::as_slice)
}
}
/// Shared decoded audio, keyed by source id.
#[derive(Debug, Clone, Default)]
pub struct SourcePool {
map: BTreeMap<SourceId, Arc<SourceAudio>>,
}
impl SourcePool {
pub fn insert(&mut self, id: SourceId, audio: Arc<SourceAudio>) {
self.map.insert(id, audio);
}
pub fn get(&self, id: SourceId) -> Option<&Arc<SourceAudio>> {
self.map.get(&id)
}
pub fn remove(&mut self, id: SourceId) -> Option<Arc<SourceAudio>> {
self.map.remove(&id)
}
pub fn contains(&self, id: SourceId) -> bool {
self.map.contains_key(&id)
}
pub fn len(&self) -> usize {
self.map.len()
}
pub fn is_empty(&self) -> bool {
self.map.is_empty()
}
pub fn ids(&self) -> impl Iterator<Item = SourceId> + '_ {
self.map.keys().copied()
}
}
impl PartialEq for SourcePool {
fn eq(&self, other: &Self) -> bool {
self.map.len() == other.map.len() && self.map.iter().zip(other.map.iter()).all(|((a, x), (b, y))| a == b && Arc::ptr_eq(x, y))
}
}
#[cfg(test)]
mod tests {
use super::FileFormat;
use craft_audio::{AudioBuffer, EncodeFormat, EncodeOptions, encode, probe};
/// Sessions written before the format code moved to craft-audio name a source's format with
/// these words; they must keep reading, and new sessions must keep writing them.
#[test]
fn the_format_names_in_session_files_do_not_change() {
use FileFormat::{Aac, Aiff, Alac, Caf, Flac, Mp3, Ogg, Other, Wav};
let names = ["Wav", "Aiff", "Flac", "Mp3", "Ogg", "Aac", "Alac", "Caf", "Other"];
for (format, name) in [Wav, Aiff, Flac, Mp3, Ogg, Aac, Alac, Caf, Other].into_iter().zip(names) {
assert_eq!(serde_json::to_string(&format).unwrap(), format!("\"{name}\""));
assert_eq!(serde_json::from_str::<FileFormat>(&format!("\"{name}\"")).unwrap(), format);
}
}
#[test]
fn a_probed_file_maps_to_its_format() {
let buf = AudioBuffer::new(48_000, 1, 480);
for (written, format) in
[(EncodeFormat::Wav, FileFormat::Wav), (EncodeFormat::Aiff, FileFormat::Aiff), (EncodeFormat::Flac, FileFormat::Flac)]
{
let info = probe(&encode(&buf, &EncodeOptions::new(written)).unwrap(), None).unwrap();
assert_eq!(FileFormat::of(&info), format);
}
}
}
-477
View File
@@ -1,477 +0,0 @@
//! Tracks and playlists.
use crate::{AutoParam, AutomationLane, ChannelFormat, Clip, ClipId, Mixer, TrackId};
use soundcraft_time::{Range, Samples};
/// Track types (Track › New).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default, serde::Serialize, serde::Deserialize)]
pub enum TrackKind {
#[default]
Audio,
Aux,
Master,
Midi,
Instrument,
Vca,
Folder,
/// Picture: clips reference a movie ([`crate::VideoSource`]); no audio path.
Video,
}
impl TrackKind {
pub const ALL: [TrackKind; 8] = [
TrackKind::Audio,
TrackKind::Aux,
TrackKind::Master,
TrackKind::Midi,
TrackKind::Instrument,
TrackKind::Vca,
TrackKind::Folder,
TrackKind::Video,
];
pub fn label(self) -> &'static str {
match self {
TrackKind::Audio => "Audio Track",
TrackKind::Aux => "Aux Input",
TrackKind::Master => "Master Fader",
TrackKind::Midi => "MIDI Track",
TrackKind::Instrument => "Instrument Track",
TrackKind::Vca => "VCA Master",
TrackKind::Folder => "Routing Folder Track",
TrackKind::Video => "Video Track",
}
}
pub fn default_name(self) -> &'static str {
match self {
TrackKind::Audio => "Audio",
TrackKind::Aux => "Aux",
TrackKind::Master => "Master",
TrackKind::Midi => "MIDI",
TrackKind::Instrument => "Inst",
TrackKind::Vca => "VCA",
TrackKind::Folder => "Folder",
TrackKind::Video => "Video",
}
}
pub fn id(self) -> &'static str {
match self {
TrackKind::Audio => "audio",
TrackKind::Aux => "aux",
TrackKind::Master => "master",
TrackKind::Midi => "midi",
TrackKind::Instrument => "instrument",
TrackKind::Vca => "vca",
TrackKind::Folder => "folder",
TrackKind::Video => "video",
}
}
pub fn from_id(s: &str) -> Option<TrackKind> {
let s = s.to_ascii_lowercase();
TrackKind::ALL.into_iter().find(|k| k.id() == s || k.label().eq_ignore_ascii_case(&s))
}
/// Track kinds that hold clips on a timeline.
pub fn has_playlist(self) -> bool {
matches!(self, TrackKind::Audio | TrackKind::Midi | TrackKind::Instrument | TrackKind::Video)
}
pub fn is_midi(self) -> bool {
matches!(self, TrackKind::Midi | TrackKind::Instrument)
}
/// Kinds that pass audio through the mixer.
pub fn is_audio_path(self) -> bool {
matches!(self, TrackKind::Audio | TrackKind::Aux | TrackKind::Master | TrackKind::Instrument | TrackKind::Folder)
}
}
/// Track heights (View › Track Height presets).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default, serde::Serialize)]
pub enum TrackHeight {
/// Any height from Micro to Extreme, in points.
Custom(u16),
Micro,
Mini,
Small,
#[default]
Medium,
Large,
Jumbo,
Extreme,
/// Fit To Window: this many points.
Fit(u16),
}
/// A saved track height: `"Micro"` … `"Extreme"` (or `{"Micro": null}` …), `{"Custom": points}`,
/// `{"Fit": points}`, or the bare `"Fit"` older sessions used. Read as it comes, never buffered (as an
/// untagged enum would), so anything else in a session file is refused at its first token.
impl<'de> serde::Deserialize<'de> for TrackHeight {
fn deserialize<D: serde::Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
d.deserialize_any(HeightVisitor)
}
}
const HEIGHT_VARIANTS: &[&str] = &["Custom", "Micro", "Mini", "Small", "Medium", "Large", "Jumbo", "Extreme", "Fit"];
struct HeightVisitor;
impl HeightVisitor {
/// The height a variant without a size names.
fn preset(name: &str) -> Option<TrackHeight> {
Some(match name {
"Micro" => TrackHeight::Micro,
"Mini" => TrackHeight::Mini,
"Small" => TrackHeight::Small,
"Medium" => TrackHeight::Medium,
"Large" => TrackHeight::Large,
"Jumbo" => TrackHeight::Jumbo,
"Extreme" => TrackHeight::Extreme,
_ => return None,
})
}
}
impl<'de> serde::de::Visitor<'de> for HeightVisitor {
type Value = TrackHeight;
fn expecting(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
f.write_str("a track height")
}
fn visit_str<E: serde::de::Error>(self, v: &str) -> Result<TrackHeight, E> {
match v {
// Older sessions stored Fit with no size, drawn 88 points tall: the default Medium height.
"Fit" => Ok(TrackHeight::Medium),
"Custom" => Err(E::invalid_type(serde::de::Unexpected::UnitVariant, &"a height in points")),
other => Self::preset(other).ok_or_else(|| E::unknown_variant(other, HEIGHT_VARIANTS)),
}
}
fn visit_map<A: serde::de::MapAccess<'de>>(self, mut map: A) -> Result<TrackHeight, A::Error> {
use serde::de::Error as _;
let Some(key) = map.next_key::<String>()? else { return Err(A::Error::invalid_length(0, &"one variant")) };
let h = match key.as_str() {
"Custom" => TrackHeight::Custom(map.next_value()?),
// `{"Fit": null}` is the old size-less Fit too, as above.
"Fit" => map.next_value::<Option<u16>>()?.map_or(TrackHeight::Medium, TrackHeight::Fit),
other => {
let h = Self::preset(other).ok_or_else(|| A::Error::unknown_variant(other, HEIGHT_VARIANTS))?;
map.next_value::<()>()?;
h
}
};
if map.next_key::<serde::de::IgnoredAny>()?.is_some() {
return Err(A::Error::invalid_length(2, &"one variant"));
}
Ok(h)
}
}
impl TrackHeight {
pub const ALL: [TrackHeight; 7] = [
TrackHeight::Micro,
TrackHeight::Mini,
TrackHeight::Small,
TrackHeight::Medium,
TrackHeight::Large,
TrackHeight::Jumbo,
TrackHeight::Extreme,
];
pub fn points(self) -> f32 {
match self {
TrackHeight::Custom(points) => f32::from(points).clamp(TrackHeight::Micro.points(), TrackHeight::Extreme.points()),
TrackHeight::Micro => 16.0,
TrackHeight::Mini => 24.0,
TrackHeight::Small => 46.0,
TrackHeight::Medium => 88.0,
TrackHeight::Large => 132.0,
TrackHeight::Jumbo => 220.0,
TrackHeight::Extreme => 440.0,
TrackHeight::Fit(points) => f32::from(points.max(16)),
}
}
pub fn label(self) -> &'static str {
match self {
TrackHeight::Custom(_) => "custom",
TrackHeight::Micro => "micro",
TrackHeight::Mini => "mini",
TrackHeight::Small => "small",
TrackHeight::Medium => "medium",
TrackHeight::Large => "large",
TrackHeight::Jumbo => "jumbo",
TrackHeight::Extreme => "extreme",
TrackHeight::Fit(_) => "fit to window",
}
}
pub fn from_id(s: &str) -> Option<TrackHeight> {
TrackHeight::ALL.into_iter().find(|h| h.label().eq_ignore_ascii_case(s))
}
/// The preset this many points tall, else a custom height clamped to Micro..Extreme.
pub fn from_points(points: f32) -> TrackHeight {
let p = points.max(TrackHeight::Micro.points()).min(TrackHeight::Extreme.points()).round() as u16;
TrackHeight::ALL.into_iter().find(|h| h.points() == f32::from(p)).unwrap_or(TrackHeight::Custom(p))
}
/// The next preset taller (or shorter) than this height; Extreme (or Micro) stays.
pub fn step(self, taller: bool) -> TrackHeight {
let p = self.points();
let next =
if taller { TrackHeight::ALL.into_iter().find(|h| h.points() > p) } else { TrackHeight::ALL.into_iter().rev().find(|h| h.points() < p) };
next.unwrap_or(self)
}
}
/// The default track colour palette (original, chosen for SoundCraft).
pub const TRACK_COLORS: [[u8; 3]; 16] = [
[86, 132, 214],
[94, 170, 214],
[80, 190, 168],
[104, 186, 98],
[168, 196, 76],
[222, 196, 72],
[230, 152, 62],
[222, 104, 72],
[214, 82, 110],
[196, 86, 168],
[150, 98, 206],
[110, 104, 214],
[130, 150, 170],
[178, 140, 110],
[120, 170, 140],
[190, 120, 150],
];
/// A playlist: an alternative take of a track's clips.
#[derive(Debug, Clone, PartialEq, Default, serde::Serialize, serde::Deserialize)]
pub struct Playlist {
pub name: String,
/// Clips sorted by start; non-overlapping except at crossfades.
pub clips: Vec<Clip>,
}
impl Playlist {
pub fn new(name: impl Into<String>) -> Self {
Playlist { name: name.into(), clips: Vec::new() }
}
pub fn sort(&mut self) {
self.clips.sort_by_key(|c| (c.start, c.id.0));
}
pub fn clip(&self, id: ClipId) -> Option<&Clip> {
self.clips.iter().find(|c| c.id == id)
}
pub fn clip_mut(&mut self, id: ClipId) -> Option<&mut Clip> {
self.clips.iter_mut().find(|c| c.id == id)
}
pub fn clips_in(&self, r: Range) -> impl Iterator<Item = &Clip> {
self.clips.iter().filter(move |c| c.range().overlaps(&r))
}
pub fn clip_at(&self, at: Samples) -> Option<&Clip> {
self.clips.iter().rev().find(|c| c.range().contains(at))
}
pub fn end(&self) -> Samples {
self.clips.iter().map(Clip::end).max().unwrap_or(0)
}
}
/// A track.
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
pub struct Track {
pub id: TrackId,
pub name: String,
pub kind: TrackKind,
pub format: ChannelFormat,
pub color: [u8; 3],
pub height: TrackHeight,
pub playlists: Vec<Playlist>,
pub active_playlist: usize,
pub mixer: Mixer,
pub automation: Vec<AutomationLane>,
/// Which lane the track shows: `waveform`/`blocks`/`notes`/`regions` or an automation param id.
pub view: String,
#[serde(default)]
pub hidden: bool,
#[serde(default)]
pub inactive: bool,
#[serde(default)]
pub frozen: bool,
#[serde(default)]
pub comments: String,
/// Folder that contains this track.
#[serde(default)]
pub folder: Option<TrackId>,
#[serde(default = "yes")]
pub folder_open: bool,
/// Instrument plugin (instrument tracks) — registry id.
#[serde(default)]
pub instrument: Option<crate::Insert>,
/// Ticks (musical) or samples timebase for clips.
#[serde(default)]
pub ticks_timebase: bool,
/// Elastic Audio algorithm, if enabled.
#[serde(default)]
pub elastic: Option<String>,
/// Pinned to top in the Edit window.
#[serde(default)]
pub pinned: bool,
}
fn yes() -> bool {
true
}
impl Track {
pub fn new(id: TrackId, name: impl Into<String>, kind: TrackKind, format: ChannelFormat, color: [u8; 3]) -> Self {
let name = name.into();
let format = if kind.is_midi() && kind == TrackKind::Midi { ChannelFormat::Mono } else { format };
let view = if kind.is_midi() {
"notes"
} else if kind.has_playlist() {
"waveform"
} else {
"volume"
};
Track {
id,
playlists: vec![Playlist::new(name.clone())],
name,
kind,
format,
color,
height: TrackHeight::Medium,
active_playlist: 0,
mixer: Mixer::new(format),
automation: Vec::new(),
view: view.into(),
hidden: false,
inactive: false,
frozen: false,
comments: String::new(),
folder: None,
folder_open: true,
instrument: None,
ticks_timebase: kind.is_midi(),
elastic: None,
pinned: false,
}
}
pub fn channels(&self) -> usize {
self.format.channels()
}
pub fn playlist(&self) -> Option<&Playlist> {
self.playlists.get(self.active_playlist)
}
pub fn playlist_mut(&mut self) -> Option<&mut Playlist> {
self.playlists.get_mut(self.active_playlist)
}
pub fn clips(&self) -> &[Clip] {
self.playlist().map_or(&[], |p| p.clips.as_slice())
}
pub fn lane(&self, p: &AutoParam) -> Option<&AutomationLane> {
self.automation.iter().find(|l| &l.param == p)
}
pub fn lane_mut(&mut self, p: &AutoParam) -> &mut AutomationLane {
let idx = match self.automation.iter().position(|l| &l.param == p) {
Some(i) => i,
None => {
self.automation.push(AutomationLane::new(p.clone()));
self.automation.len() - 1
}
};
let len = self.automation.len();
// `idx` is in range: it came from `position` or is the index of the element just pushed.
&mut self.automation[idx.min(len - 1)]
}
pub fn is_folder(&self) -> bool {
self.kind == TrackKind::Folder
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn legacy_fit_string_loads_as_default_height() {
assert_eq!(serde_json::from_str::<TrackHeight>("\"Fit\"").unwrap(), TrackHeight::Medium);
assert_eq!(serde_json::from_str::<TrackHeight>("{\"Fit\":120}").unwrap(), TrackHeight::Fit(120));
assert_eq!(serde_json::to_string(&TrackHeight::Fit(120)).unwrap(), "{\"Fit\":120}");
assert_eq!(serde_json::from_str::<TrackHeight>("\"Large\"").unwrap(), TrackHeight::Large);
assert_eq!(serde_json::from_str::<TrackHeight>("{\"Custom\":50}").unwrap(), TrackHeight::Custom(50));
}
#[test]
fn every_saved_height_form_loads_and_nothing_else() {
for h in TrackHeight::ALL.into_iter().chain([TrackHeight::Custom(50), TrackHeight::Fit(120)]) {
assert_eq!(serde_json::from_str::<TrackHeight>(&serde_json::to_string(&h).unwrap()).unwrap(), h);
assert_eq!(serde_json::from_value::<TrackHeight>(serde_json::to_value(h).unwrap()).unwrap(), h);
}
for h in TrackHeight::ALL {
let null = format!("{{{}:null}}", serde_json::to_string(&h).unwrap());
assert_eq!(serde_json::from_str::<TrackHeight>(&null).unwrap(), h, "{null}");
}
assert_eq!(serde_json::from_str::<TrackHeight>(r#"{"Fit":null}"#).unwrap(), TrackHeight::Medium);
assert!(serde_json::from_str::<TrackHeight>(r#"{"Custom":null}"#).is_err());
for bad in
[r#""Custom""#, r#""Huge""#, "7", "[]", "null", "{}", r#"{"Fit":"x"}"#, r#"{"Fit":70000}"#, r#"{"Large":1}"#, r#"{"Fit":1,"Fit":2}"#]
{
assert!(serde_json::from_str::<TrackHeight>(bad).is_err(), "{bad}");
}
}
#[test]
fn a_huge_height_is_refused_at_its_first_token() {
// #892: the height was read through an untagged enum, which buffers the whole value
// first; a session could hold a huge array there. It is refused where it starts.
for (open, rest) in [("[", "0,".repeat(100_000) + "0]"), (r#"{"Fit":["#, "0,".repeat(100_000) + "0]}")] {
let text = format!("{open}{rest}");
let err = serde_json::from_str::<TrackHeight>(&text).unwrap_err();
assert!(err.is_data() && err.line() == 1 && err.column() <= open.len() + 1, "{open}: {err}");
}
}
#[test]
fn new_track_defaults() {
let t = Track::new(TrackId(1), "Gtr", TrackKind::Audio, ChannelFormat::Stereo, TRACK_COLORS[0]);
assert_eq!(t.channels(), 2);
assert_eq!(t.view, "waveform");
assert_eq!(t.playlists.len(), 1);
let m = Track::new(TrackId(2), "Keys", TrackKind::Midi, ChannelFormat::Stereo, TRACK_COLORS[0]);
assert_eq!(m.format, ChannelFormat::Mono);
assert_eq!(m.view, "notes");
}
#[test]
fn lane_mut_creates_once() {
let mut t = Track::new(TrackId(1), "Gtr", TrackKind::Audio, ChannelFormat::Mono, TRACK_COLORS[0]);
t.lane_mut(&AutoParam::Volume).set_point(0, -3.0);
t.lane_mut(&AutoParam::Volume).set_point(10, -6.0);
assert_eq!(t.automation.len(), 1);
assert_eq!(t.lane(&AutoParam::Volume).map(|l| l.points.len()), Some(2));
}
#[test]
fn kind_ids() {
for k in TrackKind::ALL {
assert_eq!(TrackKind::from_id(k.id()), Some(k));
}
}
#[test]
fn custom_heights_round_to_presets_and_step_through_them() {
assert_eq!(TrackHeight::from_points(88.0), TrackHeight::Medium);
assert_eq!(TrackHeight::from_points(57.4), TrackHeight::Custom(57));
assert_eq!(TrackHeight::from_points(9999.0), TrackHeight::Extreme);
assert_eq!(TrackHeight::from_points(f32::NAN), TrackHeight::Micro);
assert_eq!(TrackHeight::Custom(u16::MAX).points(), 440.0);
assert_eq!(TrackHeight::Custom(57).step(true), TrackHeight::Medium);
assert_eq!(TrackHeight::Custom(57).step(false), TrackHeight::Small);
assert_eq!(TrackHeight::Extreme.step(true), TrackHeight::Extreme);
assert_eq!(TrackHeight::Micro.step(false), TrackHeight::Micro);
let json = serde_json::to_string(&TrackHeight::Custom(57)).unwrap();
assert_eq!(serde_json::from_str::<TrackHeight>(&json).unwrap(), TrackHeight::Custom(57));
assert_eq!(serde_json::from_str::<TrackHeight>("\"Large\"").unwrap(), TrackHeight::Large);
}
}
+2 -7
View File
@@ -1,6 +1,6 @@
[package]
name = "soundcraft-time"
description = "SoundCraft timebases: samples, Bars|Beats, Min:Secs, Timecode, Feet+Frames, tempo and meter maps, grid"
description = "Compatibility facade for SoundCraft's shared sample-index time API"
version.workspace = true
edition.workspace = true
license.workspace = true
@@ -8,12 +8,7 @@ rust-version.workspace = true
repository.workspace = true
[dependencies]
serde.workspace = true
thiserror.workspace = true
[dev-dependencies]
proptest.workspace = true
serde_json.workspace = true
craft-time.workspace = true
[lints]
workspace = true
-241
View File
@@ -1,241 +0,0 @@
//! Formatting and parsing positions in the five timebases.
use crate::{BarBeat, FrameRate, SampleRate, Samples, TempoMap, TimeError, Timecode, timecode::feet_frames};
/// The counters' and rulers' timebases.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default, serde::Serialize, serde::Deserialize)]
pub enum TimeFormat {
BarsBeats,
#[default]
MinSecs,
Timecode,
FeetFrames,
Samples,
}
impl TimeFormat {
pub const ALL: [TimeFormat; 5] = [TimeFormat::BarsBeats, TimeFormat::MinSecs, TimeFormat::Timecode, TimeFormat::FeetFrames, TimeFormat::Samples];
pub fn label(self) -> &'static str {
match self {
TimeFormat::BarsBeats => "Bars|Beats",
TimeFormat::MinSecs => "Min:Secs",
TimeFormat::Timecode => "Timecode",
TimeFormat::FeetFrames => "Feet+Frames",
TimeFormat::Samples => "Samples",
}
}
pub fn id(self) -> &'static str {
match self {
TimeFormat::BarsBeats => "bars_beats",
TimeFormat::MinSecs => "min_secs",
TimeFormat::Timecode => "timecode",
TimeFormat::FeetFrames => "feet_frames",
TimeFormat::Samples => "samples",
}
}
pub fn from_id(id: &str) -> Option<TimeFormat> {
TimeFormat::ALL.into_iter().find(|f| f.id() == id || f.label().eq_ignore_ascii_case(id))
}
}
fn min_secs(s: Samples, sr: SampleRate) -> String {
let neg = s < 0;
// Integer maths: a float `seconds * 1000` lands just below exact millisecond positions.
let ms_total = i64::try_from(i128::from(s).abs() * 1000 / i128::from(sr.hz().max(1))).unwrap_or(i64::MAX);
let mins = ms_total / 60_000;
let secs = (ms_total / 1000) % 60;
let ms = ms_total % 1000;
format!("{}{mins}:{secs:02}.{ms:03}", if neg { "-" } else { "" })
}
/// Format an absolute position. `tc_start` is the timecode of sample 0 in samples, `ff_start` the
/// feet+frames reading of sample 0 in frames.
pub fn format_position(s: Samples, fmt: TimeFormat, sr: SampleRate, map: &TempoMap, rate: FrameRate, tc_start: Samples, ff_start: i64) -> String {
match fmt {
TimeFormat::Samples => s.to_string(),
TimeFormat::MinSecs => min_secs(s, sr),
TimeFormat::Timecode => Timecode::from_samples(s.saturating_add(tc_start), rate, sr).format(rate),
TimeFormat::FeetFrames => {
let (ft, fr) = feet_frames(s, rate, sr, ff_start);
format!("{ft}+{fr:02}")
}
TimeFormat::BarsBeats => {
let bb = map.bar_beat_at(s, sr);
format!("{}| {}| {:03}", bb.bar, bb.beat, bb.tick)
}
}
}
/// Format a length starting at `start` (Bars|Beats lengths count from 0|0|000).
pub fn format_length(start: Samples, len: Samples, fmt: TimeFormat, sr: SampleRate, map: &TempoMap, rate: FrameRate) -> String {
match fmt {
TimeFormat::BarsBeats => {
let t0 = map.samples_to_ticks(start, sr);
let t1 = map.samples_to_ticks(start.saturating_add(len), sr);
let m = map.meter_at_tick(t0);
let d = (t1 - t0).max(0);
let bars = d / m.ticks_per_bar().max(1);
let rem = d % m.ticks_per_bar().max(1);
format!("{bars}| {}| {:03}", rem / m.ticks_per_beat().max(1), rem % m.ticks_per_beat().max(1))
}
TimeFormat::Timecode => Timecode::from_samples(len, rate, sr).format(rate),
other => format_position(len, other, sr, map, rate, 0, 0),
}
}
/// Parse a position typed into a counter or dialog.
pub fn parse_position(
text: &str,
fmt: TimeFormat,
sr: SampleRate,
map: &TempoMap,
rate: FrameRate,
tc_start: Samples,
ff_start: i64,
) -> Result<Samples, TimeError> {
let t = text.trim();
let err = || TimeError::Parse(t.to_string(), fmt.label());
match fmt {
TimeFormat::Samples => t.replace([',', '_'], "").parse::<i64>().map_err(|_| err()),
TimeFormat::MinSecs => {
let (neg, body) = match t.strip_prefix('-') {
Some(r) => (true, r),
None => (false, t),
};
let (m, s) = match body.split_once(':') {
Some((m, s)) => (m.trim().parse::<f64>().map_err(|_| err())?, s.trim().parse::<f64>().map_err(|_| err())?),
None => (0.0, body.parse::<f64>().map_err(|_| err())?),
};
if !m.is_finite() || !s.is_finite() || m < 0.0 || s < 0.0 {
return Err(err());
}
let v = sr.samples(m * 60.0 + s);
Ok(if neg { -v } else { v })
}
TimeFormat::Timecode => {
let tc = Timecode::parse(t)?;
if tc.minutes >= 60 || tc.seconds >= 60 || tc.frames >= rate.nominal().max(1) {
return Err(err());
}
if rate.is_drop() && tc.seconds == 0 && tc.minutes % 10 != 0 {
let dropped = if rate.nominal() >= 60 { 4 } else { 2 };
if tc.frames < dropped {
return Err(err());
}
}
Ok(tc.to_samples(rate, sr).saturating_sub(tc_start))
}
TimeFormat::FeetFrames => {
let (ft, fr) = t.split_once('+').ok_or_else(err)?;
let ft: i64 = ft.trim().parse().map_err(|_| err())?;
let fr: i64 = fr.trim().parse().map_err(|_| err())?;
Ok(rate.samples_at_frame(ft.saturating_mul(16).saturating_add(fr).saturating_sub(ff_start), sr))
}
TimeFormat::BarsBeats => {
let parts: Vec<i64> =
t.split(['|', '.', ' ']).filter(|p| !p.is_empty()).map(str::parse::<i64>).collect::<Result<_, _>>().map_err(|_| err())?;
let bb = BarBeat {
bar: *parts.first().ok_or_else(err)?,
beat: parts.get(1).copied().unwrap_or(1).max(1),
tick: parts.get(2).copied().unwrap_or(0).max(0),
};
Ok(map.samples_at_bar_beat(bb, sr))
}
}
}
#[cfg(test)]
mod tests {
use super::*;
fn ctx() -> (SampleRate, TempoMap, FrameRate) {
(SampleRate::HZ_48000, TempoMap::default(), FrameRate::Fps30)
}
#[test]
fn huge_typed_positions_parse_without_overflowing() {
let (sr, map, rate) = ctx();
for (text, fmt) in [
("999999999999999999:0", TimeFormat::Timecode),
("100000000000000:00:00:00", TimeFormat::Timecode),
("-9223372036854775808", TimeFormat::BarsBeats),
] {
let _ = parse_position(text, fmt, sr, &map, rate, 0, 0);
}
}
#[test]
fn formats_like_pro_tools_counters() {
let (sr, map, rate) = ctx();
assert_eq!(format_position(0, TimeFormat::MinSecs, sr, &map, rate, 0, 0), "0:00.000");
assert_eq!(format_position(48_000 * 75 + 24_000, TimeFormat::MinSecs, sr, &map, rate, 0, 0), "1:15.500");
assert_eq!(format_position(0, TimeFormat::BarsBeats, sr, &map, rate, 0, 0), "1| 1| 000");
assert_eq!(format_position(48_000, TimeFormat::Timecode, sr, &map, rate, 0, 0), "00:00:01:00");
assert_eq!(format_position(12345, TimeFormat::Samples, sr, &map, rate, 0, 0), "12345");
}
#[test]
fn minimum_bar_parses_without_overflow() {
let (sr, map, rate) = ctx();
let p = |t: &str| parse_position(t, TimeFormat::BarsBeats, sr, &map, rate, 0, 0);
assert!(p("-9223372036854775808|01|000").is_ok_and(|s| s < 0));
assert_eq!(p("2|3|000").ok(), Some(144_000));
assert_eq!(p("0|1|000").ok(), Some(-96_000));
}
#[test]
fn parse_round_trips() {
let (sr, map, rate) = ctx();
for fmt in TimeFormat::ALL {
for s in [0i64, 48_000, 96_000 * 3, 1_440_000] {
let text = format_position(s, fmt, sr, &map, rate, 0, 0);
let back = parse_position(&text, fmt, sr, &map, rate, 0, 0).unwrap();
assert!((back - s).abs() <= 48, "{fmt:?} {s} → {text} → {back}");
}
}
}
#[test]
fn parse_round_trips_timecode_negative() {
let (sr, map, _) = ctx();
let rate = FrameRate::Fps25;
for fmt in [TimeFormat::Timecode, TimeFormat::FeetFrames] {
for s in [-48_000i64, -96_000 * 3, -1_440_000] {
let text = format_position(s, fmt, sr, &map, rate, 0, 0);
let back = parse_position(&text, fmt, sr, &map, rate, 0, 0).unwrap();
assert_eq!(back, s, "{fmt:?} {s} → {text} → {back}");
}
}
}
#[test]
fn parse_rejects_garbage() {
let (sr, map, rate) = ctx();
assert!(parse_position("abc", TimeFormat::Samples, sr, &map, rate, 0, 0).is_err());
assert!(parse_position("1:x", TimeFormat::MinSecs, sr, &map, rate, 0, 0).is_err());
assert!(parse_position("", TimeFormat::BarsBeats, sr, &map, rate, 0, 0).is_err());
}
#[test]
fn drop_frame_skipped_labels_are_rejected() {
let (sr, map, _) = ctx();
for (rate, skipped) in [
(FrameRate::Fps29_97Drop, &["00:01:00;00", "00:01:00;01"][..]),
(FrameRate::Fps59_94Drop, &["00:01:00;00", "00:01:00;01", "00:01:00;02", "00:01:00;03"][..]),
] {
for text in skipped {
assert!(parse_position(text, TimeFormat::Timecode, sr, &map, rate, 0, 0).is_err(), "accepted skipped label {text} at {rate:?}");
}
assert!(parse_position("00:10:00;00", TimeFormat::Timecode, sr, &map, rate, 0, 0).is_ok());
}
}
#[test]
fn bar_lengths() {
let (sr, map, rate) = ctx();
assert_eq!(format_length(0, 96_000 + 24_000, TimeFormat::BarsBeats, sr, &map, rate), "1| 1| 000");
}
}
-809
View File
@@ -1,809 +0,0 @@
//! Grid and Nudge values: musical or absolute step sizes, grid lines and snapping.
use crate::{FrameRate, SampleRate, Samples, TICKS_PER_QUARTER, TempoMap, Timecode, to_samples};
/// Note values for Bars|Beats grids.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
pub enum NoteValue {
Bar,
Whole,
Half,
Quarter,
Eighth,
Sixteenth,
ThirtySecond,
SixtyFourth,
}
impl NoteValue {
/// Every musical increment in menu order, from largest to smallest.
pub const ALL: [NoteValue; 8] = [
NoteValue::Bar,
NoteValue::Whole,
NoteValue::Half,
NoteValue::Quarter,
NoteValue::Eighth,
NoteValue::Sixteenth,
NoteValue::ThirtySecond,
NoteValue::SixtyFourth,
];
/// Returns the nominal tick length; bars are adjusted for the active meter when used.
pub fn ticks(self) -> i64 {
match self {
NoteValue::Bar => TICKS_PER_QUARTER * 4,
NoteValue::Whole => TICKS_PER_QUARTER * 4,
NoteValue::Half => TICKS_PER_QUARTER * 2,
NoteValue::Quarter => TICKS_PER_QUARTER,
NoteValue::Eighth => TICKS_PER_QUARTER / 2,
NoteValue::Sixteenth => TICKS_PER_QUARTER / 4,
NoteValue::ThirtySecond => TICKS_PER_QUARTER / 8,
NoteValue::SixtyFourth => TICKS_PER_QUARTER / 16,
}
}
/// Returns the menu label for this musical increment.
pub fn label(self) -> &'static str {
match self {
NoteValue::Bar => "1 bar",
NoteValue::Whole => "Whole note",
NoteValue::Half => "1/2 note",
NoteValue::Quarter => "1/4 note",
NoteValue::Eighth => "1/8 note",
NoteValue::Sixteenth => "1/16 note",
NoteValue::ThirtySecond => "1/32 note",
NoteValue::SixtyFourth => "1/64 note",
}
}
}
/// The time scale used by a Grid or Nudge value.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
pub enum GridScale {
BarsBeats,
MinSecs,
Timecode,
FeetFrames,
Samples,
ClipsMarkers,
}
impl GridScale {
/// Numeric time scales shared by Grid and Nudge.
pub const TIME: [GridScale; 5] = [GridScale::BarsBeats, GridScale::MinSecs, GridScale::Timecode, GridScale::FeetFrames, GridScale::Samples];
/// Returns the user-facing scale label.
pub fn label(self) -> &'static str {
match self {
GridScale::BarsBeats => "Bars|Beats",
GridScale::MinSecs => "Min:Secs",
GridScale::Timecode => "Timecode",
GridScale::FeetFrames => "Feet+Frames",
GridScale::Samples => "Samples",
GridScale::ClipsMarkers => "Clips/Markers",
}
}
/// Returns the stable command and session identifier.
pub fn id(self) -> &'static str {
match self {
GridScale::BarsBeats => "bars_beats",
GridScale::MinSecs => "min_secs",
GridScale::Timecode => "timecode",
GridScale::FeetFrames => "feet_frames",
GridScale::Samples => "samples",
GridScale::ClipsMarkers => "clips_markers",
}
}
/// Parses a stable identifier or user-facing label.
pub fn from_id(id: &str) -> Option<GridScale> {
Self::TIME.into_iter().chain([GridScale::ClipsMarkers]).find(|scale| scale.id() == id || scale.label().eq_ignore_ascii_case(id))
}
}
/// Timecode Grid/Nudge increments, ordered independently of the session frame rate.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
pub enum TimecodeValue {
Second,
/// Five physical frames, independent of skipped drop-frame labels.
FiveFrames,
Frame,
HalfFrame,
QuarterFrame,
SubFrame,
}
impl TimecodeValue {
/// Every Timecode increment in menu order, from largest to smallest.
pub const ALL: [TimecodeValue; 6] = [
TimecodeValue::Second,
TimecodeValue::FiveFrames,
TimecodeValue::Frame,
TimecodeValue::HalfFrame,
TimecodeValue::QuarterFrame,
TimecodeValue::SubFrame,
];
/// Returns the user-facing increment label.
pub fn label(self) -> &'static str {
match self {
TimecodeValue::Second => "1 second",
TimecodeValue::FiveFrames => "5 frames",
TimecodeValue::Frame => "1 frame",
TimecodeValue::HalfFrame => "1/2 frame",
TimecodeValue::QuarterFrame => "1/4 frame",
TimecodeValue::SubFrame => "1 subframe",
}
}
/// Returns the stable command and session identifier.
pub fn id(self) -> &'static str {
match self {
TimecodeValue::Second => "second",
TimecodeValue::FiveFrames => "5_frames",
TimecodeValue::Frame => "frame",
TimecodeValue::HalfFrame => "half_frame",
TimecodeValue::QuarterFrame => "quarter_frame",
TimecodeValue::SubFrame => "subframe",
}
}
/// Parses a stable identifier or user-facing label.
pub fn from_id(id: &str) -> Option<TimecodeValue> {
Self::ALL.into_iter().find(|value| value.id() == id || value.label().eq_ignore_ascii_case(id))
}
/// Returns the increment length in frames at the session rate.
fn frames(self, rate: FrameRate) -> f64 {
match self {
TimecodeValue::Second => rate.nominal() as f64,
TimecodeValue::FiveFrames => 5.0,
TimecodeValue::Frame => 1.0,
TimecodeValue::HalfFrame => 0.5,
TimecodeValue::QuarterFrame => 0.25,
TimecodeValue::SubFrame => 0.01,
}
}
}
/// Feet+Frames Grid/Nudge increments in physical frames, independent of drop-frame label skips.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
pub enum FeetFramesValue {
Foot,
Frame,
QuarterFrame,
SubFrame,
}
impl FeetFramesValue {
/// Every Feet+Frames increment in menu order, from largest to smallest.
pub const ALL: [FeetFramesValue; 4] = [FeetFramesValue::Foot, FeetFramesValue::Frame, FeetFramesValue::QuarterFrame, FeetFramesValue::SubFrame];
/// Returns the user-facing increment label.
pub fn label(self) -> &'static str {
match self {
FeetFramesValue::Foot => "1 foot",
FeetFramesValue::Frame => "1 frame",
FeetFramesValue::QuarterFrame => "1/4 frame",
FeetFramesValue::SubFrame => "1 subframe",
}
}
/// Returns the stable command and session identifier.
pub fn id(self) -> &'static str {
match self {
FeetFramesValue::Foot => "foot",
FeetFramesValue::Frame => "frame",
FeetFramesValue::QuarterFrame => "quarter_frame",
FeetFramesValue::SubFrame => "subframe",
}
}
/// Parses a stable identifier or user-facing label.
pub fn from_id(id: &str) -> Option<FeetFramesValue> {
Self::ALL.into_iter().find(|value| value.id() == id || value.label().eq_ignore_ascii_case(id))
}
/// Returns the increment length in frames.
fn frames(self) -> f64 {
let (num, den) = self.frame_ratio();
num as f64 / den as f64
}
/// Exact numerator and denominator of the increment in physical frames.
fn frame_ratio(self) -> (i128, i128) {
match self {
Self::Foot => (16, 1),
Self::Frame => (1, 1),
Self::QuarterFrame => (1, 4),
Self::SubFrame => (1, 100),
}
}
/// Apply the counter origin in frames before rounding once to the nearest sample.
fn sample_line(self, index: i128, sr: SampleRate, rate: FrameRate, origin: i64) -> Samples {
let (num, den) = self.frame_ratio();
let (rate_num, rate_den) = rate.rational();
let frames = index * num - i128::from(origin) * den;
let samples = frames * i128::from(sr.hz().max(1)) * i128::from(rate_den);
let divisor = den * i128::from(rate_num);
let magnitude = (samples.abs() + divisor / 2) / divisor;
let rounded = if samples < 0 { -magnitude } else { magnitude };
i64::try_from(rounded).unwrap_or(if rounded < 0 { i64::MIN } else { i64::MAX })
}
/// Find a line index using exact frame arithmetic, allowing for whole-sample rounding.
fn line_index(self, position: Samples, sr: SampleRate, rate: FrameRate, origin: i64) -> i128 {
let (num, den) = self.frame_ratio();
let (rate_num, rate_den) = rate.rational();
let scale = i128::from(sr.hz().max(1)) * i128::from(rate_den);
let frames = i128::from(position) * i128::from(rate_num) + i128::from(origin) * scale;
let mut index = (frames * den).div_euclid(scale * num);
if self.sample_line(index + 1, sr, rate, origin) <= position {
index += 1;
}
index
}
/// The Feet+Frames line at or before a sample, relative to the counter's frame origin.
pub fn line_at_or_before(self, position: Samples, sr: SampleRate, rate: FrameRate, origin: i64) -> Samples {
self.sample_line(self.line_index(position, sr, rate, origin), sr, rate, origin)
}
/// The next distinct sample line; subframe increments may share a sample at low sample rates.
fn next_line(self, position: Samples, sr: SampleRate, rate: FrameRate, origin: i64) -> Samples {
self.sample_line(self.line_index(position, sr, rate, origin) + 1, sr, rate, origin).max(position.saturating_add(1))
}
/// Snap against frame-origin lines, choosing the earlier line when distances are equal.
pub fn snap(self, position: Samples, sr: SampleRate, rate: FrameRate, origin: i64) -> Samples {
let lo = self.line_at_or_before(position, sr, rate, origin);
let hi = self.next_line(lo, sr, rate, origin);
if i128::from(position) - i128::from(lo) <= i128::from(hi) - i128::from(position) { lo } else { hi }
}
/// Distinct Feet+Frames sample lines within `[start, end)`, capped at `max`.
pub fn lines(self, start: Samples, end: Samples, sr: SampleRate, rate: FrameRate, origin: i64, max: usize) -> Vec<Samples> {
let mut out = Vec::new();
let mut line = self.line_at_or_before(start, sr, rate, origin);
while line < end && out.len() < max {
if line >= start {
out.push(line);
}
let next = self.next_line(line, sr, rate, origin);
if next <= line {
break;
}
line = next;
}
out
}
}
/// A grid or nudge step.
#[derive(Debug, Clone, Copy, PartialEq, serde::Serialize, serde::Deserialize)]
pub enum GridValue {
/// Musical: note value, optionally dotted or triplet.
Note { value: NoteValue, dotted: bool, triplet: bool },
/// Absolute seconds (Min:Secs grids: 0.001 … 1 s).
Seconds(f64),
/// Frames at the session timecode rate.
/// Kept for sessions and commands written before named timecode increments were added.
Frames(i64),
/// A named Timecode increment, including fractional frames and one second.
Timecode(TimecodeValue),
/// A Feet+Frames increment (one foot is 16 frames).
FeetFrames(FeetFramesValue),
/// Raw samples.
Samples(i64),
}
impl Default for GridValue {
/// Starts on a straight sixteenth-note Bars|Beats grid.
fn default() -> Self {
GridValue::Note { value: NoteValue::Sixteenth, dotted: false, triplet: false }
}
}
impl GridValue {
/// Returns the user-facing label for this increment.
pub fn label(&self) -> String {
match self {
GridValue::Note { value, dotted, triplet } => {
let mut s = value.label().to_string();
if *dotted {
s.push_str(" dotted");
}
if *triplet {
s.push_str(" triplet");
}
s
}
GridValue::Seconds(s) => {
if *s >= 1.0 {
format!("{s} second{}", if *s == 1.0 { "" } else { "s" })
} else {
format!("{} ms", (s * 1000.0).round() as i64)
}
}
GridValue::Frames(f) => format!("{f} frame{}", if *f == 1 { "" } else { "s" }),
GridValue::Timecode(value) => value.label().to_string(),
GridValue::FeetFrames(value) => value.label().to_string(),
GridValue::Samples(n) => format!("{n} sample{}", if *n == 1 { "" } else { "s" }),
}
}
/// Returns the time scale to which this increment belongs.
pub fn scale(self) -> GridScale {
match self {
GridValue::Note { .. } => GridScale::BarsBeats,
GridValue::Seconds(_) => GridScale::MinSecs,
GridValue::Frames(_) | GridValue::Timecode(_) => GridScale::Timecode,
GridValue::FeetFrames(_) => GridScale::FeetFrames,
GridValue::Samples(_) => GridScale::Samples,
}
}
/// Returns the default increment for a newly selected time scale.
pub fn for_scale(scale: GridScale) -> GridValue {
match scale {
GridScale::BarsBeats => GridValue::default(),
GridScale::MinSecs => GridValue::Seconds(1.0),
GridScale::Timecode => GridValue::Timecode(TimecodeValue::Frame),
GridScale::FeetFrames => GridValue::FeetFrames(FeetFramesValue::Frame),
GridScale::Samples => GridValue::Samples(100),
GridScale::ClipsMarkers => GridValue::default(),
}
}
/// The next larger or smaller menu value of the same kind, or `self` at either end. Note values keep
/// their dot or triplet modifier.
pub fn stepped(self, larger: bool) -> GridValue {
/// Finds the next value in a numeric increment ladder without wrapping.
fn next<T: PartialOrd + Copy>(ladder: &[T], v: T, larger: bool) -> Option<T> {
if larger { ladder.iter().copied().find(|x| *x > v) } else { ladder.iter().rev().copied().find(|x| *x < v) }
}
match self {
GridValue::Note { value, dotted, triplet } => {
let i = NoteValue::ALL.iter().position(|v| *v == value).unwrap_or(0);
let j = if larger { i.checked_sub(1) } else { i.checked_add(1) };
j.and_then(|j| NoteValue::ALL.get(j)).map_or(self, |&value| GridValue::Note { value, dotted, triplet })
}
GridValue::Seconds(s) => next(&[0.001, 0.01, 0.1, 0.5, 1.0], s, larger).map_or(self, GridValue::Seconds),
GridValue::Frames(f) => next(&[1, 10, 100], f, larger).map_or(self, GridValue::Frames),
GridValue::Timecode(value) => step_enum(&TimecodeValue::ALL, value, larger).map_or(self, GridValue::Timecode),
GridValue::FeetFrames(value) => step_enum(&FeetFramesValue::ALL, value, larger).map_or(self, GridValue::FeetFrames),
GridValue::Samples(n) => next(&[1, 2, 10, 100, 1_000, 10_000], n, larger).map_or(self, GridValue::Samples),
}
}
fn note_ticks(value: NoteValue, dotted: bool, triplet: bool, map: &TempoMap, at_tick: i64) -> i64 {
let base = if value == NoteValue::Bar { map.meter_at_tick(at_tick).ticks_per_bar() } else { value.ticks() };
let mut t = base;
if dotted {
t = t * 3 / 2;
}
if triplet {
t = t * 2 / 3;
}
t.max(1)
}
/// The step in samples at position `at` (musical steps depend on tempo).
pub fn step_samples(&self, at: Samples, sr: SampleRate, map: &TempoMap, rate: FrameRate) -> Samples {
match *self {
GridValue::Note { value, dotted, triplet } => {
let t0 = map.samples_to_ticks(at, sr);
let dt = Self::note_ticks(value, dotted, triplet, map, t0);
(map.tick_to_samples(t0.saturating_add(dt), sr).saturating_sub(map.tick_to_samples(t0, sr))).max(1)
}
GridValue::Seconds(s) => sr.samples(s.clamp(0.000_01, 3600.0)).max(1),
GridValue::Frames(f) => to_samples(rate.samples_per_frame(sr) * f.clamp(1, 1_000_000) as f64).max(1),
GridValue::Timecode(value) => to_samples(rate.samples_per_frame(sr) * value.frames(rate)).max(1),
GridValue::FeetFrames(value) => to_samples(rate.samples_per_frame(sr) * value.frames()).max(1),
GridValue::Samples(n) => n.max(1),
}
}
/// Nearest grid line to `pos`.
pub fn snap(&self, pos: Samples, sr: SampleRate, map: &TempoMap, rate: FrameRate) -> Samples {
if let Self::FeetFrames(value) = *self {
return value.snap(pos, sr, rate, 0);
}
let lo = self.line_at_or_before(pos, sr, map, rate);
let hi = self.next_line(lo, sr, map, rate);
if (pos - lo) <= (hi - pos) { lo } else { hi }
}
/// The grid line at or before `pos`.
pub fn line_at_or_before(&self, pos: Samples, sr: SampleRate, map: &TempoMap, rate: FrameRate) -> Samples {
match *self {
Self::FeetFrames(value) => value.line_at_or_before(pos, sr, rate, 0),
GridValue::Note { value, dotted, triplet } => {
// Float ticks can land a hair either side of a line's own tick.
let mut t = map.samples_to_ticks_f(pos, sr).floor() as i64;
if map.tick_to_samples(t, sr) > pos {
t = t.saturating_sub(1);
} else if map.tick_to_samples(t.saturating_add(1), sr) <= pos {
t = t.saturating_add(1);
}
// Sub-bar values restart at each bar so odd meters stay aligned. A whole note is
// always four quarter notes and therefore keeps a continuous tick origin.
let bb = map.bar_beat_at_tick(t);
let bar_tick = map.bar_start_tick(bb.bar);
let origin = if value == NoteValue::Whole { 0 } else { bar_tick };
let step = Self::note_ticks(value, dotted, triplet, map, origin);
let k = (t - origin).div_euclid(step);
map.tick_to_samples(origin + k * step, sr)
}
_ => self.abs_line(self.abs_index_at_or_before(pos, sr, rate), sr, rate),
}
}
/// Position of absolute grid line `k` (Seconds, Frames, Samples), at an exact multiple of the step
/// rounded once, so rounding never accumulates from line to line.
fn abs_line(&self, k: i64, sr: SampleRate, rate: FrameRate) -> Samples {
if let Self::FeetFrames(value) = *self {
return value.sample_line(i128::from(k), sr, rate, 0);
}
if matches!(self, GridValue::Timecode(TimecodeValue::Second)) && rate.is_drop() {
return rate.samples_at_frame(Self::drop_second_frame(k, rate), sr);
}
if let Some(frames) = self.frame_step(rate) {
let whole = frames.round();
if frames == whole {
return rate.samples_at_frame(k.saturating_mul(whole as i64), sr);
}
let step = (rate.samples_per_frame(sr) * frames).max(1.0);
return to_samples(k as f64 * step);
}
match *self {
GridValue::Seconds(s) => to_samples(k as f64 * Self::seconds_step(s, sr)),
GridValue::Samples(n) => k.saturating_mul(n.max(1)),
GridValue::Note { .. } => 0,
GridValue::Frames(_) | GridValue::Timecode(_) | GridValue::FeetFrames(_) => 0,
}
}
/// Index of the absolute grid line at or before `pos`.
fn abs_index_at_or_before(&self, pos: Samples, sr: SampleRate, rate: FrameRate) -> i64 {
if matches!(self, GridValue::Timecode(TimecodeValue::Second)) && rate.is_drop() {
let tc = Timecode::from_frames(rate.frames_at(pos, sr), rate);
let magnitude = tc.hours.saturating_mul(3600).saturating_add(tc.minutes.saturating_mul(60)).saturating_add(tc.seconds);
let mut k = if tc.negative { magnitude.saturating_neg() } else { magnitude };
while self.abs_line(k, sr, rate) > pos {
let previous = k.saturating_sub(1);
if previous == k {
break;
}
k = previous;
}
while self.abs_line(k.saturating_add(1), sr, rate) <= pos {
let next = k.saturating_add(1);
if next == k {
break;
}
k = next;
}
return k;
}
let mut k = if let Some(frames) = self.frame_step(rate) {
let step = (rate.samples_per_frame(sr) * frames).max(1.0);
(pos as f64 / step).floor() as i64
} else {
match *self {
GridValue::Seconds(s) => (pos as f64 / Self::seconds_step(s, sr)).floor() as i64,
GridValue::Samples(n) => return pos.div_euclid(n.max(1)),
GridValue::Note { .. } => return 0,
GridValue::Frames(_) | GridValue::Timecode(_) | GridValue::FeetFrames(_) => return 0,
}
};
// Lines are rounded to whole samples, so the floored index can be one off either way.
if self.abs_line(k, sr, rate) > pos {
k = k.saturating_sub(1);
} else if self.abs_line(k.saturating_add(1), sr, rate) <= pos {
k = k.saturating_add(1);
}
k
}
/// The exact (fractional) Seconds step in samples, at least one sample.
fn seconds_step(s: f64, sr: SampleRate) -> f64 {
(s.clamp(0.000_01, 3600.0) * sr.as_f64()).max(1.0)
}
/// Maps a signed whole-second timecode label to its physical frame at a drop-frame rate.
fn drop_second_frame(index: i64, rate: FrameRate) -> i64 {
let seconds = index.unsigned_abs();
let total_minutes = seconds / 60;
let frames = if seconds.is_multiple_of(60) && !total_minutes.is_multiple_of(10) { if rate.nominal() >= 60 { 4 } else { 2 } } else { 0 };
Timecode {
negative: index < 0,
hours: i64::try_from(seconds / 3600).unwrap_or(i64::MAX),
minutes: i64::try_from((seconds / 60) % 60).unwrap_or(0),
seconds: i64::try_from(seconds % 60).unwrap_or(0),
frames,
}
.to_frames(rate)
}
/// A frame-based increment as a possibly fractional number of frames.
fn frame_step(&self, rate: FrameRate) -> Option<f64> {
match *self {
GridValue::Frames(frames) => Some(frames.clamp(1, 1_000_000) as f64),
GridValue::Timecode(value) => Some(value.frames(rate)),
GridValue::FeetFrames(value) => Some(value.frames()),
_ => None,
}
}
/// The first grid line strictly after `line`.
pub fn next_line(&self, line: Samples, sr: SampleRate, map: &TempoMap, rate: FrameRate) -> Samples {
if let Self::FeetFrames(value) = *self {
return value.next_line(line, sr, rate, 0);
}
if !matches!(self, GridValue::Note { .. }) {
let next = self.abs_line(self.abs_index_at_or_before(line, sr, rate).saturating_add(1), sr, rate);
return if next > line { next } else { line.saturating_add(1) };
}
let step = self.step_samples(line, sr, map, rate);
let candidate = self.line_at_or_before(line.saturating_add(step), sr, map, rate);
if candidate > line { candidate } else { line.saturating_add(step) }
}
/// All grid lines within `[start, end)`, capped at `max` lines.
pub fn lines(&self, start: Samples, end: Samples, sr: SampleRate, map: &TempoMap, rate: FrameRate, max: usize) -> Vec<Samples> {
if let Self::FeetFrames(value) = *self {
return value.lines(start, end, sr, rate, 0, max);
}
let mut out = Vec::new();
let mut p = self.line_at_or_before(start, sr, map, rate);
while p < end && out.len() < max {
if p >= start {
out.push(p);
}
let n = self.next_line(p, sr, map, rate);
if n <= p {
break;
}
p = n;
}
out
}
}
/// Moves one position through an ordered increment menu without wrapping.
fn step_enum<T: Copy + PartialEq>(values: &[T], value: T, larger: bool) -> Option<T> {
let index = values.iter().position(|candidate| *candidate == value)?;
let next = if larger { index.checked_sub(1) } else { index.checked_add(1) }?;
values.get(next).copied()
}
#[cfg(test)]
mod tests {
use super::*;
/// Frame origins are subtracted before rounding whole frames, quarter frames and subframes.
#[test]
fn feet_frames_origins_round_only_after_applying_the_frame_offset() {
let (sr, rate) = (SampleRate::HZ_48000, FrameRate::Fps29_97);
for at in [49_650, 100_901, 177_778] {
assert_eq!(FeetFramesValue::Foot.line_at_or_before(at, sr, rate, 1), at);
assert_eq!(FeetFramesValue::Foot.snap(at - 1, sr, rate, 1), at);
}
assert_eq!(FeetFramesValue::QuarterFrame.snap(50_049, sr, rate, 1), 50_050);
assert_eq!(FeetFramesValue::SubFrame.lines(49_665, 49_667, sr, rate, 1, 4), [49_666]);
}
/// Dense subframes retain distinct sample lines and remain bounded with extreme signed origins.
#[test]
fn feet_frames_subframes_handle_dense_steps_and_extreme_origins() {
let sr = SampleRate::new(8_000).unwrap();
let rate = FrameRate::Fps120;
for origin in [i64::MIN, -1, 0, 1, i64::MAX] {
assert_eq!(FeetFramesValue::SubFrame.lines(-3, 4, sr, rate, origin, 20), [-3, -2, -1, 0, 1, 2, 3]);
for at in [i64::MIN, -1, 0, 1, i64::MAX] {
assert_eq!(FeetFramesValue::SubFrame.line_at_or_before(at, sr, rate, origin), at);
assert_eq!(FeetFramesValue::SubFrame.snap(at, sr, rate, origin), at);
}
}
}
#[test]
fn grid_lines_are_their_own_line_at_odd_tempos() {
let (g, sr, rate) = (GridValue::default(), SampleRate::HZ_48000, FrameRate::Fps30);
for bpm in [90.0, 95.0, 110.0, 130.0, 140.0] {
let map = TempoMap::new(bpm, 4, 4).unwrap();
let mut line = 0;
for _ in 0..64 {
assert_eq!(g.line_at_or_before(line, sr, &map, rate), line, "{bpm} bpm");
line = g.next_line(line, sr, &map, rate);
}
}
}
/// Musical boundaries stay exact across a tempo change while absolute scales remain
/// independent of the tempo map.
#[test]
fn grids_stay_aligned_across_a_tempo_change() {
let (sr, rate) = (SampleRate::HZ_48000, FrameRate::Fps29_97Drop);
let mut map = TempoMap::default();
map.set_tempo(TICKS_PER_QUARTER * 4, 90.0).unwrap();
let musical = GridValue::Note { value: NoteValue::Quarter, dotted: false, triplet: false };
for tick in [0, TICKS_PER_QUARTER * 3, TICKS_PER_QUARTER * 4, TICKS_PER_QUARTER * 5] {
let line = map.tick_to_samples(tick, sr);
assert_eq!(musical.line_at_or_before(line, sr, &map, rate), line);
}
for absolute in [
GridValue::Seconds(0.5),
GridValue::Timecode(TimecodeValue::Second),
GridValue::FeetFrames(FeetFramesValue::Foot),
GridValue::Samples(1_000),
] {
let line = absolute.next_line(absolute.line_at_or_before(100_000, sr, &map, rate), sr, &map, rate);
assert_eq!(absolute.line_at_or_before(line, sr, &map, rate), line, "{absolute:?}");
}
}
#[test]
fn sixteenth_grid_at_120() {
let g = GridValue::default();
let (sr, map, rate) = (SampleRate::HZ_48000, TempoMap::default(), FrameRate::Fps30);
assert_eq!(g.step_samples(0, sr, &map, rate), 6000);
assert_eq!(g.snap(2900, sr, &map, rate), 0);
assert_eq!(g.snap(3100, sr, &map, rate), 6000);
assert_eq!(g.lines(0, 24_000, sr, &map, rate, 100), vec![0, 6000, 12000, 18000]);
}
/// Absolute increments report their expected sample lengths at a fixed rate.
#[test]
fn absolute_grids() {
let (sr, map, rate) = (SampleRate::HZ_48000, TempoMap::default(), FrameRate::Fps25);
assert_eq!(GridValue::Seconds(1.0).snap(70_000, sr, &map, rate), 48_000);
assert_eq!(GridValue::Frames(1).step_samples(0, sr, &map, rate), 1920);
assert_eq!(GridValue::Timecode(TimecodeValue::Second).step_samples(0, sr, &map, rate), 48_000);
assert_eq!(GridValue::Timecode(TimecodeValue::FiveFrames).step_samples(0, sr, &map, rate), 9_600);
assert_eq!(GridValue::Timecode(TimecodeValue::HalfFrame).step_samples(0, sr, &map, rate), 960);
assert_eq!(GridValue::Timecode(TimecodeValue::QuarterFrame).step_samples(0, sr, &map, rate), 480);
assert_eq!(GridValue::Timecode(TimecodeValue::SubFrame).step_samples(0, sr, &map, rate), 19);
assert_eq!(GridValue::FeetFrames(FeetFramesValue::Foot).step_samples(0, sr, &map, rate), 30_720);
assert_eq!(GridValue::Samples(0).step_samples(0, sr, &map, rate), 1);
}
/// Bar length follows the meter while a whole note always spans four quarter notes.
#[test]
fn a_bar_and_a_whole_note_differ_in_three_four() {
let (sr, map, rate) = (SampleRate::HZ_48000, TempoMap::new(120.0, 3, 4).unwrap(), FrameRate::Fps25);
let note = |value| GridValue::Note { value, dotted: false, triplet: false };
assert_eq!(note(NoteValue::Bar).step_samples(0, sr, &map, rate), 72_000);
assert_eq!(note(NoteValue::Whole).step_samples(0, sr, &map, rate), 96_000);
assert_eq!(note(NoteValue::Bar).line_at_or_before(96_000, sr, &map, rate), 72_000);
assert_eq!(note(NoteValue::Whole).line_at_or_before(96_000, sr, &map, rate), 96_000);
assert_eq!(note(NoteValue::Half).lines(0, 150_000, sr, &map, rate, 10), [0, 48_000, 72_000, 120_000, 144_000]);
}
#[test]
fn seconds_grid_lines_land_on_whole_seconds() {
let (sr, map, rate) = (SampleRate::HZ_44100, TempoMap::default(), FrameRate::Fps30);
for g in [GridValue::Seconds(0.001), GridValue::Seconds(0.01)] {
for n in 0..20 {
let s = n * 44_100;
assert_eq!(g.snap(s, sr, &map, rate), s, "{g:?} at {n} s");
assert_eq!(g.line_at_or_before(s, sr, &map, rate), s, "{g:?} at {n} s");
}
}
}
/// Legacy frame grids stay aligned to exact frame starts at fractional rates.
#[test]
fn frame_grid_lines_land_on_frame_starts() {
let sr = SampleRate::HZ_48000;
let map = TempoMap::default();
for rate in [FrameRate::Fps29_97, FrameRate::Fps23_976] {
for f in [1, 10] {
let g = GridValue::Frames(f);
for k in (0..5000).step_by(37) {
let s = rate.samples_at_frame(k, sr);
let line = rate.samples_at_frame(k.div_euclid(f) * f, sr);
assert_eq!(g.line_at_or_before(s, sr, &map, rate), line, "{rate:?} {f} frames at frame {k}");
if k % f == 0 {
assert_eq!(g.snap(s, sr, &map, rate), s, "{rate:?} {f} frames at frame {k}");
}
}
let start = rate.samples_at_frame(4000, sr);
let end = rate.samples_at_frame(4000 + 20 * f, sr);
let want: Vec<_> = (0..20).map(|i| rate.samples_at_frame(4000 + i * f, sr)).collect();
assert_eq!(g.lines(start, end, sr, &map, rate, 100), want, "{rate:?} {f} frames");
}
}
}
/// Named whole- and sub-frame grids retain the exact-multiple calculation used by legacy frame grids.
#[test]
fn named_frame_grids_land_on_exact_subdivisions() {
let sr = SampleRate::HZ_48000;
let map = TempoMap::default();
let grids = TimecodeValue::ALL.into_iter().map(GridValue::Timecode).chain(FeetFramesValue::ALL.into_iter().map(GridValue::FeetFrames));
for rate in [FrameRate::Fps29_97, FrameRate::Fps23_976] {
for grid in grids.clone() {
for index in 0..200 {
let line = grid.abs_line(index, sr, rate);
assert_eq!(grid.line_at_or_before(line, sr, &map, rate), line, "{grid:?} {rate:?} line {index}");
assert_eq!(grid.next_line(line, sr, &map, rate), grid.abs_line(index + 1, sr, rate), "{grid:?} {rate:?} line {index}");
}
}
}
}
/// Drop-frame second grids follow labelled second boundaries in both timeline directions.
#[test]
fn drop_frame_second_grids_follow_timecode_labels() {
let sr = SampleRate::HZ_48000;
let map = TempoMap::default();
let grid = GridValue::Timecode(TimecodeValue::Second);
for rate in [FrameRate::Fps29_97Drop, FrameRate::Fps59_94Drop] {
for seconds in [0, 59, 60, 61, 600, 601, 3600] {
let frame = GridValue::drop_second_frame(seconds, rate);
let line = rate.samples_at_frame(frame, sr);
assert_eq!(grid.line_at_or_before(line, sr, &map, rate), line, "{rate:?} second {seconds}");
assert_eq!(grid.line_at_or_before(-line, sr, &map, rate), -line, "{rate:?} second -{seconds}");
for offset in -2..=rate.nominal() + 2 {
let position = rate.samples_at_frame(frame.saturating_add(offset), sr);
let before = grid.line_at_or_before(position, sr, &map, rate);
assert!(before <= position, "{rate:?} second {seconds}, frame offset {offset}");
assert!(grid.next_line(before, sr, &map, rate) > position, "{rate:?} second {seconds}, frame offset {offset}");
}
}
let labelled = Timecode { negative: false, hours: 0, minutes: 1, seconds: 1, frames: 0 }.to_samples(rate, sr);
assert_eq!(grid.snap(labelled, sr, &map, rate), labelled);
}
for (rate, minute_frame) in [(FrameRate::Fps29_97Drop, 1800), (FrameRate::Fps59_94Drop, 3600)] {
let boundary = rate.samples_at_frame(minute_frame, sr);
assert_eq!(grid.line_at_or_before(boundary, sr, &map, rate), boundary, "{rate:?} positive minute boundary");
assert_eq!(grid.snap(boundary, sr, &map, rate), boundary, "{rate:?} positive minute snap");
assert_eq!(grid.line_at_or_before(-boundary, sr, &map, rate), -boundary, "{rate:?} negative minute boundary");
assert_eq!(grid.snap(-boundary, sr, &map, rate), -boundary, "{rate:?} negative minute snap");
}
}
#[test]
fn lines_are_capped() {
let (sr, map, rate) = (SampleRate::HZ_48000, TempoMap::default(), FrameRate::Fps25);
assert_eq!(GridValue::Samples(1).lines(0, 1_000_000, sr, &map, rate, 50).len(), 50);
}
#[test]
fn triplets_and_dots() {
let (sr, map, rate) = (SampleRate::HZ_48000, TempoMap::default(), FrameRate::Fps25);
let t = GridValue::Note { value: NoteValue::Quarter, dotted: false, triplet: true };
assert_eq!(t.step_samples(0, sr, &map, rate), 16_000);
let d = GridValue::Note { value: NoteValue::Quarter, dotted: true, triplet: false };
assert_eq!(d.step_samples(0, sr, &map, rate), 36_000);
}
/// Stepping follows each menu ladder and stops at its endpoints.
#[test]
fn values_step_to_the_next_larger_or_smaller() {
let note = |value, triplet| GridValue::Note { value, dotted: false, triplet };
assert_eq!(note(NoteValue::Sixteenth, false).stepped(true), note(NoteValue::Eighth, false));
assert_eq!(note(NoteValue::Eighth, true).stepped(false), note(NoteValue::Sixteenth, true));
assert_eq!(note(NoteValue::Bar, false).stepped(true), note(NoteValue::Bar, false));
assert_eq!(note(NoteValue::SixtyFourth, false).stepped(false), note(NoteValue::SixtyFourth, false));
assert_eq!(GridValue::Seconds(0.01).stepped(true), GridValue::Seconds(0.1));
assert_eq!(GridValue::Seconds(0.5).stepped(false), GridValue::Seconds(0.1));
assert_eq!(GridValue::Seconds(1.0).stepped(true), GridValue::Seconds(1.0));
assert_eq!(GridValue::Frames(1).stepped(true), GridValue::Frames(10));
assert_eq!(GridValue::Timecode(TimecodeValue::Frame).stepped(false), GridValue::Timecode(TimecodeValue::HalfFrame));
assert_eq!(GridValue::FeetFrames(FeetFramesValue::Frame).stepped(true), GridValue::FeetFrames(FeetFramesValue::Foot));
assert_eq!(GridValue::Samples(100).stepped(false), GridValue::Samples(10));
assert_eq!(GridValue::Samples(1).stepped(true), GridValue::Samples(2));
assert_eq!(GridValue::Samples(1).stepped(false), GridValue::Samples(1));
}
#[test]
fn snap_at_extreme_position_does_not_overflow() {
let sr = SampleRate::new(8000).unwrap();
let map = TempoMap::new(1000.0, 4, 4).unwrap();
let _ = GridValue::default().snap(i64::MAX, sr, &map, FrameRate::Fps30);
}
}
+2 -153
View File
@@ -1,155 +1,4 @@
//! SoundCraft timebases.
//!
//! Every position in a session is an absolute sample index (`i64`) at the session sample rate.
//! This crate converts those positions to and from the five Pro Tools-style timebases
//! (Bars|Beats, Min:Secs, Timecode, Feet+Frames, Samples) through a [`TempoMap`], and computes
//! grid lines and snapping for the Grid / Nudge values.
//! Compatibility facade for SoundCraft's sample-index time API in shared `craft-time`.
#![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)]
mod format;
mod grid;
mod tempo;
mod timecode;
pub use format::{TimeFormat, format_length, format_position, parse_position};
pub use grid::{FeetFramesValue, GridScale, GridValue, NoteValue, TimecodeValue};
pub use tempo::{BarBeat, MeterEvent, TICKS_PER_QUARTER, TempoEvent, TempoMap};
pub use timecode::{FrameRate, Timecode};
/// A position or length in samples.
pub type Samples = i64;
/// Largest position accepted from parameters and files: 2^40 samples (about 265 days at 48 kHz).
/// Clamping there keeps every later addition and subtraction on positions far away from overflow.
pub const MAX_POSITION: Samples = 1 << 40;
/// Errors from parsing or converting time values.
#[derive(Debug, Clone, PartialEq, thiserror::Error)]
pub enum TimeError {
#[error("cannot parse `{0}` as a {1} value")]
Parse(String, &'static str),
#[error("invalid sample rate {0}")]
SampleRate(u32),
#[error("invalid tempo {0}")]
Tempo(f64),
#[error("invalid meter {0}/{1}")]
Meter(u32, u32),
}
/// Sample rates supported by sessions (Pro Tools offers 44.1 k – 192 k; we accept 8 k – 768 k).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
pub struct SampleRate(u32);
impl SampleRate {
pub const HZ_44100: SampleRate = SampleRate(44_100);
pub const HZ_48000: SampleRate = SampleRate(48_000);
pub const COMMON: [u32; 8] = [44_100, 48_000, 88_200, 96_000, 176_400, 192_000, 352_800, 384_000];
pub fn new(hz: u32) -> Result<Self, TimeError> {
if (8_000..=768_000).contains(&hz) { Ok(SampleRate(hz)) } else { Err(TimeError::SampleRate(hz)) }
}
pub fn hz(self) -> u32 {
self.0
}
pub fn as_f64(self) -> f64 {
f64::from(self.0)
}
/// Samples → seconds.
pub fn seconds(self, s: Samples) -> f64 {
s as f64 / self.as_f64()
}
/// Seconds → samples, rounded to nearest and saturated.
pub fn samples(self, seconds: f64) -> Samples {
to_samples(seconds * self.as_f64())
}
}
impl Default for SampleRate {
fn default() -> Self {
SampleRate::HZ_48000
}
}
/// Round a float sample count to `i64`, mapping NaN to 0 and saturating infinities.
pub fn to_samples(v: f64) -> Samples {
if v.is_nan() {
0
} else if v >= i64::MAX as f64 {
i64::MAX
} else if v <= i64::MIN as f64 {
i64::MIN
} else {
v.round() as i64
}
}
/// A half-open sample range `[start, end)`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default, serde::Serialize, serde::Deserialize)]
pub struct Range {
pub start: Samples,
pub end: Samples,
}
impl Range {
pub fn new(a: Samples, b: Samples) -> Self {
if a <= b { Range { start: a, end: b } } else { Range { start: b, end: a } }
}
pub fn point(at: Samples) -> Self {
Range { start: at, end: at }
}
pub fn len(&self) -> Samples {
self.end.saturating_sub(self.start)
}
pub fn is_empty(&self) -> bool {
self.end <= self.start
}
pub fn contains(&self, s: Samples) -> bool {
s >= self.start && s < self.end
}
pub fn overlaps(&self, o: &Range) -> bool {
self.start < o.end && o.start < self.end
}
pub fn intersect(&self, o: &Range) -> Option<Range> {
let s = self.start.max(o.start);
let e = self.end.min(o.end);
(s < e).then_some(Range { start: s, end: e })
}
pub fn shifted(&self, by: Samples) -> Range {
Range { start: self.start.saturating_add(by), end: self.end.saturating_add(by) }
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn sample_rate_bounds() {
assert!(SampleRate::new(48_000).is_ok());
assert!(SampleRate::new(0).is_err());
assert!(SampleRate::new(10_000_000).is_err());
}
#[test]
fn seconds_round_trip() {
let sr = SampleRate::HZ_44100;
assert_eq!(sr.samples(1.0), 44_100);
assert!((sr.seconds(22_050) - 0.5).abs() < 1e-12);
}
#[test]
fn to_samples_is_total() {
assert_eq!(to_samples(f64::NAN), 0);
assert_eq!(to_samples(f64::INFINITY), i64::MAX);
assert_eq!(to_samples(f64::NEG_INFINITY), i64::MIN);
}
#[test]
fn range_ops() {
let a = Range::new(10, 0);
assert_eq!(a, Range { start: 0, end: 10 });
assert_eq!(a.intersect(&Range::new(5, 20)), Some(Range::new(5, 10)));
assert_eq!(a.intersect(&Range::new(10, 20)), None);
assert!(a.contains(9) && !a.contains(10));
}
}
pub use craft_time::soundcraft::*;
-622
View File
@@ -1,622 +0,0 @@
//! Tempo and meter maps: the conversion between musical time (ticks) and samples.
use crate::{MAX_POSITION, SampleRate, Samples, TimeError, to_samples};
/// Ticks per quarter note (Pro Tools' resolution).
pub const TICKS_PER_QUARTER: i64 = 960;
/// A tempo change at a tick position. `bpm` is quarter notes per minute.
#[derive(Debug, Clone, Copy, PartialEq, serde::Serialize, serde::Deserialize)]
pub struct TempoEvent {
pub tick: i64,
pub bpm: f64,
}
/// A meter (time signature) change at a tick position, which must fall on a bar line.
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct MeterEvent {
pub tick: i64,
pub numerator: u32,
pub denominator: u32,
}
impl MeterEvent {
pub fn ticks_per_beat(&self) -> i64 {
let d = i64::from(self.denominator.max(1));
(TICKS_PER_QUARTER * 4 / d).max(1)
}
pub fn ticks_per_bar(&self) -> i64 {
self.ticks_per_beat().saturating_mul(i64::from(self.numerator.max(1)))
}
}
/// A musical position: 1-based bar and beat plus ticks within the beat.
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct BarBeat {
pub bar: i64,
pub beat: i64,
pub tick: i64,
}
/// Tempo and meter changes for a session. Always has at least one of each, at tick 0.
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
#[serde(from = "TempoMapData")]
pub struct TempoMap {
tempos: Vec<TempoEvent>,
meters: Vec<MeterEvent>,
/// For each tempo event, the elapsed time at its tick in quarter-note minutes (ticks / bpm,
/// summed over the earlier segments). Lets tick/sample conversions binary-search the segment
/// instead of walking every earlier tempo change, so converting many positions against a large
/// tempo map stays O(log n) each. Rebuilt whenever `tempos` changes.
#[serde(skip)]
elapsed: Vec<f64>,
}
/// The serialized form of [`TempoMap`], without the derived lookup table.
#[derive(serde::Deserialize)]
struct TempoMapData {
tempos: Vec<TempoEvent>,
meters: Vec<MeterEvent>,
}
impl From<TempoMapData> for TempoMap {
fn from(d: TempoMapData) -> Self {
TempoMap::from_parts(d.tempos, d.meters)
}
}
impl Default for TempoMap {
fn default() -> Self {
TempoMap::from_parts(vec![TempoEvent { tick: 0, bpm: 120.0 }], vec![MeterEvent { tick: 0, numerator: 4, denominator: 4 }])
}
}
fn valid_bpm(bpm: f64) -> Result<f64, TimeError> {
if bpm.is_finite() && (5.0..=1000.0).contains(&bpm) { Ok(bpm) } else { Err(TimeError::Tempo(bpm)) }
}
fn valid_meter(numerator: u32, denominator: u32) -> bool {
(1..=64).contains(&numerator) && [1, 2, 4, 8, 16, 32, 64].contains(&denominator)
}
impl TempoMap {
fn from_parts(tempos: Vec<TempoEvent>, meters: Vec<MeterEvent>) -> Self {
let mut m = TempoMap { tempos, meters, elapsed: Vec::new() };
m.reindex();
m
}
/// Rebuild `elapsed` from `tempos`. The first segment runs from tick 0 at the first tempo.
fn reindex(&mut self) {
let mut elapsed = Vec::with_capacity(self.tempos.len());
let mut acc = 0.0;
let mut seg_tick = 0i64;
let mut bpm = self.tempos.first().map_or(120.0, |e| e.bpm);
for (i, e) in self.tempos.iter().enumerate() {
if i > 0 {
// Saturating: a file's ticks are indexed before `sanitize` orders them.
acc += e.tick.saturating_sub(seg_tick) as f64 / bpm;
seg_tick = e.tick;
bpm = e.bpm;
}
elapsed.push(acc);
}
self.elapsed = elapsed;
}
/// Samples per quarter-note minute (the unit of `elapsed`) at `sr`.
fn samples_per_elapsed(sr: SampleRate) -> f64 {
sr.as_f64() * 60.0 / TICKS_PER_QUARTER as f64
}
/// The tempo segment `i` (an index into `tempos`, 0 when it is empty): its start tick (0 for
/// the first), bpm and elapsed time at its start.
fn segment(&self, i: usize) -> (i64, f64, f64) {
let bpm = self.tempos.get(i).or(self.tempos.first()).map_or(120.0, |e| e.bpm);
let tick = if i == 0 { 0 } else { self.tempos.get(i).map_or(0, |e| e.tick) };
(tick, bpm, self.elapsed.get(i).copied().unwrap_or(0.0))
}
pub fn new(bpm: f64, numerator: u32, denominator: u32) -> Result<Self, TimeError> {
let mut m = TempoMap::default();
m.set_tempo(0, bpm)?;
m.set_meter(0, numerator, denominator)?;
Ok(m)
}
pub fn tempos(&self) -> &[TempoEvent] {
&self.tempos
}
pub fn meters(&self) -> &[MeterEvent] {
&self.meters
}
/// Repair a map read from a file: only valid tempos and meters within `0..=MAX_POSITION` ticks, in
/// tick order, with one of each at tick 0.
pub fn sanitize(&mut self) {
self.tempos.retain(|e| (0..=MAX_POSITION).contains(&e.tick) && valid_bpm(e.bpm).is_ok());
self.tempos.sort_by_key(|e| e.tick);
self.tempos.dedup_by_key(|e| e.tick);
if self.tempos.first().is_none_or(|e| e.tick > 0) {
let bpm = self.tempos.first().map_or(120.0, |e| e.bpm);
self.tempos.insert(0, TempoEvent { tick: 0, bpm });
}
self.meters.retain(|m| (0..=MAX_POSITION).contains(&m.tick) && valid_meter(m.numerator, m.denominator));
self.meters.sort_by_key(|m| m.tick);
self.meters.dedup_by_key(|m| m.tick);
if self.meters.first().is_none_or(|m| m.tick > 0) {
let (numerator, denominator) = self.meters.first().map_or((4, 4), |m| (m.numerator, m.denominator));
self.meters.insert(0, MeterEvent { tick: 0, numerator, denominator });
}
self.reindex();
}
/// Insert or replace the tempo at `tick`.
pub fn set_tempo(&mut self, tick: i64, bpm: f64) -> Result<(), TimeError> {
let bpm = valid_bpm(bpm)?;
let tick = tick.max(0);
match self.tempos.binary_search_by(|e| e.tick.cmp(&tick)) {
Ok(i) => {
if let Some(e) = self.tempos.get_mut(i) {
e.bpm = bpm;
}
}
Err(i) => self.tempos.insert(i, TempoEvent { tick, bpm }),
}
self.reindex();
Ok(())
}
/// Remove a tempo event (the one at tick 0 is kept).
pub fn remove_tempo(&mut self, tick: i64) -> bool {
if tick == 0 {
return false;
}
let before = self.tempos.len();
self.tempos.retain(|e| e.tick != tick);
self.reindex();
before != self.tempos.len()
}
/// Insert or replace the meter at `tick` (snapped to the bar line at or before it).
/// Later meter changes keep their bar numbers, so they stay on bar lines.
pub fn set_meter(&mut self, tick: i64, numerator: u32, denominator: u32) -> Result<(), TimeError> {
if !valid_meter(numerator, denominator) {
return Err(TimeError::Meter(numerator, denominator));
}
let tick = if tick <= 0 { 0 } else { self.bar_start_tick(self.bar_beat_at_tick(tick).bar) };
let later = self.take_meters_after(tick);
self.put_meter(MeterEvent { tick, numerator, denominator });
self.restore_meters_at_bars(later);
Ok(())
}
/// Remove a meter event (the one at tick 0 is kept). Later meter changes keep their bar numbers.
pub fn remove_meter(&mut self, tick: i64) -> bool {
if tick == 0 || !self.meters.iter().any(|e| e.tick == tick) {
return false;
}
let later = self.take_meters_after(tick);
self.meters.retain(|e| e.tick != tick);
self.restore_meters_at_bars(later);
true
}
fn put_meter(&mut self, ev: MeterEvent) {
match self.meters.binary_search_by(|e| e.tick.cmp(&ev.tick)) {
Ok(i) => {
if let Some(e) = self.meters.get_mut(i) {
*e = ev;
}
}
Err(i) => self.meters.insert(i, ev),
}
}
/// Remove the meters after `tick`, returning each with the bar it starts: the bar
/// [`Self::bar_beat_at_tick`] gives its tick, counted in one pass over the map rather than one
/// walk per meter, so a session with many meter changes edits in linear time.
fn take_meters_after(&mut self, tick: i64) -> Vec<(i64, MeterEvent)> {
let mut later = Vec::new();
let (mut bar, mut seg_tick) = (1i64, 0i64);
for (i, m) in self.meters.iter().enumerate() {
let tpb = m.ticks_per_bar();
if m.tick > tick {
later.push((bar.saturating_add(m.tick.saturating_sub(seg_tick) / tpb), *m));
}
if let Some(n) = self.meters.get(i + 1) {
bar = bar.saturating_add(ceil_div(n.tick.saturating_sub(seg_tick), tpb));
seg_tick = n.tick;
}
}
self.meters.retain(|e| e.tick <= tick);
later
}
/// The bar and tick of the last meter's segment, where [`Self::bar_start_tick`]'s walk ends.
fn last_segment(&self) -> (i64, i64) {
let (mut cur_bar, mut cur_tick) = (1i64, 0i64);
for (i, m) in self.meters.iter().enumerate() {
if let Some(n) = self.meters.get(i + 1) {
cur_bar = cur_bar.saturating_add(ceil_div(n.tick.saturating_sub(cur_tick), m.ticks_per_bar()));
cur_tick = n.tick;
}
}
(cur_bar, cur_tick)
}
/// Put meters back at the start of their bars under the current earlier meters, in bar order.
/// Each lands at or past the last meter, so the walk to its bar resumes from there; a bar
/// before it (meters off their bar lines) takes the full walk.
fn restore_meters_at_bars(&mut self, later: Vec<(i64, MeterEvent)>) {
let (mut cur_bar, mut cur_tick) = self.last_segment();
for (bar, ev) in later {
let tick = if bar >= cur_bar {
let tpb = self.meters.last().map_or(MeterEvent { tick: 0, numerator: 4, denominator: 4 }, |m| *m).ticks_per_bar();
cur_tick.saturating_add(bar.saturating_sub(cur_bar).saturating_mul(tpb))
} else {
self.bar_start_tick(bar)
};
self.put_meter(MeterEvent { tick, ..ev });
(cur_bar, cur_tick) = if bar >= cur_bar { (bar, tick) } else { self.last_segment() };
}
}
/// Tempo in effect at `tick`.
pub fn tempo_at_tick(&self, tick: i64) -> f64 {
let mut bpm = self.tempos.first().map_or(120.0, |e| e.bpm);
for e in &self.tempos {
if e.tick <= tick {
bpm = e.bpm;
} else {
break;
}
}
bpm
}
/// Meter in effect at `tick`.
pub fn meter_at_tick(&self, tick: i64) -> MeterEvent {
let mut m = self.meters.first().copied().unwrap_or(MeterEvent { tick: 0, numerator: 4, denominator: 4 });
for e in &self.meters {
if e.tick <= tick {
m = *e;
} else {
break;
}
}
m
}
fn samples_per_tick(bpm: f64, sr: SampleRate) -> f64 {
sr.as_f64() * 60.0 / (bpm * TICKS_PER_QUARTER as f64)
}
/// Ticks → samples (exact for constant-tempo segments).
pub fn tick_to_samples(&self, tick: i64, sr: SampleRate) -> Samples {
to_samples(self.tick_to_samples_f(tick, sr))
}
fn tick_to_samples_f(&self, tick: i64, sr: SampleRate) -> f64 {
// Tempo events after the first that start before `tick` (the segments it has passed).
let passed = self.tempos.get(1..).map_or(0, |rest| rest.partition_point(|e| e.tick < tick));
let (seg_tick, bpm, elapsed) = self.segment(passed);
elapsed * Self::samples_per_elapsed(sr) + (tick - seg_tick) as f64 * Self::samples_per_tick(bpm, sr)
}
/// Samples → ticks (fractional).
pub fn samples_to_ticks_f(&self, s: Samples, sr: SampleRate) -> f64 {
let s = s as f64;
let scale = Self::samples_per_elapsed(sr);
// Tempo events after the first that start at or before `s`.
let passed = self.elapsed.get(1..).map_or(0, |rest| rest.partition_point(|e| e * scale <= s));
let (seg_tick, bpm, elapsed) = self.segment(passed);
seg_tick as f64 + (s - elapsed * scale) / Self::samples_per_tick(bpm, sr)
}
pub fn samples_to_ticks(&self, s: Samples, sr: SampleRate) -> i64 {
to_samples(self.samples_to_ticks_f(s, sr))
}
/// Tick of the first beat of `bar` (1-based; bars before 1 extend the first meter backwards).
pub fn bar_start_tick(&self, bar: i64) -> i64 {
let mut cur_bar = 1i64;
let mut cur_tick = 0i64;
let first = self.meter_at_tick(0);
if bar < 1 {
return bar.saturating_sub(1).saturating_mul(first.ticks_per_bar());
}
for (i, m) in self.meters.iter().enumerate() {
let next_tick = self.meters.get(i + 1).map(|n| n.tick);
let tpb = m.ticks_per_bar();
let bars_in_seg = next_tick.map(|nt| ceil_div(nt.saturating_sub(cur_tick), tpb));
match bars_in_seg {
Some(n) if bar >= cur_bar.saturating_add(n) => {
cur_bar = cur_bar.saturating_add(n);
cur_tick = next_tick.unwrap_or(cur_tick);
}
_ => return cur_tick.saturating_add((bar - cur_bar).saturating_mul(tpb)),
}
}
cur_tick
}
/// Bar|beat|tick at `tick`.
pub fn bar_beat_at_tick(&self, tick: i64) -> BarBeat {
let first = self.meter_at_tick(0);
if tick < 0 {
let tpb = first.ticks_per_bar();
let abs = tick.unsigned_abs();
let tpb_u = tpb.unsigned_abs();
let bars_back_u = abs / tpb_u + u64::from(!abs.is_multiple_of(tpb_u));
let bars_back = i64::try_from(bars_back_u).unwrap_or(i64::MAX);
// Offset of `tick` inside its bar, without negating `i64::MIN`.
let within = ((tpb_u - abs % tpb_u) % tpb_u) as i64;
let beat_t = first.ticks_per_beat();
return BarBeat { bar: 1 - bars_back, beat: within / beat_t + 1, tick: within % beat_t };
}
let mut bar = 1i64;
let mut seg_tick = 0i64;
for (i, m) in self.meters.iter().enumerate() {
let tpb = m.ticks_per_bar();
if let Some(n) = self.meters.get(i + 1)
&& tick >= n.tick
{
bar = bar.saturating_add(ceil_div(n.tick.saturating_sub(seg_tick), tpb));
seg_tick = n.tick;
continue;
}
let rel = tick - seg_tick;
let bars = rel / tpb;
let within = rel % tpb;
let beat_t = m.ticks_per_beat();
return BarBeat { bar: bar + bars, beat: within / beat_t + 1, tick: within % beat_t };
}
BarBeat { bar, beat: 1, tick: 0 }
}
/// Bar|beat|tick → tick.
pub fn tick_at_bar_beat(&self, bb: BarBeat) -> i64 {
let start = self.bar_start_tick(bb.bar);
let m = self.meter_at_tick(start);
start.saturating_add(bb.beat.saturating_sub(1).max(0).saturating_mul(m.ticks_per_beat())).saturating_add(bb.tick)
}
pub fn bar_beat_at(&self, s: Samples, sr: SampleRate) -> BarBeat {
self.bar_beat_at_tick(self.samples_to_ticks(s, sr))
}
pub fn samples_at_bar_beat(&self, bb: BarBeat, sr: SampleRate) -> Samples {
self.tick_to_samples(self.tick_at_bar_beat(bb), sr)
}
/// Scale every tempo by `factor` (Tempo Operations › Scale).
pub fn scale_tempos(&mut self, factor: f64) -> Result<(), TimeError> {
let mut out = self.tempos.clone();
for e in &mut out {
e.bpm = valid_bpm(e.bpm * factor)?;
}
self.tempos = out;
self.reindex();
Ok(())
}
}
/// Ceiling division for a non-negative `num` and positive `den`, without overflow.
fn ceil_div(num: i64, den: i64) -> i64 {
num / den + i64::from(num % den != 0)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn default_map_is_120_4_4() {
let m = TempoMap::default();
let sr = SampleRate::HZ_48000;
// One bar of 4/4 at 120 bpm = 2 seconds.
assert_eq!(m.samples_at_bar_beat(BarBeat { bar: 2, beat: 1, tick: 0 }, sr), 96_000);
assert_eq!(m.bar_beat_at(96_000, sr), BarBeat { bar: 2, beat: 1, tick: 0 });
assert_eq!(m.bar_beat_at(24_000, sr), BarBeat { bar: 1, beat: 2, tick: 0 });
}
#[test]
fn meter_near_max_tick_does_not_overflow() {
let mut m = TempoMap::default();
m.set_meter(i64::MAX, 7, 8).unwrap();
let bb = m.bar_beat_at_tick(i64::MAX);
assert!(bb.bar > 1);
assert!(m.bar_start_tick(bb.bar) > 0);
assert!(m.bar_start_tick(i64::MAX) > 0);
}
#[test]
fn minimum_bar_saturates_instead_of_overflowing() {
let m = TempoMap::default();
assert_eq!(m.bar_start_tick(i64::MIN), i64::MIN);
assert_eq!(m.bar_start_tick(0), -TICKS_PER_QUARTER * 4);
}
#[test]
fn minimum_beat_is_clamped_without_overflow() {
let m = TempoMap::default();
assert_eq!(m.tick_at_bar_beat(BarBeat { bar: 1, beat: i64::MIN, tick: 0 }), 0);
}
#[test]
fn bar_beat_at_min_tick_does_not_overflow() {
let m = TempoMap::default();
let bb = m.bar_beat_at_tick(i64::MIN);
assert!(bb.bar < 1);
assert!(bb.beat >= 1 && bb.tick >= 0);
assert_eq!(m.bar_beat_at_tick(-1).bar, 0);
}
#[test]
fn tempo_change_is_integrated() {
let mut m = TempoMap::default();
let sr = SampleRate::HZ_48000;
m.set_tempo(TICKS_PER_QUARTER * 4, 60.0).unwrap();
// Bar 1 at 120 = 96000 samples; bar 2 at 60 = 192000 samples.
assert_eq!(m.tick_to_samples(TICKS_PER_QUARTER * 8, sr), 96_000 + 192_000);
assert_eq!(m.samples_to_ticks(96_000 + 192_000, sr), TICKS_PER_QUARTER * 8);
}
#[test]
fn meter_change_counts_bars() {
let mut m = TempoMap::default();
// 2 bars of 4/4 then 3/4.
m.set_meter(TICKS_PER_QUARTER * 8, 3, 4).unwrap();
assert_eq!(m.bar_beat_at_tick(TICKS_PER_QUARTER * 8), BarBeat { bar: 3, beat: 1, tick: 0 });
assert_eq!(m.bar_beat_at_tick(TICKS_PER_QUARTER * 11), BarBeat { bar: 4, beat: 1, tick: 0 });
assert_eq!(m.bar_start_tick(4), TICKS_PER_QUARTER * 11);
let bb = BarBeat { bar: 4, beat: 2, tick: 10 };
assert_eq!(m.bar_beat_at_tick(m.tick_at_bar_beat(bb)), bb);
}
#[test]
fn editing_an_early_meter_keeps_later_bars_and_scales_linearly() {
// 200 000 meter changes, one per bar, built directly: a walk per meter would take
// minutes to edit here, and even to build through `set_meter`.
let mut m = TempoMap::new(120.0, 4, 4).unwrap();
let mut tick = 0;
for bar in 1..200_000i64 {
let numerator = 3 + (bar % 2) as u32;
tick += m.meters.last().map_or(0, MeterEvent::ticks_per_bar);
m.meters.push(MeterEvent { tick, numerator, denominator: 4 });
}
let sample = [1usize, 1_000, 199_998];
let bars = |m: &TempoMap| sample.map(|i| m.bar_beat_at_tick(m.meters[i].tick).bar);
assert_eq!(bars(&m), [2, 1_001, 199_999]);
m.set_meter(0, 7, 8).unwrap();
assert_eq!(bars(&m), [2, 1_001, 199_999], "later meters keep their bar numbers");
assert_eq!(m.meters.len(), 200_000);
assert!(m.meters.windows(2).all(|w| w[0].tick < w[1].tick));
assert!(m.remove_meter(m.meters[1].tick));
assert_eq!([1usize, 999, 199_997].map(|i| m.bar_beat_at_tick(m.meters[i].tick).bar), [3, 1_001, 199_999]);
// Meters off their bar lines (counted as bars 2 and 4) land where they always did: on
// bar 2 of the new meter, then two bars of their own later.
let mut off = TempoMap::new(120.0, 4, 4).unwrap();
off.meters.push(MeterEvent { tick: 100, numerator: 3, denominator: 4 });
off.meters.push(MeterEvent { tick: 5_000, numerator: 5, denominator: 4 });
off.set_meter(0, 2, 4).unwrap();
let placed: Vec<(i64, u32)> = off.meters.iter().map(|e| (e.tick, e.numerator)).collect();
assert_eq!(placed, vec![(0, 2), (2 * TICKS_PER_QUARTER, 3), (8 * TICKS_PER_QUARTER, 5)]);
}
#[test]
fn eighth_note_meters() {
let m = TempoMap::new(120.0, 6, 8).unwrap();
assert_eq!(m.bar_beat_at_tick(TICKS_PER_QUARTER * 3), BarBeat { bar: 2, beat: 1, tick: 0 });
assert_eq!(m.bar_beat_at_tick(TICKS_PER_QUARTER / 2), BarBeat { bar: 1, beat: 2, tick: 0 });
}
#[test]
fn sanitize_keeps_what_the_setters_accept() {
let tempo = |tick, bpm| TempoEvent { tick, bpm };
let meter = |tick, numerator, denominator| MeterEvent { tick, numerator, denominator };
let mut m = TempoMap::from_parts(
vec![tempo(960, 4e9), tempo(3840, 90.0), tempo(-5, 100.0), tempo(1920, 60.0), tempo(1920, 70.0)],
vec![meter(0, 0, 4), meter(3840, 3, 4)],
);
m.sanitize();
assert_eq!(m.tempos(), [tempo(0, 60.0), tempo(1920, 60.0), tempo(3840, 90.0)]);
assert_eq!(m.meters(), [meter(0, 3, 4), meter(3840, 3, 4)]);
let mut empty = TempoMap::from_parts(Vec::new(), Vec::new());
empty.sanitize();
assert_eq!(empty, TempoMap::default());
}
#[test]
fn sanitize_drops_events_beyond_the_position_range() {
let mut m = TempoMap::from_parts(
vec![TempoEvent { tick: 0, bpm: 120.0 }, TempoEvent { tick: i64::MAX, bpm: 90.0 }],
vec![MeterEvent { tick: 0, numerator: 4, denominator: 4 }, MeterEvent { tick: i64::MAX, numerator: 3, denominator: 4 }],
);
m.sanitize();
assert_eq!(m, TempoMap::default());
assert_eq!(m.bar_start_tick(2), TICKS_PER_QUARTER * 4);
}
#[test]
fn rejects_bad_values() {
let mut m = TempoMap::default();
assert!(m.set_tempo(0, f64::NAN).is_err());
assert!(m.set_tempo(0, 0.0).is_err());
assert!(m.set_meter(0, 0, 4).is_err());
assert!(m.set_meter(0, 4, 3).is_err());
assert!(!m.remove_tempo(0));
}
#[test]
fn negative_positions() {
let m = TempoMap::default();
assert_eq!(m.bar_beat_at_tick(-TICKS_PER_QUARTER * 4), BarBeat { bar: 0, beat: 1, tick: 0 });
assert_eq!(m.bar_beat_at_tick(-TICKS_PER_QUARTER), BarBeat { bar: 0, beat: 4, tick: 0 });
}
/// The segment walk the indexed conversions replaced, kept as the reference they must match.
fn linear_tick_to_samples(m: &TempoMap, tick: i64, sr: SampleRate) -> i64 {
let mut acc = 0.0;
let mut seg_tick = 0i64;
let mut bpm = m.tempos[0].bpm;
for e in m.tempos.iter().skip(1) {
if e.tick >= tick {
break;
}
acc += (e.tick - seg_tick) as f64 * TempoMap::samples_per_tick(bpm, sr);
seg_tick = e.tick;
bpm = e.bpm;
}
to_samples(acc + (tick - seg_tick) as f64 * TempoMap::samples_per_tick(bpm, sr))
}
#[test]
fn indexed_conversions_match_the_segment_walk() {
let sr = SampleRate::HZ_48000;
let mut m = TempoMap::default();
for i in 1..200i64 {
m.set_tempo(i * 487, 40.0 + ((i * 37) % 300) as f64).unwrap();
}
assert!(m.remove_tempo(487 * 50));
for tick in (-1000..200 * 487 + 5000).step_by(113) {
let s = m.tick_to_samples(tick, sr);
assert_eq!(s, linear_tick_to_samples(&m, tick, sr), "tick {tick}");
assert!((m.samples_to_ticks(s, sr) - tick).abs() <= 1, "tick {tick}");
}
let json = serde_json::to_string(&m).unwrap();
assert!(!json.contains("elapsed"));
let back: TempoMap = serde_json::from_str(&json).unwrap();
assert_eq!(back, m);
assert_eq!(back.tick_to_samples(150 * 487 + 3, sr), m.tick_to_samples(150 * 487 + 3, sr));
}
#[test]
fn a_file_with_wild_ticks_is_indexed_without_overflow() {
let json = r#"{"tempos":[{"tick":0,"bpm":120.0},{"tick":-9223372036854775808,"bpm":100.0},{"tick":9223372036854775807,"bpm":90.0}],"meters":[{"tick":0,"numerator":4,"denominator":4}]}"#;
let mut m: TempoMap = serde_json::from_str(json).unwrap();
m.sanitize();
assert_eq!(m.tempos().first().map(|e| e.tick), Some(0));
assert_eq!(m.elapsed.len(), m.tempos().len());
}
#[test]
fn conversions_against_a_large_tempo_map_stay_fast() {
// 200k tempo changes, then 200k conversions each way: quadratic if every conversion walks
// the earlier segments, well under a second when it binary-searches them.
let sr = SampleRate::HZ_48000;
let n = 200_000i64;
let tempos = (0..n).map(|i| TempoEvent { tick: i * 10, bpm: 60.0 + (i % 7) as f64 }).collect();
let mut m = TempoMap::from_parts(tempos, vec![MeterEvent { tick: 0, numerator: 4, denominator: 4 }]);
m.sanitize();
let start = std::time::Instant::now();
let mut sum = 0i64;
for i in 0..n {
let s = m.tick_to_samples(i * 10 + 5, sr);
sum = sum.wrapping_add(s).wrapping_add(m.samples_to_ticks(s, sr));
}
assert!(sum != 0);
assert!(start.elapsed() < std::time::Duration::from_secs(5), "{:?}", start.elapsed());
}
}
-309
View File
@@ -1,309 +0,0 @@
//! SMPTE timecode and Feet+Frames.
use crate::{SampleRate, Samples, TimeError};
/// Session timecode rates.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default, serde::Serialize, serde::Deserialize)]
pub enum FrameRate {
Fps23_976,
#[default]
Fps24,
Fps25,
Fps29_97,
Fps29_97Drop,
Fps30,
Fps47_952,
Fps48,
Fps50,
Fps59_94,
Fps59_94Drop,
Fps60,
Fps100,
Fps119_88,
Fps120,
}
impl FrameRate {
pub const ALL: [FrameRate; 15] = [
FrameRate::Fps23_976,
FrameRate::Fps24,
FrameRate::Fps25,
FrameRate::Fps29_97,
FrameRate::Fps29_97Drop,
FrameRate::Fps30,
FrameRate::Fps47_952,
FrameRate::Fps48,
FrameRate::Fps50,
FrameRate::Fps59_94,
FrameRate::Fps59_94Drop,
FrameRate::Fps60,
FrameRate::Fps100,
FrameRate::Fps119_88,
FrameRate::Fps120,
];
/// Real frames per second as a rational (num, den).
pub fn rational(self) -> (i64, i64) {
match self {
FrameRate::Fps23_976 => (24_000, 1001),
FrameRate::Fps24 => (24, 1),
FrameRate::Fps25 => (25, 1),
FrameRate::Fps29_97 | FrameRate::Fps29_97Drop => (30_000, 1001),
FrameRate::Fps30 => (30, 1),
FrameRate::Fps47_952 => (48_000, 1001),
FrameRate::Fps48 => (48, 1),
FrameRate::Fps50 => (50, 1),
FrameRate::Fps59_94 | FrameRate::Fps59_94Drop => (60_000, 1001),
FrameRate::Fps60 => (60, 1),
FrameRate::Fps100 => (100, 1),
FrameRate::Fps119_88 => (120_000, 1001),
FrameRate::Fps120 => (120, 1),
}
}
/// Frames per labelled second (the counting base).
pub fn nominal(self) -> i64 {
let (n, d) = self.rational();
(n + d / 2) / d
}
pub fn is_drop(self) -> bool {
matches!(self, FrameRate::Fps29_97Drop | FrameRate::Fps59_94Drop)
}
pub fn label(self) -> &'static str {
match self {
FrameRate::Fps23_976 => "23.976",
FrameRate::Fps24 => "24",
FrameRate::Fps25 => "25",
FrameRate::Fps29_97 => "29.97",
FrameRate::Fps29_97Drop => "29.97 Drop",
FrameRate::Fps30 => "30",
FrameRate::Fps47_952 => "47.952",
FrameRate::Fps48 => "48",
FrameRate::Fps50 => "50",
FrameRate::Fps59_94 => "59.94",
FrameRate::Fps59_94Drop => "59.94 Drop",
FrameRate::Fps60 => "60",
FrameRate::Fps100 => "100",
FrameRate::Fps119_88 => "119.88",
FrameRate::Fps120 => "120",
}
}
/// Whole frame index at a sample position (floored).
pub fn frames_at(self, s: Samples, sr: SampleRate) -> i64 {
let (n, d) = self.rational();
let num = i128::from(s) * i128::from(n);
let den = i128::from(sr.hz()) * i128::from(d);
if den == 0 {
return 0;
}
i64::try_from(num.div_euclid(den)).unwrap_or(0)
}
/// Sample position of the start of frame `f`.
pub fn samples_at_frame(self, f: i64, sr: SampleRate) -> Samples {
let (n, d) = self.rational();
if n == 0 {
return 0;
}
// Round half away from zero so negative frames mirror positive ones.
let mag = (i128::from(f.unsigned_abs()) * i128::from(sr.hz()) * i128::from(d) + i128::from(n) / 2) / i128::from(n);
let v = if f < 0 { -mag } else { mag };
i64::try_from(v).unwrap_or(if v < 0 { i64::MIN } else { i64::MAX })
}
/// Samples per frame (fractional).
pub fn samples_per_frame(self, sr: SampleRate) -> f64 {
let (n, d) = self.rational();
sr.as_f64() * d as f64 / n as f64
}
}
/// A timecode label.
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct Timecode {
pub negative: bool,
pub hours: i64,
pub minutes: i64,
pub seconds: i64,
pub frames: i64,
}
impl Timecode {
/// Label for an absolute frame count.
pub fn from_frames(frames: i64, rate: FrameRate) -> Timecode {
let negative = frames < 0;
// Drop-frame labels can exceed i64::MAX even when the frame count fits. Widen before
// taking the magnitude so i64::MIN is represented exactly too.
let mut f = i128::from(frames).abs();
let base = i128::from(rate.nominal().max(1));
if rate.is_drop() {
// Drop 2 (or 4) labels each minute except every tenth.
let drop = if base >= 60 { 4 } else { 2 };
let per_10min = base * 600 - drop * 9;
let per_min = base * 60 - drop;
let tens = f / per_10min;
let rem = f % per_10min;
let extra = if rem > drop { drop * 9 * tens + drop * ((rem - drop) / per_min) } else { drop * 9 * tens };
f += extra;
}
let frames_l = f % base;
let total_secs = f / base;
Timecode {
negative,
hours: i64::try_from(total_secs / 3600).unwrap_or(i64::MAX),
minutes: i64::try_from((total_secs / 60) % 60).unwrap_or(0),
seconds: i64::try_from(total_secs % 60).unwrap_or(0),
frames: i64::try_from(frames_l).unwrap_or(0),
}
}
/// Absolute frame count of this label.
pub fn to_frames(&self, rate: FrameRate) -> i64 {
let base = rate.nominal().max(1);
let total_min = i128::from(self.hours) * 60 + i128::from(self.minutes);
let mut f = (total_min * 60 + i128::from(self.seconds)) * i128::from(base) + i128::from(self.frames);
if rate.is_drop() {
let drop = if base >= 60 { 4 } else { 2 };
f -= drop * (total_min - total_min / 10);
}
let f = if self.negative { -f } else { f };
i64::try_from(f).unwrap_or(if f < 0 { i64::MIN } else { i64::MAX })
}
pub fn from_samples(s: Samples, rate: FrameRate, sr: SampleRate) -> Timecode {
Timecode::from_frames(rate.frames_at(s, sr), rate)
}
pub fn to_samples(&self, rate: FrameRate, sr: SampleRate) -> Samples {
rate.samples_at_frame(self.to_frames(rate), sr)
}
pub fn format(&self, rate: FrameRate) -> String {
let sep = if rate.is_drop() { ';' } else { ':' };
let sign = if self.negative { "-" } else { "" };
format!("{sign}{:02}:{:02}:{:02}{sep}{:02}", self.hours, self.minutes, self.seconds, self.frames)
}
/// Parse `HH:MM:SS:FF` (also `;` / `.` separators and fewer fields, right-aligned).
pub fn parse(text: &str) -> Result<Timecode, TimeError> {
let t = text.trim();
let (negative, t) = match t.strip_prefix('-') {
Some(r) => (true, r),
None => (false, t),
};
let parts: Vec<&str> = t.split([':', ';', '.']).collect();
if parts.is_empty() || parts.len() > 4 {
return Err(TimeError::Parse(text.to_string(), "timecode"));
}
let mut nums = [0i64; 4];
let offset = 4 - parts.len();
for (i, p) in parts.iter().enumerate() {
let v: i64 = p.trim().parse().map_err(|_| TimeError::Parse(text.to_string(), "timecode"))?;
if v < 0 {
return Err(TimeError::Parse(text.to_string(), "timecode"));
}
if let Some(slot) = nums.get_mut(offset + i) {
*slot = v;
}
}
Ok(Timecode { negative, hours: nums[0], minutes: nums[1], seconds: nums[2], frames: nums[3] })
}
}
/// Feet+Frames for 35 mm film (16 frames per foot), `start` frames in at sample 0.
pub fn feet_frames(s: Samples, rate: FrameRate, sr: SampleRate, start: i64) -> (i64, i64) {
let f = rate.frames_at(s, sr).saturating_add(start);
(f.div_euclid(16), f.rem_euclid(16))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn non_drop_round_trip() {
let sr = SampleRate::HZ_48000;
let tc = Timecode::from_samples(48_000 * 3661, FrameRate::Fps25, sr);
assert_eq!(tc.format(FrameRate::Fps25), "01:01:01:00");
assert_eq!(tc.to_samples(FrameRate::Fps25, sr), 48_000 * 3661);
}
#[test]
fn negative_timecode_rounds_symmetrically() {
let sr = SampleRate::HZ_48000;
let tc = Timecode::parse("-00:00:01:00").unwrap();
assert_eq!(tc.to_samples(FrameRate::Fps25, sr), -48_000);
for r in [FrameRate::Fps25, FrameRate::Fps29_97, FrameRate::Fps29_97Drop, FrameRate::Fps23_976] {
for f in [1i64, 7, 25, 1799, 107_892] {
assert_eq!(r.samples_at_frame(-f, sr), -r.samples_at_frame(f, sr), "{r:?} frame {f}");
}
}
}
#[test]
fn drop_frame_labels() {
let r = FrameRate::Fps29_97Drop;
// Frame 1800 is the first frame of minute 1 → labelled 00:01:00;02.
assert_eq!(Timecode::from_frames(1800, r).format(r), "00:01:00;02");
assert_eq!(Timecode::from_frames(1799, r).format(r), "00:00:59;29");
// Minute 10 keeps its labels.
assert_eq!(Timecode::from_frames(17_982, r).format(r), "00:10:00;00");
for f in [0, 1, 1799, 1800, 1801, 17_981, 17_982, 107_892, 500_000] {
assert_eq!(Timecode::from_frames(f, r).to_frames(r), f, "frame {f}");
}
}
#[test]
fn huge_timecode_saturates_instead_of_overflowing() {
let sr = SampleRate::HZ_48000;
for r in [FrameRate::Fps30, FrameRate::Fps29_97Drop] {
let tc = Timecode::parse("9223372036854775807:00:00:00").unwrap();
assert_eq!(tc.to_samples(r, sr), i64::MAX);
let tc = Timecode::parse("-9223372036854775807:00:00:00").unwrap();
assert_eq!(tc.to_samples(r, sr), i64::MIN);
}
let tc = Timecode::parse("01:02:03:04").unwrap();
assert_eq!(tc.to_samples(FrameRate::Fps30, sr), 178_710_400);
}
#[test]
fn extreme_frame_counts_round_trip_at_every_rate() {
for rate in FrameRate::ALL {
for frames in [i64::MIN, i64::MIN + 1, -1, 0, 1, i64::MAX - 1, i64::MAX] {
let tc = Timecode::from_frames(frames, rate);
assert_eq!(tc.to_frames(rate), frames, "{rate:?}: {frames}");
assert_eq!(tc.negative, frames < 0);
assert!((0..60).contains(&tc.minutes));
assert!((0..60).contains(&tc.seconds));
assert!((0..rate.nominal()).contains(&tc.frames));
assert_eq!(Timecode::parse(&tc.format(rate)).unwrap(), tc);
}
}
}
#[test]
fn extreme_drop_frame_labels_do_not_overflow() {
for rate in [FrameRate::Fps29_97Drop, FrameRate::Fps59_94Drop] {
assert_eq!(Timecode::from_frames(i64::MAX, rate).to_frames(rate), i64::MAX);
}
}
#[test]
fn parse_variants() {
let tc = Timecode::parse("01:02:03:04").unwrap();
assert_eq!((tc.hours, tc.minutes, tc.seconds, tc.frames), (1, 2, 3, 4));
let tc = Timecode::parse("3:04").unwrap();
assert_eq!((tc.seconds, tc.frames), (3, 4));
assert!(Timecode::parse("a:b").is_err());
assert!(Timecode::parse("1:2:3:4:5").is_err());
}
#[test]
fn feet_and_frames() {
let sr = SampleRate::HZ_48000;
assert_eq!(feet_frames(48_000, FrameRate::Fps24, sr, 0), (1, 8));
}
}
+1
View File
@@ -8,6 +8,7 @@ rust-version = "1.95"
repository.workspace = true
[dependencies]
craft-mime.workspace = true
craft-ui.workspace = true
soundcraft-time.workspace = true
craft-audio.workspace = true
+2 -18
View File
@@ -824,24 +824,8 @@ fn encode(img: &egui::ColorImage, path: Option<&str>, token: u64) -> Value {
}
}
/// Minimal base64 (standard alphabet) to avoid another dependency.
pub fn base64(data: &[u8]) -> String {
const T: &[u8; 64] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
let mut out = String::with_capacity(data.len().div_ceil(3) * 4);
for chunk in data.chunks(3) {
let b = [chunk.first().copied().unwrap_or(0), chunk.get(1).copied().unwrap_or(0), chunk.get(2).copied().unwrap_or(0)];
let n = (u32::from(b[0]) << 16) | (u32::from(b[1]) << 8) | u32::from(b[2]);
for i in 0..4 {
if i <= chunk.len() {
let idx = ((n >> (18 - 6 * i)) & 63) as usize;
out.push(char::from(T.get(idx).copied().unwrap_or(b'A')));
} else {
out.push('=');
}
}
}
out
}
/// Standard padded Base64 without line wrapping, shared across Craft apps.
pub use craft_mime::transfer::base64;
#[cfg(test)]
mod tests;
+16
View File
@@ -899,3 +899,19 @@ fn scroll_reports_the_wheel_and_trackpad_sources_after_delivery() {
let v = reply.try_recv().expect("trackpad scroll replies after its events were applied");
assert_eq!(v["result"]["source"], "trackpad");
}
#[test]
fn shared_base64_retains_control_wire_bytes() {
assert_eq!(super::base64(b""), "");
assert_eq!(super::base64(b"f"), "Zg==");
assert_eq!(super::base64(b"fo"), "Zm8=");
assert_eq!(super::base64(b"foo"), "Zm9v");
assert_eq!(super::base64(&[251, 255, 255]), "+///");
let bytes: Vec<u8> = (0..=255).cycle().take(513).collect();
for len in 0..=bytes.len() {
let encoded = super::base64(&bytes[..len]);
assert_eq!(encoded.len(), len.div_ceil(3) * 4);
assert!(!encoded.contains(['\r', '\n']));
assert_eq!(craft_mime::base64::STANDARD.decode(&encoded).unwrap(), bytes[..len]);
}
}
+71 -18
View File
@@ -82,6 +82,8 @@ pub struct Dialogs {
strip_values: StripValues,
/// The Strip Silence rectangles the Edit window draws while Strip Silence is open.
strip_rectangles: StripRectangles,
/// True while a dialog was drawn on the previous frame.
shown: bool,
}
impl Dialogs {
@@ -115,22 +117,25 @@ impl Dialogs {
/// Whether a worker thread is finding Strip Silence rectangles.
pub fn strip_scan_running(&self) -> bool {
#[cfg(not(target_arch = "wasm32"))]
if self.strip_rectangles.running.is_some() {
return true;
{
self.strip_rectangles.running.is_some()
}
#[cfg(target_arch = "wasm32")]
{
false
}
false
}
pub fn open_rename_track(&mut self, id: TrackId, name: &str) {
self.open = Some(Dialog::RenameTrack { id, name: name.to_string() });
self.set(Dialog::RenameTrack { id, name: name.to_string() });
}
pub fn open_rename_clip(&mut self, id: ClipId, name: &str) {
self.open = Some(Dialog::RenameClip { id, name: name.to_string() });
self.set(Dialog::RenameClip { id, name: name.to_string() });
}
pub fn open_rename_marker(&mut self, id: soundcraft_model::MarkerId, name: &str) {
self.open = Some(Dialog::RenameMarker { id, name: name.to_string() });
self.set(Dialog::RenameMarker { id, name: name.to_string() });
}
/// Groups list › Modify Group…: the creation dialog, pre-filled from an existing group. False
@@ -139,24 +144,31 @@ impl Dialogs {
let s = e.session();
let Some(g) = s.group(id) else { return false };
let members = s.tracks.iter().map(|t| (t.id.0, t.name.clone(), g.members.contains(&t.id))).collect();
self.open = Some(Dialog::Group { id: Some(id), name: g.name.clone(), edit: g.edit, mix: g.mix, members });
self.set(Dialog::Group { id: Some(id), name: g.name.clone(), edit: g.edit, mix: g.mix, members });
true
}
/// Clip List › Clear: confirm before clips and files leave the session. False when there is nothing to clear.
pub fn open_clear(&mut self, e: &Engine, clips: Vec<ClipId>, sources: Vec<SourceId>) -> bool {
let Some(message) = clear_message(e.session(), &clips, &sources) else { return false };
self.open = Some(Dialog::ClearClips { clips, sources, message });
self.set(Dialog::ClearClips { clips, sources, message });
true
}
/// Opens `d` in place of any open dialog. Its first frame clears the keyboard focus and ignores Enter, as for a
/// dialog opened when none was open.
pub(crate) fn set(&mut self, d: Dialog) {
self.open = Some(d);
self.shown = false;
}
/// Clip List › Auto Rename: the clips' naming scheme, filled in from the first clip's. False
/// when none of the clips is in the session.
pub fn open_auto_rename(&mut self, e: &Engine, clips: Vec<ClipId>) -> bool {
let s = e.session();
let Some((_, first)) = clips.iter().find_map(|id| s.find_clip(*id)) else { return false };
let names = first.auto_name.and_then(|k| s.auto_names.get(&k)).map_or_else(StripNames::default, StripNames::from);
self.open = Some(Dialog::AutoRename { clips, names });
self.set(Dialog::AutoRename { clips, names });
true
}
@@ -165,7 +177,7 @@ impl Dialogs {
self.after_save = None;
let home = default_dir();
let name = e.session().name.clone();
self.open = Some(match id {
let d = match id {
"clip.clear" => {
let ed = &e.session().edit;
return self.open_clear(e, ed.selected_clips.clone(), ed.selected_sources.clone());
@@ -246,7 +258,8 @@ impl Dialogs {
Some(d) => d,
None => return false,
},
});
};
self.set(d);
true
}
}
@@ -286,6 +299,15 @@ fn clear_message(s: &Session, clips: &[ClipId], sources: &[SourceId]) -> Option<
Some(text)
}
/// The file a bounce in `format` writes for the path in the form: the path itself when its
/// extension already names the format (`Mix.aifc` and `Mix.AIFF` stay as picked, so the file
/// the save dialog confirmed is the one written), otherwise the path with the format's extension.
pub fn bounce_path(path: &str, format: EncodeFormat) -> std::path::PathBuf {
let p = std::path::Path::new(path);
let named = p.extension().and_then(std::ffi::OsStr::to_str).and_then(craft_audio::encode_format_for);
if named == Some(format) { p.to_path_buf() } else { p.with_extension(format.extension()) }
}
/// The Bounce form's file row: a path field and a Browse button that also picks the format.
fn bounce_file_row(app: &mut SoundApp, ui: &mut egui::Ui, path: &mut String, format: &mut EncodeFormat) {
ui.horizontal(|ui| {
@@ -312,7 +334,25 @@ fn bounce_file_row(app: &mut SoundApp, ui: &mut egui::Ui, path: &mut String, for
fn path_prompt_form(app: &mut SoundApp, ui: &mut egui::Ui, cmd: &str, path: &mut String, key: &str, enter: bool) -> Option<(String, Value)> {
crate::widgets::named_text_field(ui.add(egui::TextEdit::singleline(path).desired_width(420.0)), "Path");
let mut picked = None;
// Import Audio picks several files at once; every other open dialog takes one.
let multi = cmd == "file.import_audio" && app.services.pick_open_many.is_some();
if multi
&& let Some(pick) = &app.services.pick_open_many
&& ui.button("Browse…").clicked()
{
let mut files = pick(cmd);
if files.len() > 1 {
let texts: Result<Vec<&str>, _> = files.iter().map(|f| unicode(f)).collect();
match texts {
Ok(texts) => return Some((cmd.to_string(), json!({"paths": texts}))),
Err(e) => app.ui.status = e.to_string(),
}
} else {
picked = files.pop();
}
}
if let Some(pick) = &app.services.pick_open
&& !multi
&& cmd != "session.save_as"
&& cmd != "session.save_copy"
&& key != "dir"
@@ -367,10 +407,10 @@ fn strip_silence_form(
let mut rename_from = None;
let [threshold, min_ms, pre_ms, post_ms] = values;
let before = [*threshold, *min_ms, *pre_ms, *post_ms];
ui.add(egui::Slider::new(threshold, -96.0..=0.0).text("Threshold dB"));
ui.add(egui::Slider::new(min_ms, 0.0..=4000.0).text("Min strip duration ms"));
ui.add(egui::Slider::new(pre_ms, 0.0..=500.0).text("Clip start pad ms"));
ui.add(egui::Slider::new(post_ms, 0.0..=2000.0).text("Clip end pad ms"));
crate::widgets::fine_slider(ui, threshold, -96.0..=0.0, "Threshold dB");
crate::widgets::fine_slider(ui, min_ms, 0.0..=4000.0, "Min strip duration ms");
crate::widgets::fine_slider(ui, pre_ms, 0.0..=500.0, "Clip start pad ms");
crate::widgets::fine_slider(ui, post_ms, 0.0..=2000.0, "Clip end pad ms");
let values = [*threshold, *min_ms, *pre_ms, *post_ms];
// The Edit window is drawn before this window, so it draws the rectangles for new values in the next frame.
if values != before {
@@ -416,7 +456,16 @@ pub fn show(app: &mut SoundApp, ctx: &egui::Context) {
if !matches!(app.dialogs.open, Some(Dialog::StripSilence { .. } | Dialog::StripSilenceRename { .. })) {
app.dialogs.strip_rectangles = StripRectangles::default();
}
let Some(mut d) = app.dialogs.open.take() else { return };
let Some(mut d) = app.dialogs.open.take() else {
app.dialogs.shown = false;
return;
};
// On the first frame a dialog is drawn, clear the keyboard focus a widget outside it may still hold, and ignore Enter:
// an Enter in that frame is the one that opened the dialog from a menu item or the command palette.
let first_frame = !std::mem::replace(&mut app.dialogs.shown, true);
if first_frame {
ctx.memory_mut(|m| m.stop_text_input());
}
let mut keep = true;
let mut action: Option<(String, Value)> = None;
let mut bounce_params: Option<Value> = None;
@@ -449,7 +498,11 @@ pub fn show(app: &mut SoundApp, ctx: &egui::Context) {
Dialog::SaveChanges { .. } => "Unsaved Changes",
}
.to_string();
let enter = ctx.input(|i| i.key_pressed(egui::Key::Enter));
// egui treats Enter on a focused button, checkbox, radio button or combo box as a click, so Enter presses the default
// button only when no widget has the keyboard focus or a text field has it. A held Enter's repeats do not press it.
let fresh_enter =
|i: &egui::InputState| i.events.iter().any(|e| matches!(e, egui::Event::Key { key: egui::Key::Enter, pressed: true, repeat: false, .. }));
let enter = !first_frame && ctx.input(fresh_enter) && (ctx.memory(|m| m.focused().is_none()) || ctx.text_edit_focused());
let esc = ctx.input(|i| i.key_pressed(egui::Key::Escape));
egui::Window::new(egui::RichText::new(&title).font(bold(13.0)))
.collapsible(false)
@@ -511,7 +564,7 @@ pub fn show(app: &mut SoundApp, ctx: &egui::Context) {
.color(Tokens::current().text_dim),
);
if buttons(ui, "Bounce", enter) {
let p = std::path::Path::new(path.as_str()).with_extension(format.extension());
let p = bounce_path(path, *format);
bounce_params = Some(
json!({"path": p.to_string_lossy(), "format": format.extension(), "bit_depth": bit_depth, "normalize": normalize, "dither": *dither && bit_depth.as_str() != "32f"}),
);
+15 -4
View File
@@ -18,21 +18,32 @@ pub(super) fn audiosuite_window(app: &mut SoundApp, ctx: &egui::Context) {
if let Some(info) = soundcraft_dsp::plugin_info(&process) {
for p in info.params {
let v = params.entry(p.id.to_string()).or_insert(p.default);
ui.add(egui::Slider::new(v, p.min..=p.max).text(p.name));
let mut wide = f64::from(*v);
crate::widgets::FineSlider::new(&mut wide, f64::from(p.min)..=f64::from(p.max))
.text(p.name)
.default_value(f64::from(p.default))
.show(ui);
*v = wide as f32;
}
} else {
match process.as_str() {
"normalize" => {
let v = params.entry("target_db".into()).or_insert(-0.1);
ui.add(egui::Slider::new(v, -24.0..=0.0).text("Peak dB"));
let mut wide = f64::from(*v);
crate::widgets::FineSlider::new(&mut wide, -24.0..=0.0).text("Peak dB").default_value(-0.1).show(ui);
*v = wide as f32;
}
"time_stretch" => {
let v = params.entry("ratio".into()).or_insert(1.0);
ui.add(egui::Slider::new(v, 0.25..=4.0).text("Length ratio"));
let mut wide = f64::from(*v);
crate::widgets::FineSlider::new(&mut wide, 0.25..=4.0).text("Length ratio").default_value(1.0).show(ui);
*v = wide as f32;
}
"pitch_shift" => {
let v = params.entry("semitones".into()).or_insert(0.0);
ui.add(egui::Slider::new(v, -24.0..=24.0).text("Semitones"));
let mut wide = f64::from(*v);
crate::widgets::FineSlider::new(&mut wide, -24.0..=24.0).text("Semitones").default_value(0.0).show(ui);
*v = wide as f32;
}
"speed_ramp" => {
for (id, label, default, choices) in SPEED_RAMP_CHOICES {
+1 -1
View File
@@ -14,7 +14,7 @@ pub fn ask_to_save(app: &mut SoundApp, id: &str, params: &Value) -> bool {
if !app.engine.is_dirty() {
return false;
}
app.dialogs.open = Some(Dialog::SaveChanges { then: (id.into(), params.clone()) });
app.dialogs.set(Dialog::SaveChanges { then: (id.into(), params.clone()) });
true
}
+7 -1
View File
@@ -27,9 +27,13 @@ mod universe;
#[cfg(test)]
use automation::automation_id;
pub(crate) use clips::draw_clip_labels;
pub use clips::strip_silence_stroke;
#[cfg(test)]
use clips::{clip_name_height, fade_curve_points};
use clips::{
ClipCut, PILL_BG, PILL_HEIGHT, PILL_INSET, PILL_MIN_WIDTH, PILL_PAD, clip_cut, clip_cut_in, clip_cuts, clip_marks, fade_curve_points,
marks_pill_left, name_pill_width,
};
use drag::drive_drag;
#[cfg(test)]
use drag::fade_drag_range;
@@ -43,6 +47,8 @@ use layout::COLUMN_TITLES_H;
use layout::lane_rect;
pub use layout::{COLUMN_W, EditLayout, HEADER_W, LANE_H, RULER_H, edit_columns, header_width, rect, sample_at, track_height, x_of};
use overlay::overlay;
#[cfg(test)]
use overlay::playhead_colour;
pub use rulers::ruler_height;
use rulers::{column_titles, draw_rulers, views_button};
pub use scroll::zoom_toggle;
+196 -35
View File
@@ -2,9 +2,10 @@ use super::drag::fade_drag_preview;
use super::input::{clip_gain_icon, near_fade_midpoint};
use super::layout::{sample_at, x_of};
use super::{
Align2, AutoParam, Clip, ClipContent, Color32, CornerRadius, Gesture, GridValue, NoteValue, Pos2, Range, Rect, Session, Shape, SoundApp, Stroke,
StrokeKind, TimeFormat, Tokens, Tool, Track, bold, clip_colors, format_position, pos2, regular, vec2,
Align2, AutoParam, Clip, ClipContent, Color32, CornerRadius, Gesture, GridValue, NoteValue, Pos2, Range, Rect, Samples, Session, Shape, SoundApp,
Stroke, StrokeKind, TimeFormat, Tokens, Tool, Track, bold, clip_colors, format_position, pos2, regular, vec2,
};
use std::sync::Arc;
/// The outline of the Strip Silence rectangles.
pub fn strip_silence_stroke() -> Stroke {
@@ -66,8 +67,9 @@ impl GridLines {
}
}
pub(super) fn clip_name_height(s: &Session, lane: Rect) -> f32 {
if lane.height() >= 40.0 && s.edit.flag("view.clip.name") { 13.0 } else { 0.0 }
/// Whether clips show their name (View › Clip › Name) in a lane tall enough to carry it.
pub(super) fn clip_name_shown(s: &Session, lane: Rect) -> bool {
lane.height() >= 40.0 && s.edit.flag("view.clip.name")
}
pub(super) fn fade_curve_points(fade: soundcraft_model::Fade, fade_in: bool, a: f32, b: f32, body: Rect, pixels_per_point: f32) -> Vec<Pos2> {
@@ -177,7 +179,6 @@ struct ClipGeom<'a> {
vis: Rect,
x0: f32,
x1: f32,
name_h: f32,
body: Color32,
wave: Color32,
selected: bool,
@@ -185,48 +186,44 @@ struct ClipGeom<'a> {
fade_in: soundcraft_model::Fade,
fade_out: soundcraft_model::Fade,
prev: Option<&'a Clip>,
/// How the clip meets its neighbours in its playlist (see [`clip_cuts`]).
cut: ClipCut,
}
pub(super) fn draw_clip(cl: &ClipLane<'_>, clip: &Clip, dim: bool) {
/// `cut`: how the clip meets its neighbours in its playlist (see [`clip_cuts`]).
pub(super) fn draw_clip(cl: &ClipLane<'_>, clip: &Clip, cut: ClipCut, dim: bool) {
let ClipLane { app, painter, s, track, tl, lane, .. } = *cl;
let x0 = x_of(s, tl, clip.start);
let x1 = x_of(s, tl, clip.end()).max(x0 + 1.0);
let r = Rect::from_min_max(pos2(x0, lane.min.y + 1.0), pos2(x1, lane.max.y - 1.0));
let r = clip_rect(s, tl, lane, clip);
let (x0, x1) = (r.min.x, r.max.x);
let selected = s.edit.selected_clips.contains(&clip.id)
|| (s.edit.selected_tracks.contains(&track.id)
&& !s.edit.selection.is_empty()
&& s.edit.selection.start <= clip.start
&& s.edit.selection.end >= clip.end());
let color = clip.color.unwrap_or(track.color);
let (mut body, bar, mut wave) = clip_colors(color, selected);
let (mut body, _, mut wave) = clip_colors(color, selected);
if clip.muted || dim {
body = body.gamma_multiply(0.45);
wave = Color32::from_rgb(70, 70, 70);
}
let body = if s.edit.flag("view.clip.transparency") { body.gamma_multiply(0.55) } else { body };
painter.rect(r, CornerRadius::same(2), body, Stroke::new(1.0, Color32::from_rgb(10, 10, 10)), StrokeKind::Inside);
let name_h = clip_name_height(s, lane);
if name_h > 0.0 {
let nb = Rect::from_min_max(r.min, pos2(r.max.x, r.min.y + name_h));
painter.rect_filled(nb, CornerRadius { nw: 2, ne: 2, sw: 0, se: 0 }, if clip.muted { Color32::from_rgb(70, 70, 70) } else { bar });
let name_clip = nb.intersect(lane);
// Narrower than this, no glyph would show: the name starts 4 points in.
if name_clip.width() > 6.0 {
painter.with_clip_rect(name_clip).text(
pos2(nb.min.x.max(lane.min.x) + 4.0, nb.center().y),
Align2::LEFT_CENTER,
&clip.name,
bold(10.5),
Color32::from_rgb(12, 12, 12),
);
}
let corners = cut.corners();
let outline = Stroke::new(1.0, CLIP_OUTLINE);
if cut.right {
// The cut's single line is drawn by the next clip: stroke past our right edge and clip it away.
painter.rect_filled(r, corners, body);
let past = Rect::from_min_max(r.min, pos2(r.max.x + 2.0, r.max.y));
painter.with_clip_rect(painter.clip_rect().intersect(r)).rect_stroke(past, corners, outline, StrokeKind::Inside);
} else {
painter.rect(r, corners, body, outline, StrokeKind::Inside);
}
let body_r = Rect::from_min_max(pos2(r.min.x, r.min.y + name_h), r.max);
// The body is the whole clip: the name is a small pill drawn over it (see `draw_clip_pills`).
let body_r = r;
let vis = body_r.intersect(lane);
// The waveform and fade curve share the same temporary envelope every frame.
let (fade_in, fade_out) = clip_preview_fades(clip, app.gesture.as_ref().filter(|_| !dim));
let prev = crossfade_partner(track, clip);
let g = ClipGeom { r, body_r, vis, x0, x1, name_h, body, wave, selected, dim, fade_in, fade_out, prev };
let g = ClipGeom { r, body_r, vis, x0, x1, body, wave, selected, dim, fade_in, fade_out, prev, cut };
draw_clip_content(cl, clip, &g);
draw_crossfade_tail(cl, &g);
draw_gain_line(cl, clip, &g);
@@ -383,12 +380,9 @@ fn draw_fades(cl: &ClipLane<'_>, clip: &Clip, g: &ClipGeom<'_>) {
/// Optional clip overlays (View › Clip).
fn draw_clip_overlays(cl: &ClipLane<'_>, clip: &Clip, g: &ClipGeom<'_>) {
let ClipLane { painter, s, track, sorted, tl, lane, .. } = *cl;
let ClipGeom { r, name_h, .. } = *g;
let ClipGeom { r, cut, .. } = *g;
// Optional clip overlays (View › Clip).
let ov = Color32::from_rgb(16, 16, 16);
if s.edit.flag("view.clip.rating") && clip.rating > 0 && name_h > 0.0 {
painter.text(pos2(r.max.x - 18.0, r.min.y + 7.0), Align2::RIGHT_CENTER, "★".repeat(usize::from(clip.rating.min(5))), regular(9.0), ov);
}
if s.edit.flag("view.clip.sync_point") && clip.sync_point > 0 {
let x = x_of(s, tl, clip.start + clip.sync_point);
painter.add(Shape::convex_polygon(
@@ -396,7 +390,7 @@ fn draw_clip_overlays(cl: &ClipLane<'_>, clip: &Clip, g: &ClipGeom<'_>) {
Color32::WHITE,
Stroke::NONE,
));
painter.line_segment([pos2(x, r.min.y + name_h), pos2(x, r.max.y)], Stroke::new(1.0, Color32::from_rgba_unmultiplied(255, 255, 255, 90)));
painter.line_segment([pos2(x, r.min.y), pos2(x, r.max.y)], Stroke::new(1.0, Color32::from_rgba_unmultiplied(255, 255, 255, 90)));
}
let time_text = clip_time_text(s, clip);
if let Some(tt) = time_text
@@ -425,8 +419,13 @@ fn draw_clip_overlays(cl: &ClipLane<'_>, clip: &Clip, g: &ClipGeom<'_>) {
}
}
}
if clip.edit_locked || clip.time_locked {
painter.text(pos2(r.max.x - 10.0, r.min.y + 7.0), Align2::CENTER_CENTER, "🔒", regular(9.0), Color32::WHITE);
// Video clips get their pills from `video_track::draw_lane`, after their thumbnails.
if !clip.is_video() {
draw_clip_labels(painter, s, tl, lane, clip);
}
if cut.left {
// The cut's one line, on top of the waveform so it runs the full height.
painter.rect_filled(Rect::from_min_size(r.min, vec2(1.0, r.height())), 0.0, CLIP_OUTLINE);
}
}
@@ -451,6 +450,168 @@ fn clip_time_text(s: &Session, clip: &Clip) -> Option<String> {
}
}
/// Where a clip sits in its lane: one point in from the lane's top and bottom, at least one point wide.
pub(super) fn clip_rect(s: &Session, tl: Rect, lane: Rect, clip: &Clip) -> Rect {
let x0 = x_of(s, tl, clip.start);
let x1 = x_of(s, tl, clip.end()).max(x0 + 1.0);
Rect::from_min_max(pos2(x0, lane.min.y + 1.0), pos2(x1, lane.max.y - 1.0))
}
/// The pills of `clip` (name, marks). Drawn right after the clip itself, so clips and overlays drawn later cover them.
pub(crate) fn draw_clip_labels(painter: &egui::Painter, s: &Session, tl: Rect, lane: Rect, clip: &Clip) {
let r = clip_rect(s, tl, lane, clip);
if r.max.x < lane.min.x || r.min.x > lane.max.x {
return;
}
let show_name = clip_name_shown(s, lane);
let marks = clip_marks(clip, s.edit.flag("view.clip.rating"));
draw_clip_pills(painter, lane, r, show_name.then_some(clip.name.as_str()), &marks);
}
/// The marks a clip carries at its top-right: its star rating and a lock when edit- or time-locked.
pub(super) fn clip_marks(clip: &Clip, show_rating: bool) -> String {
let mut marks = if show_rating && clip.rating > 0 { "★".repeat(usize::from(clip.rating.min(5))) } else { String::new() };
if clip.edit_locked || clip.time_locked {
if !marks.is_empty() {
marks.push(' ');
}
marks.push('🔒');
}
marks
}
/// Gap between a pill and the clip outline's inner edge (so the pill sits 1 pt inside the outline).
pub(super) const PILL_INSET: f32 = 2.0;
pub(super) const PILL_HEIGHT: f32 = 14.0;
/// Horizontal padding between a pill's edge and its text.
pub(super) const PILL_PAD: f32 = 3.0;
/// The narrowest name pill worth drawing: its padding plus about one letter.
pub(super) const PILL_MIN_WIDTH: f32 = 12.0;
/// Half-transparent black behind white text, readable on the light and the dark clip bodies alike.
pub(super) const PILL_BG: Color32 = Color32::from_black_alpha(125);
/// Width of the name pill: the text plus its padding, capped to the `room` the clip leaves it (the text is then
/// cut cleanly at the pill's edge, with no ellipsis). `None` when there is no room for anything useful.
pub(super) fn name_pill_width(room: f32, text_w: f32) -> Option<f32> {
if room.is_nan() || room < PILL_MIN_WIDTH {
return None;
}
Some((text_w.max(0.0) + 2.0 * PILL_PAD).min(room))
}
/// Left edge of the marks pill, which sits at the clip's top-right: `None` when the clip is too narrow to hold it
/// (it would veil the clip), where `left` is the leftmost x a pill may use.
pub(super) fn marks_pill_left(left: f32, clip_right: f32, w: f32) -> Option<f32> {
let x = clip_right - PILL_INSET - w;
(x >= left).then_some(x)
}
/// The clip's name as a small rounded pill at its top-left, and its marks (rating, lock) as a matching pill at the
/// top-right. The name keeps clear of the marks and floats to the left edge of the lane when the clip starts
/// off-screen, so it stays readable while scrolling.
fn draw_clip_pills(painter: &egui::Painter, lane: Rect, r: Rect, name: Option<&str>, marks: &str) {
let p = painter.with_clip_rect(painter.clip_rect().intersect(r));
let pill = |rect: Rect, galley: Arc<egui::Galley>| {
p.rect_filled(rect, CornerRadius::same(3), PILL_BG);
let at = pos2(rect.min.x + PILL_PAD, rect.center().y - galley.size().y * 0.5);
p.with_clip_rect(p.clip_rect().intersect(rect)).galley(at, galley, Color32::WHITE);
};
let top = r.min.y + PILL_INSET;
let left = r.min.x.max(lane.min.x) + PILL_INSET;
let mut right = r.max.x.min(lane.max.x) - PILL_INSET;
if !marks.is_empty() {
let galley = p.layout_no_wrap(marks.to_owned(), regular(9.0), Color32::WHITE);
let w = galley.size().x + 2.0 * PILL_PAD;
if let Some(x) = marks_pill_left(left, r.max.x, w) {
right = right.min(x - PILL_INSET);
pill(Rect::from_min_size(pos2(x, top), vec2(w, PILL_HEIGHT)), galley);
}
}
// No layout where no pill fits: zoomed out on a large session, most clips are too narrow.
if let Some(name) = name.filter(|n| !n.is_empty() && right - left >= PILL_MIN_WIDTH) {
let galley = p.layout_no_wrap(name.to_owned(), bold(10.0), Color32::WHITE);
if let Some(w) = name_pill_width(right - left, galley.size().x) {
pill(Rect::from_min_size(pos2(left, top), vec2(w, PILL_HEIGHT)), galley);
}
}
}
/// The 1 pt outline around a clip.
const CLIP_OUTLINE: Color32 = Color32::from_rgb(10, 10, 10);
/// How a clip meets its neighbours in the playlist. Two clips butted at a cut share ONE 1 pt line, as in
/// the incumbent: the later clip draws it (its left edge), the earlier one drops its right edge, and both
/// sides of the cut have square corners. A gap or an overlap leaves the clip drawn on its own.
/// Selection only recolours the body, so it never changes which edges are drawn.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub(super) struct ClipCut {
/// Starts exactly where the clip before it ends.
pub(super) left: bool,
/// Ends exactly where the clip after it starts.
pub(super) right: bool,
}
impl ClipCut {
/// The body's corners: square on a side that meets another clip, rounded otherwise.
pub(super) fn corners(self) -> CornerRadius {
let (nw, ne) = (if self.left { 0 } else { 2 }, if self.right { 0 } else { 2 });
CornerRadius { nw, ne, sw: nw, se: ne }
}
}
/// The cut of the clip spanning `start..end`, given the `(start, end)` of every other clip of its playlist, in any
/// order (a playlist's order is its stacking order, not its time order). A side is butted when another clip ends
/// (or starts) exactly there and no third clip covers that point.
pub(super) fn clip_cut(start: Samples, end: Samples, others: impl IntoIterator<Item = (Samples, Samples)>) -> ClipCut {
// An empty clip has no span to share a line with.
if end <= start {
return ClipCut::default();
}
let (mut left, mut right, mut left_covered, mut right_covered) = (false, false, false, false);
for (s, e) in others.into_iter().filter(|(s, e)| e > s) {
left |= e == start;
right |= s == end;
left_covered |= s < start && start < e;
right_covered |= s < end && end < e;
}
ClipCut { left: left && !left_covered, right: right && !right_covered }
}
/// [`clip_cut`] for `clip` among its playlist's `clips`, scanning them all.
pub(super) fn clip_cut_in(clips: &[Clip], clip: &Clip) -> ClipCut {
clip_cut(clip.start, clip.end(), clips.iter().filter(|c| c.id != clip.id).map(|c| (c.start, c.end())))
}
/// [`clip_cut`] for each clip of a playlist. Sorted by start (the usual case), one pass over the playlist and two
/// binary searches per clip, so large sessions do no per-clip scan every frame; unsorted, a scan per clip.
pub(super) fn clip_cuts(clips: &[Clip]) -> impl Fn(&Clip) -> ClipCut + '_ {
// reach[k]: the furthest end of the first k clips (empty clips reach nothing).
let reach: Option<Vec<Samples>> = clips.is_sorted_by_key(|c| c.start).then(|| {
std::iter::once(Samples::MIN)
.chain(clips.iter().scan(Samples::MIN, |far, c| {
if c.end() > c.start {
*far = (*far).max(c.end());
}
Some(*far)
}))
.collect()
});
move |clip| {
let Some(reach) = &reach else { return clip_cut_in(clips, clip) };
let (start, end) = (clip.start, clip.end());
if end <= start {
return ClipCut::default();
}
let reached = |k: usize| reach.get(k).copied().unwrap_or(Samples::MIN);
// Butted and uncovered on the left: the clips starting before it reach exactly its start. On the right: the
// clips starting before its end (itself included) reach exactly its end, and a clip starts there.
let before = clips.partition_point(|c| c.start < start);
let upto = clips.partition_point(|c| c.start < end);
let next = clips.get(upto..).unwrap_or_default().iter().take_while(|c| c.start == end).any(|c| c.end() > c.start);
ClipCut { left: reached(before) == start, right: next && reached(upto) == end }
}
}
/// The earlier clip of a crossfade pair: the one before `clip` in its playlist, overlapping its start.
fn crossfade_partner<'a>(track: &'a Track, clip: &Clip) -> Option<&'a Clip> {
let (p, i) = track.playlists.iter().find_map(|p| {
+1 -1
View File
@@ -315,7 +315,7 @@ fn trim_clip(app: &mut SoundApp, clip: ClipId, to: Samples, start: bool) {
pub(super) fn spot_or_run(app: &mut SoundApp, spot: Option<Spot>) {
let Some(spot) = spot else { return };
if app.engine.session().edit.edit_mode == EditMode::Spot {
app.dialogs.open = Some(Dialog::Spot(spot));
app.dialogs.set(Dialog::Spot(spot));
} else if let Some((id, params)) = spot.command(app.engine.session()) {
let _ = app.run(&id, params);
}
+136 -4
View File
@@ -1,8 +1,8 @@
use super::layout::STRIP_W;
use super::tracks::{click_track, folder_depth, height_targets, in_edit_window, track_context_menu, view_label, view_menu};
use super::{
Align2, AutoParam, Color32, Pos2, Rect, Sense, Shape, SoundApp, Stroke, Tokens, Track, TrackHeight, TrackId, TrackKind, Ui, bold, groove, json,
level_pos, meter_pos, pos2, raised_key, regular, rgb, shade, vec2,
Align2, AutoParam, Color32, Gesture, Pos2, Rect, Sense, Shape, SoundApp, Stroke, Tokens, Track, TrackHeight, TrackId, TrackKind, Ui, bold,
groove, json, level_pos, meter_pos, pos2, raised_key, regular, rgb, shade, vec2,
};
/// Draw one track header: resize edge, selection and context menu, folder/options control, automation-lane toggle,
@@ -550,7 +550,7 @@ fn io_column(app: &mut SoundApp, ui: &mut Ui, track: &Track, r: Rect) {
ui.painter().rect_filled(r, 0.0, t.slot_well);
let ui = column_ui(ui, "io", r);
let row = |y: f32, h: f32| (r.height() >= y + h + 1.0).then_some(Rect::from_min_size(r.min + vec2(7.0, y), vec2(r.width() - 14.0, h)));
if let Some(input) = row(4.0, 15.0) {
if let Some(input) = row(4.0, 15.0).filter(|_| crate::mix_window::has_input_selector(track.kind)) {
let input_name = crate::mix_window::input_name(app, track);
let resp = header_selector(&ui, input, ui.scope_id().with(("in", id.0)), &input_name, t.text, true, &format!("Input for {}", track.name));
crate::mix_window::route_popup(app, &resp.on_hover_text("Input"), id, true);
@@ -584,11 +584,143 @@ fn io_column(app: &mut SoundApp, ui: &mut Ui, track: &Track, r: Rect) {
let pan = m.pan.first().map_or("-".into(), |p| crate::widgets::pan_text(*p));
// The readout is black in both themes, so it takes the dark palette's green.
let ink = Tokens::DARK.counter_text;
for (y, label, value) in [(7.5, "vol", crate::widgets::db_text(m.volume_db)), (22.0, "pan", pan)] {
// The two rows are buttons: a click opens a fader or pan sliders, an Alt-click resets.
// The Mix window's strip has no stereo pan for a VCA or for a track on a multichannel output (it shows a surround panner), so the pan stays text.
// ponytail: surround-panned tracks are placed in the Mix window. Upgrade: `widgets::surround_panner` in the pan popup.
let pannable = !m.pan.is_empty() && track.kind != TrackKind::Vca && !crate::mix_window::surround_panned(app, track);
let vol_row = Rect::from_x_y_ranges(vr.x_range(), vr.min.y + 1.5..=vr.min.y + 14.0).shrink2(vec2(1.5, 0.0));
let pan_row = Rect::from_x_y_ranges(vr.x_range(), vr.min.y + 15.0..=vr.max.y - 1.5).shrink2(vec2(1.5, 0.0));
let vol_text = crate::widgets::db_text(m.volume_db);
let vol = readout_button(
&ui,
vol_row,
ui.scope_id().with(("vol", id.0)),
&format!("{} volume", track.name),
&vol_text,
"Volume: click for a fader, Alt-click for 0 dB",
);
let pan_resp = pannable.then(|| {
readout_button(
&ui,
pan_row,
ui.scope_id().with(("pan", id.0)),
&format!("{} pan", track.name),
&pan,
"Pan: click for sliders, Alt-click to reset",
)
});
let alt = ui.input(|i| i.modifiers.alt);
if vol.clicked() && alt && m.volume_db.abs() > f32::EPSILON {
// Merged, so a reset during an automation pass joins the pass's undo step; the merge ends by itself on the next frame.
let _ = app.engine.execute_merged("mix.volume", &json!({"track": id.0, "db": 0.0}), &format!("fader:{}", id.0));
}
if let Some(resp) = &pan_resp
&& resp.clicked()
&& alt
{
reset_pan(app, track);
}
for (y, label, value) in [(7.5, "vol", vol_text), (22.0, "pan", pan)] {
ui.painter().text(pos2(vr.min.x + 7.0, vr.min.y + y), Align2::LEFT_CENTER, label, bold(10.0), ink);
ui.painter().text(pos2(vr.max.x - 6.5, vr.min.y + y), Align2::RIGHT_CENTER, value, bold(10.0), ink);
}
ui.painter().hline(vr.min.x + 7.0..=vr.max.x - 6.0, vr.min.y + 14.5, Stroke::new(1.0, Color32::from_gray(35)));
readout_popup(&vol, !alt).show(|ui| volume_fader(app, ui, track));
if let Some(resp) = &pan_resp {
readout_popup(resp, !alt).show(|ui| pan_sliders(app, ui, track));
}
}
}
/// A readout row of the I/O column as a button, named `name` (the track's, so two tracks showing the same value differ) and its value. It lights up under the pointer, with the keyboard focus or while its popup is open,
/// and paints nothing otherwise, so an untouched column looks the same as before.
fn readout_button(ui: &Ui, r: Rect, id: egui::Id, name: &str, value: &str, tip: &str) -> egui::Response {
let resp = ui.interact(r, id, Sense::click()).on_hover_text(tip).on_hover_cursor(egui::CursorIcon::PointingHand);
resp.widget_info(|| egui::WidgetInfo::labeled(egui::Role::Button, true, format!("{name} {value}")));
if resp.hovered() || resp.has_focus() || egui::Popup::is_id_open(ui.ctx(), egui::Popup::default_response_id(&resp)) {
ui.painter().rect_filled(r, 1.5, Color32::from_gray(38));
}
resp
}
/// The popup of a readout button, to its right so it doesn't hide the tracks below. An Alt-click
/// resets instead of opening (`toggle` false). Clicks inside it keep it open; Escape or a click outside closes it.
fn readout_popup(resp: &egui::Response, toggle: bool) -> egui::Popup<'static> {
egui::Popup::from_response(resp)
.open_memory((toggle && resp.clicked()).then_some(egui::SetOpenCommand::Toggle))
.close_behavior(egui::PopupCloseBehavior::CloseOnClickOutside)
.align(egui::RectAlign::RIGHT_START)
.gap(2.0)
}
/// What a panner returns to: centre on a mono track, hard left or right on a stereo one, as on a new track.
fn default_pan(track: &Track, index: usize) -> f32 {
soundcraft_model::Mixer::new(track.format).pan.get(index).copied().unwrap_or(0.0)
}
/// Put every panner of the track (L and R on a stereo one) back to its default, as one undo step. The merge ends by itself on the next frame.
fn reset_pan(app: &mut SoundApp, track: &Track) {
let key = format!("pan-reset:{}", track.id.0);
for (i, now) in track.mixer.pan.iter().enumerate().take(2) {
let to = default_pan(track, i);
// Only a real change may start the merged step: a no-op first call would leave the next one folded into an older edit.
if (now - to).abs() > f32::EPSILON {
let _ = app.engine.execute_merged("mix.pan", &json!({"track": track.id.0, "pan": to, "index": i}), &key);
}
}
}
/// The volume popup: a fader like the Mix window's, over a readout that takes a typed value.
/// A drag is one undo step and goes through [`Gesture::Fader`], so Touch and Latch automation write it.
fn volume_fader(app: &mut SoundApp, ui: &mut Ui, track: &Track) {
let t = Tokens::current();
let id = track.id;
let db = track.mixer.volume_db;
let key = format!("fader:{}", id.0);
let (well, _) = ui.allocate_exact_size(vec2(56.0, 16.0), Sense::hover());
crate::widgets::inset(ui.painter(), well, t.counter_bg);
let readout_id = ui.scope_id().with(("io-vol", id.0));
let (_, typed) = crate::mix_window::editable_readout(
ui,
well,
readout_id,
crate::mix_window::Readout {
name: &format!("{} volume", track.name),
display: &crate::widgets::db_text(db),
edit_initial: &format!("{db:.1}"),
parse: crate::mix_window::parse_db,
reset: 0.0,
},
);
if let Some(db) = typed {
let _ = app.engine.execute_merged("mix.volume", &json!({"track": id.0, "db": db}), &key);
}
ui.add_space(4.0);
// ponytail: the fader is drawn, not focusable, so the keyboard sets the volume in the readout above it. Upgrade: arrow keys in `widgets::fader`.
let (slot, _) = ui.allocate_exact_size(vec2(56.0, 170.0), Sense::hover());
if let Some(moved) = crate::widgets::fader(ui, slot, db, ui.scope_id().with(("io-fader", id.0)), &format!("{} level", track.name)) {
app.gesture = Some(Gesture::Fader { track: id, start_db: db });
let _ = app.engine.execute_merged("mix.volume", &json!({"track": id.0, "db": moved}), &key);
}
}
/// The pan popup: one slider, or L and R on a stereo track. A drag is one undo step per panner.
fn pan_sliders(app: &mut SoundApp, ui: &mut Ui, track: &Track) {
ui.spacing_mut().slider_width = 120.0;
let stereo = track.mixer.pan.len() > 1;
for (i, side) in ["L", "R"].into_iter().enumerate().take(track.mixer.pan.len()) {
let before = track.mixer.pan.get(i).copied().unwrap_or(0.0);
let mut pan = before;
// ponytail: no double-click reset on the sliders; the readout's Alt-click resets every panner at once.
// No step either: a slider rounds to it when drawn, which would rewrite a pan off the grid just by opening the popup.
let slider = egui::Slider::new(&mut pan, -1.0..=1.0)
.text(if stereo { side } else { "Pan" })
.custom_formatter(|v, _| crate::widgets::pan_text(v as f32))
.custom_parser(|s| crate::mix_window::parse_pan(s).map(f64::from));
ui.add(slider);
if (pan - before).abs() > f32::EPSILON {
let _ = app.engine.execute_merged("mix.pan", &json!({"track": track.id.0, "pan": pan, "index": i}), &format!("pan:{}:{i}", track.id.0));
}
}
}
+12 -6
View File
@@ -1,5 +1,5 @@
use super::automation::{automation_id, breakpoint_at, plugin_range, shown_automation};
use super::clips::{clip_name_height, fade_control_position};
use super::clips::fade_control_position;
use super::drag::{drag_snap, fade_curve_drag_value, fade_drag_range, spot_or_run};
use super::layout::{sample_at, x_of};
use super::{
@@ -24,7 +24,7 @@ fn fade_handle_at(s: &Session, tl: Rect, lane: Rect, clip: &Clip, p: Pos2) -> Op
if clip.length <= 0 || clip.end() <= clip.start {
return None;
}
let y = lane.min.y + 1.0 + clip_name_height(s, lane);
let y = lane.min.y + 1.0;
[(true, clip.fade_in), (false, clip.fade_out)]
.into_iter()
.filter(|(_, fade)| fade.len > 0)
@@ -55,7 +55,7 @@ pub(super) fn fade_midpoint(s: &Session, tl: Rect, lane: Rect, clip: &Clip, fade
if b - a < 32.0 {
return None;
}
let body = Rect::from_min_max(pos2(a, lane.min.y + 1.0 + clip_name_height(s, lane)), pos2(b, lane.max.y - 1.0));
let body = Rect::from_min_max(pos2(a, lane.min.y + 1.0), pos2(b, lane.max.y - 1.0));
let p = fade_control_position(fade, a, b, body);
lane.contains(p).then_some(p)
}
@@ -277,7 +277,7 @@ fn start_gesture(app: &mut SoundApp, ui: &mut Ui, c: &LaneCtx<'_>, resp: &egui::
{
let fade = if fade_in { c.fade_in } else { c.fade_out };
let anchor = (origin.y, fade.gain(0.5));
let height = (lane.height() - 2.0 - clip_name_height(s, lane)).max(1.0);
let height = (lane.height() - 2.0).max(1.0);
app.gesture = Some(Gesture::FadeCurve { clip: c.id, fade_in, curvature: fade_curve_drag_value(fade, anchor, height, p.y), anchor, height });
ui.ctx().request_repaint();
} else if resp.drag_started()
@@ -356,14 +356,20 @@ fn lane_click(app: &mut SoundApp, ui: &mut Ui, c: &LaneCtx<'_>, resp: &egui::Res
ids.push(c.id.0);
let _ = app.run("edit.select", json!({"clips": ids}));
if s.edit.edit_mode == EditMode::Spot {
app.dialogs.open = Spot::clips(s, vec![c.id], None, 0).map(Dialog::Spot);
match Spot::clips(s, vec![c.id], None, 0) {
Some(spot) => app.dialogs.set(Dialog::Spot(spot)),
None => app.dialogs.open = None,
}
}
}
}
Tool::Trim if s.edit.edit_mode == EditMode::Spot && hit.is_some() => {
if let Some(c) = &hit {
let start = at - c.start < c.end() - at;
app.dialogs.open = Spot::trim(s, c.id, start, if start { c.start } else { c.end() }).map(Dialog::Spot);
match Spot::trim(s, c.id, start, if start { c.start } else { c.end() }) {
Some(spot) => app.dialogs.set(Dialog::Spot(spot)),
None => app.dialogs.open = None,
}
}
}
Tool::Zoom => {
+7 -1
View File
@@ -4,6 +4,12 @@ use super::layout::x_of;
use super::scroll::{FollowFrame, follow_update};
use super::{Color32, Gesture, Rect, Samples, Shape, SoundApp, Stroke, StrokeKind, Tokens, TrackId, Ui, json, ops, pos2, rgb};
/// The playback cursor is red only while recording; plain playback draws it in its own neutral
/// grey, apart from the insertion colour of the edit cursor that stands beside it.
pub(super) fn playhead_colour(t: &Tokens, recording: bool) -> Color32 {
if recording { t.playhead } else { t.playback }
}
/// Selection overlay, insertion point and playhead across rulers and tracks.
/// While playing, auto-scroll keeps the playhead in view for the follow
/// modes (`page`/`continuous`/`center`) — but any outside view move (a manual
@@ -61,7 +67,7 @@ pub(super) fn overlay(app: &mut SoundApp, ui: &mut Ui, tl: Rect, area: Rect) {
}
if app.is_playing() {
let x = x_of(s, tl, app.position());
painter.line_segment([pos2(x, area.min.y), pos2(x, area.max.y)], Stroke::new(1.5, t.playhead));
painter.line_segment([pos2(x, area.min.y), pos2(x, area.max.y)], Stroke::new(1.5, playhead_colour(&t, app.engine.transport.recording)));
// Session reads first (owned copies), then the layout mutation: the
// two borrow disjoint fields.
let pos = app.position();
+1 -1
View File
@@ -110,7 +110,7 @@ pub(super) fn draw_rulers(app: &mut SoundApp, ui: &mut Ui, area: Rect, tl: Rect,
{
let at = snap(&s, sample_at(&s, tl, p.x).max(0));
let bpm = s.tempo.tempo_at_tick(s.tempo.samples_to_ticks(at, sr));
app.dialogs.open = Some(crate::dialogs::Dialog::TempoChange { at, bpm });
app.dialogs.set(crate::dialogs::Dialog::TempoChange { at, bpm });
}
let mut last_label = f32::MIN;
for ev in s.tempo.tempos() {
+326 -1
View File
@@ -1043,7 +1043,7 @@ fn fade_midpoint_handles_remain_visible_and_can_be_grabbed_again() {
h.step();
assert_eq!(h.output().platform_output.cursor_icon, egui::CursorIcon::ResizeVertical);
start_drag(&mut h, control);
let to = control - vec2(0.0, (lane.height() - 2.0 - clip_name_height(&before, lane)) * 0.1);
let to = control - vec2(0.0, (lane.height() - 2.0) * 0.1);
h.hover_at(to);
h.step();
assert!(
@@ -1574,6 +1574,331 @@ fn alt_click_on_a_breakpoint_deletes_it() {
}
}
const ALONE: ClipCut = ClipCut { left: false, right: false };
#[test]
fn butted_pair_shares_one_seam() {
// 0..100 then 100..200: the first drops its right edge, the second draws the seam on its left.
assert_eq!(clip_cut(0, 100, [(100, 200)]), ClipCut { left: false, right: true });
assert_eq!(clip_cut(100, 200, [(0, 100)]), ClipCut { left: true, right: false });
}
#[test]
fn middle_clip_of_a_run_is_butted_on_both_sides() {
let cut = clip_cut(100, 200, [(0, 100), (200, 300)]);
assert_eq!(cut, ClipCut { left: true, right: true });
let c = cut.corners();
assert_eq!((c.nw, c.sw, c.ne, c.se), (0, 0, 0, 0));
}
#[test]
fn gap_overlap_and_lone_clips_keep_their_own_outline() {
// First and last clips: nothing on the open side.
assert_eq!(clip_cut(0, 100, []), ALONE);
// A gap of one sample is not a cut.
assert_eq!(clip_cut(101, 200, [(0, 100), (201, 300)]), ALONE);
// Overlaps (crossfades, layers) keep both outlines.
assert_eq!(clip_cut(90, 200, [(0, 100), (150, 300)]), ALONE);
assert_eq!(clip_cut(0, 100, [(60, 160)]), ALONE);
// A third clip lying across the cut hides it: no shared seam.
assert_eq!(clip_cut(100, 200, [(0, 100), (50, 150)]), ALONE);
// Empty clips share nothing, and are not neighbours either.
assert_eq!(clip_cut(100, 100, [(0, 100), (100, 200)]), ALONE);
assert_eq!(clip_cut(100, 200, [(100, 100)]), ALONE);
}
#[test]
fn sorted_playlists_find_the_same_cuts_without_a_scan() {
let layouts: [&[(Samples, Samples)]; 8] = [
&[(0, 100), (100, 100)],
&[(0, 100), (100, 100), (200, 100)],
&[(0, 100), (101, 99), (201, 99)],
&[(0, 100), (90, 110), (150, 150)],
&[(0, 100), (50, 100), (100, 100)],
&[(0, 100), (100, 0), (100, 100)],
&[(0, 1000), (100, 100), (200, 100)],
&[(0, 100), (100, 50), (100, 100), (200, 100)],
];
for spans in layouts {
let clips: Vec<Clip> = spans.iter().zip(1..).map(|(&(start, len), id)| test_clip(id, start, len)).collect();
let cut_of = clip_cuts(&clips);
for c in &clips {
assert_eq!(cut_of(c), clip_cut_in(&clips, c), "{spans:?}, clip at {}", c.start);
}
}
// Not vacuous: the middle clip of a run is butted on both sides.
let run: Vec<Clip> = (0..3).map(|i| test_clip(i + 1, i as Samples * 100, 100)).collect();
assert_eq!(clip_cuts(&run)(&run[1]), ClipCut { left: true, right: true });
}
#[test]
fn only_butted_sides_lose_their_round_corners() {
let round = ALONE.corners();
assert_eq!((round.nw, round.sw, round.ne, round.se), (2, 2, 2, 2));
let first = ClipCut { left: false, right: true }.corners();
assert_eq!((first.nw, first.sw, first.ne, first.se), (2, 2, 0, 0));
let second = ClipCut { left: true, right: false }.corners();
assert_eq!((second.nw, second.sw, second.ne, second.se), (0, 0, 2, 2));
}
#[test]
fn name_pill_is_as_wide_as_its_text_and_cut_cleanly_on_short_clips() {
// Room to spare: the text plus its padding on both sides.
assert_eq!(name_pill_width(200.0, 50.0), Some(50.0 + 2.0 * PILL_PAD));
// A short clip caps the pill at the room it leaves; the text is then cut at the pill's edge.
assert_eq!(name_pill_width(30.0, 80.0), Some(30.0));
assert_eq!(name_pill_width(PILL_MIN_WIDTH, 80.0), Some(PILL_MIN_WIDTH));
// Too narrow to show anything useful, or nonsense numbers: no pill.
assert_eq!(name_pill_width(PILL_MIN_WIDTH - 0.1, 80.0), None);
assert_eq!(name_pill_width(-5.0, 80.0), None);
assert_eq!(name_pill_width(f32::NAN, 80.0), None);
// A bogus text width never makes a negative or NaN pill.
assert_eq!(name_pill_width(100.0, f32::NAN), Some(2.0 * PILL_PAD));
assert_eq!(name_pill_width(100.0, -40.0), Some(2.0 * PILL_PAD));
}
#[test]
fn marks_list_the_rating_and_the_lock() {
let mut clip = test_clip(1, 0, 100);
assert_eq!(clip_marks(&clip, true), "");
clip.rating = 3;
assert_eq!(clip_marks(&clip, true), "★★★");
assert_eq!(clip_marks(&clip, false), "");
clip.edit_locked = true;
assert_eq!(clip_marks(&clip, true), "★★★ 🔒");
assert_eq!(clip_marks(&clip, false), "🔒");
clip.rating = 200;
assert_eq!(clip_marks(&clip, true), "★★★★★ 🔒");
}
#[test]
fn the_clip_name_is_a_pill_over_the_waveform_not_a_bar() {
for show_name in [true, false] {
let mut h = smart_demo();
h.state_mut().engine.execute("view.clip_name", &json!({"value": show_name})).unwrap();
h.step();
let tl = timeline(&h);
let (clip, lane) = clip_on_screen(&h, |xs, xe| xs > tl.min.x + 30.0 && xe < tl.max.x - 100.0 && xe - xs > 100.0);
let s = h.state().engine.session();
let (xs, xe) = (x_of(s, tl, clip.start), x_of(s, tl, clip.end()));
let clip_rect = Rect::from_min_max(pos2(xs, lane.min.y), pos2(xe, lane.max.y));
let shapes = &h.output().shapes;
let pills: Vec<Rect> = shapes
.iter()
.filter_map(|shape| match &shape.shape {
Shape::Rect(r) if r.fill == PILL_BG && clip_rect.contains_rect(r.rect) => Some(r.rect),
_ => None,
})
.collect();
if !show_name {
assert!(pills.is_empty(), "names are off");
continue;
}
assert_eq!(pills.len(), 1, "one pill per clip");
let pill = pills[0];
// At the top-left, 1 pt inside the outline, as wide as its text plus padding: far from a full-width bar.
assert_eq!(pill.min, pos2(xs + PILL_INSET, lane.min.y + 1.0 + PILL_INSET));
assert_eq!(pill.height(), PILL_HEIGHT);
let text_w = shapes
.iter()
.find_map(|shape| match &shape.shape {
Shape::Text(t) if t.galley.text() == clip.name && pill.contains(t.pos) => Some(t.galley.size().x),
_ => None,
})
.expect("the name is drawn on the pill");
assert_eq!(pill.width(), text_w + 2.0 * PILL_PAD);
assert!(pill.width() < clip_rect.width() * 0.5);
}
}
#[test]
fn the_rating_stars_follow_the_rating_switch_whatever_the_name_switch_says() {
for (show_name, show_rating) in [(false, true), (true, true), (true, false), (false, false)] {
let mut h = smart_demo();
for (cmd, on) in [("view.clip_name", show_name), ("view.clip_rating", show_rating)] {
h.state_mut().engine.execute(cmd, &json!({"value": on})).unwrap();
}
h.state_mut().engine.session_mut().tracks.iter_mut().flat_map(|t| &mut t.playlists).flat_map(|p| &mut p.clips).for_each(|c| c.rating = 3);
h.step();
let stars = h.output().shapes.iter().filter(|c| matches!(&c.shape, Shape::Text(t) if t.galley.text() == "★★★")).count();
assert_eq!(stars > 0, show_rating, "names {show_name}, rating {show_rating}: {stars} rated clips drew their stars");
}
}
#[test]
fn alternate_playlist_lanes_draw_only_the_clips_on_screen() {
let mut h = harness(1);
let tl = timeline(&h);
let s = h.state().engine.session();
let (view, color) = (Range::new(sample_at(s, tl, tl.min.x), sample_at(s, tl, tl.max.x)), s.tracks[0].color);
let len = (view.end - view.start) / 20;
// One clip well inside the view, one running past its right edge, two far beyond it.
let starts = [view.start + 4 * len, view.end - len / 2, view.end + 20 * len, view.end + 2_000 * len];
let s = h.state_mut().engine.session_mut();
let mut alt = soundcraft_model::Playlist::new("alt");
alt.clips = starts.iter().map(|&at| test_clip(s.new_clip_id().0, at, len)).collect();
let track = s.tracks.first_mut().unwrap();
(track.view, track.active_playlist) = ("playlists".into(), 0);
track.playlists.push(alt);
h.run_steps(3);
// The main playlist is empty: every clip body in the track's rows belongs to the alternate lane.
let bodies: Vec<Rect> = h
.output()
.shapes
.iter()
.filter_map(|c| match &c.shape {
Shape::Rect(r) if r.fill == clip_colors(color, false).0 => Some(r.rect),
_ => None,
})
.collect();
assert_eq!(bodies.len(), 2, "only the two clips that show are drawn: {bodies:?}");
assert!(bodies.iter().all(|r| r.max.x > tl.min.x && r.min.x < tl.max.x), "{bodies:?} are not all on screen");
}
fn test_clip(id: u64, start: Samples, len: Samples) -> Clip {
Clip::audio(ClipId(id), "c", soundcraft_model::SourceId(0), 0, start, len)
}
#[test]
fn cut_is_found_among_the_playlist_neighbours() {
// Selection never enters: it recolours the body, so a selected clip gets the same edges as any other.
let clips = [test_clip(1, 0, 100), test_clip(2, 100, 100), test_clip(3, 200, 100), test_clip(4, 400, 50)];
let cuts: Vec<ClipCut> = clips.iter().map(|c| clip_cut_in(&clips, c)).collect();
assert_eq!(cuts, [ClipCut { left: false, right: true }, ClipCut { left: true, right: true }, ClipCut { left: true, right: false }, ALONE]);
}
#[test]
fn cut_does_not_depend_on_the_playlist_order() {
// Bring to Front / Send to Back reorder a playlist (its order is the stacking order): the seam stays.
let (a, b) = (test_clip(1, 0, 100), test_clip(2, 100, 100));
let reordered = [b.clone(), a.clone()];
assert_eq!(clip_cut_in(&reordered, &a), ClipCut { left: false, right: true });
assert_eq!(clip_cut_in(&reordered, &b), ClipCut { left: true, right: false });
let run = [test_clip(3, 200, 100), test_clip(1, 0, 100), test_clip(2, 100, 100)];
let cuts: Vec<ClipCut> = run.iter().map(|c| clip_cut_in(&run, c)).collect();
assert_eq!(cuts, [ClipCut { left: true, right: false }, ClipCut { left: false, right: true }, ClipCut { left: true, right: true }]);
}
#[test]
fn layered_clips_share_a_seam_only_where_they_really_meet() {
// A short clip inside a long one does not hide the seam between the long clip and the next.
let clips = [test_clip(1, 0, 100), test_clip(2, 10, 10), test_clip(3, 100, 100)];
assert_eq!(clip_cut_in(&clips, &clips[2]), ClipCut { left: true, right: false });
assert_eq!(clip_cut_in(&clips, &clips[0]), ClipCut { left: false, right: true });
assert_eq!(clip_cut_in(&clips, &clips[1]), ALONE);
// Two clips on one start, and a lone clip.
let same = [test_clip(1, 0, 100), test_clip(2, 0, 100), test_clip(3, 300, 100)];
assert_eq!(clip_cut_in(&same, &same[1]), ALONE);
assert_eq!(clip_cut_in(&same, &same[2]), ALONE);
assert_eq!(clip_cut_in(&[], &test_clip(9, 0, 100)), ALONE);
}
#[test]
fn marks_pill_is_hidden_when_the_clip_is_too_narrow_for_it() {
// A 60 pt clip from x=100 (pills start at 102) holds a 30 pt pill at its right end.
assert_eq!(marks_pill_left(102.0, 160.0, 30.0), Some(128.0));
// Exactly full: the pill's left edge meets the leftmost x.
assert_eq!(marks_pill_left(102.0, 134.0, 30.0), Some(102.0));
// Any narrower and it would veil the clip: hidden.
assert_eq!(marks_pill_left(102.0, 133.0, 30.0), None);
assert_eq!(marks_pill_left(102.0, f32::NAN, 30.0), None);
}
#[test]
fn an_overlapping_clip_covers_the_pill_of_the_clip_below() {
let mut h = smart_demo();
h.step();
let tl = timeline(&h);
let (below, lane) = clip_on_screen(&h, |xs, xe| xs > tl.min.x + 30.0 && xe < tl.max.x - 100.0 && xe - xs > 100.0);
// A copy on top (last in the playlist), starting at the same place.
let s = h.state_mut().engine.session_mut();
let (track, _) = s.find_clip(below.id).unwrap();
let id = s.new_clip_id();
let top = Clip { id, name: "Top".into(), ..below.clone() };
s.track_mut(track).unwrap().playlist_mut().unwrap().clips.push(top);
h.step();
h.step();
let s = h.state().engine.session();
let at = pos2(x_of(s, tl, below.start) + PILL_INSET, lane.min.y + 1.0 + PILL_INSET);
let shapes = &h.output().shapes;
let pills: Vec<(usize, Rect)> = shapes
.iter()
.enumerate()
.filter_map(|(i, shape)| match &shape.shape {
Shape::Rect(r) if r.fill == PILL_BG && r.rect.min == at => Some((i, r.rect)),
_ => None,
})
.collect();
assert_eq!(pills.len(), 2, "both clips draw a pill at the same spot");
let ((lower, pill), (upper, _)) = (pills[0], pills[1]);
assert!(
shapes[lower + 1..upper].iter().any(|shape| matches!(&shape.shape, Shape::Rect(r) if r.fill != PILL_BG && r.rect.contains_rect(pill))),
"the top clip's body is painted between the two pills, so it hides the lower pill"
);
}
#[test]
fn playhead_is_red_only_while_recording() {
for t in [Tokens::DARK, Tokens::LIGHT] {
assert_eq!(playhead_colour(&t, true), t.playhead);
assert_eq!(playhead_colour(&t, false), t.playback);
assert_ne!(t.playback, t.playhead, "plain playback must not read as the record line");
}
}
/// WCAG contrast ratio between two colours (1 = identical, 21 = black on white).
fn contrast(a: Color32, b: Color32) -> f32 {
let lum = |c: Color32| {
let lin = |v: u8| {
let v = f32::from(v) / 255.0;
if v <= 0.04045 { v / 12.92 } else { ((v + 0.055) / 1.055).powf(2.4) }
};
0.2126 * lin(c.r()) + 0.7152 * lin(c.g()) + 0.0722 * lin(c.b())
};
let (hi, lo) = (lum(a).max(lum(b)), lum(a).min(lum(b)));
(hi + 0.05) / (lo + 0.05)
}
/// The edit cursor stands still beside the moving playback line while playing: in both themes
/// they must be two clearly different greys, and the line must still read on the lanes.
#[test]
fn the_playback_line_is_set_apart_from_the_insertion_point_in_both_themes() {
for (name, t) in [("dark", Tokens::DARK), ("light", Tokens::LIGHT)] {
let apart = contrast(t.playback, t.insertion);
assert!(apart >= 2.5, "{name}: playback {:?} and insertion {:?} are only {apart:.2}:1 apart", t.playback, t.insertion);
let reads = contrast(t.playback, t.playlist_bg);
assert!(reads >= 3.0, "{name}: the playback line is only {reads:.2}:1 on the lanes");
}
}
/// The colour `overlay` gives the full-height playback line (width 1.5) in this transport state.
fn playback_line_colour(recording: bool) -> Option<Color32> {
use egui::{Rect, pos2};
let _palette = crate::theme::lock_test_palette();
let mut app = SoundApp::new(soundcraft_engine::Engine::default(), None, crate::Services::default());
app.engine.session_mut().edit.zoom.samples_per_px = 1_000.0;
app.engine.transport.playing = true;
app.engine.transport.recording = recording;
app.engine.transport.position = 80_000;
let tl = Rect::from_min_max(pos2(100.0, 100.0), pos2(900.0, 500.0));
let area = Rect::from_min_max(pos2(100.0, 50.0), tl.max);
let ctx = egui::Context::default();
let mut output = ctx.run_ui(egui::RawInput::default(), |ui| overlay(&mut app, ui, tl, area));
output.textures_delta.clear();
output.shapes.iter().find_map(|s| match &s.shape {
Shape::LineSegment { points, stroke } if points[0].x == 180.0 && stroke.width == 1.5 => Some(stroke.color),
_ => None,
})
}
#[test]
fn overlay_draws_the_playback_line_neutral_and_the_record_line_red() {
let _palette = crate::theme::lock_test_palette();
let t = Tokens::current();
assert_eq!(playback_line_colour(false), Some(t.playback));
assert_eq!(playback_line_colour(true), Some(t.playhead));
}
#[test]
fn alt_click_on_a_marker_deletes_it() {
let mut engine = soundcraft_engine::demo::demo_engine();
+5 -3
View File
@@ -1,5 +1,5 @@
use super::automation::draw_automation;
use super::clips::{ClipLane, GridLines, draw_clip, draw_live_take, draw_strip_silence};
use super::clips::{ClipLane, GridLines, clip_cuts, draw_clip, draw_live_take, draw_strip_silence};
use super::header::{header_column, track_header};
use super::input::lane_interaction;
use super::layout::{Column, HEADER_W, LANE_H, STRIP_W, lane_rect, sample_at, x_of};
@@ -90,11 +90,12 @@ pub(super) fn track_row(app: &mut SoundApp, ui: &mut Ui, id: TrackId, row: Rect,
let view = Range::new(sample_at(&s, tl, tl.min.x), sample_at(&s, tl, tl.max.x));
let auto_view = AutoParam::parse(&track.view);
let sorted = track.clips().is_sorted_by_key(|c| c.start);
let cut_of = clip_cuts(track.clips());
for clip in track.clips() {
if !clip.range().overlaps(&view) {
continue;
}
draw_clip(&ClipLane { app, painter: &painter, s: &s, track, sorted, tl, lane }, clip, auto_view.is_some());
draw_clip(&ClipLane { app, painter: &painter, s: &s, track, sorted, tl, lane }, clip, cut_of(clip), auto_view.is_some());
}
draw_live_take(app, &painter, &s, track, tl, lane);
crate::video_track::draw_lane(app, &painter, &s, track, tl, lane);
@@ -143,8 +144,9 @@ fn playlist_lanes(app: &mut SoundApp, ui: &mut Ui, track: &Track, row: Rect, mai
let painter = ui.painter().with_clip_rect(lane.intersect(ui.clip_rect()));
let sorted = track.clips().is_sorted_by_key(|c| c.start);
let view = Range::new(sample_at(&s, tl, tl.min.x), sample_at(&s, tl, tl.max.x));
let cut_of = clip_cuts(&pl.clips);
for c in pl.clips.iter().filter(|c| c.range().overlaps(&view)) {
draw_clip(&ClipLane { app, painter: &painter, s: &s, track, sorted, tl, lane }, c, false);
draw_clip(&ClipLane { app, painter: &painter, s: &s, track, sorted, tl, lane }, c, cut_of(c), false);
}
let resp = ui.interact(lane, ui.scope_id().with(("pl_lane", track.id.0, i)), Sense::click());
if resp.clicked()
+6 -1
View File
@@ -1,3 +1,4 @@
use super::overlay::playhead_colour;
use super::{Color32, Rect, Samples, Sense, Session, SoundApp, Stroke, StrokeKind, Tokens, Track, Ui, json, pos2, rgb, vec2};
/// The Universe's clip blocks for one document revision and area: each track's clips merged into
@@ -82,7 +83,11 @@ pub(super) fn universe(app: &mut SoundApp, ui: &mut Ui) {
ui.painter().rect_stroke(vr, 2.0, Stroke::new(1.5, Tokens::current().universe_view), StrokeKind::Outside);
if app.is_playing() {
let x = r.min.x + app.position() as f32 / total * r.width();
ui.painter().line_segment([pos2(x, r.min.y), pos2(x, r.max.y)], Stroke::new(1.0, Tokens::current().playhead));
let line = [pos2(x, r.min.y), pos2(x, r.max.y)];
// The Universe paints clips in their raw track colours, some as light as the neutral playback line: a dark
// edge on each side keeps it readable over all of them.
ui.painter().line_segment(line, Stroke::new(3.0, Color32::from_black_alpha(160)));
ui.painter().line_segment(line, Stroke::new(1.0, playhead_colour(&Tokens::current(), app.engine.transport.recording)));
}
let resp = ui.interact(r, ui.scope_id().with("universe"), Sense::click_and_drag());
if let Some(p) = resp.interact_pointer_pos()
+37 -1
View File
@@ -46,8 +46,30 @@ impl Imports {
}
/// Imports the audio file `params["path"]` names (the `file.import_audio` params) in the
/// background. Anything else (several `paths`, no path, the browser) runs the command now.
/// background; several `paths` queue one import each. Anything else (no path, the browser) runs the command now.
pub fn start(app: &mut SoundApp, params: Value) {
// Several picked files: one queue entry each, so they decode one at a time and the app stays responsive.
if !cfg!(target_arch = "wasm32")
&& params.get("path").is_none()
&& let Some(paths) = params.get("paths").and_then(Value::as_array)
{
let singles: Vec<Value> = paths
.iter()
.filter_map(Value::as_str)
.map(|p| {
let mut one = params.clone();
if let Some(o) = one.as_object_mut() {
o.remove("paths");
o.insert("path".into(), Value::from(p));
}
one
})
.collect();
if !singles.is_empty() {
app.imports.queue.extend(singles);
return;
}
}
if cfg!(target_arch = "wasm32") || params.get("path").and_then(Value::as_str).is_none() {
run_now(app, params);
return;
@@ -213,6 +235,20 @@ mod tests {
assert_eq!(app.engine.session().tracks.len(), 1, "the good file still imports");
}
/// Issue #1089: several files picked in one Browse go to the queue, one entry each.
#[test]
fn several_paths_import_one_after_another() {
let ctx = egui::Context::default();
let mut app = SoundApp::new(soundcraft_engine::Engine::default(), None, Services::default());
let (a, b, c) = (tone_wav("many-a.wav"), tone_wav("many-b.wav"), tone_wav("many-c.wav"));
start(&mut app, json!({"paths": [a, b, c]}));
assert_eq!(app.imports.queue.len(), 3);
step(&mut app, &ctx);
assert_eq!(app.imports.label().map(|l| l.1), Some(2), "the other two wait for the first");
settle(&mut app, &ctx);
assert_eq!(app.engine.session().tracks.len(), 3);
}
#[test]
fn a_failed_import_stays_on_the_status_line_while_the_next_one_starts() {
let ctx = egui::Context::default();
+39 -18
View File
@@ -20,7 +20,7 @@ pub use plugin_menu::{instrument_menu, plugin_info, plugin_menu};
pub(crate) use routing::{input_name, route_name, route_popup};
#[cfg(test)]
use sends::dragged_send_db;
use sends::{Readout, editable_readout, parse_db, parse_pan};
pub(crate) use sends::{Readout, editable_readout, parse_db, parse_pan};
pub(crate) use sends::{send_slot, send_window};
pub(crate) use slots::{insert_slot, instrument_slot};
@@ -90,6 +90,12 @@ fn output_format(app: &SoundApp, track: &Track) -> ChannelFormat {
}
}
/// Whether the strip pans a track with the surround panner, because its output is multichannel, instead of the stereo pans.
pub(crate) fn surround_panned(app: &SoundApp, track: &Track) -> bool {
let out_fmt = output_format(app, track);
out_fmt.channels() > 2 && !out_fmt.is_ambisonic() && !matches!(track.kind, TrackKind::Master | TrackKind::Vca) && track.channels() <= 2
}
/// Speaker dots of a format, placed on the panner square.
fn panner_speakers(fmt: ChannelFormat) -> Vec<PannerSpeaker> {
let sp = fmt.speakers();
@@ -371,27 +377,36 @@ fn comments_section(app: &mut SoundApp, ui: &mut Ui, cx: &StripCtx<'_>, mut y: f
y
}
/// Input, output and automation mode selectors.
/// Whether a track shows an input selector. A master fader has no input of its own:
/// it only shows the output of the path it controls.
pub(crate) fn has_input_selector(kind: TrackKind) -> bool {
kind != TrackKind::Master
}
/// Input (except on master faders), output and automation mode selectors.
fn io_section(app: &mut SoundApp, ui: &mut Ui, cx: &StripCtx<'_>, mut y: f32) -> f32 {
let StripCtx { track, id, narrow, x0, inner_w, .. } = *cx;
let t = Tokens::current();
if cx.shows("io") {
let has_input = has_input_selector(track.kind);
let sec = Rect::from_min_size(pos2(x0, y), vec2(inner_w, 16.0 + 2.0 * 18.0 + 30.0));
inset(ui.painter(), sec, t.strip_section);
section_label(ui, sec, "I / O", narrow);
let in_r = Rect::from_min_size(pos2(x0 + 2.0, sec.min.y + 15.0), vec2(inner_w - 4.0, 16.0));
let out_r = Rect::from_min_size(pos2(x0 + 2.0, sec.min.y + 33.0), vec2(inner_w - 4.0, 16.0));
let mut c = ui.new_child(egui::UiBuilder::new().id_salt(("mix-input", id.0)).max_rect(in_r));
let input = input_name(app, track);
let resp = selector_box_named(
&mut c,
in_r.width(),
in_r.height(),
&input,
t.text,
SelectorLabels { name: &format!("Input for {}", track.name), value: &input, tooltip: "Input" },
);
route_popup(app, &resp, id, true);
let out_r = if has_input { Rect::from_min_size(pos2(x0 + 2.0, sec.min.y + 33.0), vec2(inner_w - 4.0, 16.0)) } else { in_r };
if has_input {
let mut c = ui.new_child(egui::UiBuilder::new().id_salt(("mix-input", id.0)).max_rect(in_r));
let input = input_name(app, track);
let resp = selector_box_named(
&mut c,
in_r.width(),
in_r.height(),
&input,
t.text,
SelectorLabels { name: &format!("Input for {}", track.name), value: &input, tooltip: "Input" },
);
route_popup(app, &resp, id, true);
}
let mut c = ui.new_child(egui::UiBuilder::new().id_salt(("mix-output", id.0)).max_rect(out_r));
let output = route_name(app, &track.mixer.output);
let resp = selector_box_named(
@@ -404,7 +419,7 @@ fn io_section(app: &mut SoundApp, ui: &mut Ui, cx: &StripCtx<'_>, mut y: f32) ->
);
route_popup(app, &resp, id, false);
// Automation mode.
let am_r = Rect::from_min_size(pos2(x0 + 2.0, sec.min.y + 51.0), vec2(inner_w - 4.0, 16.0));
let am_r = Rect::from_min_size(pos2(x0 + 2.0, sec.min.y + 15.0 + 2.0 * 18.0), vec2(inner_w - 4.0, 16.0));
ui.painter().text(pos2(am_r.center().x, am_r.min.y - 1.0), Align2::CENTER_BOTTOM, "", regular(9.0), t.text_dim);
let mut c = ui.new_child(egui::UiBuilder::new().max_rect(am_r));
let mode = track.mixer.automation_mode;
@@ -470,9 +485,7 @@ fn pan_section(app: &mut SoundApp, ui: &mut Ui, cx: &StripCtx<'_>, mut y: f32) -
let t = Tokens::current();
// Pan: a surround panner when the output is multichannel, else the stereo knobs.
let out_fmt = output_format(app, track);
let surround_out =
out_fmt.channels() > 2 && !out_fmt.is_ambisonic() && !matches!(track.kind, TrackKind::Master | TrackKind::Vca) && track.channels() <= 2;
if surround_out {
if surround_panned(app, track) {
let size = (inner_w - 4.0).min(88.0);
let sp = track.mixer.surround.unwrap_or_else(|| {
SurroundPan::from_stereo(if track.mixer.pan.len() == 1 { track.mixer.pan.first().copied().unwrap_or(0.0) } else { 0.0 })
@@ -667,6 +680,14 @@ fn name_and_readout(app: &mut SoundApp, ui: &mut Ui, cx: &StripCtx<'_>) {
mod tests {
use super::{Readout, dragged_send_db, editable_readout, parse_db, parse_pan};
use egui::{Event, Modifiers, PointerButton, Rect, pos2, vec2};
use soundcraft_model::TrackKind;
#[test]
fn master_strip_has_no_input_selector() {
assert!(!super::has_input_selector(TrackKind::Master));
assert!(super::has_input_selector(TrackKind::Audio));
assert!(super::has_input_selector(TrackKind::Aux));
}
#[test]
fn typed_db_accepts_console_notation_and_clamps() {
+11 -10
View File
@@ -208,22 +208,22 @@ pub(super) fn dragged_send_db(db: f32, dy: f32, fine: bool) -> f32 {
}
/// What an [`editable_readout`] shows and how a typed value is parsed.
pub(super) struct Readout<'a> {
pub(crate) struct Readout<'a> {
/// Accessible name of the text field shown while editing.
pub(super) name: &'a str,
pub(crate) name: &'a str,
/// The value painted while not editing.
pub(super) display: &'a str,
pub(crate) display: &'a str,
/// The editor's initial text.
pub(super) edit_initial: &'a str,
pub(super) parse: fn(&str) -> Option<f32>,
pub(crate) edit_initial: &'a str,
pub(crate) parse: fn(&str) -> Option<f32>,
/// The value an Alt-click sets.
pub(super) reset: f32,
pub(crate) reset: f32,
}
/// Paint a value until clicked, then replace it with a focused text field. Enter or clicking away
/// commits a valid value; Escape cancels, and so does the field losing focus while hidden.
/// Alt-click returns `reset`. The interaction response remains available for dragging.
pub(super) fn editable_readout(ui: &mut Ui, r: Rect, id: egui::Id, readout: Readout<'_>) -> (Option<egui::Response>, Option<f32>) {
pub(crate) fn editable_readout(ui: &mut Ui, r: Rect, id: egui::Id, readout: Readout<'_>) -> (Option<egui::Response>, Option<f32>) {
let Readout { name, display, edit_initial, parse, reset } = readout;
let t = Tokens::current();
let active_id = id.with("active");
@@ -280,7 +280,7 @@ pub(super) fn editable_readout(ui: &mut Ui, r: Rect, id: egui::Id, readout: Read
(response, value)
}
pub(super) fn parse_db(text: &str) -> Option<f32> {
pub(crate) fn parse_db(text: &str) -> Option<f32> {
let cleaned = text.trim().to_ascii_lowercase().replace("db", "").replace(',', ".");
if matches!(cleaned.trim(), "-inf" | "-infinity" | "off") {
return Some(-144.0);
@@ -288,7 +288,7 @@ pub(super) fn parse_db(text: &str) -> Option<f32> {
cleaned.trim().parse::<f32>().ok().filter(|v| v.is_finite()).map(|v| v.clamp(-144.0, 12.0))
}
pub(super) fn parse_pan(text: &str) -> Option<f32> {
pub(crate) fn parse_pan(text: &str) -> Option<f32> {
let cleaned = text.trim().to_ascii_lowercase().replace(',', ".");
let (sign, number) = if let Some(v) = cleaned.strip_prefix('<').or_else(|| cleaned.strip_prefix('l')) {
(-1.0, v)
@@ -335,7 +335,6 @@ enum SendFooter {
/// that scrolls, "new bus..." (and "Remove send" for an existing send) fixed under it.
/// `current` is the send's destination and its name, if the slot has a send.
fn send_route_menu(app: &mut SoundApp, ui: &Ui, field: &egui::Response, id: TrackId, slot: usize, current: Option<(Route, String)>) {
let busses = app.engine.session().busses.clone();
let which = (b'A' + slot as u8) as char;
let has_send = current.is_some();
let header = |ui: &mut Ui| {
@@ -355,6 +354,7 @@ fn send_route_menu(app: &mut SoundApp, ui: &Ui, field: &egui::Response, id: Trac
}
}
let search = MenuSearch::show(ui, egui::Id::unique(("send-bus-search", id.0, slot)), "Search buses…");
let busses = &app.engine.session().busses;
let visible: Vec<_> = busses.iter().filter(|bus| search.matches(&bus.name)).collect();
(search, visible)
};
@@ -402,6 +402,7 @@ fn send_route_menu(app: &mut SoundApp, ui: &Ui, field: &egui::Response, id: Trac
}
match footer {
Some(SendFooter::NewBus) => {
let busses = &app.engine.session().busses;
let mut n = busses.len() + 1;
while busses.iter().any(|bus| bus.name.eq_ignore_ascii_case(&format!("Bus {n}")))
|| app.engine.session().track_by_name(&format!("Bus {n}")).is_some()
+12 -2
View File
@@ -78,16 +78,26 @@ pub(super) fn plugin_windows(app: &mut SoundApp, ctx: &egui::Context) {
}
});
x = idx as f32;
if r.response.clicked() && ui.input(|i| i.modifiers.alt) {
x = p.default;
egui::Popup::close_all(ui.ctx());
}
r.response
} else {
let mut sl = egui::Slider::new(&mut x, p.min..=p.max).show_value(false);
let mut wide = f64::from(x);
let mut sl = crate::widgets::FineSlider::new(&mut wide, f64::from(p.min)..=f64::from(p.max))
.id_salt(p.id)
.show_value(false)
.default_value(f64::from(p.default));
if instrument && crate::sampler_window::whole_number(&ins.plugin, p.id) {
sl = sl.integer();
}
if p.taper == soundcraft_dsp::Taper::Log && p.min > 0.0 {
sl = sl.logarithmic(true);
}
ui.add(sl)
let r = sl.show(ui);
x = wide as f32;
r
};
// The grid cell's painted label is not an accesskit name; tie the
// combo box or slider to it so screen readers and label queries find it.
+10 -2
View File
@@ -791,7 +791,13 @@ pub fn clip_effects(app: &mut SoundApp, ctx: &egui::Context) {
let key = format!("{prefix}{}", p.id);
let mut v = get(app, &key, p.default);
let before = v;
col.add(egui::Slider::new(&mut v, p.min..=p.max).text(p.name));
let mut wide = f64::from(v);
crate::widgets::FineSlider::new(&mut wide, f64::from(p.min)..=f64::from(p.max))
.text(p.name)
.logarithmic(p.taper == soundcraft_dsp::Taper::Log && p.min > 0.0)
.default_value(f64::from(p.default))
.show(col);
v = wide as f32;
if (v - before).abs() > f32::EPSILON {
let mut m = serde_json::Map::new();
m.insert(key.clone(), json!(v));
@@ -806,7 +812,9 @@ pub fn clip_effects(app: &mut SoundApp, ctx: &egui::Context) {
});
let mut g = get(app, "gain", 0.0);
let before = g;
ui.add(egui::Slider::new(&mut g, -36.0..=24.0).text("Clip effects gain (dB)"));
let mut wide = f64::from(g);
crate::widgets::FineSlider::new(&mut wide, -36.0..=24.0).text("Clip effects gain (dB)").default_value(0.0).show(ui);
g = wide as f32;
if (g - before).abs() > f32::EPSILON {
let _ =
app.engine.execute_merged("clip.effects_set", &json!({"clips": [cid.0], "params": {"gain": g}}), &format!("cfx:{}:gain", cid.0));
+2
View File
@@ -5,6 +5,8 @@ use std::path::{Path, PathBuf};
#[derive(Default)]
pub struct Services {
pub pick_open: Option<Box<dyn Fn(&str, &[&str]) -> Option<PathBuf>>>,
/// Like `pick_open`, but the user may select several files (Import Audio). Empty when cancelled.
pub pick_open_many: Option<Box<dyn Fn(&str) -> Vec<PathBuf>>>,
/// The purpose and the file to start from.
pub pick_save: Option<Box<dyn Fn(&str, &Path) -> Option<PathBuf>>>,
/// The folder to start in.
+68
View File
@@ -1,6 +1,7 @@
//! Design tokens. Colours were chosen by eye to give a dark, studio-style look; they are ours.
use egui::{Color32, FontFamily, FontId};
use soundcraft_model::EditMode;
use std::sync::atomic::{AtomicBool, Ordering};
/// Whether the Light palette is active. Dark is the default (the incumbent is dark-only).
@@ -143,6 +144,9 @@ pub struct Tokens {
pub selection: Color32,
pub playhead: Color32,
pub insertion: Color32,
/// The playback line while the transport runs without recording: a mid grey, set well apart
/// from `insertion` (the edit cursor stands beside it while playing) in both themes.
pub playback: Color32,
pub solo: Color32,
pub mute: Color32,
pub rec: Color32,
@@ -165,6 +169,13 @@ pub struct Tokens {
pub group_border: Color32,
/// Edit-mode cell, not selected.
pub mode_off: Color32,
/// Edit-mode cell, selected: one colour per mode so the active mode reads at a glance
/// (see [`Tokens::edit_mode_on`]). The label on them is `mode_on_text`.
pub mode_shuffle: Color32,
pub mode_spot: Color32,
pub mode_slip: Color32,
pub mode_grid: Color32,
pub mode_grid_relative: Color32,
/// Counter-panel toggle (Grid, Count Off), off.
pub counter_off: Color32,
/// Outline of the counter panels.
@@ -253,6 +264,7 @@ impl Tokens {
selection: Color32::from_rgba_premultiplied(40, 70, 110, 110),
playhead: Color32::from_rgb(232, 60, 50),
insertion: Color32::from_rgb(230, 230, 230),
playback: Color32::from_rgb(140, 140, 142),
solo: Color32::from_rgb(222, 196, 52),
mute: Color32::from_rgb(232, 148, 40),
rec: Color32::from_rgb(214, 52, 46),
@@ -272,6 +284,11 @@ impl Tokens {
menu_bar: Color32::from_rgb(22, 22, 23),
group_border: Color32::from_rgb(8, 8, 8),
mode_off: Color32::from_rgb(20, 36, 22),
mode_shuffle: Color32::from_rgb(246, 112, 102),
mode_spot: Color32::from_rgb(236, 148, 38),
mode_slip: Color32::from_rgb(92, 196, 108),
mode_grid: Color32::from_rgb(86, 162, 238),
mode_grid_relative: Color32::from_rgb(176, 134, 238),
counter_off: Color32::from_rgb(30, 50, 32),
counter_border: Color32::from_rgb(50, 50, 50),
counter_rule: Color32::from_rgb(40, 40, 40),
@@ -338,6 +355,7 @@ impl Tokens {
selection: Color32::from_rgba_premultiplied(100, 145, 205, 90),
playhead: Color32::from_rgb(210, 48, 42),
insertion: Color32::from_rgb(35, 36, 40),
playback: Color32::from_rgb(110, 113, 119),
solo: Color32::from_rgb(178, 145, 20),
mute: Color32::from_rgb(194, 106, 22),
rec: Color32::from_rgb(190, 42, 38),
@@ -357,6 +375,11 @@ impl Tokens {
menu_bar: Color32::from_rgb(204, 206, 210),
group_border: Color32::from_rgb(118, 121, 127),
mode_off: Color32::from_rgb(202, 212, 204),
mode_shuffle: Color32::from_rgb(188, 48, 42),
mode_spot: Color32::from_rgb(176, 94, 4),
mode_slip: Color32::from_rgb(44, 128, 62),
mode_grid: Color32::from_rgb(62, 116, 180),
mode_grid_relative: Color32::from_rgb(124, 82, 178),
counter_off: Color32::from_rgb(192, 212, 198),
counter_border: Color32::from_rgb(118, 121, 127),
counter_rule: Color32::from_rgb(188, 191, 196),
@@ -386,6 +409,19 @@ impl Tokens {
pub fn current() -> Tokens {
if is_light() { Self::LIGHT } else { Self::DARK }
}
/// Fill of the edit-mode key while `mode` is the active one.
// ponytail: one label colour (`mode_on_text`) serves the five fills; Light needs fills dark enough for
// white text, so Spot is a deep orange. A per-mode label token is the way out if a fill must go lighter.
pub fn edit_mode_on(&self, mode: EditMode) -> Color32 {
match mode {
EditMode::Shuffle => self.mode_shuffle,
EditMode::Spot => self.mode_spot,
EditMode::Slip => self.mode_slip,
EditMode::Grid => self.mode_grid,
EditMode::GridRelative => self.mode_grid_relative,
}
}
}
/// Whether the Light palette is the active one.
@@ -620,4 +656,36 @@ mod tests {
}
}
}
#[test]
fn each_edit_mode_has_its_own_readable_active_colour() {
// WCAG relative luminance and contrast ratio.
fn lum(c: Color32) -> f64 {
let lin = |v: u8| {
let v = f64::from(v) / 255.0;
if v <= 0.04045 { v / 12.92 } else { ((v + 0.055) / 1.055).powf(2.4) }
};
0.2126 * lin(c.r()) + 0.7152 * lin(c.g()) + 0.0722 * lin(c.b())
}
fn contrast(a: Color32, b: Color32) -> f64 {
let (a, b) = (lum(a), lum(b));
(a.max(b) + 0.05) / (a.min(b) + 0.05)
}
let modes = [EditMode::Shuffle, EditMode::Spot, EditMode::Slip, EditMode::Grid, EditMode::GridRelative];
for (name, t) in [("dark", Tokens::DARK), ("light", Tokens::LIGHT)] {
for (i, a) in modes.into_iter().enumerate() {
let fill = t.edit_mode_on(a);
assert_ne!(fill, t.mode_off, "{name}: {a:?} active must differ from inactive");
// Small bold label: WCAG AA wants 4.5:1, at rest and at both ends of the pressed gradient.
let pressed = crate::widgets::PRESSED_SHADE.map(|k| crate::widgets::shade(fill, k));
for (state, face) in [("active", fill), ("pressed top", pressed[0]), ("pressed bottom", pressed[1])] {
let ratio = contrast(face, t.mode_on_text);
assert!(ratio >= 4.5, "{name}: {a:?} {state} label contrast {ratio:.2} is below 4.5");
}
for b in &modes[i + 1..] {
assert_ne!(fill, t.edit_mode_on(*b), "{name}: {a:?} and {b:?} share an active colour");
}
}
}
}
}
+5 -2
View File
@@ -60,10 +60,13 @@ fn edit_mode_tooltip(m: EditMode, locked: bool, cmd: &str, fkey: &str) -> String
fn draw_mode_cell(p: &egui::Painter, cell: Rect, resp: &egui::Response, t: &Tokens, m: EditMode, state: &ModeCellState, label: &str) {
let on = state.cur == m || (m == EditMode::Grid && state.cur == EditMode::GridRelative);
key(p, cell, resp, if on { t.mode_on } else { t.mode_off });
// `on` means `cur == m`, or Grid showing Relative Grid: the active colour is always cur's.
key(p, cell, resp, if on { t.edit_mode_on(state.cur) } else { t.mode_off });
let fg = if on { t.mode_on_text } else { t.mode_off_text };
if m == EditMode::Grid && state.snap {
p.rect_stroke(cell, 1.0, Stroke::new(1.5, t.mode_on), StrokeKind::Inside);
// The Snap to Grid outline wears the Grid colour, so it never reads as the green of Slip.
let grid = t.edit_mode_on(if state.cur.is_grid() { state.cur } else { EditMode::Grid });
p.rect_stroke(cell, 1.0, Stroke::new(1.5, grid), StrokeKind::Inside);
}
if m == EditMode::Shuffle && state.locked {
icons::draw(p, Rect::from_center_size(cell.center(), vec2(15.0, 15.0)), "lock", fg);
+52 -49
View File
@@ -439,63 +439,66 @@ pub fn draw_lane(app: &mut SoundApp, painter: &Painter, s: &Session, track: &Tra
let ctx = painter.ctx().clone();
app.video.poll(&ctx);
let on = online(s);
let name_h = if lane.height() >= 40.0 && s.edit.flag("view.clip.name") { 13.0 } else { 0.0 };
let ppp = ctx.pixels_per_point();
for clip in track.clips() {
let ClipContent::Video { source, offset } = clip.content else { continue };
let x0 = x_of(s, tl, clip.start);
let x1 = x_of(s, tl, clip.end()).max(x0 + 1.0);
let body = Rect::from_min_max(pos2(x0 + 1.0, lane.min.y + 2.0 + name_h), pos2(x1 - 1.0, lane.max.y - 2.0));
let vis = body.intersect(lane);
if vis.width() < 2.0 || vis.height() < 6.0 {
continue;
}
let p = painter.with_clip_rect(vis);
let Some(v) = s.video(source) else {
p.text(vis.center(), egui::Align2::CENTER_CENTER, "movie missing from session", regular(10.0), Color32::from_rgb(220, 120, 120));
continue;
};
if !on {
p.text(vis.center(), egui::Align2::CENTER_CENTER, "Video Track Offline", regular(10.0), Color32::from_gray(170));
continue;
}
if let Some(why) = &v.offline {
p.text(vis.center(), egui::Align2::CENTER_CENTER, format!("media offline: {why}"), regular(10.0), Color32::from_rgb(220, 120, 120));
continue;
}
let key = MovieKey::new(source, &v.path);
if let Some(Err(e)) = app.video.info(key) {
p.text(vis.center(), egui::Align2::CENTER_CENTER, format!("media offline: {e}"), regular(10.0), Color32::from_rgb(220, 120, 120));
continue;
}
let aspect = app.video.info(key).and_then(|i| i.as_ref().ok()).map_or(v.aspect(), MovieInfo::display_aspect) as f32;
let th = body.height();
let tw = (th * aspect).clamp(8.0, 600.0);
// Pixel height bucket (multiples of 16) so zooming the track does not re-decode constantly.
let hpx = (((th * ppp) / 16.0).ceil() * 16.0).clamp(16.0, 256.0) as u32;
let first = ((vis.min.x - body.min.x) / tw).floor().max(0.0) as i64;
let mut k = first;
loop {
let tx = body.min.x + k as f32 * tw;
if tx > vis.max.x || k > first + 400 {
break;
'thumbs: {
let x0 = x_of(s, tl, clip.start);
let x1 = x_of(s, tl, clip.end()).max(x0 + 1.0);
let body = Rect::from_min_max(pos2(x0 + 1.0, lane.min.y + 2.0), pos2(x1 - 1.0, lane.max.y - 2.0));
let vis = body.intersect(lane);
if vis.width() < 2.0 || vis.height() < 6.0 {
break 'thumbs;
}
let at = sample_at(s, tl, tx);
let secs = movie_secs(s, clip.start, offset, at.max(clip.start));
let r = Rect::from_min_max(pos2(tx, body.min.y), pos2((tx + tw).min(body.max.x), body.max.y));
match app.video.thumb(&ctx, key, &v.path, secs.max(0.0), hpx) {
Some(tex) => {
let full = Rect::from_min_size(r.min, egui::vec2(tw, th));
let uv = Rect::from_min_max(pos2(0.0, 0.0), pos2((r.width() / tw).clamp(0.0, 1.0), 1.0));
p.image(tex.id(), Rect::from_min_max(full.min, pos2(r.max.x, full.max.y)), uv, Color32::WHITE);
let p = painter.with_clip_rect(vis);
let Some(v) = s.video(source) else {
p.text(vis.center(), egui::Align2::CENTER_CENTER, "movie missing from session", regular(10.0), Color32::from_rgb(220, 120, 120));
break 'thumbs;
};
if !on {
p.text(vis.center(), egui::Align2::CENTER_CENTER, "Video Track Offline", regular(10.0), Color32::from_gray(170));
break 'thumbs;
}
if let Some(why) = &v.offline {
p.text(vis.center(), egui::Align2::CENTER_CENTER, format!("media offline: {why}"), regular(10.0), Color32::from_rgb(220, 120, 120));
break 'thumbs;
}
let key = MovieKey::new(source, &v.path);
if let Some(Err(e)) = app.video.info(key) {
p.text(vis.center(), egui::Align2::CENTER_CENTER, format!("media offline: {e}"), regular(10.0), Color32::from_rgb(220, 120, 120));
break 'thumbs;
}
let aspect = app.video.info(key).and_then(|i| i.as_ref().ok()).map_or(v.aspect(), MovieInfo::display_aspect) as f32;
let th = body.height();
let tw = (th * aspect).clamp(8.0, 600.0);
// Pixel height bucket (multiples of 16) so zooming the track does not re-decode constantly.
let hpx = (((th * ppp) / 16.0).ceil() * 16.0).clamp(16.0, 256.0) as u32;
let first = ((vis.min.x - body.min.x) / tw).floor().max(0.0) as i64;
let mut k = first;
loop {
let tx = body.min.x + k as f32 * tw;
if tx > vis.max.x || k > first + 400 {
break;
}
None => {
p.rect_filled(r.shrink(1.0), 0.0, Color32::from_rgb(24, 24, 26));
let at = sample_at(s, tl, tx);
let secs = movie_secs(s, clip.start, offset, at.max(clip.start));
let r = Rect::from_min_max(pos2(tx, body.min.y), pos2((tx + tw).min(body.max.x), body.max.y));
match app.video.thumb(&ctx, key, &v.path, secs.max(0.0), hpx) {
Some(tex) => {
let full = Rect::from_min_size(r.min, egui::vec2(tw, th));
let uv = Rect::from_min_max(pos2(0.0, 0.0), pos2((r.width() / tw).clamp(0.0, 1.0), 1.0));
p.image(tex.id(), Rect::from_min_max(full.min, pos2(r.max.x, full.max.y)), uv, Color32::WHITE);
}
None => {
p.rect_filled(r.shrink(1.0), 0.0, Color32::from_rgb(24, 24, 26));
}
}
p.line_segment([pos2(tx, body.min.y), pos2(tx, body.max.y)], egui::Stroke::new(1.0, Color32::from_black_alpha(160)));
k += 1;
}
p.line_segment([pos2(tx, body.min.y), pos2(tx, body.max.y)], egui::Stroke::new(1.0, Color32::from_black_alpha(160)));
k += 1;
}
// The name pill goes over the thumbnails.
crate::edit_window::draw_clip_labels(painter, s, tl, lane, clip);
}
}
+174 -1
View File
@@ -69,11 +69,15 @@ pub fn key_fill(resp: &Response) -> Color32 {
if resp.hovered() { t.button_hi } else { t.button }
}
/// The [`shade`] of a pressed key's face, top and bottom of its gradient.
pub const PRESSED_SHADE: [f32; 2] = [-0.14, 0.02];
/// A key in `fill`, softly lit from above; it sinks while pressed.
pub fn key(p: &Painter, r: Rect, resp: &Response, fill: Color32) {
let face = r.shrink(1.0);
if resp.is_pointer_button_down_on() {
vgradient(p, face, shade(fill, -0.14), shade(fill, 0.02));
let [top, bottom] = PRESSED_SHADE;
vgradient(p, face, shade(fill, top), shade(fill, bottom));
} else {
p.rect_filled(face, 0.0, fill);
key_light(p, face);
@@ -424,6 +428,175 @@ pub fn db_text(db: f32) -> String {
if db <= -143.9 { "-inf".into() } else { format!("{db:.1}") }
}
/// Where a [`fine_slider`] drag is anchored: the pointer's x and the value there, whether the key
/// was held at the anchor, whether the drag moves the value from the anchor and not to the
/// pointer, and the pass that last drew the slider during the drag.
#[derive(Clone, Copy)]
struct FineDrag {
x: f32,
value: f64,
fine: bool,
relative: bool,
pass: u64,
}
/// A horizontal slider that moves a tenth as far for the same drag while Ctrl (Command on a Mac)
/// is held, as a [`fader`] does, on the rail and on the value box. Pressing or releasing the key
/// during a drag on the rail or the value box leaves the value where it is, and the drag goes on
/// from there. The value box shows the decimal places that a tenth of a point's step needs, and a
/// drag on the rail that the key has touched rounds the value to them.
pub fn fine_slider(ui: &mut Ui, value: &mut f64, range: std::ops::RangeInclusive<f64>, text: &str) -> Response {
FineSlider::new(value, range).text(text).show(ui)
}
/// A [`fine_slider`] with the options of `egui::Slider` that plug-in parameters use.
pub struct FineSlider<'a> {
value: &'a mut f64,
range: std::ops::RangeInclusive<f64>,
text: &'a str,
id_salt: Option<&'a str>,
show_value: bool,
logarithmic: bool,
integer: bool,
default: Option<f64>,
}
impl<'a> FineSlider<'a> {
pub fn new(value: &'a mut f64, range: std::ops::RangeInclusive<f64>) -> Self {
Self { value, range, text: "", id_salt: None, show_value: true, logarithmic: false, integer: false, default: None }
}
/// The value that an Alt-click (Option-click on a Mac) on the slider or its value box sets,
/// as a [`fader`] returns to 0 dB. While Alt is held, a drag leaves the value where it is, and
/// an Alt-click on the value box does not open it for typing.
pub fn default_value(mut self, default: f64) -> Self {
self.default = Some(default);
self
}
/// The label after the slider. It also tells sliders in the same `Ui` apart unless
/// [`Self::id_salt`] is set.
pub fn text(mut self, text: &'a str) -> Self {
self.text = text;
self
}
/// Tells sliders in the same `Ui` apart when they have no label.
pub fn id_salt(mut self, salt: &'a str) -> Self {
self.id_salt = Some(salt);
self
}
/// Whether the value box is shown. Without it, a drag that the key has touched rounds the
/// value only on a whole-number slider.
pub fn show_value(mut self, show: bool) -> Self {
self.show_value = show;
self
}
/// A logarithmic rail, as `egui::Slider::logarithmic`, for a range above zero. A range that
/// reaches zero or below stays linear.
pub fn logarithmic(mut self, logarithmic: bool) -> Self {
self.logarithmic = logarithmic;
self
}
/// Whole numbers only, as `egui::Slider::integer`.
pub fn integer(mut self) -> Self {
self.integer = true;
self
}
pub fn show(self, ui: &mut Ui) -> Response {
let Self { value, range, text, id_salt, show_value, logarithmic, integer, default } = self;
let id = ui.scope_id().with(("fine_slider", id_salt.unwrap_or(text)));
let before = *value;
let (fine, x) = ui.input(|i| (i.modifiers.command || i.modifiers.ctrl, i.pointer.interact_pos().map(|p| p.x)));
let reset = default.is_some() && ui.input(|i| i.modifiers.alt);
// A drag lasts through the pass that releases the button, where egui still sets the value
// from the pointer. An anchor that the previous pass did not store was left by an earlier
// drag, and is ignored.
let pass = ui.ctx().cumulative_pass_nr();
let mut drag = ui.data(|d| d.get_temp::<FineDrag>(id)).filter(|d| d.pass + 1 == pass);
let (lo, hi) = (*range.start(), *range.end());
let log = logarithmic && lo > 0.0 && hi > lo;
// The handle's travel along the rail, as `egui::Slider` lays it out.
let thickness = ui.text_style_height(&egui::TextStyle::Body).max(ui.spacing().interact_size.y);
let travel = f64::from((ui.spacing().slider_width - 2.0 * thickness / 2.5).max(1.0));
// Where a value sits along the rail (0 to 1), and back, as `egui::Slider` maps them.
let span = if log { (hi / lo).ln() } else { hi - lo };
let along = |v: f64| if span > 0.0 { if log { (v / lo).ln() / span } else { (v - lo) / span } } else { 0.0 };
let at = |n: f64| {
let n = n.clamp(0.0, 1.0);
if log { lo * (n * span).exp() } else { lo + n * span }
};
// How far the value moves for one point of the rail at `v`.
let per_point = |v: f64| if log { v * span / travel } else { span / travel };
let scale = if fine { 0.1 } else { 1.0 };
let decimals = if integer { 0 } else { (-(per_point(lo) * 0.1).log10()).ceil().clamp(0.0, 6.0) as usize };
if let (Some(d), Some(x)) = (&mut drag, x) {
if d.fine != fine || reset {
*d = FineDrag { x, value: *value, fine, relative: true, pass };
} else if d.relative {
let moved = at(along(d.value) + f64::from(x - d.x) / travel * scale);
let moved = if show_value || integer { egui::emath::round_to_decimals(moved, decimals) } else { moved };
*value = moved.clamp(lo, hi);
}
}
let hold = reset || drag.is_some_and(|d| d.relative);
let speed = per_point(*value) * scale;
let mut slider = egui::Slider::from_get_set(range, |v| {
if let Some(v) = v
&& !hold
{
*value = v;
}
*value
})
.show_value(show_value)
.logarithmic(log)
.min_decimals(decimals)
.text(text);
if integer {
slider = slider.integer();
}
if fine {
slider = slider.drag_value_speed(speed);
}
let mut resp = ui.add(slider);
// A drag from the anchor set the value before the slider read it.
if *value != before {
resp.mark_changed();
}
// The rail senses drags only, so a click on it ends as a drag that the pointer did not
// move far enough to count as one.
let clicked = resp.clicked() || (resp.drag_stopped() && ui.input(|i| i.pointer.could_any_button_be_click()));
if let Some(d) = default
&& reset
&& clicked
{
let d = if integer { d.round() } else { d };
*value = d.clamp(lo, hi);
resp.surrender_focus();
resp.mark_changed();
}
let rail = Rect::from_min_size(resp.rect.min, vec2(ui.spacing().slider_width, resp.rect.height()));
let on_rail = resp.dragged() && ui.input(|i| i.pointer.press_origin()).is_some_and(|p| rail.contains(p));
let next = match (on_rail, drag, x) {
(true, Some(d), _) => Some(FineDrag { pass, ..d }),
(true, None, Some(x)) => Some(FineDrag { x, value: *value, fine, relative: fine, pass }),
_ => None,
};
ui.data_mut(|m| match next {
Some(d) => {
m.insert_temp(id, d);
}
None => m.remove::<FineDrag>(id),
});
resp
}
}
/// A speaker dot on the surround panner: label, square position (-1..1, y up = front), height layer.
pub struct PannerSpeaker {
pub label: &'static str,
+21
View File
@@ -39,6 +39,27 @@ fn bounce_from_dialog(h: &mut Harness<'_, SoundApp>, file: &Path) {
assert!(h.query_by_label("Bounce Mix").is_none());
}
#[test]
fn enter_on_a_focused_cancel_closes_the_dialog_and_starts_no_bounce() {
let dir = scratch("bounce-enter-cancel");
let file = dir.join("Mix.wav");
let mut h = harness();
let app = h.state_mut();
assert!(app.dialogs.open_for_command(&app.engine, "file.bounce_mix"));
if let Some(Dialog::Bounce { path, .. }) = &mut app.dialogs.open {
*path = file.to_string_lossy().into_owned();
}
h.run_steps(2);
h.get_by_label("Cancel").focus();
h.run_steps(2);
h.event(egui::Event::Key { key: egui::Key::Enter, physical_key: None, pressed: true, repeat: false, modifiers: egui::Modifiers::NONE });
h.run_steps(3);
assert!(h.state().dialogs.open.is_none());
assert!(h.state().bounce.is_none(), "Cancel started a bounce");
assert!(!file.exists());
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
fn a_bounce_renders_across_frames_with_progress_until_its_file_is_written() {
let dir = scratch("bounce-frames");
+17
View File
@@ -54,3 +54,20 @@ fn bounce_of_a_session_with_a_relative_path_starts_in_the_default_folder() {
other => panic!("{other:?}"),
}
}
/// A picked name that already says the format is written as picked: `Mix.aifc` is not turned
/// into `Mix.aif`, a file the save dialog never asked about.
#[test]
fn bounce_keeps_an_extension_that_names_the_format() {
use soundcraft_ui_egui::dialogs::bounce_path;
for (path, format, written) in [
("/Mixes/Mix.aifc", EncodeFormat::Aiff, "/Mixes/Mix.aifc"),
("/Mixes/Mix.AIFF", EncodeFormat::Aiff, "/Mixes/Mix.AIFF"),
("/Mixes/Mix.bwf", EncodeFormat::Wav, "/Mixes/Mix.bwf"),
("/Mixes/Mix.aifc", EncodeFormat::Flac, "/Mixes/Mix.flac"),
("/Mixes/Mix.wav", EncodeFormat::Aiff, "/Mixes/Mix.aif"),
("/Mixes/Mix", EncodeFormat::Wav, "/Mixes/Mix.wav"),
] {
assert_eq!(bounce_path(path, format), PathBuf::from(written), "{path} as {format:?}");
}
}
+34
View File
@@ -146,6 +146,40 @@ fn spot_drags_ask_where_before_moving() {
assert!((starts(&app, &ids)[0] - 2 * SEC).abs() < 4000);
}
#[test]
fn a_spot_dialog_that_replaces_an_open_dialog_ignores_the_enter_of_its_first_frame() {
let (mut app, ctx, t, ids) = app("spot", "grabber", &[(SEC, SEC)]);
assert!(app.dialogs.open_for_command(&app.engine, "setup.session"));
frame(&ctx, &mut app, vec![]);
frame(&ctx, &mut app, vec![]);
let (from, to) = (point(&app, t, SEC * 3 / 2), point(&app, t, SEC * 5 / 2));
frame(&ctx, &mut app, vec![Event::PointerMoved(from)]);
frame(&ctx, &mut app, vec![button(from, true)]);
let started = from + vec2(7.0, 0.0);
for k in 0..=5 {
frame(&ctx, &mut app, vec![Event::PointerMoved(started + (to - started) * (k as f32 / 5.0))]);
}
frame(&ctx, &mut app, vec![button(to, false), key_event(Key::Enter, true, Modifiers::NONE)]);
assert_eq!(app.dialogs.open_name(), Some("spot"), "the Enter of the Spot dialog's first frame leaves it open");
assert_eq!(starts(&app, &ids), vec![SEC]);
}
#[test]
fn a_spot_dialog_that_a_click_opens_over_an_open_dialog_ignores_the_enter_of_its_first_frame() {
for (tool, at) in [("grabber", SEC * 3 / 2), ("trim", SEC + SEC / 8)] {
let (mut app, ctx, t, ids) = app("spot", tool, &[(SEC, SEC)]);
assert!(app.dialogs.open_for_command(&app.engine, "setup.session"));
frame(&ctx, &mut app, vec![]);
frame(&ctx, &mut app, vec![]);
let at = point(&app, t, at);
frame(&ctx, &mut app, vec![Event::PointerMoved(at)]);
frame(&ctx, &mut app, vec![button(at, true)]);
frame(&ctx, &mut app, vec![button(at, false), key_event(Key::Enter, true, Modifiers::NONE)]);
assert_eq!(app.dialogs.open_name(), Some("spot"), "{tool}: the Enter of the Spot dialog's first frame leaves it open");
assert_eq!(starts(&app, &ids), vec![SEC], "{tool}");
}
}
#[test]
fn slip_trims_stop_at_the_next_clip() {
let (mut app, ctx, t, ids) = app("slip", "trim", &[(0, SEC), (2 * SEC, SEC)]);
+215 -3
View File
@@ -1,12 +1,13 @@
use egui::{Event, Modifiers, PointerButton, Pos2, pos2, vec2};
use egui::{Event, Modifiers, PointerButton, Pos2, Rect, pos2, vec2};
use egui_kittest::Harness;
use egui_kittest::kittest::Queryable;
use serde_json::json;
use soundcraft_model::{Track, TrackId};
use soundcraft_model::{AutoParam, Track, TrackId};
use soundcraft_ui_egui::edit_window::HEADER_W;
use soundcraft_ui_egui::{MainWindow, Services, SoundApp};
use soundcraft_ui_egui::{Gesture, MainWindow, Services, SoundApp};
const CMD: Modifiers = Modifiers { mac_cmd: true, command: true, ..Modifiers::NONE };
const ALT: Modifiers = Modifiers { alt: true, ..Modifiers::NONE };
/// Feed one harness frame through the raw-input hook before stepping, so scripted input reaches the app.
fn step(h: &mut Harness<'_, SoundApp>) {
@@ -181,3 +182,214 @@ fn only_a_sends_fader_icon_opens_its_level() {
assert_eq!(h.state().ui.send_window, Some((snare, 0)));
assert!(h.query_by_label("DESTINATION").is_none());
}
/// The I/O column's "vol" and "pan" rows, for a track whose row is `row`.
fn vol_at(row: [f32; 4]) -> Pos2 {
pos2(row[0] + HEADER_W + 40.0, row[1] + 52.5)
}
fn pan_at(row: [f32; 4]) -> Pos2 {
pos2(row[0] + HEADER_W + 40.0, row[1] + 66.5)
}
fn undo_steps(h: &Harness<'_, SoundApp>) -> usize {
h.state().engine.undo_history().len()
}
fn track(h: &Harness<'_, SoundApp>, name: &str) -> Track {
h.state().engine.session().track_by_name(name).cloned().unwrap()
}
/// The open popup (the only foreground layer while one is open).
fn popup_rect(h: &Harness<'_, SoundApp>) -> Rect {
let rects: Vec<Rect> = h.ctx.memory(|m| {
m.areas().visible_layer_ids().into_iter().filter(|l| l.order == egui::Order::Foreground).filter_map(|l| m.area_rect(l.id)).collect()
});
assert_eq!(rects.len(), 1, "expected exactly one popup, found {}", rects.len());
rects[0]
}
/// Press at `from`, drag to `to` in four moves and release. `during` runs after each move, button down.
fn drag(h: &mut Harness<'_, SoundApp>, from: Pos2, to: Pos2, mut during: impl FnMut(&mut Harness<'_, SoundApp>)) {
let modifiers = Modifiers::NONE;
h.state_mut()
.synthetic
.extend([Event::PointerMoved(from), Event::PointerButton { pos: from, button: PointerButton::Primary, pressed: true, modifiers }]);
for _ in 0..6 {
step(h);
}
for i in 1..=4 {
h.state_mut().synthetic.push(Event::PointerMoved(from + (to - from) * (i as f32 / 4.0)));
for _ in 0..3 {
step(h);
}
during(h);
}
h.state_mut().synthetic.push(Event::PointerButton { pos: to, button: PointerButton::Primary, pressed: false, modifiers });
for _ in 0..6 {
step(h);
}
}
#[test]
fn alt_click_on_the_io_readouts_resets_volume_and_pan() {
let (mut h, id, row) = harness("Kick", "medium", "view.edit_io");
h.state_mut().run("mix.pan", json!({"track": id.0, "pan": 0.5})).unwrap();
assert!(kick(&h).mixer.volume_db != 0.0 && kick(&h).mixer.pan == [0.5]);
let before = undo_steps(&h);
press(&mut h, vol_at(row), PointerButton::Primary, ALT);
assert_eq!(kick(&h).mixer.volume_db, 0.0);
press(&mut h, pan_at(row), PointerButton::Primary, ALT);
assert_eq!(kick(&h).mixer.pan, [0.0]);
assert_eq!(undo_steps(&h), before + 2, "each reset is one undo step");
assert!(!egui::Popup::is_any_open(&h.ctx), "an Alt-click opened a popup instead of resetting");
}
#[test]
fn alt_click_on_a_stereo_tracks_pan_resets_both_panners_in_one_step() {
let (mut h, id, row) = harness("Pad", "medium", "view.edit_io");
for (index, pan) in [(0, -0.25), (1, 0.75)] {
h.state_mut().run("mix.pan", json!({"track": id.0, "pan": pan, "index": index})).unwrap();
}
assert_eq!(track(&h, "Pad").mixer.pan, [-0.25, 0.75]);
let before = undo_steps(&h);
press(&mut h, pan_at(row), PointerButton::Primary, ALT);
assert_eq!(track(&h, "Pad").mixer.pan, [-1.0, 1.0], "a stereo track goes back to hard left and right, like a new one");
assert_eq!(undo_steps(&h), before + 1);
assert!(!egui::Popup::is_any_open(&h.ctx));
}
#[test]
fn clicking_the_volume_readout_opens_a_fader_whose_drag_is_one_undo_step() {
let (mut h, id, _) = harness("Kick", "medium", "view.edit_io");
let db = kick(&h).mixer.volume_db;
let before = undo_steps(&h);
assert!(!egui::Popup::is_any_open(&h.ctx));
// The readout is a button named after its track and its value.
let readout = h.get_by_label("Kick volume -3.0").rect().center();
click(&mut h, readout);
assert!(egui::Popup::is_any_open(&h.ctx), "the click didn't open the fader");
assert_eq!(kick(&h).mixer.volume_db, db, "opening the fader changed the volume");
// The popup floats over the timeline: a drag in it must not also select or move anything there.
let edit = h.state().engine.session().edit.clone();
// The fader fills the bottom of the popup and moves by drag distance, so any point on it will do.
let popup = popup_rect(&h);
let from = pos2(popup.center().x, popup.max.y - 50.0);
let mut gestures = 0;
drag(&mut h, from, from - vec2(0.0, 60.0), |h| {
assert!(matches!(h.state().gesture, Some(Gesture::Fader { track, .. }) if track == id), "the drag isn't a Fader gesture");
gestures += 1;
});
assert_eq!(gestures, 4);
assert!(kick(&h).mixer.volume_db > db, "dragging up didn't raise the volume");
assert_eq!(undo_steps(&h), before + 1, "a whole drag is one undo step");
let after = h.state().engine.session().edit.clone();
assert!(after.selection == edit.selection && after.selected_tracks == edit.selected_tracks, "the drag leaked through to the timeline");
assert!(h.state().gesture.is_none(), "the gesture outlived the drag");
assert!(egui::Popup::is_any_open(&h.ctx), "releasing the fader closed it");
h.state_mut().engine.undo();
assert_eq!(kick(&h).mixer.volume_db, db, "one undo should restore the volume from before the drag");
click(&mut h, pos2(800.0, 990.0));
assert!(!egui::Popup::is_any_open(&h.ctx), "a click outside left the fader open");
}
#[test]
fn the_popup_fader_writes_touch_automation() {
let (mut h, id, row) = harness("Kick", "medium", "view.edit_io");
h.state_mut().run("mix.automation_mode", json!({"track": id.0, "mode": "touch"})).unwrap();
click(&mut h, vol_at(row));
let popup = popup_rect(&h);
let from = pos2(popup.center().x, popup.max.y - 50.0);
h.state_mut().engine.transport.playing = true;
drag(&mut h, from, from - vec2(0.0, 60.0), |h| {
// Touch writes at the playhead, which has to keep moving for a new point to be due.
h.state_mut().engine.transport.position += 4800;
});
h.state_mut().engine.transport.playing = false;
step(&mut h);
let points = kick(&h).lane(&AutoParam::Volume).map_or(0, |lane| lane.points.len());
assert!(points > 0, "dragging the popup fader in Touch mode wrote no automation");
}
#[test]
fn clicking_the_pan_readout_opens_one_slider_on_mono_and_two_on_stereo() {
for (name, panners) in [("Kick", 1), ("Pad", 2)] {
let (mut h, _, row) = harness(name, "medium", "view.edit_io");
let before = undo_steps(&h);
click(&mut h, pan_at(row));
assert!(egui::Popup::is_any_open(&h.ctx), "{name}: the click didn't open the sliders");
let sliders: Vec<_> = h.query_all_by_role(egui::accesskit::Role::Slider).collect();
assert_eq!(sliders.len(), panners, "{name}: wrong number of pan sliders");
assert_eq!(undo_steps(&h), before, "{name}: opening the sliders changed the session");
// Drag the last panner's slider (R on stereo) by 30 points: a slider follows the pointer.
let slider = sliders.last().unwrap().rect();
let (from, to) = (slider.center(), slider.center() + vec2(30.0, 0.0));
let index = panners - 1;
let pan = track(&h, name).mixer.pan.clone();
drag(&mut h, from, to, |_| {});
let moved = track(&h, name).mixer.pan;
assert_eq!(moved.len(), panners);
assert!(moved[index] != pan[index], "{name}: the slider didn't move the panner: {pan:?} -> {moved:?}");
assert_eq!(moved[..index], pan[..index], "{name}: moving R touched L");
assert_eq!(undo_steps(&h), before + 1, "{name}: a slider drag is one undo step");
}
}
#[test]
fn a_value_typed_in_the_fader_popup_sets_the_volume() {
let (mut h, _, row) = harness("Kick", "medium", "view.edit_io");
click(&mut h, vol_at(row));
let popup = popup_rect(&h);
click(&mut h, pos2(popup.center().x, popup.min.y + 14.0));
let key = |key, modifiers| {
[
Event::Key { key, physical_key: None, pressed: true, repeat: false, modifiers },
Event::Key { key, physical_key: None, pressed: false, repeat: false, modifiers },
]
};
h.state_mut().synthetic.extend(key(egui::Key::A, CMD));
h.state_mut().synthetic.push(Event::Text("-12".into()));
h.state_mut().synthetic.extend(key(egui::Key::Enter, Modifiers::NONE));
for _ in 0..10 {
step(&mut h);
}
assert_eq!(kick(&h).mixer.volume_db, -12.0);
}
#[test]
fn opening_the_pan_popup_leaves_a_pan_off_the_percent_grid_alone() {
let (mut h, id, row) = harness("Kick", "medium", "view.edit_io");
h.state_mut().run("mix.pan", json!({"track": id.0, "pan": 0.3337})).unwrap();
let before = undo_steps(&h);
click(&mut h, pan_at(row));
assert!(egui::Popup::is_any_open(&h.ctx));
assert_eq!(kick(&h).mixer.pan, [0.3337], "opening the sliders rewrote the pan");
assert_eq!(undo_steps(&h), before, "opening the sliders left an undo step");
assert!(h.query_by_role_and_label(egui::accesskit::Role::Slider, "Pan").is_some(), "the lone pan slider has no name");
}
#[test]
fn a_track_on_a_surround_output_keeps_a_text_only_pan() {
let (mut h, id, row) = harness("Kick", "medium", "view.edit_io");
h.state_mut().run("setup.main_format", json!({"format": "5.1"})).unwrap();
h.state_mut().run("mix.pan", json!({"track": id.0, "pan": 0.5})).unwrap();
for _ in 0..4 {
step(&mut h);
}
let before = undo_steps(&h);
click(&mut h, pan_at(row));
assert!(!egui::Popup::is_any_open(&h.ctx), "the Mix window pans this track with a surround panner, not these sliders");
press(&mut h, pan_at(row), PointerButton::Primary, ALT);
assert_eq!(kick(&h).mixer.pan, [0.5]);
assert_eq!(undo_steps(&h), before);
}
@@ -0,0 +1,298 @@
//! Enter on a focused dialog button, checkbox, radio button or combo box acts on that widget and does not press the
//! dialog's default button.
use std::path::PathBuf;
use craft_audio::EncodeFormat;
use egui::accesskit::Role;
use egui::epaint::ClippedShape;
use egui::{Event, Key, Modifiers, PointerButton, Pos2, RawInput, Rect, Shape, pos2, vec2};
use egui_kittest::{Harness, kittest::Queryable};
use serde_json::json;
use soundcraft_model::GroupId;
use soundcraft_ui_egui::dialogs::{Dialog, ask_to_save};
use soundcraft_ui_egui::{Services, SoundApp, menus};
fn harness(app: SoundApp) -> Harness<'static, SoundApp> {
let mut h = Harness::builder().with_size(vec2(1600.0, 1000.0)).build_ui_state(
|ui, app: &mut SoundApp| {
let ctx = ui.ctx().clone();
app.logic(&ctx);
app.ui(ui);
},
app,
);
h.run_steps(3);
h
}
/// Gives the button the keyboard focus, then presses Enter.
fn enter_on(h: &mut Harness<'_, SoundApp>, button: &str) {
enter_on_widget(h, Role::Button, button);
}
/// Gives the widget the keyboard focus, then presses Enter.
fn enter_on_widget(h: &mut Harness<'_, SoundApp>, role: Role, label: &str) {
h.get_by_role_and_label(role, label).focus();
h.run_steps(2);
h.event(Event::Key { key: Key::Enter, physical_key: None, pressed: true, repeat: false, modifiers: Modifiers::NONE });
h.run_steps(1);
h.event(Event::Key { key: Key::Enter, physical_key: None, pressed: false, repeat: false, modifiers: Modifiers::NONE });
h.run_steps(2);
}
#[test]
fn enter_on_a_focused_cancel_closes_the_dialog_without_its_action() {
let mut app = SoundApp::new(soundcraft_engine::demo::demo_engine(), None, Services::default());
let groups = app.engine.session().groups.len();
menus::invoke_menu(&mut app, "track.group", "Track > Group...");
let mut h = harness(app);
assert_eq!(h.state().dialogs.open_name(), Some("group"));
enter_on(&mut h, "Cancel");
assert_eq!(h.state().dialogs.open_name(), None);
assert_eq!(h.state().engine.session().groups.len(), groups);
}
#[test]
fn enter_on_a_focused_dont_save_closes_without_saving() {
let mut app = SoundApp::new(soundcraft_engine::demo::demo_engine(), None, Services::default());
app.run("mix.mute", json!({"track": "Kick"})).unwrap();
menus::invoke_menu(&mut app, "session.close", "File > Close Session");
let mut h = harness(app);
assert_eq!(h.state().dialogs.open_name(), Some("save_changes"));
enter_on(&mut h, "Don't Save");
assert_eq!(h.state().dialogs.open_name(), None, "no Save As prompt");
assert!(h.state().engine.session().tracks.is_empty());
}
#[test]
fn enter_on_a_focused_browse_browses_without_pressing_ok() {
let picked = "picked/Song.scraft";
let services = Services { pick_open: Some(Box::new(move |_, _| Some(PathBuf::from(picked)))), ..Services::default() };
let mut app = SoundApp::new(soundcraft_engine::demo::demo_engine(), None, services);
menus::invoke_menu(&mut app, "session.open", "File > Open Session...");
let mut h = harness(app);
enter_on(&mut h, "Browse…");
assert!(matches!(&h.state().dialogs.open, Some(Dialog::PathPrompt { path, .. }) if path == picked));
assert!(h.state().engine.session().track_by_name("Kick").is_some(), "the demo session is still open");
}
#[test]
fn enter_on_a_focused_browse_in_bounce_mix_does_not_bounce() {
let picked = "picked/Mix.flac";
let services = Services { pick_save: Some(Box::new(move |_, _| Some(PathBuf::from(picked)))), ..Services::default() };
let mut app = SoundApp::new(soundcraft_engine::demo::demo_engine(), None, services);
menus::invoke_menu(&mut app, "file.bounce_mix", "File > Bounce Mix...");
let mut h = harness(app);
enter_on(&mut h, "Browse…");
assert!(h.state().bounce.is_none(), "Enter on Browse started a bounce");
assert!(matches!(&h.state().dialogs.open, Some(Dialog::Bounce { path, format, .. }) if path == picked && *format == EncodeFormat::Flac));
}
#[test]
fn enter_on_a_focused_default_button_runs_its_action_once() {
let mut app = SoundApp::new(soundcraft_engine::demo::demo_engine(), None, Services::default());
let tracks = app.engine.session().tracks.len();
assert!(app.dialogs.open_for_command(&app.engine, "track.new"));
let mut h = harness(app);
enter_on(&mut h, "Create");
assert_eq!(h.state().dialogs.open_name(), None);
assert_eq!(h.state().engine.session().tracks.len(), tracks + 1);
}
#[test]
fn enter_on_a_focused_checkbox_toggles_it_and_keeps_the_dialog_open() {
let mut app = SoundApp::new(soundcraft_engine::demo::demo_engine(), None, Services::default());
menus::invoke_menu(&mut app, "file.score_setup", "File > Score Setup...");
let mut h = harness(app);
let Some(Dialog::ScoreSetup { show_track_names: before, .. }) = h.state().dialogs.open.clone() else { panic!("Score Setup did not open") };
enter_on_widget(&mut h, Role::CheckBox, "Show track names");
assert!(matches!(&h.state().dialogs.open, Some(Dialog::ScoreSetup { show_track_names, .. }) if *show_track_names != before));
}
#[test]
fn enter_on_a_focused_radio_button_picks_it_and_keeps_the_dialog_open() {
let mut app = SoundApp::new(soundcraft_engine::demo::demo_engine(), None, Services::default());
menus::invoke_menu(&mut app, "edit.fades_create", "Edit > Fades > Create...");
let mut h = harness(app);
assert_eq!(h.state().dialogs.open_name(), Some("fades"));
enter_on_widget(&mut h, Role::RadioButton, "Linear");
assert!(matches!(&h.state().dialogs.open, Some(Dialog::Fades { shape }) if shape == "linear"));
}
#[test]
fn enter_with_nothing_focused_presses_the_default_button() {
let mut app = SoundApp::new(soundcraft_engine::demo::demo_engine(), None, Services::default());
menus::invoke_menu(&mut app, "edit.fades_create", "Edit > Fades > Create...");
let mut h = harness(app);
assert_eq!(h.state().dialogs.open_name(), Some("fades"));
h.event(Event::Key { key: Key::Enter, physical_key: None, pressed: true, repeat: false, modifiers: Modifiers::NONE });
h.run_steps(2);
assert_eq!(h.state().dialogs.open_name(), None);
}
#[test]
fn a_dialog_opened_while_a_window_button_has_the_focus_takes_enter() {
let app = SoundApp::new(soundcraft_engine::demo::demo_engine(), None, Services::default());
let mut h = harness(app);
h.get_all_by_role(Role::Button).next().expect("the window has a button").focus();
h.run_steps(2);
assert!(h.ctx.memory(|m| m.focused().is_some()));
menus::invoke_menu(h.state_mut(), "edit.fades_create", "Edit > Fades > Create...");
h.run_steps(2);
assert_eq!(h.state().dialogs.open_name(), Some("fades"));
h.event(Event::Key { key: Key::Enter, physical_key: None, pressed: true, repeat: false, modifiers: Modifiers::NONE });
h.run_steps(2);
assert_eq!(h.state().dialogs.open_name(), None, "Enter pressed Create");
}
#[test]
fn the_enter_that_opens_a_dialog_does_not_press_its_default_button() {
let mut app = SoundApp::new(soundcraft_engine::demo::demo_engine(), None, Services::default());
let ctx = egui::Context::default();
run_frame(&ctx, &mut app, vec![]);
menus::invoke_menu(&mut app, "edit.fades_create", "Edit > Fades > Create...");
run_frame(&ctx, &mut app, vec![enter_key(false), enter_up()]);
assert_eq!(app.dialogs.open_name(), Some("fades"), "the opening Enter leaves the dialog open");
run_frame(&ctx, &mut app, vec![enter_key(false)]);
assert_eq!(app.dialogs.open_name(), None, "the next Enter presses Create");
}
/// Runs one frame of `app` with `events` and returns what it painted.
fn run_frame(ctx: &egui::Context, app: &mut SoundApp, events: Vec<Event>) -> Vec<ClippedShape> {
let raw = RawInput { screen_rect: Some(Rect::from_min_size(Pos2::ZERO, vec2(1600.0, 1000.0))), events, ..Default::default() };
let mut out = ctx.run_ui(raw, |ui| {
app.logic(ui.ctx());
app.ui(ui);
});
out.textures_delta.clear();
out.shapes
}
fn enter_key(repeat: bool) -> Event {
Event::Key { key: Key::Enter, physical_key: None, pressed: true, repeat, modifiers: Modifiers::NONE }
}
fn enter_up() -> Event {
Event::Key { key: Key::Enter, physical_key: None, pressed: false, repeat: false, modifiers: Modifiers::NONE }
}
#[test]
fn the_enter_that_replaces_an_open_dialog_does_not_press_the_new_default_button() {
let mut app = SoundApp::new(soundcraft_engine::demo::demo_engine(), None, Services::default());
let ctx = egui::Context::default();
menus::invoke_menu(&mut app, "setup.session", "Setup > Session");
run_frame(&ctx, &mut app, vec![]);
run_frame(&ctx, &mut app, vec![]);
assert_eq!(app.dialogs.open_name(), Some("session"));
menus::invoke_menu(&mut app, "edit.fades_create", "Edit > Fades > Create...");
run_frame(&ctx, &mut app, vec![enter_key(false), enter_up()]);
assert_eq!(app.dialogs.open_name(), Some("fades"), "the opening Enter leaves the new dialog open");
run_frame(&ctx, &mut app, vec![enter_key(false)]);
assert_eq!(app.dialogs.open_name(), None, "the next Enter presses Create");
}
#[test]
fn a_rename_dialog_that_replaces_an_open_dialog_ignores_the_enter_of_its_first_frame() {
let mut app = SoundApp::new(soundcraft_engine::demo::demo_engine(), None, Services::default());
let ctx = egui::Context::default();
menus::invoke_menu(&mut app, "setup.session", "Setup > Session");
run_frame(&ctx, &mut app, vec![]);
run_frame(&ctx, &mut app, vec![]);
assert_eq!(app.dialogs.open_name(), Some("session"));
let id = app.engine.session().tracks[0].id;
app.dialogs.open_rename_track(id, "Renamed");
run_frame(&ctx, &mut app, vec![enter_key(false), enter_up()]);
assert_eq!(app.dialogs.open_name(), Some("rename_track"), "the first frame's Enter leaves the new dialog open");
}
/// The first Kick clip of `app`.
fn kick_clip(app: &SoundApp) -> soundcraft_model::ClipId {
app.engine.session().track_by_name("Kick").unwrap().clips()[0].id
}
#[test]
fn clear_auto_rename_modify_group_and_save_changes_replacing_a_dialog_ignore_the_enter_of_their_first_frame() {
type Open = fn(&mut SoundApp);
let cases: [(&str, Open); 4] = [
("clear_clips", |app| {
let clip = kick_clip(app);
assert!(app.dialogs.open_clear(&app.engine, vec![clip], vec![]));
}),
("auto_rename", |app| {
let clip = kick_clip(app);
assert!(app.dialogs.open_auto_rename(&app.engine, vec![clip]));
}),
("modify_group", |app| {
let track = app.engine.session().tracks[0].id.0;
let group = app.run("track.group", json!({"tracks": [track], "name": "Drums"})).unwrap()["group"].as_u64().unwrap();
assert!(app.dialogs.open_modify_group(&app.engine, GroupId(group)));
}),
("save_changes", |app| {
app.run("mix.mute", json!({"track": "Kick"})).unwrap();
assert!(ask_to_save(app, "session.close", &json!({})));
}),
];
for (name, open) in cases {
let mut app = SoundApp::new(soundcraft_engine::demo::demo_engine(), None, Services::default());
let ctx = egui::Context::default();
menus::invoke_menu(&mut app, "setup.session", "Setup > Session");
run_frame(&ctx, &mut app, vec![]);
run_frame(&ctx, &mut app, vec![]);
open(&mut app);
run_frame(&ctx, &mut app, vec![enter_key(false), enter_up()]);
assert_eq!(app.dialogs.open_name(), Some(name), "the first frame's Enter leaves the new dialog open");
}
}
#[test]
fn a_tempo_change_dialog_that_replaces_an_open_dialog_ignores_the_enter_of_its_first_frame() {
let mut app = SoundApp::new(soundcraft_engine::demo::demo_engine(), None, Services::default());
let ctx = egui::Context::default();
menus::invoke_menu(&mut app, "setup.session", "Setup > Session");
run_frame(&ctx, &mut app, vec![]);
let shapes = run_frame(&ctx, &mut app, vec![]);
assert_eq!(app.dialogs.open_name(), Some("session"));
let [left, ..] = app.edit_layout.timeline;
let y = shapes
.iter()
.find_map(|c| match &c.shape {
Shape::Text(t) if t.galley.text() == "Tempo" && t.pos.x < left => Some(t.galley.rect.translate(t.pos.to_vec2()).center().y),
_ => None,
})
.expect("the Tempo ruler's label");
let at = pos2(left + 50.0, y);
let button = |pressed| Event::PointerButton { pos: at, button: PointerButton::Primary, pressed, modifiers: Modifiers::NONE };
for pressed in [true, false, true] {
run_frame(&ctx, &mut app, vec![Event::PointerMoved(at), button(pressed)]);
}
run_frame(&ctx, &mut app, vec![button(false), enter_key(false), enter_up()]);
assert_eq!(app.dialogs.open_name(), Some("tempo_change"), "the first frame's Enter leaves the new dialog open");
}
#[test]
fn a_held_enter_does_not_press_the_default_button_until_it_is_pressed_again() {
let mut app = SoundApp::new(soundcraft_engine::demo::demo_engine(), None, Services::default());
let ctx = egui::Context::default();
run_frame(&ctx, &mut app, vec![]);
menus::invoke_menu(&mut app, "edit.fades_create", "Edit > Fades > Create...");
run_frame(&ctx, &mut app, vec![enter_key(false)]);
run_frame(&ctx, &mut app, vec![enter_key(true)]);
run_frame(&ctx, &mut app, vec![enter_key(true)]);
assert_eq!(app.dialogs.open_name(), Some("fades"), "the held Enter's repeats leave the dialog open");
run_frame(&ctx, &mut app, vec![enter_up()]);
run_frame(&ctx, &mut app, vec![enter_key(false)]);
assert_eq!(app.dialogs.open_name(), None, "a new press of Enter presses Create");
}
#[test]
fn enter_on_a_focused_combo_box_opens_it_and_keeps_the_dialog_open() {
let mut app = SoundApp::new(soundcraft_engine::demo::demo_engine(), None, Services::default());
menus::invoke_menu(&mut app, "setup.session", "Setup > Session");
let mut h = harness(app);
assert_eq!(h.state().dialogs.open_name(), Some("session"));
assert!(h.query_by_label("47.952").is_none());
enter_on_widget(&mut h, Role::ComboBox, "Timecode Rate");
assert_eq!(h.state().dialogs.open_name(), Some("session"));
assert!(h.query_by_label("47.952").is_some(), "the Timecode Rate list is open");
}
+41
View File
@@ -0,0 +1,41 @@
//! Import Audio's Browse button lets the user select several files at once (#1089).
use egui::vec2;
use egui_kittest::{Harness, kittest::Queryable};
use soundcraft_ui_egui::{Services, SoundApp, dialogs::Dialog};
use std::path::PathBuf;
fn browse(picked: Vec<PathBuf>) -> Harness<'static, SoundApp> {
let pick_open_many: Box<dyn Fn(&str) -> Vec<PathBuf>> = Box::new(move |purpose| {
assert_eq!(purpose, "file.import_audio");
picked.clone()
});
let mut app = SoundApp::new(soundcraft_engine::Engine::default(), None, Services { pick_open_many: Some(pick_open_many), ..Default::default() });
assert!(app.dialogs.open_for_command(&app.engine, "file.import_audio"));
let mut h = Harness::builder().with_size(vec2(1600.0, 1000.0)).build_ui_state(
|ui, app: &mut SoundApp| {
app.logic(&ui.ctx().clone());
app.ui(ui);
},
app,
);
h.run_steps(3);
h.get_by_label("Browse…").click();
h.run_steps(1);
h
}
#[test]
fn browse_with_several_files_imports_them_and_closes_the_dialog() {
let h = browse(vec!["/nope/a.wav".into(), "/nope/b.wav".into()]);
assert!(h.state().dialogs.open.is_none());
assert!(h.state().imports.busy() || !h.state().ui.status.is_empty(), "the files were queued");
}
#[test]
fn browse_with_one_file_fills_the_path_field() {
let h = browse(vec!["/nope/a.wav".into()]);
match &h.state().dialogs.open {
Some(Dialog::PathPrompt { path, .. }) => assert_eq!(path, "/nope/a.wav"),
other => panic!("{other:?}"),
}
}
+5
View File
@@ -18,9 +18,11 @@ mod docking_workflows;
mod edit_menu;
mod edit_modes;
mod edit_window_headers;
mod focused_dialog_buttons;
mod group_dialog;
mod hide_floating;
mod idle;
mod import_browse;
mod key_families;
mod long_menu_scroll;
mod marker_rename;
@@ -35,6 +37,8 @@ mod mix_separate;
mod new_midi_clip;
mod new_tracks;
mod open_on_insert;
mod plugin_default_reset;
mod plugin_fine_sliders;
mod plugin_window;
mod pro_tools_keys;
mod record_hint;
@@ -52,6 +56,7 @@ mod side_lists_text;
mod strip_silence_dialog;
mod strip_silence_rectangles;
mod strip_silence_rename;
mod strip_silence_sliders;
mod sub_counter;
mod toolbar_scroll;
mod toolbar_tempo;
@@ -0,0 +1,347 @@
use egui::accesskit::Role;
use egui::{Event, Id, Modifiers, PointerButton, Pos2, Rect, vec2};
use egui_kittest::{Harness, kittest::Queryable};
use serde_json::json;
use soundcraft_dsp::ParamInfo;
use soundcraft_model::TrackId;
use soundcraft_ui_egui::{Services, SoundApp};
use std::collections::BTreeMap;
fn harness(app: SoundApp) -> Harness<'static, SoundApp> {
let mut h = Harness::builder().with_size(vec2(1600.0, 1000.0)).build_ui_state(
|ui, app: &mut SoundApp| {
let ctx = ui.ctx().clone();
app.logic(&ctx);
app.ui(ui);
},
app,
);
h.run_steps(4);
h
}
fn hold(h: &mut Harness<'_, SoundApp>, modifiers: Modifiers) {
h.event(Event::ModifiersChanged(modifiers));
h.run_steps(1);
}
fn press(h: &mut Harness<'_, SoundApp>, pos: Pos2, pressed: bool, modifiers: Modifiers) {
h.event(Event::PointerButton { pos, button: PointerButton::Primary, pressed, modifiers });
h.run_steps(1);
}
fn move_to(h: &mut Harness<'_, SoundApp>, pos: Pos2) {
h.event(Event::PointerMoved(pos));
h.run_steps(1);
}
/// Presses at `at` with Alt held, moves `dx` points right in six steps, and releases; `dx` of 0
/// is an Alt-click.
fn alt_press(h: &mut Harness<'_, SoundApp>, at: Pos2, dx: f32) {
hold(h, Modifiers::ALT);
move_to(h, at);
press(h, at, true, Modifiers::ALT);
for i in 1..=6 {
if dx != 0.0 {
move_to(h, at + vec2(dx * i as f32 / 6.0, 0.0));
}
}
press(h, at + vec2(dx, 0.0), false, Modifiers::ALT);
hold(h, Modifiers::NONE);
h.run_steps(2);
}
/// A point near the left end of `rail`, where a plain click would set a value far from the
/// parameter's default.
fn near_left(rail: Rect) -> Pos2 {
rail.left_center() + vec2(3.0, 0.0)
}
/// The rails of the unlabeled sliders inside the window with id `window`, top to bottom.
fn window_sliders(h: &Harness<'_, SoundApp>, window: Id) -> Vec<Rect> {
let area = h.ctx.memory(|m| m.area_rect(window)).unwrap();
let mut rails: Vec<Rect> = h.query_all_by_role(Role::Slider).map(|n| n.rect()).filter(|r| area.contains_rect(*r)).collect();
rails.sort_by(|a, b| a.top().total_cmp(&b.top()));
rails
}
fn param(plugin: &str, id: &str) -> &'static ParamInfo {
soundcraft_dsp::plugin_info(plugin).unwrap().params.iter().find(|p| p.id == id).unwrap()
}
fn insert_param(h: &Harness<'_, SoundApp>, track: TrackId, slot: usize, p: &ParamInfo) -> f32 {
let tr = h.state().engine.session().track(track).unwrap();
let ins = if slot == soundcraft_mix::INSTRUMENT_SLOT { tr.instrument.as_ref() } else { tr.mixer.inserts[slot].as_ref() };
ins.unwrap().params.get(p.id).copied().unwrap_or(p.default)
}
/// The center of the value box on the same row as `rail`, to the rail's right.
fn value_box(h: &Harness<'_, SoundApp>, rail: Rect) -> Pos2 {
h.query_all_by_role(Role::SpinButton)
.map(|n| n.rect())
.find(|r| r.left() > rail.right() && (r.center().y - rail.center().y).abs() < 4.0)
.unwrap()
.center()
}
/// A plain click at `at`, which sets a value from the pointer.
fn plain_click(h: &mut Harness<'_, SoundApp>, at: Pos2) {
move_to(h, at);
press(h, at, true, Modifiers::NONE);
press(h, at, false, Modifiers::NONE);
}
fn undo_steps(h: &Harness<'_, SoundApp>) -> usize {
h.state().engine.undo_history().len()
}
/// Kick's EQ 7-Band window, with Input at 6 dB and HPF Slope on its last choice.
fn kick_eq() -> (Harness<'static, SoundApp>, TrackId) {
let mut app = SoundApp::new(soundcraft_engine::demo::demo_engine(), None, Services::default());
let kick = app.engine.session().track_by_name("Kick").unwrap().id;
let slope = param("eq_7band", "hpf_slope");
app.run("mix.insert_param", json!({"track": kick.0, "slot": 0, "param": "input_gain", "value": 6.0})).unwrap();
app.run("mix.insert_param", json!({"track": kick.0, "slot": 0, "param": "hpf_slope", "value": slope.max})).unwrap();
app.engine.end_merge();
app.ui.plugin_windows = vec![(kick, 0)];
(harness(app), kick)
}
#[test]
fn alt_click_returns_a_plugin_window_slider_to_its_default_in_one_undo_step() {
let (mut h, kick) = kick_eq();
let input = param("eq_7band", "input_gain");
assert_ne!(input.default, 6.0);
let rail = window_sliders(&h, Id::unique(("plugin", kick.0, 0usize)))[0];
let steps = undo_steps(&h);
alt_press(&mut h, near_left(rail), 0.0);
assert_eq!(insert_param(&h, kick, 0, input), input.default, "Alt-click did not return Input to its default");
assert_eq!(undo_steps(&h), steps + 1, "Alt-click is not one undo step");
h.state_mut().engine.execute("edit.undo", &json!({})).unwrap();
assert_eq!(insert_param(&h, kick, 0, input), 6.0, "Undo did not bring Input back");
}
#[test]
fn alt_drag_leaves_a_plugin_window_slider_where_it_is() {
let (mut h, kick) = kick_eq();
let input = param("eq_7band", "input_gain");
let rail = window_sliders(&h, Id::unique(("plugin", kick.0, 0usize)))[0];
let steps = undo_steps(&h);
alt_press(&mut h, near_left(rail), 40.0);
assert_eq!(insert_param(&h, kick, 0, input), 6.0, "an Alt-drag moved Input");
assert_eq!(undo_steps(&h), steps, "an Alt-drag added an undo step");
}
#[test]
fn alt_click_returns_a_plugin_window_choice_to_its_default_without_opening_the_list() {
let (mut h, kick) = kick_eq();
let slope = param("eq_7band", "hpf_slope");
assert_ne!(slope.default, slope.max);
let row = h.get_by_label("HPF Slope").rect();
let combo = h.query_all_by_role(Role::ComboBox).map(|n| n.rect()).find(|r| (r.center().y - row.center().y).abs() < 4.0).unwrap();
let steps = undo_steps(&h);
alt_press(&mut h, combo.center(), 0.0);
assert_eq!(insert_param(&h, kick, 0, slope), slope.default, "Alt-click did not return HPF Slope to its default");
assert!(!egui::Popup::is_any_open(&h.ctx), "Alt-click opened the list");
assert_eq!(undo_steps(&h), steps + 1, "Alt-click is not one undo step");
h.state_mut().engine.execute("edit.undo", &json!({})).unwrap();
assert_eq!(insert_param(&h, kick, 0, slope), slope.max, "Undo did not bring HPF Slope back");
}
#[test]
fn alt_pressed_during_a_drag_holds_the_value_and_the_drag_goes_on_from_there() {
let (mut h, kick) = kick_eq();
let input = param("eq_7band", "input_gain");
let from = window_sliders(&h, Id::unique(("plugin", kick.0, 0usize)))[0].center();
move_to(&mut h, from);
press(&mut h, from, true, Modifiers::NONE);
let pressed = insert_param(&h, kick, 0, input);
move_to(&mut h, from + vec2(10.0, 0.0));
let before = insert_param(&h, kick, 0, input);
assert!(before > pressed, "a plain drag did not move Input");
hold(&mut h, Modifiers::ALT);
assert_eq!(insert_param(&h, kick, 0, input), before, "pressing Alt moved Input");
for i in 1..=6 {
move_to(&mut h, from + vec2(10.0 + 5.0 * i as f32, 0.0));
}
assert_eq!(insert_param(&h, kick, 0, input), before, "a drag with Alt held moved Input");
hold(&mut h, Modifiers::NONE);
assert_eq!(insert_param(&h, kick, 0, input), before, "releasing Alt moved Input to the pointer");
move_to(&mut h, from + vec2(50.0, 0.0));
let moved = insert_param(&h, kick, 0, input) - before;
press(&mut h, from + vec2(50.0, 0.0), false, Modifiers::NONE);
let ratio = moved / (before - pressed);
assert!((0.8..=1.2).contains(&ratio), "10 points after Alt moved Input {moved}, 10 points before it {}", before - pressed);
}
/// Kick's compressor window, with Attack away from its default.
fn kick_compressor_attack() -> (Harness<'static, SoundApp>, TrackId, f32) {
let mut app = SoundApp::new(soundcraft_engine::demo::demo_engine(), None, Services::default());
let kick = app.engine.session().track_by_name("Kick").unwrap().id;
let attack = param("compressor", "attack");
let away = (attack.default * 2.0).min(attack.max);
assert_ne!(away, attack.default);
assert!(attack.taper == soundcraft_dsp::Taper::Log, "Attack is no longer a log-taper parameter");
app.run("mix.insert_param", json!({"track": kick.0, "slot": 1, "param": "attack", "value": away})).unwrap();
app.engine.end_merge();
app.ui.plugin_windows = vec![(kick, 1)];
(harness(app), kick, away)
}
fn compressor_attack_rail(h: &Harness<'_, SoundApp>, kick: TrackId) -> Rect {
let params = soundcraft_dsp::plugin_info("compressor").unwrap().params;
let index = params.iter().filter(|p| p.choices.is_empty()).position(|p| p.id == "attack").unwrap();
window_sliders(h, Id::unique(("plugin", kick.0, 1usize)))[index]
}
#[test]
fn alt_click_returns_a_log_taper_slider_to_its_default() {
let (mut h, kick, _) = kick_compressor_attack();
let attack = param("compressor", "attack");
let rail = compressor_attack_rail(&h, kick);
alt_press(&mut h, near_left(rail), 0.0);
assert_eq!(insert_param(&h, kick, 1, attack), attack.default, "Alt-click did not return Attack to its default");
}
#[test]
fn alt_drag_leaves_a_log_taper_slider_where_it_is() {
let (mut h, kick, away) = kick_compressor_attack();
let attack = param("compressor", "attack");
let rail = compressor_attack_rail(&h, kick);
let steps = undo_steps(&h);
alt_press(&mut h, near_left(rail), 40.0);
assert_eq!(insert_param(&h, kick, 1, attack), away, "an Alt-drag moved Attack");
assert_eq!(undo_steps(&h), steps, "an Alt-drag added an undo step");
}
#[test]
fn alt_click_returns_a_whole_number_slider_to_its_default() {
let mut app = SoundApp::new(soundcraft_engine::demo::demo_engine(), None, Services::default());
let keys = app.engine.session().track_by_name("Keys").unwrap().id;
app.run("mix.instrument", json!({"track": keys.0, "plugin": "sampler"})).unwrap();
app.run("mix.instrument_param", json!({"track": keys.0, "param": "root_note", "value": 72.0})).unwrap();
app.engine.end_merge();
app.ui.plugin_windows = vec![(keys, soundcraft_mix::INSTRUMENT_SLOT)];
let mut h = harness(app);
let root = param("sampler", "root_note");
assert_ne!(root.default, 72.0);
let rail = window_sliders(&h, Id::unique(("plugin", keys.0, soundcraft_mix::INSTRUMENT_SLOT)))[0];
let steps = undo_steps(&h);
alt_press(&mut h, near_left(rail), 0.0);
let v = insert_param(&h, keys, soundcraft_mix::INSTRUMENT_SLOT, root);
assert_eq!(v, root.default, "Alt-click did not return Root Note to its default");
assert_eq!(v.fract(), 0.0, "Root Note is not a whole number after an Alt-click");
assert_eq!(undo_steps(&h), steps + 1, "Alt-click is not one undo step");
}
#[test]
fn alt_click_returns_clip_effects_sliders_and_value_boxes_to_their_defaults() {
let mut app = SoundApp::new(soundcraft_engine::demo::demo_engine(), None, Services::default());
let s = app.engine.session();
let clip = s.track_by_name("Kick").unwrap().clips().iter().find(|c| c.is_audio()).unwrap().id;
app.run("edit.select", json!({"clips": [clip.0]})).unwrap();
app.run("clip.effects_set", json!({"clips": [clip.0], "params": {"eq.input_gain": 6.0, "comp.threshold": -6.0}})).unwrap();
app.engine.end_merge();
app.ui.show_clip_effects = true;
let mut h = harness(app);
for (label, key, p, on_box) in
[("Input", "eq.input_gain", param("eq_7band", "input_gain"), false), ("Threshold", "comp.threshold", param("compressor", "threshold"), true)]
{
let key = format!("clip_fx.{}.{key}", clip.0);
let read = |h: &Harness<'_, SoundApp>| h.state().engine.session().edit.values.get(&key).copied();
assert_ne!(read(&h), Some(f64::from(p.default)));
let rail = h.get_by_role_and_label(Role::Slider, label).rect();
let at = if on_box { value_box(&h, rail) } else { near_left(rail) };
// A plain click on the value box gives it the keyboard focus for typing.
let focus = h.ctx.memory(|m| m.focused());
let steps = undo_steps(&h);
alt_press(&mut h, at, 0.0);
assert_eq!(read(&h), Some(f64::from(p.default)), "Alt-click did not return {label} to its default");
assert_eq!(undo_steps(&h), steps + 1, "Alt-click on {label} is not one undo step");
assert_eq!(h.ctx.memory(|m| m.focused()), focus, "Alt-click opened {label}'s value box for typing");
}
}
#[test]
fn fallback_audiosuite_sliders_reset_with_alt_click() {
for (process, label, id, default) in [
("normalize", "Peak dB", "target_db", -0.1f32),
("time_stretch", "Length ratio", "ratio", 1.0),
("pitch_shift", "Semitones", "semitones", 0.0),
] {
let mut app = SoundApp::new(soundcraft_engine::demo::demo_engine(), None, Services::default());
app.ui.audiosuite = Some(process.into());
let mut h = harness(app);
let read = |h: &Harness<'_, SoundApp>| -> BTreeMap<String, f32> {
h.ctx.data(|d| d.get_temp(Id::unique(("as_params", process.to_string())))).unwrap_or_default()
};
let rail = h.get_by_role_and_label(Role::Slider, label).rect();
move_to(&mut h, near_left(rail));
press(&mut h, near_left(rail), true, Modifiers::NONE);
press(&mut h, near_left(rail), false, Modifiers::NONE);
assert_ne!(read(&h)[id], default, "{process}: plain click did not move the value");
alt_press(&mut h, near_left(rail), 0.0);
assert_eq!(read(&h)[id], default, "{process}: Alt-click did not restore the default");
plain_click(&mut h, near_left(rail));
assert_ne!(read(&h)[id], default, "{process}: plain click did not move the value");
let focus = h.ctx.memory(|m| m.focused());
let at = value_box(&h, rail);
alt_press(&mut h, at, 0.0);
assert_eq!(read(&h)[id], default, "{process}: Alt-click on the value box did not restore the default");
assert_eq!(h.ctx.memory(|m| m.focused()), focus, "{process}: Alt-click opened the value box for typing");
}
}
#[test]
fn alt_click_returns_audiosuite_parameter_sliders_to_their_defaults() {
let mut app = SoundApp::new(soundcraft_engine::demo::demo_engine(), None, Services::default());
app.ui.audiosuite = Some("compressor".into());
let mut h = harness(app);
let read = |h: &Harness<'_, SoundApp>| -> BTreeMap<String, f32> {
h.ctx.data(|d| d.get_temp(Id::unique(("as_params", "compressor".to_string())))).unwrap_or_default()
};
let threshold = param("compressor", "threshold");
let rail = h.get_by_role_and_label(Role::Slider, "Threshold").rect();
move_to(&mut h, near_left(rail));
press(&mut h, near_left(rail), true, Modifiers::NONE);
press(&mut h, near_left(rail), false, Modifiers::NONE);
assert_ne!(read(&h)["threshold"], threshold.default, "a plain click did not move Threshold");
alt_press(&mut h, near_left(rail), 0.0);
assert_eq!(read(&h)["threshold"], threshold.default, "Alt-click did not return Threshold to its default");
plain_click(&mut h, near_left(rail));
assert_ne!(read(&h)["threshold"], threshold.default, "a plain click did not move Threshold");
let focus = h.ctx.memory(|m| m.focused());
let at = value_box(&h, rail);
alt_press(&mut h, at, 0.0);
assert_eq!(read(&h)["threshold"], threshold.default, "Alt-click on the value box did not return Threshold to its default");
assert_eq!(h.ctx.memory(|m| m.focused()), focus, "Alt-click opened Threshold's value box for typing");
}
#[test]
fn clip_effects_gain_resets_with_alt_click_and_undo_restores_the_value() {
let mut app = SoundApp::new(soundcraft_engine::demo::demo_engine(), None, Services::default());
let clip = app.engine.session().track_by_name("Kick").unwrap().clips().iter().find(|c| c.is_audio()).unwrap().id;
app.run("edit.select", json!({"clips": [clip.0]})).unwrap();
app.run("clip.effects_set", json!({"clips": [clip.0], "params": {"gain": 6.0}})).unwrap();
app.engine.end_merge();
app.ui.show_clip_effects = true;
let mut h = harness(app);
let key = format!("clip_fx.{}.gain", clip.0);
let input = h.get_by_role_and_label(Role::Slider, "Input").rect();
move_to(&mut h, input.center());
h.event(Event::MouseWheel {
unit: egui::MouseWheelUnit::Point,
delta: vec2(0.0, -2000.0),
modifiers: Modifiers::NONE,
phase: egui::TouchPhase::Move,
source: egui::MouseWheelSource::Unknown,
});
h.run_steps(30);
let rail = h.get_by_role_and_label(Role::Slider, "Clip effects gain (dB)").rect();
let steps = undo_steps(&h);
alt_press(&mut h, near_left(rail), 0.0);
assert_eq!(h.state().engine.session().edit.values.get(&key), Some(&0.0));
assert_eq!(undo_steps(&h), steps + 1);
h.state_mut().engine.execute("edit.undo", &json!({})).unwrap();
assert_eq!(h.state().engine.session().edit.values.get(&key), Some(&6.0));
}
@@ -0,0 +1,359 @@
use egui::accesskit::Role;
use egui::{Event, Id, Modifiers, PointerButton, Pos2, Rect, vec2};
use egui_kittest::{Harness, kittest::Queryable};
use serde_json::json;
use soundcraft_dsp::ParamInfo;
use soundcraft_model::TrackId;
use soundcraft_ui_egui::{Services, SoundApp};
use std::collections::BTreeMap;
fn harness(app: SoundApp) -> Harness<'static, SoundApp> {
let mut h = Harness::builder().with_size(vec2(1600.0, 1000.0)).build_ui_state(
|ui, app: &mut SoundApp| {
let ctx = ui.ctx().clone();
app.logic(&ctx);
app.ui(ui);
},
app,
);
h.run_steps(4);
h
}
fn hold(h: &mut Harness<'_, SoundApp>, modifiers: Modifiers) {
h.event(Event::ModifiersChanged(modifiers));
h.run_steps(1);
}
fn press(h: &mut Harness<'_, SoundApp>, pos: Pos2, pressed: bool, modifiers: Modifiers) {
h.event(Event::PointerButton { pos, button: PointerButton::Primary, pressed, modifiers });
h.run_steps(1);
}
fn move_to(h: &mut Harness<'_, SoundApp>, pos: Pos2) {
h.event(Event::PointerMoved(pos));
h.run_steps(1);
}
/// Presses the middle of `rail` and drags `dx` points right in six steps with `modifiers` held,
/// calling `read` after each step; returns how far `read` moved from where the press put it.
fn drag(h: &mut Harness<'_, SoundApp>, rail: Rect, dx: f32, modifiers: Modifiers, read: &dyn Fn(&Harness<'_, SoundApp>) -> f64) -> f64 {
hold(h, modifiers);
let from = rail.center();
move_to(h, from);
press(h, from, true, modifiers);
let start = read(h);
for i in 1..=6 {
move_to(h, from + vec2(dx * i as f32 / 6.0, 0.0));
read(h);
}
let moved = read(h) - start;
press(h, from + vec2(dx, 0.0), false, modifiers);
assert_eq!(read(h) - start, moved, "releasing the button moved the value");
hold(h, Modifiers::NONE);
moved
}
/// A plain 30-point drag and a Ctrl-drag of the same length on `rail`; returns how far each
/// moved `read`.
fn plain_and_fine(h: &mut Harness<'_, SoundApp>, rail: Rect, read: &dyn Fn(&Harness<'_, SoundApp>) -> f64) -> (f64, f64) {
let plain = drag(h, rail, 30.0, Modifiers::NONE, read);
let fine = drag(h, rail, 30.0, Modifiers::COMMAND, read);
(plain, fine)
}
fn assert_tenth(what: &str, (plain, fine): (f64, f64), within: std::ops::RangeInclusive<f64>) {
assert!(plain > 0.0, "{what}: a plain drag did not move the value");
assert!(within.contains(&(fine / plain)), "{what}: Ctrl-drag moved {fine}, a plain drag {plain}");
}
/// The value box on the same row as `rail`, to the rail's right.
fn value_box(h: &Harness<'_, SoundApp>, rail: Rect) -> Rect {
h.query_all_by_role(Role::SpinButton).map(|n| n.rect()).find(|r| r.left() > rail.right() && (r.center().y - rail.center().y).abs() < 4.0).unwrap()
}
/// The rails of the unlabeled sliders inside the window with id `window`, top to bottom.
fn window_sliders(h: &Harness<'_, SoundApp>, window: Id) -> Vec<Rect> {
let area = h.ctx.memory(|m| m.area_rect(window)).unwrap();
let mut rails: Vec<Rect> = h.query_all_by_role(Role::Slider).map(|n| n.rect()).filter(|r| area.contains_rect(*r)).collect();
rails.sort_by(|a, b| a.top().total_cmp(&b.top()));
rails
}
/// The plug-in's parameters that its window shows as sliders, in order.
fn slider_params(plugin: &str) -> Vec<&'static ParamInfo> {
soundcraft_dsp::plugin_info(plugin).unwrap().params.iter().filter(|p| p.choices.is_empty()).collect()
}
fn insert_param(h: &Harness<'_, SoundApp>, track: TrackId, slot: usize, p: &ParamInfo) -> f64 {
let tr = h.state().engine.session().track(track).unwrap();
let ins = if slot == soundcraft_mix::INSTRUMENT_SLOT { tr.instrument.as_ref() } else { tr.mixer.inserts[slot].as_ref() };
f64::from(ins.unwrap().params.get(p.id).copied().unwrap_or(p.default))
}
#[test]
fn plugin_window_sliders_move_a_tenth_as_far_with_ctrl_held() {
let mut app = SoundApp::new(soundcraft_engine::demo::demo_engine(), None, Services::default());
let kick = app.engine.session().track_by_name("Kick").unwrap().id;
app.ui.plugin_windows = vec![(kick, 0)];
let mut h = harness(app);
let rails = window_sliders(&h, Id::unique(("plugin", kick.0, 0usize)));
let params = slider_params("eq_7band");
assert_eq!((params[0].id, params[2].id), ("input_gain", "hpf_freq"));
for i in [0, 2] {
let p = params[i];
// Along the rail, so that a log-taper parameter compares as a linear one does.
let read = move |h: &Harness<'_, SoundApp>| f64::from(p.to_normalized(insert_param(h, kick, 0, p) as f32));
let others = |h: &Harness<'_, SoundApp>| params.iter().filter(|o| o.id != p.id).map(|o| insert_param(h, kick, 0, o)).collect::<Vec<_>>();
let before = others(&h);
assert_tenth(p.name, plain_and_fine(&mut h, rails[i], &read), 0.08..=0.12);
assert_eq!(others(&h), before, "dragging {} moved another parameter", p.name);
}
}
#[test]
fn the_control_key_alone_also_gives_a_fine_drag_on_a_plugin_slider() {
let mut app = SoundApp::new(soundcraft_engine::demo::demo_engine(), None, Services::default());
let kick = app.engine.session().track_by_name("Kick").unwrap().id;
app.ui.plugin_windows = vec![(kick, 0)];
let mut h = harness(app);
let rails = window_sliders(&h, Id::unique(("plugin", kick.0, 0usize)));
let p = slider_params("eq_7band")[0];
let read = move |h: &Harness<'_, SoundApp>| insert_param(h, kick, 0, p);
let plain = drag(&mut h, rails[0], 30.0, Modifiers::NONE, &read);
let fine = drag(&mut h, rails[0], 30.0, Modifiers::CTRL, &read);
assert_tenth(p.name, (plain, fine), 0.08..=0.12);
}
/// Presses `rail`'s middle, drags 10 points, presses Ctrl, drags 30 more, releases Ctrl and then
/// the button. Each change of the key and the release leave `read` where it was, and the 30
/// points move it less than `plain` moved for the same distance.
fn toggle_ctrl_during_a_drag(h: &mut Harness<'_, SoundApp>, what: &str, rail: Rect, plain: f64, read: &dyn Fn(&Harness<'_, SoundApp>) -> f64) {
let from = rail.center();
move_to(h, from);
press(h, from, true, Modifiers::NONE);
move_to(h, from + vec2(10.0, 0.0));
let before = read(h);
hold(h, Modifiers::COMMAND);
assert_eq!(read(h), before, "{what}: pressing Ctrl moved the value");
for i in 1..=6 {
move_to(h, from + vec2(10.0 + 5.0 * i as f32, 0.0));
read(h);
}
let held = read(h);
assert!(held > before && held - before < plain / 2.0, "{what}: 30 points with Ctrl moved {}, a plain drag {plain}", held - before);
hold(h, Modifiers::NONE);
assert_eq!(read(h), held, "{what}: releasing Ctrl moved the value");
press(h, from + vec2(40.0, 0.0), false, Modifiers::NONE);
assert_eq!(read(h), held, "{what}: releasing the button moved the value");
}
#[test]
fn pressing_and_releasing_ctrl_during_a_drag_keeps_the_value_of_a_logarithmic_slider() {
let mut app = SoundApp::new(soundcraft_engine::demo::demo_engine(), None, Services::default());
let kick = app.engine.session().track_by_name("Kick").unwrap().id;
app.ui.plugin_windows = vec![(kick, 0)];
let mut h = harness(app);
let rails = window_sliders(&h, Id::unique(("plugin", kick.0, 0usize)));
let p = slider_params("eq_7band")[2];
assert_eq!(p.id, "hpf_freq");
let read = move |h: &Harness<'_, SoundApp>| f64::from(p.to_normalized(insert_param(h, kick, 0, p) as f32));
let plain = drag(&mut h, rails[2], 30.0, Modifiers::NONE, &read);
toggle_ctrl_during_a_drag(&mut h, p.name, rails[2], plain, &read);
}
#[test]
fn pressing_and_releasing_ctrl_during_a_drag_keeps_a_whole_number_slider_whole() {
let mut app = SoundApp::new(soundcraft_engine::demo::demo_engine(), None, Services::default());
let keys = app.engine.session().track_by_name("Keys").unwrap().id;
app.run("mix.instrument", json!({"track": keys.0, "plugin": "sampler"})).unwrap();
app.ui.plugin_windows = vec![(keys, soundcraft_mix::INSTRUMENT_SLOT)];
let mut h = harness(app);
let rails = window_sliders(&h, Id::unique(("plugin", keys.0, soundcraft_mix::INSTRUMENT_SLOT)));
let root = slider_params("sampler")[0];
assert_eq!(root.id, "root_note");
let read = move |h: &Harness<'_, SoundApp>| {
let v = insert_param(h, keys, soundcraft_mix::INSTRUMENT_SLOT, root);
assert_eq!(v.fract(), 0.0, "Root Note is {v} during a drag");
v
};
let plain = drag(&mut h, rails[0], 30.0, Modifiers::NONE, &read);
toggle_ctrl_during_a_drag(&mut h, root.name, rails[0], plain, &read);
}
#[test]
fn a_small_ctrl_drag_at_the_low_end_of_a_log_slider_moves_the_value() {
let mut app = SoundApp::new(soundcraft_engine::demo::demo_engine(), None, Services::default());
let kick = app.engine.session().track_by_name("Kick").unwrap().id;
app.ui.plugin_windows = vec![(kick, 1)];
let mut h = harness(app);
let rails = window_sliders(&h, Id::unique(("plugin", kick.0, 1usize)));
let attack = slider_params("compressor")[2];
assert_eq!(attack.id, "attack");
let from = rails[2].left_center() + vec2(1.0, 0.0);
hold(&mut h, Modifiers::COMMAND);
move_to(&mut h, from);
press(&mut h, from, true, Modifiers::COMMAND);
let start = insert_param(&h, kick, 1, attack);
assert_eq!(start, f64::from(attack.min), "the press did not put Attack at its lowest value");
move_to(&mut h, from + vec2(1.0, 0.0));
let moved = insert_param(&h, kick, 1, attack);
press(&mut h, from + vec2(1.0, 0.0), false, Modifiers::COMMAND);
assert!(moved > start, "a one-point Ctrl-drag left Attack at {moved}");
assert_eq!(insert_param(&h, kick, 1, attack), moved, "releasing the button moved the value");
}
#[test]
fn a_small_ctrl_drag_at_the_low_end_of_a_log_slider_with_a_value_box_moves_the_value() {
let mut app = SoundApp::new(soundcraft_engine::demo::demo_engine(), None, Services::default());
let clip = app.engine.session().track_by_name("Kick").unwrap().clips().iter().find(|c| c.is_audio()).unwrap().id;
app.run("edit.select", json!({"clips": [clip.0]})).unwrap();
app.ui.show_clip_effects = true;
let mut h = harness(app);
let attack = slider_params("compressor")[2];
assert_eq!(attack.id, "attack");
let key = format!("clip_fx.{}.comp.attack", clip.0);
let from = h.get_by_role_and_label(Role::Slider, attack.name).rect().left_center() + vec2(1.0, 0.0);
hold(&mut h, Modifiers::COMMAND);
move_to(&mut h, from);
press(&mut h, from, true, Modifiers::COMMAND);
let read = |h: &Harness<'_, SoundApp>| h.state().engine.session().edit.values.get(&key).copied().unwrap();
let start = read(&h);
assert!((start - f64::from(attack.min)).abs() < 1e-6, "the press put Attack at {start}");
move_to(&mut h, from + vec2(1.0, 0.0));
let moved = read(&h);
press(&mut h, from + vec2(1.0, 0.0), false, Modifiers::COMMAND);
assert!(moved > start, "a one-point Ctrl-drag left Attack at {moved}");
}
#[test]
fn whole_number_instrument_sliders_move_a_tenth_as_far_and_stay_whole() {
let mut app = SoundApp::new(soundcraft_engine::demo::demo_engine(), None, Services::default());
let keys = app.engine.session().track_by_name("Keys").unwrap().id;
app.run("mix.instrument", json!({"track": keys.0, "plugin": "sampler"})).unwrap();
app.ui.plugin_windows = vec![(keys, soundcraft_mix::INSTRUMENT_SLOT)];
let mut h = harness(app);
let rails = window_sliders(&h, Id::unique(("plugin", keys.0, soundcraft_mix::INSTRUMENT_SLOT)));
let root = slider_params("sampler")[0];
assert_eq!(root.id, "root_note");
// Each read during the drags checks that the instrument was sent a whole number.
let read = move |h: &Harness<'_, SoundApp>| {
let v = insert_param(h, keys, soundcraft_mix::INSTRUMENT_SLOT, root);
assert_eq!(v.fract(), 0.0, "Root Note is {v} during a drag");
v
};
assert_tenth(root.name, plain_and_fine(&mut h, rails[0], &read), 0.06..=0.14);
}
#[test]
fn clip_effects_sliders_move_a_tenth_as_far_with_ctrl_held() {
let mut app = SoundApp::new(soundcraft_engine::demo::demo_engine(), None, Services::default());
let s = app.engine.session();
let clip = s.track_by_name("Kick").unwrap().clips().iter().find(|c| c.is_audio()).unwrap().id;
app.run("edit.select", json!({"clips": [clip.0]})).unwrap();
app.ui.show_clip_effects = true;
let mut h = harness(app);
for (label, key, default) in [("Input", "eq.input_gain", 0.0), ("Threshold", "comp.threshold", -20.0), ("Clip effects gain (dB)", "gain", 0.0)] {
if key == "gain" {
let input = h.get_by_role_and_label(Role::Slider, "Input").rect();
move_to(&mut h, input.center());
h.event(Event::MouseWheel {
unit: egui::MouseWheelUnit::Point,
delta: vec2(0.0, -2000.0),
modifiers: Modifiers::NONE,
phase: egui::TouchPhase::Move,
source: egui::MouseWheelSource::Unknown,
});
h.run_steps(30);
}
let key = format!("clip_fx.{}.{key}", clip.0);
let read = move |h: &Harness<'_, SoundApp>| h.state().engine.session().edit.values.get(&key).copied().unwrap_or(default);
let rail = h.get_by_role_and_label(Role::Slider, label).rect();
assert_tenth(label, plain_and_fine(&mut h, rail, &read), 0.08..=0.12);
}
}
#[test]
fn clip_effects_value_boxes_move_a_tenth_as_far_with_ctrl_held() {
let mut app = SoundApp::new(soundcraft_engine::demo::demo_engine(), None, Services::default());
let s = app.engine.session();
let clip = s.track_by_name("Kick").unwrap().clips().iter().find(|c| c.is_audio()).unwrap().id;
app.run("edit.select", json!({"clips": [clip.0]})).unwrap();
app.ui.show_clip_effects = true;
let mut h = harness(app);
for (label, key, default) in [("Input", "eq.input_gain", 0.0), ("Threshold", "comp.threshold", -20.0)] {
let key = format!("clip_fx.{}.{key}", clip.0);
let read = move |h: &Harness<'_, SoundApp>| h.state().engine.session().edit.values.get(&key).copied().unwrap_or(default);
let rail = h.get_by_role_and_label(Role::Slider, label).rect();
let boxed = value_box(&h, rail);
assert_tenth(label, plain_and_fine(&mut h, boxed, &read), 0.07..=0.13);
}
}
#[test]
fn clip_effects_log_taper_sliders_use_a_logarithmic_rail() {
let mut app = SoundApp::new(soundcraft_engine::demo::demo_engine(), None, Services::default());
let s = app.engine.session();
let clip = s.track_by_name("Kick").unwrap().clips().iter().find(|c| c.is_audio()).unwrap().id;
app.run("edit.select", json!({"clips": [clip.0]})).unwrap();
app.ui.show_clip_effects = true;
let mut h = harness(app);
let ratio = slider_params("compressor")[1];
assert_eq!(ratio.id, "ratio");
let rail = h.get_by_role_and_label(Role::Slider, ratio.name).rect();
move_to(&mut h, rail.center());
press(&mut h, rail.center(), true, Modifiers::NONE);
press(&mut h, rail.center(), false, Modifiers::NONE);
let key = format!("clip_fx.{}.comp.ratio", clip.0);
let v = h.state().engine.session().edit.values.get(&key).copied().unwrap();
// The middle of a 1 to 100 log rail is 10; a linear rail puts it near 50.
assert!((7.0..=14.0).contains(&v), "a click in the middle of the Ratio rail set {v}");
}
#[test]
fn fallback_audiosuite_sliders_move_a_tenth_as_far_with_ctrl_held() {
for (process, label, id) in
[("normalize", "Peak dB", "target_db"), ("time_stretch", "Length ratio", "ratio"), ("pitch_shift", "Semitones", "semitones")]
{
let mut app = SoundApp::new(soundcraft_engine::demo::demo_engine(), None, Services::default());
app.ui.audiosuite = Some(process.into());
let mut h = harness(app);
let read = |h: &Harness<'_, SoundApp>| {
let params: BTreeMap<String, f32> = h.ctx.data(|d| d.get_temp(Id::unique(("as_params", process.to_string())))).unwrap_or_default();
f64::from(params[id])
};
let rail = h.get_by_role_and_label(Role::Slider, label).rect();
assert_tenth(label, plain_and_fine(&mut h, rail, &read), 0.08..=0.12);
}
}
#[test]
fn audiosuite_parameter_sliders_move_a_tenth_as_far_with_ctrl_held() {
let mut app = SoundApp::new(soundcraft_engine::demo::demo_engine(), None, Services::default());
app.ui.audiosuite = Some("compressor".into());
let mut h = harness(app);
for (label, id) in [("Threshold", "threshold"), ("Knee", "knee")] {
let read = move |h: &Harness<'_, SoundApp>| {
let params: BTreeMap<String, f32> = h.ctx.data(|d| d.get_temp(Id::unique(("as_params", "compressor".to_string())))).unwrap_or_default();
f64::from(params[id])
};
let rail = h.get_by_role_and_label(Role::Slider, label).rect();
assert_tenth(label, plain_and_fine(&mut h, rail, &read), 0.08..=0.12);
}
}
#[test]
fn audiosuite_value_boxes_move_a_tenth_as_far_with_ctrl_held() {
let mut app = SoundApp::new(soundcraft_engine::demo::demo_engine(), None, Services::default());
app.ui.audiosuite = Some("compressor".into());
let mut h = harness(app);
for (label, id) in [("Threshold", "threshold"), ("Knee", "knee")] {
let read = move |h: &Harness<'_, SoundApp>| {
let params: BTreeMap<String, f32> = h.ctx.data(|d| d.get_temp(Id::unique(("as_params", "compressor".to_string())))).unwrap_or_default();
f64::from(params[id])
};
let rail = h.get_by_role_and_label(Role::Slider, label).rect();
let boxed = value_box(&h, rail);
assert_tenth(label, plain_and_fine(&mut h, boxed, &read), 0.07..=0.13);
}
}
+16
View File
@@ -96,6 +96,22 @@ fn save_in_the_question_saves_before_closing() {
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
fn enter_on_a_focused_cancel_neither_saves_nor_closes() {
let mut h = harness();
let tracks = h.state().engine.session().tracks.len();
mute_kick(&mut h);
close_session(&mut h);
assert_eq!(h.state().dialogs.open_name(), Some("save_changes"));
h.get_by_label("Cancel").focus();
steps(&mut h);
h.event(egui::Event::Key { key: egui::Key::Enter, physical_key: None, pressed: true, repeat: false, modifiers: egui::Modifiers::NONE });
steps(&mut h);
assert!(h.state().dialogs.open.is_none(), "no Save As dialog follows");
assert_eq!(h.state().engine.session().tracks.len(), tracks);
assert!(h.state().engine.is_dirty());
}
/// A session the browser read: an empty one, so opening it is easy to see.
fn session_bytes() -> Vec<u8> {
soundcraft_engine::Engine::default().session().to_json().unwrap().into_bytes()
@@ -80,6 +80,45 @@ fn enter_on_a_focused_separate_separates_and_does_not_strip() {
assert_eq!(covered(&separated), covered(&before), "Enter on Separate keeps all the audio");
}
#[test]
fn enter_on_a_focused_cancel_closes_the_dialog_and_strips_nothing() {
let (mut h, track, before) = strip_silence_dialog();
h.get_by_role_and_label(Role::Button, "Cancel").focus();
h.run_steps(2);
h.event(Event::Key { key: Key::Enter, physical_key: None, pressed: true, repeat: false, modifiers: Modifiers::NONE });
h.run_steps(2);
assert_eq!(h.state().dialogs.open_name(), None);
assert_eq!(clips(&h, track), before, "Cancel leaves the clips as they were");
}
#[test]
fn enter_right_after_switching_between_strip_silence_and_rename_presses_the_new_default_button() {
let (mut h, _, _) = strip_silence_dialog();
let enter = |h: &mut Harness<'static, SoundApp>| {
for pressed in [true, false] {
h.event(Event::Key { key: Key::Enter, physical_key: None, pressed, repeat: false, modifiers: Modifiers::NONE });
}
};
h.get_by_role_and_label(Role::Button, "Rename…").focus();
h.run_steps(2);
enter(&mut h);
h.run_steps(1);
assert_eq!(h.state().dialogs.open_name(), Some("strip_silence_rename"));
enter(&mut h);
h.run_steps(2);
assert_eq!(h.state().dialogs.open_name(), Some("strip_silence"), "the next Enter presses OK in Rename Selected Clips");
h.get_by_role_and_label(Role::Button, "Rename…").click();
h.run_steps(2);
h.get_by_role_and_label(Role::Button, "OK").focus();
h.run_steps(2);
enter(&mut h);
h.run_steps(1);
assert_eq!(h.state().dialogs.open_name(), Some("strip_silence"));
enter(&mut h);
h.run_steps(2);
assert_eq!(h.state().dialogs.open_name(), None, "the next Enter presses Strip in Strip Silence");
}
#[test]
fn strip_silence_strip_removes_the_silence() {
let (mut h, track, before) = strip_silence_dialog();
@@ -0,0 +1,223 @@
use egui::accesskit::Role;
use egui::{Event, Modifiers, PointerButton, Pos2, vec2};
use egui_kittest::{Harness, kittest::Queryable};
use serde_json::json;
use soundcraft_ui_egui::dialogs::Dialog;
use soundcraft_ui_egui::{Services, SoundApp, menus, widgets};
const SLIDERS: [&str; 4] = ["Threshold dB", "Min strip duration ms", "Clip start pad ms", "Clip end pad ms"];
/// Selects the whole Kick track and opens Edit > Strip Silence from the menu.
fn strip_silence_dialog() -> Harness<'static, SoundApp> {
let mut app = SoundApp::new(soundcraft_engine::demo::demo_engine(), None, Services::default());
let track = app.engine.session().track_by_name("Kick").unwrap().id;
let end = app.engine.session().content_end();
app.run("edit.select", json!({"tracks": [track.0], "start": 0, "end": end})).unwrap();
menus::invoke_menu(&mut app, "edit.strip_silence", "Edit > Strip Silence");
let mut h = Harness::builder().with_size(vec2(1600.0, 1000.0)).build_ui_state(
|ui, app: &mut SoundApp| {
let ctx = ui.ctx().clone();
app.logic(&ctx);
app.ui(ui);
},
app,
);
h.run_steps(3);
h
}
/// The value of the slider labeled `label`, in the order the window shows them.
fn value(h: &Harness<'_, SoundApp>, label: &str) -> f64 {
let Some(Dialog::StripSilence { threshold, min_ms, pre_ms, post_ms }) = h.state().dialogs.open else { panic!("Strip Silence is not open") };
let i = SLIDERS.iter().position(|s| *s == label).unwrap();
[threshold, min_ms, pre_ms, post_ms][i]
}
fn hold<S>(h: &mut Harness<'_, S>, modifiers: Modifiers) {
h.event(Event::ModifiersChanged(modifiers));
h.run_steps(1);
}
fn press<S>(h: &mut Harness<'_, S>, pos: Pos2, pressed: bool, modifiers: Modifiers) {
h.event(Event::PointerButton { pos, button: PointerButton::Primary, pressed, modifiers });
h.run_steps(1);
}
fn move_to<S>(h: &mut Harness<'_, S>, pos: Pos2) {
h.event(Event::PointerMoved(pos));
h.run_steps(1);
}
/// Presses the middle of a slider's rail (`role` `Slider`) or value box (`SpinButton`) and drags
/// `dx` points right in six steps, with `modifiers` held; returns how far the value moved from
/// where the press put it.
fn drag_on(h: &mut Harness<'_, SoundApp>, role: Role, label: &str, dx: f32, modifiers: Modifiers) -> f64 {
hold(h, modifiers);
let from = h.get_by_role_and_label(role, label).rect().center();
move_to(h, from);
press(h, from, true, modifiers);
let start = value(h, label);
for i in 1..=6 {
move_to(h, from + vec2(dx * i as f32 / 6.0, 0.0));
}
let moved = value(h, label) - start;
press(h, from + vec2(dx, 0.0), false, modifiers);
assert_eq!(value(h, label) - start, moved, "{label}: releasing the button moved the value");
hold(h, Modifiers::NONE);
moved
}
fn drag(h: &mut Harness<'_, SoundApp>, label: &str, dx: f32, modifiers: Modifiers) -> f64 {
drag_on(h, Role::Slider, label, dx, modifiers)
}
#[test]
fn strip_silence_sliders_move_a_tenth_as_far_with_ctrl_held() {
let mut h = strip_silence_dialog();
for label in SLIDERS {
let plain = drag(&mut h, label, 30.0, Modifiers::NONE);
let fine = drag(&mut h, label, 30.0, Modifiers::COMMAND);
assert!(plain > 0.0, "{label}: a plain drag did not move the value");
let ratio = fine / plain;
assert!((0.08..=0.12).contains(&ratio), "{label}: Ctrl-drag moved {fine}, a plain drag {plain}");
}
}
#[test]
fn ctrl_dragging_the_value_box_moves_a_tenth_as_far_too() {
let mut h = strip_silence_dialog();
for label in ["Threshold dB", "Clip start pad ms"] {
let plain = drag_on(&mut h, Role::SpinButton, label, 20.0, Modifiers::NONE);
let fine = drag_on(&mut h, Role::SpinButton, label, 20.0, Modifiers::COMMAND);
assert!(plain > 0.0, "{label}: a plain drag on the value box did not move the value");
let ratio = fine / plain;
assert!((0.07..=0.13).contains(&ratio), "{label}: Ctrl-drag on the value box moved {fine}, a plain drag {plain}");
}
}
#[test]
fn ctrl_without_command_moves_a_tenth_as_far() {
let mut h = strip_silence_dialog();
for (role, dx, within) in [(Role::Slider, 30.0, 0.08..=0.12), (Role::SpinButton, 20.0, 0.07..=0.13)] {
let label = "Threshold dB";
let plain = drag_on(&mut h, role, label, dx, Modifiers::NONE);
let fine = drag_on(&mut h, role, label, dx, Modifiers::CTRL);
assert!(plain > 0.0, "{role:?}: a plain drag did not move the value");
assert!(within.contains(&(fine / plain)), "{role:?}: Ctrl-drag moved {fine}, a plain drag {plain}");
}
}
#[test]
fn alt_click_and_alt_drag_do_what_a_plain_click_and_drag_do() {
let mut h = strip_silence_dialog();
let label = "Threshold dB";
let rail = h.get_by_role_and_label(Role::Slider, label).rect();
let near = rail.left_center() + vec2(3.0, 0.0);
let click = |h: &mut Harness<'static, SoundApp>, at: Pos2, modifiers: Modifiers| {
hold(h, modifiers);
move_to(h, at);
press(h, at, true, modifiers);
press(h, at, false, modifiers);
hold(h, Modifiers::NONE);
};
click(&mut h, near, Modifiers::NONE);
let clicked = value(&h, label);
click(&mut h, rail.center(), Modifiers::NONE);
assert_ne!(value(&h, label), clicked, "a click in the middle of the rail did not move the value");
click(&mut h, near, Modifiers::ALT);
assert_eq!(value(&h, label), clicked, "an Alt-click did not do what a plain click does");
let plain = drag(&mut h, label, 30.0, Modifiers::NONE);
let alt = drag(&mut h, label, 30.0, Modifiers::ALT);
assert!(plain > 0.0, "a plain drag did not move the value");
assert_eq!(alt, plain, "an Alt-drag did not do what a plain drag does");
}
#[test]
fn the_value_box_shows_the_tenths_a_ctrl_drag_reaches() {
let mut h = strip_silence_dialog();
drag(&mut h, "Threshold dB", 7.0, Modifiers::COMMAND);
let v = value(&h, "Threshold dB");
let shown = h.get_by_role_and_label(Role::SpinButton, "Threshold dB").value().unwrap_or_default();
assert_eq!(shown, format!("{v:.1}"));
}
#[test]
fn pressing_and_releasing_ctrl_during_a_drag_keeps_the_value() {
let mut h = strip_silence_dialog();
let label = "Threshold dB";
let plain = drag(&mut h, label, 30.0, Modifiers::NONE);
let from = h.get_by_role_and_label(Role::Slider, label).rect().center();
move_to(&mut h, from);
press(&mut h, from, true, Modifiers::NONE);
move_to(&mut h, from + vec2(10.0, 0.0));
let before = value(&h, label);
hold(&mut h, Modifiers::COMMAND);
assert_eq!(value(&h, label), before, "pressing Ctrl moved the value");
for i in 1..=6 {
move_to(&mut h, from + vec2(10.0 + 5.0 * i as f32, 0.0));
}
let fine = value(&h, label) - before;
assert!((0.08..=0.12).contains(&(fine / plain)), "Ctrl-drag moved {fine}, a plain drag {plain}");
let held = value(&h, label);
hold(&mut h, Modifiers::NONE);
assert_eq!(value(&h, label), held, "releasing Ctrl moved the value back to the pointer");
press(&mut h, from + vec2(40.0, 0.0), false, Modifiers::NONE);
assert_eq!(value(&h, label), held, "releasing the button moved the value back to the pointer");
}
#[test]
fn pressing_ctrl_during_a_drag_on_the_value_box_keeps_the_value() {
let mut h = strip_silence_dialog();
let label = "Threshold dB";
let plain = drag_on(&mut h, Role::SpinButton, label, 30.0, Modifiers::NONE);
let held = drag_on(&mut h, Role::SpinButton, label, 30.0, Modifiers::COMMAND);
assert!(held < plain / 2.0, "a Ctrl-drag on the value box moved {held}, a plain drag {plain}");
let from = h.get_by_role_and_label(Role::SpinButton, label).rect().center();
move_to(&mut h, from);
press(&mut h, from, true, Modifiers::NONE);
move_to(&mut h, from + vec2(10.0, 0.0));
let before = value(&h, label);
hold(&mut h, Modifiers::COMMAND);
assert_eq!(value(&h, label), before, "pressing Ctrl moved the value");
for i in 1..=6 {
move_to(&mut h, from + vec2(10.0 + 5.0 * i as f32, 0.0));
}
let fine = value(&h, label) - before;
// The rest of the drag moves as far as a drag with Ctrl held from the start.
assert!((0.8..=1.2).contains(&(fine / held)), "after Ctrl, the value box moved {fine}; a Ctrl-drag moves {held}");
let last = value(&h, label);
press(&mut h, from + vec2(40.0, 0.0), false, Modifiers::COMMAND);
assert_eq!(value(&h, label), last, "releasing the button moved the value");
hold(&mut h, Modifiers::NONE);
}
#[test]
fn the_control_key_alone_also_gives_a_fine_drag() {
let mut h = strip_silence_dialog();
let label = "Threshold dB";
let plain = drag(&mut h, label, 30.0, Modifiers::NONE);
let fine = drag(&mut h, label, 30.0, Modifiers::CTRL);
assert!((0.08..=0.12).contains(&(fine / plain)), "Control-drag moved {fine}, a plain drag {plain}");
}
#[test]
fn a_ctrl_drag_reports_every_change_in_the_response() {
let mut h = Harness::builder().with_size(vec2(400.0, 100.0)).build_ui_state(
|ui, (value, changed): &mut (f64, bool)| {
*changed = widgets::fine_slider(ui, value, 0.0..=96.0, "Level").changed();
},
(48.0, false),
);
h.run_steps(2);
let from = h.get_by_role_and_label(Role::Slider, "Level").rect().center();
hold(&mut h, Modifiers::COMMAND);
move_to(&mut h, from);
press(&mut h, from, true, Modifiers::COMMAND);
for i in 1..=6 {
let before = h.state().0;
move_to(&mut h, from + vec2(5.0 * i as f32, 0.0));
assert_ne!(h.state().0, before, "step {i} did not move the value");
assert!(h.state().1, "step {i} moved the value without reporting a change");
}
press(&mut h, from + vec2(30.0, 0.0), false, Modifiers::COMMAND);
}
+2 -2
View File
@@ -11,14 +11,14 @@ repository.workspace = true
default = []
# Frame-parallel H.264 and slice-parallel ProRes decoding (rayon). Off by default: SoundCraft
# decodes on its own background thread and only needs one picture at a time.
threads = ["dep:rayon"]
threads = ["craft-video/threads"]
[dependencies]
craft-media.workspace = true
craft-video.workspace = true
thiserror.workspace = true
serde.workspace = true
image = { workspace = true, features = ["jpeg"] }
rayon = { workspace = true, optional = true }
[dev-dependencies]
proptest.workspace = true
-83
View File
@@ -1,83 +0,0 @@
//! Clean-room pure-Rust H.264 / AVC decoder, implemented from ITU-T Rec. H.264 (ISO/IEC 14496-10).
//!
//! Supported: progressive (frame) coding, 8-bit 4:2:0, Baseline / Main / High profiles — CAVLC and CABAC,
//! I/P/B slices, 8x8 transform, custom scaling matrices, weighted prediction, spatial/temporal direct,
//! multiple slices, long-term references and all MMCOs, deblocking.
//!
//! ```no_run
//! let mut dec = soundcraft_video::h264::Decoder::new();
//! let stream = std::fs::read("video.h264").unwrap();
//! for pic in dec.decode(&stream, 0).unwrap() {
//! println!("{}x{} poc {}", pic.width, pic.height, pic.poc);
//! }
//! for pic in dec.flush() {
//! let _ = pic;
//! }
//! ```
// Index loops over fixed-size blocks read more clearly than iterator chains in codec code.
#![allow(clippy::needless_range_loop, clippy::too_many_arguments)]
mod cabac;
mod cabac_mb;
#[rustfmt::skip]
mod cabac_tables;
mod cavlc;
#[rustfmt::skip]
mod cavlc_tables;
mod deblock;
mod decoder;
mod dpb;
mod error;
mod inter;
mod intra;
mod mbtypes;
pub mod params;
mod picture;
pub use picture::set_plane_allocator;
pub mod slice;
mod slicedec;
mod tables;
mod transform;
pub use decoder::{DecodeStats, Decoder, default_threads};
pub use error::{Error, Result};
/// Colour description from the VUI (ITU-T H.273 code points).
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct ColorInfo {
/// `video_full_range_flag`.
pub full_range: bool,
pub primaries: u8,
pub transfer: u8,
pub matrix: u8,
}
/// A decoded, cropped picture in 8-bit planar 4:2:0.
#[derive(Clone, Debug)]
pub struct Picture {
/// Cropped luma width.
pub width: u32,
/// Cropped luma height.
pub height: u32,
pub chroma_width: u32,
pub chroma_height: u32,
pub y: Vec<u8>,
pub u: Vec<u8>,
pub v: Vec<u8>,
pub y_stride: usize,
pub uv_stride: usize,
/// Presentation timestamp passed to [`Decoder::decode`] with the access unit of this picture.
pub pts: i64,
/// Picture order count.
pub poc: i32,
/// IDR picture.
pub key: bool,
pub color: ColorInfo,
/// Sample aspect ratio (0, 0 when unspecified).
pub sar: (u16, u16),
/// Decoded in draft mode ([`Decoder::set_draft`]): a non-reference picture whose deblocking
/// filter was skipped. Its samples are approximate (not the conforming output); no other
/// picture is affected.
pub draft: bool,
}
-335
View File
@@ -1,335 +0,0 @@
//! CABAC arithmetic decoding engine (9.3.1.2, 9.3.3.2) and context initialisation (9.3.1.1).
use crate::h264::cabac_tables::CABAC_INIT_MN;
use crate::h264::error::{Result, ensure};
/// Table 9-44 rangeTabLPS[pStateIdx][qCodIRangeIdx].
#[rustfmt::skip]
pub(crate) static RANGE_TAB_LPS: [[u8; 4]; 64] = [
[128, 176, 208, 240], [128, 167, 197, 227], [128, 158, 187, 216], [123, 150, 178, 205],
[116, 142, 169, 195], [111, 135, 160, 185], [105, 128, 152, 175], [100, 122, 144, 166],
[95, 116, 137, 158], [90, 110, 130, 150], [85, 104, 123, 142], [81, 99, 117, 135],
[77, 94, 111, 128], [73, 89, 105, 122], [69, 85, 100, 116], [66, 80, 95, 110],
[62, 76, 90, 104], [59, 72, 86, 99], [56, 69, 81, 94], [53, 65, 77, 89],
[51, 62, 73, 85], [48, 59, 69, 80], [46, 56, 66, 76], [43, 53, 63, 72],
[41, 50, 59, 69], [39, 48, 56, 65], [37, 45, 54, 62], [35, 43, 51, 59],
[33, 41, 48, 56], [32, 39, 46, 53], [30, 37, 43, 50], [29, 35, 41, 48],
[27, 33, 39, 45], [26, 31, 37, 43], [24, 30, 35, 41], [23, 28, 33, 39],
[22, 27, 32, 37], [21, 26, 30, 35], [20, 24, 29, 33], [19, 23, 27, 31],
[18, 22, 26, 30], [17, 21, 25, 28], [16, 20, 23, 27], [15, 19, 22, 25],
[14, 18, 21, 24], [14, 17, 20, 23], [13, 16, 19, 22], [12, 15, 18, 21],
[12, 14, 17, 20], [11, 14, 16, 19], [11, 13, 15, 18], [10, 12, 15, 17],
[10, 12, 14, 16], [9, 11, 13, 15], [9, 11, 12, 14], [8, 10, 12, 14],
[8, 9, 11, 13], [7, 9, 11, 12], [7, 9, 10, 12], [7, 8, 10, 11],
[6, 8, 9, 11], [6, 7, 9, 10], [6, 7, 8, 9], [2, 2, 2, 2],
];
/// Table 9-45 transIdxLPS.
#[rustfmt::skip]
static TRANS_IDX_LPS: [u8; 64] = [
0, 0, 1, 2, 2, 4, 4, 5, 6, 7, 8, 9, 9, 11, 11, 12,
13, 13, 15, 15, 16, 16, 18, 18, 19, 19, 21, 21, 22, 22, 23, 24,
24, 25, 26, 26, 27, 27, 28, 29, 29, 30, 30, 30, 31, 32, 32, 33,
33, 33, 34, 34, 35, 35, 35, 36, 36, 36, 37, 37, 37, 38, 38, 63,
];
/// Table 9-45 transIdxMPS.
#[rustfmt::skip]
static TRANS_IDX_MPS: [u8; 64] = [
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32,
33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48,
49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 62, 63,
];
/// Combined state transition: index = (pStateIdx << 1 | valMPS) * 2 + bin_is_lps.
/// Each context is stored as `pStateIdx << 1 | valMPS`.
pub(crate) static NEXT_STATE: [[u8; 2]; 128] = {
let mut t = [[0u8; 2]; 128];
let mut s = 0;
while s < 128 {
let p = s >> 1;
let mps = s & 1;
// MPS path
t[s][0] = (TRANS_IDX_MPS[p] << 1) | mps as u8;
// LPS path
let new_mps = if p == 0 { 1 - mps } else { mps };
t[s][1] = (TRANS_IDX_LPS[p] << 1) | new_mps as u8;
s += 1;
}
t
};
/// Number of context variables used (0..=1023).
pub const NUM_CTX: usize = 1024;
pub struct Cabac<'a> {
data: &'a [u8],
/// Byte position of the next byte to load into `value`.
next_byte: usize,
range: u32,
/// codIOffset scaled by 2^bits: value = (codIOffset << bits) | (bits already loaded but not yet
/// consumed by the arithmetic decoder).
value: u64,
bits: u32,
pub ctx: [u8; NUM_CTX],
}
impl<'a> Cabac<'a> {
/// Create an engine over `data` (slice RBSP) starting at bit `pos` (must be byte aligned),
/// with contexts initialised for `slice_qp` and `init` (0..=2 cabac_init_idc, 3 = I slice).
pub fn new(data: &'a [u8], pos: usize, slice_qp: i32, init: usize) -> Result<Self> {
let mut c = Cabac { data, next_byte: pos / 8, range: 0, value: 0, bits: 0, ctx: [0; NUM_CTX] };
c.init_contexts(slice_qp, init);
c.init_engine()?;
Ok(c)
}
pub fn init_contexts(&mut self, slice_qp: i32, init: usize) {
let qp = slice_qp.clamp(0, 51);
let tab = &CABAC_INIT_MN[init];
for (c, &(m, n)) in self.ctx.iter_mut().zip(tab.iter()) {
let pre = (((m as i32) * qp) >> 4) + n as i32;
let pre = pre.clamp(1, 126);
*c = if pre <= 63 { ((63 - pre) << 1) as u8 } else { (((pre - 64) << 1) | 1) as u8 };
}
// ctxIdx 276 (end_of_slice / I_PCM) is handled by decode_terminate.
}
/// 9.3.1.2: codIRange = 510, codIOffset = read_bits(9) (from the current byte-aligned position).
pub fn init_engine(&mut self) -> Result<()> {
self.range = 510;
self.value = 0;
self.bits = 0;
self.refill();
// the top 9 loaded bits form codIOffset; the remaining ones stay buffered below it
self.bits -= 9;
ensure!((self.value >> self.bits) < 510, "invalid CABAC offset at init");
Ok(())
}
/// Load 32 more bits (zeros past the end of the data).
#[inline(always)]
fn refill(&mut self) {
let b = self.next_byte;
let mut w = [0u8; 4];
if let Some(src) = self.data.get(b..b + 4) {
w.copy_from_slice(src);
} else if b < self.data.len() {
let n = self.data.len() - b;
w[..n].copy_from_slice(&self.data[b..]);
}
self.next_byte += 4;
self.value = (self.value << 32) | u32::from_be_bytes(w) as u64;
self.bits += 32;
}
/// Bit position of the next bit not consumed by the arithmetic decoder (for I_PCM alignment
/// after decode_terminate returned 1).
pub fn bit_pos(&self) -> usize {
self.next_byte * 8 - self.bits as usize
}
/// Continue reading at byte-aligned bit position `pos` (call `init_engine` afterwards).
pub fn set_bit_pos(&mut self, pos: usize) {
self.next_byte = pos / 8;
self.value = 0;
self.bits = 0;
}
pub fn data(&self) -> &'a [u8] {
self.data
}
/// True when the reader has run past the end of the data (corrupt stream).
pub fn overrun(&self) -> bool {
self.next_byte > self.data.len() + 16
}
#[inline(always)]
fn renorm(&mut self) {
// codIRange < 512 always, so the shift is 0 when no renormalisation is needed: no branch on
// the (unpredictable) range, only on the rare refill.
let shift = self.range.leading_zeros() - 23;
self.range <<= shift;
if self.bits < shift {
self.refill();
}
self.bits -= shift;
}
/// 9.3.3.2.1, branch-free: the MPS / LPS outcome of a context-coded bin is close to random
/// at high bit rates, so it selects values instead of jumping.
#[inline(always)]
pub fn decode_decision(&mut self, ctx_idx: usize) -> u32 {
// Every ctxIdx is < 1024 and every state < 128: the masks only let the compiler drop the
// bounds checks from the per-bin path.
let ctx_idx = ctx_idx & (NUM_CTX - 1);
let s = (self.ctx[ctx_idx] & 127) as usize;
let q = ((self.range >> 6) & 3) as usize;
let lps = RANGE_TAB_LPS[s >> 1][q] as u32;
let mps_range = self.range - lps;
let scaled = (mps_range as u64) << self.bits;
let is_lps = self.value >= scaled;
self.value -= scaled & (is_lps as u64).wrapping_neg();
self.range = if is_lps { lps } else { mps_range };
self.ctx[ctx_idx] = NEXT_STATE[s][is_lps as usize];
self.renorm();
(s & 1) as u32 ^ is_lps as u32
}
#[inline(always)]
pub fn decode_bypass(&mut self) -> u32 {
if self.bits == 0 {
self.refill();
}
self.bits -= 1;
let scaled = (self.range as u64) << self.bits;
let one = self.value >= scaled;
self.value -= scaled & (one as u64).wrapping_neg();
one as u32
}
pub fn decode_terminate(&mut self) -> u32 {
self.range -= 2;
let scaled = (self.range as u64) << self.bits;
if self.value >= scaled {
1
} else {
self.renorm();
0
}
}
}
#[cfg(test)]
mod tests {
use super::*;
/// Minimal CABAC encoder (9.3.4, informative) used to round-trip test the decoder.
struct Enc {
low: u32,
range: u32,
outstanding: u32,
first: bool,
bits: Vec<bool>,
ctx: Vec<u8>,
}
impl Enc {
fn new(ctx: &[u8]) -> Self {
Enc { low: 0, range: 510, outstanding: 0, first: true, bits: vec![], ctx: ctx.to_vec() }
}
fn put(&mut self, b: bool) {
if self.first {
self.first = false;
} else {
self.bits.push(b);
}
while self.outstanding > 0 {
self.bits.push(!b);
self.outstanding -= 1;
}
}
fn renorm(&mut self) {
while self.range < 256 {
if self.low < 256 {
self.put(false);
} else if self.low >= 512 {
self.low -= 512;
self.put(true);
} else {
self.low -= 256;
self.outstanding += 1;
}
self.range <<= 1;
self.low <<= 1;
}
}
fn encode(&mut self, ctx_idx: usize, bin: u32) {
let s = self.ctx[ctx_idx] as usize;
let p = s >> 1;
let mps = (s & 1) as u32;
let q = ((self.range >> 6) & 3) as usize;
let lps = RANGE_TAB_LPS[p][q] as u32;
self.range -= lps;
if bin != mps {
self.low += self.range;
self.range = lps;
self.ctx[ctx_idx] = NEXT_STATE[s][1];
} else {
self.ctx[ctx_idx] = NEXT_STATE[s][0];
}
self.renorm();
}
fn bypass(&mut self, bin: u32) {
self.low <<= 1;
if bin != 0 {
self.low += self.range;
}
if self.low >= 1024 {
self.put(true);
self.low -= 1024;
} else if self.low < 512 {
self.put(false);
} else {
self.low -= 512;
self.outstanding += 1;
}
}
fn terminate_flush(&mut self) -> Vec<u8> {
// encode terminate bin = 1 then flush (9.3.4.5)
self.range -= 2;
self.low += self.range;
self.range = 2;
self.renorm();
self.put((self.low >> 9) & 1 != 0);
self.bits.push((self.low >> 8) & 1 != 0);
self.bits.push(true); // rbsp_stop_one_bit
while !self.bits.len().is_multiple_of(8) {
self.bits.push(false);
}
self.bits.chunks(8).map(|c| c.iter().fold(0u8, |a, &b| (a << 1) | b as u8)).collect()
}
}
#[test]
fn context_init_formula() {
let c = Cabac::new(&[0, 0, 0, 0], 0, 26, 3).unwrap();
// ctxIdx 0: m=20, n=-15 at QP 26: ((20*26)>>4) - 15 = 32 - 15 = 17 -> pState 46, MPS 0
assert_eq!(c.ctx[0], 46 << 1);
// ctxIdx 60: m=0,n=41 -> pre 41 -> pState 22 MPS 0
assert_eq!(c.ctx[60], 22 << 1);
// ctxIdx 70 (I): m=0 n=11 -> pState 52 MPS 0
assert_eq!(c.ctx[70], 52 << 1);
}
#[test]
fn round_trip_random_bins() {
let mut seed = 12345u32;
let mut rnd = || {
seed ^= seed << 13;
seed ^= seed >> 17;
seed ^= seed << 5;
seed
};
let dummy = Cabac::new(&[0, 0, 0, 0], 0, 30, 0).unwrap();
let mut enc = Enc::new(&dummy.ctx);
let mut ops = Vec::new();
for _ in 0..5000 {
let kind = rnd() % 4;
let ctx = (rnd() % 16) as usize;
// skewed bins so contexts adapt
let bin = if rnd() % 10 < 8 { (ctx & 1) as u32 } else { 1 - (ctx & 1) as u32 };
if kind == 0 {
enc.bypass(bin);
} else {
enc.encode(ctx, bin);
}
ops.push((kind, ctx, bin));
}
let bytes = enc.terminate_flush();
let mut dec = Cabac::new(&bytes, 0, 30, 0).unwrap();
for (i, &(kind, ctx, bin)) in ops.iter().enumerate() {
let got = if kind == 0 { dec.decode_bypass() } else { dec.decode_decision(ctx) };
assert_eq!(got, bin, "bin {i}");
}
assert_eq!(dec.decode_terminate(), 1);
}
}
-689
View File
@@ -1,689 +0,0 @@
//! CABAC slice data and macroblock layer parsing (7.3.4, 7.3.5 with 9.3 binarisations and context
//! index derivations).
use crate::h264::cabac::Cabac;
use crate::h264::error::{Result, ensure, invalid};
use crate::h264::mbtypes::{MbTypeInfo, Part, SubMbInfo, b_mb_type, b_sub_mb_type, intra_mb_type, p_mb_type, p_sub_mb_type};
use crate::h264::picture::MbKind;
use crate::h264::slice::SliceType;
use crate::h264::slicedec::{MbCur, SliceDecoder};
/// Table 9-43: significant_coeff_flag ctxIdxInc for 8x8 frame blocks.
#[rustfmt::skip]
const SIG8_FRAME: [u8; 63] = [
0, 1, 2, 3, 4, 5, 5, 4, 4, 3, 3, 4, 4, 4, 5, 5, 4, 4, 4, 4, 3, 3, 6, 7, 7, 7, 8, 9, 10, 9, 8, 7,
7, 6, 11, 12, 13, 11, 6, 7, 8, 9, 14, 10, 9, 8, 6, 11, 12, 13, 11, 6, 9, 14, 10, 9, 11, 12, 13, 11, 14, 10, 12,
];
/// Table 9-43: last_significant_coeff_flag ctxIdxInc for 8x8 blocks.
#[rustfmt::skip]
const LAST8: [u8; 63] = [
0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8,
];
/// ctxIdxInc = levelListIdx (ctxBlockCat 0, 1, 2, 4).
const IDENT_CTX: [u8; 64] = {
let mut t = [0u8; 64];
let mut i = 0;
while i < 64 {
t[i] = i as u8;
i += 1;
}
t
};
/// ctxIdxInc = Min(levelListIdx / NumC8x8, 2) for 4:2:0 chroma DC (ctxBlockCat 3).
const CHROMA_DC_CTX: [u8; 4] = [0, 1, 2, 2];
/// Table 9-40 ctxIdxBlockCatOffset for ctxBlockCat 0..=5: (coded_block_flag, significant/last, abs level).
const CAT_OFFSET: [(usize, usize, usize); 6] = [(0, 0, 0), (4, 15, 10), (8, 29, 20), (12, 44, 30), (16, 47, 39), (0, 0, 0)];
impl SliceDecoder<'_> {
/// Decode slice_data() with CABAC. `rbsp` is the whole slice RBSP.
pub fn decode_cabac(&mut self, rbsp: &[u8]) -> Result<()> {
let start = self.sh.header_bits.div_ceil(8) * 8;
let init = if self.sh.slice_type.is_intra() { 3 } else { self.sh.cabac_init_idc as usize };
let mut c = Cabac::new(rbsp, start, self.sh.qp(self.pps), init)?;
let total = self.mb_w * self.mb_h;
let mut addr = self.sh.first_mb_in_slice as usize;
let intra_slice = self.sh.slice_type.is_intra();
loop {
ensure!(addr < total, "macroblock address {addr} out of range");
self.start_mb(addr);
let skip = !intra_slice && self.decode_skip_flag(&mut c);
if skip {
self.decode_skip()?;
} else {
self.decode_mb_cabac(&mut c)?;
}
self.pic.mb_done(addr);
ensure!(!c.overrun(), "CABAC read past end of slice data");
if c.decode_terminate() == 1 {
break;
}
addr += 1;
}
Ok(())
}
fn decode_skip_flag(&mut self, c: &mut Cabac) -> bool {
let cond = |n: Option<usize>| n.map(|m| !self.pic.mbs[m].kind_is_skip()).unwrap_or(false) as usize;
let inc = cond(self.nb[0]) + cond(self.nb[1]);
let base = if self.sh.slice_type.is_b() { 24 } else { 11 };
c.decode_decision(base + inc) == 1
}
/// Intra mb_type suffix / I-slice mb_type (Table 9-36). `base` is 3 (I slice) or 17/32 (prefix
/// offsets in P/B slices). Returns the I-slice mb_type value 0..=25.
fn decode_mb_type_i(&mut self, c: &mut Cabac, base: usize) -> u32 {
let islice = base == 3;
let b0 = if islice {
let cond = |n: Option<usize>| n.map(|m| !matches!(self.pic.mbs[m].kind, MbKind::I4x4 | MbKind::I8x8)).unwrap_or(false) as usize;
let inc = cond(self.nb[0]) + cond(self.nb[1]);
c.decode_decision(base + inc)
} else {
c.decode_decision(base)
};
if b0 == 0 {
return 0;
}
if c.decode_terminate() == 1 {
return 25;
}
let (c_luma, c_chroma, c_chroma2, c_pred0, c_pred1) =
if islice { (base + 3, base + 4, base + 5, base + 6, base + 7) } else { (base + 1, base + 2, base + 2, base + 3, base + 3) };
let luma = c.decode_decision(c_luma);
let mut chroma = c.decode_decision(c_chroma);
if chroma != 0 {
chroma += c.decode_decision(c_chroma2);
}
let p0 = c.decode_decision(c_pred0);
let p1 = c.decode_decision(c_pred1);
1 + (p0 * 2 + p1) + 4 * chroma + 12 * luma
}
fn decode_mb_type(&mut self, c: &mut Cabac) -> Result<MbTypeInfo> {
let info = match self.sh.slice_type {
SliceType::I | SliceType::Si => intra_mb_type(self.decode_mb_type_i(c, 3)),
SliceType::P | SliceType::Sp => {
if c.decode_decision(14) == 1 {
intra_mb_type(self.decode_mb_type_i(c, 17))
} else if c.decode_decision(15) == 0 {
p_mb_type(if c.decode_decision(16) == 0 { 0 } else { 3 })
} else {
p_mb_type(if c.decode_decision(17) == 1 { 1 } else { 2 })
}
}
SliceType::B => {
let cond =
|n: Option<usize>| n.map(|m| !matches!(self.pic.mbs[m].kind, MbKind::BSkip | MbKind::BDirect16x16)).unwrap_or(false) as usize;
let inc = cond(self.nb[0]) + cond(self.nb[1]);
if c.decode_decision(27 + inc) == 0 {
b_mb_type(0)
} else if c.decode_decision(27 + 3) == 0 {
b_mb_type(1 + c.decode_decision(27 + 5))
} else {
let b2 = c.decode_decision(27 + 4);
let b3 = c.decode_decision(27 + 5);
let b4 = c.decode_decision(27 + 5);
let b5 = c.decode_decision(27 + 5);
let v = (b2 << 3) | (b3 << 2) | (b4 << 1) | b5;
match v {
0b1101 => intra_mb_type(self.decode_mb_type_i(c, 32)),
0b1111 => b_mb_type(22),
0b1110 => b_mb_type(11),
_ if b2 == 0 => b_mb_type(3 + v),
_ => {
let b6 = c.decode_decision(27 + 5);
b_mb_type(12 + (((v & 7) << 1) | b6))
}
}
}
}
};
match info {
Some(i) => Ok(i),
None => invalid("invalid CABAC mb_type"),
}
}
fn decode_sub_mb_type(&mut self, c: &mut Cabac) -> Result<SubMbInfo> {
let r = if self.sh.slice_type.is_b() {
if c.decode_decision(36) == 0 {
b_sub_mb_type(0)
} else if c.decode_decision(37) == 0 {
b_sub_mb_type(1 + c.decode_decision(39))
} else if c.decode_decision(38) == 1 {
if c.decode_decision(39) == 1 {
b_sub_mb_type(11 + c.decode_decision(39))
} else {
let b4 = c.decode_decision(39);
let b5 = c.decode_decision(39);
b_sub_mb_type(7 + (b4 << 1) + b5)
}
} else {
let b3 = c.decode_decision(39);
let b4 = c.decode_decision(39);
b_sub_mb_type(3 + (b3 << 1) + b4)
}
} else if c.decode_decision(21) == 1 {
p_sub_mb_type(0)
} else if c.decode_decision(22) == 0 {
p_sub_mb_type(1)
} else {
p_sub_mb_type(if c.decode_decision(23) == 1 { 2 } else { 3 })
};
match r {
Some(s) => Ok(s),
None => invalid("invalid CABAC sub_mb_type"),
}
}
/// ref_idx_lX for the partition whose top-left 4x4 block is (x, y) (9.3.3.1.1.6).
fn decode_ref_idx(&mut self, c: &mut Cabac, list: usize, x: usize, y: usize) -> u32 {
let cond = |xx: i32, yy: i32| -> usize {
let (mb, bx, by) = if xx < 0 {
(self.nb[0], 3usize, yy as usize)
} else if yy < 0 {
(self.nb[1], xx as usize, 3usize)
} else {
(Some(self.mb_addr), xx as usize, yy as usize)
};
let Some(m) = mb else { return 0 };
let st = &self.pic.mbs[m];
let b8 = (by >> 1) * 2 + (bx >> 1);
if st.kind.is_intra() || st.kind_is_skip() || st.direct8x8 & (1 << b8) != 0 {
return 0;
}
(st.ref_idx[list][b8] > 0) as usize
};
let inc = cond(x as i32 - 1, y as i32) + 2 * cond(x as i32, y as i32 - 1);
let mut v = 0u32;
if c.decode_decision(54 + inc) == 1 {
v = 1;
if c.decode_decision(54 + 4) == 1 {
v = 2;
while c.decode_decision(54 + 5) == 1 {
v += 1;
if v > 32 {
break;
}
}
}
}
v
}
/// One mvd component for the (sub-)partition with top-left 4x4 block (x, y) (9.3.3.1.1.7, UEG3).
fn decode_mvd(&mut self, c: &mut Cabac, list: usize, comp: usize, x: usize, y: usize) -> i32 {
let absn = |xx: i32, yy: i32| -> u32 {
let (mb, bx, by) = if xx < 0 {
(self.nb[0], 3usize, yy as usize)
} else if yy < 0 {
(self.nb[1], xx as usize, 3usize)
} else {
(Some(self.mb_addr), xx as usize, yy as usize)
};
match mb {
Some(m) => self.pic.mbs[m].mvd[list][by * 4 + bx][comp] as u32,
None => 0,
}
};
let sum = absn(x as i32 - 1, y as i32) + absn(x as i32, y as i32 - 1);
let inc = if sum > 32 {
2
} else if sum > 2 {
1
} else {
0
};
let base = if comp == 0 { 40 } else { 47 };
if c.decode_decision(base + inc) == 0 {
return 0;
}
let mut prefix: i32 = 1;
while prefix < 9 && c.decode_decision(base + (prefix + 2).min(6) as usize) == 1 {
prefix += 1;
}
let mut val = prefix;
if prefix >= 9 {
// EG3 suffix
let mut k = 3;
while c.decode_bypass() == 1 {
val += 1 << k;
k += 1;
if k > 24 {
break;
}
}
while k > 0 {
k -= 1;
val += (c.decode_bypass() as i32) << k;
}
}
if c.decode_bypass() == 1 { -val } else { val }
}
fn set_cur_ref(&mut self, list: usize, x: usize, y: usize, w: usize, h: usize, r: i8) {
let st = self.mb_mut();
for yy in (y..y + h).step_by(2) {
for xx in (x..x + w).step_by(2) {
st.ref_idx[list][(yy >> 1) * 2 + (xx >> 1)] = r;
}
}
}
fn set_cur_mvd(&mut self, list: usize, x: usize, y: usize, w: usize, h: usize, d: [i32; 2]) {
let a = [d[0].unsigned_abs().min(127) as u8, d[1].unsigned_abs().min(127) as u8];
let st = self.mb_mut();
for yy in y..y + h {
for xx in x..x + w {
st.mvd[list][yy * 4 + xx] = a;
}
}
}
fn decode_cbp(&mut self, c: &mut Cabac) -> u8 {
let mut luma = 0u8;
for b8 in 0..4usize {
let (bx, by) = ((b8 & 1) * 2, (b8 >> 1) * 2);
// A
let ca = if bx > 0 { ((luma >> (b8 - 1)) & 1 == 0) as usize } else { self.cbp_luma_cond(self.nb[0], b8 + 1) };
let cb = if by > 0 { ((luma >> (b8 - 2)) & 1 == 0) as usize } else { self.cbp_luma_cond(self.nb[1], b8 + 2) };
let _ = (bx, by);
let bin = c.decode_decision(73 + ca + 2 * cb);
luma |= (bin as u8) << b8;
}
let cond = |n: Option<usize>, bin: usize| -> usize {
let Some(m) = n else { return 0 };
let st = &self.pic.mbs[m];
if st.kind == MbKind::IPcm {
return 1;
}
if st.kind_is_skip() {
return 0;
}
let ch = st.cbp >> 4;
if bin == 0 { (ch != 0) as usize } else { (ch == 2) as usize }
};
let inc0 = cond(self.nb[0], 0) + 2 * cond(self.nb[1], 0);
let mut chroma = 0u8;
if c.decode_decision(77 + inc0) == 1 {
let inc1 = cond(self.nb[0], 1) + 2 * cond(self.nb[1], 1) + 4;
chroma = 1 + c.decode_decision(77 + inc1) as u8;
}
luma | (chroma << 4)
}
/// condTermFlagN for coded_block_pattern luma bin from a neighbouring MB; `b8n` is the neighbouring
/// 8x8 block index within that MB.
fn cbp_luma_cond(&self, n: Option<usize>, b8n: usize) -> usize {
let Some(m) = n else { return 0 };
let st = &self.pic.mbs[m];
if st.kind == MbKind::IPcm {
return 0;
}
if st.kind_is_skip() {
return 1;
}
((st.cbp >> (b8n & 3)) & 1 == 0) as usize
}
fn decode_qp_delta(&mut self, c: &mut Cabac) -> Result<i32> {
let inc = self.prev_qp_delta_nz as usize;
if c.decode_decision(60 + inc) == 0 {
return Ok(0);
}
let mut k = 1u32;
let mut ctx = 62;
while c.decode_decision(ctx) == 1 {
k += 1;
ctx = 63;
ensure!(k <= 128, "mb_qp_delta too large");
}
let v = k.div_ceil(2) as i32;
Ok(if k % 2 == 1 { v } else { -v })
}
/// coded_block_flag ctxIdxInc condition for a neighbouring luma 4x4 block (cat 0..2) or chroma.
fn cbf_luma_cond(&self, bx: i32, by: i32, intra_cur: bool) -> usize {
if bx >= 0 && by >= 0 {
return (self.mb().nnz[(by * 4 + bx) as usize] != 0) as usize;
}
let n = if bx < 0 { self.nb[0] } else { self.nb[1] };
let Some(m) = n else { return intra_cur as usize };
let st = &self.pic.mbs[m];
let (x, y) = if bx < 0 { (3, by as usize) } else { (bx as usize, 3) };
(st.nnz[y * 4 + x] != 0) as usize
}
fn cbf_dc_cond(&self, n: Option<usize>, bit: u8, intra_cur: bool) -> usize {
let Some(m) = n else { return intra_cur as usize };
let st = &self.pic.mbs[m];
if st.kind == MbKind::IPcm {
return 1;
}
(st.cbf_dc & (1 << bit) != 0) as usize
}
fn cbf_chroma_ac_cond(&self, comp: usize, bx: i32, by: i32, intra_cur: bool) -> usize {
if bx >= 0 && by >= 0 {
return (self.mb().nnz_c[comp][(by * 2 + bx) as usize] != 0) as usize;
}
let n = if bx < 0 { self.nb[0] } else { self.nb[1] };
let Some(m) = n else { return intra_cur as usize };
let st = &self.pic.mbs[m];
let (x, y) = if bx < 0 { (1, by as usize) } else { (bx as usize, 1) };
(st.nnz_c[comp][y * 2 + x] != 0) as usize
}
/// residual_block_cabac for ctxBlockCat `cat` (0..=5). Coefficients are written to `coeff`
/// in list order (indices 0..max_num). `cbf_inc` is the coded_block_flag ctxIdxInc (ignored for cat 5).
/// Returns the number of non-zero coefficients.
fn residual_block_cabac(&mut self, c: &mut Cabac, coeff: &mut [i32], cat: usize, max_num: usize, cbf_inc: usize) -> u8 {
if cat != 5 && c.decode_decision(85 + CAT_OFFSET[cat].0 + cbf_inc) == 0 {
return 0;
}
let (sig_base, last_base, abs_base) =
if cat == 5 { (402, 417, 426) } else { (105 + CAT_OFFSET[cat].1, 166 + CAT_OFFSET[cat].1, 227 + CAT_OFFSET[cat].2) };
let (sig_tab, last_tab): (&[u8], &[u8]) = match cat {
3 => (&CHROMA_DC_CTX, &CHROMA_DC_CTX),
5 => (&SIG8_FRAME, &LAST8),
_ => (&IDENT_CTX, &IDENT_CTX),
};
// significance map: positions of the significant coefficients in scan order
let mut pos = [0u8; 64];
let mut n = 0usize;
let last_i = max_num - 1;
let mut found_last = false;
for i in 0..last_i {
if c.decode_decision(sig_base + sig_tab[i] as usize) == 1 {
pos[n] = i as u8;
n += 1;
if c.decode_decision(last_base + last_tab[i] as usize) == 1 {
found_last = true;
break;
}
}
}
if !found_last {
pos[n] = last_i as u8;
n += 1;
}
let mut eq1 = 0usize;
let mut gt1 = 0usize;
let gt1_cap = if cat == 3 { 3 } else { 4 };
for &p in pos[..n].iter().rev() {
let inc0 = if gt1 != 0 { 0 } else { (1 + eq1).min(4) };
let mut abs = 1i32;
if c.decode_decision(abs_base + inc0) == 1 {
let ctx = abs_base + 5 + gt1.min(gt1_cap);
let mut prefix = 1;
while prefix < 14 && c.decode_decision(ctx) == 1 {
prefix += 1;
}
abs = prefix + 1;
if prefix == 14 {
let mut k = 0;
let mut suf = 0i32;
while c.decode_bypass() == 1 {
suf += 1 << k;
k += 1;
if k > 24 {
break;
}
}
while k > 0 {
k -= 1;
suf += (c.decode_bypass() as i32) << k;
}
abs += suf;
}
gt1 += 1;
} else {
eq1 += 1;
}
coeff[p as usize] = if c.decode_bypass() == 1 { -abs } else { abs };
}
n as u8
}
fn residual_cabac(&mut self, c: &mut Cabac, cbp: u8) -> Result<()> {
let kind = self.mb().kind;
let intra = kind.is_intra();
let t8 = self.mb().transform_8x8;
if kind == MbKind::I16x16 {
let inc = self.cbf_dc_cond(self.nb[0], 0, intra) + 2 * self.cbf_dc_cond(self.nb[1], 0, intra);
let mut lv = [0i32; 16];
if self.residual_block_cabac(c, &mut lv, 0, 16, inc) > 0 {
self.mb_mut().cbf_dc |= 1;
self.put_luma_dc(&lv);
}
}
for b8 in 0..4 {
let (bx8, by8) = ((b8 & 1) * 2, (b8 >> 1) * 2);
if cbp & (1 << b8) == 0 {
continue;
}
if t8 {
let mut lv = [0i32; 64];
self.residual_block_cabac(c, &mut lv, 5, 64, 0);
let st = self.mb_mut();
for r in [by8 * 4 + bx8, by8 * 4 + bx8 + 1, (by8 + 1) * 4 + bx8, (by8 + 1) * 4 + bx8 + 1] {
st.nnz[r] = 1;
}
self.put_luma8(b8, &lv);
} else {
for i4 in 0..4 {
let (bx, by) = (bx8 + (i4 & 1), by8 + (i4 >> 1));
let raster = by * 4 + bx;
let inc = self.cbf_luma_cond(bx as i32 - 1, by as i32, intra) + 2 * self.cbf_luma_cond(bx as i32, by as i32 - 1, intra);
let mut lv = [0i32; 16];
let n = if kind == MbKind::I16x16 {
self.residual_block_cabac(c, &mut lv[1..], 1, 15, inc)
} else {
self.residual_block_cabac(c, &mut lv, 2, 16, inc)
};
self.mb_mut().nnz[raster] = n;
if n > 0 {
self.put_luma4(raster, &lv, kind == MbKind::I16x16);
}
}
}
}
let cbp_c = cbp >> 4;
if cbp_c != 0 {
for comp in 0..2 {
let bit = 1 + comp as u8;
let inc = self.cbf_dc_cond(self.nb[0], bit, intra) + 2 * self.cbf_dc_cond(self.nb[1], bit, intra);
let mut lv = [0i32; 4];
if self.residual_block_cabac(c, &mut lv, 3, 4, inc) > 0 {
self.mb_mut().cbf_dc |= 1 << bit;
self.put_chroma_dc(comp, &lv);
}
}
}
if cbp_c == 2 {
for comp in 0..2 {
for b in 0..4 {
let (bx, by) = ((b & 1) as i32, (b >> 1) as i32);
let inc = self.cbf_chroma_ac_cond(comp, bx - 1, by, intra) + 2 * self.cbf_chroma_ac_cond(comp, bx, by - 1, intra);
let mut lv = [0i32; 16];
let n = self.residual_block_cabac(c, &mut lv[1..], 4, 15, inc);
self.mb_mut().nnz_c[comp][b] = n;
if n > 0 {
self.put_chroma_ac(comp, b, &lv);
}
}
}
}
Ok(())
}
/// Parse and reconstruct one macroblock_layer() with CABAC.
fn decode_mb_cabac(&mut self, c: &mut Cabac) -> Result<()> {
let mut info = self.decode_mb_type(c)?;
self.cur = MbCur { info, ..Default::default() };
if info.kind == MbKind::IPcm {
// pcm alignment + samples, then re-initialise the arithmetic decoder
let pos = c.bit_pos().div_ceil(8);
let data = c.data();
ensure!(data.len() >= pos + 384, "truncated I_PCM macroblock");
let Some(&samples) = data.get(pos..).and_then(|d| d.first_chunk::<384>()) else {
return invalid("truncated I_PCM macroblock");
};
c.set_bit_pos((pos + 384) * 8);
c.init_engine()?;
self.finish_pcm();
self.write_pcm(&samples);
self.prev_qp_delta_nz = false;
return Ok(());
}
let intra = info.kind.is_intra();
if info.kind == MbKind::I4x4 && self.pps.transform_8x8_mode && self.decode_t8x8_flag(c) {
info.kind = MbKind::I8x8;
self.cur.info.kind = MbKind::I8x8;
self.mb_mut().transform_8x8 = true;
}
self.mb_mut().kind = info.kind;
let n_ref = self.sh.num_ref_idx_active;
match info.kind {
MbKind::I4x4 | MbKind::I8x8 => {
let n = if info.kind == MbKind::I8x8 { 4 } else { 16 };
for i in 0..n {
self.cur.rem_mode[i] = if c.decode_decision(68) == 1 {
-1
} else {
let b0 = c.decode_decision(69);
let b1 = c.decode_decision(69);
let b2 = c.decode_decision(69);
(b0 | (b1 << 1) | (b2 << 2)) as i8
};
}
self.derive_intra_modes(info.kind == MbKind::I8x8);
let m = self.decode_chroma_mode(c);
self.mb_mut().intra_chroma_mode = m;
}
MbKind::I16x16 => {
let m = self.decode_chroma_mode(c);
self.mb_mut().intra_chroma_mode = m;
}
MbKind::BDirect16x16 => {
self.mb_mut().direct8x8 = 0xf;
}
_ => {
if info.part == Part::P8x8 {
for p in 0..4 {
self.cur.sub[p] = self.decode_sub_mb_type(c)?;
}
let direct_mask: u8 = (0..4).filter(|&p| self.cur.sub[p].direct).map(|p| 1u8 << p).sum();
self.mb_mut().direct8x8 = direct_mask;
for l in 0..2 {
for p in 0..4 {
let sub = self.cur.sub[p];
if sub.direct || sub.pred & (1 << l) == 0 {
continue;
}
let (x, y) = ((p & 1) * 2, (p >> 1) * 2);
let r = if n_ref[l] > 1 && !info.ref0 { self.decode_ref_idx(c, l, x, y) as i8 } else { 0 };
self.cur.ref_idx[l][p] = r;
self.set_cur_ref(l, x, y, 2, 2, r);
}
}
for l in 0..2 {
for p in 0..4 {
let sub = self.cur.sub[p];
if sub.direct || sub.pred & (1 << l) == 0 {
continue;
}
let (x8, y8) = ((p & 1) * 2, (p >> 1) * 2);
for j in 0..sub.shape.num_parts() {
let (sx, sy, w, h) = sub.shape.rect(j);
let (x, y) = (x8 + sx, y8 + sy);
let dx = self.decode_mvd(c, l, 0, x, y);
let dy = self.decode_mvd(c, l, 1, x, y);
self.set_cur_mvd(l, x, y, w, h, [dx, dy]);
self.cur.mvd[l][p * 4 + j] = [dx as i16, dy as i16];
}
}
}
} else {
let np = info.part.num_parts();
for l in 0..2 {
for p in 0..np {
if info.pred[p] & (1 << l) == 0 {
continue;
}
let (x, y, w, h) = info.part.rect(p);
let r = if n_ref[l] > 1 { self.decode_ref_idx(c, l, x, y) as i8 } else { 0 };
self.cur.ref_idx[l][p] = r;
self.set_cur_ref(l, x, y, w, h, r);
}
}
for l in 0..2 {
for p in 0..np {
if info.pred[p] & (1 << l) == 0 {
continue;
}
let (x, y, w, h) = info.part.rect(p);
let dx = self.decode_mvd(c, l, 0, x, y);
let dy = self.decode_mvd(c, l, 1, x, y);
self.set_cur_mvd(l, x, y, w, h, [dx, dy]);
self.cur.mvd[l][p * 4] = [dx as i16, dy as i16];
}
}
}
}
}
let cbp = if info.kind == MbKind::I16x16 { info.i16_cbp } else { self.decode_cbp(c) };
self.cur.cbp = cbp;
self.mb_mut().cbp = cbp;
if !intra && cbp & 15 != 0 && self.pps.transform_8x8_mode && self.no_sub_8x8_lt() {
let t = self.decode_t8x8_flag(c);
self.mb_mut().transform_8x8 = t;
}
if !intra {
// ref_idx written during parsing is replaced by the derived motion
self.derive_inter_motion()?;
}
if cbp != 0 || info.kind == MbKind::I16x16 {
let d = self.decode_qp_delta(c)?;
self.apply_qp_delta(d)?;
self.prev_qp_delta_nz = d != 0;
} else {
self.prev_qp_delta_nz = false;
}
self.store_qp();
self.residual_cabac(c, cbp)?;
if intra {
self.reconstruct_intra();
} else {
self.predict_inter();
self.reconstruct_inter_residual();
}
Ok(())
}
fn decode_t8x8_flag(&mut self, c: &mut Cabac) -> bool {
let cond = |n: Option<usize>| n.map(|m| self.pic.mbs[m].transform_8x8).unwrap_or(false) as usize;
let inc = cond(self.nb[0]) + cond(self.nb[1]);
c.decode_decision(399 + inc) == 1
}
fn decode_chroma_mode(&mut self, c: &mut Cabac) -> u8 {
let cond = |n: Option<usize>| {
n.map(|m| {
let st = &self.pic.mbs[m];
st.kind.is_intra() && st.kind != MbKind::IPcm && st.intra_chroma_mode != 0
})
.unwrap_or(false) as usize
};
let inc = cond(self.nb[0]) + cond(self.nb[1]);
if c.decode_decision(64 + inc) == 0 {
return 0;
}
if c.decode_decision(67) == 0 {
return 1;
}
if c.decode_decision(67) == 0 { 2 } else { 3 }
}
}
-525
View File
@@ -1,525 +0,0 @@
// Generated from ITU-T H.264 (06/2026) Tables 9-12..9-33 (m, n) by a table-extraction script.
// Index: [0..=2] = cabac_init_idc for P/SP/B slices, [3] = I/SI slices. (0, 0) marks "na".
#[rustfmt::skip]
pub(crate) static CABAC_INIT_MN: [[(i8, i8); 1024]; 4] = [
[
(20, -15), (2, 54), (3, 74), (20, -15), (2, 54), (3, 74), (-28, 127), (-23, 104),
(-6, 53), (-1, 54), (7, 51), (23, 33), (23, 2), (21, 0), (1, 9), (0, 49),
(-37, 118), (5, 57), (-13, 78), (-11, 65), (1, 62), (12, 49), (-4, 73), (17, 50),
(18, 64), (9, 43), (29, 0), (26, 67), (16, 90), (9, 104), (-46, 127), (-20, 104),
(1, 67), (-13, 78), (-11, 65), (1, 62), (-6, 86), (-17, 95), (-6, 61), (9, 45),
(-3, 69), (-6, 81), (-11, 96), (6, 55), (7, 67), (-5, 86), (2, 88), (0, 58),
(-3, 76), (-10, 94), (5, 54), (4, 69), (-3, 81), (0, 88), (-7, 67), (-5, 74),
(-4, 74), (-5, 80), (-7, 72), (1, 58), (0, 41), (0, 63), (0, 63), (0, 63),
(-9, 83), (4, 86), (0, 97), (-7, 72), (13, 41), (3, 62), (0, 45), (-4, 78),
(-3, 96), (-27, 126), (-28, 98), (-25, 101), (-23, 67), (-28, 82), (-20, 94), (-16, 83),
(-22, 110), (-21, 91), (-18, 102), (-13, 93), (-29, 127), (-7, 92), (-5, 89), (-7, 96),
(-13, 108), (-3, 46), (-1, 65), (-1, 57), (-9, 93), (-3, 74), (-9, 92), (-8, 87),
(-23, 126), (5, 54), (6, 60), (6, 59), (6, 69), (-1, 48), (0, 68), (-4, 69),
(-8, 88), (-2, 85), (-6, 78), (-1, 75), (-7, 77), (2, 54), (5, 50), (-3, 68),
(1, 50), (6, 42), (-4, 81), (1, 63), (-4, 70), (0, 67), (2, 57), (-2, 76),
(11, 35), (4, 64), (1, 61), (11, 35), (18, 25), (12, 24), (13, 29), (13, 36),
(-10, 93), (-7, 73), (-2, 73), (13, 46), (9, 49), (-7, 100), (9, 53), (2, 53),
(5, 53), (-2, 61), (0, 56), (0, 56), (-13, 63), (-5, 60), (-1, 62), (4, 57),
(-6, 69), (4, 57), (14, 39), (4, 51), (13, 68), (3, 64), (1, 61), (9, 63),
(7, 50), (16, 39), (5, 44), (4, 52), (11, 48), (-5, 60), (-1, 59), (0, 59),
(22, 33), (5, 44), (14, 43), (-1, 78), (0, 60), (9, 69), (11, 28), (2, 40),
(3, 44), (0, 49), (0, 46), (2, 44), (2, 51), (0, 47), (4, 39), (2, 62),
(6, 46), (0, 54), (3, 54), (2, 58), (4, 63), (6, 51), (6, 57), (7, 53),
(6, 52), (6, 55), (11, 45), (14, 36), (8, 53), (-1, 82), (7, 55), (-3, 78),
(15, 46), (22, 31), (-1, 84), (25, 7), (30, -7), (28, 3), (28, 4), (32, 0),
(34, -1), (30, 6), (30, 6), (32, 9), (31, 19), (26, 27), (26, 30), (37, 20),
(28, 34), (17, 70), (1, 67), (5, 59), (9, 67), (16, 30), (18, 32), (18, 35),
(22, 29), (24, 31), (23, 38), (18, 43), (20, 41), (11, 63), (9, 59), (9, 64),
(-1, 94), (-2, 89), (-9, 108), (-6, 76), (-2, 44), (0, 45), (0, 52), (-3, 64),
(-2, 59), (-4, 70), (-4, 75), (-8, 82), (-17, 102), (-9, 77), (3, 24), (0, 42),
(0, 48), (0, 55), (-6, 59), (-7, 71), (-12, 83), (-11, 87), (-30, 119), (1, 58),
(-3, 29), (-1, 36), (1, 38), (2, 43), (-6, 55), (0, 58), (0, 64), (-3, 74),
(-10, 90), (0, 70), (-4, 29), (5, 31), (7, 42), (1, 59), (-2, 58), (-3, 72),
(-3, 81), (-11, 97), (0, 58), (8, 5), (10, 14), (14, 18), (13, 27), (2, 40),
(0, 58), (-3, 70), (-6, 79), (-8, 85), (0, 0), (-13, 106), (-16, 106), (-10, 87),
(-21, 114), (-18, 110), (-14, 98), (-22, 110), (-21, 106), (-18, 103), (-21, 107), (-23, 108),
(-26, 112), (-10, 96), (-12, 95), (-5, 91), (-9, 93), (-22, 94), (-5, 86), (9, 67),
(-4, 80), (-10, 85), (-1, 70), (7, 60), (9, 58), (5, 61), (12, 50), (15, 50),
(18, 49), (17, 54), (10, 41), (7, 46), (-1, 51), (7, 49), (8, 52), (9, 41),
(6, 47), (2, 55), (13, 41), (10, 44), (6, 50), (5, 53), (13, 49), (4, 63),
(6, 64), (-2, 69), (-2, 59), (6, 70), (10, 44), (9, 31), (12, 43), (3, 53),
(14, 34), (10, 38), (-3, 52), (13, 40), (17, 32), (7, 44), (7, 38), (13, 50),
(10, 57), (26, 43), (14, 11), (11, 14), (9, 11), (18, 11), (21, 9), (23, -2),
(32, -15), (32, -15), (34, -21), (39, -23), (42, -33), (41, -31), (46, -28), (38, -12),
(21, 29), (45, -24), (53, -45), (48, -26), (65, -43), (43, -19), (39, -10), (30, 9),
(18, 26), (20, 27), (0, 57), (-14, 82), (-5, 75), (-19, 97), (-35, 125), (27, 0),
(28, 0), (31, -4), (27, 6), (34, 8), (30, 10), (24, 22), (33, 19), (22, 32),
(26, 31), (21, 41), (26, 44), (23, 47), (16, 65), (14, 71), (8, 60), (6, 63),
(17, 65), (21, 24), (23, 20), (26, 23), (27, 32), (28, 23), (28, 24), (23, 40),
(24, 32), (28, 29), (23, 42), (19, 57), (22, 53), (22, 61), (11, 86), (12, 40),
(11, 51), (14, 59), (-4, 79), (-7, 71), (-5, 69), (-9, 70), (-8, 66), (-10, 68),
(-19, 73), (-12, 69), (-16, 70), (-15, 67), (-20, 62), (-19, 70), (-16, 66), (-22, 65),
(-20, 63), (9, -2), (26, -9), (33, -9), (39, -7), (41, -2), (45, 3), (49, 9),
(45, 27), (36, 59), (-6, 66), (-7, 35), (-7, 42), (-8, 45), (-5, 48), (-12, 56),
(-6, 60), (-5, 62), (-8, 66), (-8, 76), (-5, 85), (-6, 81), (-10, 77), (-7, 81),
(-17, 80), (-18, 73), (-4, 74), (-10, 83), (-9, 71), (-9, 67), (-1, 61), (-8, 66),
(-14, 66), (0, 59), (2, 59), (21, -13), (33, -14), (39, -7), (46, -2), (51, 2),
(60, 6), (61, 17), (55, 34), (42, 62), (-7, 92), (-5, 89), (-7, 96), (-13, 108),
(-3, 46), (-1, 65), (-1, 57), (-9, 93), (-3, 74), (-9, 92), (-8, 87), (-23, 126),
(-7, 92), (-5, 89), (-7, 96), (-13, 108), (-3, 46), (-1, 65), (-1, 57), (-9, 93),
(-3, 74), (-9, 92), (-8, 87), (-23, 126), (-2, 85), (-6, 78), (-1, 75), (-7, 77),
(2, 54), (5, 50), (-3, 68), (1, 50), (6, 42), (-4, 81), (1, 63), (-4, 70),
(0, 67), (2, 57), (-2, 76), (11, 35), (4, 64), (1, 61), (11, 35), (18, 25),
(12, 24), (13, 29), (13, 36), (-10, 93), (-7, 73), (-2, 73), (13, 46), (9, 49),
(-7, 100), (9, 53), (2, 53), (5, 53), (-2, 61), (0, 56), (0, 56), (-13, 63),
(-5, 60), (-1, 62), (4, 57), (-6, 69), (4, 57), (14, 39), (4, 51), (13, 68),
(-2, 85), (-6, 78), (-1, 75), (-7, 77), (2, 54), (5, 50), (-3, 68), (1, 50),
(6, 42), (-4, 81), (1, 63), (-4, 70), (0, 67), (2, 57), (-2, 76), (11, 35),
(4, 64), (1, 61), (11, 35), (18, 25), (12, 24), (13, 29), (13, 36), (-10, 93),
(-7, 73), (-2, 73), (13, 46), (9, 49), (-7, 100), (9, 53), (2, 53), (5, 53),
(-2, 61), (0, 56), (0, 56), (-13, 63), (-5, 60), (-1, 62), (4, 57), (-6, 69),
(4, 57), (14, 39), (4, 51), (13, 68), (11, 28), (2, 40), (3, 44), (0, 49),
(0, 46), (2, 44), (2, 51), (0, 47), (4, 39), (2, 62), (6, 46), (0, 54),
(3, 54), (2, 58), (4, 63), (6, 51), (6, 57), (7, 53), (6, 52), (6, 55),
(11, 45), (14, 36), (8, 53), (-1, 82), (7, 55), (-3, 78), (15, 46), (22, 31),
(-1, 84), (25, 7), (30, -7), (28, 3), (28, 4), (32, 0), (34, -1), (30, 6),
(30, 6), (32, 9), (31, 19), (26, 27), (26, 30), (37, 20), (28, 34), (17, 70),
(11, 28), (2, 40), (3, 44), (0, 49), (0, 46), (2, 44), (2, 51), (0, 47),
(4, 39), (2, 62), (6, 46), (0, 54), (3, 54), (2, 58), (4, 63), (6, 51),
(6, 57), (7, 53), (6, 52), (6, 55), (11, 45), (14, 36), (8, 53), (-1, 82),
(7, 55), (-3, 78), (15, 46), (22, 31), (-1, 84), (25, 7), (30, -7), (28, 3),
(28, 4), (32, 0), (34, -1), (30, 6), (30, 6), (32, 9), (31, 19), (26, 27),
(26, 30), (37, 20), (28, 34), (17, 70), (-4, 79), (-7, 71), (-5, 69), (-9, 70),
(-8, 66), (-10, 68), (-19, 73), (-12, 69), (-16, 70), (-15, 67), (-20, 62), (-19, 70),
(-16, 66), (-22, 65), (-20, 63), (-5, 85), (-6, 81), (-10, 77), (-7, 81), (-17, 80),
(-18, 73), (-4, 74), (-10, 83), (-9, 71), (-9, 67), (-1, 61), (-8, 66), (-14, 66),
(0, 59), (2, 59), (9, -2), (26, -9), (33, -9), (39, -7), (41, -2), (45, 3),
(49, 9), (45, 27), (36, 59), (21, -13), (33, -14), (39, -7), (46, -2), (51, 2),
(60, 6), (61, 17), (55, 34), (42, 62), (-6, 66), (-7, 35), (-7, 42), (-8, 45),
(-5, 48), (-12, 56), (-6, 60), (-5, 62), (-8, 66), (-8, 76), (-4, 79), (-7, 71),
(-5, 69), (-9, 70), (-8, 66), (-10, 68), (-19, 73), (-12, 69), (-16, 70), (-15, 67),
(-20, 62), (-19, 70), (-16, 66), (-22, 65), (-20, 63), (-5, 85), (-6, 81), (-10, 77),
(-7, 81), (-17, 80), (-18, 73), (-4, 74), (-10, 83), (-9, 71), (-9, 67), (-1, 61),
(-8, 66), (-14, 66), (0, 59), (2, 59), (9, -2), (26, -9), (33, -9), (39, -7),
(41, -2), (45, 3), (49, 9), (45, 27), (36, 59), (21, -13), (33, -14), (39, -7),
(46, -2), (51, 2), (60, 6), (61, 17), (55, 34), (42, 62), (-6, 66), (-7, 35),
(-7, 42), (-8, 45), (-5, 48), (-12, 56), (-6, 60), (-5, 62), (-8, 66), (-8, 76),
(-13, 106), (-16, 106), (-10, 87), (-21, 114), (-18, 110), (-14, 98), (-22, 110), (-21, 106),
(-18, 103), (-21, 107), (-23, 108), (-26, 112), (-10, 96), (-12, 95), (-5, 91), (-9, 93),
(-22, 94), (-5, 86), (9, 67), (-4, 80), (-10, 85), (-1, 70), (7, 60), (9, 58),
(5, 61), (12, 50), (15, 50), (18, 49), (17, 54), (10, 41), (7, 46), (-1, 51),
(7, 49), (8, 52), (9, 41), (6, 47), (2, 55), (13, 41), (10, 44), (6, 50),
(5, 53), (13, 49), (4, 63), (6, 64), (-13, 106), (-16, 106), (-10, 87), (-21, 114),
(-18, 110), (-14, 98), (-22, 110), (-21, 106), (-18, 103), (-21, 107), (-23, 108), (-26, 112),
(-10, 96), (-12, 95), (-5, 91), (-9, 93), (-22, 94), (-5, 86), (9, 67), (-4, 80),
(-10, 85), (-1, 70), (7, 60), (9, 58), (5, 61), (12, 50), (15, 50), (18, 49),
(17, 54), (10, 41), (7, 46), (-1, 51), (7, 49), (8, 52), (9, 41), (6, 47),
(2, 55), (13, 41), (10, 44), (6, 50), (5, 53), (13, 49), (4, 63), (6, 64),
(14, 11), (11, 14), (9, 11), (18, 11), (21, 9), (23, -2), (32, -15), (32, -15),
(34, -21), (39, -23), (42, -33), (41, -31), (46, -28), (38, -12), (21, 29), (45, -24),
(53, -45), (48, -26), (65, -43), (43, -19), (39, -10), (30, 9), (18, 26), (20, 27),
(0, 57), (-14, 82), (-5, 75), (-19, 97), (-35, 125), (27, 0), (28, 0), (31, -4),
(27, 6), (34, 8), (30, 10), (24, 22), (33, 19), (22, 32), (26, 31), (21, 41),
(26, 44), (23, 47), (16, 65), (14, 71), (14, 11), (11, 14), (9, 11), (18, 11),
(21, 9), (23, -2), (32, -15), (32, -15), (34, -21), (39, -23), (42, -33), (41, -31),
(46, -28), (38, -12), (21, 29), (45, -24), (53, -45), (48, -26), (65, -43), (43, -19),
(39, -10), (30, 9), (18, 26), (20, 27), (0, 57), (-14, 82), (-5, 75), (-19, 97),
(-35, 125), (27, 0), (28, 0), (31, -4), (27, 6), (34, 8), (30, 10), (24, 22),
(33, 19), (22, 32), (26, 31), (21, 41), (26, 44), (23, 47), (16, 65), (14, 71),
(-6, 76), (-2, 44), (0, 45), (0, 52), (-3, 64), (-2, 59), (-4, 70), (-4, 75),
(-8, 82), (-17, 102), (-9, 77), (3, 24), (0, 42), (0, 48), (0, 55), (-6, 59),
(-7, 71), (-12, 83), (-11, 87), (-30, 119), (1, 58), (-3, 29), (-1, 36), (1, 38),
(2, 43), (-6, 55), (0, 58), (0, 64), (-3, 74), (-10, 90), (-6, 76), (-2, 44),
(0, 45), (0, 52), (-3, 64), (-2, 59), (-4, 70), (-4, 75), (-8, 82), (-17, 102),
(-9, 77), (3, 24), (0, 42), (0, 48), (0, 55), (-6, 59), (-7, 71), (-12, 83),
(-11, 87), (-30, 119), (1, 58), (-3, 29), (-1, 36), (1, 38), (2, 43), (-6, 55),
(0, 58), (0, 64), (-3, 74), (-10, 90), (-3, 74), (-9, 92), (-8, 87), (-23, 126),
(-3, 74), (-9, 92), (-8, 87), (-23, 126), (-3, 74), (-9, 92), (-8, 87), (-23, 126),
],
[
(20, -15), (2, 54), (3, 74), (20, -15), (2, 54), (3, 74), (-28, 127), (-23, 104),
(-6, 53), (-1, 54), (7, 51), (22, 25), (34, 0), (16, 0), (-2, 9), (4, 41),
(-29, 118), (2, 65), (-6, 71), (-13, 79), (5, 52), (9, 50), (-3, 70), (10, 54),
(26, 34), (19, 22), (40, 0), (57, 2), (41, 36), (26, 69), (-45, 127), (-15, 101),
(-4, 76), (-6, 71), (-13, 79), (5, 52), (6, 69), (-13, 90), (0, 52), (8, 43),
(-2, 69), (-5, 82), (-10, 96), (2, 59), (2, 75), (-3, 87), (-3, 100), (1, 56),
(-3, 74), (-6, 85), (0, 59), (-3, 81), (-7, 86), (-5, 95), (-1, 66), (-1, 77),
(1, 70), (-2, 86), (-5, 72), (0, 61), (0, 41), (0, 63), (0, 63), (0, 63),
(-9, 83), (4, 86), (0, 97), (-7, 72), (13, 41), (3, 62), (13, 15), (7, 51),
(2, 80), (-39, 127), (-18, 91), (-17, 96), (-26, 81), (-35, 98), (-24, 102), (-23, 97),
(-27, 119), (-24, 99), (-21, 110), (-18, 102), (-36, 127), (0, 80), (-5, 89), (-7, 94),
(-4, 92), (0, 39), (0, 65), (-15, 84), (-35, 127), (-2, 73), (-12, 104), (-9, 91),
(-31, 127), (3, 55), (7, 56), (7, 55), (8, 61), (-3, 53), (0, 68), (-7, 74),
(-9, 88), (-13, 103), (-13, 91), (-9, 89), (-14, 92), (-8, 76), (-12, 87), (-23, 110),
(-24, 105), (-10, 78), (-20, 112), (-17, 99), (-78, 127), (-70, 127), (-50, 127), (-46, 127),
(-4, 66), (-5, 78), (-4, 71), (-8, 72), (2, 59), (-1, 55), (-7, 70), (-6, 75),
(-8, 89), (-34, 119), (-3, 75), (32, 20), (30, 22), (-44, 127), (0, 54), (-5, 61),
(0, 58), (-1, 60), (-3, 61), (-8, 67), (-25, 84), (-14, 74), (-5, 65), (5, 52),
(2, 57), (0, 61), (-9, 69), (-11, 70), (18, 55), (-4, 71), (0, 58), (7, 61),
(9, 41), (18, 25), (9, 32), (5, 43), (9, 47), (0, 44), (0, 51), (2, 46),
(19, 38), (-4, 66), (15, 38), (12, 42), (9, 34), (0, 89), (4, 45), (10, 28),
(10, 31), (33, -11), (52, -43), (18, 15), (28, 0), (35, -22), (38, -25), (34, 0),
(39, -18), (32, -12), (102, -94), (0, 0), (56, -15), (33, -4), (29, 10), (37, -5),
(51, -29), (39, -9), (52, -34), (69, -58), (67, -63), (44, -5), (32, 7), (55, -29),
(32, 1), (0, 0), (27, 36), (33, -25), (34, -30), (36, -28), (38, -28), (38, -27),
(34, -18), (35, -16), (34, -14), (32, -8), (37, -6), (35, 0), (30, 10), (28, 18),
(26, 25), (29, 41), (0, 75), (2, 72), (8, 77), (14, 35), (18, 31), (17, 35),
(21, 30), (17, 45), (20, 42), (18, 45), (27, 26), (16, 54), (7, 66), (16, 56),
(11, 73), (10, 67), (-10, 116), (-23, 112), (-15, 71), (-7, 61), (0, 53), (-5, 66),
(-11, 77), (-9, 80), (-9, 84), (-10, 87), (-34, 127), (-21, 101), (-3, 39), (-5, 53),
(-7, 61), (-11, 75), (-15, 77), (-17, 91), (-25, 107), (-25, 111), (-28, 122), (-11, 76),
(-10, 44), (-10, 52), (-10, 57), (-9, 58), (-16, 72), (-7, 69), (-4, 69), (-5, 74),
(-9, 86), (2, 66), (-9, 34), (1, 32), (11, 31), (5, 52), (-2, 55), (-2, 67),
(0, 73), (-8, 89), (3, 52), (7, 4), (10, 8), (17, 8), (16, 19), (3, 37),
(-1, 61), (-5, 73), (-1, 70), (-4, 78), (0, 0), (-21, 126), (-23, 124), (-20, 110),
(-26, 126), (-25, 124), (-17, 105), (-27, 121), (-27, 117), (-17, 102), (-26, 117), (-27, 116),
(-33, 122), (-10, 95), (-14, 100), (-8, 95), (-17, 111), (-28, 114), (-6, 89), (-2, 80),
(-4, 82), (-9, 85), (-8, 81), (-1, 72), (5, 64), (1, 67), (9, 56), (0, 69),
(1, 69), (7, 69), (-7, 69), (-6, 67), (-16, 77), (-2, 64), (2, 61), (-6, 67),
(-3, 64), (2, 57), (-3, 65), (-3, 66), (0, 62), (9, 51), (-1, 66), (-2, 71),
(-2, 75), (-1, 70), (-9, 72), (14, 60), (16, 37), (0, 47), (18, 35), (11, 37),
(12, 41), (10, 41), (2, 48), (12, 41), (13, 41), (0, 59), (3, 50), (19, 40),
(3, 66), (18, 50), (19, -6), (18, -6), (14, 0), (26, -12), (31, -16), (33, -25),
(33, -22), (37, -28), (39, -30), (42, -30), (47, -42), (45, -36), (49, -34), (41, -17),
(32, 9), (69, -71), (63, -63), (66, -64), (77, -74), (54, -39), (52, -35), (41, -10),
(36, 0), (40, -1), (30, 14), (28, 26), (23, 37), (12, 55), (11, 65), (37, -33),
(39, -36), (40, -37), (38, -30), (46, -33), (42, -30), (40, -24), (49, -29), (38, -12),
(40, -10), (38, -3), (46, -5), (31, 20), (29, 30), (25, 44), (12, 48), (11, 49),
(26, 45), (22, 22), (23, 22), (27, 21), (33, 20), (26, 28), (30, 24), (27, 34),
(18, 42), (25, 39), (18, 50), (12, 70), (21, 54), (14, 71), (11, 83), (25, 32),
(21, 49), (21, 54), (-5, 85), (-6, 81), (-10, 77), (-7, 81), (-17, 80), (-18, 73),
(-4, 74), (-10, 83), (-9, 71), (-9, 67), (-1, 61), (-8, 66), (-14, 66), (0, 59),
(2, 59), (17, -10), (32, -13), (42, -9), (49, -5), (53, 0), (64, 3), (68, 10),
(66, 27), (47, 57), (-5, 71), (0, 24), (-1, 36), (-2, 42), (-2, 52), (-9, 57),
(-6, 63), (-4, 65), (-4, 67), (-7, 82), (-3, 81), (-3, 76), (-7, 72), (-6, 78),
(-12, 72), (-14, 68), (-3, 70), (-6, 76), (-5, 66), (-5, 62), (0, 57), (-4, 61),
(-9, 60), (1, 54), (2, 58), (17, -10), (32, -13), (42, -9), (49, -5), (53, 0),
(64, 3), (68, 10), (66, 27), (47, 57), (0, 80), (-5, 89), (-7, 94), (-4, 92),
(0, 39), (0, 65), (-15, 84), (-35, 127), (-2, 73), (-12, 104), (-9, 91), (-31, 127),
(0, 80), (-5, 89), (-7, 94), (-4, 92), (0, 39), (0, 65), (-15, 84), (-35, 127),
(-2, 73), (-12, 104), (-9, 91), (-31, 127), (-13, 103), (-13, 91), (-9, 89), (-14, 92),
(-8, 76), (-12, 87), (-23, 110), (-24, 105), (-10, 78), (-20, 112), (-17, 99), (-78, 127),
(-70, 127), (-50, 127), (-46, 127), (-4, 66), (-5, 78), (-4, 71), (-8, 72), (2, 59),
(-1, 55), (-7, 70), (-6, 75), (-8, 89), (-34, 119), (-3, 75), (32, 20), (30, 22),
(-44, 127), (0, 54), (-5, 61), (0, 58), (-1, 60), (-3, 61), (-8, 67), (-25, 84),
(-14, 74), (-5, 65), (5, 52), (2, 57), (0, 61), (-9, 69), (-11, 70), (18, 55),
(-13, 103), (-13, 91), (-9, 89), (-14, 92), (-8, 76), (-12, 87), (-23, 110), (-24, 105),
(-10, 78), (-20, 112), (-17, 99), (-78, 127), (-70, 127), (-50, 127), (-46, 127), (-4, 66),
(-5, 78), (-4, 71), (-8, 72), (2, 59), (-1, 55), (-7, 70), (-6, 75), (-8, 89),
(-34, 119), (-3, 75), (32, 20), (30, 22), (-44, 127), (0, 54), (-5, 61), (0, 58),
(-1, 60), (-3, 61), (-8, 67), (-25, 84), (-14, 74), (-5, 65), (5, 52), (2, 57),
(0, 61), (-9, 69), (-11, 70), (18, 55), (4, 45), (10, 28), (10, 31), (33, -11),
(52, -43), (18, 15), (28, 0), (35, -22), (38, -25), (34, 0), (39, -18), (32, -12),
(102, -94), (0, 0), (56, -15), (33, -4), (29, 10), (37, -5), (51, -29), (39, -9),
(52, -34), (69, -58), (67, -63), (44, -5), (32, 7), (55, -29), (32, 1), (0, 0),
(27, 36), (33, -25), (34, -30), (36, -28), (38, -28), (38, -27), (34, -18), (35, -16),
(34, -14), (32, -8), (37, -6), (35, 0), (30, 10), (28, 18), (26, 25), (29, 41),
(4, 45), (10, 28), (10, 31), (33, -11), (52, -43), (18, 15), (28, 0), (35, -22),
(38, -25), (34, 0), (39, -18), (32, -12), (102, -94), (0, 0), (56, -15), (33, -4),
(29, 10), (37, -5), (51, -29), (39, -9), (52, -34), (69, -58), (67, -63), (44, -5),
(32, 7), (55, -29), (32, 1), (0, 0), (27, 36), (33, -25), (34, -30), (36, -28),
(38, -28), (38, -27), (34, -18), (35, -16), (34, -14), (32, -8), (37, -6), (35, 0),
(30, 10), (28, 18), (26, 25), (29, 41), (-5, 85), (-6, 81), (-10, 77), (-7, 81),
(-17, 80), (-18, 73), (-4, 74), (-10, 83), (-9, 71), (-9, 67), (-1, 61), (-8, 66),
(-14, 66), (0, 59), (2, 59), (-3, 81), (-3, 76), (-7, 72), (-6, 78), (-12, 72),
(-14, 68), (-3, 70), (-6, 76), (-5, 66), (-5, 62), (0, 57), (-4, 61), (-9, 60),
(1, 54), (2, 58), (17, -10), (32, -13), (42, -9), (49, -5), (53, 0), (64, 3),
(68, 10), (66, 27), (47, 57), (17, -10), (32, -13), (42, -9), (49, -5), (53, 0),
(64, 3), (68, 10), (66, 27), (47, 57), (-5, 71), (0, 24), (-1, 36), (-2, 42),
(-2, 52), (-9, 57), (-6, 63), (-4, 65), (-4, 67), (-7, 82), (-5, 85), (-6, 81),
(-10, 77), (-7, 81), (-17, 80), (-18, 73), (-4, 74), (-10, 83), (-9, 71), (-9, 67),
(-1, 61), (-8, 66), (-14, 66), (0, 59), (2, 59), (-3, 81), (-3, 76), (-7, 72),
(-6, 78), (-12, 72), (-14, 68), (-3, 70), (-6, 76), (-5, 66), (-5, 62), (0, 57),
(-4, 61), (-9, 60), (1, 54), (2, 58), (17, -10), (32, -13), (42, -9), (49, -5),
(53, 0), (64, 3), (68, 10), (66, 27), (47, 57), (17, -10), (32, -13), (42, -9),
(49, -5), (53, 0), (64, 3), (68, 10), (66, 27), (47, 57), (-5, 71), (0, 24),
(-1, 36), (-2, 42), (-2, 52), (-9, 57), (-6, 63), (-4, 65), (-4, 67), (-7, 82),
(-21, 126), (-23, 124), (-20, 110), (-26, 126), (-25, 124), (-17, 105), (-27, 121), (-27, 117),
(-17, 102), (-26, 117), (-27, 116), (-33, 122), (-10, 95), (-14, 100), (-8, 95), (-17, 111),
(-28, 114), (-6, 89), (-2, 80), (-4, 82), (-9, 85), (-8, 81), (-1, 72), (5, 64),
(1, 67), (9, 56), (0, 69), (1, 69), (7, 69), (-7, 69), (-6, 67), (-16, 77),
(-2, 64), (2, 61), (-6, 67), (-3, 64), (2, 57), (-3, 65), (-3, 66), (0, 62),
(9, 51), (-1, 66), (-2, 71), (-2, 75), (-21, 126), (-23, 124), (-20, 110), (-26, 126),
(-25, 124), (-17, 105), (-27, 121), (-27, 117), (-17, 102), (-26, 117), (-27, 116), (-33, 122),
(-10, 95), (-14, 100), (-8, 95), (-17, 111), (-28, 114), (-6, 89), (-2, 80), (-4, 82),
(-9, 85), (-8, 81), (-1, 72), (5, 64), (1, 67), (9, 56), (0, 69), (1, 69),
(7, 69), (-7, 69), (-6, 67), (-16, 77), (-2, 64), (2, 61), (-6, 67), (-3, 64),
(2, 57), (-3, 65), (-3, 66), (0, 62), (9, 51), (-1, 66), (-2, 71), (-2, 75),
(19, -6), (18, -6), (14, 0), (26, -12), (31, -16), (33, -25), (33, -22), (37, -28),
(39, -30), (42, -30), (47, -42), (45, -36), (49, -34), (41, -17), (32, 9), (69, -71),
(63, -63), (66, -64), (77, -74), (54, -39), (52, -35), (41, -10), (36, 0), (40, -1),
(30, 14), (28, 26), (23, 37), (12, 55), (11, 65), (37, -33), (39, -36), (40, -37),
(38, -30), (46, -33), (42, -30), (40, -24), (49, -29), (38, -12), (40, -10), (38, -3),
(46, -5), (31, 20), (29, 30), (25, 44), (19, -6), (18, -6), (14, 0), (26, -12),
(31, -16), (33, -25), (33, -22), (37, -28), (39, -30), (42, -30), (47, -42), (45, -36),
(49, -34), (41, -17), (32, 9), (69, -71), (63, -63), (66, -64), (77, -74), (54, -39),
(52, -35), (41, -10), (36, 0), (40, -1), (30, 14), (28, 26), (23, 37), (12, 55),
(11, 65), (37, -33), (39, -36), (40, -37), (38, -30), (46, -33), (42, -30), (40, -24),
(49, -29), (38, -12), (40, -10), (38, -3), (46, -5), (31, 20), (29, 30), (25, 44),
(-23, 112), (-15, 71), (-7, 61), (0, 53), (-5, 66), (-11, 77), (-9, 80), (-9, 84),
(-10, 87), (-34, 127), (-21, 101), (-3, 39), (-5, 53), (-7, 61), (-11, 75), (-15, 77),
(-17, 91), (-25, 107), (-25, 111), (-28, 122), (-11, 76), (-10, 44), (-10, 52), (-10, 57),
(-9, 58), (-16, 72), (-7, 69), (-4, 69), (-5, 74), (-9, 86), (-23, 112), (-15, 71),
(-7, 61), (0, 53), (-5, 66), (-11, 77), (-9, 80), (-9, 84), (-10, 87), (-34, 127),
(-21, 101), (-3, 39), (-5, 53), (-7, 61), (-11, 75), (-15, 77), (-17, 91), (-25, 107),
(-25, 111), (-28, 122), (-11, 76), (-10, 44), (-10, 52), (-10, 57), (-9, 58), (-16, 72),
(-7, 69), (-4, 69), (-5, 74), (-9, 86), (-2, 73), (-12, 104), (-9, 91), (-31, 127),
(-2, 73), (-12, 104), (-9, 91), (-31, 127), (-2, 73), (-12, 104), (-9, 91), (-31, 127),
],
[
(20, -15), (2, 54), (3, 74), (20, -15), (2, 54), (3, 74), (-28, 127), (-23, 104),
(-6, 53), (-1, 54), (7, 51), (29, 16), (25, 0), (14, 0), (-10, 51), (-3, 62),
(-27, 99), (26, 16), (-4, 85), (-24, 102), (5, 57), (6, 57), (-17, 73), (14, 57),
(20, 40), (20, 10), (29, 0), (54, 0), (37, 42), (12, 97), (-32, 127), (-22, 117),
(-2, 74), (-4, 85), (-24, 102), (5, 57), (-6, 93), (-14, 88), (-6, 44), (4, 55),
(-11, 89), (-15, 103), (-21, 116), (19, 57), (20, 58), (4, 84), (6, 96), (1, 63),
(-5, 85), (-13, 106), (5, 63), (6, 75), (-3, 90), (-1, 101), (3, 55), (-4, 79),
(-2, 75), (-12, 97), (-7, 50), (1, 60), (0, 41), (0, 63), (0, 63), (0, 63),
(-9, 83), (4, 86), (0, 97), (-7, 72), (13, 41), (3, 62), (7, 34), (-9, 88),
(-20, 127), (-36, 127), (-17, 91), (-14, 95), (-25, 84), (-25, 86), (-12, 89), (-17, 91),
(-31, 127), (-14, 76), (-18, 103), (-13, 90), (-37, 127), (11, 80), (5, 76), (2, 84),
(5, 78), (-6, 55), (4, 61), (-14, 83), (-37, 127), (-5, 79), (-11, 104), (-11, 91),
(-30, 127), (0, 65), (-2, 79), (0, 72), (-4, 92), (-6, 56), (3, 68), (-8, 71),
(-13, 98), (-4, 86), (-12, 88), (-5, 82), (-3, 72), (-4, 67), (-8, 72), (-16, 89),
(-9, 69), (-1, 59), (5, 66), (4, 57), (-4, 71), (-2, 71), (2, 58), (-1, 74),
(-4, 44), (-1, 69), (0, 62), (-7, 51), (-4, 47), (-6, 42), (-3, 41), (-6, 53),
(8, 76), (-9, 78), (-11, 83), (9, 52), (0, 67), (-5, 90), (1, 67), (-15, 72),
(-5, 75), (-8, 80), (-21, 83), (-21, 64), (-13, 31), (-25, 64), (-29, 94), (9, 75),
(17, 63), (-8, 74), (-5, 35), (-2, 27), (13, 91), (3, 65), (-7, 69), (8, 77),
(-10, 66), (3, 62), (-3, 68), (-20, 81), (0, 30), (1, 7), (-3, 23), (-21, 74),
(16, 66), (-23, 124), (17, 37), (44, -18), (50, -34), (-22, 127), (4, 39), (0, 42),
(7, 34), (11, 29), (8, 31), (6, 37), (7, 42), (3, 40), (8, 33), (13, 43),
(13, 36), (4, 47), (3, 55), (2, 58), (6, 60), (8, 44), (11, 44), (14, 42),
(7, 48), (4, 56), (4, 52), (13, 37), (9, 49), (19, 58), (10, 48), (12, 45),
(0, 69), (20, 33), (8, 63), (35, -18), (33, -25), (28, -3), (24, 10), (27, 0),
(34, -14), (52, -44), (39, -24), (19, 17), (31, 25), (36, 29), (24, 33), (34, 15),
(30, 20), (22, 73), (20, 34), (19, 31), (27, 44), (19, 16), (15, 36), (15, 36),
(21, 28), (25, 21), (30, 20), (31, 12), (27, 16), (24, 42), (0, 93), (14, 56),
(15, 57), (26, 38), (-24, 127), (-24, 115), (-22, 82), (-9, 62), (0, 53), (0, 59),
(-14, 85), (-13, 89), (-13, 94), (-11, 92), (-29, 127), (-21, 100), (-14, 57), (-12, 67),
(-11, 71), (-10, 77), (-21, 85), (-16, 88), (-23, 104), (-15, 98), (-37, 127), (-10, 82),
(-8, 48), (-8, 61), (-8, 66), (-7, 70), (-14, 75), (-10, 79), (-9, 83), (-12, 92),
(-18, 108), (-4, 79), (-22, 69), (-16, 75), (-2, 58), (1, 58), (-13, 78), (-9, 83),
(-4, 81), (-13, 99), (-13, 81), (-6, 38), (-13, 62), (-6, 58), (-2, 59), (-16, 73),
(-10, 76), (-13, 86), (-9, 83), (-10, 87), (0, 0), (-22, 127), (-25, 127), (-25, 120),
(-27, 127), (-19, 114), (-23, 117), (-25, 118), (-26, 117), (-24, 113), (-28, 118), (-31, 120),
(-37, 124), (-10, 94), (-15, 102), (-10, 99), (-13, 106), (-50, 127), (-5, 92), (17, 57),
(-5, 86), (-13, 94), (-12, 91), (-2, 77), (0, 71), (-1, 73), (4, 64), (-7, 81),
(5, 64), (15, 57), (1, 67), (0, 68), (-10, 67), (1, 68), (0, 77), (2, 64),
(0, 68), (-5, 78), (7, 55), (5, 59), (2, 65), (14, 54), (15, 44), (5, 60),
(2, 70), (-2, 76), (-18, 86), (12, 70), (5, 64), (-12, 70), (11, 55), (5, 56),
(0, 69), (2, 65), (-6, 74), (5, 54), (7, 54), (-6, 76), (-11, 82), (-2, 77),
(-2, 77), (25, 42), (17, -13), (16, -9), (17, -12), (27, -21), (37, -30), (41, -40),
(42, -41), (48, -47), (39, -32), (46, -40), (52, -51), (46, -41), (52, -39), (43, -19),
(32, 11), (61, -55), (56, -46), (62, -50), (81, -67), (45, -20), (35, -2), (28, 15),
(34, 1), (39, 1), (30, 17), (20, 38), (18, 45), (15, 54), (0, 79), (36, -16),
(37, -14), (37, -17), (32, 1), (34, 15), (29, 15), (24, 25), (34, 22), (31, 16),
(35, 18), (31, 28), (33, 41), (36, 28), (27, 47), (21, 62), (18, 31), (19, 26),
(36, 24), (24, 23), (27, 16), (24, 30), (31, 29), (22, 41), (22, 42), (16, 60),
(15, 52), (14, 60), (3, 78), (-16, 123), (21, 53), (22, 56), (25, 61), (21, 33),
(19, 50), (17, 61), (-3, 78), (-8, 74), (-9, 72), (-10, 72), (-18, 75), (-12, 71),
(-11, 63), (-5, 70), (-17, 75), (-14, 72), (-16, 67), (-8, 53), (-14, 59), (-9, 52),
(-11, 68), (9, -2), (30, -10), (31, -4), (33, -1), (33, 7), (31, 12), (37, 23),
(31, 38), (20, 64), (-9, 71), (-7, 37), (-8, 44), (-11, 49), (-10, 56), (-12, 59),
(-8, 63), (-9, 67), (-6, 68), (-10, 79), (-3, 78), (-8, 74), (-9, 72), (-10, 72),
(-18, 75), (-12, 71), (-11, 63), (-5, 70), (-17, 75), (-14, 72), (-16, 67), (-8, 53),
(-14, 59), (-9, 52), (-11, 68), (9, -2), (30, -10), (31, -4), (33, -1), (33, 7),
(31, 12), (37, 23), (31, 38), (20, 64), (11, 80), (5, 76), (2, 84), (5, 78),
(-6, 55), (4, 61), (-14, 83), (-37, 127), (-5, 79), (-11, 104), (-11, 91), (-30, 127),
(11, 80), (5, 76), (2, 84), (5, 78), (-6, 55), (4, 61), (-14, 83), (-37, 127),
(-5, 79), (-11, 104), (-11, 91), (-30, 127), (-4, 86), (-12, 88), (-5, 82), (-3, 72),
(-4, 67), (-8, 72), (-16, 89), (-9, 69), (-1, 59), (5, 66), (4, 57), (-4, 71),
(-2, 71), (2, 58), (-1, 74), (-4, 44), (-1, 69), (0, 62), (-7, 51), (-4, 47),
(-6, 42), (-3, 41), (-6, 53), (8, 76), (-9, 78), (-11, 83), (9, 52), (0, 67),
(-5, 90), (1, 67), (-15, 72), (-5, 75), (-8, 80), (-21, 83), (-21, 64), (-13, 31),
(-25, 64), (-29, 94), (9, 75), (17, 63), (-8, 74), (-5, 35), (-2, 27), (13, 91),
(-4, 86), (-12, 88), (-5, 82), (-3, 72), (-4, 67), (-8, 72), (-16, 89), (-9, 69),
(-1, 59), (5, 66), (4, 57), (-4, 71), (-2, 71), (2, 58), (-1, 74), (-4, 44),
(-1, 69), (0, 62), (-7, 51), (-4, 47), (-6, 42), (-3, 41), (-6, 53), (8, 76),
(-9, 78), (-11, 83), (9, 52), (0, 67), (-5, 90), (1, 67), (-15, 72), (-5, 75),
(-8, 80), (-21, 83), (-21, 64), (-13, 31), (-25, 64), (-29, 94), (9, 75), (17, 63),
(-8, 74), (-5, 35), (-2, 27), (13, 91), (4, 39), (0, 42), (7, 34), (11, 29),
(8, 31), (6, 37), (7, 42), (3, 40), (8, 33), (13, 43), (13, 36), (4, 47),
(3, 55), (2, 58), (6, 60), (8, 44), (11, 44), (14, 42), (7, 48), (4, 56),
(4, 52), (13, 37), (9, 49), (19, 58), (10, 48), (12, 45), (0, 69), (20, 33),
(8, 63), (35, -18), (33, -25), (28, -3), (24, 10), (27, 0), (34, -14), (52, -44),
(39, -24), (19, 17), (31, 25), (36, 29), (24, 33), (34, 15), (30, 20), (22, 73),
(4, 39), (0, 42), (7, 34), (11, 29), (8, 31), (6, 37), (7, 42), (3, 40),
(8, 33), (13, 43), (13, 36), (4, 47), (3, 55), (2, 58), (6, 60), (8, 44),
(11, 44), (14, 42), (7, 48), (4, 56), (4, 52), (13, 37), (9, 49), (19, 58),
(10, 48), (12, 45), (0, 69), (20, 33), (8, 63), (35, -18), (33, -25), (28, -3),
(24, 10), (27, 0), (34, -14), (52, -44), (39, -24), (19, 17), (31, 25), (36, 29),
(24, 33), (34, 15), (30, 20), (22, 73), (-3, 78), (-8, 74), (-9, 72), (-10, 72),
(-18, 75), (-12, 71), (-11, 63), (-5, 70), (-17, 75), (-14, 72), (-16, 67), (-8, 53),
(-14, 59), (-9, 52), (-11, 68), (-3, 78), (-8, 74), (-9, 72), (-10, 72), (-18, 75),
(-12, 71), (-11, 63), (-5, 70), (-17, 75), (-14, 72), (-16, 67), (-8, 53), (-14, 59),
(-9, 52), (-11, 68), (9, -2), (30, -10), (31, -4), (33, -1), (33, 7), (31, 12),
(37, 23), (31, 38), (20, 64), (9, -2), (30, -10), (31, -4), (33, -1), (33, 7),
(31, 12), (37, 23), (31, 38), (20, 64), (-9, 71), (-7, 37), (-8, 44), (-11, 49),
(-10, 56), (-12, 59), (-8, 63), (-9, 67), (-6, 68), (-10, 79), (-3, 78), (-8, 74),
(-9, 72), (-10, 72), (-18, 75), (-12, 71), (-11, 63), (-5, 70), (-17, 75), (-14, 72),
(-16, 67), (-8, 53), (-14, 59), (-9, 52), (-11, 68), (-3, 78), (-8, 74), (-9, 72),
(-10, 72), (-18, 75), (-12, 71), (-11, 63), (-5, 70), (-17, 75), (-14, 72), (-16, 67),
(-8, 53), (-14, 59), (-9, 52), (-11, 68), (9, -2), (30, -10), (31, -4), (33, -1),
(33, 7), (31, 12), (37, 23), (31, 38), (20, 64), (9, -2), (30, -10), (31, -4),
(33, -1), (33, 7), (31, 12), (37, 23), (31, 38), (20, 64), (-9, 71), (-7, 37),
(-8, 44), (-11, 49), (-10, 56), (-12, 59), (-8, 63), (-9, 67), (-6, 68), (-10, 79),
(-22, 127), (-25, 127), (-25, 120), (-27, 127), (-19, 114), (-23, 117), (-25, 118), (-26, 117),
(-24, 113), (-28, 118), (-31, 120), (-37, 124), (-10, 94), (-15, 102), (-10, 99), (-13, 106),
(-50, 127), (-5, 92), (17, 57), (-5, 86), (-13, 94), (-12, 91), (-2, 77), (0, 71),
(-1, 73), (4, 64), (-7, 81), (5, 64), (15, 57), (1, 67), (0, 68), (-10, 67),
(1, 68), (0, 77), (2, 64), (0, 68), (-5, 78), (7, 55), (5, 59), (2, 65),
(14, 54), (15, 44), (5, 60), (2, 70), (-22, 127), (-25, 127), (-25, 120), (-27, 127),
(-19, 114), (-23, 117), (-25, 118), (-26, 117), (-24, 113), (-28, 118), (-31, 120), (-37, 124),
(-10, 94), (-15, 102), (-10, 99), (-13, 106), (-50, 127), (-5, 92), (17, 57), (-5, 86),
(-13, 94), (-12, 91), (-2, 77), (0, 71), (-1, 73), (4, 64), (-7, 81), (5, 64),
(15, 57), (1, 67), (0, 68), (-10, 67), (1, 68), (0, 77), (2, 64), (0, 68),
(-5, 78), (7, 55), (5, 59), (2, 65), (14, 54), (15, 44), (5, 60), (2, 70),
(17, -13), (16, -9), (17, -12), (27, -21), (37, -30), (41, -40), (42, -41), (48, -47),
(39, -32), (46, -40), (52, -51), (46, -41), (52, -39), (43, -19), (32, 11), (61, -55),
(56, -46), (62, -50), (81, -67), (45, -20), (35, -2), (28, 15), (34, 1), (39, 1),
(30, 17), (20, 38), (18, 45), (15, 54), (0, 79), (36, -16), (37, -14), (37, -17),
(32, 1), (34, 15), (29, 15), (24, 25), (34, 22), (31, 16), (35, 18), (31, 28),
(33, 41), (36, 28), (27, 47), (21, 62), (17, -13), (16, -9), (17, -12), (27, -21),
(37, -30), (41, -40), (42, -41), (48, -47), (39, -32), (46, -40), (52, -51), (46, -41),
(52, -39), (43, -19), (32, 11), (61, -55), (56, -46), (62, -50), (81, -67), (45, -20),
(35, -2), (28, 15), (34, 1), (39, 1), (30, 17), (20, 38), (18, 45), (15, 54),
(0, 79), (36, -16), (37, -14), (37, -17), (32, 1), (34, 15), (29, 15), (24, 25),
(34, 22), (31, 16), (35, 18), (31, 28), (33, 41), (36, 28), (27, 47), (21, 62),
(-24, 115), (-22, 82), (-9, 62), (0, 53), (0, 59), (-14, 85), (-13, 89), (-13, 94),
(-11, 92), (-29, 127), (-21, 100), (-14, 57), (-12, 67), (-11, 71), (-10, 77), (-21, 85),
(-16, 88), (-23, 104), (-15, 98), (-37, 127), (-10, 82), (-8, 48), (-8, 61), (-8, 66),
(-7, 70), (-14, 75), (-10, 79), (-9, 83), (-12, 92), (-18, 108), (-24, 115), (-22, 82),
(-9, 62), (0, 53), (0, 59), (-14, 85), (-13, 89), (-13, 94), (-11, 92), (-29, 127),
(-21, 100), (-14, 57), (-12, 67), (-11, 71), (-10, 77), (-21, 85), (-16, 88), (-23, 104),
(-15, 98), (-37, 127), (-10, 82), (-8, 48), (-8, 61), (-8, 66), (-7, 70), (-14, 75),
(-10, 79), (-9, 83), (-12, 92), (-18, 108), (-5, 79), (-11, 104), (-11, 91), (-30, 127),
(-5, 79), (-11, 104), (-11, 91), (-30, 127), (-5, 79), (-11, 104), (-11, 91), (-30, 127),
],
[
(20, -15), (2, 54), (3, 74), (20, -15), (2, 54), (3, 74), (-28, 127), (-23, 104),
(-6, 53), (-1, 54), (7, 51), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0),
(0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0),
(0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0),
(0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0),
(0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0),
(0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0),
(0, 0), (0, 0), (0, 0), (0, 0), (0, 41), (0, 63), (0, 63), (0, 63),
(-9, 83), (4, 86), (0, 97), (-7, 72), (13, 41), (3, 62), (0, 11), (1, 55),
(0, 69), (-17, 127), (-13, 102), (0, 82), (-7, 74), (-21, 107), (-27, 127), (-31, 127),
(-24, 127), (-18, 95), (-27, 127), (-21, 114), (-30, 127), (-17, 123), (-12, 115), (-16, 122),
(-11, 115), (-12, 63), (-2, 68), (-15, 84), (-13, 104), (-3, 70), (-8, 93), (-10, 90),
(-30, 127), (-1, 74), (-6, 97), (-7, 91), (-20, 127), (-4, 56), (-5, 82), (-7, 76),
(-22, 125), (-7, 93), (-11, 87), (-3, 77), (-5, 71), (-4, 63), (-4, 68), (-12, 84),
(-7, 62), (-7, 65), (8, 61), (5, 56), (-2, 66), (1, 64), (0, 61), (-2, 78),
(1, 50), (7, 52), (10, 35), (0, 44), (11, 38), (1, 45), (0, 46), (5, 44),
(31, 17), (1, 51), (7, 50), (28, 19), (16, 33), (14, 62), (-13, 108), (-15, 100),
(-13, 101), (-13, 91), (-12, 94), (-10, 88), (-16, 84), (-10, 86), (-7, 83), (-13, 87),
(-19, 94), (1, 70), (0, 72), (-5, 74), (18, 59), (-8, 102), (-15, 100), (0, 95),
(-4, 75), (2, 72), (-11, 75), (-3, 71), (15, 46), (-13, 69), (0, 62), (0, 65),
(21, 37), (-15, 72), (9, 57), (16, 54), (0, 62), (12, 72), (24, 0), (15, 9),
(8, 25), (13, 18), (15, 9), (13, 19), (10, 37), (12, 18), (6, 29), (20, 33),
(15, 30), (4, 45), (1, 58), (0, 62), (7, 61), (12, 38), (11, 45), (15, 39),
(11, 42), (13, 44), (16, 45), (12, 41), (10, 49), (30, 34), (18, 42), (10, 55),
(17, 51), (17, 46), (0, 89), (26, -19), (22, -17), (26, -17), (30, -25), (28, -20),
(33, -23), (37, -27), (33, -23), (40, -28), (38, -17), (33, -11), (40, -15), (41, -6),
(38, 1), (41, 17), (30, -6), (27, 3), (26, 22), (37, -16), (35, -4), (38, -8),
(38, -3), (37, 3), (38, 5), (42, 0), (35, 16), (39, 22), (14, 48), (27, 37),
(21, 60), (12, 68), (2, 97), (-3, 71), (-6, 42), (-5, 50), (-3, 54), (-2, 62),
(0, 58), (1, 63), (-2, 72), (-1, 74), (-9, 91), (-5, 67), (-5, 27), (-3, 39),
(-2, 44), (0, 46), (-16, 64), (-8, 68), (-10, 78), (-6, 77), (-10, 86), (-12, 92),
(-15, 55), (-10, 60), (-6, 62), (-4, 65), (-12, 73), (-8, 76), (-7, 80), (-9, 88),
(-17, 110), (-11, 97), (-20, 84), (-11, 79), (-6, 73), (-4, 74), (-13, 86), (-13, 96),
(-11, 97), (-19, 117), (-8, 78), (-5, 33), (-4, 48), (-2, 53), (-3, 62), (-13, 71),
(-10, 79), (-12, 86), (-13, 90), (-14, 97), (0, 0), (-6, 93), (-6, 84), (-8, 79),
(0, 66), (-1, 71), (0, 62), (-2, 60), (-2, 59), (-5, 75), (-3, 62), (-4, 58),
(-9, 66), (-1, 79), (0, 71), (3, 68), (10, 44), (-7, 62), (15, 36), (14, 40),
(16, 27), (12, 29), (1, 44), (20, 36), (18, 32), (5, 42), (1, 48), (10, 62),
(17, 46), (9, 64), (-12, 104), (-11, 97), (-16, 96), (-7, 88), (-8, 85), (-7, 85),
(-9, 85), (-13, 88), (4, 66), (-3, 77), (-3, 76), (-6, 76), (10, 58), (-1, 76),
(-1, 83), (-7, 99), (-14, 95), (2, 95), (0, 76), (-5, 74), (0, 70), (-11, 75),
(1, 68), (0, 65), (-14, 73), (3, 62), (4, 62), (-1, 68), (-13, 75), (11, 55),
(5, 64), (12, 70), (15, 6), (6, 19), (7, 16), (12, 14), (18, 13), (13, 11),
(13, 15), (15, 16), (12, 23), (13, 23), (15, 20), (14, 26), (14, 44), (17, 40),
(17, 47), (24, 17), (21, 21), (25, 22), (31, 27), (22, 29), (19, 35), (14, 50),
(10, 57), (7, 63), (-2, 77), (-4, 82), (-3, 94), (9, 69), (-12, 109), (36, -35),
(36, -34), (32, -26), (37, -30), (44, -32), (34, -18), (34, -15), (40, -15), (33, -7),
(35, -5), (33, 0), (38, 2), (33, 13), (23, 35), (13, 58), (29, -3), (26, 0),
(22, 30), (31, -7), (35, -15), (34, -3), (34, 3), (36, -1), (34, 5), (32, 11),
(35, 5), (34, 12), (39, 11), (30, 29), (34, 26), (29, 39), (19, 66), (31, 21),
(31, 31), (25, 50), (-17, 120), (-20, 112), (-18, 114), (-11, 85), (-15, 92), (-14, 89),
(-26, 71), (-15, 81), (-14, 80), (0, 68), (-14, 70), (-24, 56), (-23, 68), (-24, 50),
(-11, 74), (23, -13), (26, -13), (40, -15), (49, -14), (44, 3), (45, 6), (44, 34),
(33, 54), (19, 82), (-3, 75), (-1, 23), (1, 34), (1, 43), (0, 54), (-2, 55),
(0, 61), (1, 64), (0, 68), (-9, 92), (-14, 106), (-13, 97), (-15, 90), (-12, 90),
(-18, 88), (-10, 73), (-9, 79), (-14, 86), (-10, 73), (-10, 70), (-10, 69), (-5, 66),
(-9, 64), (-5, 58), (2, 59), (21, -10), (24, -11), (28, -8), (28, -1), (29, 3),
(29, 9), (35, 20), (29, 36), (14, 67), (-17, 123), (-12, 115), (-16, 122), (-11, 115),
(-12, 63), (-2, 68), (-15, 84), (-13, 104), (-3, 70), (-8, 93), (-10, 90), (-30, 127),
(-17, 123), (-12, 115), (-16, 122), (-11, 115), (-12, 63), (-2, 68), (-15, 84), (-13, 104),
(-3, 70), (-8, 93), (-10, 90), (-30, 127), (-7, 93), (-11, 87), (-3, 77), (-5, 71),
(-4, 63), (-4, 68), (-12, 84), (-7, 62), (-7, 65), (8, 61), (5, 56), (-2, 66),
(1, 64), (0, 61), (-2, 78), (1, 50), (7, 52), (10, 35), (0, 44), (11, 38),
(1, 45), (0, 46), (5, 44), (31, 17), (1, 51), (7, 50), (28, 19), (16, 33),
(14, 62), (-13, 108), (-15, 100), (-13, 101), (-13, 91), (-12, 94), (-10, 88), (-16, 84),
(-10, 86), (-7, 83), (-13, 87), (-19, 94), (1, 70), (0, 72), (-5, 74), (18, 59),
(-7, 93), (-11, 87), (-3, 77), (-5, 71), (-4, 63), (-4, 68), (-12, 84), (-7, 62),
(-7, 65), (8, 61), (5, 56), (-2, 66), (1, 64), (0, 61), (-2, 78), (1, 50),
(7, 52), (10, 35), (0, 44), (11, 38), (1, 45), (0, 46), (5, 44), (31, 17),
(1, 51), (7, 50), (28, 19), (16, 33), (14, 62), (-13, 108), (-15, 100), (-13, 101),
(-13, 91), (-12, 94), (-10, 88), (-16, 84), (-10, 86), (-7, 83), (-13, 87), (-19, 94),
(1, 70), (0, 72), (-5, 74), (18, 59), (24, 0), (15, 9), (8, 25), (13, 18),
(15, 9), (13, 19), (10, 37), (12, 18), (6, 29), (20, 33), (15, 30), (4, 45),
(1, 58), (0, 62), (7, 61), (12, 38), (11, 45), (15, 39), (11, 42), (13, 44),
(16, 45), (12, 41), (10, 49), (30, 34), (18, 42), (10, 55), (17, 51), (17, 46),
(0, 89), (26, -19), (22, -17), (26, -17), (30, -25), (28, -20), (33, -23), (37, -27),
(33, -23), (40, -28), (38, -17), (33, -11), (40, -15), (41, -6), (38, 1), (41, 17),
(24, 0), (15, 9), (8, 25), (13, 18), (15, 9), (13, 19), (10, 37), (12, 18),
(6, 29), (20, 33), (15, 30), (4, 45), (1, 58), (0, 62), (7, 61), (12, 38),
(11, 45), (15, 39), (11, 42), (13, 44), (16, 45), (12, 41), (10, 49), (30, 34),
(18, 42), (10, 55), (17, 51), (17, 46), (0, 89), (26, -19), (22, -17), (26, -17),
(30, -25), (28, -20), (33, -23), (37, -27), (33, -23), (40, -28), (38, -17), (33, -11),
(40, -15), (41, -6), (38, 1), (41, 17), (-17, 120), (-20, 112), (-18, 114), (-11, 85),
(-15, 92), (-14, 89), (-26, 71), (-15, 81), (-14, 80), (0, 68), (-14, 70), (-24, 56),
(-23, 68), (-24, 50), (-11, 74), (-14, 106), (-13, 97), (-15, 90), (-12, 90), (-18, 88),
(-10, 73), (-9, 79), (-14, 86), (-10, 73), (-10, 70), (-10, 69), (-5, 66), (-9, 64),
(-5, 58), (2, 59), (23, -13), (26, -13), (40, -15), (49, -14), (44, 3), (45, 6),
(44, 34), (33, 54), (19, 82), (21, -10), (24, -11), (28, -8), (28, -1), (29, 3),
(29, 9), (35, 20), (29, 36), (14, 67), (-3, 75), (-1, 23), (1, 34), (1, 43),
(0, 54), (-2, 55), (0, 61), (1, 64), (0, 68), (-9, 92), (-17, 120), (-20, 112),
(-18, 114), (-11, 85), (-15, 92), (-14, 89), (-26, 71), (-15, 81), (-14, 80), (0, 68),
(-14, 70), (-24, 56), (-23, 68), (-24, 50), (-11, 74), (-14, 106), (-13, 97), (-15, 90),
(-12, 90), (-18, 88), (-10, 73), (-9, 79), (-14, 86), (-10, 73), (-10, 70), (-10, 69),
(-5, 66), (-9, 64), (-5, 58), (2, 59), (23, -13), (26, -13), (40, -15), (49, -14),
(44, 3), (45, 6), (44, 34), (33, 54), (19, 82), (21, -10), (24, -11), (28, -8),
(28, -1), (29, 3), (29, 9), (35, 20), (29, 36), (14, 67), (-3, 75), (-1, 23),
(1, 34), (1, 43), (0, 54), (-2, 55), (0, 61), (1, 64), (0, 68), (-9, 92),
(-6, 93), (-6, 84), (-8, 79), (0, 66), (-1, 71), (0, 62), (-2, 60), (-2, 59),
(-5, 75), (-3, 62), (-4, 58), (-9, 66), (-1, 79), (0, 71), (3, 68), (10, 44),
(-7, 62), (15, 36), (14, 40), (16, 27), (12, 29), (1, 44), (20, 36), (18, 32),
(5, 42), (1, 48), (10, 62), (17, 46), (9, 64), (-12, 104), (-11, 97), (-16, 96),
(-7, 88), (-8, 85), (-7, 85), (-9, 85), (-13, 88), (4, 66), (-3, 77), (-3, 76),
(-6, 76), (10, 58), (-1, 76), (-1, 83), (-6, 93), (-6, 84), (-8, 79), (0, 66),
(-1, 71), (0, 62), (-2, 60), (-2, 59), (-5, 75), (-3, 62), (-4, 58), (-9, 66),
(-1, 79), (0, 71), (3, 68), (10, 44), (-7, 62), (15, 36), (14, 40), (16, 27),
(12, 29), (1, 44), (20, 36), (18, 32), (5, 42), (1, 48), (10, 62), (17, 46),
(9, 64), (-12, 104), (-11, 97), (-16, 96), (-7, 88), (-8, 85), (-7, 85), (-9, 85),
(-13, 88), (4, 66), (-3, 77), (-3, 76), (-6, 76), (10, 58), (-1, 76), (-1, 83),
(15, 6), (6, 19), (7, 16), (12, 14), (18, 13), (13, 11), (13, 15), (15, 16),
(12, 23), (13, 23), (15, 20), (14, 26), (14, 44), (17, 40), (17, 47), (24, 17),
(21, 21), (25, 22), (31, 27), (22, 29), (19, 35), (14, 50), (10, 57), (7, 63),
(-2, 77), (-4, 82), (-3, 94), (9, 69), (-12, 109), (36, -35), (36, -34), (32, -26),
(37, -30), (44, -32), (34, -18), (34, -15), (40, -15), (33, -7), (35, -5), (33, 0),
(38, 2), (33, 13), (23, 35), (13, 58), (15, 6), (6, 19), (7, 16), (12, 14),
(18, 13), (13, 11), (13, 15), (15, 16), (12, 23), (13, 23), (15, 20), (14, 26),
(14, 44), (17, 40), (17, 47), (24, 17), (21, 21), (25, 22), (31, 27), (22, 29),
(19, 35), (14, 50), (10, 57), (7, 63), (-2, 77), (-4, 82), (-3, 94), (9, 69),
(-12, 109), (36, -35), (36, -34), (32, -26), (37, -30), (44, -32), (34, -18), (34, -15),
(40, -15), (33, -7), (35, -5), (33, 0), (38, 2), (33, 13), (23, 35), (13, 58),
(-3, 71), (-6, 42), (-5, 50), (-3, 54), (-2, 62), (0, 58), (1, 63), (-2, 72),
(-1, 74), (-9, 91), (-5, 67), (-5, 27), (-3, 39), (-2, 44), (0, 46), (-16, 64),
(-8, 68), (-10, 78), (-6, 77), (-10, 86), (-12, 92), (-15, 55), (-10, 60), (-6, 62),
(-4, 65), (-12, 73), (-8, 76), (-7, 80), (-9, 88), (-17, 110), (-3, 71), (-6, 42),
(-5, 50), (-3, 54), (-2, 62), (0, 58), (1, 63), (-2, 72), (-1, 74), (-9, 91),
(-5, 67), (-5, 27), (-3, 39), (-2, 44), (0, 46), (-16, 64), (-8, 68), (-10, 78),
(-6, 77), (-10, 86), (-12, 92), (-15, 55), (-10, 60), (-6, 62), (-4, 65), (-12, 73),
(-8, 76), (-7, 80), (-9, 88), (-17, 110), (-3, 70), (-8, 93), (-10, 90), (-30, 127),
(-3, 70), (-8, 93), (-10, 90), (-30, 127), (-3, 70), (-8, 93), (-10, 90), (-30, 127),
],
];
-281
View File
@@ -1,281 +0,0 @@
//! CAVLC residual block parsing (9.2).
use crate::bitstream::BitReader;
use crate::h264::cavlc_tables::{COEFF_TOKEN, RUN_BEFORE, TOTAL_ZEROS_2X2, TOTAL_ZEROS_2X4, TOTAL_ZEROS_4X4};
use crate::h264::error::{Result, ensure};
use std::sync::OnceLock;
/// Prefix-code lookup keyed by (number of leading zeros, next 5 bits after the first one bit).
pub(crate) struct VlcTable {
/// entry = (len << 8) | value, 0 = invalid.
entries: Box<[u16; 17 * 32]>,
}
impl VlcTable {
pub fn build(codes: &[(&str, u8)]) -> Self {
let mut entries = Box::new([0u16; 17 * 32]);
for &(code, value) in codes {
if code.is_empty() {
continue;
}
let len = code.len();
let lz = code.bytes().take_while(|&b| b == b'0').count();
let val = ((len as u16) << 8) | value as u16;
if lz == len {
// all-zero code: any continuation with at least `len` zeros
for l in lz..=16 {
for s in 0..32 {
entries[l * 32 + s] = val;
}
}
continue;
}
let suffix = &code[lz + 1..];
assert!(suffix.len() <= 5, "suffix too long in {code}");
let sbits = suffix.bytes().fold(0u32, |a, b| (a << 1) | u32::from(b == b'1'));
let free = 5 - suffix.len();
for ext in 0..(1u32 << free) {
let s = (sbits << free) | ext;
entries[lz * 32 + s as usize] = val;
}
}
Self { entries }
}
#[inline]
pub fn decode(&self, r: &mut BitReader) -> Result<u8> {
let bits = r.peek(32);
let lz = bits.leading_zeros().min(16);
let s = if lz >= 16 { 0 } else { ((bits << lz) << 1) >> 27 };
let e = self.entries[(lz * 32 + s) as usize];
ensure!(e != 0, "invalid VLC code");
r.skip((e >> 8) as usize)?;
Ok(e as u8)
}
}
struct Tables {
/// coeff_token per nC class; value = (TrailingOnes << 5) | TotalCoeff
coeff_token: [VlcTable; 6],
total_zeros_4x4: [VlcTable; 15],
total_zeros_2x2: [VlcTable; 3],
total_zeros_2x4: [VlcTable; 7],
run_before: [VlcTable; 7],
}
fn tables() -> &'static Tables {
static T: OnceLock<Tables> = OnceLock::new();
T.get_or_init(|| {
let coeff_token = std::array::from_fn(|col| {
let codes: Vec<(&str, u8)> = COEFF_TOKEN.iter().map(|&(t1, tc, ref c)| (c[col], (t1 << 5) | tc)).collect();
VlcTable::build(&codes)
});
fn simple<const N: usize>(row: &[&'static str; N]) -> VlcTable {
let codes: Vec<(&str, u8)> = row.iter().enumerate().map(|(i, &c)| (c, i as u8)).collect();
VlcTable::build(&codes)
}
Tables {
coeff_token,
total_zeros_4x4: std::array::from_fn(|i| simple(&TOTAL_ZEROS_4X4[i])),
total_zeros_2x2: std::array::from_fn(|i| simple(&TOTAL_ZEROS_2X2[i])),
total_zeros_2x4: std::array::from_fn(|i| simple(&TOTAL_ZEROS_2X4[i])),
run_before: std::array::from_fn(|i| simple(&RUN_BEFORE[i])),
}
})
}
/// Force table construction (so the first slice does not pay for it).
pub(crate) fn init_tables() {
let _ = tables();
}
/// Parse residual_block_cavlc(). Coefficients are written to `coeff[start_idx..=end_idx]` in scan order
/// (entries outside the coded range are left untouched; the caller zeroes them).
/// `nc` is the nC value (-1 for 4:2:0 chroma DC, -2 for 4:2:2 chroma DC). Returns TotalCoeff.
pub(crate) fn residual_block(r: &mut BitReader, coeff: &mut [i32], start_idx: usize, end_idx: usize, max_num_coeff: usize, nc: i32) -> Result<u8> {
let t = tables();
let col = match nc {
-1 => 4,
-2 => 5,
0..=1 => 0,
2..=3 => 1,
4..=7 => 2,
_ => 3,
};
let tok = t.coeff_token[col].decode(r)?;
let total_coeff = (tok & 31) as usize;
let trailing_ones = (tok >> 5) as usize;
if total_coeff == 0 {
return Ok(0);
}
ensure!(total_coeff <= max_num_coeff, "TotalCoeff exceeds maxNumCoeff");
let mut level = [0i32; 16];
let mut suffix_length: u32 = if total_coeff > 10 && trailing_ones < 3 { 1 } else { 0 };
for (i, lv) in level.iter_mut().enumerate().take(total_coeff) {
if i < trailing_ones {
*lv = if r.read_bit()? { -1 } else { 1 };
continue;
}
// level_prefix
let bits = r.peek(32);
let level_prefix = bits.leading_zeros();
ensure!(level_prefix <= 25, "level_prefix too large");
r.skip(level_prefix as usize + 1)?;
let level_suffix_size = if level_prefix == 14 && suffix_length == 0 {
4
} else if level_prefix >= 15 {
level_prefix - 3
} else {
suffix_length
};
let level_suffix = if level_suffix_size > 0 { r.read_bits(level_suffix_size)? as i32 } else { 0 };
let mut level_code = ((level_prefix.min(15) as i32) << suffix_length) + level_suffix;
if level_prefix >= 15 && suffix_length == 0 {
level_code += 15;
}
if level_prefix >= 16 {
level_code += (1 << (level_prefix - 3)) - 4096;
}
if i == trailing_ones && trailing_ones < 3 {
level_code += 2;
}
*lv = if level_code % 2 == 0 { (level_code + 2) >> 1 } else { (-level_code - 1) >> 1 };
if suffix_length == 0 {
suffix_length = 1;
}
if lv.abs() > (3 << (suffix_length - 1)) && suffix_length < 6 {
suffix_length += 1;
}
}
let mut zeros_left = if total_coeff < end_idx - start_idx + 1 {
let tz_idx = total_coeff - 1;
let tz = match max_num_coeff {
4 => t.total_zeros_2x2[tz_idx].decode(r)?,
8 => t.total_zeros_2x4[tz_idx].decode(r)?,
_ => t.total_zeros_4x4[tz_idx].decode(r)?,
} as usize;
ensure!(tz + total_coeff <= end_idx - start_idx + 1, "total_zeros out of range");
tz
} else {
0
};
// runs and placement: highest-frequency coefficient first
let mut pos = start_idx + total_coeff + zeros_left - 1;
for (i, &lv) in level.iter().enumerate().take(total_coeff) {
coeff[pos] = lv;
if i + 1 == total_coeff {
break;
}
let run = if zeros_left > 0 {
let run = t.run_before[zeros_left.min(7) - 1].decode(r)? as usize;
ensure!(run <= zeros_left, "run_before out of range");
zeros_left -= run;
run
} else {
0
};
pos -= 1 + run;
}
Ok(total_coeff as u8)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::bitstream::BitWriter;
fn kraft(codes: &[&str]) -> f64 {
codes.iter().filter(|c| !c.is_empty()).map(|c| 0.5f64.powi(c.len() as i32)).sum()
}
fn prefix_free(codes: &[&str]) -> bool {
let c: Vec<&&str> = codes.iter().filter(|c| !c.is_empty()).collect();
for (i, a) in c.iter().enumerate() {
for (j, b) in c.iter().enumerate() {
if i != j && b.starts_with(**a) {
return false;
}
}
}
true
}
#[test]
fn coeff_token_tables_are_prefix_codes() {
for col in 0..6 {
let codes: Vec<&str> = COEFF_TOKEN.iter().map(|e| e.2[col]).collect();
assert!(prefix_free(&codes), "column {col}");
let k = kraft(&codes);
assert!(k <= 1.0 + 1e-12, "column {col} kraft {k}");
}
// nC>=8 is a complete 6-bit fixed code except the unused 000011 pattern
let codes: Vec<&str> = COEFF_TOKEN.iter().map(|e| e.2[3]).collect();
assert!(codes.iter().all(|c| c.len() == 6));
}
#[test]
fn total_zeros_and_run_before_are_complete_prefix_codes() {
for (i, row) in TOTAL_ZEROS_4X4.iter().enumerate() {
assert!(prefix_free(row));
// tzVlcIndex 1 leaves the all-zero 9-bit code unused; all others are complete
let k = kraft(row);
if i == 0 {
assert!((k - (1.0 - 1.0 / 512.0)).abs() < 1e-12, "{row:?}");
} else {
assert!((k - 1.0).abs() < 1e-12, "{row:?}");
}
}
for row in TOTAL_ZEROS_2X2.iter() {
assert!(prefix_free(row));
assert!((kraft(row) - 1.0).abs() < 1e-12);
}
for row in TOTAL_ZEROS_2X4.iter() {
assert!(prefix_free(row));
assert!((kraft(row) - 1.0).abs() < 1e-12);
}
for row in RUN_BEFORE.iter() {
assert!(prefix_free(row));
}
}
fn write_code(w: &mut BitWriter, code: &str) {
for b in code.bytes() {
w.write_bit(b == b'1');
}
}
#[test]
fn decodes_spec_style_example() {
// Block: 0 3 -1 0 | 0 -1 1 0 | 1 0 0 0 | 0 0 0 0 (zig-zag order), nC = 0.
// TotalCoeff 5, TrailingOnes 3: coeff_token 0000 100.
// trailing ones (reverse order): +1, -1, -1 -> sign bits 0 1 1
// level -1? No: remaining levels (reverse): 1 (idx 4?) ... use the classic example:
// coefficients in scan order: [0, 3, -1, 0, 0, -1, 1, 0, 1, 0, ...]
// reverse order non-zero: 1 (pos 8), 1 (pos 6), -1 (pos 5), -1 (pos 2), 3 (pos 1)
// TotalCoeff 5, TrailingOnes 3 (1, 1, -1).
let mut w = BitWriter::new();
write_code(&mut w, "0000100"); // coeff_token TC=5 T1=3, nC 0..2
write_code(&mut w, "0"); // +1
write_code(&mut w, "0"); // +1
write_code(&mut w, "1"); // -1
// level -1 with suffixLength 0: levelCode = 1 (T1 == 3 so no +2) -> prefix 1 => "01"
write_code(&mut w, "01");
// level 3 with suffixLength 1: levelCode = 4 -> prefix 2, suffix 0 => "001" "0"
write_code(&mut w, "0010");
// total_zeros = 4 (positions 0, 3, 4, 7) with tzVlcIndex 5 -> "110"
write_code(&mut w, "110");
// run_before: zerosLeft 4 at pos 8: run 1 -> "10"; zerosLeft 3 at pos 6: run 0 -> "11";
// zerosLeft 3 at pos 5: run 2 -> "01"; zerosLeft 1 at pos 2: run 0 -> "1"
write_code(&mut w, "10");
write_code(&mut w, "11");
write_code(&mut w, "01");
write_code(&mut w, "1");
w.rbsp_trailing();
let b = w.finish();
let mut r = BitReader::new(&b);
let mut c = [0i32; 16];
let tc = residual_block(&mut r, &mut c, 0, 15, 16, 0).unwrap();
assert_eq!(tc, 5);
assert_eq!(&c[..10], &[0, 3, -1, 0, 0, -1, 1, 0, 1, 0]);
}
}
-117
View File
@@ -1,117 +0,0 @@
// Generated from ITU-T H.264 (06/2026) Tables 9-5, 9-7..9-10 by a table-extraction script.
// Codes are bit strings; "" marks an unused entry.
/// (TrailingOnes, TotalCoeff, [nC 0..2, 2..4, 4..8, 8.., -1, -2])
#[rustfmt::skip]
pub(crate) static COEFF_TOKEN: [(u8, u8, [&str; 6]); 62] = [
(0, 0, ["1", "11", "1111", "000011", "01", "1"]),
(0, 1, ["000101", "001011", "001111", "000000", "000111", "0001111"]),
(1, 1, ["01", "10", "1110", "000001", "1", "01"]),
(0, 2, ["00000111", "000111", "001011", "000100", "000100", "0001110"]),
(1, 2, ["000100", "00111", "01111", "000101", "000110", "0001101"]),
(2, 2, ["001", "011", "1101", "000110", "001", "001"]),
(0, 3, ["000000111", "0000111", "001000", "001000", "000011", "000000111"]),
(1, 3, ["00000110", "001010", "01100", "001001", "0000011", "0001100"]),
(2, 3, ["0000101", "001001", "01110", "001010", "0000010", "0001011"]),
(3, 3, ["00011", "0101", "1100", "001011", "000101", "00001"]),
(0, 4, ["0000000111", "00000111", "0001111", "001100", "000010", "000000110"]),
(1, 4, ["000000110", "000110", "01010", "001101", "00000011", "000000101"]),
(2, 4, ["00000101", "000101", "01011", "001110", "00000010", "0001010"]),
(3, 4, ["000011", "0100", "1011", "001111", "0000000", "000001"]),
(0, 5, ["00000000111", "00000100", "0001011", "010000", "", "0000000111"]),
(1, 5, ["0000000110", "0000110", "01000", "010001", "", "0000000110"]),
(2, 5, ["000000101", "0000101", "01001", "010010", "", "000000100"]),
(3, 5, ["0000100", "00110", "1010", "010011", "", "0001001"]),
(0, 6, ["0000000001111", "000000111", "0001001", "010100", "", "00000000111"]),
(1, 6, ["00000000110", "00000110", "001110", "010101", "", "00000000110"]),
(2, 6, ["0000000101", "00000101", "001101", "010110", "", "0000000101"]),
(3, 6, ["00000100", "001000", "1001", "010111", "", "0001000"]),
(0, 7, ["0000000001011", "00000001111", "0001000", "011000", "", "000000000111"]),
(1, 7, ["0000000001110", "000000110", "001010", "011001", "", "000000000110"]),
(2, 7, ["00000000101", "000000101", "001001", "011010", "", "00000000101"]),
(3, 7, ["000000100", "000100", "1000", "011011", "", "0000000100"]),
(0, 8, ["0000000001000", "00000001011", "00001111", "011100", "", "0000000000111"]),
(1, 8, ["0000000001010", "00000001110", "0001110", "011101", "", "000000000101"]),
(2, 8, ["0000000001101", "00000001101", "0001101", "011110", "", "000000000100"]),
(3, 8, ["0000000100", "0000100", "01101", "011111", "", "00000000100"]),
(0, 9, ["00000000001111", "000000001111", "00001011", "100000", "", ""]),
(1, 9, ["00000000001110", "00000001010", "00001110", "100001", "", ""]),
(2, 9, ["0000000001001", "00000001001", "0001010", "100010", "", ""]),
(3, 9, ["00000000100", "000000100", "001100", "100011", "", ""]),
(0, 10, ["00000000001011", "000000001011", "000001111", "100100", "", ""]),
(1, 10, ["00000000001010", "000000001110", "00001010", "100101", "", ""]),
(2, 10, ["00000000001101", "000000001101", "00001101", "100110", "", ""]),
(3, 10, ["0000000001100", "00000001100", "0001100", "100111", "", ""]),
(0, 11, ["000000000001111", "000000001000", "000001011", "101000", "", ""]),
(1, 11, ["000000000001110", "000000001010", "000001110", "101001", "", ""]),
(2, 11, ["00000000001001", "000000001001", "00001001", "101010", "", ""]),
(3, 11, ["00000000001100", "00000001000", "00001100", "101011", "", ""]),
(0, 12, ["000000000001011", "0000000001111", "000001000", "101100", "", ""]),
(1, 12, ["000000000001010", "0000000001110", "000001010", "101101", "", ""]),
(2, 12, ["000000000001101", "0000000001101", "000001101", "101110", "", ""]),
(3, 12, ["00000000001000", "000000001100", "00001000", "101111", "", ""]),
(0, 13, ["0000000000001111", "0000000001011", "0000001101", "110000", "", ""]),
(1, 13, ["000000000000001", "0000000001010", "000000111", "110001", "", ""]),
(2, 13, ["000000000001001", "0000000001001", "000001001", "110010", "", ""]),
(3, 13, ["000000000001100", "0000000001100", "000001100", "110011", "", ""]),
(0, 14, ["0000000000001011", "0000000000111", "0000001001", "110100", "", ""]),
(1, 14, ["0000000000001110", "00000000001011", "0000001100", "110101", "", ""]),
(2, 14, ["0000000000001101", "0000000000110", "0000001011", "110110", "", ""]),
(3, 14, ["000000000001000", "0000000001000", "0000001010", "110111", "", ""]),
(0, 15, ["0000000000000111", "00000000001001", "0000000101", "111000", "", ""]),
(1, 15, ["0000000000001010", "00000000001000", "0000001000", "111001", "", ""]),
(2, 15, ["0000000000001001", "00000000001010", "0000000111", "111010", "", ""]),
(3, 15, ["0000000000001100", "0000000000001", "0000000110", "111011", "", ""]),
(0, 16, ["0000000000000100", "00000000000111", "0000000001", "111100", "", ""]),
(1, 16, ["0000000000000110", "00000000000110", "0000000100", "111101", "", ""]),
(2, 16, ["0000000000000101", "00000000000101", "0000000011", "111110", "", ""]),
(3, 16, ["0000000000001000", "00000000000100", "0000000010", "111111", "", ""]),
];
/// total_zeros for 4x4 blocks: [tzVlcIndex-1][total_zeros]
#[rustfmt::skip]
pub(crate) static TOTAL_ZEROS_4X4: [[&str; 16]; 15] = [
["1", "011", "010", "0011", "0010", "00011", "00010", "000011", "000010", "0000011", "0000010", "00000011", "00000010", "000000011", "000000010", "000000001"],
["111", "110", "101", "100", "011", "0101", "0100", "0011", "0010", "00011", "00010", "000011", "000010", "000001", "000000", ""],
["0101", "111", "110", "101", "0100", "0011", "100", "011", "0010", "00011", "00010", "000001", "00001", "000000", "", ""],
["00011", "111", "0101", "0100", "110", "101", "100", "0011", "011", "0010", "00010", "00001", "00000", "", "", ""],
["0101", "0100", "0011", "111", "110", "101", "100", "011", "0010", "00001", "0001", "00000", "", "", "", ""],
["000001", "00001", "111", "110", "101", "100", "011", "010", "0001", "001", "000000", "", "", "", "", ""],
["000001", "00001", "101", "100", "011", "11", "010", "0001", "001", "000000", "", "", "", "", "", ""],
["000001", "0001", "00001", "011", "11", "10", "010", "001", "000000", "", "", "", "", "", "", ""],
["000001", "000000", "0001", "11", "10", "001", "01", "00001", "", "", "", "", "", "", "", ""],
["00001", "00000", "001", "11", "10", "01", "0001", "", "", "", "", "", "", "", "", ""],
["0000", "0001", "001", "010", "1", "011", "", "", "", "", "", "", "", "", "", ""],
["0000", "0001", "01", "1", "001", "", "", "", "", "", "", "", "", "", "", ""],
["000", "001", "1", "01", "", "", "", "", "", "", "", "", "", "", "", ""],
["00", "01", "1", "", "", "", "", "", "", "", "", "", "", "", "", ""],
["0", "1", "", "", "", "", "", "", "", "", "", "", "", "", "", ""],
];
/// total_zeros for chroma DC 2x2: [tzVlcIndex-1][total_zeros]
#[rustfmt::skip]
pub(crate) static TOTAL_ZEROS_2X2: [[&str; 4]; 3] = [
["1", "01", "001", "000"],
["1", "01", "00", ""],
["1", "0", "", ""],
];
/// total_zeros for chroma DC 2x4: [tzVlcIndex-1][total_zeros]
#[rustfmt::skip]
pub(crate) static TOTAL_ZEROS_2X4: [[&str; 8]; 7] = [
["1", "010", "011", "0010", "0011", "0001", "00001", "00000"],
["000", "01", "001", "100", "101", "110", "111", ""],
["000", "001", "01", "10", "110", "111", "", ""],
["110", "00", "01", "10", "111", "", "", ""],
["00", "01", "10", "11", "", "", "", ""],
["00", "01", "1", "", "", "", "", ""],
["0", "1", "", "", "", "", "", ""],
];
/// run_before: [min(zerosLeft,7)-1][run_before]
#[rustfmt::skip]
pub(crate) static RUN_BEFORE: [[&str; 15]; 7] = [
["1", "0", "", "", "", "", "", "", "", "", "", "", "", "", ""],
["1", "01", "00", "", "", "", "", "", "", "", "", "", "", "", ""],
["11", "10", "01", "00", "", "", "", "", "", "", "", "", "", "", ""],
["11", "10", "01", "001", "000", "", "", "", "", "", "", "", "", "", ""],
["11", "10", "011", "010", "001", "000", "", "", "", "", "", "", "", "", ""],
["11", "000", "001", "011", "010", "101", "100", "", "", "", "", "", "", "", ""],
["111", "110", "101", "100", "011", "010", "001", "0001", "00001", "000001", "0000001", "00000001", "000000001", "0000000001", "00000000001"],
];
-579
View File
@@ -1,579 +0,0 @@
//! Deblocking filter (8.7) for progressive frames, 4:2:0, 8-bit.
use crate::h264::picture::{MbKind, MbState};
use crate::h264::slicedec::{PicState, SliceInfo};
use crate::h264::tables::{ALPHA, BETA, TC0};
/// Reference picture identity + mvs of one 4x4 block, for bS = 1 decisions.
#[derive(Clone, Copy)]
struct Motion {
ids: [u32; 2],
mv: [[i16; 2]; 2],
count: u8,
}
/// Referenced picture ids per 8x8 block and list (u32::MAX = list unused).
fn ref_ids8(st: &MbState, sl: &SliceInfo) -> [[u32; 2]; 4] {
std::array::from_fn(|b8| {
std::array::from_fn(|l| {
let r = st.ref_idx[l][b8];
if r >= 0 { sl.ref_ids[l].get(r as usize).copied().unwrap_or(u32::MAX - 1) } else { u32::MAX }
})
})
}
#[inline(always)]
fn block_motion(st: &MbState, ids8: &[[u32; 2]; 4], raster: usize) -> Motion {
let b8 = (raster >> 3) * 2 + ((raster & 3) >> 1);
let ids = ids8[b8];
let count = (ids[0] != u32::MAX) as u8 + (ids[1] != u32::MAX) as u8;
Motion { ids, mv: [st.mv[0][raster], st.mv[1][raster]], count }
}
#[inline(always)]
fn mv_far(a: [i16; 2], b: [i16; 2]) -> bool {
(a[0] as i32 - b[0] as i32).abs() >= 4 || (a[1] as i32 - b[1] as i32).abs() >= 4
}
#[inline]
fn motion_bs(p: &Motion, q: &Motion) -> u8 {
if p.ids == q.ids && p.mv == q.mv {
return 0;
}
if p.count != q.count {
return 1;
}
if p.count == 1 {
let (pi, pm) = if p.ids[0] != u32::MAX { (p.ids[0], p.mv[0]) } else { (p.ids[1], p.mv[1]) };
let (qi, qm) = if q.ids[0] != u32::MAX { (q.ids[0], q.mv[0]) } else { (q.ids[1], q.mv[1]) };
if pi != qi {
return 1;
}
return mv_far(pm, qm) as u8;
}
if p.count == 0 {
return 0;
}
// two motion vectors each
let same_set = (p.ids[0] == q.ids[0] && p.ids[1] == q.ids[1]) || (p.ids[0] == q.ids[1] && p.ids[1] == q.ids[0]);
if !same_set {
return 1;
}
if p.ids[0] != p.ids[1] {
// two different reference pictures: compare mvs referring to the same picture
if p.ids[0] == q.ids[0] {
(mv_far(p.mv[0], q.mv[0]) || mv_far(p.mv[1], q.mv[1])) as u8
} else {
(mv_far(p.mv[0], q.mv[1]) || mv_far(p.mv[1], q.mv[0])) as u8
}
} else {
// both mvs refer to the same picture
((mv_far(p.mv[0], q.mv[0]) || mv_far(p.mv[1], q.mv[1])) && (mv_far(p.mv[0], q.mv[1]) || mv_far(p.mv[1], q.mv[0]))) as u8
}
}
#[cfg(test)]
fn clip3(lo: i32, hi: i32, v: i32) -> i32 {
v.clamp(lo, hi)
}
/// Filter one set of eight samples p3 p2 p1 p0 | q0 q1 q2 q3 across an edge (8.7.2.3 / 8.7.2.4):
/// the per-line reference for [`filter_lanes`].
#[cfg(test)]
fn filter8(v: &mut [u8; 8], bs: u8, alpha: i32, beta: i32, tc0: i32, chroma: bool) {
let p0 = v[3] as i32;
let q0 = v[4] as i32;
let p1 = v[2] as i32;
let q1 = v[5] as i32;
if (p0 - q0).abs() >= alpha || (p1 - p0).abs() >= beta || (q1 - q0).abs() >= beta {
return;
}
if chroma {
if bs < 4 {
let tc = tc0 + 1;
let delta = clip3(-tc, tc, (((q0 - p0) << 2) + (p1 - q1) + 4) >> 3);
v[3] = (p0 + delta).clamp(0, 255) as u8;
v[4] = (q0 - delta).clamp(0, 255) as u8;
} else {
v[3] = ((2 * p1 + p0 + q1 + 2) >> 2) as u8;
v[4] = ((2 * q1 + q0 + p1 + 2) >> 2) as u8;
}
return;
}
let p2 = v[1] as i32;
let q2 = v[6] as i32;
let ap = (p2 - p0).abs();
let aq = (q2 - q0).abs();
if bs < 4 {
let tc = tc0 + (ap < beta) as i32 + (aq < beta) as i32;
let delta = clip3(-tc, tc, (((q0 - p0) << 2) + (p1 - q1) + 4) >> 3);
v[3] = (p0 + delta).clamp(0, 255) as u8;
v[4] = (q0 - delta).clamp(0, 255) as u8;
if ap < beta {
v[2] = (p1 + clip3(-tc0, tc0, (p2 + ((p0 + q0 + 1) >> 1) - (p1 << 1)) >> 1)) as u8;
}
if aq < beta {
v[5] = (q1 + clip3(-tc0, tc0, (q2 + ((p0 + q0 + 1) >> 1) - (q1 << 1)) >> 1)) as u8;
}
} else {
let strong = (p0 - q0).abs() < ((alpha >> 2) + 2);
if ap < beta && strong {
let p3 = v[0] as i32;
v[3] = ((p2 + 2 * p1 + 2 * p0 + 2 * q0 + q1 + 4) >> 3) as u8;
v[2] = ((p2 + p1 + p0 + q0 + 2) >> 2) as u8;
v[1] = ((2 * p3 + 3 * p2 + p1 + p0 + q0 + 4) >> 3) as u8;
} else {
v[3] = ((2 * p1 + p0 + q1 + 2) >> 2) as u8;
}
if aq < beta && strong {
let q3 = v[7] as i32;
v[4] = ((p1 + 2 * p0 + 2 * q0 + 2 * q1 + q2 + 4) >> 3) as u8;
v[5] = ((p0 + q0 + q1 + q2 + 2) >> 2) as u8;
v[6] = ((2 * q3 + 3 * q2 + q1 + q0 + p0 + 4) >> 3) as u8;
} else {
v[4] = ((2 * q1 + q0 + p1 + 2) >> 2) as u8;
}
}
}
/// Edge filter parameters for `n` lines; `bs[i]` / `tc0[i]` apply to lines `i * n / 4 .. (i + 1) * n / 4`.
struct EdgeParams {
bs: [u8; 4],
tc0: [i32; 4],
alpha: i32,
beta: i32,
}
/// Filter one edge for `N` lines at once (lane `i` = line `i` across the edge). `s[k][i]` holds
/// sample p3 p2 p1 p0 q0 q1 q2 q3 (k = 0..8) of line `i`. Branch-free per lane, so the loops
/// vectorise; bit-exact with [`filter8`] applied to every line (8.7.2.3 / 8.7.2.4).
#[inline(always)]
fn filter_lanes<const N: usize>(s: &mut [[i16; N]; 8], ep: &EdgeParams, chroma: bool) {
let per = N / 4;
let (alpha, beta) = (ep.alpha as i16, ep.beta as i16);
let mut bs_on = [false; N];
let mut tc0 = [0i16; N];
for k in 0..4 {
for i in k * per..(k + 1) * per {
bs_on[i] = ep.bs[k] != 0;
tc0[i] = ep.tc0[k] as i16;
}
}
let [p3, p2, p1, p0, q0, q1, q2, q3] = *s;
// Masks are 0 / -1 per lane, selects are bitwise, and clipping to per-lane bounds uses
// min / max (`clamp` would assert its bounds per lane), so every loop below vectorises.
let lt = |x: i16, y: i16| -((x < y) as i16);
let sel = |m: i16, a: i16, b: i16| (a & m) | (b & !m);
let mut on = [0i16; N];
let mut any = 0i16;
for i in 0..N {
on[i] = -(bs_on[i] as i16) & lt((p0[i] - q0[i]).abs(), alpha) & lt((p1[i] - p0[i]).abs(), beta) & lt((q1[i] - q0[i]).abs(), beta);
any |= on[i];
}
if any == 0 {
return;
}
// bS 4 is only ever assigned to all four segments of an edge
let strong_edge = ep.bs[0] >= 4;
if chroma {
for i in 0..N {
let (np0, nq0) = if strong_edge {
((2 * p1[i] + p0[i] + q1[i] + 2) >> 2, (2 * q1[i] + q0[i] + p1[i] + 2) >> 2)
} else {
let tc = tc0[i] + 1;
let delta = ((((q0[i] - p0[i]) << 2) + (p1[i] - q1[i]) + 4) >> 3).max(-tc).min(tc);
((p0[i] + delta).clamp(0, 255), (q0[i] - delta).clamp(0, 255))
};
s[3][i] = sel(on[i], np0, p0[i]);
s[4][i] = sel(on[i], nq0, q0[i]);
}
return;
}
if strong_edge {
let lim = (alpha >> 2) + 2;
for i in 0..N {
let (a, b, c, d, e, f, g, h) = (p3[i], p2[i], p1[i], p0[i], q0[i], q1[i], q2[i], q3[i]);
let strong = lt((d - e).abs(), lim);
let sp = on[i] & lt((b - d).abs(), beta) & strong;
let sq = on[i] & lt((g - e).abs(), beta) & strong;
let wp0 = (2 * c + d + f + 2) >> 2;
let wq0 = (2 * f + e + c + 2) >> 2;
let sp0 = (b + 2 * c + 2 * d + 2 * e + f + 4) >> 3;
let sq0 = (c + 2 * d + 2 * e + 2 * f + g + 4) >> 3;
s[3][i] = sel(sp, sp0, sel(on[i], wp0, d));
s[2][i] = sel(sp, (b + c + d + e + 2) >> 2, c);
s[1][i] = sel(sp, (2 * a + 3 * b + c + d + e + 4) >> 3, b);
s[4][i] = sel(sq, sq0, sel(on[i], wq0, e));
s[5][i] = sel(sq, (d + e + f + g + 2) >> 2, f);
s[6][i] = sel(sq, (2 * h + 3 * g + f + e + d + 4) >> 3, g);
}
} else {
for i in 0..N {
let ap = lt((p2[i] - p0[i]).abs(), beta);
let aq = lt((q2[i] - q0[i]).abs(), beta);
let t0 = tc0[i];
let tc = t0 - ap - aq;
let delta = ((((q0[i] - p0[i]) << 2) + (p1[i] - q1[i]) + 4) >> 3).max(-tc).min(tc);
let avg = (p0[i] + q0[i] + 1) >> 1;
let np1 = p1[i] + ((p2[i] + avg - (p1[i] << 1)) >> 1).max(-t0).min(t0);
let nq1 = q1[i] + ((q2[i] + avg - (q1[i] << 1)) >> 1).max(-t0).min(t0);
s[3][i] = sel(on[i], (p0[i] + delta).clamp(0, 255), p0[i]);
s[4][i] = sel(on[i], (q0[i] - delta).clamp(0, 255), q0[i]);
s[2][i] = sel(on[i] & ap, np1, p1[i]);
s[5][i] = sel(on[i] & aq, nq1, q1[i]);
}
}
}
/// Filter a vertical edge whose q0 column is at `x`, for `N` lines starting at row `y`.
#[inline(always)]
fn filter_vertical<const N: usize>(pix: &mut [u8], stride: usize, x: usize, y: usize, ep: &EdgeParams, chroma: bool) {
// Each line's eight samples as one little-endian word: byte k = sample k (p3 .. q3).
let mut w = [0u64; N];
for (i, w) in w.iter_mut().enumerate() {
let o = (y + i) * stride + x - 4;
let Some(&b) = pix.get(o..).and_then(<[_]>::first_chunk::<8>) else {
return;
};
*w = u64::from_le_bytes(b);
}
let mut s = [[0i16; N]; 8];
for (k, row) in s.iter_mut().enumerate() {
for i in 0..N {
row[i] = ((w[i] >> (8 * k)) & 0xff) as i16;
}
}
filter_lanes(&mut s, ep, chroma);
for (i, w) in w.iter_mut().enumerate() {
*w = (0..8).fold(0u64, |acc, k| acc | ((s[k][i] as u8 as u64) << (8 * k)));
}
for (i, w) in w.iter().enumerate() {
let o = (y + i) * stride + x - 4;
pix[o..o + 8].copy_from_slice(&w.to_le_bytes());
}
}
/// Filter a horizontal edge whose q0 row is `y`, for `N` columns starting at `x`.
#[inline(always)]
fn filter_horizontal<const N: usize>(pix: &mut [u8], stride: usize, x: usize, y: usize, ep: &EdgeParams, chroma: bool) {
let base = (y - 4) * stride + x;
let mut s = [[0i16; N]; 8];
for (k, row) in s.iter_mut().enumerate() {
let Some(src) = pix.get(base + k * stride..).and_then(<[_]>::first_chunk::<N>) else {
return;
};
for i in 0..N {
row[i] = src[i] as i16;
}
}
filter_lanes(&mut s, ep, chroma);
let (k0, k1) = if chroma { (3, 5) } else { (1, 7) };
for (k, row) in s.iter().enumerate().take(k1).skip(k0) {
let Some(dst) = pix.get_mut(base + k * stride..).and_then(<[_]>::first_chunk_mut::<N>) else {
return;
};
for i in 0..N {
dst[i] = row[i] as u8;
}
}
}
/// Deblock one macroblock (macroblocks must be processed in raster order).
pub fn deblock_mb(pic: &mut PicState, addr: usize, mb_w: usize) {
let q = &pic.mbs[addr];
if q.slice_num == u32::MAX {
return;
}
let sq = &pic.slices[q.slice_num as usize];
if sq.disable_deblocking_filter_idc == 1 {
return;
}
let (mx, my) = (addr % mb_w, addr / mb_w);
let usable = |n: Option<usize>| -> Option<usize> {
let n = n?;
let st = &pic.mbs[n];
if st.slice_num == u32::MAX || (sq.disable_deblocking_filter_idc == 2 && st.slice_num != q.slice_num) {
return None;
}
Some(n)
};
let left = usable(if mx > 0 { Some(addr - 1) } else { None });
let top = usable(if my > 0 { Some(addr - mb_w) } else { None });
let alpha_off = sq.alpha_offset;
let beta_off = sq.beta_offset;
let t8 = q.transform_8x8;
let bs = boundary_strengths(pic, q, sq, left, top, t8);
let qp_of = |st: &MbState| if st.kind == MbKind::IPcm { 0 } else { st.qp as i32 };
let q_qp = qp_of(q);
let q_qpc = q.qpc;
let p_qp = [left.map(|n| (qp_of(&pic.mbs[n]), pic.mbs[n].qpc)), top.map(|n| (qp_of(&pic.mbs[n]), pic.mbs[n].qpc))];
let params = |qpp: i32, qpq: i32, b: [u8; 4]| {
let qpav = (qpp + qpq + 1) >> 1;
let index_a = (qpav + alpha_off).clamp(0, 51) as usize;
let index_b = (qpav + beta_off).clamp(0, 51) as usize;
let tc = |b: u8| if (1..4).contains(&b) { TC0[index_a][b as usize - 1] as i32 } else { 0 };
EdgeParams { bs: b, tc0: [tc(b[0]), tc(b[1]), tc(b[2]), tc(b[3])], alpha: ALPHA[index_a] as i32, beta: BETA[index_b] as i32 }
};
filter_luma(pic, (mx, my), &bs, q_qp, p_qp, &params);
filter_chroma(pic, (mx, my), &bs, q_qpc, p_qp, &params);
}
fn boundary_strengths(pic: &PicState, q: &MbState, sq: &SliceInfo, left: Option<usize>, top: Option<usize>, t8: bool) -> [[[u8; 4]; 4]; 2] {
// bS[dir][edge][segment]; dir 0 = vertical edges (x), 1 = horizontal edges (y)
let mut bs = [[[0u8; 4]; 4]; 2];
if q.kind.is_intra() {
for dir in 0..2 {
if (if dir == 0 { left } else { top }).is_some() {
bs[dir][0] = [4; 4];
}
for e in 1..4 {
if !(t8 && e % 2 == 1) {
bs[dir][e] = [3; 4];
}
}
}
} else {
let qids = ref_ids8(q, sq);
let q_uniform = (0..2).all(|l| {
let r = q.ref_idx[l];
r[1] == r[0] && r[2] == r[0] && r[3] == r[0] && q.mv[l].iter().all(|m| *m == q.mv[l][0])
});
let qm: [Motion; 16] = std::array::from_fn(|r| block_motion(q, &qids, r));
for dir in 0..2 {
let neighbor = if dir == 0 { left } else { top };
if let Some(n) = neighbor {
let p = &pic.mbs[n];
if p.kind.is_intra() {
bs[dir][0] = [4; 4];
} else {
let pids = ref_ids8(p, &pic.slices[p.slice_num as usize]);
for k in 0..4 {
let (rq, rp) = if dir == 0 { (k * 4, k * 4 + 3) } else { (k, 12 + k) };
bs[dir][0][k] = if (p.nz_mask >> rp) & 1 != 0 || (q.nz_mask >> rq) & 1 != 0 {
2
} else {
motion_bs(&block_motion(p, &pids, rp), &qm[rq])
};
}
}
}
if q_uniform && q.nz_mask == 0 {
// one motion for the whole MB and no coefficients: all internal edges have bS 0
continue;
}
for e in 1..4 {
if t8 && e % 2 == 1 {
continue;
}
for k in 0..4 {
let (rq, rp) = if dir == 0 { (k * 4 + e, k * 4 + e - 1) } else { (e * 4 + k, e * 4 + k - 4) };
bs[dir][e][k] = if (q.nz_mask >> rp) & 1 != 0 || (q.nz_mask >> rq) & 1 != 0 { 2 } else { motion_bs(&qm[rp], &qm[rq]) };
}
}
}
}
bs
}
fn filter_luma(
pic: &mut PicState,
(mx, my): (usize, usize),
bs: &[[[u8; 4]; 4]; 2],
q_qp: i32,
p_qp: [Option<(i32, [u8; 2])>; 2],
params: &impl Fn(i32, i32, [u8; 4]) -> EdgeParams,
) {
let width = pic.planes.width;
for dir in 0..2 {
for e in 0..4 {
if bs[dir][e] == [0; 4] {
continue;
}
let qpp = if e == 0 { p_qp[dir].map(|p| p.0).unwrap_or(q_qp) } else { q_qp };
let ep = params(qpp, q_qp, bs[dir][e]);
if ep.alpha == 0 || ep.beta == 0 {
// filterSamplesFlag needs |p0 - q0| < alpha and |p1 - p0| < beta (8.7.2.2): no
// sample of the edge changes (low QPs)
continue;
}
if dir == 0 {
filter_vertical::<16>(&mut pic.planes.y, width, mx * 16 + e * 4, my * 16, &ep, false);
} else {
filter_horizontal::<16>(&mut pic.planes.y, width, mx * 16, my * 16 + e * 4, &ep, false);
}
}
}
}
fn filter_chroma(
pic: &mut PicState,
(mx, my): (usize, usize),
bs: &[[[u8; 4]; 4]; 2],
q_qpc: [u8; 2],
p_qp: [Option<(i32, [u8; 2])>; 2],
params: &impl Fn(i32, i32, [u8; 4]) -> EdgeParams,
) {
let cwidth = pic.planes.cwidth;
// chroma (4:2:0): edges at chroma samples 0 and 4 use the bS of luma edges 0 and 2
for c in 0..2 {
for dir in 0..2 {
for ce in 0..2 {
let e = ce * 2;
if bs[dir][e] == [0; 4] {
continue;
}
let qpp = if e == 0 { p_qp[dir].map(|p| p.1[c]).unwrap_or(q_qpc[c]) } else { q_qpc[c] } as i32;
let ep = params(qpp, q_qpc[c] as i32, bs[dir][e]);
if ep.alpha == 0 || ep.beta == 0 {
continue;
}
let plane = if c == 0 { &mut pic.planes.cb } else { &mut pic.planes.cr };
if dir == 0 {
filter_vertical::<8>(plane, cwidth, mx * 8 + ce * 4, my * 8, &ep, true);
} else {
filter_horizontal::<8>(plane, cwidth, mx * 8, my * 8 + ce * 4, &ep, true);
}
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
/// The vectorised edge filter equals the per-line reference [`filter8`] on random edges
/// (small differences across the edge so that every filter branch is taken).
#[test]
fn lane_filter_matches_per_line_reference() {
let mut rng = 0x9e37_79b9_7f4a_7c15u64;
let mut next = move || {
rng ^= rng << 13;
rng ^= rng >> 7;
rng ^= rng << 17;
rng
};
for iter in 0..20_000 {
let chroma = iter % 3 == 0;
let strong = iter % 4 == 1;
let bs: [u8; 4] = if strong { [4; 4] } else { std::array::from_fn(|_| (next() % 4) as u8) };
let index_a = (next() % 52) as usize;
let index_b = (next() % 52) as usize;
let tc = |b: u8| if (1..4).contains(&b) { TC0[index_a][b as usize - 1] as i32 } else { 0 };
let ep = EdgeParams { bs, tc0: bs.map(tc), alpha: ALPHA[index_a] as i32, beta: BETA[index_b] as i32 };
let base = (next() % 256) as i32;
let spread = 1 + (next() % 24) as i32;
let mut lines = [[0u8; 8]; 16];
for l in lines.iter_mut() {
for v in l.iter_mut() {
*v = (base + (next() % (2 * spread as u64 + 1)) as i32 - spread).clamp(0, 255) as u8;
}
}
let n = if chroma { 8 } else { 16 };
let mut want = lines;
for (i, l) in want.iter_mut().enumerate().take(n) {
let k = i / (n / 4);
if bs[k] != 0 {
filter8(l, bs[k], ep.alpha, ep.beta, ep.tc0[k], chroma);
}
}
let got: Vec<[u8; 8]> = if chroma {
let mut s = [[0i16; 8]; 8];
for i in 0..8 {
for k in 0..8 {
s[k][i] = lines[i][k] as i16;
}
}
filter_lanes(&mut s, &ep, true);
(0..8).map(|i| std::array::from_fn(|k| s[k][i] as u8)).collect()
} else {
let mut s = [[0i16; 16]; 8];
for i in 0..16 {
for k in 0..8 {
s[k][i] = lines[i][k] as i16;
}
}
filter_lanes(&mut s, &ep, false);
(0..16).map(|i| std::array::from_fn(|k| s[k][i] as u8)).collect()
};
assert_eq!(&got[..], &want[..n], "iteration {iter}: bs {bs:?} indexA {index_a} indexB {index_b}");
}
}
}
#[cfg(test)]
mod speed {
use super::*;
/// Rough timing of the edge filters (`cargo test --release -p soundcraft-video --lib edge_filter_speed -- --ignored --nocapture`).
#[test]
#[ignore]
fn edge_filter_speed() {
let (w, h) = (1024usize, 256usize);
let mut pix: Vec<u8> = (0..w * h).map(|i| (128 + ((i * 7919) % 13) as i32 - 6) as u8).collect();
let eps = [
EdgeParams { bs: [4; 4], tc0: [0; 4], alpha: 40, beta: 9 },
EdgeParams { bs: [3; 4], tc0: [2; 4], alpha: 40, beta: 9 },
EdgeParams { bs: [2, 1, 0, 2], tc0: [1, 1, 0, 1], alpha: 40, beta: 9 },
];
let t0 = std::time::Instant::now();
let mut n = 0;
for _ in 0..20 {
for my in 1..h / 16 {
for mx in 1..w / 16 {
for (j, ep) in eps.iter().enumerate() {
filter_horizontal::<16>(&mut pix, w, mx * 16, my * 16 + j * 4, ep, false);
n += 1;
}
}
}
}
let dt = t0.elapsed();
let tv = std::time::Instant::now();
for _ in 0..20 {
for my in 1..h / 16 {
for mx in 1..w / 16 {
for (j, ep) in eps.iter().enumerate() {
filter_vertical::<16>(&mut pix, w, mx * 16 + j * 4, my * 16, ep, false);
}
}
}
}
println!("vertical: {:.1} ns/edge", tv.elapsed().as_nanos() as f64 / n as f64);
let t1 = std::time::Instant::now();
for _ in 0..20 {
for my in 1..h / 16 {
for mx in 1..w / 16 {
for ep in eps.iter() {
for (vert, x, y) in [(true, mx * 16, my * 16), (false, mx * 16, my * 16)] {
for i in 0..16 {
let b = ep.bs[i / 4];
if b == 0 {
continue;
}
let mut v = [0u8; 8];
for k in 0..8 {
v[k] = if vert { pix[(y + i) * w + x - 4 + k] } else { pix[(y - 4 + k) * w + x + i] };
}
filter8(&mut v, b, ep.alpha, ep.beta, ep.tc0[i / 4], false);
for k in 1..7 {
if vert {
pix[(y + i) * w + x - 4 + k] = v[k];
} else {
pix[(y - 4 + k) * w + x + i] = v[k];
}
}
}
}
}
}
}
}
println!("scalar reference (both): {:.1} ns/edge", t1.elapsed().as_nanos() as f64 / (2 * n) as f64);
println!(
"horizontal: {n} luma edges in {dt:?}: {:.1} ns/edge (checksum {})",
dt.as_nanos() as f64 / n as f64,
pix.iter().map(|&v| v as u64).sum::<u64>()
);
}
}
-645
View File
@@ -1,645 +0,0 @@
//! Top-level decoder: NAL dispatch, picture boundaries, POC, DPB and output.
//!
//! The calling thread parses headers and does all DPB / reference list bookkeeping (which only needs
//! header information). Macroblock decoding of each picture runs as a job — on a thread pool with the
//! `threads` feature — that publishes finished macroblock rows into the picture's shared [`Frame`];
//! jobs of later pictures block per row on the reference data they need, so several pictures decode
//! concurrently (frame-level parallelism).
use crate::bitstream::{BitReader, annexb_nals, length_prefixed_nals, unescape_rbsp};
use crate::h264::dpb::{Dpb, Output, OutputMeta};
use crate::h264::error::{Error, Result, ensure, invalid, unsupported};
use crate::h264::params::{Pps, Sps};
use crate::h264::picture::{Frame, FrameRef, MbKind, MbState, Planes, RefPic};
use crate::h264::slice::{NalHeader, Poc, PocState, SliceHeader, SliceType, nal_type};
use crate::h264::slicedec::{PicState, SliceDecoder};
use crate::h264::transform::LevelScale;
use crate::h264::{ColorInfo, Picture};
use std::collections::VecDeque;
use std::sync::{Arc, Mutex};
/// One slice of a picture job.
struct SliceJob {
sh: SliceHeader,
pps: Arc<Pps>,
sps: Arc<Sps>,
rbsp: Vec<u8>,
refs: [Vec<RefPic>; 2],
ls: Arc<LevelScale>,
}
/// A picture whose slices are being collected.
struct PendingPic {
frame: FrameRef,
sps: Arc<Sps>,
first: SliceHeader,
poc: Poc,
pts: i64,
key: bool,
slices: Vec<SliceJob>,
}
/// Counters describing what the decoder has seen (useful to check test coverage).
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct DecodeStats {
pub pictures: u64,
pub slices_cavlc: u64,
pub slices_cabac: u64,
pub slices_i: u64,
pub slices_p: u64,
pub slices_b: u64,
pub mb_i4x4: u64,
pub mb_i8x8: u64,
pub mb_i16x16: u64,
pub mb_pcm: u64,
pub mb_p_skip: u64,
pub mb_b_skip: u64,
pub mb_b_direct16x16: u64,
pub mb_inter: u64,
/// Inter macroblocks using the 8x8 transform.
pub mb_inter_8x8_transform: u64,
pub mmco_ops: u64,
pub long_term_marks: u64,
pub frame_num_gaps: u64,
pub weighted_slices: u64,
pub temporal_direct_slices: u64,
/// Non-reference pictures decoded in draft mode (deblocking skipped).
pub draft_pictures: u64,
}
/// State shared with decoding jobs.
#[derive(Default)]
struct Shared {
error: Mutex<Option<Error>>,
stats: Mutex<DecodeStats>,
/// Recycled picture buffers.
pool: Mutex<Vec<PicState>>,
}
/// H.264 decoder.
pub struct Decoder {
spss: Vec<Option<Arc<Sps>>>,
ppss: Vec<Option<Arc<Pps>>>,
nal_length_size: Option<usize>,
dpb: Dpb,
poc_state: PocState,
prev_ref_frame_num: u32,
pending: Option<PendingPic>,
next_id: u32,
active_sps: Option<Arc<Sps>>,
ls_cache: Vec<(Arc<Pps>, Arc<LevelScale>)>,
/// Pictures leaving the DPB, converted once their decoding job has finished.
out_queue: VecDeque<Output>,
shared: Arc<Shared>,
#[cfg(feature = "threads")]
pool: Option<rayon::ThreadPool>,
in_flight: VecDeque<FrameRef>,
max_in_flight: usize,
threads: usize,
/// Draft mode: skip deblocking of non-reference pictures ([`Decoder::set_draft`]).
draft: bool,
}
impl Default for Decoder {
fn default() -> Self {
Self::new()
}
}
/// Is `sh` the first slice of a new primary coded picture relative to `prev` (7.4.1.2.4)?
fn is_new_picture(prev: &SliceHeader, sh: &SliceHeader, sps: &Sps) -> bool {
if sh.frame_num != prev.frame_num
|| sh.pps_id != prev.pps_id
|| sh.field_pic != prev.field_pic
|| sh.bottom_field != prev.bottom_field
|| (sh.nal_ref_idc == 0) != (prev.nal_ref_idc == 0)
|| sh.idr != prev.idr
|| (sh.idr && prev.idr && sh.idr_pic_id != prev.idr_pic_id)
{
return true;
}
if sps.pic_order_cnt_type == 0
&& (sh.pic_order_cnt_lsb != prev.pic_order_cnt_lsb || sh.delta_pic_order_cnt_bottom != prev.delta_pic_order_cnt_bottom)
{
return true;
}
if sps.pic_order_cnt_type == 1 && sh.delta_pic_order_cnt != prev.delta_pic_order_cnt {
return true;
}
false
}
/// The worker threads [`Decoder::new`] uses: the available cores (at most 16) with the `threads`
/// feature on native targets, 1 otherwise.
pub fn default_threads() -> usize {
#[cfg(all(feature = "threads", not(target_arch = "wasm32")))]
{
std::thread::available_parallelism().map(|n| n.get()).unwrap_or(1).min(16)
}
#[cfg(not(all(feature = "threads", not(target_arch = "wasm32"))))]
{
1
}
}
impl Decoder {
/// A decoder using all available cores (with the `threads` feature).
pub fn new() -> Self {
Self::with_threads(default_threads())
}
/// A decoder using up to `threads` worker threads (1 = decode on the calling thread).
pub fn with_threads(threads: usize) -> Self {
crate::h264::cavlc::init_tables();
#[cfg(feature = "threads")]
let pool = if threads > 1 && cfg!(not(target_arch = "wasm32")) {
rayon::ThreadPoolBuilder::new().num_threads(threads).thread_name(|i| format!("h264-{i}")).build().ok()
} else {
None
};
#[cfg(feature = "threads")]
let workers = if pool.is_some() { threads } else { 1 };
#[cfg(not(feature = "threads"))]
let workers = 1;
Decoder {
spss: vec![None; 32],
ppss: vec![None; 256],
nal_length_size: None,
dpb: Dpb::new(),
poc_state: PocState::default(),
prev_ref_frame_num: 0,
pending: None,
next_id: 1,
active_sps: None,
ls_cache: Vec::new(),
out_queue: VecDeque::new(),
shared: Arc::new(Shared::default()),
#[cfg(feature = "threads")]
pool,
in_flight: VecDeque::new(),
max_in_flight: threads.max(1) + 2,
threads: workers,
draft: false,
}
}
/// Worker threads decoding pictures (1: on the calling thread).
pub fn threads(&self) -> usize {
self.threads
}
/// Draft mode (off by default) for reduced-resolution playback: pictures submitted from now
/// on that are non-reference pictures (`nal_ref_idc` 0) skip the deblocking filter (8.7).
/// Nothing predicts from a non-reference picture (and deblocking never changes the motion
/// data used for direct prediction), so every other picture stays bit-exact; the draft
/// pictures themselves are approximate and flagged [`Picture::draft`]. Never use it for
/// export or for a frame shown while paused.
pub fn set_draft(&mut self, on: bool) {
self.draft = on;
}
pub fn draft(&self) -> bool {
self.draft
}
/// Configure from an `avcC` (AVCDecoderConfigurationRecord) box payload.
pub fn from_avcc(avcc: &[u8]) -> Result<Self> {
let mut d = Decoder::new();
d.configure_avcc(avcc)?;
Ok(d)
}
/// Parse an `avcC` record: SPS/PPS and the NAL length size used by [`Decoder::decode`].
pub fn configure_avcc(&mut self, avcc: &[u8]) -> Result<()> {
ensure!(avcc.len() >= 7, "avcC too short");
ensure!(avcc[0] == 1, "unsupported avcC version {}", avcc[0]);
self.nal_length_size = Some((avcc[4] & 3) as usize + 1);
let mut pos = 5;
let read_sets = |pos: &mut usize, count: usize, d: &mut Decoder| -> Result<()> {
for _ in 0..count {
ensure!(*pos + 2 <= avcc.len(), "avcC truncated");
let len = u16::from_be_bytes([avcc[*pos], avcc[*pos + 1]]) as usize;
*pos += 2;
ensure!(*pos + len <= avcc.len(), "avcC truncated");
d.handle_nal(&avcc[*pos..*pos + len], 0)?;
*pos += len;
}
Ok(())
};
let nsps = (avcc[pos] & 0x1f) as usize;
pos += 1;
read_sets(&mut pos, nsps, self)?;
ensure!(pos < avcc.len(), "avcC truncated");
let npps = avcc[pos] as usize;
pos += 1;
read_sets(&mut pos, npps, self)
}
/// Length of NAL length prefixes (from avcC), or None for Annex-B input.
pub fn nal_length_size(&self) -> Option<usize> {
self.nal_length_size
}
/// Decode one access unit (all NAL units of one picture, Annex-B or length-prefixed per
/// configuration; a byte stream with several complete access units is accepted too). Returns
/// pictures that became ready for output, in output order; `pts` travels with the picture of
/// this access unit through reordering.
pub fn decode(&mut self, data: &[u8], pts: i64) -> Result<Vec<Picture>> {
let nals = match self.nal_length_size {
Some(n) => length_prefixed_nals(data, n)?,
None => annexb_nals(data),
};
let mut result = Ok(());
for nal in nals {
if let Err(e) = self.handle_nal(nal, pts) {
result = Err(e);
break;
}
}
// The access unit is complete: hand its picture to a decoding job.
self.submit_pending();
if let Some(e) = self.take_error() {
result = result.and(Err(e));
}
result?;
// Return finished pictures without blocking on frames still being decoded (bounded latency).
let max_queue = self.max_in_flight * 2;
let mut pics = Vec::new();
while let Some(o) = self.out_queue.front() {
if !o.frame.is_published(o.frame.mb_h() - 1) && self.out_queue.len() <= max_queue {
break;
}
let Some(o) = self.out_queue.pop_front() else { break };
pics.push(make_picture(&o));
}
Ok(pics)
}
/// Output all remaining pictures (end of stream).
pub fn flush(&mut self) -> Vec<Picture> {
self.submit_pending();
let mut outs = Vec::new();
self.dpb.flush(&mut outs);
self.emit(outs);
let pics = self.out_queue.drain(..).map(|o| make_picture(&o)).collect();
while let Some(f) = self.in_flight.pop_front() {
f.wait_complete();
}
pics
}
/// Statistics accumulated so far (macroblock counts include finished pictures only).
pub fn stats(&self) -> DecodeStats {
self.shared.stats.lock().map(|s| s.clone()).unwrap_or_default()
}
fn stat(&self, f: impl FnOnce(&mut DecodeStats)) {
if let Ok(mut s) = self.shared.stats.lock() {
f(&mut s);
}
}
/// First error reported by a decoding job since the last call (also returned by
/// [`Decoder::decode`]); useful after [`Decoder::flush`].
pub fn take_error(&mut self) -> Option<Error> {
self.shared.error.lock().ok().and_then(|mut e| e.take())
}
fn handle_nal(&mut self, nal: &[u8], pts: i64) -> Result<()> {
if nal.is_empty() {
return Ok(());
}
let hdr = NalHeader::parse(nal[0])?;
match hdr.nal_unit_type {
nal_type::SPS => {
let sps = Sps::parse(&unescape_rbsp(&nal[1..]))?;
let id = sps.id as usize;
self.spss[id] = Some(Arc::new(sps));
}
nal_type::PPS => {
let rbsp = unescape_rbsp(&nal[1..]);
let spss: Vec<Option<Sps>> = self.spss.iter().map(|s| s.as_ref().map(|s| (**s).clone())).collect();
let pps = Pps::parse(&rbsp, &spss)?;
let id = pps.id as usize;
self.ppss[id] = Some(Arc::new(pps));
}
nal_type::SLICE | nal_type::IDR => self.handle_slice(nal, hdr, pts)?,
nal_type::SLICE_DPA | nal_type::SLICE_DPB | nal_type::SLICE_DPC => {
return unsupported("data partitioning (Extended profile)");
}
nal_type::END_SEQ | nal_type::END_STREAM => self.submit_pending(),
// SEI, AUD, filler, SPS extension, prefix NAL, subset SPS, auxiliary and extension slices: skipped.
_ => {}
}
Ok(())
}
fn level_scale(&mut self, pps: &Arc<Pps>) -> Arc<LevelScale> {
if let Some((_, ls)) = self.ls_cache.iter().find(|(p, _)| Arc::ptr_eq(p, pps)) {
return ls.clone();
}
let ls: Arc<LevelScale> = Arc::from(LevelScale::new(&pps.scaling));
self.ls_cache.retain(|(p, _)| self.ppss.iter().flatten().any(|q| Arc::ptr_eq(p, q)));
self.ls_cache.push((pps.clone(), ls.clone()));
ls
}
fn handle_slice(&mut self, nal: &[u8], hdr: NalHeader, pts: i64) -> Result<()> {
let rbsp = unescape_rbsp(&nal[1..]);
let ppss = &self.ppss;
let spss = &self.spss;
let mut found: Option<(Arc<Pps>, Arc<Sps>)> = None;
let (sh, _, _) = SliceHeader::parse(&rbsp, hdr, |id| {
let pps = ppss[id as usize].as_ref().ok_or_else(|| Error::MissingParameterSet(format!("PPS {id}")))?;
let sps = spss[pps.sps_id as usize].as_ref().ok_or_else(|| Error::MissingParameterSet(format!("SPS {}", pps.sps_id)))?;
found = Some((pps.clone(), sps.clone()));
Ok((&**pps, &**sps))
})?;
let Some((pps, sps)) = found else {
return Err(Error::MissingParameterSet("PPS".into()));
};
sps.check_supported()?;
if pps.num_slice_groups > 1 {
return unsupported("slice groups (FMO)");
}
if sh.field_pic {
return unsupported("field pictures");
}
if matches!(sh.slice_type, SliceType::Sp | SliceType::Si) {
return unsupported("SP/SI slices");
}
if sh.redundant_pic_cnt > 0 {
return Ok(()); // redundant slices are ignored
}
let new_pic = match &self.pending {
None => true,
Some(p) => is_new_picture(&p.first, &sh, &sps) || sh.first_mb_in_slice == 0 || !Arc::ptr_eq(&p.sps, &sps),
};
if new_pic {
self.submit_pending();
if sh.first_mb_in_slice != 0 && self.active_sps.is_none() {
return invalid("stream does not start with the first slice of a picture");
}
self.start_picture(&sh, &sps, pts)?;
}
let ls = self.level_scale(&pps);
let Some(pending) = self.pending.as_mut() else {
return invalid("slice without a started picture");
};
let refs = self.dpb.build_ref_lists(&sh, pending.poc.frame(), sps.max_frame_num())?;
if !sh.slice_type.is_intra() {
ensure!(!refs[0].is_empty(), "no reference pictures available for inter slice");
if sh.slice_type.is_b() {
ensure!(!refs[1].is_empty(), "empty RefPicList1 in B slice");
}
}
let weighted = (pps.weighted_pred && sh.slice_type.is_p()) || (pps.weighted_bipred_idc != 0 && sh.slice_type.is_b());
let temporal = sh.slice_type.is_b() && !sh.direct_spatial_mv_pred;
let (cabac, st, mmcos, lt) = (
pps.entropy_coding_mode,
sh.slice_type,
sh.mmcos.len() as u64,
sh.long_term_reference as u64 + sh.mmcos.iter().filter(|m| m.op == 3 || m.op == 6).count() as u64,
);
pending.slices.push(SliceJob { sh, pps, sps, rbsp, refs, ls });
self.stat(|s| {
if cabac {
s.slices_cabac += 1;
} else {
s.slices_cavlc += 1;
}
match st {
SliceType::I | SliceType::Si => s.slices_i += 1,
SliceType::P | SliceType::Sp => s.slices_p += 1,
SliceType::B => s.slices_b += 1,
}
s.weighted_slices += weighted as u64;
s.temporal_direct_slices += temporal as u64;
s.mmco_ops += mmcos;
s.long_term_marks += lt;
});
Ok(())
}
fn start_picture(&mut self, sh: &SliceHeader, sps: &Arc<Sps>, pts: i64) -> Result<()> {
// Activate SPS; a resolution change flushes the DPB.
let changed = match &self.active_sps {
None => true,
Some(a) => a.width() != sps.width() || a.height() != sps.height() || a.max_dpb_frames() != sps.max_dpb_frames(),
};
if changed && self.active_sps.is_some() {
let mut outs = Vec::new();
self.dpb.flush(&mut outs);
self.emit(outs);
self.dpb.entries.clear();
}
self.active_sps = Some(sps.clone());
self.dpb.capacity = sps.max_dpb_frames();
self.dpb.max_reorder = sps.max_num_reorder_frames().min(self.dpb.capacity);
let mb_w = sps.pic_width_in_mbs as usize;
let mb_h = sps.frame_height_in_mbs() as usize;
if sh.idr {
let mut outs = Vec::new();
self.dpb.idr(sh.no_output_of_prior_pics, &mut outs);
self.emit(outs);
self.prev_ref_frame_num = 0;
} else {
let max = sps.max_frame_num();
if sh.frame_num != self.prev_ref_frame_num && sh.frame_num != (self.prev_ref_frame_num + 1) % max {
// frame_num gap (8.2.5.2): insert "non-existing" frames. Their samples are never used by
// conforming streams; copy the latest decoded frame for concealment.
let meta = Arc::new(output_meta(sps, pts, false));
let next_id = &mut self.next_id;
let poc_state = &mut self.poc_state;
let last = self.dpb.entries.iter().filter(|e| !e.non_existing).max_by_key(|e| e.frame.id).map(|e| e.frame.clone());
let mut make = |_fnum: u32| {
let id = *next_id;
*next_id += 1;
let planes = match &last {
Some(f) if f.mb_w == mb_w && f.mb_h() == mb_h => {
let mut p = Planes::new(mb_w * 16, mb_h * 16);
let (y, u, v) = f.copy_cropped((0, 0, mb_w * 16, mb_h * 16));
p.y = y;
p.cb = u;
p.cr = v;
p
}
_ => Planes::gray(mb_w * 16, mb_h * 16),
};
(Arc::new(Frame::from_planes(id, 0, &planes)), meta.clone())
};
let mut upd = |fnum: u32| poc_state.update_gap_frame(fnum, sps);
self.shared.stats.lock().map(|mut s| s.frame_num_gaps += 1).ok();
self.dpb.fill_frame_num_gap(self.prev_ref_frame_num, sh.frame_num, max, sps.max_num_ref_frames as usize, &mut make, &mut upd);
self.prev_ref_frame_num = (sh.frame_num + max - 1) % max;
}
}
let poc = self.poc_state.compute(sh, sps);
// MMCO5: the picture's POC becomes relative to itself (tempPicOrderCnt, 8.2.1).
let frame_poc = if sh.has_mmco5() { 0 } else { poc.frame() };
let id = self.next_id;
self.next_id += 1;
let frame = Arc::new(Frame::new(id, frame_poc, mb_w, mb_h));
self.pending = Some(PendingPic { frame, sps: sps.clone(), first: sh.clone(), poc, pts, key: sh.idr, slices: Vec::new() });
Ok(())
}
/// Start decoding the pending picture and do its reference marking / DPB insertion.
fn submit_pending(&mut self) {
let Some(p) = self.pending.take() else { return };
let PendingPic { frame, sps, first, poc, pts, key, slices } = p;
let draft = self.draft && first.nal_ref_idc == 0;
self.dispatch(frame.clone(), slices, draft);
self.poc_state.update(&first, &poc);
if first.nal_ref_idc != 0 {
self.prev_ref_frame_num = if first.has_mmco5() { 0 } else { first.frame_num };
}
let meta = Arc::new(OutputMeta { draft, ..output_meta(&sps, pts, key) });
let mut outs = Vec::new();
let fpoc = frame.poc;
self.dpb.store_picture(&first, frame, fpoc, sps.max_frame_num(), sps.max_num_ref_frames as usize, meta, &mut outs);
self.emit(outs);
}
fn dispatch(&mut self, frame: FrameRef, slices: Vec<SliceJob>, draft: bool) {
let shared = self.shared.clone();
#[cfg(feature = "threads")]
if let Some(pool) = &self.pool {
self.in_flight.retain(|f| !f.is_published(f.mb_h() - 1));
while self.in_flight.len() >= self.max_in_flight {
if let Some(f) = self.in_flight.pop_front() {
f.wait_complete();
}
}
self.in_flight.push_back(frame.clone());
pool.spawn_fifo(move || run_job(frame, slices, &shared, draft));
return;
}
run_job(frame, slices, &shared, draft);
}
fn emit(&mut self, outs: Vec<Output>) {
self.out_queue.extend(outs);
}
}
/// Decode all slices of one picture and publish its rows.
fn run_job(frame: FrameRef, slices: Vec<SliceJob>, shared: &Shared, draft: bool) {
let reuse = shared.pool.lock().ok().and_then(|mut p| p.pop());
let mut pic = match reuse {
Some(mut p) if p.mb_w == frame.mb_w && p.mb_h == frame.mb_h() => {
p.reset(frame.clone());
p
}
_ => PicState::new(frame.clone()),
};
pic.skip_deblock = draft;
let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
let mut first_err = None;
for s in &slices {
if let Err(e) = decode_slice(s, &mut pic) {
first_err.get_or_insert(e);
}
}
pic.flush_rows(true);
first_err
}));
let err = match result {
Ok(e) => e,
Err(_) => {
// Make sure readers never block on this frame.
let fallback = Planes::gray(frame.width, frame.height);
let blank = vec![MbState::default(); frame.mb_w * frame.mb_h()];
for r in 0..frame.mb_h() {
if !frame.is_published(r) {
frame.publish(r, Frame::make_row(&fallback, r, &blank, &|_, _, _| u32::MAX));
}
}
pic = PicState::new(frame.clone());
Some(Error::Invalid("internal error while decoding a picture".into()))
}
};
if let Ok(mut s) = shared.stats.lock() {
s.pictures += 1;
s.draft_pictures += draft as u64;
for st in &pic.mbs {
match st.kind {
MbKind::I4x4 => s.mb_i4x4 += 1,
MbKind::I8x8 => s.mb_i8x8 += 1,
MbKind::I16x16 => s.mb_i16x16 += 1,
MbKind::IPcm => s.mb_pcm += 1,
MbKind::PSkip => s.mb_p_skip += 1,
MbKind::BSkip => s.mb_b_skip += 1,
MbKind::BDirect16x16 => s.mb_b_direct16x16 += 1,
MbKind::Inter => s.mb_inter += 1,
MbKind::None => {}
}
if st.transform_8x8 && !st.kind.is_intra() {
s.mb_inter_8x8_transform += 1;
}
}
}
if let (Some(e), Ok(mut slot)) = (err, shared.error.lock()) {
slot.get_or_insert(e);
}
if let Ok(mut p) = shared.pool.lock()
&& p.len() < 4
{
p.push(pic);
}
}
fn decode_slice(s: &SliceJob, pic: &mut PicState) -> Result<()> {
let mut sd = SliceDecoder::new(&s.sh, &s.pps, &s.sps, pic, &s.refs, &s.ls)?;
if s.pps.entropy_coding_mode {
sd.decode_cabac(&s.rbsp)
} else {
let mut r = BitReader::new(&s.rbsp);
r.seek_bits(s.sh.header_bits);
sd.decode_cavlc(&mut r)
}
}
fn output_meta(sps: &Sps, pts: i64, key: bool) -> OutputMeta {
let vui = sps.vui.clone().unwrap_or_default();
OutputMeta {
pts,
key,
crop: sps.crop_rect(),
full_range: vui.full_range,
colour_primaries: vui.colour_primaries,
transfer_characteristics: vui.transfer_characteristics,
matrix_coefficients: vui.matrix_coefficients,
sar: vui.sar,
draft: false,
}
}
fn make_picture(o: &Output) -> Picture {
let (cx, cy, cw, ch) = o.meta.crop;
let (cx, cy, cw, ch) = (cx as usize, cy as usize, cw as usize, ch as usize);
let (y, u, v) = o.frame.copy_cropped((cx, cy, cw, ch));
let (ccw, cch) = (cw.div_ceil(2), ch.div_ceil(2));
Picture {
width: cw as u32,
height: ch as u32,
chroma_width: ccw as u32,
chroma_height: cch as u32,
y,
u,
v,
y_stride: cw,
uv_stride: ccw,
pts: o.meta.pts,
poc: o.poc,
key: o.meta.key,
color: ColorInfo {
full_range: o.meta.full_range,
primaries: o.meta.colour_primaries,
transfer: o.meta.transfer_characteristics,
matrix: o.meta.matrix_coefficients,
},
sar: o.meta.sar,
draft: o.meta.draft,
}
}

Some files were not shown because too many files have changed in this diff Show More