Merge upstream main and resolve GNOME Wayland controls conflicts

This commit is contained in:
Daniel committed 2026-10-25 23:57:47 +01:00
commit 1f8b267722
160 files changed
+16326 -1436

No files matched your search

+3
View File
@@ -16,6 +16,9 @@ concurrency:
jobs:
release:
# The shared workflow creates the draft release, which needs write access to contents.
permissions:
contents: write
# Intentionally tracked at @main (not SHA-pinned) so org-wide CI improvements, security fixes, and build optimizations propagate without PR churn across all craftapp repos.
uses: storytold/craft-common-actions/.github/workflows/release.yml@main
with:
+11 -1
View File
@@ -22,9 +22,19 @@ are bundled.
## Fonts
SoundCraft bundles no font files. It uses the default fonts shipped inside the `egui` crate
(Ubuntu-Light: Ubuntu Font Licence 1.0; Hack: MIT; Noto Emoji: OFL-1.1; emoji-icon-font: OFL-1.1/MIT)
(Ubuntu-Light: Ubuntu Font Licence 1.0; Hack: MIT; Noto Emoji: OFL-1.1; emoji-icon-font: MIT)
and, at runtime, the operating system's own UI fonts when available (never redistributed).
## Bundled egui font icons
These dependency fonts are embedded by the pinned egui font definitions; no font file is added
here. The subset uses default_fonts; the parent face uses monochrome_emoji_fonts.
| Asset | Author | Source | Licence |
|---|---|---|---|
| `epaint_default_fonts/fonts/egui-icons.ttf` | John Slegers (emoji-icon-font) | https://github.com/storytold/egui/blob/6d518fc799681571963842f95b55a6b68431b15a/crates/epaint_default_fonts/fonts/egui-icons.txt; original https://github.com/jslegers/emoji-icon-font | MIT (complete copyright and permission text in NOTICE) |
| `epaint_default_fonts/fonts/emoji-icon-font.ttf` | John Slegers | https://github.com/storytold/egui/blob/6d518fc799681571963842f95b55a6b68431b15a/crates/epaint_default_fonts/fonts/emoji-icon-font-mit-license.txt; original https://github.com/jslegers/emoji-icon-font | MIT (complete copyright and permission text in NOTICE) |
## Files
| Path | Author | Source | License |
Generated
+705 -120
View File
File diff suppressed because it is too large. Load diff
+32 -8
View File
@@ -4,7 +4,7 @@ members = ["crates/*", "apps/*", "xtask"]
default-members = ["crates/*", "apps/*", "xtask"]
[workspace.package]
version = "0.12.1"
version = "0.13.1"
edition = "2024"
license = "MIT OR Apache-2.0"
rust-version = "1.90"
@@ -45,17 +45,23 @@ 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 = "82a24b006eefa036b43c09a67fb9a3fbd17ffe63" }
craft-docpath = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "16fb3d46e1eaa0d455465e4e66a3ac7b17b55bd3" }
# Video containers and bitstreams, shared with the other craft apps.
craft-media = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "f0ad6c1e1d297908915f4be315b0fcfc9be7b5d4" }
craft-media = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "16fb3d46e1eaa0d455465e4e66a3ac7b17b55bd3" }
craft-update = { version = "=0.1.0", git = "https://github.com/storytold/craft-libs", rev = "495e5971bc024aa96e6ed318cb6e7cd3a0103184", features = ["egui"] }
# ui
craft-ui = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "5c874d51eeb3de70e735c8ce87a59a7dedd47675", features = ["serde"] }
# egui 0.37 needs craft-libs#199 (craft-ui) first; pin 0.36 until it lands.
egui = "0.36"
eframe = { version = "0.36", default-features = false }
egui_kittest = "0.36"
# 650b1c0b058017c4eb3af06d5b39586d6732e07b is the revision produced by the separate craft-libs current-main-0135
# integration: 25da's egui-0.37 port + 6e7f's docking Workspace/DockCommand + final egui/winit
# pins. Raw 6e7f/16fb3d46 contain the older egui 0.36 UI; keep this reviewed
# immutable revision to retain the required docking APIs on egui 0.37.
craft-ui = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "650b1c0b058017c4eb3af06d5b39586d6732e07b", features = ["serde"] }
egui = { version = "0.37" }
eframe = { version = "0.37", default-features = false }
# Monotonic clock on native and the browser (the `web_time::Instant` panics-free equivalent).
web-time = "1.1"
egui_kittest = "0.37"
rfd = "0.17"
image = { version = "0.25", default-features = false, features = ["png"] }
@@ -81,3 +87,21 @@ opt-level = 3
lto = "thin"
codegen-units = 16
debug = 0
# Keep the complete egui family and native event bridge on reviewed fork commits.
[patch.crates-io]
ecolor = { git = "https://github.com/storytold/egui.git", rev = "6d518fc799681571963842f95b55a6b68431b15a" }
emath = { git = "https://github.com/storytold/egui.git", rev = "6d518fc799681571963842f95b55a6b68431b15a" }
epaint = { git = "https://github.com/storytold/egui.git", rev = "6d518fc799681571963842f95b55a6b68431b15a" }
epaint_default_fonts = { git = "https://github.com/storytold/egui.git", rev = "6d518fc799681571963842f95b55a6b68431b15a" }
egui = { git = "https://github.com/storytold/egui.git", rev = "6d518fc799681571963842f95b55a6b68431b15a" }
egui-winit = { git = "https://github.com/storytold/egui.git", rev = "6d518fc799681571963842f95b55a6b68431b15a" }
egui-wgpu = { git = "https://github.com/storytold/egui.git", rev = "6d518fc799681571963842f95b55a6b68431b15a" }
egui_glow = { git = "https://github.com/storytold/egui.git", rev = "6d518fc799681571963842f95b55a6b68431b15a" }
egui_extras = { git = "https://github.com/storytold/egui.git", rev = "6d518fc799681571963842f95b55a6b68431b15a" }
egui_kittest = { git = "https://github.com/storytold/egui.git", rev = "6d518fc799681571963842f95b55a6b68431b15a" }
egui_system_font_provider = { git = "https://github.com/storytold/egui.git", rev = "6d518fc799681571963842f95b55a6b68431b15a" }
eframe = { git = "https://github.com/storytold/egui.git", rev = "6d518fc799681571963842f95b55a6b68431b15a" }
egui_demo_lib = { git = "https://github.com/storytold/egui.git", rev = "6d518fc799681571963842f95b55a6b68431b15a" }
egui_inspection = { git = "https://github.com/storytold/egui.git", rev = "6d518fc799681571963842f95b55a6b68431b15a" }
winit = { git = "https://github.com/storytold/winit.git", rev = "a517b3846ee8457d84ee4975110bfda6797c44dc" }
+275 -3
View File
@@ -12,9 +12,281 @@ Third-party material
This product bundles third-party material under its own licenses, listed below. The per-file
list (author, source, license) is in ATTRIBUTION.md.
None: every bundled asset is original to SoundCraft. Icons are drawn in code; demo audio is
synthesised in code. The egui default fonts (Ubuntu-Light, Hack, Noto Emoji, Emoji Icon Font) ship
inside the egui crate under their own open licenses (see ATTRIBUTION.md).
Original SoundCraft material: icons are drawn in code; demo audio is synthesised in code.
The egui default fonts (Ubuntu-Light, Hack, Noto Emoji, Emoji Icon Font) ship inside the egui crate
under their own open licenses (see ATTRIBUTION.md).
Default egui fonts (dependency assets)
The pinned egui 0.37 dependency embeds these faces through `default_fonts` and
`monochrome_emoji_fonts`:
https://github.com/storytold/egui/tree/6d518fc799681571963842f95b55a6b68431b15a/crates/epaint_default_fonts/fonts
No font file is copied into this repository. The full upstream notices follow.
egui icon font subset (dependency asset)
----------------------------------------
epaint_default_fonts 0.37 embeds egui-icons.ttf, a five-glyph subset of
emoji-icon-font by John Slegers. The full monochrome emoji-icon-font fallback
uses the same MIT license. Upstream license text follows unchanged.
MIT License
Copyright (c) 2014 John Slegers
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Noto Emoji font (dependency asset)
----------------------------------
epaint_default_fonts 0.37, pinned with egui 0.37 at
https://github.com/storytold/egui/tree/6d518fc799681571963842f95b55a6b68431b15a/crates/epaint_default_fonts/fonts,
embeds NotoEmoji-Regular.ttf, the monochrome emoji fallback used when egui's
`monochrome_emoji_fonts` feature is enabled. The font's copyright notice and the
upstream SIL Open Font License 1.1 text follow unchanged.
Copyright 2013 Google Inc. All Rights Reserved.
This Font Software is licensed under the SIL Open Font License,
Version 1.1.
This license is copied below, and is also available with a FAQ at:
http://scripts.sil.org/OFL
-----------------------------------------------------------
SIL OPEN FONT LICENSE Version 1.1 - 26 February 2007
-----------------------------------------------------------
PREAMBLE
The goals of the Open Font License (OFL) are to stimulate worldwide
development of collaborative font projects, to support the font
creation efforts of academic and linguistic communities, and to
provide a free and open framework in which fonts may be shared and
improved in partnership with others.
The OFL allows the licensed fonts to be used, studied, modified and
redistributed freely as long as they are not sold by themselves. The
fonts, including any derivative works, can be bundled, embedded,
redistributed and/or sold with any software provided that any reserved
names are not used by derivative works. The fonts and derivatives,
however, cannot be released under any other type of license. The
requirement for fonts to remain under this license does not apply to
any document created using the fonts or their derivatives.
DEFINITIONS
"Font Software" refers to the set of files released by the Copyright
Holder(s) under this license and clearly marked as such. This may
include source files, build scripts and documentation.
"Reserved Font Name" refers to any names specified as such after the
copyright statement(s).
"Original Version" refers to the collection of Font Software
components as distributed by the Copyright Holder(s).
"Modified Version" refers to any derivative made by adding to,
deleting, or substituting -- in part or in whole -- any of the
components of the Original Version, by changing formats or by porting
the Font Software to a new environment.
"Author" refers to any designer, engineer, programmer, technical
writer or other person who contributed to the Font Software.
PERMISSION & CONDITIONS
Permission is hereby granted, free of charge, to any person obtaining
a copy of the Font Software, to use, study, copy, merge, embed,
modify, redistribute, and sell modified and unmodified copies of the
Font Software, subject to the following conditions:
1) Neither the Font Software nor any of its individual components, in
Original or Modified Versions, may be sold by itself.
2) Original or Modified Versions of the Font Software may be bundled,
redistributed and/or sold with any software, provided that each copy
contains the above copyright notice and this license. These can be
included either as stand-alone text files, human-readable headers or
in the appropriate machine-readable metadata fields within text or
binary files as long as those fields can be easily viewed by the user.
3) No Modified Version of the Font Software may use the Reserved Font
Name(s) unless explicit written permission is granted by the
corresponding Copyright Holder. This restriction only applies to the
primary font name as presented to the users.
4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font
Software shall not be used to promote, endorse or advertise any
Modified Version, except to acknowledge the contribution(s) of the
Copyright Holder(s) and the Author(s) or with their explicit written
permission.
5) The Font Software, modified or unmodified, in part or in whole,
must be distributed entirely under this license, and must not be
distributed under any other license. The requirement for fonts to
remain under this license does not apply to any document created using
the Font Software.
TERMINATION
This license becomes null and void if any of the above conditions are
not met.
DISCLAIMER
THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE
COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL
DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM
OTHER DEALINGS IN THE FONT SOFTWARE.
Ubuntu Light font (dependency asset)
------------------------------------
epaint_default_fonts 0.37, pinned with egui 0.37 at
https://github.com/storytold/egui/tree/6d518fc799681571963842f95b55a6b68431b15a/crates/epaint_default_fonts/fonts,
embeds Ubuntu-Light.ttf as a default fallback. The font's copyright notice and the
upstream Ubuntu Font Licence 1.0 text follow unchanged.
Copyright 2011 Canonical Ltd. Licensed under the Ubuntu Font Licence 1.0
-------------------------------
UBUNTU FONT LICENCE Version 1.0
-------------------------------
PREAMBLE
This licence allows the licensed fonts to be used, studied, modified and
redistributed freely. The fonts, including any derivative works, can be
bundled, embedded, and redistributed provided the terms of this licence
are met. The fonts and derivatives, however, cannot be released under
any other licence. The requirement for fonts to remain under this
licence does not require any document created using the fonts or their
derivatives to be published under this licence, as long as the primary
purpose of the document is not to be a vehicle for the distribution of
the fonts.
DEFINITIONS
"Font Software" refers to the set of files released by the Copyright
Holder(s) under this licence and clearly marked as such. This may
include source files, build scripts and documentation.
"Original Version" refers to the collection of Font Software components
as received under this licence.
"Modified Version" refers to any derivative made by adding to, deleting,
or substituting -- in part or in whole -- any of the components of the
Original Version, by changing formats or by porting the Font Software to
a new environment.
"Copyright Holder(s)" refers to all individuals and companies who have a
copyright ownership of the Font Software.
"Substantially Changed" refers to Modified Versions which can be easily
identified as dissimilar to the Font Software by users of the Font
Software comparing the Original Version with the Modified Version.
To "Propagate" a work means to do anything with it that, without
permission, would make you directly or secondarily liable for
infringement under applicable copyright law, except executing it on a
computer or modifying a private copy. Propagation includes copying,
distribution (with or without modification and with or without charging
a redistribution fee), making available to the public, and in some
countries other activities as well.
PERMISSION & CONDITIONS
This licence does not grant any rights under trademark law and all such
rights are reserved.
Permission is hereby granted, free of charge, to any person obtaining a
copy of the Font Software, to propagate the Font Software, subject to
the below conditions:
1) Each copy of the Font Software must contain the above copyright
notice and this licence. These can be included either as stand-alone
text files, human-readable headers or in the appropriate machine-
readable metadata fields within text or binary files as long as those
fields can be easily viewed by the user.
2) The font name complies with the following:
(a) The Original Version must retain its name, unmodified.
(b) Modified Versions which are Substantially Changed must be renamed to
avoid use of the name of the Original Version or similar names entirely.
(c) Modified Versions which are not Substantially Changed must be
renamed to both (i) retain the name of the Original Version and (ii) add
additional naming elements to distinguish the Modified Version from the
Original Version. The name of such Modified Versions must be the name of
the Original Version, with "derivative X" where X represents the name of
the new work, appended to that name.
3) The name(s) of the Copyright Holder(s) and any contributor to the
Font Software shall not be used to promote, endorse or advertise any
Modified Version, except (i) as required by this licence, (ii) to
acknowledge the contribution(s) of the Copyright Holder(s) or (iii) with
their explicit written permission.
4) The Font Software, modified or unmodified, in part or in whole, must
be distributed entirely under this licence, and must not be distributed
under any other licence. The requirement for fonts to remain under this
licence does not affect any document created using the Font Software,
except any version of the Font Software extracted from a document
created using the Font Software may only be distributed under this
licence.
TERMINATION
This licence becomes null and void if any of the above conditions are
not met.
DISCLAIMER
THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF
COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE
COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL
DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM OTHER
DEALINGS IN THE FONT SOFTWARE.
Hack font (dependency asset)
----------------------------
epaint_default_fonts 0.37, pinned with egui 0.37 at
https://github.com/storytold/egui/tree/6d518fc799681571963842f95b55a6b68431b15a/crates/epaint_default_fonts/fonts,
embeds Hack-Regular.ttf as the default monospace fallback. The upstream notice and
licences follow unchanged.
The work in the Hack project is Copyright 2018 Source Foundry Authors and licensed under the MIT License
The work in the DejaVu project was committed to the public domain.
Bitstream Vera Sans Mono Copyright 2003 Bitstream Inc. and licensed under the Bitstream Vera License with Reserved Font Names "Bitstream" and "Vera"
MIT License
Copyright (c) 2018 Source Foundry Authors
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
BITSTREAM VERA LICENSE
Copyright (c) 2003 by Bitstream, Inc. All Rights Reserved. Bitstream Vera is a trademark of Bitstream, Inc.
Permission is hereby granted, free of charge, to any person obtaining a copy of the fonts accompanying this license ("Fonts") and associated documentation files (the "Font Software"), to reproduce and distribute the Font Software, including without limitation the rights to use, copy, merge, publish, distribute, and/or sell copies of the Font Software, and to permit persons to whom the Font Software is furnished to do so, subject to the following conditions:
The above copyright and trademark notices and this permission notice shall be included in all copies of one or more of the Font Software typefaces.
The Font Software may be modified, altered, or added to, and in particular the designs of glyphs or characters in the Fonts may be modified and additional glyphs or characters may be added to the Fonts, only if the fonts are renamed to names not containing either the words "Bitstream" or the word "Vera".
This License becomes null and void to the extent applicable to Fonts or Font Software that has been modified and is distributed under the "Bitstream Vera" names.
The Font Software may be sold as part of a larger software package but no copy of one or more of the Font Software typefaces may be sold by itself.
THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL BITSTREAM OR THE GNOME FOUNDATION BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM OTHER DEALINGS IN THE FONT SOFTWARE.
Except as contained in this notice, the names of Gnome, the Gnome Foundation, and Bitstream Inc., shall not be used in advertising or otherwise to promote the sale, use or other dealings in this Font Software without prior written authorization from the Gnome Foundation or Bitstream Inc., respectively. For further information, contact: fonts at gnome dot org.
Linux Wayland window decorations
-------------------------------
+7 -7
View File
@@ -122,11 +122,11 @@ where things are.
- **Automation:** volume, pan, mute, send and every plugin parameter; Write, Touch, Latch and
Trim passes recorded live from the faders; thin, glide, coalesce and convert to clip gain.
- **Plugins:** 7-band and 1-band EQ, compressor/limiter, expander/gate, de-esser, maximizer,
channel strip, room and plate reverbs, mod delay, chorus, flanger, phaser, saturator, lo-fi,
rectifier, pitch shifter, time shift, gain, trim, invert, DC removal, signal generator, dither,
a subtractive synth, a drum synth and a sampler (plays any WAV, AIFF, FLAC, MP3 or OGG across the
keyboard), all original. **CLAP plugins** load too. Any processor
runs offline as AudioSuite, and user presets save per plugin.
channel strip, room and plate reverbs, mod delay, auto pan, chorus, flanger, phaser, ring
modulator, saturator, lo-fi, rectifier, subharmonic, pitch shifter, time shift, gain, trim,
invert, DC removal, signal generator, dither, a subtractive synth, a drum synth and a sampler
(plays any WAV, AIFF, FLAC, MP3 or OGG across the keyboard), all original. **CLAP plugins**
load too. Any processor runs offline as AudioSuite, and user presets save per plugin.
- **MIDI:** instrument and MIDI tracks, a piano-roll MIDI editor with a velocity lane, an event
list, a score view, step input, quantize, transpose, real-time properties, Standard MIDI Files,
and Audio-to-MIDI from a sung or played melody.
@@ -138,7 +138,7 @@ where things are.
pitch-preserving Elastic warping.
- **Audio files:** WAV/BWF/RF64, AIFF/AIFC and FLAC in and out; MP3, Ogg Vorbis, AAC, ALAC, CAF
and the audio of MP4/MOV movies in. Any sample rate and bit depth.
- **Bounce:** mix or stems to WAV, AIFF or FLAC at 16/24/32-bit float with dither and
- **Bounce:** mix or stems to WAV, AIFF or FLAC at 16/24/32-bit float with optional dither and
normalisation, reporting peak, true peak and integrated loudness (LUFS).
- **Fast:** a parallel mix engine, copy-on-write undo, and a UI that holds 120 frames per second
with seventy-odd tracks playing.
@@ -387,4 +387,4 @@ Forks and modified versions must remove them.
<sub>Made by the <a href="https://getartcraft.com/">ArtCraft</a> team and community.</sub>
</p>
Tracks and Groups, Clips, MIDI Editor, Memory Locations, and Undo History use movable panels. Edit and Mix retain separate arrangements; Clips and MIDI Editor belong to Edit. Transport, plug-in, and specialist utility windows retain their existing hosts. Drag a panel header to a target edge or another header. Its context menu offers Float panel, Move to group, and Close panel. Existing panel toggles reopen panels at their saved location; Reset Panel Layout restores the app layout. Floating panels remain inside the current app window.
Tracks and Groups, Clips, MIDI Editor, Memory Locations, and Undo History use movable panels. Edit and Mix retain separate arrangements; Clips and MIDI Editor belong to Edit. Transport, plug-in, and specialist utility windows retain their existing hosts. Drag a panel header to a target edge or another header. Its context menu offers Float Panel, Move Panel to Group, and Close Panel; the tab group's menu adds Float Tab Group and Close Tab Group. Existing panel toggles reopen panels at their saved location; Reset Panel Layout restores the app layout. Floating panels remain inside the current app window.
+27 -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-18 · **Last updated:** 2026-10-18 · **Change:** trivial (mono device playback fold-down) · **Target:** Avid Pro Tools Ultimate 2026.4.1
> **Last reviewed:** 2026-10-24 · **Last updated:** 2026-10-25 · **Change:** trivial (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
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
@@ -122,12 +122,26 @@ mutated and truncated movies. Field reports from the first public week are track
## Progress log
- **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:** 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.
- **2026-10-24:** The Strip Silence window has Extract and Separate buttons. Extract removes the audio and keeps the silent parts of the selection, and Separate separates the clips at the edges of the silence without removing anything. Minimum Strip Duration goes up to 4,000 ms. No readiness percentage change.
- **2026-10-24:** Clip › Loop… (Cmd+Alt+L) opens the Clip Looping dialog: Number of Loops, Loop Length in the main counter's time format, or Loop Until End of Session or Next Clip. A loop length that is not a whole number of loops cuts the last loop short, and Unloop removes that short loop too. No readiness percentage change.
- **2026-10-24:** AudioSuite ▸ Pitch Shift ▸ Vari-Fi slows the selected clips down to a complete stop or speeds them up from one, and the pitch follows the speed. Fit To keeps each clip's length and plays two-thirds of it; Extend plays all of it and makes the clip 150 % long. Fades On fades the audio out with the slowdown or in with the speedup. Before, the menu item resampled the clip at a constant speed. No readiness percentage change.
- **2026-10-24:** A built-in Auto Pan plugin pans the mono sum of a stereo track between left and right. An LFO with seven waveforms moves it at a rate in BPM or at the session tempo, running free, once, in step with the transport, or when the level crosses a threshold or a manual trigger changes; an envelope follower can move it by level instead. Width, Manual and Path set the field, and Output sets the level. No readiness percentage change.
- **2026-10-24:** A built-in Subharmonic (`subharmonic`) adds the octave below a lower and an upper band of the input, chosen from four frequency ranges, with a direct bass band, a gain and a solo for each band, Drive with six characters, HP and LP filters, and a Mix that keeps one path at full level. No readiness percentage change.
- **2026-10-23:** A built-in Ring Modulator (`ring_mod`) applies ring modulation, frequency modulation or a positive or negative resonator, and an LFO, an envelope follower or sample and hold moves its frequency. AudioSuite › Modulation › Sci-Fi now runs it instead of the Flanger. No readiness percentage change.
- **2026-10-19:** Float32 WAV and AIFF-C encoding preserve finite samples above full scale. Integer PCM still clips at full scale. Tests cover whole-file and streaming encoders, non-finite samples, and bounce output with normalization on and off. No readiness percentage change.
- **2026-10-18:** Mono audio devices receive the average of the stereo channels before output clipping, retaining right-only audio. Surround mixes use the same average after their ITU stereo fold-down. Regression tests cover channel balance, polarity cancellation, clipping, centered mono tracks, each 5.1 channel and several callback sizes. No readiness percentage change.
- **2026-10-16:** Master fader volume automation uses each render block's actual last sample, matching track faders. Breakpoints in the next block no longer pull the preceding block's level toward them, and ramps reach their last-sample value. Regression tests cover nonzero starts, one-sample callbacks and several block sizes. No readiness percentage change.
- **2026-10-16:** Aux, folder and master latency calculations exclude inserts already taken out for producing NaN or infinity, matching audio tracks. Subsequent blocks no longer compensate for an insert that is bypassed after failure. Regression tests check impulse timing, healthy insert latency and reset/retry at 64- and 512-sample blocks. No readiness percentage change.
- **2026-10-16:** Split into Mono extracts the correct source channel for each mono track instead of playing channel zero on every track. Clip offsets, gain, fades and stretch, source timestamps, Undo/Redo and saved audio are preserved; repeated clips share each extracted mono source. Unavailable media returns an error without partial edits. No readiness percentage change.
- **2026-10-14:** MCP `batch` rejects more than 1000 calls before executing any command, instead of silently running only the first 1000 (#460). No readiness percentage change.
- **2026-10-14:** `soundcraft-cli convert` dithers only when the output loses resolution (fewer bits than the source, a float or lossy source, or resampled/normalized samples); a same-depth, wider or 32-bit float conversion copies the samples exactly and digital silence stays silent. `--dither` and `--no-dither` override the choice (#548)
- **2026-10-13:** WAV: an empty data chunk followed by metadata (LIST, bext) reads as no audio; a recording that was never finalised (sizes left at 0) still gives what was written (#466)
- **2026-10-13:** Sampler: a ping-pong loop keeps its phase and direction when a step is wider than the loop (a narrow loop with high transposition), instead of reflecting once and dropping the rest of the travel (#463)
- **2026-10-13 (VST3 host messages):** The VST3 host creates `IMessage` and `IAttributeList` objects for plugins (`IHostApplication::createInstance`), so plugins whose controller and processor talk through `IConnectionPoint::notify` work: Line 6 Helix Stadium Native now loads its content database (it reported "Failed to get content database recovery info (Out of memory condition)") and exposes its parameters. No readiness percentage change.
- **2026-10-13:** `mix.instrument` takes the built-in instrument's parameters in `params` (validated, clamped) and refuses any other field, so a synth setting written beside `plugin` is an error instead of being accepted and ignored (#265)
- **2026-10-13:** A bounce of a bus that an aux returns onto lines up with the timeline: the aux insert's lookahead is compensated like the main mix's (it left the bus late and cut the end off), and tracks routed to that bus wait for it too (#246)
- **2026-10-13:** Delay compensation covers the inserts of the master faders: a bounce of the main mix with a Maximizer on the master lines up with the same mix without it (it came out 71 samples late) (#351)
@@ -161,9 +175,21 @@ mutated and truncated movies. Field reports from the first public week are track
| Date | Change | Summary |
|---|---|---|
| 2026-10-25 | trivial | Progress log: Auto Pan stereo balance; 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 |
| 2026-10-24 | trivial | Progress log: Strip Silence Extract and Separate; readiness estimates unchanged |
| 2026-10-24 | trivial | Progress log: Clip Looping dialog; readiness estimates unchanged |
| 2026-10-24 | trivial | Progress log: AudioSuite Vari-Fi speed ramps; readiness estimates unchanged |
| 2026-10-24 | trivial | Progress log: Auto Pan plugin; readiness estimates unchanged |
| 2026-10-24 | trivial | Progress log: Subharmonic plugin; readiness estimates unchanged |
| 2026-10-23 | trivial | Progress log: Ring Modulator plugin; readiness estimates unchanged |
| 2026-10-19 | trivial | Float32 file encoding preserves finite headroom (#791); readiness estimates unchanged |
| 2026-10-18 | trivial | Mono device playback retains both stereo sides and surround fold-down channels; readiness estimates unchanged |
| 2026-10-16 | trivial | Master volume automation block boundaries corrected; readiness estimates unchanged |
| 2026-10-16 | trivial | Failed inserts no longer contribute aux, folder or master latency after containment; readiness estimates unchanged |
| 2026-10-16 | trivial | Split into Mono preserves each source channel, clip edits and saved audio; readiness estimates unchanged |
| 2026-10-14 | trivial | Progress log: oversized MCP batches rejected before execution (#460) |
| 2026-10-13 | trivial | Progress log: empty WAV data chunk followed by metadata (#466) |
| 2026-10-13 | trivial | Progress log: sampler ping-pong phase for wide steps (#463) |
+160 -21
View File
@@ -2,9 +2,11 @@
//!
//! ```text
//! soundcraft-cli info FILE audio file or .scraft summary
//! soundcraft-cli convert IN OUT [--bit-depth 16|24|32f] [--rate HZ] [--normalize]
//! soundcraft-cli convert IN OUT [--bit-depth 16|24|32f] [--rate HZ] [--normalize] [--dither | --no-dither]
//! dithers only when the output loses resolution, unless told
//! soundcraft-cli run [--in S.scraft | --demo] [--cmd ID[=JSON]]… [--bounce OUT] [--start T --end T] [--save S.scraft] [--inspect]
//! soundcraft-cli script FILE [--in S | --demo] [--save S] [--bounce OUT] (one `ID JSON` per line)
//! soundcraft-cli script FILE [--in S | --demo] [--save S] [--bounce OUT [--start T --end T]] (one `ID JSON` per line;
//! without --start/--end, --bounce covers the whole session)
//! soundcraft-cli commands [FILTER] list commands
//! soundcraft-cli describe ID one command's details
//! soundcraft-cli app [--port P] METHOD [JSON] call a running app's control channel
@@ -40,9 +42,57 @@ macro_rules! outln {
}
fn arg_value(args: &[String], flag: &str) -> Option<String> {
args.iter().position(|a| a == flag).and_then(|i| args.get(i + 1)).cloned()
args.iter().enumerate().find_map(|(i, a)| {
if a == flag { args.get(i + 1).cloned() } else { a.strip_prefix(flag).and_then(|rest| rest.strip_prefix('=')).map(str::to_owned) }
})
}
/// Every option a subcommand accepts, with whether it carries a value.
type Options = [(&'static str, bool)];
/// A misspelled option, or one whose value is missing, reads as absent through `arg_value`, so the
/// subcommand would run without what was asked for and still report success. Reject both instead.
fn check_options(args: &[String], allowed: &Options) -> Result<(), String> {
let mut i = 0;
while i < args.len() {
let Some(name) = args[i].strip_prefix("--") else {
i += 1;
continue;
};
let (name, inline) = name.split_once('=').map_or((name, None), |(n, v)| (n, Some(v)));
let flag = format!("--{name}");
let Some(&(_, takes_value)) = allowed.iter().find(|(f, _)| f == &flag) else {
return Err(format!("unknown option {flag}"));
};
match (takes_value, inline) {
(false, Some(_)) => return Err(format!("{flag} takes no value")),
(true, None) => {
// A following option is the next flag, not this one's value.
if args.get(i + 1).is_none_or(|v| v.starts_with("--")) {
return Err(format!("{flag} needs a value"));
}
i += 1;
}
_ => {}
}
i += 1;
}
Ok(())
}
/// `--in`, `--demo`/`--sample`, `--bounce`, `--start`, `--end`, `--save` and `--inspect`, read by
/// `load_engine` and `finish` and so shared by `run` and `script`.
const ENGINE_OPTIONS: &Options = &[
("--in", true),
("--demo", false),
("--sample", false),
("--bounce", true),
("--start", true),
("--end", true),
("--save", true),
("--inspect", false),
];
fn fail(msg: impl std::fmt::Display) -> ExitCode {
eprintln!("soundcraft-cli: {msg}");
ExitCode::FAILURE
@@ -124,6 +174,10 @@ fn info(args: &[String]) -> ExitCode {
fn convert(args: &[String]) -> ExitCode {
let (Some(input), Some(output)) = (args.first(), args.get(1)) else { return fail("convert IN OUT") };
const OPTIONS: &Options = &[("--rate", true), ("--bit-depth", true), ("--normalize", false), ("--dither", false), ("--no-dither", false)];
if let Err(e) = check_options(&args[2..], OPTIONS) {
return fail(e);
}
let rate = match parse_rate(arg_value(args, "--rate").as_deref()) {
Ok(r) => r,
Err(e) => return fail(e),
@@ -132,12 +186,16 @@ fn convert(args: &[String]) -> ExitCode {
Ok(b) => b,
Err(e) => return fail(e),
};
let dither_flag = match parse_dither(args) {
Ok(d) => d,
Err(e) => return fail(e),
};
let bytes = match std::fs::read(input) {
Ok(b) => b,
Err(e) => return fail(format!("{input}: {e}")),
};
let ext = std::path::Path::new(input).extension().and_then(std::ffi::OsStr::to_str);
let (_, mut buf) = match soundcraft_audio_io::decode(&bytes, ext) {
let (info, mut buf) = match soundcraft_audio_io::decode(&bytes, ext) {
Ok(x) => x,
Err(e) => return fail(format!("{input}: {e}")),
};
@@ -145,14 +203,17 @@ fn convert(args: &[String]) -> ExitCode {
buf.channels = soundcraft_dsp::offline::resample(&buf.channels, buf.sample_rate, rate);
buf.sample_rate = rate;
}
if args.iter().any(|a| a == "--normalize") {
let normalize = args.iter().any(|a| a == "--normalize");
if normalize {
soundcraft_dsp::offline::normalize(&mut buf.channels, -0.1, false);
}
let processed = rate.is_some_and(|r| r != info.sample_rate) || normalize;
let dither = dither_flag.unwrap_or_else(|| auto_dither(info.sample_format, bit_depth, processed));
let out_ext = std::path::Path::new(output).extension().and_then(std::ffi::OsStr::to_str).unwrap_or("wav").to_ascii_lowercase();
let Some(format) = soundcraft_audio_io::encode_format_for(&out_ext) else {
return fail(format!("{output}: cannot write `.{out_ext}` files (supported: {})", soundcraft_audio_io::ENCODE_EXTENSIONS));
};
let opts = soundcraft_audio_io::EncodeOptions { format, bit_depth, dither: true, bwf: None };
let opts = soundcraft_audio_io::EncodeOptions { format, bit_depth, dither, bwf: None };
match soundcraft_audio_io::encode(&buf, &opts).map_err(|e| e.to_string()).and_then(|b| std::fs::write(output, b).map_err(|e| e.to_string())) {
Ok(()) => {
outln!("wrote {output} ({} ch, {} Hz, {:.2} s)", buf.num_channels(), buf.sample_rate, buf.duration_secs());
@@ -181,6 +242,41 @@ fn parse_bit_depth(v: Option<&str>) -> Result<soundcraft_audio_io::BitDepth, Str
}
}
/// `--dither` / `--no-dither`: `Some` when the caller chose, `None` to decide from the depths.
fn parse_dither(args: &[String]) -> Result<Option<bool>, String> {
match (args.iter().any(|a| a == "--dither"), args.iter().any(|a| a == "--no-dither")) {
(true, true) => Err("--dither and --no-dither: choose one".into()),
(true, false) => Ok(Some(true)),
(false, true) => Ok(Some(false)),
(false, false) => Ok(None),
}
}
/// Whether `convert` dithers when not told: only when writing integer samples that cannot hold the
/// source exactly, i.e. the output has fewer bits than the source, the source is float or lossy, or
/// resampling/normalizing produced new sample values. A 16-bit file written as 16 or 24-bit, or
/// anything written as 32-bit float, is copied sample for sample (digital silence stays silent).
fn auto_dither(source: soundcraft_audio_io::SampleFormat, target: soundcraft_audio_io::BitDepth, processed: bool) -> bool {
use soundcraft_audio_io::{BitDepth, SampleFormat};
let target_bits = match target {
BitDepth::Float32 => return false,
BitDepth::Int16 => 16,
BitDepth::Int24 => 24,
BitDepth::Int32 => 32,
};
if processed {
return true;
}
match source {
SampleFormat::Int8 => target_bits < 8,
SampleFormat::Int16 => target_bits < 16,
SampleFormat::Int24 => target_bits < 24,
// Decoded through f32, which holds 24 bits: a 32-bit integer source has already lost
// resolution, so treat it like float.
SampleFormat::Int32 | SampleFormat::Float32 | SampleFormat::Float64 | SampleFormat::Compressed => true,
}
}
/// One stderr line per media file a session refers to but could not load (the exit code is unchanged).
fn missing_warnings(missing: &[String]) -> Vec<String> {
missing.iter().map(|m| format!("warning: missing media: {m}")).collect()
@@ -214,6 +310,11 @@ fn finish(e: &mut Engine, args: &[String]) -> ExitCode {
if let Some(s) = arg_value(args, "--end") {
p["end"] = time_arg(&s);
}
// No range given: bounce the whole session, not whatever edit selection the commands left.
if p.get("start").is_none() && p.get("end").is_none() {
p["start"] = json!(0);
p["end"] = json!(e.session().content_end().max(1));
}
match e.execute("file.bounce_mix", &p) {
Ok(v) => outln!("{v}"),
Err(err) => return fail(err),
@@ -245,32 +346,42 @@ fn parse_cmd_spec(spec: &str) -> Result<(String, Value), String> {
}
fn run(args: &[String]) -> ExitCode {
let options: Vec<(&str, bool)> = ENGINE_OPTIONS.iter().copied().chain([("--cmd", true)]).collect();
if let Err(e) = check_options(args, &options) {
return fail(e);
}
let mut e = match load_engine(args) {
Ok(e) => e,
Err(err) => return fail(err),
};
let mut i = 0;
while i < args.len() {
if args.get(i).is_some_and(|a| a == "--cmd")
&& let Some(spec) = args.get(i + 1)
{
let (id, params) = match parse_cmd_spec(spec) {
Ok(v) => v,
Err(err) => return fail(err),
};
match e.execute(&id, &params) {
Ok(v) => outln!("{id}: {v}"),
Err(err) => return fail(format!("{id}: {err}")),
}
i += 1;
let mut it = args.iter();
while let Some(a) = it.next() {
let spec = match a.strip_prefix("--cmd=") {
Some(inline) => inline,
None if a == "--cmd" => match it.next() {
Some(next) => next,
None => continue,
},
None => continue,
};
let (id, params) = match parse_cmd_spec(spec) {
Ok(v) => v,
Err(err) => return fail(err),
};
match e.execute(&id, &params) {
Ok(v) => outln!("{id}: {v}"),
Err(err) => return fail(format!("{id}: {err}")),
}
i += 1;
}
finish(&mut e, args)
}
fn script(args: &[String]) -> ExitCode {
let Some(path) = args.first() else { return fail("script FILE") };
let options: Vec<(&str, bool)> = ENGINE_OPTIONS.iter().copied().chain([("--keep-going", false)]).collect();
if let Err(e) = check_options(&args[1..], &options) {
return fail(e);
}
let text = match std::fs::read_to_string(path) {
Ok(t) => t,
Err(e) => return fail(format!("{path}: {e}")),
@@ -514,6 +625,34 @@ mod tests {
assert!(parse_bit_depth(Some("8")).is_err());
}
#[test]
fn convert_dither_flags() {
let args = |a: &[&str]| a.iter().map(|s| s.to_string()).collect::<Vec<_>>();
assert_eq!(parse_dither(&args(&["in.wav", "out.wav"])), Ok(None));
assert_eq!(parse_dither(&args(&["in.wav", "out.wav", "--dither"])), Ok(Some(true)));
assert_eq!(parse_dither(&args(&["in.wav", "out.wav", "--no-dither"])), Ok(Some(false)));
assert!(parse_dither(&args(&["in.wav", "out.wav", "--dither", "--no-dither"])).is_err());
}
#[test]
fn convert_dithers_only_when_resolution_is_lost() {
use soundcraft_audio_io::{BitDepth, SampleFormat};
// Same depth or wider: the samples fit exactly.
assert!(!auto_dither(SampleFormat::Int16, BitDepth::Int16, false));
assert!(!auto_dither(SampleFormat::Int16, BitDepth::Int24, false));
assert!(!auto_dither(SampleFormat::Int24, BitDepth::Int24, false));
assert!(!auto_dither(SampleFormat::Int8, BitDepth::Int16, false));
// Float output never dithers.
assert!(!auto_dither(SampleFormat::Float32, BitDepth::Float32, true));
assert!(!auto_dither(SampleFormat::Int24, BitDepth::Float32, false));
// Fewer bits, a float or lossy source, or processed samples: dither.
assert!(auto_dither(SampleFormat::Int24, BitDepth::Int16, false));
assert!(auto_dither(SampleFormat::Float32, BitDepth::Int24, false));
assert!(auto_dither(SampleFormat::Compressed, BitDepth::Int16, false));
assert!(auto_dither(SampleFormat::Int32, BitDepth::Int24, false));
assert!(auto_dither(SampleFormat::Int16, BitDepth::Int16, true));
}
#[test]
fn missing_media_gets_one_warning_each() {
assert!(missing_warnings(&[]).is_empty());
+95
View File
@@ -0,0 +1,95 @@
//! `soundcraft-cli convert` dithers only when the output loses resolution (#548), run as the real binary.
// Integration tests: unwrapping and panicking on failure is fine here, unlike in shipped code (AGENTS.md).
#![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]
use std::path::{Path, PathBuf};
use std::process::Command;
use soundcraft_audio_io::{AudioBuffer, BitDepth, EncodeOptions, FileFormat};
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);
std::fs::create_dir_all(&dir).unwrap();
dir
}
/// A 16-bit mono WAV of `samples`, written without dither so the file holds exactly these values.
fn write_wav16(path: &Path, samples: &[i16]) {
let mut buf = AudioBuffer::new(48_000, 1, samples.len());
for (d, s) in buf.channels[0].iter_mut().zip(samples) {
*d = f32::from(*s) / 32768.0;
}
let opts = EncodeOptions { format: FileFormat::Wav, bit_depth: BitDepth::Int16, dither: false, bwf: None };
std::fs::write(path, soundcraft_audio_io::encode(&buf, &opts).unwrap()).unwrap();
}
fn convert(input: &Path, output: &Path, extra: &[&str]) {
let o = Command::new(env!("CARGO_BIN_EXE_soundcraft-cli"))
.env("SOUNDCRAFT_NO_PREFS", "1")
.arg("convert")
.arg(input)
.arg(output)
.args(extra)
.output()
.unwrap();
assert!(o.status.success(), "{}", String::from_utf8_lossy(&o.stderr));
}
/// The decoded samples of `path`, scaled to 16-bit steps.
fn steps16(path: &Path) -> Vec<f64> {
let (_, buf) = soundcraft_audio_io::decode(&std::fs::read(path).unwrap(), None).unwrap();
buf.channels[0].iter().map(|s| f64::from(*s) * 32768.0).collect()
}
#[test]
fn convert_copies_samples_when_nothing_is_lost() {
let dir = tmpdir("exact");
let (silence, tone) = (dir.join("silence.wav"), dir.join("tone.wav"));
let tone_samples: Vec<i16> = (0..4800).map(|i| (8000.0 * (f64::from(i) * 0.05).sin()) as i16).collect();
write_wav16(&silence, &[0; 4800]);
write_wav16(&tone, &tone_samples);
let want: Vec<f64> = tone_samples.iter().map(|s| f64::from(*s)).collect();
// Same depth, wider and float: every sample comes back unchanged, silence stays silent.
for (depth, name) in [("16", "s16.wav"), ("24", "s24.wav"), ("32f", "s32.wav")] {
let out = dir.join(name);
convert(&silence, &out, &["--bit-depth", depth]);
assert!(steps16(&out).iter().all(|s| *s == 0.0), "silence at {depth} is not silent");
let out = dir.join(format!("t{name}"));
convert(&tone, &out, &["--bit-depth", depth]);
assert_eq!(steps16(&out), want, "tone at {depth} changed");
}
// The default output depth (24-bit) too.
let out = dir.join("default.wav");
convert(&tone, &out, &[]);
assert_eq!(steps16(&out), want);
// Asked for, dither is still applied.
let out = dir.join("forced.wav");
convert(&silence, &out, &["--bit-depth", "16", "--dither"]);
assert!(steps16(&out).iter().any(|s| *s != 0.0), "--dither did not dither");
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
fn convert_dithers_when_reducing_depth_unless_told_not_to() {
let dir = tmpdir("reduce");
let src = dir.join("src24.wav");
// A 24-bit file with detail below one 16-bit step.
let mut buf = AudioBuffer::new(48_000, 1, 4800);
for (i, s) in buf.channels[0].iter_mut().enumerate() {
*s = 0.3 / 32768.0 * (i as f32 * 0.01).sin();
}
let opts = EncodeOptions { format: FileFormat::Wav, bit_depth: BitDepth::Int24, dither: false, bwf: None };
std::fs::write(&src, soundcraft_audio_io::encode(&buf, &opts).unwrap()).unwrap();
let dithered = dir.join("d16.wav");
convert(&src, &dithered, &["--bit-depth", "16"]);
assert!(steps16(&dithered).iter().any(|s| *s != 0.0), "24 to 16-bit was not dithered");
// Plain rounding: everything is below half a step, so it all rounds to zero.
let plain = dir.join("p16.wav");
convert(&src, &plain, &["--bit-depth", "16", "--no-dither"]);
assert!(steps16(&plain).iter().all(|s| *s == 0.0), "--no-dither still dithered");
let _ = std::fs::remove_dir_all(&dir);
}
+40 -2
View File
@@ -24,6 +24,20 @@ fn tmpdir(tag: &str) -> PathBuf {
dir
}
/// `path` made absolute with links followed (`/var` becomes `/private/var` on macOS), as the server
/// prints a root: without the `\\?\` prefix that `canonicalize` adds on Windows.
fn resolved(path: &Path) -> PathBuf {
let path = std::fs::canonicalize(path).unwrap();
if !cfg!(windows) {
return path;
}
let text = path.to_str().unwrap();
if let Some(rest) = text.strip_prefix(r"\\?\UNC\") {
return PathBuf::from(format!(r"\\{rest}"));
}
text.strip_prefix(r"\\?\").map_or(path.clone(), PathBuf::from)
}
/// Send `calls` (tool name, arguments) to `soundcraft-cli mcp <args>`; returns whether it exited
/// cleanly, its stderr, and the stdout lines after `initialize`, one per call.
fn mcp_session(args: &[&str], calls: &[(&str, Value)]) -> (bool, String, Vec<Value>) {
@@ -114,8 +128,7 @@ fn mcp_automation_roots_confine_the_server() {
/// the two roots' common parent, `..` out of a root and a link out of one stay refused.
#[test]
fn mcp_automation_roots_take_several_folders() {
// Resolved, as the server shows the roots (`/var` is `/private/var` on macOS).
let base = std::fs::canonicalize(tmpdir("several")).unwrap();
let base = resolved(&tmpdir("several"));
for d in ["in", "in2", "out", "out2", "outside"] {
std::fs::create_dir_all(base.join(d)).unwrap();
}
@@ -215,3 +228,28 @@ fn usage_lists_the_root_flags() {
let help = String::from_utf8_lossy(&o.stdout).into_owned();
assert!(help.contains("--automation-read-root DIR") && help.contains("--automation-write-root DIR"), "{help}");
}
/// Without `--start`/`--end`, `--bounce` covers the whole session, not a stray edit selection
/// left by the script (a bare `19.2` in command params is 19 samples).
#[test]
fn script_bounce_without_range_covers_the_session() {
let base = tmpdir("bounce-range");
let script = base.join("cmds.txt");
std::fs::write(
&script,
concat!(
"track.new {\"count\":1,\"kind\":\"instrument\",\"name\":\"Keys\"}\n",
"midi.new_clip {\"track\":\"Keys\",\"start\":0,\"end\":{\"seconds\":19.2}}\n",
"edit.select {\"start\":0,\"end\":19.2}\n",
),
)
.unwrap();
let out = base.join("out.wav");
let o = run(&["script", script.to_str().unwrap(), "--bounce", out.to_str().unwrap()]);
assert!(o.status.success(), "{}", String::from_utf8_lossy(&o.stderr));
let stdout = String::from_utf8_lossy(&o.stdout).into_owned();
let last: Value = serde_json::from_str(stdout.lines().last().unwrap()).unwrap();
let secs = last["seconds"].as_f64().unwrap();
assert!((secs - 19.2).abs() < 0.01, "bounced {secs} s: {stdout}");
let _ = std::fs::remove_dir_all(base);
}
+130
View File
@@ -0,0 +1,130 @@
//! Unknown options and missing option values must be reported, not silently skipped (#549).
// Integration tests: unwrapping and panicking on failure is fine here, unlike in shipped code (AGENTS.md).
#![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]
use std::path::{Path, PathBuf};
use std::process::{Command, Output};
fn bin() -> Command {
let mut cmd = Command::new(env!("CARGO_BIN_EXE_soundcraft-cli"));
cmd.env("SOUNDCRAFT_NO_PREFS", "1");
cmd
}
fn run(args: &[&str]) -> Output {
bin().args(args).output().unwrap()
}
fn tmpdir(tag: &str) -> PathBuf {
let dir = std::env::temp_dir().join(format!("sc-cli-options-{tag}-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&dir);
std::fs::create_dir_all(&dir).unwrap();
dir
}
/// A 0.1 s mono 48 kHz 16-bit WAV, written by hand so the test needs no fixture.
fn tone(dir: &Path) -> PathBuf {
let frames: u32 = 4800;
let data: Vec<u8> = (0..frames).flat_map(|_| [0x00, 0x10]).collect();
let (bytes, rate, ch, bits) = (data.len() as u32, 48_000u32, 1u16, 16u16);
let mut wav = Vec::new();
wav.extend_from_slice(b"RIFF");
wav.extend_from_slice(&(36 + bytes).to_le_bytes());
wav.extend_from_slice(b"WAVEfmt ");
wav.extend_from_slice(&16u32.to_le_bytes());
wav.extend_from_slice(&1u16.to_le_bytes());
wav.extend_from_slice(&ch.to_le_bytes());
wav.extend_from_slice(&rate.to_le_bytes());
wav.extend_from_slice(&(rate * u32::from(ch) * u32::from(bits) / 8).to_le_bytes());
wav.extend_from_slice(&(ch * bits / 8).to_le_bytes());
wav.extend_from_slice(&bits.to_le_bytes());
wav.extend_from_slice(b"data");
wav.extend_from_slice(&bytes.to_le_bytes());
wav.extend_from_slice(&data);
let path = dir.join("tone.wav");
std::fs::write(&path, wav).unwrap();
path
}
fn sample_format(cli_info: &str) -> String {
serde_json::from_str::<serde_json::Value>(cli_info).unwrap()["sample_format"].as_str().unwrap().to_owned()
}
#[test]
fn convert_rejects_an_unknown_option_instead_of_writing_the_file() {
let dir = tmpdir("convert-unknown");
let input = tone(&dir);
let out = dir.join("out.wav");
for bad in ["--bitdepth", "--normalise", "--bogus"] {
let o = run(&["convert", input.to_str().unwrap(), out.to_str().unwrap(), bad]);
assert!(!o.status.success(), "{bad} should fail");
assert!(String::from_utf8_lossy(&o.stderr).contains(&format!("unknown option {bad}")), "{bad}: {}", String::from_utf8_lossy(&o.stderr));
assert!(!out.exists(), "{bad} should not write the output");
}
}
#[test]
fn convert_rejects_an_option_whose_value_is_missing() {
let dir = tmpdir("convert-novalue");
let input = tone(&dir);
let out = dir.join("out.wav");
for bad in ["--rate", "--bit-depth"] {
let o = run(&["convert", input.to_str().unwrap(), out.to_str().unwrap(), bad]);
assert!(!o.status.success(), "{bad} should fail");
assert!(String::from_utf8_lossy(&o.stderr).contains(&format!("{bad} needs a value")), "{bad}: {}", String::from_utf8_lossy(&o.stderr));
assert!(!out.exists(), "{bad} should not write the output");
}
}
#[test]
fn convert_accepts_the_inline_value_form() {
let dir = tmpdir("convert-inline");
let input = tone(&dir);
let out = dir.join("out.wav");
let o = run(&["convert", input.to_str().unwrap(), out.to_str().unwrap(), "--bit-depth=32f"]);
assert!(o.status.success(), "{}", String::from_utf8_lossy(&o.stderr));
let info = run(&["info", out.to_str().unwrap()]);
assert_eq!(sample_format(&String::from_utf8_lossy(&info.stdout)), "Float32");
}
#[test]
fn convert_still_accepts_the_documented_space_separated_form() {
let dir = tmpdir("convert-space");
let input = tone(&dir);
let out = dir.join("out.wav");
let o = run(&["convert", input.to_str().unwrap(), out.to_str().unwrap(), "--bit-depth", "16"]);
assert!(o.status.success(), "{}", String::from_utf8_lossy(&o.stderr));
let info = run(&["info", out.to_str().unwrap()]);
assert_eq!(sample_format(&String::from_utf8_lossy(&info.stdout)), "Int16");
}
#[test]
fn run_rejects_a_misspelled_or_value_less_bounce() {
let dir = tmpdir("run-bounce");
let out = dir.join("x.wav");
let o = run(&["run", "--cmd", "session.new", "--bounce"]);
assert!(!o.status.success());
assert!(String::from_utf8_lossy(&o.stderr).contains("--bounce needs a value"), "{}", String::from_utf8_lossy(&o.stderr));
let o = run(&["run", "--cmd", "session.new", "--bounces", out.to_str().unwrap()]);
assert!(!o.status.success());
assert!(String::from_utf8_lossy(&o.stderr).contains("unknown option --bounces"), "{}", String::from_utf8_lossy(&o.stderr));
assert!(!out.exists(), "nothing should be bounced");
}
#[test]
fn run_bounces_with_the_inline_value_form() {
let dir = tmpdir("run-inline");
let out = dir.join("y.wav");
let o = run(&["run", "--cmd", "session.new", &format!("--bounce={}", out.display())]);
assert!(o.status.success(), "{}", String::from_utf8_lossy(&o.stderr));
assert!(out.exists(), "--bounce=PATH should write the file");
}
#[test]
fn run_executes_the_inline_cmd_form() {
let o = run(&["run", "--cmd=track.new", "--cmd", "track.new"]);
assert!(o.status.success(), "{}", String::from_utf8_lossy(&o.stderr));
let stdout = String::from_utf8_lossy(&o.stdout);
assert_eq!(stdout.lines().filter(|l| l.starts_with("track.new: ")).count(), 2, "both --cmd forms should run: {stdout}");
}
+2 -2
View File
@@ -15,10 +15,10 @@ path = "src/main.rs"
[target.'cfg(target_arch = "wasm32")'.dependencies]
soundcraft-engine = { workspace = true }
soundcraft-ui-egui = { workspace = true }
soundcraft-ui-egui.workspace = true
soundcraft-playback = { workspace = true }
soundcraft-model = { workspace = true }
egui = { workspace = true }
egui = { workspace = true, features = ["default_fonts", "monochrome_emoji_fonts"] }
log = { workspace = true }
serde_json = { workspace = true }
rfd = { workspace = true }
+7 -4
View File
@@ -137,11 +137,12 @@ impl WebShell {
});
}
/// Read files dropped on the browser window asynchronously: audio for an instrument track or a Sampler goes to
/// that sampler, while other drops are classified by extension and handled on a later frame.
fn take_drops(&mut self, ctx: &egui::Context) {
// In the browser a dropped file's `path()` is only its name: read its bytes instead.
// Audio dropped on an instrument track or a Sampler window goes to its sampler instead.
let dropped = self.app.take_dropped_files(ctx);
ctx.input_mut(|i| i.raw.dropped_files.clear());
let dropped = self.app.take_dropped_files();
for read in self.app.take_sample_reads() {
let (samples, ctx) = (self.samples.clone(), ctx.clone());
wasm_bindgen_futures::spawn_local(async move {
@@ -208,8 +209,10 @@ impl eframe::App for WebShell {
self.take_path_prompt(ctx);
}
fn raw_input_hook(&mut self, _ctx: &egui::Context, raw: &mut egui::RawInput) {
self.app.raw_input_hook(raw);
/// Forward raw input to the shared app hook so scripted gestures and host-key protection behave like the desktop
/// shell.
fn raw_input_hook(&mut self, ctx: &egui::Context, raw: &mut egui::RawInput) {
self.app.raw_input_hook(ctx, raw);
}
fn ui(&mut self, ui: &mut egui::Ui, _frame: &mut eframe::Frame) {
+5 -2
View File
@@ -18,8 +18,8 @@ asio = ["soundcraft-playback/asio"]
soundcraft-engine.workspace = true
soundcraft-ui-egui.workspace = true
soundcraft-playback.workspace = true
eframe = { workspace = true, features = ["default_fonts", "wgpu", "accesskit", "wayland", "x11", "links"] }
egui.workspace = true
eframe = { workspace = true, features = ["default_fonts", "wgpu", "accesskit", "wayland", "x11", "links", "clipboard"] }
egui = { workspace = true, features = ["default_fonts", "monochrome_emoji_fonts"] }
serde_json.workspace = true
log.workspace = true
chrono.workspace = true
@@ -39,3 +39,6 @@ objc2-app-kit = { version = "0.3", default-features = false, features = ["NSAppl
[target.'cfg(target_os = "linux")'.dependencies]
# Native GNOME/Wayland controls without a bundled title font.
winit = { version = "0.30", default-features = false, features = ["wayland-csd-adwaita-notitle"] }
[target.'cfg(windows)'.dependencies]
craft-update = { workspace = true }
+16 -15
View File
@@ -3,6 +3,8 @@
use std::ffi::OsString;
use std::path::{Path, PathBuf};
use soundcraft_ui_egui::docking;
/// Portable mode (#268): this file beside the executable keeps settings, autosaves, presets and
/// logs in [`PORTABLE_DATA_DIR`] next to it instead of the user profile.
const PORTABLE_MARKER: &str = "portable.txt";
@@ -88,19 +90,18 @@ fn read_ui(path: &std::path::Path, recover_docking: bool) -> Result<Option<sound
let mut saved: serde_json::Value = serde_json::from_slice(&bytes).map_err(|error| format!("Settings could not be restored: {error}"))?;
if recover_docking && let Some(fields) = saved.as_object_mut() {
for (field, mix) in [("docking", false), ("mix_docking", true)] {
let unreadable =
fields.get(field).is_some_and(|value| match serde_json::from_value::<soundcraft_ui_egui::docking::Workspace>(value.clone()) {
Ok(workspace) => if mix { workspace.validate_mix() } else { workspace.validate() }.is_err(),
Err(_) => true,
});
let unreadable = fields.get(field).is_some_and(|value| match serde_json::from_value::<docking::Workspace>(value.clone()) {
Ok(workspace) => if mix { docking::validate_mix(&workspace) } else { docking::validate(&workspace) }.is_err(),
Err(_) => true,
});
if unreadable {
fields.remove(field);
}
}
}
let state: soundcraft_ui_egui::UiState = serde_json::from_value(saved).map_err(|error| format!("Settings could not be restored: {error}"))?;
state.docking.validate()?;
state.mix_docking.validate_mix()?;
docking::validate(&state.docking)?;
docking::validate_mix(&state.mix_docking)?;
Ok(Some(state))
}
@@ -111,8 +112,8 @@ pub fn save_ui(path: &std::path::Path, ui: &soundcraft_ui_egui::UiState) -> Resu
use std::io::Write as _;
// Loading can recover a future layout for this session, but saving must preserve its source.
read_ui(path, false)?;
ui.docking.validate()?;
ui.mix_docking.validate_mix()?;
docking::validate(&ui.docking)?;
docking::validate_mix(&ui.mix_docking)?;
let mut saved = ui.clone();
saved.plugin_windows.clear();
saved.status.clear();
@@ -160,18 +161,18 @@ mod config_tests {
app.ui = load_ui(&path).unwrap().unwrap();
assert_eq!(app.ui.theme, soundcraft_ui_egui::theme::ThemeMode::Light);
assert_eq!(serde_json::to_value(&app.ui.docking).unwrap(), edit);
assert_eq!(app.ui.mix_docking.layout.floating.iter().find(|g| g.panels.contains(&Panel::Tracks)).unwrap().rect, [140.0, 150.0, 280.0, 420.0]);
assert_eq!(app.ui.mix_docking.layout.floating.iter().find(|g| g.contains(&Panel::Tracks)).unwrap().rect, [140.0, 150.0, 280.0, 420.0]);
app.ui.window = MainWindow::Edit;
app.run("ui.dock", serde_json::json!({"operation":"open","panel":"tracks"})).unwrap();
assert_eq!(app.ui.docking.layout.floating.iter().find(|g| g.panels.contains(&Panel::Tracks)).unwrap().rect, [80.0, 90.0, 260.0, 400.0]);
assert_eq!(app.ui.docking.layout.floating.iter().find(|g| g.contains(&Panel::Tracks)).unwrap().rect, [80.0, 90.0, 260.0, 400.0]);
let mut invalid_mix = app.ui.clone();
invalid_mix.mix_docking = soundcraft_ui_egui::docking::Workspace::default();
invalid_mix.mix_docking = soundcraft_ui_egui::docking::edit_default();
let invalid_bytes = serde_json::to_vec(&invalid_mix).unwrap();
std::fs::write(&path, &invalid_bytes).unwrap();
let recovered = load_ui(&path).unwrap().unwrap();
assert_eq!(recovered.theme, app.ui.theme);
assert_eq!(serde_json::to_value(recovered.docking).unwrap(), serde_json::to_value(&app.ui.docking).unwrap());
recovered.mix_docking.validate_mix().unwrap();
soundcraft_ui_egui::docking::validate_mix(&recovered.mix_docking).unwrap();
assert!(save_ui(&path, &app.ui).is_err());
assert_eq!(std::fs::read(&path).unwrap(), invalid_bytes);
std::fs::write(&path, b"damaged settings").unwrap();
@@ -201,8 +202,8 @@ mod config_tests {
let recovered = load_ui(&path).unwrap().unwrap();
assert_eq!(recovered.theme, app.ui.theme);
assert!(recovered.narrow_mix);
recovered.docking.validate().unwrap();
recovered.mix_docking.validate_mix().unwrap();
soundcraft_ui_egui::docking::validate(&recovered.docking).unwrap();
soundcraft_ui_egui::docking::validate_mix(&recovered.mix_docking).unwrap();
let other = if field == "docking" { "mix_docking" } else { "docking" };
assert_eq!(serde_json::to_value(&recovered).unwrap()[other], saved[other]);
assert!(save_ui(&path, &recovered).is_err());
+27 -13
View File
@@ -10,7 +10,7 @@ use std::io::{BufRead, BufReader, Read, Write};
use std::net::{TcpListener, TcpStream};
use std::sync::Arc;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::mpsc::{Receiver, Sender, channel};
use std::sync::mpsc::{Receiver, SyncSender, TrySendError, sync_channel};
use std::time::Duration;
/// Longest accepted request line (bytes, without the newline). The largest payload a request
@@ -22,9 +22,15 @@ const MAX_LINE: usize = soundcraft_engine::model::b64::MAX_DECODED_BYTES.div_cei
const _: () = assert!(MAX_LINE <= 96 << 20);
/// Connections served at once; further ones get an error line and are closed.
const MAX_CONNECTIONS: usize = 16;
/// Requests that may wait for the UI thread. Each connection handles one request at a time, so
/// this matches [`MAX_CONNECTIONS`] and makes the transport's bound explicit instead of relying
/// on an unbounded channel.
const MAX_QUEUED_REQUESTS: usize = MAX_CONNECTIONS;
/// Bind the loopback control listener and return the request receiver. Port 0 picks a free one and the actual port is
/// printed; bind or spawn failures are logged instead of panicking.
pub fn start(port: u16, ctx: egui::Context) -> Receiver<ControlRequest> {
let (tx, rx) = channel::<ControlRequest>();
let (tx, rx) = sync_channel::<ControlRequest>(MAX_QUEUED_REQUESTS);
let listener = match TcpListener::bind(("127.0.0.1", port)) {
Ok(l) => l,
Err(e) => {
@@ -52,7 +58,9 @@ impl Drop for Slot {
}
}
fn accept_loop(listener: TcpListener, tx: Sender<ControlRequest>, ctx: egui::Context) {
/// Accept until the listener closes, admitting at most [`MAX_CONNECTIONS`] peers; excess peers get one error line.
/// Each admitted connection is served on its own thread, releasing its slot on exit.
fn accept_loop(listener: TcpListener, tx: SyncSender<ControlRequest>, ctx: egui::Context) {
let open = Arc::new(AtomicUsize::new(0));
for mut stream in listener.incoming().flatten() {
if open.fetch_add(1, Ordering::SeqCst) >= MAX_CONNECTIONS {
@@ -94,7 +102,9 @@ fn read_bounded_line(reader: &mut impl BufRead, buf: &mut Vec<u8>, max: usize) -
Ok(true)
}
fn serve(stream: TcpStream, tx: Sender<ControlRequest>, ctx: egui::Context) {
/// 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.
fn serve(stream: TcpStream, tx: SyncSender<ControlRequest>, ctx: egui::Context) {
let Ok(read) = stream.try_clone() else { return };
let mut out = stream;
let mut reader = BufReader::new(read);
@@ -119,15 +129,19 @@ fn serve(stream: TcpStream, tx: Sender<ControlRequest>, ctx: egui::Context) {
let method = msg.get("method").and_then(Value::as_str).unwrap_or("").to_string();
let params = msg.get("params").cloned().unwrap_or(json!({}));
let (req, rrx) = ControlRequest::new(method, params);
if tx.send(req).is_err() {
break;
match tx.try_send(req) {
Ok(()) => {
ctx.request_repaint();
let mut r = rrx.recv_timeout(Duration::from_secs(90)).unwrap_or_else(|_| json!({"ok": false, "error": "timeout"}));
if let Some(o) = r.as_object_mut() {
o.insert("id".into(), id);
}
r
}
// Refuse rather than grow or block: the UI drains a bounded batch each tick.
Err(TrySendError::Full(_)) => json!({"ok": false, "id": id, "error": "control queue full"}),
Err(TrySendError::Disconnected(_)) => break,
}
ctx.request_repaint();
let mut r = rrx.recv_timeout(Duration::from_secs(90)).unwrap_or_else(|_| json!({"ok": false, "error": "timeout"}));
if let Some(o) = r.as_object_mut() {
o.insert("id".into(), id);
}
r
}
Err(e) => json!({"ok": false, "error": format!("bad JSON: {e}")}),
};
@@ -175,7 +189,7 @@ mod tests {
fn serve_echo() -> std::net::SocketAddr {
let listener = TcpListener::bind(("127.0.0.1", 0)).unwrap();
let addr = listener.local_addr().unwrap();
let (tx, rx) = channel::<ControlRequest>();
let (tx, rx) = sync_channel::<ControlRequest>(MAX_QUEUED_REQUESTS);
std::thread::spawn(move || accept_loop(listener, tx, egui::Context::default()));
std::thread::spawn(move || {
for req in rx {
+17 -5
View File
@@ -27,7 +27,7 @@ struct App(SoundApp, Option<&'static logging::AppLogger>, #[cfg(target_os = "mac
/// Sends the title only when it changes, remembered here as X11 and Wayland can't read it back.
fn set_title(ctx: &egui::Context, title: String) {
let id = egui::Id::new("window_title");
let id = egui::Id::unique("window_title");
if ctx.data(|d| d.get_temp::<String>(id)).as_deref() != Some(title.as_str()) {
ctx.data_mut(|d| d.insert_temp(id, title.clone()));
ctx.send_viewport_cmd(egui::ViewportCommand::Title(title));
@@ -35,6 +35,8 @@ fn set_title(ctx: &egui::Context, title: String) {
}
impl eframe::App for App {
/// Per-frame app logic: report audio-thread logs, run the shared app logic with the macOS menu driven around it,
/// then handle dropped paths, quit/close requests, window geometry, recent-list persistence and the window title.
fn logic(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
// What the realtime audio thread logged is written here, off that thread.
if let Some(logger) = self.1 {
@@ -52,7 +54,7 @@ impl eframe::App for App {
// Files dropped on the window: audio on an instrument track or a Sampler window loads
// the sampler; otherwise sessions open and audio/MIDI import.
let dropped: Vec<String> =
self.0.take_dropped_files(ctx).iter().map(|f| f.path().to_string_lossy().into_owned()).filter(|s| !s.is_empty()).collect();
self.0.take_dropped_files().iter().map(|f| f.path().to_string_lossy().into_owned()).filter(|s| !s.is_empty()).collect();
for p in dropped {
open_path(&mut self.0, &p);
}
@@ -86,8 +88,10 @@ impl eframe::App for App {
};
set_title(ctx, title);
}
fn raw_input_hook(&mut self, _ctx: &egui::Context, raw: &mut egui::RawInput) {
self.0.raw_input_hook(raw);
/// Forward raw input to the shared app hook so scripted gestures and host-key protection behave like the desktop
/// shell.
fn raw_input_hook(&mut self, ctx: &egui::Context, raw: &mut egui::RawInput) {
self.0.raw_input_hook(ctx, raw);
}
fn ui(&mut self, ui: &mut egui::Ui, _frame: &mut eframe::Frame) {
self.0.guarded(|app| app.ui(ui));
@@ -112,8 +116,10 @@ fn open_path(app: &mut SoundApp, p: &str) {
} else if lower.ends_with(".mid") || lower.ends_with(".midi") || lower.ends_with(".smf") {
app.run("file.import_midi", serde_json::json!({"path": p}))
} else {
// Decoded in the background so a long file doesn't freeze the window (#597).
let at = app.engine.session().edit.selection.start;
app.run("file.import_audio", serde_json::json!({"path": p, "at": at}))
soundcraft_ui_egui::import::start(app, serde_json::json!({"path": p, "at": at}));
Ok(serde_json::Value::Null)
};
if let Err(e) = r {
log::warn!("{p}: {e}");
@@ -204,6 +210,11 @@ fn app_icon() -> Option<egui::IconData> {
}
fn main() -> eframe::Result {
// Windows: the update helper is a copy of this program started with `--finish-update`.
#[cfg(windows)]
if let Some(result) = craft_update::self_update::finish(&craft_update::suite::SOUNDCRAFT, &std::env::args_os().skip(1).collect::<Vec<_>>()) {
std::process::exit(i32::from(result.is_err()));
}
// First, so every start-up record (and the engine's panic hook, installed with the first
// Engine) is captured; see `logging`.
let logger = logging::install();
@@ -270,6 +281,7 @@ fn main() -> eframe::Result {
let player =
if no_audio { None } else { Some(soundcraft_playback::Player::with_config(Arc::new(engine.session().clone()), &prefs.audio)) };
let mut app = SoundApp::new(engine, player, services());
app.set_startup_update_check(true);
app.autosave_dir = prefs_path().and_then(|p| p.parent().map(|d| d.join("Autosave")));
app.leftovers = app.autosave_dir.as_deref().map(soundcraft_engine::autosave::leftovers).unwrap_or_default();
app.preset_dir = prefs_path().and_then(|p| p.parent().map(|d| d.join("Presets")));
+6
View File
@@ -55,3 +55,9 @@ sync_github_name = true
real_name = "Marco Arnulfo"
display_name = "Marco Arnulfo"
sync_github_name = false
[people.mattburlage]
display_name = "Matt Smith-Burlage"
[people.ProdByBuddha]
sync_github_name = true
+1 -1
View File
@@ -192,7 +192,7 @@ pub enum AuError {
#[cfg(target_os = "macos")]
mod api {
use super::{AuDescriptor, AuError, Plugin, PluginInfo};
use super::{AuDescriptor, AuError, Plugin, PluginInfo, registry};
pub fn scan() -> Vec<AuDescriptor> {
registry::scan()
+1 -1
View File
@@ -14,7 +14,7 @@ fn malformed(what: &str) -> AudioError {
AudioError::Malformed(format!("CAF {what} is larger than the file"))
}
/// Walk the chunks the way symphonia 0.5 reads them: it advances by the bytes it parses, not by
/// Walk the chunks the way the CAF reader reads them: it advances by the bytes it parses, not by
/// the declared size, and after a `data` chunk of unknown size it reads on into the audio.
fn chunks(b: &[u8], mut pos: usize) -> Result<()> {
// Whether the packet table lists each packet's size and frame count (variable per packet).
+14 -3
View File
@@ -115,6 +115,7 @@ pub enum BitDepth {
Int16,
Int24,
Int32,
/// Preserve finite samples, including values above full scale; map NaN to 0 and infinities to ±1.
Float32,
}
@@ -136,14 +137,17 @@ impl Default for EncodeOptions {
}
}
/// Size of an ID3v2 tag at the start of `b`, if there is one.
/// Size of an ID3v2 tag at the start of `b`, if there is one. The 10-byte footer flag (`0x10`)
/// exists only in ID3v2.4; in v2.2/v2.3 that bit is reserved and must not extend the skip.
fn id3v2_len(b: &[u8]) -> Option<usize> {
if b.get(..3)? != b"ID3" {
return None;
}
let version = *b.get(3)?;
let flags = *b.get(5)?;
let size = b.get(6..10)?.iter().fold(0usize, |acc, &x| (acc << 7) | usize::from(x & 0x7F));
Some(10 + size + if flags & 0x10 != 0 { 10 } else { 0 })
let footer = version == 4 && flags & 0x10 != 0;
Some(10 + size + if footer { 10 } else { 0 })
}
fn detect_by_magic(b: &[u8], depth: u32) -> Option<FileFormat> {
@@ -317,7 +321,14 @@ impl<W: Write + Seek> FileEncoder<W> {
pub fn new(out: W, opts: &EncodeOptions, channels: usize, sample_rate: u32, mask: Option<u32>) -> Result<Self> {
let (depth, dither) = (opts.bit_depth, opts.dither);
Ok(FileEncoder(match opts.format {
FileFormat::Wav => Writer::Wav(wav::Writer::new(out, channels, sample_rate, depth, dither, opts.bwf.as_ref(), mask, false)?),
FileFormat::Wav => Writer::Wav(wav::Writer::new(
out,
channels,
sample_rate,
depth,
dither,
wav::HeaderExtras { bwf: opts.bwf.as_ref(), mask, force_rf64: false },
)?),
FileFormat::Aiff => Writer::Aiff(aiff::Writer::new(out, channels, sample_rate, depth, dither)?),
FileFormat::Flac => Writer::Flac(flac::Writer::new(out, channels, sample_rate, depth, dither)?),
other => return Err(AudioError::Unsupported(format!("encoding {other:?} is not supported"))),
+11 -5
View File
@@ -193,8 +193,8 @@ pub(crate) fn write_interleaved(
for (c, q) in quantizers.iter_mut().enumerate() {
let s = block.get(c).and_then(|ch| ch.get(f)).copied().unwrap_or(0.0);
match (is_float, big_endian) {
(true, false) => scratch.extend_from_slice(&clamp_float(s).to_le_bytes()),
(true, true) => scratch.extend_from_slice(&clamp_float(s).to_be_bytes()),
(true, false) => scratch.extend_from_slice(&sanitize_float(s).to_le_bytes()),
(true, true) => scratch.extend_from_slice(&sanitize_float(s).to_be_bytes()),
(false, false) => scratch.extend_from_slice(q.quantize(s).to_le_bytes().get(..size).unwrap_or_default()),
(false, true) => scratch.extend_from_slice(q.quantize(s).to_be_bytes().get(4 - size..).unwrap_or_default()),
}
@@ -206,9 +206,15 @@ pub(crate) fn write_interleaved(
Ok(())
}
/// Clamp a float sample to [-1, 1], mapping NaN to 0.
pub(crate) fn clamp_float(x: f32) -> f32 {
if x.is_nan() { 0.0 } else { x.clamp(-1.0, 1.0) }
/// Preserve finite float samples, mapping NaN to 0 and infinities to their sign (±1).
fn sanitize_float(x: f32) -> f32 {
if x.is_finite() {
x
} else if x.is_nan() {
0.0
} else {
x.signum()
}
}
/// Check that a buffer is encodable: at least one channel and a non-zero sample rate.
+452 -112
View File
@@ -1,9 +1,8 @@
//! Decode path for everything that isn't native PCM WAV/AIFF, backed by `symphonia`.
use std::io::Cursor;
use std::io::{Cursor, Seek, SeekFrom};
use std::panic::{AssertUnwindSafe, catch_unwind};
use symphonia::core::codecs::CodecParameters;
use symphonia::core::codecs::audio::well_known::{
CODEC_ID_AAC, CODEC_ID_ADPCM_G722, CODEC_ID_ADPCM_G726, CODEC_ID_ADPCM_G726LE, CODEC_ID_ADPCM_IMA_QT, CODEC_ID_ADPCM_IMA_WAV, CODEC_ID_ADPCM_MS,
CODEC_ID_ALAC, CODEC_ID_FLAC, CODEC_ID_MP1, CODEC_ID_MP2, CODEC_ID_MP3, CODEC_ID_OPUS, CODEC_ID_PCM_ALAW, CODEC_ID_PCM_F32BE, CODEC_ID_PCM_F32LE,
@@ -11,9 +10,9 @@ use symphonia::core::codecs::audio::well_known::{
};
use symphonia::core::codecs::audio::{AudioCodecId, AudioCodecParameters, AudioDecoderOptions, CODEC_ID_NULL_AUDIO};
use symphonia::core::errors::Error as SymError;
use symphonia::core::formats::probe::Hint;
use symphonia::core::formats::{FormatOptions, FormatReader};
use symphonia::core::io::{MediaSourceStream, MediaSourceStreamOptions, ReadBytes};
use symphonia::core::formats::probe::{Hint, ProbeOptions, ProbeableFormat, Score, Scoreable};
use symphonia::core::formats::{FormatOptions, FormatReader, Track};
use symphonia::core::io::{MediaSourceStream, MediaSourceStreamOptions, ScopedStream};
use symphonia::core::meta::MetadataOptions;
use crate::pcm::{MAX_DECODE_SAMPLES, be_u32};
@@ -23,6 +22,45 @@ use crate::{AudioBuffer, AudioError, AudioInfo, FileFormat, Result, SampleFormat
/// takes its frame length from the file (encoders use 4096).
const MAX_PACKET_FRAMES: u64 = 1 << 20;
/// How far [`first_marker`] looks for the first format marker: the same 1 MiB a default symphonia
/// probe scans.
const MAX_PREFIX_SCAN: usize = 1 << 20;
/// How many MPEG/ADTS candidates [`first_marker`] may hand to the readers' expensive `Scoreable`
/// implementations. A crafted 1 MiB prefix of repeated sync markers otherwise asks for hundreds
/// of thousands of scores, each allowed up to symphonia's 16 KiB score depth. Once the budget is
/// spent, the scan stops at the next audio candidate: a container-like byte string later in an
/// unscored audio payload must not become authoritative. A container marker reached first is still
/// checked; an unchecked one the probe selects is refused after probing.
const MAX_AUDIO_SCORE_ATTEMPTS: usize = 256;
/// A per-scan budget for `Scoreable::score` calls. Taking a slot consumes it even when the score
/// rejects the candidate, so a stream of false syncs cannot multiply the work.
struct ScoreBudget {
used: usize,
}
impl ScoreBudget {
/// Start a scan with no expensive score attempts spent.
fn new() -> Self {
Self { used: 0 }
}
/// Spend one score attempt, returning false once the per-scan budget is exhausted.
fn take(&mut self) -> bool {
if self.exhausted() {
return false;
}
self.used = self.used.saturating_add(1);
true
}
/// Whether no attempt is left for this scan.
fn exhausted(&self) -> bool {
self.used >= MAX_AUDIO_SCORE_ATTEMPTS
}
}
/// Turn a symphonia error into the audio layer's error, keeping "unsupported" and "too large"
/// distinct from a malformed file.
fn map_err(e: SymError) -> AudioError {
@@ -44,61 +82,84 @@ enum Opened {
Empty(AudioInfo),
}
/// Skip a leading ID3v2 tag before probing.
///
/// Tags aren't used, and symphonia allocates whatever an ID3v2 frame claims, so the tag is skipped
/// with [`ReadBytes::ignore_bytes`] (which allocates nothing) instead of being handed to the
/// metadata reader. Returns the stream position the container starts at.
fn skip_leading_id3(mss: &mut MediaSourceStream<'_>, bytes: &[u8]) -> Result<usize> {
/// Decoder options for every compressed format. Gapless playback used to be a
/// `FormatOptions` flag in symphonia 0.5; in 0.6 the decoder option (on by default) trims the
/// encoder delay and padding that the demuxer reports on each packet.
fn decoder_options() -> AudioDecoderOptions {
AudioDecoderOptions::default().gapless(true)
}
/// The first byte after any ID3v2 tags at the start of `b`. Symphonia's tag reader allocates
/// whatever a frame claims, so the tags are skipped, not read: they are not used here.
fn skip_id3v2(b: &[u8]) -> usize {
let mut at = 0usize;
loop {
let at = usize::try_from(mss.pos()).unwrap_or(usize::MAX);
let head = bytes.get(at..).unwrap_or_default();
if head.len() < 10 || head.get(..3) != Some(b"ID3") {
return Ok(at);
match b.get(at..).and_then(crate::id3v2_len) {
Some(n) if n > 0 => at = at.saturating_add(n),
_ => return at,
}
let size = head.get(6..10).map(|size| size.iter().fold(0u64, |v, &x| (v << 7) | u64::from(x & 0x7f))).unwrap_or(0);
mss.ignore_bytes(10 + size).map_err(|e| AudioError::Malformed(e.to_string()))?;
}
}
/// How far symphonia's probe scans for a format marker before giving up.
const MAX_PROBE_DEPTH: usize = 1024 * 1024;
/// The container marker each detected format starts with, if its reader needs size checks first.
fn container_markers(detected: FileFormat) -> &'static [&'static [u8; 4]] {
match detected {
// RF64 and BW64 are the >4 GiB RIFF forms.
FileFormat::Wav => &[b"RIFF", b"RF64", b"BW64"],
FileFormat::Flac => &[b"fLaC"],
FileFormat::Caf => &[b"caff"],
// MP4/M4A put `ftyp` after the first atom's size field.
FileFormat::Aac | FileFormat::Alac => &[b"ftyp"],
// Unknown files may carry junk before the container; symphonia's probe scans for markers.
FileFormat::Other => &[b"caff", b"ftyp", b"fLaC", b"RIFF"],
FileFormat::Aiff | FileFormat::Mp3 | FileFormat::Ogg => &[],
}
/// The first format marker the probe can reach from `start`, within its 1 MiB scan limit.
#[derive(Clone, Copy)]
enum FirstMarker {
Flac(usize),
Caf(usize),
Mp4(usize),
Wav(usize),
/// MPEG audio, Ogg, Matroska or AIFF: formats with no separate container size check.
Audio,
}
/// The probe short names a container marker stands for.
fn expected_probe_format(marker: &[u8; 4]) -> &'static [&'static str] {
match marker {
b"caff" => &["caf"],
// The isomp4 reader reads the `ftyp` atom.
b"ftyp" => &["isomp4"],
b"fLaC" => &["flac"],
b"RIFF" | b"RF64" | b"BW64" => &["wave"],
_ => &[],
}
/// What the preflight container checks covered before the probe built a reader.
enum Precheck {
/// A zero-frame FLAC: returned without a reader, because Symphonia 0.6's reader needs a first
/// audio frame to resynchronize to.
Empty(AudioInfo),
/// The size checks ran for the container the probe reports under this short name.
Checked(&'static str),
/// No container size check was needed.
Unchecked,
}
/// Probe short names whose demuxer allocates from sizes in the file, so the matching container
/// check has to run before the stream is decoded.
const SIZE_CHECKED_PROBE_FORMATS: &[&str] = &["caf", "flac", "isomp4", "wave"];
/// Find the first container marker at or after `at`, within symphonia's probe depth. A RIFF form
/// other than WAVE is skipped, as the probe skips it and reads a WAVE behind it.
fn find_marker<'a>(bytes: &[u8], at: usize, markers: &'a [&'static [u8; 4]]) -> Option<(usize, &'a [u8; 4])> {
let end = bytes.len().min(at.saturating_add(MAX_PROBE_DEPTH));
/// The container markers to check for the family `detected` names. `None` is a raw audio stream
/// (MP3, Ogg, AIFF), whose payload may contain container-like bytes: the unrestricted scan below
/// finds a container only when one starts the stream, before any audio marker.
fn container_markers(detected: FileFormat) -> Option<&'static [&'static [u8; 4]]> {
match detected {
// RF64 and BW64 are the >4 GiB RIFF forms.
FileFormat::Wav => Some(&[b"RIFF", b"RF64", b"BW64"]),
FileFormat::Flac => Some(&[b"fLaC"]),
FileFormat::Caf => Some(&[b"caff"]),
// MP4/M4A put `ftyp` after the first atom's size field.
FileFormat::Aac | FileFormat::Alac => Some(&[b"ftyp"]),
// Unknown files may carry junk before the container; symphonia's probe scans for markers.
FileFormat::Other => Some(&[b"caff", b"ftyp", b"fLaC", b"RIFF"]),
FileFormat::Aiff | FileFormat::Mp3 | FileFormat::Ogg => None,
}
}
/// The probe short name a container marker stands for.
fn expected_probe_format(marker: &[u8; 4]) -> &'static str {
match marker {
b"caff" => "caf",
// The isomp4 reader reads the `ftyp` atom.
b"ftyp" => "isomp4",
b"fLaC" => "flac",
// RIFF, RF64 and BW64.
_ => "wave",
}
}
/// Find the first marker of `markers` at or after `at`, within symphonia's probe depth. A RIFF
/// form other than WAVE is skipped, as the probe skips it and reads a WAVE behind it.
fn find_container_marker<'a>(bytes: &[u8], at: usize, markers: &'a [&'static [u8; 4]]) -> Option<(usize, &'a [u8; 4])> {
let end = bytes.len().min(at.saturating_add(MAX_PREFIX_SCAN));
for pos in at..end.saturating_sub(3) {
let window = bytes.get(pos..pos + 4)?;
if let Some(marker) = markers.iter().find(|marker| window == marker.as_slice()) {
@@ -111,58 +172,211 @@ fn find_marker<'a>(bytes: &[u8], at: usize, markers: &'a [&'static [u8; 4]]) ->
None
}
/// Probe for a container, checking the container it finds before its reader allocates whatever the
/// file claims.
///
/// The check has to run on the container the probe actually selects, but the probe reports that
/// container only through the reader it returns. The check therefore runs on the first marker the
/// detected format allows, and the reader is refused if its format is not the checked one: a
/// misleading marker must not let a later container slip through unchecked.
fn open(bytes: &[u8], detected: FileFormat) -> Result<Opened> {
let mut mss = MediaSourceStream::new(Box::new(Cursor::new(bytes.to_vec())), MediaSourceStreamOptions::default());
let at = skip_leading_id3(&mut mss, bytes)?;
let mut checked: Option<&'static [&'static str]> = None;
if let Some((found, marker)) = find_marker(bytes, at, container_markers(detected)) {
match marker {
b"caff" => {
crate::caf::check(bytes, found)?;
checked = Some(expected_probe_format(marker));
}
b"ftyp" => {
crate::mp4::check(bytes, found.saturating_sub(4))?;
checked = Some(expected_probe_format(marker));
}
b"fLaC" => {
crate::flac::check_metadata(bytes, found)?;
if let Some(info) = crate::flac::empty_stream_info(bytes, found) {
return Ok(Opened::Empty(info));
}
checked = Some(expected_probe_format(marker));
}
b"RIFF" | b"RF64" | b"BW64" => {
crate::wav::check_info(bytes, found)?;
checked = Some(expected_probe_format(marker));
}
_ => {}
}
/// The scan variant for a marker found by [`find_container_marker`], with the probe short name its
/// size check stands for. `ftyp` sits four bytes into its atom, so the MP4 check starts there.
fn marker_kind(at: usize, marker: &[u8; 4]) -> (FirstMarker, &'static str) {
match marker {
b"fLaC" => (FirstMarker::Flac(at), expected_probe_format(marker)),
b"caff" => (FirstMarker::Caf(at), expected_probe_format(marker)),
b"ftyp" => (FirstMarker::Mp4(at.saturating_sub(4)), expected_probe_format(marker)),
_ => (FirstMarker::Wav(at), expected_probe_format(marker)),
}
let reader = symphonia::default::get_probe().probe(&Hint::new(), mss, FormatOptions::default(), MetadataOptions::default()).map_err(map_err)?;
}
/// The probe short name a container scan variant stands for.
fn marker_name(marker: FirstMarker) -> &'static str {
match marker {
FirstMarker::Flac(_) => "flac",
FirstMarker::Caf(_) => "caf",
FirstMarker::Mp4(_) => "isomp4",
FirstMarker::Wav(_) => "wave",
FirstMarker::Audio => "",
}
}
/// Whether `rest` starts with one of `R`'s exact registered probe markers.
fn has_probe_marker<R: ProbeableFormat<'static>>(rest: &[u8]) -> bool {
R::probe_data().iter().any(|data| data.spec.markers.iter().any(|marker| rest.starts_with(marker)))
}
/// Score `R` at `at` exactly as symphonia's private probe does: `Supported` wins, EOF and every
/// other scoring error mean "not this format", while a real I/O error aborts the probe.
fn scores_supported<R: Scoreable>(stream: &mut MediaSourceStream<'_>, at: usize, depth: u16) -> Result<bool> {
stream.seek(SeekFrom::Start(u64::try_from(at).unwrap_or(u64::MAX)))?;
match R::score(ScopedStream::new(stream, u64::from(depth))) {
Ok(Score::Supported(_)) => Ok(true),
Err(SymError::IoError(e)) if e.kind() != std::io::ErrorKind::UnexpectedEof => Err(AudioError::Malformed(e.to_string())),
_ => Ok(false),
}
}
/// Whether `b[at..]` is an MPEG/ADTS candidate the probe would actually select: an exact
/// `probe_data()` marker and a `Supported` score from the same reader. A loose `0xFFEx` pair is
/// not enough: `0xFFE0` is neither a registered marker nor a valid frame, so it must not stop the
/// scan before a later FLAC/CAF/MP4 candidate.
fn audio_sync_at<'a>(b: &'a [u8], at: usize, stream: &mut Option<MediaSourceStream<'a>>, budget: &mut ScoreBudget) -> Result<bool> {
let rest = b.get(at..).unwrap_or_default();
let adts = has_probe_marker::<symphonia::default::formats::AdtsReader<'static>>(rest);
let mpa = has_probe_marker::<symphonia::default::formats::MpaReader<'static>>(rest);
if !adts && !mpa {
return Ok(false);
}
if stream.is_none() {
*stream = Some(MediaSourceStream::new(Box::new(Cursor::new(b)), MediaSourceStreamOptions::default()));
}
let Some(mss) = stream.as_mut() else { return Ok(false) };
let depth = ProbeOptions::default().max_score_depth;
if adts && budget.take() && scores_supported::<symphonia::default::formats::AdtsReader<'static>>(mss, at, depth)? {
return Ok(true);
}
if mpa && budget.take() && scores_supported::<symphonia::default::formats::MpaReader<'static>>(mss, at, depth)? {
return Ok(true);
}
Ok(false)
}
/// Find the first marker that can start a container or audio stream. Symphonia's `Probe::probe`
/// constructs the reader itself and exposes no pre-construction hook, while checking every magic
/// in the scan window would validate bytes the probe never reaches (for example a valid MP3's
/// audio payload). Stopping at the first marker avoids that: it is the candidate the probe selects
/// for FLAC, CAF, MP4, Ogg and Matroska, and for WAV/AIFF once their full form is verified below.
/// MPEG/ADTS candidates are checked with the readers' own `probe_data()`/`Scoreable` APIs, so a
/// false sync byte pair cannot stop the scan. If that scoring budget runs out, the scan stops at
/// the next audio candidate instead of trusting a container-like byte string later in the unscored
/// audio payload. A container marker reached before any such candidate is still preflighted; if
/// the probe selects a size-checked container the scan did not check, `open` refuses it.
///
/// WAV candidates include RF64/BW64 so `check_info` can run, although symphonia 0.6 registers only
/// the RIFF marker. Native PCM RF64/BW64 is parsed by `wav::parse` before this path; a foreign
/// RF64/BW64 stream therefore remains unsupported by the probe rather than misdetected.
fn first_marker(b: &[u8], start: usize) -> Result<Option<FirstMarker>> {
first_marker_with_budget(b, start, &mut ScoreBudget::new())
}
/// [`first_marker`] with an explicit score budget, so a test can prove a prefix crowded with
/// false sync markers performs only the bounded number of reader scores.
fn first_marker_with_budget(b: &[u8], start: usize, budget: &mut ScoreBudget) -> Result<Option<FirstMarker>> {
let end = b.len().min(start.saturating_add(MAX_PREFIX_SCAN));
let mut at = start;
let mut audio_probe = None;
while at.saturating_add(4) <= end {
let rest = b.get(at..).unwrap_or_default();
if rest.get(..4) == Some(b"fLaC".as_slice()) {
return Ok(Some(FirstMarker::Flac(at)));
}
if rest.get(..4) == Some(b"caff".as_slice()) {
return Ok(Some(FirstMarker::Caf(at)));
}
if rest.get(..4) == Some(b"OggS".as_slice()) || rest.get(..4) == Some(b"\x1A\x45\xDF\xA3".as_slice()) || crate::aiff::is_aiff(rest) {
return Ok(Some(FirstMarker::Audio));
}
if crate::wav::is_wav(rest) {
return Ok(Some(FirstMarker::Wav(at)));
}
if let (Some(&b0), Some(&b1)) = (b.get(at), b.get(at + 1))
&& b0 == 0xFF
&& b1 & 0xE0 == 0xE0
{
// A candidate after the cap cannot be scored. Stop rather than let a container-like
// byte string later in an unscored audio payload become preflight-authoritative; the
// probe will select the reader, and `open` refuses an unchecked size-checked one.
if budget.exhausted() {
return Ok(None);
}
if audio_sync_at(b, at, &mut audio_probe, budget)? {
return Ok(Some(FirstMarker::Audio));
}
}
if rest.get(4..8) == Some(b"ftyp".as_slice()) {
return Ok(Some(FirstMarker::Mp4(at)));
}
at += 1;
}
Ok(None)
}
/// Run the early size checks on the container the probe can select. `start` is the first byte
/// after leading ID3v2 tags. A zero-frame FLAC is returned as an empty stream because Symphonia
/// 0.6's reader requires a first audio frame to resynchronize to.
fn precheck(b: &[u8], start: usize, detected: FileFormat) -> Result<Precheck> {
let any_marker = |b: &[u8], start: usize| -> Result<Option<(FirstMarker, &'static str)>> {
Ok(match first_marker(b, start)? {
// Stop at the first marker of any kind, so container magic incidental to an audio
// payload is not mistaken for the stream's container.
Some(FirstMarker::Audio) | None => None,
Some(marker) => Some((marker, marker_name(marker))),
})
};
let found = match container_markers(detected) {
// Follow the detection: check the first marker of the family it names, so a misleading
// marker cannot let a differently sized container reach the probe unchecked. A detection
// is only a hint, though: when the stream holds none of its markers (a FLAC behind a
// `.mp3` name), the probe can still select another container, which is checked here.
Some(markers) => match find_container_marker(b, start, markers) {
Some((at, marker)) => Some(marker_kind(at, marker)),
None => any_marker(b, start)?,
},
None => any_marker(b, start)?,
};
let Some((marker, expected)) = found else { return Ok(Precheck::Unchecked) };
match marker {
FirstMarker::Flac(at) => {
crate::flac::check_metadata(b, at)?;
if let Some(info) = crate::flac::empty_stream_info(b, at) {
return Ok(Precheck::Empty(info));
}
Ok(Precheck::Checked(expected))
}
FirstMarker::Caf(at) => {
crate::caf::check(b, at)?;
Ok(Precheck::Checked(expected))
}
FirstMarker::Mp4(at) => {
crate::mp4::check(b, at)?;
Ok(Precheck::Checked(expected))
}
FirstMarker::Wav(at) => {
crate::wav::check_info(b, at)?;
Ok(Precheck::Checked(expected))
}
FirstMarker::Audio => Ok(Precheck::Unchecked),
}
}
/// Skip leading ID3v2 tags, run the container size preflight, then probe with symphonia; a zero-frame FLAC found by
/// the preflight comes back as [`Opened::Empty`] without probing. A size-checked container is refused when probing
/// selects a different reader or one that was never checked, so incidental payload magic cannot bypass those limits.
fn open(bytes: &[u8], detected: FileFormat) -> Result<Opened> {
let start = skip_id3v2(bytes);
if start > 0 && start >= bytes.len() {
return Err(AudioError::Malformed("ID3v2 tag extends past the end of the file".into()));
}
let checked = precheck(bytes, start, detected)?;
if let Precheck::Empty(info) = checked {
return Ok(Opened::Empty(info));
}
let mut mss = MediaSourceStream::new(Box::new(Cursor::new(bytes.to_vec())), MediaSourceStreamOptions::default());
// Seek (rather than pre-positioning the cursor) so the stream's absolute position is `start`
// too: container offsets such as MP4's `stco` are absolute file offsets.
mss.seek(SeekFrom::Start(u64::try_from(start).unwrap_or(u64::MAX)))?;
let reader: Box<dyn FormatReader> =
symphonia::default::get_probe().probe(&Hint::new(), mss, FormatOptions::default(), MetadataOptions::default()).map_err(map_err)?;
let probed = reader.format_info().short_name;
// Only containers whose readers allocate from sizes in the file need the check. A stream the
// probe reads with another reader (MP3, Ogg, AIFF, ...) has no such size fields, so a marker
// found in its audio data must not reject it.
if let Some(names) = checked.filter(|_| SIZE_CHECKED_PROBE_FORMATS.contains(&probed)) {
if !names.contains(&probed) {
match checked {
// Only containers whose readers allocate from sizes in the file need the check. A stream
// the probe reads with another reader (MP3, Ogg, AIFF, ...) has no such size fields, so a
// marker found in its audio data must not reject it.
Precheck::Checked(expected) if SIZE_CHECKED_PROBE_FORMATS.contains(&probed) && probed != expected => {
return Err(AudioError::Malformed(format!("the probed container ({probed}) is not the one whose size checks ran")));
}
} else if checked.is_none() && SIZE_CHECKED_PROBE_FORMATS.contains(&probed) {
return Err(AudioError::Malformed(format!("the probed container ({probed}) was not size-checked")));
Precheck::Unchecked if SIZE_CHECKED_PROBE_FORMATS.contains(&probed) => {
return Err(AudioError::Malformed(format!("the probed container ({probed}) was not size-checked")));
}
_ => {}
}
Ok(Opened::Reader(reader))
}
/// The container `detected` stands for: the detection wins when it knows the file, otherwise the
/// decoded codec names the family.
fn format_from_codec(codec: AudioCodecId, detected: FileFormat) -> FileFormat {
if detected != FileFormat::Other {
return detected;
@@ -213,16 +427,19 @@ fn sample_format_of(p: &AudioCodecParameters) -> SampleFormat {
}
}
/// The audio parameters of `track`, if it carries any.
fn audio_params(track: &Track) -> Option<&AudioCodecParameters> {
track.codec_params.as_ref()?.audio()
}
/// The first track with a decodable audio codec, with the stream information the UI shows.
fn info_of(reader: &dyn FormatReader, detected: FileFormat) -> Result<(u32, AudioInfo)> {
let (track, p) = reader
let track = reader
.tracks()
.iter()
.find_map(|t| match t.codec_params.as_ref().and_then(CodecParameters::audio) {
Some(p) if p.codec != CODEC_ID_NULL_AUDIO => Some((t, p)),
_ => None,
})
.find(|t| audio_params(t).is_some_and(|p| p.codec != CODEC_ID_NULL_AUDIO))
.ok_or_else(|| AudioError::Unsupported("no decodable audio track".into()))?;
let p = audio_params(track).ok_or_else(|| AudioError::Unsupported("no decodable audio track".into()))?;
let sample_rate = p.sample_rate.filter(|&r| r > 0).ok_or_else(|| AudioError::Malformed("unknown sample rate".into()))?;
let channels = p.channels.as_ref().map(|c| c.count()).unwrap_or(0);
let channels = u16::try_from(channels).map_err(|_| AudioError::Unsupported("too many channels".into()))?;
@@ -249,28 +466,22 @@ fn run(bytes: &[u8], detected: FileFormat, count_only: bool) -> Result<(AudioInf
}
};
let (track_id, mut info) = info_of(reader.as_ref(), detected)?;
if let Some(n) = reader.tracks().iter().find(|t| t.id == track_id).and_then(|t| t.num_frames) {
let track = reader.tracks().iter().find(|t| t.id == track_id).ok_or_else(|| AudioError::Malformed("track vanished".into()))?;
if let Some(n) = track.num_frames {
let total = n.saturating_mul(u64::from(info.channels.max(1)));
if !count_only && total > MAX_DECODE_SAMPLES {
return Err(AudioError::TooLarge(format!("{total} samples exceeds the decode limit")));
}
}
let params = reader
.tracks()
.iter()
.find(|t| t.id == track_id)
.and_then(|t| t.codec_params.as_ref())
.and_then(CodecParameters::audio)
.cloned()
.ok_or_else(|| AudioError::Malformed("track vanished".into()))?;
let params = audio_params(track).cloned().ok_or_else(|| AudioError::Malformed("track vanished".into()))?;
let alac = params.extra_data.as_deref().filter(|_| params.codec == CODEC_ID_ALAC).and_then(|d| be_u32(d, 0));
if params.max_frames_per_packet.into_iter().chain(alac.map(u64::from)).any(|n| n > MAX_PACKET_FRAMES) {
return Err(AudioError::Malformed("the file declares packets far longer than any codec uses".into()));
}
let mut decoder = symphonia::default::get_codecs().make_audio_decoder(&params, &AudioDecoderOptions::default()).map_err(map_err)?;
let mut decoder = symphonia::default::get_codecs().make_audio_decoder(&params, &decoder_options()).map_err(map_err)?;
let mut planar: Vec<Vec<f32>> = Vec::new();
let mut scratch: Vec<Vec<f32>> = Vec::new();
let mut interleaved: Vec<f32> = Vec::new();
let mut frames: u64 = 0;
let mut errors = 0usize;
loop {
@@ -303,8 +514,9 @@ fn run(bytes: &[u8], detected: FileFormat, count_only: bool) -> Result<(AudioInf
Err(SymError::IoError(_)) => break,
Err(e) => return Err(map_err(e)),
};
let spec = decoded.spec();
let ch = spec.channels().count();
// The interleaved copy below is laid out by planes: use their count so a decoder whose
// channel spec disagrees with its planes cannot trip the copy's length assertion.
let ch = decoded.num_planes();
if ch == 0 {
continue;
}
@@ -321,9 +533,19 @@ fn run(bytes: &[u8], detected: FileFormat, count_only: bool) -> Result<(AudioInf
if count_only {
continue;
}
decoded.copy_to_vecs_planar::<f32>(&mut scratch);
for (dst, src) in planar.iter_mut().zip(&scratch) {
dst.extend(src.iter().map(|&s| if s.is_finite() { s } else { 0.0 }));
let needed = usize::try_from(n)
.ok()
.and_then(|n| n.checked_mul(ch))
.ok_or_else(|| AudioError::TooLarge("decoded audio exceeds the 4 GiB limit".into()))?;
if interleaved.len() < needed {
interleaved.try_reserve(needed - interleaved.len()).map_err(|_| AudioError::TooLarge("cannot allocate decoded audio".into()))?;
interleaved.resize(needed, 0.0);
}
decoded.copy_to_slice_interleaved(&mut interleaved[..needed]);
for frame in interleaved[..needed].chunks_exact(ch) {
for (dst, &s) in planar.iter_mut().zip(frame) {
dst.push(if s.is_finite() { s } else { 0.0 });
}
}
}
if info.channels == 0 {
@@ -355,3 +577,121 @@ pub(crate) fn probe(bytes: &[u8], detected: FileFormat) -> Result<AudioInfo> {
pub(crate) fn decode(bytes: &[u8], detected: FileFormat) -> Result<(AudioInfo, AudioBuffer)> {
guarded(|| run(bytes, detected, false))
}
#[cfg(test)]
mod tests {
use super::*;
use symphonia::core::codecs::audio::well_known::{
CODEC_ID_AAC, CODEC_ID_ADPCM_MS, CODEC_ID_ALAC, CODEC_ID_FLAC, CODEC_ID_MP3, CODEC_ID_PCM_F32LE, CODEC_ID_PCM_F64LE, CODEC_ID_PCM_S16LE,
CODEC_ID_VORBIS,
};
/// Build codec parameters for a test, optionally declaring a bit depth.
fn params(codec: AudioCodecId, bits_per_sample: Option<u32>) -> AudioCodecParameters {
let mut params = AudioCodecParameters::new();
params.for_codec(codec);
if let Some(bits) = bits_per_sample {
params.with_bits_per_sample(bits);
}
params
}
/// Codec ids map to their reported formats, while a format detected from the container wins over the codec.
#[test]
fn codecs_map_to_the_reported_formats() {
assert_eq!(format_from_codec(CODEC_ID_FLAC, FileFormat::Other), FileFormat::Flac);
assert_eq!(format_from_codec(CODEC_ID_MP3, FileFormat::Other), FileFormat::Mp3);
assert_eq!(format_from_codec(CODEC_ID_AAC, FileFormat::Other), FileFormat::Aac);
assert_eq!(format_from_codec(CODEC_ID_ALAC, FileFormat::Other), FileFormat::Alac);
assert_eq!(format_from_codec(CODEC_ID_VORBIS, FileFormat::Other), FileFormat::Ogg);
// The container detected from the bytes wins over the codec.
assert_eq!(format_from_codec(CODEC_ID_ALAC, FileFormat::Aac), FileFormat::Aac);
}
/// PCM and FLAC keep their declared bit depth; lossy codecs report `Compressed` regardless of their decoded
/// width.
#[test]
fn sample_formats_keep_their_depth_and_lossy_marker() {
assert_eq!(sample_format_of(&params(CODEC_ID_PCM_S16LE, Some(16))), SampleFormat::Int16);
assert_eq!(sample_format_of(&params(CODEC_ID_FLAC, Some(24))), SampleFormat::Int24);
assert_eq!(sample_format_of(&params(CODEC_ID_FLAC, Some(8))), SampleFormat::Int8);
assert_eq!(sample_format_of(&params(CODEC_ID_FLAC, Some(32))), SampleFormat::Int32);
assert_eq!(sample_format_of(&params(CODEC_ID_PCM_F32LE, Some(32))), SampleFormat::Float32);
assert_eq!(sample_format_of(&params(CODEC_ID_PCM_F64LE, Some(64))), SampleFormat::Float64);
// Lossy codecs report no fixed bit depth, whatever their decoded sample format is.
assert_eq!(sample_format_of(&params(CODEC_ID_AAC, Some(16))), SampleFormat::Compressed);
assert_eq!(sample_format_of(&params(CODEC_ID_ADPCM_MS, Some(4))), SampleFormat::Compressed);
assert_eq!(sample_format_of(&params(CODEC_ID_ALAC, None)), SampleFormat::Compressed);
}
/// Decoder options keep gapless trimming enabled so the encoder delay and padding reported by the demuxer are
/// removed.
#[test]
fn gapless_decoding_stays_enabled() {
assert!(decoder_options().gapless);
}
/// A complete MPEG frame header is a candidate; loose or unregistered sync pairs are rejected before scoring,
/// while a registered but truncated header is rejected by the reader's score after consuming a budget slot.
#[test]
fn only_the_readers_own_audio_markers_are_candidates() {
/// Whether the preflight would score `b` as an audio candidate at offset 0.
fn candidate(b: &[u8]) -> bool {
let mut stream = None;
let mut budget = ScoreBudget::new();
matches!(audio_sync_at(b, 0, &mut stream, &mut budget), Ok(true))
}
let mut mp3_frame = vec![0u8; 417];
mp3_frame[..4].copy_from_slice(&[0xFF, 0xFB, 0x90, 0x00]); // MPEG-1 Layer III, 128 kbps, 44.1 kHz
assert!(candidate(&[&mp3_frame[..], &mp3_frame].concat()));
for false_sync in [vec![0xFF, 0xE0, 0x00], vec![0xFF, 0xF0, 0x00], vec![0xFF, 0xFB, 0xF0, 0x00]] {
assert!(!candidate(&false_sync), "{false_sync:?} is not a probe candidate");
}
}
/// A prefix made of false MPEG sync markers must not ask the readers to score every byte
/// position: the per-scan budget caps the expensive work. A cheap container marker reached
/// before the next audio candidate is still found, but once the cap is spent an audio
/// candidate stops the scan so later payload magic cannot be preflight-authoritative.
#[test]
fn a_prefix_crowded_with_sync_markers_scores_only_the_budget() {
let mut crowded = vec![0u8; 1 << 16];
for pair in crowded.as_chunks_mut::<2>().0 {
pair.copy_from_slice(&[0xFF, 0xE2]);
}
let mut budget = ScoreBudget::new();
assert!(matches!(first_marker_with_budget(&crowded, 0, &mut budget), Ok(None)));
assert_eq!(budget.used, MAX_AUDIO_SCORE_ATTEMPTS, "no candidate after the cap is scored");
// At the cap boundary, with no later audio candidate, the cheap marker is still found.
let mut at_cap: Vec<u8> = (0..MAX_AUDIO_SCORE_ATTEMPTS).flat_map(|_| [0xFFu8, 0xE2]).collect();
at_cap.extend_from_slice(b"fLaC");
let mut budget = ScoreBudget::new();
assert!(
matches!(first_marker_with_budget(&at_cap, 0, &mut budget), Ok(Some(FirstMarker::Flac(_)))),
"a container marker reached before the next audio candidate is still found"
);
assert_eq!(budget.used, MAX_AUDIO_SCORE_ATTEMPTS);
// One more false candidate between the cap and the magic makes the scan stop unchecked:
// the same magic could instead sit in a valid audio payload.
let mut after_candidate = at_cap[..2 * MAX_AUDIO_SCORE_ATTEMPTS].to_vec();
after_candidate.extend_from_slice(&[0xFF, 0xE2]);
after_candidate.extend_from_slice(b"fLaC");
let mut budget = ScoreBudget::new();
assert!(matches!(first_marker_with_budget(&after_candidate, 0, &mut budget), Ok(None)), "an audio candidate after the cap stops the scan");
assert_eq!(budget.used, MAX_AUDIO_SCORE_ATTEMPTS);
}
/// ID3v2 sizes are read in their 7-bit syncsafe form; consecutive tags are skipped by their declared sizes, and
/// the v2.3 reserved footer bit does not extend a tag.
#[test]
fn leading_id3_tags_are_skipped_by_their_declared_size() {
let tag = [&b"ID3\x04\0\0\0\0\0\x05"[..], b"hello"].concat();
assert_eq!(skip_id3v2(&[&tag[..], &tag[..]].concat()), 30);
assert_eq!(skip_id3v2(b"fLaC"), 0);
// The 10-byte footer flag exists only in v2.4; the same bit in v2.3 is reserved.
assert_eq!(crate::id3v2_len(b"ID3\x03\0\x10\0\0\0\0"), Some(10));
assert_eq!(crate::id3v2_len(b"ID3\x04\0\x10\0\0\0\0"), Some(20));
}
}
+31 -17
View File
@@ -10,6 +10,8 @@ const WAVE_FORMAT_IEEE_FLOAT: u16 = 3;
const WAVE_FORMAT_EXTENSIBLE: u16 = 0xFFFE;
/// Tail of the KSDATAFORMAT_SUBTYPE_* GUIDs (bytes 2..16); bytes 0..2 hold the format tag.
const GUID_TAIL: [u8; 14] = [0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x80, 0x00, 0x00, 0xAA, 0x00, 0x38, 0x9B, 0x71];
/// Tail of the Ambisonic subtype GUIDs {0000000x-0721-11D3-8644-C8C1CA000000}: PCM (x = 1) and float (x = 3) as well.
const AMBISONIC_TAIL: [u8; 14] = [0x00, 0x00, 0x21, 0x07, 0xD3, 0x11, 0x86, 0x44, 0xC8, 0xC1, 0xCA, 0x00, 0x00, 0x00];
const BEXT_MIN_LEN: usize = 602;
/// Parsed WAV layout (no sample data decoded yet).
@@ -50,13 +52,25 @@ fn parse_fmt(c: &[u8]) -> Result<Fmt> {
fmt.valid_bits = fmt.bits;
if fmt.tag == WAVE_FORMAT_EXTENSIBLE {
// cbSize(2) validBits(2) channelMask(4) subFormat GUID(16)
if let (Some(valid), Some(sub)) = (le_u16(c, 18), le_u16(c, 24)) {
if valid != 0 {
fmt.valid_bits = valid;
}
fmt.tag = sub;
} else {
let (Some(cb_size), Some(valid), Some(sub)) = (le_u16(c, 16), le_u16(c, 18), le_u16(c, 24)) else {
return Err(AudioError::Malformed("WAVE_FORMAT_EXTENSIBLE fmt chunk too short".into()));
};
let Some(tail) = c.get(26..40) else {
return Err(AudioError::Malformed("WAVE_FORMAT_EXTENSIBLE fmt chunk too short".into()));
};
if cb_size < 22 {
return Err(AudioError::Malformed("WAVE_FORMAT_EXTENSIBLE cbSize is below 22".into()));
}
if valid > fmt.bits {
return Err(AudioError::Malformed("WAVE_FORMAT_EXTENSIBLE valid bits exceed the container size".into()));
}
if valid != 0 {
fmt.valid_bits = valid;
}
// A subtype outside the KSDATAFORMAT_SUBTYPE_* family is not PCM/float, whatever its first two bytes say:
// keep the extensible tag so the file goes to the foreign decoder.
if tail == GUID_TAIL || tail == AMBISONIC_TAIL {
fmt.tag = sub;
}
}
Ok(fmt)
@@ -355,17 +369,17 @@ pub(crate) struct Writer<W> {
failed: bool,
}
/// The optional header parts of a WAV file: BWF metadata, speaker mask, and whether to force RF64.
#[derive(Clone, Copy)]
pub(crate) struct HeaderExtras<'a> {
pub(crate) bwf: Option<&'a BwfInfo>,
pub(crate) mask: Option<u32>,
pub(crate) force_rf64: bool,
}
impl<W: Write + Seek> Writer<W> {
pub(crate) fn new(
mut out: W,
channels: usize,
sample_rate: u32,
depth: BitDepth,
dither: bool,
bwf: Option<&BwfInfo>,
mask: Option<u32>,
force_rf64: bool,
) -> Result<Self> {
pub(crate) fn new(mut out: W, channels: usize, sample_rate: u32, depth: BitDepth, dither: bool, extras: HeaderExtras<'_>) -> Result<Self> {
let HeaderExtras { bwf, mask, force_rf64 } = extras;
validate_layout(channels, sample_rate)?;
let format = sample_format(depth);
let start = out.stream_position()?;
@@ -449,7 +463,7 @@ pub(crate) fn encode(
let mut out = Vec::new();
out.try_reserve_exact(head.len().saturating_add(data_len).saturating_add(1))
.map_err(|_| AudioError::TooLarge(format!("cannot allocate {data_len} bytes for WAV output")))?;
let mut w = Writer::new(Cursor::new(out), channels, buf.sample_rate, depth, dither, bwf, mask, force_rf64)?;
let mut w = Writer::new(Cursor::new(out), channels, buf.sample_rate, depth, dither, HeaderExtras { bwf, mask, force_rf64 })?;
w.write(&buf.channels, frames)?;
Ok(w.finish()?.into_inner())
}
+101
View File
@@ -0,0 +1,101 @@
use soundcraft_audio_io::{AudioBuffer, BitDepth, EncodeOptions, FileEncoder, FileFormat, SampleFormat, decode, encode};
use std::io::Cursor;
fn encoded(audio: &AudioBuffer, options: &EncodeOptions, streaming: bool) -> Vec<u8> {
if !streaming {
return encode(audio, options).unwrap();
}
let mut writer = FileEncoder::new(Cursor::new(Vec::new()), options, audio.num_channels(), audio.sample_rate, None).unwrap();
// Cross the writer's 4096-frame scratch boundary, then write a short final block.
for start in (0..audio.frames()).step_by(4097) {
let end = (start + 4097).min(audio.frames());
let block: Vec<Vec<f32>> = audio.channels.iter().map(|channel| channel[start..end].to_vec()).collect();
writer.write(&block, end - start).unwrap();
}
writer.finish().unwrap().into_inner()
}
fn assert_float_headroom(streaming: bool) {
let samples = [
-f32::MAX,
-2.0,
-1.25,
-1.0,
-0.5,
-f32::MIN_POSITIVE,
-f32::from_bits(1),
-0.0,
0.0,
f32::from_bits(1),
f32::MIN_POSITIVE,
0.5,
1.0,
1.25,
2.0,
f32::MAX,
];
for format in [FileFormat::Wav, FileFormat::Aiff] {
for dither in [false, true] {
for channels in [1, 2] {
let audio = AudioBuffer {
sample_rate: 48_000,
channels: (0..channels).map(|c| samples.iter().copied().cycle().skip(c).take(4103).collect()).collect(),
};
let options = EncodeOptions { format, bit_depth: BitDepth::Float32, dither, bwf: None };
let bytes = encoded(&audio, &options, streaming);
let pcm: Vec<u8> =
audio.interleaved().iter().flat_map(|v| if format == FileFormat::Wav { v.to_le_bytes() } else { v.to_be_bytes() }).collect();
assert!(bytes.ends_with(&pcm), "{format:?}, dither={dither}: stored sample bytes changed");
let (info, decoded) = decode(&bytes, None).unwrap();
assert_eq!(info.sample_format, SampleFormat::Float32);
assert_eq!((decoded.sample_rate, decoded.num_channels(), decoded.frames()), (48_000, channels, 4103));
for (channel, (actual, expected)) in decoded.channels.iter().zip(&audio.channels).enumerate() {
for (frame, (actual, expected)) in actual.iter().zip(expected).enumerate() {
assert_eq!(actual.to_bits(), expected.to_bits(), "{format:?}, dither={dither}, channel={channel}, frame={frame}");
}
}
}
}
}
}
#[test]
fn whole_file_float32_preserves_finite_headroom() {
assert_float_headroom(false);
}
#[test]
fn streaming_float32_preserves_finite_headroom() {
assert_float_headroom(true);
}
#[test]
fn float32_keeps_non_finite_sample_handling() {
let audio = AudioBuffer { sample_rate: 48_000, channels: vec![vec![f32::NAN, f32::NEG_INFINITY, f32::INFINITY]] };
let expected = [0.0_f32, -1.0, 1.0];
for format in [FileFormat::Wav, FileFormat::Aiff] {
for streaming in [false, true] {
let options = EncodeOptions { format, bit_depth: BitDepth::Float32, dither: false, bwf: None };
let bytes = encoded(&audio, &options, streaming);
// Inspect stored samples too: the decoder itself replaces non-finite values.
let pcm: Vec<u8> = expected.iter().flat_map(|v| if format == FileFormat::Wav { v.to_le_bytes() } else { v.to_be_bytes() }).collect();
assert!(bytes.ends_with(&pcm), "{format:?}, streaming={streaming}");
assert_eq!(decode(&bytes, None).unwrap().1.channels[0], expected);
}
}
}
#[test]
fn integer_pcm_still_clips_at_full_scale() {
let audio = AudioBuffer { sample_rate: 48_000, channels: vec![vec![-2.0, -1.25, -1.0, -0.5, 0.0, 0.5, 1.0, 1.25, 2.0]] };
for (depth, max) in [(BitDepth::Int16, 32767.0 / 32768.0), (BitDepth::Int24, 8_388_607.0 / 8_388_608.0), (BitDepth::Int32, 1.0)] {
for format in [FileFormat::Wav, FileFormat::Aiff, FileFormat::Flac] {
for streaming in [false, true] {
let options = EncodeOptions { format, bit_depth: depth, dither: false, bwf: None };
let bytes = encoded(&audio, &options, streaming);
let (_, decoded) = decode(&bytes, None).unwrap();
assert_eq!(decoded.channels[0], [-1.0, -1.0, -1.0, -0.5, 0.0, 0.5, max, max, max], "{format:?} {depth:?}, streaming={streaming}");
}
}
}
}
+1
View File
@@ -1,6 +1,7 @@
//! Integration tests, built as one test binary: each file is a module.
mod encode_format;
mod float_headroom;
mod malformed;
mod roundtrip;
mod stream;
+233
View File
@@ -13,6 +13,17 @@ fn small(format: FileFormat, depth: BitDepth, channels: usize) -> Vec<u8> {
encode(&b, &EncodeOptions { format, bit_depth: depth, dither: false, bwf: None }).unwrap()
}
/// An ID3v2.4 header whose syncsafe size field declares `size` bytes of tag body.
fn id3_header(size: usize) -> [u8; 10] {
let size = size as u32;
[b'I', b'D', b'3', 4, 0, 0, ((size >> 21) & 0x7F) as u8, ((size >> 14) & 0x7F) as u8, ((size >> 7) & 0x7F) as u8, (size & 0x7F) as u8]
}
/// Wrap `body` in an ID3v2.4 tag.
fn id3_tag(body: &[u8]) -> Vec<u8> {
[&id3_header(body.len())[..], body].concat()
}
/// Little helper for building RIFF files by hand.
fn riff(chunks: &[(&[u8; 4], Vec<u8>)]) -> Vec<u8> {
let mut body = b"WAVE".to_vec();
@@ -314,6 +325,190 @@ fn an_id3_tag_before_the_audio_is_skipped_unread() {
assert_eq!(decode(&[tag, flac].concat(), None).unwrap().1.frames(), 300);
}
/// Container magic inside an MP3 frame payload must not change detection or make probing reject the stream.
#[test]
fn a_valid_mp3_with_incidental_container_magic_still_probes() {
let mut frame = vec![0u8; 417];
frame[..4].copy_from_slice(&[0xFF, 0xFB, 0x90, 0x00]); // MPEG-1 Layer III, 128 kbps, 44.1 kHz, stereo
let mut f: Vec<u8> = (0..8).flat_map(|_| frame.clone()).collect();
let at = 417 * 2 + 10; // Inside frame 2's payload, leaving every frame header intact.
f[at..at + 8].copy_from_slice(b"caff\0\x01\0\0");
f[at + 8..at + 12].copy_from_slice(b"kuki");
f[at + 12..at + 20].copy_from_slice(&(1u64 << 50).to_be_bytes());
assert_eq!(detect_format(&f, None), FileFormat::Mp3);
let info = probe(&f, None).expect("valid MP3 with incidental container magic");
assert_eq!((info.format, info.sample_rate, info.channels), (FileFormat::Mp3, 44_100, 2));
}
/// Symphonia 0.6's CAF/FLAC/MP4 `Scoreable` implementations are unconditional
/// (`Supported(255)`), so a container magic before the first audio frame *is* the reader the probe
/// selects; there is no API to ask the probe to skip it. The preflight must therefore keep
/// refusing an oversized CAF cookie instead of ignoring the marker and playing the MPEG frames
/// behind it (which would also let a real malformed container bypass the size guard).
#[test]
fn a_container_marker_before_the_first_audio_frame_is_the_probed_candidate() {
let mut frame = vec![0u8; 417];
frame[..4].copy_from_slice(&[0xFF, 0xFB, 0x90, 0x00]); // MPEG-1 Layer III, 128 kbps, 44.1 kHz, stereo
let mp3: Vec<u8> = (0..8).flat_map(|_| frame.clone()).collect();
let malformed = caf(&[caf_desc(b"aac ", 0, 1024, 2, 0), (b"kuki", 1i64 << 50, vec![0; 8])]);
for f in [[vec![0u8], malformed.clone(), mp3.clone()].concat(), [vec![0u8; 32], malformed.clone(), mp3.clone()].concat()] {
// The hint still names the raw stream, but the probe sees the CAF marker first.
assert_eq!(detect_format(&f, Some("mp3")), FileFormat::Mp3);
assert!(matches!(probe(&f, Some("mp3")), Err(AudioError::Malformed(m)) if m.contains("larger than the file")));
assert!(matches!(decode(&f, Some("mp3")), Err(AudioError::Malformed(m)) if m.contains("larger than the file")));
}
// Plain leading junk without format magic is tolerated, as before.
let f = [vec![0u8; 32], mp3].concat();
assert_eq!(detect_format(&f, Some("mp3")), FileFormat::Mp3);
let info = probe(&f, Some("mp3")).expect("junk before a valid MP3");
assert_eq!((info.format, info.sample_rate, info.channels), (FileFormat::Mp3, 44_100, 2));
}
/// More false `0xFFEx` pairs than the preflight's scoring budget must not make a later container
/// magic inside the valid audio payload authoritative. Past the cap the scan is unchecked and the
/// probe selects the MPEG reader at the first real frame.
#[test]
fn an_exhausted_audio_score_budget_does_not_reject_the_mp3_behind_it() {
let mut frame = vec![0u8; 417];
frame[..4].copy_from_slice(&[0xFF, 0xFB, 0x90, 0x00]);
let mut audio: Vec<u8> = (0..8).flat_map(|_| frame.clone()).collect();
let at = 417 * 2 + 10; // Inside frame 2's payload, leaving every frame header intact.
audio[at..at + 8].copy_from_slice(b"caff\0\x01\0\0");
audio[at + 8..at + 12].copy_from_slice(b"kuki");
audio[at + 12..at + 20].copy_from_slice(&(1u64 << 50).to_be_bytes());
let false_syncs: Vec<u8> = (0..256).flat_map(|_| [0xFFu8, 0xE2]).collect();
let f = [false_syncs, audio].concat();
assert_eq!(detect_format(&f, Some("mp3")), FileFormat::Mp3);
let info = probe(&f, Some("mp3")).expect("valid MP3 after an exhausted audio score budget");
assert_eq!((info.format, info.sample_rate, info.channels), (FileFormat::Mp3, 44_100, 2));
let (decoded, buf) = decode(&f, Some("mp3")).expect("decode the same MP3");
assert_eq!((decoded.format, decoded.sample_rate, decoded.channels), (FileFormat::Mp3, 44_100, 2));
assert_eq!(decoded.frames, buf.frames() as u64);
}
/// Past the score budget the preflight does not run, so if the probe then selects a container
/// whose reader allocates from sizes in the file, the post-probe refusal still applies.
#[test]
fn a_container_selected_without_its_size_check_is_refused() {
let flac = small(FileFormat::Flac, BitDepth::Int16, 1);
// Exactly the cap of false candidates, then one more that the reader rejects (invalid
// bit-rate), so the preflight stops unchecked before reaching the FLAC marker behind it.
let mut false_syncs: Vec<u8> = (0..256).flat_map(|_| [0xFFu8, 0xE2]).collect();
false_syncs.extend_from_slice(&[0xFF, 0xFB, 0xF0, 0x00]);
let f = [false_syncs, flac].concat();
assert_eq!(detect_format(&f, None), FileFormat::Mp3);
assert!(matches!(probe(&f, None), Err(AudioError::Malformed(m)) if m.contains("was not size-checked")));
assert!(matches!(decode(&f, None), Err(AudioError::Malformed(m)) if m.contains("was not size-checked")));
}
/// A zero-frame FLAC after arbitrary bytes or ID3 tags is still detected and reported as an empty stream, not a
/// decode failure.
#[test]
fn zero_frame_flac_after_a_prefix_is_still_empty() {
let flac =
encode(&AudioBuffer::new(48_000, 1, 0), &EncodeOptions { format: FileFormat::Flac, bit_depth: BitDepth::Int16, dither: false, bwf: None })
.unwrap();
let tag = id3_tag(b"tag");
for (f, detected) in [
([vec![0x7A; 64], flac.clone()].concat(), FileFormat::Other),
([tag.clone(), vec![0x7A; 16], flac.clone()].concat(), FileFormat::Mp3),
([tag.clone(), tag, flac.clone()].concat(), FileFormat::Mp3),
] {
assert_eq!(detect_format(&f, None), detected);
let info = probe(&f, None).expect("empty FLAC after a prefix");
assert_eq!((info.format, info.sample_rate, info.channels, info.frames), (FileFormat::Flac, 48_000, 1, 0));
assert_eq!(decode(&f, None).unwrap().1.frames(), 0);
}
}
/// Build one MPEG-4 AAC-LC ADTS frame at 48 kHz stereo around `payload`, with the 13-bit length covering the 7-byte
/// header.
fn adts_frame(payload: &[u8]) -> Vec<u8> {
// MPEG-4 AAC-LC, 48 kHz, stereo; the 13-bit frame length includes this 7-byte header.
let len = 7 + payload.len();
let mut h = [0xFF, 0xF1, 0x4C, 0x00, 0x00, 0x1F, 0xFC];
h[3] = 0x80 | ((len >> 11) & 0x03) as u8; // stereo channel config low bits
h[4] = ((len >> 3) & 0xFF) as u8;
h[5] = (((len & 0x07) as u8) << 5) | 0x1F;
[&h[..], payload].concat()
}
/// False MPEG sync prefixes must not stop a following zero-frame FLAC from probing as empty.
#[test]
fn zero_frame_flac_after_a_false_mpeg_sync_is_still_empty() {
let flac =
encode(&AudioBuffer::new(48_000, 1, 0), &EncodeOptions { format: FileFormat::Flac, bit_depth: BitDepth::Int16, dither: false, bwf: None })
.unwrap();
for junk in [vec![0xFF, 0xE0, 0x00], vec![0xFF, 0xF0, 0x00], vec![0xFF, 0xFB, 0xF0, 0x00]] {
let f = [junk, flac.clone()].concat();
let info = probe(&f, None).expect("empty FLAC after a false sync");
assert_eq!((info.format, info.sample_rate, info.channels, info.frames), (FileFormat::Flac, 48_000, 1, 0));
assert_eq!(decode(&f, None).unwrap().1.frames(), 0);
}
}
/// A false MPEG sync prefix must not let malformed CAF or MP4 size fields bypass the container preflight.
#[test]
fn a_false_sync_does_not_bypass_container_checks() {
let bad_caf = caf(&[
caf_desc(b"aac ", 0, 1024, 2, 0),
(b"pakt", 32, packet_table(1, 1024, &[0x82, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0])),
(b"data", -1, vec![0; 64]),
]);
assert!(matches!(probe(&bad_caf, None), Err(AudioError::Malformed(m)) if m.contains("larger than the file")));
let f = [vec![0xFF, 0xE0, 0x00], bad_caf].concat();
assert!(matches!(probe(&f, None), Err(AudioError::Malformed(m)) if m.contains("larger than the file")));
assert!(matches!(decode(&f, None), Err(AudioError::Malformed(m)) if m.contains("larger than the file")));
let pcm = sample_entry(b"sowt", 0, &[], &[]);
let bad_mp4 = mp4(480, &pcm, |kind, mut a| {
if kind == b"stsz" {
a[12..16].copy_from_slice(&u32::MAX.to_be_bytes());
}
a
});
assert!(probe(&bad_mp4, None).is_err());
let f = [vec![0xFF, 0xE0, 0x00], bad_mp4].concat();
assert!(matches!(probe(&f, None), Err(AudioError::Malformed(m)) if m.contains("larger than the file")));
assert!(matches!(decode(&f, None), Err(AudioError::Malformed(m)) if m.contains("larger than the file")));
}
/// Container magic inside a valid ADTS payload must not override AAC detection or make probing fail.
#[test]
fn a_valid_adts_stream_with_incidental_container_magic_still_probes() {
let payload = [0u8; 400];
let mut frames: Vec<Vec<u8>> = (0..8).map(|_| adts_frame(&payload)).collect();
let at = 100; // Inside frame 2's payload, leaving every frame header intact.
frames[2][7 + at..7 + at + 12].copy_from_slice(b"caff\0\x01\0\0kuki");
let f: Vec<u8> = frames.concat();
assert_eq!(detect_format(&f, None), FileFormat::Aac);
let info = probe(&f, None).expect("valid ADTS with incidental container magic");
assert_eq!((info.format, info.sample_rate, info.channels), (FileFormat::Aac, 48_000, 2));
}
/// A later valid CAF must not excuse an earlier malformed container that the preflight or probe rejects.
#[test]
fn an_earlier_malformed_container_is_not_excused_by_a_later_valid_one() {
let malformed = caf(&[caf_desc(b"aac ", 0, 1024, 2, 0), (b"kuki", 1i64 << 50, vec![0; 8])]);
let pcm: Vec<u8> = (0..32i16).flat_map(|i| (i * 50).to_be_bytes()).collect();
let data = [vec![0; 4], pcm].concat();
let valid = caf(&[caf_desc(b"lpcm", 2, 1, 1, 16), (b"data", data.len() as i64, data)]);
let f = [vec![0u8; 32], malformed, valid].concat();
assert!(matches!(probe(&f, None), Err(AudioError::Malformed(m)) if m.contains("larger than the file")));
assert!(matches!(decode(&f, None), Err(AudioError::Malformed(m)) if m.contains("larger than the file")));
}
/// Truncated ID3 bodies, declared tag sizes beyond the file, and a complete tag with no audio after it are malformed
/// for both probe and decode.
#[test]
fn truncated_or_oversized_id3_tags_are_malformed() {
let cases = vec![[&id3_header(100)[..], b"xx"].concat(), id3_header(0x0FFF_FFFF).to_vec(), id3_tag(b"tag")];
for f in cases {
assert!(matches!(probe(&f, None), Err(AudioError::Malformed(_))));
assert!(matches!(decode(&f, None), Err(AudioError::Malformed(_))));
}
}
#[test]
fn wav_info_entries_beyond_the_file_are_refused() {
let alaw = |len: u32| {
@@ -511,3 +706,41 @@ proptest! {
prop_assert_eq!(flac, wav);
}
}
/// An extensible `fmt ` chunk: cbSize, valid bits, mask, then the 16-byte subtype GUID.
fn extensible_fmt(cb_size: u16, valid_bits: u16, guid: &[u8]) -> Vec<u8> {
let mut f = fmt(0xFFFE, 1, 44_100, 16);
f.extend_from_slice(&cb_size.to_le_bytes());
f.extend_from_slice(&valid_bits.to_le_bytes());
f.extend_from_slice(&4u32.to_le_bytes());
f.extend_from_slice(guid);
f
}
const PCM_GUID: [u8; 16] = [1, 0, 0, 0, 0, 0, 0x10, 0, 0x80, 0, 0, 0xAA, 0, 0x38, 0x9B, 0x71];
#[test]
fn extensible_fmt_is_checked_in_full() {
let data = vec![0u8; 8];
let good = riff(&[(b"fmt ", extensible_fmt(22, 16, &PCM_GUID)), (b"data", data.clone())]);
assert_eq!(probe(&good, None).unwrap().sample_format, SampleFormat::Int16);
// Subtype GUID cut short after the format code.
let cut = riff(&[(b"fmt ", extensible_fmt(22, 16, &PCM_GUID[..4])), (b"data", data.clone())]);
assert!(matches!(probe(&cut, None), Err(AudioError::Malformed(_))));
assert!(matches!(decode(&cut, None), Err(AudioError::Malformed(_))));
// cbSize too small for the extension.
let small_cb = riff(&[(b"fmt ", extensible_fmt(0, 16, &PCM_GUID)), (b"data", data.clone())]);
assert!(matches!(probe(&small_cb, None), Err(AudioError::Malformed(_))));
// More valid bits than the 16-bit container holds.
let wide = riff(&[(b"fmt ", extensible_fmt(22, 24, &PCM_GUID)), (b"data", data.clone())]);
assert!(matches!(probe(&wide, None), Err(AudioError::Malformed(_))));
// PCM-like prefix but a foreign GUID tail is not read as native PCM.
let mut foreign = PCM_GUID;
foreign[15] ^= 0xFF;
let foreign = riff(&[(b"fmt ", extensible_fmt(22, 16, &foreign)), (b"data", data)]);
assert!(!matches!(probe(&foreign, None), Ok(i) if i.sample_format == SampleFormat::Int16));
// The Ambisonic PCM subtype is PCM.
let ambi = [1, 0, 0, 0, 0x21, 0x07, 0xD3, 0x11, 0x86, 0x44, 0xC8, 0xC1, 0xCA, 0, 0, 0];
let ambi = riff(&[(b"fmt ", extensible_fmt(22, 16, &ambi)), (b"data", vec![0u8; 8])]);
assert_eq!(probe(&ambi, None).unwrap().sample_format, SampleFormat::Int16);
}
+2 -2
View File
@@ -205,7 +205,7 @@ fn encode_rejects_bad_buffers_and_formats() {
for f in [FileFormat::Mp3, FileFormat::Ogg, FileFormat::Aac, FileFormat::Alac, FileFormat::Caf, FileFormat::Other] {
assert!(encode(&sine(48_000, 1, 10), &EncodeOptions { format: f, ..EncodeOptions::default() }).is_err());
}
// Non-finite and out-of-range samples are clamped, not errors.
// Non-finite samples are sanitized; only integer output clips finite samples.
let mut b = AudioBuffer::new(48_000, 1, 4);
b.channels[0] = vec![f32::NAN, f32::INFINITY, -5.0, 0.5];
for format in [FileFormat::Wav, FileFormat::Aiff, FileFormat::Flac] {
@@ -217,7 +217,7 @@ fn encode_rejects_bad_buffers_and_formats() {
let (_, out) = decode(&bytes, None).unwrap();
assert_eq!(out.channels[0][0], 0.0);
assert!(out.channels[0][1] > 0.99 && out.channels[0][1] <= 1.0);
assert_eq!(out.channels[0][2], -1.0);
assert_eq!(out.channels[0][2], if depth == BitDepth::Float32 { -5.0 } else { -1.0 });
}
}
// Ragged buffers encode the common length.
+1 -1
View File
@@ -9,7 +9,7 @@ use std::sync::OnceLock;
pub fn fixture_dir() -> &'static Path {
static DIR: OnceLock<PathBuf> = OnceLock::new();
DIR.get_or_init(|| {
let manifest = Path::new(env!("CARGO_MANIFEST_DIR")).join("tests/fixture/Cargo.toml");
let manifest = Path::new(env!("CARGO_MANIFEST_DIR")).join("../clap-host/tests/fixture/Cargo.toml");
let target = Path::new(env!("CARGO_TARGET_TMPDIR")).join("clap-fixture-target");
let cargo = std::env::var("CARGO").unwrap_or_else(|_| "cargo".into());
let st = Command::new(cargo)
+1 -1
View File
@@ -1,4 +1,4 @@
# A tiny CLAP plugin used only by soundcraft-clap-host's integration tests. Not part of the
# A tiny CLAP plugin used only by SoundCraft's integration tests. Not part of the
# SoundCraft workspace (the empty [workspace] table makes it its own root); the test builds it
# with `cargo build` and loads the resulting library. Never shipped.
[package]
+182 -1
View File
@@ -226,16 +226,95 @@ fn resample_ratio(ch: &[Vec<f32>], ratio: f64, out_len: usize) -> Vec<Vec<f32>>
ch.iter().map(|c| resample_channel(c, ratio, out_len, &table)).collect()
}
/// Most kernel weights a [`Polyphase`] bank may hold (16 MiB of f32).
const MAX_POLYPHASE_WEIGHTS: usize = 1 << 22;
/// The windowed-sinc kernel of [`resample_channel`] precomputed for every phase of a rational
/// rate change `from` → `to` (reduced to `step` / `phases`). Output sample `n` reads input time
/// `n * step / phases`: whole part `i`, phase `p`. Its taps are input samples `i + first ..`, one
/// weight each, so every output is one dot product over contiguous memory instead of a table
/// lookup and interpolation per tap. The weights are the ones `resample_channel` computes, taps
/// beyond the kernel's half width weigh 0, and taps are summed in the same order.
struct Polyphase {
phases: u64,
step: u64,
/// Offset of the first tap from `i`.
first: i64,
taps: usize,
/// `phases * taps` weights, phase-major.
weights: Vec<f32>,
}
impl Polyphase {
/// `None` when the bank would be too large (rates with no small common ratio); the caller
/// then falls back to [`resample_channel`].
fn new(from: u32, to: u32, table: &SincTable) -> Option<Polyphase> {
let g = gcd(from, to);
let (step, phases) = (u64::from(from / g), u64::from(to / g));
let hw = table.half_width;
let first = (-hw).floor() as i64;
let last = hw.ceil() as i64 + 1;
let taps = usize::try_from(last - first + 1).ok()?;
if usize::try_from(phases).ok()?.checked_mul(taps)? > MAX_POLYPHASE_WEIGHTS {
return None;
}
let mut weights = Vec::with_capacity(taps * phases as usize);
for p in 0..phases {
let frac = p as f64 / phases as f64;
for k in first..first + taps as i64 {
let d = frac - k as f64;
weights.push(if d.abs() <= hw { table.at(d) } else { 0.0 });
}
}
Some(Polyphase { phases, step, first, taps, weights })
}
fn run(&self, x: &[f32], out_len: usize) -> Vec<f32> {
let len = x.len() as i64;
let mut out = Vec::with_capacity(out_len);
for n in 0..out_len as u64 {
let Some(pos) = n.checked_mul(self.step) else { break };
let (i, p) = ((pos / self.phases) as i64, (pos % self.phases) as usize);
let w = self.weights.get(p * self.taps..(p + 1) * self.taps).unwrap_or_default();
let start = i + self.first;
let acc = match usize::try_from(start).ok().and_then(|s| x.get(s..s + self.taps)) {
// Inside the signal: a plain dot product.
Some(xs) => xs.iter().zip(w).fold(0.0f32, |a, (v, k)| a + v * k),
// At the edges, taps outside the signal are skipped, as `resample_channel` does.
None => (0..self.taps as i64)
.filter(|k| (0..len).contains(&(start + k)))
.fold(0.0f32, |a, k| a + x.get((start + k) as usize).copied().unwrap_or(0.0) * w.get(k as usize).copied().unwrap_or(0.0)),
};
out.push(acc);
}
out.resize(out_len, 0.0);
out
}
}
fn gcd(mut a: u32, mut b: u32) -> u32 {
while b != 0 {
(a, b) = (b, a % b);
}
a.max(1)
}
/// High-quality windowed-sinc sample-rate conversion from `from` Hz to `to` Hz.
pub fn resample(ch: &[Vec<f32>], from: u32, to: u32) -> Vec<Vec<f32>> {
if from == 0 || to == 0 || from == to {
return ch.to_vec();
}
let ratio = f64::from(to) / f64::from(from);
let cutoff = if ratio < 1.0 { ratio * 0.94 } else { 0.97 };
let table = SincTable::new(cutoff);
let bank = Polyphase::new(from, to, &table);
ch.iter()
.map(|c| {
let out_len = ((c.len() as f64 * ratio).round() as usize).min(MAX_OFFLINE_SAMPLES);
resample_ratio(std::slice::from_ref(c), ratio, out_len).into_iter().next().unwrap_or_default()
match &bank {
Some(b) => b.run(c, out_len),
None => resample_channel(c, ratio, out_len, &table),
}
})
.collect()
}
@@ -373,6 +452,78 @@ pub fn pitch_shift(ch: &[Vec<f32>], semitones: f32, sample_rate: f32) -> Vec<Vec
out
}
/// Which way [`speed_ramp`] ramps the playback speed.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SpeedRampChange {
/// From normal speed down to a complete stop.
SlowDown,
/// From a complete stop up to normal speed.
SpeedUp,
}
/// How much of the input [`speed_ramp`] plays, and how long the result is.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SpeedRampSelection {
/// The result is as long as the input. Two thirds of the input are played: the first two
/// thirds for Slow Down, the last two thirds for Speed Up, so each joins its neighbour
/// without a jump.
FitTo,
/// All of the input is played, and the result is 150 % of its length.
Extend,
}
/// Plays `ch` with the speed ramping between normal speed and a complete stop, so the pitch follows
/// the speed. The speed is `1 - u²` for Slow Down and `1 - (1 - u)²` for Speed Up, where `u`
/// runs from 0 to 1 over the result; both curves average 2/3 of normal speed. With `fades`, the
/// gain follows the same curve: a fade-out for Slow Down and a fade-in for Speed Up. Every channel
/// is read at the same positions, and a channel shorter than the others is silent past its end.
pub fn speed_ramp(ch: &[Vec<f32>], change: SpeedRampChange, selection: SpeedRampSelection, fades: bool) -> Vec<Vec<f32>> {
let len = max_len(ch) as f64;
let (total, first, played) = match selection {
SpeedRampSelection::Extend => ((len * 1.5).round(), 0.0, len),
SpeedRampSelection::FitTo => {
let played = len * 2.0 / 3.0;
(len, if change == SpeedRampChange::SlowDown { 0.0 } else { len - played }, played)
}
};
let out_len = (total as usize).min(MAX_OFFLINE_SAMPLES);
let table = SincTable::new(0.97);
let hw = table.half_width;
// The fraction of the played input reached at `u`, and the speed there (0 to 1).
let at = |u: f64| -> (f64, f64) {
match change {
SpeedRampChange::SlowDown => ((3.0 * u - u * u * u) / 2.0, 1.0 - u * u),
SpeedRampChange::SpeedUp => {
let r = 1.0 - u;
(1.0 - (3.0 * r - r * r * r) / 2.0, 1.0 - r * r)
}
}
};
// The interpolation reads the first or last sample of the input for taps before or after it.
let last = (len as i64 - 1).max(0);
ch.iter()
.map(|x| {
(0..out_len)
.map(|n| {
let (pos, speed) = at(n as f64 / total);
let t = first + played * pos;
let lo = (t - hw).ceil() as i64;
let hi = (t + hw).floor() as i64;
let mut acc = 0.0f32;
let mut j = lo;
while j <= hi {
if let Some(&v) = x.get(j.clamp(0, last) as usize) {
acc += v * table.at(t - j as f64);
}
j += 1;
}
if fades { acc * speed as f32 } else { acc }
})
.collect()
})
.collect()
}
// ---------------------------------------------------------------------------------------------
// Generators and analysis
@@ -662,6 +813,36 @@ pub fn peak_db(ch: &[Vec<f32>]) -> f32 {
mod tests {
use super::*;
/// The polyphase bank gives what the per-tap kernel gives (it only moves where the weights
/// are computed), at common rate pairs, both directions, and at the signal's edges.
#[test]
fn polyphase_resample_matches_the_per_tap_kernel() {
let x: Vec<f32> = (0..3_000).map(|i| (i as f32 * 0.071).sin() * 0.6 + (i as f32 * 0.53).sin() * 0.3).collect();
let pairs = [(44_100, 48_000), (48_000, 44_100), (48_000, 96_000), (96_000, 48_000), (22_050, 48_000), (8_000, 44_100), (48_000, 22_050)];
for (from, to) in pairs {
let ratio = f64::from(to) / f64::from(from);
let out_len = ((x.len() as f64 * ratio).round() as usize).min(MAX_OFFLINE_SAMPLES);
let cutoff = if ratio < 1.0 { ratio * 0.94 } else { 0.97 };
let table = SincTable::new(cutoff);
let reference = resample_channel(&x, ratio, out_len, &table);
let bank = Polyphase::new(from, to, &table).expect("small common ratio");
let fast = bank.run(&x, out_len);
assert_eq!(fast.len(), reference.len(), "{from}->{to}");
let worst = fast.iter().zip(&reference).map(|(a, b)| (a - b).abs()).fold(0.0f32, f32::max);
assert!(worst < 1e-5, "{from}->{to}: worst difference {worst}");
assert_eq!(resample(std::slice::from_ref(&x), from, to)[0], fast, "resample uses the bank");
}
// Signals shorter than the kernel are all edge.
let short = [0.5f32, -0.25, 0.75];
let table = SincTable::new(0.97);
let reference = resample_channel(&short, 48_000.0 / 44_100.0, 3, &table);
let fast = Polyphase::new(44_100, 48_000, &table).expect("bank").run(&short, 3);
assert!(fast.iter().zip(&reference).all(|(a, b)| (a - b).abs() < 1e-6), "{fast:?} {reference:?}");
// Rates with no small common ratio fall back to the per-tap kernel.
assert!(Polyphase::new(44_100, 96_001, &table).is_none());
assert_eq!(resample(&[short.to_vec()], 44_100, 96_001)[0].len(), 7);
}
#[test]
fn fade_shapes_endpoints() {
for s in FadeShape::ALL {
+360
View File
@@ -0,0 +1,360 @@
//! Auto Pan: moves a signal between the left and right channels with an LFO or an envelope.
use std::f32::consts::{FRAC_PI_4, PI, SQRT_2, TAU};
use crate::params::{Params, choice, lin, log, param_plumbing, toggle};
use crate::util::{Smoother, frames_in, ms_to_samples, sane_channels, sane_sr, time_coef};
use crate::{Category, ParamInfo, Plugin, PluginInfo, Transport, Unit, db_to_gain, gain_to_db};
const SOURCE_CHOICES: &[&str] = &["LFO", "Envelope"];
const PATH_CHOICES: &[&str] = &["Left to Right", "Right to Left"];
const WAVEFORM_CHOICES: &[&str] = &["Sine", "Triangle", "Square", "Saw Up", "Saw Down", "Half Sine", "4 Step Triangle"];
const TRIGGER_CHOICES: &[&str] = &["Free", "Single", "Beat Clock", "Envelope", "Manual"];
const DURATION_CHOICES: &[&str] = &["16th Note", "8th Note", "1 Beat", "2 Beats", "1 Bar", "2 Bars", "4 Bars"];
const SOURCE_ENVELOPE: usize = 1;
const PATH_RIGHT_TO_LEFT: usize = 1;
const TRIGGER_FREE: usize = 0;
const TRIGGER_SINGLE: usize = 1;
const TRIGGER_BEAT_CLOCK: usize = 2;
const TRIGGER_ENVELOPE: usize = 3;
const TRIGGER_MANUAL: usize = 4;
static AUTOPAN_PARAMS: [ParamInfo; 14] = [
lin("output", "Output", -24.0, 12.0, 0.0, Unit::Db),
lin("width", "Width", 0.0, 100.0, 100.0, Unit::Percent),
lin("manual", "Manual", -100.0, 100.0, 0.0, Unit::Percent),
choice("source", "Source", SOURCE_CHOICES, 0),
choice("path", "Path", PATH_CHOICES, 0),
log("rate", "Rate (BPM)", 1.0, 960.0, 120.0, Unit::None),
choice("waveform", "Waveform", WAVEFORM_CHOICES, 0),
choice("trigger", "Trigger", TRIGGER_CHOICES, 0),
toggle("retrigger", "Manual Trigger", false),
toggle("link_tempo", "Link to Tempo", false),
choice("duration", "Duration", DURATION_CHOICES, 4),
lin("threshold", "Threshold", -60.0, -1.0, -30.0, Unit::Db),
log("attack", "Attack", 0.1, 100.0, 5.0, Unit::Ms),
log("release", "Release", 5.0, 2000.0, 150.0, Unit::Ms),
];
pub static AUTOPAN_INFO: PluginInfo = PluginInfo {
id: "auto_pan",
name: "Auto Pan",
short_name: "Auto Pan",
category: Category::Modulation,
params: &AUTOPAN_PARAMS,
is_instrument: false,
audiosuite: true,
};
/// The LFO shape at `t` in 0..=1 of a cycle, from -1 (the start of the path) to 1 (its end).
fn wave_at(waveform: usize, t: f32) -> f32 {
let t = t.clamp(0.0, 1.0);
let tri = if t < 0.5 { 4.0 * t - 1.0 } else { 3.0 - 4.0 * t };
match waveform {
1 => tri,
2 => {
if t < 0.5 {
-1.0
} else {
1.0
}
}
3 => 2.0 * t - 1.0,
4 => 1.0 - 2.0 * t,
5 => 2.0 * (PI * t).sin() - 1.0,
6 => {
let step = ((tri + 1.0) * 2.0).floor().clamp(0.0, 3.0);
step * 2.0 / 3.0 - 1.0
}
_ => -(TAU * t).cos(),
}
}
/// Length of one Duration choice in quarter notes, for a bar of `bar` quarter notes.
fn duration_quarters(duration: usize, bar: f64) -> f64 {
match duration {
0 => 0.25,
1 => 0.5,
2 => 1.0,
3 => 2.0,
5 => 2.0 * bar,
6 => 4.0 * bar,
_ => bar,
}
}
/// Number of bars in a bar-based Duration choice.
fn duration_bars(duration: usize) -> Option<i64> {
match duration {
4 => Some(1),
5 => Some(2),
6 => Some(4),
_ => None,
}
}
/// Moves the balance of the first two channels with a constant-power law: as the pan position
/// moves right the left channel's gain falls and the right's rises, and each channel stays on its
/// own side. The pan position is `manual · (1 - width) + modulator · width`, where the modulator is
/// the LFO or the envelope follower in -1..=1. At the center both gains are 1, so a stereo signal
/// passes unchanged; a signal that is the same on both channels pans across the field at a
/// constant level.
pub struct AutoPan {
p: Params,
sr: f32,
ch: usize,
transport: Transport,
/// Samples processed since the last `set_transport`, for hosts that call it less than once a block.
since_transport: u64,
was_playing: bool,
/// LFO phase in cycles, in `f64`: at the slowest rates an `f32` step is below half an ULP.
phase: f64,
/// A Single, Envelope or Manual trigger has finished its cycle and holds the last position.
holding: bool,
env: f32,
armed: bool,
/// Samples the envelope has spent below the threshold since the last Envelope trigger. The
/// trigger re-arms once this reaches the Release time, so a longer Release spaces triggers out.
below: u64,
last_retrigger: bool,
last_trigger: usize,
output: Smoother,
width: Smoother,
manual: Smoother,
/// Short declicking smoother on the pan position.
position: Smoother,
/// The next sample starts the position at its target instead of gliding to it.
snap_position: bool,
}
impl Default for AutoPan {
fn default() -> Self {
Self::new()
}
}
impl AutoPan {
pub fn new() -> Self {
let mut a = Self {
p: Params::new(&AUTOPAN_PARAMS),
sr: 48_000.0,
ch: 0,
transport: Transport::default(),
since_transport: 0,
was_playing: false,
phase: 0.0,
holding: false,
env: 0.0,
armed: true,
below: 0,
last_retrigger: false,
last_trigger: TRIGGER_FREE,
output: Smoother::new(1.0),
width: Smoother::new(1.0),
manual: Smoother::new(0.0),
position: Smoother::new(0.0),
snap_position: true,
};
a.update();
a.restart();
a
}
fn update(&mut self) {
self.output.set(db_to_gain(self.p.v(0)));
self.width.set(self.p.v(1) / 100.0);
self.manual.set(self.p.v(2) / 100.0);
let trigger = self.p.choice(7);
if trigger != self.last_trigger {
self.last_trigger = trigger;
self.restart();
}
let retrigger = self.p.on(8);
if retrigger != self.last_retrigger {
self.last_retrigger = retrigger;
if trigger == TRIGGER_MANUAL {
self.phase = 0.0;
self.holding = false;
}
}
}
/// Puts the LFO back at the start of the path. Single runs one cycle from there; Envelope and
/// Manual wait at the start for their first trigger.
fn restart(&mut self) {
self.phase = 0.0;
self.holding = matches!(self.p.choice(7), TRIGGER_ENVELOPE | TRIGGER_MANUAL);
self.armed = true;
self.below = 0;
}
/// LFO frequency in cycles per second.
fn lfo_hz(&self) -> f64 {
let tempo = if self.p.on(9) && self.transport.tempo.is_finite() && self.transport.tempo > 0.0 {
self.transport.tempo
} else {
f64::from(self.p.v(5))
};
tempo / 60.0 / duration_quarters(self.p.choice(10), self.bar_quarters())
}
fn bar_quarters(&self) -> f64 {
let (num, den) = (self.transport.numerator, self.transport.denominator);
if num == 0 || den == 0 { 4.0 } else { (f64::from(num) * 4.0 / f64::from(den)).clamp(0.25, 64.0) }
}
/// The LFO phase at `n` samples into the block when Beat Clock follows a playing transport.
/// Beat Clock restarts the LFO at the start of every Duration and runs it at Rate in between,
/// so a Rate slower than the session tempo plays only part of each cycle before the restart.
fn beat_clock_phase(&self, n: usize, hz: f64) -> Option<f64> {
let t = &self.transport;
if !(t.playing && t.tempo.is_finite() && t.tempo > 0.0 && t.quarters.is_finite()) {
return None;
}
let duration = self.p.choice(10);
let bar = self.bar_quarters();
let dur = duration_quarters(duration, bar);
let elapsed = self.since_transport.saturating_add(n as u64) as f64;
let quarters = t.quarters + elapsed * t.tempo / 60.0 / f64::from(self.sr);
// Bar-based cycles start on the bar line, so a change of time signature does not shift them;
// note and beat cycles follow the song position in quarter notes.
let into = match duration_bars(duration) {
Some(bars) => {
let in_bar = if t.bar_start.is_finite() { quarters - t.bar_start } else { quarters };
((t.bar.rem_euclid(bars) as f64 + in_bar / bar) / bars as f64).rem_euclid(1.0)
}
None => (quarters / dur).rem_euclid(1.0),
};
// Cycles of the LFO per Duration: 1 when its rate matches the session tempo.
let ratio = hz * dur * 60.0 / t.tempo;
Some((into * ratio).rem_euclid(1.0))
}
}
impl Plugin for AutoPan {
param_plumbing!(AUTOPAN_INFO);
fn prepare(&mut self, sample_rate: f32, _max_block: usize, channels: usize) {
self.sr = sane_sr(sample_rate);
self.ch = sane_channels(channels);
for s in [&mut self.output, &mut self.width, &mut self.manual] {
s.set_time(20.0, self.sr);
}
self.position.set_time(2.0, self.sr);
self.update();
self.reset();
}
fn reset(&mut self) {
self.restart();
self.env = 0.0;
for s in [&mut self.output, &mut self.width, &mut self.manual] {
s.snap();
}
self.snap_position = true;
}
fn set_transport(&mut self, transport: &Transport) {
self.transport = *transport;
self.since_transport = 0;
if transport.playing && !self.was_playing && self.p.choice(7) == TRIGGER_SINGLE {
self.restart();
}
self.was_playing = transport.playing;
}
fn process(&mut self, io: &mut [Vec<f32>], frames: usize) {
let ch = self.ch.min(io.len());
let frames = frames_in(io, frames, ch);
let hz = self.lfo_hz();
let inc = if hz.is_finite() { (hz / f64::from(self.sr)).clamp(0.0, 0.5) } else { 0.0 };
let waveform = self.p.choice(6);
let trigger = self.p.choice(7);
let envelope_source = self.p.choice(3) == SOURCE_ENVELOPE;
let sign = if self.p.choice(4) == PATH_RIGHT_TO_LEFT { -1.0 } else { 1.0 };
let threshold_db = self.p.v(11);
let threshold = db_to_gain(threshold_db);
let attack = time_coef(self.p.v(12), self.sr);
let release = time_coef(self.p.v(13), self.sr);
let release_samples = ms_to_samples(self.p.v(13), self.sr) as u64;
for n in 0..frames {
let sample = |io: &[Vec<f32>], c: usize| io.get(c).and_then(|b| b.get(n)).copied().filter(|v| v.is_finite()).unwrap_or(0.0);
let left = sample(io, 0);
let right = if ch >= 2 { sample(io, 1) } else { left };
// The louder channel drives the follower, so anti-phase material still has a level.
let level = left.abs().max(right.abs());
let k = if level > self.env { attack } else { release };
self.env = level + (self.env - level) * k;
if self.env < 1.0e-12 {
self.env = 0.0;
}
if trigger == TRIGGER_ENVELOPE {
if self.armed && self.env >= threshold {
self.armed = false;
self.phase = 0.0;
self.holding = false;
self.below = 0;
} else if !self.armed {
if self.env < threshold {
self.below = self.below.saturating_add(1);
self.armed = self.below >= release_samples;
} else {
self.below = 0;
}
}
}
let lfo_phase = match (trigger == TRIGGER_BEAT_CLOCK).then(|| self.beat_clock_phase(n, hz)).flatten() {
Some(phase) => phase,
None if self.holding => self.phase,
None => {
let current = self.phase;
self.phase += inc;
if self.phase >= 1.0 {
if matches!(trigger, TRIGGER_FREE | TRIGGER_BEAT_CLOCK) {
self.phase -= 1.0;
} else {
self.phase = 1.0;
self.holding = true;
}
}
current
}
};
let modulator = if envelope_source {
let above = (gain_to_db(self.env) - threshold_db) / -threshold_db;
2.0 * above.clamp(0.0, 1.0) - 1.0
} else {
wave_at(waveform, lfo_phase as f32)
};
let width = self.width.next();
let manual = self.manual.next();
self.position.set(manual * (1.0 - width) + sign * modulator * width);
if self.snap_position {
self.snap_position = false;
self.position.snap();
}
let position = self.position.next();
let out = self.output.next();
if ch >= 2 {
// Constant power: sin² + cos² = 1 across the field, and both gains are 1 at the center.
let theta = (position.clamp(-1.0, 1.0) + 1.0) * FRAC_PI_4;
let (l, r) = (theta.cos().max(0.0), theta.sin().max(0.0));
if let Some(x) = io.get_mut(0).and_then(|b| b.get_mut(n)) {
*x = left * l * SQRT_2 * out;
}
if let Some(x) = io.get_mut(1).and_then(|b| b.get_mut(n)) {
*x = right * r * SQRT_2 * out;
}
for c in 2..ch {
if let Some(x) = io.get_mut(c).and_then(|b| b.get_mut(n)) {
*x = if x.is_finite() { *x * out } else { 0.0 };
}
}
} else if let Some(x) = io.get_mut(0).and_then(|b| b.get_mut(n)) {
*x = left * out;
}
}
self.since_transport = self.since_transport.saturating_add(frames as u64);
}
}
+291 -3
View File
@@ -1,7 +1,9 @@
//! Harmonic effects: Saturator (tanh), Lo-Fi (bit/sample-rate reduction) and Rectifier.
//! Harmonic effects: Saturator (tanh), Lo-Fi (bit/sample-rate reduction), Rectifier and
//! Subharmonic (octave-down bass synthesis).
use crate::params::{Params, choice, lin, log, param_plumbing};
use crate::util::{DcBlocker, Rng, Smoother, flush, frames_in, sane_channels, sane_sr};
use crate::biquad::{Biquad, FilterType};
use crate::params::{Params, choice, lin, log, param_plumbing, toggle};
use crate::util::{DcBlocker, Rng, Smoother, flush, frames_in, sane_channels, sane_sr, time_coef};
use crate::{Category, ParamInfo, Plugin, PluginInfo, Unit, db_to_gain};
static SAT_PARAMS: [ParamInfo; 5] = [
@@ -307,3 +309,289 @@ impl Plugin for Rectifier {
}
}
}
// ---------------------------------------------------------------------------------------------
const SUB_RANGES: &[&str] = &["120-180 Hz", "80-120 Hz", "60-90 Hz", "40-60 Hz"];
/// Lower and upper band frequency of each range.
const SUB_RANGE_HZ: [(f32, f32); 4] = [(120.0, 180.0), (80.0, 120.0), (60.0, 90.0), (40.0, 60.0)];
const SUB_CHARACTERS: &[&str] = &["Clean I", "Clean II", "Resonant I", "Resonant II", "Distort I", "Distort II"];
/// Band gains at or below this are off.
const SUB_OFF_DB: f32 = -120.0;
/// Q of the band-pass that picks each band out of the input.
const SUB_BAND_Q: f32 = 2.0;
/// Cutoff of the low-pass after each divider, as a fraction of the band frequency.
const SUB_SMOOTH: f32 = 0.75;
/// Zero-crossing hysteresis, as a fraction of the band's envelope.
const SUB_HYSTERESIS: f32 = 0.1;
/// Release of the band envelope that sets the hysteresis.
const SUB_ENV_RELEASE_MS: f32 = 50.0;
/// Bias of the asymmetric Distort I curve.
const SUB_ASYM: f32 = 0.25;
static SUB_PARAMS: [ParamInfo; 17] = [
lin("input", "Input", -18.0, 18.0, 0.0, Unit::Db),
choice("range", "Range", SUB_RANGES, 2),
lin("lower_gain", "Lower Gain", SUB_OFF_DB, 24.0, 0.0, Unit::Db),
lin("upper_gain", "Upper Gain", SUB_OFF_DB, 24.0, -6.0, Unit::Db),
lin("direct_gain", "Direct Gain", SUB_OFF_DB, 24.0, SUB_OFF_DB, Unit::Db),
toggle("lower_solo", "Lower Solo", false),
toggle("upper_solo", "Upper Solo", false),
toggle("direct_solo", "Direct Solo", false),
lin("drive", "Drive", -18.0, 18.0, 0.0, Unit::Db),
choice("character", "Character", SUB_CHARACTERS, 0),
log("hp_freq", "HP Freq", 5.0, 1000.0, 20.0, Unit::Hz),
log("hp_q", "HP Q", 0.1, 5.0, 0.707, Unit::None),
log("lp_freq", "LP Freq", 5.0, 1000.0, 200.0, Unit::Hz),
log("lp_q", "LP Q", 0.1, 5.0, 0.707, Unit::None),
lin("mix", "Mix", 0.0, 100.0, 50.0, Unit::Percent),
toggle("wet_solo", "Wet Solo", false),
lin("output", "Output", -18.0, 18.0, 0.0, Unit::Db),
];
pub static SUB_INFO: PluginInfo = PluginInfo {
id: "subharmonic",
name: "Subharmonic",
short_name: "SubHarm",
category: Category::Harmonic,
params: &SUB_PARAMS,
is_instrument: false,
audiosuite: true,
};
/// One synthesized band: a band-pass, a divide-by-two flip-flop and a smoothing low-pass.
#[derive(Debug, Clone, Copy)]
struct SubBand {
band: Biquad,
smooth: Biquad,
/// Divider output: ±1, flipped on every rising zero crossing of the band.
flip: f32,
/// The band has gone below the negative threshold since the last flip.
armed: bool,
env: f32,
}
impl SubBand {
fn new() -> Self {
Self { band: Biquad::default(), smooth: Biquad::default(), flip: 1.0, armed: false, env: 0.0 }
}
fn reset(&mut self) {
self.band.reset();
self.smooth.reset();
self.flip = 1.0;
self.armed = false;
self.env = 0.0;
}
/// The octave below the band's part of `x`; `rel` is the envelope's release coefficient.
#[inline]
fn process(&mut self, x: f32, rel: f32) -> f32 {
let b = self.band.process_sample(x);
let a = b.abs();
self.env = flush(if a > self.env { a } else { self.env * rel });
let th = self.env * SUB_HYSTERESIS;
if b < -th {
self.armed = true;
} else if self.armed && b > th {
self.armed = false;
self.flip = -self.flip;
}
self.smooth.process_sample(b * self.flip)
}
}
/// Filter and divider state of one channel.
#[derive(Debug, Clone, Copy)]
struct SubChannel {
/// The lower and the upper band.
bands: [SubBand; 2],
/// Low-pass that takes the direct bass out of the input.
direct: Biquad,
hp: Biquad,
lp: Biquad,
dc: DcBlocker,
}
impl SubChannel {
fn new(sr: f32) -> Self {
Self { bands: [SubBand::new(); 2], direct: Biquad::default(), hp: Biquad::default(), lp: Biquad::default(), dc: DcBlocker::new(5.0, sr) }
}
fn reset(&mut self) {
self.bands.iter_mut().for_each(SubBand::reset);
self.direct.reset();
self.hp.reset();
self.lp.reset();
self.dc.reset();
}
}
/// Subharmonic synthesizer. The Range sets a lower and an upper band frequency. Each band
/// band-passes the input at its frequency, and a divide-by-two flip-flop on the band's rising zero
/// crossings multiplies the band, which moves it an octave down at a level that follows the band.
/// The direct band is the input low-passed at the upper band frequency.
///
/// - The three bands are summed with their gains, raised by Drive and saturated, then
/// shaped by the HP and LP filters. A soloed band plays alone and mutes the dry signal.
/// - Clean I saturates with tanh and Clean II with a softer algebraic curve. Resonant I and II use
/// tanh and raise the Q of the low-pass after each divider to 2 and 4. Distort I bends the curve
/// asymmetrically (even harmonics, DC removed) and Distort II clips hard at full scale.
/// - Below 50 % Mix the dry signal stays at full level and the wet level falls; above 50 % the wet
/// signal stays at full level and the dry level falls. Wet Solo mutes the dry signal.
pub struct SubHarmonic {
p: Params,
sr: f32,
ch: Vec<SubChannel>,
input: Smoother,
bands: [Smoother; 3],
drive: Smoother,
dry: Smoother,
wet: Smoother,
out: Smoother,
env_rel: f32,
}
impl Default for SubHarmonic {
fn default() -> Self {
Self::new()
}
}
impl SubHarmonic {
pub fn new() -> Self {
let mut s = Self {
p: Params::new(&SUB_PARAMS),
sr: 48_000.0,
ch: Vec::new(),
input: Smoother::new(1.0),
bands: [Smoother::new(0.0); 3],
drive: Smoother::new(1.0),
dry: Smoother::new(1.0),
wet: Smoother::new(1.0),
out: Smoother::new(1.0),
env_rel: 0.0,
};
s.update();
s
}
/// The lower and upper band frequencies of the current range.
fn range_hz(&self) -> (f32, f32) {
SUB_RANGE_HZ.get(self.p.choice(1)).copied().unwrap_or((60.0, 90.0))
}
/// Q of the low-pass after each divider for the current Drive Character.
fn smooth_q(&self) -> f32 {
match self.p.choice(9) {
2 => 2.0,
3 => 4.0,
_ => std::f32::consts::FRAC_1_SQRT_2,
}
}
fn update(&mut self) {
let gain = |db: f32| if db <= SUB_OFF_DB { 0.0 } else { db_to_gain(db) };
let solo = [self.p.on(5), self.p.on(6), self.p.on(7)];
let any_solo = solo.contains(&true);
for (k, s) in self.bands.iter_mut().enumerate() {
let heard = !any_solo || solo.get(k).copied().unwrap_or(false);
s.set(if heard { gain(self.p.v(2 + k)) } else { 0.0 });
}
self.input.set(db_to_gain(self.p.v(0)));
self.drive.set(db_to_gain(self.p.v(8)));
self.out.set(db_to_gain(self.p.v(16)));
let m = self.p.v(14) / 100.0;
self.wet.set((2.0 * m).min(1.0));
self.dry.set(if any_solo || self.p.on(15) { 0.0 } else { (2.0 * (1.0 - m)).min(1.0) });
self.env_rel = time_coef(SUB_ENV_RELEASE_MS, self.sr);
let (lo, hi) = self.range_hz();
let sq = self.smooth_q();
let sr = self.sr;
let (hp_f, hp_q, lp_f, lp_q) = (self.p.v(10), self.p.v(11), self.p.v(12), self.p.v(13));
for c in &mut self.ch {
for (b, f) in c.bands.iter_mut().zip([lo, hi]) {
b.band.set(FilterType::BandPass, f, SUB_BAND_Q, 0.0, sr);
b.smooth.set(FilterType::LowPass, f * SUB_SMOOTH, sq, 0.0, sr);
}
c.direct.set(FilterType::LowPass, hi, std::f32::consts::FRAC_1_SQRT_2, 0.0, sr);
c.hp.set(FilterType::HighPass, hp_f, hp_q, 0.0, sr);
c.lp.set(FilterType::LowPass, lp_f, lp_q, 0.0, sr);
}
}
fn smoothers(&mut self) -> impl Iterator<Item = &mut Smoother> {
[&mut self.input, &mut self.drive, &mut self.dry, &mut self.wet, &mut self.out].into_iter().chain(self.bands.iter_mut())
}
}
/// Saturation curve of each Drive Character (index into `SUB_CHARACTERS`).
fn sub_saturate(character: usize, x: f32) -> f32 {
match character {
1 => x / (1.0 + x * x).sqrt(),
4 => ((x + SUB_ASYM).tanh() - SUB_ASYM.tanh()) / (1.0 + SUB_ASYM.tanh()),
5 => x.clamp(-1.0, 1.0),
_ => x.tanh(),
}
}
impl Plugin for SubHarmonic {
param_plumbing!(SUB_INFO);
fn prepare(&mut self, sample_rate: f32, _max_block: usize, channels: usize) {
self.sr = sane_sr(sample_rate);
self.ch = vec![SubChannel::new(self.sr); sane_channels(channels)];
let sr = self.sr;
for s in self.smoothers() {
s.set_time(20.0, sr);
}
self.update();
self.reset();
}
fn reset(&mut self) {
self.ch.iter_mut().for_each(SubChannel::reset);
for s in self.smoothers() {
s.snap();
}
}
fn process(&mut self, io: &mut [Vec<f32>], frames: usize) {
let ch = self.ch.len().min(io.len());
let frames = frames_in(io, frames, ch);
let character = self.p.choice(9);
let rel = self.env_rel;
for n in 0..frames {
let gin = self.input.next();
let g = [self.bands[0].next(), self.bands[1].next(), self.bands[2].next()];
let drive = self.drive.next();
let dg = self.dry.next();
let wg = self.wet.next();
let o = self.out.next();
for c in 0..ch {
let (Some(buf), Some(st)) = (io.get_mut(c), self.ch.get_mut(c)) else { continue };
let Some(x) = buf.get_mut(n) else { continue };
let dry = if x.is_finite() { *x * gin } else { 0.0 };
let mut sum = st.direct.process_sample(dry) * g[2];
for (band, gain) in st.bands.iter_mut().zip(g) {
sum += band.process(dry, rel) * gain;
}
let mut y = sub_saturate(character, sum * drive);
if character == 4 {
y = st.dc.process(y);
}
let wet = st.lp.process_sample(st.hp.process_sample(y));
*x = (dry * dg + wet * wg) * o;
}
}
}
fn tail_samples(&self) -> usize {
// Time for the slowest, most resonant filter to ring down by 60 dB (6.9 time constants).
let (lo, _) = self.range_hz();
let f = self.p.v(10).min(self.p.v(12)).min(lo * SUB_SMOOTH).max(1.0);
let q = self.p.v(11).max(self.p.v(13)).max(self.smooth_q()).max(SUB_BAND_Q);
(6.9 * q / (std::f32::consts::PI * f) * self.sr) as usize
}
}
+7
View File
@@ -1,5 +1,6 @@
//! The built-in plugins and their registry.
pub mod auto_pan;
pub mod drums;
pub mod dynamics;
pub mod eq;
@@ -26,12 +27,15 @@ pub(crate) static REGISTRY: &[&PluginInfo] = &[
&reverb::ROOM_INFO,
&reverb::PLATE_INFO,
&modulation::DELAY_INFO,
&auto_pan::AUTOPAN_INFO,
&modulation::CHORUS_INFO,
&modulation::FLANGER_INFO,
&modulation::PHASER_INFO,
&modulation::RING_INFO,
&harmonic::SAT_INFO,
&harmonic::LOFI_INFO,
&harmonic::RECT_INFO,
&harmonic::SUB_INFO,
&pitch::PITCH_INFO,
&utility::SHIFT_INFO,
&utility::GAIN_INFO,
@@ -58,12 +62,15 @@ pub(crate) fn instantiate(id: &str) -> Option<Box<dyn Plugin>> {
"room_reverb" => Box::new(reverb::Reverb::room()),
"plate_reverb" => Box::new(reverb::Reverb::plate()),
"mod_delay" => Box::new(modulation::ModDelay::new()),
"auto_pan" => Box::new(auto_pan::AutoPan::new()),
"chorus" => Box::new(modulation::ModFx::chorus()),
"flanger" => Box::new(modulation::ModFx::flanger()),
"phaser" => Box::new(modulation::Phaser::new()),
"ring_mod" => Box::new(modulation::RingMod::new()),
"saturator" => Box::new(harmonic::Saturator::new()),
"lofi" => Box::new(harmonic::LoFi::new()),
"rectifier" => Box::new(harmonic::Rectifier::new()),
"subharmonic" => Box::new(harmonic::SubHarmonic::new()),
"pitch_shifter" => Box::new(pitch::PitchShifter::new()),
"time_shift" => Box::new(utility::TimeShift::new()),
"gain" => Box::new(utility::Gain::new()),
+220 -2
View File
@@ -1,7 +1,7 @@
//! Delay and modulation effects: Mod Delay, Chorus, Flanger, Phaser.
//! Delay and modulation effects: Mod Delay, Chorus, Flanger, Phaser, Ring Modulator.
use crate::params::{Params, choice, lin, log, param_plumbing};
use crate::util::{DelayLine, Lfo, Smoother, flush, frames_in, ms_to_samples, sane_channels, sane_sr};
use crate::util::{DelayLine, Lfo, Rng, Smoother, flush, frames_in, ms_to_samples, sane_channels, sane_sr, time_coef};
use crate::{Category, ParamInfo, Plugin, PluginInfo, Unit};
/// Per-channel LFO phase offset for stereo movement (channel 1 is offset by `spread` cycles).
@@ -459,3 +459,221 @@ impl Plugin for Phaser {
ms_to_samples(100.0, self.sr) as usize
}
}
// ---------------------------------------------------------------------------------------------
const RING_EFFECTS: &[&str] = &["Ring", "Frequency Mod", "Resonator +", "Resonator -"];
const RING_SOURCES: &[&str] = &["LFO", "Envelope", "Sample & Hold"];
const RING_MIN_HZ: f32 = 20.0;
/// Octaves the frequency moves at full modulation depth.
const RING_MOD_OCTAVES: f32 = 2.0;
/// Peak delay swing of Frequency Mod.
const RING_FM_SWING_MS: f32 = 1.0;
/// Sample-and-hold noise seed (the same sequence after every reset).
const RING_SEED: u32 = 0x5EED_1234;
static RING_PARAMS: [ParamInfo; 7] = [
choice("effect", "Effect", RING_EFFECTS, 0),
log("frequency", "Frequency", RING_MIN_HZ, 5000.0, 440.0, Unit::Hz),
lin("resonance", "Resonance", 0.0, 95.0, 50.0, Unit::Percent),
choice("mod_source", "Mod Source", RING_SOURCES, 0),
log("mod_rate", "Mod Rate", 0.05, 20.0, 1.0, Unit::Hz),
lin("mod_depth", "Mod Depth", 0.0, 100.0, 0.0, Unit::Percent),
lin("mix", "Mix", 0.0, 100.0, 100.0, Unit::Percent),
];
pub static RING_INFO: PluginInfo = PluginInfo {
id: "ring_mod",
name: "Ring Modulator",
short_name: "RingMod",
category: Category::Modulation,
params: &RING_PARAMS,
is_instrument: false,
audiosuite: true,
};
/// Ring modulation, frequency modulation and positive or negative comb resonators, with the
/// frequency moved by an LFO, an envelope follower or a sample-and-hold source.
///
/// - Ring multiplies the input by a sine carrier at the frequency.
/// - Frequency Mod reads the input through a delay that the carrier swings by ±1 ms.
/// - Resonator + and Resonator - feed the output back after one period of the frequency, with
/// a positive or a negative sign. The output is scaled by `1 - resonance`, so the peak gain at
/// resonance is unity.
///
/// At 100 % depth the modulation moves the frequency by up to two octaves either way. The
/// envelope follower moves it up only.
pub struct RingMod {
p: Params,
sr: f32,
lines: Vec<DelayLine>,
lfo: Lfo,
rng: Rng,
carrier: f32,
env: f32,
env_attack: f32,
env_release: f32,
held: f32,
hold_left: usize,
last_effect: usize,
freq: Smoother,
res: Smoother,
depth: Smoother,
mix: Smoother,
}
impl Default for RingMod {
fn default() -> Self {
Self::new()
}
}
impl RingMod {
pub fn new() -> Self {
let mut r = Self {
p: Params::new(&RING_PARAMS),
sr: 48_000.0,
lines: Vec::new(),
lfo: Lfo::new(),
rng: Rng::new(RING_SEED),
carrier: 0.0,
env: 0.0,
env_attack: 0.0,
env_release: 0.0,
held: 0.0,
hold_left: 0,
last_effect: 0,
freq: Smoother::new(440.0),
res: Smoother::new(0.0),
depth: Smoother::new(0.0),
mix: Smoother::new(1.0),
};
r.update();
r
}
fn update(&mut self) {
self.freq.set(self.p.v(1));
self.res.set(self.p.v(2) / 100.0);
self.depth.set(self.p.v(5) / 100.0);
self.mix.set(self.p.v(6) / 100.0);
}
/// Samples in one period of the lowest frequency the modulation can reach.
fn longest_period(&self) -> f32 {
self.sr / (self.p.v(1) * 2f32.powf(-RING_MOD_OCTAVES * self.p.v(5) / 100.0)).max(1.0)
}
}
impl Plugin for RingMod {
param_plumbing!(RING_INFO);
fn prepare(&mut self, sample_rate: f32, _max_block: usize, channels: usize) {
self.sr = sane_sr(sample_rate);
let comb = self.sr / (RING_MIN_HZ * 2f32.powf(-RING_MOD_OCTAVES));
let max = comb.max(ms_to_samples(2.0 * RING_FM_SWING_MS, self.sr)) as usize + 8;
self.lines = (0..sane_channels(channels))
.map(|_| {
let mut d = DelayLine::default();
d.allocate(max);
d
})
.collect();
self.env_attack = time_coef(5.0, self.sr);
self.env_release = time_coef(100.0, self.sr);
for s in [&mut self.freq, &mut self.res, &mut self.depth, &mut self.mix] {
s.set_time(20.0, self.sr);
}
self.update();
self.reset();
}
fn reset(&mut self) {
self.lines.iter_mut().for_each(DelayLine::clear);
self.lfo.reset();
self.rng = Rng::new(RING_SEED);
self.carrier = 0.0;
self.env = 0.0;
self.held = 0.0;
self.hold_left = 0;
self.last_effect = self.p.choice(0);
for s in [&mut self.freq, &mut self.res, &mut self.depth, &mut self.mix] {
s.snap();
}
}
fn process(&mut self, io: &mut [Vec<f32>], frames: usize) {
let ch = self.lines.len().min(io.len());
let frames = frames_in(io, frames, ch);
let effect = self.p.choice(0);
if effect != self.last_effect {
// The lines hold the previous effect's state.
self.lines.iter_mut().for_each(DelayLine::clear);
self.last_effect = effect;
}
let source = self.p.choice(3);
let rate = self.p.v(4);
let inc = rate / self.sr;
let hold = ((self.sr / rate.max(0.05)) as usize).max(1);
let nyq = self.sr * 0.45;
let swing = ms_to_samples(RING_FM_SWING_MS, self.sr);
for n in 0..frames {
let level = io.iter().take(ch).filter_map(|b| b.get(n)).filter(|x| x.is_finite()).fold(0.0f32, |m, x| m.max(x.abs()));
let k = if level > self.env { self.env_attack } else { self.env_release };
self.env = flush(level + (self.env - level) * k);
let lfo = Lfo::sin_at(self.lfo.tick(inc));
let m = match source {
1 => self.env.min(1.0),
2 => {
if self.hold_left == 0 {
self.held = self.rng.bipolar();
self.hold_left = hold;
}
self.hold_left -= 1;
self.held
}
_ => lfo,
};
let f = (self.freq.next() * 2f32.powf(RING_MOD_OCTAVES * self.depth.next() * m)).clamp(1.0, nyq);
let fb = self.res.next();
let mix = self.mix.next();
self.carrier += f / self.sr;
if self.carrier >= 1.0 {
self.carrier -= 1.0;
}
let carrier = Lfo::sin_at(self.carrier);
for c in 0..ch {
let (Some(buf), Some(line)) = (io.get_mut(c), self.lines.get_mut(c)) else { continue };
let Some(x) = buf.get_mut(n) else { continue };
let dry = if x.is_finite() { *x } else { 0.0 };
let wet = match effect {
1 => {
line.push(dry);
line.read_cubic(2.0 + swing * (1.0 + carrier))
}
2 | 3 => {
let sign = if effect == 2 { 1.0 } else { -1.0 };
let y = flush(dry + sign * fb * line.read_cubic(self.sr / f));
line.push(y);
y * (1.0 - fb)
}
_ => dry * carrier,
};
*x = dry * (1.0 - mix) + wet * mix;
}
}
}
fn tail_samples(&self) -> usize {
match self.p.choice(0) {
1 => ms_to_samples(2.0 * RING_FM_SWING_MS, self.sr) as usize + 4,
2 | 3 => {
// Time for the feedback to fall by 60 dB at the longest period the modulation reaches.
let fb = (self.p.v(2) / 100.0).clamp(0.0, 0.99);
let repeats = if fb < 1e-3 { 1.0 } else { (-60.0 / (20.0 * fb.log10())).min(500.0) + 1.0 };
(self.longest_period() * repeats) as usize
}
_ => 0,
}
}
}
+344
View File
@@ -0,0 +1,344 @@
use std::f32::consts::{FRAC_PI_4, PI, TAU};
use soundcraft_dsp::{Plugin, Transport, create, db_to_gain};
const SR: f32 = 48_000.0;
const BLOCK: usize = 512;
fn auto_pan(params: &[(&str, f32)]) -> Box<dyn Plugin> {
let mut p = create("auto_pan").unwrap();
p.prepare(SR, BLOCK, 2);
for (id, v) in params {
assert!(p.set_param(id, *v), "{id}");
}
p.reset();
p
}
/// Runs `seconds` of a constant `level` on both channels in host-sized blocks.
fn run_level(p: &mut dyn Plugin, level: f32, seconds: f32) -> Vec<Vec<f32>> {
let len = (seconds * SR) as usize;
let mut out = vec![Vec::new(), Vec::new()];
let mut pos = 0;
while pos < len {
let n = BLOCK.min(len - pos);
let mut buf = vec![vec![level; n]; 2];
p.process(&mut buf, n);
for c in 0..2 {
out[c].extend_from_slice(&buf[c]);
}
pos += n;
}
out
}
/// Pan position (-1 left ..= 1 right) of frame `i`, from the constant-power gains.
fn position(out: &[Vec<f32>], i: usize) -> f32 {
4.0 / PI * out[1][i].atan2(out[0][i]) - 1.0
}
fn at(seconds: f32) -> usize {
(seconds * SR) as usize
}
fn assert_near(got: f32, want: f32, tol: f32, what: &str) {
assert!((got - want).abs() <= tol, "{what}: got {got}, want {want}");
}
#[test]
fn auto_pan_manual_places_the_signal_when_width_is_zero() {
let mut p = auto_pan(&[("width", 0.0), ("manual", -100.0)]);
let out = run_level(p.as_mut(), 0.5, 0.1);
assert_near(out[0][at(0.05)], 0.5 * 2f32.sqrt(), 1e-4, "hard left, left channel");
assert_near(out[1][at(0.05)], 0.0, 1e-4, "hard left, right channel");
let mut p = auto_pan(&[("width", 0.0), ("manual", 50.0)]);
let out = run_level(p.as_mut(), 0.5, 0.1);
assert_near(position(&out, at(0.05)), 0.5, 1e-3, "manual 50 %");
let mut p = auto_pan(&[("width", 0.0)]);
let out = run_level(p.as_mut(), 0.5, 0.1);
assert_near(out[0][at(0.05)], 0.5, 1e-4, "center keeps the level on the left");
assert_near(out[1][at(0.05)], 0.5, 1e-4, "center keeps the level on the right");
}
#[test]
fn auto_pan_lfo_moves_from_left_to_right_and_back() {
// 60 BPM with a Duration of 1 beat is one cycle per second.
let mut p = auto_pan(&[("rate", 60.0), ("duration", 2.0)]);
let out = run_level(p.as_mut(), 0.5, 1.0);
for (t, want) in [(0.0, -1.0), (0.25, 0.0), (0.5, 1.0), (0.75, 0.0)] {
assert_near(position(&out, at(t)), want, 0.05, &format!("left to right at {t} s"));
}
let mut p = auto_pan(&[("rate", 60.0), ("duration", 2.0), ("path", 1.0)]);
let out = run_level(p.as_mut(), 0.5, 1.0);
assert_near(position(&out, at(0.01)), 1.0, 0.05, "right to left starts on the right");
assert_near(position(&out, at(0.5)), -1.0, 0.05, "right to left at 0.5 s");
// Width 50 % sweeps half the field, and Manual moves that half to the right.
let mut p = auto_pan(&[("rate", 60.0), ("duration", 2.0), ("width", 50.0), ("manual", 50.0)]);
let out = run_level(p.as_mut(), 0.5, 1.0);
assert_near(position(&out, at(0.0)), -0.25, 0.05, "width 50 % at the start");
assert_near(position(&out, at(0.5)), 0.75, 0.05, "width 50 % at the far end");
}
#[test]
fn auto_pan_keeps_moving_at_the_slowest_rate() {
// Rate 1 BPM over 4 bars of 4/4 is one cycle in 960 s; 600 s in, a saw is 62.5 % across.
let mut p = auto_pan(&[("rate", 1.0), ("duration", 6.0), ("waveform", 3.0)]);
let mut buf = vec![vec![0.5f32; BLOCK]; 2];
for _ in 0..(600.0 * SR) as usize / BLOCK {
buf.iter_mut().for_each(|c| c.fill(0.5));
p.process(&mut buf, BLOCK);
}
assert_near(position(&buf, BLOCK - 1), 0.25, 0.01, "600 s into a 960 s cycle");
}
#[test]
fn auto_pan_waveforms_take_their_shapes() {
let shape = |waveform: f32, t: f32| {
let mut p = auto_pan(&[("rate", 60.0), ("duration", 2.0), ("waveform", waveform)]);
let out = run_level(p.as_mut(), 0.5, 1.0);
position(&out, at(t))
};
assert_near(shape(1.0, 0.25), 0.0, 0.05, "triangle at a quarter cycle");
assert_near(shape(1.0, 0.125), -0.5, 0.05, "triangle at an eighth of a cycle");
assert_near(shape(2.0, 0.2), -1.0, 0.02, "square in its first half");
assert_near(shape(2.0, 0.7), 1.0, 0.02, "square in its second half");
assert_near(shape(3.0, 0.25), -0.5, 0.05, "saw up");
assert_near(shape(4.0, 0.25), 0.5, 0.05, "saw down");
assert_near(shape(5.0, 1.0 / 6.0), 0.0, 0.05, "half sine reaches the center at a sixth of a cycle");
assert_near(shape(6.0, 0.1), -1.0, 0.02, "4 step triangle, first step");
assert_near(shape(6.0, 0.2), -1.0 / 3.0, 0.02, "4 step triangle, second step");
assert_near(shape(6.0, 0.45), 1.0, 0.02, "4 step triangle, top step");
}
#[test]
fn auto_pan_rate_follows_the_session_tempo_and_duration() {
// Linked to a 120 BPM session with a Duration of 1 beat: two cycles per second.
let mut p = auto_pan(&[("link_tempo", 1.0), ("rate", 30.0), ("duration", 2.0)]);
p.set_transport(&Transport { tempo: 120.0, numerator: 4, denominator: 4, ..Transport::default() });
let out = run_level(p.as_mut(), 0.5, 0.5);
assert_near(position(&out, at(0.25)), 1.0, 0.05, "linked to 120 BPM, 1 beat");
// Not linked: Rate 120 BPM and the default Duration of 1 bar (4/4) is one cycle in 2 s.
let mut p = auto_pan(&[]);
p.set_transport(&Transport { tempo: 60.0, numerator: 4, denominator: 4, ..Transport::default() });
let out = run_level(p.as_mut(), 0.5, 1.5);
assert_near(position(&out, at(1.0)), 1.0, 0.05, "Rate 120 BPM, 1 bar");
// A 3/4 bar at 120 BPM lasts 1.5 s.
let mut p = auto_pan(&[("link_tempo", 1.0)]);
p.set_transport(&Transport { tempo: 120.0, numerator: 3, denominator: 4, ..Transport::default() });
let out = run_level(p.as_mut(), 0.5, 1.0);
assert_near(position(&out, at(0.75)), 1.0, 0.05, "1 bar of 3/4");
}
#[test]
fn auto_pan_beat_clock_follows_the_transport_position() {
let mut p = auto_pan(&[("trigger", 2.0), ("link_tempo", 1.0), ("duration", 2.0)]);
// Half a beat into the song, the LFO is half way through its cycle.
p.set_transport(&Transport { playing: true, tempo: 120.0, numerator: 4, denominator: 4, quarters: 7.5, ..Transport::default() });
let out = run_level(p.as_mut(), 0.5, 0.01);
assert_near(position(&out, 0), 1.0, 0.05, "half a beat in");
let mut p = auto_pan(&[("trigger", 2.0), ("link_tempo", 1.0), ("duration", 2.0)]);
p.set_transport(&Transport { playing: true, tempo: 120.0, numerator: 4, denominator: 4, quarters: 3.0, ..Transport::default() });
let out = run_level(p.as_mut(), 0.5, 0.2);
assert_near(position(&out, 0), -1.0, 0.05, "on the beat");
assert_near(position(&out, at(0.125)), 0.0, 0.05, "a quarter of a beat later");
// Not linked, Rate 60 BPM in a 120 BPM session: the LFO restarts on every beat (0.5 s) and
// runs at 1 Hz in between, so it reaches the right and then starts again from the left.
let mut p = auto_pan(&[("trigger", 2.0), ("rate", 60.0), ("duration", 2.0)]);
p.set_transport(&Transport { playing: true, tempo: 120.0, numerator: 4, denominator: 4, quarters: 3.0, ..Transport::default() });
let out = run_level(p.as_mut(), 0.5, 1.0);
assert_near(position(&out, at(0.25)), 0.0, 0.05, "a quarter cycle at 1 Hz");
assert_near(position(&out, at(0.49)), 1.0, 0.05, "on the right at the end of the beat");
assert_near(position(&out, at(0.51)), -1.0, 0.05, "restarted on the next beat");
assert_near(position(&out, at(0.75)), 0.0, 0.05, "a quarter cycle after the restart");
// 2 Beats in 3/4, with the host updating the transport every block: the cycle runs on across
// the bar line at quarter 3 instead of restarting there.
let mut p = auto_pan(&[("trigger", 2.0), ("link_tempo", 1.0), ("waveform", 3.0), ("duration", 3.0)]);
let mut buf = vec![vec![0.5f32; 100]; 2];
let mut positions = Vec::new();
for block in 0..480 {
let quarters = 2.0 + block as f64 * 100.0 * 2.0 / f64::from(SR);
let bar = (quarters / 3.0).floor();
p.set_transport(&Transport {
playing: true,
tempo: 120.0,
numerator: 3,
denominator: 4,
quarters,
bar_start: bar * 3.0,
bar: bar as i64,
..Transport::default()
});
buf.iter_mut().for_each(|c| c.fill(0.5));
p.process(&mut buf, 100);
positions.push((quarters, position(&buf, 0)));
}
for (q, want) in [(2.5, -0.5), (3.0, 0.0), (3.5, 0.5)] {
let got = positions.iter().find(|(quarters, _)| (quarters - q).abs() < 1e-9).map(|(_, pos)| *pos).unwrap();
assert_near(got, want, 0.02, &format!("2 beats in 3/4 at quarter {q}"));
}
// Bar-based cycles start on the bar line after a change from 4/4 to 3/4 at quarter 4.
let three_four = |quarters: f64| Transport {
playing: true,
tempo: 120.0,
numerator: 3,
denominator: 4,
quarters,
bar_start: 4.0,
bar: 1,
..Transport::default()
};
let mut p = auto_pan(&[("trigger", 2.0), ("link_tempo", 1.0), ("waveform", 3.0)]);
p.set_transport(&three_four(4.0));
let out = run_level(p.as_mut(), 0.5, 0.01);
assert_near(position(&out, 0), -1.0, 0.02, "1 bar starts on the 3/4 bar line");
let mut p = auto_pan(&[("trigger", 2.0), ("link_tempo", 1.0), ("waveform", 3.0), ("duration", 5.0)]);
p.set_transport(&three_four(5.5));
let out = run_level(p.as_mut(), 0.5, 0.01);
assert_near(position(&out, 0), 0.5, 0.02, "half way through the second bar of 2 bars");
}
#[test]
fn auto_pan_single_runs_one_cycle_and_holds() {
let mut p = auto_pan(&[("trigger", 1.0), ("rate", 60.0), ("duration", 2.0), ("waveform", 3.0)]);
let out = run_level(p.as_mut(), 0.5, 2.0);
assert_near(position(&out, at(0.5)), 0.0, 0.05, "half way through the one cycle");
assert_near(position(&out, at(1.5)), 1.0, 0.02, "holds at the end of the path");
assert_near(position(&out, at(1.99)), 1.0, 0.02, "still holding");
// Starting the transport runs the cycle again.
p.set_transport(&Transport { playing: true, tempo: 120.0, numerator: 4, denominator: 4, ..Transport::default() });
let out = run_level(p.as_mut(), 0.5, 0.5);
assert_near(position(&out, at(0.25)), -0.5, 0.05, "restarted when playback starts");
}
#[test]
fn auto_pan_envelope_source_pans_by_level() {
let settled = |level: f32| {
let mut p = auto_pan(&[("source", 1.0), ("threshold", -30.0)]);
let out = run_level(p.as_mut(), level, 0.5);
position(&out, at(0.45))
};
assert_near(settled(db_to_gain(-40.0)), -1.0, 0.02, "below the threshold");
assert_near(settled(db_to_gain(-15.0)), 0.0, 0.05, "half way to full scale");
assert_near(settled(1.0), 1.0, 0.02, "full scale");
}
#[test]
fn auto_pan_envelope_and_manual_triggers_start_a_cycle() {
let mut p = auto_pan(&[("trigger", 3.0), ("rate", 60.0), ("duration", 2.0), ("waveform", 3.0), ("threshold", -30.0)]);
let quiet = run_level(p.as_mut(), db_to_gain(-40.0), 0.5);
assert_near(position(&quiet, at(0.4)), -1.0, 0.02, "waits at the start below the threshold");
let loud = run_level(p.as_mut(), 1.0, 1.5);
assert_near(position(&loud, at(0.25)), -0.5, 0.05, "a quarter cycle after the level crosses the threshold");
assert_near(position(&loud, at(1.4)), 1.0, 0.02, "holds after one cycle while the level stays up");
let mut p = auto_pan(&[("trigger", 4.0), ("rate", 60.0), ("duration", 2.0), ("waveform", 3.0)]);
let before = run_level(p.as_mut(), 0.5, 0.5);
assert_near(position(&before, at(0.4)), -1.0, 0.02, "waits for the manual trigger");
assert!(p.set_param("retrigger", 1.0));
let after = run_level(p.as_mut(), 0.5, 0.5);
assert_near(position(&after, at(0.25)), -0.5, 0.05, "a quarter cycle after switching the trigger on");
let held = run_level(p.as_mut(), 0.5, 1.0);
assert!(p.set_param("retrigger", 0.0));
let again = run_level(p.as_mut(), 0.5, 0.5);
assert_near(position(&held, at(0.9)), 1.0, 0.02, "holds after one cycle");
assert_near(position(&again, at(0.25)), -0.5, 0.05, "switching the trigger off starts another cycle");
}
#[test]
fn auto_pan_envelope_trigger_waits_for_the_release() {
let mut p = auto_pan(&[
("trigger", 3.0),
("rate", 60.0),
("duration", 2.0),
("waveform", 3.0),
("threshold", -30.0),
("attack", 0.1),
("release", 100.0),
]);
let quiet = db_to_gain(-40.0);
let first = run_level(p.as_mut(), 1.0, 1.5);
assert_near(position(&first, at(1.4)), 1.0, 0.02, "the first cycle ends and holds");
// The envelope falls below the threshold about 0.38 s into the quiet part, so a 0.42 s dip
// leaves it below for less than the 100 ms Release.
run_level(p.as_mut(), quiet, 0.42);
let short = run_level(p.as_mut(), 1.0, 0.5);
assert_near(position(&short, at(0.25)), 1.0, 0.02, "no trigger before the release is over");
run_level(p.as_mut(), quiet, 1.0);
let long = run_level(p.as_mut(), 1.0, 0.5);
assert_near(position(&long, at(0.25)), -0.5, 0.05, "a trigger once the release is over");
}
#[test]
fn auto_pan_output_gain_and_mono_tracks() {
let mut p = auto_pan(&[("width", 0.0), ("output", 6.0)]);
let out = run_level(p.as_mut(), 0.5, 0.1);
assert_near(out[0][at(0.05)], 0.5 * db_to_gain(6.0), 1e-4, "+6 dB at the center");
// A mono insert has nothing to pan to; Output still applies.
let mut p = create("auto_pan").unwrap();
p.prepare(SR, BLOCK, 1);
assert!(p.set_param("output", -6.0));
p.reset();
let mut buf = vec![vec![0.5f32; BLOCK]];
p.process(&mut buf, BLOCK);
assert_near(buf[0][BLOCK - 1], 0.5 * db_to_gain(-6.0), 1e-4, "mono output");
}
#[test]
fn auto_pan_balances_a_stereo_signal_without_summing_it() {
// A sine on the left, the same sine inverted plus an offset on the right: the channels differ,
// and a mono sum would cancel the sine.
let len = at(0.2);
let left: Vec<f32> = (0..len).map(|i| 0.5 * (TAU * 440.0 * i as f32 / SR).sin()).collect();
let right: Vec<f32> = left.iter().map(|x| 0.25 - x).collect();
let run = |p: &mut dyn Plugin| {
let mut out = vec![Vec::new(), Vec::new()];
for start in (0..len).step_by(BLOCK) {
let end = (start + BLOCK).min(len);
let mut buf = vec![left[start..end].to_vec(), right[start..end].to_vec()];
p.process(&mut buf, end - start);
out[0].extend_from_slice(&buf[0]);
out[1].extend_from_slice(&buf[1]);
}
out
};
let mut p = auto_pan(&[("width", 0.0)]);
let out = run(p.as_mut());
for i in 0..len {
assert_near(out[0][i], left[i], 1e-5, "center passes the left channel");
assert_near(out[1][i], right[i], 1e-5, "center passes the right channel");
}
// Half left: each channel keeps its own content, the left louder and the right quieter.
let mut p = auto_pan(&[("width", 0.0), ("manual", -50.0)]);
let out = run(p.as_mut());
let theta = 0.5 * FRAC_PI_4;
let (gl, gr) = (2f32.sqrt() * theta.cos(), 2f32.sqrt() * theta.sin());
for i in at(0.05)..len {
assert_near(out[0][i], left[i] * gl, 1e-5, "half left, left channel");
assert_near(out[1][i], right[i] * gr, 1e-5, "half left, right channel");
}
// Anti-phase channels still drive the envelope follower to the right.
let mut p = auto_pan(&[("source", 1.0), ("threshold", -30.0)]);
let mut buf = vec![vec![1.0f32; BLOCK], vec![-1.0f32; BLOCK]];
for _ in 0..at(0.5) / BLOCK {
buf[0].fill(1.0);
buf[1].fill(-1.0);
p.process(&mut buf, BLOCK);
}
assert_near(buf[0][BLOCK - 1], 0.0, 1e-3, "anti-phase at full scale pans right: left");
assert_near(buf[1][BLOCK - 1], -(2f32.sqrt()), 1e-3, "anti-phase at full scale pans right: right");
}
+3
View File
@@ -46,6 +46,9 @@ proptest! {
let _ = offline::detect_transients(&ch, a, a);
let _ = offline::time_stretch(&ch, r, a);
let _ = offline::pitch_shift(&ch, a, a);
let change = if n % 2 == 0 { offline::SpeedRampChange::SpeedUp } else { offline::SpeedRampChange::SlowDown };
let selection = if n % 3 == 0 { offline::SpeedRampSelection::Extend } else { offline::SpeedRampSelection::FitTo };
let _ = offline::speed_ramp(&ch, change, selection, n % 5 == 0);
let _ = offline::resample(&ch, n as u32, (n as u32).wrapping_mul(3) % 200_000);
let mut m = ch.clone();
offline::normalize(&mut m, a, n % 2 == 0);
+2
View File
@@ -1,7 +1,9 @@
//! Integration tests, built as one test binary: each file is a module.
mod auto_pan;
mod fuzz;
mod offline_meters;
mod offline_normalize;
mod plugins;
mod speed_ramp;
mod transients;
+198
View File
@@ -513,6 +513,112 @@ fn saturator_adds_harmonics_and_stays_bounded() {
assert!(spec[third - 1..=third + 1].iter().cloned().fold(f32::MIN, f32::max) > -30.0);
}
/// Amplitude of the `freq` component of `x` (a whole number of cycles keeps it exact).
fn tone_level(x: &[f32], freq: f32) -> f32 {
let (mut re, mut im) = (0.0f64, 0.0f64);
for (i, v) in x.iter().enumerate() {
let w = f64::from(TAU * freq / SR) * i as f64;
re += f64::from(*v) * w.cos();
im += f64::from(*v) * w.sin();
}
(2.0 * (re * re + im * im).sqrt() / x.len() as f64) as f32
}
/// A Subharmonic with only the wet path and wide filters, so a band's octave reaches the output.
fn subharmonic_wet() -> Box<dyn Plugin> {
let mut p = prepared("subharmonic");
p.set_param("mix", 100.0);
p.set_param("hp_freq", 5.0);
p.set_param("lp_freq", 1000.0);
p
}
#[test]
fn subharmonic_adds_the_octave_below_each_range_band() {
for (range, lower, upper) in [(0.0, 120.0, 180.0), (1.0, 80.0, 120.0), (2.0, 60.0, 90.0), (3.0, 40.0, 60.0)] {
for (band, freq) in [("lower", lower), ("upper", upper)] {
let mut p = subharmonic_wet();
p.set_param("range", range);
p.set_param("lower_gain", if band == "lower" { 0.0 } else { -120.0 });
p.set_param("upper_gain", if band == "upper" { 0.0 } else { -120.0 });
let out = run(p.as_mut(), &[sine(freq, 0.5, 96_000)]);
// One second after the filters settle: a whole number of cycles of both tones.
let tail = &out[0][48_000..];
let sub = tone_level(tail, freq / 2.0);
assert!(sub > 0.2, "range {range} {band} band: {sub} at {} Hz", freq / 2.0);
assert!(sub > 4.0 * tone_level(tail, freq), "range {range} {band} band keeps its input tone");
}
}
}
#[test]
fn subharmonic_mix_keeps_one_path_at_full_level() {
// A 1 kHz tone is far above every band, so the output is the dry path alone.
for (mix, dry) in [(0.0, 1.0), (25.0, 1.0), (50.0, 1.0), (75.0, 0.5), (100.0, 0.0)] {
let mut p = prepared("subharmonic");
p.set_param("mix", mix);
let out = run(p.as_mut(), &[sine(1000.0, 0.5, 48_000)]);
let level = tone_level(&out[0][24_000..], 1000.0);
assert!((level - 0.5 * dry).abs() < 0.01, "mix {mix}: dry level {level}");
}
// The wet path is at full level from 50 % up and at half level at 25 %.
let wet = |mix: f32| {
let mut p = prepared("subharmonic");
p.set_param("mix", mix);
p.set_param("wet_solo", 1.0);
let out = run(p.as_mut(), &[sine(60.0, 0.5, 96_000)]);
tone_level(&out[0][48_000..], 30.0)
};
let full = wet(50.0);
assert!(full > 0.1, "wet level {full}");
assert!((wet(100.0) - full).abs() < 0.01 * full);
assert!((wet(25.0) - 0.5 * full).abs() < 0.01 * full);
}
#[test]
fn subharmonic_solo_plays_one_band_and_mutes_the_dry_signal() {
let level = |solo: &str, gain: f32| {
let mut p = prepared("subharmonic");
p.set_param("lower_gain", gain);
p.set_param(solo, 1.0);
let x = sine(60.0, 0.5, 96_000).iter().zip(sine(1000.0, 0.5, 96_000)).map(|(a, b)| a + b).collect();
let out = run(p.as_mut(), &[x]);
let tail = &out[0][48_000..];
(tone_level(tail, 30.0), tone_level(tail, 1000.0))
};
let (sub, dry) = level("lower_solo", 0.0);
assert!(sub > 0.1 && dry < 0.01, "lower solo: sub {sub}, dry {dry}");
// With the soloed band turned off nothing else plays, though the upper band is on.
let (sub, dry) = level("lower_solo", -120.0);
assert!(sub < 0.01 && dry < 0.01, "silent lower solo: sub {sub}, dry {dry}");
let (sub, dry) = level("upper_solo", -120.0);
assert!(sub > 0.05 && dry < 0.01, "upper solo: sub {sub}, dry {dry}");
}
#[test]
fn subharmonic_drive_keeps_every_character_bounded() {
let driven = |character: usize, q: f32| {
let mut p = subharmonic_wet();
p.set_param("input", 18.0);
p.set_param("lower_gain", 24.0);
p.set_param("upper_gain", 24.0);
p.set_param("drive", 18.0);
p.set_param("character", character as f32);
p.set_param("hp_q", q);
p.set_param("lp_q", q);
run(p.as_mut(), &[sine(60.0, 1.0, 48_000)])
};
for character in 0..6 {
let out = driven(character, 0.707);
let top = peak(&out[0][24_000..]);
assert!(top > 0.5 && top < 1.5, "character {character}: peak {top}");
// Q 5 filters after the saturator ring well above full scale, but never to infinity.
let out = driven(character, 5.0);
assert!(out[0].iter().all(|v| v.is_finite()), "character {character} at Q 5");
assert!(peak(&out[0][24_000..]) > 0.5, "character {character} at Q 5 is silent");
}
}
#[test]
fn lofi_quantizes() {
let mut p = prepared("lofi");
@@ -524,3 +630,95 @@ fn lofi_quantizes() {
levels.dedup();
assert!(levels.len() <= 5, "{levels:?}");
}
fn ring_mod(params: &[(&str, f32)]) -> Box<dyn Plugin> {
let mut p = prepared("ring_mod");
for &(id, v) in params {
assert!(p.set_param(id, v), "ring_mod.{id}");
}
p.reset();
p
}
/// Loudest bin within two bins of `hz` in an 8192-point spectrum.
fn band_db(spec: &[f32], hz: f32) -> f32 {
let bin = (hz / (SR / 8192.0)).round() as usize;
spec[bin - 2..=bin + 2].iter().cloned().fold(f32::MIN, f32::max)
}
fn zero_crossings(x: &[f32]) -> usize {
x.windows(2).filter(|w| (w[0] < 0.0) != (w[1] < 0.0)).count()
}
#[test]
fn ring_mod_makes_sum_and_difference_tones() {
let mut p = ring_mod(&[("effect", 0.0), ("frequency", 300.0), ("mod_depth", 0.0), ("mix", 100.0)]);
let out = run(p.as_mut(), &[sine(1000.0, 0.5, 48_000)]);
let spec = soundcraft_dsp::spectrum::magnitude_db(&out[0][8192..], 8192);
let (low, high, input) = (band_db(&spec, 700.0), band_db(&spec, 1300.0), band_db(&spec, 1000.0));
// The two tones fall at different places between bins, so their bin peaks differ a little.
assert!((low - high).abs() < 2.0, "700 Hz {low} dB, 1300 Hz {high} dB");
assert!(low - input > 40.0, "the 1000 Hz input is left at {input} dB against {low} dB");
}
#[test]
fn ring_mod_frequency_mod_adds_sidebands() {
let x = sine(1000.0, 0.5, 48_000);
let dry = soundcraft_dsp::spectrum::magnitude_db(&x[8192..], 8192);
assert!(band_db(&dry, 1000.0) - band_db(&dry, 1200.0) > 40.0);
let mut p = ring_mod(&[("effect", 1.0), ("frequency", 200.0), ("mix", 100.0)]);
let out = run(p.as_mut(), &[x]);
let spec = soundcraft_dsp::spectrum::magnitude_db(&out[0][8192..], 8192);
for hz in [800.0, 1200.0] {
assert!(band_db(&spec, 1000.0) - band_db(&spec, hz) < 20.0, "no sideband at {hz} Hz");
}
}
#[test]
fn ring_mod_resonators_ring_at_the_frequency_with_their_sign() {
// 480 Hz at 48 kHz is a 100-sample period.
for (effect, sign) in [(2.0, 1.0), (3.0, -1.0)] {
let mut p = ring_mod(&[("effect", effect), ("frequency", 480.0), ("resonance", 90.0), ("mix", 100.0)]);
let mut x = vec![0.0f32; 9600];
x[0] = 1.0;
let out = run(p.as_mut(), &[x]);
let y = &out[0];
assert!((y[0] - 0.1).abs() < 1e-4, "effect {effect}: direct {}", y[0]);
assert!((y[100] - sign * 0.09).abs() < 1e-4, "effect {effect}: first echo {}", y[100]);
assert!((y[200] - 0.081).abs() < 1e-4, "effect {effect}: second echo {}", y[200]);
assert!(y[50].abs() < 1e-6 && y[150].abs() < 1e-6, "effect {effect}: sound between the echoes");
let last = y.iter().rposition(|v| v.abs() > 0.1e-3).unwrap();
assert!(last < p.tail_samples(), "effect {effect}: ringing at {last}, tail {}", p.tail_samples());
}
}
#[test]
fn ring_mod_modulation_sources_move_the_frequency() {
// A constant input through Ring gives the carrier itself.
let dc = vec![1.0f32; 48_000];
// The envelope of a full-scale input lifts 100 Hz by one octave at 50 % depth.
let mut p = ring_mod(&[("effect", 0.0), ("frequency", 100.0), ("mod_source", 1.0), ("mod_depth", 50.0), ("mix", 100.0)]);
let out = run(p.as_mut(), std::slice::from_ref(&dc));
let n = zero_crossings(&out[0][24_000..]);
assert!((195..=205).contains(&n), "envelope: {n} crossings in 0.5 s, 200 expected for 200 Hz");
// A 1 Hz LFO raises the frequency for its first half cycle and lowers it for the second.
let mut p = ring_mod(&[("effect", 0.0), ("frequency", 100.0), ("mod_source", 0.0), ("mod_rate", 1.0), ("mod_depth", 100.0), ("mix", 100.0)]);
let out = run(p.as_mut(), std::slice::from_ref(&dc));
let (up, down) = (zero_crossings(&out[0][..12_000]), zero_crossings(&out[0][24_000..36_000]));
assert!(up > 3 * down, "LFO: {up} crossings rising, {down} falling");
// Sample and hold at 4 Hz keeps one frequency for each 12,000-sample step.
let mut p = ring_mod(&[("effect", 0.0), ("frequency", 100.0), ("mod_source", 2.0), ("mod_rate", 4.0), ("mod_depth", 100.0), ("mix", 100.0)]);
let out = run(p.as_mut(), std::slice::from_ref(&dc));
let mut steps = Vec::new();
for s in out[0].chunks(12_000) {
let (a, b) = (zero_crossings(&s[..6000]), zero_crossings(&s[6000..]));
assert!(a.abs_diff(b) <= 2, "sample and hold changed within a step: {a} then {b}");
steps.push(a);
}
steps.sort_unstable();
steps.dedup_by(|a, b| a.abs_diff(*b) <= 2);
assert!(steps.len() >= 3, "sample and hold steps: {steps:?}");
}
+119
View File
@@ -0,0 +1,119 @@
use soundcraft_dsp::offline::{self, SpeedRampChange, SpeedRampSelection};
/// A channel whose samples rise from 0 to 1, so each output sample shows where it was read.
fn ramp(len: usize) -> Vec<f32> {
(0..len).map(|i| i as f32 / len as f32).collect()
}
/// The fraction of the played input reached at `u` (0 to 1) of the result.
fn slow_down_pos(u: f64) -> f64 {
(3.0 * u - u * u * u) / 2.0
}
fn speed_up_pos(u: f64) -> f64 {
1.0 - slow_down_pos(1.0 - u)
}
fn near(got: f32, want: f64, what: &str) {
assert!((f64::from(got) - want).abs() < 2e-3, "{what}: got {got}, want {want}");
}
#[test]
fn speed_ramp_fit_to_keeps_the_length_and_plays_two_thirds() {
let len = 30_000;
let x = vec![ramp(len)];
let slow = offline::speed_ramp(&x, SpeedRampChange::SlowDown, SpeedRampSelection::FitTo, false);
assert_eq!(slow[0].len(), len);
// Slow Down plays the first two thirds: it starts at normal speed at the start and stops at the end.
for u in [0.0, 0.25, 0.5, 0.75, 0.9] {
let n = (u * len as f64) as usize;
near(slow[0][n], 2.0 / 3.0 * slow_down_pos(u), &format!("slow down at {u}"));
}
// The speed over 50 samples: a one-sample step inside the input also shows the interpolation's ripple.
let step = |a: &[f32], n: usize| f64::from(a[n + 50] - a[n]) * len as f64 / 50.0;
assert!((step(&slow[0], 100) - 1.0).abs() < 0.01, "slow down starts at normal speed: {}", step(&slow[0], 100));
assert!(step(&slow[0], len - 200) < 0.02, "slow down ends near a stop: {}", step(&slow[0], len - 200));
// Speed Up plays the last two thirds: it starts from a stop and reaches normal speed at the end.
let fast = offline::speed_ramp(&x, SpeedRampChange::SpeedUp, SpeedRampSelection::FitTo, false);
assert_eq!(fast[0].len(), len);
for u in [0.1, 0.25, 0.5, 0.75, 0.99] {
let n = (u * len as f64) as usize;
near(fast[0][n], 1.0 / 3.0 + 2.0 / 3.0 * speed_up_pos(u), &format!("speed up at {u}"));
}
assert!(step(&fast[0], 200) < 0.02, "speed up starts near a stop: {}", step(&fast[0], 200));
assert!((step(&fast[0], len - 100) - 1.0).abs() < 0.01, "speed up ends at normal speed: {}", step(&fast[0], len - 100));
}
#[test]
fn speed_ramp_extend_plays_everything_over_150_percent() {
let len = 30_000;
let x = vec![ramp(len), ramp(len)];
for change in [SpeedRampChange::SlowDown, SpeedRampChange::SpeedUp] {
let out = offline::speed_ramp(&x, change, SpeedRampSelection::Extend, false);
assert_eq!(out.len(), 2);
assert_eq!(out[0].len(), 45_000, "{change:?}");
assert_eq!(out[0], out[1], "{change:?}: both channels are read at the same positions");
for u in [0.1, 0.25, 0.5, 0.75, 0.97] {
let n = (u * 45_000.0) as usize;
let pos = if change == SpeedRampChange::SlowDown { slow_down_pos(u) } else { speed_up_pos(u) };
near(out[0][n], pos, &format!("{change:?} at {u}"));
}
}
}
#[test]
fn speed_ramp_fades_follow_the_speed() {
let len = 30_000;
let x = vec![vec![0.5; len]];
let slow = offline::speed_ramp(&x, SpeedRampChange::SlowDown, SpeedRampSelection::FitTo, true);
let fast = offline::speed_ramp(&x, SpeedRampChange::SpeedUp, SpeedRampSelection::FitTo, true);
for u in [0.25, 0.5, 0.75] {
let n = (u * len as f64) as usize;
near(slow[0][n], 0.5 * (1.0 - u * u), &format!("slow down fades out at {u}"));
near(fast[0][n], 0.5 * (1.0 - (1.0 - u) * (1.0 - u)), &format!("speed up fades in at {u}"));
}
assert!(slow[0][len - 1].abs() < 1e-3 && fast[0][0].abs() < 1e-3, "the fades reach silence at the stop");
// Fades Off keeps the level.
let off = offline::speed_ramp(&x, SpeedRampChange::SlowDown, SpeedRampSelection::FitTo, false);
for u in [0.25, 0.5, 0.75, 0.9] {
near(off[0][(u * len as f64) as usize], 0.5, &format!("slow down without fades at {u}"));
}
// ... at the stop too, where the reads reach either end of the input.
let up = offline::speed_ramp(&x, SpeedRampChange::SpeedUp, SpeedRampSelection::FitTo, false);
near(up[0][0], 0.5, "speed up without fades from the stop");
near(off[0][len - 1], 0.5, "slow down without fades at the stop");
let off = offline::speed_ramp(&x, SpeedRampChange::SlowDown, SpeedRampSelection::Extend, false);
near(off[0][off[0].len() - 1], 0.5, "slow down with Extend and without fades at the stop");
}
#[test]
fn speed_ramp_pitch_falls_as_the_tape_slows() {
let sr = 48_000.0;
let len = 96_000;
let x = vec![(0..len).map(|i| (2.0 * std::f32::consts::PI * 1000.0 * i as f32 / sr).sin() * 0.5).collect::<Vec<f32>>()];
let out = offline::speed_ramp(&x, SpeedRampChange::SlowDown, SpeedRampSelection::Extend, false);
// Upward zero crossings per second in a 0.1 s window starting at `u`.
let rate = |u: f64| {
let a = (u * out[0].len() as f64) as usize;
let w = &out[0][a..a + 4800];
w.windows(2).filter(|p| p[0] < 0.0 && p[1] >= 0.0).count() as f64 * 10.0
};
assert!((rate(0.0) - 1000.0).abs() < 20.0, "starts at the original pitch: {}", rate(0.0));
let half = 1000.0 * 0.75;
assert!((rate(0.5) - half).abs() < 30.0, "three quarters speed halfway: {}", rate(0.5));
assert!(rate(0.9) < 250.0, "low near the stop: {}", rate(0.9));
}
#[test]
fn speed_ramp_handles_empty_and_uneven_channels() {
assert!(offline::speed_ramp(&[], SpeedRampChange::SlowDown, SpeedRampSelection::Extend, true).is_empty());
let out = offline::speed_ramp(&[vec![], vec![]], SpeedRampChange::SpeedUp, SpeedRampSelection::FitTo, true);
assert!(out.iter().all(Vec::is_empty));
// A shorter channel is read as silence past its end, and every channel gets the same length.
let out = offline::speed_ramp(&[vec![0.5; 300], vec![0.5; 30]], SpeedRampChange::SlowDown, SpeedRampSelection::Extend, false);
assert_eq!((out[0].len(), out[1].len()), (450, 450));
assert!(out.iter().flatten().all(|v| v.is_finite()));
near(out[0][449], 0.5, "the longer channel at the stop");
assert!(out[1][200..].iter().all(|v| v.abs() < 1e-3), "the shorter channel is silent past its end");
}
+1
View File
@@ -186,6 +186,7 @@ impl Autosave {
let mut n = 1;
loop {
// Not recursive, so a folder that already exists is reported and the next name tried.
#[cfg_attr(not(unix), allow(unused_mut))]
let mut builder = std::fs::DirBuilder::new();
#[cfg(unix)]
std::os::unix::fs::DirBuilderExt::mode(&mut builder, 0o700);
+17
View File
@@ -176,6 +176,8 @@ pub fn import(e: &mut Engine, path: &str, first: Option<TrackId>, at: Samples) -
c.sync_point = scale(c.sync_point);
c.gain_env.iter_mut().for_each(|(at, _)| *at = scale(*at));
c.group = Some(group_id);
// The file does not hold the Auto Rename schemes of the session it came from.
c.auto_name = None;
c.sanitize();
placed.push(c.id);
prepared.push(c);
@@ -231,6 +233,21 @@ mod tests {
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
fn imported_clips_carry_no_auto_rename_scheme() {
let dir = std::env::temp_dir().join(format!("sc-grp-names-{}", std::process::id()));
let _ = std::fs::create_dir_all(&dir);
let path = dir.join("named.scgrp").to_string_lossy().to_string();
let mut e = crate::demo::demo_engine();
let kick = e.session().track_by_name("Kick").unwrap().clips()[0].id.0;
e.execute("clip.auto_rename", &json!({"clips": [kick], "name": "Hit"})).unwrap();
e.execute("file.export_clip_groups", &json!({"path": path, "clips": [kick]})).unwrap();
let r = e.execute("file.import_clip_groups", &json!({"path": path, "at": 30_000_000, "track": "Bass"})).unwrap();
let placed = soundcraft_model::ClipId(r["clips"][0].as_u64().unwrap());
assert_eq!(e.session().find_clip(placed).unwrap().1.auto_name, None);
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
fn positions_in_a_group_file_are_kept_within_reach() {
let dir = std::env::temp_dir().join(format!("sc-grp-far-{}", std::process::id()));
+50 -2
View File
@@ -5,6 +5,7 @@ use soundcraft_time::Range;
use crate::{Engine, Result, cmd};
use serde_json::{Value, json};
use soundcraft_audio_io::{AudioBuffer, FileFormat};
use soundcraft_dsp::offline::{SpeedRampChange, SpeedRampSelection};
use soundcraft_model::{Clip, ClipContent, TrackKind};
pub fn specs() -> Vec<CommandSpec> {
@@ -13,7 +14,7 @@ pub fn specs() -> Vec<CommandSpec> {
"AudioSuite Process",
[],
None,
"{process: <plugin id> | normalize | reverse | invert | gain | duplicate | time_stretch | pitch_shift | varispeed | signal_generator, params?: {..}, clips?, tracks?, start?, end?} — signal_generator fills start..end (default: the selection) with a new clip on each audio track in `tracks` (default: the selected tracks)",
"{process: <plugin id> | normalize | reverse | invert | gain | duplicate | time_stretch | pitch_shift | varispeed | speed_ramp | signal_generator, params?: {..}, clips?, tracks?, start?, end?} — signal_generator fills start..end (default: the selection) with a new clip on each audio track in `tracks` (default: the selected tracks); speed_ramp takes change (0 slow down, 1 speed up), selection (0 fit to, 1 extend) and fades (1 on, 0 off)",
has_selection,
process
)]
@@ -31,7 +32,16 @@ fn process(e: &mut Engine, p: &Value) -> Result<Value> {
let known = soundcraft_dsp::plugin_info(&name).is_some()
|| matches!(
name.as_str(),
"normalize" | "reverse" | "invert" | "gain" | "duplicate" | "time_stretch" | "pitch_shift" | "varispeed" | "dc_offset_removal"
"normalize"
| "reverse"
| "invert"
| "gain"
| "duplicate"
| "time_stretch"
| "pitch_shift"
| "varispeed"
| "speed_ramp"
| "dc_offset_removal"
);
if !known {
return Err(bad("audiosuite.process", format!("unknown process `{name}`")));
@@ -50,12 +60,16 @@ fn process(e: &mut Engine, p: &Value) -> Result<Value> {
let ratio = f64::from(get("ratio", 1.0).clamp(0.25, 4.0));
let semitones = get("semitones", 0.0).clamp(-24.0, 24.0);
let to = (sr.as_f64() / f64::from(get("speed", 1.0).clamp(0.25, 4.0))).round().max(1.0) as u32;
let change = if get("change", 0.0) >= 0.5 { SpeedRampChange::SpeedUp } else { SpeedRampChange::SlowDown };
let selection = if get("selection", 0.0) >= 0.5 { SpeedRampSelection::Extend } else { SpeedRampSelection::FitTo };
let fades = get("fades", 1.0) >= 0.5;
// These processes cut their output at MAX_OFFLINE_SAMPLES frames.
let longest = |grows: f64| (soundcraft_dsp::offline::MAX_OFFLINE_SAMPLES as f64 / grows) as usize;
let most = match name.as_str() {
"time_stretch" => longest(ratio),
"pitch_shift" => longest(2f64.powf(f64::from(semitones) / 12.0)),
"varispeed" => longest(f64::from(to) / sr.as_f64()),
"speed_ramp" => longest(if selection == SpeedRampSelection::Extend { 1.5 } else { 1.0 }),
_ => usize::MAX,
};
let mut done = 0;
@@ -101,6 +115,7 @@ fn process(e: &mut Engine, p: &Value) -> Result<Value> {
"time_stretch" => soundcraft_dsp::offline::time_stretch(&audio, ratio, srf),
"pitch_shift" => soundcraft_dsp::offline::pitch_shift(&audio, semitones, srf),
"varispeed" => soundcraft_dsp::offline::resample(&audio, sr.hz(), to),
"speed_ramp" => soundcraft_dsp::offline::speed_ramp(&audio, change, selection, fades),
plugin => {
apply(plugin, &params, &mut audio, srf)?;
audio
@@ -242,6 +257,39 @@ mod tests {
}
}
#[test]
fn speed_ramp_ramps_the_speed_and_extend_lengthens_the_clip() {
// A mono clip at 480 whose samples rise from 0 to 1, so each output sample shows where it was read.
let run = |params: Value| {
let mut s = Session::default();
let t = s.add_track(TrackKind::Audio, ChannelFormat::Mono, Some("Ramp"));
let buf = AudioBuffer { sample_rate: 48_000, channels: vec![(0..4800).map(|i| i as f32 / 4800.0).collect()] };
let src = crate::io::add_source(&mut s, "ramp", buf, None, FileFormat::Wav);
let id = s.new_clip_id();
crate::edit::place_clip(&mut s, t, Clip::audio(id, "ramp", src, 0, 480, 4800));
let mut e = crate::Engine::new(s);
e.execute("audiosuite.process", &json!({"process": "speed_ramp", "params": params, "clips": [id.0]})).unwrap();
let c = e.session().tracks[0].clips()[0].clone();
let audio = e.session().pool.get(c.source().unwrap()).unwrap().buffer.channels[0].clone();
(c.start, c.length, c.name, audio)
};
// The defaults: Slow Down, Fit To, Fades On. Halfway through, the read position is 11/16 of the
// way through the first two thirds, and the gain is the speed there, 3/4.
let (start, length, name, audio) = run(json!({}));
assert_eq!((start, length, name.as_str()), (480, 4800, "ramp-speeramp"));
let want = (2.0 / 3.0 * 11.0 / 16.0) * 0.75;
assert!((audio[2400] - want).abs() < 2e-3, "slow down halfway: got {}, want {want}", audio[2400]);
assert!(audio[4790].abs() < 0.01, "the fade-out ends near silence, got {}", audio[4790]);
// Speed Up with Extend and no fades: all of the clip is played over 150 % of its length, and the
// clip still starts at 480.
let (start, length, _, audio) = run(json!({"change": 1, "selection": 1, "fades": 0}));
assert_eq!((start, length), (480, 7200));
assert!((audio[3600] - 5.0 / 16.0).abs() < 2e-3, "speed up halfway: got {}", audio[3600]);
let r = 1.0 - 7100.0 / 7200.0;
let want = 1.0 - (3.0 * r - r * r * r) / 2.0;
assert!((audio[7100] - want).abs() < 2e-3, "speed up near the end: got {}, want {want}", audio[7100]);
}
#[test]
fn signal_generator_fills_the_selection_with_a_clip() {
let (mut e, _) = gained_clip();
+249
View File
@@ -0,0 +1,249 @@
//! Clip List › Auto Rename: the names that editing gives the clips it separates from a clip.
use super::{CommandSpec, bad};
use crate::{Engine, Result, cmd};
use serde::Deserialize;
use serde_json::{Value, json};
use soundcraft_model::ClipId;
use std::collections::BTreeSet;
const AUTO_RENAME: &str = "clip.auto_rename";
pub fn specs() -> Vec<CommandSpec> {
vec![cmd!(
"clip.auto_rename",
"Auto Rename...",
[],
None,
"{clips?: [id], name?, number?: 0..999999999, zeros?: 0..9, suffix?}: sets the names that editing gives the clips it separates from these clips (default: the Clip List's selected clips): the piece after each cut is named `name`, then `zeros` zeros, then a number counting up from `number` (default 1), then `suffix`, in one number sequence for all of them. Without a name, the clips keep their names when they are separated",
has_clip_selection,
auto_rename
)]
}
fn has_clip_selection(e: &Engine) -> std::result::Result<(), String> {
if e.session().edit.selected_clips.is_empty() { Err("select a clip in the Clip List".into()) } else { Ok(()) }
}
fn auto_rename(e: &mut Engine, p: &Value) -> Result<Value> {
let s = e.session();
let clips: BTreeSet<ClipId> = match p.get("clips") {
None => s.edit.selected_clips.iter().copied().collect(),
Some(v) => BTreeSet::deserialize(v).map_err(|_| bad(AUTO_RENAME, "`clips` must be an array of ids"))?,
};
if clips.is_empty() {
return Err(bad(AUTO_RENAME, "select a clip in the Clip List, or pass `clips`"));
}
let known: BTreeSet<ClipId> = s.all_clips().map(|c| c.id).collect();
if let Some(c) = clips.iter().find(|c| !known.contains(c)) {
return Err(bad(AUTO_RENAME, format!("no clip with id {c}")));
}
let names = super::edit::strip_names(AUTO_RENAME, p)?;
let s = e.session_mut();
let key = names.map(|n| {
let (key, id) = (s.alloc(), crate::new_scheme_id(s));
s.auto_names.insert(key, soundcraft_model::AutoName { id, ..n.into() });
key
});
for c in s.tracks.iter_mut().flat_map(|t| t.playlists.iter_mut()).flat_map(|pl| pl.clips.iter_mut()) {
if clips.contains(&c.id) {
c.auto_name = key;
}
}
// Earlier schemes stay while the session is open: copies on the clipboard can still use them.
Ok(json!({"clips": clips.len(), "named": key.is_some()}))
}
#[cfg(test)]
mod tests {
use super::*;
/// The first Kick clip: its track, id, start and length.
fn first_kick(e: &Engine) -> (soundcraft_model::TrackId, ClipId, i64, i64) {
let s = e.session();
let t = s.track_by_name("Kick").unwrap();
let c = &t.clips()[0];
(t.id, c.id, c.start, c.length)
}
/// The names of the clips on `t` that lie inside `[start, end)`, in time order.
fn names_in(e: &Engine, t: soundcraft_model::TrackId, start: i64, end: i64) -> Vec<String> {
e.session().track(t).unwrap().clips().iter().filter(|c| c.start >= start && c.end() <= end).map(|c| c.name.clone()).collect()
}
#[test]
fn separating_an_auto_renamed_clip_names_each_new_piece() {
let mut e = crate::demo::demo_engine();
let (t, id, start, len) = first_kick(&e);
let old = e.session().find_clip(id).unwrap().1.name.clone();
let r = e.execute("clip.auto_rename", &json!({"clips": [id], "name": "SFX", "number": 23, "zeros": 1, "suffix": ".Reel1"})).unwrap();
assert_eq!(r, json!({"clips": 1, "named": true}));
assert_eq!(e.undo_label(), Some("Auto Rename..."));
assert_eq!(names_in(&e, t, start, start + len), [old.as_str()], "setting the scheme leaves the clip's name");
// Cut the middle, then each half: every piece after a cut is a new clip with the next name.
for at in [start + len / 2, start + len / 4, start + len * 3 / 4] {
e.execute("edit.separate", &json!({"tracks": [t], "at": at})).unwrap();
}
assert_eq!(names_in(&e, t, start, start + len), [old.as_str(), "SFX024.Reel1", "SFX023.Reel1", "SFX025.Reel1"]);
assert!(e.undo());
assert_eq!(names_in(&e, t, start, start + len).len(), 3);
assert_eq!(e.session().auto_names.values().next().map(|a| a.number), Some(25), "undo takes the number back");
}
#[test]
fn auto_rename_uses_the_clip_list_selection_and_one_number_sequence() {
let mut e = crate::demo::demo_engine();
let s = e.session();
let t = s.track_by_name("Kick").unwrap();
let (track, a, b) = (t.id, t.clips()[0].clone(), t.clips()[1].clone());
e.execute("edit.select", &json!({"clips": [a.id, b.id]})).unwrap();
e.execute("clip.auto_rename", &json!({"name": "Hit"})).unwrap();
e.execute("edit.separate", &json!({"tracks": [track], "at": b.start + b.length / 2})).unwrap();
e.execute("edit.separate", &json!({"tracks": [track], "at": a.start + a.length / 2})).unwrap();
assert_eq!(names_in(&e, track, b.start, b.end())[1], "Hit1");
assert_eq!(names_in(&e, track, a.start, a.end())[1], "Hit2", "both clips count up one sequence");
assert_eq!(e.session().auto_names.len(), 1);
}
#[test]
fn auto_rename_without_a_name_removes_the_scheme() {
let mut e = crate::demo::demo_engine();
let (t, id, start, len) = first_kick(&e);
let old = e.session().find_clip(id).unwrap().1.name.clone();
e.execute("clip.auto_rename", &json!({"clips": [id], "name": "SFX"})).unwrap();
let r = e.execute("clip.auto_rename", &json!({"clips": [id], "name": " "})).unwrap();
assert_eq!(r["named"], false);
assert_eq!(e.session().find_clip(id).unwrap().1.auto_name, None);
e.execute("edit.separate", &json!({"tracks": [t], "at": start + len / 2})).unwrap();
assert_eq!(names_in(&e, t, start, start + len), [old.clone(), old]);
}
#[test]
fn a_pasted_copy_keeps_the_scheme_it_was_copied_with() {
let mut e = crate::demo::demo_engine();
let (t, id, start, len) = first_kick(&e);
let old = e.session().find_clip(id).unwrap().1.name.clone();
e.execute("clip.auto_rename", &json!({"clips": [id], "name": "SFX"})).unwrap();
e.execute("edit.copy", &json!({"tracks": [t], "start": start, "end": start + len})).unwrap();
e.execute("clip.auto_rename", &json!({"clips": [id]})).unwrap();
let at = e.session().content_end() + 48_000;
e.execute("edit.paste", &json!({"tracks": [t], "at": at})).unwrap();
e.execute("edit.separate", &json!({"tracks": [t], "at": at + len / 2})).unwrap();
assert_eq!(names_in(&e, t, at, at + len)[1], "SFX1", "the copy's scheme outlives the original clip's");
// Pasted into another session, the copy brings its scheme along and counts on from it.
e.execute("edit.copy", &json!({"tracks": [t], "start": at, "end": at + len})).unwrap();
e.replace_session(crate::demo::demo_engine().session().clone());
let t = first_kick(&e).0;
e.execute("edit.paste", &json!({"tracks": [t], "at": at})).unwrap();
e.execute("edit.separate", &json!({"tracks": [t], "at": at + len / 4})).unwrap();
assert_eq!(names_in(&e, t, at, at + len), [old.as_str(), "SFX2", "SFX1"]);
}
#[test]
fn a_copy_keeps_its_scheme_when_undo_hands_the_key_to_another() {
let mut e = crate::demo::demo_engine();
let (t, id, start, len) = first_kick(&e);
let bass = e.session().track_by_name("Bass").unwrap().clips()[0].id;
e.execute("clip.auto_rename", &json!({"clips": [id], "name": "SFX"})).unwrap();
e.execute("edit.copy", &json!({"tracks": [t], "start": start, "end": start + len})).unwrap();
assert!(e.undo());
// The same name under the key the undo freed is still another scheme.
e.execute("clip.auto_rename", &json!({"clips": [bass], "name": "SFX", "number": 100})).unwrap();
let at = e.session().content_end() + 48_000;
e.execute("edit.paste", &json!({"tracks": [t], "at": at})).unwrap();
e.execute("edit.separate", &json!({"tracks": [t], "at": at + len / 2})).unwrap();
assert_eq!(names_in(&e, t, at, at + len)[1], "SFX1");
let s = e.session();
assert_eq!(s.auto_names.len(), 2);
let key = s.find_clip(bass).unwrap().1.auto_name.unwrap();
assert_eq!(s.auto_names[&key].number, 100, "the other scheme's count is untouched");
}
#[test]
fn a_copy_keeps_its_scheme_when_another_session_holds_its_id_with_other_text() {
let mut e = crate::demo::demo_engine();
let (t, id, start, len) = first_kick(&e);
e.execute("clip.auto_rename", &json!({"clips": [id], "name": "SFX"})).unwrap();
let copied = e.session().auto_names.values().next().unwrap().id;
e.execute("edit.copy", &json!({"tracks": [t], "start": start, "end": start + len})).unwrap();
// Another session holds a scheme under the same id with another name.
e.replace_session(crate::demo::demo_engine().session().clone());
let bass = e.session().track_by_name("Bass").unwrap().clips()[0].id;
let s = e.session_mut();
let key = s.alloc();
s.auto_names.insert(key, soundcraft_model::AutoName { name: "Other".into(), number: 100, zeros: 0, suffix: String::new(), id: copied });
s.tracks
.iter_mut()
.flat_map(|t| t.playlists.iter_mut())
.flat_map(|pl| pl.clips.iter_mut())
.filter(|c| c.id == bass)
.for_each(|c| c.auto_name = Some(key));
let t = first_kick(&e).0;
let at = e.session().content_end() + 48_000;
e.execute("edit.paste", &json!({"tracks": [t], "at": at})).unwrap();
e.execute("edit.separate", &json!({"tracks": [t], "at": at + len / 2})).unwrap();
assert_eq!(names_in(&e, t, at, at + len)[1], "SFX1");
let s = e.session();
assert_eq!(s.auto_names.len(), 2);
assert_eq!(s.auto_names[&key].number, 100, "the other scheme's count is untouched");
}
#[test]
fn a_refused_paste_leaves_the_clipboard_as_it_was() {
let mut e = crate::demo::demo_engine();
let (t, id, start, len) = first_kick(&e);
e.execute("clip.auto_rename", &json!({"clips": [id], "name": "SFX"})).unwrap();
e.execute("edit.copy", &json!({"tracks": [t], "start": start, "end": start + len})).unwrap();
// Without its scheme in the session, a paste puts the scheme back under a new key.
e.session_mut().auto_names.clear();
let copied = e.clipboard.clone();
// A time-locked clip at the paste point leaves Shuffle no room.
e.session_mut().find_clip_mut(id).unwrap().time_locked = true;
e.session_mut().edit.edit_mode = soundcraft_model::EditMode::Shuffle;
assert!(e.execute("edit.paste", &json!({"tracks": [t], "at": start})).is_err());
assert_eq!(e.clipboard, copied);
e.session_mut().edit.edit_mode = soundcraft_model::EditMode::Slip;
let at = e.session().content_end() + 48_000;
e.execute("edit.paste", &json!({"tracks": [t], "at": at})).unwrap();
assert_ne!(e.clipboard, copied, "a paste that succeeds keeps the scheme's new key");
}
#[test]
fn bad_auto_rename_params_are_refused() {
let mut e = crate::demo::demo_engine();
let (_, id, _, _) = first_kick(&e);
for p in [
json!({"name": "SFX"}),
json!({"clips": [], "name": "SFX"}),
json!({"clips": [999_999], "name": "SFX"}),
json!({"clips": "Kick", "name": "SFX"}),
json!({"clips": [id], "name": 5}),
json!({"clips": [id], "name": "SFX", "zeros": 10}),
json!({"clips": [id], "name": "SFX", "number": 1_000_000_000}),
json!({"clips": [id], "number": 3}),
] {
assert!(e.execute("clip.auto_rename", &p).is_err(), "{p}");
}
assert!(e.session().auto_names.is_empty());
assert!(e.session().find_clip(id).unwrap().1.auto_name.is_none());
}
#[test]
fn auto_rename_schemes_are_saved_and_repaired_on_open() {
let mut e = crate::demo::demo_engine();
let (t, id, start, len) = first_kick(&e);
e.execute("clip.auto_rename", &json!({"clips": [id], "name": "SFX", "number": 7})).unwrap();
let mut v: Value = serde_json::from_str(&e.session().to_json().unwrap()).unwrap();
let key = *e.session().auto_names.keys().next().unwrap();
v["auto_names"][key.to_string()]["zeros"] = json!(usize::MAX);
v["auto_names"]["999999999"] = json!({"name": "Unused", "number": 1, "zeros": 0, "suffix": ""});
let mut s = soundcraft_model::Session::from_json(&v.to_string()).unwrap();
assert_eq!(s.auto_names.len(), 1, "a scheme no clip uses is dropped");
assert_eq!(s.auto_names[&key].zeros, soundcraft_model::AutoName::MAX_ZEROS);
assert!(s.next_id > key);
crate::edit::separate_at(&mut s, t, start + len / 2);
let names: Vec<String> =
s.track(t).unwrap().clips().iter().filter(|c| c.start >= start && c.end() <= start + len).map(|c| c.name.clone()).collect();
assert_eq!(names[1], "SFX0000000007");
}
}
+47 -3
View File
@@ -67,7 +67,15 @@ pub fn specs() -> Vec<CommandSpec> {
}
Ok(json!({}))
}),
cmd!("clip.loop", "Loop...", ["Clip"], Some("Cmd+Alt+L"), "{clips?, count?: n, length?: samples}", has_selection, loop_clips),
cmd!(
"clip.loop",
"Loop...",
["Clip"],
Some("Cmd+Alt+L"),
"{clips?, count?: n, length?: samples, fill?: bool}: count is the clip plus its loops; length is the whole looped clip, with the last loop cut short to fit; fill loops up to the next clip on the track, or else to the session end",
has_selection,
loop_clips
),
cmd!("clip.rename", "Rename...", ["Clip"], Some("Cmd+Alt+Shift+R"), "{clip?, name}", has_selection, rename),
cmd!("clip.gain", "Clip Gain", [], None, "{clips?, db | delta_db}", has_selection, gain),
cmd!("clip.gain_nudge_up", "Nudge Clip Gain Up", [], Some("Ctrl+Shift+Up"), "{clips?}", has_selection, |e, p| nudge_gain(e, p, 1.0)),
@@ -287,15 +295,46 @@ fn layer(e: &mut Engine, p: &Value, front: bool) -> Result<Value> {
Ok(json!({}))
}
/// The most loops `clip.loop` makes of one clip.
const MAX_LOOPS: i64 = 1000;
fn loop_clips(e: &mut Engine, p: &Value) -> Result<Value> {
let ids = clip_ids_param(e, p);
let count = i64_or(p, "count", 4).clamp(1, 512);
let total = position_param(e, "clip.loop", p, "length")?;
let fill = p.get("fill").and_then(Value::as_bool).unwrap_or(false);
let s = e.session_mut();
let session_end = s.content_end();
let mut made = 0;
for id in ids {
let Some((tid, mut c)) = s.find_clip(id).map(|(t, c)| (t, c.clone())) else { continue };
let reps = total.map_or(count - 1, |l| (l / c.length.max(1)).max(1) - 1).clamp(0, 1000);
if c.length <= 0 {
continue;
}
let len = c.length;
// Where the looped clip ends: the next clip or the session end, or `length` after the start.
let end = if fill {
let next = s.track(tid).and_then(|t| t.clips().iter().filter(|x| x.id != c.id && x.start >= c.end()).map(|x| x.start).min());
Some(next.unwrap_or(session_end))
} else {
total.map(|l| c.start.saturating_add(l))
};
// Whole iterations, then one cut short to reach `end` exactly.
let (reps, last) = match end {
Some(end) => {
let span = end.saturating_sub(c.end()).max(0);
let (whole, rest) = (span / len, span % len);
let reps = whole.saturating_add(i64::from(rest > 0));
if reps > MAX_LOOPS {
return Err(bad(
"clip.loop",
format!("“{}” would need {reps} loops to fill that; a clip loops at most {MAX_LOOPS} times", c.name),
));
}
(reps, if rest > 0 { rest } else { len })
}
None => ((count - 1).clamp(0, MAX_LOOPS), len),
};
if reps > 0 && c.loop_id.is_none() {
let lid = s.alloc();
if let Some(src) = s.find_clip_mut(id) {
@@ -307,10 +346,15 @@ fn loop_clips(e: &mut Engine, p: &Value) -> Result<Value> {
for k in 1..=reps {
let mut nc = c.clone();
nc.start = 0;
if k == reps && last < c.length {
// `start` is 0 here, so the end is the length; the trim also shortens the fades to fit and removes a sync point past the cut.
nc.trim_end_to(last);
}
copies.push((c.start + c.length * k, nc));
}
for (at, nc) in copies {
let _ = crate::edit::paste(s, tid, at, &[nc], c.length, false);
let length = nc.length;
let _ = crate::edit::paste(s, tid, at, &[nc], length, false);
made += 1;
}
}
+125 -11
View File
@@ -99,24 +99,40 @@ fn regroup(e: &mut Engine, p: &Value) -> Result<Value> {
Ok(json!({"group": g, "clips": n}))
}
fn same_material(a: &Clip, b: &Clip) -> bool {
a.length == b.length
&& (a.loop_id.is_some() && a.loop_id == b.loop_id
|| match (&a.content, &b.content) {
(ClipContent::Audio { source: s1, offset: o1 }, ClipContent::Audio { source: s2, offset: o2 }) => s1 == s2 && o1 == o2,
(ClipContent::Midi { sequence: q1 }, ClipContent::Midi { sequence: q2 }) => q1 == q2,
(ClipContent::Video { source: s1, offset: o1 }, ClipContent::Video { source: s2, offset: o2 }) => s1 == s2 && o1 == o2,
_ => false,
})
fn same_content(a: &Clip, b: &Clip) -> bool {
match (&a.content, &b.content) {
(ClipContent::Audio { source: s1, offset: o1 }, ClipContent::Audio { source: s2, offset: o2 }) => s1 == s2 && o1 == o2,
(ClipContent::Midi { sequence: q1 }, ClipContent::Midi { sequence: q2 }) => q1 == q2,
(ClipContent::Video { source: s1, offset: o1 }, ClipContent::Video { source: s2, offset: o2 }) => s1 == s2 && o1 == o2,
_ => false,
}
}
/// The loop a clip belongs to: back-to-back copies of the same material. Origin first.
fn same_material(a: &Clip, b: &Clip) -> bool {
a.length == b.length && (a.loop_id.is_some() && a.loop_id == b.loop_id || same_content(a, b))
}
/// Whether `short` is the last iteration of `full`'s loop, cut short at its end. It shares the loop,
/// it is shorter, and no clip of the loop starts where it ends, as the other half of a split clip
/// does. When `full` is a whole copy (an iteration of the same length ends where it starts), that is
/// enough. When `full` is the loop's origin alone, `short` also has to hold `full`'s material.
fn cut_short(clips: &[Clip], full: &Clip, short: &Clip) -> bool {
let whole_copy = || clips.iter().any(|x| x.id != full.id && x.end() == full.start && same_material(x, full));
full.loop_id.is_some()
&& full.loop_id == short.loop_id
&& short.length < full.length
&& !clips.iter().any(|x| x.loop_id == short.loop_id && x.start == short.end())
&& (whole_copy() || same_content(full, short))
}
/// The loop a clip belongs to: back-to-back copies of the same material, the last of which can be
/// cut short. Origin first.
fn loop_chain(s: &Session, t: TrackId, id: ClipId) -> Vec<ClipId> {
let Some(tr) = s.track(t) else { return Vec::new() };
let clips = tr.clips();
let Some(mut cur) = clips.iter().find(|c| c.id == id) else { return Vec::new() };
let mut guard = 0;
while let Some(prev) = clips.iter().find(|x| x.id != cur.id && x.end() == cur.start && same_material(x, cur)) {
while let Some(prev) = clips.iter().find(|x| x.id != cur.id && x.end() == cur.start && (same_material(x, cur) || cut_short(clips, x, cur))) {
cur = prev;
guard += 1;
if guard > clips.len() {
@@ -128,6 +144,9 @@ fn loop_chain(s: &Session, t: TrackId, id: ClipId) -> Vec<ClipId> {
chain.push(next.id);
cur = next;
}
if let Some(last) = clips.iter().find(|x| !chain.contains(&x.id) && x.start == cur.end() && cut_short(clips, cur, x)) {
chain.push(last.id);
}
chain
}
@@ -342,6 +361,101 @@ mod tests {
assert!(e.session().track(t).unwrap().clips().iter().all(|c| c.loop_id.is_none() && c.group.is_none()));
}
fn spans(e: &Engine, t: TrackId) -> Vec<(Samples, Samples)> {
e.session().track(t).unwrap().clips().iter().map(|c| (c.start, c.length)).collect()
}
#[test]
fn loop_length_cuts_the_last_iteration_short() {
let (mut e, t, id) = midi_engine();
assert_eq!(e.execute("clip.loop", &json!({"clip": id.0, "length": 250})).unwrap()["loops"], 2);
assert_eq!(spans(&e, t), vec![(0, 100), (100, 100), (200, 50)]);
// A length that is a whole number of loops leaves no short one.
let (mut e, t, id) = midi_engine();
assert_eq!(e.execute("clip.loop", &json!({"clip": id.0, "length": 300})).unwrap()["loops"], 2);
assert_eq!(spans(&e, t), vec![(0, 100), (100, 100), (200, 100)]);
}
#[test]
fn a_short_last_iteration_fits_its_fades_and_drops_a_sync_point_past_the_cut() {
let (mut e, t, id) = midi_engine();
if let Some(c) = e.session_mut().find_clip_mut(id) {
c.sync_point = 80;
c.fade_out.len = 70;
}
e.execute("clip.loop", &json!({"clip": id.0, "length": 150})).unwrap();
let clips = e.session().track(t).unwrap().clips();
let last = clips.last().unwrap();
assert_eq!((last.start, last.length, last.sync_point, last.fade_out.len), (100, 50, 0, 50));
assert_eq!((clips[0].sync_point, clips[0].fade_out.len), (80, 70));
}
#[test]
fn unloop_removes_a_short_last_iteration_too() {
let (mut e, t, id) = midi_engine();
e.execute("clip.loop", &json!({"clip": id.0, "length": 250})).unwrap();
e.execute("clip.unloop", &json!({"clip": id.0})).unwrap();
assert_eq!(spans(&e, t), vec![(0, 100)]);
// An edit to the source note or to the short loop's note.
for edited in [0, 2] {
let (mut e, t, id) = midi_engine();
e.execute("clip.loop", &json!({"clip": id.0, "length": 250})).unwrap();
let target = e.session().track(t).unwrap().clips()[edited].id;
if let Some(c) = e.session_mut().find_clip_mut(target)
&& let ClipContent::Midi { sequence } = &mut c.content
{
sequence.notes[0].length = 1100;
}
e.execute("clip.unloop", &json!({"clip": id.0})).unwrap();
assert_eq!(spans(&e, t), vec![(0, 100)], "edited loop {edited}");
}
}
#[test]
fn unloop_keeps_both_halves_of_a_split_clip() {
let (mut e, t, ids) = engine_with(&[(0, 100, 0)]);
e.execute("clip.loop", &json!({"clip": ids[0].0, "count": 3})).unwrap();
crate::edit::separate_at(e.session_mut(), t, 230);
e.execute("clip.unloop", &json!({"clip": ids[0].0})).unwrap();
assert_eq!(spans(&e, t), vec![(0, 100), (200, 30), (230, 70)]);
// A split loop origin is not a loop.
let (mut e, t, ids) = engine_with(&[(0, 100, 0)]);
e.execute("clip.loop", &json!({"clip": ids[0].0, "count": 2})).unwrap();
crate::edit::separate_at(e.session_mut(), t, 30);
assert!(e.execute("clip.unloop", &json!({"clip": ids[0].0})).is_err());
assert_eq!(spans(&e, t), vec![(0, 30), (30, 70), (100, 100)]);
}
#[test]
fn loop_fill_stops_at_the_next_clip() {
let (mut e, t, ids) = engine_with(&[(0, 100, 0), (430, 100, 0)]);
assert_eq!(e.execute("clip.loop", &json!({"clip": ids[0].0, "fill": true})).unwrap()["loops"], 4);
assert_eq!(spans(&e, t), vec![(0, 100), (100, 100), (200, 100), (300, 100), (400, 30), (430, 100)]);
}
#[test]
fn loop_fill_without_a_next_clip_runs_to_the_session_end() {
let (mut e, t, ids) = engine_with(&[(0, 100, 0)]);
let other = e.session_mut().add_track(TrackKind::Audio, ChannelFormat::Mono, None);
let far = e.session_mut().new_clip_id();
crate::edit::place_clip(e.session_mut(), other, Clip::audio(far, "far", SourceId(9), 0, 200, 150));
assert_eq!(e.execute("clip.loop", &json!({"clip": ids[0].0, "fill": true})).unwrap()["loops"], 3);
assert_eq!(spans(&e, t), vec![(0, 100), (100, 100), (200, 100), (300, 50)]);
// The last clip in the session has nothing to fill up to.
assert_eq!(e.execute("clip.loop", &json!({"clip": far.0, "fill": true})).unwrap()["loops"], 0);
}
#[test]
fn a_fill_or_length_past_the_loop_limit_is_refused_without_an_edit() {
let (mut e, t, ids) = engine_with(&[(0, 100, 0), (100_150, 100, 0)]);
let err = e.execute("clip.loop", &json!({"clip": ids[0].0, "fill": true})).unwrap_err().to_string();
assert!(err.contains("1001 loops"), "{err}");
assert!(e.execute("clip.loop", &json!({"clip": ids[0].0, "length": 100_200})).is_err());
assert_eq!(spans(&e, t), vec![(0, 100), (100_150, 100)]);
// 1000 loops still fit.
assert_eq!(e.execute("clip.loop", &json!({"clip": ids[0].0, "length": 100_100})).unwrap()["loops"], 1000);
}
#[test]
fn regroup_reuses_group() {
let (mut e, _, ids) = engine_with(&[(0, 100, 0), (500, 100, 0), (900, 100, 0)]);
+117 -15
View File
@@ -121,10 +121,19 @@ pub fn specs() -> Vec<CommandSpec> {
"Strip Silence",
["Edit"],
Some("Cmd+U"),
"{threshold_db?: -48, min_length_ms?: 50, pad_before_ms?: 5, pad_after_ms?: 20}",
"{threshold_db?: -48, min_length_ms?: 50, pad_before_ms?: 5, pad_after_ms?: 20, mode?: strip|extract|separate, name?, number?: 1, zeros?: 0, suffix?} (strip removes the silence; extract removes the audio and keeps the silence; separate separates the clips at the edges of the silence and does not remove any; with a name, the clips left in the selection are named name, zeros, a number counting up from number, suffix)",
has_range,
strip_silence
),
cmd!(
query "edit.strip_silence_preview",
"Strip Silence Rectangles",
[],
None,
"{threshold_db?: -48, min_length_ms?: 50, pad_before_ms?: 5, pad_after_ms?: 20, tracks?, start?, end?} (the ranges edit.strip_silence finds with the same settings, padded, on each track: what Strip keeps and the Strip Silence window draws as rectangles; changes nothing)",
has_range,
strip_silence_preview
),
cmd!(
"edit.fades_create",
"Create...",
@@ -280,6 +289,7 @@ fn cut_copy(e: &mut Engine, p: &Value, cut: bool, id: &str) -> Result<Value> {
.unwrap_or_default();
cb.automation.push(lanes);
}
cb.keep_auto_names(e.session());
cb.markers = e
.session()
.markers
@@ -316,10 +326,21 @@ fn cut_track(s: &mut Session, t: TrackId, r: soundcraft_time::Range, sh: bool, f
ops::clear_range(s, t, r, sh);
}
/// The clipboard for a paste, with the Auto Rename schemes of its clips put into the session first
/// ([`Clipboard::adopt_auto_names`]). A paste that succeeds stores it back in `e.clipboard`, so later
/// pastes find its schemes under their session keys; a refused paste leaves the clipboard as it was.
pub(super) fn clipboard_for_paste(e: &mut Engine) -> Clipboard {
let mut cb = e.clipboard.clone();
if cb.tracks.iter().flatten().any(|c| c.auto_name.is_some()) {
cb.adopt_auto_names(e.session_mut());
}
cb
}
fn paste(e: &mut Engine, p: &Value) -> Result<Value> {
let tracks = tracks_required(e, "edit.paste", p)?;
let given = position_param(e, "edit.paste", p, "at")?;
let cb = e.clipboard.clone();
let cb = clipboard_for_paste(e);
let s = e.session();
let at = match given {
Some(at) => at,
@@ -352,6 +373,7 @@ fn paste(e: &mut Engine, p: &Value) -> Result<Value> {
}
}
s.edit.selection = soundcraft_time::Range::new(at, at + cb.length);
e.clipboard = cb;
Ok(json!({"pasted": pasted}))
}
@@ -417,7 +439,7 @@ fn repeat_to_fill(e: &mut Engine, p: &Value) -> Result<Value> {
let range = range_param(e, "edit.paste_repeat_to_fill", p)?;
let len = e.clipboard.length.max(1);
let tracks = tracks_required(e, "edit.paste_repeat_to_fill", p)?;
let cb = e.clipboard.clone();
let cb = clipboard_for_paste(e);
let s = e.session_mut();
let mut at = range.start;
let mut n = 0;
@@ -441,6 +463,7 @@ fn repeat_to_fill(e: &mut Engine, p: &Value) -> Result<Value> {
at += len;
n += 1;
}
e.clipboard = cb;
Ok(json!({"repeats": n}))
}
@@ -789,25 +812,94 @@ fn copy_to_new_playlist(e: &mut Engine, p: &Value) -> Result<Value> {
Ok(json!({"tracks": tracks.len()}))
}
fn strip_silence(e: &mut Engine, p: &Value) -> Result<Value> {
let tracks = tracks_required(e, "edit.strip_silence", p)?;
let r = range_param(e, "edit.strip_silence", p)?;
/// Strip Silence's tracks, range, threshold, minimum strip duration and clip start and end pads.
type StripSettings = (Vec<TrackId>, soundcraft_time::Range, f32, Samples, (Samples, Samples));
/// The [`StripSettings`] in Strip Silence's params, with their defaults and limits. `cmd` names the
/// command in errors.
fn strip_settings(e: &Engine, cmd: &str, p: &Value) -> Result<StripSettings> {
let tracks = tracks_required(e, cmd, p)?;
let r = range_param(e, cmd, p)?;
let thr = f32_or(p, "threshold_db", -48.0).clamp(-96.0, 0.0);
let sr = e.session().sample_rate;
let ms = |k: &str, d: f64| sr.samples(f64_or(p, k, d).clamp(0.0, 10_000.0) / 1000.0);
let (min_len, pre, post) = (ms("min_length_ms", 50.0), ms("pad_before_ms", 5.0), ms("pad_after_ms", 20.0));
Ok((tracks, r, thr, ms("min_length_ms", 50.0), (ms("pad_before_ms", 5.0), ms("pad_after_ms", 20.0))))
}
/// The scan `edit.strip_silence_preview` runs with params `p`, on the session as it is now. The Edit
/// window runs it while the Strip Silence window or its Rename Selected Clips dialog is open: on a
/// worker thread outside the web build, and on the frame thread in the web build or when no thread
/// can be started.
pub fn strip_silence_scan(e: &Engine, p: &Value) -> Result<crate::io::StripScan> {
let (tracks, range, threshold_db, min_len, pad) = strip_settings(e, "edit.strip_silence_preview", p)?;
Ok(crate::io::StripScan { session: e.session_arc(), tracks, range, threshold_db, min_len, pad, stop: Default::default() })
}
fn strip_silence_preview(e: &mut Engine, p: &Value) -> Result<Value> {
let found = strip_silence_scan(e, p)?.run()?;
let tracks: Vec<Value> =
found.iter().map(|(t, ranges)| json!({"track": t.0, "ranges": ranges.iter().map(|x| [x.start, x.end]).collect::<Vec<_>>()})).collect();
Ok(json!({"tracks": tracks}))
}
fn strip_silence(e: &mut Engine, p: &Value) -> Result<Value> {
let (tracks, r, thr, min_len, (pre, post)) = strip_settings(e, "edit.strip_silence", p)?;
let mode = match p.get("mode") {
None => crate::io::StripMode::Strip,
Some(Value::String(m)) => crate::io::StripMode::from_id(m).ok_or_else(|| bad("edit.strip_silence", format!("unknown mode `{m}`")))?,
Some(_) => return Err(bad("edit.strip_silence", "mode must be strip, extract or separate")),
};
let names = strip_names("edit.strip_silence", p)?;
let s = e.session_mut();
let mut kept = 0;
let mut named = 0;
for t in &tracks {
kept += crate::io::strip_silence(s, *t, r, thr, min_len, pre, post)?;
kept += crate::io::strip_silence(s, *t, r, thr, min_len, (pre, post), mode, names.is_some())?;
if let Some(names) = &names {
named += crate::io::name_stripped_clips(s, *t, r, names, named);
}
}
Ok(json!({"clips": kept}))
Ok(json!({"clips": kept, "named": named}))
}
/// The naming scheme in the `name`, `number`, `zeros` and `suffix` params of `cmd`
/// (`edit.strip_silence`, `clip.auto_rename`), or `None` when `name` is missing or blank.
pub(super) fn strip_names(cmd: &str, p: &Value) -> Result<Option<crate::io::StripNames>> {
use crate::io::StripNames;
let text = |k: &str| match p.get(k) {
None => Ok(None),
Some(Value::String(v)) => Ok(Some(v.clone())),
Some(_) => Err(bad(cmd, format!("`{k}` must be text"))),
};
let whole = |k: &str, max: u64| match p.get(k) {
None => Ok(None),
Some(v) => v.as_u64().filter(|n| *n <= max).map(Some).ok_or_else(|| bad(cmd, format!("`{k}` must be a whole number from 0 to {max}"))),
};
let (name, suffix) = (text("name")?, text("suffix")?);
let (number, zeros) = (whole("number", StripNames::MAX_NUMBER)?, whole("zeros", StripNames::MAX_ZEROS as u64)?);
let Some(name) = name.filter(|n| !n.trim().is_empty()) else {
if suffix.is_some() || number.is_some() || zeros.is_some() {
return Err(bad(cmd, "`number`, `zeros` and `suffix` need a `name`"));
}
return Ok(None);
};
let d = StripNames::default();
let suffix = suffix.unwrap_or(d.suffix);
let max_text = soundcraft_model::AutoName::MAX_TEXT_CHARS;
if name.chars().count() + suffix.chars().count() > max_text {
return Err(bad(cmd, format!("`name` and `suffix` together must be at most {max_text} characters")));
}
Ok(Some(StripNames { name, number: number.unwrap_or(d.number), zeros: zeros.and_then(|z| usize::try_from(z).ok()).unwrap_or(d.zeros), suffix }))
}
fn fades_create(e: &mut Engine, p: &Value) -> Result<Value> {
let shape = str_param(p, "shape")
.map_or(Some(FadeShape::EqualPower), FadeShape::from_id)
.ok_or_else(|| bad("edit.fades_create", "unknown fade shape"))?;
let shape = match p.get("shape") {
None => FadeShape::EqualPower,
Some(v) => v
.as_str()
.and_then(FadeShape::from_id)
.ok_or_else(|| bad("edit.fades_create", "shape must be linear|equal power|s-curve|exponential|logarithmic"))?,
};
if p.get("clip").is_some() || p.get("side").is_some() {
let curvature = match p.get("curvature") {
None => 0.0,
@@ -835,9 +927,6 @@ fn fades_create(e: &mut Engine, p: &Value) -> Result<Value> {
if available <= 0 {
return Err(bad("edit.fades_create", "clip has no length"));
}
if p.get("shape").is_some_and(|v| !v.is_string()) {
return Err(bad("edit.fades_create", "shape must be a fade shape name"));
}
let fade = Fade { len: length.min(available), shape, curvature };
let clip = e.session_mut().find_clip_mut(id).ok_or_else(|| bad("edit.fades_create", "clip not found"))?;
if fade_in {
@@ -1074,6 +1163,19 @@ mod tests {
assert_eq!(e.session(), &before);
}
#[test]
fn range_fades_refuse_a_shape_that_is_not_a_shape_name() {
for shape in [json!("bad"), json!(null), json!(true), json!(1)] {
let (mut e, t, _) = trim_session(&[(0, 10_000, 0), (10_000, 10_000, 10_000)]);
let before = e.session().clone();
let history = e.undo_history();
let params = json!({"tracks": [t], "start": 9_500, "end": 10_500, "shape": shape});
assert!(e.execute("edit.fades_create", &params).is_err(), "{params}");
assert_eq!(e.session(), &before, "{params}");
assert_eq!(e.undo_history(), history, "{params}");
}
}
#[test]
fn targeted_fades_support_one_sample_clips() {
let mut e = crate::demo::demo_engine();
+2 -1
View File
@@ -452,7 +452,7 @@ fn paste_merge_midi(e: &mut Engine, p: &Value) -> Result<Value> {
let id = "edit.paste_merge_midi";
let tracks = tracks_required(e, id, p)?;
let at = position_param(e, id, p, "at")?.unwrap_or(e.session().edit.selection.start).max(0);
let cb = e.clipboard.clone();
let cb = super::edit::clipboard_for_paste(e);
let s = e.session_mut();
let tempo = s.tempo.clone();
let sr = s.sample_rate;
@@ -510,6 +510,7 @@ fn paste_merge_midi(e: &mut Engine, p: &Value) -> Result<Value> {
}
}
}
e.clipboard = cb;
Ok(json!({"merged": merged, "placed": placed}))
}
+52 -4
View File
@@ -88,7 +88,7 @@ pub fn specs() -> Vec<CommandSpec> {
}
),
cmd!(noundo "file.bounce_mix", "Bounce Mix...", ["File"], Some("Cmd+Alt+B"), "{path, format?: wav|aiff|flac, bit_depth?: 16|24|32f, start?, end?, source?: main|bus name, normalize?: false, dither?: true, fold_down?: stereo}", always, bounce),
cmd!(noundo "file.bounce_stems", "Bounce Stems...", [], None, "{dir, format?: wav|aiff|flac, bit_depth?: 16|24|32f, start?, end?, fold_down?: stereo}", has_tracks, |e, p| {
cmd!(noundo "file.bounce_stems", "Bounce Stems...", [], None, "{dir, format?: wav|aiff|flac, bit_depth?: 16|24|32f, start?, end?, dither?: true, fold_down?: stereo}", has_tracks, |e, p| {
let dir = str_param(p, "dir").ok_or_else(|| bad("file.bounce_stems", "`dir` required"))?.to_string();
let r = range_param(e, "file.bounce_stems", p)?;
let r = if r.is_empty() { soundcraft_time::Range::new(0, e.session().content_end().max(1)) } else { r };
@@ -103,7 +103,7 @@ pub fn specs() -> Vec<CommandSpec> {
_ => soundcraft_audio_io::BitDepth::Int24,
};
let fold = fold_down_param(p, "file.bounce_stems")?;
let files = crate::io::bounce_stems_with(e, &dir, r, &soundcraft_audio_io::EncodeOptions { format, bit_depth, dither: true, bwf: None }, fold)?;
let files = crate::io::bounce_stems_with(e, &dir, r, &soundcraft_audio_io::EncodeOptions { format, bit_depth, dither: bool_or(p, "dither", true), bwf: None }, fold)?;
Ok(json!({"files": files}))
}),
cmd!(noundo "file.export_midi", "MIDI...", ["File", "Export"], None, "{path, tracks?}", always, |e, p| {
@@ -241,9 +241,17 @@ fn import_audio(e: &mut Engine, p: &Value) -> Result<Value> {
let target = import_audio_target(e, p)?;
let mut out = Vec::new();
for path in &paths {
let bytes = crate::io::read_file(path)?;
let name = std::path::Path::new(path).file_name().and_then(std::ffi::OsStr::to_str).unwrap_or("Audio").to_string();
let r = crate::io::import_audio_bytes(e, &name, &bytes, Some(path.as_str()), target, at)?;
// Audio the app decoded on a worker thread (`Engine::offer_import`) is only placed here;
// the path is checked the same way either way.
crate::file_access::check_read(std::path::Path::new(path)).map_err(crate::EngineError::Io)?;
let r = match e.take_import(std::path::Path::new(path)) {
Some(d) => crate::io::place_decoded(e, &name, d, Some(path.as_str()), target, at)?,
None => {
let bytes = crate::io::read_file(path)?;
crate::io::import_audio_bytes(e, &name, &bytes, Some(path.as_str()), target, at)?
}
};
out.push(r);
}
Ok(json!({"imported": out}))
@@ -354,6 +362,46 @@ mod tests {
path.to_string_lossy().into_owned()
}
/// Issue #597: the app decodes on a worker thread and hands the audio over; the command then
/// only places it. Audio decoded for another rate is ignored and the file is read again.
#[test]
fn import_audio_places_audio_offered_by_a_worker() {
let dir = std::env::temp_dir().join(format!("sc-import-offered-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&dir);
std::fs::create_dir_all(&dir).unwrap();
let wav = tone_wav(&dir, "tone.wav");
let mut e = Engine::default();
let rate = e.session().sample_rate.hz();
let decoded = std::thread::spawn({
let wav = wav.clone();
move || crate::io::decode_import(&wav, rate)
})
.join()
.unwrap()
.unwrap();
assert_eq!(decoded.frames(), 4_800);
e.offer_import(decoded);
// The file changes after decoding: the offered audio is placed, not the new bytes.
std::fs::write(&wav, b"not audio").unwrap();
let r = e.execute("file.import_audio", &json!({"path": wav, "at": 960})).unwrap();
assert_eq!(r["imported"][0]["frames"], 4_800);
assert_eq!(e.session().tracks.len(), 1);
assert_eq!(e.session().tracks[0].clips()[0].start, 960);
assert_eq!(e.session().sources[0].path, wav);
// Taken once: the next import reads the (now broken) file.
assert!(e.execute("file.import_audio", &json!({"path": wav})).is_err());
// Decoded for another rate: ignored, so the broken file is read and refused.
let good = tone_wav(&dir, "good.wav");
let other = crate::io::decode_import(&good, rate / 2).unwrap();
std::fs::write(&good, b"not audio").unwrap();
e.offer_import(other);
assert!(e.execute("file.import_audio", &json!({"path": good})).is_err());
e.drop_offered_imports();
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
fn export_session_text_needs_replace_to_overwrite() {
let dir = std::env::temp_dir().join(format!("sc-export-text-replace-{}", std::process::id()));
+26 -1
View File
@@ -130,8 +130,19 @@ fn notes_move(e: &mut Engine, p: &Value) -> Result<Value> {
n.pitch = i64::from(n.pitch).saturating_add(ds).clamp(0, 127) as u8;
}
}
let moved: Vec<Note> = seq.notes.iter().enumerate().filter(|(i, _)| idx.contains(i)).map(|(_, n)| *n).collect();
seq.sort();
Ok(json!({"moved": idx.len()}))
// Where the moved notes ended up after sorting, so the editor can keep them selected.
let mut taken = vec![false; seq.notes.len()];
let mut indices = Vec::with_capacity(moved.len());
for m in &moved {
if let Some(j) = seq.notes.iter().enumerate().position(|(j, n)| !taken[j] && n == m) {
taken[j] = true;
indices.push(j);
}
}
indices.sort_unstable();
Ok(json!({"moved": idx.len(), "indices": indices}))
}
#[cfg(test)]
@@ -172,6 +183,20 @@ mod tests {
assert_eq!(n["notes"][0]["pitch"], 127);
}
#[test]
fn notes_move_reports_where_the_moved_notes_went() {
let mut e = Engine::default();
let t = e.session_mut().add_track(TrackKind::Instrument, ChannelFormat::Stereo, Some("Keys"));
let clip = e.execute("midi.new_clip", &json!({"track": t.0})).unwrap()["clip"].as_u64().unwrap();
for start in [0, 480, 960] {
e.execute("midi.note_add", &json!({"clip": clip, "pitch": 60, "start_ticks": start})).unwrap();
}
let r = e.execute("midi.notes_move", &json!({"clip": clip, "indices": [0], "ticks": 720})).unwrap();
assert_eq!(r["indices"], json!([1]));
let notes = e.execute("midi.notes", &json!({"clip": clip})).unwrap();
assert_eq!(notes["notes"][1]["start"], 720);
}
#[test]
fn rejects_audio_tracks() {
let mut e = Engine::default();
+91 -8
View File
@@ -697,23 +697,35 @@ fn clip_gain_to_vol(e: &mut Engine, p: &Value) -> Result<Value> {
for t in &tracks {
let Some(tr) = s.track_mut(*t) else { continue };
let base = tr.mixer.volume_db;
// An existing lane overrides the fader, so the clip gain rides on top of the lane value.
let old = tr.lane(&AutoParam::Volume).cloned();
let level = |at: i64| old.as_ref().map_or(base, |l| l.value_at(at, base));
let mut pins = Vec::new();
let mut pts = Vec::new();
if let Some(pl) = tr.playlist_mut() {
for c in pl.clips.iter_mut().filter(|c| c.range().overlaps(&r)) {
if c.gain_env.is_empty() {
pts.push((c.start, base + c.gain_db));
pts.push((c.end() - 1, base + c.gain_db));
} else {
for (o, db) in &c.gain_env {
pts.push((c.start + o, base + c.gain_db + db));
}
// Pin the unchanged level just outside the clip so the neighbouring segments keep their shape.
if c.start > 0 {
pins.push((c.start - 1, level(c.start - 1)));
}
pins.push((c.end(), level(c.end())));
// The clip edges always get a point, so the envelope's first and last values hold out to them.
let mut ats = vec![c.start, c.end() - 1];
ats.extend(c.gain_env.iter().map(|&(o, _)| c.start + o).filter(|&at| at > c.start && at < c.end() - 1));
// The lane's own points inside the clip carry the clip gain too.
if let Some(l) = &old {
ats.extend(l.points.iter().map(|pt| pt.at).filter(|&at| at > c.start && at < c.end() - 1));
}
for at in ats {
pts.push((at, level(at) + c.gain_db + env_db(&c.gain_env, at - c.start)));
}
c.gain_db = 0.0;
c.gain_env.clear();
}
}
let lane = tr.lane_mut(&AutoParam::Volume);
for (at, v) in pts {
// Pins first, so a neighbouring clip's converted points win where they meet.
for (at, v) in pins.into_iter().chain(pts) {
lane.set_point(at, v);
}
}
@@ -872,6 +884,77 @@ mod tests {
}
}
#[test]
fn clip_gain_to_volume_keeps_the_existing_lane() {
let clip_gain_to_volume = |points: &[(i64, f32)], env: Vec<(i64, f32)>| {
let (mut e, t) = tone_session(points);
let clip = e.session().track(t).unwrap().clips()[0].id;
let c = e.session_mut().find_clip_mut(clip).unwrap();
c.gain_db = 6.0;
c.gain_env = env;
let before = rendered(e.session());
e.execute("automation.clip_gain_to_volume", &json!({"track": t.0, "start": 0, "end": 8192})).unwrap();
let after = rendered(e.session());
assert!(e.session().find_clip(clip).unwrap().1.gain_db == 0.0);
(before, after)
};
let (before, after) = clip_gain_to_volume(&[(0, -6.0), (8192, -6.0)], Vec::new());
let delta = rms_db(&before, &after);
assert!(delta.abs() < 0.02, "level changed by {delta} dB");
// A moving lane keeps its shape, and its points inside the clip carry the gain.
let (before, after) = clip_gain_to_volume(&[(0, -12.0), (4096, -6.0), (8192, -12.0)], Vec::new());
for (start, end) in [(0, 2048), (2048, 4096), (4096, 6144), (6144, 8192)] {
let delta = rms_db(&before[start..end], &after[start..end]);
assert!(delta.abs() < 0.1, "{start}..{end} changed by {delta} dB");
}
// The clip's own gain envelope rides on the lane value as well.
let (before, after) = clip_gain_to_volume(&[(0, -6.0), (8192, -6.0)], vec![(2000, 0.0), (6000, -10.0)]);
for (start, end) in [(3900, 4100), (5000, 5200)] {
let delta = rms_db(&before[start..end], &after[start..end]);
assert!(delta.abs() < 0.2, "{start}..{end} changed by {delta} dB");
}
}
#[test]
fn clip_gain_to_volume_holds_the_envelope_ends_out_to_the_clip_edges() {
let (mut e, t) = tone_session(&[(0, -6.0), (8192, -6.0)]);
let clip = e.session().track(t).unwrap().clips()[0].id;
let c = e.session_mut().find_clip_mut(clip).unwrap();
c.gain_db = 6.0;
c.gain_env = vec![(2000, 0.0), (6000, -10.0)];
let before = rendered(e.session());
e.execute("automation.clip_gain_to_volume", &json!({"track": t.0, "start": 0, "end": 8192})).unwrap();
let after = rendered(e.session());
for (start, end) in [(0, 1000), (1000, 2000), (6000, 7000), (7000, 8192)] {
let delta = rms_db(&before[start..end], &after[start..end]);
assert!(delta.abs() < 0.2, "{start}..{end} changed by {delta} dB");
}
}
#[test]
fn clip_gain_to_volume_leaves_audio_outside_the_clip_alone() {
// Three clips in a row; only the middle one is converted, and the lane extends past it on both sides.
let (mut e, t) = tone_session(&[(0, -6.0), (16384, -6.0)]);
let (middle, last) = (e.session_mut().new_clip_id(), e.session_mut().new_clip_id());
let pl = e.session_mut().track_mut(t).unwrap().playlist_mut().unwrap();
if let Some(c) = pl.clips.first_mut() {
c.length = 2048;
}
pl.clips.push(Clip::audio(middle, "mid", SourceId(910), 2048, 2048, 4096));
pl.clips.push(Clip::audio(last, "end", SourceId(910), 6144, 6144, 2048));
e.session_mut().find_clip_mut(middle).unwrap().gain_db = 6.0;
let before = rendered(e.session());
e.execute("automation.clip_gain_to_volume", &json!({"track": t.0, "start": 2048, "end": 6144})).unwrap();
let after = rendered(e.session());
assert!(e.session().find_clip(middle).unwrap().1.gain_db == 0.0);
for (start, end) in [(0, 2040), (2056, 6136), (6152, 8192)] {
let delta = rms_db(&before[start..end], &after[start..end]);
assert!(delta.abs() < 0.1, "{start}..{end} changed by {delta} dB");
}
}
#[test]
fn insert_params_rejects_unknown_and_non_numeric_keys() {
let mut e = Engine::default();
+3
View File
@@ -8,6 +8,7 @@ use std::sync::OnceLock;
mod audio_midi;
mod audiosuite;
mod auto_rename;
mod clap;
mod clip;
mod clip_list;
@@ -62,6 +63,7 @@ mod panic_probe {
pub use file::bounce_job;
pub(crate) use edit::remember_selection;
pub use edit::strip_silence_scan;
pub type Run = fn(&mut Engine, &Value) -> Result<Value>;
/// `Err(reason)` = disabled.
@@ -147,6 +149,7 @@ pub fn command_specs() -> &'static [CommandSpec] {
v.extend(track_more::specs());
v.extend(clip_more::specs());
v.extend(clip_list::specs());
v.extend(auto_rename::specs());
v.extend(event_more::specs());
v.extend(setup_more::specs());
v.extend(clap::specs());
+33
View File
@@ -461,6 +461,8 @@ fn import_session_data(e: &mut Engine, p: &Value) -> Result<Value> {
s.sources.push(ns);
}
let mut out = Vec::new();
// The donor's Auto Rename schemes come along under new keys, shared as they were.
let mut names: std::collections::BTreeMap<u64, u64> = Default::default();
for mut tr in picked {
tr.id = TrackId(s.alloc());
tr.name = s.unique_track_name(&tr.name);
@@ -474,6 +476,14 @@ fn import_session_data(e: &mut Engine, p: &Value) -> Result<Value> {
{
*source = *n;
}
c.auto_name = c.auto_name.and_then(|k| {
let scheme = other.auto_names.get(&k)?;
Some(*names.entry(k).or_insert_with(|| {
let (key, id) = (s.alloc(), crate::new_scheme_id(s));
s.auto_names.insert(key, soundcraft_model::AutoName { id, ..scheme.clone() });
key
}))
});
}
}
// The donor's source ids mean nothing here: remap the sample, or drop it when it didn't come along.
@@ -601,6 +611,29 @@ mod tests {
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
fn import_session_data_brings_auto_rename_schemes_under_new_keys() {
let dir = std::env::temp_dir().join(format!("sc-import-names-{}", std::process::id()));
std::fs::create_dir_all(&dir).unwrap();
let path = dir.join("donor.scraft").to_string_lossy().into_owned();
let mut donor = crate::demo::demo_engine();
let kick: Vec<_> = donor.session().track_by_name("Kick").unwrap().clips().iter().map(|c| c.id.0).collect();
donor.execute("clip.auto_rename", &json!({"clips": kick, "name": "Hit", "number": 5})).unwrap();
crate::io::save_session(&mut donor, &path).unwrap();
let mut e = crate::demo::demo_engine();
let bass = e.session().track_by_name("Bass").unwrap().clips()[0].id;
e.execute("clip.auto_rename", &json!({"clips": [bass], "name": "Low"})).unwrap();
e.execute("file.import_session_data", &json!({"path": path, "tracks": ["Kick"]})).unwrap();
let s = e.session();
assert_eq!(s.auto_names.len(), 2, "the donor's scheme comes in beside this session's");
let imported = s.tracks.iter().rfind(|t| t.name.starts_with("Kick")).unwrap();
let keys: std::collections::BTreeSet<_> = imported.clips().iter().map(|c| c.auto_name).collect();
assert_eq!(keys.len(), 1, "the imported clips still share one scheme");
let key = keys.into_iter().next().flatten().unwrap();
assert_eq!((s.auto_names[&key].name.as_str(), s.auto_names[&key].number), ("Hit", 5));
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
fn import_session_data_brings_a_samplers_sample() {
let dir = std::env::temp_dir().join(format!("sc-import-sampler-{}", std::process::id()));
+52 -5
View File
@@ -5,7 +5,7 @@ use super::{
use crate::{Engine, Result, cmd};
use serde_json::{Value, json};
use soundcraft_model::{ChannelFormat, Group, GroupId, Playlist, Route, Session, Track, TrackHeight, TrackId, TrackKind};
use soundcraft_model::{ChannelFormat, ClipContent, Group, GroupId, Playlist, Route, Session, Track, TrackHeight, TrackId, TrackKind};
pub fn specs() -> Vec<CommandSpec> {
vec![
@@ -414,6 +414,8 @@ fn split_mono(e: &mut Engine, p: &Value) -> Result<Value> {
let tracks = tracks_required(e, "track.split_into_mono", p)?;
let s = e.session_mut();
let mut out = Vec::new();
let mut mono_sources = std::collections::BTreeMap::new();
let mut copied_samples = 0usize;
for t in tracks {
let Some(src) = s.track(t).cloned() else { continue };
if src.channels() < 2 || src.kind != TrackKind::Audio {
@@ -429,12 +431,46 @@ fn split_mono(e: &mut Engine, p: &Value) -> Result<Value> {
let mut clips = src.clips().to_vec();
for c in &mut clips {
c.id = s.new_clip_id();
// Mono tracks read channel 0 of the source; record the channel in the name for now.
if let ClipContent::Audio { source, .. } = &mut c.content {
let audio =
s.pool.get(*source).ok_or_else(|| bad("track.split_into_mono", format!("audio unavailable for clip `{}`", c.name)))?;
let channels = audio.buffer.channels.len();
if channels == 0 {
return Err(bad("track.split_into_mono", format!("audio has no channels for clip `{}`", c.name)));
}
// Match the renderer's channel wrapping when a track is wider than its source.
// An already-mono source can be shared without copying it.
if channels > 1 {
let key = (*source, ch % channels);
let mono = if let Some(&id) = mono_sources.get(&key) {
id
} else {
let samples =
audio.buffer.channels.get(key.1).ok_or_else(|| bad("track.split_into_mono", "source channel unavailable"))?;
copied_samples = copied_samples
.checked_add(samples.len())
.filter(|&n| n <= crate::io::MAX_IMPORT_SAMPLES)
.ok_or_else(|| bad("track.split_into_mono", "split would copy too much audio"))?;
// Keep the full channel: the cloned clips retain their offsets, stretch,
// gain and fades, and every reader sees genuine mono media (including saves).
let buf = soundcraft_audio_io::AudioBuffer { sample_rate: s.sample_rate.hz(), channels: vec![samples.clone()] };
let original = s.source(*source);
let name = format!("{}.{suffix}.wav", original.map_or(c.name.as_str(), |source| source.name.as_str()));
let time_reference = original.map_or(0, |source| source.time_reference);
let id = crate::io::add_source(s, &name, buf, None, soundcraft_audio_io::FileFormat::Wav);
if let Some(source) = s.sources.iter_mut().find(|source| source.id == id) {
source.time_reference = time_reference;
}
mono_sources.insert(key, id);
id
};
*source = mono;
}
}
c.name = format!("{}.{suffix}", c.name);
}
if let Some(tr) = s.track_mut(id) {
tr.mixer.pan = vec![if ch == 0 { -1.0 } else { 1.0 }];
tr.comments = format!("source_channel={ch}");
if let Some(pl) = tr.playlist_mut() {
pl.clips = clips;
}
@@ -704,15 +740,26 @@ fn delete(e: &mut Engine, p: &Value) -> Result<Value> {
fn rename(e: &mut Engine, p: &Value) -> Result<Value> {
let t = track_param(e, "track.rename", p, "track")?.ok_or_else(|| bad("track.rename", "`track` required"))?;
let name = str_param(p, "name").map(str::trim).filter(|n| !n.is_empty()).ok_or_else(|| bad("track.rename", "`name` required"))?;
let s = e.session_mut();
// Validate the stored name, including its existing 64-character limit.
let name: String = name.chars().take(64).collect();
let s = e.session();
if s.tracks.iter().any(|x| x.name == name && x.id != t) {
return Err(bad("track.rename", format!("a track named `{name}` already exists")));
}
let name: String = name.chars().take(64).collect();
let bus = s.track(t).and_then(|track| match (track.kind, &track.mixer.input) {
(TrackKind::Folder, Route::Bus(bus)) => Some(*bus),
_ => None,
});
let track = s.track(t).ok_or_else(|| bad("track.rename", "unknown track"))?;
if track.name == name
&& bus.is_none_or(|id| {
let expected = unique_folder_bus_name(s, &format!("{name} Bus"), Some(id));
s.bus(id).is_none_or(|bus| bus.name == expected)
})
{
return Ok(json!({"name": name}));
}
let s = e.session_mut();
if let Some(track) = s.track_mut(t) {
track.name.clone_from(&name);
}
+162 -7
View File
@@ -78,8 +78,16 @@ fn rebase_midi_start(clip: &mut Clip, old_start: Samples, t: TimeCtx<'_>) {
}
}
/// Split every clip on a track's active playlist that spans `at`.
/// Split every clip on a track's active playlist that spans `at`. The piece before `at` keeps the
/// clip's name. The piece after it is a new clip, and takes the next name of the clip's Auto Rename
/// scheme when it has one.
pub fn separate_at(s: &mut Session, track: TrackId, at: Samples) -> usize {
separate_at_if(s, track, at, |_| true)
}
/// [`separate_at`], giving the piece after `at` the next Auto Rename name only when `auto_name`
/// returns true for it. Any other piece keeps the clip's name, and its scheme does not count up.
pub fn separate_at_if(s: &mut Session, track: TrackId, at: Samples, auto_name: impl Fn(&Clip) -> bool) -> usize {
let tc_tempo = s.tempo.clone();
let sr = s.sample_rate;
let t = TimeCtx { tempo: &tc_tempo, sr };
@@ -88,15 +96,22 @@ pub fn separate_at(s: &mut Session, track: TrackId, at: Samples) -> usize {
for _ in 0..n_split {
ids.push(s.new_clip_id());
}
let Some(pl) = s.track_mut(track).and_then(|t| t.playlist_mut()) else { return 0 };
let mut schemes = std::mem::take(&mut s.auto_names);
let Some(pl) = s.track_mut(track).and_then(|t| t.playlist_mut()) else {
s.auto_names = schemes;
return 0;
};
let mut out = Vec::with_capacity(pl.clips.len() + ids.len());
let mut count = 0;
for c in pl.clips.drain(..) {
if at > c.start
&& at < c.end()
&& let Some(id) = ids.pop()
&& let Some((l, r)) = split_clip(&c, at, id, t)
&& let Some((l, mut r)) = split_clip(&c, at, id, t)
{
if auto_name(&r) {
take_auto_name(&mut schemes, &mut r);
}
out.push(l);
out.push(r);
count += 1;
@@ -106,9 +121,34 @@ pub fn separate_at(s: &mut Session, track: TrackId, at: Samples) -> usize {
}
pl.clips = out;
pl.sort();
s.auto_names = schemes;
count
}
/// Gives `c` the next name of its Auto Rename scheme, when it has one, and counts the scheme up.
fn take_auto_name(schemes: &mut std::collections::BTreeMap<u64, soundcraft_model::AutoName>, c: &mut Clip) {
if let Some(scheme) = c.auto_name.and_then(|k| schemes.get_mut(&k)) {
c.name = crate::io::StripNames::from(&*scheme).clip_name(0);
scheme.number = scheme.number.saturating_add(1);
}
}
/// Gives each clip on a track's active playlist whose id is not in `old`, and for which `auto_name`
/// returns true, the next name of its Auto Rename scheme, in time order. Pieces cut with
/// [`separate_at_if`] and `|_| false` take their names here once the caller has removed the ones it
/// does not keep.
pub fn auto_name_new_clips(s: &mut Session, track: TrackId, old: &HashSet<ClipId>, auto_name: impl Fn(&Clip) -> bool) {
let mut schemes = std::mem::take(&mut s.auto_names);
if let Some(pl) = s.track_mut(track).and_then(|t| t.playlist_mut()) {
let mut new: Vec<&mut Clip> = pl.clips.iter_mut().filter(|c| !old.contains(&c.id) && auto_name(c)).collect();
new.sort_by_key(|c| (c.start, c.id.0));
for c in new {
take_auto_name(&mut schemes, c);
}
}
s.auto_names = schemes;
}
/// Remove material inside `range` on a track. In Shuffle mode later clips move left, up to the
/// first locked clip (see [`ripple`]). Returns the removed material as clips positioned relative
/// to `range.start`.
@@ -407,6 +447,11 @@ pub fn trim_end_to(s: &mut Session, track: TrackId, at: Samples) -> usize {
n
}
/// Source samples covered by `length` timeline samples of a clip with the given `stretch`.
fn source_len(length: Samples, stretch: f64) -> Samples {
if stretch.is_finite() && stretch > 0.0 && (stretch - 1.0).abs() >= 1e-9 { (length as f64 / stretch).round() as Samples } else { length }
}
/// Heal separations inside `range`: merge neighbouring audio clips that continue the same file,
/// either touching (a split) or with an empty gap that matches the gap in the source (a cut).
pub fn heal(s: &mut Session, track: TrackId, range: Range) -> usize {
@@ -420,11 +465,15 @@ pub fn heal(s: &mut Session, track: TrackId, range: Range) -> usize {
let gap = if contiguous { c.start.saturating_sub(prev.end()) } else { 0 };
let same = match (&prev.content, &c.content) {
(ClipContent::Audio { source: a, offset: oa }, ClipContent::Audio { source: b, offset: ob }) => {
a == b && oa.saturating_add(prev.length).saturating_add(gap) == *ob
a == b && oa.saturating_add(source_len(prev.length.saturating_add(gap), prev.stretch)) == *ob
}
_ => false,
};
if contiguous && same && (prev.gain_db - c.gain_db).abs() < 1e-6 {
let same_playback = prev.muted == c.muted && (prev.stretch - c.stretch).abs() < 1e-9 && (prev.gain_db - c.gain_db).abs() < 1e-6;
if contiguous && same && same_playback {
if !prev.gain_env.is_empty() || !c.gain_env.is_empty() {
join_gain_env(prev, &c);
}
prev.length = c.end().saturating_sub(prev.start);
prev.fade_out = c.fade_out;
healed += 1;
@@ -437,6 +486,24 @@ pub fn heal(s: &mut Session, track: TrackId, range: Range) -> usize {
healed
}
/// Give `l` the gain envelope of `l` followed by `r`, which starts at or after where `l` ends, so
/// every sample keeps its gain. Call it before `l.length` grows to cover `r`.
fn join_gain_env(l: &mut Clip, r: &Clip) {
let cut = l.length;
let seam = r.start.saturating_sub(l.start);
let last = cut.saturating_sub(1);
let (end_db, start_db) = (l.env_db_at(last), r.env_db_at(0));
l.gain_env.retain(|&(at, _)| at < cut);
l.gain_env.extend(r.gain_env.iter().filter(|&&(at, _)| at >= 0).map(|&(at, db)| (at.saturating_add(seam), db)));
// Pin both sides of the seam where the joined points would interpolate across it.
for (at, db) in [(seam, start_db), (last, end_db)] {
if (l.env_db_at(at) - db).abs() > 1e-4 {
let i = l.gain_env.partition_point(|&(t, _)| t < at);
l.gain_env.insert(i, (at, db));
}
}
}
/// Move clips by `delta`, overwriting what they land on; `dest` sends whole tracks of them
/// elsewhere (see [`destinations`]).
pub fn move_clips(s: &mut Session, ids: &[ClipId], delta: Samples, dest: &[(TrackId, TrackId)]) -> usize {
@@ -818,8 +885,10 @@ pub fn create_fades(s: &mut Session, track: TrackId, range: Range, shape: FadeSh
pub fn delete_fades(s: &mut Session, track: TrackId, range: Range) -> usize {
let Some(pl) = s.track_mut(track).and_then(|t| t.playlist_mut()) else { return 0 };
let mut n = 0;
let mut touched: HashSet<ClipId> = HashSet::new();
for c in &mut pl.clips {
if c.range().overlaps(&range) || (range.is_empty() && c.range().contains(range.start)) {
touched.insert(c.id);
if c.fade_in.len > 0 || c.fade_out.len > 0 {
n += 1;
}
@@ -827,11 +896,12 @@ pub fn delete_fades(s: &mut Session, track: TrackId, range: Range) -> usize {
c.fade_out = Fade::default();
}
}
// Overlapping (crossfaded) neighbours are trimmed back to the midpoint.
// Overlapping (crossfaded) neighbours in the range are trimmed back to the midpoint; other overlaps stay as they are.
pl.sort();
for i in 0..pl.clips.len().saturating_sub(1) {
let (Some(a_end), Some(b_start)) = (pl.clips.get(i).map(Clip::end), pl.clips.get(i + 1).map(|c| c.start)) else { continue };
if a_end > b_start {
let in_range = pl.clips.get(i).is_some_and(|a| touched.contains(&a.id)) || pl.clips.get(i + 1).is_some_and(|b| touched.contains(&b.id));
if in_range && a_end > b_start {
let mid = b_start + (a_end - b_start) / 2;
if let Some(a) = pl.clips.get_mut(i) {
a.trim_end_to(mid);
@@ -931,6 +1001,72 @@ mod tests {
assert_eq!(clips(&s, t).len(), 2);
}
#[test]
fn heal_skips_clips_with_different_playback() {
for tweak in [(|c: &mut Clip| c.muted = true) as fn(&mut Clip), |c: &mut Clip| c.stretch = 1.5] {
let (mut s, t) = session_with_clip();
separate_at(&mut s, t, 3000);
if let Some(pl) = s.track_mut(t).and_then(|t| t.playlist_mut()) {
tweak(&mut pl.clips[1]);
}
assert_eq!(heal(&mut s, t, Range::new(0, 10_000)), 0);
assert_eq!(s.track(t).unwrap().clips().len(), 2);
}
}
#[test]
fn heal_keeps_each_side_gain_envelope() {
let cases: [(Vec<(Samples, f32)>, Vec<(Samples, f32)>); 4] = [
(vec![(500, -3.0), (2500, -12.0)], vec![]),
(vec![], vec![(0, -6.0), (1000, 0.0)]),
(vec![(0, 0.0), (1999, -20.0)], vec![(-100, 4.0), (300, -9.0)]),
(vec![(500, -3.0), (2500, -12.0)], vec![(0, -9.75), (500, -12.0)]),
];
for (left_env, right_env) in cases {
let (mut s, t) = session_with_clip();
separate_at(&mut s, t, 3000);
let pl = s.track_mut(t).and_then(|t| t.playlist_mut()).unwrap();
pl.clips[0].gain_env = left_env;
pl.clips[1].gain_env = right_env;
let (l, r) = (pl.clips[0].clone(), pl.clips[1].clone());
assert_eq!(heal(&mut s, t, Range::new(0, 10_000)), 1);
let healed = &s.track(t).unwrap().clips()[0];
for rel in 0..4000 {
let want = if rel < 2000 { l.gain_at(rel) } else { r.gain_at(rel - 2000) };
assert!((healed.gain_at(rel) - want).abs() < 1e-4, "sample {rel}: {} != {want}", healed.gain_at(rel));
}
}
}
#[test]
fn heal_keeps_gain_envelopes_across_a_gap() {
let (mut s, t) = session_with_clip();
clear_range(&mut s, t, Range::new(2000, 3000), false);
let pl = s.track_mut(t).and_then(|t| t.playlist_mut()).unwrap();
pl.clips[0].gain_env = vec![(0, 0.0), (999, -6.0)];
pl.clips[1].gain_env = vec![(0, -12.0), (1999, -3.0)];
let (l, r) = (pl.clips[0].clone(), pl.clips[1].clone());
assert_eq!(heal(&mut s, t, Range::new(1500, 3500)), 1);
let healed = &s.track(t).unwrap().clips()[0];
assert_eq!(healed.length, 4000);
for rel in (0..1000).chain(2000..4000) {
let want = if rel < 1000 { l.gain_at(rel) } else { r.gain_at(rel - 2000) };
assert!((healed.gain_at(rel) - want).abs() < 1e-4, "sample {rel}: {} != {want}", healed.gain_at(rel));
}
}
#[test]
fn split_then_heal_rejoins_stretched_clip() {
let (mut s, t) = session_with_clip();
if let Some(c) = s.track_mut(t).and_then(|t| t.playlist_mut()).and_then(|p| p.clips.first_mut()) {
c.stretch = 2.0;
}
assert_eq!(separate_at(&mut s, t, 3000), 1);
assert_eq!(clips(&s, t), vec![(1000, 2000, 1000), (3000, 2000, 2000)]);
assert_eq!(heal(&mut s, t, Range::new(0, 10_000)), 1);
assert_eq!(clips(&s, t), vec![(1000, 4000, 1000)]);
}
#[test]
fn clear_slip_and_shuffle() {
let (mut s, t) = session_with_clip();
@@ -1108,4 +1244,23 @@ mod tests {
assert!(got.windows(2).all(|w| w[0].0 <= w[1].0), "playlist not sorted: {got:?}");
assert!(got.iter().all(|c| c.1 > 0), "empty clip: {got:?}");
}
#[test]
fn delete_fades_leaves_unrelated_overlaps_alone() {
let (mut s, t) = session_with_clip();
let ids: Vec<ClipId> = (0..4).map(|_| s.new_clip_id()).collect();
if let Some(pl) = s.track_mut(t).and_then(|t| t.playlist_mut()) {
pl.clips.clear();
for (id, start, len) in [(ids[0], 0, 1100), (ids[1], 1000, 1000), (ids[2], 5000, 1100), (ids[3], 6000, 1000)] {
let mut c = Clip::audio(id, "c", SourceId(500), start, start, len);
c.fade_in = Fade { len: 100, shape: FadeShape::Linear, curvature: 0.0 };
c.fade_out = Fade { len: 100, shape: FadeShape::Linear, curvature: 0.0 };
pl.clips.push(c);
}
}
assert_eq!(delete_fades(&mut s, t, Range::new(900, 1200)), 2);
assert_eq!(clips(&s, t), vec![(0, 1050, 0), (1050, 950, 1050), (5000, 1100, 5000), (6000, 1000, 6000)]);
let c = s.track(t).unwrap().clips();
assert_eq!((c[1].fade_in.len, c[2].fade_out.len, c[3].fade_in.len), (0, 100, 100));
}
}
+533 -34
View File
@@ -8,6 +8,7 @@ use soundcraft_time::{Range, Samples};
use std::ffi::OsStr;
use std::path::{Path, PathBuf};
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, Ordering};
/// Largest import we accept (samples per channel × channels), ~4 GiB of f32.
pub(crate) const MAX_IMPORT_SAMPLES: usize = 1 << 30;
@@ -117,6 +118,11 @@ pub fn add_source(s: &mut Session, name: &str, mut buf: AudioBuffer, path: Optio
buf.channels = soundcraft_dsp::offline::resample(&buf.channels, buf.sample_rate, sr);
buf.sample_rate = sr;
}
add_source_audio(s, name, Arc::new(SourceAudio::new(buf)), path, format)
}
/// Register audio already at the session rate, with its peaks built, as a new source.
fn add_source_audio(s: &mut Session, name: &str, audio: Arc<SourceAudio>, path: Option<&str>, format: FileFormat) -> SourceId {
let id = SourceId(s.alloc());
let stem = Path::new(name).file_stem().and_then(OsStr::to_str).unwrap_or(name).to_string();
let stored = match path {
@@ -127,14 +133,14 @@ pub fn add_source(s: &mut Session, name: &str, mut buf: AudioBuffer, path: Optio
id,
name: stem.clone(),
path: stored,
channels: u16::try_from(buf.num_channels()).unwrap_or(u16::MAX),
frames: buf.frames() as u64,
sample_rate: sr,
channels: u16::try_from(audio.buffer.num_channels()).unwrap_or(u16::MAX),
frames: audio.frames() as u64,
sample_rate: s.sample_rate.hz(),
format,
time_reference: 0,
unsaved: true,
});
s.pool.insert(id, Arc::new(SourceAudio::new(buf)));
s.pool.insert(id, audio);
id
}
@@ -147,20 +153,69 @@ fn stamp_at_rate(stamp: u64, file_rate: u32, session_rate: u32) -> u64 {
u64::try_from((u128::from(stamp) * to + from / 2) / from).unwrap_or(u64::MAX)
}
/// Decode and place an audio file. With no target track, a new track of matching width is made.
pub fn import_audio_bytes(e: &mut Engine, name: &str, bytes: &[u8], path: Option<&str>, target: Option<TrackId>, at: Samples) -> Result<Value> {
/// An audio file decoded, resampled to a session rate and given its waveform peaks: everything an
/// import costs except placing the clip. Making one needs no engine, so the app makes it on a
/// worker thread ([`decode_import`]) and hands it over with [`Engine::offer_import`] before it
/// runs `file.import_audio`, which then only places the clip (issue #597).
#[derive(Debug, Clone)]
pub struct DecodedImport {
pub(crate) key: PathBuf,
pub(crate) rate: u32,
info: soundcraft_audio_io::AudioInfo,
audio: Arc<SourceAudio>,
}
impl DecodedImport {
/// Frames at the session rate it was decoded for.
pub fn frames(&self) -> usize {
self.audio.frames()
}
}
/// Reads and decodes the audio file at `path` for a session at `rate`. Thread-safe and slow (the
/// whole file is read, decoded, resampled and given peaks): run it off the UI thread.
pub fn decode_import(path: &str, rate: u32) -> Result<DecodedImport> {
let bytes = read_file(path)?;
let name = Path::new(path).file_name().and_then(OsStr::to_str).unwrap_or("Audio");
let (info, audio) = decode_import_bytes(name, &bytes, rate)?;
Ok(DecodedImport { key: file_key(Path::new(path)), rate, info, audio })
}
fn decode_import_bytes(name: &str, bytes: &[u8], rate: u32) -> Result<(soundcraft_audio_io::AudioInfo, Arc<SourceAudio>)> {
let ext = Path::new(name).extension().and_then(OsStr::to_str).map(str::to_ascii_lowercase);
let (info, buf) = soundcraft_audio_io::decode(bytes, ext.as_deref()).map_err(|err| EngineError::Io(format!("{name}: {err}")))?;
let buf = at_rate(buf, e.session().sample_rate.hz(), MAX_IMPORT_SAMPLES)
.ok_or_else(|| EngineError::Io(format!("{name}: file is too large to import")))?;
let buf = at_rate(buf, rate, MAX_IMPORT_SAMPLES).ok_or_else(|| EngineError::Io(format!("{name}: file is too large to import")))?;
if buf.frames() == 0 {
return Err(EngineError::Io(format!("{name}: no audio")));
}
let channels = buf.num_channels();
Ok((info, Arc::new(SourceAudio::new(buf))))
}
/// Decode and place an audio file. With no target track, a new track of matching width is made.
pub fn import_audio_bytes(e: &mut Engine, name: &str, bytes: &[u8], path: Option<&str>, target: Option<TrackId>, at: Samples) -> Result<Value> {
let (info, audio) = decode_import_bytes(name, bytes, e.session().sample_rate.hz())?;
place_import(e, name, &info, audio, path, target, at)
}
/// Place audio from [`decode_import`] (made for this session's rate) as `import_audio_bytes` does.
pub(crate) fn place_decoded(e: &mut Engine, name: &str, d: DecodedImport, path: Option<&str>, target: Option<TrackId>, at: Samples) -> Result<Value> {
place_import(e, name, &d.info, d.audio, path, target, at)
}
fn place_import(
e: &mut Engine,
name: &str,
info: &soundcraft_audio_io::AudioInfo,
audio: Arc<SourceAudio>,
path: Option<&str>,
target: Option<TrackId>,
at: Samples,
) -> Result<Value> {
let channels = audio.buffer.num_channels();
let file_rate = info.sample_rate;
let s = e.session_mut();
let session_rate = s.sample_rate.hz();
let src = add_source(s, name, buf, path, info.format);
let src = add_source_audio(s, name, audio, path, info.format);
if let (Some(bwf), Some(sx)) = (info.bwf.as_ref(), s.sources.last_mut()) {
sx.time_reference = stamp_at_rate(bwf.time_reference, file_rate, session_rate);
}
@@ -679,23 +734,126 @@ pub fn transients_in(s: &Session, t: TrackId, r: Range, sens: f32) -> Vec<Sample
soundcraft_dsp::offline::detect_transients(&audio, s.sample_rate.as_f64() as f32, sens).into_iter().map(|i| r.start + i as i64).collect()
}
/// Strip Silence: keep only the non-silent ranges of clips inside `r`.
pub fn strip_silence(s: &mut Session, t: TrackId, r: Range, thr: f32, min_len: Samples, pre: Samples, post: Samples) -> Result<usize> {
/// What Strip Silence does with the clips inside the selection once it has found the silence.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum StripMode {
/// Remove the silent parts and keep the parts that hold audio.
Strip,
/// Remove the parts that hold audio and keep the silent parts.
Extract,
/// Separate the clips at the boundaries of the silent parts without removing any clip.
Separate,
}
impl StripMode {
pub fn from_id(id: &str) -> Option<Self> {
match id {
"strip" => Some(Self::Strip),
"extract" => Some(Self::Extract),
"separate" => Some(Self::Separate),
_ => None,
}
}
}
/// The names Strip Silence gives the clips it leaves in the selection: `name`, then `zeros` zeros,
/// then a number that starts at `number` and counts up one clip at a time, then `suffix`.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct StripNames {
pub name: String,
pub number: u64,
pub zeros: usize,
pub suffix: String,
}
impl Default for StripNames {
fn default() -> Self {
Self { name: String::new(), number: 1, zeros: 0, suffix: String::new() }
}
}
impl StripNames {
/// The largest first number.
pub const MAX_NUMBER: u64 = soundcraft_model::AutoName::MAX_NUMBER;
/// The most zeros before the number.
pub const MAX_ZEROS: usize = soundcraft_model::AutoName::MAX_ZEROS;
/// The name of the clip `i` places after the first one.
pub fn clip_name(&self, i: u64) -> String {
format!("{}{}{}{}", self.name, "0".repeat(self.zeros), self.number.saturating_add(i), self.suffix)
}
}
impl From<&soundcraft_model::AutoName> for StripNames {
fn from(a: &soundcraft_model::AutoName) -> Self {
Self { name: a.name.clone(), number: a.number, zeros: a.zeros, suffix: a.suffix.clone() }
}
}
impl From<StripNames> for soundcraft_model::AutoName {
fn from(n: StripNames) -> Self {
Self { name: n.name, number: n.number, zeros: n.zeros, suffix: n.suffix, id: 0 }
}
}
/// Names the audio clips that lie inside `r` on `t` with `names`, in time order, the first of them
/// `next` places after `names.number`. Returns how many clips it named.
pub fn name_stripped_clips(s: &mut Session, t: TrackId, r: Range, names: &StripNames, next: u64) -> u64 {
let r = bounded_to_track(s, t, r);
let keep = non_silent(s, t, r, thr, min_len, pre, post)?;
let Some(pl) = s.track_mut(t).and_then(|x| x.playlist_mut()) else { return 0 };
let mut inside: Vec<&mut Clip> = pl.clips.iter_mut().filter(|c| c.is_audio() && c.start >= r.start && c.end() <= r.end).collect();
inside.sort_by_key(|c| (c.start, c.id.0));
let mut named = 0;
for c in inside {
c.name = names.clip_name(next.saturating_add(named));
named += 1;
}
named
}
/// Strip Silence: separate the clips inside `r` where silence starts and ends, then keep only the
/// non-silent ranges (`Strip`), only the silent ranges (`Extract`), or every piece (`Separate`).
/// `pad` is the clip start and end pad added around each non-silent range. Returns the number of
/// ranges kept: non-silent ranges for `Strip` and `Separate`, silent ranges that still hold a clip for
/// `Extract`. The new pieces that are kept take Auto Rename names in time order, and the removed
/// ones do not. When `named` is true the caller names the audio clips left inside `r` with
/// [`name_stripped_clips`], so those clips do not take Auto Rename names either.
#[allow(clippy::too_many_arguments)]
pub fn strip_silence(
s: &mut Session,
t: TrackId,
r: Range,
thr: f32,
min_len: Samples,
pad: (Samples, Samples),
mode: StripMode,
named: bool,
) -> Result<usize> {
let r = bounded_to_track(s, t, r);
let keep = non_silent(s, t, r, thr, min_len, pad, &AtomicBool::new(false))?;
let mut cut_points = vec![r.start];
for (a, b) in &keep {
cut_points.push(*a);
cut_points.push(*b);
}
cut_points.push(r.end);
let old: std::collections::HashSet<_> = s.track(t).map(|x| x.clips().iter().map(|c| c.id).collect()).unwrap_or_default();
for p in &cut_points {
crate::edit::separate_at(s, t, *p);
crate::edit::separate_at_if(s, t, *p, |_| false);
}
let kept = strip_pieces(s, t, r, &keep, mode);
crate::edit::auto_name_new_clips(s, t, &old, |c| !(named && c.is_audio() && c.start >= r.start && c.end() <= r.end));
Ok(kept)
}
/// Removes the pieces that Strip Silence does not keep in `mode` and returns the number of ranges kept.
fn strip_pieces(s: &mut Session, t: TrackId, r: Range, keep: &[(Samples, Samples)], mode: StripMode) -> usize {
if mode == StripMode::Separate {
return keep.len();
}
// Remove pieces that fall in the gaps.
let mut gaps = Vec::new();
let mut prev = r.start;
for (a, b) in &keep {
for (a, b) in keep {
if *a > prev {
gaps.push(Range::new(prev, *a));
}
@@ -704,10 +862,56 @@ pub fn strip_silence(s: &mut Session, t: TrackId, r: Range, thr: f32, min_len: S
if prev < r.end {
gaps.push(Range::new(prev, r.end));
}
if let Some(pl) = s.track_mut(t).and_then(|x| x.playlist_mut()) {
pl.clips.retain(|c| !gaps.iter().any(|g| c.start >= g.start && c.end() <= g.end));
let audio: Vec<Range> = keep.iter().map(|(a, b)| Range::new(*a, *b)).collect();
let remove = if mode == StripMode::Extract { &audio } else { &gaps };
let inside = |c: &Clip, g: &Range| c.start >= g.start && c.end() <= g.end;
let Some(pl) = s.track_mut(t).and_then(|x| x.playlist_mut()) else { return if mode == StripMode::Extract { 0 } else { keep.len() } };
pl.clips.retain(|c| !remove.iter().any(|g| inside(c, g)));
if mode == StripMode::Extract {
return gaps.iter().filter(|g| pl.clips.iter().any(|c| inside(c, g))).count();
}
keep.len()
}
/// The ranges [`strip_silence`] finds on `t` inside `r` with the same settings: the non-silent ranges
/// with `pad` added, which Strip keeps. The session is read and not changed. Setting `stop` ends the
/// scan with an error before the next window of audio is rendered.
pub fn strip_silence_ranges(
s: &Session,
t: TrackId,
r: Range,
thr: f32,
min_len: Samples,
pad: (Samples, Samples),
stop: &AtomicBool,
) -> Result<Vec<Range>> {
let r = bounded_to_track(s, t, r);
Ok(non_silent(s, t, r, thr, min_len, pad, stop)?.into_iter().map(|(a, b)| Range::new(a, b)).collect())
}
/// A Strip Silence scan of a session's tracks, which can run on another thread
/// (`cmd::strip_silence_scan` makes one from Strip Silence's params).
#[derive(Clone)]
pub struct StripScan {
pub session: Arc<Session>,
pub tracks: Vec<TrackId>,
pub range: Range,
pub threshold_db: f32,
pub min_len: Samples,
pub pad: (Samples, Samples),
/// Set from another thread to end [`StripScan::run`] early, with an error.
pub stop: Arc<AtomicBool>,
}
impl StripScan {
/// The ranges Strip keeps on each track ([`strip_silence_ranges`]).
pub fn run(&self) -> Result<Vec<(TrackId, Vec<Range>)>> {
let s = &self.session;
self.tracks
.iter()
.map(|t| Ok((*t, strip_silence_ranges(s, *t, self.range, self.threshold_db, self.min_len, self.pad, &self.stop)?)))
.collect()
}
Ok(keep.len())
}
/// Most samples (over all channels) Strip Silence renders at once.
@@ -715,8 +919,17 @@ const STRIP_WINDOW_SAMPLES: usize = 1 << 24;
/// The non-silent ranges of `t`'s clips in `r`. The audio is rendered a window at a time where
/// audio clips play, so all of `r` is scanned however long it is (one render stops at
/// `MAX_RENDER_SAMPLES`); at most a day of clips, Pro Tools' longest session.
fn non_silent(s: &Session, t: TrackId, r: Range, thr: f32, min_len: Samples, pre: Samples, post: Samples) -> Result<Vec<(Samples, Samples)>> {
/// `MAX_RENDER_SAMPLES`); at most a day of clips, Pro Tools' longest session. `pad` is the clip start
/// and end pad. Setting `stop` ends the scan with an error before the next window is rendered.
fn non_silent(
s: &Session,
t: TrackId,
r: Range,
thr: f32,
min_len: Samples,
(pre, post): (Samples, Samples),
stop: &AtomicBool,
) -> Result<Vec<(Samples, Samples)>> {
let Some(tr) = s.track(t) else { return Ok(Vec::new()) };
let mut clips: Vec<Range> = tr.clips().iter().filter(|c| c.is_audio() && !c.muted).filter_map(|c| c.range().intersect(&r)).collect();
clips.sort_by_key(|x| x.start);
@@ -738,6 +951,9 @@ fn non_silent(s: &Session, t: TrackId, r: Range, thr: f32, min_len: Samples, pre
scan.skip(len(x.start - at));
at = x.start;
while at < x.end {
if stop.load(Ordering::Relaxed) {
return Err(EngineError::Disabled("edit.strip_silence_preview".into(), "the scan was stopped".into()));
}
let end = at.saturating_add(window).min(x.end);
scan.push(&soundcraft_mix::render_clips(s, t, Range::new(at, end)));
at = end;
@@ -832,12 +1048,27 @@ pub fn commit_track(e: &mut Engine, t: TrackId) -> Result<Option<TrackId>> {
if end <= 0 {
return Ok(None);
}
let range = Range::new(0, end + s.sample_rate.samples(2.0));
// The track renders through a main mix of at least two channels.
refuse_longer(s, "track.commit", &tr.name, "commit", range.len(), render_limit(tr.channels().max(2)))?;
let fmt = tr.format;
if fmt.channels() > soundcraft_mix::MAX_CHANNELS {
return Err(EngineError::BadParams(
"track.commit".into(),
format!("track commit supports at most {} channels; cannot render {}", soundcraft_mix::MAX_CHANNELS, fmt.label()),
));
}
let too_long = || EngineError::BadParams("track.commit".into(), format!("`{}` is too long to commit within the offline render limit", tr.name));
let end = end.checked_add(s.sample_rate.samples(2.0)).ok_or_else(too_long)?;
let frames = usize::try_from(end).map_err(|_| too_long())?;
// The shared renderer uses a stereo main mix for mono, then folds it back to mono.
// Reject requests it would truncate before rendering or replacing the source track.
if frames > soundcraft_mix::MAX_RENDER_SAMPLES / fmt.channels().max(2) {
return Err(too_long());
}
let range = Range::new(0, end);
let audio = soundcraft_mix::render_track_pre_fader(s, t, range);
if audio.len() != fmt.channels() || audio.iter().any(|channel| channel.len() != frames) {
return Err(EngineError::Io("track commit did not render the complete channel format and range".into()));
}
let name = format!("{}.cm", tr.name);
let fmt = ChannelFormat::for_channels(audio.len().clamp(1, 2));
let mixer = tr.mixer.clone();
let buf = AudioBuffer { sample_rate: s.sample_rate.hz(), channels: audio };
let s = e.session_mut();
@@ -1211,9 +1442,15 @@ pub(crate) fn decode_file(files: &mut DecodedFiles, p: &Path, rate: u32) -> std:
/// saves and opens sessions as bundles.
pub const BUNDLE_EXTENSION: &str = "scraftz";
/// Most entries and bytes a bundle may unpack to (bundles are hostile input).
const MAX_BUNDLE_ENTRIES: usize = 10_000;
const MAX_BUNDLE_BYTES: u64 = 1 << 30;
/// 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).
#[derive(Clone, Copy, Debug)]
struct BundleLimits {
entries: usize,
bytes: u64,
}
const BUNDLE_LIMITS: BundleLimits = BundleLimits { entries: 10_000, bytes: 1 << 30 };
/// Hands a finished file (name, bytes) to the person on a host without a filesystem: the web app
/// installs one that starts a download. Native builds write files and never install it.
@@ -1242,6 +1479,11 @@ pub fn write_file(path: &str, bytes: &[u8]) -> Result<()> {
/// `Audio Files/` (stored, not compressed: audio barely compresses). Returns the bytes and the
/// session with its source paths pointing into the bundle.
pub fn session_bundle(session: &Session) -> Result<(Vec<u8>, Session)> {
session_bundle_within(session, BUNDLE_LIMITS)
}
/// [`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}"));
@@ -1249,6 +1491,20 @@ pub fn session_bundle(session: &Session) -> Result<(Vec<u8>, Session)> {
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);
@@ -1260,6 +1516,7 @@ pub fn session_bundle(session: &Session) -> Result<(Vec<u8>, Session)> {
}
let opts_wav = EncodeOptions { format: FileFormat::Wav, bit_depth: soundcraft_audio_io::BitDepth::Float32, dither: false, bwf: None };
let bytes = soundcraft_audio_io::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());
@@ -1268,6 +1525,7 @@ pub fn session_bundle(session: &Session) -> Result<(Vec<u8>, Session)> {
src.format = FileFormat::Wav;
}
let text = s.to_json().map_err(|err| EngineError::Io(err.to_string()))?;
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();
@@ -1296,7 +1554,7 @@ fn save_session_bundle(e: &mut Engine, path: &str) -> Result<usize> {
/// [`open_session`].
pub fn open_session_bytes(e: &mut Engine, name: &str, bytes: &[u8]) -> Result<Vec<String>> {
let (text, mut files) = if bytes.starts_with(b"PK\x03\x04") {
read_bundle(bytes)?
read_bundle(bytes, BUNDLE_LIMITS)?
} else {
(
String::from_utf8(bytes.to_vec()).map_err(|_| EngineError::Io(format!("{name}: not a SoundCraft session")))?,
@@ -1344,11 +1602,11 @@ pub fn open_session_bytes(e: &mut Engine, name: &str, bytes: &[u8]) -> Result<Ve
}
/// Unpack a bundle: the one `.scraft` at its top level and the files beside it, by relative path.
fn read_bundle(bytes: &[u8]) -> Result<(String, std::collections::HashMap<String, Vec<u8>>)> {
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() > MAX_BUNDLE_ENTRIES {
if z.len() > limits.entries {
return Err(EngineError::Io("bundle: too many files".into()));
}
let (mut text, mut files, mut total) = (None, std::collections::HashMap::new(), 0u64);
@@ -1360,7 +1618,7 @@ fn read_bundle(bytes: &[u8]) -> Result<(String, std::collections::HashMap<String
let rel = f.name().map_err(zerr)?.to_string();
let size = f.size();
total = total.saturating_add(size);
if total > MAX_BUNDLE_BYTES {
if total > limits.bytes {
return Err(EngineError::Io("bundle: too large".into()));
}
let mut data = Vec::new();
@@ -1756,7 +2014,7 @@ mod save_tests {
let mut r = Engine::default();
assert_eq!(
open_session(&mut r, &path("B/Moved.scraft")).unwrap(),
vec![dir.join("A").join("Audio Files/Audio 1_02.wav").display().to_string()]
vec![dir.join("A").join("Audio Files").join("Audio 1_02.wav").display().to_string()]
);
let _ = std::fs::remove_dir_all(&dir);
}
@@ -2107,6 +2365,49 @@ mod stem_tests {
assert_eq!(n, 7, "{r}");
let _ = std::fs::remove_dir_all(&dir);
}
/// The largest gap between the 16-bit stem in `dir16` and the 32-bit float one in `dir32`, in LSBs.
fn max_error_lsb(dir16: &std::path::Path, dir32: &std::path::Path) -> f64 {
let mut worst = 0.0f64;
let mut names: Vec<_> = std::fs::read_dir(dir32).unwrap().map(|f| f.unwrap().file_name()).collect();
names.sort();
assert!(!names.is_empty());
for name in names {
let (_, float) = soundcraft_audio_io::decode(&std::fs::read(dir32.join(&name)).unwrap(), None).unwrap();
let (_, int) = soundcraft_audio_io::decode(&std::fs::read(dir16.join(&name)).unwrap(), None).unwrap();
for (f, i) in float.channels.iter().zip(&int.channels) {
for (a, b) in f.iter().zip(i) {
worst = worst.max((f64::from(a.clamp(-1.0, 1.0)) - f64::from(*b)).abs() * 32768.0);
}
}
}
worst
}
#[test]
fn stems_dither_only_when_asked() {
let mut e = crate::demo::demo_engine();
let root = std::env::temp_dir().join(format!("soundcraft-stems-dither-{}", std::process::id()));
let mut bounce = |name: &str, extra: serde_json::Value| {
let dir = root.join(name);
let mut p = serde_json::json!({"dir": dir.to_string_lossy(), "start": {"seconds": 10.0}, "end": {"seconds": 10.5}});
for (k, v) in extra.as_object().unwrap() {
p[k] = v.clone();
}
e.execute("file.bounce_stems", &p).unwrap();
dir
};
let float = bounce("float", serde_json::json!({"bit_depth": "32f"}));
let plain = bounce("plain", serde_json::json!({"bit_depth": "16", "dither": false}));
let dithered = bounce("dithered", serde_json::json!({"bit_depth": "16"}));
// Without dither every sample is the float one rounded to the nearest step; TPDF dither of
// ±1 LSB moves some of them further than half a step.
let plain_error = max_error_lsb(&plain, &float);
let dithered_error = max_error_lsb(&dithered, &float);
assert!(plain_error <= 0.5 + 1e-3, "{plain_error}");
assert!(dithered_error > 0.6, "{dithered_error}");
let _ = std::fs::remove_dir_all(&root);
}
}
#[cfg(test)]
@@ -2138,6 +2439,189 @@ mod strip_tests {
assert!(e.session().track(t).unwrap().clips().iter().any(|c| c.start >= far));
}
/// A stereo track with one three-second clip: a second of tone, a second of silence, a second of tone.
fn tone_gap_tone() -> (Engine, TrackId) {
let mut e = Engine::default();
e.execute("track.new", &json!({"format": "stereo"})).unwrap();
let t = e.session().tracks[0].id;
let s = e.session_mut();
let mut ch = vec![0.5; 144_000];
ch[48_000..96_000].fill(0.0);
let src = add_source(s, "tone gap tone", AudioBuffer { sample_rate: 48_000, channels: vec![ch.clone(), ch] }, None, FileFormat::Wav);
let id = s.new_clip_id();
crate::edit::place_clip(s, t, Clip::audio(id, "tone gap tone", src, 0, 0, 144_000));
(e, t)
}
fn strip_mode(mode: &str) -> (Value, Vec<(Samples, Samples)>) {
let (mut e, t) = tone_gap_tone();
let p = json!({"track": t.0, "start": 0, "end": 144_000, "pad_before_ms": 0, "pad_after_ms": 0, "mode": mode});
let r = e.execute("edit.strip_silence", &p).unwrap();
(r, e.session().track(t).unwrap().clips().iter().map(|c| (c.start, c.end())).collect())
}
#[test]
fn strip_keeps_the_audio_and_removes_the_silence() {
let (r, clips) = strip_mode("strip");
assert_eq!(r["clips"], 2);
assert_eq!(clips, vec![(0, 48_000), (96_000, 144_000)]);
}
#[test]
fn extract_keeps_the_silence_and_removes_the_audio() {
let (r, clips) = strip_mode("extract");
assert_eq!(r["clips"], 1);
assert_eq!(clips, vec![(48_000, 96_000)]);
}
#[test]
fn separate_cuts_at_the_silence_and_removes_nothing() {
let (r, clips) = strip_mode("separate");
assert_eq!(r["clips"], 2);
assert_eq!(clips, vec![(0, 48_000), (48_000, 96_000), (96_000, 144_000)]);
}
#[test]
fn the_rectangles_are_what_strip_keeps_and_change_nothing() {
let (mut e, t) = tone_gap_tone();
let p = json!({"track": t.0, "start": 0, "end": 144_000, "pad_before_ms": 10, "pad_after_ms": 20});
let rev = e.revision;
let r = e.execute("edit.strip_silence_preview", &p).unwrap();
assert_eq!(e.revision, rev, "the preview changed the session");
assert_eq!(e.session().track(t).unwrap().clips().len(), 1);
assert_eq!(r["tracks"][0]["track"], t.0);
let ranges: Vec<(Samples, Samples)> =
r["tracks"][0]["ranges"].as_array().unwrap().iter().map(|x| (x[0].as_i64().unwrap(), x[1].as_i64().unwrap())).collect();
assert_eq!(ranges, vec![(0, 48_960), (95_520, 144_000)]);
e.execute("edit.strip_silence", &p).unwrap();
let kept: Vec<(Samples, Samples)> = e.session().track(t).unwrap().clips().iter().map(|c| (c.start, c.end())).collect();
assert_eq!(kept, ranges);
}
#[test]
fn a_stopped_scan_ends_with_an_error() {
let (e, t) = tone_gap_tone();
let scan = crate::cmd::strip_silence_scan(&e, &json!({"track": t.0, "start": 0, "end": 144_000})).unwrap();
assert_eq!(scan.run().unwrap()[0].1.len(), 2);
scan.stop.store(true, Ordering::Relaxed);
assert!(scan.run().is_err());
}
#[test]
fn the_rectangles_follow_the_threshold() {
let (mut e, t) = tone_gap_tone();
// The tone plays at -6 dB.
let r = e.execute("edit.strip_silence_preview", &json!({"track": t.0, "start": 0, "end": 144_000, "threshold_db": -3})).unwrap();
assert_eq!(r["tracks"][0]["ranges"], json!([]));
let r = e.execute("edit.strip_silence_preview", &json!({"track": t.0, "start": 0, "end": 144_000, "threshold_db": -12})).unwrap();
assert_eq!(r["tracks"][0]["ranges"].as_array().map(Vec::len), Some(2));
}
#[test]
fn an_unknown_strip_mode_is_refused() {
let (mut e, t) = tone_gap_tone();
for mode in [json!("remove"), json!(null), json!(true), json!(1)] {
assert!(e.execute("edit.strip_silence", &json!({"track": t.0, "start": 0, "end": 144_000, "mode": mode})).is_err());
assert_eq!(e.session().track(t).unwrap().clips().len(), 1);
}
}
fn names(e: &Engine, t: TrackId) -> Vec<String> {
e.session().track(t).unwrap().clips().iter().map(|c| c.name.clone()).collect()
}
#[test]
fn strip_names_follow_the_guide_example() {
let n = StripNames { name: "SFX".into(), number: 23, zeros: 1, suffix: ".Reel1".into() };
assert_eq!((0..4).map(|i| n.clip_name(i)).collect::<Vec<_>>(), ["SFX023.Reel1", "SFX024.Reel1", "SFX025.Reel1", "SFX026.Reel1"]);
}
#[test]
fn strip_names_the_clips_it_keeps() {
let (mut e, t) = tone_gap_tone();
let p = json!({"track": t.0, "start": 0, "end": 144_000, "pad_before_ms": 0, "pad_after_ms": 0, "name": "SFX", "number": 23, "zeros": 1, "suffix": ".Reel1"});
let r = e.execute("edit.strip_silence", &p).unwrap();
assert_eq!(r["named"], 2);
assert_eq!(names(&e, t), ["SFX023.Reel1", "SFX024.Reel1"]);
}
#[test]
fn separate_names_every_piece_in_time_order() {
let (mut e, t) = tone_gap_tone();
let p = json!({"track": t.0, "start": 0, "end": 144_000, "pad_before_ms": 0, "pad_after_ms": 0, "mode": "separate", "name": "Take "});
e.execute("edit.strip_silence", &p).unwrap();
assert_eq!(names(&e, t), ["Take 1", "Take 2", "Take 3"]);
}
#[test]
fn strip_numbers_run_on_from_one_track_to_the_next() {
let (mut e, a) = tone_gap_tone();
e.execute("track.new", &json!({"format": "stereo"})).unwrap();
let b = e.session().tracks[1].id;
let s = e.session_mut();
let src = s.sources[0].id;
let id = s.new_clip_id();
crate::edit::place_clip(s, b, Clip::audio(id, "copy", src, 0, 0, 144_000));
let p = json!({"tracks": [a.0, b.0], "start": 0, "end": 144_000, "pad_before_ms": 0, "pad_after_ms": 0, "name": "Hit", "zeros": 2});
let r = e.execute("edit.strip_silence", &p).unwrap();
assert_eq!(r["named"], 4);
assert_eq!(names(&e, a), ["Hit001", "Hit002"]);
assert_eq!(names(&e, b), ["Hit003", "Hit004"]);
}
#[test]
fn strip_without_a_name_keeps_the_clip_names() {
let (mut e, t) = tone_gap_tone();
let r = e.execute("edit.strip_silence", &json!({"track": t.0, "start": 0, "end": 144_000, "name": " "})).unwrap();
assert_eq!(r["named"], 0);
assert_eq!(names(&e, t), ["tone gap tone", "tone gap tone"]);
}
#[test]
fn strip_names_leave_the_auto_rename_sequence_unbroken() {
let (mut e, t) = tone_gap_tone();
let id = e.session().track(t).unwrap().clips()[0].id;
e.execute("clip.auto_rename", &json!({"clips": [id], "name": "Take"})).unwrap();
let p = json!({"track": t.0, "start": 0, "end": 120_000, "pad_before_ms": 0, "pad_after_ms": 0, "mode": "separate", "name": "SFX"});
assert_eq!(e.execute("edit.strip_silence", &p).unwrap()["named"], 3);
// Strip Silence does not name the piece after the range, so that piece takes the first Auto Rename name.
assert_eq!(names(&e, t), ["SFX1", "SFX2", "SFX3", "Take1"]);
e.execute("edit.separate", &json!({"tracks": [t], "at": 132_000})).unwrap();
assert_eq!(names(&e, t)[4], "Take2");
}
#[test]
fn only_the_pieces_strip_silence_keeps_take_auto_rename_numbers() {
for (mode, kept) in [("strip", ["tone gap tone", "Take1", "Take2"].as_slice()), ("extract", ["Take1", "Take2"].as_slice())] {
let (mut e, t) = tone_gap_tone();
let id = e.session().track(t).unwrap().clips()[0].id;
e.execute("clip.auto_rename", &json!({"clips": [id], "name": "Take"})).unwrap();
let p = json!({"track": t.0, "start": 0, "end": 120_000, "pad_before_ms": 0, "pad_after_ms": 0, "mode": mode});
e.execute("edit.strip_silence", &p).unwrap();
assert_eq!(names(&e, t), kept, "{mode}");
}
}
#[test]
fn bad_naming_params_are_refused() {
for bad in [
json!({"suffix": ".Reel1"}),
json!({"name": "", "number": 2}),
json!({"name": 7}),
json!({"name": "SFX", "number": -1}),
json!({"name": "SFX", "number": StripNames::MAX_NUMBER + 1}),
json!({"name": "SFX", "zeros": StripNames::MAX_ZEROS + 1}),
json!({"name": "SFX", "suffix": null}),
json!({"name": "S".repeat(300)}),
] {
let (mut e, t) = tone_gap_tone();
let mut p = json!({"track": t.0, "start": 0, "end": 144_000});
p.as_object_mut().unwrap().extend(bad.as_object().unwrap().clone());
assert!(e.execute("edit.strip_silence", &p).is_err(), "{bad} was taken");
assert_eq!(names(&e, t), ["tone gap tone"], "{bad} changed the track");
}
}
#[test]
fn more_than_a_day_of_clips_is_refused() {
let (mut e, t) = tones(&[0], 48_000 * 86_401);
@@ -2175,7 +2659,7 @@ mod bundle_tests {
use std::io::Write as _;
let e = crate::demo::demo_engine();
let (bytes, _) = session_bundle(e.session()).unwrap();
let (text, files) = read_bundle(&bytes).unwrap();
let (text, files) = read_bundle(&bytes, BUNDLE_LIMITS).unwrap();
let mut s = Session::from_json(&text).unwrap();
s.sources[1].path = s.sources[0].path.clone();
let mut w = zip::ZipWriter::new(std::io::Cursor::new(Vec::new()));
@@ -2195,6 +2679,21 @@ mod bundle_tests {
}
}
#[test]
fn a_bundle_is_saved_only_if_it_opens_again() {
// #433: the writer keeps to the reader's limits.
let e = crate::demo::demo_engine();
let (bytes, _) = session_bundle(e.session()).unwrap();
let mut z = zip::ZipArchive::new(std::io::Cursor::new(&bytes)).unwrap();
let entries = z.len();
let size: u64 = (0..entries).map(|i| z.by_index(i).unwrap().size()).sum();
let exact = BundleLimits { entries, bytes: size };
let (at_limit, _) = session_bundle_within(e.session(), exact).unwrap();
assert!(read_bundle(&at_limit, exact).is_ok(), "what the writer takes, the reader opens");
assert!(session_bundle_within(e.session(), BundleLimits { entries: entries - 1, ..exact }).is_err());
assert!(session_bundle_within(e.session(), BundleLimits { bytes: size - 1, ..exact }).is_err());
}
#[test]
fn same_named_sources_get_distinct_files() {
let mut e = crate::demo::demo_engine();
+80
View File
@@ -33,6 +33,22 @@ pub fn shutdown_plugin_hosts() {
soundcraft_vst3_host::shutdown();
}
/// A new [`soundcraft_model::AutoName::id`] for a scheme in `s`: a hash of a counter, never 0 and
/// not held by another scheme in `s`. Native builds seed the hash from the system, so each run of
/// the app gives other ids.
pub fn new_scheme_id(s: &Session) -> u64 {
use std::hash::{BuildHasher, Hasher};
static NEXT: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(1);
loop {
let mut h = std::collections::hash_map::RandomState::new().build_hasher();
h.write_u64(NEXT.fetch_add(1, std::sync::atomic::Ordering::Relaxed));
let id = h.finish();
if id != 0 && !s.auto_names.values().any(|a| a.id == id) {
return id;
}
}
}
#[derive(Debug, Clone, PartialEq, thiserror::Error)]
pub enum EngineError {
#[error("unknown command `{0}`")]
@@ -94,6 +110,8 @@ pub struct Clipboard {
pub grid_offset: Samples,
/// Copy Special › Automation: pasting it leaves the clips alone, in Shuffle mode too.
pub automation_only: bool,
/// The Auto Rename schemes of the copied clips, keyed as in their `auto_name`.
pub auto_names: std::collections::BTreeMap<u64, soundcraft_model::AutoName>,
}
impl Clipboard {
@@ -102,6 +120,39 @@ impl Clipboard {
let line = s.edit.grid.line_at_or_before(range.start, s.sample_rate, &s.tempo, s.frame_rate);
Clipboard { length: range.len(), grid_offset: range.start.saturating_sub(line), ..Clipboard::default() }
}
/// Keep a copy of the Auto Rename schemes that the copied clips use, as `s` holds them.
pub fn keep_auto_names(&mut self, s: &Session) {
self.auto_names = self.tracks.iter().flatten().filter_map(|c| c.auto_name).filter_map(|k| Some((k, s.auto_names.get(&k)?.clone()))).collect();
}
/// Before a paste into `s`: point the copied clips at schemes that `s` holds. A scheme that `s`
/// holds with the same [`AutoName::id`](soundcraft_model::AutoName::id), name, zeros and suffix
/// is used as it is, and its numbers count on. Any other goes into `s` under a new key, counting
/// on from where it was when it was copied.
pub fn adopt_auto_names(&mut self, s: &mut Session) {
let mut keys = std::collections::BTreeMap::new();
for (k, a) in &self.auto_names {
let held = s
.auto_names
.iter()
.find(|(_, h)| a.id != 0 && h.id == a.id && h.name == a.name && h.zeros == a.zeros && h.suffix == a.suffix)
.map(|(key, _)| *key);
let key = match held {
Some(key) => key,
None => {
let key = s.alloc();
s.auto_names.insert(key, a.clone());
key
}
};
keys.insert(*k, key);
}
for c in self.tracks.iter_mut().flatten() {
c.auto_name = c.auto_name.and_then(|k| keys.get(&k).copied());
}
self.auto_names = keys.values().filter_map(|k| Some((*k, s.auto_names.get(k)?.clone()))).collect();
}
}
#[derive(Debug, Clone)]
@@ -110,6 +161,9 @@ struct HistoryEntry {
doc: Arc<Session>,
}
/// Most decoded imports [`Engine::offer_import`] holds at once (each can be gigabytes).
const MAX_OFFERED_IMPORTS: usize = 4;
/// Engine state: the document, history, clipboard and transport plumbing.
pub struct Engine {
doc: Arc<Session>,
@@ -139,6 +193,8 @@ pub struct Engine {
/// The edit selection (range, tracks) before the last change, for Restore Last Selection.
/// Kept out of the document so selecting never marks the session modified.
pub last_selection: Option<(soundcraft_time::Range, Vec<TrackId>)>,
/// Audio decoded off the UI thread, waiting for its `file.import_audio` (see `offer_import`).
imports: Vec<io::DecodedImport>,
}
impl Default for Engine {
@@ -169,9 +225,33 @@ impl Engine {
merge_hold: None,
messages: Vec::new(),
last_selection: None,
imports: Vec::new(),
}
}
/// Hands over audio [`io::decode_import`] made off the UI thread: the next `file.import_audio`
/// of the same file at the session's rate places it instead of reading the file again. The
/// command still checks the path may be read. Only the few newest are kept.
pub fn offer_import(&mut self, d: io::DecodedImport) {
self.imports.retain(|x| x.key != d.key);
self.imports.push(d);
let extra = self.imports.len().saturating_sub(MAX_OFFERED_IMPORTS);
self.imports.drain(..extra);
}
/// Drops audio offered for imports that never ran (the command failed or was not sent).
pub fn drop_offered_imports(&mut self) {
self.imports.clear();
}
/// The offered audio of the file at `path`, if it was decoded for the session's current rate.
pub(crate) fn take_import(&mut self, path: &std::path::Path) -> Option<io::DecodedImport> {
let key = io::file_key(path);
let rate = self.session().sample_rate.hz();
let i = self.imports.iter().position(|d| d.key == key && d.rate == rate)?;
Some(self.imports.remove(i))
}
pub fn session(&self) -> &Session {
&self.doc
}
+52
View File
@@ -5,6 +5,58 @@ use soundcraft_engine::io::{Bounce, bounce_stems};
use soundcraft_time::Range;
use std::path::PathBuf;
#[test]
fn float_bounce_preserves_headroom_and_reports_the_written_levels() {
let mut e = soundcraft_engine::Engine::default();
let sr = e.session().sample_rate.hz();
let frames = sr as usize + 17;
let samples: Vec<f32> = (0..frames).map(|i| 1.5 * (std::f32::consts::TAU * 440.0 * i as f32 / sr as f32).sin()).collect();
let audio = soundcraft_audio_io::AudioBuffer { sample_rate: sr, channels: vec![samples.clone(), samples] };
let s = e.session_mut();
// A new track has no inserts, so no limiter can mask encoder clipping.
let track = s.add_track(soundcraft_model::TrackKind::Audio, soundcraft_model::ChannelFormat::Stereo, Some("Headroom"));
let source = soundcraft_engine::io::add_source(s, "headroom", audio, None, FileFormat::Wav);
let clip = soundcraft_model::Clip::audio(s.new_clip_id(), "headroom", source, 0, 0, frames as i64);
soundcraft_engine::edit::place_clip(s, track, clip);
let mix = soundcraft_mix::render_range(e.session(), Range::new(0, frames as i64), 1024);
assert!(soundcraft_dsp::offline::peak(&mix) > 1.0);
let dir = scratch("bounce-float-headroom");
for extension in ["wav", "aiff"] {
for normalize in [false, true] {
let path = dir.join(format!("Mix.{extension}"));
let result = e
.execute(
"file.bounce_mix",
&serde_json::json!({
"path": path, "bit_depth": "32f", "dither": false, "normalize": normalize,
"start": 0, "end": frames,
}),
)
.unwrap();
let bytes = std::fs::read(&path).unwrap();
let (_, file) = soundcraft_audio_io::decode(&bytes, None).unwrap();
let mut expected = mix.clone();
if normalize {
soundcraft_dsp::offline::normalize(&mut expected, -0.1, false);
assert!(file.peak() < 1.0);
} else {
assert!(file.peak() > 1.0, "{extension}: headroom was clipped");
}
assert_eq!(file.channels, expected, "{extension}, normalize={normalize}");
let mut meter = soundcraft_dsp::meter::LoudnessMeter::new(sr as f32, file.num_channels());
meter.process(&file.channels, file.frames());
for (key, value) in [
("peak_db", soundcraft_dsp::offline::peak_db(&file.channels)),
("lufs", meter.integrated_lufs()),
("true_peak_db", meter.true_peak_db()),
] {
assert_eq!(result[key].as_f64(), Some(f64::from(value)), "{extension}, normalize={normalize}, {key}");
}
}
}
let _ = std::fs::remove_dir_all(&dir);
}
fn scratch(name: &str) -> PathBuf {
let dir = std::env::temp_dir().join(format!("soundcraft-{name}-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&dir);
@@ -0,0 +1,160 @@
use serde_json::json;
use soundcraft_audio_io::{AudioBuffer, BitDepth, EncodeOptions, FileFormat};
use soundcraft_engine::{Engine, io};
use soundcraft_model::{ChannelFormat, Clip, ClipContent, Insert, Route, SendSlot, Session, TrackId, TrackKind};
use soundcraft_time::{Range, SampleRate};
use std::sync::Arc;
const SIGNAL_FRAMES: usize = 128;
fn fixture(format: ChannelFormat) -> (Engine, TrackId, Vec<Vec<f32>>) {
let mut session = Session::new("Commit layout", SampleRate::new(8_000).unwrap());
session.outputs[0].format = format;
let track = session.add_track(TrackKind::Audio, format, Some("Distinct channels"));
// Each channel has a different bipolar waveform: swapping, summing or dropping channels
// cannot pass a width-only assertion or an identical-signal comparison.
let channels: Vec<Vec<f32>> = (0..format.channels())
.map(|ch| (0..SIGNAL_FRAMES).map(|frame| (((frame * (ch + 3)) % 31) as f32 - 15.0) * (ch + 1) as f32 / 3_200.0).collect())
.collect();
let source =
io::add_source(&mut session, "Distinct channels", AudioBuffer { sample_rate: 8_000, channels: channels.clone() }, None, FileFormat::Wav);
let clip = session.new_clip_id();
let tr = session.track_mut(track).unwrap();
tr.playlist_mut().unwrap().clips.push(Clip::audio(clip, "Signal", source, 0, 0, SIGNAL_FRAMES as i64));
tr.mixer.inserts[0] = Some(Insert::new("invert"));
session.edit.selected_tracks = vec![track];
(Engine::new(session), track, channels)
}
fn commit(e: &mut Engine, track: TrackId) -> TrackId {
TrackId(e.execute("track.commit", &json!({"tracks": [track.0]})).unwrap()["tracks"][0].as_u64().unwrap())
}
fn committed_audio(e: &Engine, track: TrackId) -> &AudioBuffer {
let ClipContent::Audio { source, .. } = e.session().track(track).unwrap().clips()[0].content else { panic!("commit must create audio") };
&e.session().pool.get(source).unwrap().buffer
}
fn assert_audio_eq(actual: &[Vec<f32>], expected: &[Vec<f32>]) {
assert_eq!(actual.len(), expected.len());
for (ch, (actual, expected)) in actual.iter().zip(expected).enumerate() {
assert_eq!(actual.len(), expected.len(), "channel {ch} length");
for (frame, (actual, expected)) in actual.iter().zip(expected).enumerate() {
assert!((actual - expected).abs() < 1e-6, "channel {ch}, frame {frame}: {actual} != {expected}");
}
}
}
#[test]
fn commit_keeps_every_renderable_exact_format_and_processed_channel_order() {
for format in ChannelFormat::ALL.into_iter().filter(|format| format.channels() <= soundcraft_mix::MAX_CHANNELS) {
let (mut e, track, signal) = fixture(format);
let committed = commit(&mut e, track);
assert_eq!(e.session().track(committed).unwrap().format, format, "{}", format.label());
let audio = committed_audio(&e, committed);
assert_eq!(audio.num_channels(), format.channels(), "{}", format.label());
assert_eq!(audio.frames(), SIGNAL_FRAMES + 16_000);
// Existing mono rendering uses a -3 dB center pan followed by SQRT_2 compensation.
// Model that small baseline gain explicitly, retaining the same strict signal tolerance.
let gain = if format == ChannelFormat::Mono { 10.0_f32.powf(-3.0 / 20.0) * std::f32::consts::SQRT_2 } else { 1.0 };
let expected: Vec<Vec<f32>> = signal.iter().map(|ch| ch.iter().map(|sample| -sample * gain).collect()).collect();
let head: Vec<Vec<f32>> = audio.channels.iter().map(|ch| ch[..SIGNAL_FRAMES].to_vec()).collect();
assert_audio_eq(&head, &expected);
assert!(audio.channels.iter().all(|ch| ch[SIGNAL_FRAMES..].iter().all(|sample| sample.abs() < 1e-6)));
assert!(e.session().track(track).unwrap().inactive && e.session().track(track).unwrap().hidden);
assert_eq!(e.session().track_index(committed), e.session().track_index(track).map(|index| index + 1));
}
}
#[test]
fn commit_preserves_bus_and_send_audio_after_undo_reopen_and_wav_export() {
for format in [ChannelFormat::Surround60, ChannelFormat::Atmos504, ChannelFormat::Ambisonics(1)] {
let (mut e, track, _) = fixture(format);
let s = e.session_mut();
let bus = s.add_bus("Same-layout bus", format);
let aux = s.add_track(TrackKind::Aux, format, Some("Bus return"));
s.track_mut(aux).unwrap().mixer.input = Route::Bus(bus);
let tr = s.track_mut(track).unwrap();
tr.mixer.output = Route::Bus(bus);
tr.mixer.volume_db = -4.5;
let mut send = SendSlot::new(Route::Main);
send.level_db = -9.0;
tr.mixer.sends[0] = Some(send);
let mixer = tr.mixer.clone();
let range = Range::new(0, SIGNAL_FRAMES as i64);
let before = soundcraft_mix::render_range(e.session(), range, 64);
let committed = commit(&mut e, track);
let tr = e.session().track(committed).unwrap();
assert_eq!(tr.mixer.output, mixer.output);
assert_eq!(tr.mixer.sends, mixer.sends);
assert_eq!(tr.mixer.volume_db, mixer.volume_db);
assert!(tr.mixer.inserts.iter().all(Option::is_none), "processing is baked once");
assert_audio_eq(&soundcraft_mix::render_range(e.session(), range, 64), &before);
assert!(e.undo());
assert!(e.session().track(committed).is_none());
assert!(!e.session().track(track).unwrap().inactive);
assert_audio_eq(&soundcraft_mix::render_range(e.session(), range, 64), &before);
assert!(e.redo());
let dir = std::env::temp_dir().join(format!("soundcraft-commit-layout-{}-{}", std::process::id(), format.channels()));
let _ = std::fs::remove_dir_all(&dir);
let path = dir.join("Commit.scraft").to_string_lossy().into_owned();
io::save_session(&mut e, &path).unwrap();
let mut reopened = Engine::default();
assert!(io::open_session(&mut reopened, &path).unwrap().is_empty());
assert_eq!(reopened.session().track(committed).unwrap().format, format);
assert_audio_eq(&committed_audio(&reopened, committed).channels, &committed_audio(&e, committed).channels);
assert_audio_eq(&soundcraft_mix::render_range(reopened.session(), range, 64), &before);
let opts = EncodeOptions { format: FileFormat::Wav, bit_depth: BitDepth::Float32, dither: false, bwf: None };
let wav_path = dir.join("mix.wav");
io::Bounce::new(reopened.session_arc(), &wav_path, range, &opts, None, false, false).unwrap().run().unwrap();
let wav = std::fs::read(&wav_path).unwrap();
let (_, decoded) = soundcraft_audio_io::decode(&wav, Some("wav")).unwrap();
assert_audio_eq(&decoded.channels, &before);
// The WAV extensible mask must describe this layout, not another layout of equal width.
let fmt_at = wav.windows(4).position(|tag| tag == b"fmt ").unwrap();
let mask = u32::from_le_bytes(wav[fmt_at + 28..fmt_at + 32].try_into().unwrap());
assert_eq!(mask, format.channel_mask());
std::fs::remove_dir_all(&dir).unwrap();
}
}
#[test]
fn oversized_or_overflowing_commit_rolls_back_all_selected_tracks() {
for (format, length) in [
(ChannelFormat::Mono, (soundcraft_mix::MAX_RENDER_SAMPLES / 2 + 1) as i64),
(ChannelFormat::Surround51, (soundcraft_mix::MAX_RENDER_SAMPLES / 6 + 1) as i64),
(ChannelFormat::Ambisonics(3), (soundcraft_mix::MAX_RENDER_SAMPLES / 16 + 1) as i64),
(ChannelFormat::Surround51, i64::MAX),
] {
let (mut e, first, _) = fixture(format);
let s = e.session_mut();
let second = s.add_track(TrackKind::Audio, format, Some("Too long"));
let mut clip = s.track(first).unwrap().clips()[0].clone();
clip.id = s.new_clip_id();
clip.length = length;
s.track_mut(second).unwrap().playlist_mut().unwrap().clips.push(clip);
let before = e.session_arc();
let dirty = e.is_dirty();
let result = e.execute("track.commit", &json!({"tracks": [first.0, second.0]}));
assert!(result.unwrap_err().to_string().contains("offline render limit"));
assert!(Arc::ptr_eq(&before, &e.session_arc()), "no hidden track, truncated source or partially committed first track");
assert_eq!(e.is_dirty(), dirty);
assert!(!e.can_undo(), "failed commit must not add an undo step");
}
}
#[test]
fn commit_rejects_formats_wider_than_the_renderer_without_replacing_tracks() {
for format in ChannelFormat::ALL.into_iter().filter(|format| format.channels() > soundcraft_mix::MAX_CHANNELS) {
let (mut e, track, _) = fixture(format);
let before = e.session_arc();
let result = e.execute("track.commit", &json!({"tracks": [track.0]}));
assert!(result.unwrap_err().to_string().contains("at most 16 channels"));
assert!(Arc::ptr_eq(&before, &e.session_arc()));
assert!(!e.session().track(track).unwrap().inactive);
assert!(!e.session().track(track).unwrap().hidden);
assert!(!e.can_undo());
}
}
+15
View File
@@ -48,6 +48,21 @@ fn audiosuite_refuses_results_longer_than_one_render() {
}
}
#[test]
fn speed_ramp_refuses_mono_clips_longer_than_one_render() {
// A mono clip may be longer than one offline buffer, so the speed ramp must refuse it rather than cut it short.
for (frames, selection) in [(MAX_OFFLINE_SAMPLES + 1, 0), (MAX_OFFLINE_SAMPLES * 2 / 3 + 1, 1)] {
let mut s = Session::default();
let t = s.add_track(TrackKind::Audio, ChannelFormat::Mono, Some("Talk"));
let buf = AudioBuffer { sample_rate: 48_000, channels: vec![vec![0.25; 48_000]] };
let src = soundcraft_engine::io::add_source(&mut s, "talk", buf, None, FileFormat::Wav);
let id = s.new_clip_id();
soundcraft_engine::edit::place_clip(&mut s, t, Clip::audio(id, "Talk", src, 0, 0, frames as i64));
let mut e = Engine::new(s);
refused(&mut e, "audiosuite.process", &json!({"process": "speed_ramp", "params": {"selection": selection}, "clips": [id.0]}));
}
}
#[test]
fn signal_generator_refuses_a_selection_longer_than_one_render() {
let (mut e, _) = long_clip(48_000);
+3
View File
@@ -2,6 +2,7 @@
mod bounce;
mod clap_e2e;
mod commit_multichannel;
mod consolidate_selection;
mod current_feet_frames_position;
mod edit_modes;
@@ -14,4 +15,6 @@ mod mix_required_params;
mod random_edits;
mod recording;
mod session_transport;
mod split_into_mono;
mod track_name_collisions;
mod trim_to_selection;
+159
View File
@@ -0,0 +1,159 @@
//! Split tracks into mono without replacing the right/surround channels with channel zero.
use serde_json::json;
use soundcraft_audio_io::{AudioBuffer, FileFormat};
use soundcraft_engine::{Engine, io};
use soundcraft_model::{ChannelFormat, Clip, Session, SourceAudio, SourceId, TrackId, TrackKind};
use soundcraft_time::Range;
use std::sync::Arc;
fn fixture(format: ChannelFormat, channels: usize) -> (Engine, TrackId, SourceId) {
let mut s = Session::default();
let track = s.add_track(TrackKind::Audio, format, Some("Original"));
let channels = (0..channels).map(|ch| (0..256).map(|i| (ch + 1) as f32 * 0.1 + i as f32 * 0.0001).collect()).collect();
let source = io::add_source(&mut s, "Original.wav", AudioBuffer { sample_rate: 48_000, channels }, None, FileFormat::Wav);
s.sources[0].time_reference = 123_456;
let mut clip = Clip::audio(s.new_clip_id(), "Edited", source, 7, 13, 47);
clip.gain_db = -6.0;
clip.gain_env = vec![(0, -3.0), (20, 0.0), (46, -9.0)];
clip.fade_in.len = 5;
clip.fade_out.len = 9;
clip.stretch = 1.25;
let mut second = clip.clone();
second.id = s.new_clip_id();
second.start = 100;
second.content = soundcraft_model::ClipContent::Audio { source, offset: 90 };
s.track_mut(track).unwrap().playlist_mut().unwrap().clips = vec![clip, second];
(Engine::new(s), track, source)
}
fn split(e: &mut Engine, track: TrackId) -> Vec<TrackId> {
let result = e.execute("track.split_into_mono", &json!({"track": track.0})).unwrap();
result["tracks"].as_array().unwrap().iter().map(|id| TrackId(id.as_u64().unwrap())).collect()
}
#[test]
fn split_into_mono_preserves_every_channel_and_clip_edit() {
for (format, channels) in [(ChannelFormat::Stereo, 2), (ChannelFormat::Surround51, 6)] {
let (mut e, track, source) = fixture(format, channels);
let original = e.session().track(track).unwrap().clone();
let audio = Arc::clone(e.session().pool.get(source).unwrap());
let expected = soundcraft_mix::render_clips(e.session(), track, Range::new(0, 192));
let tracks = split(&mut e, track);
assert_eq!(tracks.len(), channels);
assert_eq!(e.session().track(track).unwrap(), &original);
assert!(Arc::ptr_eq(e.session().pool.get(source).unwrap(), &audio));
// Two clips referencing the same source share one extracted source per channel.
assert_eq!(e.session().sources.len(), 1 + channels);
for (ch, &id) in tracks.iter().enumerate() {
let mono = e.session().track(id).unwrap();
assert_eq!(mono.format, ChannelFormat::Mono);
let actual = soundcraft_mix::render_clips(e.session(), id, Range::new(0, 192));
assert_eq!(actual[0], expected[ch], "{format:?} channel {ch}");
let mono_source = mono.clips()[0].source().unwrap();
assert_ne!(mono_source, source);
assert!(mono.clips().iter().all(|clip| clip.source() == Some(mono_source)));
let data = e.session().pool.get(mono_source).unwrap();
assert_eq!(data.buffer.channels, vec![audio.buffer.channels[ch].clone()]);
assert_eq!(data.peaks.len(), 1);
let metadata = e.session().source(mono_source).unwrap();
assert_eq!((metadata.channels, metadata.time_reference), (1, 123_456));
assert!(metadata.unsaved);
for (clip, original) in mono.clips().iter().zip(original.clips()) {
let mut expected_clip = original.clone();
expected_clip.id = clip.id;
expected_clip.name = clip.name.clone();
expected_clip.content = soundcraft_model::ClipContent::Audio { source: mono_source, offset: original.source_offset() };
assert_eq!(clip, &expected_clip);
}
}
}
}
#[test]
fn split_stereo_matches_the_original_mix_in_playback_and_bounce_blocks() {
let (mut e, track, _) = fixture(ChannelFormat::Stereo, 2);
let original = e.session().clone();
let tracks = split(&mut e, track);
assert_eq!(e.session().track(tracks[0]).unwrap().name, "Original.L");
assert_eq!(e.session().track(tracks[1]).unwrap().name, "Original.R");
e.session_mut().track_mut(track).unwrap().inactive = true;
for block in [37, 512, 1024] {
let expected = soundcraft_mix::render_range(&original, Range::new(0, 192), block);
let actual = soundcraft_mix::render_range(e.session(), Range::new(0, 192), block);
assert_eq!(actual, expected, "block {block}");
}
}
#[test]
fn split_stereo_preserves_distinct_left_and_right_levels() {
let (mut e, track, source) = fixture(ChannelFormat::Stereo, 2);
let s = e.session_mut();
s.pool.insert(source, Arc::new(SourceAudio::new(AudioBuffer { sample_rate: 48_000, channels: vec![vec![0.2; 256], vec![0.7; 256]] })));
let clip = Clip::audio(s.new_clip_id(), "Distinct channels", source, 0, 0, 256);
s.track_mut(track).unwrap().playlist_mut().unwrap().clips = vec![clip];
split(&mut e, track);
e.session_mut().track_mut(track).unwrap().inactive = true;
let audio = soundcraft_mix::render_range(e.session(), Range::new(0, 128), 64);
eprintln!("Split into Mono output: L={}, R={}", audio[0][64], audio[1][64]);
assert_eq!(audio[0][64], 0.2);
assert_eq!(audio[1][64], 0.7);
}
#[test]
fn split_sources_survive_save_and_reopen_with_distinct_audio_files() {
let (mut e, track, _) = fixture(ChannelFormat::Stereo, 2);
let tracks = split(&mut e, track);
let (bytes, saved) = io::session_bundle(e.session()).unwrap();
let paths: std::collections::BTreeSet<_> = saved.sources.iter().map(|source| &source.path).collect();
assert_eq!(paths.len(), 3);
let mut reopened = Engine::default();
assert!(io::open_session_bytes(&mut reopened, "split.scraftz", &bytes).unwrap().is_empty());
for id in tracks {
let expected = soundcraft_mix::render_clips(e.session(), id, Range::new(0, 192));
let actual = soundcraft_mix::render_clips(reopened.session(), id, Range::new(0, 192));
assert_eq!(actual, expected);
}
}
#[test]
fn split_tracks_and_sources_undo_and_redo_together() {
let (mut e, track, _) = fixture(ChannelFormat::Stereo, 2);
let before = e.session().clone();
split(&mut e, track);
let after = e.session().clone();
assert_eq!(e.undo_history().len(), 1);
assert!(e.undo());
assert_eq!(e.session(), &before);
assert!(e.redo());
assert_eq!(e.session(), &after);
}
#[test]
fn split_reuses_a_mono_source_on_a_stereo_track() {
let (mut e, track, source) = fixture(ChannelFormat::Stereo, 1);
let expected = soundcraft_mix::render_clips(e.session(), track, Range::new(0, 192));
for id in split(&mut e, track) {
assert!(e.session().track(id).unwrap().clips().iter().all(|clip| clip.source() == Some(source)));
assert_eq!(soundcraft_mix::render_clips(e.session(), id, Range::new(0, 192))[0], expected[0]);
}
assert_eq!(e.session().sources.len(), 1);
}
#[test]
fn missing_or_empty_media_refuses_the_split_without_partial_changes() {
for empty in [false, true] {
let (mut e, track, _) = fixture(ChannelFormat::Stereo, 2);
let missing = SourceId(9999);
if empty {
e.session_mut().pool.insert(missing, Arc::new(SourceAudio::new(AudioBuffer { sample_rate: 48_000, channels: vec![] })));
}
let clip = Clip::audio(e.session_mut().new_clip_id(), "Unavailable", missing, 0, 160, 16);
e.session_mut().track_mut(track).unwrap().playlist_mut().unwrap().clips.push(clip);
let before = e.session().clone();
let error = e.execute("track.split_into_mono", &json!({"track": track.0})).unwrap_err();
assert!(matches!(error, soundcraft_engine::EngineError::BadParams(..)), "{error}");
assert_eq!(e.session(), &before);
assert!(e.undo_history().is_empty());
}
}
@@ -0,0 +1,173 @@
use serde_json::json;
use soundcraft_engine::Engine;
use soundcraft_model::{ChannelFormat, Session, TrackKind};
use std::sync::Arc;
#[test]
fn truncated_ascii_and_unicode_names_cannot_collide_with_another_track() {
for character in ['A', '界'] {
let existing: String = std::iter::repeat_n(character, 64).collect();
let mut session = Session::default();
let occupied = session.add_track(TrackKind::Audio, ChannelFormat::Stereo, Some(&existing));
let target = session.add_track(TrackKind::Aux, ChannelFormat::Mono, Some("Target"));
let mut engine = Engine::new(session);
let before = engine.session_arc();
let revision = engine.revision;
let result = engine.execute("track.rename", &json!({"track": target.0, "name": format!(" {existing} extra ")}));
assert!(result.is_err());
assert!(Arc::ptr_eq(&before, &engine.session_arc()));
assert_eq!(engine.revision, revision);
assert_eq!(engine.session().track(occupied).unwrap().name, existing);
assert_eq!(engine.session().track(target).unwrap().name, "Target");
assert!(!engine.is_dirty());
assert!(!engine.can_undo());
}
}
#[test]
fn rename_returns_the_stored_unicode_name_and_undo_restores_the_document() {
let mut session = Session::default();
let track = session.add_track(TrackKind::Audio, ChannelFormat::Surround51, Some("Original"));
session.track_mut(track).unwrap().comments = "Retain this comment".into();
let mut engine = Engine::new(session);
let before = engine.session().to_json().unwrap();
let stored = "é".repeat(64);
let result = engine.execute("track.rename", &json!({"track": track.0, "name": format!(" {stored}extra ")})).unwrap();
assert_eq!(result["name"], stored);
let renamed = engine.session().track(track).unwrap();
assert_eq!(renamed.name, stored);
assert_eq!(renamed.name.chars().count(), 64);
assert_eq!(renamed.id, track);
assert_eq!(renamed.format, ChannelFormat::Surround51);
assert_eq!(renamed.comments, "Retain this comment");
assert_eq!(engine.undo_history(), ["Rename Track"]);
assert!(engine.undo());
assert_eq!(engine.session().to_json().unwrap(), before);
assert!(engine.redo());
assert_eq!(engine.session().track(track).unwrap().name, stored);
}
#[test]
fn unchanged_name_after_truncation_does_not_make_an_undo_step() {
let name = "A".repeat(64);
let mut session = Session::default();
let track = session.add_track(TrackKind::Audio, ChannelFormat::Mono, Some(&name));
let mut engine = Engine::new(session);
let before = engine.session_arc();
let revision = engine.revision;
let result = engine.execute("track.rename", &json!({"track": track.0, "name": format!("{name} suffix")})).unwrap();
assert_eq!(result["name"], name);
assert!(Arc::ptr_eq(&before, &engine.session_arc()));
assert_eq!(engine.revision, revision);
assert!(!engine.is_dirty());
assert!(!engine.can_undo());
}
#[test]
fn routing_folder_rename_preserves_unique_owned_bus_routes_and_history() {
use soundcraft_model::{BusId, Route, TrackId};
let stored = "界".repeat(64);
let first_name = format!("{stored} Bus");
let second_name = format!("{stored} Bus 2");
let mut session = Session::default();
let member = session.add_track(TrackKind::Audio, ChannelFormat::Stereo, Some("Member"));
let user_bus = session.add_bus(&first_name, ChannelFormat::Stereo);
let second_user_bus = session.add_bus(&second_name, ChannelFormat::Stereo);
let mut engine = Engine::new(session);
let unrelated = engine.execute("track.new", &json!({"kind": "folder", "name": "Unrelated"})).unwrap();
let unrelated = TrackId(unrelated["tracks"][0].as_u64().unwrap());
let unrelated_route = engine.session().track(unrelated).unwrap().mixer.input.clone();
let created = engine.execute("track.move_to_new_folder", &json!({"tracks": [member.0], "name": "Initial"})).unwrap();
let folder = TrackId(created["folder"].as_u64().unwrap());
let owned_bus = BusId(created["bus"].as_u64().unwrap());
let before = engine.session().to_json().unwrap();
let history = engine.undo_history();
let bus_count = engine.session().busses.len();
let result = engine.execute("track.rename", &json!({"track": folder.0, "name": format!(" {stored} suffix ")})).unwrap();
assert_eq!(result["name"], stored);
assert_eq!(engine.session().track(folder).unwrap().name, stored);
assert_eq!(engine.session().bus(owned_bus).unwrap().name, format!("{stored} Bus 3"));
assert_eq!(engine.session().bus(user_bus).unwrap().name, first_name);
assert_eq!(engine.session().bus(second_user_bus).unwrap().name, second_name);
assert_eq!(engine.session().track(unrelated).unwrap().mixer.input, unrelated_route);
assert_eq!(engine.session().track(folder).unwrap().mixer.input, Route::Bus(owned_bus));
assert_eq!(engine.session().track(member).unwrap().mixer.output, Route::Bus(owned_bus));
assert_eq!(engine.session().track(member).unwrap().folder, Some(folder));
assert_eq!(engine.session().busses.len(), bus_count);
assert_eq!(engine.undo_history().len(), history.len() + 1);
assert_eq!(engine.undo_label(), Some("Rename Track"));
let renamed = engine.session().to_json().unwrap();
let document = engine.session_arc();
let revision = engine.revision;
let history = engine.undo_history();
engine.execute("track.rename", &json!({"track": folder.0, "name": format!("{stored} more")})).unwrap();
assert!(Arc::ptr_eq(&document, &engine.session_arc()));
assert_eq!(engine.revision, revision);
assert_eq!(engine.undo_history(), history);
assert!(engine.undo());
assert_eq!(engine.session().to_json().unwrap(), before);
assert!(engine.redo());
assert_eq!(engine.session().to_json().unwrap(), renamed);
// Current-main same-name folder rename also repairs the folder's own bus, without taking
// either colliding user bus or changing its members' bus identity.
engine.session_mut().busses.iter_mut().find(|bus| bus.id == owned_bus).unwrap().name = "Manually named owned bus".into();
let manual = engine.session().to_json().unwrap();
engine.execute("track.rename", &json!({"track": folder.0, "name": stored})).unwrap();
assert_eq!(engine.session().bus(owned_bus).unwrap().name, format!("{stored} Bus 3"));
assert_eq!(engine.session().bus(user_bus).unwrap().name, first_name);
assert_eq!(engine.session().bus(second_user_bus).unwrap().name, second_name);
assert_eq!(engine.session().track(member).unwrap().mixer.output, Route::Bus(owned_bus));
assert!(engine.undo());
assert_eq!(engine.session().to_json().unwrap(), manual);
assert!(engine.redo());
assert_eq!(engine.session().to_json().unwrap(), renamed);
}
#[test]
fn truncated_folder_collision_refuses_bus_and_document_changes_with_redo_pending() {
use soundcraft_model::{Route, TrackId};
for character in ['B', '界'] {
let occupied_name: String = std::iter::repeat_n(character, 64).collect();
let mut session = Session::default();
let occupied = session.add_track(TrackKind::Audio, ChannelFormat::Stereo, Some(&occupied_name));
let mut engine = Engine::new(session);
let created = engine.execute("track.new", &json!({"kind": "folder", "name": "Target"})).unwrap();
let folder = TrackId(created["tracks"][0].as_u64().unwrap());
let owned_bus = match engine.session().track(folder).unwrap().mixer.input {
Route::Bus(bus) => bus,
_ => panic!("the routing folder must own a bus"),
};
engine.execute("mix.volume", &json!({"track": folder.0, "db": -3})).unwrap();
assert!(engine.undo());
engine.mark_clean();
let document = engine.session_arc();
let before = engine.session().to_json().unwrap();
let revision = engine.revision;
let journal = engine.journal.clone();
let undo = engine.undo_history();
let redo = engine.redo_label().map(str::to_owned);
assert!(redo.is_some());
let result = engine.execute("track.rename", &json!({"track": folder.0, "name": format!(" {occupied_name} suffix ")}));
assert!(result.unwrap_err().to_string().contains(&occupied_name));
assert!(Arc::ptr_eq(&document, &engine.session_arc()));
assert_eq!(engine.session().to_json().unwrap(), before);
assert_eq!(engine.revision, revision);
assert_eq!(engine.journal, journal);
assert_eq!(engine.undo_history(), undo);
assert_eq!(engine.redo_label(), redo.as_deref());
assert!(!engine.is_dirty());
assert_eq!(engine.session().track(occupied).unwrap().name, occupied_name);
assert_eq!(engine.session().track(folder).unwrap().name, "Target");
assert_eq!(engine.session().bus(owned_bus).unwrap().name, "Target Bus");
assert_eq!(engine.session().track(folder).unwrap().mixer.input, Route::Bus(owned_bus));
}
}
+28 -3
View File
@@ -204,7 +204,12 @@ pub fn read_smf(bytes: &[u8]) -> Result<Smf, MidiError> {
tracks.push(track);
}
if format == 1 && tracks.len() > 1 && tracks.first().is_some_and(|t| t.sequence.notes.is_empty() && t.sequence.ctrls.is_empty()) {
// 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);
}
@@ -444,7 +449,12 @@ fn write_track(out: &mut Vec<u8>, mut events: Vec<WEvent>) -> Result<(), MidiErr
/// 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> {
let ntrks = u16::try_from(smf.tracks.len().saturating_add(1)).map_err(|_| MidiError::Write("more than 65535 tracks".into()))?;
// 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());
@@ -476,7 +486,9 @@ pub fn write_smf(smf: &Smf) -> Result<Vec<u8>, MidiError> {
for (t, text) in &smf.markers {
ev.push(WEvent { tick: clamp_tick(*t), order: 3, bytes: meta(0x06, text.as_bytes())? });
}
write_track(&mut out, ev)?;
if has_conductor {
write_track(&mut out, ev)?;
}
for track in &smf.tracks {
let mut ev = Vec::new();
@@ -565,6 +577,19 @@ mod tests {
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();
+242 -23
View File
@@ -148,7 +148,9 @@ pub fn create_instance(id: &str, state: Option<&[u8]>, sample_rate: f32, max_blo
Some(NewInstance { plugin, refused_state })
}
/// Peak meter values for one strip (linear, per channel, max over the last block).
/// Peak meter values for one strip (linear, per channel). [`MixEngine`] measures one block; a
/// realtime host folds blocks together with [`StripMeter::accumulate`] so a reader polling less
/// often than blocks arrive sees the highest peak since it last looked, not just the latest block.
#[derive(Debug, Clone, Default, PartialEq)]
pub struct StripMeter {
/// First two channels (mono strips repeat channel 0), for stereo meters.
@@ -186,6 +188,34 @@ impl StripMeter {
self.plugin_failures = o.plugin_failures;
}
/// Fold another (later) measurement in: per-channel peaks and the gain reduction keep their
/// maximum, plugin failures take the later value (the count only grows). Allocation-free once
/// the channel count is stable, so it can run on the audio thread every block.
pub fn accumulate(&mut self, o: &StripMeter) {
if self.peaks.len() != o.peaks.len() {
// The strip's format changed (a document edit): earlier channels no longer line up.
self.peaks.clone_from(&o.peaks);
self.peak = o.peak;
} else {
for (p, q) in self.peaks.iter_mut().zip(&o.peaks) {
*p = max_level(*p, *q);
}
for (p, q) in self.peak.iter_mut().zip(o.peak) {
*p = max_level(*p, q);
}
}
self.gain_reduction_db = max_level(self.gain_reduction_db, o.gain_reduction_db);
self.plugin_failures = o.plugin_failures;
}
/// Zero the levels (peaks and gain reduction) after a reader took them, keeping the channel
/// count and the plugin failure record. Never allocates.
pub fn reset_levels(&mut self) {
self.peak = [0.0; 2];
self.peaks.iter_mut().for_each(|p| *p = 0.0);
self.gain_reduction_db = 0.0;
}
fn measure(&mut self, bufs: &[Vec<f32>], frames: usize, gr: f32) {
if self.peaks.len() != bufs.len() {
self.peaks.resize(bufs.len(), 0.0);
@@ -199,6 +229,16 @@ impl StripMeter {
}
}
/// The larger of two meter levels; a NaN (from a misbehaving plugin) never wins or sticks.
fn max_level(a: f32, b: f32) -> f32 {
match (a.is_finite(), b.is_finite()) {
(true, true) => a.max(b),
(true, false) => a,
(false, true) => b,
(false, false) => 0.0,
}
}
/// A bus: its format and one buffer per channel.
struct BusBuf {
fmt: ChannelFormat,
@@ -544,8 +584,10 @@ impl MixEngine {
fx.reset();
}
}
self.meters.clear();
self.main_meter = StripMeter::default();
// Zeroed in place: rebuilding the entries would allocate on the audio thread at every
// play, stop and locate.
self.meters.values_mut().for_each(StripMeter::reset_levels);
self.main_meter.reset_levels();
}
/// Bring plugin instances in line with the session (creates/destroys as needed). Allocates;
@@ -560,6 +602,7 @@ impl MixEngine {
let mut cx = SlotCtx { sr: self.sample_rate, mb, external: self.external, adopt: &mut self.adopt, retired: &mut self.retired };
let gone: Vec<TrackId> = self.strips.keys().filter(|id| s.track(**id).is_none()).copied().collect();
for id in gone {
self.meters.remove(&id);
if let Some(mut st) = self.strips.remove(&id) {
for p in st.plugins.drain(..) {
cx.retire(p);
@@ -1070,9 +1113,8 @@ fn process_strip(
}
strip.gr = gr;
strip.latency = latency.min(MAX_LATENCY);
let auto_mute = t.mixer.automation_mode.reads() && t.lane(&AutoParam::Mute).is_some_and(|l| l.value_at(pos, 0.0) >= 0.5);
let soloed_out = any_solo && !t.mixer.solo && !t.mixer.solo_safe && !receives_solo(s, t);
strip.muted = t.mixer.mute || auto_mute || soloed_out;
strip.muted = t.mixer.mute || soloed_out;
if t.mixer.sends.iter().flatten().any(|x| x.pre_fader) {
strip.pre.resize_with(strip.buf.len(), Vec::new);
for (d, src) in strip.pre.iter_mut().zip(strip.buf.iter()) {
@@ -1113,6 +1155,23 @@ fn process_strip(
}
}
}
// Track mute gates the audio and its pre-fader tap without splitting processor blocks.
// Keep the existing curve interpolation, first-lane and mode policy, and render-position
// clock. Processor/bus age and Master/VCA automation policy are separate concerns.
if t.mixer.automation_mode.reads()
&& let Some(lane) = t.lane(&AutoParam::Mute).filter(|lane| !lane.points.is_empty())
{
for i in 0..frames {
let at = pos.saturating_add(i64::try_from(i).unwrap_or(i64::MAX));
if lane.value_at(at, 0.0) >= 0.5 {
for channel in strip.buf.iter_mut().chain(strip.pre.iter_mut()) {
if let Some(sample) = channel.get_mut(i) {
*sample = 0.0;
}
}
}
}
}
}
/// Silences a block the plugin filled with NaN or infinity and takes the plugin out until the
@@ -1342,26 +1401,97 @@ fn processing_order(s: &Session) -> Vec<usize> {
(0..s.tracks.len()).filter(|&i| s.tracks.get(i).is_some_and(|t| !matches!(t.kind, TrackKind::Aux | TrackKind::Folder))).collect();
let mut auxes: Vec<usize> =
(0..s.tracks.len()).filter(|&i| s.tracks.get(i).is_some_and(|t| matches!(t.kind, TrackKind::Aux | TrackKind::Folder))).collect();
// Depth = how many aux hops feed this aux's input bus (bounded; cycles cap out).
let depth = |i: usize| -> usize {
let mut d = 0;
let mut cur = vec![i];
for _ in 0..8 {
let inputs: Vec<&Route> = cur.iter().filter_map(|&k| s.tracks.get(k)).map(|t| &t.mixer.input).collect();
let feeders: Vec<usize> = auxes_feeding(s, &inputs);
if feeders.is_empty() {
break;
}
d += 1;
cur = feeders;
}
d
};
auxes.sort_by_key(|&i| depth(i));
if let Some(depths) = acyclic_aux_depths(s, &auxes) {
// Stable sorting preserves session order among auxes at the same longest-path depth.
auxes.sort_by_key(|&i| depths.get(i).copied().unwrap_or(0));
} else {
// A cycle keeps the existing bounded ordering for the entire graph, including any
// independent acyclic branches. Feedback does not have a finite longest-path depth.
auxes.sort_by_key(|&i| bounded_aux_depth(s, i));
}
order.extend(auxes);
order
}
/// Exact aux-hop depths when the whole Aux/Folder graph is acyclic. Bus joins retain only
/// producer counts and consumers, avoiding a producer-by-consumer adjacency matrix.
fn acyclic_aux_depths(s: &Session, auxes: &[usize]) -> Option<Vec<usize>> {
#[derive(Default)]
struct BusDependencies {
remaining: usize,
depth: usize,
last_producer: Option<usize>,
last_processed: Option<usize>,
consumers: Vec<usize>,
}
let mut busses = HashMap::<BusId, BusDependencies>::new();
for &i in auxes {
for bus in aux_output_busses(s.tracks.get(i)?) {
let dependency = busses.entry(bus).or_default();
// Output and multiple sends from this producer may name the same bus.
if dependency.last_producer != Some(i) {
dependency.last_producer = Some(i);
dependency.remaining = dependency.remaining.checked_add(1)?;
}
}
}
let mut depths = vec![0usize; s.tracks.len()];
let mut ready = Vec::with_capacity(auxes.len());
for &i in auxes {
if let Route::Bus(bus) = &s.tracks.get(i)?.mixer.input {
let dependency = busses.entry(*bus).or_default();
dependency.consumers.push(i);
if dependency.remaining > 0 {
continue;
}
}
ready.push(i);
}
let mut processed = 0usize;
while let Some(&i) = ready.get(processed) {
processed = processed.checked_add(1)?;
let next_depth = depths.get(i)?.checked_add(1)?;
for bus in aux_output_busses(s.tracks.get(i)?) {
let dependency = busses.get_mut(&bus)?;
if dependency.last_processed == Some(i) {
continue;
}
dependency.last_processed = Some(i);
dependency.depth = dependency.depth.max(next_depth);
dependency.remaining = dependency.remaining.checked_sub(1)?;
if dependency.remaining == 0 {
for &consumer in &dependency.consumers {
*depths.get_mut(consumer)? = dependency.depth;
ready.push(consumer);
}
}
}
}
(processed == auxes.len()).then_some(depths)
}
fn aux_output_busses(t: &Track) -> impl Iterator<Item = BusId> + '_ {
std::iter::once(&t.mixer.output)
.chain(t.mixer.sends.iter().flatten().map(|send| &send.target))
.filter_map(|route| if let Route::Bus(bus) = route { Some(*bus) } else { None })
}
/// The previous bounded depth calculation, retained unchanged for any cyclic graph.
fn bounded_aux_depth(s: &Session, i: usize) -> usize {
let mut d = 0;
let mut cur = vec![i];
for _ in 0..8 {
let inputs: Vec<&Route> = cur.iter().filter_map(|&k| s.tracks.get(k)).map(|t| &t.mixer.input).collect();
let feeders: Vec<usize> = auxes_feeding(s, &inputs);
if feeders.is_empty() {
break;
}
d += 1;
cur = feeders;
}
d
}
fn auxes_feeding(s: &Session, inputs: &[&Route]) -> Vec<usize> {
s.tracks
.iter()
@@ -1374,6 +1504,60 @@ fn auxes_feeding(s: &Session, inputs: &[&Route]) -> Vec<usize> {
.collect()
}
#[cfg(test)]
#[allow(clippy::unwrap_used)]
mod processing_order_tests {
use super::*;
use soundcraft_model::SendSlot;
#[test]
fn acyclic_bus_joins_use_longest_depth_and_keep_session_order_for_ties() {
let mut s = Session::default();
s.add_track(TrackKind::Audio, ChannelFormat::Stereo, None);
let consumer = s.add_track(TrackKind::Aux, ChannelFormat::Stereo, None);
let first_root = s.add_track(TrackKind::Aux, ChannelFormat::Stereo, None);
let second_root = s.add_track(TrackKind::Aux, ChannelFormat::Stereo, None);
let middle = s.add_track(TrackKind::Folder, ChannelFormat::Stereo, None);
let first_bus = s.add_bus("First", ChannelFormat::Stereo);
let join = s.add_bus("Join", ChannelFormat::Stereo);
s.track_mut(consumer).unwrap().mixer.input = Route::Bus(join);
let root = s.track_mut(first_root).unwrap();
root.mixer.output = Route::Main;
root.mixer.sends[0] = Some(SendSlot::new(Route::Bus(first_bus)));
let root = s.track_mut(second_root).unwrap();
root.mixer.output = Route::Bus(join);
root.mixer.sends[0] = Some(SendSlot::new(Route::Bus(join)));
root.mixer.sends[1] = Some(SendSlot::new(Route::Bus(join)));
let folder = s.track_mut(middle).unwrap();
folder.mixer.input = Route::Bus(first_bus);
folder.mixer.output = Route::Bus(join);
folder.mixer.sends[0] = Some(SendSlot::new(Route::Bus(join)));
assert_eq!(processing_order(&s), vec![0, 2, 3, 4, 1]);
}
#[test]
fn a_cycle_preserves_bounded_order_for_the_whole_graph() {
let mut s = Session::default();
let busses: Vec<_> = (0..10).map(|i| s.add_bus(&format!("Chain {i}"), ChannelFormat::Stereo)).collect();
let tracks: Vec<_> = (0..10).map(|_| s.add_track(TrackKind::Aux, ChannelFormat::Stereo, None)).collect();
for (i, id) in tracks.iter().enumerate() {
let track = s.track_mut(*id).unwrap();
track.mixer.input = Route::Bus(busses[i]);
track.mixer.output = if i == 0 { Route::Main } else { Route::Bus(busses[i - 1]) };
}
assert_eq!(processing_order(&s), vec![9, 8, 7, 6, 5, 4, 3, 2, 1, 0]);
let cycle_bus = s.add_bus("Feedback", ChannelFormat::Stereo);
let cycle = s.add_track(TrackKind::Aux, ChannelFormat::Stereo, None);
let track = s.track_mut(cycle).unwrap();
track.mixer.input = Route::Bus(cycle_bus);
track.mixer.output = Route::Bus(cycle_bus);
// Both deepest independent returns tie with the cycle at the legacy eight-hop cap.
assert_eq!(processing_order(&s), vec![9, 8, 7, 6, 5, 4, 3, 2, 0, 1, 10]);
}
}
/// Implicit solo: an aux is not muted when a soloed track feeds its input bus.
fn receives_solo(s: &Session, t: &Track) -> bool {
let Route::Bus(b) = &t.mixer.input else { return false };
@@ -1480,7 +1664,7 @@ fn render_clip(s: &Session, clip: &Clip, pos: Samples, frames: usize, buf: &mut
let x0 = f.floor();
let fr = (f - x0) as f32;
let a = if x0 >= 0.0 { src.get(x0 as usize).copied().unwrap_or(0.0) } else { 0.0 };
let b = if x0 + 1.0 >= 0.0 { src.get((x0 as usize).saturating_add(1)).copied().unwrap_or(0.0) } else { 0.0 };
let b = if x0 + 1.0 >= 0.0 { src.get((x0 + 1.0) as usize).copied().unwrap_or(0.0) } else { 0.0 };
a + (b - a) * fr
};
let t = (k - i) as f32 / n;
@@ -2258,6 +2442,13 @@ pub fn render_track_pre_fader(s: &Session, track: TrackId, range: Range) -> Vec<
/// A built-in plugin, else a hosted CLAP plugin (`clap:<id>`).
fn create_plugin(id: &str) -> Option<Box<dyn Plugin>> {
// Tests never load real plug-ins; adopt a fake instead. Scanning the machine's plug-in folders
// loads every installed plug-in in-process, and some never unload, so a test that reached the
// real hosts would hang on a developer machine (CI has none installed).
#[cfg(test)]
if is_third_party(id) {
return None;
}
soundcraft_dsp::create(id)
.or_else(|| soundcraft_clap_host::create(id))
.or_else(|| soundcraft_vst3_host::create(id))
@@ -2310,6 +2501,10 @@ mod audio_thread_tests {
#[cfg(test)]
mod tests {
//! Rule: tests never call the real plug-in hosts. Third-party ids are not created here
//! (create_plugin returns None under cfg(test)); a test that needs a hosted plug-in
//! adopts a fake under a clap:test.* id via MixEngine::adopt.
use super::*;
use soundcraft_audio_io::AudioBuffer;
use soundcraft_model::{Insert, SourceAudio, SourceId};
@@ -2432,6 +2627,30 @@ mod tests {
assert!(out[0][100] > 0.49 && out[1][100].abs() < 1e-4);
}
#[test]
fn a_stretched_clip_before_its_source_start_reads_from_the_first_sample() {
// A clip that extends before its source (a negative offset) plays silence there. When it
// is stretched, the read position passes through [-1, 0), which must interpolate from
// that silence towards sample 0 of the file -- not towards sample 1.
let mut s = Session::default();
let t = s.add_track(TrackKind::Audio, ChannelFormat::Mono, None);
let buf = AudioBuffer { sample_rate: 48_000, channels: vec![vec![1.0, 0.5, 0.25, 0.125]] };
s.pool.insert(SourceId(901), Arc::new(SourceAudio::new(buf)));
let id = s.new_clip_id();
let mut c = Clip::audio(id, "late", SourceId(901), -1, 0, 4);
c.stretch = 2.0;
if let Some(pl) = s.track_mut(t).and_then(|t| t.playlist_mut()) {
pl.clips.push(c);
}
let out = render_clips(&s, t, Range::new(0, 4));
// rel 0 -> source -1.0: silence; rel 1 -> source -0.5: halfway from silence to sample 0;
// rel 2 -> source 0.0: sample 0; rel 3 -> source 0.5: halfway to sample 1.
let want = [0.0, 0.5, 1.0, 0.75];
for (i, w) in want.iter().enumerate() {
assert!((out[0][i] - w).abs() < 1e-6, "frame {i}: {} vs {w}", out[0][i]);
}
}
#[test]
fn fade_curvature_reaches_the_rendered_audio() {
for fade_in in [true, false] {
@@ -3009,7 +3228,7 @@ mod tests {
s.track_mut(t).unwrap().mixer.inserts[0].as_mut().unwrap().params.insert("gain".into(), -12.0);
render_block(&mut eng, &s, 1024);
assert_eq!(sets.load(std::sync::atomic::Ordering::Relaxed), 2, "later session edits apply");
// Offline (not external): unknown third-party plugins are simply absent.
// Offline (not external): third-party plugins are simply absent (tests never load real ones).
let out = render_range(&s, Range::new(0, 1024), 512);
assert!(out[0][100] > 0.3);
}
+306
View File
@@ -0,0 +1,306 @@
//! Aux and routing-folder dependencies preserve PCM beyond eight hops. Cyclic graphs keep
//! the established whole-graph fallback, including its session-order-dependent audio.
use soundcraft_audio_io::AudioBuffer;
use soundcraft_mix::{MixEngine, render_range};
use soundcraft_model::{BusId, ChannelFormat, Clip, Route, SendSlot, Session, SourceAudio, SourceId, TrackId, TrackKind};
use soundcraft_time::Range;
use std::sync::Arc;
const FRAMES: usize = 4097;
const DEPTH: usize = 12;
fn signal() -> Vec<Vec<f32>> {
// Distinct, exactly representable channels expose lost, duplicated, swapped and delayed PCM.
vec![
(0..FRAMES).map(|frame| ((frame % 31) as i32 - 15) as f32 / 256.0).collect(),
(0..FRAMES).map(|frame| ((frame % 17) as i32 - 8) as f32 / 512.0).collect(),
]
}
fn scaled_signal(gain: f32) -> Vec<Vec<f32>> {
signal().into_iter().map(|channel| channel.into_iter().map(|value| value * gain).collect()).collect()
}
fn session(pdc: bool) -> Session {
let mut session = Session::default();
session.edit.click = false;
session.edit.delay_compensation = pdc;
session
}
fn add_source(session: &mut Session, output: Route) -> TrackId {
let source = SourceId(session.alloc());
session.pool.insert(source, Arc::new(SourceAudio::new(AudioBuffer { sample_rate: 48_000, channels: signal() })));
let clip = session.new_clip_id();
let track = session.add_track(TrackKind::Audio, ChannelFormat::Stereo, Some("PCM source"));
let track_state = session.track_mut(track).unwrap();
track_state.mixer.output = output;
track_state.playlist_mut().unwrap().clips.push(Clip::audio(clip, "PCM", source, 0, 0, FRAMES as i64));
track
}
fn add_aux(session: &mut Session, kind: TrackKind, input: BusId, output: Route) -> TrackId {
let track = session.add_track(kind, ChannelFormat::Stereo, None);
let mixer = &mut session.track_mut(track).unwrap().mixer;
mixer.input = Route::Bus(input);
mixer.output = output;
track
}
fn send(bus: BusId) -> SendSlot {
let mut send = SendSlot::new(Route::Bus(bus));
send.level_db = 0.0;
send
}
struct Chain {
session: Session,
buses: Vec<BusId>,
/// Logical routing order, deliberately different from the session's creation order.
tracks: Vec<TrackId>,
}
fn reverse_chain(pdc: bool, send_links: bool, folders: bool) -> Chain {
let mut session = session(pdc);
let buses: Vec<_> = (0..DEPTH).map(|stage| session.add_bus(&format!("Stage {stage}"), ChannelFormat::Stereo)).collect();
let mut tracks = Vec::new();
for stage in (0..DEPTH).rev() {
let kind = if folders && stage % 3 == 1 { TrackKind::Folder } else { TrackKind::Aux };
let output = if stage + 1 == DEPTH {
Route::Main
} else if send_links {
Route::None
} else {
Route::Bus(buses[stage + 1])
};
let track = add_aux(&mut session, kind, buses[stage], output);
if send_links && stage + 1 < DEPTH {
session.track_mut(track).unwrap().mixer.sends[0] = Some(send(buses[stage + 1]));
}
tracks.push(track);
}
tracks.reverse();
add_source(&mut session, Route::Bus(buses[0]));
Chain { session, buses, tracks }
}
fn assert_pcm(actual: &[Vec<f32>], expected: &[Vec<f32>], context: &str) {
assert_eq!(actual.len(), expected.len(), "{context}: channel count");
for (channel, (actual, expected)) in actual.iter().zip(expected).enumerate() {
assert_eq!(actual.len(), expected.len(), "{context}: channel {channel} length");
for (frame, (actual, expected)) in actual.iter().zip(expected).enumerate() {
assert_eq!(actual, expected, "{context}: channel {channel}, frame {frame}");
}
}
}
fn render(session: &Session, block: usize) -> Vec<Vec<f32>> {
render_range(session, Range::new(0, FRAMES as i64), block)
}
fn engine_block(engine: &mut MixEngine, session: &Session) -> Vec<Vec<f32>> {
let mut out = vec![vec![0.0; 128]; 2];
engine.render(session, 0, 128, &mut out);
out
}
fn first_block(pcm: Vec<Vec<f32>>) -> Vec<Vec<f32>> {
pcm.into_iter().map(|channel| channel.into_iter().take(128).collect()).collect()
}
#[test]
fn reverse_created_twelve_aux_chain_preserves_every_sample() {
for pdc in [false, true] {
let chain = reverse_chain(pdc, false, false);
assert_pcm(&render(&chain.session, 1024), &signal(), "deep output chain");
}
}
#[test]
fn deep_dependencies_through_sends_without_outputs_preserve_every_sample() {
for pdc in [false, true] {
let chain = reverse_chain(pdc, true, false);
assert_pcm(&render(&chain.session, 97), &signal(), "deep send-only chain");
}
}
#[test]
fn output_and_send_to_the_same_bus_sum_audio_without_duplicate_dependency() {
for pdc in [false, true] {
let mut chain = reverse_chain(pdc, false, false);
chain.session.track_mut(chain.tracks[4]).unwrap().mixer.sends[0] = Some(send(chain.buses[5]));
assert_pcm(&render(&chain.session, 256), &scaled_signal(2.0), "duplicate destination");
}
}
#[test]
fn short_and_deep_branches_both_reach_the_join() {
for pdc in [false, true] {
let mut chain = reverse_chain(pdc, false, false);
add_aux(&mut chain.session, TrackKind::Aux, chain.buses[0], Route::Bus(chain.buses[DEPTH - 1]));
assert_pcm(&render(&chain.session, 512), &scaled_signal(2.0), "branch join");
}
}
#[test]
fn routing_folders_participate_in_deep_dependencies() {
for pdc in [false, true] {
let chain = reverse_chain(pdc, true, true);
assert_pcm(&render(&chain.session, 63), &signal(), "aux and folder send chain");
}
}
#[test]
fn output_edit_reorders_the_same_engine_before_the_next_block() {
let mut session = session(true);
let root = session.add_bus("Root", ChannelFormat::Stereo);
let join = session.add_bus("Join", ChannelFormat::Stereo);
add_aux(&mut session, TrackKind::Aux, join, Route::Main);
let producer = add_aux(&mut session, TrackKind::Aux, root, Route::Main);
add_source(&mut session, Route::Bus(root));
let mut engine = MixEngine::new(48_000.0, 128);
let expected = first_block(signal());
assert_pcm(&engine_block(&mut engine, &session), &expected, "before output edit");
session.track_mut(producer).unwrap().mixer.output = Route::Bus(join);
assert_pcm(&engine_block(&mut engine, &session), &expected, "after output edit");
}
#[test]
fn input_edit_reorders_the_same_engine_before_the_next_block() {
let mut session = session(true);
let root = session.add_bus("Root", ChannelFormat::Stereo);
let join = session.add_bus("Join", ChannelFormat::Stereo);
let consumer = add_aux(&mut session, TrackKind::Folder, root, Route::Main);
add_aux(&mut session, TrackKind::Aux, root, Route::Bus(join));
add_source(&mut session, Route::Bus(root));
let mut engine = MixEngine::new(48_000.0, 128);
let expected = first_block(signal());
assert_pcm(&engine_block(&mut engine, &session), &expected, "before input edit");
session.track_mut(consumer).unwrap().mixer.input = Route::Bus(join);
assert_pcm(&engine_block(&mut engine, &session), &expected, "after input edit");
}
#[test]
fn send_add_retarget_remove_and_readd_update_the_same_engine() {
let mut session = session(true);
let root = session.add_bus("Root", ChannelFormat::Stereo);
let join = session.add_bus("Join", ChannelFormat::Stereo);
let unused = session.add_bus("Unused", ChannelFormat::Stereo);
add_aux(&mut session, TrackKind::Aux, join, Route::Main);
let producer = add_aux(&mut session, TrackKind::Folder, root, Route::None);
add_source(&mut session, Route::Bus(root));
let mut engine = MixEngine::new(48_000.0, 128);
let expected = first_block(signal());
let silence = vec![vec![0.0; 128]; 2];
assert_pcm(&engine_block(&mut engine, &session), &silence, "before send");
session.track_mut(producer).unwrap().mixer.sends[0] = Some(send(join));
assert_pcm(&engine_block(&mut engine, &session), &expected, "send added");
session.track_mut(producer).unwrap().mixer.sends[0].as_mut().unwrap().target = Route::Bus(unused);
assert_pcm(&engine_block(&mut engine, &session), &silence, "send retargeted");
session.track_mut(producer).unwrap().mixer.sends[0].as_mut().unwrap().target = Route::Bus(join);
assert_pcm(&engine_block(&mut engine, &session), &expected, "send retargeted back");
session.track_mut(producer).unwrap().mixer.sends[0] = None;
assert_pcm(&engine_block(&mut engine, &session), &silence, "send removed");
session.track_mut(producer).unwrap().mixer.sends[0] = Some(send(join));
assert_pcm(&engine_block(&mut engine, &session), &expected, "send readded");
}
#[test]
fn track_reorder_removal_and_new_identity_rebuild_the_same_engine() {
let mut chain = reverse_chain(true, false, false);
let mut engine = MixEngine::new(48_000.0, 128);
let expected = first_block(signal());
assert_pcm(&engine_block(&mut engine, &chain.session), &expected, "initial track order");
chain.session.tracks.reverse();
assert_pcm(&engine_block(&mut engine, &chain.session), &expected, "tracks reordered");
chain.session.tracks.retain(|track| track.id != chain.tracks[6]);
let silence = vec![vec![0.0; 128]; 2];
assert_pcm(&engine_block(&mut engine, &chain.session), &silence, "track removed");
let replacement = add_aux(&mut chain.session, TrackKind::Aux, chain.buses[6], Route::Bus(chain.buses[7]));
assert_ne!(replacement, chain.tracks[6]);
assert_pcm(&engine_block(&mut engine, &chain.session), &expected, "track readded with new identity");
}
#[test]
fn strip_and_bus_format_edits_refresh_the_same_engine_workspace() {
let mut chain = reverse_chain(true, false, false);
let mut engine = MixEngine::new(48_000.0, 128);
let stereo = first_block(signal());
assert_pcm(&engine_block(&mut engine, &chain.session), &stereo, "initial stereo formats");
chain.session.track_mut(chain.tracks[5]).unwrap().format = ChannelFormat::Mono;
// A stereo bus folds to mono at -6 dB; the retained hard-left pan sends that mono to L.
let mono: Vec<_> = stereo[0].iter().zip(&stereo[1]).map(|(left, right)| 0.5 * (left + right)).collect();
assert_pcm(&engine_block(&mut engine, &chain.session), &[mono.clone(), vec![0.0; 128]], "mono strip");
chain.session.track_mut(chain.tracks[5]).unwrap().format = ChannelFormat::Stereo;
assert_pcm(&engine_block(&mut engine, &chain.session), &stereo, "stereo strip restored");
chain.session.busses.iter_mut().find(|bus| bus.id == chain.buses[5]).unwrap().format = ChannelFormat::Mono;
// A mono bus feeds both channels of the following stereo aux at unity.
assert_pcm(&engine_block(&mut engine, &chain.session), &[mono.clone(), mono], "mono bus");
}
#[test]
fn deep_routing_is_invariant_to_offline_and_callback_block_sizes() {
let chain = reverse_chain(true, true, true);
let expected = signal();
for block in [16, 63, 97, 256, 512, 1024] {
assert_pcm(&render(&chain.session, block), &expected, "offline block size");
}
let mut engine = MixEngine::new(48_000.0, 1024);
let callbacks = [1, 63, 257, 17, 511, 1024];
let mut out = vec![Vec::new(), Vec::new()];
let mut position = 0;
let mut callback = 0;
while position < FRAMES {
let frames = callbacks[callback % callbacks.len()].min(FRAMES - position);
let mut block = vec![vec![0.0; frames]; 2];
engine.render(&chain.session, position as i64, frames, &mut block);
for (destination, channel) in out.iter_mut().zip(block) {
destination.extend(channel);
}
position += frames;
callback += 1;
}
assert_pcm(&out, &expected, "varying callback sizes");
}
#[test]
fn self_loop_preserves_audio_and_selects_fallback_for_the_whole_graph() {
for pdc in [false, true] {
let mut chain = reverse_chain(pdc, false, false);
let loop_bus = chain.session.add_bus("Self loop", ChannelFormat::Stereo);
let aux = add_aux(&mut chain.session, TrackKind::Aux, loop_bus, Route::Main);
chain.session.track_mut(aux).unwrap().mixer.sends[0] = Some(send(loop_bus));
add_source(&mut chain.session, Route::Bus(loop_bus));
// The self-loop reads its source once, then writes back to the already-read bus.
// The disconnected deep chain remains silent under the established eight-hop fallback.
assert_pcm(&render(&chain.session, 97), &signal(), "self-loop whole-graph fallback");
}
}
#[test]
fn two_aux_cycle_preserves_session_order_audio_and_whole_graph_fallback() {
for pdc in [false, true] {
for reverse in [false, true] {
let mut chain = reverse_chain(pdc, false, false);
let a_bus = chain.session.add_bus("Cycle A", ChannelFormat::Stereo);
let b_bus = chain.session.add_bus("Cycle B", ChannelFormat::Stereo);
let (a, b) = if reverse {
let b = add_aux(&mut chain.session, TrackKind::Aux, b_bus, Route::Main);
let a = add_aux(&mut chain.session, TrackKind::Folder, a_bus, Route::Main);
(a, b)
} else {
let a = add_aux(&mut chain.session, TrackKind::Folder, a_bus, Route::Main);
let b = add_aux(&mut chain.session, TrackKind::Aux, b_bus, Route::Main);
(a, b)
};
chain.session.track_mut(a).unwrap().mixer.sends[0] = Some(send(b_bus));
chain.session.track_mut(b).unwrap().mixer.sends[0] = Some(send(a_bus));
add_source(&mut chain.session, Route::Bus(a_bus));
// A then B: both hear the source. B then A: B reads its still-empty bus.
// Buses clear each block, so neither order feeds a previous block back into this one.
let expected = scaled_signal(if reverse { 1.0 } else { 2.0 });
assert_pcm(&render(&chain.session, 256), &expected, "two-aux whole-graph fallback");
}
}
}
+4
View File
@@ -1,6 +1,10 @@
//! Integration tests, built as one test binary: each file is a module.
#[allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]
mod aux_routing_order;
#[allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]
mod bus_aux_interval;
#[allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]
mod master_insert_latency;
#[allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]
mod strip_mute_automation;
@@ -0,0 +1,242 @@
//! Ordinary strip mute changes sample gates without splitting processor blocks.
use soundcraft_audio_io::AudioBuffer;
use soundcraft_dsp::Plugin;
use soundcraft_mix::{MixEngine, PreparedInstance, render_range};
use soundcraft_model::{
AutoParam, AutomationLane, AutomationMode, ChannelFormat, Clip, Insert, Route, SendSlot, Session, SourceAudio, SourceId, TrackId, TrackKind,
};
use soundcraft_time::Range;
use std::sync::{Arc, Mutex};
const LEN: usize = 768;
fn fixture() -> (Session, TrackId) {
let mut s = Session::default();
s.pool.insert(SourceId(901), Arc::new(SourceAudio::new(AudioBuffer { sample_rate: 48_000, channels: vec![vec![0.25; LEN]; 2] })));
let t = s.add_track(TrackKind::Audio, ChannelFormat::Stereo, Some("Source"));
let clip = s.new_clip_id();
let tr = s.track_mut(t).unwrap();
tr.playlist_mut().unwrap().clips.push(Clip::audio(clip, "tone", SourceId(901), 0, 0, LEN as i64));
tr.mixer.pan = vec![-1.0, 1.0];
tr.mixer.output = Route::Main;
(s, t)
}
fn mute(s: &mut Session, t: TrackId, start: i64, end: i64) {
let lane = s.track_mut(t).unwrap().lane_mut(&AutoParam::Mute);
for (at, value) in [(0, 0.0), (start - 1, 0.0), (start, 1.0), (end - 1, 1.0), (end, 0.0)] {
lane.set_point(at, value);
}
}
fn rendered(s: &Session, block: usize) -> Vec<Vec<f32>> {
render_range(s, Range::new(0, LEN as i64), block)
}
fn gated(actual: &[Vec<f32>], reference: &[Vec<f32>], start: usize, end: usize) {
assert_eq!(actual.len(), reference.len());
assert!(reference.iter().flatten().any(|x| x.abs() > 0.01));
for (got, want) in actual.iter().zip(reference) {
assert_eq!(got.len(), LEN);
for i in 0..LEN {
assert_eq!(got[i], if (start..end).contains(&i) { 0.0 } else { want[i] }, "frame {i}");
}
}
}
#[test]
fn internal_and_exact_block_edges_are_partition_independent() {
for (start, end) in [(101, 200), (128, 256), (1, 767)] {
let (mut s, t) = fixture();
let reference = rendered(&s, 512);
mute(&mut s, t, start, end);
for block in [16, 63, 128, 512, 1024] {
gated(&rendered(&s, block), &reference, start as usize, end as usize);
}
}
}
#[test]
fn existing_held_mute_curve_and_threshold_are_preserved() {
let (mut s, t) = fixture();
let reference = rendered(&s, 512);
let lane = s.track_mut(t).unwrap().lane_mut(&AutoParam::Mute);
lane.set_point(0, 0.49);
lane.set_point(200, 0.5);
gated(&rendered(&s, 512), &reference, 200, LEN);
}
#[test]
fn pre_and_post_fader_send_audio_share_the_track_mute_gate() {
for pre in [false, true] {
let (mut s, t) = fixture();
let bus = s.add_bus("Send", ChannelFormat::Stereo);
let aux = s.add_track(TrackKind::Aux, ChannelFormat::Stereo, Some("Return"));
s.track_mut(aux).unwrap().mixer.input = Route::Bus(bus);
s.track_mut(aux).unwrap().mixer.output = Route::Main;
let tr = s.track_mut(t).unwrap();
tr.mixer.output = Route::None;
tr.mixer.volume_db = -9.0;
let mut send = SendSlot::new(Route::Bus(bus));
send.pre_fader = pre;
send.level_db = -3.0;
tr.mixer.sends[0] = Some(send);
let reference = rendered(&s, 512);
mute(&mut s, t, 101, 200);
for block in [63, 512] {
gated(&rendered(&s, block), &reference, 101, 200);
}
}
}
#[test]
fn off_empty_and_first_empty_lane_keep_exact_unautomated_pcm() {
let (mut s, t) = fixture();
let reference = rendered(&s, 512);
s.track_mut(t).unwrap().lane_mut(&AutoParam::Mute);
assert_eq!(rendered(&s, 512), reference);
let mut second = AutomationLane::new(AutoParam::Mute);
second.set_point(0, 1.0);
s.track_mut(t).unwrap().automation.push(second);
assert_eq!(rendered(&s, 512), reference, "first empty lane remains authoritative");
s.track_mut(t).unwrap().automation.clear();
mute(&mut s, t, 101, 200);
s.track_mut(t).unwrap().mixer.automation_mode = AutomationMode::Off;
assert_eq!(rendered(&s, 512), reference);
}
#[test]
fn reading_modes_retain_existing_policy() {
for mode in AutomationMode::ALL.into_iter().filter(|mode| mode.reads()) {
let (mut s, t) = fixture();
let reference = rendered(&s, 512);
mute(&mut s, t, 101, 200);
s.track_mut(t).unwrap().mixer.automation_mode = mode;
gated(&rendered(&s, 512), &reference, 101, 200);
}
}
#[test]
fn static_mute_and_solo_suppression_cannot_be_reopened_by_automation() {
for solo in [false, true] {
let (mut s, t) = fixture();
if solo {
let other = s.add_track(TrackKind::Audio, ChannelFormat::Stereo, None);
s.track_mut(other).unwrap().mixer.solo = true;
} else {
s.track_mut(t).unwrap().mixer.mute = true;
}
let reference = rendered(&s, 512);
assert!(reference.iter().flatten().all(|x| x.abs() < 1e-6));
mute(&mut s, t, 101, 200);
gated_silent(&rendered(&s, 512));
}
}
fn gated_silent(output: &[Vec<f32>]) {
assert!(output.iter().flatten().all(|x| x.abs() < 1e-6));
}
#[test]
fn aux_and_folder_inputs_gate_their_audio_inside_the_block() {
for kind in [TrackKind::Aux, TrackKind::Folder] {
let (mut s, source) = fixture();
let bus = s.add_bus("Input", ChannelFormat::Stereo);
s.track_mut(source).unwrap().mixer.output = Route::Bus(bus);
let target = s.add_track(kind, ChannelFormat::Stereo, Some("Return"));
s.track_mut(target).unwrap().mixer.input = Route::Bus(bus);
s.track_mut(target).unwrap().mixer.output = Route::Main;
let reference = rendered(&s, 512);
mute(&mut s, target, 101, 200);
gated(&rendered(&s, 512), &reference, 101, 200);
}
}
#[test]
fn no_active_mute_lane_preserves_fader_and_pan_schedules_exactly() {
let (mut s, t) = fixture();
for (param, a, b) in [(AutoParam::Volume, -12.0, -3.0), (AutoParam::Pan(0), -1.0, 0.5)] {
let lane = s.track_mut(t).unwrap().lane_mut(&param);
lane.set_point(0, a);
lane.set_point(LEN as i64, b);
}
for block in [63, 256, 512] {
let reference = rendered(&s, block);
let mut with_lane = s.clone();
with_lane.track_mut(t).unwrap().lane_mut(&AutoParam::Mute).set_point(0, 0.0);
assert_eq!(rendered(&with_lane, block), reference);
with_lane.track_mut(t).unwrap().mixer.automation_mode = AutomationMode::Off;
let mut no_lane = with_lane.clone();
no_lane.track_mut(t).unwrap().automation.retain(|lane| lane.param != AutoParam::Mute);
assert_eq!(rendered(&with_lane, block), rendered(&no_lane, block));
}
}
#[test]
fn static_vca_mute_remains_dominant_over_member_mute_automation() {
let (mut s, t) = fixture();
let vca = s.add_track(TrackKind::Vca, ChannelFormat::Stereo, Some("VCA"));
s.track_mut(vca).unwrap().mixer.mute = true;
s.track_mut(t).unwrap().mixer.vca = Some(vca.0);
let reference = rendered(&s, 512);
gated_silent(&reference);
mute(&mut s, t, 101, 200);
gated_silent(&rendered(&s, 512));
}
struct Counted {
inner: Box<dyn Plugin>,
calls: Arc<Mutex<Vec<usize>>>,
instrument: bool,
}
impl Plugin for Counted {
fn info(&self) -> &'static soundcraft_dsp::PluginInfo {
self.inner.info()
}
fn prepare(&mut self, sr: f32, block: usize, ch: usize) {
self.inner.prepare(sr, block, ch);
}
fn reset(&mut self) {
self.inner.reset();
}
fn set_param(&mut self, id: &str, value: f32) -> bool {
self.inner.set_param(id, value)
}
fn param(&self, id: &str) -> Option<f32> {
self.inner.param(id)
}
fn process(&mut self, io: &mut [Vec<f32>], frames: usize) {
self.calls.lock().unwrap().push(frames);
if self.instrument {
for channel in io {
channel.iter_mut().take(frames).for_each(|sample| *sample = 0.25);
}
} else {
self.inner.process(io, frames);
}
}
}
#[test]
fn hosted_processing_keeps_the_callers_blocks_and_unmuted_pcm() {
for instrument in [false, true] {
let (mut s, t) = fixture();
let reference = rendered(&s, 256);
mute(&mut s, t, 101, 200);
let slot = if instrument {
s.track_mut(t).unwrap().kind = TrackKind::Instrument;
s.track_mut(t).unwrap().instrument = Some(Insert::new("clap:test.mute-cadence"));
soundcraft_mix::INSTRUMENT_SLOT
} else {
s.track_mut(t).unwrap().mixer.inserts[0] = Some(Insert::new("clap:test.mute-cadence"));
0
};
let calls = Arc::new(Mutex::new(Vec::with_capacity(4)));
let mut plugin = soundcraft_dsp::create("gain").unwrap();
plugin.prepare(48_000.0, 256, 2);
let mut mix = MixEngine::new(48_000.0, 256);
mix.set_external_instances(true);
mix.adopt(vec![PreparedInstance {
track: t,
slot,
id: "clap:test.mute-cadence".into(),
ch: 2,
plugin: Box::new(Counted { inner: plugin, calls: Arc::clone(&calls), instrument }),
}]);
let mut output = vec![Vec::new(); 2];
for (pos, frames) in [(0, 256), (256, 128), (384, 256), (640, 128)] {
let mut block = vec![vec![0.0; frames]; 2];
mix.render(&s, pos, frames, &mut block);
for (out, channel) in output.iter_mut().zip(block) {
out.extend(channel);
}
}
assert_eq!(*calls.lock().unwrap(), vec![256, 128, 256, 128]);
gated(&output, &reference, 101, 200);
}
}
+37 -2
View File
@@ -126,6 +126,10 @@ pub struct Clip {
/// 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,
@@ -160,6 +164,7 @@ impl Clip {
rating: 0,
sync_point: 0,
color: None,
auto_name: None,
stretch: 1.0,
group: None,
loop_id: None,
@@ -242,6 +247,11 @@ impl Clip {
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
@@ -251,8 +261,16 @@ impl Clip {
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);
if let ClipContent::Audio { offset, .. } | ClipContent::Video { offset, .. } = &mut self.content {
*offset = offset.saturating_add(delta);
// 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);
@@ -506,4 +524,21 @@ mod tests {
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);
}
}
+2 -2
View File
@@ -23,8 +23,8 @@ 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::{
BitDepthSetting, EditMode, EditState, FEET_FRAMES_START, MAX_COMMENT_CHARS, MAX_NAME_CHARS, SESSION_EXTENSION, Session, SessionError, Tool,
TrackView, ZoomState, ZoomToggle, truncate_chars,
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::{Source, SourceAudio, SourcePool, VideoSource};
pub use track::{Playlist, TRACK_COLORS, Track, TrackHeight, TrackKind};
+126
View File
@@ -295,6 +295,40 @@ impl Default for EditState {
}
}
/// Clip List › Auto Rename: the names that editing gives the clips it separates from a clip.
/// Each new clip is named `name`, then `zeros` zeros, then `number`, then `suffix`, and `number`
/// counts up by one for the next clip.
#[derive(Debug, Clone, PartialEq, Eq, Default, serde::Serialize, serde::Deserialize)]
pub struct AutoName {
pub name: String,
/// The number the next new clip gets.
pub number: u64,
pub zeros: usize,
pub suffix: String,
/// Tells this scheme from another one under the same key after an undo or in another session,
/// for pastes from the clipboard. 0 matches no scheme.
#[serde(default)]
pub id: u64,
}
impl AutoName {
/// The largest starting number.
pub const MAX_NUMBER: u64 = 999_999_999;
/// The most zeros before the number.
pub const MAX_ZEROS: usize = 9;
/// Room for `name` and `suffix` together: 29 characters stay free for 9 zeros and the 20
/// digits of a u64, so a clip name never loses its number to [`MAX_NAME_CHARS`].
pub const MAX_TEXT_CHARS: usize = MAX_NAME_CHARS - 29;
/// Repair a scheme read from a file: names that can be drawn and at most [`Self::MAX_ZEROS`] zeros.
pub fn sanitize(&mut self) {
truncate_chars(&mut self.name, Self::MAX_TEXT_CHARS);
truncate_chars(&mut self.suffix, Self::MAX_TEXT_CHARS - self.name.chars().count());
self.zeros = self.zeros.min(Self::MAX_ZEROS);
}
}
/// The document.
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
pub struct Session {
@@ -317,6 +351,10 @@ pub struct Session {
pub outputs: Vec<OutputPath>,
pub markers: Vec<MemoryLocation>,
pub groups: Vec<Group>,
/// Clip List › Auto Rename schemes, keyed by the ids in [`Clip::auto_name`]. The clips separated
/// from one clip share its scheme, so they count up one number sequence.
#[serde(default)]
pub auto_names: std::collections::BTreeMap<u64, crate::AutoName>,
pub edit: EditState,
pub next_id: u64,
#[serde(default)]
@@ -358,6 +396,7 @@ impl Session {
outputs: vec![OutputPath { name: "Out 1-2".into(), first_channel: 0, format: ChannelFormat::Stereo }],
markers: Vec::new(),
groups: Vec::new(),
auto_names: std::collections::BTreeMap::new(),
edit: EditState::default(),
next_id: 1,
comments: String::new(),
@@ -526,6 +565,12 @@ impl Session {
ClipId(self.alloc())
}
/// Drop the Auto Rename schemes that no clip on any playlist uses.
pub fn prune_auto_names(&mut self) {
let used: std::collections::BTreeSet<u64> = self.all_clips().filter_map(|c| c.auto_name).collect();
self.auto_names.retain(|k, _| used.contains(k));
}
// ---- persistence ---------------------------------------------------------------------
pub fn to_json(&self) -> Result<String, SessionError> {
@@ -541,6 +586,7 @@ impl Session {
}
let mut s: Session = serde_json::from_value(v).map_err(|e| SessionError::Format(e.to_string()))?;
s.sanitize();
s.validate()?;
Ok(s)
}
@@ -552,6 +598,7 @@ impl Session {
.map(|t| t.id.0)
.chain(self.tracks.iter().flat_map(|t| t.playlists.iter().flat_map(|p| p.clips.iter().map(|c| c.id.0))))
.chain(self.sources.iter().map(|s| s.id.0))
.chain(self.auto_names.keys().copied())
.chain(self.videos.iter().map(|v| v.id.0))
.chain(self.busses.iter().map(|b| b.id.0))
.chain(self.markers.iter().map(|m| m.id.0))
@@ -625,6 +672,8 @@ impl Session {
let clip_ids: std::collections::BTreeSet<u64> =
self.tracks.iter().flat_map(|t| t.playlists.iter().flat_map(|p| p.clips.iter().map(|c| c.id.0))).collect();
self.midi_originals.retain(|id, _| clip_ids.contains(id));
self.prune_auto_names();
self.auto_names.values_mut().for_each(crate::AutoName::sanitize);
if !self.edit.zoom.samples_per_px.is_finite() || self.edit.zoom.samples_per_px <= 0.0 {
self.edit.zoom.samples_per_px = 1024.0;
}
@@ -633,6 +682,25 @@ impl Session {
}
self.tempo.sanitize();
}
/// Reject a document whose ids are ambiguous or exhausted, run after [`Session::sanitize`] on
/// untrusted data. Duplicate ids in a namespace make commands, selections and source
/// associations act on an arbitrary one of the objects; a maximal `next_id` -- or a maximal
/// existing id, which `sanitize` folds into `next_id` -- makes every later allocation return
/// `u64::MAX` and repeat itself.
fn validate(&self) -> Result<(), SessionError> {
if self.next_id == u64::MAX {
return Err(SessionError::Invalid("the session id space is exhausted".into()));
}
distinct("track", self.tracks.iter().map(|t| t.id.0))?;
distinct("clip", self.tracks.iter().flat_map(|t| t.playlists.iter().flat_map(|p| p.clips.iter().map(|c| c.id.0))))?;
distinct("source", self.sources.iter().map(|s| s.id.0))?;
distinct("video", self.videos.iter().map(|v| v.id.0))?;
distinct("bus", self.busses.iter().map(|b| b.id.0))?;
distinct("marker", self.markers.iter().map(|m| m.id.0))?;
distinct("group", self.groups.iter().map(|g| g.id.0))?;
Ok(())
}
}
/// The most characters kept of a name read from a file: longer ones can't be shown, and drawing
@@ -648,6 +716,17 @@ pub fn truncate_chars(s: &mut String, max: usize) {
}
}
/// Every id in `ids` distinct, or an error naming the namespace and the repeated id.
fn distinct(kind: &str, ids: impl Iterator<Item = u64>) -> Result<(), SessionError> {
let mut seen = std::collections::HashSet::new();
for id in ids {
if !seen.insert(id) {
return Err(SessionError::Invalid(format!("duplicate {kind} id {id}")));
}
}
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
@@ -681,6 +760,19 @@ mod tests {
assert_eq!(back, s);
}
#[test]
fn session_with_legacy_fit_height_loads() {
let mut s = Session::default();
let t = s.add_track(TrackKind::Audio, ChannelFormat::Stereo, None);
if let Some(track) = s.track_mut(t) {
track.height = crate::TrackHeight::Large;
}
let text = s.to_json().unwrap().replace("\"height\": \"Large\"", "\"height\": \"Fit\"");
assert!(text.contains("\"height\": \"Fit\""));
let back = Session::from_json(&text).unwrap();
assert_eq!(back.track(t).map(|tr| tr.height), Some(crate::TrackHeight::Medium));
}
#[test]
fn video_clip_round_trips_and_old_sessions_load() {
let mut s = Session::default();
@@ -770,6 +862,33 @@ mod tests {
assert!(matches!(Session::from_json(&v.to_string()), Err(SessionError::Version(999))));
}
#[test]
fn duplicate_ids_are_rejected_on_load() {
let mut s = Session::default();
s.add_track(TrackKind::Audio, ChannelFormat::Mono, None);
s.add_track(TrackKind::Audio, ChannelFormat::Mono, None);
let mut v = serde_json::to_value(&s).unwrap();
v["tracks"][1]["id"] = v["tracks"][0]["id"].clone();
let err = Session::from_json(&v.to_string()).unwrap_err();
assert!(matches!(&err, SessionError::Invalid(m) if m.contains("duplicate track id")), "{err}");
}
#[test]
fn an_exhausted_id_space_is_rejected_on_load() {
let mut s = Session::default();
s.add_track(TrackKind::Audio, ChannelFormat::Mono, None);
let mut v = serde_json::to_value(&s).unwrap();
v["next_id"] = serde_json::json!(u64::MAX);
let err = Session::from_json(&v.to_string()).unwrap_err();
assert!(matches!(&err, SessionError::Invalid(m) if m.contains("exhausted")), "{err}");
// A maximal existing id is exhausted too, however `next_id` was written: every later
// allocation would return `u64::MAX` again.
let mut v = serde_json::to_value(&s).unwrap();
v["tracks"][0]["id"] = serde_json::json!(u64::MAX);
let err = Session::from_json(&v.to_string()).unwrap_err();
assert!(matches!(&err, SessionError::Invalid(m) if m.contains("exhausted")), "{err}");
}
#[test]
fn sanitize_repairs() {
let mut s = Session::default();
@@ -785,4 +904,11 @@ mod tests {
assert_eq!(tr.mixer.volume_db, 0.0);
assert!(s.next_id > t.0);
}
#[test]
fn a_loaded_auto_name_scheme_leaves_room_for_its_number() {
let mut scheme = crate::AutoName { name: "n".repeat(300), suffix: "s".repeat(300), ..Default::default() };
scheme.sanitize();
assert_eq!(scheme.name.chars().count() + scheme.suffix.chars().count(), crate::AutoName::MAX_TEXT_CHARS);
}
}
+55
View File
@@ -84,6 +84,7 @@ impl TrackKind {
/// Track heights (View › Track Height presets).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default, serde::Serialize, serde::Deserialize)]
#[serde(from = "TrackHeightRepr")]
pub enum TrackHeight {
/// Any height from Micro to Extreme, in points.
Custom(u16),
@@ -99,6 +100,51 @@ pub enum TrackHeight {
Fit(u16),
}
/// What a saved track height may look like: the current form, or the bare `"Fit"` string older sessions used.
#[derive(serde::Deserialize)]
#[serde(untagged)]
enum TrackHeightRepr {
Current(TrackHeightCurrent),
LegacyFit(LegacyFit),
}
#[derive(serde::Deserialize)]
enum LegacyFit {
Fit,
}
/// Mirror of `TrackHeight` that derives the current serialized form.
#[derive(serde::Deserialize)]
enum TrackHeightCurrent {
Custom(u16),
Micro,
Mini,
Small,
Medium,
Large,
Jumbo,
Extreme,
Fit(u16),
}
impl From<TrackHeightRepr> for TrackHeight {
fn from(repr: TrackHeightRepr) -> Self {
match repr {
TrackHeightRepr::Current(TrackHeightCurrent::Custom(p)) => TrackHeight::Custom(p),
TrackHeightRepr::Current(TrackHeightCurrent::Micro) => TrackHeight::Micro,
TrackHeightRepr::Current(TrackHeightCurrent::Mini) => TrackHeight::Mini,
TrackHeightRepr::Current(TrackHeightCurrent::Small) => TrackHeight::Small,
TrackHeightRepr::Current(TrackHeightCurrent::Medium) => TrackHeight::Medium,
TrackHeightRepr::Current(TrackHeightCurrent::Large) => TrackHeight::Large,
TrackHeightRepr::Current(TrackHeightCurrent::Jumbo) => TrackHeight::Jumbo,
TrackHeightRepr::Current(TrackHeightCurrent::Extreme) => TrackHeight::Extreme,
TrackHeightRepr::Current(TrackHeightCurrent::Fit(p)) => TrackHeight::Fit(p),
// Older sessions stored Fit with no size, drawn 88 points tall: the default Medium height.
TrackHeightRepr::LegacyFit(LegacyFit::Fit) => TrackHeight::Medium,
}
}
}
impl TrackHeight {
pub const ALL: [TrackHeight; 7] = [
TrackHeight::Micro,
@@ -328,6 +374,15 @@ impl Track {
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 new_track_defaults() {
let t = Track::new(TrackId(1), "Gtr", TrackKind::Audio, ChannelFormat::Stereo, TRACK_COLORS[0]);
+102 -3
View File
@@ -64,11 +64,15 @@ pub struct AudioSystem {
pub inputs: Vec<DeviceInfo>,
}
/// The name the host gives `d`, if it can produce a description for it.
/// The device's user-facing name. cpal 0.18 removed `DeviceTrait::name()` in favour of the
/// structured `description()`; `Display` for a device also formats that name but reports a
/// formatting error (which `to_string` would turn into a panic), so keep the fallible call here.
pub(crate) fn device_name(d: &cpal::Device) -> Option<String> {
d.description().ok().map(|description| description.name().to_owned())
}
/// Describe one cpal device as [`DeviceInfo`], or return `None` when its name or supported configurations cannot be
/// read. Only formats the playback path can convert are considered.
fn describe(d: &cpal::Device, default_name: Option<&str>, output: bool) -> Option<DeviceInfo> {
let name = device_name(d)?;
let ranges: Vec<cpal::SupportedStreamConfigRange> = if output {
@@ -77,8 +81,7 @@ fn describe(d: &cpal::Device, default_name: Option<&str>, output: bool) -> Optio
d.supported_input_configs().ok()?.filter(|c| format_rank(c.sample_format()).is_some()).collect()
};
let max_channels = ranges.iter().map(cpal::SupportedStreamConfigRange::channels).max().unwrap_or(0);
let sample_rates =
STANDARD_RATES.iter().copied().filter(|r| ranges.iter().any(|c| c.min_sample_rate() <= *r && c.max_sample_rate() >= *r)).collect();
let sample_rates = STANDARD_RATES.iter().copied().filter(|r| ranges.iter().any(|c| c.contains_rate(*r))).collect();
let (mut buffer_min, mut buffer_max) = (None::<u32>, None::<u32>);
for c in &ranges {
if let cpal::SupportedBufferSize::Range { min, max } = c.buffer_size() {
@@ -106,6 +109,45 @@ pub fn audio_systems() -> Vec<AudioSystem> {
.collect()
}
/// True when a cpal error means the stream is dead and only a rebuild can restore audio. cpal 0.18
/// reports every operation through one [`cpal::Error`]: `DeviceNotAvailable` means the device went
/// away, and `StreamInvalidated` explicitly requires the stream to be rebuilt. SoundCraft has no
/// in-place rebuild, so both map to the existing actionable "reconnect audio device" trouble
/// instead of leaving a dead stream silently logging. A rerouted default device
/// (`DeviceChanged`) keeps playing, and an xrun is transient, so neither is a lost stream.
/// `HostUnavailable`, `DeviceBusy` and `BackendError` stay log-only until their retry/rebuild
/// policies exist.
pub(crate) fn stream_lost(e: &cpal::Error) -> bool {
// ErrorKind is non-exhaustive; an equality test avoids a wildcard match arm.
let kind = e.kind();
kind == cpal::ErrorKind::DeviceNotAvailable || kind == cpal::ErrorKind::StreamInvalidated
}
/// The device's configuration at `sample_rate` in the best sample format these streams convert.
/// cpal 0.18 ranks `I24` (and `F64`) above `I16` when it picks a default configuration, so a
/// default alone can name a format the callbacks do not decode; this looks for the same rate in a
/// format [`FORMATS`] holds. The exact channel count is preferred; when the device offers none in
/// a usable format at that rate (for example only an 8-channel F32 stream), the closest available
/// channel count is used instead. Both callbacks fold or copy channels, so a wider or narrower
/// device stream is better than falling back to the silent clock. Callers pass a requested count
/// of at least 2 for output; input may ask for mono.
pub(crate) fn supported_format_config(
configs: &[cpal::SupportedStreamConfigRange],
sample_rate: u32,
channels: u16,
) -> Option<cpal::SupportedStreamConfig> {
let usable = || configs.iter().filter(|c| c.contains_rate(sample_rate)).filter_map(|c| format_rank(c.sample_format()).map(|rank| (rank, c)));
// Exact count first, then the nearest count (more channels breaks the tie).
let exact = usable().filter(|(_, c)| c.channels() == channels).min_by_key(|(rank, _)| *rank);
let best = exact.or_else(|| {
usable().min_by_key(|(rank, c)| {
let distance = c.channels().abs_diff(channels);
(*rank, distance, std::cmp::Reverse(c.channels()))
})
});
best.and_then(|(_, c)| c.try_with_sample_rate(sample_rate))
}
/// The chosen audio system, else the default one.
pub(crate) fn host(cfg: &AudioConfig) -> cpal::Host {
cfg.system
@@ -163,6 +205,63 @@ mod tests {
assert_eq!(serde_json::from_str::<AudioConfig>(r#"{"output":"X","unknown":1}"#).unwrap().output.as_deref(), Some("X"));
}
/// Build a cpal configuration range for tests with a 64..1024-frame buffer.
fn range(channels: u16, min: u32, max: u32, format: cpal::SampleFormat) -> cpal::SupportedStreamConfigRange {
cpal::SupportedStreamConfigRange::new(channels, min, max, cpal::SupportedBufferSize::Range { min: 64, max: 1024 }, format)
}
/// When the default sample format has no usable conversion, the same rate opens with a supported format; the
/// nearest channel count keeps the device open, and unsupported rates fail.
#[test]
fn a_default_format_the_streams_cannot_convert_uses_a_supported_one_at_the_same_rate() {
let configs = [
range(2, 44_100, 48_000, cpal::SampleFormat::I24),
range(2, 44_100, 48_000, cpal::SampleFormat::I16),
range(2, 44_100, 48_000, cpal::SampleFormat::F32),
range(2, 96_000, 96_000, cpal::SampleFormat::F32),
];
let c = supported_format_config(&configs, 48_000, 2).unwrap();
assert_eq!((c.sample_rate(), c.channels(), c.sample_format()), (48_000, 2, cpal::SampleFormat::F32));
assert_eq!(supported_format_config(&configs, 96_000, 2).map(|c| c.sample_format()), Some(cpal::SampleFormat::F32));
assert_eq!(
supported_format_config(&configs, 48_000, 1).map(|c| (c.channels(), c.sample_format())),
Some((2, cpal::SampleFormat::F32)),
"with no mono stream, the nearest usable channel count keeps the device open"
);
let i24_only = [range(2, 44_100, 48_000, cpal::SampleFormat::I24)];
assert!(supported_format_config(&i24_only, 48_000, 2).is_none(), "no supported format at this rate");
}
/// cpal 0.18 may name an I24 stereo default while the only format the callbacks decode is a
/// wider F32 stream at another supported rate. The default-rate search finds nothing usable,
/// so the output fallback retries at the session rate over any usable channel count; the mix
/// already folds or copies channels and resamples.
#[test]
fn a_default_format_with_no_usable_same_rate_stream_falls_back_to_the_session_rate() {
let configs = [range(2, 44_100, 44_100, cpal::SampleFormat::I24), range(8, 48_000, 48_000, cpal::SampleFormat::F32)];
assert!(supported_format_config(&configs, 44_100, 2).is_none(), "the default rate has no usable format");
let c = supported_format_config(&configs, 48_000, 2).expect("the wider F32 stream at the session rate");
assert_eq!((c.sample_rate(), c.channels(), c.sample_format()), (48_000, 8, cpal::SampleFormat::F32));
}
/// Only device-not-available and invalidated-stream errors stop playback; other cpal errors are treated as
/// recoverable.
#[test]
fn only_a_dead_stream_is_reported() {
let error = |kind| cpal::Error::with_message(kind, "test");
assert!(stream_lost(&error(cpal::ErrorKind::DeviceNotAvailable)));
assert!(stream_lost(&error(cpal::ErrorKind::StreamInvalidated)), "the stream must be rebuilt");
for kind in [
cpal::ErrorKind::DeviceChanged,
cpal::ErrorKind::DeviceBusy,
cpal::ErrorKind::HostUnavailable,
cpal::ErrorKind::Xrun,
cpal::ErrorKind::BackendError,
] {
assert!(!stream_lost(&error(kind)), "{kind:?} must not stop the stream");
}
}
#[test]
fn buffer_sizes_follow_the_device_range() {
let r = cpal::SupportedBufferSize::Range { min: 64, max: 1024 };
+365 -66
View File
@@ -45,11 +45,53 @@ const OLD_SESSIONS: usize = 16;
/// renders on worker threads; the cpal callbacks here set it.
pub use soundcraft_mix::{mark_audio_thread, on_audio_thread};
/// Meter values for the UI.
/// Meter values for the UI: the highest peak (and gain reduction) of every block rendered since
/// the UI last took them with [`Player::take_meters`]. The audio thread renders blocks faster than
/// the UI draws (about 94 blocks a second at 512 frames and 48 kHz against 20-60 polls), so
/// keeping only the latest block would miss most transients.
#[derive(Debug, Clone, Default)]
pub struct MeterSnapshot {
pub tracks: HashMap<TrackId, StripMeter>,
pub main: StripMeter,
/// Something was written since the UI last took the values.
fresh: bool,
}
impl MeterSnapshot {
/// Fold one rendered block's meters in (max). Allocation-free once the tracks are known.
fn accumulate(&mut self, main: &StripMeter, tracks: &HashMap<TrackId, StripMeter>) {
self.main.accumulate(main);
for (k, v) in tracks {
match self.tracks.get_mut(k) {
Some(d) => d.accumulate(v),
None => {
self.tracks.insert(*k, v.clone());
}
}
}
self.fresh = true;
}
/// Start a new accumulation period, keeping every entry and its storage.
fn reset_levels(&mut self) {
self.main.reset_levels();
self.tracks.values_mut().for_each(StripMeter::reset_levels);
self.fresh = false;
}
/// Nothing is playing or monitored: every meter reads silence. Levels are zeroed in place,
/// so starting again does not allocate the tracks' entries anew.
fn silence(&mut self) {
self.reset_levels();
self.fresh = true;
}
/// Drop the entries of deleted tracks.
fn forget(&mut self, gone: &[TrackId]) {
for id in gone {
self.tracks.remove(id);
}
}
}
/// Plugin states read from the live instances: `(track, slot or INSTRUMENT_SLOT, state)`.
@@ -72,6 +114,8 @@ enum Cmd {
},
Stop,
Locate(Samples),
/// Tracks the latest session no longer has (worked out on the UI thread): their meters go.
Forget(Vec<TrackId>),
}
struct Shared {
@@ -87,7 +131,7 @@ struct Shared {
pub enum OutputTrouble {
/// A block panicked; the output stays silent.
Bug,
/// The device went away.
/// The device went away, or the stream was invalidated and must be rebuilt.
Gone,
/// The device stopped asking for audio while playing.
Stalled,
@@ -98,6 +142,7 @@ pub enum OutputTrouble {
struct Fault {
/// A block panicked: output stays silent from then on.
failed: AtomicBool,
/// The device went away or the stream was invalidated: only a reconnect can restore audio.
gone: AtomicBool,
/// Blocks so far, to tell a device that stopped asking for audio.
beats: std::sync::atomic::AtomicU64,
@@ -171,6 +216,11 @@ struct AudioState {
/// Retired plugin instances go back to the UI thread to be dropped there.
garbage: Sender<Vec<Box<dyn Plugin>>>,
old_sessions: Option<SyncSender<Arc<Session>>>,
/// Meters of blocks rendered while the UI held the shared snapshot (`try_lock` failed), so
/// their peaks reach it with the next block instead of being lost.
unsent: MeterSnapshot,
/// Deleted tracks still to drop from the shared snapshot (it was locked when they went).
forget_shared: Vec<TrackId>,
}
fn new_engine(sr: f32) -> MixEngine {
@@ -202,6 +252,8 @@ impl AudioState {
device_rate,
input: None,
recording: false,
unsent: MeterSnapshot::default(),
forget_shared: Vec::new(),
}
}
@@ -210,6 +262,15 @@ impl AudioState {
while let Ok(c) = self.rx.try_recv() {
match c {
Cmd::Input(r) => self.input = Some(r),
Cmd::Forget(gone) => {
self.unsent.forget(&gone);
if self.forget_shared.is_empty() {
self.forget_shared = gone;
} else {
self.forget_shared.extend_from_slice(&gone);
}
self.forget_meters();
}
Cmd::Recording(r) => self.recording = r,
Cmd::Session(s, instances) => {
changed = true;
@@ -310,23 +371,34 @@ impl AudioState {
self.mix.render(&self.session, self.pos, n, &mut self.pending);
self.send_garbage();
self.pending_len = n;
if let Ok(mut m) = self.shared.meters.try_lock() {
m.main.copy_from(&self.mix.main_meter);
for (k, v) in &self.mix.meters {
match m.tracks.get_mut(k) {
Some(d) => d.copy_from(v),
None => {
m.tracks.insert(*k, v.clone());
}
}
}
}
self.publish_meters();
if self.playing {
self.pos += n as i64;
self.shared.position.store(self.pos, Ordering::Relaxed);
}
}
/// Drop deleted tracks' meters from the shared snapshot, unless the UI holds it right now.
fn forget_meters(&mut self) {
if !self.forget_shared.is_empty()
&& let Ok(mut m) = self.shared.meters.try_lock()
{
m.forget(&self.forget_shared);
self.forget_shared.clear();
}
}
/// Hand this block's meters to the UI without blocking: they are folded (max) into what the
/// UI has not taken yet. If the UI holds the snapshot right now, they wait in `unsent`.
fn publish_meters(&mut self) {
self.unsent.accumulate(&self.mix.main_meter, &self.mix.meters);
self.forget_meters();
if let Ok(mut m) = self.shared.meters.try_lock() {
m.accumulate(&self.unsent.main, &self.unsent.tracks);
self.unsent.reset_levels();
}
}
/// Fill an interleaved device buffer.
fn fill(&mut self, out: &mut [f32], channels: usize) {
self.drain();
@@ -334,9 +406,10 @@ impl AudioState {
let monitoring = self.input.is_some() && self.session.tracks.iter().any(|t| t.mixer.input_monitor || t.mixer.record_arm);
if !self.playing && !monitoring {
out.iter_mut().for_each(|x| *x = 0.0);
self.unsent.silence();
self.forget_meters();
if let Ok(mut m) = self.shared.meters.try_lock() {
m.main = StripMeter::default();
m.tracks.clear();
m.silence();
}
return;
}
@@ -418,11 +491,30 @@ struct Side {
refused: HashSet<(TrackId, usize)>,
garbage: Receiver<Vec<Box<dyn Plugin>>>,
old_sessions: Option<Receiver<Arc<Session>>>,
/// The tracks of the session last sent, to tell the audio thread which ones went.
tracks: HashSet<TrackId>,
}
impl Side {
fn new(rate: f32, garbage: Receiver<Vec<Box<dyn Plugin>>>) -> Side {
Side { shadow: HashMap::new(), rate, editors: HashMap::new(), states: BTreeMap::new(), refused: HashSet::new(), garbage, old_sessions: None }
Side {
shadow: HashMap::new(),
rate,
editors: HashMap::new(),
states: BTreeMap::new(),
refused: HashSet::new(),
garbage,
old_sessions: None,
tracks: HashSet::new(),
}
}
/// Note `s`'s tracks; the ones the previous session had and `s` doesn't.
fn gone_tracks(&mut self, s: &Session) -> Vec<TrackId> {
let now: HashSet<TrackId> = s.tracks.iter().map(|t| t.id).collect();
let gone = self.tracks.difference(&now).copied().collect();
self.tracks = now;
gone
}
/// Drops retired instances (closing their editors first, inside the plugin's drop) and
@@ -511,6 +603,9 @@ impl Player {
self.side.lock().unwrap_or_else(PoisonError::into_inner)
}
/// Open the output device for `session`, trying the requested buffer size before the device default, then build
/// the shared player state. If no configuration starts or the device cannot open, fall back to a silent clock at
/// the session rate.
fn open(session: Arc<Session>, cfg: &AudioConfig) -> Player {
let shared = Arc::new(Shared {
position: AtomicI64::new(0),
@@ -521,7 +616,11 @@ impl Player {
let want = session.sample_rate.hz();
let want_ch = u16::try_from(main_channels(&session)).unwrap_or(2);
let side_rate = session.sample_rate.as_f64() as f32;
let new_side = |garbage: Receiver<Vec<Box<dyn Plugin>>>| Mutex::new(Side::new(side_rate, garbage));
let new_side = |garbage: Receiver<Vec<Box<dyn Plugin>>>| {
let mut side = Side::new(side_rate, garbage);
side.gone_tracks(&session);
Mutex::new(side)
};
match open_device(want, want_ch, cfg) {
Ok((device, config, format, name)) => {
// The chosen buffer size first; if the device refuses it, its default.
@@ -539,9 +638,9 @@ impl Player {
let state = AudioState::new(rx, Arc::clone(&shared), Arc::clone(&session), f64::from(rate), gtx);
let fault = Arc::new(Fault::default());
let stream = match format {
cpal::SampleFormat::I32 => build_output::<i32>(&device, config, state, channels, &fault, &shared),
cpal::SampleFormat::I16 => build_output::<i16>(&device, config, state, channels, &fault, &shared),
_ => build_output::<f32>(&device, config, state, channels, &fault, &shared),
cpal::SampleFormat::I32 => build_output::<i32>(&device, *config, state, channels, &fault, &shared),
cpal::SampleFormat::I16 => build_output::<i16>(&device, *config, state, channels, &fault, &shared),
_ => build_output::<f32>(&device, *config, state, channels, &fault, &shared),
}?;
Ok(Opened { stream, tx, grx, fault })
},
@@ -678,8 +777,14 @@ impl Player {
/// Sends a new session snapshot to the audio thread, with freshly created (state-restored,
/// prepared) instances of any third-party plugin it newly needs. Call on the UI thread.
pub fn update_session(&self, s: Arc<Session>) {
let instances = self.side().prepare(&s);
let (instances, gone) = {
let mut side = self.side();
(side.prepare(&s), side.gone_tracks(&s))
};
let _ = self.tx.send(Cmd::Session(s, instances));
if !gone.is_empty() {
let _ = self.tx.send(Cmd::Forget(gone));
}
}
/// Reads the state of every live third-party plugin, for saving the session.
@@ -801,18 +906,35 @@ impl Player {
self.shared.playing.load(Ordering::Relaxed)
}
/// A copy of the meters accumulated since the last [`Player::take_meters`], leaving them in
/// place (for inspection; the UI takes them instead).
pub fn meters(&self) -> MeterSnapshot {
self.shared.meters.lock().unwrap_or_else(PoisonError::into_inner).clone()
}
/// Copy meters into reusable UI storage. Once track/channel counts are stable this performs
/// no allocation, unlike cloning a new snapshot every video frame.
pub fn copy_meters(&self, out: &mut MeterSnapshot) {
let source = self.shared.meters.lock().unwrap_or_else(PoisonError::into_inner);
copy_meter_snapshot(&source, out);
/// Take the meters accumulated since the last call into reusable UI storage and start a new
/// period: `out` gets the highest peak of every block rendered in between. If no block was
/// rendered since (a large device buffer renders several blocks per callback), `out` keeps
/// the previous values rather than dropping to silence. Call it from one place only (two
/// readers would split the peaks between them). Once track/channel counts are stable this
/// performs no allocation. The audio thread only ever `try_lock`s, so holding the lock here
/// never blocks it; blocks rendered meanwhile reach the next call.
pub fn take_meters(&self, out: &mut MeterSnapshot) {
let mut source = self.shared.meters.lock().unwrap_or_else(PoisonError::into_inner);
take_meter_snapshot(&mut source, out);
}
}
fn take_meter_snapshot(source: &mut MeterSnapshot, out: &mut MeterSnapshot) {
if !source.fresh {
// No block since the last take: keep the readings, but not those of deleted tracks.
out.tracks.retain(|id, _| source.tracks.contains_key(id));
return;
}
copy_meter_snapshot(source, out);
source.reset_levels();
}
fn copy_meter_snapshot(source: &MeterSnapshot, out: &mut MeterSnapshot) {
out.tracks.retain(|id, _| source.tracks.contains_key(id));
for (id, meter) in &source.tracks {
@@ -953,7 +1075,7 @@ fn first_started<A: std::fmt::Debug, S, E: std::fmt::Display>(
/// never allocates), converted to the device's sample type.
fn build_output<T>(
device: &cpal::Device,
config: &cpal::StreamConfig,
config: cpal::StreamConfig,
mut state: AudioState,
channels: usize,
fault: &Arc<Fault>,
@@ -968,7 +1090,7 @@ where
let chunk = scratch.len();
let (failed, guard, gone) = (Arc::clone(fault), Arc::clone(shared), Arc::clone(fault));
device.build_output_stream(
*config,
config,
move |data: &mut [T], _: &cpal::OutputCallbackInfo| {
mark_audio_thread();
for out in data.chunks_mut(chunk) {
@@ -983,7 +1105,11 @@ where
// broken): marked, so the logger hands the record off instead of writing it.
move |e: cpal::Error| {
mark_audio_thread();
if e.kind() == cpal::ErrorKind::DeviceNotAvailable {
// cpal 0.18 uses one error type for every operation. A device that went away, or a
// stream that must be rebuilt (`StreamInvalidated`), stops playback until the user
// reconnects; a rerouted default device (`DeviceChanged`) keeps playing on the new
// route, and an xrun is transient, so neither counts as losing the stream.
if devices::stream_lost(&e) {
gone.gone.store(true, Ordering::Relaxed);
}
log::warn!("audio stream error: {e}");
@@ -1004,51 +1130,67 @@ fn open_device(want_rate: u32, want_ch: u16, cfg: &AudioConfig) -> Result<(cpal:
// Prefer a config at the chosen (else session) rate; else the device default (we resample).
let want_ch = want_ch.max(2);
let rates: Vec<u32> = cfg.sample_rate.into_iter().chain(Some(want_rate)).collect();
let mut chosen: Option<(cpal::StreamConfig, cpal::SupportedBufferSize, cpal::SampleFormat)> = None;
// Better: a preferred format first, then more channels.
let better = |c: &cpal::SupportedStreamConfigRange, x: &(cpal::StreamConfig, cpal::SupportedBufferSize, cpal::SampleFormat)| {
let (rc, rx) = (devices::format_rank(c.sample_format()).unwrap_or(usize::MAX), devices::format_rank(x.2).unwrap_or(usize::MAX));
rc < rx || (rc == rx && c.channels() > x.0.channels)
};
if let Ok(configs) = device.supported_output_configs() {
let configs: Vec<cpal::SupportedStreamConfigRange> = configs.collect();
for rate in rates {
for c in &configs {
if devices::format_rank(c.sample_format()).is_some()
&& c.min_sample_rate() <= rate
&& c.max_sample_rate() >= rate
&& c.channels() >= 2
&& c.channels() <= want_ch
&& chosen.as_ref().is_none_or(|x| better(c, x))
{
chosen = Some((c.with_sample_rate(rate).config(), *c.buffer_size(), c.sample_format()));
}
}
if chosen.is_some() {
break;
}
}
}
let (config, format) = match chosen {
let configs: Vec<cpal::SupportedStreamConfigRange> = device.supported_output_configs().map(|configs| configs.collect()).unwrap_or_default();
let (config, format) = match pick_output_config(&configs, &rates, want_ch) {
Some((mut c, buffers, format)) => {
c.buffer_size = devices::buffer_size(cfg.buffer_size, &buffers);
(c, format)
}
None => {
let d = device.default_output_config().map_err(|e| e.to_string())?;
if devices::format_rank(d.sample_format()).is_none() {
return Err(format!("unsupported device sample format {:?}", d.sample_format()));
}
let buffers = *d.buffer_size();
let format = d.sample_format();
let mut c = d.config();
c.buffer_size = devices::buffer_size(cfg.buffer_size, &buffers);
(c, format)
// cpal 0.18's default can name a format these callbacks do not decode (I24, F64);
// open the same rate and channels in a format they do, rather than lose output.
let c = if devices::format_rank(format).is_some() {
d
} else {
// Same rate and channels first; else any usable channel count at the default
// rate; else the session rate (the mix resamples and folds channels). A device
// whose only decoded format is wider or at another rate would otherwise open no
// stream at all.
devices::supported_format_config(&configs, d.sample_rate(), d.channels())
.or_else(|| devices::supported_format_config(&configs, want_rate, want_ch))
.ok_or_else(|| format!("unsupported device sample format {format:?}"))?
};
let mut stream_config = c.config();
stream_config.buffer_size = devices::buffer_size(cfg.buffer_size, c.buffer_size());
(stream_config, c.sample_format())
}
};
Ok((device, config, format, name))
}
/// Pick the output configuration for the first requested rate the device offers (the configured
/// rate first, then the session rate), preferring the best sample format and then the most
/// channels from stereo up to `want_ch`. `None` when the device offers none of the requested
/// rates in a usable format; the caller then uses the device's default output configuration,
/// which the mix resamples to the session rate.
fn pick_output_config(
configs: &[cpal::SupportedStreamConfigRange],
rates: &[u32],
want_ch: u16,
) -> Option<(cpal::StreamConfig, cpal::SupportedBufferSize, cpal::SampleFormat)> {
for &rate in rates {
let mut best: Option<(cpal::StreamConfig, cpal::SupportedBufferSize, cpal::SampleFormat, usize, u16)> = None;
for c in configs {
let Some(rank) = devices::format_rank(c.sample_format()) else { continue };
if !c.contains_rate(rate) || c.channels() < 2 || c.channels() > want_ch {
continue;
}
let Some(config) = c.try_with_sample_rate(rate) else { continue };
// Better: a preferred format first, then more channels (ties keep the earlier range).
let better = (rank, std::cmp::Reverse(c.channels()));
if best.as_ref().is_none_or(|x| better < (x.3, std::cmp::Reverse(x.4))) {
best = Some((config.config(), *c.buffer_size(), c.sample_format(), rank, c.channels()));
}
}
if let Some((config, buffers, format, _, _)) = best {
return Some((config, buffers, format));
}
}
None
}
#[cfg(test)]
mod tests {
use super::*;
@@ -1068,13 +1210,13 @@ mod tests {
fn pause(&self) -> Result<(), cpal::Error> {
Ok(())
}
/// The fake stream claims one fixed buffer.
/// The fake stream has no clock and no fixed buffer; neither is used by these tests.
fn buffer_size(&self) -> Result<cpal::FrameCount, cpal::Error> {
Ok(1024)
Ok(0)
}
/// The fake stream has no clock.
/// The fake stream has no usable clock; these tests do not read it.
fn now(&self) -> cpal::StreamInstant {
cpal::StreamInstant::new(0, 0)
cpal::StreamInstant::from_nanos(0)
}
}
@@ -1106,6 +1248,47 @@ mod tests {
assert_eq!(won.map(|(n, _)| n), Some(2));
}
/// Build a cpal configuration range for tests with a 64..1024-frame buffer.
fn range(channels: u16, min: u32, max: u32, format: cpal::SampleFormat) -> cpal::SupportedStreamConfigRange {
cpal::SupportedStreamConfigRange::new(channels, min, max, cpal::SupportedBufferSize::Range { min: 64, max: 1024 }, format)
}
/// Rates are tried in order, so a stale configured rate falls through to the session rate; when no requested rate
/// is offered the caller uses the device default and resamples.
#[test]
fn output_config_uses_the_first_requested_rate_the_device_offers() {
let configs = [
range(2, 44_100, 48_000, cpal::SampleFormat::I16),
range(2, 44_100, 96_000, cpal::SampleFormat::F32),
range(8, 44_100, 96_000, cpal::SampleFormat::F32),
];
let (config, buffers, format) = pick_output_config(&configs, &[96_000, 48_000], 8).unwrap();
assert_eq!((config.sample_rate, config.channels, config.buffer_size), (96_000, 8, cpal::BufferSize::Default));
assert_eq!(format, cpal::SampleFormat::F32);
assert_eq!(buffers, cpal::SupportedBufferSize::Range { min: 64, max: 1024 });
// 192 kHz is not offered: the session's 48 kHz is tried next.
let (config, _, format) = pick_output_config(&configs, &[192_000, 48_000], 8).unwrap();
assert_eq!((config.sample_rate, config.channels, format), (48_000, 8, cpal::SampleFormat::F32));
// No requested rate is offered: the caller uses the device's default config and resamples.
assert!(pick_output_config(&configs, &[192_000], 8).is_none());
}
/// At a supported rate, a preferred sample format wins over more channels; output stays between stereo and the
/// requested channel cap, and mono-only or wider devices take the default path.
#[test]
fn output_config_prefers_format_over_channels_and_needs_stereo_within_the_cap() {
let configs = [range(8, 44_100, 48_000, cpal::SampleFormat::I16), range(2, 44_100, 48_000, cpal::SampleFormat::F32)];
let (config, _, format) = pick_output_config(&configs, &[48_000], 8).unwrap();
assert_eq!((config.channels, format), (2, cpal::SampleFormat::F32), "a preferred format wins over more channels");
let (config, _, _) = pick_output_config(&configs, &[48_000], 2).unwrap();
assert_eq!(config.channels, 2, "the channel cap applies");
let mono = [range(1, 44_100, 48_000, cpal::SampleFormat::F32)];
assert!(pick_output_config(&mono, &[48_000], 8).is_none(), "a mono-only device takes the default config path");
let surround = [range(6, 44_100, 48_000, cpal::SampleFormat::F32)];
assert!(pick_output_config(&surround, &[48_000], 2).is_none(), "a wider device is not narrowed here");
assert_eq!(pick_output_config(&surround, &[48_000], 6).map(|(c, _, _)| c.channels), Some(6));
}
#[test]
fn no_config_that_starts_yields_none_so_the_caller_falls_back_to_a_silent_clock() {
let mut tried = 0;
@@ -1136,6 +1319,122 @@ mod tests {
assert_eq!(out.main, source.main);
}
/// A mono track playing a quiet signal with one short spike in the block starting at
/// `spike_at`, and an audio thread ready to play it.
fn spike_session(spike_at: usize) -> (Arc<Shared>, AudioState, std::sync::mpsc::Sender<Cmd>, TrackId) {
use soundcraft_model::{ChannelFormat, Clip, SourceAudio, SourceId, TrackKind};
let mut s = soundcraft_model::Session::default();
let t = s.add_track(TrackKind::Audio, ChannelFormat::Mono, None);
let mut x = vec![0.01f32; 48_000];
x[spike_at..spike_at + 32].fill(0.9);
s.pool.insert(SourceId(5), Arc::new(SourceAudio::new(soundcraft_audio_io::AudioBuffer { sample_rate: 48_000, channels: vec![x] })));
let id = s.new_clip_id();
s.track_mut(t).unwrap().playlist_mut().unwrap().clips.push(Clip::audio(id, "x", SourceId(5), 0, 0, 48_000));
let shared = Arc::new(Shared {
position: AtomicI64::new(0),
playing: AtomicBool::new(false),
speed: AtomicU32::new(1000),
meters: Mutex::new(MeterSnapshot::default()),
});
let (tx, rx) = channel();
let st = AudioState::new(rx, Arc::clone(&shared), Arc::new(s), 48_000.0, channel().0);
tx.send(Cmd::Play { from: 0, end: None, looped: None }).unwrap();
(shared, st, tx, t)
}
#[test]
fn a_spike_in_one_block_among_quiet_ones_reaches_the_ui() {
// Eight 512-frame blocks per callback, as between two UI polls; the spike is in the 3rd.
let (shared, mut st, _tx, t) = spike_session(2 * 512 + 100);
let mut out = vec![0.0f32; 2 * 8 * 512];
st.fill(&mut out, 2);
// The last block alone (what the UI used to see) is quiet.
assert!(st.mix.meters[&t].peak[0] < 0.05);
let mut ui = MeterSnapshot::default();
take_meter_snapshot(&mut shared.meters.lock().unwrap(), &mut ui);
assert!(ui.tracks[&t].peak[0] > 0.85, "{:?}", ui.tracks[&t]);
assert!(ui.tracks[&t].peaks[0] > 0.85);
assert!(ui.main.peak[0] > 0.5 && ui.main.peaks.iter().all(|p| *p > 0.5), "{:?}", ui.main);
// Taking starts a new period: the next quiet blocks read quiet.
st.fill(&mut out, 2);
take_meter_snapshot(&mut shared.meters.lock().unwrap(), &mut ui);
assert!(ui.tracks[&t].peak[0] < 0.05 && ui.main.peak[0] < 0.05);
// No block since the last take: the reading stays rather than dropping to silence.
take_meter_snapshot(&mut shared.meters.lock().unwrap(), &mut ui);
assert!(ui.tracks[&t].peak[0] > 0.005);
}
#[test]
fn a_spike_rendered_while_the_ui_holds_the_meters_arrives_later() {
let (shared, mut st, _tx, t) = spike_session(100);
let mut out = vec![0.0f32; 2 * 512];
{
let _held = shared.meters.lock().unwrap();
st.fill(&mut out, 2);
}
st.fill(&mut out, 2);
let mut ui = MeterSnapshot::default();
take_meter_snapshot(&mut shared.meters.lock().unwrap(), &mut ui);
assert!(ui.tracks[&t].peak[0] > 0.85, "{:?}", ui.tracks[&t]);
}
#[test]
fn stopping_zeroes_meters_in_place_and_removed_tracks_are_pruned() {
let (shared, mut st, tx, t) = spike_session(100);
let mut out = vec![0.0f32; 2 * 512];
st.fill(&mut out, 2);
tx.send(Cmd::Stop).unwrap();
st.fill(&mut out, 2);
{
let m = shared.meters.lock().unwrap();
let e = m.tracks.get(&t).expect("kept while stopped, so playing again does not allocate it");
assert!(e.peak == [0.0; 2] && e.peaks.iter().all(|p| *p == 0.0) && e.peaks.len() == 1, "{e:?}");
assert!(m.fresh, "the UI sees the silence");
}
// The track is deleted: the UI side works out which tracks went and says so.
let mut side = Side::new(48_000.0, channel().1);
side.gone_tracks(&st.session);
let empty = soundcraft_model::Session::default();
let gone = side.gone_tracks(&empty);
assert_eq!(gone, vec![t]);
tx.send(Cmd::Session(Arc::new(empty), Vec::new())).unwrap();
tx.send(Cmd::Forget(gone)).unwrap();
tx.send(Cmd::Play { from: 0, end: None, looped: None }).unwrap();
st.fill(&mut out, 2);
assert!(!shared.meters.lock().unwrap().tracks.contains_key(&t));
assert!(!st.unsent.tracks.contains_key(&t) && !st.mix.meters.contains_key(&t));
}
#[test]
fn a_take_without_new_blocks_still_drops_deleted_tracks() {
let gone = TrackId(7);
let mut source = MeterSnapshot::default();
source.accumulate(&StripMeter::default(), &HashMap::from([(gone, StripMeter::default())]));
let mut ui = MeterSnapshot::default();
take_meter_snapshot(&mut source, &mut ui);
source.forget(&[gone]);
take_meter_snapshot(&mut source, &mut ui);
assert!(ui.tracks.is_empty());
}
#[test]
fn a_channel_count_change_takes_the_new_stereo_peaks() {
let mut acc = StripMeter { peak: [0.9, 0.9], peaks: vec![0.9], ..StripMeter::default() };
acc.accumulate(&StripMeter { peak: [0.9, 0.0], peaks: vec![0.9, 0.0], ..StripMeter::default() });
assert_eq!(acc.peak, [0.9, 0.0]);
}
#[test]
fn gain_reduction_keeps_its_maximum_until_taken() {
let mut acc = StripMeter::default();
for gr in [1.0, 9.0, 2.0, f32::NAN] {
acc.accumulate(&StripMeter { peak: [0.1, 0.1], peaks: vec![0.1], gain_reduction_db: gr, ..StripMeter::default() });
}
assert_eq!(acc.gain_reduction_db, 9.0);
acc.reset_levels();
assert_eq!((acc.gain_reduction_db, acc.peak, acc.peaks.len()), (0.0, [0.0; 2], 1));
}
#[test]
fn a_block_that_panics_goes_silent_and_is_reported_once() {
let shared = Shared {
+65 -17
View File
@@ -142,7 +142,7 @@ pub struct Recorder {
#[cfg(not(target_os = "freebsd"))]
fn build_input<T>(
device: &cpal::Device,
config: &cpal::StreamConfig,
config: cpal::StreamConfig,
shared: &Arc<Shared>,
monitor: &Arc<InputRing>,
) -> Result<cpal::Stream, cpal::Error>
@@ -158,7 +158,7 @@ where
let chunk = scratch.len();
let mut owed = 0;
device.build_input_stream(
*config,
config,
move |data: &[T], _: &cpal::InputCallbackInfo| {
crate::mark_audio_thread();
sh.guarded(|| {
@@ -179,7 +179,10 @@ where
move |e: cpal::Error| {
// Possibly the audio thread (see `crate::mark_audio_thread`).
crate::mark_audio_thread();
if e.kind() == cpal::ErrorKind::DeviceNotAvailable {
// cpal 0.18 reports every operation through one error type; a device that went away
// or a stream that must be rebuilt (`StreamInvalidated`) stops capture.
// `DeviceChanged` (a rerouted default device) and an xrun are not faults.
if crate::devices::stream_lost(&e) {
let _ = errors.fault.compare_exchange(0, GONE, Ordering::Relaxed, Ordering::Relaxed);
}
log::warn!("input stream error: {e}");
@@ -188,6 +191,23 @@ where
)
}
/// The input configuration at `rate` when the device offers one: the best sample format first,
/// then the most channels (so a stereo interface is preferred over a mono one). `None` lets the
/// caller use the device's default input configuration (the mix resamples when the rates differ).
fn pick_input_config(configs: &[cpal::SupportedStreamConfigRange], rate: u32) -> Option<cpal::SupportedStreamConfig> {
configs
.iter()
.filter_map(|c| {
let rank = crate::devices::format_rank(c.sample_format())?;
if !c.contains_rate(rate) {
return None;
}
c.try_with_sample_rate(rate).map(|config| (rank, std::cmp::Reverse(c.channels()), config))
})
.min_by_key(|(rank, channels, _)| (*rank, *channels))
.map(|(_, _, config)| config)
}
impl Recorder {
/// Open the default input device. Errors when there is none (the caller shows a message).
pub fn open() -> Result<Recorder, String> {
@@ -205,17 +225,21 @@ impl Recorder {
let name = crate::devices::device_name(&device).unwrap_or_else(|| "Input".into());
// At the chosen rate when the device offers it, so input and output run together: the
// best format first, then the most channels.
let at_rate = audio.sample_rate.and_then(|r| {
device
.supported_input_configs()
.ok()?
.filter(|c| crate::devices::format_rank(c.sample_format()).is_some() && c.min_sample_rate() <= r && c.max_sample_rate() >= r)
.min_by_key(|c| (crate::devices::format_rank(c.sample_format()), std::cmp::Reverse(c.channels())))
.map(|c| c.with_sample_rate(r))
});
let configs: Vec<cpal::SupportedStreamConfigRange> = device.supported_input_configs().map(|configs| configs.collect()).unwrap_or_default();
let at_rate = audio.sample_rate.and_then(|r| pick_input_config(&configs, r));
let cfg = match at_rate {
Some(c) => c,
None => device.default_input_config().map_err(|e| e.to_string())?,
None => {
let d = device.default_input_config().map_err(|e| e.to_string())?;
// cpal 0.18's default can name a format these callbacks do not decode (I24, F64);
// open the same rate and channels in a format they do.
if crate::devices::format_rank(d.sample_format()).is_some() {
d
} else {
crate::devices::supported_format_config(&configs, d.sample_rate(), d.channels())
.ok_or_else(|| format!("unsupported input sample format {:?}", d.sample_format()))?
}
}
};
let format = cfg.sample_format();
if crate::devices::format_rank(format).is_none() {
@@ -227,18 +251,18 @@ impl Recorder {
let ring = CaptureRing::new((config.sample_rate as usize).saturating_mul(channels * RING_SECONDS).min(1 << 25));
let shared = Arc::new(Shared::new(ring));
let monitor = InputRing::new(usize::from(config.channels), config.sample_rate);
let build = |config: &cpal::StreamConfig| match format {
let build = |config: cpal::StreamConfig| match format {
cpal::SampleFormat::I32 => build_input::<i32>(&device, config, &shared, &monitor),
cpal::SampleFormat::I16 => build_input::<i16>(&device, config, &shared, &monitor),
_ => build_input::<f32>(&device, config, &shared, &monitor),
};
// The chosen buffer size first; if the device refuses it, its default.
let stream = match build(&config) {
let stream = match build(config) {
Ok(s) => s,
Err(e) if config.buffer_size != cpal::BufferSize::Default => {
log::warn!("cannot open the input with buffer {:?}: {e}; using the device default", config.buffer_size);
config.buffer_size = cpal::BufferSize::Default;
build(&config).map_err(|e| e.to_string())?
build(config).map_err(|e| e.to_string())?
}
Err(e) => return Err(e.to_string()),
};
@@ -290,7 +314,7 @@ impl Recorder {
}
let name = &self.device_name;
match self.shared.fault.load(Ordering::Relaxed) {
GONE => return Some(Trouble::Device(format!("“{name}” was disconnected"))),
GONE => return Some(Trouble::Device(format!("“{name}” is unavailable"))),
BROKEN => return Some(Trouble::Device("the audio input hit an internal error (see the log)".into())),
_ => {}
}
@@ -462,6 +486,28 @@ mod tests {
assert_eq!(out, [0.5, 0.0, 0.0, 0.0, -0.5]);
}
/// Build a cpal configuration range for tests with a 64..1024-frame buffer.
fn range(channels: u16, min: u32, max: u32, format: cpal::SampleFormat) -> cpal::SupportedStreamConfigRange {
cpal::SupportedStreamConfigRange::new(channels, min, max, cpal::SupportedBufferSize::Range { min: 64, max: 1024 }, format)
}
/// At the chosen rate, input selection prefers the best supported format, then more channels; a rate the device
/// does not offer returns `None` so the caller can fall back.
#[test]
fn input_config_prefers_the_best_format_then_more_channels_at_the_chosen_rate() {
let configs = [
range(1, 44_100, 192_000, cpal::SampleFormat::I32),
range(2, 44_100, 96_000, cpal::SampleFormat::I16),
range(2, 48_000, 96_000, cpal::SampleFormat::F32),
range(8, 44_100, 48_000, cpal::SampleFormat::F32),
];
let c = pick_input_config(&configs, 96_000).unwrap();
assert_eq!((c.sample_rate(), c.channels(), c.sample_format()), (96_000, 2, cpal::SampleFormat::F32));
let c = pick_input_config(&configs, 48_000).unwrap();
assert_eq!((c.sample_rate(), c.channels(), c.sample_format()), (48_000, 8, cpal::SampleFormat::F32), "more channels win in the same format");
assert!(pick_input_config(&configs, 192_001).is_none(), "an unsupported rate falls back to the device default");
}
fn recorder(shared: &Arc<Shared>) -> Recorder {
Recorder {
shared: Arc::clone(shared),
@@ -476,6 +522,8 @@ mod tests {
}
}
/// A stalled armed input, a panicking callback and a lost device all surface as `Trouble`; a quiet input that is
/// not recording does not.
#[test]
fn a_bug_a_stall_or_a_lost_device_is_trouble() {
let shared = Arc::new(Shared::new(CaptureRing::new(64)));
@@ -496,7 +544,7 @@ mod tests {
assert_eq!(r.trouble(), Some(Trouble::Device("the audio input hit an internal error (see the log)".into())));
let gone = Arc::new(Shared::new(CaptureRing::new(64)));
gone.fault.store(GONE, Ordering::Relaxed);
assert_eq!(recorder(&gone).trouble(), Some(Trouble::Device("“Mic In” was disconnected".into())));
assert_eq!(recorder(&gone).trouble(), Some(Trouble::Device("“Mic In” is unavailable".into())));
}
#[test]
+13
View File
@@ -186,6 +186,19 @@ mod tests {
}
}
#[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();
+15 -1
View File
@@ -108,7 +108,9 @@ impl FrameRate {
if n == 0 {
return 0;
}
let v = (i128::from(f) * i128::from(sr.hz()) * i128::from(d) + i128::from(n) / 2) / i128::from(n);
// 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 })
}
@@ -229,6 +231,18 @@ mod tests {
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;
+7 -1
View File
@@ -21,7 +21,10 @@ soundcraft-mix.workspace = true
soundcraft-engine.workspace = true
soundcraft-playback.workspace = true
soundcraft-video.workspace = true
egui.workspace = true
# 0.37 makes the bundled faces opt-in; the app's own font loader starts from
# `FontDefinitions::default()`, and kittest never supplies fonts to this root.
egui = { workspace = true, features = ["default_fonts", "monochrome_emoji_fonts"] }
web-time.workspace = true
serde.workspace = true
serde_json.workspace = true
log.workspace = true
@@ -30,6 +33,9 @@ image.workspace = true
[target.'cfg(target_os = "linux")'.dependencies]
zbus = "5"
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
craft-update = { workspace = true }
[dev-dependencies]
egui_kittest = { workspace = true, features = ["wgpu", "snapshot"] }
+2 -1
View File
@@ -62,7 +62,8 @@ fn main() {
READY.store(true, Ordering::Relaxed);
let step = |h: &mut egui_kittest::Harness<SoundApp>| {
let mut raw = std::mem::take(h.input_mut());
h.state_mut().raw_input_hook(&mut raw);
let ctx = h.ctx.clone();
h.state_mut().raw_input_hook(&ctx, &mut raw);
*h.input_mut() = raw;
h.step();
};
+1 -1
View File
@@ -66,7 +66,7 @@ fn slice() -> impl FnMut() -> bool {
pub fn show(app: &mut SoundApp, ctx: &egui::Context) {
let Some(job) = &app.bounce else { return };
let mut cancel = ctx.input(|i| i.key_pressed(egui::Key::Escape));
egui::Modal::new(egui::Id::new("bounce_progress")).show(ctx, |ui| {
egui::Modal::new(egui::Id::unique("bounce_progress")).show(ctx, |ui| {
ui.spacing_mut().item_spacing = egui::vec2(8.0, 8.0);
ui.set_width(320.0);
let name = job.path().file_name().unwrap_or_default().to_string_lossy();
File diff suppressed because it is too large. Load diff
+7 -5
View File
@@ -210,6 +210,7 @@ impl Default for View {
}
}
/// Controls for name style, sort key and sort direction above the contributor table.
fn contributors_toolbar(ui: &mut egui::Ui, v: &mut View) {
ui.horizontal_wrapped(|ui| {
ui.label("Show");
@@ -219,8 +220,8 @@ fn contributors_toolbar(ui: &mut egui::Ui, v: &mut View) {
}
}
ui.separator();
ui.label("Sort");
egui::ComboBox::from_id_salt("credits_sort").selected_text(v.key.label().0).show_ui(ui, |ui| {
let sort = ui.label("Sort");
let combo = egui::ComboBox::from_id_salt("credits_sort").selected_text(v.key.label().0).show_ui(ui, |ui| {
for k in SortKey::ALL {
if ui.selectable_label(v.key == k, k.label().0).clicked() {
v.key = k;
@@ -228,6 +229,7 @@ fn contributors_toolbar(ui: &mut egui::Ui, v: &mut View) {
}
}
});
combo.response.labelled_by(sort.id);
if ui.button(if v.ascending { "⏶" } else { "⏷" }).on_hover_text("Reverse the order").clicked() {
v.ascending = !v.ascending;
}
@@ -252,9 +254,9 @@ fn contributors_grab_bag(ui: &mut egui::Ui, list: &[&Contributor], names: NameMo
});
}
/// About ▸ Contributors: a name toggle, a sort, and the list as a grab bag or a table.
/// About › Contributors: a name toggle, a sort, and the list as a grab bag or a table.
pub fn contributors_ui(ui: &mut egui::Ui) {
let id = egui::Id::new("credits_view");
let id = egui::Id::unique("credits_view");
let mut v = ui.data_mut(|d| d.get_temp::<View>(id)).unwrap_or_default();
contributors_toolbar(ui, &mut v);
let list = sorted(CONTRIBUTORS, v.names, v.key, v.ascending);
@@ -302,7 +304,7 @@ fn table(ui: &mut egui::Ui, list: &[&Contributor], v: &mut View) {
});
}
/// About ▸ Models: AI models credited in Co-Authored-By trailers.
/// About › Models: AI models credited in Co-Authored-By trailers.
pub fn models_ui(ui: &mut egui::Ui) {
if MODELS.is_empty() {
ui.label("No model credits were built into this copy.");
+554 -39
View File
@@ -5,14 +5,16 @@ use crate::theme::{Tokens, bold};
use egui::{Align2, vec2};
use serde_json::{Value, json};
use soundcraft_engine::Engine;
use soundcraft_engine::io::StripNames;
use soundcraft_model::{ClipId, GroupId, Session, SourceId, TrackId};
use soundcraft_time::Samples;
use std::sync::Arc;
#[derive(Debug, Clone)]
pub enum Dialog {
NewTracks { count: u32, format: String, kind: String, timebase: String, name: String },
NewSession { name: String, sample_rate: u32, demo: bool },
Bounce { path: String, format: String, bit_depth: String, normalize: bool },
Bounce { path: String, format: String, bit_depth: String, normalize: bool, dither: bool },
RenameTrack { id: TrackId, name: String },
RenameClip { id: ClipId, name: String },
RenameMarker { id: soundcraft_model::MarkerId, name: String },
@@ -23,13 +25,31 @@ pub enum Dialog {
Spot(crate::spot::Spot),
Fades { shape: String },
StripSilence { threshold: f64, min_ms: f64, pre_ms: f64, post_ms: f64 },
// Strip Silence › Rename: `strip` holds the Strip Silence values to return to.
StripSilenceRename { strip: [f64; 4], names: StripNames },
// Clip List › Auto Rename: the names that editing gives the clips it separates from `clips`.
AutoRename { clips: Vec<ClipId>, names: StripNames },
// `id: None` creates (Track › Group…); `id: Some` modifies that group (Groups list › Modify Group…).
Group { id: Option<GroupId>, name: String, edit: bool, mix: bool, members: Vec<(u64, String, bool)> },
Session { frame_rate: String, bit_depth: String },
// Clip › Loop…: `length` is typed in `format`, measured from `start` (the first selected clip's).
// `filled` is the text the field opened with and the exact length it stands for.
Loop { by: LoopBy, count: u32, length: String, format: soundcraft_time::TimeFormat, start: Samples, filled: (String, Samples) },
ScoreSetup { title: String, composer: String, bars_per_system: u32, show_track_names: bool },
SaveChanges { then: (String, Value) },
}
/// The Clip Looping dialog's choice of how far a clip is looped.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum LoopBy {
/// Number of Loops.
Count,
/// Loop Length.
Length,
/// Loop Until End of Session or Next Clip.
Fill,
}
#[derive(Default)]
pub struct Dialogs {
pub open: Option<Dialog>,
@@ -37,6 +57,41 @@ pub struct Dialogs {
after_save: Option<(String, Value)>,
/// A session the browser read while the open one had changes, waiting for `SaveChanges`.
pending_session: Option<(String, Vec<u8>)>,
/// The naming scheme Strip Silence gives the clips it leaves, kept from one use to the next.
pub strip_names: StripNames,
/// The Strip Silence rectangles the Edit window draws while Strip Silence is open.
strip_rectangles: StripRectangles,
}
/// What Strip keeps on each track: the Strip Silence rectangles.
pub type StripRects = Arc<Vec<(TrackId, Vec<soundcraft_time::Range>)>>;
/// The session revision and the Strip Silence values (as bits) that rectangles were found for.
type StripKey = (u64, [u64; 4]);
/// The Strip Silence rectangles last found, and the scan running for newer values.
#[derive(Default)]
struct StripRectangles {
shown: Option<(StripKey, StripRects)>,
#[cfg(not(target_arch = "wasm32"))]
running: Option<StripScanning>,
}
/// A Strip Silence scan on a worker thread: what it was started for, where its result arrives, and
/// the flag that stops it.
#[cfg(not(target_arch = "wasm32"))]
struct StripScanning {
key: StripKey,
rx: std::sync::mpsc::Receiver<StripRects>,
stop: Arc<std::sync::atomic::AtomicBool>,
}
/// A scan whose rectangles nobody will draw stops before its next window of audio.
#[cfg(not(target_arch = "wasm32"))]
impl Drop for StripScanning {
fn drop(&mut self) {
self.stop.store(true, std::sync::atomic::Ordering::Relaxed);
}
}
impl Dialogs {
@@ -55,14 +110,26 @@ impl Dialogs {
Dialog::Spot(_) => "spot",
Dialog::Fades { .. } => "fades",
Dialog::StripSilence { .. } => "strip_silence",
Dialog::StripSilenceRename { .. } => "strip_silence_rename",
Dialog::AutoRename { .. } => "auto_rename",
Dialog::Group { id: None, .. } => "group",
Dialog::Group { id: Some(_), .. } => "modify_group",
Dialog::Session { .. } => "session",
Dialog::Loop { .. } => "clip_looping",
Dialog::ScoreSetup { .. } => "score_setup",
Dialog::SaveChanges { .. } => "save_changes",
})
}
/// 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;
}
false
}
pub fn open_rename_track(&mut self, id: TrackId, name: &str) {
self.open = Some(Dialog::RenameTrack { id, name: name.to_string() });
}
@@ -92,6 +159,16 @@ impl Dialogs {
true
}
/// 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 });
true
}
/// Open the dialog for a command; false when the command has no dialog.
pub fn open_for_command(&mut self, e: &Engine, id: &str) -> bool {
self.after_save = None;
@@ -113,7 +190,13 @@ impl Dialogs {
.and_then(|p| std::path::Path::new(p).parent())
.filter(|d| !d.as_os_str().is_empty())
.map_or(home.clone(), |d| d.display().to_string());
Dialog::Bounce { path: format!("{dir}/{name} Bounce.wav"), format: "wav".into(), bit_depth: "24".into(), normalize: false }
Dialog::Bounce {
path: format!("{dir}/{name} Bounce.wav"),
format: "wav".into(),
bit_depth: "24".into(),
normalize: false,
dither: false,
}
}
"file.import_audio" => Dialog::PathPrompt { cmd: id.into(), title: "Import Audio".into(), path: home, key: "path".into() },
"file.import_midi" => Dialog::PathPrompt { cmd: id.into(), title: "Import MIDI".into(), path: home, key: "path".into() },
@@ -152,11 +235,21 @@ impl Dialogs {
let s = soundcraft_engine::score::ScoreSetup::from_session(e.session());
Dialog::ScoreSetup { title: s.title, composer: s.composer, bars_per_system: s.bars_per_system, show_track_names: s.show_track_names }
}
"clip.auto_rename" => return self.open_auto_rename(e, e.session().edit.selected_clips.clone()),
"clip.rename" => {
let target = soundcraft_engine::cmd::clip_ids_param(e, &json!({})).first().and_then(|id| e.session().find_clip(*id));
let Some((_, clip)) = target else { return false };
Dialog::RenameClip { id: clip.id, name: clip.name.clone() }
}
"clip.loop" => {
let target = soundcraft_engine::cmd::clip_ids_param(e, &json!({})).first().and_then(|id| e.session().find_clip(*id));
let Some((_, clip)) = target else { return false };
let s = e.session();
let format = s.edit.main_counter;
let exact = clip.length.saturating_mul(4);
let length = soundcraft_time::format_length(clip.start, exact, format, s.sample_rate, &s.tempo, s.frame_rate);
Dialog::Loop { by: LoopBy::Count, count: 4, length: length.clone(), format, start: clip.start, filled: (length, exact) }
}
_ => match path_dialog(id, &home, &name) {
Some(d) => d,
None => return false,
@@ -207,6 +300,8 @@ pub fn open_session_bytes(app: &mut SoundApp, name: String, bytes: Vec<u8>) {
}
}
/// Open the session bytes read by the browser after the save prompt, stopping transport first. A successful open
/// marks the document replaced so pending captures cancel and reports any missing media.
fn open_pending_session(app: &mut SoundApp) {
let Some((name, bytes)) = app.dialogs.pending_session.take() else { return };
if crate::bounce::refuses_session_switch(app) {
@@ -214,8 +309,11 @@ fn open_pending_session(app: &mut SoundApp) {
}
let _ = app.run("transport.stop", json!({}));
app.ui.status = match soundcraft_engine::io::open_session_bytes(&mut app.engine, &name, &bytes) {
Ok(missing) if missing.is_empty() => format!("Opened {name}"),
Ok(missing) => format!("Opened {name} ({} missing media)", missing.len()),
Ok(missing) => {
// This path replaces the document without going through `SoundApp::run`.
app.document_replaced();
if missing.is_empty() { format!("Opened {name}") } else { format!("Opened {name} ({} missing media)", missing.len()) }
}
Err(e) => e.to_string(),
};
}
@@ -307,11 +405,16 @@ fn default_dir() -> String {
pub fn show(app: &mut SoundApp, ctx: &egui::Context) {
audiosuite_window(app, ctx);
crate::bounce::show(app, ctx);
crate::import::show(app, ctx);
let Some(mut d) = app.dialogs.open.take() else { return };
let mut keep = true;
let mut action: Option<(String, Value)> = None;
let mut save_first: Option<(String, Value)> = None;
let confirmed = matches!(d, Dialog::SaveChanges { .. });
// Strip Silence's Rename button, and the naming scheme its dialog's OK keeps.
let mut rename_from: Option<[f64; 4]> = None;
let mut renamed: Option<StripNames> = None;
let strip_names = app.dialogs.strip_names.clone();
let title = match &d {
Dialog::NewTracks { .. } => "New Tracks",
Dialog::NewSession { .. } => "New Session",
@@ -325,9 +428,11 @@ pub fn show(app: &mut SoundApp, ctx: &egui::Context) {
Dialog::Spot(_) => "Spot",
Dialog::Fades { .. } => "Fades",
Dialog::StripSilence { .. } => "Strip Silence",
Dialog::StripSilenceRename { .. } | Dialog::AutoRename { .. } => "Rename Selected Clips",
Dialog::Group { id: None, .. } => "Create Group",
Dialog::Group { id: Some(_), .. } => "Modify Group",
Dialog::Session { .. } => "Session Setup",
Dialog::Loop { .. } => "Clip Looping",
Dialog::ScoreSetup { .. } => "Score Setup",
Dialog::SaveChanges { then } if then.0 == "session.revert" => "Revert to Saved",
Dialog::SaveChanges { .. } => "Unsaved Changes",
@@ -344,15 +449,16 @@ pub fn show(app: &mut SoundApp, ctx: &egui::Context) {
match &mut d {
Dialog::NewTracks { count, format, kind, timebase, name } => {
ui.horizontal(|ui| {
ui.label("Create");
ui.add(egui::DragValue::new(count).range(1..=128));
let create = ui.label("Create");
ui.add(egui::DragValue::new(count).range(1..=128)).labelled_by(create.id);
ui.label("new");
egui::ComboBox::from_id_salt("nt_fmt").selected_text(format.as_str()).show_ui(ui, |ui| {
let fmt = egui::ComboBox::from_id_salt("nt_fmt").selected_text(format.as_str()).show_ui(ui, |ui| {
for f in soundcraft_model::ChannelFormat::ALL {
ui.selectable_value(format, f.label().to_string(), f.label());
}
});
egui::ComboBox::from_id_salt("nt_kind")
fmt.response.accessible_name("Channel format");
let kind_combo = egui::ComboBox::from_id_salt("nt_kind")
.selected_text(soundcraft_model::TrackKind::from_id(kind).map_or("Audio Track", |k| k.label()))
.show_ui(ui, |ui| {
for k in soundcraft_model::TrackKind::ALL {
@@ -362,16 +468,20 @@ pub fn show(app: &mut SoundApp, ctx: &egui::Context) {
}
}
});
kind_combo.response.accessible_name("Track kind");
ui.label("in");
egui::ComboBox::from_id_salt("nt_tb").selected_text(if timebase == "ticks" { "Ticks" } else { "Samples" }).show_ui(
let tb = egui::ComboBox::from_id_salt("nt_tb").selected_text(if timebase == "ticks" { "Ticks" } else { "Samples" }).show_ui(
ui,
|ui| {
ui.selectable_value(timebase, "samples".to_string(), "Samples");
ui.selectable_value(timebase, "ticks".to_string(), "Ticks");
},
);
tb.response.accessible_name("Timebase");
});
ui.horizontal(|ui| {
ui.label("Name:");
ui.add(egui::TextEdit::singleline(name).desired_width(140.0));
crate::widgets::named_text_field(ui.add(egui::TextEdit::singleline(name).desired_width(240.0)), "Name");
});
if buttons(ui, "Create", enter) {
action =
@@ -381,7 +491,7 @@ pub fn show(app: &mut SoundApp, ctx: &egui::Context) {
Dialog::NewSession { name, sample_rate, demo } => {
ui.horizontal(|ui| {
ui.label("Name");
ui.text_edit_singleline(name);
crate::widgets::named_text_field(ui.text_edit_singleline(name), "Name");
});
egui::ComboBox::from_label("Sample Rate").selected_text(format!("{sample_rate} Hz")).show_ui(ui, |ui| {
for r in soundcraft_time::SampleRate::COMMON {
@@ -396,10 +506,10 @@ pub fn show(app: &mut SoundApp, ctx: &egui::Context) {
));
}
}
Dialog::Bounce { path, format, bit_depth, normalize } => {
Dialog::Bounce { path, format, bit_depth, normalize, dither } => {
ui.horizontal(|ui| {
ui.label("File");
ui.add(egui::TextEdit::singleline(path).desired_width(420.0));
crate::widgets::named_text_field(ui.add(egui::TextEdit::singleline(path).desired_width(420.0)), "File");
if let Some(pick) = &app.services.pick_save
&& ui.button("Browse…").clicked()
&& let Some(p) = pick("file.bounce_mix", path)
@@ -426,6 +536,11 @@ pub fn show(app: &mut SoundApp, ctx: &egui::Context) {
});
});
crate::widgets::checkbox(ui, normalize, "Normalize to -0.1 dBFS");
// 32-bit float keeps every sample as it is: there is nothing to dither.
ui.add_enabled_ui(bit_depth.as_str() != "32f", |ui| {
crate::widgets::checkbox(ui, dither, "Dither (TPDF)")
.on_hover_text("Adds ±1 LSB triangular dither when writing 16- or 24-bit files. Leave it off when the master chain already dithers.");
});
ui.label(
egui::RichText::new("Bounces the edit selection, or the whole session when nothing is selected.")
.small()
@@ -435,7 +550,7 @@ pub fn show(app: &mut SoundApp, ctx: &egui::Context) {
let p = std::path::Path::new(path.as_str()).with_extension(if format == "aiff" { "aif" } else { format.as_str() });
crate::bounce::start(
app,
&json!({"path": p.to_string_lossy(), "format": format, "bit_depth": bit_depth, "normalize": normalize}),
&json!({"path": p.to_string_lossy(), "format": format, "bit_depth": bit_depth, "normalize": normalize, "dither": *dither && bit_depth.as_str() != "32f"}),
);
keep = false;
}
@@ -466,7 +581,7 @@ pub fn show(app: &mut SoundApp, ctx: &egui::Context) {
}
}
Dialog::PathPrompt { cmd, path, key, .. } => {
ui.add(egui::TextEdit::singleline(path).desired_width(420.0));
crate::widgets::named_text_field(ui.add(egui::TextEdit::singleline(path).desired_width(420.0)), "Path");
if let Some(pick) = &app.services.pick_open
&& cmd != "session.save_as"
&& cmd != "session.save_copy"
@@ -506,7 +621,7 @@ pub fn show(app: &mut SoundApp, ctx: &egui::Context) {
}
Dialog::Number { cmd, key, value, suffix, .. } => {
ui.horizontal(|ui| {
ui.add(egui::DragValue::new(value).speed(0.1));
ui.add(egui::DragValue::new(value).speed(0.1)).accessible_name(format!("Value in {suffix}"));
ui.label(suffix.as_str());
});
if buttons(ui, "OK", enter) {
@@ -517,7 +632,7 @@ pub fn show(app: &mut SoundApp, ctx: &egui::Context) {
Dialog::TempoChange { at, bpm } => {
ui.horizontal(|ui| {
// Same limits the engine enforces (time crate `valid_bpm`).
ui.add(egui::DragValue::new(bpm).speed(0.1).range(5.0..=1000.0));
ui.add(egui::DragValue::new(bpm).speed(0.1).range(5.0..=1000.0)).accessible_name("Tempo (BPM)");
ui.label("bpm");
});
if buttons(ui, "OK", enter) {
@@ -540,21 +655,67 @@ pub fn show(app: &mut SoundApp, ctx: &egui::Context) {
}
}
Dialog::StripSilence { threshold, min_ms, pre_ms, post_ms } => {
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..=2000.0).text("Min strip duration ms"));
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"));
if buttons(ui, "Strip", enter) {
action = Some((
"edit.strip_silence".into(),
json!({"threshold_db": threshold, "min_length_ms": min_ms, "pad_before_ms": pre_ms, "pad_after_ms": post_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 {
ui.ctx().request_repaint();
}
if !strip_names.name.trim().is_empty() {
let names = format!("New clips are named {}, {} and so on.", strip_names.clip_name(0), strip_names.clip_name(1));
ui.label(egui::RichText::new(names).small().color(Tokens::current().text_dim));
}
ui.separator();
ui.horizontal(|ui| {
let mut mode = None;
let mut rename = false;
if ui.button("Extract").clicked() {
mode = Some("extract");
}
if ui.button("Rename…").clicked() {
rename = true;
}
if ui.button("Separate").clicked() {
mode = Some("separate");
}
// Enter on a focused Extract or Separate clicks that button, so Enter presses Strip only when neither was clicked.
if buttons(ui, "Strip", enter) && mode.is_none() {
mode = Some("strip");
}
// Enter on a focused Rename… also presses Strip, so Rename… goes first.
if rename {
rename_from = Some(values);
} else if let Some(mode) = mode {
action = Some(strip_silence_action(mode, values, &strip_names));
}
});
}
Dialog::StripSilenceRename { names, .. } => {
strip_name_fields(ui, names, STRIP_NAME_LABELS);
let cleared = ui.button("Clear").clicked();
if cleared {
*names = StripNames::default();
}
// Enter on a focused Clear clicks it, and leaves the dialog open.
if buttons(ui, "OK", enter) && !cleared {
renamed = Some(names.clone());
}
}
Dialog::AutoRename { clips, names } => {
ui.label("Text for naming the clips that editing makes from the selected clips:");
strip_name_fields(ui, names, AUTO_NAME_LABELS);
if buttons(ui, "OK", enter) {
action = Some(auto_rename_action(clips, names));
}
}
Dialog::Group { id, name, edit, mix, members } => {
ui.horizontal(|ui| {
ui.label("Name");
ui.text_edit_singleline(name);
crate::widgets::named_text_field(ui.text_edit_singleline(name), "Name");
});
ui.horizontal(|ui| {
crate::widgets::checkbox(ui, edit, "Edit group");
@@ -595,16 +756,34 @@ pub fn show(app: &mut SoundApp, ctx: &egui::Context) {
action = Some(("setup.session".into(), json!({"frame_rate": frame_rate, "bit_depth": bit_depth})));
}
}
Dialog::Loop { by, count, length, format, start, filled } => {
let s = app.engine.session();
ui.horizontal(|ui| {
let count_label = ui.radio_value(by, LoopBy::Count, "Number of Loops");
ui.add_enabled(*by == LoopBy::Count, egui::DragValue::new(count).range(1..=512)).labelled_by(count_label.id);
});
ui.horizontal(|ui| {
let length_label = ui.radio_value(by, LoopBy::Length, "Loop Length");
ui.add_enabled(*by == LoopBy::Length, egui::TextEdit::singleline(length).desired_width(140.0)).labelled_by(length_label.id);
ui.label(format.label());
});
ui.radio_value(by, LoopBy::Fill, "Loop Until End of Session or Next Clip");
let params = loop_params(s, *by, *count, length, *format, *start, filled);
if buttons_enabled(ui, "OK", enter, params.is_some()) {
action = params.map(|p| ("clip.loop".into(), p));
}
}
Dialog::ScoreSetup { title, composer, bars_per_system, show_track_names } => {
egui::Grid::new("score_setup").num_columns(2).spacing(vec2(10.0, 8.0)).show(ui, |ui| {
ui.label("Title");
ui.text_edit_singleline(title);
// The painted label is not an accesskit name; name the field itself.
crate::widgets::named_text_field(ui.text_edit_singleline(title), "Score title");
ui.end_row();
ui.label("Composer");
ui.text_edit_singleline(composer);
crate::widgets::named_text_field(ui.text_edit_singleline(composer), "Score composer");
ui.end_row();
ui.label("Bars per system");
ui.add(egui::DragValue::new(bars_per_system).range(1..=16));
ui.add(egui::DragValue::new(bars_per_system).range(1..=16)).accessible_name("Bars per system");
ui.end_row();
});
crate::widgets::checkbox(ui, show_track_names, "Show track names");
@@ -643,11 +822,35 @@ pub fn show(app: &mut SoundApp, ctx: &egui::Context) {
}
}
});
if esc || ctx.memory(|m| m.data.get_temp::<bool>(egui::Id::new("dlg_cancel")).unwrap_or(false)) {
if esc || ctx.memory(|m| m.data.get_temp::<bool>(egui::Id::unique("dlg_cancel")).unwrap_or(false)) {
keep = false;
app.dialogs.after_save = None;
app.dialogs.pending_session = None;
ctx.memory_mut(|m| m.data.remove::<bool>(egui::Id::new("dlg_cancel")));
ctx.memory_mut(|m| m.data.remove::<bool>(egui::Id::unique("dlg_cancel")));
}
// Enter on a focused Cancel also presses OK. In Auto Rename, the dialog then closes without running the command.
if !keep && matches!(d, Dialog::AutoRename { .. }) {
action = None;
}
if let Some(strip) = rename_from
&& keep
{
d = Dialog::StripSilenceRename { strip, names: strip_names };
}
// OK, Cancel and Escape in Rename Selected Clips go back to Strip Silence; OK keeps the names.
// Enter on a focused Cancel also presses OK, and `keep` is false then, so Cancel wins.
let back = match &d {
Dialog::StripSilenceRename { strip, .. } if !keep || renamed.is_some() => Some(*strip),
_ => None,
};
if let Some([threshold, min_ms, pre_ms, post_ms]) = back {
if let Some(names) = renamed
&& keep
{
app.dialogs.strip_names = names;
}
d = Dialog::StripSilence { threshold, min_ms, pre_ms, post_ms };
keep = true;
}
if let Some(then) = save_first {
keep = false;
@@ -666,12 +869,17 @@ pub fn show(app: &mut SoundApp, ctx: &egui::Context) {
let saving = action.as_ref().is_some_and(|(cmd, _)| cmd.starts_with("session.save"));
if let Some((cmd, params)) = action {
keep = false;
match run_then(app, &cmd, params) {
Ok(v) if cmd == "file.export_clips" => {
app.ui.status = format!("Exported {} clip(s) to {}", v["written"], v["dir"].as_str().unwrap_or_default());
if cmd == "file.import_audio" {
// Decoded in the background so a long file doesn't freeze the app (#597).
crate::import::start(app, params);
} else {
match run_then(app, &cmd, params) {
Ok(v) if cmd == "file.export_clips" => {
app.ui.status = format!("Exported {} clip(s) to {}", v["written"], v["dir"].as_str().unwrap_or_default());
}
Ok(_) => {}
Err(e) => app.ui.status = e,
}
Ok(_) => {}
Err(e) => app.ui.status = e,
}
}
if saving
@@ -695,6 +903,137 @@ fn run_then(app: &mut SoundApp, cmd: &str, params: Value) -> Result<Value, Strin
app.run(cmd, params)
}
/// The ranges Strip keeps on each selected track, found by the scan of `edit.strip_silence_preview`
/// with the values of the open Strip Silence window (or of the one Rename Selected Clips goes back
/// to). The Edit window draws them as the Strip Silence rectangles. Empty when neither window is
/// open. A scan runs again only when the session or a value changes. Off the web it runs on a worker
/// thread, one at a time, and the rectangles last found stay until it ends. On the web it runs on the
/// frame thread once no pointer button is down, so a slider drag scans once, on release. A scan for
/// older values, or one that runs on after the window closes, stops before its next window of audio.
pub fn strip_silence_rectangles(app: &mut SoundApp, ctx: &egui::Context) -> StripRects {
let values = match &app.dialogs.open {
Some(Dialog::StripSilence { threshold, min_ms, pre_ms, post_ms }) => [*threshold, *min_ms, *pre_ms, *post_ms],
Some(Dialog::StripSilenceRename { strip, .. }) => *strip,
_ => {
app.dialogs.strip_rectangles = StripRectangles::default();
return Arc::default();
}
};
let key: StripKey = (app.engine.revision, values.map(f64::to_bits));
let r = &mut app.dialogs.strip_rectangles;
#[cfg(not(target_arch = "wasm32"))]
if let Some(job) = &r.running {
match job.rx.try_recv() {
Ok(rects) => {
r.shown = Some((job.key, rects));
r.running = None;
}
Err(std::sync::mpsc::TryRecvError::Disconnected) => r.running = None,
// Values changed since it started: stop it, and the next scan starts when it has ended.
Err(std::sync::mpsc::TryRecvError::Empty) if job.key != key => job.stop.store(true, std::sync::atomic::Ordering::Relaxed),
Err(std::sync::mpsc::TryRecvError::Empty) => {}
}
}
let shown = r.shown.as_ref().map(|(k, rects)| (*k == key, Arc::clone(rects)));
#[cfg(not(target_arch = "wasm32"))]
let busy = r.running.is_some();
// The browser's scan blocks the frame, so it waits for the release that ends a slider drag.
#[cfg(target_arch = "wasm32")]
let busy = ctx.input(|i| i.pointer.any_down());
if let Some((true, rects)) = shown {
return rects;
}
if busy {
return shown.map(|(_, rects)| rects).unwrap_or_default();
}
let [threshold, min_ms, pre_ms, post_ms] = values;
let params = json!({"threshold_db": threshold, "min_length_ms": min_ms, "pad_before_ms": pre_ms, "pad_after_ms": post_ms});
let Ok(scan) = soundcraft_engine::cmd::strip_silence_scan(&app.engine, &params) else {
app.dialogs.strip_rectangles.shown = Some((key, Arc::default()));
return Arc::default();
};
let scan = Arc::new(scan);
#[cfg(not(target_arch = "wasm32"))]
{
let (tx, rx) = std::sync::mpsc::channel();
let (ctx, job) = (ctx.clone(), Arc::clone(&scan));
let work = move || {
let found = job.run();
// A stopped scan sends nothing: its sender drops, and the rectangles last found stay.
if !job.stop.load(std::sync::atomic::Ordering::Relaxed) {
let _ = tx.send(Arc::new(found.unwrap_or_default()));
}
ctx.request_repaint();
};
if std::thread::Builder::new().name("strip-silence-scan".into()).spawn(work).is_ok() {
app.dialogs.strip_rectangles.running = Some(StripScanning { key, rx, stop: Arc::clone(&scan.stop) });
return shown.map(|(_, rects)| rects).unwrap_or_default();
}
}
// The browser has no threads, and without a thread to spare the scan runs on this one.
let rects: StripRects = Arc::new(scan.run().unwrap_or_default());
app.dialogs.strip_rectangles.shown = Some((key, Arc::clone(&rects)));
rects
}
/// The command a Strip Silence button runs: `mode` is strip, extract or separate, and `values` holds
/// the threshold in dB, the minimum strip duration and the clip start and end pads in milliseconds.
/// `names` names the clips it leaves when its name is not blank.
fn strip_silence_action(mode: &str, values: [f64; 4], names: &StripNames) -> (String, Value) {
let [threshold, min_ms, pre_ms, post_ms] = values;
let mut params = json!({"threshold_db": threshold, "min_length_ms": min_ms, "pad_before_ms": pre_ms, "pad_after_ms": post_ms, "mode": mode});
if !names.name.trim().is_empty() {
params["name"] = json!(names.name);
params["number"] = json!(names.number);
params["zeros"] = json!(names.zeros);
params["suffix"] = json!(names.suffix);
}
("edit.strip_silence".into(), params)
}
/// The command Auto Rename's OK runs: the scheme for `clips`, or no scheme when the name is blank.
fn auto_rename_action(clips: &[ClipId], names: &StripNames) -> (String, Value) {
let mut params = json!({"clips": clips});
if !names.name.trim().is_empty() {
params["name"] = json!(names.name);
params["number"] = json!(names.number);
params["zeros"] = json!(names.zeros);
params["suffix"] = json!(names.suffix);
}
("clip.auto_rename".into(), params)
}
/// The labels of the number and zeros fields in Strip Silence's Rename Selected Clips.
const STRIP_NAME_LABELS: [&str; 2] = ["Number", "Zeros"];
/// The labels of the number and zeros fields in Auto Rename's Rename Selected Clips.
const AUTO_NAME_LABELS: [&str; 2] = ["Starting Number", "Number of Places"];
/// The Name, number, zeros and Suffix fields of Rename Selected Clips, with the first name they
/// give. `labels` names the number and zeros fields.
fn strip_name_fields(ui: &mut egui::Ui, names: &mut StripNames, labels: [&str; 2]) {
let [number, zeros] = labels;
egui::Grid::new("strip_names").num_columns(2).spacing(vec2(10.0, 8.0)).show(ui, |ui| {
let name = ui.label("Name");
ui.text_edit_singleline(&mut names.name).labelled_by(name.id);
ui.end_row();
let number = ui.label(number);
ui.add(egui::DragValue::new(&mut names.number).range(0..=StripNames::MAX_NUMBER)).labelled_by(number.id);
ui.end_row();
let zeros = ui.label(zeros);
ui.add(egui::DragValue::new(&mut names.zeros).range(0..=StripNames::MAX_ZEROS)).labelled_by(zeros.id);
ui.end_row();
let suffix = ui.label("Suffix");
ui.text_edit_singleline(&mut names.suffix).labelled_by(suffix.id);
ui.end_row();
});
let example = if names.name.trim().is_empty() {
"Without a name, the clips keep their names.".to_string()
} else {
format!("First clip: {}", names.clip_name(0))
};
ui.label(egui::RichText::new(example).small().color(Tokens::current().text_dim));
}
/// Cancel/OK row; returns true when OK was pressed (or Enter).
fn buttons(ui: &mut egui::Ui, ok: &str, enter: bool) -> bool {
buttons_enabled(ui, ok, enter, true)
@@ -706,7 +1045,7 @@ fn buttons_enabled(ui: &mut egui::Ui, ok: &str, enter: bool, ready: bool) -> boo
ui.separator();
ui.horizontal(|ui| {
if ui.button("Cancel").clicked() {
ui.ctx().memory_mut(|m| m.data.insert_temp(egui::Id::new("dlg_cancel"), true));
ui.ctx().memory_mut(|m| m.data.insert_temp(egui::Id::unique("dlg_cancel"), true));
}
let b = egui::Button::new(egui::RichText::new(ok).strong()).fill(Tokens::current().accent);
if ui.add_enabled(ready, b).clicked() {
@@ -719,6 +1058,7 @@ fn buttons_enabled(ui: &mut egui::Ui, ok: &str, enter: bool, ready: bool) -> boo
/// A name field that takes the focus with its text selected, so typing replaces the old name.
fn name_field(ui: &mut egui::Ui, name: &mut String) {
let mut out = egui::TextEdit::singleline(name).show(ui);
let _ = crate::widgets::named_text_field(out.response.response.clone(), "Name");
if !out.response.has_focus() {
out.response.request_focus();
let all = egui::text::CCursorRange::two(egui::text::CCursor::new(0), egui::text::CCursor::new(name.chars().count()));
@@ -732,12 +1072,20 @@ fn tempo_change_action(at: Samples, bpm: f64) -> (String, Value) {
("event.tempo".into(), json!({"bpm": bpm, "at": at}))
}
/// The speed ramp's choices in the AudioSuite window: param id, label, default, and (value, name) pairs.
const SPEED_RAMP_CHOICES: [(&str, &str, f32, &[(f32, &str)]); 3] = [
("change", "Change", 0.0, &[(0.0, "Slow Down"), (1.0, "Speed Up")]),
("selection", "Selection", 0.0, &[(0.0, "Fit To"), (1.0, "Extend")]),
("fades", "Fades", 1.0, &[(1.0, "On"), (0.0, "Off")]),
];
/// Process-parameter window for the chosen AudioSuite process, with its controls and Render action.
fn audiosuite_window(app: &mut SoundApp, ctx: &egui::Context) {
let Some(process) = app.ui.audiosuite.clone() else { return };
let mut open = true;
let title = soundcraft_dsp::plugin_info(&process).map_or_else(|| process.replace('_', " "), |p| p.name.to_string());
egui::Window::new(format!("AudioSuite · {title}")).open(&mut open).default_width(360.0).show(ctx, |ui| {
let key = egui::Id::new(("as_params", process.clone()));
let key = egui::Id::unique(("as_params", process.clone()));
let mut params: std::collections::BTreeMap<String, f32> = ui.ctx().memory(|m| m.data.get_temp(key)).unwrap_or_default();
if let Some(info) = soundcraft_dsp::plugin_info(&process) {
for p in info.params {
@@ -758,9 +1106,16 @@ fn audiosuite_window(app: &mut SoundApp, ctx: &egui::Context) {
let v = params.entry("semitones".into()).or_insert(0.0);
ui.add(egui::Slider::new(v, -24.0..=24.0).text("Semitones"));
}
"varispeed" => {
let v = params.entry("speed".into()).or_insert(1.0);
ui.add(egui::Slider::new(v, 0.25..=4.0).text("Speed"));
"speed_ramp" => {
for (id, label, default, choices) in SPEED_RAMP_CHOICES {
let v = params.entry(id.to_string()).or_insert(default);
ui.horizontal(|ui| {
ui.label(label);
for &(value, name) in choices {
ui.radio_value(v, value, name);
}
});
}
}
_ => {
ui.label("No parameters.");
@@ -784,6 +1139,42 @@ fn audiosuite_window(app: &mut SoundApp, ctx: &egui::Context) {
}
}
/// The `clip.loop` params the Clip Looping dialog's OK sends; None while the typed length does not
/// read as a duration longer than zero. An untouched length field sends the exact length it was
/// filled with, which its text can round.
fn loop_params(
s: &Session,
by: LoopBy,
count: u32,
length: &str,
format: soundcraft_time::TimeFormat,
start: Samples,
filled: &(String, Samples),
) -> Option<Value> {
match by {
LoopBy::Count => Some(json!({"count": count})),
LoopBy::Length if length == filled.0 => Some(json!({"length": filled.1})),
LoopBy::Length => loop_length(s, length, format, start).filter(|l| *l > 0).map(|l| json!({"length": l})),
LoopBy::Fill => Some(json!({"fill": true})),
}
}
/// A duration typed in `format`, the way `format_length` writes it, for a span that starts at `start`.
fn loop_length(s: &Session, text: &str, format: soundcraft_time::TimeFormat, start: Samples) -> Option<Samples> {
if format != soundcraft_time::TimeFormat::BarsBeats {
return soundcraft_time::parse_position(text, format, s.sample_rate, &s.tempo, s.frame_rate, 0, 0).ok();
}
let parts: Vec<i64> = text.split(['|', '.', ' ']).filter(|p| !p.is_empty()).map(str::parse::<i64>).collect::<Result<_, _>>().ok()?;
if parts.is_empty() || parts.len() > 3 || parts.iter().any(|p| *p < 0) {
return None;
}
let from = s.tempo.samples_to_ticks(start, s.sample_rate);
let meter = s.tempo.meter_at_tick(from);
let part = |i: usize, unit: i64| parts.get(i).copied().unwrap_or(0).checked_mul(unit);
let ticks = part(0, meter.ticks_per_bar())?.checked_add(part(1, meter.ticks_per_beat())?)?.checked_add(part(2, 1)?)?;
Some(s.tempo.tick_to_samples(from.checked_add(ticks)?, s.sample_rate).saturating_sub(start))
}
#[cfg(test)]
mod tests {
use super::*;
@@ -823,6 +1214,30 @@ mod tests {
assert!(matches!(d.open, Some(Dialog::RenameClip { id, ref name }) if id == clip.id && *name == clip.name));
}
#[test]
fn speed_ramp_menu_opens_the_window_and_its_choices_reach_the_engine() {
let mut app = SoundApp::new(Engine::default(), None, crate::Services::default());
crate::menus::invoke_menu(&mut app, "audiosuite.process", "AudioSuite > Pitch Shift > Vari-Fi");
assert_eq!(app.ui.audiosuite.as_deref(), Some("speed_ramp"));
// Renders the Kick clip with `params` and returns its new length and samples.
let render = |params: Value| {
let mut e = soundcraft_engine::demo::demo_engine();
let clip = e.session().track_by_name("Kick").unwrap().clips()[0].id;
e.execute("audiosuite.process", &json!({"process": "speed_ramp", "params": params, "clips": [clip.0]})).unwrap();
let c = e.session().track_by_name("Kick").unwrap().clips()[0].clone();
(c.length, e.session().pool.get(c.source().unwrap()).unwrap().buffer.channels.clone())
};
let defaults: serde_json::Map<String, Value> = SPEED_RAMP_CHOICES.iter().map(|(id, _, d, _)| (id.to_string(), json!(d))).collect();
let (length, audio) = render(Value::Object(defaults));
assert!(render(json!({})) == (length, audio.clone()), "the window's defaults are the engine's");
let (extended, _) = render(json!({"selection": 1.0}));
assert_eq!(extended, (length as f64 * 1.5).round() as i64, "Extend makes the clip 150 % long");
let (_, fast) = render(json!({"change": 1.0}));
assert_ne!(fast, audio, "Speed Up renders a different ramp");
let (_, unfaded) = render(json!({"fades": 0.0}));
assert_ne!(unfaded, audio, "Fades Off renders without the fade");
}
#[test]
fn tempo_change_dialog_runs_a_real_command_at_its_position() {
let (cmd, params) = tempo_change_action(96_000, 133.5);
@@ -831,6 +1246,55 @@ mod tests {
assert_eq!(params["bpm"], 133.5);
}
#[test]
fn strip_silence_buttons_send_strip_extract_and_separate() {
let mut e = soundcraft_engine::demo::demo_engine();
let kick = e.session().track_by_name("Kick").unwrap().id;
let end = e.session().content_end();
e.execute("edit.select", &json!({"tracks": [kick.0], "start": 0, "end": end})).unwrap();
let mut d = Dialogs::default();
assert!(d.open_for_command(&e, "edit.strip_silence"));
let Some(Dialog::StripSilence { threshold, min_ms, pre_ms, post_ms }) = d.open.take() else {
panic!("Strip Silence did not open its dialog")
};
let values = [threshold, min_ms, pre_ms, post_ms];
let clips = |e: &Engine| e.session().track(kick).unwrap().clips().iter().map(|c| (c.start, c.end())).collect::<Vec<_>>();
let covered = |c: &[(i64, i64)]| c.iter().map(|(a, b)| b - a).sum::<i64>();
let before = clips(&e);
let mut run = |mode: &str| {
let (cmd, params) = strip_silence_action(mode, values, &StripNames::default());
assert_eq!(params["mode"], mode);
e.execute(&cmd, &params).unwrap();
let after = clips(&e);
e.execute("edit.undo", &json!({})).unwrap();
after
};
let (strip, extract, separate) = (run("strip"), run("extract"), run("separate"));
assert!(separate.len() > before.len(), "Separate cuts the clips");
assert_eq!(covered(&separate), covered(&before), "Separate keeps all the audio");
assert!(!strip.is_empty() && !extract.is_empty());
assert_eq!(covered(&strip) + covered(&extract), covered(&before), "Strip and Extract keep opposite parts");
}
#[test]
fn strip_silence_sends_the_naming_scheme_only_with_a_name() {
let values = [-48.0, 50.0, 5.0, 20.0];
let (_, plain) = strip_silence_action("strip", values, &StripNames::default());
assert!(plain.get("name").is_none() && plain.get("suffix").is_none());
let names = StripNames { name: "SFX".into(), number: 23, zeros: 1, suffix: ".Reel1".into() };
let (cmd, params) = strip_silence_action("separate", values, &names);
assert_eq!((params["name"].clone(), params["number"].clone(), params["zeros"].clone()), (json!("SFX"), json!(23), json!(1)));
assert_eq!(params["suffix"], ".Reel1");
let mut e = soundcraft_engine::demo::demo_engine();
let kick = e.session().track_by_name("Kick").unwrap().id;
let end = e.session().content_end();
e.execute("edit.select", &json!({"tracks": [kick.0], "start": 0, "end": end})).unwrap();
e.execute(&cmd, &params).unwrap();
let named: Vec<String> = e.session().track(kick).unwrap().clips().iter().map(|c| c.name.clone()).collect();
assert!(named.len() > 1);
assert_eq!(named[..2], ["SFX023.Reel1", "SFX024.Reel1"]);
}
#[test]
fn clear_always_asks_and_names_the_timeline_clips_that_go_too() {
let mut e = soundcraft_engine::demo::demo_engine();
@@ -862,4 +1326,55 @@ mod tests {
"Remove 2 files from the session? They are used by 4 clips on the timeline, which are removed too. The files stay on disk."
);
}
#[test]
fn clip_loop_opens_clip_looping_for_the_selected_clip() {
let mut e = soundcraft_engine::demo::demo_engine();
let mut d = Dialogs::default();
e.execute("edit.select_none", &json!({})).unwrap();
assert!(!d.open_for_command(&e, "clip.loop"));
let clip = e.session().track_by_name("Kick").unwrap().clips()[0].clone();
e.execute("edit.select", &json!({"clips": [clip.id.0]})).unwrap();
assert!(d.open_for_command(&e, "clip.loop"));
assert_eq!(d.open_name(), Some("clip_looping"));
let Some(Dialog::Loop { by, count, length, format, start, filled }) = &d.open else { panic!("{:?}", d.open) };
assert_eq!((*by, *count, *start), (LoopBy::Count, 4, clip.start));
assert_eq!(*format, e.session().edit.main_counter);
// OK on the untouched field loops exactly four times, whatever its text rounds to.
let sent = loop_params(e.session(), LoopBy::Length, *count, length, *format, *start, filled);
assert_eq!(sent, Some(json!({"length": clip.length * 4})));
}
#[test]
fn loop_length_reads_a_duration_in_every_time_format() {
let e = soundcraft_engine::demo::demo_engine();
let s = e.session();
let start = s.sample_rate.samples(3.0);
for format in soundcraft_time::TimeFormat::ALL {
for len in [s.sample_rate.samples(2.0), s.sample_rate.samples(7.5)] {
let text = soundcraft_time::format_length(start, len, format, s.sample_rate, &s.tempo, s.frame_rate);
assert_eq!(loop_length(s, &text, format, start), Some(len), "{format:?} {text}");
}
}
let bars = soundcraft_time::TimeFormat::BarsBeats;
assert_eq!(loop_length(s, "1| 0| 000", bars, start), loop_length(s, "1", bars, start));
assert_eq!(loop_length(s, "-1| 0| 000", bars, start), None);
assert_eq!(loop_length(s, "two bars", bars, start), None);
}
#[test]
fn clip_looping_sends_the_chosen_option() {
let e = soundcraft_engine::demo::demo_engine();
let s = e.session();
let samples = soundcraft_time::TimeFormat::Samples;
let filled = ("0:01.000".to_string(), 48_001);
let send = |by, length: &str| loop_params(s, by, 3, length, samples, 0, &filled);
assert_eq!(send(LoopBy::Count, ""), Some(json!({"count": 3})));
assert_eq!(send(LoopBy::Length, "4800"), Some(json!({"length": 4800})));
assert_eq!(send(LoopBy::Length, "0:01.000"), Some(json!({"length": 48_001})));
assert_eq!(send(LoopBy::Length, "0"), None);
assert_eq!(send(LoopBy::Length, "soon"), None);
assert_eq!(send(LoopBy::Fill, "soon"), Some(json!({"fill": true})));
assert!(soundcraft_engine::command_specs().iter().any(|c| c.id == "clip.loop"));
}
}
+241 -257
View File
@@ -1,7 +1,10 @@
//! Application panel identities and persistence around the shared docking renderer.
//! Document contents remain owned by their existing view implementations.
use craft_ui::docking::{Action, DockArea, DockStyle, Layout, Location, Node, PanelLimits, Permissions, Placement, Zone};
use craft_ui::docking::{
Action, DockArea, DockCommand, DockError, DockOperation, DockStyle, FloatSpace, Floating, Layout, Location, Node, PanelLimits, Permissions,
Placement, UiDock, Zone, ZoneName,
};
use craft_ui::layout::{SplitAxis, SplitSize};
use serde::{Deserialize, Serialize};
use serde_json::{Value, json};
@@ -19,16 +22,6 @@ pub enum Panel {
impl Panel {
pub const ALL: &[Self] = &[Self::Canvas, Self::Tracks, Self::Clips, Self::Midi, Self::Memory, Self::History];
pub fn id(self) -> &'static str {
match self {
Self::Canvas => "canvas",
Self::Tracks => "tracks",
Self::Clips => "clips",
Self::Midi => "midi",
Self::Memory => "memory",
Self::History => "history",
}
}
fn label(self) -> &'static str {
match self {
Self::Canvas => "Session",
@@ -49,15 +42,16 @@ impl Panel {
Self::History => Zone::Right,
}
}
fn parse(id: &str) -> Result<Self, String> {
Self::ALL.iter().copied().find(|panel| panel.id() == id).ok_or_else(|| format!("Unknown panel: {id}"))
}
}
/// An Edit or Mix panel arrangement, saved with the UI preferences.
pub type Workspace = craft_ui::docking::Workspace<Panel>;
const MIDI_TAB_HEIGHT: f32 = 26.0;
/// The document view stays docked and open, and panels open in splits beside it.
fn permissions(panel: &Panel) -> Permissions {
if *panel == Panel::Canvas { Permissions::PROTECTED } else { Permissions::default() }
if *panel == Panel::Canvas { Permissions { accepts_splits: true, ..Permissions::PROTECTED } } else { Permissions::default() }
}
fn tabs(panel: Panel) -> Node<Panel> {
@@ -68,232 +62,184 @@ fn split(axis: SplitAxis, size: SplitSize, first: Node<Panel>, second: Node<Pane
Node::Split { axis, size, first: Box::new(first), second: Box::new(second) }
}
#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(default)]
pub struct Workspace {
pub layout: Layout<Panel>,
/// Closed panels retain their most recent group, side, size or floating rectangle.
pub hidden: Vec<(Panel, Location<Panel>)>,
/// The dock area that draws the Edit or Mix workspace.
pub fn area_id(window: crate::MainWindow) -> egui::Id {
egui::Id::unique(("soundcraft-docking", window == crate::MainWindow::Edit))
}
impl Default for Workspace {
fn default() -> Self {
Self {
layout: Layout {
root: Some(split(
SplitAxis::Vertical,
SplitSize::FixedSecond(320.0 + MIDI_TAB_HEIGHT),
split(
SplitAxis::Horizontal,
SplitSize::FixedFirst(168.0),
tabs(Panel::Tracks),
split(SplitAxis::Horizontal, SplitSize::FixedSecond(230.0), tabs(Panel::Canvas), tabs(Panel::Clips)),
),
tabs(Panel::Midi),
)),
floating: vec![
craft_ui::docking::Floating { panels: vec![Panel::Memory], active: 0, rect: [160.0, 120.0, 480.0, 280.0] },
craft_ui::docking::Floating { panels: vec![Panel::History], active: 0, rect: [680.0, 120.0, 260.0, 300.0] },
],
},
hidden: Vec::new(),
}
}
fn area(edit: bool) -> egui::Id {
area_id(if edit { crate::MainWindow::Edit } else { crate::MainWindow::Mix })
}
impl Workspace {
pub fn validate(&self) -> Result<(), String> {
self.layout.validate().map_err(|error| error.to_string())?;
if !self.layout.contains(&Panel::Canvas) || self.hidden.len() > Panel::ALL.len() {
return Err("The panel layout must contain its document view and bounded hidden panels".into());
}
// Saved settings cannot turn the protected document view into a floating or closable tab.
if self.layout.floating.iter().any(|group| group.panels.contains(&Panel::Canvas)) {
return Err("The document view cannot float".into());
}
let mut nodes: Vec<_> = self.layout.root.iter().collect();
while let Some(node) = nodes.pop() {
match node {
Node::Split { first, second, .. } => {
nodes.push(first);
nodes.push(second);
}
Node::Tabs { panels, .. } if panels.contains(&Panel::Canvas) && panels.len() != 1 => {
return Err("The document view cannot share a tab group".into());
}
Node::Stack { entries } if entries.iter().any(|entry| entry.panel == Panel::Canvas) => {
return Err("The document view cannot be collapsed".into());
}
_ => {}
}
}
let mut seen = std::collections::HashSet::new();
for (panel, location) in &self.hidden {
if *panel == Panel::Canvas || self.layout.contains(panel) || !seen.insert(*panel) {
return Err("A hidden panel has an invalid or duplicate identity".into());
}
if location.anchor == Some(Panel::Canvas) && matches!(location.placement, Placement::Tab { .. } | Placement::Split(Zone::Center)) {
return Err("A hidden panel cannot restore over the document view".into());
}
let mut probe = self.layout.clone();
if let Err(error) = probe.restore(*panel, location)
&& error != craft_ui::docking::DockError::MissingTarget
{
return Err(error.to_string());
}
}
Ok(())
}
pub fn validate_mix(&self) -> Result<(), String> {
self.validate()?;
if self.layout.contains(&Panel::Clips) || self.layout.contains(&Panel::Midi) {
return Err("Clips and MIDI panels belong to the Edit window".into());
}
Ok(())
}
pub fn apply(&mut self, action: Action<Panel>) -> Result<(), String> {
self.validate()?;
let mut next = self.clone();
if let Action::Close { panel } = &action {
let location = next.layout.location(panel).map_err(|error| error.to_string())?;
next.hidden.retain(|(id, _)| id != panel);
next.hidden.push((*panel, location));
} else if let Action::Open { panel, .. } | Action::OpenAt { panel, .. } = &action {
next.hidden.retain(|(id, _)| id != panel);
}
next.layout.apply_with_permissions(action, permissions).map_err(|error| error.to_string())?;
next.validate()?;
*self = next;
Ok(())
}
pub fn set_visible(&mut self, panel: Panel, visible: bool) -> Result<(), String> {
if self.layout.contains(&panel) == visible {
return Ok(());
}
if !visible {
return self.apply(Action::Close { panel });
}
self.validate()?;
let mut next = self.clone();
let mut location = next.hidden.iter().find(|(id, _)| *id == panel).map(|(_, location)| location.clone());
next.hidden.retain(|(id, _)| *id != panel);
if let Some(saved) = &mut location
&& saved.floating.is_some()
&& saved.anchor.as_ref().is_some_and(|anchor| !next.layout.floating.iter().any(|group| group.panels.contains(anchor)))
{
// The other members of a floating group may have been closed too.
saved.anchor = None;
saved.placement = Placement::Tab { before: None };
}
let restored = location.as_ref().is_some_and(|location| next.layout.restore(panel, location).is_ok());
if !restored {
next.layout
.apply_with_permissions(
Action::OpenAt { panel, anchor: Panel::Canvas, placement: Placement::Split(panel.default_side()) },
permissions,
)
.map_err(|error| error.to_string())?;
}
next.validate()?;
*self = next;
Ok(())
}
fn command(&mut self, params: &Value) -> Result<(), String> {
self.validate()?;
if let Some(action) = params.get("action") {
let action: Action<Panel> = serde_json::from_value(action.clone()).map_err(|error| error.to_string())?;
return self.apply(action);
}
let operation = params.get("operation").and_then(Value::as_str).ok_or("operation is required")?;
if operation == "resizeSplit" {
let path: Vec<bool> =
serde_json::from_value(params.get("path").cloned().ok_or("path is required")?).map_err(|error| error.to_string())?;
let size: SplitSize =
serde_json::from_value(params.get("size").cloned().ok_or("size is required")?).map_err(|error| error.to_string())?;
return self.apply(Action::ResizeSplit { path, size });
}
let panel = Panel::parse(params.get("panel").and_then(Value::as_str).ok_or("panel is required")?)?;
let action = match operation {
"open" => return self.set_visible(panel, true),
"close" => Action::Close { panel },
"activate" => Action::Activate { panel },
"setStackOpen" => Action::SetStackOpen { panel, open: params.get("open").and_then(Value::as_bool).ok_or("open must be a boolean")? },
"resizeStack" => {
let value = params.get("height").ok_or("height is required")?;
let height = if value.is_null() {
None
} else {
Some(
value
.as_f64()
.filter(|height| height.is_finite() && (0.0..=1_000_000.0).contains(height))
.ok_or("height must be finite and bounded")? as f32,
)
};
Action::ResizeStack { panel, height }
}
"float" | "moveFloating" => {
let rect = params.get("rect").and_then(Value::as_array).filter(|rect| rect.len() == 4).ok_or("rect must be [x,y,width,height]")?;
let mut values = [0.0f32; 4];
for (output, value) in values.iter_mut().zip(rect) {
*output = value
.as_f64()
.filter(|number| number.is_finite() && number.abs() <= 1_000_000.0)
.ok_or("rect values must be finite and bounded")? as f32;
}
if operation == "float" { Action::Float { panel, rect: values } } else { Action::MoveFloating { panel, rect: values } }
}
"move" => {
let anchor = Panel::parse(params.get("target").and_then(Value::as_str).ok_or("target is required")?)?;
let placement = match params.get("zone").and_then(Value::as_str).unwrap_or("center") {
"center" => Placement::Split(Zone::Center),
"left" => Placement::Split(Zone::Left),
"right" => Placement::Split(Zone::Right),
"top" => Placement::Split(Zone::Top),
"bottom" => Placement::Split(Zone::Bottom),
_ => return Err("zone must be center, left, right, top or bottom".into()),
};
let placement = if let Some(before) = params.get("before") {
if !matches!(placement, Placement::Split(Zone::Center)) {
return Err("before only applies to a center move".into());
}
Placement::Tab {
before: if before.is_null() {
None
} else {
Some(Panel::parse(before.as_str().ok_or("before must be a panel ID or null")?)?)
},
}
} else {
placement
};
Action::Move { panel, anchor, placement }
}
_ => return Err("operation must be open, close, activate, float, move, moveFloating, resizeSplit, setStackOpen or resizeStack".into()),
};
self.apply(action)
}
/// The Edit window's built-in arrangement.
pub fn edit_default() -> Workspace {
Workspace::new(Layout {
root: Some(split(
SplitAxis::Vertical,
SplitSize::FixedSecond(320.0 + MIDI_TAB_HEIGHT),
split(
SplitAxis::Horizontal,
SplitSize::FixedFirst(168.0),
tabs(Panel::Tracks),
split(SplitAxis::Horizontal, SplitSize::FixedSecond(230.0), tabs(Panel::Canvas), tabs(Panel::Clips)),
),
tabs(Panel::Midi),
)),
// Fixed ids: the default is rebuilt every frame and must stay the same windows.
floating: vec![
Floating::single(vec![Panel::Memory], 0, [160.0, 120.0, 480.0, 280.0]).with_id(1),
Floating::single(vec![Panel::History], 0, [680.0, 120.0, 260.0, 300.0]).with_id(2),
],
})
}
/// Mix retains its own arrangement; the timeline-only Clips and MIDI panels stay in Edit.
pub fn mix_default() -> Workspace {
let mut workspace = Workspace::default();
let _ = workspace.set_visible(Panel::Clips, false);
let _ = workspace.set_visible(Panel::Midi, false);
let mut workspace = edit_default();
let _ = set_visible(&mut workspace, Panel::Clips, false);
let _ = set_visible(&mut workspace, Panel::Midi, false);
workspace
}
/// The document view where the Edit window's rules keep it: in the docked frames, alone in
/// its tab group. `None` when `node` does not hold it.
fn canvas_placement(node: &Node<Panel>) -> Option<Result<(), &'static str>> {
match node {
Node::Split { first, second, .. } => canvas_placement(first).or_else(|| canvas_placement(second)),
Node::Tabs { panels, .. } if panels.contains(&Panel::Canvas) => {
Some(if panels.len() == 1 { Ok(()) } else { Err("The document view cannot share a tab group") })
}
Node::Stack { entries } if entries.iter().any(|entry| entry.panel == Panel::Canvas) => Some(Err("The document view cannot be collapsed")),
Node::Dock { columns, .. } if columns.iter().flat_map(|column| &column.groups).any(|group| group.panels.contains(&Panel::Canvas)) => {
Some(Err("The document view cannot join a dock column"))
}
_ => None,
}
}
/// Check a workspace against craft-ui's rules and SoundCraft's own: saved settings cannot
/// float, hide, collapse or share the protected document view.
pub fn validate(workspace: &Workspace) -> Result<(), String> {
workspace.validate().map_err(|error| error.to_string())?;
let layout = &workspace.layout;
if !layout.contains(&Panel::Canvas) || workspace.hidden.len() > Panel::ALL.len() {
return Err("The panel layout must contain its document view and bounded hidden panels".into());
}
if layout.floating.iter().any(|window| window.contains(&Panel::Canvas)) {
return Err("The document view cannot float".into());
}
layout.root.as_ref().and_then(canvas_placement).unwrap_or(Err("The document view cannot float"))?;
for (panel, location) in &workspace.hidden {
if *panel == Panel::Canvas {
return Err("A hidden panel has an invalid or duplicate identity".into());
}
if location.anchor == Some(Panel::Canvas) && matches!(location.placement, Placement::Tab { .. } | Placement::Split(Zone::Center)) {
return Err("A hidden panel cannot restore over the document view".into());
}
let mut probe = layout.clone();
if let Err(error) = probe.restore(*panel, location)
&& error != DockError::MissingTarget
{
return Err(error.to_string());
}
}
Ok(())
}
/// [`validate`] plus the Mix window's rule: Clips and MIDI stay in Edit.
pub fn validate_mix(workspace: &Workspace) -> Result<(), String> {
validate(workspace)?;
if workspace.layout.contains(&Panel::Clips) || workspace.layout.contains(&Panel::Midi) {
return Err("Clips and MIDI panels belong to the Edit window".into());
}
Ok(())
}
/// Change a copy of `workspace` and keep it only when it passes [`validate`].
fn checked(workspace: &mut Workspace, change: impl FnOnce(&mut Workspace) -> Result<(), DockError>) -> Result<(), String> {
validate(workspace)?;
let mut next = workspace.clone();
change(&mut next).map_err(|error| error.to_string())?;
validate(&next)?;
*workspace = next;
Ok(())
}
fn apply(workspace: &mut Workspace, action: Action<Panel>) -> Result<(), String> {
checked(workspace, |next| next.apply(action, permissions).map(drop))
}
/// Whether [`Workspace::set_visible`] can actually put `panel` back at its recorded place.
/// It follows the recorded location and the chain of hidden anchors; when none of that
/// restores, craft-ui falls back to an unanchored open, which since craft-ui 650b1c0b chooses
/// a dock group or right edge instead of reporting the missing anchor. Probe each chain
/// location with the public [`Layout::restore`] on a clone, so a deleted anchor is recognized
/// before calling in; craft-ui still performs the restore and rewrites the chain itself.
fn restores(workspace: &Workspace, panel: Panel) -> bool {
let recorded = |p: &Panel| workspace.hidden.iter().find(|(q, _)| q == p).map(|(_, location)| location.clone());
let mut chain: Vec<Location<Panel>> = Vec::new();
let mut seen = vec![panel];
let mut current = panel;
while let Some(location) = recorded(&current) {
let anchor = location.anchor;
chain.push(location);
let Some(anchor) = anchor.filter(|anchor| !seen.contains(anchor)) else { break };
seen.push(anchor);
current = anchor;
}
// Like the chain itself, the panel's own group/column state applies to every attempt.
let group = chain.first().and_then(|location| location.group.clone());
// Revisit this eligibility probe when updating the pinned craft-ui restore algorithm.
chain
.iter()
.rev()
.find_map(|location| {
let location = Location { group: group.clone().or_else(|| location.group.clone()), ..location.clone() };
let mut probe = workspace.layout.clone();
match probe.restore(panel, &location) {
Err(DockError::MissingTarget) => None,
result => Some(result.is_ok()),
}
})
.unwrap_or(false)
}
/// Show a panel at its recorded place, else beside the document view on its default side.
fn set_visible(workspace: &mut Workspace, panel: Panel, visible: bool) -> Result<(), String> {
if workspace.layout.contains(&panel) == visible {
return Ok(());
}
checked(workspace, |next| {
if !visible {
return next.apply(Action::Close { panel }, permissions).map(drop);
}
if next.hidden.iter().any(|(p, _)| *p == panel) && restores(next, panel) && next.set_visible(panel, true, None, permissions).is_ok() {
return Ok(());
}
next.apply(Action::OpenAt { panel, anchor: Panel::Canvas, placement: Placement::Split(panel.default_side()) }, permissions).map(drop)
})
}
/// Run a shared docking command. A hidden panel that is opened or activated comes back with
/// [`set_visible`], so it gets SoundCraft's default place when it has no recorded one.
fn run(workspace: &mut Workspace, command: &DockCommand<Panel>) -> Result<(), String> {
if let DockCommand::Activate { panel } | DockCommand::UiDock(UiDock::Operation(DockOperation::Open { panel } | DockOperation::Activate { panel })) =
command
&& !workspace.layout.contains(panel)
{
return set_visible(workspace, *panel, true);
}
// Only `window.panel.float` reads the float space, and SoundCraft does not register it.
checked(workspace, |next| command.apply(next, false, &FloatSpace::new(egui::Rect::NOTHING), permissions).map(drop))
}
fn sync(workspace: &mut Workspace, app: &crate::SoundApp, edit: bool) -> Result<(), String> {
workspace.set_visible(Panel::Tracks, app.ui.show_tracks_list)?;
workspace.set_visible(Panel::Memory, app.ui.show_memory_locations)?;
workspace.set_visible(Panel::History, app.ui.show_undo_history)?;
set_visible(workspace, Panel::Tracks, app.ui.show_tracks_list)?;
set_visible(workspace, Panel::Memory, app.ui.show_memory_locations)?;
set_visible(workspace, Panel::History, app.ui.show_undo_history)?;
if edit {
workspace.set_visible(Panel::Clips, app.ui.show_clip_list)?;
workspace.set_visible(Panel::Midi, app.ui.show_midi_editor)?;
set_visible(workspace, Panel::Clips, app.ui.show_clip_list)?;
set_visible(workspace, Panel::Midi, app.ui.show_midi_editor)?;
}
Ok(())
}
@@ -316,20 +262,57 @@ fn store(app: &mut crate::SoundApp, workspace: Workspace, edit: bool) {
}
}
pub fn command(app: &mut crate::SoundApp, reset: bool, params: &Value) -> Result<Value, String> {
/// A `ui.dock` operation. Panels and zones are checked first so an error names the field;
/// parsed through the untagged [`UiDock`], every mistake reads "did not match any variant".
fn operation(fields: &serde_json::Map<String, Value>) -> Result<DockOperation<Panel>, String> {
let name = fields.get("operation").ok_or("operation is required")?.as_str().ok_or("operation must be a string")?;
if name != "resizeSplit" && !fields.contains_key("panel") {
return Err("panel is required".into());
}
for field in ["panel", "target"] {
if let Some(value) = fields.get(field) {
let id = value.as_str().ok_or_else(|| format!("{field} must be a panel ID"))?;
serde_json::from_value::<Panel>(json!(id)).map_err(|_| format!("Unknown panel: {id}"))?;
}
}
if let Some(zone) = fields.get("zone")
&& serde_json::from_value::<ZoneName>(zone.clone()).is_err()
{
return Err("zone must be tab, center, left, right, top, bottom, above or below".into());
}
serde_json::from_value(Value::Object(fields.clone())).map_err(|error| error.to_string())
}
/// `ui.dock` and `ui.dock.reset`: the shared [`DockCommand`] vocabulary, applied to the
/// front window's workspace.
pub fn command(app: &mut crate::SoundApp, id: &str, params: &Value) -> Result<Value, String> {
let edit = app.ui.window == crate::MainWindow::Edit;
if !edit && ["panel", "target"].iter().any(|field| matches!(params.get(*field).and_then(Value::as_str), Some("clips" | "midi"))) {
return Err("Clips and MIDI panels belong to the Edit window".into());
}
let mut fields = match params {
Value::Object(fields) => fields.clone(),
Value::Null => serde_json::Map::new(),
_ => return Err("Docking params must be an object".into()),
};
let command = if id == "ui.dock" && !fields.contains_key("action") {
DockCommand::UiDock(UiDock::Operation(operation(&fields)?))
} else {
fields.insert("command".into(), json!(id));
serde_json::from_value(Value::Object(fields)).map_err(|error| error.to_string())?
};
let previous = if edit { app.ui.docking.clone() } else { app.ui.mix_docking.clone() };
let result = command_for(app, reset, params, edit)?;
let result = command_for(app, &command, edit)?;
let next = if edit { &app.ui.docking } else { &app.ui.mix_docking };
cancel_replaced_workspace(app, &previous, next, edit);
Ok(result)
}
fn cancel_replaced_workspace(app: &crate::SoundApp, previous: &Workspace, next: &Workspace, edit: bool) {
if (previous.layout != next.layout || previous.hidden != next.hidden)
if previous != next
&& let Some(ctx) = &app.docking_context
{
craft_ui::docking::cancel_drag::<Panel>(ctx, egui::Id::new(("soundcraft-docking", edit)));
craft_ui::docking::cancel_drag::<Panel>(ctx, area(edit));
}
}
@@ -339,22 +322,23 @@ pub(crate) fn cancel_replaced_state(app: &crate::SoundApp, next: &crate::UiState
cancel_replaced_workspace(app, &app.ui.mix_docking, &next.mix_docking, false);
}
fn command_for(app: &mut crate::SoundApp, reset: bool, params: &Value, edit: bool) -> Result<Value, String> {
if !edit && ["panel", "target"].iter().any(|field| matches!(params.get(*field).and_then(Value::as_str), Some("clips" | "midi"))) {
return Err("Clips and MIDI panels belong to the Edit window".into());
}
fn command_for(app: &mut crate::SoundApp, command: &DockCommand<Panel>, edit: bool) -> Result<Value, String> {
let mut workspace = if edit { app.ui.docking.clone() } else { app.ui.mix_docking.clone() };
if !reset && !edit {
workspace.validate_mix()?;
}
if reset {
workspace = if edit { Workspace::default() } else { mix_default() };
if command.is_app_owned() {
// Reset is the only application-owned command SoundCraft registers.
if !matches!(command, DockCommand::ResetWorkspace {}) {
return Err("Named panel workspaces are not supported".into());
}
workspace = if edit { edit_default() } else { mix_default() };
sync(&mut workspace, app, edit)?;
} else {
sync(&mut workspace, app, edit)?;
workspace.command(params)?;
if !edit {
workspace.validate_mix()?;
validate_mix(&workspace)?;
}
sync(&mut workspace, app, edit)?;
run(&mut workspace, command)?;
if !edit {
validate_mix(&workspace)?;
}
}
publish_visibility(app, &workspace, edit);
@@ -368,31 +352,34 @@ pub(crate) fn arrange(app: &mut crate::SoundApp) -> Result<(), String> {
let edit = app.ui.window == crate::MainWindow::Edit;
let mut workspace = if edit { app.ui.docking.clone() } else { app.ui.mix_docking.clone() };
if edit {
workspace.validate()?;
validate(&workspace)?;
} else {
workspace.validate_mix()?;
validate_mix(&workspace)?;
}
let previous = workspace.clone();
let floating: Vec<_> = workspace.layout.floating.iter().filter_map(|group| group.panels.first().map(|panel| (*panel, group.rect))).collect();
let floating: Vec<_> = workspace.layout.floating.iter().filter_map(|window| window.panels().next().map(|panel| (*panel, window.rect))).collect();
for (index, (panel, mut rect)) in floating.into_iter().enumerate() {
rect[0] = 32.0 + index as f32 * 28.0;
rect[1] = 80.0 + index as f32 * 28.0;
workspace.apply(Action::MoveFloating { panel, rect })?;
apply(&mut workspace, Action::MoveFloating { panel, rect })?;
}
cancel_replaced_workspace(app, &previous, &workspace, edit);
store(app, workspace, edit);
Ok(())
}
/// Draw and edit the dock layout for the requested window, falling back to the default layout if validation or panel
/// sync fails, then publish panel visibility, store the workspace and apply the frame's layout actions.
pub fn show(app: &mut crate::SoundApp, ui: &mut egui::Ui, window: crate::MainWindow) {
app.docking_context = Some(ui.ctx().clone());
let t = crate::theme::Tokens::current();
let edit = window == crate::MainWindow::Edit;
let mut workspace = if edit { std::mem::take(&mut app.ui.docking) } else { std::mem::replace(&mut app.ui.mix_docking, mix_default()) };
let mut workspace =
if edit { std::mem::replace(&mut app.ui.docking, edit_default()) } else { std::mem::replace(&mut app.ui.mix_docking, mix_default()) };
let before_sync = workspace.clone();
if let Err(error) = (if edit { workspace.validate() } else { workspace.validate_mix() }).and_then(|()| sync(&mut workspace, app, edit)) {
if let Err(error) = (if edit { validate(&workspace) } else { validate_mix(&workspace) }).and_then(|()| sync(&mut workspace, app, edit)) {
app.ui.status = format!("Panel layout could not be restored: {error}");
workspace = if edit { Workspace::default() } else { mix_default() };
workspace = if edit { edit_default() } else { mix_default() };
let _ = sync(&mut workspace, app, edit);
}
cancel_replaced_workspace(app, &before_sync, &workspace, edit);
@@ -407,7 +394,7 @@ pub fn show(app: &mut crate::SoundApp, ui: &mut egui::Ui, window: crate::MainWin
style.border = egui::Stroke::new(1.0, t.border);
style.accent = t.accent;
style.font = crate::theme::regular(11.5);
let output = DockArea::new(egui::Id::new(("soundcraft-docking", edit))).show_with_limits(
let output = DockArea::new(area(edit)).show_with_limits(
ui,
&workspace.layout,
&style,
@@ -426,7 +413,7 @@ pub fn show(app: &mut crate::SoundApp, ui: &mut egui::Ui, window: crate::MainWin
Panel::Tracks => crate::panels::tracks_and_groups(app, ui),
Panel::Clips => crate::panels::clip_list(app, ui),
Panel::Midi => {
ui.ctx().data_mut(|data| data.insert_temp(egui::Id::new("soundcraft-midi-body"), ui.max_rect()));
ui.ctx().data_mut(|data| data.insert_temp(egui::Id::unique("soundcraft-midi-body"), ui.max_rect()));
minimum_body(ui, egui::vec2(420.0, 260.0), |ui| crate::midi_editor::show(app, ui));
}
Panel::Memory => crate::panels::memory_locations(app, ui),
@@ -437,10 +424,7 @@ pub fn show(app: &mut crate::SoundApp, ui: &mut egui::Ui, window: crate::MainWin
publish_visibility(app, &workspace, edit);
store(app, workspace, edit);
for action in output.actions {
if let Err(error) = serde_json::to_value(action)
.map_err(|error| error.to_string())
.and_then(|action| command_for(app, false, &json!({"action": action}), edit))
{
if let Err(error) = command_for(app, &DockCommand::Layout { action }, edit) {
app.ui.status = format!("Panel move was not applied: {error}");
}
}
+363 -74
View File
@@ -159,7 +159,7 @@ fn universe_runs(s: &Session, r: Rect) -> Vec<(Rect, Color32)> {
/// [`Session::content_end`], worked out once per document revision: it walks every clip.
fn content_end(app: &SoundApp, ctx: &egui::Context) -> Samples {
let key = egui::Id::new("content_end");
let key = egui::Id::unique("content_end");
let revision = app.engine.revision;
if let Some((at, end)) = ctx.memory(|m| m.data.get_temp::<(u64, Samples)>(key))
&& at == revision
@@ -174,7 +174,7 @@ fn content_end(app: &SoundApp, ctx: &egui::Context) -> Samples {
/// Universe: the whole session in miniature; the box is the visible area (drag or click to move).
fn universe(app: &mut SoundApp, ui: &mut Ui) {
let r = ui.max_rect().shrink2(vec2(8.0, 4.0));
let key = egui::Id::new("universe_runs");
let key = egui::Id::unique("universe_runs");
let revision = app.engine.revision;
let runs = match ui.ctx().memory(|m| m.data.get_temp::<UniverseRuns>(key)).filter(|u| u.revision == revision && u.area == r) {
Some(u) => u.runs,
@@ -199,7 +199,7 @@ fn universe(app: &mut SoundApp, ui: &mut Ui) {
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 resp = ui.interact(r, ui.id().with("universe"), Sense::click_and_drag());
let resp = ui.interact(r, ui.scope_id().with("universe"), Sense::click_and_drag());
if let Some(p) = resp.interact_pointer_pos()
&& (resp.clicked() || resp.dragged())
{
@@ -209,6 +209,7 @@ fn universe(app: &mut SoundApp, ui: &mut Ui) {
}
}
/// Three dock toggle tabs for MIDI Editor, Memory Locations and Undo History.
fn dock_tabs(app: &mut SoundApp, ui: &mut Ui) {
let t = Tokens::current();
let r = ui.max_rect();
@@ -224,7 +225,7 @@ fn dock_tabs(app: &mut SoundApp, ui: &mut Ui) {
ui.painter().rect_filled(tr, 3.0, t.accent_dark);
}
ui.painter().galley(pos2(tr.min.x + 8.0, tr.center().y - g.size().y * 0.5), g, Color32::WHITE);
if ui.interact(tr, ui.id().with(("dock", label)), Sense::click()).clicked() {
if ui.interact(tr, ui.scope_id().with(("dock", label)), Sense::click()).clicked() {
let _ = app.run(id, json!({}));
}
x = tr.max.x + 6.0;
@@ -269,6 +270,7 @@ pub(crate) fn main_area(app: &mut SoundApp, ui: &mut Ui) {
crate::docking::minimum_body(ui, minimum, |ui| main_area_content(app, ui));
}
/// Lay out the Edit window: rulers, track headers and lanes, empty-area selection, overlays and both scrollbars.
fn main_area_content(app: &mut SoundApp, ui: &mut Ui) {
let t = Tokens::current();
let full = ui.max_rect();
@@ -320,7 +322,7 @@ fn main_area_content(app: &mut SoundApp, ui: &mut Ui) {
// Below the tracks a click clears the selection and a double-click opens New Tracks.
let empty = Rect::from_min_max(pos2(full.min.x, y.max(tl.min.y)), tl.max);
if empty.height() > 4.0 {
let resp = ui.interact(empty, ui.id().with("empty"), Sense::click());
let resp = ui.interact(empty, ui.scope_id().with("empty"), Sense::click());
if resp.double_clicked() {
let _ = app.dialogs.open_for_command(&app.engine, "track.new");
} else if resp.clicked() {
@@ -340,7 +342,7 @@ fn main_area_content(app: &mut SoundApp, ui: &mut Ui) {
let off = app.edit_layout.scroll_y / content_h;
let thumb = Rect::from_min_size(pos2(vs.min.x + 2.0, vs.min.y + vs.height() * off), vec2(vs.width() - 4.0, (vs.height() * frac).max(20.0)));
ui.painter().rect_filled(thumb, 3.0, t.button_hi);
let resp = ui.interact(vs, ui.id().with("vscroll"), Sense::drag());
let resp = ui.interact(vs, ui.scope_id().with("vscroll"), Sense::drag());
if resp.dragged() {
app.edit_layout.scroll_by(resp.drag_delta().y / frac, tl.height());
}
@@ -447,6 +449,7 @@ fn pan(app: &mut SoundApp, px: f64) {
}
}
/// Draw and drag the horizontal timeline scrollbar; its thumb reflects the visible sample range plus trailing space.
fn hscrollbar(app: &mut SoundApp, ui: &mut Ui, sb: Rect, tl: Rect) {
let t = Tokens::current();
ui.painter().rect_filled(sb, 0.0, t.panel_bg);
@@ -458,7 +461,7 @@ fn hscrollbar(app: &mut SoundApp, ui: &mut Ui, sb: Rect, tl: Rect) {
let off = (s.edit.zoom.scroll as f64 / total).clamp(0.0, 1.0) as f32;
let thumb = Rect::from_min_size(pos2(sb.min.x + sb.width() * off, sb.min.y + 3.0), vec2((sb.width() * frac).max(24.0), sb.height() - 6.0));
ui.painter().rect_filled(thumb, 3.0, t.button_hi);
let resp = ui.interact(sb, ui.id().with("hscroll"), Sense::drag());
let resp = ui.interact(sb, ui.scope_id().with("hscroll"), Sense::drag());
if resp.dragged() {
let dx = resp.drag_delta().x / sb.width();
let by_px = f64::from(dx) * total / s.edit.zoom.samples_per_px;
@@ -535,7 +538,7 @@ fn tempo_editor(app: &mut SoundApp, ui: &mut Ui, painter: &egui::Painter, s: &Se
painter.add(Shape::line(pts, Stroke::new(1.5, Color32::from_rgb(140, 230, 160))));
painter.text(pos2(tl.min.x + 4.0, row.min.y + 8.0), Align2::LEFT_CENTER, format!("{hi:.0}"), regular(9.0), Color32::from_rgb(120, 160, 130));
painter.text(pos2(tl.min.x + 4.0, row.max.y - 8.0), Align2::LEFT_CENTER, format!("{lo:.0}"), regular(9.0), Color32::from_rgb(120, 160, 130));
let resp = ui.interact(row, ui.id().with("tempo_editor"), Sense::click());
let resp = ui.interact(row, ui.scope_id().with("tempo_editor"), Sense::click());
if resp.clicked()
&& let Some(p) = resp.interact_pointer_pos()
{
@@ -568,6 +571,7 @@ fn nice_step(spp: f64, sr: f64, min_px: f64) -> i64 {
(3600.0 * sr) as i64
}
/// Draw each enabled ruler and handle its clicks and double-clicks against the visible sample range.
fn draw_rulers(app: &mut SoundApp, ui: &mut Ui, area: Rect, tl: Rect, rulers: &[String]) {
let t = Tokens::current();
let s = app.engine.session_arc();
@@ -586,7 +590,7 @@ fn draw_rulers(app: &mut SoundApp, ui: &mut Ui, area: Rect, tl: Rect, rulers: &[
match id.as_str() {
"tempo" => {
painter.rect_filled(row.shrink2(vec2(0.0, 1.0)), 0.0, t.tempo_ruler);
let tresp = ui.interact(row, ui.id().with("tempo_ruler"), Sense::click());
let tresp = ui.interact(row, ui.scope_id().with("tempo_ruler"), Sense::click());
if tresp.double_clicked()
&& let Some(p) = tresp.interact_pointer_pos()
{
@@ -640,7 +644,7 @@ fn draw_rulers(app: &mut SoundApp, ui: &mut Ui, area: Rect, tl: Rect, rulers: &[
painter.line_segment([pos2(x, row.min.y + 2.0), pos2(x, row.max.y - 2.0)], Stroke::new(2.0, Color32::from_rgb(200, 180, 240)));
painter.text(pos2(x + 5.0, row.center().y), Align2::LEFT_CENTER, k, bold(11.0), Color32::from_rgb(236, 230, 250));
}
let resp = ui.interact(row, ui.id().with("key_ruler"), Sense::click());
let resp = ui.interact(row, ui.scope_id().with("key_ruler"), Sense::click());
if resp.double_clicked()
&& let Some(p) = resp.interact_pointer_pos()
{
@@ -687,7 +691,7 @@ fn draw_rulers(app: &mut SoundApp, ui: &mut Ui, area: Rect, tl: Rect, rulers: &[
painter.text(pos2(x + 8.0, row.center().y), Align2::LEFT_CENTER, &m.name, bold(11.0), t.marker_text);
}
// Click on the marker ruler: recall a marker; double-click renames it, or adds one.
let resp = ui.interact(row, ui.id().with(("marker_ruler", lane)), Sense::click());
let resp = ui.interact(row, ui.scope_id().with(("marker_ruler", lane)), Sense::click());
if let Some(p) = resp.interact_pointer_pos() {
let hit = s
.markers
@@ -712,7 +716,7 @@ fn draw_rulers(app: &mut SoundApp, ui: &mut Ui, area: Rect, tl: Rect, rulers: &[
if let Some(f) = fmt {
timebase_ruler(ui, &painter, &s, tl, row, view, f, s.frame_rate, i + 1 == rulers.len());
// Clicking a timebase ruler locates.
let resp = ui.interact(row, ui.id().with(("ruler", id.as_str())), Sense::click_and_drag());
let resp = ui.interact(row, ui.scope_id().with(("ruler", id.as_str())), Sense::click_and_drag());
if (resp.clicked() || resp.dragged())
&& let Some(p) = resp.interact_pointer_pos()
{
@@ -865,7 +869,7 @@ fn views_button(app: &mut SoundApp, ui: &mut Ui, rulers: Rect) {
let r = Rect::from_min_size(rulers.min + vec2(6.0, 5.0), vec2(50.0, 18.0));
let mut child = ui.new_child(egui::UiBuilder::new().max_rect(r));
let resp = crate::widgets::selector_box(&mut child, r.width(), r.height(), "Views", Tokens::current().text);
resp.widget_info(|| egui::WidgetInfo::labeled(egui::WidgetType::ComboBox, child.is_enabled(), "Views"));
resp.widget_info(|| egui::WidgetInfo::labeled(egui::Role::ComboBox, child.is_enabled(), "Views"));
let resp = resp.on_hover_text("Columns beside the track headers: inserts, sends, I/O…");
egui::Popup::menu(&resp).close_behavior(egui::PopupCloseBehavior::CloseOnClickOutside).show(|ui| {
let shown = edit_columns(app.engine.session());
@@ -944,12 +948,35 @@ fn track_row(app: &mut SoundApp, ui: &mut Ui, id: TrackId, row: Rect, tl: Rect,
painter.rect_filled(Rect::from_min_max(pos2(x0, lane.min.y), pos2(x1, lane.max.y)), 0.0, t.selection);
}
}
let strip = crate::dialogs::strip_silence_rectangles(app, ui.ctx());
if let Some((_, ranges)) = strip.iter().find(|(t, _)| *t == id) {
draw_strip_silence(&painter, &s, tl, lane, ranges);
}
lane_interaction(app, ui, track, lane, tl);
if track.view == "playlists" {
playlist_lanes(app, ui, track, row, main_h, tl);
}
}
/// The outline of the Strip Silence rectangles.
pub fn strip_silence_stroke() -> Stroke {
Stroke::new(1.5, Tokens::current().accent)
}
/// The Strip Silence rectangles on one lane, while the Strip Silence window is open: one around each
/// range Strip keeps.
fn draw_strip_silence(painter: &egui::Painter, s: &Session, tl: Rect, lane: Rect, ranges: &[Range]) {
let stroke = strip_silence_stroke();
for r in ranges {
let x0 = x_of(s, tl, r.start).max(lane.min.x);
let x1 = x_of(s, tl, r.end).min(lane.max.x);
if x1 > x0 {
let rect = Rect::from_min_max(pos2(x0, lane.min.y + 1.0), pos2(x1, lane.max.y - 1.0));
painter.rect(rect, 0.0, stroke.color.gamma_multiply(0.12), stroke, StrokeKind::Inside);
}
}
}
/// Alternate playlists as lanes under the main one; clicking a clip promotes it (comping).
fn playlist_lanes(app: &mut SoundApp, ui: &mut Ui, track: &Track, row: Rect, main_h: f32, tl: Rect) {
let t = Tokens::current();
@@ -969,7 +996,7 @@ fn playlist_lanes(app: &mut SoundApp, ui: &mut Ui, track: &Track, row: Rect, mai
for c in pl.clips.iter().filter(|c| c.range().overlaps(&view)) {
draw_clip(app, &painter, &s, track, sorted, c, tl, lane, false);
}
let resp = ui.interact(lane, ui.id().with(("pl_lane", track.id.0, i)), Sense::click());
let resp = ui.interact(lane, ui.scope_id().with(("pl_lane", track.id.0, i)), Sense::click());
if resp.clicked()
&& let Some(p) = resp.interact_pointer_pos()
{
@@ -989,12 +1016,14 @@ fn playlist_lanes(app: &mut SoundApp, ui: &mut Ui, track: &Track, row: Rect, mai
}
}
/// Draw one track header: resize edge, selection and context menu, folder/options control, automation-lane toggle,
/// name and the remaining track controls.
fn track_header(app: &mut SoundApp, ui: &mut Ui, track: &Track, head: Rect, selected: bool, head_bg: Color32) {
let t = Tokens::current();
let id = track.id;
// Drag the bottom edge to resize (snaps to the height presets).
let edge = Rect::from_min_max(pos2(head.min.x, head.max.y - 4.0), pos2(head.max.x, head.max.y + 2.0));
let eresp = ui.interact(edge, ui.id().with(("hedge", id.0)), Sense::drag());
let eresp = ui.interact(edge, ui.scope_id().with(("hedge", id.0)), Sense::drag());
if eresp.hovered() || eresp.dragged() {
ui.ctx().set_cursor_icon(egui::CursorIcon::ResizeVertical);
}
@@ -1022,7 +1051,7 @@ fn track_header(app: &mut SoundApp, ui: &mut Ui, track: &Track, head: Rect, sele
}
let h = head.height();
// Click on header background selects the track.
let bg = ui.interact(head, ui.id().with(("hdr", id.0)), Sense::click());
let bg = ui.interact(head, ui.scope_id().with(("hdr", id.0)), Sense::click());
if bg.clicked() {
click_track(app, id, ui.input(|i| i.modifiers), in_edit_window);
}
@@ -1032,7 +1061,7 @@ fn track_header(app: &mut SoundApp, ui: &mut Ui, track: &Track, head: Rect, sele
let ink = shade(strip, -0.8);
// The strip's round button opens a folder, or any other track's options.
let round = at(2.0, 2.0, 10.0, 10.0);
let rresp = ui.interact(round, ui.id().with(("options", id.0)), Sense::click());
let rresp = ui.interact(round, ui.scope_id().with(("options", id.0)), Sense::click());
ui.painter().circle_filled(round.center(), 5.0, shade(strip, if rresp.hovered() { 0.6 } else { 0.45 }));
arrow(ui.painter(), round.center(), !track.is_folder() || track.folder_open, ink);
if track.is_folder() {
@@ -1044,7 +1073,7 @@ fn track_header(app: &mut SoundApp, ui: &mut Ui, track: &Track, head: Rect, sele
}
if h >= 40.0 && track.kind.has_playlist() {
let ar = at(3.0, h - 12.0, 9.0, 9.0);
let aresp = ui.interact(ar, ui.id().with(("auto_lane", id.0)), Sense::click());
let aresp = ui.interact(ar, ui.scope_id().with(("auto_lane", id.0)), Sense::click());
let showing = AutoParam::parse(&track.view).is_some();
ui.painter().rect_filled(ar, 1.5, shade(strip, if aresp.hovered() || showing { 0.6 } else { 0.45 }));
let (a, b) = (ar.min + vec2(2.5, 2.5), ar.max - vec2(2.5, 2.5));
@@ -1076,7 +1105,7 @@ fn track_header(app: &mut SoundApp, ui: &mut Ui, track: &Track, head: Rect, sele
};
let name_color = if selected { t.text_dark } else { t.text.lerp_to_gamma(t.text_dim, 0.4) };
ui.painter().with_clip_rect(name_r).text(pos2(name_r.min.x + 1.5, name_r.center().y), Align2::LEFT_CENTER, &shown_name, bold(12.5), name_color);
let nresp = ui.interact(name_r, ui.id().with(("name", id.0)), Sense::click_and_drag());
let nresp = ui.interact(name_r, ui.scope_id().with(("name", id.0)), Sense::click_and_drag());
if nresp.double_clicked() {
app.dialogs.open_rename_track(id, &track.name);
} else if nresp.clicked() {
@@ -1097,7 +1126,7 @@ fn track_header(app: &mut SoundApp, ui: &mut Ui, track: &Track, head: Rect, sele
.min_by_key(|y| ((y - p.y).abs() * 10.0) as i64)
.unwrap_or(p.y);
ui.ctx()
.layer_painter(egui::LayerId::new(egui::Order::Foreground, egui::Id::new("reorder")))
.layer_painter(egui::LayerId::new(egui::Order::Foreground, egui::Id::unique("reorder")))
.line_segment([pos2(head.min.x, line_y), pos2(head.max.x + 600.0, line_y)], Stroke::new(3.0, Tokens::current().accent));
}
if nresp.drag_stopped()
@@ -1115,7 +1144,7 @@ fn track_header(app: &mut SoundApp, ui: &mut Ui, track: &Track, head: Rect, sele
}
}
let pl_r = at(114.0, 1.0, 11.0, 14.5);
let presp = ui.interact(pl_r, ui.id().with(("pl", id.0)), Sense::click());
let presp = ui.interact(pl_r, ui.scope_id().with(("pl", id.0)), Sense::click());
ui.painter().rect_filled(pl_r, 1.5, shade(head_bg, if presp.hovered() { -0.2 } else { -0.35 }));
arrow(ui.painter(), pos2(pl_r.center().x, pl_r.min.y + 5.75), true, t.text);
egui::Popup::menu(&presp.on_hover_text("Playlists")).show(|ui| {
@@ -1146,19 +1175,27 @@ fn track_header(app: &mut SoundApp, ui: &mut Ui, track: &Track, head: Rect, sele
(HeaderKey::Mute, m.mute, implied, "mix.mute", mixes),
];
for (i, (key, lit, implied, cmd, shown)) in keys.into_iter().enumerate() {
if shown && header_toggle(ui, at(23.0 + i as f32 * 26.0, 20.0, 16.0, 16.0), id, key, lit, implied).clicked() {
if shown && header_toggle(ui, at(23.0 + i as f32 * 26.0, 20.0, 16.0, 16.0), id, &track.name, key, lit, implied).clicked() {
let _ = app.run(cmd, json!({"track": id.0}));
}
}
}
if h >= 58.0 {
let vresp = header_selector(ui, at(17.0, 39.0, 105.0, 17.0), ui.id().with(("view", id.0)), &view_label(&track.view), t.text, true);
let vresp = header_selector(
ui,
at(17.0, 39.0, 105.0, 17.0),
ui.scope_id().with(("view", id.0)),
&view_label(&track.view),
t.text,
true,
&format!("Track view for {}", track.name),
);
egui::Popup::menu(&vresp).show(|ui| view_menu(app, ui, track));
}
if h >= 76.0 {
if matches!(track.kind, TrackKind::Audio | TrackKind::Instrument | TrackKind::Aux) {
let r = at(17.0, 57.0, 13.0, 17.0);
let resp = ui.interact(r, ui.id().with(("freeze", id.0)), Sense::click());
let resp = ui.interact(r, ui.scope_id().with(("freeze", id.0)), Sense::click());
raised_key(ui.painter(), r, if track.frozen { t.slot_bypassed } else { t.slot_face }, resp.hovered());
for a in [0.0f32, 60.0, 120.0] {
let d = vec2(a.to_radians().sin(), a.to_radians().cos()) * 3.5;
@@ -1170,7 +1207,8 @@ fn track_header(app: &mut SoundApp, ui: &mut Ui, track: &Track, head: Rect, sele
}
if !matches!(track.kind, TrackKind::Master | TrackKind::Vca | TrackKind::Folder) {
let (voice, color) = if track.inactive { ("off", t.text_dim) } else { ("dyn", t.text) };
let resp = header_selector(ui, at(31.0, 57.0, 31.0, 17.0), ui.id().with(("voice", id.0)), voice, color, false);
let voice_name = format!("Voice for {}", track.name);
let resp = header_selector(ui, at(31.0, 57.0, 31.0, 17.0), ui.scope_id().with(("voice", id.0)), voice, color, false, &voice_name);
egui::Popup::menu(&resp.on_hover_text("Voice: dyn plays the track, off makes it inactive")).show(|ui| {
for (label, inactive) in [("dyn", false), ("off", true)] {
if ui.selectable_label(track.inactive == inactive, label).clicked() {
@@ -1187,7 +1225,8 @@ fn track_header(app: &mut SoundApp, ui: &mut Ui, track: &Track, head: Rect, sele
soundcraft_model::AutomationMode::Latch => t.auto_latch,
_ => t.text_dim,
};
let aresp = header_selector(ui, at(63.0, 57.0, 59.0, 17.0), ui.id().with(("auto_mode", id.0)), mode.label(), col, true);
let mode_name = format!("Automation mode for {}", track.name);
let aresp = header_selector(ui, at(63.0, 57.0, 59.0, 17.0), ui.scope_id().with(("auto_mode", id.0)), mode.label(), col, true, &mode_name);
egui::Popup::menu(&aresp).show(|ui| {
for m in soundcraft_model::AutomationMode::ALL {
if ui.selectable_label(m == mode, m.label()).clicked() {
@@ -1199,7 +1238,7 @@ fn track_header(app: &mut SoundApp, ui: &mut Ui, track: &Track, head: Rect, sele
if h >= 94.0 {
if track.kind.has_playlist() {
let r = at(17.0, 75.0, 13.0, 17.0);
let resp = ui.interact(r, ui.id().with(("timebase", id.0)), Sense::click());
let resp = ui.interact(r, ui.scope_id().with(("timebase", id.0)), Sense::click());
raised_key(ui.painter(), r, t.slot_face, resp.hovered());
let c = r.center();
if track.ticks_timebase {
@@ -1226,7 +1265,7 @@ fn track_header(app: &mut SoundApp, ui: &mut Ui, track: &Track, head: Rect, sele
}
let field = at(30.0, 75.0, 92.0, 17.0);
if matches!(track.kind, TrackKind::Audio | TrackKind::Instrument) {
let resp = ui.interact(field, ui.id().with(("field", id.0)), Sense::click());
let resp = ui.interact(field, ui.scope_id().with(("field", id.0)), Sense::click());
ui.painter().rect_filled(field, 2.0, shade(head_bg, if resp.hovered() { -0.02 } else { -0.1 }));
if track.kind == TrackKind::Instrument {
let name =
@@ -1268,9 +1307,15 @@ fn arrow(p: &egui::Painter, c: Pos2, down: bool, color: Color32) {
}
/// A header selector (track view, automation mode…): the value centred, and for menus an arrow
/// in the top-right corner.
fn header_selector(ui: &Ui, r: Rect, id: egui::Id, text: &str, color: Color32, menu: bool) -> egui::Response {
/// in the top-right corner. `name` is the accessible meaning of the value (for example
/// "Automation mode"), not the short text painted inside the slot.
fn header_selector(ui: &Ui, r: Rect, id: egui::Id, text: &str, color: Color32, menu: bool, name: &str) -> egui::Response {
let resp = ui.interact(r, id, Sense::click());
resp.widget_info(|| {
let mut info = egui::WidgetInfo::labeled(egui::accesskit::Role::ComboBox, ui.is_enabled(), name);
info.current_text_value = Some(text.to_string());
info
});
raised_key(ui.painter(), r, Tokens::current().slot_face, resp.hovered());
let tr = if menu { r.with_max_x(r.max.x - 7.0) } else { r };
ui.painter().with_clip_rect(tr.shrink(1.0)).text(tr.center(), Align2::CENTER_CENTER, text, bold(10.0), color);
@@ -1290,7 +1335,7 @@ enum HeaderKey {
/// Record, input, solo or mute: `lit` gives the key its colour, `implied` a dark shade of it (a
/// mute that another track's solo implies). Record keys stay dark red when off, as in Pro Tools.
fn header_toggle(ui: &Ui, r: Rect, track: TrackId, key: HeaderKey, lit: bool, implied: bool) -> egui::Response {
fn header_toggle(ui: &Ui, r: Rect, track: TrackId, track_name: &str, key: HeaderKey, lit: bool, implied: bool) -> egui::Response {
let t = Tokens::current();
let (label, color) = match key {
HeaderKey::Rec => ("Record enable", t.rec),
@@ -1298,8 +1343,9 @@ fn header_toggle(ui: &Ui, r: Rect, track: TrackId, key: HeaderKey, lit: bool, im
HeaderKey::Solo => ("Solo", t.solo),
HeaderKey::Mute => ("Mute", t.mute),
};
let resp = ui.interact(r, ui.id().with((label, track.0)), Sense::click());
resp.widget_info(|| egui::WidgetInfo::selected(egui::WidgetType::Button, ui.is_enabled(), lit, label));
let name = format!("{label} for {track_name}");
let resp = ui.interact(r, ui.scope_id().with((label, track.0)), Sense::click());
resp.widget_info(|| egui::WidgetInfo::selected(egui::Role::Button, ui.is_enabled(), lit, &name));
let rec = key == HeaderKey::Rec;
let (tint, k) = if lit {
(color, 0.85)
@@ -1370,7 +1416,11 @@ fn header_column(app: &mut SoundApp, ui: &mut Ui, track: &Track, col: &str, r: R
ui.painter().vline(r.min.x, r.y_range(), Stroke::new(1.0, Tokens::current().border));
let mut c = track.comments.clone();
let mut child = ui.new_child(egui::UiBuilder::new().max_rect(r.shrink(3.0)));
if child.add(egui::TextEdit::multiline(&mut c).desired_width(r.width() - 6.0).font(regular(10.0))).changed() {
let field = crate::widgets::named_text_field(
child.add(egui::TextEdit::multiline(&mut c).desired_width(r.width() - 6.0).font(regular(10.0))),
&format!("Comments for {}", track.name),
);
if field.changed() {
let _ =
app.engine.execute_merged("track.comments", &json!({"track": track.id.0, "comments": c}), &format!("comments:{}", track.id.0));
}
@@ -1387,17 +1437,23 @@ fn io_column(app: &mut SoundApp, ui: &mut Ui, track: &Track, r: Rect) {
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) {
let resp = header_selector(&ui, input, ui.id().with(("in", id.0)), &crate::mix_window::input_name(app, track), t.text, true);
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);
}
if let Some(out) = row(21.0, 17.0) {
let (sel, fader) = (out.with_max_x(out.max.x - 11.0), out.with_min_x(out.max.x - 11.0));
let resp = ui.interact(sel, ui.id().with(("out", id.0)), Sense::click());
let fresp = ui.interact(fader, ui.id().with(("out_fader", id.0)), Sense::click());
let resp = ui.interact(sel, ui.scope_id().with(("out", id.0)), Sense::click());
let output_name = crate::mix_window::route_name(app, &track.mixer.output);
resp.widget_info(|| {
let mut info = egui::WidgetInfo::labeled(egui::accesskit::Role::ComboBox, ui.is_enabled(), format!("Output for {}", track.name));
info.current_text_value = Some(output_name.clone());
info
});
let fresp = ui.interact(fader, ui.scope_id().with(("out_fader", id.0)), Sense::click());
raised_key(ui.painter(), out, t.slot_face, resp.hovered() || fresp.hovered());
groove(ui.painter(), fader.min.x - 0.5, out.min.y + 1.0..=out.max.y - 1.0, t.slot_face[0]);
let name = crate::mix_window::route_name(app, &track.mixer.output);
ui.painter().with_clip_rect(sel.shrink(1.0)).text(sel.center(), Align2::CENTER_CENTER, name, bold(10.0), t.text);
ui.painter().with_clip_rect(sel.shrink(1.0)).text(sel.center(), Align2::CENTER_CENTER, &output_name, bold(10.0), t.text);
let c = fader.center() + vec2(0.5, 0.0);
ui.painter().vline(c.x, c.y - 5.0..=c.y + 5.0, Stroke::new(1.0, t.text));
ui.painter().rect_filled(Rect::from_center_size(c + vec2(0.0, -1.5), vec2(5.0, 2.5)), 0.5, t.text);
@@ -1656,7 +1712,14 @@ fn draw_live_take(app: &SoundApp, painter: &egui::Painter, s: &Session, track: &
let (body, _, wave) = clip_colors([t.rec.r(), t.rec.g(), t.rec.b()], false);
painter.rect(r, CornerRadius::same(2), body, Stroke::new(1.0, Color32::from_rgb(10, 10, 10)), StrokeKind::Inside);
if track.view != "blocks" {
crate::waveform::draw_live(&painter.with_clip_rect(r.intersect(lane)), s, tl, origin as f64, block_len, &take, r, wave);
crate::waveform::draw_live(
crate::waveform::Canvas { painter: &painter.with_clip_rect(r.intersect(lane)), s, tl },
origin as f64,
block_len,
&take,
r,
wave,
);
}
}
@@ -1722,7 +1785,14 @@ fn draw_clip(app: &SoundApp, painter: &egui::Painter, s: &Session, track: &Track
preview = Clip { fade_in, fade_out, ..clip.clone() };
&preview
};
crate::waveform::draw(&painter.with_clip_rect(vis), s, tl, shown, audio, track.channels(), body_r, wave);
crate::waveform::draw(
crate::waveform::Canvas { painter: &painter.with_clip_rect(vis), s, tl },
shown,
audio,
track.channels(),
body_r,
wave,
);
let n = track.channels().min(audio.peaks.len()).max(1);
for k in 1..n {
let y = body_r.min.y + body_r.height() / n as f32 * k as f32;
@@ -1776,7 +1846,7 @@ fn draw_clip(app: &SoundApp, painter: &egui::Painter, s: &Session, track: &Track
} else {
clip_colors(prev.color.unwrap_or(track.color), false).2.gamma_multiply(0.6)
};
crate::waveform::draw(&painter.with_clip_rect(xf), s, tl, prev, audio, track.channels(), body_r, tail);
crate::waveform::draw(crate::waveform::Canvas { painter: &painter.with_clip_rect(xf), s, tl }, prev, audio, track.channels(), body_r, tail);
}
// Clip gain line.
let show_gain = s.edit.flag("view.clip.gain_line") && (!clip.gain_env.is_empty() || clip.gain_db.abs() > 0.01 || track.view == "clip_gain");
@@ -2053,9 +2123,10 @@ pub fn clip_gain_icon(s: &Session, tl: Rect, lane: Rect, clip: &Clip) -> Option<
(shown && x1 - x0 >= 48.0 && lane.height() >= 30.0).then(|| Rect::from_min_size(pos2(x0 + 4.0, lane.max.y - 16.0), vec2(9.0, 13.0)))
}
/// Draw the read-only clip-gain fader tooltip at `pos`.
fn clip_gain_popup(ctx: &egui::Context, fader_bottom: Pos2, pos: f32) {
let t = Tokens::current();
let p = ctx.layer_painter(egui::LayerId::new(egui::Order::Tooltip, egui::Id::new("clip_gain_fader")));
let p = ctx.layer_painter(egui::LayerId::new(egui::Order::Tooltip, egui::Id::unique("clip_gain_fader")));
let track = Rect::from_min_max(pos2(fader_bottom.x - 2.0, fader_bottom.y - CLIP_FADER_TRAVEL), pos2(fader_bottom.x + 2.0, fader_bottom.y));
let panel = Rect::from_min_max(pos2(track.min.x - 22.0, track.min.y - 26.0), pos2(track.max.x + 22.0, track.max.y + 8.0));
p.rect(panel, 3.0, Color32::from_rgb(38, 38, 40), Stroke::new(1.0, Color32::from_rgb(10, 10, 10)), StrokeKind::Inside);
@@ -2068,8 +2139,10 @@ fn clip_gain_popup(ctx: &egui::Context, fader_bottom: Pos2, pos: f32) {
p.text(pos2(panel.center().x, panel.min.y + 12.0), Align2::CENTER_CENTER, format!("{db:+.1}"), regular(10.5), t.counter_text);
}
/// Handle one track lane: clip context menus, drag-and-drop sources, sampler drops, and the active tool's selection,
/// trim, fade, automation and clip editing.
fn lane_interaction(app: &mut SoundApp, ui: &mut Ui, track: &Track, lane: Rect, tl: Rect) {
let id = ui.id().with(("lane", track.id.0));
let id = ui.scope_id().with(("lane", track.id.0));
let resp = ui.interact(lane, id, Sense::click_and_drag());
// The clip menu is attached before the early return below: once the menu is open the pointer
// is over the popup, the lane has no hover position, and the rest of this function bails out.
@@ -2327,8 +2400,13 @@ fn lane_interaction(app: &mut SoundApp, ui: &mut Ui, track: &Track, lane: Rect,
"automation.set_point",
json!({"track": track.id.0, "param": automation_id(param), "at": at, "value": lo + (hi - lo) * k}),
);
} else if track.kind.is_midi() && hit.is_none() {
new_midi_clip(app, &s, track.id, at, at);
} else if track.kind.is_midi() {
match &hit {
Some(c) => {
let _ = app.run("edit.select", json!({"clips": [c.id.0]}));
}
None => new_midi_clip(app, &s, track.id, at, at),
}
}
}
_ if !selector_press => place_insertion(app, ui, &s, track.id, at, mods.shift),
@@ -2603,7 +2681,7 @@ struct DragPlan {
/// until the document, the Edit mode or the target changes.
fn drag_plan(ctx: &egui::Context, s: &Session, revision: u64, clips: &[ClipId], (from, to): (TrackId, TrackId), grab_at: Samples) -> Arc<DragPlan> {
type Key = (u64, EditMode, Vec<ClipId>, TrackId, TrackId, Samples);
let id = egui::Id::new("clip_drag_plan");
let id = egui::Id::unique("clip_drag_plan");
let key: Key = (revision, s.edit.edit_mode, clips.to_vec(), from, to, grab_at);
if let Some((k, plan)) = ctx.data(|d| d.get_temp::<(Key, Arc<DragPlan>)>(id))
&& k == key
@@ -2732,6 +2810,18 @@ fn clip_context_menu(app: &mut SoundApp, ui: &mut Ui, clip: Option<ClipId>) {
}
}
/// One frame's inputs to [`follow_update`].
struct FollowFrame {
playing: bool,
mode_follow: bool,
playhead_visible: bool,
cur: Samples,
last_scroll: Samples,
last_follow_to: Option<Samples>,
hold: bool,
pos: Samples,
}
/// Next follow state after observing one frame: `(hold, follow_to)`.
///
/// The view is ours to move only while nothing else moved it: when the
@@ -2741,16 +2831,8 @@ fn clip_context_menu(app: &mut SoundApp, ui: &mut Ui, clip: Option<ClipId>) {
/// jump is issued. Stopping, or scrolling back to the playhead, clears the
/// hold. Only `page`/`continuous`/`center` ever issue follow jumps while
/// playing; `none`/`after_playback` leave the view alone.
fn follow_update(
playing: bool,
mode_follow: bool,
playhead_visible: bool,
cur: Samples,
last_scroll: Samples,
last_follow_to: Option<Samples>,
hold: bool,
pos: Samples,
) -> (bool, Option<Samples>) {
fn follow_update(frame: FollowFrame) -> (bool, Option<Samples>) {
let FollowFrame { playing, mode_follow, playhead_visible, cur, last_scroll, last_follow_to, hold, pos } = frame;
if !playing || playhead_visible {
return (false, None);
}
@@ -2799,7 +2881,7 @@ fn overlay(app: &mut SoundApp, ui: &mut Ui, tl: Rect, area: Rect) {
} else if !app.is_playing() {
let x = x_of(s, tl, sel.start);
let now = ui.input(|i| i.time);
let (id, key) = (ui.id().with("insertion_blink"), (sel.start, s.edit.selected_tracks.clone()));
let (id, key) = (ui.scope_id().with("insertion_blink"), (sel.start, s.edit.selected_tracks.clone()));
let since = ui.ctx().data_mut(|d| {
let (last, since) = d.get_temp_mut_or_insert_with(id, || (key.clone(), now));
if *last != key {
@@ -2828,7 +2910,16 @@ fn overlay(app: &mut SoundApp, ui: &mut Ui, tl: Rect, area: Rect) {
let mode_follow = matches!(s.edit.scrolling.as_str(), "page" | "continuous" | "center");
let visible = x >= tl.min.x && x <= tl.max.x;
let layout = &mut app.edit_layout;
let (hold, follow_to) = follow_update(true, mode_follow, visible, cur, layout.last_scroll, layout.last_follow_to, layout.follow_hold, pos);
let (hold, follow_to) = follow_update(FollowFrame {
playing: true,
mode_follow,
playhead_visible: visible,
cur,
last_scroll: layout.last_scroll,
last_follow_to: layout.last_follow_to,
hold: layout.follow_hold,
pos,
});
layout.follow_hold = hold;
layout.last_scroll = cur;
layout.last_follow_to = follow_to;
@@ -2939,7 +3030,84 @@ mod universe_tests {
#[cfg(test)]
mod tests {
use super::*;
use egui_kittest::Harness;
use egui::accesskit::Role;
use egui_kittest::{Harness, kittest::Queryable};
/// The painted header selectors are interactive controls; their accessible name is the field
/// they edit, not the short value text ("Read", "Waveform") painted inside them.
#[test]
fn track_header_selectors_have_accessible_names() {
let mut h = Harness::builder().with_context_setup(crate::fonts::install).build_ui(|ui| {
let view = Rect::from_min_size(Pos2::ZERO, vec2(60.0, 17.0));
super::header_selector(ui, view, egui::Id::unique("test-view"), "Waveform", Color32::WHITE, true, "Track view");
let mode = Rect::from_min_size(pos2(0.0, 20.0), vec2(60.0, 17.0));
super::header_selector(ui, mode, egui::Id::unique("test-mode"), "Read", Color32::WHITE, true, "Automation mode");
});
h.run();
assert!(h.query_by_role_and_label(Role::ComboBox, "Track view").is_some());
assert!(h.query_by_role_and_label(Role::ComboBox, "Automation mode").is_some());
}
/// Repeated per-track selectors and toggles must name their track and publish their current
/// value; a screen reader otherwise sees several identical "Solo" or "Track view" controls.
#[test]
fn repeated_track_controls_are_named_and_valued_per_track() {
let mut h = harness_with(soundcraft_engine::demo::demo_engine());
for name in ["Kick", "Snare"] {
let view_label = format!("Track view for {name}");
let view = h.query_by_role_and_label(Role::ComboBox, &view_label).unwrap_or_else(|| panic!("missing track view for {name}"));
assert_eq!(view.value().as_deref(), Some("waveform"), "track view value for {name}");
let voice_label = format!("Voice for {name}");
let voice = h.query_by_role_and_label(Role::ComboBox, &voice_label).unwrap_or_else(|| panic!("missing voice for {name}"));
assert_eq!(voice.value().as_deref(), Some("dyn"), "voice value for {name}");
let mode_label = format!("Automation mode for {name}");
let mode = h.query_by_role_and_label(Role::ComboBox, &mode_label).unwrap_or_else(|| panic!("missing automation mode for {name}"));
assert_eq!(mode.value().as_deref(), Some("read"), "automation value for {name}");
for label in ["Record enable", "Input monitoring", "Solo", "Mute"] {
assert!(h.query_by_label(&format!("{label} for {name}")).is_some(), "missing {label} for {name}");
}
}
assert!(h.query_all_by_label_contains("Track view for").count() >= 2, "both visible tracks expose their own control");
// The Mix window repeats the same controls per strip; those names must be track-qualified
// too, and the automation selector must publish its value rather than only a tooltip.
h.state_mut().ui.window = crate::MainWindow::Mix;
h.run_steps(5);
for name in ["Kick", "Snare"] {
let mode_label = format!("Automation mode for {name}");
let mode = h.query_by_role_and_label(Role::ComboBox, &mode_label).unwrap_or_else(|| panic!("missing mix automation mode for {name}"));
assert_eq!(mode.value().as_deref(), Some("auto read"), "mix automation value for {name}");
for label in ["Input", "Output", "Group", "Record enable", "Input monitoring", "Solo", "Mute", "Insert A", "Send A"] {
assert!(h.query_by_label(&format!("{label} for {name}")).is_some(), "missing mix {label} for {name}");
}
}
// A used send's level is another per-track control; it must name its track too.
assert!(h.query_by_label("Send A level for Snare").is_some(), "missing mix send A level for Snare");
}
/// Every track's comments editor carries its own name, so label queries and screen readers can
/// tell two tracks apart instead of seeing several identical "Comments" fields.
#[test]
fn every_track_comments_field_is_named_for_its_track() {
let engine = soundcraft_engine::demo::demo_engine();
let tracks: Vec<Track> = engine.session().tracks.iter().take(2).cloned().collect();
assert_eq!(tracks.len(), 2);
let names: Vec<String> = tracks.iter().map(|t| format!("Comments for {}", t.name)).collect();
let app = SoundApp::new(engine, None, crate::Services::default());
let mut h = Harness::new_ui_state(
move |ui, app: &mut SoundApp| {
for (i, track) in tracks.iter().enumerate() {
let r = Rect::from_min_size(pos2(i as f32 * 200.0, 0.0), vec2(180.0, 80.0));
header_column(app, ui, track, "comments", r, Color32::BLACK);
}
},
app,
);
h.run();
for name in &names {
assert!(h.query_by_label(name).is_some(), "missing comments field {name}");
}
}
#[test]
fn automation_id_round_trips_for_high_slots() {
@@ -2955,40 +3123,160 @@ mod tests {
#[test]
fn follow_jumps_to_offscreen_playhead_while_playing() {
// Steady view, follow mode, playhead left the screen: jump to it.
assert_eq!(follow_update(true, true, false, 1_000, 1_000, None, false, 96_000), (false, Some(96_000)));
assert_eq!(
follow_update(FollowFrame {
playing: true,
mode_follow: true,
playhead_visible: false,
cur: 1_000,
last_scroll: 1_000,
last_follow_to: None,
hold: false,
pos: 96_000
}),
(false, Some(96_000))
);
}
#[test]
fn follow_leaves_onscreen_playhead_alone() {
assert_eq!(follow_update(true, true, true, 1_000, 1_000, None, false, 96_000), (false, None));
assert_eq!(
follow_update(FollowFrame {
playing: true,
mode_follow: true,
playhead_visible: true,
cur: 1_000,
last_scroll: 1_000,
last_follow_to: None,
hold: false,
pos: 96_000
}),
(false, None)
);
}
#[test]
fn follow_never_scrolls_when_stopped_nor_in_free_modes() {
assert_eq!(follow_update(false, true, false, 1_000, 1_000, None, false, 96_000), (false, None));
assert_eq!(
follow_update(FollowFrame {
playing: false,
mode_follow: true,
playhead_visible: false,
cur: 1_000,
last_scroll: 1_000,
last_follow_to: None,
hold: false,
pos: 96_000
}),
(false, None)
);
// `none`/`after_playback` never follow, even with a steady view.
assert_eq!(follow_update(true, false, false, 1_000, 1_000, None, false, 96_000), (false, None));
assert_eq!(follow_update(true, false, false, 9_000, 1_000, None, false, 96_000), (false, None));
assert_eq!(
follow_update(FollowFrame {
playing: true,
mode_follow: false,
playhead_visible: false,
cur: 1_000,
last_scroll: 1_000,
last_follow_to: None,
hold: false,
pos: 96_000
}),
(false, None)
);
assert_eq!(
follow_update(FollowFrame {
playing: true,
mode_follow: false,
playhead_visible: false,
cur: 9_000,
last_scroll: 1_000,
last_follow_to: None,
hold: false,
pos: 96_000
}),
(false, None)
);
}
#[test]
fn outside_view_move_pauses_the_follow() {
// The view moved and it was not our own jump: pause, no jump.
assert_eq!(follow_update(true, true, false, 9_000, 1_000, None, false, 96_000), (true, None));
assert_eq!(
follow_update(FollowFrame {
playing: true,
mode_follow: true,
playhead_visible: false,
cur: 9_000,
last_scroll: 1_000,
last_follow_to: None,
hold: false,
pos: 96_000
}),
(true, None)
);
// A held view stays held while it sits still elsewhere.
assert_eq!(follow_update(true, true, false, 9_000, 9_000, None, true, 96_000), (true, None));
assert_eq!(
follow_update(FollowFrame {
playing: true,
mode_follow: true,
playhead_visible: false,
cur: 9_000,
last_scroll: 9_000,
last_follow_to: None,
hold: true,
pos: 96_000
}),
(true, None)
);
}
#[test]
fn own_follow_jump_is_not_mistaken_for_a_takeover() {
// The scroll equals the target we issued last frame: keep following.
assert_eq!(follow_update(true, true, false, 96_000, 1_000, Some(96_000), false, 97_000), (false, Some(97_000)));
assert_eq!(
follow_update(FollowFrame {
playing: true,
mode_follow: true,
playhead_visible: false,
cur: 96_000,
last_scroll: 1_000,
last_follow_to: Some(96_000),
hold: false,
pos: 97_000
}),
(false, Some(97_000))
);
}
#[test]
fn hold_clears_when_playhead_returns_or_transport_stops() {
assert_eq!(follow_update(true, true, true, 9_000, 9_000, None, true, 96_000), (false, None));
assert_eq!(follow_update(false, true, false, 9_000, 9_000, None, true, 96_000), (false, None));
assert_eq!(
follow_update(FollowFrame {
playing: true,
mode_follow: true,
playhead_visible: true,
cur: 9_000,
last_scroll: 9_000,
last_follow_to: None,
hold: true,
pos: 96_000
}),
(false, None)
);
assert_eq!(
follow_update(FollowFrame {
playing: false,
mode_follow: true,
playhead_visible: false,
cur: 9_000,
last_scroll: 9_000,
last_follow_to: None,
hold: true,
pos: 96_000
}),
(false, None)
);
}
fn harness(n: usize) -> Harness<'static, SoundApp> {
@@ -4106,13 +4394,14 @@ mod tests {
assert!(s.edit.selection.end > pointed, "the selection did not follow the scroll");
}
/// A pass redrawn by egui must still contribute one frame-time sample, measured from the host clock rather than
/// UI input time.
#[test]
fn a_redrawn_frame_counts_once_in_the_frame_time() {
let mut h = harness(4);
let mut t = 1.0;
let mut frame = |h: &mut Harness<SoundApp>| {
t += 1.0 / 60.0;
h.input_mut().time = Some(t);
// The monotonic host clock, not the UI input time, drives frame statistics.
let frame = |h: &mut Harness<SoundApp>| {
h.state_mut().advance_test_clock(std::time::Duration::from_secs_f64(1.0 / 60.0));
h.step();
};
for _ in 0..120 {
+22 -22
View File
@@ -90,7 +90,7 @@ fn renderer(app: &mut SoundApp, ctx: &egui::Context) {
}
let t = Tokens::current();
let fmt = app.engine.session().main_format();
let peaks: Vec<f32> = app.player.as_ref().map(|p| p.meters().main.peaks).unwrap_or_default();
let peaks: Vec<f32> = if app.player.is_some() { app.meter_snapshot.main.peaks.clone() } else { Vec::new() };
let objects: Vec<(String, f32, f32, f32)> = app
.engine
.session()
@@ -145,6 +145,7 @@ fn renderer(app: &mut SoundApp, ctx: &egui::Context) {
app.extra.show_renderer = open;
}
/// Appearance window: interface scale, reduce-motion, theme, auto-update configuration and Reset.
fn ui_customization(app: &mut SoundApp, ctx: &egui::Context) {
let mut open = app.extra.show_ui_customization;
if !open {
@@ -153,30 +154,29 @@ fn ui_customization(app: &mut SoundApp, ctx: &egui::Context) {
egui::Window::new(egui::RichText::new("Appearance").font(bold(13.0))).open(&mut open).default_size(vec2(320.0, 180.0)).resizable(false).show(
ctx,
|ui| {
egui::Grid::new("ui_custom").num_columns(2).spacing(vec2(12.0, 8.0)).show(ui, |ui| {
ui.label("Interface scale");
ui.add(egui::Slider::new(&mut app.extra.ui_scale, 0.75..=1.5).step_by(0.05).custom_formatter(|v, _| format!("{:.0} %", v * 100.0)));
ui.end_row();
ui.label("Reduce motion");
crate::widgets::checkbox(ui, &mut app.extra.reduce_motion, "");
ui.end_row();
ui.label("Theme");
let mut chosen = None;
egui::ComboBox::from_id_salt("theme_mode").selected_text(app.ui.theme.label()).show_ui(ui, |ui| {
for mode in crate::theme::ThemeMode::ALL {
if ui.selectable_label(app.ui.theme == mode, mode.label()).clicked() {
chosen = Some(mode);
}
// Each control carries its own visible label, which is also its accessible name. A
// label in a neighbouring grid cell is only painted text: a screen reader (and the
// kittest accessibility check) would see an unnamed slider, checkbox or combo box.
ui.add(
egui::Slider::new(&mut app.extra.ui_scale, 0.75..=1.5)
.step_by(0.05)
.custom_formatter(|v, _| format!("{:.0} %", v * 100.0))
.text("Interface scale"),
);
crate::widgets::checkbox(ui, &mut app.extra.reduce_motion, "Reduce motion");
let mut chosen = None;
egui::ComboBox::from_label("Theme").selected_text(app.ui.theme.label()).show_ui(ui, |ui| {
for mode in crate::theme::ThemeMode::ALL {
if ui.selectable_label(app.ui.theme == mode, mode.label()).clicked() {
chosen = Some(mode);
}
});
if let Some(mode) = chosen {
let _ = app.run("ui.theme", json!({"mode": mode.id()}));
}
ui.end_row();
ui.label("Auto-update configuration");
crate::widgets::checkbox(ui, &mut app.extra.auto_update_config, "");
ui.end_row();
});
if let Some(mode) = chosen {
let _ = app.run("ui.theme", json!({"mode": mode.id()}));
}
crate::widgets::checkbox(ui, &mut app.extra.auto_update_config, "Auto-update configuration");
ui.add_space(4.0);
if ui.button("Reset").clicked() {
app.extra.ui_scale = 1.0;
app.extra.reduce_motion = false;
+23
View File
@@ -151,6 +151,29 @@ mod tests {
assert!(HANGUL.iter().filter(|(p, _)| p.ends_with("NotoSansCJK-Regular.ttc")).all(|(_, i)| *i == 1));
}
/// Every literal glyph the UI fixtures paint is covered by the bundled faces in every family,
/// without relying on an installed system font. This is the 0.37 font-feature regression test.
#[test]
fn bundled_faces_cover_the_ui_glyphs_in_every_family() {
let mut defs = FontDefinitions::default();
let proportional = defs.families.get(&FontFamily::Proportional).cloned().unwrap_or_default();
defs.families.insert(FontFamily::Name("bold".into()), proportional);
let ctx = egui::Context::default();
ctx.set_fonts(defs);
let mut output = ctx.run_ui(egui::RawInput::default(), |_| {});
output.textures_delta.clear();
// Status/menu text, transport/meter symbols, file/audio/search icons, music symbols and
// platform key hints used by the UI. Close/delete controls paint a cross in `icons.rs`
// instead of typesetting U+2715, which no bundled face covers in every family.
let glyphs = ["›", "v", "♩", "𝄞", "⌘", "📁", "🔊", "🔍", "⬆", "−", "∞"];
for family in [FontFamily::Proportional, FontFamily::Monospace, FontFamily::Name("bold".into())] {
let font = egui::FontId::new(13.0, family.clone());
for glyph in glyphs {
assert!(ctx.fonts_mut(|f| f.has_glyphs(&font, glyph)), "{family:?} has no glyph for {glyph:?}");
}
}
}
/// Korean names draw in every family, not as boxes; skipped when the system has no Korean font.
#[test]
fn every_family_draws_hangul_with_a_system_korean_font() {
+5
View File
@@ -143,6 +143,11 @@ pub fn draw(painter: &Painter, r: Rect, name: &str, c: Color32) {
painter.line_segment([c0, c1], Stroke::new(s.width * 1.4, c));
}
}
"close" => {
// A code-drawn close cross: no bundled UI font has U+2715 in every family.
painter.line_segment([p(r, 0.3, 0.3), p(r, 0.7, 0.7)], s);
painter.line_segment([p(r, 0.7, 0.3), p(r, 0.3, 0.7)], s);
}
"lock" => {
painter.rect_filled(Rect::from_min_max(p(r, 0.22, 0.46), p(r, 0.78, 0.88)), r.width() * 0.08, c);
let (x, y, w) = (r.center().x, r.min.y + r.height() * 0.46, r.width() * 0.19);
+235
View File
@@ -0,0 +1,235 @@
//! Import Audio reads and decodes on a worker thread, so the app keeps drawing and stays usable
//! while a long file loads (issue #597). When the audio is ready it is handed to the engine
//! (`Engine::offer_import`) and `file.import_audio` runs as before, now only placing the clip:
//! undo, the journal, the CLI, MCP and the control channel see the same command, and calls from
//! the control channel still run synchronously.
use crate::SoundApp;
use serde_json::Value;
use std::collections::VecDeque;
/// Imports waiting and the one decoding now. One file decodes at a time: each can take gigabytes.
#[derive(Default)]
pub struct Imports {
queue: VecDeque<Value>,
#[cfg(not(target_arch = "wasm32"))]
running: Option<Running>,
}
#[cfg(not(target_arch = "wasm32"))]
struct Running {
params: Value,
name: String,
/// The session rate the audio is decoded for.
rate: u32,
rx: std::sync::mpsc::Receiver<Result<soundcraft_engine::io::DecodedImport, String>>,
}
impl Imports {
/// Whether a file is decoding or waiting.
pub fn busy(&self) -> bool {
#[cfg(not(target_arch = "wasm32"))]
if self.running.is_some() {
return true;
}
!self.queue.is_empty()
}
/// The file decoding now and how many wait after it.
fn label(&self) -> Option<(String, usize)> {
#[cfg(not(target_arch = "wasm32"))]
if let Some(r) = &self.running {
return Some((r.name.clone(), self.queue.len()));
}
None
}
}
/// 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.
pub fn start(app: &mut SoundApp, params: Value) {
if cfg!(target_arch = "wasm32") || params.get("path").and_then(Value::as_str).is_none() {
run_now(app, params);
return;
}
app.imports.queue.push_back(params);
}
/// Starts the next import, and places a finished one. Called every frame from `logic`.
pub fn step(app: &mut SoundApp, ctx: &egui::Context) {
#[cfg(not(target_arch = "wasm32"))]
{
finish(app);
if app.imports.running.is_none()
&& let Some(params) = app.imports.queue.pop_front()
{
spawn(app, params, ctx);
}
}
#[cfg(target_arch = "wasm32")]
{
let _ = ctx;
while let Some(params) = app.imports.queue.pop_front() {
run_now(app, params);
}
}
}
#[cfg(not(target_arch = "wasm32"))]
fn spawn(app: &mut SoundApp, params: Value, ctx: &egui::Context) {
let path = params.get("path").and_then(Value::as_str).unwrap_or_default().to_string();
let name = std::path::Path::new(&path).file_name().map_or_else(|| path.clone(), |n| n.to_string_lossy().into_owned());
let rate = app.engine.session().sample_rate.hz();
let (tx, rx) = std::sync::mpsc::channel();
let ctx = ctx.clone();
let work = move || {
let r = soundcraft_engine::io::decode_import(&path, rate).map_err(|e| e.to_string());
let _ = tx.send(r);
ctx.request_repaint();
};
match std::thread::Builder::new().name("audio-import".into()).spawn(work) {
// No status line here: it may hold the previous file's failure, and the progress note names this file.
Ok(_) => app.imports.running = Some(Running { params, name, rate, rx }),
// No thread to spare: import the old way rather than not at all.
Err(_) => {
run_now(app, params);
}
}
}
#[cfg(not(target_arch = "wasm32"))]
fn finish(app: &mut SoundApp) {
let Some(r) = &app.imports.running else { return };
let done = match r.rx.try_recv() {
Ok(done) => done,
Err(std::sync::mpsc::TryRecvError::Empty) => return,
Err(std::sync::mpsc::TryRecvError::Disconnected) => Err(format!("{}: the import stopped", r.name)),
};
let Some(Running { params, name, rate, .. }) = app.imports.running.take() else { return };
// The session changed rate meanwhile (another session was opened): decode again for it.
if rate != app.engine.session().sample_rate.hz() {
app.imports.queue.push_front(params);
return;
}
match done {
Ok(decoded) => {
app.engine.offer_import(decoded);
if run_now(app, params) {
app.ui.status = format!("Imported {name}");
}
app.engine.drop_offered_imports();
}
Err(e) => app.ui.status = e,
}
}
/// Runs `file.import_audio` on this thread; the status line shows a failure. True on success.
fn run_now(app: &mut SoundApp, params: Value) -> bool {
match app.run("file.import_audio", params) {
Ok(_) => true,
Err(e) => {
app.ui.status = e;
false
}
}
}
/// A small, non-blocking note while files import: the rest of the app stays usable.
pub fn show(app: &SoundApp, ctx: &egui::Context) {
let Some((name, waiting)) = app.imports.label() else { return };
egui::Area::new(egui::Id::unique("audio_import_progress")).anchor(egui::Align2::RIGHT_BOTTOM, egui::vec2(-16.0, -40.0)).interactable(false).show(
ctx,
|ui| {
egui::Frame::popup(ui.style()).show(ui, |ui| {
ui.horizontal(|ui| {
ui.add(egui::Spinner::new());
let more = if waiting > 0 { format!(" ({waiting} more)") } else { String::new() };
ui.label(format!("Importing {name}{more}"));
});
});
},
);
}
#[cfg(test)]
mod tests {
use super::*;
use crate::{Services, SoundApp};
use serde_json::json;
fn tone_wav(name: &str) -> String {
let dir = std::env::temp_dir().join(format!("sc-ui-import-{}", std::process::id()));
std::fs::create_dir_all(&dir).unwrap();
let tone: Vec<f32> = (0..9_600).map(|i| (i as f32 * 0.05).sin() * 0.2).collect();
let buf = soundcraft_audio_io::AudioBuffer { sample_rate: 48_000, channels: vec![tone.clone(), tone] };
let bytes = soundcraft_audio_io::encode(&buf, &soundcraft_audio_io::EncodeOptions::default()).unwrap();
let path = dir.join(name);
std::fs::write(&path, bytes).unwrap();
path.to_string_lossy().into_owned()
}
fn settle(app: &mut SoundApp, ctx: &egui::Context) {
for _ in 0..2_000 {
step(app, ctx);
if !app.imports.busy() {
return;
}
std::thread::sleep(std::time::Duration::from_millis(5));
}
panic!("import never finished");
}
/// Issue #597: starting an import returns at once; the clip lands, as one undo step, later.
#[test]
fn import_runs_in_the_background_and_places_one_undoable_clip() {
let ctx = egui::Context::default();
let mut app = SoundApp::new(soundcraft_engine::Engine::default(), None, Services::default());
let wav = tone_wav("bg.wav");
start(&mut app, json!({"path": wav, "at": 480}));
assert!(app.imports.busy());
assert!(app.engine.session().tracks.is_empty(), "nothing is placed before the audio is ready");
settle(&mut app, &ctx);
let s = app.engine.session();
assert_eq!(s.tracks.len(), 1);
assert_eq!(s.tracks[0].clips()[0].start, 480);
assert_eq!(s.sources[0].frames, 9_600);
assert!(app.ui.status.starts_with("Imported bg.wav"), "{}", app.ui.status);
app.run("edit.undo", json!({})).unwrap();
assert!(app.engine.session().tracks.is_empty());
}
#[test]
fn imports_queue_and_a_bad_file_reports_without_stopping_the_rest() {
let ctx = egui::Context::default();
let mut app = SoundApp::new(soundcraft_engine::Engine::default(), None, Services::default());
let junk = tone_wav("junk.wav");
std::fs::write(&junk, b"not audio").unwrap();
let good = tone_wav("good.wav");
start(&mut app, json!({"path": junk}));
start(&mut app, json!({"path": good}));
step(&mut app, &ctx);
assert_eq!(app.imports.label().map(|l| l.1), Some(1), "the second waits for the first");
settle(&mut app, &ctx);
assert_eq!(app.engine.session().tracks.len(), 1, "the good file still imports");
}
#[test]
fn a_failed_import_stays_on_the_status_line_while_the_next_one_starts() {
let ctx = egui::Context::default();
let mut app = SoundApp::new(soundcraft_engine::Engine::default(), None, Services::default());
let junk = tone_wav("junk-status.wav");
std::fs::write(&junk, b"not audio").unwrap();
start(&mut app, json!({"path": junk}));
start(&mut app, json!({"path": tone_wav("next-status.wav")}));
for _ in 0..2_000 {
step(&mut app, &ctx);
if app.imports.label().is_some_and(|(name, _)| name == "next-status.wav") {
break;
}
std::thread::sleep(std::time::Duration::from_millis(5));
}
assert!(app.imports.label().is_some_and(|(name, _)| name == "next-status.wav"), "the second import never started");
assert!(!app.ui.status.is_empty() && !app.ui.status.starts_with("Importing"), "the failure was overwritten: {}", app.ui.status);
settle(&mut app, &ctx);
}
}
+1687 -50
View File
File diff suppressed because it is too large. Load diff
+24 -5
View File
@@ -7,6 +7,7 @@ use soundcraft_engine::catalog;
/// UI-layer commands: (id, label, catalog path or "", shortcut).
pub const UI_COMMANDS: &[(&str, &str, &str, Option<&str>)] = &[
("app.checkForUpdates", "Check for Updates…", "", None),
("ui.dock", "Arrange Panel", "", None),
("ui.dock.reset", "Reset Panel Layout", "Window > Reset Panel Layout", None),
("window.mix", "Mix", "Window > Mix", Some("Cmd+=")),
@@ -128,10 +129,10 @@ pub const AUDIOSUITE: &[(&str, &str)] = &[
("AudioSuite > Pitch Shift > Pitch II", "pitch_shift"),
("AudioSuite > Pitch Shift > Pitch Shift Legacy", "pitch_shift"),
("AudioSuite > Pitch Shift > Time Shift", "time_stretch"),
("AudioSuite > Pitch Shift > Vari-Fi", "varispeed"),
("AudioSuite > Pitch Shift > Vari-Fi", "speed_ramp"),
("AudioSuite > Reverb > D-Verb", "room_reverb"),
("AudioSuite > Delay > Mod Delay III", "mod_delay"),
("AudioSuite > Modulation > Sci-Fi", "flanger"),
("AudioSuite > Modulation > Sci-Fi", "ring_mod"),
("AudioSuite > Harmonic > Eleven Lite", "saturator"),
("AudioSuite > Harmonic > Lo-Fi", "lofi"),
("AudioSuite > Harmonic > Recti-Fi", "rectifier"),
@@ -166,7 +167,9 @@ const DIALOG_COMMANDS: &[&str] = &[
"event.quantize",
"event.transpose",
"edit.strip_silence",
"clip.auto_rename",
"clip.rename",
"clip.loop",
];
/// Shared menu hierarchy for egui and native desktop hosts.
@@ -230,6 +233,9 @@ pub fn menu_bar(app: &mut SoundApp, ui: &mut egui::Ui) {
if ui.button("About SoundCraft").clicked() {
app.ui.show_about = true;
}
if ui.button("Check for Updates…").clicked() {
let _ = app.run("app.checkForUpdates", json!({}));
}
if ui.button("Session Info").clicked() {
app.ui.show_session_info = true;
}
@@ -504,7 +510,7 @@ fn wants_dialog(id: &str) -> bool {
/// Handle UI-layer commands. Returns None when `id` is not a UI command.
pub fn run_ui_command(app: &mut SoundApp, id: &str, p: &Value) -> Option<Result<Value, String>> {
if matches!(id, "ui.dock" | "ui.dock.reset") {
return Some(crate::docking::command(app, id == "ui.dock.reset", p));
return Some(crate::docking::command(app, id, p));
}
if let Some(v) = crate::extra_windows::run(app, id, p) {
return Some(Ok(v));
@@ -517,6 +523,10 @@ pub fn run_ui_command(app: &mut SoundApp, id: &str, p: &Value) -> Option<Result<
json!({"value": *v})
};
let r = match id {
"app.checkForUpdates" | "help.checkForUpdates" => {
crate::updates_ui::check(app);
json!({})
}
"window.mix" => {
app.ui.window = MainWindow::Mix;
app.focus_mix = app.ui.mix_separate;
@@ -591,7 +601,7 @@ pub fn run_ui_command(app: &mut SoundApp, id: &str, p: &Value) -> Option<Result<
}
"window.configurations" => toggle(&mut app.ui.show_configurations),
"window.config_new" | "window.config_update" => {
if let Err(error) = app.ui.docking.validate().and_then(|()| app.ui.mix_docking.validate_mix()) {
if let Err(error) = crate::docking::validate(&app.ui.docking).and_then(|()| crate::docking::validate_mix(&app.ui.mix_docking)) {
return Some(Err(error));
}
let mut snap = app.ui.clone();
@@ -687,7 +697,7 @@ pub fn run_ui_command(app: &mut SoundApp, id: &str, p: &Value) -> Option<Result<
}
match serde_json::from_value::<crate::UiState>(cur) {
Ok(ns) => {
if let Err(error) = ns.docking.validate().and_then(|()| ns.mix_docking.validate_mix()) {
if let Err(error) = crate::docking::validate(&ns.docking).and_then(|()| crate::docking::validate_mix(&ns.mix_docking)) {
return Some(Err(error));
}
crate::docking::cancel_replaced_state(app, &ns);
@@ -772,4 +782,13 @@ mod tests {
e.execute("edit.select", &json!({"tracks": [audio, inst]})).unwrap();
assert_eq!(instrument_track(e.session()).map(|t| t.0), Some(inst));
}
#[test]
fn audiosuite_sci_fi_opens_the_ring_modulator() {
let mut app = SoundApp::new(soundcraft_engine::Engine::default(), None, Services::default());
invoke_menu(&mut app, "audiosuite.process", "AudioSuite > Modulation > Sci-Fi");
assert_eq!(app.ui.audiosuite.as_deref(), Some("ring_mod"));
let info = soundcraft_dsp::plugin_info("ring_mod").unwrap();
assert!(info.audiosuite && info.category == soundcraft_dsp::Category::Modulation);
}
}
+38 -5
View File
@@ -21,6 +21,19 @@ pub struct MidiEditorState {
pub band: Option<egui::Pos2>,
}
impl MidiEditorState {
/// Start editing `id`. Note indices only mean something inside one clip, so the selection and
/// any gesture in progress are dropped when the clip changes.
fn switch_clip(&mut self, id: ClipId) {
if self.clip != Some(id) {
self.selected.clear();
self.drag = None;
self.band = None;
}
self.clip = Some(id);
}
}
#[derive(Debug, Clone)]
pub struct NoteDrag {
pub index: usize,
@@ -55,6 +68,7 @@ fn is_black(p: i32) -> bool {
matches!(p.rem_euclid(12), 1 | 3 | 6 | 8 | 10)
}
/// Draw the MIDI editor for the target clip, with an empty-state prompt that adds a clip on the selected MIDI track.
pub fn show(app: &mut SoundApp, ui: &mut Ui) {
// Painter-only contents do not advance the layout. Claim the resized
// space, including the empty state, so the dock keeps its chosen height.
@@ -77,7 +91,7 @@ pub fn show(app: &mut SoundApp, ui: &mut Ui) {
ui.painter().text(full.center(), Align2::CENTER_CENTER, msg, regular(13.0), t.text_dim);
let body = Rect::from_min_max(pos2(full.min.x, header.max.y), full.max);
if let Some((id, _)) = track
&& ui.interact(body, ui.id().with("new_clip"), Sense::click()).clicked()
&& ui.interact(body, ui.scope_id().with("new_clip"), Sense::click()).clicked()
{
let sel = app.engine.session().edit.selection;
if let Ok(v) = app.run("midi.new_clip", json!({"track": id.0, "start": sel.start, "end": sel.end})) {
@@ -148,7 +162,7 @@ pub fn show(app: &mut SoundApp, ui: &mut Ui) {
let hi = sequence.notes.iter().map(|n| i32::from(n.pitch)).max().unwrap_or(72);
let lo = sequence.notes.iter().map(|n| i32::from(n.pitch)).min().unwrap_or(48);
app.midi.scroll = (127 - (hi + lo) / 2 - rows / 2) as f32 * ROW_H;
app.midi.clip = Some(cid);
app.midi.switch_clip(cid);
}
if ui.rect_contains_pointer(roll) {
app.midi.scroll -= ui.input(|i| i.smooth_scroll_delta.y);
@@ -221,7 +235,7 @@ pub fn show(app: &mut SoundApp, ui: &mut Ui) {
painter.rect(r, 2.0, fill, Stroke::new(1.0, Color32::BLACK), StrokeKind::Inside);
}
// Interaction.
let resp = ui.interact(roll, ui.id().with(("roll", cid.0)), Sense::click_and_drag());
let resp = ui.interact(roll, ui.scope_id().with(("roll", cid.0)), Sense::click_and_drag());
let hit = |pos: egui::Pos2| -> Option<(usize, bool)> {
sequence.notes.iter().enumerate().rev().find_map(|(i, n)| {
let r = Rect::from_min_max(
@@ -346,7 +360,10 @@ pub fn show(app: &mut SoundApp, ui: &mut Ui) {
0
};
if (semi != 0 || ticks != 0) && !app.midi.selected.is_empty() {
let _ = app.run("midi.notes_move", json!({"clip": cid.0, "indices": app.midi.selected, "ticks": ticks, "semitones": semi}));
let moved = app.run("midi.notes_move", json!({"clip": cid.0, "indices": app.midi.selected, "ticks": ticks, "semitones": semi}));
// The engine re-sorts the notes: follow the moved ones to their new indices, so the
// selection never points at other notes and the arrows can keep nudging it.
app.midi.selected = moved.ok().and_then(|v| serde_json::from_value(v["indices"].clone()).ok()).unwrap_or_default();
}
}
if !app.midi.selected.is_empty()
@@ -359,7 +376,7 @@ pub fn show(app: &mut SoundApp, ui: &mut Ui) {
// Velocity lane.
ui.painter().rect_filled(vel, 0.0, Color32::from_rgb(28, 28, 30));
ui.painter().text(pos2(full.min.x + 6.0, vel.center().y), Align2::LEFT_CENTER, "Velocity", regular(10.0), t.text_dim);
let vresp = ui.interact(vel, ui.id().with(("vel", cid.0)), Sense::click_and_drag());
let vresp = ui.interact(vel, ui.scope_id().with(("vel", cid.0)), Sense::click_and_drag());
for (i, n) in sequence.notes.iter().enumerate() {
let x = x_of(n.start);
let h = (vel.height() - 4.0) * f32::from(n.velocity) / 127.0;
@@ -376,3 +393,19 @@ pub fn show(app: &mut SoundApp, ui: &mut Ui) {
let _ = app.engine.execute_merged("midi.note_edit", &json!({"clip": cid.0, "index": i, "velocity": v}), &format!("vel:{}", cid.0));
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn switching_clip_drops_selection_and_gestures() {
let mut st = MidiEditorState { selected: vec![0, 2], clip: Some(ClipId(1)), band: Some(pos2(1.0, 2.0)), ..Default::default() };
st.switch_clip(ClipId(1));
assert_eq!(st.selected, vec![0, 2]);
st.switch_clip(ClipId(2));
assert!(st.selected.is_empty());
assert!(st.drag.is_none() && st.band.is_none());
assert_eq!(st.clip, Some(ClipId(2)));
}
}
+145 -57
View File
@@ -3,8 +3,8 @@
use crate::SoundApp;
use crate::theme::{Tokens, bold, regular, rgb};
use crate::widgets::{
PannerEdit, PannerSpeaker, db_text, fader, fader_slot, groove, inset, meter, meter_scale, multi_meter, pan_knob, pan_text, raised_key,
rec_toggle, selector_box, selector_box_with_tooltip, shade, surround_panner, text_toggle, vgradient,
PannerEdit, PannerSpeaker, SelectorLabels, db_text, fader, fader_slot, groove, inset, meter, meter_scale, multi_meter, pan_knob, pan_text,
raised_key, rec_toggle, selector_box_named, shade, surround_panner, text_toggle, vgradient,
};
use egui::{Align2, Color32, CornerRadius, Rect, Sense, Stroke, StrokeKind, Ui, pos2, vec2};
use serde_json::json;
@@ -43,10 +43,13 @@ pub fn separate_closed(app: &mut SoundApp) {
app.ui.window = crate::MainWindow::Mix;
}
/// Draw the horizontal Mix window strips for visible non-video tracks, scroll one requested strip into view and open
/// New Tracks when the space past the last strip is double-clicked.
pub(crate) fn content(app: &mut SoundApp, ui: &mut Ui) {
let ids: Vec<TrackId> = app.engine.session().tracks.iter().filter(|x| !x.hidden && x.kind != TrackKind::Video).map(|x| x.id).collect();
// Per-channel peaks for multichannel meters (read once per frame).
let snap = app.player.as_ref().filter(|_| app.meters_live()).map(|p| p.meters());
// Per-channel peaks for multichannel meters, from the snapshot `logic` takes from the player
// (taking them here too would split each period's peaks between two readers).
let snap = (app.player.is_some() && app.meters_live()).then(|| app.meter_snapshot.clone());
// Taken for this frame only: a strip that isn't drawn (hidden track) must not scroll the Mix
// whenever it comes back. A request made during this frame (the strip's menu) waits for the next.
let scroll_to = app.mix_scroll_into_view.take();
@@ -57,7 +60,7 @@ pub(crate) fn content(app: &mut SoundApp, ui: &mut Ui) {
strip(app, ui, id, snap.as_ref(), scroll_to == Some(id));
}
let rest = Rect::from_min_max(pos2(ui.cursor().min.x, ui.clip_rect().min.y), ui.clip_rect().max);
if rest.width() > 4.0 && ui.interact(rest, ui.id().with("past_strips"), Sense::click()).double_clicked() {
if rest.width() > 4.0 && ui.interact(rest, ui.scope_id().with("past_strips"), Sense::click()).double_clicked() {
let _ = app.dialogs.open_for_command(&app.engine, "track.new");
}
});
@@ -90,7 +93,7 @@ fn panner_speakers(fmt: ChannelFormat) -> Vec<PannerSpeaker> {
/// Ballistic per-channel meter levels for multichannel strips, kept in egui memory.
fn channel_levels(ui: &Ui, key: (&str, u64), peaks: &[f32], n: usize) -> Vec<f32> {
let id = egui::Id::new(key);
let id = egui::Id::unique(key);
let dt = ui.input(|i| i.stable_dt).clamp(0.0, 0.25);
let fall = 10f32.powf(-26.0 * dt / 20.0);
let mut lv: Vec<f32> = ui.data(|d| d.get_temp(id)).unwrap_or_default();
@@ -152,6 +155,8 @@ pub(crate) fn route_popup(app: &mut SoundApp, resp: &egui::Response, id: TrackId
egui::Popup::menu(resp).close_behavior(egui::PopupCloseBehavior::CloseOnClickOutside).show(|ui| route_menu(app, ui, id, input));
}
/// Draw one mixer channel strip: preamp, instrument, object, insert and send sections, EQ curve, routing, fader and
/// meters, with its interactions.
fn strip(app: &mut SoundApp, ui: &mut Ui, id: TrackId, snap: Option<&MeterSnapshot>, scroll_here: bool) {
let t = Tokens::current();
let session = app.engine.session_arc();
@@ -188,7 +193,9 @@ fn strip(app: &mut SoundApp, ui: &mut Ui, id: TrackId, snap: Option<&MeterSnapsh
let kr = Rect::from_min_size(pos2(x0 + 6.0, sec.min.y + 15.0), vec2(inner_w - 12.0, 16.0));
let mut db = track.mixer.trim_db;
let mut c = ui.new_child(egui::UiBuilder::new().max_rect(kr));
let resp = c.add_sized(kr.size(), egui::DragValue::new(&mut db).range(-24.0..=24.0).speed(0.1).suffix(" dB").max_decimals(1));
let resp = c
.add_sized(kr.size(), egui::DragValue::new(&mut db).range(-24.0..=24.0).speed(0.1).suffix(" dB").max_decimals(1))
.accessible_name("Input trim (dB)");
if resp.changed() {
let _ = app.engine.execute_merged("mix.trim", &json!({"tracks": [id.0], "db": db}), &format!("trim:{}", id.0));
}
@@ -282,7 +289,11 @@ fn strip(app: &mut SoundApp, ui: &mut Ui, id: TrackId, snap: Option<&MeterSnapsh
let mut c = track.comments.clone();
let mut child = ui.new_child(egui::UiBuilder::new().max_rect(sec));
let hint = if narrow { "" } else { "comments" };
if child.add(egui::TextEdit::multiline(&mut c).desired_width(inner_w).desired_rows(2).hint_text(hint).font(regular(10.0))).changed() {
let field = crate::widgets::named_text_field(
child.add(egui::TextEdit::multiline(&mut c).desired_width(inner_w).desired_rows(2).hint_text(hint).font(regular(10.0))),
&format!("Comments for {}", track.name),
);
if field.changed() {
let _ = app.engine.execute_merged("track.comments", &json!({"track": id.0, "comments": c}), &format!("comments:{}", id.0));
}
y = sec.max.y + 4.0;
@@ -294,12 +305,26 @@ fn strip(app: &mut SoundApp, ui: &mut Ui, id: TrackId, snap: Option<&MeterSnapsh
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 resp = selector_box(&mut c, in_r.width(), in_r.height(), &input_name(app, track), t.text);
resp.widget_info(|| egui::WidgetInfo::labeled(egui::WidgetType::ComboBox, true, "Input"));
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 resp = selector_box(&mut c, out_r.width(), out_r.height(), &route_name(app, &track.mixer.output), t.text);
resp.widget_info(|| egui::WidgetInfo::labeled(egui::WidgetType::ComboBox, true, "Output"));
let output = route_name(app, &track.mixer.output);
let resp = selector_box_named(
&mut c,
out_r.width(),
out_r.height(),
&output,
t.text,
SelectorLabels { name: &format!("Output for {}", track.name), value: &output, tooltip: "Output" },
);
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));
@@ -321,7 +346,14 @@ fn strip(app: &mut SoundApp, ui: &mut Ui, id: TrackId, snap: Option<&MeterSnapsh
} else {
&full_label
};
let resp = selector_box_with_tooltip(&mut c, am_r.width(), am_r.height(), label, col, &full_label);
let resp = selector_box_named(
&mut c,
am_r.width(),
am_r.height(),
label,
col,
SelectorLabels { name: &format!("Automation mode for {}", track.name), value: &full_label, tooltip: &full_label },
);
egui::Popup::menu(&resp).show(|ui| {
for m in soundcraft_model::AutomationMode::ALL {
if ui.selectable_label(m == mode, m.label()).clicked() {
@@ -336,7 +368,14 @@ fn strip(app: &mut SoundApp, ui: &mut Ui, id: TrackId, snap: Option<&MeterSnapsh
let gname = app.engine.session().groups.iter().find(|g| g.members.contains(&id)).map_or(no_group.to_string(), |g| g.name.clone());
let gr = Rect::from_min_size(pos2(x0 + 2.0, y), vec2(inner_w - 4.0, 16.0));
let mut c = ui.new_child(egui::UiBuilder::new().max_rect(gr));
let _ = selector_box(&mut c, gr.width(), gr.height(), &gname, t.text_dim);
let _ = selector_box_named(
&mut c,
gr.width(),
gr.height(),
&gname,
t.text_dim,
SelectorLabels { name: &format!("Group for {}", track.name), value: &gname, tooltip: "Group" },
);
y += 22.0;
// Pan: a surround panner when the output is multichannel, else the stereo knobs.
let out_fmt = output_format(app, track);
@@ -378,8 +417,18 @@ fn strip(app: &mut SoundApp, ui: &mut Ui, id: TrackId, snap: Option<&MeterSnapsh
let _ = resp;
let pr = Rect::from_center_size(pos2(cx, y + 42.0), vec2(ks + 8.0, 13.0));
inset(ui.painter(), pr, t.counter_bg);
let (_, entered) =
editable_readout(ui, pr, ui.id().with(("pan-value", id.0, i)), &pan_text(v), &format!("{:.0}", v * 100.0), parse_pan, 0.0);
let (_, entered) = editable_readout(
ui,
pr,
ui.scope_id().with(("pan-value", id.0, i)),
Readout {
name: &format!("{} pan {}", track.name, i + 1),
display: &pan_text(v),
edit_initial: &format!("{:.0}", v * 100.0),
parse: parse_pan,
reset: 0.0,
},
);
if let Some(v) = entered {
let _ = app.engine.execute_merged("mix.pan", &json!({"track": id.0, "pan": v, "index": i}), &format!("pan:{}:{i}", id.0));
}
@@ -393,10 +442,12 @@ fn strip(app: &mut SoundApp, ui: &mut Ui, id: TrackId, snap: Option<&MeterSnapsh
);
row.spacing_mut().item_spacing.x = 6.0;
if track.kind.has_playlist() {
if text_toggle(&mut row, vec2(bw, 18.0), "I", track.mixer.input_monitor, t.input, "Input monitoring").clicked() {
let input_label = format!("Input monitoring for {}", track.name);
if text_toggle(&mut row, vec2(bw, 18.0), "I", track.mixer.input_monitor, t.input, &input_label).clicked() {
let _ = app.run("mix.input_monitor", json!({"track": id.0}));
}
if rec_toggle(&mut row, vec2(bw, 18.0), track.mixer.record_arm, "Record enable").clicked() {
let record_label = format!("Record enable for {}", track.name);
if rec_toggle(&mut row, vec2(bw, 18.0), track.mixer.record_arm, &record_label).clicked() {
let _ = app.run("mix.record_arm", json!({"track": id.0}));
}
}
@@ -406,10 +457,12 @@ fn strip(app: &mut SoundApp, ui: &mut Ui, id: TrackId, snap: Option<&MeterSnapsh
);
row.spacing_mut().item_spacing.x = 6.0;
if track.kind != TrackKind::Master {
if text_toggle(&mut row, vec2(bw, 18.0), "S", track.mixer.solo, t.solo, "Solo").clicked() {
let solo_label = format!("Solo for {}", track.name);
if text_toggle(&mut row, vec2(bw, 18.0), "S", track.mixer.solo, t.solo, &solo_label).clicked() {
let _ = app.run("mix.solo", json!({"track": id.0}));
}
if text_toggle(&mut row, vec2(bw, 18.0), "M", track.mixer.mute, t.mute, "Mute").clicked() {
let mute_label = format!("Mute for {}", track.name);
if text_toggle(&mut row, vec2(bw, 18.0), "M", track.mixer.mute, t.mute, &mute_label).clicked() {
let _ = app.run("mix.mute", json!({"track": id.0}));
}
}
@@ -419,7 +472,7 @@ fn strip(app: &mut SoundApp, ui: &mut Ui, id: TrackId, snap: Option<&MeterSnapsh
let fh = (r.max.y - bottom_h - y).max(120.0);
let wide_meter = (if track.kind == TrackKind::Master { app.engine.session().main_format() } else { track.format }).channels() > 2;
let fr = Rect::from_min_size(pos2(x0, y), vec2(inner_w * if wide_meter { 0.45 } else { 0.6 }, fh));
if let Some(db) = fader(ui, fr, track.mixer.volume_db, ui.id().with(("fader", id.0))) {
if let Some(db) = fader(ui, fr, track.mixer.volume_db, ui.scope_id().with(("fader", id.0)), &format!("{} level", track.name)) {
app.gesture = Some(crate::Gesture::Fader { track: id, start_db: track.mixer.volume_db });
let _ = app.engine.execute_merged("mix.volume", &json!({"track": id.0, "db": db}), &format!("fader:{}", id.0));
}
@@ -454,11 +507,14 @@ fn strip(app: &mut SoundApp, ui: &mut Ui, id: TrackId, snap: Option<&MeterSnapsh
let (_, entered) = editable_readout(
ui,
vr,
ui.id().with(("volume-value", id.0)),
&db_text(track.mixer.volume_db),
&format!("{:.1}", track.mixer.volume_db),
parse_db,
0.0,
ui.scope_id().with(("volume-value", id.0)),
Readout {
name: &format!("{} volume", track.name),
display: &db_text(track.mixer.volume_db),
edit_initial: &format!("{:.1}", track.mixer.volume_db),
parse: parse_db,
reset: 0.0,
},
);
if let Some(db) = entered {
let _ = app.engine.execute_merged("mix.volume", &json!({"track": id.0, "db": db}), &format!("fader:{}", id.0));
@@ -483,7 +539,7 @@ fn strip(app: &mut SoundApp, ui: &mut Ui, id: TrackId, snap: Option<&MeterSnapsh
StrokeKind::Inside,
);
ui.painter().with_clip_rect(nr).text(nr.center(), Align2::CENTER_CENTER, &track.name, bold(12.0), t.text_dark);
let nresp = ui.interact(nr, ui.id().with(("strip_name", id.0)), Sense::click());
let nresp = ui.interact(nr, ui.scope_id().with(("strip_name", id.0)), Sense::click());
if nresp.clicked() {
crate::edit_window::click_track(app, id, ui.input(|i| i.modifiers), |_, t| !t.hidden && t.kind != TrackKind::Video);
}
@@ -505,8 +561,8 @@ pub(crate) fn instrument_slot(app: &mut SoundApp, ui: &mut Ui, track: &Track, r:
let (id, slot) = (track.id, soundcraft_mix::INSTRUMENT_SLOT);
let ins = track.instrument.as_ref();
let info = ins.and_then(|i| plugin_info(&i.plugin));
let resp = ui.interact(r, ui.id().with(("instrument", id.0)), Sense::click());
resp.widget_info(|| egui::WidgetInfo::labeled(egui::WidgetType::Button, true, "Instrument"));
let resp = ui.interact(r, ui.scope_id().with(("instrument", id.0)), Sense::click());
resp.widget_info(|| egui::WidgetInfo::labeled(egui::Role::Button, true, format!("Instrument for {}", track.name)));
match info {
Some(info) => used_slot(ui.painter(), r, t.slot_face, Mark::Dot, info.short_name, t.text, None),
None => empty_slot(ui.painter(), r, resp.hovered(), Mark::Dot),
@@ -592,12 +648,13 @@ fn level_icon(r: Rect) -> Rect {
Rect::from_min_max(pos2(outer.max.x - 9.0, outer.min.y + 2.0), pos2(outer.max.x - 4.0, outer.max.y - 2.0))
}
/// Draw one insert slot. Clicking a filled slot opens its plugin window; Cmd/Ctrl-click toggles bypass.
pub(crate) fn insert_slot(app: &mut SoundApp, ui: &mut Ui, track: &Track, slot: usize, r: Rect) {
let t = Tokens::current();
let id = track.id;
let ins = track.mixer.inserts.get(slot).cloned().flatten();
let resp = ui.interact(r, ui.id().with(("ins", id.0, slot)), Sense::click());
resp.widget_info(|| egui::WidgetInfo::labeled(egui::WidgetType::Button, true, format!("Insert {}", (b'A' + slot as u8) as char)));
let resp = ui.interact(r, ui.scope_id().with(("ins", id.0, slot)), Sense::click());
resp.widget_info(|| egui::WidgetInfo::labeled(egui::Role::Button, true, format!("Insert {} for {}", (b'A' + slot as u8) as char, track.name)));
let info = ins.as_ref().and_then(|i| plugin_info(&i.plugin));
let name = ins.as_ref().map(|i| info.map_or(i.plugin.as_str(), |p| p.short_name)).unwrap_or_default();
match &ins {
@@ -626,7 +683,7 @@ pub(crate) fn insert_slot(app: &mut SoundApp, ui: &mut Ui, track: &Track, slot:
/// An insert's menu: a search field that takes typing at once, then (until something is typed)
/// Bypass and removal and the plugins by category and format.
pub fn plugin_menu(app: &mut SoundApp, ui: &mut Ui, id: TrackId, slot: usize, occupied: bool) {
let search = MenuSearch::show(ui, egui::Id::new(("plugin-search", id.0, slot)), "Search plugins…");
let search = MenuSearch::show(ui, egui::Id::unique(("plugin-search", id.0, slot)), "Search plugins…");
if search.typing() {
if let Some(plugin) = plugin_search(ui, &search, false) {
pick_insert(app, ui, id, slot, &plugin);
@@ -667,7 +724,7 @@ pub fn plugin_menu(app: &mut SoundApp, ui: &mut Ui, id: TrackId, slot: usize, oc
/// instruments.
pub fn instrument_menu(app: &mut SoundApp, ui: &mut Ui, id: TrackId) {
let slot = soundcraft_mix::INSTRUMENT_SLOT;
let search = MenuSearch::show(ui, egui::Id::new(("instrument-search", id.0)), "Search instruments…");
let search = MenuSearch::show(ui, egui::Id::unique(("instrument-search", id.0)), "Search instruments…");
if search.typing() {
if let Some(plugin) = plugin_search(ui, &search, true) {
run_pick(app, ui, "mix.instrument", json!({"track": id.0, "plugin": plugin}));
@@ -925,6 +982,7 @@ pub fn plugin_info(id: &str) -> Option<&'static soundcraft_dsp::PluginInfo> {
.or_else(|| soundcraft_au_host::plugin_info(id))
}
/// Draw one send slot: the route area picks a destination, while the level area opens or edits the send level.
pub(crate) fn send_slot(app: &mut SoundApp, ui: &mut Ui, track: &Track, slot: usize, r: Rect) {
let t = Tokens::current();
let id = track.id;
@@ -934,8 +992,9 @@ pub(crate) fn send_slot(app: &mut SoundApp, ui: &mut Ui, track: &Track, slot: us
// destination. A short track's square has no icon, so all of it is the level.
let level_r = if compact { r } else { level_icon(r).expand2(vec2(3.0, 0.0)).with_min_y(r.min.y).with_max_y(r.max.y) };
let route_r = Rect::from_min_max(r.min, pos2((level_r.min.x - 1.0).max(r.min.x), r.max.y));
let route_resp = ui.interact(if snd.is_some() { route_r } else { r }, ui.id().with(("snd-route", id.0, slot)), Sense::click());
route_resp.widget_info(|| egui::WidgetInfo::labeled(egui::WidgetType::Button, true, format!("Send {}", (b'A' + slot as u8) as char)));
let route_resp = ui.interact(if snd.is_some() { route_r } else { r }, ui.scope_id().with(("snd-route", id.0, slot)), Sense::click());
route_resp
.widget_info(|| egui::WidgetInfo::labeled(egui::Role::Button, true, format!("Send {} for {}", (b'A' + slot as u8) as char, track.name)));
let mark = if compact { Mark::Arrow } else { Mark::Letter((b'a' + slot as u8) as char) };
match &snd {
Some(s) => {
@@ -945,8 +1004,8 @@ pub(crate) fn send_slot(app: &mut SoundApp, ui: &mut Ui, track: &Track, slot: us
used_slot(ui.painter(), r, t.slot_face, mark, &name, color, Some(level));
let which = (b'A' + slot as u8) as char;
let shown = app.ui.send_window == Some((id, slot));
let level_resp = ui.interact(level_r, ui.id().with(("send-value", id.0, slot)), Sense::click_and_drag());
level_resp.widget_info(|| egui::WidgetInfo::labeled(egui::WidgetType::Button, true, format!("Send {which} level")));
let level_resp = ui.interact(level_r, ui.scope_id().with(("send-value", id.0, slot)), Sense::click_and_drag());
level_resp.widget_info(|| egui::WidgetInfo::labeled(egui::Role::Button, true, format!("Send {which} level for {}", track.name)));
if shown {
ui.painter().rect_stroke(level_r.shrink(0.5), 1.0, Stroke::new(1.5, t.accent), StrokeKind::Inside);
}
@@ -1004,7 +1063,7 @@ pub(crate) fn send_slot(app: &mut SoundApp, ui: &mut Ui, track: &Track, slot: us
pub(crate) fn send_window(app: &mut SoundApp, ctx: &egui::Context) {
// egui closes menus and drops text focus on Escape before this runs, so whether this Escape
// was theirs comes from the last frame. A dialog or the search on top takes it too.
let busy_id = egui::Id::new("send_window_escape");
let busy_id = egui::Id::unique("send_window_escape");
let busy = ctx.data(|d| d.get_temp(busy_id)).unwrap_or(false) || app.dialogs.open.is_some() || app.ui.show_search;
if !busy && ctx.input(|i| i.key_pressed(egui::Key::Escape)) {
app.ui.send_window = None;
@@ -1014,6 +1073,7 @@ pub(crate) fn send_window(app: &mut SoundApp, ctx: &egui::Context) {
ctx.data_mut(|d| d.insert_temp(busy_id, busy));
}
/// Draw the floating send window for the remembered track and slot, or close it when that send no longer exists.
fn send_window_contents(app: &mut SoundApp, ctx: &egui::Context) {
let Some((id, slot)) = app.ui.send_window else { return };
let Some((name, send)) = app.engine.session().track(id).and_then(|t| Some((t.name.clone(), t.mixer.sends.get(slot).cloned().flatten()?))) else {
@@ -1023,7 +1083,7 @@ fn send_window_contents(app: &mut SoundApp, ctx: &egui::Context) {
let t = Tokens::current();
let mut open = true;
egui::Window::new(format!("{name} · Send {}", (b'A' + slot as u8) as char))
.id(egui::Id::new("send_window"))
.id(egui::Id::unique("send_window"))
.open(&mut open)
.auto_sized()
.collapsible(false)
@@ -1053,8 +1113,10 @@ fn send_window_contents(app: &mut SoundApp, ctx: &egui::Context) {
}
ui.label(egui::RichText::new(pan_text(send.pan)).color(t.counter_text).small());
let (fr, fresp) = ui.allocate_exact_size(vec2(56.0, 220.0), Sense::hover());
fresp.widget_info(|| egui::WidgetInfo::labeled(egui::WidgetType::Slider, true, "Send level"));
if let Some(db) = fader(ui, fr, send.level_db, ui.id().with("send_fader")) {
fresp.widget_info(|| egui::WidgetInfo::labeled(egui::Role::Slider, true, "Send level"));
// Distinct from the strip's "Send A level for <track>" icon and from the slot's "Send level", so a
// label query while the window is open still picks one node.
if let Some(db) = fader(ui, fr, send.level_db, ui.scope_id().with("send_fader"), "Send level fader") {
let _ = app.engine.execute_merged(
"mix.send_level",
&json!({"track": id.0, "slot": slot, "db": db}),
@@ -1064,7 +1126,18 @@ fn send_window_contents(app: &mut SoundApp, ctx: &egui::Context) {
let (rr, _) = ui.allocate_exact_size(vec2(64.0, 15.0), Sense::hover());
ui.painter().rect_filled(rr, 1.0, t.counter_bg);
let shown = db_text(send.level_db);
let (_, typed) = editable_readout(ui, rr, ui.id().with("send_level_value"), &shown, &format!("{:.1}", send.level_db), parse_db, 0.0);
let (_, typed) = editable_readout(
ui,
rr,
ui.scope_id().with("send_level_value"),
Readout {
name: &format!("{name} send level"),
display: &shown,
edit_initial: &format!("{:.1}", send.level_db),
parse: parse_db,
reset: 0.0,
},
);
if let Some(db) = typed {
let _ = app.run("mix.send_level", json!({"track": id.0, "slot": slot, "db": db}));
}
@@ -1129,18 +1202,24 @@ fn dragged_send_db(db: f32, dy: f32, fine: bool) -> f32 {
soundcraft_model::fader_pos_to_db(soundcraft_model::fader_db_to_pos(db) - dy * per_point)
}
/// What an [`editable_readout`] shows and how a typed value is parsed.
struct Readout<'a> {
/// Accessible name of the text field shown while editing.
name: &'a str,
/// The value painted while not editing.
display: &'a str,
/// The editor's initial text.
edit_initial: &'a str,
parse: fn(&str) -> Option<f32>,
/// The value an Alt-click sets.
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.
fn editable_readout(
ui: &mut Ui,
r: Rect,
id: egui::Id,
display: &str,
edit_initial: &str,
parse: fn(&str) -> Option<f32>,
reset: f32,
) -> (Option<egui::Response>, Option<f32>) {
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");
let text_id = id.with("text");
@@ -1170,10 +1249,10 @@ fn editable_readout(
let edit = egui::TextEdit::singleline(&mut text)
.id(id.with("editor"))
.frame(egui::Frame::NONE)
.horizontal_align(egui::Align::Center)
.align(egui::Align2::CENTER_TOP)
.font(regular(9.0))
.desired_width(r.width());
let edit_resp = child.add_sized(r.size(), edit);
let edit_resp = crate::widgets::named_text_field(child.add_sized(r.size(), edit), name);
if started {
edit_resp.request_focus();
}
@@ -1241,9 +1320,10 @@ fn send_level_scale(ui: &mut Ui, db: f32) {
ui.painter().line_segment([pos2(x, r.min.y), pos2(x, line_y + 3.0)], Stroke::new(2.0, t.counter_text));
}
/// Searchable bus destination list with a New Bus action for a send.
fn send_route_menu(app: &mut SoundApp, ui: &mut Ui, id: TrackId, slot: usize, current: Option<&Route>) {
let busses = app.engine.session().busses.clone();
let search = MenuSearch::show(ui, egui::Id::new(("send-bus-search", id.0, slot)), "Search buses…");
let search = MenuSearch::show(ui, egui::Id::unique(("send-bus-search", id.0, slot)), "Search buses…");
let visible: Vec<_> = busses.iter().filter(|bus| search.matches(&bus.name)).collect();
let picked = search.rows(ui, visible.len(), 260.0, |ui, i, highlighted| {
let current = matches!(current, Some(Route::Bus(bus_id)) if *bus_id == visible[i].id);
@@ -1268,6 +1348,7 @@ fn send_route_menu(app: &mut SoundApp, ui: &mut Ui, id: TrackId, slot: usize, cu
}
}
/// Input or output menu: no route and buses on both, plus the main output (outputs) or inputs 1-8 (inputs).
fn route_menu(app: &mut SoundApp, ui: &mut Ui, id: TrackId, input: bool) {
let key = if input { "input" } else { "output" };
let cmd = if input { "track.input" } else { "track.output" };
@@ -1291,7 +1372,7 @@ fn route_menu(app: &mut SoundApp, ui: &mut Ui, id: TrackId, input: bool) {
ui.separator();
let busses: Vec<String> = app.engine.session().busses.iter().map(|b| b.name.clone()).collect();
ui.strong("Bus");
let search = MenuSearch::show(ui, egui::Id::new(("route-bus-search", id.0, input)), "Search buses…");
let search = MenuSearch::show(ui, egui::Id::unique(("route-bus-search", id.0, input)), "Search buses…");
let visible: Vec<&String> = busses.iter().filter(|b| search.matches(b)).collect();
if let Some(i) = search.rows(ui, visible.len(), 420.0, |ui, i, highlighted| menu_row(ui, highlighted, visible[i])) {
let _ = app.run(cmd, json!({"track": id.0, key: visible[i]}));
@@ -1309,7 +1390,7 @@ fn route_menu(app: &mut SoundApp, ui: &mut Ui, id: TrackId, input: bool) {
#[cfg(test)]
mod tests {
use super::{dragged_send_db, editable_readout, parse_db, parse_pan};
use super::{Readout, dragged_send_db, editable_readout, parse_db, parse_pan};
use egui::{Event, Modifiers, PointerButton, Rect, pos2, vec2};
#[test]
@@ -1341,6 +1422,8 @@ mod tests {
assert_eq!(dragged_send_db(12.0, -50.0, false), 12.0);
}
/// Alt-click resets a readout; a click opens its text field, and hiding the widget drops focus so the formatted
/// display returns.
#[test]
fn readouts_reset_on_alt_click_and_drop_a_field_that_lost_focus() {
let ctx = egui::Context::default();
@@ -1355,7 +1438,12 @@ mod tests {
let mut out = (false, None);
let mut full = ctx.run_ui(egui::RawInput { events, time: Some(time), ..Default::default() }, |ui| {
if shown {
let (resp, value) = editable_readout(ui, r, egui::Id::new("readout"), "-3.0", "-3.0", parse_db, 0.0);
let (resp, value) = editable_readout(
ui,
r,
egui::Id::unique("readout"),
Readout { name: "Readout", display: "-3.0", edit_initial: "-3.0", parse: parse_db, reset: 0.0 },
);
out = (resp.is_some(), value);
}
});
+125 -13
View File
@@ -30,6 +30,10 @@ pub struct OpsState {
pub rtp_transpose: i64,
pub rtp_duration: f64,
pub rtp_delay: i64,
/// The MIDI tracks the Real-Time Properties form was last filled from, and the values it read:
/// the form re-reads the tracks when either changes (#444).
pub rtp_targets: Vec<soundcraft_model::TrackId>,
pub rtp_loaded: Option<RtpValues>,
pub identify_bars: u32,
}
@@ -57,6 +61,8 @@ impl Default for OpsState {
rtp_transpose: 0,
rtp_duration: 100.0,
rtp_delay: 0,
rtp_targets: Vec::new(),
rtp_loaded: None,
identify_bars: 1,
}
}
@@ -82,6 +88,7 @@ pub fn show(app: &mut SoundApp, ctx: &egui::Context) {
rtp(app, ctx);
}
/// Beat Detective window: sensitivity, separate and set-tempo options, Analyze and Apply results, and Identify Beat.
fn beat_detective(app: &mut SoundApp, ctx: &egui::Context) {
let mut open = app.ui.show_beat_detective;
if !open {
@@ -108,7 +115,7 @@ fn beat_detective(app: &mut SoundApp, ctx: &egui::Context) {
ui.separator();
ui.horizontal(|ui| {
ui.label("Identify Beat: selection is");
ui.add(egui::DragValue::new(&mut app.ops.identify_bars).range(1..=64));
ui.add(egui::DragValue::new(&mut app.ops.identify_bars).range(1..=64)).accessible_name("Identify beat bars");
ui.label("bar(s)");
if ui.button("Set Tempo").clicked() {
let r = app.run("event.identify_beat", json!({"bars": app.ops.identify_bars}));
@@ -138,6 +145,8 @@ pub(crate) fn tempo_ops_apply(curve: &str, start_bpm: f64, end_bpm: f64) -> (&'s
}
}
/// Tempo Operations window: choose a curve and start/end tempo, then dispatch its command using the edit selection.
/// Constant sends only the start BPM; without a nonempty range, its command defaults to sample zero.
fn tempo_ops(app: &mut SoundApp, ctx: &egui::Context) {
let mut open = app.ui.show_tempo_ops;
if !open {
@@ -149,8 +158,8 @@ fn tempo_ops(app: &mut SoundApp, ctx: &egui::Context) {
ui.selectable_value(&mut app.ops.tempo_curve, c.to_string(), c);
}
});
ui.add(egui::DragValue::new(&mut app.ops.tempo_start).range(5.0..=999.0).prefix("start bpm "));
ui.add(egui::DragValue::new(&mut app.ops.tempo_end).range(5.0..=999.0).prefix("end bpm "));
ui.add(egui::DragValue::new(&mut app.ops.tempo_start).range(5.0..=999.0).prefix("start bpm ")).accessible_name("Start tempo (BPM)");
ui.add(egui::DragValue::new(&mut app.ops.tempo_end).range(5.0..=999.0).prefix("end bpm ")).accessible_name("End tempo (BPM)");
ui.label(
egui::RichText::new(
"Applies across the edit selection (constant: at the insertion point; scale: factor = end / start; stretch: factor = start / end).",
@@ -166,6 +175,8 @@ fn tempo_ops(app: &mut SoundApp, ctx: &egui::Context) {
app.ui.show_tempo_ops = open;
}
/// Time Operations window: insert a duration in seconds at the selection start, cut the edit selection, change the
/// meter, or move the song start to the selection start.
fn time_ops(app: &mut SoundApp, ctx: &egui::Context) {
let mut open = app.ui.show_time_ops;
if !open {
@@ -180,17 +191,18 @@ fn time_ops(app: &mut SoundApp, ctx: &egui::Context) {
match app.ops.time_op.as_str() {
"change meter" => {
ui.horizontal(|ui| {
ui.add(egui::DragValue::new(&mut app.ops.meter_num).range(1..=64));
ui.add(egui::DragValue::new(&mut app.ops.meter_num).range(1..=64)).accessible_name("Meter numerator");
ui.label("/");
egui::ComboBox::from_id_salt("den").selected_text(app.ops.meter_den.to_string()).show_ui(ui, |ui| {
let den = egui::ComboBox::from_id_salt("den").selected_text(app.ops.meter_den.to_string()).show_ui(ui, |ui| {
for d in [2u32, 4, 8, 16] {
ui.selectable_value(&mut app.ops.meter_den, d, d.to_string());
}
});
den.response.accessible_name("Meter denominator");
});
}
"insert" => {
ui.add(egui::DragValue::new(&mut app.ops.time_seconds).range(0.0..=3600.0).suffix(" s"));
ui.add(egui::DragValue::new(&mut app.ops.time_seconds).range(0.0..=3600.0).suffix(" s")).accessible_name("Time (seconds)");
}
_ => {
ui.label(egui::RichText::new("Uses the edit selection.").small());
@@ -261,15 +273,16 @@ fn rtp(app: &mut SoundApp, ctx: &egui::Context) {
if !open {
return;
}
let targets = app.engine.target_tracks();
sync_rtp_form(&mut app.ops, app.engine.session(), targets, ctx.dragged_id().is_some());
egui::Window::new("MIDI Real-Time Properties").open(&mut open).default_size(vec2(340.0, 200.0)).show(ctx, |ui| {
ui.label("Non-destructive, applied at playback to the selected MIDI tracks.");
let mut changed = false;
changed |= ui.add(egui::Slider::new(&mut app.ops.rtp_velocity, -127..=127).text("Velocity +")).changed();
changed |= ui.add(egui::Slider::new(&mut app.ops.rtp_transpose, -48..=48).text("Transpose")).changed();
changed |= ui.add(egui::Slider::new(&mut app.ops.rtp_duration, 1.0..=400.0).text("Duration %")).changed();
changed |= ui.add(egui::Slider::new(&mut app.ops.rtp_delay, -960..=960).text("Delay (ticks)")).changed();
if changed {
let p = json!({"velocity": app.ops.rtp_velocity, "transpose": app.ops.rtp_transpose, "duration": app.ops.rtp_duration, "delay": app.ops.rtp_delay});
let mut changed = RtpChanged::default();
changed.velocity = ui.add(egui::Slider::new(&mut app.ops.rtp_velocity, -127..=127).text("Velocity +")).changed();
changed.transpose = ui.add(egui::Slider::new(&mut app.ops.rtp_transpose, -48..=48).text("Transpose")).changed();
changed.duration = ui.add(egui::Slider::new(&mut app.ops.rtp_duration, 1.0..=400.0).text("Duration %")).changed();
changed.delay = ui.add(egui::Slider::new(&mut app.ops.rtp_delay, -960..=960).text("Delay (ticks)")).changed();
if let Some(p) = rtp_params(changed, &app.ops) {
let _ = app.engine.execute_merged("event.midi_rtp", &p, "rtp");
}
ui.horizontal(|ui| {
@@ -288,6 +301,66 @@ fn rtp(app: &mut SoundApp, ctx: &egui::Context) {
app.ui.show_rtp = open;
}
/// Which of the form's controls the user changed this frame.
#[derive(Default, Clone, Copy)]
struct RtpChanged {
velocity: bool,
transpose: bool,
duration: bool,
delay: bool,
}
/// A form change submits only the properties the user touched: the command stores each key it is
/// sent, so sending untouched ones would overwrite the target's values with the form's (#444).
fn rtp_params(changed: RtpChanged, ops: &OpsState) -> Option<Value> {
let mut p = serde_json::Map::new();
if changed.velocity {
p.insert("velocity".into(), json!(ops.rtp_velocity));
}
if changed.transpose {
p.insert("transpose".into(), json!(ops.rtp_transpose));
}
if changed.duration {
p.insert("duration".into(), json!(ops.rtp_duration));
}
if changed.delay {
p.insert("delay".into(), json!(ops.rtp_delay));
}
(!p.is_empty()).then_some(Value::Object(p))
}
/// Velocity, transpose, duration % and delay, as the form shows them.
pub type RtpValues = (i64, i64, f64, i64);
/// Fills the form from the tracks it edits whenever the targets or their stored values change
/// (selection, undo, a command), so it never shows (and a control change never writes back) stale
/// values (#444). A slider being dragged keeps its value until it is released.
fn sync_rtp_form(ops: &mut OpsState, s: &soundcraft_model::Session, targets: Vec<soundcraft_model::TrackId>, dragging: bool) {
let values = rtp_form_values(s, &targets);
if ops.rtp_targets != targets || (ops.rtp_loaded != Some(values) && !dragging) {
let (velocity, transpose, duration, delay) = values;
ops.rtp_targets = targets;
ops.rtp_loaded = Some(values);
ops.rtp_velocity = velocity;
ops.rtp_transpose = transpose;
ops.rtp_duration = duration;
ops.rtp_delay = delay;
}
}
/// The values the form shows for `targets`: the first target's own Real-Time Properties, or the
/// neutral defaults when nothing is selected.
fn rtp_form_values(s: &soundcraft_model::Session, targets: &[soundcraft_model::TrackId]) -> RtpValues {
let Some(&t) = targets.first() else { return (0, 0, 100.0, 0) };
let value = |key: &str, d: f64| s.edit.value(&format!("rtp.{}.{key}", t.0), d);
(
value("velocity", 0.0).clamp(-127.0, 127.0) as i64,
value("transpose", 0.0).clamp(-127.0, 127.0) as i64,
value("duration", 100.0).clamp(1.0, 1000.0),
value("delay", 0.0).clamp(-1e7, 1e7) as i64,
)
}
#[cfg(test)]
mod tests {
use super::*;
@@ -308,4 +381,43 @@ mod tests {
assert_eq!(id, "event.tempo_linear");
assert_eq!(p["end_bpm"], 140.0);
}
/// One control moved: only its property is submitted, so the command cannot overwrite the
/// target's other Real-Time Properties with the form's values (#444).
#[test]
fn rtp_submits_only_the_property_that_changed() {
let ops = OpsState { rtp_velocity: 12, rtp_transpose: 7, rtp_duration: 50.0, rtp_delay: 480, ..OpsState::default() };
let only_velocity = RtpChanged { velocity: true, ..RtpChanged::default() };
assert_eq!(rtp_params(only_velocity, &ops), Some(json!({"velocity": 12})));
let only_duration = RtpChanged { duration: true, ..RtpChanged::default() };
assert_eq!(rtp_params(only_duration, &ops), Some(json!({"duration": 50.0})));
assert_eq!(rtp_params(RtpChanged::default(), &ops), None, "no control moved, no command");
}
/// The form reads the target's own values when the window opens or the selection changes.
#[test]
fn the_rtp_form_shows_the_target_tracks_values() {
let mut e = soundcraft_engine::Engine::default();
e.execute("track.new", &json!({"kind": "instrument", "name": "Synth"})).unwrap();
let t = e.session().tracks.last().unwrap().id;
e.execute("event.midi_rtp", &json!({"track": t.0, "velocity": -12, "transpose": 7, "duration": 50.0, "delay": 480})).unwrap();
assert_eq!(rtp_form_values(e.session(), &[]), (0, 0, 100.0, 0), "no target: the neutral defaults");
assert_eq!(rtp_form_values(e.session(), &[t]), (-12, 7, 50.0, 480));
}
/// Undo keeps the same targets but changes their stored values: the form follows.
#[test]
fn the_rtp_form_follows_an_undo_on_the_same_targets() {
let mut e = soundcraft_engine::Engine::default();
e.execute("track.new", &json!({"kind": "instrument", "name": "Synth"})).unwrap();
let t = e.session().tracks.last().unwrap().id;
let mut ops = OpsState::default();
sync_rtp_form(&mut ops, e.session(), vec![t], false);
e.execute("event.midi_rtp", &json!({"track": t.0, "velocity": -12})).unwrap();
sync_rtp_form(&mut ops, e.session(), vec![t], false);
assert_eq!(ops.rtp_velocity, -12);
e.execute("edit.undo", &json!({})).unwrap();
sync_rtp_form(&mut ops, e.session(), vec![t], false);
assert_eq!(ops.rtp_velocity, 0, "the form kept the undone value");
}
}
+68 -30
View File
@@ -23,7 +23,7 @@ fn drag_track(app: &mut SoundApp, ui: &Ui, row: Rect, id: u64, dropped: bool) {
let y = row.min.y + (gap - from as f32) * pitch - spacing / 2.0;
ui.ctx().set_cursor_icon(egui::CursorIcon::Grabbing);
ui.ctx()
.layer_painter(egui::LayerId::new(egui::Order::Foreground, egui::Id::new("track_list_reorder")))
.layer_painter(egui::LayerId::new(egui::Order::Foreground, egui::Id::unique("track_list_reorder")))
.line_segment([pos2(row.min.x, y), pos2(row.max.x, y)], Stroke::new(2.0, Tokens::current().accent));
}
}
@@ -40,6 +40,7 @@ fn panel_header(ui: &mut Ui, title: &str) {
/// the panel and the window beside it.
const GUTTER_W: f32 = 10.0;
/// Tracks and Groups panels: headers, track and group lists, and their scrollbars in the panel gutter.
pub fn tracks_and_groups(app: &mut SoundApp, ui: &mut Ui) {
let t = Tokens::current();
let full = ui.max_rect();
@@ -61,23 +62,31 @@ pub fn tracks_and_groups(app: &mut SoundApp, ui: &mut Ui) {
s.tracks.iter().map(|x| (x.id.0, x.name.clone(), !x.hidden, x.color, s.edit.selected_tracks.contains(&x.id))).collect()
};
let hidden = egui::scroll_area::ScrollBarVisibility::AlwaysHidden;
let lists =
egui::ScrollArea::vertical().id_salt("tracks_scroll").max_height(total * 0.62).auto_shrink([false, false]).scroll_bar_visibility(hidden);
let lists = with_pending_offset(egui::ScrollArea::vertical().id_salt("tracks_scroll"), ui, "tracks_scroll")
.max_height(total * 0.62)
.auto_shrink([false, false])
.scroll_bar_visibility(hidden);
let out = lists.show(ui, |ui| {
for (id, name, shown, color, sel) in tracks {
let (r, resp) = ui.allocate_exact_size(vec2(ui.available_width(), 17.0), Sense::click_and_drag());
resp.widget_info(|| egui::WidgetInfo::selected(egui::WidgetType::SelectableLabel, true, sel, &name));
resp.widget_info(|| egui::WidgetInfo::selected(egui::Role::Button, true, sel, &name));
if sel {
ui.painter().rect_filled(r, 0.0, t.row_selected);
}
let dot = Rect::from_center_size(pos2(r.min.x + 10.0, r.center().y), vec2(8.0, 8.0));
let dresp = ui.interact(dot.expand(3.0), ui.id().with(("vis", id)), Sense::click());
let dresp = ui.interact(dot.expand(3.0), ui.scope_id().with(("vis", id)), Sense::click());
ui.painter().circle(dot.center(), 3.5, if shown { t.visible_dot } else { Color32::TRANSPARENT }, Stroke::new(1.0, t.text_dim));
if dresp.clicked() {
let _ = app.run("track.hide", json!({"tracks": [id], "hidden": shown}));
}
ui.painter().rect_filled(Rect::from_min_size(pos2(r.min.x + 20.0, r.min.y + 3.0), vec2(4.0, 11.0)), 0.0, rgb(color));
ui.painter().text(pos2(r.min.x + 30.0, r.center().y), Align2::LEFT_CENTER, &name, regular(11.5), if shown { t.text } else { t.text_dim });
ui.painter().with_clip_rect(r).text(
pos2(r.min.x + 30.0, r.center().y),
Align2::LEFT_CENTER,
&name,
regular(11.5),
if shown { t.text } else { t.text_dim },
);
if resp.clicked() {
crate::edit_window::click_track(app, soundcraft_model::TrackId(id), ui.input(|i| i.modifiers), |_, _| true);
}
@@ -91,11 +100,12 @@ pub fn tracks_and_groups(app: &mut SoundApp, ui: &mut Ui) {
panel_header(ui, "GROUPS");
let groups: Vec<(u64, char, String, bool, [u8; 3])> =
app.engine.session().groups.iter().map(|g| (g.id.0, g.letter, g.name.clone(), g.active, g.color)).collect();
let out = egui::ScrollArea::vertical().id_salt("groups_scroll").auto_shrink([false, false]).scroll_bar_visibility(hidden).show(ui, |ui| {
let groups_area = with_pending_offset(egui::ScrollArea::vertical().id_salt("groups_scroll"), ui, "groups_scroll");
let out = groups_area.auto_shrink([false, false]).scroll_bar_visibility(hidden).show(ui, |ui| {
let (r, _) = ui.allocate_exact_size(vec2(ui.available_width(), 17.0), Sense::hover());
ui.painter().text(pos2(r.min.x + 30.0, r.center().y), Align2::LEFT_CENTER, "<ALL>", regular(11.5).clone(), t.text);
let all = ui.interact(r, ui.id().with("group_all"), Sense::click());
all.widget_info(|| egui::WidgetInfo::labeled(egui::WidgetType::Button, true, "<ALL>"));
let all = ui.interact(r, ui.scope_id().with("group_all"), Sense::click());
all.widget_info(|| egui::WidgetInfo::labeled(egui::Role::Button, true, "<ALL>"));
if all.double_clicked() {
let _ = app.run("edit.select_all", json!({}));
}
@@ -107,7 +117,7 @@ pub fn tracks_and_groups(app: &mut SoundApp, ui: &mut Ui) {
});
for (id, letter, name, active, color) in groups {
let (r, resp) = ui.allocate_exact_size(vec2(ui.available_width(), 17.0), Sense::click());
resp.widget_info(|| egui::WidgetInfo::selected(egui::WidgetType::Checkbox, true, active, &name));
resp.widget_info(|| egui::WidgetInfo::selected(egui::Role::CheckBox, true, active, &name));
if active {
ui.painter().rect_filled(Rect::from_min_size(pos2(r.min.x + 4.0, r.min.y + 2.0), vec2(14.0, 13.0)), 1.0, rgb(color));
}
@@ -118,7 +128,7 @@ pub fn tracks_and_groups(app: &mut SoundApp, ui: &mut Ui) {
bold(10.0),
if active { t.text_dark } else { t.text_dim },
);
ui.painter().text(pos2(r.min.x + 30.0, r.center().y), Align2::LEFT_CENTER, &name, regular(11.5), t.text);
ui.painter().with_clip_rect(r).text(pos2(r.min.x + 30.0, r.center().y), Align2::LEFT_CENTER, &name, regular(11.5), t.text);
if resp.clicked() {
let _ = app.run("track.group_toggle", json!({"group": id}));
}
@@ -150,8 +160,21 @@ pub fn tracks_and_groups(app: &mut SoundApp, ui: &mut Ui) {
gutter_scroll(ui, gutter, &out);
}
/// Apply a pending gutter page/drag target to the next frame's `ScrollArea`. `State::offset` is
/// private in egui 0.37, so the target cannot be written directly; it is applied here before layout.
fn with_pending_offset(area: egui::ScrollArea, ui: &Ui, salt: &str) -> egui::ScrollArea {
// Match `ScrollArea` exactly: it hashes an `IdSalt` into the persistent id.
let id = ui.make_persistent_id(egui::IdSalt::new(salt));
let pending = ui.ctx().memory_mut(|m| m.data.remove_temp::<f32>(id.with("gutter_target")));
match pending {
Some(y) => area.vertical_scroll_offset(y),
None => area,
}
}
/// A list's scroll thumb in the gutter beside it, when the list is longer than its space: drag it,
/// or click above or below it to page.
/// or click above or below it to page. The target applies on the next frame, because the public
/// `ScrollArea` API has no way to write the current pass's offset.
fn gutter_scroll<R>(ui: &Ui, gutter: Rect, out: &egui::scroll_area::ScrollAreaOutput<R>) {
let t = Tokens::current();
let (view, content) = (out.inner_rect.height(), out.content_size.y);
@@ -160,12 +183,12 @@ fn gutter_scroll<R>(ui: &Ui, gutter: Rect, out: &egui::scroll_area::ScrollAreaOu
}
let bar = Rect::from_x_y_ranges(gutter.x_range(), out.inner_rect.y_range());
let (max, h) = (content - view, (bar.height() * view / content).max(20.0));
let mut state = out.state;
let thumb = Rect::from_min_size(pos2(bar.min.x + 2.0, bar.min.y + (bar.height() - h) * state.offset.y / max), vec2(bar.width() - 4.0, h));
let offset = out.state.clamped_offset().y;
let thumb = Rect::from_min_size(pos2(bar.min.x + 2.0, bar.min.y + (bar.height() - h) * offset / max), vec2(bar.width() - 4.0, h));
let resp = ui.interact(bar, out.id.with("gutter"), Sense::click_and_drag());
ui.painter().rect_filled(thumb, 3.0, if resp.hovered() || resp.dragged() { t.button_hi.lerp_to_gamma(Color32::WHITE, 0.1) } else { t.button_hi });
let to = if resp.dragged() {
Some(state.offset.y + resp.drag_delta().y * max / (bar.height() - h).max(1.0))
Some(offset + resp.drag_delta().y * max / (bar.height() - h).max(1.0))
} else if let Some(p) = resp.interact_pointer_pos().filter(|_| resp.clicked()) {
let page = if p.y < thumb.min.y {
-view
@@ -174,13 +197,13 @@ fn gutter_scroll<R>(ui: &Ui, gutter: Rect, out: &egui::scroll_area::ScrollAreaOu
} else {
0.0
};
Some(state.offset.y + page)
Some(offset + page)
} else {
None
};
if let Some(y) = to {
state.offset.y = y.clamp(0.0, max);
state.store(ui.ctx(), out.id);
ui.ctx().memory_mut(|m| m.data.insert_temp(out.id.with("gutter_target"), y.clamp(0.0, max)));
ui.ctx().request_repaint();
}
}
@@ -212,15 +235,16 @@ fn clip_rows(s: &soundcraft_model::Session, needle: &str) -> Vec<ClipRow> {
sources.chain(clips).collect()
}
/// Clips panel with a filter and a revision-cached list of audio sources and clips.
pub fn clip_list(app: &mut SoundApp, ui: &mut Ui) {
let t = Tokens::current();
panel_header(ui, "CLIPS");
let fkey = egui::Id::new("clip_filter");
let fkey = egui::Id::unique("clip_filter");
let mut filter: String = ui.ctx().memory(|m| m.data.get_temp(fkey)).unwrap_or_default();
ui.add(egui::TextEdit::singleline(&mut filter).hint_text("🔍 Find clips").desired_width(f32::INFINITY));
ui.ctx().memory_mut(|m| m.data.insert_temp(fkey, filter.clone()));
let needle = filter.to_lowercase();
let rkey = egui::Id::new("clip_rows");
let rkey = egui::Id::unique("clip_rows");
let revision = app.engine.revision;
let cached = ui.ctx().memory(|m| m.data.get_temp::<ClipRows>(rkey)).filter(|c| c.revision == revision && c.filter == needle);
let rows = match cached {
@@ -232,7 +256,8 @@ pub fn clip_list(app: &mut SoundApp, ui: &mut Ui) {
}
};
let row_h = 17.0;
egui::ScrollArea::vertical().id_salt("clips_scroll").auto_shrink([false, false]).show_rows(ui, row_h, rows.len(), |ui, shown| {
let clips_area = with_pending_offset(egui::ScrollArea::vertical().id_salt("clips_scroll"), ui, "clips_scroll");
clips_area.auto_shrink([false, false]).show_rows(ui, row_h, rows.len(), |ui, shown| {
// Only the rows in view: what they show is read now, before any of them acts on the session.
let items: Vec<(u64, String, bool, [u8; 3], bool)> = {
let s = app.engine.session();
@@ -268,6 +293,12 @@ pub fn clip_list(app: &mut SoundApp, ui: &mut Ui) {
}
ui.separator();
}
if !whole && ui.button("Auto Rename…").clicked() {
// The selected clips when the row is one of them, otherwise the row's clip.
let clips = if selected { app.engine.session().edit.selected_clips.clone() } else { vec![ClipId(id)] };
app.dialogs.open_auto_rename(&app.engine, clips);
ui.close();
}
if ui.button("Clear…").clicked() {
let (clips, sources) = if whole { (vec![], vec![SourceId(id)]) } else { (vec![ClipId(id)], vec![]) };
app.dialogs.open_clear(&app.engine, clips, sources);
@@ -414,13 +445,14 @@ fn big_counter(app: &mut SoundApp, ctx: &egui::Context) {
app.ui.show_big_counter = open;
}
/// One recallable memory-location row: number button, editable name, time, length, kind and delete.
fn memory_location_row(app: &mut SoundApp, ui: &mut Ui, n: u32, name: &str, loc: &str, len: &str, kind: &str) {
if ui.button(n.to_string()).on_hover_text("Recall").clicked() {
let _ = app.run("markers.recall", json!({"number": n}));
}
let key = egui::Id::new(("mem_name", n));
let key = egui::Id::unique(("mem_name", n));
let mut edit: String = ui.ctx().memory(|m| m.data.get_temp(key)).unwrap_or_else(|| name.to_string());
let r = ui.add(egui::TextEdit::singleline(&mut edit).desired_width(140.0));
let r = crate::widgets::named_text_field(ui.add(egui::TextEdit::singleline(&mut edit).desired_width(140.0)), "Name");
if r.changed() {
ui.ctx().memory_mut(|m| m.data.insert_temp(key, edit.clone()));
}
@@ -431,7 +463,7 @@ fn memory_location_row(app: &mut SoundApp, ui: &mut Ui, n: u32, name: &str, loc:
ui.label(egui::RichText::new(loc).font(mono(11.0)));
ui.label(egui::RichText::new(len).font(mono(11.0)));
ui.label(kind);
if ui.small_button("✕").on_hover_text("Delete").clicked() {
if crate::widgets::close_button(ui, "Delete marker").clicked() {
let _ = app.run("markers.delete", json!({"number": n}));
}
ui.end_row();
@@ -492,6 +524,7 @@ pub(crate) fn undo_history(app: &mut SoundApp, ui: &mut Ui) {
});
}
/// Draw plugin windows for instrument and insert slots that still exist, removing closed ones from the list.
fn plugin_windows(app: &mut SoundApp, ctx: &egui::Context) {
let wins = app.ui.plugin_windows.clone();
let mut keep = Vec::new();
@@ -508,7 +541,7 @@ fn plugin_windows(app: &mut SoundApp, ctx: &egui::Context) {
let place = if instrument { "instrument".to_string() } else { char::from(b'a'.saturating_add(slot as u8)).to_string() };
let mut open = true;
let shown = egui::Window::new(format!("{tname} · {place} · {}", info.name))
.id(egui::Id::new(("plugin", tid.0, slot)))
.id(egui::Id::unique(("plugin", tid.0, slot)))
.open(&mut open)
.default_width(340.0)
.default_pos(egui::pos2(ctx.content_rect().width() - 380.0, 120.0))
@@ -521,7 +554,7 @@ fn plugin_windows(app: &mut SoundApp, ctx: &egui::Context) {
if ui.button(label).clicked() {
let _ = app.run("mix.insert_bypass", json!({"track": tid.0, "slot": slot}));
}
ui.menu_button("Presets ▾", |ui| preset_menu(app, ui, tid, slot, info, &ins));
ui.menu_button("Presets v", |ui| preset_menu(app, ui, tid, slot, info, &ins));
}
if soundcraft_mix::is_third_party(&ins.plugin) {
let open = app.player.as_ref().is_some_and(|p| p.editor_open(tid, slot));
@@ -557,7 +590,7 @@ fn plugin_windows(app: &mut SoundApp, ctx: &egui::Context) {
egui::Grid::new(("pg", tid.0, slot)).num_columns(3).show(ui, |ui| {
for p in info.params {
let v = ins.params.get(p.id).copied().unwrap_or(p.default);
ui.label(p.name);
let label = ui.label(p.name);
let mut x = v;
let resp = if !p.choices.is_empty() {
let mut idx = x.round().max(0.0) as usize;
@@ -580,6 +613,9 @@ fn plugin_windows(app: &mut SoundApp, ctx: &egui::Context) {
}
ui.add(sl)
};
// 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.
let resp = resp.labelled_by(label.id);
if (x - v).abs() > f32::EPSILON && instrument {
let _ = app.engine.execute_merged(
"mix.instrument_param",
@@ -646,12 +682,13 @@ fn eq_curve(ui: &mut Ui, id: &str, ins: &soundcraft_model::Insert) {
ui.painter().add(egui::Shape::line(pts, Stroke::new(2.0, t.counter_text)));
}
/// About window tabs for version information, contributors and models.
fn about(app: &mut SoundApp, ctx: &egui::Context) {
let mut open = app.ui.show_about;
if !open {
return;
}
let tab_id = egui::Id::new("about_tab");
let tab_id = egui::Id::unique("about_tab");
egui::Window::new("About SoundCraft").open(&mut open).default_size(vec2(640.0, 420.0)).collapsible(false).show(ctx, |ui| {
let mut tab = ui.data_mut(|d| d.get_temp::<u8>(tab_id)).unwrap_or(0);
ui.horizontal(|ui| {
@@ -698,6 +735,7 @@ fn preset_dir(app: &SoundApp, plugin: &str) -> Option<std::path::PathBuf> {
app.preset_dir.as_ref().map(|d| d.join(crate::preset_folder_name(plugin)))
}
/// Factory default plus user presets for a plugin insert; user presets need a desktop preset directory.
fn preset_menu(
app: &mut SoundApp,
ui: &mut Ui,
@@ -739,10 +777,10 @@ fn preset_menu(
}
}
ui.separator();
let key = egui::Id::new(("preset_name", tid.0, slot));
let key = egui::Id::unique(("preset_name", tid.0, slot));
let mut name: String = ui.ctx().memory(|m| m.data.get_temp(key)).unwrap_or_else(|| format!("{} preset", info.name));
ui.horizontal(|ui| {
ui.add(egui::TextEdit::singleline(&mut name).desired_width(140.0));
crate::widgets::named_text_field(ui.add(egui::TextEdit::singleline(&mut name).desired_width(140.0)), "Preset name");
if ui.button("Save").clicked() {
let clean = soundcraft_engine::io::sanitize_name(&name);
let body = json!({"format": "soundcraft-plugin-preset", "plugin": info.id, "params": ins.params});
+10 -31
View File
@@ -56,7 +56,7 @@ pub fn drop_target(app: &mut SoundApp, ctx: &egui::Context, r: Rect, t: TrackId)
app.sample_drop_targets.push((r, t));
let over = ctx.input(|i| !i.raw.hovered_files.is_empty() && i.pointer.latest_pos().is_some_and(|p| r.contains(p)));
if over {
ctx.layer_painter(egui::LayerId::new(egui::Order::Foreground, egui::Id::new(("sample_drop", t.0)))).rect_stroke(
ctx.layer_painter(egui::LayerId::new(egui::Order::Foreground, egui::Id::unique(("sample_drop", t.0)))).rect_stroke(
r,
2.0,
Stroke::new(2.0, Tokens::current().accent),
@@ -82,38 +82,16 @@ impl SampleRead {
}
impl SoundApp {
/// Takes this frame's dropped files: audio dropped on an instrument track or a Sampler window
/// loads into that track's sampler (the sampler becomes its instrument). A file with a full
/// path loads straight away; one without (the browser) waits in [`SoundApp::take_sample_reads`]
/// for the shell to read. Everything else is returned for the shell to open or import.
pub fn take_dropped_files(&mut self, ctx: &egui::Context) -> Vec<egui::DroppedFileHandle> {
let (files, pos) = ctx.input(|i| (i.raw.dropped_files.clone(), i.pointer.latest_pos()));
if files.is_empty() {
return files;
}
let Some(track) = self.drop_track(pos) else { return files };
let mut rest = Vec::new();
let mut claimed = false;
for f in files {
if claimed || !is_audio_name(&f.path().to_string_lossy()) {
rest.push(f);
continue;
}
claimed = true;
if f.path().is_absolute() {
let path = f.path().to_string_lossy().into_owned();
let r = self.run("mix.sampler_load", json!({"track": track.0, "path": path}));
self.sample_loaded(track, &path, r);
} else {
self.sample_reads.push(SampleRead { track, file: f });
}
}
rest
/// Dropped files this frame's UI pass did not load into a sampler, for the shell to open or
/// import. The capture happened at the start of the UI pass, before the font gate; this only
/// takes the queued result, so it is safe (and needed) on a logic-only tick after a paint.
pub fn take_dropped_files(&mut self) -> Vec<egui::DroppedFileHandle> {
std::mem::take(&mut self.host_drops)
}
/// The track a drop at `pos` loads into. Without a position (some platforms report none
/// during a drag), a single open Sampler window takes it.
fn drop_track(&self, pos: Option<egui::Pos2>) -> Option<TrackId> {
pub(crate) fn drop_track(&self, pos: Option<egui::Pos2>) -> Option<TrackId> {
if let Some(p) = pos {
return self.sample_drop_targets.iter().rev().find(|(r, _)| r.contains(p)).map(|(_, t)| *t);
}
@@ -140,7 +118,8 @@ impl SoundApp {
self.sample_loaded(read.track, &name, r);
}
fn sample_loaded(&mut self, track: TrackId, what: &str, r: Result<serde_json::Value, String>) {
/// Report a sampler load and open its instrument window on success, or show the failure status.
pub(crate) fn sample_loaded(&mut self, track: TrackId, what: &str, r: Result<serde_json::Value, String>) {
match r {
Ok(v) => {
if !self.ui.plugin_windows.contains(&(track, soundcraft_mix::INSTRUMENT_SLOT)) {
@@ -246,7 +225,7 @@ pub fn sample_strip(app: &mut SoundApp, ui: &mut Ui, tid: TrackId, ins: &Insert)
p.text(flag.center(), Align2::CENTER_CENTER, *tag, regular(9.0), t.text_dark);
}
// Dragging the nearest marker sets its parameter.
let grab_id = ui.id().with(("sample_marker", tid.0));
let grab_id = ui.scope_id().with(("sample_marker", tid.0));
let nearest = |x: f32| {
shown
.iter()
+10 -1
View File
@@ -33,6 +33,8 @@ fn diatonic(pitch: u8) -> (i32, bool) {
(oct * 7 + step, sharp)
}
/// Read-only score view for the target MIDI clip, drawing its notes as notation; notes are edited in the MIDI Editor
/// or Event List.
pub fn show(app: &mut SoundApp, ctx: &egui::Context) {
let mut open = app.ui.show_score;
if !open {
@@ -119,7 +121,14 @@ pub fn show(app: &mut SoundApp, ctx: &egui::Context) {
p.add(Shape::ellipse_filled(head.center(), head.size() * 0.5, ink));
}
if sharp {
p.text(pos2(x - 12.0, y), Align2::CENTER_CENTER, "♯", regular(12.0), ink);
// An original drawn sharp sign: no bundled family covers U+266F.
let cx = x - 12.0;
for dx in [-2.6, 1.2] {
p.line_segment([pos2(cx + dx - 1.0, y - 5.0), pos2(cx + dx + 1.0, y + 5.0)], Stroke::new(1.1, ink));
}
for dy in [-1.8, 1.8] {
p.line_segment([pos2(cx - 5.0, y + dy), pos2(cx + 5.0, y + dy - 1.0)], Stroke::new(1.1, ink));
}
}
if q < 3.5 {
// Stems up below the middle line of each staff, down above it.
+18 -3
View File
@@ -54,17 +54,32 @@ fn search_chord(m: Modifiers, mac: bool) -> bool {
mac && m.command && m.ctrl
}
/// Dispatch global keyboard shortcuts once no text field or dialog wants input, ignoring non-scripted auto-repeats
/// while still running a host press alongside a scripted one.
pub fn handle(app: &mut SoundApp, ctx: &egui::Context) {
// Cmd+= and Cmd+0 are Mix/Edit and Quantize to Grid, not egui's built-in interface zoom.
ctx.options_mut(|o| o.zoom_with_keyboard = false);
if ctx.egui_wants_keyboard_input() || app.dialogs.open.is_some() {
return;
}
// A scripted press is identified by its exact position in this pass's events, not by matching
// key/modifier values. A fresh host press with the same chord must still run its own action in
// addition to the scripted one, while a host auto-repeat stays ignored: egui rewrites an
// event's `repeat` flag from the physical key state, which says nothing about whether that
// particular event was injected. Every non-scripted repeat is ignored as before.
let scripted = app.scripted_key_press_indices();
let events: Vec<(Key, Modifiers)> = ctx.input(|i| {
i.events
.iter()
.filter_map(|e| match e {
egui::Event::Key { key, pressed: true, repeat: false, modifiers, .. } => Some((*key, *modifiers)),
.enumerate()
.filter_map(|(index, e)| match e {
egui::Event::Key { key, pressed: true, repeat, modifiers, .. } => {
if scripted.contains(&index) {
Some((*key, *modifiers))
} else {
(!*repeat).then_some((*key, *modifiers))
}
}
_ => None,
})
.collect()
@@ -241,7 +256,7 @@ fn edit_mode_key(app: &mut SoundApp, pressed: Key) {
let ed = &app.engine.session().edit;
let keep_grid = ed.edit_mode.is_grid() && ed.flag("options.edit_tool_mode_keyboard_lock");
// F4 pressed first in an F4+F1–F3 chord switched the Grid mode on its own; put that back.
let flipped = egui::Id::new("edit_mode_key_grid_before_f4");
let flipped = egui::Id::unique("edit_mode_key_grid_before_f4");
let before =
(ed.edit_mode.is_grid() && !keep_grid).then(|| if ed.flag(soundcraft_engine::edit::RELATIVE_GRID) { "relative_grid" } else { "grid" });
let restore = match &ctx {
+8 -5
View File
@@ -113,15 +113,17 @@ fn recorded_at(s: &Session, time_reference: u64) -> Samples {
i64::try_from(time_reference).unwrap_or(0).saturating_sub(s.timecode_start)
}
/// One editable Spot field: accept the entered position when it parses and lies in `-MAX_POSITION..=MAX_POSITION`,
/// or show the formatted value when read-only.
fn spot_field_row(ui: &mut Ui, s: &Session, d: &mut Spot, i: usize, label: &str) {
let show = |at: Samples, f: TimeFormat| s.format_position(at, f);
ui.label(label);
let lab = ui.label(label);
if d.editable(i) {
let id = Id::new(("spot_field", i));
let id = Id::unique(("spot_field", i));
if !ui.memory(|m| m.has_focus(id)) {
d.text[i] = show(d.value(i), d.format);
}
let r = ui.add(TextEdit::singleline(&mut d.text[i]).id(id).font(mono(13.0)).desired_width(150.0));
let r = ui.add(TextEdit::singleline(&mut d.text[i]).id(id).font(mono(13.0)).desired_width(150.0)).labelled_by(lab.id);
if r.changed()
&& let Ok(v) = s.parse_position(&d.text[i], d.format)
&& (-MAX_POSITION..=MAX_POSITION).contains(&v)
@@ -152,12 +154,13 @@ fn original_time_stamp_row(ui: &mut Ui, s: &Session, d: &mut Spot, at: Samples)
/// The dialog's fields; the caller adds OK and Cancel.
pub fn fields(ui: &mut Ui, s: &Session, d: &mut Spot) {
egui::Grid::new("spot").num_columns(2).spacing(vec2(10.0, 6.0)).show(ui, |ui| {
ui.label("Time Scale");
egui::ComboBox::from_id_salt("spot_scale").selected_text(d.format.label()).show_ui(ui, |ui| {
let scale = ui.label("Time Scale");
let time_scale = egui::ComboBox::from_id_salt("spot_scale").selected_text(d.format.label()).show_ui(ui, |ui| {
for f in TimeFormat::ALL {
ui.selectable_value(&mut d.format, f, f.label());
}
});
time_scale.response.labelled_by(scale.id);
ui.end_row();
for (i, label) in FIELDS.into_iter().enumerate() {
if i == 1 && matches!(d.target, Target::Trim { .. }) {
+19 -11
View File
@@ -73,6 +73,7 @@ fn draw_mode_cell(p: &egui::Painter, cell: Rect, resp: &egui::Response, t: &Toke
}
}
/// The Shuffle, Spot, Slip and Grid mode selector with its keyboard hints and relative-grid state.
fn edit_modes(app: &mut SoundApp, ui: &mut Ui) {
let t = Tokens::current();
let ed = &app.engine.session().edit;
@@ -92,10 +93,10 @@ fn edit_modes(app: &mut SoundApp, ui: &mut Ui) {
let ch = inner.height() / 2.0;
for (m, cx, cy, label, fkey) in modes {
let cell = Rect::from_min_size(pos2(inner.min.x + cw * cx as f32, inner.min.y + ch * cy as f32), vec2(cw, ch)).shrink(1.0);
let resp = ui.interact(cell, ui.id().with(("mode", label)), Sense::click());
let resp = ui.interact(cell, ui.scope_id().with(("mode", label)), Sense::click());
draw_mode_cell(ui.painter(), cell, &resp, &t, m, &state, label);
let on = state.cur == m || (m == EditMode::Grid && state.cur == EditMode::GridRelative);
resp.widget_info(|| egui::WidgetInfo::selected(egui::WidgetType::RadioButton, true, on, label));
resp.widget_info(|| egui::WidgetInfo::selected(egui::Role::RadioButton, true, on, label));
if resp.clicked() {
mode_click(app, m, ui.input(|i| i.modifiers));
}
@@ -164,6 +165,7 @@ fn zoom_group(app: &mut SoundApp, ui: &mut Ui) {
});
}
/// The tool selector, including Smart-tool membership and Zoom Toggle.
fn tools(app: &mut SoundApp, ui: &mut Ui) {
let t = Tokens::current();
let cur = app.engine.session().edit.tool;
@@ -195,7 +197,7 @@ fn tools(app: &mut SoundApp, ui: &mut Ui) {
let bx0 = rr.min.x + 76.0;
let bx1 = bx0 + 38.0 * 3.0 - 2.0;
let bracket = Rect::from_min_max(pos2(bx0 - 2.0, r.min.y + 1.0), pos2(bx1 + 2.0, r.min.y + 5.0));
let resp = ui.interact(bracket.expand2(vec2(0.0, 2.0)), ui.id().with("smart"), Sense::click());
let resp = ui.interact(bracket.expand2(vec2(0.0, 2.0)), ui.scope_id().with("smart"), Sense::click());
ui.painter().rect_filled(bracket, 1.0, if cur == Tool::Smart { t.accent } else { t.tool_bracket_off });
if resp.on_hover_text("Smart Tool (F6+F7 / F7+F8)").clicked() {
let _ = app.run("edit.tool", json!({"tool": "smart"}));
@@ -230,6 +232,7 @@ fn fmt_pos(app: &SoundApp, at: i64, f: TimeFormat) -> String {
s.format_position(at, f)
}
/// Main and sub position counters, a main-counter format menu, selection readouts and dirty/device status.
fn counters(app: &mut SoundApp, ui: &mut Ui) {
let t = Tokens::current();
let (main_fmt, sub_fmt, sel) = {
@@ -242,7 +245,7 @@ fn counters(app: &mut SoundApp, ui: &mut Ui) {
let main_r = Rect::from_min_max(r.min, pos2(r.min.x + 180.0, r.min.y + 44.0));
let txt = fmt_pos(app, pos, main_fmt);
ui.painter().text(pos2(main_r.max.x - 18.0, main_r.center().y), Align2::RIGHT_CENTER, txt, mono(26.0), t.counter_text);
let resp = ui.interact(main_r, ui.id().with("main_counter"), Sense::click());
let resp = ui.interact(main_r, ui.scope_id().with("main_counter"), Sense::click());
icons::draw(ui.painter(), Rect::from_center_size(pos2(main_r.max.x - 9.0, main_r.center().y), vec2(10.0, 10.0)), "triangle_down", t.text_dim);
egui::Popup::menu(&resp).show(|ui| {
for f in TimeFormat::ALL {
@@ -273,6 +276,7 @@ fn counters(app: &mut SoundApp, ui: &mut Ui) {
ui.painter().text(pos2(r.max.x - 8.0, y), Align2::RIGHT_CENTER, format!("{st} · {dev}"), regular(10.0), t.text_dim);
}
/// Grid-line toggle, grid value menu and nudge value menu.
fn grid_nudge(app: &mut SoundApp, ui: &mut Ui) {
let t = Tokens::current();
let (grid, nudge, lines) = {
@@ -282,7 +286,7 @@ fn grid_nudge(app: &mut SoundApp, ui: &mut Ui) {
let (r, _) = ui.allocate_exact_size(vec2(170.0, 64.0), Sense::hover());
ui.painter().rect(r, CornerRadius::same(4), t.counter_bg, Stroke::new(1.0, t.counter_border), StrokeKind::Inside);
let gl = Rect::from_min_size(pos2(r.min.x + 6.0, r.min.y + 8.0), vec2(48.0, 16.0));
let resp = ui.interact(gl, ui.id().with("grid_lines"), Sense::click());
let resp = ui.interact(gl, ui.scope_id().with("grid_lines"), Sense::click());
key(ui.painter(), gl, &resp, if lines { t.mode_on } else { t.counter_off });
ui.painter().text(gl.center(), Align2::CENTER_CENTER, "Grid", bold(11.0), if lines { t.mode_on_text } else { t.mode_off_text });
if resp.on_hover_text("Show grid lines").clicked() {
@@ -290,12 +294,12 @@ fn grid_nudge(app: &mut SoundApp, ui: &mut Ui) {
}
let gr = Rect::from_min_max(pos2(r.min.x + 60.0, r.min.y + 6.0), pos2(r.max.x - 4.0, r.min.y + 26.0));
ui.painter().text(pos2(gr.max.x - 14.0, gr.center().y), Align2::RIGHT_CENTER, grid.label(), mono(11.0), t.counter_text);
let gresp = ui.interact(gr, ui.id().with("grid_value"), Sense::click());
let gresp = ui.interact(gr, ui.scope_id().with("grid_value"), Sense::click());
value_menu(app, &gresp, "view.grid");
ui.painter().text(pos2(r.min.x + 10.0, r.min.y + 44.0), Align2::LEFT_CENTER, "Nudge", regular(11.0), t.counter_label);
let nr = Rect::from_min_max(pos2(r.min.x + 60.0, r.min.y + 34.0), pos2(r.max.x - 4.0, r.min.y + 54.0));
ui.painter().text(pos2(nr.max.x - 14.0, nr.center().y), Align2::RIGHT_CENTER, nudge.label(), mono(11.0), t.counter_text);
let nresp = ui.interact(nr, ui.id().with("nudge_value"), Sense::click());
let nresp = ui.interact(nr, ui.scope_id().with("nudge_value"), Sense::click());
value_menu(app, &nresp, "view.nudge");
for rr in [gr, nr] {
icons::draw(ui.painter(), Rect::from_center_size(pos2(rr.max.x - 6.0, rr.center().y), vec2(10.0, 10.0)), "triangle_down", t.text_dim);
@@ -353,7 +357,8 @@ fn transport(app: &mut SoundApp, ui: &mut Ui) {
let (r, resp) = ui.allocate_exact_size(vec2(38.0, 28.0), Sense::click());
key(ui.painter(), r, &resp, if recording { Color32::from_rgb(110, 30, 30) } else { key_fill(&resp) });
icons::draw(ui.painter(), r.shrink(5.0), "record", t.rec);
let resp = resp.on_hover_text("Record (Cmd+Space)");
// macOS gives Cmd+Space to Spotlight unless the user frees it, so name F12 first.
let resp = resp.on_hover_text("Record (F12 or Cmd+Space)");
if let Some(why) = app.record_hint() {
// Below the whole group, so the hint covers none of its buttons.
let below = Rect::from_min_max(r.min, pos2(r.max.x, g.max.y));
@@ -380,6 +385,8 @@ fn transport(app: &mut SoundApp, ui: &mut Ui) {
});
}
/// Count-off, meter and tempo readouts; the tempo value is editable in place for the tempo event at or before the
/// playhead.
fn tempo_meter(app: &mut SoundApp, ui: &mut Ui) {
let t = Tokens::current();
let (bpm, meter, countoff, click, bars) = {
@@ -391,7 +398,7 @@ fn tempo_meter(app: &mut SoundApp, ui: &mut Ui) {
let (r, _) = ui.allocate_exact_size(vec2(150.0, 64.0), Sense::hover());
ui.painter().rect(r, CornerRadius::same(4), t.counter_bg, Stroke::new(1.0, t.counter_border), StrokeKind::Inside);
let co = Rect::from_min_size(pos2(r.min.x + 6.0, r.min.y + 6.0), vec2(64.0, 15.0));
let resp = ui.interact(co, ui.id().with("countoff"), Sense::click());
let resp = ui.interact(co, ui.scope_id().with("countoff"), Sense::click());
key(ui.painter(), co, &resp, if countoff { t.mode_on } else { t.counter_off });
ui.painter().text(co.center(), Align2::CENTER_CENTER, "Count Off", bold(10.0), if countoff { t.mode_on_text } else { t.mode_off_text });
if resp.clicked() {
@@ -405,7 +412,7 @@ fn tempo_meter(app: &mut SoundApp, ui: &mut Ui) {
tempo_value(app, ui, r.max.x - 8.0, r.min.y + 48.0, bpm);
let clk = Rect::from_min_size(pos2(r.min.x + 6.0, r.min.y + 40.0), vec2(18.0, 18.0));
icons::draw(ui.painter(), clk, "click", if click { t.counter_text } else { t.text_dim });
if ui.interact(clk, ui.id().with("click"), Sense::click()).on_hover_text("Click").clicked() {
if ui.interact(clk, ui.scope_id().with("click"), Sense::click()).on_hover_text("Click").clicked() {
let _ = app.run("options.click", json!({}));
}
}
@@ -426,7 +433,8 @@ fn tempo_value(app: &mut SoundApp, ui: &mut Ui, right: f32, y: f32, bpm: f64) {
}
v.widgets.hovered.bg_stroke = Stroke::new(1.0, t.counter_border);
let mut value = bpm;
let resp = c.add_sized(rect.size(), egui::DragValue::new(&mut value).speed(0.1).range(5.0..=1000.0).fixed_decimals(4));
let resp =
c.add_sized(rect.size(), egui::DragValue::new(&mut value).speed(0.1).range(5.0..=1000.0).fixed_decimals(4)).accessible_name("Tempo (BPM)");
if resp.changed() {
let s = app.engine.session();
let tick = s.tempo.samples_to_ticks(app.position(), s.sample_rate);
Loaded 100 of 160 files, more files were not shown because too many files have changed in this diff. Show more