Compare commits

..
Author SHA1 Message Date
Alexandru CarausandClaude Opus 5.5 a02d892f83 MIDI input devices: live play-through and MIDI recording (#493)
* MIDI input devices: live play-through and MIDI recording

Keyboards and pads could not be used at all: there was no MIDI device
layer (G-02).

- crates/playback/src/midi.rs opens every MIDI input port through midir
  (ALSA sequencer, CoreMIDI, WinMM), follows devices that are plugged in or
  removed, and passes channel messages to the audio thread over a lock-free
  ring, each with the time it arrived.
- The mix engine plays them on the instruments of record-enabled and
  input-monitored MIDI and instrument tracks (crates/mix/src/live_midi.rs):
  notes at their sample offset, the sustain pedal holding note-offs back.
- While recording, the events are kept with their timeline position and
  become MIDI clips on the record-enabled tracks
  (crates/engine/src/io/midi_take.rs), with controllers, pitch bend and
  aftertouch. A pass can be MIDI only: the audio input is opened for audio
  tracks alone.
- A MIDI or instrument track chooses a device and a channel at its input
  selector (kept in the track's input route as `MIDI:<channel>:<device>`).
- Setup > Playback Engine lists the input ports with a switch each and, on
  request, what arrives from one; Setup > MIDI > MIDI Input Devices opens
  it. The switches are app preferences.

Note lifecycle:

- A strip keeps the keys and the sustain pedal of every owner (input port
  and channel) apart. A pitch held by two devices or channels sounds until
  the last one releases it, and one owner's pedal does not hold another's
  notes. A strip follows 16 owners at a time; further ones share the last
  place (documented in live_midi.rs).
- A full queue is never silent. An event that does not reach the audio
  thread (the 1,024-entry input ring or the 256 events a callback takes)
  marks its port; the audio thread then drops what it still has queued from
  that port and the engine releases the notes and pedal the port holds. A
  port that closes, a replaced feed and events skipped while nothing
  listens take the same path.
- A take records where input was dropped: the notes that port held end
  there, and the status line says the take is incomplete. Recorded events
  the UI thread did not collect in time are counted and reported too.
  Recorded notes are told apart by device as well as channel and pitch.

On Linux, live play was checked with a hardware keyboard (Akai MPK mini 3)
and recording with a virtual one (crates/playback/examples/midi_keys.rs),
before the note-lifecycle work; that part is covered by tests only. macOS
and Windows are type-checked only; there is no MIDI input on FreeBSD or
the web.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>

* midi real device tests

* midi device drop test

* midi device fix pr review comments

* MIDI input: review fixes for note ownership, lost input and takes

Live notes (crates/mix/src/live_midi.rs):

- An owner without a place of its own joins the shared place while it is
  in use, even when another place has come free. It could get a second
  place before, and the release of its key in the shared place was then
  not found: the note stayed on. The same held for the pedal.
- A change of the track's input releases the shared place when the input
  no longer takes any one of the owners in it, not only the first.
- The strip remembers which port and channel last set each controller.
  When that source loses its input or the track stops listening to it,
  the instrument's controllers are set again from the clips and from the
  sources that are left, so a bend from an unplugged device does not stay.
- When playback runs out under monitoring, the clips' sustain pedal goes
  up with their notes.

Capture (crates/playback):

- A loss carries its time. What a port sends after it is played and comes
  after the mark in the take; before, it was dropped and recorded ahead
  of the mark.
- The age of an event is counted at the transport speed.
- Loop recording writes a mark where the loop goes round, and a key played
  with the playhead at the end of the loop stays inside the loop.
- The end of a record pass is acknowledged by its number, so the late
  answer to an earlier pass does not end the wait for a later one.
- A connection the backend closes marks its port, also when a device with
  the same name is opened in its place.

Takes (crates/engine/src/io/midi_take.rs, crates/ui-egui):

- A note held into the next round of a loop is split there, also when its
  release is later on the timeline than its press.
- A track taken out of record during a pass keeps what it recorded.
- A pass whose end the audio engine did not confirm is reported as
  incomplete in the status line.
- An inactive record-enabled audio track no longer turns a MIDI pass into
  an audio one that is then refused.

docs/midi-input.md: Windows drivers that allow one program per device, and
the new status text.

* MIDI input: review follow-ups on loss order, controllers and loop takes

- Each port has its own time of loss. One port's later loss no longer
  takes along what another port sent after its own: that is played, and
  comes after that port's mark in the take.
- A retired connection is closed before its port is marked as lost. Marked
  first, a note-on delivered while it closed carried a later time than the
  loss and could be played after the release.
- The strip keeps the last controller value of every source, not only the
  newest. When one of two sources that set the same controller goes away,
  the controller goes back to the value of the one that stays. 64 values
  are kept; past that the oldest is forgotten.
- A take can say where the pass stopped (MidiTakeEvent::END), and the UI
  says so for loop passes. A key still down then ends at the stop in the
  round it is in: held over a round or more it covers the loop, where it
  used to end at the loop end of its first round.
- A pass that records MIDI does not start while the audio engine has not
  confirmed the end of the previous one. It takes the MIDI since its last
  callback with the state of the pass it knows, so a pass started and
  stopped meanwhile had its notes written at the old position.

* MIDI input: loss marks in the queue, connection gate, stop position

- A loss from a full queue is also written into the queue, ahead of the
  first event that fits again. The audio thread deals with it at that
  place. Before, a loss flagged after the callback had read the flags was
  seen one callback late and ended a note played after it. The flag for a
  loss already met in the queue is not a second loss.
- A connection is shut off under the feed's lock when it is closed
  (MidiFeed::close_port), and what its device thread still delivers is
  discarded. Dropping a connection joins that thread on ALSA; CoreMIDI
  (MIDIPortDispose) and WinMM (midiInStop, midiInReset, midiInClose) do not
  promise to, so the order of drop and mark is no longer relied on.
- When the controller table is full and forgets a value, the strip keeps
  the port and channel it came from. The controllers are still reset when
  that source goes away, where a bend pushed out of the table used to stay.
- A loop pass ends where the audio engine is. The larger of the engine's
  and the UI's position was used, and just after the loop went round the
  UI's is still near the loop end: a short held note covered the loop.
- The end of a pass waits for the audio engine by what the pass set out to
  record, not by the devices connected at Stop. A pass whose last device
  was unplugged before Stop left nothing pending, and a new pass could
  start before the engine was done with it.

* MIDI input: stop at the loop end, and passes owed by the audio engine

- A key played while the playhead stands at the end of the loop is the
  last thing of that round. It was written as the first of the next round,
  with a mark that the loop had gone round; a pass stopped right there
  never went round, and a note held to the stop grew a second part.
- The feed knows whether an ended pass is still owed by the audio engine
  (MidiFeed::pass_owed), and the UI asks it before a pass that records
  MIDI starts. The UI kept that itself, where no test could stall the
  audio callback against it.

* MIDI input: stop boundary, loop rounds, loss marks and shared owners

- A pass ends at a time on the feed's clock (MidiFeed::end_pass). Only
  what arrived up to it is written to the take, however late the audio
  thread hears of the Stop, and the callback takes the queue in once more
  after the command before it confirms: a note that arrived between its
  reading the queue and the Stop was missing from a confirmed take.
- The confirmation carries where the playhead stood (MidiFeed::stopped_at).
  A loop pass ends there; the player's position read afterwards could be
  a round further on, which the take knows nothing about. Without a
  confirmation no stop position is used.
- The take builder is told whether the pass looped (MidiPass). In a pass
  that did not, a release stamped before its key press (a late callback)
  is one short note, where it was split around the ends of the clip. The
  stop position is a parameter, not an entry that the event cap could cut.
- A pass records the tracks that were in record when it started, and only
  those: the same list the end of the pass waits by. A track put in record
  during the pass got what was played before that, without a wait.
- A short loop goes round several times in a callback: each entry is put
  in the round it arrived in, so two entries a round apart have one mark
  between them.
- A mark in the queue is stamped with the time of its loss. A port that
  overflowed, played on and was then closed keeps a mark for the close
  (the new note was held to the end of the take), and a loss told by the
  port's flag is not written again when its mark arrives.
- Live events stay in order of arrival when they are played: a release
  taken by a shorter callback was due before its note-on.
- A callback reads the transport speed once, for rendering and for the
  ages of the next callback's events.
- The shared owner place is left alone by a source that never joined it:
  its note-off no longer ends another owner's note there, and its loss no
  longer releases the place.
- When a live source goes away, the instrument's controllers are restored
  before the block's clip events are sent, so a clip controller at the
  start of that block keeps its new value.
- Tests that assert MIDI positions use a clock the test sets.

* MIDI input: pass boundaries, loop punch-in and review follow-ups

- A pass begun while the transport runs has a start time on the feed
  (MidiFeed::begin_pass): what is played after Record is written down
  also when the audio thread takes it in before it reads the command.
- A pass goes on through a transport stop until the command that ends it.
  The UI sends the two one after the other; when they reached two
  callbacks, a note played just before the stop was left out of a
  confirmed take.
- The end of a pass is asked for every entry, so a Stop that comes while
  the queue is being read still cuts off what follows it.
- A mark in the queue carries the time its events were dropped, kept apart
  from a later close of the port. An overflow before Stop is in the take
  when the port closes after it.
- A pass punched in while a loop plays is a loop take over that loop. In a
  line from the punch-in to a stop in the next round nothing fell inside
  the range.
- In a loop take with marks, only the marks say that the loop went round:
  a release stamped before its press within one round is one short note.
- A key pressed in the last samples of the range starts at the last tick
  the clip plays, not at the tick of its end.
- When the clips' controllers are sent again while stopped, their sustain
  pedal goes up again: no pedal-up of theirs would follow.
- The render step is taken after the commands, so a session at another
  sample rate is rendered at that rate from the callback that installs it.
- The stop position and the incomplete-take warning go by one answer from
  the audio engine.

* MIDI input: test that a callback renders at the speed it keeps

A speed change that arrives while a callback runs takes effect from the
next one: the callback renders at the speed it read, and the next callback
counts the ages of its events at that same speed.

* MIDI input: volume and pan go back to MIDI's defaults with their device

The reset sent when a live source goes away is Reset All Controllers,
which leaves the channel volume (CC 7) and the pan (CC 10) alone. A
device that turned the volume down to nothing and was unplugged left the
track silent. When the source that went away had set one of the two, and
neither the clips nor a source that is still there gives it a value, the
instrument is sent the value MIDI names as the default (100, and 64).
The same when the track stops playing live input.

* MIDI input: third round of review follow-ups

- The stop cutoff is one value on the feed, in force from the store that
  ends the pass and gone when the audio thread confirms it.
- The start of a pass holds for the whole pass: what was played before
  Record stays out also when it is taken in after the start command.
- What is played just before a Stop is put inside the range that ends at
  the stop, not on its end.
- A port closed while the queue is read is met through the mark of its
  overflow; the flag of that close, a callback later, is not a second
  loss (it ended a key the new connection played).
- An overflow and the loss it is have one time. Read apart, they could
  differ by a microsecond and count as two losses.
- A volume or pan the full controller table forgot goes back to its
  default when its own device goes, not when another does.
- The chase of the clips' controllers uses the track's MIDI delay, as
  playing them does.
- Recorded controllers in the last samples of the range stay where the
  clip plays; in a take without marks the press as played, not the note's
  start, says whether the loop went round; a take cut at the event cap
  does not use the stop position.
- The loop the MIDI of a punched-in pass goes to is the loop the audio
  engine plays: not a range too short to loop, nor one that Play From
  dropped.
- The audio input stops taking in before the wait for the MIDI.

* MIDI input: fourth round of review follow-ups

- A record pass goes on through the natural end of playback until the UI
  ends it, up to the time playback ran out: what was played as it reached
  the end is in the take, what follows is not.
- Before the end of a pass is confirmed, the device threads are kept out
  of the queue while it is read once more, so a message stamped before
  the Stop is not put in after the answer. The audio thread does not wait
  for a device thread that is in there.
- A later loss of a port is noted as dealt with only at the end of the
  callback: with two overflow marks of one port in the queue, the second
  was skipped and the key played between them had no end.
- Whether a pass was begun is read before when: a pass seen as begun has
  its own start time.
- The volume and the pan go back to their defaults when the source that
  set them last goes, also when the controller table forgot its value,
  and not when an earlier setter goes.
- A clip's note-off that is held back because a live key holds the pitch
  is remembered for the block: the key going up earlier in the block no
  longer ends the note before the clip would have.
- In a take without marks, whether the loop went round between a press
  and its release is judged by their samples, not their ticks.
- The stop position of a loop pass is not used when recorded entries did
  not reach the take: one may be the mark of a round.
- Loop recording over a selection too short to loop is recorded as a
  punch, as the transport plays it.

* MIDI input: fifth round of review follow-ups

- Whether a pass was begun is asked for every event: a pass begun while
  the callback is reading the queue records from when it began.
- The ages of a pass's last events are counted up to when the transport
  stopped. Counted up to the callback that takes them in, the time the
  playhead stood still moved them back.
- An overflow the port's flag told of keeps its time when an older mark
  in the queue shows a later close: its own mark may never come.
- What becomes of the clips' held back note-offs is settled after the
  block's live events. A key that goes down again before the clip's note
  would have ended is not ended by it, and with two clip notes of a pitch
  in a block the one that sounds when the key goes up lasts to its end.
- When the source that set the volume or the pan last goes and another
  one's value is sent again, that one is the last setter from then on.

* MIDI input: sixth round of review follow-ups

- A loss is followed whenever the transport runs, and written to the take
  by when it happened, like an event: a port lost in a pass that began
  while the queue was being read ends its notes in the take.
- A pass started and ended by the commands of one callback is still that
  pass in the last read of the queue: what was played in it is kept.
- When a live key takes a pitch again, the clips' held back note-offs
  from since the last key went up are sent where they were, so the
  instrument lets go of the clip's note before the new press.

---------

Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-11-19 08:37:06 +07:00
ljkx 0089a99b92 Keep locked clips whole when a range is cleared or a clip is placed over them (#1532)
* Keep locked clips whole when a range is cleared or a clip is placed over them

clear_range removed a time- or edit-locked clip lying entirely inside the
range, so edit.clear and edit.cut deleted it (#1434), and place_clip, which
clears what the new clip covers, destroyed it on import, record, Place Audio
File and paste (#1435). Locked clips now stay whole and a placed clip
overlaps them. In Shuffle a locked clip left in the range holds the clips
after it. Consolidate and Prepare DPE Tracks refuse a range holding a locked
clip, since they would render it into the new clip. Event Cut Time keeps
removing locked clips through clear_all_range.

Fixes #1434
Fixes #1435

* Format the placing test

* Keep a locked clip off the clipboard on Cut and out of Move Selection

Cut and the Move Selection To commands copied the whole range, then cleared it. Since a
time- or edit-locked clip now stays in place, it ended up both in place and on the
clipboard (or the destination playlist), so pasting duplicated it. Both now carry only
what the clear removes (`take_range`). Copy and Copy Selection To still copy locked clips.

Also pin down that a Shuffle clear over several tracks holds only the track whose
locked clip the range covers part of.
2026-11-18 17:03:18 -06:00
Javier Brines GarcíaandClaude Fable 5.2 65daf0e8e1 fix(automation): Remote drops a connection whose reply is not its own (#894) (#1538)
* fix(automation): Remote drops a connection whose reply is not its own (#894)

`Remote::roundtrip` returned whatever line came next without comparing
its `id` with the request, and a read timeout left the connection open.
A reply that arrived after the timeout was then read as the answer to
the next call, and every later call stayed one behind.

- A reply answers a request only with that request's id. Another id, or
  a success with none, is `RemoteError::OutOfStep`.
- A read timeout, a reply that is not JSON and an out-of-step reply end
  the connection, like a lost one: a call that changes state is reported
  as unconfirmed and not repeated, a read is asked once more on a new
  connection. A second failure also leaves no connection behind.
- A refusal without an id (too many connections, a line the app cannot
  read) is still the app's error, word for word; the app closes that
  connection, so it is not used again.

The read timeout is now a private field so tests can shorten it; the
default stays 120 s.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>

* fix(automation): a mutation sent on the second attempt is unconfirmed too (#894)

When the first attempt could not be written to a dead connection, the
call is sent on a new one. If that attempt went out and its reply was
lost, the error came back bare, without the warning that the call may
have run. Both attempts now report it the same way, and both drop a
connection they lose.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>

* fix(automation): a reply that is not UTF-8 ends the connection (#894)

`read_line` fails with `InvalidData` on a reply that is not UTF-8,
before any JSON parsing, and that error kept the connection. It now
counts as a lost connection, like a reply that is not JSON: a call that
changes state is unconfirmed and a read is asked once more.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>

---------

Co-authored-by: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-18 16:56:15 -06:00
FredApps 56d5c38547 Merge pull request #1479 from ljkx/feat/frequency-shifter
Add a built-in Frequency Shifter plugin
2026-11-18 20:53:20 +07:00
FredApps 7289de8e07 Merge main into this branch
Signed-off-by: FredApps <153173854+FredApps@users.noreply.github.com>
2026-11-18 20:38:48 +07:00
FredApps 4e50956769 pt-BR: translate the Frequency Shifter's name and short name
Every built-in plugin's name and short name has a row in the pt-BR catalog; add the Frequency Shifter's.

Signed-off-by: FredApps <153173854+FredApps@users.noreply.github.com>
2026-11-18 20:38:48 +07:00
FredApps 42d7fa390b sonar: leave the locale catalogs out of duplication detection (#1631)
The pt-BR catalog (#1346) is one match of string-literal rows, which Sonar's copy-paste
detection reports as 2028 of the 2134 duplicated new lines, failing the Quality Gate on main
since 2026-11-16. Exclude crates/ui-egui/locales/** from duplication only, as gridcraft does
for its region table; the catalogs stay analysed otherwise.

Fixes #1630

Signed-off-by: FredApps <153173854+FredApps@users.noreply.github.com>
2026-11-18 20:21:31 +07:00
FredApps 62ace3e0fe Merge main into this branch
Docs-only conflicts: the status lines and history rows keep both main's Linux/X11 plugin editor and Command-click entries and this branch's Frequency Shifter entries.

Signed-off-by: FredApps <153173854+FredApps@users.noreply.github.com>
2026-11-18 17:17:26 +07:00
FredApps b5392da2a2 Merge pull request #1395 from MMexicann/fix/command-click-automation-point
Add automation points with Command-click
2026-11-18 17:11:17 +07:00
FredApps 0bf8fe27b9 Merge main into this branch
Signed-off-by: FredApps <153173854+FredApps@users.noreply.github.com>
2026-11-18 16:49:01 +07:00
lucasegen d570045b63 Keep the crossfade when a clip of it is processed in place (#1379)
- Keep the crossfade when a clip of it is processed in place
- test: verify processed crossfades preserve rendered overlap
- Test that a clip inside or around the processed clip is overwritten
- Keep only the other half of a crossfade when replacing a processed clip
- Build: take upstream's fixes for main (#1522, #1523)
2026-11-18 16:41:35 +07:00
lucasegen c27ccac979 Mark the selection in the ruler with a split arrow and keep the edit cursor drawn while playing (#973)
- Mark the selection in the main ruler with a split arrow
- Keep the insertion point drawn while playing
- Tighten the playback bound in the track-click test
- Build: take upstream's fixes for main (#1522, #1523)
- Look for the neutral playback line beside the insertion point
2026-11-18 16:41:14 +07:00
NetterSilver 248f0e4e25 Edit window: the Delete and Backspace keys clear the selection (#404) (#480)
- Edit window: the Delete and Backspace keys clear the selection (#404)
- Docs: update the progress docs for this change
- Docs: put the new sentence in the Status column
- fix: Delete/Backspace follow the drawn Edit window and a valid MIDI target
2026-11-18 16:36:33 +07:00
FredApps dc21f5ee10 Merge pull request #1472 from ljkx/feat/output-window
Edit window: the Output fader button opens a floating Output window (#1392)
2026-11-18 16:36:13 +07:00
FredApps 0c85e784d2 Merge pull request #533 from ayozetr/linux-plugin-editors
Plugin editor windows on Linux (VST3 and embedded CLAP, X11)
2026-11-18 16:36:01 +07:00
lucasegen 8ee63dc695 Let a soloed VCA solo its members (#800)
Use the effective solo state of active VCA members consistently in the mixer and implied-mute display. Preserve aux/output and send-return routing, empty-VCA behavior, Solo Safe and inactive-track semantics. Add VCA member, empty-master, aux-output, send-return and inactive-source regressions.
2026-11-17 22:13:46 -06:00
Dylan M. Taylor b47a58653c Merge pull request #1607 from storytold/chore/score-merged-pin
chore(score): pin writers to merged shared revision
2026-11-17 03:19:04 -05:00
Dylan M. Taylor 19acf6f21a chore(score): pin writers to merged shared revision 2026-11-17 02:19:12 -05:00
Dylan M. Taylor 16bd7327f3 docs: prefer craft-libs for reusable formats, codecs, protocols and UI (#1592) 2026-11-16 21:59:50 -05:00
Dylan M. Taylor 9676d492db refactor(score): use shared MusicXML and SVG writers with contributor tie fixes (#1598) 2026-11-16 21:59:45 -05:00
Ayoze Torres 63dfe72b1f Merge remote-tracking branch 'upstream/main' into linux-plugin-editors
# Conflicts:
#	ROADMAP.md
#	docs/gaps.md
#	docs/roadmap.md
2026-11-17 02:30:21 +00:00
Mex 0500172d2b Merge branch 'main' into fix/command-click-automation-point 2026-11-16 21:24:12 +01:00
FredApps 648ed9db9a Merge pull request #1589 from storytold/extract/shared-mjpeg
video: decode Motion JPEG with craft-video's shared mjpeg codec
2026-11-16 13:42:06 +01:00
FredApps d0479e609c video: pin craft-video to craft-libs main ddbe7823 (Adobe APP14 transform fix, craft-libs#1847) 2026-11-16 13:26:44 +01:00
FredApps fc4dddd7c7 video: decode Motion JPEG with craft-video's shared mjpeg codec
Movie frames from MJPEG tracks were decoded with the image crate's JPEG
decoder, and the synthetic MJPEG fixtures were encoded with its encoder.
Both now go through craft_video::mjpeg (storytold/craft-libs#1757), a
frame-level wrapper over craft-jpeg. The picture size is still checked
from the header against MAX_PIXELS before any pixel is decoded, and grey
and 12-bit pictures map to RGBA8. soundcraft-video no longer depends on
the image crate.

craft-video is pinned at craft-libs main 341020b2 with the mjpeg feature.

Refs storytold/craft-libs#778, storytold/craft-libs#781.
2026-11-16 11:23:42 +01:00
FredApps 5976545fe1 Merge pull request #1585 from storytold/extract/shared-score-xml
Use shared XML escaping for MusicXML and SVG scores
2026-11-16 06:55:01 +01:00
FredApps 559c2b9fc2 Merge main into extract/shared-score-xml 2026-11-16 06:44:40 +01:00
ljkx 9fa3754a13 Merge remote-tracking branch 'origin/main' into feat/frequency-shifter
Conflicts in the doc headers and change logs of ROADMAP.md, docs/gaps.md, docs/roadmap.md and
docs/target-app-parity.md: kept main's pt-BR entries and this branch's Frequency Shifter
entries, newest first.
2026-11-16 03:24:20 +01:00
ljkx aba1cdcdfc Merge remote-tracking branch 'origin/main' into feat/output-window
The Edit window's output fader button now goes through main's UI localization: its
"Output window" tooltip uses crate::localization::text, with a pt-BR catalog entry.
2026-11-16 03:22:47 +01:00
John Peter Sa 4898ff03ee Add Brazilian Portuguese UI localization
Add a pt-BR catalog and persisted language selection, covering UI labels, menus, dialogs, tooltips, accessibility text, and engine menu labels, and update the localization parity docs.
2026-11-15 22:14:40 +01:00
Dylan M. Taylor 1a09c16295 Merge pull request #1586 from storytold/extract/shared-control-base64
Use shared Base64 encoding for control responses
2026-11-15 14:52:36 -05:00
Dylan M. Taylor 498849745a Reuse shared Base64 for control responses 2026-11-15 13:31:48 -05:00
Dylan M. Taylor 4bdda5ffad Reuse shared XML escaping for score exports 2026-11-15 13:23:47 -05:00
Andreas Haraminac 5d1fa9a69d Merge upstream main and preserve Command-click automation regressions 2026-11-14 16:06:47 +01:00
Zinadin Zidan 2688ccac88 io: import a MIDI file with stray bytes after its last track (#1528) (#1540)
* io: import a MIDI file with stray bytes after its last track (#1528)

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

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

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

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

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

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

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

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

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

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

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

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

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

Move the six craft-libs crates from the PR's commit 4db60c4 to its merge
into craft-libs main, 45a8e16. None of these crates, nor craft-zip which
craft-scraft-format uses, changed between the two.
2026-11-14 15:55:41 +01:00
Ayoze Torres 244dcd7767 Merge upstream main into linux-plugin-editors 2026-11-14 12:00:04 +00:00
Dylan M. Taylor ae652ab34b Merge pull request #1533 from storytold/fix/clip-name-pill-height
Fix clip name pill text fit
2026-11-13 09:03:26 -05:00
Dylan M. Taylor cd7ff05bdd Fix clip name pill text fit 2026-11-13 13:49:21 +00:00
FredApps 6d525c73d4 CI: use the shared git-sources check (#1509)
nix.yml ran its own copy of the git-sources job; it now calls
craft-common-actions git-sources.yml.
2026-11-13 14:04:32 +01:00
Dylan M. Taylor a8bbf12014 Merge pull request #1527 from storytold/codex/soundcraft-session-format
Use craft-libs for SoundCraft session files
2026-11-13 07:24:21 -05:00
Dylan M. Taylor ff66d48326 chore: pin shared session format to merged release 2026-11-13 12:12:38 +00:00
Dylan M. Taylor 48ea8ecfe4 Merge remote-tracking branch 'origin/main' into codex/soundcraft-session-format
# Conflicts:
#	Cargo.lock
#	Cargo.toml
2026-11-13 10:39:45 +00:00
Dylan M. Taylor 2aa2f66fd3 pin SoundCraft format to merged shared release 2026-11-13 10:29:15 +00:00
ljkx cd743a8d59 Merge remote-tracking branch 'origin/main' into feat/output-window
Conflicts: mix_window.rs keeps main's surround_panned (the same check as this branch's surround_out), which the Output window now calls; edit_window/header.rs imports both Gesture (main) and StrokeKind (this branch).
2026-11-13 11:17:58 +01:00
ljkx dd14d15f55 Merge remote-tracking branch 'origin/main' into feat/frequency-shifter 2026-11-13 10:42:15 +01:00
FredApps 1775ce5061 Merge pull request #974 from lucasegen/fix/clip-cut-seam
Edit window: butted clips share one seam, and the clip name is a small pill
2026-11-13 10:37:31 +01:00
FredApps e6b77d0314 Merge pull request #972 from lucasegen/feat/edit-io-volume-pan
Let the Edit window's I/O column set volume and pan
2026-11-13 10:33:33 +01:00
FredApps f0c7b35a4a Merge pull request #969 from lucasegen/fix/edit-mode-colors
Give each edit mode its own colour when it is active
2026-11-13 10:31:01 +01:00
Dylan M. Taylor 6b35582667 Merge pull request #1525 from storytold/codex/778-soundcraft-video
Use shared H.264 and ProRes codecs in SoundCraft
2026-11-13 04:09:41 -05:00
Dylan M. Taylor 869db7dc6a Pin shared video codecs to merged revision 2026-11-13 08:51:39 +00:00
Dylan M. Taylor ff9c9114f0 pin SoundCraft time to shared consolidation 2026-11-13 08:07:35 +00:00
ljkx 33917ea79a Output window: Close Window and Hide All Floating Windows close it
The shared window.close / window.hide_floating path clears it along with
the other floating windows.
2026-11-13 05:36:04 +01:00
ljkx 8a5647ab49 Output window: opening another session closes it and drops its draft
document_replaced now closes the Output window: a track of the new session
can have the id of the old one's track, and the window would show it. The
volume and pan readouts' ids also name the document, so a value typed for
the old session's track is not reused for a track of the new one.
2026-11-13 05:36:03 +01:00
Dylan M. Taylor acb547286d Move SoundCraft session format into craft-libs 2026-11-13 04:30:16 +00:00
Dylan M. Taylor 1f57bb19dc Fix test binary lookup in archived runs 2026-11-13 03:48:01 +00:00
Dylan M. Taylor 935505bb07 Move SoundCraft H.264 and ProRes codecs to craft-libs 2026-11-13 03:23:51 +00:00
Ayoze Torres 8ddda478fd Merge upstream main into linux-plugin-editors 2026-11-13 02:38:13 +00:00
Dylan M. Taylor e600ed6b0d Merge remote-tracking branch 'origin/main' into codex/778-soundcraft-video 2026-11-13 02:22:27 +00:00
ljkx 20c4038cb3 ui: strip_scan_running returns the cfg'd value directly
Newer clippy rejects an if guard that returns a bool literal and is
followed by another (crates/ui-egui/src/dialogs.rs:118), which fails
the gate on current stable for every branch based on main.
2026-11-13 02:21:19 +01:00
FredApps 2bde8b9dbc Merge pull request #971 from lucasegen/fix/playhead-record-colour
Draw the playback cursor neutral and keep red for recording
2026-11-13 02:17:51 +01:00
MarekSpodBiedry c542fe931d Hide the input selector on the Master strip (#1271) (#1356)
* Hide the input selector on the Master strip (#1271)

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

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

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

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

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

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

* Test: every outcome of relinking a movie

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

---------

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

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

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

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

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

---------

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

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

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

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

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

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

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

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

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

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

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

* Add missing VST3 paths for Windows

* Add missing VST3 paths for macOS

* Make os_search_paths private

* Use hermetic search_paths in unix test and remove os_search_paths

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

* Add docstring to env_from

---------

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

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

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

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

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

The protocol doc says so next to the error format.
2026-11-13 01:43:08 +01:00
ljkx 194f63b4b6 Merge remote-tracking branch 'upstream/main' into feat/frequency-shifter
# Conflicts:
#	ROADMAP.md
2026-11-13 01:40:12 +01:00
.CooCoo 4a5eb1514a Plugins: Alt-click returns a parameter to its default value (#1081)
* Strip Silence: the sliders move a tenth as far while Ctrl or Command is held

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

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

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

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

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

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

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

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

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

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

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

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

* Read send destinations only while the menu is open

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

The plugin window's logarithmic and whole-number sliders keep their value
when Ctrl is pressed or released during a drag, and a drag with Control
alone is fine. The tenth-as-far test names the parameters it drags and
checks the others do not change, and the low-end test checks the value
after the release.
2026-11-13 01:38:35 +01:00
ljkx 35cc20c457 ui: strip_scan_running returns the cfg'd value directly
Newer clippy rejects an if guard that returns a bool literal and is
followed by another (crates/ui-egui/src/dialogs.rs:118), which fails
the gate on current stable for every branch based on main.
2026-11-13 01:04:58 +01:00
ljkx ae5ec2ee54 Merge remote-tracking branch 'upstream/main' into feat/output-window 2026-11-13 01:04:11 +01:00
Ayoze Torres 7236035284 Merge upstream main into linux-plugin-editors 2026-11-12 21:47:05 +00:00
ljkx 3cc12a0758 Output window: the volume and pan readouts' ids include the track (#1392)
The window's id is the same for every track, so the readouts' ids were too.
A value being typed for one track stayed open when the window switched to
another track, and Enter then set that track instead. The ids now include
the track's id, as the Send window's per-slot ids do.

New test: a value typed into Kick's volume readout is not carried over when
the window shows Snare, and Enter there leaves Snare's volume alone. It
fails without the fix (Snare goes to -20 dB).
2026-11-12 20:15:22 +01:00
Dylan M. Taylor 6a0949d9d0 Review fixes after the craft-audio switch; MIDI files from craft-midi (#1513)
* Review fixes after the craft-audio switch; pin craft-audio at craft-libs main

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

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

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

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

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

The SMF and Sequence tests are deleted here; they run in craft-midi, which
also adds one for the track limit. The serde names do not change (the same
derives behind craft-midi's serde feature), so sessions read as before.
2026-11-11 19:51:41 -05:00
Dylan M. Taylor 4782cfa600 chore: bump to 0.20.1 for the Flatpak updater patch release (#1512) 2026-11-11 19:16:01 -05:00
Dylan M. Taylor 91952090ec Take audio file formats from craft-audio; remove crates/audio-io (#1506)
craft-audio in craft-libs was built from audio-io and FilmCraft's codecs, so
each format has one implementation that every Craft app shares. Call sites use
its API directly: EncodeOptions::new(format).with_*(), EncodeFormat,
DECODE_EXTENSIONS, ENCODE_EXTENSIONS, encode_format_for, AudioInfo's container,
codec and layout.

soundcraft_model::FileFormat stays as the kind of file a session records for
each source: its variant names are part of the session format. It maps from
craft-audio's AudioInfo.

craft-audio and craft-media are pinned to the craft-libs branch of
storytold/craft-libs#824, which adds the extension tables SoundCraft had in
FileFormat and fixes the quadratic WAV chunk scan (#893).

Behaviour that changes: AAC, ALAC, Opus, AC-3/E-AC-3 and MP2 decode with
craft-audio's own decoders; MP4 and Matroska tracks import at their
presentation length; .opus, .ac3, .eac3, .ec3, .mov, .mka and .mpa are
importable names; .aifc is a writable name; FLAC files come from craft-audio's
encoder.
2026-11-11 18:46:56 -05:00
FredAppsandFredApps 55c89b364d Release: SoundCraft v0.20.0 on main (#1511)
Co-authored-by: FredApps <FredApps@users.noreply.github.com>
2026-11-11 23:20:08 +01:00
FredApps eeb5d653a5 CI: nightly release dry run (#1508)
release.yml also runs nightly on main with craft-common-actions' dry-run: every release build
except macOS, with nothing signed, pushed or drafted. Breakage that only the release build hits
(riscv64, Flatpak, FreeBSD, packaging) then shows up the next morning instead of on release
day, as it did on 11 Nov 2026.
2026-11-11 23:04:40 +01:00
ljkx bf6f1166d1 Merge remote-tracking branch 'origin/main' into feat/frequency-shifter 2026-11-11 19:08:53 +01:00
ljkx d4468ff935 Frequency Shifter: doc comments on the section, the constructor and the processing methods
Section::tick, FreqShifter::new, design and update, and the prepare,
reset and process methods say what they do. No code change.
2026-11-10 09:21:58 +01:00
ljkx 3e6714d59f Docs: the alpha gate counts 28 built-in processors
docs/roadmap.md still said 27 in the alpha gate's mixing row; the
Frequency Shifter makes it 28, as the other parity docs say.
2026-11-10 01:15:20 +01:00
ljkx dd7f3188aa Docs: Frequency Shifter in the plugin list and the built-in counts
The README plugin list, the ROADMAP progress log and the plugin parity
doc's Modulation row name the Frequency Shifter, and the parity doc no
longer lists frequency shift as missing. docs/plugin-parity.md,
docs/gaps.md (G-08) and docs/target-app-parity.md count 28 processors +
3 instruments. Readiness percentages are unchanged.
2026-11-09 20:10:10 +00:00
ljkx 9e37794db7 Add a built-in Frequency Shifter plugin
The built-in plugins had no frequency shifter (G-08). Frequency Shifter
(`freq_shifter`, Modulation category, also offline as AudioSuite) moves
every frequency in the signal by the same number of hertz, with the
controls Avid documents for AIR Frequency Shifter: Frequency, Mode (Up,
Down, Up & Down, Stereo), Feedback and Mix.

It is single-sideband modulation. Two chains of ten second-order allpass
sections in f64 split the input into a 90-degree pair. The network is an
elliptic half-band design (Valenzuela and Constantinides) moved by a
quarter of the sample rate and designed for the host rate in prepare,
accurate from 20 Hz to 20 Hz below Nyquist. The output
i cos - s q sin keeps the upper sideband (s = 1), the lower one (s = -1)
or both at half level (s = 0). There is no latency.

Frequency runs from 0.01 Hz to 5 kHz. Feedback stops at 70 %: the shift
raises a signal's energy by at most sqrt(2), so the loop is stable for
any input below 1/sqrt(2). Stereo shifts left down and right up on a
two-channel insert. Mono and wider inserts, whose left and right the
plugin cannot know, play both sidebands. Frequency, Feedback and Mix
glide over 20 ms with a continuous carrier phase, and each channel's
sideband weight glides too, so a Mode change crossfades instead of
clicking. Non-finite input is treated as silence, and filter states
are flushed below 1e-30.

The tests measure tones by single-bin DFT over whole cycles. A shifted
tone lands within 0.001 dB, with the image at least 100 dB down at
22.05 to 96 kHz and 95 dB down at 192 kHz. They also cover the band
edges, Up & Down, Stereo, multichannel inserts, the feedback trail,
Mix, click-free parameter changes, block-size independence, the tail,
and the network's response at 8 to 384 kHz.
2026-11-09 20:10:10 +00:00
ljkx 976ea150b7 Output window: typed volume and pan join the strip's merge keys; update the header test (#1392)
A typed value in the Output window went through app.run, so during an
automation pass it became its own undo step. It now uses the strip's
merge keys (fader:<id>, pan:<id>:<i>), as a drag does.

header_keys_freeze_silence_and_show_automation clicked the Output fader
button and expected the Mix window; it now expects Kick's Output window.
2026-11-09 15:24:23 +01:00
ljkx 9506ad6039 Edit window: the Output fader button opens a floating Output window (#1392)
The fader button beside a track's Output selector selected the track and
switched to the Mix window. As in Pro Tools, it now opens a floating
Output window for that track, modelled on the Send window: the output
destination, Solo and Mute, the pan knobs, the volume fader and an
editable dB readout, running the strip's own commands and merge keys.
Clicking the button again, Escape, the close button or deleting the track
closes it.
2026-11-09 15:03:04 +01:00
Mex 51c3159ff1 docs: record Command-click automation review corrections
Record existing-point and clip-control priority, the insertion cursor and hint, and the added selection and drag regression coverage while preserving earlier progress notes and readiness estimates.
2026-11-08 13:49:15 +01:00
Mex a743f5b4ef fix(ui): preserve automation point hits on Command-click
Keep existing automation points and clip controls ahead of modifier insertion, expose an insertion cursor and hint, and preserve active-tool drags and edit selection. Add actual-app regression coverage across tools, point deletion and undo, cursor restoration, and a Selector drag starting near a point.
2026-11-08 13:45:59 +01:00
Mex 3158855080 Merge branch 'storytold:main' into fix/command-click-automation-point 2026-11-08 13:35:09 +01:00
Mex d842ee7421 Document preserved selection and fallback volume clicks
Update the existing #1394 progress entry to describe the corrected selection and visible fallback-volume behavior, without changing readiness estimates.
2026-11-05 21:55:26 +01:00
Mex b2f8978387 Cover fallback lanes and selection-preserving Command-click
Reproduce the fallback-volume and existing-selection failures with actual UI events, then cover undo and Command-drag behavior. Strengthen the clicked-value assertion with an off-curve midpoint. Both added regressions fail on the previous handler; all 232 UI tests pass with the correction.
2026-11-05 21:54:21 +01:00
Mex dafcc16c32 Preserve selection and target the drawn automation lane
Resolve the visible parameter for Command-click and defer Selector insertion until a real drag starts. Keep the stored lane view and existing drag, fade and clip-gain dispatch unchanged.
2026-11-05 21:53:33 +01:00
Mex 6d7fec682b Record the Command-click automation fix
Note the undoable modifier-click behavior for issue #1394 and retain the existing readiness estimates.
2026-11-05 21:15:11 +01:00
Mex 705697178c Cover Command-click automation points and undo
Drive actual modifier clicks with Smart, Grabber and Selector tools; check the clicked time/value, plain-click control, unchanged clips and one-step undo. The regression fails with the original handler and passes with the fix.
2026-11-05 21:14:13 +01:00
Mex bb988dc7ff Add automation points with Command-click
Route Command-click in an automation view through the existing Pencil insertion path while retaining the active tool and drag handling.
2026-11-05 21:13:22 +01:00
lucasegen 6fddda5618 Merge remote-tracking branch 'upstream/main' into fix/clip-cut-seam
# Conflicts:
#	crates/ui-egui/src/edit_window/tracks.rs
2026-11-05 13:14:25 +01:00
ayozetr 2a45435254 Merge main into linux-plugin-editors 2026-11-03 22:14:41 +00:00
lucasegen 045f70d823 Merge remote-tracking branch 'upstream/main' into fix/clip-cut-seam
# Conflicts:
#	crates/ui-egui/src/edit_window/tests.rs
2026-11-03 16:06:52 +01:00
lucasegen e4f2d888ab Merge remote-tracking branch 'upstream/main' into fix/playhead-record-colour
# Conflicts:
#	crates/ui-egui/src/edit_window/tests.rs
2026-11-03 16:06:51 +01:00
ayozetr cca413bd32 Merge main into linux-plugin-editors
# Conflicts:
#	ROADMAP.md
2026-11-01 17:39:04 +00:00
Ayoze Torres 60fbbcb4e5 Run-loop tests: take t0 before registering the timers
A timer's first tick is set at registration, so taking the reference
time afterwards made the "not due yet" check depend on the test thread
not stalling for a timer period (1 ms in the CLAP test). Same pattern
fixed in the VST3 test.
2026-10-30 01:57:25 +00:00
Ayoze Torres c3f51a6030 docs: plugin editors on Linux
plugin-parity: VST3 editors are hosted on every desktop platform, and
embedded CLAP editors on Windows and Linux/X11. gaps: G-05 shrinks to
the macOS work (AU views, embedded CLAP, the entitlement), 10-15 h.
roadmap and ROADMAP: the same estimate, the mix row's VST3 windows, and
a progress log entry.
2026-10-30 01:57:25 +00:00
Ayoze Torres d06bd91ff6 clap-host: host embedded plugin editors on Linux (X11)
CLAP plugins that only offer an embedded editor now get a host window on
Linux and the BSDs too (ffi/hostwin/x11.rs): create(x11, false),
get_size, set_parent with a top-level X11 window of ours (XWayland on
Wayland desktops), show. request_resize is applied by the editor's idle
as on Windows, and closing the window destroys the GUI, then the window.
Host windows now hand over their own clap_window_handle; the X11 id goes
in as a c_ulong, which is 32 bits on 32-bit Unix.

X11 editors rely on the host's event loop, so the host now offers
clap.timer-support and clap.posix-fd-support there (ffi/run_loop.rs).
Registrations are kept per instance and serviced by the editor's idle on
the main thread (ready descriptors, then due timers; a late frame fires
each timer once). They are dropped when the instance goes. Floating X11
editors benefit from them as well.

New Linux/BSD-only dependencies: x11rb and libc, as in vst3-host.
2026-10-30 01:57:25 +00:00
Ayoze Torres d3331784ee vst3-host: host plugin editors on Linux (X11)
VST3 editors now open on Linux and the BSDs. The view is embedded
(kPlatformTypeX11EmbedWindowID) into a top-level X11 window on our own
connection to the X server (ffi/window/x11.rs), which is XWayland on
Wayland desktops. It is sized from getSize, resized on
IPlugFrame::resizeView, and the close button only hides it; the editor
link's idle then detaches the view and destroys the window, as on macOS
and Windows.

X11 editors have no event loop of their own, so the frame is also the
view's Linux::IRunLoop (ffi/run_loop.rs): plugins register their display
connection's descriptor and their repaint timers, and the editor link's
idle (once per UI frame) calls back the ready descriptors and due
timers. Without it JUCE editors open but never repaint. Registrations
are dropped on teardown and when attaching fails, and removed handlers
are released after the run loop's lock, since a plugin destructor may
call back into it.

New Linux/BSD-only dependencies: x11rb (pure Rust, already in the
lockfile through winit) and libc for the zero-timeout poll.
2026-10-30 01:57:25 +00:00
lucasegenandClaude Opus 5.5 4f99e2a686 Edge the Universe's playback line so it reads over every track colour
The Universe paints clips in their raw track colours, and the neutral playback line is as light as some of them (1.1:1 over the first one in the dark theme): a dark edge on each side keeps it visible.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Refs #724.

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

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-23 15:08:14 +02:00
302 changed files with 20047 additions and 26146 deletions
+5
View File
@@ -1,6 +1,10 @@
name: Release
on:
# A nightly dry run on main: every release build except macOS, nothing signed or released
# (craft-common-actions release.yml `dry-run`). Finds release-only breakage before release day.
schedule:
- cron: "36 2 * * *"
push:
branches: [release]
workflow_dispatch:
@@ -30,6 +34,7 @@ jobs:
# 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:
dry-run: ${{ github.event_name == 'schedule' }}
app-name: soundcraft
app-title: SoundCraft
version: ${{ inputs.version }}
+16 -3
View File
@@ -86,7 +86,7 @@ People trust SoundCraft with their recordings; a crash loses takes. **This outra
## Architecture (layers enforced by `cargo xtask layers`)
| Layer | Crate | Role |
|---|---|---|
| L0 | `soundcraft-time`, `soundcraft-audio-io`, `soundcraft-midi` | Timebases/tempo/grid; audio file formats + peaks; Standard MIDI Files + MIDI ops |
| L0 | `soundcraft-time`, `soundcraft-midi` | Timebases/tempo/grid; Standard MIDI Files + MIDI ops |
| L1 | `soundcraft-dsp` | Plugins (EQ, dynamics, reverb, delay, modulation, harmonic, pitch, utility, instruments), offline processing, meters, FFT |
| L2 | `soundcraft-model` | The session document (tracks, playlists, clips, mixer, routing, automation, markers, groups) |
| L3 | `soundcraft-mix` | Mix engine shared by realtime playback and offline bounce |
@@ -95,6 +95,8 @@ People trust SoundCraft with their recordings; a crash loses takes. **This outra
| L6 | `soundcraft-ui-egui` | The swappable UI (egui) |
| apps | `soundcraft`, `soundcraft-cli`, `soundcraft-web` | Entry points |
Audio file formats, codecs and waveform peaks are not a SoundCraft crate: they come from `craft-audio` in `storytold/craft-libs`, which every Craft app shares. Fix or extend a format there, never in a local copy; SoundCraft keeps only the mapping onto its session model (`soundcraft_model::FileFormat`).
Nothing below L6 depends on egui/eframe/winit/rfd. The UI is thin: panels read engine state and act through `engine.execute(id, params)`.
## Everything is a command
@@ -107,9 +109,16 @@ Parity is measured, not guessed: `crates/engine/catalog/menus.txt` lists the inc
- Drive it: JSON lines over TCP, e.g. `{"id":1,"method":"engine.execute","params":{"command":"track.new","params":{"count":2,"format":"stereo"}}}`, then `{"id":2,"method":"ui.screenshot","params":{"path":"/tmp/shot.png"}}`. Methods: `crates/ui-egui/src/control.rs`, docs: `docs/control-protocol.md`.
- Offscreen UI render (no window, no focus stealing): `cargo run -p soundcraft-ui-egui --example ui_shot -- out.png [script.jsonl]`.
- Headless: `soundcraft-cli run --demo --cmd 'mix.volume={"track":"Kick","db":-6}' --bounce out.wav`; MCP: `soundcraft-cli mcp [--connect PORT]`.
- **For UI work, look at the result** (render, read the PNG) and compare with `plan/protools/screenshots/`.
- **Language / localization:** the interface is English by default; Brazilian Portuguese
(`pt-BR`) is selected under `Setup › Appearance › Language` (or the `ui.language` command with
`{"language":"pt-BR"}`). The pt-BR catalog is complete for every fixed UI string — tests cover
placeholder parity, unique keys and source coverage, and the `cargo test` suite passes.
- Commands: build `cargo build -p soundcraft --bin soundcraft`; test
`cargo test -p soundcraft --bin soundcraft`; run `DISPLAY=:0 ./target/debug/soundcraft`. The
`cargo xtask ci` gate runs formatting, clippy, tests, assets, layering and the WebAssembly build.
- Parallel agents: separate `CARGO_TARGET_DIR` per agent; edit only the crates you own; write
manifests atomically; delete your target dir when done (disk).
- Shell gotcha: `mv`/`cp` are aliased interactive here — use `/bin/mv -f` / `/bin/cp -f`.
- Parallel agents: separate `CARGO_TARGET_DIR` per agent; edit only the crates you own; write manifests atomically; delete your target dir when done (disk).
## Quality gates (before every commit)
`cargo xtask ci` = fmt check, `clippy --workspace --all-targets -D warnings`, tests, assets, layers, wasm check. Commit after each arc of work that builds; one topic per commit.
@@ -141,3 +150,7 @@ Push to the `release` branch to build signed installers for macOS (universal DMG
- Some workflow files here still run their own jobs. Don't add to them. Moving a job into craft-common-actions is its own task, never part of another PR, and the job stays here until a shared workflow runs it.
New workspace crates include `[lints] workspace = true` in `Cargo.toml`. Production crate roots declare the crate-level lint attributes before modules; crate-level `//!` documentation may precede them.
## Shared Craft libraries
Before adding or changing reusable file-format, codec, protocol or UI code, check [craft-libs](https://github.com/storytold/craft-libs) and existing contributor work; reuse its public APIs or extend the appropriate shared library instead of adding an app-local duplicate, keeping app-specific policy and model mapping local and preserving compatibility, security limits and regression coverage.
-1
View File
@@ -55,4 +55,3 @@ here. The subset uses default_fonts; the parent face uses monochrome_emoji_fonts
| `assets/app-icon/soundcraft.ico` | SoundCraft contributors | original, rendered from `icon.svg` | MIT OR Apache-2.0 |
| `assets/app-icon/hicolor/` | SoundCraft contributors | original, rendered from `icon.svg` (16–512 px PNGs and the scalable SVG) | MIT OR Apache-2.0 |
| `docs/images/` | SoundCraft contributors | screenshots of SoundCraft rendered from the synthesised demo session | MIT OR Apache-2.0 |
| `crates/audio-io/tests/fixtures/` | SoundCraft contributors | original test tones and silence generated with ffmpeg (sine) or by hand; commands in `crates/audio-io/tests/fixtures/README.md` | MIT OR Apache-2.0 |
Generated
+740 -80
View File
File diff suppressed because it is too large Load Diff
+30 -8
View File
@@ -4,16 +4,18 @@ members = ["crates/*", "apps/*", "xtask"]
default-members = ["crates/*", "apps/*", "xtask"]
[workspace.package]
version = "0.19.1"
version = "0.20.1"
edition = "2024"
license = "MIT OR Apache-2.0"
rust-version = "1.90"
rust-version = "1.95"
repository = "https://github.com/storytold/soundcraft"
[workspace.dependencies]
craft-score = { git = "https://github.com/storytold/craft-libs", rev = "3a1a627dc69c677d3a26c22b72e070242ee6b841" }
craft-xml = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "ea24422ae9859dc5da398d0462bc87f8e02e0d10" }
craft-mime = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "ea24422ae9859dc5da398d0462bc87f8e02e0d10" }
# internal
soundcraft-time = { path = "crates/time" }
soundcraft-audio-io = { path = "crates/audio-io" }
soundcraft-midi = { path = "crates/midi" }
soundcraft-video = { path = "crates/video" }
soundcraft-dsp = { path = "crates/dsp" }
@@ -43,22 +45,37 @@ same-file = "1"
proptest = "1"
# audio
symphonia = { version = "0.6", default-features = false, features = ["mp1", "mp2", "mp3", "flac", "vorbis", "ogg", "wav", "pcm", "aiff", "isomp4", "aac", "alac", "adpcm", "caf", "mkv"] }
rustfft = "6"
# Session bundles (one file holding a session and its audio, for the browser): stored entries only.
zip = { version = "9", default-features = false }
cpal = "0.18"
# MIDI input devices. Pinned: it talks to the system's MIDI service and runs on its threads.
# Only soundcraft-playback depends on it, and only on Linux, macOS and Windows; there it builds
# alsa (already used by cpal), coremidi + coremidi-sys, or the `windows` crate (already in the
# tree). Its Android and web dependencies (jni, android-build, java-locator, dlopen2,
# process_path, wasm-bindgen) are in Cargo.lock because the lock file covers every target, and
# are never compiled for SoundCraft.
midir = "=0.11.1"
# Which file paths stored in a session may be opened.
craft-docpath = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "6e03f65bf45181db77ba6229716e6624db9eb25b" }
craft-docpath = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "ea24422ae9859dc5da398d0462bc87f8e02e0d10" }
# Video containers and bitstreams, shared with the other craft apps.
craft-media = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "6e03f65bf45181db77ba6229716e6624db9eb25b" }
craft-media = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "45a8e16669e70dadb780dfbc759c882dd5b7ea1c" }
# H.264 and ProRes codecs shared with the other Craft apps.
craft-video = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "ddbe78231b5b23273b3d191f04b1a38f4c0445b3", default-features = false, features = ["h264-decode", "prores", "mjpeg"] }
# Audio file formats, codecs and waveform overviews, shared with the other craft apps. It shares
# types with craft-media, so both stay on one rev.
# Standard MIDI Files and the note/controller model, shared with the other craft apps.
craft-midi = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "45a8e16669e70dadb780dfbc759c882dd5b7ea1c", features = ["serde"] }
craft-audio = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "45a8e16669e70dadb780dfbc759c882dd5b7ea1c", features = ["decode", "lossless-encode", "peaks", "serde"] }
craft-time = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "45a8e16669e70dadb780dfbc759c882dd5b7ea1c" }
craft-scraft-format = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "45a8e16669e70dadb780dfbc759c882dd5b7ea1c" }
# craft-update: app-specific pretend versions apply to startup and manual checks.
craft-update = { version = "=0.1.0", git = "https://github.com/storytold/craft-libs", rev = "6e03f65bf45181db77ba6229716e6624db9eb25b", features = ["egui"] }
craft-update = { version = "=0.1.0", git = "https://github.com/storytold/craft-libs", rev = "ea24422ae9859dc5da398d0462bc87f8e02e0d10", features = ["egui"] }
# ui
craft-ui = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "6e03f65bf45181db77ba6229716e6624db9eb25b", features = ["serde"] }
craft-ui = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "ea24422ae9859dc5da398d0462bc87f8e02e0d10", features = ["serde"] }
egui = { version = "0.37" }
# Resizable table columns (About ▸ Contributors / Models); no image loaders.
egui_extras = { version = "0.37", default-features = false }
@@ -88,6 +105,11 @@ debug = false
opt-level = 1
debug = "line-tables-only"
# symphonia's FLAC decoder overflows an i32 add on 32-bit LPC FLAC; the wrapped value is correct
# (craft-audio README, "symphonia").
[profile.dev.package.symphonia-bundle-flac]
overflow-checks = false
[profile.release]
opt-level = 3
lto = "thin"
+33 -13
View File
@@ -123,13 +123,15 @@ where things are.
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, 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.
modulator, frequency shifter, 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.
and Audio-to-MIDI from a sung or played melody. MIDI keyboards play the instruments live and
are recorded into MIDI clips ([how](docs/midi-input.md)).
- **Recording:** punch in on a selection or on the fly, pre/post-roll, loop recording with a
playlist per take, input monitoring through the full channel strip, and autosave with recovery.
- **Time and tempo:** Bars|Beats, Min:Secs, Timecode (every rate, drop-frame included),
@@ -146,8 +148,7 @@ where things are.
## Getting started
Download a build from the [releases page](https://github.com/storytold/soundcraft/releases), or build
from source with Rust 1.95 or later for the desktop/web UI (the core workspace's declared
minimum remains Rust 1.90):
from source with Rust 1.95 or later:
```sh
git clone https://github.com/storytold/soundcraft
@@ -178,6 +179,16 @@ and **⌥⌘↑/↓** its timebase. **⇧⌃⌘↓/↑** adds/removes the active
**⇧⌘↓/↑** works too. Windows/Linux use **Ctrl** for **⌘**. The active row follows the
field you click. A new row starts with its count focused and selected; Enter creates and Escape cancels.
### MIDI keyboards
Plug a keyboard in, record-enable an instrument track (its **R** button) and play: the keyboard
is heard through the track's instrument, and a record pass turns what you play into a MIDI clip.
**Setup › Playback Engine…** lists the MIDI devices with a switch each (**Setup › MIDI › MIDI
Input Devices…** opens the same window), and a track's input selector chooses a device and a
channel. Tried on Linux so far; on macOS and Windows it builds but has not been tried with a
device, and there is no MIDI input on FreeBSD or the web. The guide is
[docs/midi-input.md](docs/midi-input.md).
### Logs
The desktop app writes its `log` records to standard error and to `logs/soundcraft.log` in the
@@ -244,14 +255,13 @@ about egui, so the interface could be swapped for another one.
| Crate | What it does |
|---|---|
| `soundcraft-time` | Timebases, tempo and meter maps, timecode, grid |
| `soundcraft-audio-io` | Audio file formats and waveform overviews |
| `soundcraft-time` | Compatibility re-export of `craft-time`'s SoundCraft sample-index API |
| `soundcraft-midi` | Standard MIDI Files and MIDI operations |
| `soundcraft-dsp` | Plugins, offline processing, meters, loudness, FFT |
| `soundcraft-model` | The session document |
| `soundcraft-model` | Compatibility re-export of `craft-scraft-format`'s schema, validation and JSON API |
| `soundcraft-mix` | The mix engine (realtime and offline share it) |
| `soundcraft-engine` | Commands, undo, editing, import/export, bounce |
| `soundcraft-playback` | Audio devices (CoreAudio, WASAPI, ALSA, WebAudio) and recording |
| `soundcraft-playback` | Audio devices (CoreAudio, WASAPI, ALSA, WebAudio), MIDI input devices and recording |
| `soundcraft-automation` | MCP server and control-channel client |
| `soundcraft-ui-egui` | The user interface |
@@ -265,8 +275,18 @@ SoundCraft is **pre-alpha**: it covers 94 % of the incumbent's menu items (measu
its features by presence, and is roughly 40 % of the way to replacing it for real work: record,
edit, mix and bounce work end to end, but choosing an audio interface and buffer size,
recording MIDI from a controller and session interchange (AAF/OMF) are still missing, and those
are the alpha checklist. Translations are missing too. The status, the stage and our effort
estimates are in [`ROADMAP.md`](ROADMAP.md); the full assessment is in
are the alpha checklist.
**Translations:** the Brazilian Portuguese (`pt-BR`) catalog is **100 % complete** for every fixed
UI string found in the current source — menus, dialogs and buttons, tooltips (including those
localized inside buttons and toggles), accessibility names, the Edit-window status bar, the
remove-session question, formatted messages, engine error templates and engine error text at display
time. All these entries are covered by a test, placeholders match, keys are unique, and the
`pt-BR` catalog is chosen automatically when `ui.language` is `pt-BR` (or "Portuguese (Brazil)")
under `Setup › Appearance`. Engine errors keep their run-time values and a decimal comma in
`pt-BR`. The remaining localization work is a native-speaker (human) review of wording plus CJK /
Devanagari / Arabic font fallbacks and RTL layout — not new UI translation. The status, the stage
and our effort estimates are in [`ROADMAP.md`](ROADMAP.md); the full assessment is in
[`docs/target-app-parity.md`](docs/target-app-parity.md), the ranked list of what's missing in
[`docs/gaps.md`](docs/gaps.md), and the menu-by-menu comparison in
[`docs/parity-checklist.md`](docs/parity-checklist.md). Bug reports and wish lists are
+28 -8
View File
@@ -2,7 +2,7 @@
**Stage: pre-alpha** · next: alpha, ~5% and ~110–180 h away (the alpha gate's three failing workflows)
> **Last reviewed:** 2026-10-24 · **Last updated:** 2026-10-25 · **Change:** trivial (Cancel Zoom Toggle, side panels in dock columns, Auto Pan stereo balance, Clip List Auto Rename, Strip Silence rectangles, Strip Silence Rename, Strip Silence Extract and Separate, Clip Looping dialog, AudioSuite Vari-Fi speed ramps, Auto Pan plugin, Subharmonic plugin, Ring Modulator plugin, Float32 file headroom) · **Target:** Avid Pro Tools Ultimate 2026.4.1
> **Last reviewed:** 2026-10-24 · **Last updated:** 2026-11-18 · **Change:** minor (MIDI input in the progress log; previously trivial: Frequency Shifter plugin, Command-click target priority and cursor, Command-click automation points, plugin editors on Linux/X11, Cancel Zoom Toggle, side panels in dock columns, Auto Pan stereo balance, Alt-click returns a plugin parameter to its default, Clip List Auto Rename, Strip Silence rectangles, Strip Silence Rename, Strip Silence Extract and Separate, Clip Looping dialog, AudioSuite Vari-Fi speed ramps, Auto Pan plugin, Subharmonic plugin, Ring Modulator plugin, Float32 file headroom) · **Target:** Avid Pro Tools Ultimate 2026.4.1
SoundCraft is a clean-room, pure-Rust digital audio workstation that aims at full parity with
Avid Pro Tools, and then beyond it: faster, open, scriptable and agent-controllable. This page is
@@ -36,8 +36,10 @@ Detail: [target-app-parity.md](docs/target-app-parity.md#readiness-by-audience).
**Why pre-alpha:** the core-workflow gate in [docs/roadmap.md](docs/roadmap.md#alpha-gate) fails.
Editing, mixing, bouncing, and saving and reopening work end to end. But a Pro Tools user can't
record from a chosen audio interface at a chosen buffer size (only the default devices, a fixed
512-sample block, and takes held in RAM). They can't record MIDI from a keyboard (there is no
MIDI device layer). And they can't bring a session in or hand one back (no AAF/OMF/`.ptx`).
512-sample block, and takes held in RAM). Recording MIDI from a keyboard is built but checked on
Linux only: macOS and Windows are type-checked and have not been tried with a device, and there
is no MIDI input on FreeBSD or the web. And they can't bring a session in or hand one back (no
AAF/OMF/`.ptx`).
Readiness (~40 %) sits right at the alpha bar, but a gate failure keeps the app pre-alpha. The
2026-10-07 estimate ("~65 % overall, 110–150 h to 100 %") counted feature presence. This one
measures readiness and adds hardware, localization and field bug reports.
@@ -50,7 +52,7 @@ measures readiness and adds hardware, localization and field bug reports.
| UI/UX fidelity | ~55 % | 60–100 | [ui-parity.md](docs/ui-parity.md) |
| File formats | ~40 % | 70–120 | [file-format-parity.md](docs/file-format-parity.md) |
| Hardware (interfaces, MIDI, surfaces, sync) | ~12 % | 110–180 | [hardware-parity.md](docs/hardware-parity.md) |
| Localization | ~12 % (1 of Pro Tools' 8 languages) | 70–110 | [localization-parity.md](docs/localization-parity.md) |
| Localization | ~12 % (1 of Pro Tools' 8 languages; English and pt-BR fixed UI strings, no CJK/RTL font fallback or native review) | 70–110 | [localization-parity.md](docs/localization-parity.md) |
| Performance | ~35 % (unmeasured) | 40–70 | [hardware-parity.md](docs/hardware-parity.md#performance) |
| Stability | ~45 % | 50–80 | [gaps.md](docs/gaps.md) |
| Platforms | ~65 % (5 platforms built, macOS exercised) | 20–40 | [gaps.md](docs/gaps.md) |
@@ -89,23 +91,23 @@ Weights and evidence per row: [target-app-parity.md](docs/target-app-parity.md#f
| Hindi | hi | none | 0 % |
| Arabic | ar | none | 0 % |
| French | fr | none (PR #143 open) | 0 % |
| Portuguese | pt | none | 0 % |
| Portuguese (Brazil) | pt-BR | full — 100 % of fixed UI strings; tests pass | 100 % (native-speaker review pending) |
| Indonesian | id | none | 0 % |
| Japanese | ja | none | 0 % |
| German | de | none | 0 % |
| Korean | ko | none | 0 % |
| Vietnamese | vi | none | 0 % |
Other languages shipped: 0. Detail: [localization-parity.md](docs/localization-parity.md).
Other languages shipped: 0 complete. Brazilian Portuguese covers the fixed UI strings (100 %), with native-speaker review, CJK/RTL font fallbacks and script shaping still pending. Detail: [localization-parity.md](docs/localization-parity.md).
## Upcoming
Ranked; the alpha gate, the alpha checklist and the beta plan are in [docs/roadmap.md](docs/roadmap.md).
1. **Alpha blocker:** audio interfaces: device and buffer-size choice, duplex stream, ASIO; record to disk (G-01, G-06). 35–55 h.
2. **Alpha blocker:** MIDI devices: record from keyboards, play instruments live, output (G-02). 20–35 h.
2. **Alpha blocker:** MIDI devices: keyboard input, live play and recording are in (checked on Linux only; macOS and Windows type-checked, none on FreeBSD or the web); output, clock and loop/merge recording remain (G-02). 12–22 h.
3. **Alpha blocker:** AAF/OMF import and export (G-03). 50–90 h.
4. macOS plugin entitlement (#153), then AU editors (G-05). 1 h for the entitlement (needed for the mix row), then 15–25 h.
4. macOS plugin entitlement (#153), then AU editors and embedded CLAP editors on macOS (G-05). 1 h for the entitlement (needed for the mix row), then 10–15 h.
5. Land the open bug-fix PRs and clear the correctness backlog (G-04). 15–25 h.
6. PR CI running `cargo xtask ci` on three OSes (G-14). 5–10 h.
7. Fidelity harness against Pro Tools (G-07). 20–30 h.
@@ -122,9 +124,17 @@ mutated and truncated movies. Field reports from the first public week are track
## Progress log
- **2026-11-18 (MIDI input):** MIDI input devices: keyboards play record-enabled and input-monitored MIDI and instrument tracks live and are recorded into MIDI clips, with a device and channel choice per track. The device switches are in the MIDI inputs section of Setup › Playback Engine (Setup › MIDI › MIDI Input Devices opens the same window) and are kept in the app preferences, per computer, not in the session. Controllers, pitch bend and aftertouch reach the instrument live; a pitch held on two devices or channels sounds until both release it; a full input queue releases the notes of its device and marks the take incomplete. On Linux, live play was checked with a hardware keyboard (Akai MPK mini 3) and recording with a virtual one; macOS and Windows are type-checked only, and there is no MIDI input on FreeBSD or the web. Guide: `docs/midi-input.md`. No readiness percentage change until that is measured.
- **2026-11-09:** A built-in Frequency Shifter (`freq_shifter`) moves every frequency in the signal by the same number of hertz, from 0.01 Hz to 5 kHz: up, down, both ways at once, or down on the left and up on the right of a stereo track. The unwanted sideband stays at least 100 dB down from 20 Hz to 20 Hz below Nyquist at 44.1 to 96 kHz. Feedback sends the output round again for a trail of further shifts, Mix blends in the dry signal, and every change glides without a click. No readiness percentage change.
- **2026-11-08:** Command-click near an existing automation breakpoint keeps that point as the click target with every active tool, and existing fade and clip-gain controls retain their normal priority. Empty automation space shows a crosshair and an insertion hint while Command is held. The active tool's drag gestures stay unchanged. No readiness percentage change.
- **2026-11-05:** Command-click in an automation lane adds a point at the clicked time and value with the current editing tool (#1394), using the existing Pencil mapping. Command-click preserves the existing selection and also works on the fallback volume lanes of Master, Aux and VCA tracks. Plain clicks and clip contents stay unchanged, and one Undo removes the new point. No readiness percentage change.
- **2026-10-27 (plugin editors on Linux):** VST3 editors and embedded CLAP editors open in a SoundCraft X11 host window on Linux and the BSDs (XWayland on Wayland desktops), resized on request and closed from its title bar; the host run loop (VST3 `Linux::IRunLoop`, CLAP `timer-support` and `posix-fd-support`) lets X11 editors read their display connection and repaint. Verified with Graillon 3 and Surge XT Effects (JUCE) in both formats (#527). No readiness percentage change.
- **2026-10-25:** Alt+Shift+E (`view.zoom_toggle_cancel`) and an Alt-click on the Zoom Toggle button cancel Zoom Toggle and keep the current view: the zoom, the scroll and the track heights stay as they are, and the button is no longer lit. The next Zoom Toggle zooms in from that view and goes back to it. No readiness percentage change.
- **2026-10-25:** Tracks and Groups and the Clip List sit in dock columns that run the Edit window's full height at its left and right edges, with the MIDI Editor under the session view between them; Mix docks Tracks and Groups at its left edge. A panel dragged onto a dock group joins it as a tab, stacks above or below it, or makes a new column; one dragged to an empty window edge makes a dock there. A list with no saved place reopens in its dock, and makes the dock again when it is gone. Preferences and window configurations saved with the earlier split-frame layouts load with the new default layouts and keep their other settings. No readiness percentage change.
- **2026-10-25:** Auto Pan moves the balance of a stereo track: the left channel's level falls as the position moves right and the right channel's as it moves left, at constant power, and each channel stays on its own side. At the center the track passes unchanged, so a wide or out-of-phase stereo image keeps its width, and the envelope follower follows the louder channel. No readiness percentage change.
- **2026-10-24:** An Alt-click (Option-click on a Mac) on a parameter in the plugin window, the Clip Effects window or an AudioSuite plugin's window returns it to its default value, on a slider or a slider's value box, and in the plugin window on a choice list too, as an Alt-click returns a fader to 0 dB. In the plugin window and the Clip Effects window the reset is one undo step. While Alt is held, a drag on a slider leaves the value where it is. No readiness percentage change.
- **2026-10-24:** The parameter sliders of the plugin window, the Clip Effects window and AudioSuite plugins move a tenth as far for the same drag while Ctrl (Command on a Mac) is held, as the Strip Silence sliders do. Logarithmic sliders move a tenth as far along the slider, and whole-number sliders stay on whole numbers. No readiness percentage change.
- **2026-10-24:** The Strip Silence window's four sliders move a tenth as far for the same drag while Ctrl (Command on a Mac) is held, on the slider and on its value box. Pressing or releasing the key during a drag leaves the value where it is. The Threshold and Clip Start Pad boxes show one decimal place. No readiness percentage change.
- **2026-10-24:** Clip List › Auto Rename (`clip.auto_rename`) sets the names that editing gives the clips it separates from the selected clips: a name, a starting number, a number of places (zeros before the number) and a suffix. The piece after each cut takes the next name, the piece before it keeps its name, and all the pieces count up one number sequence. Pieces that Strip Silence names with its own Rename fields, or removes, take no Auto Rename name. Copied clips keep their scheme when they are pasted, saved sessions hold the schemes, and Import Session Data brings them along. No readiness percentage change.
- **2026-10-24:** While the Strip Silence window is open, the Edit window draws a rectangle around each range that Strip would keep on the selected tracks, padded by Clip Start Pad and Clip End Pad, and the rectangles follow the sliders as they move. They are removed when the window closes. `edit.strip_silence_preview` returns the same ranges without changing the session. No readiness percentage change.
- **2026-10-24:** The Strip Silence window has a Rename button. It opens Rename Selected Clips, where a name, a starting number, a count of zeros and a suffix set the names of the clips that Strip, Extract and Separate leave in the selection (for example SFX023.Reel1, SFX024.Reel1). The app keeps the scheme for later uses of Strip Silence until the app quits, or until Clear and then OK empty it. No readiness percentage change.
@@ -177,9 +187,17 @@ mutated and truncated movies. Field reports from the first public week are track
| Date | Change | Summary |
|---|---|---|
| 2026-11-09 | trivial | Progress log: Frequency Shifter plugin; readiness estimates unchanged |
| 2026-11-08 | trivial | Command-click breakpoint/control priority and insertion affordance; active-tool drags retained; readiness estimates unchanged |
| 2026-11-05 | trivial | Progress log: Command-click adds an undoable automation point (#1394); readiness estimates unchanged |
| 2026-11-05 | trivial | Languages table: Brazilian Portuguese (pt-BR) near complete for fixed UI strings via PR #1346; needs native review. Estimates unchanged |
| 2026-10-27 | trivial | VST3 and embedded CLAP editors on Linux/X11 (#527); G-05 estimate down to 10–15 h; readiness estimates unchanged |
| 2026-10-25 | trivial | Progress log: Cancel Zoom Toggle; readiness estimates unchanged |
| 2026-10-25 | trivial | Progress log: side panels in dock columns; readiness estimates unchanged |
| 2026-10-25 | trivial | Progress log: Auto Pan stereo balance; readiness estimates unchanged |
| 2026-10-24 | trivial | Progress log: Alt-click returns a plugin parameter to its default; readiness estimates unchanged |
| 2026-10-24 | trivial | Progress log: fine adjustment on plugin parameter sliders; readiness estimates unchanged |
| 2026-10-24 | trivial | Progress log: Strip Silence fine adjustment; readiness estimates unchanged |
| 2026-10-24 | trivial | Progress log: Clip List Auto Rename; readiness estimates unchanged |
| 2026-10-24 | trivial | Progress log: Strip Silence rectangles; readiness estimates unchanged |
| 2026-10-24 | trivial | Progress log: Strip Silence Rename; readiness estimates unchanged |
@@ -197,6 +215,8 @@ mutated and truncated movies. Field reports from the first public week are track
| 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) |
| 2026-10-13 | trivial | Progress log: Delete key clears the selection (#404) |
| 2026-10-13 | The Delete and Backspace keys clear the edit selection in the Edit window, as Pro Tools' Delete does (Edit › Clear is Cmd+B); the MIDI editor keeps them for its selected notes (#404) |
| 2026-10-13 | trivial | Progress log: mix.instrument params, unknown fields refused (#265) |
| 2026-10-13 | trivial | Progress log: bus bounce compensates aux returns (#246) |
| 2026-10-13 | trivial | Progress log: master insert latency compensated (#351) |
+1 -1
View File
@@ -20,7 +20,7 @@ workspace = true
[dependencies]
soundcraft-engine.workspace = true
soundcraft-automation.workspace = true
soundcraft-audio-io.workspace = true
craft-audio.workspace = true
soundcraft-dsp.workspace = true
serde.workspace = true
serde_json.workspace = true
+9 -10
View File
@@ -11,31 +11,30 @@ use std::sync::OnceLock;
use clap::builder::NonEmptyStringValueParser;
use clap::error::ErrorKind;
use clap::{ArgAction, Args, CommandFactory, FromArgMatches, Parser, Subcommand, ValueEnum};
use craft_audio::{DECODE_EXTENSIONS, ENCODE_EXTENSIONS};
use serde_json::{Value, json};
use soundcraft_audio_io::FileFormat;
use soundcraft_automation::ControlTarget;
use soundcraft_automation::args::{AutomationArgs, ControlClientArgs, blank_env_is_unset, control_target, long_version, name_env_vars};
/// Help text that names the extensions `list` gives, built once from
/// [`FileFormat::readable_extensions`] or [`FileFormat::writable_extensions`] so it can't drift
/// from what the code accepts.
fn with_formats(cell: &'static OnceLock<String>, before: &str, list: fn() -> Vec<&'static str>, after: &str) -> &'static str {
cell.get_or_init(|| format!("{before}({}){after}", list().join(", ")))
/// Help text that names the extensions in `list`, built once from craft-audio's
/// [`DECODE_EXTENSIONS`] or [`ENCODE_EXTENSIONS`] so it can't drift from what the code accepts.
fn with_formats(cell: &'static OnceLock<String>, before: &str, list: &[&str], after: &str) -> &'static str {
cell.get_or_init(|| format!("{before}({}){after}", list.join(", ")))
}
fn info_file_help() -> &'static str {
static C: OnceLock<String> = OnceLock::new();
with_formats(&C, "An audio file ", FileFormat::readable_extensions, " or a .scraft session")
with_formats(&C, "An audio file ", DECODE_EXTENSIONS, " or a .scraft session")
}
fn read_help() -> &'static str {
static C: OnceLock<String> = OnceLock::new();
with_formats(&C, "The audio file to read ", FileFormat::readable_extensions, "")
with_formats(&C, "The audio file to read ", DECODE_EXTENSIONS, "")
}
fn write_help() -> &'static str {
static C: OnceLock<String> = OnceLock::new();
with_formats(&C, "The audio file to write; its extension picks the format ", FileFormat::writable_extensions, "")
with_formats(&C, "The audio file to write; its extension picks the format ", ENCODE_EXTENSIONS, "")
}
/// soundcraft-cli: SoundCraft without a window
@@ -361,7 +360,7 @@ pub(crate) enum BitDepthArg {
Float32,
}
impl From<BitDepthArg> for soundcraft_audio_io::BitDepth {
impl From<BitDepthArg> for craft_audio::BitDepth {
fn from(depth: BitDepthArg) -> Self {
match depth {
BitDepthArg::Int16 => Self::Int16,
+26 -10
View File
@@ -14,7 +14,16 @@ pub(crate) fn convert(args: &ConvertArgs, out: &mut dyn Write) -> Result<(), Cli
};
let bytes = std::fs::read(input).map_err(|source| CliError::File { path: input.clone(), source })?;
let ext = input.extension().and_then(std::ffi::OsStr::to_str);
let (info, mut buf) = soundcraft_audio_io::decode(&bytes, ext).map_err(|source| CliError::Audio { path: input.clone(), source })?;
let (info, mut buf) = craft_audio::decode(&bytes, ext).map_err(|source| CliError::Audio { path: input.clone(), source })?;
// The decoder replaces packets it cannot decode with silence of their length and counts them.
// Such a file is not a copy of what was recorded: never let it replace its source, and say so
// when it is written elsewhere.
if info.damaged > 0 {
if args.in_place {
return Err(ConvertError::Damaged { input: input.clone(), packets: info.damaged }.into());
}
log::warn!("{}: {} damaged packet(s) decoded as silence", input.display(), info.damaged);
}
let source_rate = buf.sample_rate;
if let Some(rate) = rate {
buf.channels = soundcraft_dsp::offline::resample(&buf.channels, buf.sample_rate, rate);
@@ -28,17 +37,22 @@ pub(crate) fn convert(args: &ConvertArgs, out: &mut dyn Write) -> Result<(), Cli
let dither = dither_flag.unwrap_or_else(|| auto_dither(info.sample_format, bit_depth, processed));
// Lossy only for the message: an extension that is not valid Unicode names no format.
let out_ext = output.extension().map_or_else(|| "wav".into(), |e| e.to_string_lossy().to_ascii_lowercase());
let Some(format) = soundcraft_audio_io::FileFormat::from_write_extension(&out_ext) else {
let Some(format) = craft_audio::encode_format_for(&out_ext) else {
return Err(ConvertError::OutputFormat { output: output.clone(), ext: out_ext }.into());
};
// A BWF source keeps its `bext` (written for WAV output only). The time reference counts
// samples, so it moves to the output rate.
let bwf = info.bwf.map(|mut b| {
let mut opts = craft_audio::EncodeOptions::new(format).with_bit_depth(bit_depth).with_dither(dither);
// The source's speakers, not the usual ones for its channel count (5.1 with side surrounds
// stays that). Channels with no speaker assignment get the format's default.
if info.layout.is_ordered() {
opts = opts.with_layout(info.layout);
}
opts.bwf = info.bwf.map(|mut b| {
b.time_reference = at_rate(b.time_reference, source_rate, buf.sample_rate);
b
});
let opts = soundcraft_audio_io::EncodeOptions { format, bit_depth, dither, bwf };
let bytes = soundcraft_audio_io::encode(&buf, &opts).map_err(|source| CliError::Audio { path: output.clone(), source })?;
let bytes = craft_audio::encode(&buf, &opts).map_err(|source| CliError::Audio { path: output.clone(), source })?;
write_replacing(&args.write_path(), &bytes).map_err(|source| CliError::File { path: output.clone(), source })?;
outln!(out, "wrote {} ({} ch, {} Hz, {:.2} s)", output.display(), buf.num_channels(), buf.sample_rate, buf.duration_secs());
Ok(())
@@ -75,13 +89,15 @@ fn write_replacing(target: &std::path::Path, bytes: &[u8]) -> std::io::Result<()
/// 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};
fn auto_dither(source: craft_audio::SampleFormat, target: craft_audio::BitDepth, processed: bool) -> bool {
use craft_audio::{BitDepth, SampleFormat};
let target_bits = match target {
BitDepth::Float32 => return false,
BitDepth::Int16 => 16,
BitDepth::Int24 => 24,
BitDepth::Int32 => 32,
// A depth added to craft-audio later: dither, as for any step that may lose resolution.
_ => return true,
};
if processed {
return true;
@@ -91,8 +107,8 @@ fn auto_dither(source: soundcraft_audio_io::SampleFormat, target: soundcraft_aud
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,
// resolution, so treat it like float (as is any format added to craft-audio later).
_ => true,
}
}
@@ -102,7 +118,7 @@ mod tests {
#[test]
fn convert_dithers_only_when_resolution_is_lost() {
use soundcraft_audio_io::{BitDepth, SampleFormat};
use craft_audio::{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));
+4 -2
View File
@@ -1,6 +1,6 @@
use std::path::{Path, PathBuf};
use soundcraft_audio_io::AudioError;
use craft_audio::AudioError;
use soundcraft_automation::RemoteError;
use soundcraft_engine::EngineError;
use soundcraft_engine::file_access::RootError;
@@ -46,6 +46,8 @@ pub(crate) fn unicode(path: &Path) -> Result<&str, CliError> {
/// A `convert` option or output the tool refuses.
#[derive(Debug, PartialEq, thiserror::Error)]
pub(crate) enum ConvertError {
#[error("{}: cannot write `.{ext}` files (supported: {})", output.display(), soundcraft_audio_io::FileFormat::writable_extensions().join(", "))]
#[error("{}: cannot write `.{ext}` files (supported: {})", output.display(), craft_audio::ENCODE_EXTENSIONS.join(", "))]
OutputFormat { output: PathBuf, ext: String },
#[error("{}: {packets} damaged packet(s) would be written as silence; not replacing the file (convert to another file to keep what decodes)", input.display())]
Damaged { input: PathBuf, packets: u64 },
}
+2 -2
View File
@@ -17,10 +17,10 @@ pub(crate) fn info(args: &InfoArgs, out: &mut dyn Write, err: &mut dyn Write) ->
}
let bytes = std::fs::read(path).map_err(|source| CliError::File { path: path.clone(), source })?;
let ext = path.extension().and_then(std::ffi::OsStr::to_str);
let i = soundcraft_audio_io::probe(&bytes, ext).map_err(|source| CliError::Audio { path: path.clone(), source })?;
let i = craft_audio::probe(&bytes, ext).map_err(|source| CliError::Audio { path: path.clone(), source })?;
print_json(
out,
&json!({"format": i.format, "sample_format": i.sample_format, "sample_rate": i.sample_rate, "channels": i.channels, "frames": i.frames, "seconds": i.duration_secs(), "bwf": i.bwf}),
&json!({"format": soundcraft_engine::model::FileFormat::of(&i), "sample_format": i.sample_format, "sample_rate": i.sample_rate, "channels": i.channels, "frames": i.frames, "seconds": i.duration_secs(), "bwf": i.bwf}),
args.compact,
);
Ok(())
+10
View File
@@ -0,0 +1,10 @@
//! Test helpers shared by the `soundcraft-cli` integration test binaries.
use std::ffi::OsString;
/// Nextest archives relocate binaries; plain Cargo keeps the compile-time path valid.
pub fn soundcraft_cli_binary() -> OsString {
std::env::var_os("NEXTEST_BIN_EXE_soundcraft_cli")
.or_else(|| std::env::var_os("NEXTEST_BIN_EXE_soundcraft-cli"))
.unwrap_or_else(|| env!("CARGO_BIN_EXE_soundcraft-cli").into())
}
+70 -19
View File
@@ -5,7 +5,9 @@
use std::path::{Path, PathBuf};
use std::process::Command;
use soundcraft_audio_io::{AudioBuffer, BitDepth, BwfInfo, EncodeOptions, FileFormat};
use craft_audio::{AudioBuffer, BitDepth, BwfInfo, EncodeOptions};
mod common;
fn tmpdir(tag: &str) -> PathBuf {
let dir = std::env::temp_dir().join(format!("sc-cli-convert-{tag}-{}", std::process::id()));
@@ -20,12 +22,12 @@ fn write_wav16(path: &Path, samples: &[i16]) {
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();
let opts = EncodeOptions::new(craft_audio::EncodeFormat::Wav).with_bit_depth(BitDepth::Int16);
std::fs::write(path, craft_audio::encode(&buf, &opts).unwrap()).unwrap();
}
fn convert(input: &Path, output: &Path, extra: &[&str]) {
let o = Command::new(env!("CARGO_BIN_EXE_soundcraft-cli"))
let o = Command::new(common::soundcraft_cli_binary())
.env("SOUNDCRAFT_NO_PREFS", "1")
.arg("convert")
.arg(input)
@@ -38,7 +40,7 @@ fn convert(input: &Path, output: &Path, extra: &[&str]) {
/// 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();
let (_, buf) = craft_audio::decode(&std::fs::read(path).unwrap(), None).unwrap();
buf.channels[0].iter().map(|s| f64::from(*s) * 32768.0).collect()
}
@@ -81,8 +83,8 @@ fn convert_dithers_when_reducing_depth_unless_told_not_to() {
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 opts = EncodeOptions::new(craft_audio::EncodeFormat::Wav).with_bit_depth(BitDepth::Int24);
std::fs::write(&src, craft_audio::encode(&buf, &opts).unwrap()).unwrap();
let dithered = dir.join("d16.wav");
convert(&src, &dithered, &["--bit-depth", "16"]);
@@ -99,18 +101,15 @@ fn convert_keeps_the_bwf_time_stamp() {
let dir = tmpdir("bext");
let src = dir.join("take.wav");
// 01:00:00:00 at 48 kHz.
let bwf = BwfInfo {
description: "take 3".into(),
originator: "recorder".into(),
origination_date: "2026-11-03".into(),
origination_time: "10:00:00".into(),
time_reference: 172_800_000,
..Default::default()
};
let bwf = BwfInfo::default()
.with_description("take 3")
.with_originator("recorder")
.with_origination("2026-11-03", "10:00:00")
.with_time_reference(172_800_000);
let buf = AudioBuffer::new(48_000, 1, 4800);
let opts = EncodeOptions { format: FileFormat::Wav, bit_depth: BitDepth::Int24, dither: false, bwf: Some(bwf.clone()) };
std::fs::write(&src, soundcraft_audio_io::encode(&buf, &opts).unwrap()).unwrap();
let bext = |p: &Path| soundcraft_audio_io::probe(&std::fs::read(p).unwrap(), None).unwrap().bwf;
let opts = EncodeOptions::default().with_bwf(bwf.clone());
std::fs::write(&src, craft_audio::encode(&buf, &opts).unwrap()).unwrap();
let bext = |p: &Path| craft_audio::probe(&std::fs::read(p).unwrap(), None).unwrap().bwf;
let out = dir.join("16.wav");
convert(&src, &out, &["--bit-depth", "16"]);
@@ -118,6 +117,58 @@ fn convert_keeps_the_bwf_time_stamp() {
// Resampled, the stamp still points at 01:00:00:00: 158,760,000 samples at 44.1 kHz.
let out = dir.join("44k.wav");
convert(&src, &out, &["--rate", "44100"]);
assert_eq!(bext(&out), Some(BwfInfo { time_reference: 158_760_000, ..bwf }));
assert_eq!(bext(&out), Some(bwf.with_time_reference(158_760_000)));
let _ = std::fs::remove_dir_all(&dir);
}
/// Twelve MPEG-1 Layer I frames (mono, 32 kHz, 48 bytes each) of silence. The seventh has the
/// forbidden bit allocation 15 in every subband: the decoder writes silence for it and counts it.
fn mp1_with_a_damaged_frame() -> Vec<u8> {
let header = [0xFF, 0xFF, 0x18, 0xC0];
let good = [&header[..], &[0; 44]].concat();
let bad = [&header[..], &[0xFF; 16], &[0; 28]].concat();
(0..12).flat_map(|i| if i == 6 { bad.clone() } else { good.clone() }).collect()
}
#[test]
fn convert_warns_about_damaged_packets_and_never_replaces_the_source_with_them() {
let dir = tmpdir("damaged");
let src = dir.join("take.mp1");
let bytes = mp1_with_a_damaged_frame();
std::fs::write(&src, &bytes).unwrap();
let run = |args: &[&std::ffi::OsStr]| {
Command::new(common::soundcraft_cli_binary()).env("SOUNDCRAFT_NO_PREFS", "1").arg("convert").args(args).output().unwrap()
};
// To another file: written, with a warning that says how much was lost.
let out = dir.join("take.wav");
let o = run(&[src.as_os_str(), out.as_os_str()]);
assert!(o.status.success(), "{}", String::from_utf8_lossy(&o.stderr));
let warning = format!("warning: {}: 1 damaged packet(s) decoded as silence\n", src.display());
assert_eq!(String::from_utf8_lossy(&o.stderr), warning);
assert_eq!(steps16(&out).len(), 12 * 384);
// Over the source: refused, and the source is untouched.
let o = run(&[src.as_os_str(), src.as_os_str(), "--in-place".as_ref()]);
assert_eq!(o.status.code(), Some(1));
let stderr = String::from_utf8_lossy(&o.stderr);
assert!(stderr.starts_with(&format!("error: {}: 1 damaged packet(s) would be written as silence", src.display())), "{stderr}");
assert_eq!(std::fs::read(&src).unwrap(), bytes);
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
fn convert_keeps_the_speaker_layout() {
use craft_audio::ChannelLayout;
let dir = tmpdir("layout");
let (src, out) = (dir.join("side.wav"), dir.join("out.wav"));
// 5.1 with side surrounds: not the layout a reader assumes for six channels.
let opts = EncodeOptions::default().with_layout(ChannelLayout::SURROUND_5_1_SIDE);
std::fs::write(&src, craft_audio::encode(&AudioBuffer::new(48_000, 6, 480), &opts).unwrap()).unwrap();
let layout = |p: &Path| craft_audio::probe(&std::fs::read(p).unwrap(), None).unwrap().layout;
assert_eq!(layout(&src), ChannelLayout::SURROUND_5_1_SIDE);
convert(&src, &out, &[]);
assert_eq!(layout(&out), ChannelLayout::SURROUND_5_1_SIDE);
let flac = dir.join("out.flac");
convert(&src, &flac, &[]);
assert_eq!(layout(&flac), ChannelLayout::SURROUND_5_1_SIDE);
let _ = std::fs::remove_dir_all(&dir);
}
+16 -9
View File
@@ -4,7 +4,7 @@
use std::path::{Path, PathBuf};
use std::process::{Command, Stdio};
use soundcraft_audio_io::{AudioBuffer, BitDepth, EncodeOptions, FileFormat};
use craft_audio::{AudioBuffer, BitDepth, EncodeOptions};
/// What one run of the binary produced.
struct Invoke {
@@ -19,7 +19,7 @@ fn invoke(dir: &Path, args: &[&str]) -> Invoke {
}
fn invoke_env(dir: &Path, args: &[&str], control_port: Option<&str>) -> Invoke {
let mut cmd = Command::new(env!("CARGO_BIN_EXE_soundcraft-cli"));
let mut cmd = Command::new(crate::common::soundcraft_cli_binary());
cmd.env("SOUNDCRAFT_NO_PREFS", "1").env_remove("SOUNDCRAFT_CONTROL_PORT");
if let Some(port) = control_port {
cmd.env("SOUNDCRAFT_CONTROL_PORT", port);
@@ -37,20 +37,20 @@ fn sandbox(tag: &str) -> PathBuf {
for (i, d) in buf.channels[0].iter_mut().enumerate() {
*d = i as f32 / 400.0;
}
let opts = EncodeOptions { format: FileFormat::Wav, bit_depth: BitDepth::Int16, dither: false, bwf: None };
std::fs::write(dir.join("in.wav"), soundcraft_audio_io::encode(&buf, &opts).unwrap()).unwrap();
let opts = EncodeOptions::new(craft_audio::EncodeFormat::Wav).with_bit_depth(BitDepth::Int16);
std::fs::write(dir.join("in.wav"), craft_audio::encode(&buf, &opts).unwrap()).unwrap();
std::fs::write(dir.join("s.txt"), "track.new {}\n").unwrap();
dir
}
/// The sample rate of the WAV `name` in `dir`.
fn rate_of(dir: &Path, name: &str) -> u32 {
soundcraft_audio_io::probe(&std::fs::read(dir.join(name)).unwrap(), None).unwrap().sample_rate
craft_audio::probe(&std::fs::read(dir.join(name)).unwrap(), None).unwrap().sample_rate
}
/// Bit depth of the WAV `name` in `dir`, as the probe reports the sample format.
fn format_of(dir: &Path, name: &str) -> String {
format!("{:?}", soundcraft_audio_io::probe(&std::fs::read(dir.join(name)).unwrap(), None).unwrap().sample_format)
format!("{:?}", craft_audio::probe(&std::fs::read(dir.join(name)).unwrap(), None).unwrap().sample_format)
}
/// Exit 1, nothing on stdout, and exactly `error: {message}` on stderr.
@@ -80,12 +80,16 @@ fn not_found() -> std::io::Error {
std::io::Error::from_raw_os_error(2)
}
/// Exit 1 with the refused-connection message for `port`; the OS's text for the refusal differs by platform.
/// Exit 1 with the unreachable message for `port`. The reason is either the OS's refusal, whose text
/// differs by platform, or the client's connect timeout, which on Windows can expire before a
/// closed loopback port is refused.
#[track_caller]
fn assert_refused(got: &Invoke, port: u16) {
assert_eq!((got.code, got.stdout.as_str()), (1, ""));
let prefix = format!("error: cannot reach SoundCraft at 127.0.0.1:{port}: ");
assert!(got.stderr.starts_with(&prefix) && got.stderr.contains(" (os error ") && got.stderr.ends_with(")\n"), "{}", got.stderr);
let reason = got.stderr.strip_prefix(&prefix).unwrap_or_else(|| panic!("{}", got.stderr));
let refused = reason.contains(" (os error ") && reason.ends_with(")\n");
assert!(refused || reason == "connection timed out\n", "{}", got.stderr);
}
// ---- logging ----
@@ -186,7 +190,10 @@ fn repeated_flags_are_usage_errors_in_either_spelling() {
fn convert_input_and_output_errors() {
let dir = sandbox("convert-io");
assert_fails(&invoke(&dir, &["convert", "nope.wav", "o.wav"]), &format!("nope.wav: {}", not_found()));
assert_fails(&invoke(&dir, &["convert", "in.wav", "o.xyz"]), "o.xyz: cannot write `.xyz` files (supported: wav, wave, bwf, aif, aiff, flac)");
assert_fails(
&invoke(&dir, &["convert", "in.wav", "o.xyz"]),
"o.xyz: cannot write `.xyz` files (supported: wav, wave, bwf, aif, aiff, aifc, flac)",
);
}
#[test]
+1 -1
View File
@@ -8,7 +8,7 @@ use std::process::{Command, Output, Stdio};
use serde_json::Value;
fn bin() -> Command {
let mut cmd = Command::new(env!("CARGO_BIN_EXE_soundcraft-cli"));
let mut cmd = Command::new(crate::common::soundcraft_cli_binary());
cmd.env("SOUNDCRAFT_NO_PREFS", "1");
cmd
}
@@ -3,7 +3,7 @@
use std::process::{Command, Stdio};
fn bin() -> Command {
let mut cmd = Command::new(env!("CARGO_BIN_EXE_soundcraft-cli"));
let mut cmd = Command::new(crate::common::soundcraft_cli_binary());
cmd.env("SOUNDCRAFT_NO_PREFS", "1");
cmd
}
+3
View File
@@ -1,5 +1,8 @@
//! Integration tests, built as one test binary: each file is a module.
#[path = "../common.rs"]
mod common;
#[allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]
mod behaviour;
#[allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]
+8 -5
View File
@@ -53,12 +53,16 @@ fn not_found() -> std::io::Error {
std::io::Error::from_raw_os_error(2)
}
/// Exit 1 with the refused-connection message for `port`; the OS's text for the refusal differs by platform.
/// Exit 1 with the unreachable message for `port`. The reason is either the OS's refusal, whose text
/// differs by platform, or the client's connect timeout, which on Windows can expire before a
/// closed loopback port is refused.
#[track_caller]
fn assert_refused(got: &Invoke, port: u16) {
assert_eq!((got.code, got.stdout.as_str()), (1, ""));
let prefix = format!("error: cannot reach SoundCraft at 127.0.0.1:{port}: ");
assert!(got.stderr.starts_with(&prefix) && got.stderr.contains(" (os error ") && got.stderr.ends_with(")\n"), "{}", got.stderr);
let reason = got.stderr.strip_prefix(&prefix).unwrap_or_else(|| panic!("{}", got.stderr));
let refused = reason.contains(" (os error ") && reason.ends_with(")\n");
assert!(refused || reason == "connection timed out\n", "{}", got.stderr);
}
#[test]
@@ -100,9 +104,8 @@ fn help_subcommand_shows_that_subcommands_help() {
#[test]
fn help_names_every_format_the_command_reads_and_writes() {
use soundcraft_audio_io::FileFormat;
let read = format!("({})", FileFormat::readable_extensions().join(", "));
let write = format!("its extension picks the format ({})", FileFormat::writable_extensions().join(", "));
let read = format!("({})", craft_audio::DECODE_EXTENSIONS.join(", "));
let write = format!("its extension picks the format ({})", craft_audio::ENCODE_EXTENSIONS.join(", "));
let convert = invoke(&["convert", "--help"]);
let convert = assert_help(&convert);
assert!(convert.contains(&read) && convert.contains(&write), "{convert}");
@@ -23,7 +23,7 @@ impl Drop for Scratch {
}
fn invoke(dir: &Path, args: &[&str]) -> Output {
Command::new(env!("CARGO_BIN_EXE_soundcraft-cli")).current_dir(dir).env("SOUNDCRAFT_NO_PREFS", "1").args(args).output().unwrap()
Command::new(crate::common::soundcraft_cli_binary()).current_dir(dir).env("SOUNDCRAFT_NO_PREFS", "1").args(args).output().unwrap()
}
#[test]
+3 -1
View File
@@ -5,8 +5,10 @@
use std::path::{Path, PathBuf};
use std::process::{Command, Output};
mod common;
fn bin() -> Command {
let mut cmd = Command::new(env!("CARGO_BIN_EXE_soundcraft-cli"));
let mut cmd = Command::new(common::soundcraft_cli_binary());
cmd.env("SOUNDCRAFT_NO_PREFS", "1");
cmd
}
+1 -1
View File
@@ -15,7 +15,7 @@ path = "src/main.rs"
[target.'cfg(target_arch = "wasm32")'.dependencies]
soundcraft-engine = { workspace = true }
soundcraft-audio-io.workspace = true
craft-audio.workspace = true
soundcraft-ui-egui.workspace = true
soundcraft-playback = { workspace = true }
soundcraft-model = { workspace = true }
+33 -14
View File
@@ -1,8 +1,9 @@
//! The browser shell: the eframe web runner around [`SoundApp`].
use soundcraft_audio_io::FileFormat;
use soundcraft_engine::{Engine, io};
use soundcraft_ui_egui::dialogs::Dialog;
use soundcraft_ui_egui::localization::{self, Language};
use soundcraft_ui_egui::localized_format;
use soundcraft_ui_egui::sampler_window::SampleRead;
use soundcraft_ui_egui::{Services, SoundApp};
use std::cell::RefCell;
@@ -13,10 +14,19 @@ use wasm_bindgen::JsCast as _;
const CANVAS_ID: &str = "soundcraft_canvas";
const LOADING_ID: &str = "soundcraft_loading";
/// The language the browser asks for, for text shown before or outside a running app.
fn browser_language() -> Language {
web_sys::window().and_then(|w| w.navigator().language()).and_then(|tag| Language::parse(&tag)).unwrap_or_default()
}
pub fn start() {
eframe::WebLogger::init(log::LevelFilter::Info).ok();
io::set_deliver(download);
wasm_bindgen_futures::spawn_local(async {
let _language = localization::enter(browser_language());
if let Some(el) = web_sys::window().and_then(|w| w.document()).and_then(|d| d.get_element_by_id(LOADING_ID)) {
el.set_text_content(Some(localization::text("Loading SoundCraft…")));
}
let Some(document) = web_sys::window().and_then(|w| w.document()) else {
log::error!("no document");
return;
@@ -49,7 +59,11 @@ pub fn start() {
if let Some(el) = document.get_element_by_id(LOADING_ID) {
match result {
Ok(()) => el.remove(),
Err(e) => el.set_inner_html(&format!("<p>SoundCraft failed to start: {e:?}</p><p>A browser with WebGPU or WebGL2 is required.</p>")),
Err(e) => el.set_text_content(Some(&format!(
"{} {}",
localized_format!("SoundCraft failed to start: {}", format!("{e:?}")),
localization::text("A browser with WebGPU or WebGL2 is required.")
))),
}
}
});
@@ -93,11 +107,11 @@ impl Purpose {
}
}
fn filter(self) -> (&'static str, Vec<&'static str>) {
fn filter(self) -> (String, Vec<&'static str>) {
match self {
Self::Session => ("SoundCraft Session", SESSION_EXTS.to_vec()),
Self::Audio => ("Audio", FileFormat::readable_extensions()),
Self::Midi => ("MIDI", MIDI_EXTS.to_vec()),
Self::Session => (localization::text("SoundCraft Session").to_owned(), SESSION_EXTS.to_vec()),
Self::Audio => (localization::text("Audio").to_owned(), craft_audio::DECODE_EXTENSIONS.to_vec()),
Self::Midi => (localization::text("MIDI").to_owned(), MIDI_EXTS.to_vec()),
}
}
}
@@ -127,9 +141,12 @@ impl WebShell {
let Some(purpose) = purpose else { return };
self.app.dialogs.open = None;
let (inbox, ctx) = (self.inbox.clone(), ctx.clone());
let (label, exts) = {
let _language = localization::enter(self.app.ui.language);
purpose.filter()
};
wasm_bindgen_futures::spawn_local(async move {
let (label, exts) = purpose.filter();
let Some(file) = rfd::AsyncFileDialog::new().add_filter(label, &exts).pick_file().await else { return };
let Some(file) = rfd::AsyncFileDialog::new().add_filter(&label, &exts).pick_file().await else { return };
let bytes = file.read().await;
inbox.borrow_mut().push((purpose, file.file_name(), bytes));
ctx.request_repaint();
@@ -166,6 +183,7 @@ impl WebShell {
}
fn handle(&mut self, purpose: Purpose, name: String, bytes: Vec<u8>) {
let _language = localization::enter(self.app.ui.language);
let e = &mut self.app.engine;
let at = e.session().edit.selection.start.max(0);
let result = match purpose {
@@ -175,15 +193,15 @@ impl WebShell {
Purpose::Audio => {
let before = e.session_arc();
io::import_audio_bytes(e, &name, &bytes, None, None, at).map(|_| {
e.push_undo("Import Audio", before);
format!("Imported {name}")
e.push_undo(localization::text("Import Audio"), before);
localized_format!("Imported {name}", name = name)
})
}
Purpose::Midi => {
let before = e.session_arc();
io::import_midi_bytes(e, &bytes, at, true).map(|_| {
e.push_undo("Import MIDI", before);
format!("Imported {name}")
e.push_undo(localization::text("Import MIDI"), before);
localized_format!("Imported {name}", name = name)
})
}
};
@@ -222,7 +240,8 @@ impl eframe::App for WebShell {
/// Hand a saved or exported file to the person as a download named `name` (the engine's delivery
/// hook on the web).
fn download(name: &str, bytes: &[u8]) -> Result<(), String> {
let js = |e: wasm_bindgen::JsValue| format!("download failed: {e:?}");
let _language = localization::enter(browser_language());
let js = |e: wasm_bindgen::JsValue| localized_format!("Download failed: {}", format!("{e:?}"));
let window = web_sys::window().ok_or("no window")?;
let document = window.document().ok_or("no document")?;
let parts = js_sys::Array::of1(&js_sys::Uint8Array::from(bytes));
@@ -235,7 +254,7 @@ fn download(name: &str, bytes: &[u8]) -> Result<(), String> {
Some("txt") => "text/plain",
Some("json" | "scraft") => "application/json",
Some("musicxml") => "application/vnd.recordare.musicxml+xml",
ext => ext.and_then(FileFormat::from_extension).map_or("application/octet-stream", FileFormat::mime_type),
ext => ext.and_then(craft_audio::encode_format_for).map_or("application/octet-stream", craft_audio::EncodeFormat::mime_type),
});
let blob = web_sys::Blob::new_with_u8_array_sequence_and_options(&parts, &opts).map_err(js)?;
let url = web_sys::Url::create_object_url_with_blob(&blob).map_err(js)?;
+1 -1
View File
@@ -16,7 +16,7 @@ asio = ["soundcraft-playback/asio"]
[dependencies]
soundcraft-engine.workspace = true
soundcraft-audio-io.workspace = true
craft-audio.workspace = true
soundcraft-ui-egui.workspace = true
soundcraft-playback.workspace = true
soundcraft-automation.workspace = true
+43 -7
View File
@@ -1,5 +1,7 @@
//! Loopback JSON-lines control server: one request per line, one reply per line.
//! This is the transport the MCP server (`soundcraft-cli mcp --connect PORT`) wraps.
//! A line that is not a JSON object with a string `method` gets one error reply, and the
//! connection is closed.
//!
//! Lines are bounded ([`MAX_LINE`]): a longer one is refused and the connection closed, without
//! buffering the rest. So is the number of connections served at once ([`MAX_CONNECTIONS`]).
@@ -103,7 +105,8 @@ fn read_bounded_line(reader: &mut impl BufRead, buf: &mut Vec<u8>, max: usize) -
}
/// Answer newline-delimited JSON requests for one connection: bound each line, forward it to the UI thread with a 90
/// s reply deadline, and stop at EOF, invalid UTF-8 or a closed request queue. Bad JSON still gets an error reply.
/// s reply deadline, and stop at EOF, invalid UTF-8, a closed request queue or a line that isn't a request (see
/// [`parse_request`]). That line gets one error reply first.
fn serve(stream: TcpStream, tx: SyncSender<ControlRequest>, ctx: egui::Context) {
let Ok(read) = stream.try_clone() else { return };
let mut out = stream;
@@ -123,11 +126,8 @@ fn serve(stream: TcpStream, tx: SyncSender<ControlRequest>, ctx: egui::Context)
if line.trim().is_empty() {
continue;
}
let reply = match serde_json::from_str::<Value>(line) {
Ok(msg) => {
let id = msg.get("id").cloned().unwrap_or(Value::Null);
let method = msg.get("method").and_then(Value::as_str).unwrap_or("").to_string();
let params = msg.get("params").cloned().unwrap_or(json!({}));
let reply = match parse_request(line) {
Ok((id, method, params)) => {
let (req, rrx) = ControlRequest::new(method, params);
match tx.try_send(req) {
Ok(()) => {
@@ -143,7 +143,12 @@ fn serve(stream: TcpStream, tx: SyncSender<ControlRequest>, ctx: egui::Context)
Err(TrySendError::Disconnected(_)) => break,
}
}
Err(e) => json!({"ok": false, "error": format!("bad JSON: {e}")}),
// Control clients send one request object per line. A peer that sends anything else isn't
// one: answer once and close, so nothing it sends after that line runs.
Err(e) => {
let _ = writeln!(out, "{}", json!({"ok": false, "error": format!("{e}; closing the connection")}));
break;
}
};
if writeln!(out, "{reply}").is_err() {
break;
@@ -151,6 +156,15 @@ fn serve(stream: TcpStream, tx: SyncSender<ControlRequest>, ctx: egui::Context)
}
}
/// A request line's `(id, method, params)`. The line must be a JSON object with a string `method`;
/// otherwise the error says what it is instead.
fn parse_request(line: &str) -> Result<(Value, String, Value), String> {
let msg = serde_json::from_str::<Value>(line).map_err(|e| format!("bad JSON: {e}"))?;
let Some(msg) = msg.as_object() else { return Err("not a request: expected a JSON object".into()) };
let Some(method) = msg.get("method").and_then(Value::as_str) else { return Err("not a request: no string `method`".into()) };
Ok((msg.get("id").cloned().unwrap_or(Value::Null), method.to_string(), msg.get("params").cloned().unwrap_or(json!({}))))
}
#[cfg(test)]
mod tests {
use super::*;
@@ -226,4 +240,26 @@ mod tests {
assert!(line.contains("too many control connections"), "{line}");
drop(held);
}
#[test]
fn a_line_that_is_not_a_request_ends_the_connection() {
let addr = serve_echo();
for junk in ["not json", "[1, 2]", "\"ping\"", r#"{"id": 5}"#, r#"{"id": 5, "method": 7}"#] {
let mut c = TcpStream::connect(addr).unwrap();
c.set_read_timeout(Some(Duration::from_secs(5))).unwrap();
// One write, so both lines arrive together and the request after the junk is waiting.
c.write_all(format!("{junk}\n{{\"id\":6,\"method\":\"ping\"}}\n").as_bytes()).unwrap();
let mut r = BufReader::new(c);
let mut line = String::new();
r.read_line(&mut line).unwrap();
let v: Value = serde_json::from_str(&line).unwrap();
assert_eq!(v["ok"].as_bool(), Some(false), "{junk}: {line}");
line.clear();
let closed = match r.read_line(&mut line) {
Ok(n) => n == 0,
Err(e) => e.kind() == std::io::ErrorKind::ConnectionReset,
};
assert!(closed, "{junk}: the connection stays open and the next request is answered: {line}");
}
}
}
+5 -3
View File
@@ -23,7 +23,6 @@ mod native_menu;
mod switch;
use env::Env;
use soundcraft_audio_io::FileFormat;
use soundcraft_engine::Engine;
use soundcraft_ui_egui::{Services, SoundApp, UiState, dialogs};
use std::sync::Arc;
@@ -153,13 +152,16 @@ fn services() -> Services {
let d = match purpose {
"session.open" => d.add_filter("SoundCraft Session", &["scraft"]),
"file.import_midi" => d.add_filter("MIDI", &["mid", "midi", "smf"]),
_ => d.add_filter("Audio", &FileFormat::readable_extensions()),
_ => d.add_filter("Audio", craft_audio::DECODE_EXTENSIONS),
};
d.pick_file()
})),
pick_open_many: Some(Box::new(|_purpose: &str| {
rfd::FileDialog::new().add_filter("Audio", craft_audio::DECODE_EXTENSIONS).pick_files().unwrap_or_default()
})),
pick_save: Some(Box::new(|purpose: &str, path: &std::path::Path| {
let mut d = match purpose {
"file.bounce_mix" => rfd::FileDialog::new().add_filter("Audio", &FileFormat::writable_extensions()),
"file.bounce_mix" => rfd::FileDialog::new().add_filter("Audio", craft_audio::ENCODE_EXTENSIONS),
_ => rfd::FileDialog::new().add_filter("SoundCraft Session", &["scraft"]),
};
if let Some(dir) = path.parent().filter(|dir| dir.is_dir()) {
+6 -1
View File
@@ -2,8 +2,13 @@
use std::process::{Command, Output};
/// Nextest archives relocate binaries; plain Cargo keeps the compile-time path valid.
fn soundcraft_binary() -> std::ffi::OsString {
std::env::var_os("NEXTEST_BIN_EXE_soundcraft").unwrap_or_else(|| env!("CARGO_BIN_EXE_soundcraft").into())
}
fn soundcraft(args: &[&str], control_port: &str) -> Output {
Command::new(env!("CARGO_BIN_EXE_soundcraft"))
Command::new(soundcraft_binary())
.args(args)
.env("SOUNDCRAFT_NO_PREFS", "1")
.env("SOUNDCRAFT_CONTROL_PORT", control_port)
-21
View File
@@ -1,21 +0,0 @@
[package]
name = "soundcraft-audio-io"
description = "SoundCraft audio file I/O: WAV/BWF/RF64, AIFF/AIFF-C native readers and writers, FLAC encoder, symphonia-backed decode of compressed formats, waveform peaks"
version.workspace = true
edition.workspace = true
license.workspace = true
rust-version.workspace = true
repository.workspace = true
[dependencies]
serde.workspace = true
thiserror.workspace = true
log.workspace = true
symphonia.workspace = true
[dev-dependencies]
proptest.workspace = true
serde_json.workspace = true
[lints]
workspace = true
-283
View File
@@ -1,283 +0,0 @@
//! Native AIFF / AIFF-C reader and writer (Apple "Audio Interchange File Format" 1.3 and the
//! AIFF-C draft: big-endian IFF chunks, 80-bit IEEE extended sample rate).
use crate::pcm::{PcmKind, Quantizer, be_u16, be_u32, deinterleave, sample_format, tag, validate_buffer, validate_layout, write_interleaved};
use crate::{AudioBuffer, AudioError, AudioInfo, BitDepth, FileFormat, Result, SampleFormat};
use std::io::{Cursor, Seek, SeekFrom, Write};
/// AIFF-C version 1 timestamp for the FVER chunk.
const AIFC_VERSION_1: u32 = 0xA280_5140;
const MAX_SAMPLE_RATE: f64 = 10_000_000.0;
pub(crate) struct AiffLayout {
pub info: AudioInfo,
pub kind: PcmKind,
pub data_start: usize,
pub data_len: usize,
}
pub(crate) enum AiffParse {
Native(AiffLayout),
/// Compressed AIFF-C (ulaw, alaw, ima4, ...): handed to symphonia.
Foreign,
}
/// True when the bytes start like an AIFF or AIFF-C file.
pub(crate) fn is_aiff(b: &[u8]) -> bool {
tag(b, 0).is_some_and(|t| &t == b"FORM") && tag(b, 8).is_some_and(|t| &t == b"AIFF" || &t == b"AIFC")
}
/// Decode an 80-bit IEEE 754 extended-precision float.
pub(crate) fn extended_to_f64(b: &[u8]) -> Option<f64> {
let se = be_u16(b, 0)?;
let hi = u64::from(be_u32(b, 2)?);
let lo = u64::from(be_u32(b, 6)?);
let mant = (hi << 32) | lo;
if mant == 0 {
return Some(0.0);
}
let exp = i32::from(se & 0x7FFF);
if exp == 0x7FFF {
return None; // inf / NaN
}
let v = mant as f64 * 2f64.powi(exp - 16383 - 63);
Some(if se & 0x8000 != 0 { -v } else { v })
}
/// Encode a positive integer as an 80-bit IEEE 754 extended float (exact).
pub(crate) fn u32_to_extended(r: u32) -> [u8; 10] {
let mut out = [0u8; 10];
if r == 0 {
return out;
}
let e = 31 - r.leading_zeros();
let exp = (16383 + e) as u16;
let mant = u64::from(r) << (63 - e);
out[..2].copy_from_slice(&exp.to_be_bytes());
out[2..].copy_from_slice(&mant.to_be_bytes());
out
}
pub(crate) fn parse(b: &[u8]) -> Result<AiffParse> {
if !is_aiff(b) {
return Err(AudioError::Malformed("not an AIFF/AIFF-C file".into()));
}
let is_aifc = tag(b, 8).is_some_and(|t| &t == b"AIFC");
let mut comm: Option<(i16, u32, i16, f64, [u8; 4])> = None;
let mut ssnd: Option<(usize, usize)> = None;
let mut pos: usize = 12;
let mut chunks = 0usize;
while let (Some(id), Some(size32)) = (tag(b, pos), be_u32(b, pos + 4)) {
chunks += 1;
if chunks > 100_000 {
break;
}
let body = pos.checked_add(8).ok_or_else(|| AudioError::Malformed("chunk offset overflow".into()))?;
let remaining = b.len().saturating_sub(body);
let declared = usize::try_from(size32).unwrap_or(usize::MAX);
let len = declared.min(remaining);
let c = b.get(body..body + len).unwrap_or_default();
match &id {
b"COMM" => {
let short = || AudioError::Malformed("COMM chunk too short".into());
let channels = be_u16(c, 0).ok_or_else(short)? as i16;
let frames = be_u32(c, 2).ok_or_else(short)?;
let bits = be_u16(c, 6).ok_or_else(short)? as i16;
let rate = extended_to_f64(c.get(8..18).ok_or_else(short)?).ok_or_else(|| AudioError::Malformed("invalid sample rate".into()))?;
let compression = if is_aifc { tag(c, 18).ok_or_else(short)? } else { *b"NONE" };
comm = Some((channels, frames, bits, rate, compression));
}
b"SSND" if ssnd.is_none() => {
let offset = usize::try_from(be_u32(c, 0).unwrap_or(0)).unwrap_or(usize::MAX);
let start = body.saturating_add(8).saturating_add(offset).min(b.len());
let data_len = len.saturating_sub(8).saturating_sub(offset);
ssnd = Some((start, data_len));
}
_ => {}
}
if declared > remaining {
break;
}
pos = match body.checked_add(declared).and_then(|p| p.checked_add(declared & 1)) {
Some(p) => p,
None => break,
};
}
let (channels, frames, bits, rate, compression) = comm.ok_or_else(|| AudioError::Malformed("AIFF has no COMM chunk".into()))?;
if channels <= 0 {
return Err(AudioError::Malformed("AIFF declares zero or negative channels".into()));
}
if !(1.0..=MAX_SAMPLE_RATE).contains(&rate) {
return Err(AudioError::Malformed(format!("AIFF sample rate {rate} out of range")));
}
let sample_rate = rate.round() as u32;
let int_kind = |big_endian: bool| -> Result<(PcmKind, SampleFormat)> {
if !(1..=32).contains(&bits) {
return Err(AudioError::Unsupported(format!("AIFF sample size {bits}")));
}
let bytes = (bits as usize).div_ceil(8);
let sf = match bits {
1..=8 => SampleFormat::Int8,
9..=16 => SampleFormat::Int16,
17..=24 => SampleFormat::Int24,
_ => SampleFormat::Int32,
};
Ok((PcmKind::Int { bytes, big_endian, unsigned: false }, sf))
};
let (kind, sample_format) = match &compression {
b"NONE" | b"twos" => int_kind(true)?,
b"sowt" => int_kind(false)?,
b"fl32" | b"FL32" => (PcmKind::Float { bytes: 4, big_endian: true }, SampleFormat::Float32),
b"fl64" | b"FL64" => (PcmKind::Float { bytes: 8, big_endian: true }, SampleFormat::Float64),
_ => return Ok(AiffParse::Foreign),
};
let (data_start, data_len) = ssnd.unwrap_or((b.len(), 0));
let block = kind.bytes().saturating_mul(channels as usize).max(1);
let frames = u64::from(frames).min((data_len / block) as u64);
Ok(AiffParse::Native(AiffLayout {
info: AudioInfo { format: FileFormat::Aiff, sample_format, sample_rate, channels: channels as u16, frames, bwf: None },
kind,
data_start,
data_len,
}))
}
pub(crate) fn decode(b: &[u8], layout: &AiffLayout) -> Result<AudioBuffer> {
let end = layout.data_start.saturating_add(layout.data_len).min(b.len());
let data = b.get(layout.data_start..end).unwrap_or_default();
let channels = deinterleave(data, layout.kind, usize::from(layout.info.channels), layout.info.frames)?;
Ok(AudioBuffer { sample_rate: layout.info.sample_rate, channels })
}
fn push_chunk(out: &mut Vec<u8>, id: &[u8; 4], body: &[u8]) {
out.extend_from_slice(id);
out.extend_from_slice(&u32::try_from(body.len()).unwrap_or(u32::MAX).to_be_bytes());
out.extend_from_slice(body);
if body.len() % 2 == 1 {
out.push(0);
}
}
/// Encode AIFF (integer depths) or AIFF-C `fl32` (Float32).
pub(crate) fn encode(buf: &AudioBuffer, depth: BitDepth, dither: bool) -> Result<Vec<u8>> {
validate_buffer(buf)?;
let frames = buf.frames();
let head_len = header(buf.channels.len(), buf.sample_rate, depth, frames as u64)?.len();
let data_len = usize::from(sample_format(depth).0 / 8).saturating_mul(buf.channels.len()).saturating_mul(frames);
let total = head_len.saturating_add(data_len).saturating_add(1);
let mut out = Vec::new();
out.try_reserve_exact(total).map_err(|_| AudioError::TooLarge(format!("cannot allocate {total} bytes for AIFF output")))?;
let mut w = Writer::new(Cursor::new(out), buf.channels.len(), buf.sample_rate, depth, dither)?;
w.write(&buf.channels, frames)?;
Ok(w.finish()?.into_inner())
}
/// An AIFF written block by block: a placeholder header first, then the real one (the same
/// length) written over it by [`Writer::finish`].
pub(crate) struct Writer<W> {
out: W,
start: u64,
channels: usize,
sample_rate: u32,
depth: BitDepth,
quantizers: Vec<Quantizer>,
frames: u64,
bytes: Vec<u8>,
/// Set when a block write has failed: the sink may hold a prefix of that block while
/// `frames` does not count it, so the writer refuses every later call.
failed: bool,
}
impl<W: Write + Seek> Writer<W> {
pub(crate) fn new(mut out: W, channels: usize, sample_rate: u32, depth: BitDepth, dither: bool) -> Result<Self> {
validate_layout(channels, sample_rate)?;
let start = out.stream_position()?;
out.write_all(&header(channels, sample_rate, depth, 0)?)?;
let quantizers = Quantizer::per_channel(u32::from(sample_format(depth).0), dither, channels);
Ok(Writer { out, start, channels, sample_rate, depth, quantizers, frames: 0, bytes: Vec::new(), failed: false })
}
/// The frame count after `frames` more, or an error when AIFF can't hold that many (4 GiB).
pub(crate) fn check_room(&self, frames: u64) -> Result<u64> {
let total = self.frames.saturating_add(frames);
header(self.channels, self.sample_rate, self.depth, total)?;
Ok(total)
}
/// Appends `frames` frames of planar audio (missing samples are silence), unless they don't
/// fit (see [`Writer::check_room`]).
pub(crate) fn write(&mut self, block: &[Vec<f32>], frames: usize) -> Result<()> {
if self.failed {
return Err(crate::failed_write_error());
}
let total = self.check_room(frames as u64)?;
if let Err(e) = write_interleaved(&mut self.out, &mut self.bytes, block, frames, &mut self.quantizers, sample_format(self.depth), true) {
self.failed = true;
return Err(AudioError::from(e));
}
self.frames = total;
Ok(())
}
pub(crate) fn finish(mut self) -> Result<W> {
if self.failed {
return Err(crate::failed_write_error());
}
let data_len = u64::from(sample_format(self.depth).0 / 8).saturating_mul(self.channels as u64).saturating_mul(self.frames);
if data_len % 2 == 1 {
self.out.write_all(&[0])?;
}
let head = header(self.channels, self.sample_rate, self.depth, self.frames)?;
self.out.seek(SeekFrom::Start(self.start))?;
self.out.write_all(&head)?;
self.out.seek(SeekFrom::End(0))?;
self.out.flush()?;
Ok(self.out)
}
}
/// Everything before the sample data of an AIFF holding `frames` frames; its length doesn't
/// depend on `frames`.
fn header(channels: usize, sample_rate: u32, depth: BitDepth, frames: u64) -> Result<Vec<u8>> {
let channels_i16 = i16::try_from(channels).map_err(|_| AudioError::Encode("too many channels for AIFF".into()))?;
let frames_u32 = u32::try_from(frames).map_err(|_| AudioError::TooLarge("too many frames for AIFF".into()))?;
let (bits, is_float) = sample_format(depth);
let data_len = u64::from(bits / 8).saturating_mul(channels as u64).saturating_mul(frames);
let mut comm = Vec::with_capacity(40);
comm.extend_from_slice(&channels_i16.to_be_bytes());
comm.extend_from_slice(&frames_u32.to_be_bytes());
comm.extend_from_slice(&bits.to_be_bytes());
comm.extend_from_slice(&u32_to_extended(sample_rate));
if is_float {
comm.extend_from_slice(b"fl32");
let name = b"32-bit float";
comm.push(name.len() as u8);
comm.extend_from_slice(name);
if (name.len() + 1) % 2 == 1 {
comm.push(0);
}
}
let mut head = Vec::with_capacity(128);
head.extend_from_slice(b"FORM");
head.extend_from_slice(&[0, 0, 0, 0]); // patched below
head.extend_from_slice(if is_float { b"AIFC" } else { b"AIFF" });
if is_float {
push_chunk(&mut head, b"FVER", &AIFC_VERSION_1.to_be_bytes());
}
push_chunk(&mut head, b"COMM", &comm);
head.extend_from_slice(b"SSND");
let ssnd_len = u32::try_from(data_len.saturating_add(8)).map_err(|_| AudioError::TooLarge("AIFF data exceeds 4 GiB".into()))?;
head.extend_from_slice(&ssnd_len.to_be_bytes());
head.extend_from_slice(&[0u8; 8]); // offset, blockSize
let total = (head.len() as u64).saturating_add(data_len).saturating_add(data_len & 1);
let form_len = u32::try_from(total - 8).map_err(|_| AudioError::TooLarge("AIFF file exceeds 4 GiB".into()))?;
if let Some(slot) = head.get_mut(4..8) {
slot.copy_from_slice(&form_len.to_be_bytes());
}
Ok(head)
}
-71
View File
@@ -1,71 +0,0 @@
//! Planar `f32` audio buffer.
/// Planar f32 audio.
///
/// Every channel is expected to hold the same number of frames; [`AudioBuffer::frames`] reports the
/// shortest channel so a ragged buffer never causes out-of-bounds access.
#[derive(Debug, Clone, Default, PartialEq)]
pub struct AudioBuffer {
/// Sample rate in Hz.
pub sample_rate: u32,
/// One `Vec` of samples per channel.
pub channels: Vec<Vec<f32>>,
}
impl AudioBuffer {
/// A silent buffer with `channels` channels of `frames` frames each.
pub fn new(sample_rate: u32, channels: usize, frames: usize) -> Self {
Self { sample_rate, channels: vec![vec![0.0; frames]; channels] }
}
/// Number of frames (the length of the shortest channel; 0 when there are no channels).
pub fn frames(&self) -> usize {
self.channels.iter().map(Vec::len).min().unwrap_or(0)
}
/// Number of channels.
pub fn num_channels(&self) -> usize {
self.channels.len()
}
/// Duration in seconds (0 when the sample rate is 0).
pub fn duration_secs(&self) -> f64 {
if self.sample_rate == 0 {
return 0.0;
}
self.frames() as f64 / f64::from(self.sample_rate)
}
/// Interleaved copy of the samples (frame-major: `L0 R0 L1 R1 ...`).
pub fn interleaved(&self) -> Vec<f32> {
let frames = self.frames();
let mut out = Vec::with_capacity(frames.saturating_mul(self.channels.len()));
for f in 0..frames {
for ch in &self.channels {
out.push(ch.get(f).copied().unwrap_or(0.0));
}
}
out
}
/// Build a planar buffer from interleaved samples. A trailing partial frame is dropped;
/// `channels == 0` yields an empty buffer.
pub fn from_interleaved(sr: u32, channels: usize, data: &[f32]) -> Self {
if channels == 0 {
return Self { sample_rate: sr, channels: Vec::new() };
}
let frames = data.len() / channels;
let mut out: Vec<Vec<f32>> = (0..channels).map(|_| Vec::with_capacity(frames)).collect();
for frame in data.chunks_exact(channels) {
for (ch, &s) in out.iter_mut().zip(frame) {
ch.push(s);
}
}
Self { sample_rate: sr, channels: out }
}
/// Absolute peak over all channels (NaN samples are ignored).
pub fn peak(&self) -> f32 {
self.channels.iter().flat_map(|c| c.iter()).fold(0.0f32, |acc, &s| acc.max(s.abs()))
}
}
-98
View File
@@ -1,98 +0,0 @@
//! Size checks on CAF chunks before symphonia reads them: it allocates whatever a magic cookie,
//! a packet table or a packet claims, and a failed allocation aborts the process.
use crate::pcm::{be_u16, be_u32, be_u64};
use crate::{AudioError, Result};
/// Refuse the CAF stream at `at` if its magic cookie, packet table or packets claim more bytes
/// than the file holds.
pub(crate) fn check(b: &[u8], at: usize) -> Result<()> {
if be_u16(b, at + 4) == Some(1) { chunks(b, at + 8) } else { Ok(()) }
}
fn malformed(what: &str) -> AudioError {
AudioError::Malformed(format!("CAF {what} is larger than the file"))
}
/// 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).
let (mut lists_sizes, mut lists_frames, mut described) = (false, false, false);
while let (Some(kind), Some(size)) = (b.get(pos..pos + 4), be_u64(b, pos + 4)) {
let body = pos + 12;
let used = match kind {
b"desc" if size == 32 => {
lists_sizes = be_u32(b, body + 16) == Some(0);
lists_frames = be_u32(b, body + 20) == Some(0);
described = true;
Some(32)
}
b"data" if size == u64::MAX => Some(4),
b"chan" if size >= 12 => be_u32(b, body + 8).map(|n| 12 + u64::from(n) * 20),
b"pakt" if size >= 24 && described => packet_table(b, body, lists_sizes, lists_frames)?,
b"kuki" if size > b.len().saturating_sub(body) as u64 => return Err(malformed("magic cookie")),
b"desc" | b"pakt" | b"chan" => None,
_ => Some(size),
};
match used.filter(|_| described).and_then(|n| usize::try_from(n).ok()).and_then(|n| body.checked_add(n)) {
Some(next) if next < b.len() => pos = next,
_ => return Ok(()),
}
}
Ok(())
}
/// Check a packet table's count and packet sizes; returns the bytes symphonia reads for it, or
/// None where symphonia stops. It reserves room for every packet before reading any, so a table
/// it would refuse after that, or that runs past the file, is refused first.
fn packet_table(b: &[u8], body: usize, lists_sizes: bool, lists_frames: bool) -> Result<Option<u64>> {
let valid = |at: usize| be_u64(b, at).filter(|&n| n as i64 >= 0);
let (Some(packets), Some(_)) = (valid(body), valid(body + 8)) else { return Ok(None) };
let mut at = body + 24;
if packets > b.len().saturating_sub(at) as u64 {
return Err(malformed("packet table"));
}
if !lists_sizes && !lists_frames {
return if packets == 0 { Ok(Some(24)) } else { Err(AudioError::Malformed("a constant bit rate CAF lists packets".into())) };
}
for _ in 0..packets {
if lists_sizes {
let Some((size, len)) = varint(b, at) else { return Err(malformed("packet table")) };
if size > b.len() as u64 {
return Err(malformed("packet"));
}
at += len;
}
if lists_frames {
let Some((_, len)) = varint(b, at) else { return Err(malformed("packet table")) };
at += len;
}
}
Ok(Some((at - body) as u64))
}
/// A CAF variable-length integer at `at` and its length in bytes.
fn varint(b: &[u8], at: usize) -> Option<(u64, usize)> {
let mut v = 0u64;
for i in 0..9 {
let x = *b.get(at + i)?;
v |= u64::from(x & 0x7f);
if x & 0x80 == 0 {
return Some((v, i + 1));
}
v <<= 7;
}
None
}
#[cfg(test)]
mod tests {
#[test]
fn rejects_cookie_size_above_signed_range() {
let mut bytes = b"caff\0\x01\0\0kuki".to_vec();
bytes.extend_from_slice(&(1u64 << 63).to_be_bytes());
assert!(super::check(&bytes, 0).is_err());
}
}
-563
View File
@@ -1,563 +0,0 @@
//! A small pure-Rust FLAC encoder (RFC 9639): fixed blocksize, independent channels, and per
//! subframe the cheapest of CONSTANT, VERBATIM or FIXED (orders 0–4) prediction with
//! partitioned Rice-coded residuals. Decoding is symphonia's, after [`check_metadata`].
use crate::pcm::{Quantizer, be_u32, le_u32, validate_buffer};
use crate::{AudioBuffer, AudioError, AudioInfo, BitDepth, FileFormat, Result, SampleFormat};
use std::io::{Cursor, Seek, SeekFrom, Write};
const BLOCK_SIZE: usize = 4096;
const MAX_PARTITION_ORDER: u32 = 6;
struct BitWriter {
out: Vec<u8>,
acc: u64,
nbits: u32,
}
impl BitWriter {
fn new() -> Self {
Self { out: Vec::new(), acc: 0, nbits: 0 }
}
/// Write the low `n` (≤ 32) bits of `v`, MSB first.
fn write(&mut self, v: u64, n: u32) {
if n == 0 {
return;
}
let n = n.min(32);
let mask = (1u64 << n) - 1;
self.acc = (self.acc << n) | (v & mask);
self.nbits += n;
while self.nbits >= 8 {
self.nbits -= 8;
self.out.push((self.acc >> self.nbits) as u8);
}
self.acc &= (1u64 << self.nbits) - 1;
}
fn write_signed(&mut self, v: i64, n: u32) {
self.write(v as u64, n);
}
/// `q` zero bits followed by a one bit.
fn write_unary(&mut self, mut q: u64) {
while q >= 32 {
self.write(0, 32);
q -= 32;
}
self.write(1, (q as u32) + 1);
}
fn align(&mut self) {
if self.nbits > 0 {
let pad = 8 - self.nbits;
self.write(0, pad);
}
}
fn into_bytes(mut self) -> Vec<u8> {
self.align();
self.out
}
}
fn crc8(data: &[u8]) -> u8 {
let mut crc: u8 = 0;
for &b in data {
crc ^= b;
for _ in 0..8 {
crc = if crc & 0x80 != 0 { (crc << 1) ^ 0x07 } else { crc << 1 };
}
}
crc
}
fn crc16(data: &[u8]) -> u16 {
let mut crc: u16 = 0;
for &b in data {
crc ^= u16::from(b) << 8;
for _ in 0..8 {
crc = if crc & 0x8000 != 0 { (crc << 1) ^ 0x8005 } else { crc << 1 };
}
}
crc
}
/// FLAC's UTF-8-like variable-length integer coding (up to 36 bits).
fn write_utf8_number(w: &mut BitWriter, v: u64) {
if v < 0x80 {
w.write(v, 8);
return;
}
// Payload bits available for an n-byte coding: 5n + 1 (n = 2..=6), 36 for n = 7.
let n: u32 = (2..=7).find(|&n| if n == 7 { true } else { v < (1u64 << (5 * n + 1)) }).unwrap_or(7);
let cont_bits = 6 * (n - 1);
let lead_payload = if n == 7 { 0 } else { 7 - n };
let prefix: u64 = (0xFFu64 << (8 - n)) & 0xFF;
let lead = prefix | ((v >> cont_bits) & ((1u64 << lead_payload) - 1));
w.write(lead, 8);
for i in (0..n - 1).rev() {
w.write(0x80 | ((v >> (6 * i)) & 0x3F), 8);
}
}
fn sample_rate_code(r: u32) -> (u64, Option<(u64, u32)>) {
match r {
88_200 => (1, None),
176_400 => (2, None),
192_000 => (3, None),
8_000 => (4, None),
16_000 => (5, None),
22_050 => (6, None),
24_000 => (7, None),
32_000 => (8, None),
44_100 => (9, None),
48_000 => (10, None),
96_000 => (11, None),
r if r % 1000 == 0 && r / 1000 <= 255 => (12, Some((u64::from(r / 1000), 8))),
r if r <= 65_535 => (13, Some((u64::from(r), 16))),
r if r % 10 == 0 && r / 10 <= 65_535 => (14, Some((u64::from(r / 10), 16))),
_ => (0, None),
}
}
fn bps_code(bps: u32) -> u64 {
match bps {
8 => 1,
12 => 2,
16 => 4,
20 => 5,
24 => 6,
_ => 0, // from STREAMINFO
}
}
fn zigzag(r: i64) -> u64 {
((r << 1) ^ (r >> 63)) as u64
}
/// Fixed-predictor residuals for `order`, or `None` when any residual leaves the i32 range.
fn fixed_residual(x: &[i64], order: usize) -> Option<Vec<i64>> {
let mut res = Vec::with_capacity(x.len().saturating_sub(order));
for n in order..x.len() {
let g = |k: usize| x.get(n - k).copied().unwrap_or(0);
let r = match order {
0 => g(0),
1 => g(0) - g(1),
2 => g(0) - 2 * g(1) + g(2),
3 => g(0) - 3 * g(1) + 3 * g(2) - g(3),
_ => g(0) - 4 * g(1) + 6 * g(2) - 4 * g(3) + g(4),
};
if r < i64::from(i32::MIN) || r > i64::from(i32::MAX) {
return None;
}
res.push(r);
}
Some(res)
}
/// Best Rice parameter and its cost in bits for one partition.
fn best_rice(part: &[u64]) -> (u32, u64) {
if part.is_empty() {
return (0, 0);
}
let sum: u64 = part.iter().sum();
let mean = sum / part.len() as u64;
let guess = if mean == 0 { 0 } else { 63 - mean.leading_zeros() };
let mut best = (0u32, u64::MAX);
for k in guess.saturating_sub(1)..=(guess + 1).min(30) {
let cost = part.len() as u64 * u64::from(k + 1) + part.iter().map(|&u| u >> k).sum::<u64>();
if cost < best.1 {
best = (k, cost);
}
}
best
}
/// Residual coding plan: partition order, per-partition parameters, total bits.
struct RicePlan {
order: u32,
params: Vec<u32>,
bits: u64,
wide: bool,
}
fn plan_residual(u: &[u64], block_len: usize, pred_order: usize) -> RicePlan {
let mut best: Option<RicePlan> = None;
for p in 0..=MAX_PARTITION_ORDER {
let parts = 1usize << p;
if !block_len.is_multiple_of(parts) || block_len / parts <= pred_order {
break;
}
let per = block_len / parts;
let mut params = Vec::with_capacity(parts);
let mut bits = 0u64;
let mut start = 0usize;
for i in 0..parts {
let len = if i == 0 { per - pred_order } else { per };
let part = u.get(start..start + len).unwrap_or_default();
start += len;
let (k, cost) = best_rice(part);
params.push(k);
bits += cost;
}
let wide = params.iter().any(|&k| k > 14);
bits += 6 + parts as u64 * if wide { 5 } else { 4 };
if best.as_ref().is_none_or(|b| bits < b.bits) {
best = Some(RicePlan { order: p, params, bits, wide });
}
}
best.unwrap_or(RicePlan { order: 0, params: vec![0], bits: u64::MAX, wide: false })
}
fn write_residual(w: &mut BitWriter, u: &[u64], block_len: usize, pred_order: usize, plan: &RicePlan) {
w.write(u64::from(plan.wide), 2);
w.write(u64::from(plan.order), 4);
let parts = 1usize << plan.order;
let per = block_len / parts;
let mut start = 0usize;
for (i, &k) in plan.params.iter().enumerate() {
let len = if i == 0 { per - pred_order } else { per };
w.write(u64::from(k), if plan.wide { 5 } else { 4 });
for &v in u.get(start..start + len).unwrap_or_default() {
w.write_unary(v >> k);
w.write(v, k);
}
start += len;
}
}
fn write_subframe(w: &mut BitWriter, x: &[i64], bps: u32) {
let n = x.len();
let first = x.first().copied().unwrap_or(0);
if x.iter().all(|&s| s == first) {
w.write(0, 1);
w.write(0, 6);
w.write(0, 1);
w.write_signed(first, bps);
return;
}
let verbatim_bits = n as u64 * u64::from(bps);
let mut best: Option<(usize, Vec<u64>, RicePlan)> = None;
for order in 0..=4usize.min(n.saturating_sub(1)) {
let Some(res) = fixed_residual(x, order) else { continue };
let u: Vec<u64> = res.into_iter().map(zigzag).collect();
let plan = plan_residual(&u, n, order);
let bits = plan.bits.saturating_add(order as u64 * u64::from(bps));
if bits < verbatim_bits && best.as_ref().is_none_or(|b| bits < b.2.bits.saturating_add(b.0 as u64 * u64::from(bps))) {
best = Some((order, u, plan));
}
}
match best {
Some((order, u, plan)) => {
w.write(0, 1);
w.write(8 + order as u64, 6);
w.write(0, 1);
for &s in x.iter().take(order) {
w.write_signed(s, bps);
}
write_residual(w, &u, n, order, &plan);
}
None => {
w.write(0, 1);
w.write(1, 6);
w.write(0, 1);
for &s in x {
w.write_signed(s, bps);
}
}
}
}
/// Checks that FLAC can hold the layout; returns the bits per sample.
fn check(channels: usize, sample_rate: u32, depth: BitDepth) -> Result<u32> {
crate::pcm::validate_layout(channels, sample_rate)?;
if channels > 8 {
return Err(AudioError::Unsupported(format!("FLAC supports at most 8 channels, got {channels}")));
}
if sample_rate > 655_350 {
return Err(AudioError::Unsupported(format!("FLAC sample rate {sample_rate} exceeds 655350 Hz")));
}
match depth {
BitDepth::Int16 => Ok(16),
BitDepth::Int24 => Ok(24),
BitDepth::Int32 => Ok(32),
BitDepth::Float32 => Err(AudioError::Unsupported("FLAC cannot store 32-bit float samples".into())),
}
}
fn stream_info(channels: usize, sample_rate: u32, bps: u32, total: u64) -> Vec<u8> {
let mut si = BitWriter::new();
si.write(1, 1); // last metadata block
si.write(0, 7); // STREAMINFO
si.write(34, 24);
si.write(BLOCK_SIZE as u64, 16);
si.write(BLOCK_SIZE as u64, 16);
si.write(0, 24);
si.write(0, 24);
si.write(u64::from(sample_rate), 20);
si.write(channels as u64 - 1, 3);
si.write(u64::from(bps - 1), 5);
si.write(total >> 32, 4);
si.write(total & 0xFFFF_FFFF, 32);
for _ in 0..4 {
si.write(0, 32); // MD5 unknown
}
si.into_bytes()
}
/// A FLAC stream written block by block; [`Writer::finish`] fills in the total sample count.
pub(crate) struct Writer<W> {
out: W,
start: u64,
channels: usize,
sample_rate: u32,
bps: u32,
quantizers: Vec<Quantizer>,
pending: Vec<Vec<i64>>,
frames: u64,
frame_no: u64,
/// Set when a frame write has failed: `write_frame` clears the pending samples before
/// writing, so a failed frame is lost while `frames` still counts it; the writer refuses
/// every later call.
failed: bool,
}
impl<W: Write + Seek> Writer<W> {
pub(crate) fn new(mut out: W, channels: usize, sample_rate: u32, depth: BitDepth, dither: bool) -> Result<Self> {
let bps = check(channels, sample_rate, depth)?;
let start = out.stream_position()?;
out.write_all(b"fLaC")?;
out.write_all(&stream_info(channels, sample_rate, bps, 0))?;
Ok(Writer {
out,
start,
channels,
sample_rate,
bps,
quantizers: Quantizer::per_channel(bps, dither, channels),
pending: (0..channels).map(|_| Vec::with_capacity(BLOCK_SIZE)).collect(),
frames: 0,
frame_no: 0,
failed: false,
})
}
/// The frame count after `frames` more, or an error past what FLAC can count (2^36 frames).
pub(crate) fn check_room(&self, frames: u64) -> Result<u64> {
let total = self.frames.saturating_add(frames);
if total >= 1u64 << 36 {
return Err(AudioError::TooLarge("too many frames for FLAC".into()));
}
Ok(total)
}
/// Appends `frames` frames of planar audio (missing samples are silence), unless they don't
/// fit (see [`Writer::check_room`]).
pub(crate) fn write(&mut self, block: &[Vec<f32>], frames: usize) -> Result<()> {
if self.failed {
return Err(crate::failed_write_error());
}
let total = self.check_room(frames as u64)?;
let mut done = 0;
while done < frames {
let held = self.pending.first().map_or(0, Vec::len);
let n = (BLOCK_SIZE - held).min(frames - done);
for (c, (p, q)) in self.pending.iter_mut().zip(self.quantizers.iter_mut()).enumerate() {
let src = block.get(c).and_then(|ch| ch.get(done..)).unwrap_or_default();
p.extend((0..n).map(|i| i64::from(q.quantize(src.get(i).copied().unwrap_or(0.0)))));
}
done += n;
if held + n == BLOCK_SIZE
&& let Err(e) = self.write_frame()
{
self.failed = true;
return Err(e);
}
}
self.frames = total;
Ok(())
}
fn write_frame(&mut self) -> Result<()> {
let len = self.pending.first().map_or(0, Vec::len);
if len == 0 {
return Ok(());
}
let (sr_code, sr_extra) = sample_rate_code(self.sample_rate);
let mut w = BitWriter::new();
w.write(0x3FFE, 14);
w.write(0, 1);
w.write(0, 1); // fixed blocksize stream
w.write(7, 4); // 16-bit (blocksize-1) follows
w.write(sr_code, 4);
w.write(self.channels as u64 - 1, 4);
w.write(bps_code(self.bps), 3);
w.write(0, 1);
write_utf8_number(&mut w, self.frame_no);
w.write(len as u64 - 1, 16);
if let Some((v, n)) = sr_extra {
w.write(v, n);
}
let header = w.into_bytes();
let mut w = BitWriter::new();
for &b in &header {
w.write(u64::from(b), 8);
}
w.write(u64::from(crc8(&header)), 8);
for p in &mut self.pending {
write_subframe(&mut w, p, self.bps);
p.clear();
}
let mut frame = w.into_bytes();
let crc = crc16(&frame);
frame.extend_from_slice(&crc.to_be_bytes());
self.out.write_all(&frame)?;
self.frame_no += 1;
Ok(())
}
pub(crate) fn finish(mut self) -> Result<W> {
if self.failed {
return Err(crate::failed_write_error());
}
self.write_frame()?;
let info = stream_info(self.channels, self.sample_rate, self.bps, self.frames);
self.out.seek(SeekFrom::Start(self.start.saturating_add(4)))?;
self.out.write_all(&info)?;
self.out.seek(SeekFrom::End(0))?;
self.out.flush()?;
Ok(self.out)
}
}
pub(crate) fn encode(buf: &AudioBuffer, depth: BitDepth, dither: bool) -> Result<Vec<u8>> {
validate_buffer(buf)?;
let bps = check(buf.channels.len(), buf.sample_rate, depth)?;
let frames = buf.frames();
let mut out = Vec::new();
out.try_reserve(frames.saturating_mul(buf.channels.len()).saturating_mul(bps as usize / 8) / 2)
.map_err(|_| AudioError::TooLarge("cannot allocate FLAC output".into()))?;
let mut w = Writer::new(Cursor::new(out), buf.channels.len(), buf.sample_rate, depth, dither)?;
w.write(&buf.channels, frames)?;
Ok(w.finish()?.into_inner())
}
/// Refuse the FLAC stream at `at` if a comment or picture field claims more bytes than its
/// metadata block holds: symphonia allocates each field's length before reading it.
pub(crate) fn check_metadata(b: &[u8], at: usize) -> Result<()> {
let mut pos = at + 4;
while let Some(head) = be_u32(b, pos) {
let (body, len) = (pos + 4, (head & 0x00ff_ffff) as usize);
let end = (body + len).min(b.len());
let overruns = match (head >> 24) & 0x7f {
4 => comments_overrun(b, body, end),
// Picture: type, MIME type, description, four size fields, data.
6 => overruns(b, body + 4, end, be_u32, [0, 0, 16]),
_ => false,
};
if overruns {
return Err(AudioError::Malformed("a FLAC metadata field is larger than its block".into()));
}
if head >> 31 == 1 {
break;
}
pos = body + len;
}
Ok(())
}
/// The info of a FLAC stream with no audio frames at all: STREAMINFO declares 0 samples and
/// nothing follows the metadata blocks. Symphonia's reader fails on these, as it looks for a first
/// frame, so the caller answers them itself.
pub(crate) fn empty_stream_info(b: &[u8], at: usize) -> Option<AudioInfo> {
let mut pos = at.checked_add(4)?;
let mut first = true;
loop {
let head = be_u32(b, pos)?;
let (body, len) = (pos.checked_add(4)?, (head & 0x00ff_ffff) as usize);
let end = body.checked_add(len)?;
if end > b.len() {
return None;
}
if first && ((head >> 24) & 0x7f != 0 || len != 34) {
return None;
}
first = false;
if head >> 31 == 1 {
if end != b.len() {
return None;
}
break;
}
pos = end;
}
// STREAMINFO, from the sample rate on: 20 bits rate, 3 channels - 1, 5 bits per sample - 1, 36 bits total.
let si = at.checked_add(4 + 4 + 10)?;
let packed = u64::from_be_bytes(b.get(si..si.checked_add(8)?)?.try_into().ok()?);
let sample_rate = (packed >> 44) as u32;
let channels = ((packed >> 41) & 0x7) as u16 + 1;
let bps = ((packed >> 36) & 0x1f) as u32 + 1;
if packed & 0xf_ffff_ffff != 0 || sample_rate == 0 {
return None;
}
let sample_format = match bps {
1..=8 => SampleFormat::Int8,
9..=16 => SampleFormat::Int16,
17..=24 => SampleFormat::Int24,
_ => SampleFormat::Int32,
};
Some(AudioInfo { format: FileFormat::Flac, sample_format, sample_rate, channels, frames: 0, bwf: None })
}
/// Whether a vendor string or comment in a (little-endian) Vorbis comment block claims more than
/// the block holds.
fn comments_overrun(b: &[u8], body: usize, end: usize) -> bool {
let Some(vendor) = le_u32(b, body).filter(|_| body.saturating_add(4) <= end) else { return false };
if vendor as usize > end - (body + 4) {
return true;
}
let at = (body + 4).saturating_add(vendor as usize);
let Some(count) = le_u32(b, at).filter(|_| at.saturating_add(4) <= end) else { return false };
overruns(b, at + 4, end, le_u32, std::iter::repeat_n(0, count as usize))
}
/// Whether one of the length-prefixed fields from `at` claims more than `end` leaves; `gaps` are
/// the bytes skipped before each length, which `read` reads.
fn overruns(b: &[u8], mut at: usize, end: usize, read: fn(&[u8], usize) -> Option<u32>, gaps: impl IntoIterator<Item = usize>) -> bool {
for gap in gaps {
at = at.saturating_add(gap);
let Some(n) = read(b, at).filter(|_| at.saturating_add(4) <= end) else { return false };
at += 4;
if n as usize > end - at {
return true;
}
at += n as usize;
}
false
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn crc_known_values() {
// CRC-8/SMBUS and CRC-16/UMTS (FLAC's polynomials) of "123456789".
assert_eq!(crc8(b"123456789"), 0xF4);
assert_eq!(crc16(b"123456789"), 0xFEE8);
}
#[test]
fn utf8_numbers() {
let enc = |v| {
let mut w = BitWriter::new();
write_utf8_number(&mut w, v);
w.into_bytes()
};
assert_eq!(enc(0x7F), vec![0x7F]);
assert_eq!(enc(0x80), vec![0xC2, 0x80]);
assert_eq!(enc(0x800), vec![0xE0, 0xA0, 0x80]);
}
}
-459
View File
@@ -1,459 +0,0 @@
//! SoundCraft audio file I/O.
//!
//! Bytes in, bytes out (no filesystem access, so the crate also builds for `wasm32`):
//!
//! * [`detect_format`] / [`probe`] / [`decode`] read WAV (PCM 8/16/24/32, float 32/64,
//! `WAVE_FORMAT_EXTENSIBLE`, RF64/BW64, BWF `bext`) and AIFF/AIFF-C (big-endian, `sowt`,
//! `fl32`/`fl64`) with native readers, and everything else (FLAC, MP3, Ogg Vorbis, AAC/ALAC in
//! MP4, CAF, Matroska, ADPCM/A-law/µ-law WAV, ...) through `symphonia`.
//! * [`encode`] writes WAV/BWF (RF64 above 4 GiB), AIFF / AIFF-C `fl32`, and FLAC (built-in
//! encoder), with optional TPDF dither.
//! * [`wav_float_header`] heads a float WAV written while it grows (recording).
//! * [`peaks`] builds multi-resolution min/max overviews for waveform drawing.
//! * [`FileEncoder`] writes the same formats block by block into any `Write + Seek`, so a file
//! of any length never has to fit in memory.
#![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)]
mod aiff;
mod buffer;
mod caf;
mod flac;
mod mp4;
mod pcm;
pub mod peaks;
mod symph;
mod wav;
pub use buffer::AudioBuffer;
use std::io::{Seek, Write};
/// Errors from probing, decoding and encoding.
#[derive(Debug, thiserror::Error)]
pub enum AudioError {
/// The format, codec or option is valid but not supported.
#[error("unsupported audio: {0}")]
Unsupported(String),
/// The input is corrupt or not what it claims to be.
#[error("malformed audio: {0}")]
Malformed(String),
/// The input (or requested output) exceeds a size limit.
#[error("audio too large: {0}")]
TooLarge(String),
/// Encoding failed (e.g. an empty buffer).
#[error("audio encode failed: {0}")]
Encode(String),
/// Writing the output failed.
#[error("audio write failed: {0}")]
Io(#[from] std::io::Error),
}
/// Result alias for this crate.
pub type Result<T> = std::result::Result<T, AudioError>;
/// Container / codec family of an audio file.
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub enum FileFormat {
Wav,
Aiff,
Flac,
Mp3,
Ogg,
Aac,
Alac,
Caf,
/// Any other container `symphonia` reads: Matroska and WebM by name.
Other,
}
impl FileFormat {
/// The formats [`decode`] reads, found by extension. Contents decide when they disagree.
pub const READ: &'static [FileFormat] =
&[FileFormat::Wav, FileFormat::Aiff, FileFormat::Flac, FileFormat::Mp3, FileFormat::Ogg, FileFormat::Aac, FileFormat::Caf, FileFormat::Other];
/// The formats [`encode`] writes.
pub const WRITE: &'static [FileFormat] = &[FileFormat::Wav, FileFormat::Aiff, FileFormat::Flac];
/// The extensions that name this format, lower case and without the dot; the first is the one
/// a new file gets. ALAC shares AAC's MP4 container, so a `.m4a` name reads as AAC until the
/// contents say otherwise.
pub fn extensions(self) -> &'static [&'static str] {
match self {
FileFormat::Wav => &["wav", "wave", "bwf", "rf64"],
FileFormat::Aiff => &["aif", "aiff", "aifc"],
FileFormat::Flac => &["flac"],
FileFormat::Mp3 => &["mp3", "mp2", "mp1"],
FileFormat::Ogg => &["ogg", "oga"],
FileFormat::Aac => &["m4a", "m4b", "aac", "mp4", "adts"],
FileFormat::Alac => &["m4a"],
FileFormat::Caf => &["caf"],
FileFormat::Other => &["mkv", "webm"],
}
}
/// The name people know the format by, as menus show it.
pub fn name(self) -> &'static str {
match self {
FileFormat::Wav => "WAV",
FileFormat::Aiff => "AIFF",
FileFormat::Flac => "FLAC",
FileFormat::Mp3 => "MP3",
FileFormat::Ogg => "Ogg Vorbis",
FileFormat::Aac => "AAC",
FileFormat::Alac => "ALAC",
FileFormat::Caf => "CAF",
FileFormat::Other => "Other",
}
}
/// The media type a file of this format is served or downloaded as.
pub fn mime_type(self) -> &'static str {
match self {
FileFormat::Wav => "audio/wav",
FileFormat::Aiff => "audio/aiff",
FileFormat::Flac => "audio/flac",
FileFormat::Mp3 => "audio/mpeg",
FileFormat::Ogg => "audio/ogg",
FileFormat::Aac | FileFormat::Alac => "audio/mp4",
FileFormat::Caf => "audio/x-caf",
FileFormat::Other => "application/octet-stream",
}
}
/// The extension a new file of this format gets.
pub fn default_extension(self) -> &'static str {
self.extensions().first().copied().unwrap_or("bin")
}
/// The readable format an extension or format name names (`wav`, `.AIFF`; ASCII
/// case-insensitive), or `None` when nothing reads it.
pub fn from_extension(ext: &str) -> Option<FileFormat> {
let ext = ext.trim_start_matches('.');
Self::READ.iter().copied().find(|f| f.extensions().iter().any(|x| x.eq_ignore_ascii_case(ext)))
}
/// The readable format `path`'s extension names, or `None` without a known extension.
pub fn from_path(path: impl AsRef<std::path::Path>) -> Option<FileFormat> {
path.as_ref().extension().and_then(std::ffi::OsStr::to_str).and_then(Self::from_extension)
}
/// The extensions [`encode`] writes this format under: the names whose readers expect what it
/// writes. RF64 and AIFF-C names are left out because `encode` writes plain RIFF and AIFF
/// headers for them.
pub fn write_extensions(self) -> &'static [&'static str] {
match self {
FileFormat::Wav => &["wav", "wave", "bwf"],
FileFormat::Aiff => &["aif", "aiff"],
FileFormat::Flac => &["flac"],
_ => &[],
}
}
/// The format [`encode`] writes for an extension or format name (ASCII case-insensitive), or
/// `None` when it writes none.
pub fn from_write_extension(ext: &str) -> Option<FileFormat> {
let ext = ext.trim_start_matches('.');
Self::WRITE.iter().copied().find(|f| f.write_extensions().iter().any(|x| x.eq_ignore_ascii_case(ext)))
}
/// Every extension of [`FileFormat::READ`], in order: what file pickers and browsers filter by.
pub fn readable_extensions() -> Vec<&'static str> {
Self::READ.iter().flat_map(|f| f.extensions().iter().copied()).collect()
}
/// Every extension of [`FileFormat::WRITE`], in order: what save dialogs, help and errors list.
pub fn writable_extensions() -> Vec<&'static str> {
Self::WRITE.iter().flat_map(|f| f.write_extensions().iter().copied()).collect()
}
}
/// Stored sample representation.
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub enum SampleFormat {
Int8,
Int16,
Int24,
Int32,
Float32,
Float64,
/// A lossy codec (MP3, AAC, Vorbis, ...): no fixed bit depth.
Compressed,
}
/// Broadcast Wave (`bext`) metadata.
#[derive(Debug, Clone, Default, PartialEq, serde::Serialize, serde::Deserialize)]
pub struct BwfInfo {
pub description: String,
pub originator: String,
pub originator_reference: String,
/// `yyyy-mm-dd`.
pub origination_date: String,
/// `hh:mm:ss`.
pub origination_time: String,
/// Sample count since midnight of the first sample (the timestamp Pro Tools spots to).
pub time_reference: u64,
}
/// What [`probe`] learns about a file without decoding all of it.
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
pub struct AudioInfo {
pub format: FileFormat,
pub sample_format: SampleFormat,
pub sample_rate: u32,
pub channels: u16,
pub frames: u64,
pub bwf: Option<BwfInfo>,
}
impl AudioInfo {
/// Duration in seconds.
pub fn duration_secs(&self) -> f64 {
if self.sample_rate == 0 { 0.0 } else { self.frames as f64 / f64::from(self.sample_rate) }
}
}
/// Bit depth for [`encode`].
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub enum BitDepth {
Int16,
Int24,
Int32,
/// Preserve finite samples, including values above full scale; map NaN to 0 and infinities to ±1.
Float32,
}
/// Options for [`encode`].
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
pub struct EncodeOptions {
/// `Wav`, `Aiff` or `Flac`; anything else is [`AudioError::Unsupported`].
pub format: FileFormat,
pub bit_depth: BitDepth,
/// Add TPDF dither when quantizing to an integer depth.
pub dither: bool,
/// Write a BWF `bext` chunk (WAV only; ignored for other formats).
pub bwf: Option<BwfInfo>,
}
impl Default for EncodeOptions {
fn default() -> Self {
Self { format: FileFormat::Wav, bit_depth: BitDepth::Int24, dither: false, bwf: None }
}
}
/// 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));
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> {
let head = |n: usize| b.get(..n);
if wav::is_wav(b) {
return Some(FileFormat::Wav);
}
if aiff::is_aiff(b) {
return Some(FileFormat::Aiff);
}
match head(4)? {
b"fLaC" => return Some(FileFormat::Flac),
b"OggS" => return Some(FileFormat::Ogg),
b"caff" => return Some(FileFormat::Caf),
[0x1A, 0x45, 0xDF, 0xA3] => return Some(FileFormat::Other),
_ => {}
}
if b.get(4..8) == Some(b"ftyp") {
// MP4 / M4A: the sample entry inside `stsd` names the codec.
let found = b.windows(4).position(|w| w == b"stsd");
let codec = found.and_then(|p| b.get(p.checked_add(16)?..p.checked_add(20)?));
return Some(if codec == Some(b"alac") { FileFormat::Alac } else { FileFormat::Aac });
}
if let Some(len) = id3v2_len(b) {
if depth == 0
&& let Some(f) = b.get(len..).and_then(|rest| detect_by_magic(rest, 1))
{
return Some(f);
}
return Some(FileFormat::Mp3);
}
let (b0, b1) = (*b.first()?, *b.get(1)?);
if b0 == 0xFF && b1 & 0xE0 == 0xE0 {
let layer = (b1 >> 1) & 0x3;
if b1 & 0xF0 == 0xF0 && layer == 0 {
return Some(FileFormat::Aac); // ADTS
}
if layer != 0 {
return Some(FileFormat::Mp3);
}
}
None
}
/// Identify a file by its magic bytes, falling back to the extension hint (`"wav"`, `".flac"`).
pub fn detect_format(bytes: &[u8], ext_hint: Option<&str>) -> FileFormat {
detect_by_magic(bytes, 0).unwrap_or_else(|| ext_hint.and_then(FileFormat::from_extension).unwrap_or(FileFormat::Other))
}
/// Read a file's format, sample rate, channel count, length and BWF metadata.
pub fn probe(bytes: &[u8], ext_hint: Option<&str>) -> Result<AudioInfo> {
let format = detect_format(bytes, ext_hint);
match format {
FileFormat::Wav if wav::is_wav(bytes) => match wav::parse(bytes)? {
wav::WavParse::Native(l) => Ok(l.info),
wav::WavParse::Foreign { bwf } => {
let mut info = symph::probe(bytes, format)?;
info.bwf = bwf;
Ok(info)
}
},
FileFormat::Aiff if aiff::is_aiff(bytes) => match aiff::parse(bytes)? {
aiff::AiffParse::Native(l) => Ok(l.info),
aiff::AiffParse::Foreign => symph::probe(bytes, format),
},
_ => symph::probe(bytes, format),
}
}
/// Decode a whole file to planar `f32`.
pub fn decode(bytes: &[u8], ext_hint: Option<&str>) -> Result<(AudioInfo, AudioBuffer)> {
let format = detect_format(bytes, ext_hint);
match format {
FileFormat::Wav if wav::is_wav(bytes) => match wav::parse(bytes)? {
wav::WavParse::Native(l) => {
let buf = wav::decode(bytes, &l)?;
let mut info = l.info;
info.frames = buf.frames() as u64;
Ok((info, buf))
}
wav::WavParse::Foreign { bwf } => {
let (mut info, buf) = symph::decode(bytes, format)?;
info.bwf = bwf;
Ok((info, buf))
}
},
FileFormat::Aiff if aiff::is_aiff(bytes) => match aiff::parse(bytes)? {
aiff::AiffParse::Native(l) => {
let buf = aiff::decode(bytes, &l)?;
let mut info = l.info;
info.frames = buf.frames() as u64;
Ok((info, buf))
}
aiff::AiffParse::Foreign => symph::decode(bytes, format),
},
_ => symph::decode(bytes, format),
}
}
/// Encode `buf` as WAV/BWF, AIFF/AIFF-C or FLAC. Samples are clamped to [-1, 1].
pub fn encode(buf: &AudioBuffer, opts: &EncodeOptions) -> Result<Vec<u8>> {
match opts.format {
FileFormat::Wav => wav::encode(buf, opts.bit_depth, opts.dither, opts.bwf.as_ref(), false, None),
FileFormat::Aiff => aiff::encode(buf, opts.bit_depth, opts.dither),
FileFormat::Flac => flac::encode(buf, opts.bit_depth, opts.dither),
other => Err(AudioError::Unsupported(format!("encoding {other:?} is not supported"))),
}
}
/// Encode a WAV file in the RF64 layout regardless of size (normally used only above 4 GiB).
pub fn encode_wav_rf64(buf: &AudioBuffer, opts: &EncodeOptions) -> Result<Vec<u8>> {
wav::encode(buf, opts.bit_depth, opts.dither, opts.bwf.as_ref(), true, None)
}
/// Like [`encode`], with an explicit WAV speaker mask (`dwChannelMask`, e.g. 0x60F) for layouts
/// a channel count alone cannot identify (7.0 vs 6.1, 5.1.4 vs 7.1.2, Ambisonics = 0). AIFF and
/// FLAC have fixed channel orders and ignore it.
pub fn encode_with_channel_mask(buf: &AudioBuffer, opts: &EncodeOptions, mask: u32) -> Result<Vec<u8>> {
match opts.format {
FileFormat::Wav => wav::encode(buf, opts.bit_depth, opts.dither, opts.bwf.as_ref(), false, Some(mask)),
_ => encode(buf, opts),
}
}
/// The error a stream encoder returns for every call after one of its writes has failed: the
/// sink may already hold a prefix of the failed block, so the encoder's counters can no longer
/// describe the bytes on the sink, and later writes or `finish` must not claim success.
pub(crate) fn failed_write_error() -> AudioError {
AudioError::Encode("the stream encoder already failed on an earlier write; its output is incomplete".into())
}
/// Writes WAV/BWF (RF64 above 4 GiB), AIFF / AIFF-C `fl32` or FLAC block by block, producing
/// the same bytes [`encode_with_channel_mask`] would for the whole buffer.
pub struct FileEncoder<W: Write + Seek>(Writer<W>);
enum Writer<W> {
Wav(wav::Writer<W>),
Aiff(aiff::Writer<W>),
Flac(flac::Writer<W>),
}
impl<W: Write + Seek> FileEncoder<W> {
/// Starts a file at the current position of `out`. `mask` overrides the WAV speaker mask
/// derived from the channel count.
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,
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"))),
}))
}
/// Fails, without writing anything, when `frames` more frames would not fit the format (an
/// AIFF holds 4 GiB, a FLAC 2^36 frames).
pub fn check_room(&self, frames: u64) -> Result<()> {
match &self.0 {
Writer::Wav(w) => w.check_room(frames),
Writer::Aiff(w) => w.check_room(frames),
Writer::Flac(w) => w.check_room(frames),
}
.map(drop)
}
/// Appends the first `frames` frames of `block`, one `Vec` per channel (missing samples are
/// silence), unless they don't fit (see [`FileEncoder::check_room`]). Once a write has
/// failed, the encoder is unusable: the sink may hold a prefix of the failed block, so
/// every later `write` and `finish` fails instead of completing an inconsistent file.
pub fn write(&mut self, block: &[Vec<f32>], frames: usize) -> Result<()> {
match &mut self.0 {
Writer::Wav(w) => w.write(block, frames),
Writer::Aiff(w) => w.write(block, frames),
Writer::Flac(w) => w.write(block, frames),
}
}
/// Completes the headers and hands back `out`, positioned at the end of the file.
pub fn finish(self) -> Result<W> {
match self.0 {
Writer::Wav(w) => w.finish(),
Writer::Aiff(w) => w.finish(),
Writer::Flac(w) => w.finish(),
}
}
}
/// The WAVE_FORMAT_EXTENSIBLE speaker mask of a WAV file, if it has one.
pub fn wav_channel_mask(bytes: &[u8]) -> Option<u32> {
wav::read_channel_mask(bytes)
}
/// The header of a 32-bit float WAV holding `frames` frames (little-endian samples follow it). Its
/// length doesn't depend on `frames`, so a recorder can rewrite it in place as the file grows; it
/// switches to RF64 above 4 GiB.
pub fn wav_float_header(channels: usize, sample_rate: u32, frames: u64) -> Result<Vec<u8>> {
if channels == 0 || sample_rate == 0 {
return Err(AudioError::Encode("a WAV needs channels and a sample rate".into()));
}
wav::header(channels, sample_rate, (32, true), None, None, frames, false)
}
-155
View File
@@ -1,155 +0,0 @@
//! Size checks on MP4 atoms before symphonia reads them: it allocates whatever an atom, a sample
//! table or a sample claims, and a failed allocation aborts the process.
use crate::pcm::{be_u32, be_u64};
use crate::{AudioError, Result};
/// The most `stts` entries a sound track may have. Symphonia finds each packet's timestamp by
/// scanning the table from its start, so a table of one-sample runs makes decoding quadratic in
/// the packet count. Audio encoders write one run per packet length (a handful of entries);
/// this bound keeps the worst case at a few hundred million steps.
const MAX_AUDIO_STTS_ENTRIES: u32 = 1 << 14;
#[derive(Clone, Copy)]
enum Level {
Top,
Moov,
Trak,
Mdia,
Minf,
/// `audio`: the sample entry is an audio format, so symphonia will decode its packets (whatever the
/// handler says: it builds codec parameters from `stsd` alone).
Stbl {
audio: bool,
},
Udta,
Meta,
Ilst,
Tag,
}
/// Refuse the MP4 file whose top-level atoms start at `at` if an atom symphonia reads whole, a
/// sample table or a sample claims more bytes than the file holds.
pub(crate) fn check(b: &[u8], at: usize) -> Result<()> {
atoms(b, at, b.len(), Level::Top).map(|_| ())
}
fn malformed(what: &str) -> AudioError {
AudioError::Malformed(format!("MP4 {what} is larger than the file"))
}
/// Walk the atoms in `pos..end` that symphonia descends into, as it reads them. False where
/// symphonia stops reading: at a header it refuses, or after an atom that runs past `end`.
fn atoms(b: &[u8], mut pos: usize, end: usize, level: Level) -> Result<bool> {
// A header that starts inside the container is read whole, even past its end.
while pos < end {
let (Some(size), Some(kind)) = (be_u32(b, pos), b.get(pos + 4..pos + 8)) else { return Ok(false) };
let head = if size == 1 { 16 } else { 8 };
// Size 0: no data, and the next atom starts at the container's end.
let len = match size {
0 => head,
1 => be_u64(b, pos + 8).unwrap_or(0),
n => u64::from(n),
};
if len < head {
return Ok(false);
}
let body = pos + head as usize;
let data_end = usize::try_from(len).ok().and_then(|n| pos.checked_add(n));
let next = if size == 0 { Some(end) } else { data_end };
let inner = data_end.map_or(b.len(), |n| n.min(b.len()));
let more = match (level, kind) {
(Level::Top, b"moov") => atoms(b, body, inner, Level::Moov)?,
(Level::Top | Level::Udta, b"meta") => atoms(b, body + 4, inner, Level::Meta)?,
(Level::Moov, b"trak") => atoms(b, body, inner, Level::Trak)?,
(Level::Moov, b"udta") => atoms(b, body, inner, Level::Udta)?,
(Level::Trak, b"mdia") => atoms(b, body, inner, Level::Mdia)?,
(Level::Mdia, b"minf") => atoms(b, body, inner, Level::Minf)?,
(Level::Minf, b"stbl") => atoms(b, body, inner, Level::Stbl { audio: entry_is_audio(b, body, inner) })?,
(Level::Meta, b"ilst") => atoms(b, body, inner, Level::Ilst)?,
(Level::Ilst, _) => atoms(b, body, inner, Level::Tag)?,
(Level::Stbl { audio }, b"stts") => {
let fits = table(b, body + 4, 8, inner)?;
if audio && be_u32(b, body + 4).is_some_and(|n| n > MAX_AUDIO_STTS_ENTRIES) {
return Err(AudioError::Malformed("MP4 audio timing table has more runs than packets could need".into()));
}
fits
}
(Level::Stbl { .. }, b"co64") => table(b, body + 4, 8, inner)?,
(Level::Stbl { .. }, b"stsc") => table(b, body + 4, 12, inner)?,
(Level::Stbl { .. }, b"stco") => table(b, body + 4, 4, inner)?,
(Level::Stbl { .. }, b"stsz") => sample_sizes(b, body, inner)?,
(Level::Mdia, b"hdlr") | (Level::Tag, b"data" | b"name" | b"mean") if len > (b.len() - pos) as u64 => {
return Err(malformed(&format!("`{}` atom", String::from_utf8_lossy(kind))));
}
_ => true,
};
match next {
Some(n) if more && n <= end => pos = n,
_ => return Ok(false),
}
}
Ok(true)
}
/// The sample entry formats symphonia reads as audio (the list `stsd` is matched against in
/// symphonia-format-isomp4 0.6); every other format, video and subtitles included, has no audio packets.
const AUDIO_FORMATS: [&[u8; 4]; 18] = [
b"mp4a", b"alac", b"ac-3", b"ec-3", b"fLaC", b"Opus", b".mp3", b"lpcm", b"wave", b"alaw", b"ulaw", b"raw ", b"sowt", b"twos", b"in24", b"in32",
b"fl32", b"fl64",
];
/// Whether the `stbl` atom whose children lie in `pos..end` describes audio: its `stsd` may come before
/// or after the timing tables, so the children are walked once more for it. Symphonia keeps the last
/// `stsd` it reads, so the last one decides.
fn entry_is_audio(b: &[u8], mut pos: usize, end: usize) -> bool {
let mut audio = false;
while pos < end {
let (Some(size), Some(kind)) = (be_u32(b, pos), b.get(pos + 4..pos + 8)) else { return audio };
let head = if size == 1 { 16 } else { 8 };
if kind == b"stsd" {
// Version and flags, the entry count, then the first entry's size and format.
audio = b.get(pos + head + 12..pos + head + 16).is_some_and(|f| AUDIO_FORMATS.iter().any(|a| a.as_slice() == f));
}
let len = match size {
0 => return audio,
1 => be_u64(b, pos + 8).unwrap_or(0),
n => u64::from(n),
};
match usize::try_from(len).ok().filter(|&n| n >= head).and_then(|n| pos.checked_add(n)) {
Some(next) => pos = next,
None => return audio,
}
}
audio
}
/// A sample table's entries, counted at `count`, must fit in the file: symphonia reserves room for
/// all of them first. False if they run past the table's atom (`end`), where symphonia stops.
fn table(b: &[u8], count: usize, entry: u64, end: usize) -> Result<bool> {
let Some(n) = be_u32(b, count) else { return Ok(false) };
let (first, bytes) = (count + 4, u64::from(n) * entry);
if bytes > b.len().saturating_sub(first) as u64 {
return Err(malformed("sample table"));
}
Ok(first as u64 + bytes <= end as u64)
}
/// Every sample's size must fit in the file: symphonia reads each one into a buffer of that size.
/// Sizes past the atom are never used, as symphonia stops before reading a packet.
fn sample_sizes(b: &[u8], body: usize, end: usize) -> Result<bool> {
let (Some(size), Some(count)) = (be_u32(b, body + 4), be_u32(b, body + 8)) else { return Ok(false) };
let file = b.len() as u64;
if u64::from(size) > file {
return Err(malformed("sample"));
}
if size != 0 {
return Ok(true);
}
let fits = table(b, body + 8, 4, end)?;
let (sizes, _) = b.get(body + 12..end).unwrap_or_default().as_chunks::<4>();
if sizes.iter().take(count as usize).any(|s| u64::from(u32::from_be_bytes(*s)) > file) {
return Err(malformed("sample"));
}
Ok(fits)
}
-279
View File
@@ -1,279 +0,0 @@
//! Byte-level PCM helpers shared by the WAV, AIFF and FLAC code: bounded readers, sample
//! decoding to `f32`, and quantization (with optional TPDF dither) for encoding.
use crate::{AudioError, BitDepth, Result};
use std::io::Write;
/// Maximum number of decoded `f32` samples (all channels) we will produce: 4 GiB of output.
pub(crate) const MAX_DECODE_SAMPLES: u64 = (4u64 << 30) / 4;
/// Little-endian `u16` at `pos`.
pub(crate) fn le_u16(b: &[u8], pos: usize) -> Option<u16> {
let s = b.get(pos..pos.checked_add(2)?)?;
Some(u16::from_le_bytes([*s.first()?, *s.get(1)?]))
}
/// Little-endian `u32` at `pos`.
pub(crate) fn le_u32(b: &[u8], pos: usize) -> Option<u32> {
let s: [u8; 4] = b.get(pos..pos.checked_add(4)?)?.try_into().ok()?;
Some(u32::from_le_bytes(s))
}
/// Little-endian `u64` at `pos`.
pub(crate) fn le_u64(b: &[u8], pos: usize) -> Option<u64> {
let s: [u8; 8] = b.get(pos..pos.checked_add(8)?)?.try_into().ok()?;
Some(u64::from_le_bytes(s))
}
/// Big-endian `u16` at `pos`.
pub(crate) fn be_u16(b: &[u8], pos: usize) -> Option<u16> {
let s: [u8; 2] = b.get(pos..pos.checked_add(2)?)?.try_into().ok()?;
Some(u16::from_be_bytes(s))
}
/// Big-endian `u32` at `pos`.
pub(crate) fn be_u32(b: &[u8], pos: usize) -> Option<u32> {
let s: [u8; 4] = b.get(pos..pos.checked_add(4)?)?.try_into().ok()?;
Some(u32::from_be_bytes(s))
}
/// Big-endian `u64` at `pos`.
pub(crate) fn be_u64(b: &[u8], pos: usize) -> Option<u64> {
let s: [u8; 8] = b.get(pos..pos.checked_add(8)?)?.try_into().ok()?;
Some(u64::from_be_bytes(s))
}
/// Four-byte tag at `pos`.
pub(crate) fn tag(b: &[u8], pos: usize) -> Option<[u8; 4]> {
b.get(pos..pos.checked_add(4)?)?.try_into().ok()
}
/// How the samples in a PCM byte stream are encoded.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum PcmKind {
/// Signed (or, for 1-byte WAV, unsigned) integers occupying `bytes` bytes each, left-justified.
Int { bytes: usize, big_endian: bool, unsigned: bool },
/// IEEE float, 4 or 8 bytes.
Float { bytes: usize, big_endian: bool },
}
impl PcmKind {
pub(crate) fn bytes(self) -> usize {
match self {
PcmKind::Int { bytes, .. } | PcmKind::Float { bytes, .. } => bytes,
}
}
}
/// Decode one sample from exactly `kind.bytes()` bytes. Returns 0 for a short slice.
fn decode_sample(s: &[u8], kind: PcmKind) -> f32 {
match kind {
PcmKind::Int { bytes, big_endian, unsigned } => {
if bytes == 0 || bytes > 4 || s.len() < bytes {
return 0.0;
}
let mut acc: u32 = 0;
for i in 0..bytes {
let idx = if big_endian { i } else { bytes - 1 - i };
let mut byte = s.get(idx).copied().unwrap_or(0);
if unsigned && i == 0 {
byte ^= 0x80;
}
acc = (acc << 8) | u32::from(byte);
}
let shifted = acc << (32 - 8 * bytes as u32);
(f64::from(shifted as i32) / 2_147_483_648.0) as f32
}
PcmKind::Float { bytes, big_endian } => {
let v = match bytes {
4 => {
let Some(a) = s.get(..4).and_then(|x| <[u8; 4]>::try_from(x).ok()) else { return 0.0 };
if big_endian { f32::from_be_bytes(a) } else { f32::from_le_bytes(a) }
}
8 => {
let Some(a) = s.get(..8).and_then(|x| <[u8; 8]>::try_from(x).ok()) else { return 0.0 };
(if big_endian { f64::from_be_bytes(a) } else { f64::from_le_bytes(a) }) as f32
}
_ => 0.0,
};
if v.is_finite() { v } else { 0.0 }
}
}
}
/// Deinterleave `frames` frames of `channels`-channel PCM from `data` into planar `f32`.
pub(crate) fn deinterleave(data: &[u8], kind: PcmKind, channels: usize, frames: u64) -> Result<Vec<Vec<f32>>> {
let bytes = kind.bytes();
let block = bytes.checked_mul(channels).filter(|&b| b > 0).ok_or_else(|| AudioError::Malformed("zero-sized sample frame".into()))?;
let available = (data.len() / block) as u64;
let frames = frames.min(available);
let total = frames.checked_mul(channels as u64).ok_or_else(|| AudioError::TooLarge("sample count overflows".into()))?;
if total > MAX_DECODE_SAMPLES {
return Err(AudioError::TooLarge(format!("{total} samples exceeds the {MAX_DECODE_SAMPLES}-sample decode limit")));
}
let frames = usize::try_from(frames).map_err(|_| AudioError::TooLarge("frame count exceeds address space".into()))?;
let mut out: Vec<Vec<f32>> = (0..channels).map(|_| Vec::with_capacity(frames)).collect();
for frame in data.chunks_exact(block).take(frames) {
for (ch, s) in out.iter_mut().zip(frame.chunks_exact(bytes)) {
ch.push(decode_sample(s, kind));
}
}
Ok(out)
}
/// Converts `f32` samples to integers of a given bit depth, clamping to [-1, 1] and optionally
/// adding triangular (TPDF) dither of ±1 LSB.
pub(crate) struct Quantizer {
bits: u32,
dither: bool,
rng: u64,
}
impl Quantizer {
/// A quantizer for one channel; each channel gets an independent dither sequence.
pub(crate) fn new(bits: u32, dither: bool, channel: usize) -> Self {
let rng = 0x9E37_79B9_7F4A_7C15u64 ^ (channel as u64).wrapping_add(1).wrapping_mul(0xD1B5_4A32_D192_ED03);
Self { bits: bits.clamp(2, 32), dither, rng: if rng == 0 { 1 } else { rng } }
}
/// One quantizer per channel.
pub(crate) fn per_channel(bits: u32, dither: bool, channels: usize) -> Vec<Self> {
(0..channels).map(|c| Self::new(bits, dither, c)).collect()
}
/// Uniform in [0, 1) from a xorshift64* generator (deterministic, no OS entropy needed).
fn uniform(&mut self) -> f64 {
let mut x = self.rng;
x ^= x >> 12;
x ^= x << 25;
x ^= x >> 27;
self.rng = x;
(x.wrapping_mul(0x2545_F491_4F6C_DD1D) >> 11) as f64 / (1u64 << 53) as f64
}
pub(crate) fn quantize(&mut self, x: f32) -> i32 {
let x = if x.is_nan() { 0.0 } else { x.clamp(-1.0, 1.0) };
let scale = (1u64 << (self.bits - 1)) as f64;
let mut v = f64::from(x) * scale;
if self.dither {
v += self.uniform() - self.uniform();
}
let max = scale - 1.0;
round_half_away(v.clamp(-scale, max)) as i32
}
}
#[inline]
fn round_half_away(v: f64) -> f64 {
let t = v as i64;
let f = v - t as f64;
(t + i64::from(f >= 0.5) - i64::from(f <= -0.5)) as f64
}
/// Bits per sample at `depth`, and whether they are float.
pub(crate) fn sample_format(depth: BitDepth) -> (u16, bool) {
match depth {
BitDepth::Int16 => (16, false),
BitDepth::Int24 => (24, false),
BitDepth::Int32 => (32, false),
BitDepth::Float32 => (32, true),
}
}
/// Interleaves `frames` frames of planar audio (missing samples are silence) as `bits`-bit
/// samples and writes them to `out`, a few thousand frames at a time through `scratch`.
pub(crate) fn write_interleaved(
out: &mut impl Write,
scratch: &mut Vec<u8>,
block: &[Vec<f32>],
frames: usize,
quantizers: &mut [Quantizer],
(bits, is_float): (u16, bool),
big_endian: bool,
) -> std::io::Result<()> {
let size = usize::from(bits / 8);
let mut start = 0;
while start < frames {
let end = frames.min(start + 4096);
scratch.clear();
for f in start..end {
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(&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()),
}
}
}
out.write_all(scratch)?;
start = end;
}
Ok(())
}
/// 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.
pub(crate) fn validate_buffer(buf: &crate::AudioBuffer) -> Result<()> {
validate_layout(buf.channels.len(), buf.sample_rate)
}
/// Check that a stream is encodable: at least one channel and a non-zero sample rate.
pub(crate) fn validate_layout(channels: usize, sample_rate: u32) -> Result<()> {
if channels == 0 {
return Err(AudioError::Encode("buffer has no channels".into()));
}
if channels > usize::from(u16::MAX) {
return Err(AudioError::Encode("too many channels".into()));
}
if sample_rate == 0 {
return Err(AudioError::Encode("sample rate is 0".into()));
}
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn round_half_away_matches_f64_round_on_the_quantizer_range() {
let edges = [
0.0,
-0.0,
0.5,
-0.5,
1.5,
-1.5,
2.5,
0.499_999_999_999_999_94,
-0.499_999_999_999_999_94,
8_388_607.5,
-8_388_608.0,
2_147_483_647.0,
-2_147_483_648.0,
];
let mut x = 0x2545_f491_4f6c_dd1du64;
let spread = (0..10_000).map(|_| {
x ^= x << 13;
x ^= x >> 7;
x ^= x << 17;
(x >> 11) as f64 / (1u64 << 53) as f64 * 4_294_967_295.0 - 2_147_483_648.0
});
for v in edges.into_iter().chain(spread) {
assert!(round_half_away(v) == v.round(), "{v}");
assert_eq!(round_half_away(v) as i32, v.round() as i32, "{v}");
}
}
}
-280
View File
@@ -1,280 +0,0 @@
//! Multi-resolution overview for waveform drawing.
//!
//! Level 0 sums up each block of [`BLOCK`] samples (min, max and sum of squares); each following
//! level merges [`FANOUT`] blocks of the level below, until a level has a single block. Range
//! queries combine the coarsest whole blocks that fit inside the range and read the partial blocks
//! at its edges from the samples, so a query is exact and costs O(log n) regardless of zoom.
/// Samples per level-0 block.
pub const BLOCK: usize = 32;
/// Blocks of level `n` merged into one block of level `n + 1`.
pub const FANOUT: usize = 4;
/// Upper bound on the number of spans [`Peaks::spans`] will produce.
pub const MAX_COLUMNS: usize = 1 << 20;
/// Multi-resolution overview of one channel for waveform drawing.
#[derive(Debug, Clone, Default, PartialEq, serde::Serialize, serde::Deserialize)]
pub struct Peaks {
len: usize,
levels: Vec<Vec<Block>>,
}
/// What a run of samples sums up to; NaN samples count for nothing.
#[derive(Debug, Clone, Copy, PartialEq, serde::Serialize, serde::Deserialize)]
struct Block {
min: f32,
max: f32,
squares: f64,
}
impl Block {
const EMPTY: Block = Block { min: f32::INFINITY, max: f32::NEG_INFINITY, squares: 0.0 };
fn point(s: f32) -> Block {
Block { min: s, max: s, squares: if s.is_finite() { f64::from(s) * f64::from(s) } else { 0.0 } }
}
fn of(samples: &[f32]) -> Block {
samples.iter().fold(Block::EMPTY, |b, &s| b.merge(Block::point(s)))
}
fn merge(self, o: Block) -> Block {
Block { min: self.min.min(o.min), max: self.max.max(o.max), squares: self.squares + o.squares }
}
/// (min, max), or (0, 0) when nothing was summed up.
fn range(self) -> (f32, f32) {
if self.min > self.max { (0.0, 0.0) } else { (self.min, self.max) }
}
}
impl Peaks {
/// Build the overview of `samples` (one channel). NaN samples are ignored.
pub fn build(samples: &[f32]) -> Peaks {
if samples.is_empty() {
return Peaks::default();
}
let mut levels: Vec<Vec<Block>> = vec![samples.chunks(BLOCK).map(Block::of).collect()];
while let Some(last) = levels.last().filter(|l| l.len() > 1) {
let next = last.chunks(FANOUT).map(|c| c.iter().fold(Block::EMPTY, |a, &b| a.merge(b))).collect();
levels.push(next);
}
Peaks { len: samples.len(), levels }
}
/// Number of samples the overview was built from.
pub fn len_samples(&self) -> usize {
self.len
}
/// Number of resolution levels (0 for an empty overview).
pub fn num_levels(&self) -> usize {
self.levels.len()
}
/// (min, max) over the sample range `[start, end)`, clamped to the data. `samples` must be the
/// data the overview was built from. Returns `(0.0, 0.0)` for an empty range.
pub fn minmax(&self, samples: &[f32], start: usize, end: usize) -> (f32, f32) {
self.sum(samples, start, end).range()
}
fn sum(&self, samples: &[f32], start: usize, end: usize) -> Block {
let end = end.min(self.len).min(samples.len());
let (first, last) = (start.div_ceil(BLOCK), end / BLOCK);
let raw = |a: usize, b: usize| Block::of(samples.get(a..b).unwrap_or_default());
if first >= last {
return raw(start, end);
}
raw(start, first * BLOCK).merge(raw(last * BLOCK, end)).merge(self.blocks(first, last))
}
/// The level-0 blocks `[lo, hi)`, summed from the coarsest blocks that fit.
fn blocks(&self, mut lo: usize, mut hi: usize) -> Block {
let mut acc = Block::EMPTY;
for (lvl, level) in self.levels.iter().enumerate() {
let mut add = |i: usize| acc = acc.merge(level.get(i).copied().unwrap_or(Block::EMPTY));
if lvl + 1 == self.levels.len() || hi.saturating_sub(lo) < 2 * FANOUT {
(lo..hi).for_each(add);
break;
}
while !lo.is_multiple_of(FANOUT) {
add(lo);
lo += 1;
}
while !hi.is_multiple_of(FANOUT) {
hi -= 1;
add(hi);
}
lo /= FANOUT;
hi /= FANOUT;
}
acc
}
/// The spans of sample positions `[start + c * step, start + (c + 1) * step + overlap]` that
/// reach the data, clamped to it.
fn windows(&self, samples: &[f32], start: f64, step: f64, overlap: f64, cols: usize) -> impl Iterator<Item = Option<(f64, f64)>> {
let n = self.len.min(samples.len());
let valid = n > 0 && start.is_finite() && step.is_finite() && step > 0.0 && overlap.is_finite() && overlap >= 0.0;
let last = n.saturating_sub(1) as f64;
(0..cols.min(MAX_COLUMNS)).map(move |c| {
let (a, b) = (start + step * c as f64, start + step * (c + 1) as f64 + overlap);
(valid && b >= 0.0 && a <= last).then(|| (a.max(0.0), b.min(last)))
})
}
/// (min, max) of the line through `samples` over each of `cols` spans of sample positions
/// `[start + c * step, start + (c + 1) * step + overlap]`; fractional positions interpolate
/// between samples. Neighbouring spans share at least their boundary, so a waveform drawn from
/// them stays connected at any zoom. `None` for spans outside the data.
pub fn spans(&self, samples: &[f32], start: f64, step: f64, overlap: f64, cols: usize) -> Vec<Option<(f32, f32)>> {
let at = |t: f64| {
let i = t.floor();
let a = samples.get(i as usize).copied().unwrap_or(0.0);
let b = samples.get(i as usize + 1).copied().unwrap_or(a);
Block::point(a + (b - a) * (t - i) as f32)
};
self.windows(samples, start, step, overlap, cols)
.map(|w| w.map(|(a, b)| self.sum(samples, a.ceil() as usize, b.floor() as usize + 1).merge(at(a)).merge(at(b)).range()))
.collect()
}
/// The RMS of `samples` over each span [`Peaks::spans`] gives, including the samples just
/// outside a span's ends.
pub fn rms_spans(&self, samples: &[f32], start: f64, step: f64, overlap: f64, cols: usize) -> Vec<Option<f32>> {
self.windows(samples, start, step, overlap, cols)
.map(|w| {
w.map(|(a, b)| {
let (s, e) = (a.floor() as usize, (b.ceil() as usize).saturating_add(1).min(self.len).min(samples.len()));
(self.sum(samples, s, e).squares / e.saturating_sub(s).max(1) as f64).sqrt() as f32
})
})
.collect()
}
}
#[cfg(test)]
mod tests {
use super::*;
fn brute(samples: &[f32], start: usize, end: usize) -> (f32, f32) {
let s = &samples[start..end];
(s.iter().copied().fold(f32::INFINITY, f32::min), s.iter().copied().fold(f32::NEG_INFINITY, f32::max))
}
fn signal(n: usize) -> Vec<f32> {
(0..n).map(|i| ((i as f32 * 0.037).sin() * (i as f32 * 0.0011).cos()) + if i % 997 == 0 { 0.5 } else { 0.0 }).collect()
}
#[test]
fn every_range_is_exact() {
let x = signal(100_003);
let p = Peaks::build(&x);
for &(a, b) in &[(0, 32), (0, 100_003), (32, 64_000), (3200, 3232), (96, 99_968), (64, 100_003), (5, 6), (31, 33), (1000, 1031)] {
assert_eq!(p.minmax(&x, a, b), brute(&x, a, b), "range {a}..{b}");
}
let mut seed = 12345u64;
for _ in 0..2000 {
seed = seed.wrapping_mul(6364136223846793005).wrapping_add(1442695040888963407);
let a = (seed >> 33) as usize % 100_003;
seed = seed.wrapping_mul(6364136223846793005).wrapping_add(1442695040888963407);
let b = a + 1 + (seed >> 33) as usize % (100_003 - a);
assert_eq!(p.minmax(&x, a, b), brute(&x, a, b), "range {a}..{b}");
}
}
#[test]
fn degenerate_inputs() {
let p = Peaks::build(&[]);
assert_eq!(p.len_samples(), 0);
assert_eq!(p.minmax(&[], 0, 10), (0.0, 0.0));
assert_eq!(p.spans(&[], 0.0, 1.0, 0.0, 4), vec![None; 4]);
let x = [0.5, -0.25];
let p = Peaks::build(&x);
assert_eq!(p.minmax(&x, 0, 2), (-0.25, 0.5));
assert_eq!(p.minmax(&x, 5, 3), (0.0, 0.0));
assert_eq!(p.minmax(&x, 0, usize::MAX), (-0.25, 0.5));
assert_eq!(p.minmax(&x[..1], 0, 2), (0.5, 0.5));
for (start, step, overlap) in [
(f64::NAN, 1.0, 0.0),
(0.0, 0.0, 0.0),
(0.0, -1.0, 0.0),
(0.0, f64::INFINITY, 0.0),
(f64::NEG_INFINITY, 1.0, 0.0),
(0.0, 1.0, -0.5),
(0.0, 1.0, f64::NAN),
] {
assert_eq!(p.spans(&x, start, step, overlap, 3), vec![None; 3]);
}
assert_eq!(p.spans(&x, 0.0, 1.0, 0.0, 0).len(), 0);
assert_eq!(p.spans(&x, 0.0, 1.0, 0.0, usize::MAX).len(), MAX_COLUMNS);
let x = [f32::NAN, 1.0];
let p = Peaks::build(&x);
assert_eq!(p.minmax(&x, 0, 2), (1.0, 1.0));
assert_eq!(p.spans(&x, 0.0, 1.0, 0.0, 1), vec![Some((1.0, 1.0))]);
assert!(p.rms_spans(&x, 0.0, 1.0, 0.0, 1)[0].is_some_and(f32::is_finite));
}
#[test]
fn finite_large_samples_keep_finite_rms() {
let samples = vec![1e20_f32; BLOCK];
let peaks = Peaks::build(&samples);
assert_eq!(peaks.rms_spans(&samples, 0.0, BLOCK as f64, 0.0, 1), vec![Some(1e20)]);
}
#[test]
fn rms_spans_match_the_samples() {
let x = signal(20_000);
let p = Peaks::build(&x);
let rms = |s: usize, e: usize| (x[s..e].iter().map(|&v| f64::from(v) * f64::from(v)).sum::<f64>() / (e - s) as f64).sqrt() as f32;
for (step, overlap) in [(1.5, 0.0), (37.0, 37.0), (1000.0, 0.0), (4096.0, 512.0)] {
let cols = ((19_999.0 - overlap) / step) as usize;
for (c, got) in p.rms_spans(&x, 0.0, step, overlap, cols).into_iter().enumerate() {
let (a, b) = (c as f64 * step, ((c + 1) as f64 * step + overlap).min(19_999.0));
let (got, want) = (got.unwrap(), rms(a.floor() as usize, (b.ceil() as usize + 1).min(20_000)));
assert!((got - want).abs() <= want * 1e-4 + 1e-6, "span {c} at step {step}: {got} vs {want}");
}
}
let sine: Vec<f32> = (0..48_000).map(|i| (i as f32 * 0.01).sin()).collect();
let p = Peaks::build(&sine);
let r = p.rms_spans(&sine, 0.0, 47_000.0, 0.0, 1)[0].unwrap();
assert!((r - std::f32::consts::FRAC_1_SQRT_2).abs() < 1e-3, "{r}");
assert_eq!(p.rms_spans(&sine, f64::NAN, 1.0, 0.0, 2), vec![None, None]);
assert_eq!(p.rms_spans(&sine, 50_000.0, 1.0, 0.0, 1), vec![None]);
}
#[test]
fn spans_cover_their_samples_and_touch_their_neighbours() {
let x = signal(10_000);
let p = Peaks::build(&x);
for step in [0.3, 1.0, 7.5, 100.0, 333.3] {
let cols = (9_999.0 / step) as usize;
let spans = p.spans(&x, 0.0, step, 0.0, cols);
for (c, s) in spans.iter().enumerate() {
let (lo, hi) = s.unwrap_or_else(|| panic!("span {c} at step {step}"));
let (a, b) = ((c as f64 * step).ceil() as usize, ((c + 1) as f64 * step).floor() as usize);
if a <= b {
let truth = brute(&x, a, b + 1);
assert!(lo <= truth.0 && hi >= truth.1, "span {c} at step {step}");
}
if let Some(Some((nlo, nhi))) = spans.get(c + 1) {
assert!(*nlo <= hi && *nhi >= lo, "spans {c} and {} at step {step} don't touch", c + 1);
}
}
let all = spans.iter().flatten().fold((f32::INFINITY, f32::NEG_INFINITY), |a, s| (a.0.min(s.0), a.1.max(s.1)));
assert_eq!(all, brute(&x, 0, ((cols as f64 * step).floor() as usize + 1).min(10_000)), "step {step}");
}
}
#[test]
fn spans_follow_the_line_between_samples() {
let x = [0.0, 1.0, -1.0, 0.5];
let p = Peaks::build(&x);
assert_eq!(p.spans(&x, 0.0, 0.25, 0.0, 4), vec![Some((0.0, 0.25)), Some((0.25, 0.5)), Some((0.5, 0.75)), Some((0.75, 1.0))]);
assert_eq!(p.spans(&x, 0.5, 1.0, 0.0, 1), vec![Some((0.0, 1.0))]);
let spans = vec![None, Some((0.0, 0.0)), Some((0.0, 1.0)), Some((-1.0, 1.0)), Some((-1.0, 0.5)), Some((0.5, 0.5)), None];
assert_eq!(p.spans(&x, -2.0, 1.0, 0.0, 7), spans);
assert_eq!(p.spans(&x, 0.0, 1.0, 1.0, 3), vec![Some((-1.0, 1.0)), Some((-1.0, 1.0)), Some((-1.0, 0.5))]);
assert_eq!(p.spans(&x, 0.0, 1.0, 0.5, 3), vec![Some((0.0, 1.0)), Some((-1.0, 1.0)), Some((-1.0, 0.5))]);
}
}
-765
View File
@@ -1,765 +0,0 @@
//! Decode path for everything that isn't native PCM WAV/AIFF, backed by `symphonia`.
use std::io::{Cursor, Seek, SeekFrom};
use std::panic::{AssertUnwindSafe, catch_unwind};
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,
CODEC_ID_PCM_F64BE, CODEC_ID_PCM_F64LE, CODEC_ID_PCM_MULAW, CODEC_ID_VORBIS,
};
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, 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};
use crate::{AudioBuffer, AudioError, AudioInfo, FileFormat, Result, SampleFormat};
/// The most frames a packet may declare: decoders allocate a packet's frames up front, and ALAC
/// takes its frame length from the file (encoders use 4096).
const MAX_PACKET_FRAMES: u64 = 1 << 20;
/// The most samples (frames × channels) a packet may declare: the PCM decoder allocates that
/// many for its output up front, so a CAF with thousands of channels and a long packet could
/// otherwise claim gigabytes before a sample is decoded (64 MiB of f32).
const MAX_PACKET_SAMPLES: u64 = 1 << 24;
/// 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 {
match e {
SymError::Unsupported(m) => AudioError::Unsupported(m.to_string()),
SymError::LimitError(m) => AudioError::TooLarge(m.to_string()),
other => AudioError::Malformed(other.to_string()),
}
}
/// Run `f`, turning a panic inside the third-party decoder into an error.
fn guarded<T>(f: impl FnOnce() -> Result<T>) -> Result<T> {
catch_unwind(AssertUnwindSafe(f)).unwrap_or_else(|_| Err(AudioError::Malformed("decoder failed on malformed input".into())))
}
/// What [`open`] found: a reader, or a stream that holds no audio frames and has no reader.
enum Opened {
Reader(Box<dyn FormatReader>),
Empty(AudioInfo),
}
/// 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 {
match b.get(at..).and_then(crate::id3v2_len) {
Some(n) if n > 0 => at = at.saturating_add(n),
_ => return at,
}
}
}
/// 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,
}
/// 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"];
/// 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()) {
if matches!(*marker, b"RIFF" | b"RF64" | b"BW64") && bytes.get(pos + 8..pos + 12) != Some(&b"WAVE"[..]) {
continue;
}
return Some((pos, *marker));
}
}
None
}
/// 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)),
}
}
/// 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;
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")));
}
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))
}
fn format_from_codec(codec: AudioCodecId, detected: FileFormat) -> FileFormat {
if detected != FileFormat::Other {
return detected;
}
match codec {
c if c == CODEC_ID_FLAC => FileFormat::Flac,
c if c == CODEC_ID_MP3 || c == CODEC_ID_MP2 || c == CODEC_ID_MP1 => FileFormat::Mp3,
c if c == CODEC_ID_AAC => FileFormat::Aac,
c if c == CODEC_ID_ALAC => FileFormat::Alac,
c if c == CODEC_ID_VORBIS => FileFormat::Ogg,
_ => FileFormat::Other,
}
}
/// The stored sample format, using the codec and its declared bit depth. Lossy and unusual
/// codecs decode to `Compressed`.
fn sample_format_of(p: &AudioCodecParameters) -> SampleFormat {
let c = p.codec;
if c == CODEC_ID_PCM_F32LE || c == CODEC_ID_PCM_F32BE {
return SampleFormat::Float32;
}
if c == CODEC_ID_PCM_F64LE || c == CODEC_ID_PCM_F64BE {
return SampleFormat::Float64;
}
let lossy = [
CODEC_ID_MP1,
CODEC_ID_MP2,
CODEC_ID_MP3,
CODEC_ID_AAC,
CODEC_ID_VORBIS,
CODEC_ID_OPUS,
CODEC_ID_ADPCM_MS,
CODEC_ID_ADPCM_IMA_WAV,
CODEC_ID_ADPCM_IMA_QT,
CODEC_ID_ADPCM_G722,
CODEC_ID_ADPCM_G726,
CODEC_ID_ADPCM_G726LE,
CODEC_ID_PCM_ALAW,
CODEC_ID_PCM_MULAW,
];
let lossless = !lossy.contains(&c);
match (lossless, p.bits_per_sample) {
(true, Some(1..=8)) => SampleFormat::Int8,
(true, Some(9..=16)) => SampleFormat::Int16,
(true, Some(17..=24)) => SampleFormat::Int24,
(true, Some(25..=32)) => SampleFormat::Int32,
_ => SampleFormat::Compressed,
}
}
/// 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 = reader
.tracks()
.iter()
.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()))?;
Ok((
track.id,
AudioInfo {
format: format_from_codec(p.codec, detected),
sample_format: sample_format_of(p),
sample_rate,
channels,
frames: track.num_frames.unwrap_or(0),
bwf: None,
},
))
}
/// A demux error ends the decode; it is only an error when nothing was decoded yet.
fn demux_failure(frames: u64, e: SymError) -> Result<()> {
if frames == 0 {
return Err(map_err(e));
}
log::warn!("stopping decode after demux error: {e}");
Ok(())
}
/// Note a corrupt packet (the `errors`-th); a stream that never decodes is malformed.
fn corrupt_packet(errors: usize, frames: u64, msg: &str) -> Result<()> {
log::debug!("skipping corrupt packet: {msg}");
if errors > 1000 && frames == 0 {
return Err(AudioError::Malformed(format!("no decodable packets: {msg}")));
}
Ok(())
}
/// The decoded stream is authoritative for the channel count: record it and allocate the planes.
fn init_planar(info: &mut AudioInfo, ch: usize) -> Result<Vec<Vec<f32>>> {
info.channels = u16::try_from(ch).map_err(|_| AudioError::Unsupported("too many channels".into()))?;
Ok((0..ch).map(|_| Vec::new()).collect())
}
/// Append one interleaved block to the planes, replacing non-finite samples with silence.
fn append_interleaved(planar: &mut [Vec<f32>], interleaved: &[f32], ch: usize) {
for frame in interleaved.chunks_exact(ch) {
for (dst, &s) in planar.iter_mut().zip(frame) {
dst.push(if s.is_finite() { s } else { 0.0 });
}
}
}
/// Decode the whole first audio track. When `count_only`, samples are counted but not stored.
fn run(bytes: &[u8], detected: FileFormat, count_only: bool) -> Result<(AudioInfo, AudioBuffer)> {
let mut reader = match open(bytes, detected)? {
Opened::Reader(reader) => reader,
Opened::Empty(info) => {
let buf = AudioBuffer::new(info.sample_rate, usize::from(info.channels), 0);
return Ok((info, buf));
}
};
let (track_id, mut info) = info_of(reader.as_ref(), detected)?;
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 = 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));
let ch = u64::from(info.channels);
if params.max_frames_per_packet.into_iter().chain(alac.map(u64::from)).any(|n| n > MAX_PACKET_FRAMES || n.saturating_mul(ch) > MAX_PACKET_SAMPLES)
{
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, &decoder_options()).map_err(map_err)?;
let mut planar: Vec<Vec<f32>> = Vec::new();
let mut interleaved: Vec<f32> = Vec::new();
let mut frames: u64 = 0;
let mut errors = 0usize;
loop {
let packet = match reader.next_packet() {
Ok(Some(packet)) => packet,
Ok(None) => break,
Err(SymError::IoError(e)) if e.kind() == std::io::ErrorKind::UnexpectedEof => break,
Err(SymError::ResetRequired) => break,
Err(e) => {
demux_failure(frames, e)?;
break;
}
};
if packet.track_id != track_id {
continue;
}
// The declared frames per packet are not the packet's size: a container supplies that on its
// own, and the PCM decoder sizes its output by the bytes it is given.
if let Some(bytes_per_sample) = params.bits_per_coded_sample.map(|b| u64::from(b / 8)).filter(|&n| n > 0)
&& packet.data.len() as u64 / bytes_per_sample > MAX_PACKET_SAMPLES
{
return Err(AudioError::Malformed("a packet holds far more samples than any codec uses".into()));
}
let decoded = match decoder.decode(&packet) {
Ok(d) => d,
Err(SymError::DecodeError(m)) => {
errors += 1;
corrupt_packet(errors, frames, m)?;
continue;
}
Err(SymError::IoError(_)) => break,
Err(e) => return Err(map_err(e)),
};
// 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;
}
if planar.is_empty() {
planar = init_planar(&mut info, ch)?;
}
let n = decoded.frames() as u64;
frames = frames.saturating_add(n);
if frames.saturating_mul(ch as u64) > MAX_DECODE_SAMPLES {
return Err(AudioError::TooLarge("decoded audio exceeds the 4 GiB limit".into()));
}
if count_only {
continue;
}
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]);
append_interleaved(&mut planar, &interleaved[..needed], ch);
}
if info.channels == 0 {
return Err(AudioError::Malformed("stream contains no audio".into()));
}
if planar.is_empty() {
planar = (0..usize::from(info.channels)).map(|_| Vec::new()).collect();
}
info.frames = if count_only { frames } else { planar.iter().map(Vec::len).min().unwrap_or(0) as u64 };
let sample_rate = info.sample_rate;
Ok((info, AudioBuffer { sample_rate, channels: planar }))
}
/// Frames symphonia's AAC decoder yields for every packet; it supports no other frame length.
const AAC_FRAMES_PER_PACKET: u64 = 1024;
/// Whether `track` of `reader` is AAC in MP4. The MP4 reader reports the stts total as the length,
/// but the decoder yields a whole frame per packet (sample durations are not applied), so a last
/// sample whose duration differs from a frame makes the two disagree.
fn is_mp4_aac(reader: &dyn FormatReader, track_id: u32) -> bool {
reader.format_info().short_name == "isomp4"
&& reader.tracks().iter().find(|t| t.id == track_id).and_then(audio_params).is_some_and(|p| p.codec == CODEC_ID_AAC)
}
/// The decoded length of AAC in MP4: count the track's packets without decoding them.
fn mp4_aac_frames(reader: &mut dyn FormatReader, track_id: u32) -> Result<u64> {
let mut frames: u64 = 0;
loop {
match reader.next_packet() {
Ok(Some(packet)) if packet.track_id == track_id => frames = frames.saturating_add(AAC_FRAMES_PER_PACKET),
Ok(Some(_)) => {}
Ok(None) => break,
Err(SymError::IoError(e)) if e.kind() == std::io::ErrorKind::UnexpectedEof => break,
Err(SymError::ResetRequired) => break,
Err(e) => {
demux_failure(frames, e)?;
break;
}
}
}
Ok(frames)
}
pub(crate) fn probe(bytes: &[u8], detected: FileFormat) -> Result<AudioInfo> {
guarded(|| {
let mut reader = match open(bytes, detected)? {
Opened::Reader(reader) => reader,
Opened::Empty(info) => return Ok(info),
};
let (track_id, mut info) = info_of(reader.as_ref(), detected)?;
if info.frames > 0 && info.channels > 0 && is_mp4_aac(reader.as_ref(), track_id) {
info.frames = mp4_aac_frames(reader.as_mut(), track_id)?;
}
if info.frames > 0 && info.channels > 0 {
return Ok(info);
}
// Length not in the header (e.g. MP3 without a Xing frame): count by decoding.
run(bytes, detected, true).map(|(i, _)| i)
})
}
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));
}
}
-574
View File
@@ -1,574 +0,0 @@
//! Native WAV / BWF / RF64 reader and writer (RIFF chunk layout per the Microsoft multimedia
//! specification, `bext` per EBU Tech 3285, `ds64` per EBU Tech 3306).
use crate::pcm::{PcmKind, Quantizer, deinterleave, le_u16, le_u32, le_u64, sample_format, tag, validate_buffer, validate_layout, write_interleaved};
use crate::{AudioBuffer, AudioError, AudioInfo, BitDepth, BwfInfo, FileFormat, Result, SampleFormat};
use std::io::{Cursor, Seek, SeekFrom, Write};
const WAVE_FORMAT_PCM: u16 = 1;
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).
pub(crate) struct WavLayout {
pub info: AudioInfo,
pub kind: PcmKind,
pub data_start: usize,
pub data_len: usize,
}
/// Outcome of parsing a WAV header.
pub(crate) enum WavParse {
/// A PCM / float layout we decode natively.
Native(WavLayout),
/// A valid WAV whose codec we hand to symphonia (ADPCM, A-law, ...).
Foreign { bwf: Option<BwfInfo> },
}
struct Fmt {
tag: u16,
channels: u16,
sample_rate: u32,
block_align: u16,
bits: u16,
valid_bits: u16,
}
fn parse_fmt(c: &[u8]) -> Result<Fmt> {
let short = || AudioError::Malformed("fmt chunk too short".into());
let mut fmt = Fmt {
tag: le_u16(c, 0).ok_or_else(short)?,
channels: le_u16(c, 2).ok_or_else(short)?,
sample_rate: le_u32(c, 4).ok_or_else(short)?,
block_align: le_u16(c, 12).ok_or_else(short)?,
bits: le_u16(c, 14).unwrap_or(0),
valid_bits: 0,
};
fmt.valid_bits = fmt.bits;
if fmt.tag == WAVE_FORMAT_EXTENSIBLE {
// cbSize(2) validBits(2) channelMask(4) subFormat GUID(16)
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)
}
/// Read a NUL-padded ASCII field.
fn fixed_str(b: &[u8]) -> String {
let end = b.iter().position(|&c| c == 0).unwrap_or(b.len());
String::from_utf8_lossy(b.get(..end).unwrap_or_default()).trim_end().to_string()
}
fn parse_bext(c: &[u8]) -> Option<BwfInfo> {
if c.len() < 346 {
return None;
}
let lo = u64::from(le_u32(c, 338)?);
let hi = u64::from(le_u32(c, 342)?);
Some(BwfInfo {
description: fixed_str(c.get(0..256)?),
originator: fixed_str(c.get(256..288)?),
originator_reference: fixed_str(c.get(288..320)?),
origination_date: fixed_str(c.get(320..330)?),
origination_time: fixed_str(c.get(330..338)?),
time_reference: (hi << 32) | lo,
})
}
/// Refuse the WAVE file at `at`, which symphonia reads (A-law, ADPCM, ...), if an INFO entry
/// claims more bytes than the file holds: symphonia allocates each entry's length. Chunks are
/// walked where symphonia reads them: after the bytes a chunk's parser used, not its length.
pub(crate) fn check_info(b: &[u8], at: usize) -> Result<()> {
if tag(b, at + 8) != Some(*b"WAVE") {
return Ok(());
}
let mut pos = at + 12;
while let Some(id) = tag(b, pos) {
let Some(len) = le_u32(b, pos + 4).map(|n| n as usize) else { break };
let (body, pad) = (pos + 8, len & 1);
let used = match &id {
b"data" => break,
b"fmt " => fmt_used(b, body, len),
b"LIST" if len < 4 => break,
b"LIST" => {
let entries = if tag(b, body) == Some(*b"INFO") { info_entries(b, body + 4, len - 4)? } else { 0 };
// The list pads itself, then the chunk is padded again.
4 + entries.max(len - 4) + pad
}
_ => len,
};
pos = body.saturating_add(used).saturating_add(pad);
}
Ok(())
}
/// The bytes symphonia reads of a `fmt ` chunk: some formats' parsers go by their own extra size.
fn fmt_used(b: &[u8], body: usize, len: usize) -> usize {
match le_u16(b, body) {
Some(2 | 6 | 7 | 0x11) => le_u16(b, body + 16).map_or(len, |extra| 18 + usize::from(extra)),
Some(0xfffe) => 40,
_ => len,
}
}
/// Check the INFO entries in the `len` bytes from `start`; returns the bytes symphonia reads of
/// them, with the pad it reads after an odd entry.
fn info_entries(b: &[u8], start: usize, len: usize) -> Result<usize> {
let mut used = 0;
while used + 8 <= len {
let Some(n) = le_u32(b, start + used + 4).map(|n| n as usize) else { break };
if n > b.len().saturating_sub(start + used + 8) {
return Err(AudioError::Malformed("a WAV INFO entry is larger than the file".into()));
}
used += 8 + n;
used += used & 1;
}
Ok(used)
}
/// True when the bytes start like a RIFF/RF64/BW64 WAVE file.
pub(crate) fn is_wav(b: &[u8]) -> bool {
matches!(tag(b, 0), Some(t) if &t == b"RIFF" || &t == b"RF64" || &t == b"BW64") && tag(b, 8).is_some_and(|t| &t == b"WAVE")
}
/// True when everything from `from` to the end of the file is whole RIFF chunks (four printable
/// ASCII id bytes, a size that fits, the pad byte after an odd size; the last chunk's pad may be
/// missing): the metadata of a finished file, not samples.
fn only_chunks_follow(b: &[u8], from: usize) -> bool {
let mut pos = from;
while pos < b.len() {
let (Some(id), Some(size)) = (tag(b, pos), le_u32(b, pos + 4)) else { return false };
if !id.iter().all(|c| (0x20..0x7f).contains(c)) {
return false;
}
let size = size as usize;
let Some(end) = pos.checked_add(8).and_then(|body| body.checked_add(size)) else { return false };
if end > b.len() {
return false;
}
let next = end + (size & 1);
if end == b.len() || next == b.len() {
return true;
}
pos = next;
}
false
}
/// A `data` chunk header as found in the file, for [`data_chunk_len`].
struct DataChunk {
body: usize,
size32: u32,
is_rf64: bool,
remaining: usize,
declared: usize,
}
/// Bytes of audio a `data` chunk holds: leniently, whatever is present when the size is wrong or
/// the recording was never finalised.
fn data_chunk_len(b: &[u8], c: DataChunk) -> usize {
let finished = u64::from(le_u32(b, 4).unwrap_or(0)) + 8 == b.len() as u64;
let empty = c.size32 == 0 && !c.is_rf64 && finished && only_chunks_follow(b, c.body);
if c.size32 == 0 && !c.is_rf64 && !empty { c.remaining } else { c.declared.min(c.remaining) }
}
/// The sample layout of a `fmt` chunk, `None` for a format we do not read natively.
fn pcm_layout(fmt: &Fmt) -> Result<Option<(PcmKind, SampleFormat)>> {
Ok(Some(match (fmt.tag, fmt.bits) {
(WAVE_FORMAT_PCM, bits @ 1..=32) => {
let bytes = usize::from(bits.div_ceil(8));
let sf = match fmt.valid_bits.min(bits) {
0..=8 => SampleFormat::Int8,
9..=16 => SampleFormat::Int16,
17..=24 => SampleFormat::Int24,
_ => SampleFormat::Int32,
};
(PcmKind::Int { bytes, big_endian: false, unsigned: bytes == 1 }, sf)
}
(WAVE_FORMAT_IEEE_FLOAT, 32) => (PcmKind::Float { bytes: 4, big_endian: false }, SampleFormat::Float32),
(WAVE_FORMAT_IEEE_FLOAT, 64) => (PcmKind::Float { bytes: 8, big_endian: false }, SampleFormat::Float64),
(WAVE_FORMAT_PCM | WAVE_FORMAT_IEEE_FLOAT, bits) => {
return Err(AudioError::Unsupported(format!("WAV format {} with {bits} bits per sample", fmt.tag)));
}
_ => return Ok(None),
}))
}
pub(crate) fn parse(b: &[u8]) -> Result<WavParse> {
if !is_wav(b) {
return Err(AudioError::Malformed("not a RIFF/RF64 WAVE file".into()));
}
let is_rf64 = tag(b, 0).is_some_and(|t| &t != b"RIFF");
let mut ds64_data: Option<u64> = None;
let mut fmt: Option<Fmt> = None;
let mut bwf: Option<BwfInfo> = None;
let mut data: Option<(usize, usize)> = None;
let mut pos: usize = 12;
// Bound the number of chunks we look at so a file of tiny chunks can't spin forever.
let mut chunks = 0usize;
while let (Some(id), Some(size32)) = (tag(b, pos), le_u32(b, pos + 4)) {
chunks += 1;
if chunks > 100_000 {
break;
}
let body = pos.checked_add(8).ok_or_else(|| AudioError::Malformed("chunk offset overflow".into()))?;
let remaining = b.len().saturating_sub(body);
let mut size = u64::from(size32);
if &id == b"data" && is_rf64 && size32 == u32::MAX {
size = ds64_data.unwrap_or(remaining as u64);
}
let declared = usize::try_from(size).unwrap_or(usize::MAX);
if &id == b"data" {
// Lenient: a truncated recording (or a streaming writer's 0 / 0xFFFFFFFF size) still
// yields whatever audio is actually present.
// A size of 0 in a file whose RIFF size is right and that has only chunks after it is an
// empty data chunk followed by metadata (LIST, bext, …); otherwise it is a recording
// that was never finalised, and what follows is audio.
let len = data_chunk_len(b, DataChunk { body, size32, is_rf64, remaining, declared });
if data.is_none() {
data = Some((body, len));
}
if declared > remaining {
break;
}
} else {
if declared > remaining {
// A truncated non-audio chunk at the end of the file: stop scanning.
break;
}
let c = b.get(body..body + declared).unwrap_or_default();
match &id {
b"fmt " => fmt = Some(parse_fmt(c)?),
b"bext" => bwf = parse_bext(c),
b"ds64" => ds64_data = le_u64(c, 8),
_ => {}
}
}
let padded = declared.saturating_add(declared & 1);
pos = match body.checked_add(padded) {
Some(p) => p,
None => break,
};
}
let fmt = fmt.ok_or_else(|| AudioError::Malformed("WAV has no fmt chunk".into()))?;
if fmt.channels == 0 {
return Err(AudioError::Malformed("WAV declares zero channels".into()));
}
if fmt.sample_rate == 0 {
return Err(AudioError::Malformed("WAV declares a zero sample rate".into()));
}
let channels = usize::from(fmt.channels);
let Some((kind, sample_format)) = pcm_layout(&fmt)? else { return Ok(WavParse::Foreign { bwf }) };
let block = kind.bytes().checked_mul(channels).ok_or_else(|| AudioError::Malformed("block size overflow".into()))?;
if usize::from(fmt.block_align) != block {
log::debug!("WAV block_align {} disagrees with computed {block}; using computed", fmt.block_align);
}
let (data_start, data_len) = data.ok_or_else(|| AudioError::Malformed("WAV has no data chunk".into()))?;
let frames = (data_len / block) as u64;
Ok(WavParse::Native(WavLayout {
info: AudioInfo { format: FileFormat::Wav, sample_format, sample_rate: fmt.sample_rate, channels: fmt.channels, frames, bwf },
kind,
data_start,
data_len,
}))
}
pub(crate) fn decode(b: &[u8], layout: &WavLayout) -> Result<AudioBuffer> {
let end = layout.data_start.saturating_add(layout.data_len).min(b.len());
let data = b.get(layout.data_start..end).unwrap_or_default();
let channels = deinterleave(data, layout.kind, usize::from(layout.info.channels), layout.info.frames)?;
Ok(AudioBuffer { sample_rate: layout.info.sample_rate, channels })
}
/// Default speaker mask for a channel count (callers that know the layout pass their own):
/// mono, stereo, LCR, quad, 5.0, 5.1, 6.1, 7.1 (sides + rears), 7.1.4.
fn channel_mask(channels: usize) -> u32 {
match channels {
1 => 0x4,
2 => 0x3,
3 => 0x7,
4 => 0x33,
5 => 0x37,
6 => 0x3F,
7 => 0x13F,
8 => 0x63F,
// L R C LFE Lrs Rrs Lss Rss Ltf Rtf Ltr Rtr (7.1.4).
12 => 0x2_D63F,
n if n <= 18 => (1u32 << n) - 1,
_ => 0,
}
}
/// The `dwChannelMask` of a WAVE_FORMAT_EXTENSIBLE file (None for plain PCM/float headers).
pub(crate) fn read_channel_mask(b: &[u8]) -> Option<u32> {
if !is_wav(b) {
return None;
}
let mut pos = 12usize;
for _ in 0..10_000 {
let (id, size) = (tag(b, pos)?, usize::try_from(le_u32(b, pos + 4)?).ok()?);
if &id == b"fmt " {
return (le_u16(b, pos + 8)? == WAVE_FORMAT_EXTENSIBLE).then(|| le_u32(b, pos + 8 + 20)).flatten();
}
pos = pos.checked_add(8)?.checked_add(size)?.checked_add(size & 1)?;
}
None
}
fn put_fixed_str(out: &mut Vec<u8>, s: &str, len: usize) {
let bytes = s.as_bytes();
let mut n = bytes.len().min(len);
while !s.is_char_boundary(n) {
n -= 1;
}
out.extend_from_slice(bytes.get(..n).unwrap_or_default());
out.resize(out.len() + (len - n), 0);
}
fn bext_chunk(bwf: &BwfInfo) -> Vec<u8> {
let mut c = Vec::with_capacity(BEXT_MIN_LEN);
put_fixed_str(&mut c, &bwf.description, 256);
put_fixed_str(&mut c, &bwf.originator, 32);
put_fixed_str(&mut c, &bwf.originator_reference, 32);
put_fixed_str(&mut c, &bwf.origination_date, 10);
put_fixed_str(&mut c, &bwf.origination_time, 8);
c.extend_from_slice(&((bwf.time_reference & 0xFFFF_FFFF) as u32).to_le_bytes());
c.extend_from_slice(&((bwf.time_reference >> 32) as u32).to_le_bytes());
c.extend_from_slice(&1u16.to_le_bytes()); // version 1
c.resize(BEXT_MIN_LEN, 0); // UMID, loudness, reserved; no coding history
c
}
fn push_chunk(out: &mut Vec<u8>, id: &[u8; 4], body: &[u8]) {
out.extend_from_slice(id);
out.extend_from_slice(&u32::try_from(body.len()).unwrap_or(u32::MAX).to_le_bytes());
out.extend_from_slice(body);
if body.len() % 2 == 1 {
out.push(0);
}
}
/// A WAV written block by block: a placeholder header first, then the real one (the same length,
/// RF64 above 4 GiB, or always when `force_rf64`) written over it by [`Writer::finish`].
pub(crate) struct Writer<W> {
out: W,
start: u64,
channels: usize,
sample_rate: u32,
format: (u16, bool),
bwf: Option<BwfInfo>,
mask: Option<u32>,
force_rf64: bool,
quantizers: Vec<Quantizer>,
frames: u64,
bytes: Vec<u8>,
/// Set when a block write has failed: the sink may hold a prefix of that block while
/// `frames` does not count it, so the writer refuses every later call.
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, 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()?;
out.write_all(&header(channels, sample_rate, format, bwf, mask, 0, force_rf64)?)?;
let quantizers = Quantizer::per_channel(u32::from(format.0), dither, channels);
Ok(Writer {
out,
start,
channels,
sample_rate,
format,
bwf: bwf.cloned(),
mask,
force_rf64,
quantizers,
frames: 0,
bytes: Vec::new(),
failed: false,
})
}
fn header(&self, frames: u64) -> Result<Vec<u8>> {
header(self.channels, self.sample_rate, self.format, self.bwf.as_ref(), self.mask, frames, self.force_rf64)
}
/// The frame count after `frames` more (RF64 has room for any bounce).
pub(crate) fn check_room(&self, frames: u64) -> Result<u64> {
let total = self.frames.saturating_add(frames);
self.header(total)?;
Ok(total)
}
/// Appends `frames` frames of planar audio (missing samples are silence).
pub(crate) fn write(&mut self, block: &[Vec<f32>], frames: usize) -> Result<()> {
if self.failed {
return Err(crate::failed_write_error());
}
let total = self.check_room(frames as u64)?;
if let Err(e) = write_interleaved(&mut self.out, &mut self.bytes, block, frames, &mut self.quantizers, self.format, false) {
self.failed = true;
return Err(AudioError::from(e));
}
self.frames = total;
Ok(())
}
pub(crate) fn finish(mut self) -> Result<W> {
if self.failed {
return Err(crate::failed_write_error());
}
let data_len = u64::from(self.format.0 / 8).saturating_mul(self.channels as u64).saturating_mul(self.frames);
if data_len % 2 == 1 {
self.out.write_all(&[0])?;
}
let head = self.header(self.frames)?;
self.out.seek(SeekFrom::Start(self.start))?;
self.out.write_all(&head)?;
self.out.seek(SeekFrom::End(0))?;
self.out.flush()?;
Ok(self.out)
}
}
/// Encode a WAV file. `force_rf64` writes the RF64 layout even for small files (for tests);
/// it is used automatically when the file would exceed 4 GiB.
/// `mask` overrides the speaker mask derived from the channel count.
pub(crate) fn encode(
buf: &AudioBuffer,
depth: BitDepth,
dither: bool,
bwf: Option<&BwfInfo>,
force_rf64: bool,
mask: Option<u32>,
) -> Result<Vec<u8>> {
validate_buffer(buf)?;
let channels = buf.channels.len();
let frames = buf.frames();
let format = sample_format(depth);
let head = header(channels, buf.sample_rate, format, bwf, mask, frames as u64, force_rf64)?;
let data_len = (usize::from(format.0 / 8) * channels).checked_mul(frames).ok_or_else(|| AudioError::TooLarge("data size overflow".into()))?;
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, HeaderExtras { bwf, mask, force_rf64 })?;
w.write(&buf.channels, frames)?;
Ok(w.finish()?.into_inner())
}
/// Everything before the sample data of a WAV holding `frames` frames.
pub(crate) fn header(
channels: usize,
sample_rate: u32,
(bits, is_float): (u16, bool),
bwf: Option<&BwfInfo>,
mask: Option<u32>,
frames: u64,
force_rf64: bool,
) -> Result<Vec<u8>> {
let block = usize::from(bits / 8).checked_mul(channels).ok_or_else(|| AudioError::TooLarge("block size overflow".into()))?;
let block_align = u16::try_from(block).map_err(|_| AudioError::Encode("too many channels for WAV".into()))?;
let byte_rate = sample_rate.checked_mul(u32::from(block_align)).ok_or_else(|| AudioError::Encode("WAV byte rate overflow".into()))?;
let data_len = u64::from(block_align).checked_mul(frames).ok_or_else(|| AudioError::TooLarge("data size overflow".into()))?;
let extensible = channels > 2 || bits > 16 || mask.is_some_and(|m| m != channel_mask(channels));
let fmt_tag = if is_float { WAVE_FORMAT_IEEE_FLOAT } else { WAVE_FORMAT_PCM };
let mut fmt = Vec::with_capacity(40);
fmt.extend_from_slice(&(if extensible { WAVE_FORMAT_EXTENSIBLE } else { fmt_tag }).to_le_bytes());
fmt.extend_from_slice(&(channels as u16).to_le_bytes());
fmt.extend_from_slice(&sample_rate.to_le_bytes());
fmt.extend_from_slice(&byte_rate.to_le_bytes());
fmt.extend_from_slice(&block_align.to_le_bytes());
fmt.extend_from_slice(&bits.to_le_bytes());
if extensible {
fmt.extend_from_slice(&22u16.to_le_bytes());
fmt.extend_from_slice(&bits.to_le_bytes());
fmt.extend_from_slice(&mask.unwrap_or_else(|| channel_mask(channels)).to_le_bytes());
fmt.extend_from_slice(&fmt_tag.to_le_bytes());
fmt.extend_from_slice(&GUID_TAIL);
}
let mut head = Vec::with_capacity(1024);
let rf64_body_len = 28usize;
// Reserve room for ds64 (as JUNK when not needed, so a later writer could upgrade in place).
let mut meta = Vec::new();
if let Some(bwf) = bwf {
push_chunk(&mut meta, b"bext", &bext_chunk(bwf));
}
push_chunk(&mut meta, b"fmt ", &fmt);
if is_float {
push_chunk(&mut meta, b"fact", &u32::try_from(frames).unwrap_or(u32::MAX).to_le_bytes());
}
let total = ((4 + (8 + rf64_body_len) + meta.len() + 8) as u64)
.checked_add(data_len)
.and_then(|t| t.checked_add(data_len & 1))
.ok_or_else(|| AudioError::TooLarge("WAV size overflow".into()))?;
let rf64 = force_rf64 || u32::try_from(total).is_err();
if rf64 {
head.extend_from_slice(b"RF64");
head.extend_from_slice(&u32::MAX.to_le_bytes());
head.extend_from_slice(b"WAVE");
let mut ds64 = Vec::with_capacity(rf64_body_len);
ds64.extend_from_slice(&total.to_le_bytes());
ds64.extend_from_slice(&data_len.to_le_bytes());
ds64.extend_from_slice(&frames.to_le_bytes());
ds64.extend_from_slice(&0u32.to_le_bytes());
push_chunk(&mut head, b"ds64", &ds64);
} else {
head.extend_from_slice(b"RIFF");
head.extend_from_slice(&(total as u32).to_le_bytes());
head.extend_from_slice(b"WAVE");
push_chunk(&mut head, b"JUNK", &[0u8; 28]);
}
head.extend_from_slice(&meta);
head.extend_from_slice(b"data");
head.extend_from_slice(&(if rf64 { u32::MAX } else { data_len as u32 }).to_le_bytes());
Ok(head)
}
#[cfg(test)]
mod tests {
use super::header;
#[test]
fn rejects_byte_rate_overflow() {
let result = header(2, 1_100_000_000, (16, false), None, None, 0, false);
assert!(result.is_err(), "overflowing byte rate was accepted");
}
}
-17
View File
@@ -1,17 +0,0 @@
Tiny fixtures for `tests/it/decode_read_list.rs`: one per container or codec the READ list names
that the crate cannot write itself. Made with ffmpeg 8.0 from a 0.1 s mono 440 Hz sine
(`F="-y -f lavfi -i sine=frequency=440:duration=0.1 -ac 1"`):
ffmpeg $F -ar 8000 -c:a libmp3lame -b:a 16k tone.mp3
ffmpeg $F -ar 24000 -c:a mp2 -b:a 32k tone.mp2
ffmpeg $F -ar 8000 -c:a libvorbis -q:a -1 tone.ogg
ffmpeg $F -ar 8000 -c:a aac -b:a 16k tone.m4a
ffmpeg $F -ar 8000 -c:a aac -b:a 16k -f adts tone.aac
ffmpeg $F -ar 8000 -c:a pcm_s16le tone.caf
ffmpeg $F -ar 8000 -c:a pcm_s16le tone.mkv
ffmpeg $F -ar 8000 -c:a libvorbis -q:a -1 tone.webm
ffmpeg $F -ar 8000 -c:a pcm_s16le -f aiff tone.aifc # AIFF-C "sowt"
`silence.mp1` has no ffmpeg encoder. It is four hand-built MPEG-1 Layer I frames (mono, 32 kHz,
32 kbit/s, 48 bytes each: header `FF FF 18 C0`, all subband allocations zero), which decode to
silence.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -1,51 +0,0 @@
//! Every extension the format table reads decodes with the compiled-in `symphonia` features.
use soundcraft_audio_io::{AudioBuffer, BitDepth, EncodeOptions, FileFormat, decode, encode, probe};
fn fixture(name: &str) -> Vec<u8> {
let path = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("tests/fixtures").join(name);
std::fs::read(&path).unwrap_or_else(|e| panic!("{}: {e}", path.display()))
}
fn generated(format: FileFormat) -> Vec<u8> {
let tone: Vec<f32> = (0..800).map(|i| (i as f32 * 0.1).sin() * 0.5).collect();
let buf = AudioBuffer { sample_rate: 8000, channels: vec![tone] };
encode(&buf, &EncodeOptions { format, bit_depth: BitDepth::Int16, dither: false, bwf: None }).unwrap()
}
fn bytes_for(ext: &str) -> Vec<u8> {
match ext {
"wav" | "wave" | "bwf" => generated(FileFormat::Wav),
"rf64" => {
let tone = AudioBuffer { sample_rate: 8000, channels: vec![vec![0.25; 800]] };
let opts = EncodeOptions { format: FileFormat::Wav, bit_depth: BitDepth::Int16, dither: false, bwf: None };
soundcraft_audio_io::encode_wav_rf64(&tone, &opts).unwrap()
}
"aif" | "aiff" => generated(FileFormat::Aiff),
"aifc" => fixture("tone.aifc"),
"flac" => generated(FileFormat::Flac),
"mp3" => fixture("tone.mp3"),
"mp2" => fixture("tone.mp2"),
"mp1" => fixture("silence.mp1"),
"ogg" | "oga" => fixture("tone.ogg"),
"m4a" | "m4b" | "mp4" => fixture("tone.m4a"),
"aac" | "adts" => fixture("tone.aac"),
"caf" => fixture("tone.caf"),
"mkv" => fixture("tone.mkv"),
"webm" => fixture("tone.webm"),
other => panic!("no fixture for the READ extension {other:?}: add one"),
}
}
#[test]
fn every_read_extension_probes_and_decodes() {
for &format in FileFormat::READ {
for ext in format.extensions() {
let bytes = bytes_for(ext);
let info = probe(&bytes, Some(ext)).unwrap_or_else(|e| panic!("probe .{ext}: {e}"));
let (_, buf) = decode(&bytes, Some(ext)).unwrap_or_else(|e| panic!("decode .{ext}: {e}"));
assert!(info.channels >= 1, ".{ext}");
assert!(buf.frames() > 0, ".{ext}: no frames");
}
}
}
-36
View File
@@ -1,36 +0,0 @@
use soundcraft_audio_io::FileFormat;
#[test]
fn every_read_and_write_format_round_trips_through_its_extensions() {
for &format in FileFormat::READ {
assert!(!format.extensions().is_empty(), "{format:?}");
for ext in format.extensions() {
assert_eq!(FileFormat::from_extension(ext), Some(format), "{ext}");
assert_eq!(FileFormat::from_extension(&format!(".{}", ext.to_ascii_uppercase())), Some(format), "{ext}");
assert_eq!(FileFormat::from_path(format!("dir.x/Take 1.{ext}")), Some(format), "{ext}");
}
}
for &format in FileFormat::WRITE {
assert!(!format.write_extensions().is_empty(), "{format:?}");
for ext in format.write_extensions() {
assert_eq!(FileFormat::from_write_extension(ext), Some(format), "{ext}");
assert_eq!(FileFormat::from_write_extension(&ext.to_ascii_uppercase()), Some(format), "{ext}");
assert_eq!(FileFormat::from_extension(ext), Some(format), "{ext}: what is written reads back");
}
assert!(format.write_extensions().contains(&format.default_extension()), "{format:?}");
}
}
#[test]
fn only_names_whose_readers_expect_the_encoder_output_are_writable() {
assert_eq!(FileFormat::writable_extensions(), ["wav", "wave", "bwf", "aif", "aiff", "flac"]);
for read_only in ["rf64", "aifc", "mp3", "ogg", "m4a", "mkv"] {
assert!(FileFormat::from_extension(read_only).is_some(), "{read_only}");
assert_eq!(FileFormat::from_write_extension(read_only), None, "{read_only}");
}
for unknown in ["xyz", "", "mid"] {
assert_eq!(FileFormat::from_extension(unknown), None, "{unknown}");
}
assert_eq!(FileFormat::from_path("no-extension"), None);
assert_eq!(FileFormat::Aiff.default_extension(), "aif");
}
-101
View File
@@ -1,101 +0,0 @@
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}");
}
}
}
}
-10
View File
@@ -1,10 +0,0 @@
//! Integration tests, built as one test binary: each file is a module.
mod decode_read_list;
mod file_format;
mod float_headroom;
mod malformed;
mod mp4_length;
mod roundtrip;
mod stream;
mod wav_empty_data;
-878
View File
@@ -1,878 +0,0 @@
//! Hostile inputs must produce `Err` (or a best-effort `Ok`), never a panic.
use proptest::prelude::*;
use soundcraft_audio_io::{AudioBuffer, AudioError, BitDepth, EncodeOptions, FileFormat, SampleFormat, decode, detect_format, encode, probe};
fn small(format: FileFormat, depth: BitDepth, channels: usize) -> Vec<u8> {
let mut b = AudioBuffer::new(44_100, channels, 300);
for ch in &mut b.channels {
for (i, s) in ch.iter_mut().enumerate() {
*s = ((i as f32) * 0.1).sin() * 0.5;
}
}
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();
for (id, data) in chunks {
body.extend_from_slice(*id);
body.extend_from_slice(&(data.len() as u32).to_le_bytes());
body.extend_from_slice(data);
if data.len() % 2 == 1 {
body.push(0);
}
}
let mut out = b"RIFF".to_vec();
out.extend_from_slice(&(body.len() as u32).to_le_bytes());
out.extend_from_slice(&body);
out
}
fn fmt(tag: u16, channels: u16, sr: u32, bits: u16) -> Vec<u8> {
let align = channels * bits.div_ceil(8);
let mut f = Vec::new();
f.extend_from_slice(&tag.to_le_bytes());
f.extend_from_slice(&channels.to_le_bytes());
f.extend_from_slice(&sr.to_le_bytes());
f.extend_from_slice(&(sr * u32::from(align)).to_le_bytes());
f.extend_from_slice(&align.to_le_bytes());
f.extend_from_slice(&bits.to_le_bytes());
f
}
#[test]
fn every_truncation_is_handled() {
let files = [
small(FileFormat::Wav, BitDepth::Int16, 2),
small(FileFormat::Wav, BitDepth::Float32, 6),
small(FileFormat::Aiff, BitDepth::Int24, 1),
small(FileFormat::Aiff, BitDepth::Float32, 2),
small(FileFormat::Flac, BitDepth::Int16, 2),
small(FileFormat::Flac, BitDepth::Int24, 1),
];
for file in &files {
for n in 0..file.len() {
let cut = &file[..n];
let _ = probe(cut, None);
if let Ok((info, buf)) = decode(cut, None) {
assert_eq!(buf.num_channels(), info.channels as usize);
assert_eq!(buf.frames() as u64, info.frames);
}
}
}
}
#[test]
fn truncated_data_chunk_yields_partial_audio() {
let file = small(FileFormat::Wav, BitDepth::Int16, 2);
let (info, buf) = decode(&file[..file.len() - 400], None).unwrap();
assert_eq!(info.frames, 200);
assert_eq!(buf.frames(), 200);
}
#[test]
fn every_single_byte_corruption_is_handled() {
for file in [small(FileFormat::Wav, BitDepth::Int24, 2), small(FileFormat::Aiff, BitDepth::Int16, 2), small(FileFormat::Flac, BitDepth::Int16, 1)]
{
for i in 0..file.len().min(200) {
for v in [0x00, 0xFF, 0x7F, 0x80] {
let mut f = file.clone();
f[i] = v;
let _ = probe(&f, None);
let _ = decode(&f, None);
}
}
}
}
#[test]
fn zero_channels_is_malformed() {
let wav = riff(&[(b"fmt ", fmt(1, 0, 44_100, 16)), (b"data", vec![0; 16])]);
assert!(matches!(decode(&wav, None), Err(AudioError::Malformed(_))));
assert!(probe(&wav, None).is_err());
let mut aiff = small(FileFormat::Aiff, BitDepth::Int16, 1);
let comm = aiff.windows(4).position(|w| w == b"COMM").unwrap();
aiff[comm + 8] = 0;
aiff[comm + 9] = 0;
assert!(matches!(decode(&aiff, None), Err(AudioError::Malformed(_))));
}
#[test]
fn aiff_negative_channel_count_is_malformed() {
let mut aiff = small(FileFormat::Aiff, BitDepth::Int16, 1);
let comm = aiff.windows(4).position(|w| w == b"COMM").unwrap();
aiff[comm + 8] = 0x80;
aiff[comm + 9] = 0;
assert!(probe(&aiff, None).is_err());
}
#[test]
fn zero_sample_rate_is_malformed() {
let wav = riff(&[(b"fmt ", fmt(1, 2, 0, 16)), (b"data", vec![0; 16])]);
assert!(decode(&wav, None).is_err());
let mut aiff = small(FileFormat::Aiff, BitDepth::Int16, 1);
let comm = aiff.windows(4).position(|w| w == b"COMM").unwrap();
for b in &mut aiff[comm + 16..comm + 26] {
*b = 0;
}
assert!(decode(&aiff, None).is_err());
// Infinite exponent.
aiff[comm + 16] = 0x7F;
aiff[comm + 17] = 0xFF;
assert!(decode(&aiff, None).is_err());
}
#[test]
fn absurd_chunk_sizes() {
// fmt chunk claims 4 GiB.
let mut wav = riff(&[(b"fmt ", fmt(1, 2, 44_100, 16)), (b"data", vec![0; 16])]);
wav[16..20].copy_from_slice(&u32::MAX.to_le_bytes());
assert!(decode(&wav, None).is_err());
// data chunk claims 4 GiB: lenient read of what's there.
let mut wav = riff(&[(b"fmt ", fmt(1, 2, 44_100, 16)), (b"data", vec![0; 16])]);
let d = wav.windows(4).position(|w| w == b"data").unwrap();
wav[d + 4..d + 8].copy_from_slice(&u32::MAX.to_le_bytes());
let (info, buf) = decode(&wav, None).unwrap();
assert_eq!((info.frames, buf.frames()), (4, 4));
// An unknown chunk with an absurd size before fmt.
let mut wav = riff(&[(b"junk", vec![0; 4]), (b"fmt ", fmt(1, 2, 44_100, 16)), (b"data", vec![0; 16])]);
wav[16..20].copy_from_slice(&0xFFFF_FFF0u32.to_le_bytes());
assert!(decode(&wav, None).is_err());
// RF64 with a ds64 claiming an exabyte of data.
let mut ds64 = Vec::new();
ds64.extend_from_slice(&u64::MAX.to_le_bytes());
ds64.extend_from_slice(&u64::MAX.to_le_bytes());
ds64.extend_from_slice(&u64::MAX.to_le_bytes());
ds64.extend_from_slice(&0u32.to_le_bytes());
let mut rf = riff(&[(b"ds64", ds64), (b"fmt ", fmt(1, 1, 8_000, 8)), (b"data", vec![0x80; 10])]);
rf[..4].copy_from_slice(b"RF64");
let d = rf.windows(4).rposition(|w| w == b"data").unwrap();
rf[d + 4..d + 8].copy_from_slice(&u32::MAX.to_le_bytes());
let (info, buf) = decode(&rf, None).unwrap();
assert_eq!((info.frames, buf.frames()), (10, 10));
// AIFF COMM claims more frames than present; SSND offset beyond the end.
let mut aiff = small(FileFormat::Aiff, BitDepth::Int16, 1);
let comm = aiff.windows(4).position(|w| w == b"COMM").unwrap();
aiff[comm + 10..comm + 14].copy_from_slice(&u32::MAX.to_be_bytes());
assert_eq!(decode(&aiff, None).unwrap().1.frames(), 300);
let ssnd = aiff.windows(4).position(|w| w == b"SSND").unwrap();
aiff[ssnd + 8..ssnd + 12].copy_from_slice(&u32::MAX.to_be_bytes());
assert_eq!(decode(&aiff, None).unwrap().1.frames(), 0);
}
fn caf(chunks: &[(&[u8; 4], i64, Vec<u8>)]) -> Vec<u8> {
let mut out = b"caff\0\x01\0\0".to_vec();
for (kind, size, body) in chunks {
out.extend_from_slice(*kind);
out.extend_from_slice(&size.to_be_bytes());
out.extend_from_slice(body);
}
out
}
fn caf_desc(format: &[u8; 4], bytes_per_packet: u32, frames_per_packet: u32, channels: u32, bits: u32) -> (&'static [u8; 4], i64, Vec<u8>) {
let mut d = 48_000f64.to_be_bytes().to_vec();
d.extend_from_slice(format);
for v in [0, bytes_per_packet, frames_per_packet, channels, bits] {
d.extend_from_slice(&v.to_be_bytes());
}
(b"desc", 32, d)
}
fn packet_table(packets: i64, frames: i64, entries: &[u8]) -> Vec<u8> {
[&packets.to_be_bytes()[..], &frames.to_be_bytes(), &[0; 8], entries].concat()
}
#[test]
fn caf_sizes_beyond_the_file_are_errors_not_aborts() {
let aac = || caf_desc(b"aac ", 0, 1024, 2, 0);
let huge = 1i64 << 50;
let cookie = caf(&[aac(), (b"kuki", huge, vec![0; 8])]);
let files = [
cookie.clone(),
[vec![0; 100], cookie].concat(),
caf(&[aac(), (b"data", -1, [&[0; 4][..], b"kuki", &huge.to_be_bytes()].concat())]),
caf(&[aac(), (b"pakt", 24, packet_table(huge, 1024, &[])), (b"data", -1, vec![0; 16])]),
caf(&[aac(), (b"pakt", 32, packet_table(1, 1024, &[0x82, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0])), (b"data", -1, vec![0; 64])]),
caf(&[aac(), (b"pakt", 25, packet_table(1, 1024, &[0x80]))]),
];
for f in files {
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 constant = caf(&[caf_desc(b"lpcm", 2, 1, 1, 16), (b"pakt", 25, packet_table(1, 1, &[1])), (b"data", -1, vec![0; 16])]);
assert!(matches!(decode(&constant, None), Err(AudioError::Malformed(m)) if m.contains("lists packets")));
}
/// An EBML element: `id`, then `size` as an 8-byte vint (so any size up to 2^56 − 2 fits, and
/// `u64::MAX` writes the reserved "unknown size"), then `body`.
fn ebml(id: &[u8], size: u64, body: &[u8]) -> Vec<u8> {
let size = if size == u64::MAX { 0x01FF_FFFF_FFFF_FFFF } else { 0x0100_0000_0000_0000 | size };
[id, &size.to_be_bytes(), body].concat()
}
/// The EBML header of a Matroska file.
fn ebml_header() -> Vec<u8> {
let doc_type = ebml(&[0x42, 0x82], 8, b"matroska");
ebml(&[0x1A, 0x45, 0xDF, 0xA3], doc_type.len() as u64, &doc_type)
}
/// #878: symphonia's Matroska reader takes element sizes from the file. With symphonia 0.5 a size
/// far beyond the file was allocated up front and aborted the process (no `catch_unwind` can catch
/// an allocation failure); since 0.6 such reads grow with the data actually read. Keep it that way
/// for the header, for codec data and for block frames.
#[test]
fn matroska_sizes_beyond_the_file_are_errors_not_aborts() {
let huge = 1u64 << 47;
// The 30-byte file from the report: EBML header and DocType sizes of 2^48 − 1 and 2^47.
let reported = [
0x1A, 0x45, 0xDF, 0xA3, 0x01, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x42, 0x82, 0x01, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x6D,
0x61, 0x74, 0x72, 0x6F, 0x73, 0x6B, 0x61,
];
let segment = |body: Vec<u8>| [ebml_header(), ebml(&[0x18, 0x53, 0x80, 0x67], u64::MAX, &body)].concat();
let info = [ebml(&[0x2A, 0xD7, 0xB1], 3, &[0x0F, 0x42, 0x40]), ebml(&[0x4D, 0x80], 4, b"test"), ebml(&[0x57, 0x41], 4, b"test")].concat();
let info = ebml(&[0x15, 0x49, 0xA9, 0x66], info.len() as u64, &info);
// A valid 16-bit PCM track entry's children, followed by `extra`.
let pcm = |extra: &[u8]| {
let audio = [ebml(&[0xB5], 8, &48_000f64.to_be_bytes()), ebml(&[0x9F], 1, &[1]), ebml(&[0x62, 0x64], 1, &[16])].concat();
[
ebml(&[0xD7], 1, &[1]),
ebml(&[0x73, 0xC5], 1, &[1]),
ebml(&[0x83], 1, &[2]),
ebml(&[0x86], 13, b"A_PCM/INT/LIT"),
ebml(&[0xE1], audio.len() as u64, &audio),
extra.to_vec(),
]
.concat()
};
// That track with a CodecPrivate claiming 2^46 bytes, inside parents that claim even more: the
// segment is valid up to that read, which runs out of file.
let track = ebml(&[0xAE], huge, &pcm(&ebml(&[0x63, 0xA2], huge >> 1, &[0; 16])));
let codec_data = segment([info.clone(), ebml(&[0x16, 0x54, 0xAE, 0x6B], huge, &track)].concat());
// The plain track, then a cluster whose SimpleBlock claims 2^47 bytes of frame data.
let track = pcm(&[]);
let track = ebml(&[0xAE], track.len() as u64, &track);
let block = ebml(&[0x1F, 0x43, 0xB6, 0x75], u64::MAX, &[ebml(&[0xE7], 1, &[0]), ebml(&[0xA3], huge, &[0x81, 0, 0, 0x80])].concat());
let cluster = segment([info, ebml(&[0x16, 0x54, 0xAE, 0x6B], track.len() as u64, &track), block].concat());
let out_of_file = |r: Result<_, AudioError>| matches!(r, Err(AudioError::Malformed(m)) if m.contains("end of file"));
for hint in [None, Some("mkv"), Some("wav")] {
assert!(probe(&reported, hint).is_err(), "{hint:?}");
assert!(decode(&reported, hint).is_err(), "{hint:?}");
// Refused at the oversized read itself, not by anything before it.
assert!(out_of_file(probe(&codec_data, hint).map(|_| ())), "{hint:?}: {:?}", probe(&codec_data, hint).err());
assert!(out_of_file(decode(&codec_data, hint).map(|_| ())), "{hint:?}");
// The file and its track open, so decoding reaches the block; the block reader stops at the
// end of the file, a best-effort read of what is there.
assert!(probe(&cluster, hint).is_ok(), "{hint:?}: {:?}", probe(&cluster, hint).err());
assert!(decode(&cluster, hint).map_or(0, |(_, b)| b.frames()) < 1_000, "{hint:?}");
}
}
#[test]
fn well_formed_caf_still_reads() {
let pcm: Vec<u8> = (0..480i16).flat_map(|i| (i * 50).to_be_bytes()).collect();
let chan = [((100u32 << 16) | 1).to_be_bytes(), [0; 4], [0; 4]].concat();
let data = [vec![0; 4], pcm].concat();
let lpcm = caf(&[caf_desc(b"lpcm", 2, 1, 1, 16), (b"chan", 12, chan), (b"free", 8, vec![0; 8]), (b"data", data.len() as i64, data)]);
let (info, buf) = decode(&lpcm, None).unwrap();
assert_eq!((info.format, buf.frames()), (FileFormat::Caf, 480));
let table = packet_table(3, 3072, &[10, 0x81, 0x48, 5]);
let aac = caf(&[caf_desc(b"aac ", 0, 1024, 2, 0), (b"pakt", table.len() as i64, table), (b"data", 219, vec![0; 219])]);
assert_eq!(probe(&aac, None).unwrap().frames, 3072);
}
fn atom(kind: &[u8; 4], body: &[u8]) -> Vec<u8> {
[&(8 + body.len() as u32).to_be_bytes()[..], kind, body].concat()
}
/// A mono 16-bit, 48 kHz audio sample entry of `version`, its version fields and codec atoms.
fn sample_entry(kind: &[u8; 4], version: u8, fields: &[u8], codec: &[u8]) -> Vec<u8> {
let base = [0, 0, 0, 0, 0, 0, 0, 1, 0, version, 0, 0, 0, 0, 0, 0, 0, 1, 0, 16, 0, 0, 0, 0];
atom(kind, &[&base[..], &(48_000u32 << 16).to_be_bytes(), fields, codec].concat())
}
/// An MP4 of `frames` 2-byte samples described by `entry`; `swap` may replace any version-0 atom.
fn mp4(frames: u32, entry: &[u8], swap: impl Fn(&[u8; 4], Vec<u8>) -> Vec<u8>) -> Vec<u8> {
let full = |kind: &[u8; 4], fields: &[u32]| {
let body: Vec<u8> = [0].iter().chain(fields).flat_map(|v: &u32| v.to_be_bytes()).collect();
swap(kind, atom(kind, &body))
};
let moov = |offset: u32| {
let stsd = atom(b"stsd", &[&[0, 0, 0, 0, 0, 0, 0, 1][..], entry].concat());
let stbl =
[stsd, full(b"stts", &[1, frames, 1]), full(b"stsc", &[1, 1, frames, 1]), full(b"stsz", &[2, frames]), full(b"stco", &[1, offset])];
let minf = [full(b"smhd", &[0]), atom(b"stbl", &stbl.concat())].concat();
let mdia = [full(b"mdhd", &[0, 0, 48_000, frames, 0]), full(b"hdlr", &[0, u32::from_be_bytes(*b"soun"), 0, 0, 0, 0]), atom(b"minf", &minf)];
let trak = [full(b"tkhd", &[0; 20]), atom(b"mdia", &mdia.concat())].concat();
atom(b"moov", &[full(b"mvhd", &[[0, 0, 48_000, frames, 1 << 16].as_slice(), &[0; 18], &[2]].concat()), atom(b"trak", &trak)].concat())
};
let ftyp = atom(b"ftyp", b"M4A \0\0\0\0M4A isom");
let offset = (ftyp.len() + moov(0).len() + 8) as u32;
let pcm: Vec<u8> = (0..frames as i16).flat_map(|i| i.wrapping_mul(40).to_le_bytes()).collect();
[ftyp, moov(offset), atom(b"mdat", &pcm)].concat()
}
#[test]
fn mp4_sizes_beyond_the_file_are_errors_not_aborts() {
let pcm = sample_entry(b"sowt", 0, &[], &[]);
let (_, buf) = decode(&mp4(480, &pcm, |_, a| a), None).unwrap();
assert_eq!(buf.frames(), 480);
let huge = |kind: &[u8; 4], a: Vec<u8>| [&1u32.to_be_bytes()[..], kind, &(1u64 << 60).to_be_bytes(), &a[8..]].concat();
// The first field after version and flags: stts's entry count, stsz's sample size.
let first_field_max = |mut a: Vec<u8>| {
a[12..16].copy_from_slice(&u32::MAX.to_be_bytes());
a
};
let sizes: Vec<u8> = (0..480u32).flat_map(|i| if i == 479 { u32::MAX } else { 2 }.to_be_bytes()).collect();
let per_sample = atom(b"stsz", &[&[0; 8][..], &480u32.to_be_bytes(), &sizes].concat());
// A header that starts inside its container is read whole, past the container's end.
let mdia = atom(b"mdia", &1u32.to_be_bytes());
let straddling = [&b"hdlr"[..], &(1u64 << 60).to_be_bytes(), &[0; 24]].concat();
let files = [
mp4(480, &pcm, |k, a| if k == b"hdlr" { huge(k, a) } else { a }),
mp4(480, &pcm, |k, a| if k == b"stts" { first_field_max(a) } else { a }),
mp4(480, &pcm, |k, a| if k == b"stsz" { first_field_max(a) } else { a }),
mp4(480, &pcm, |k, a| if k == b"stsz" { per_sample.clone() } else { a }),
[atom(b"ftyp", b"M4A \0\0\0\0M4A isom"), atom(b"moov", &atom(b"trak", &mdia)), straddling].concat(),
];
for f in files {
assert!(matches!(decode(&f, None), Err(AudioError::Malformed(m)) if m.contains("larger than the file")));
}
}
#[test]
fn atoms_running_past_their_parent_are_walked_once() {
// Each moov holds a trak that runs to the end of the file; symphonia stops after the first.
let unit = atom(b"moov", &[&u32::MAX.to_be_bytes()[..], b"trak"].concat());
let f = [atom(b"ftyp", b"M4A \0\0\0\0M4A isom"), unit.repeat(1 << 18)].concat();
assert!(decode(&f, None).is_err());
}
#[test]
fn packets_longer_than_any_codec_uses_are_refused() {
let be = |v: &[u32]| v.iter().flat_map(|x| x.to_be_bytes()).collect::<Vec<u8>>();
let v2 = |frames: u32| [&be(&[72])[..], &48_000f64.to_be_bytes(), &be(&[1, 0x7f00_0000, 16, 4, 2, frames])].concat();
let lpcm = |frames: u32| sample_entry(b"lpcm", 2, &v2(frames), &[]);
let config = |frames: u32| [&be(&[0, frames])[..], &[0, 16, 40, 10, 14, 1, 0, 255], &be(&[0, 0, 48_000])].concat();
let alac = |frames: u32| sample_entry(b"alac", 0, &[], &atom(b"alac", &config(frames)));
let (_, buf) = decode(&mp4(480, &lpcm(1), |_, a| a), None).unwrap();
assert_eq!(buf.frames(), 480);
for entry in [lpcm(1 << 21), alac(1 << 21)] {
assert!(matches!(decode(&mp4(480, &entry, |_, a| a), None), Err(AudioError::Malformed(m)) if m.contains("declares packets")));
}
// Frames and channels multiply into the decoder's packet buffer; an ordinary stereo stream
// with long packets stays well inside the bound.
let wide = |channels: u32, frames: u32| {
let fields = [&be(&[72])[..], &48_000f64.to_be_bytes(), &be(&[channels, 0x7f00_0000, 16, 4, 2 * channels, frames])].concat();
sample_entry(b"lpcm", 2, &fields, &[])
};
assert!(decode(&mp4(480, &wide(2, 1 << 13), |_, a| a), None).is_ok());
}
#[test]
fn an_audio_timing_table_with_a_run_per_sample_is_bounded() {
let pcm = sample_entry(b"sowt", 0, &[], &[]);
// One stts run per sample: symphonia scans the runs from the start for every packet.
let runs = |n: u32| atom(b"stts", &[&[0; 4][..], &n.to_be_bytes(), &[1u32, 1].map(u32::to_be_bytes).concat().repeat(n as usize)[..]].concat());
let frames = 1 << 14;
let (_, buf) = decode(&mp4(frames, &pcm, |k, a| if k == b"stts" { runs(frames) } else { a }), None).unwrap();
assert_eq!(buf.frames(), frames as usize, "a table at the bound still decodes");
let f = mp4(frames + 1, &pcm, |k, a| if k == b"stts" { runs(frames + 1) } else { a });
assert!(matches!(decode(&f, None), Err(AudioError::Malformed(m)) if m.contains("timing table")), "one run past the bound is refused");
}
#[test]
fn the_audio_timing_bound_follows_the_sample_entry_not_the_handler() {
let pcm = sample_entry(b"sowt", 0, &[], &[]);
let runs = |n: u32| atom(b"stts", &[&[0; 4][..], &n.to_be_bytes(), &[1u32, 1].map(u32::to_be_bytes).concat().repeat(n as usize)[..]].concat());
// an audio sample entry under a video handler is still decoded as audio by symphonia
let video_handler = atom(b"hdlr", &[0u32, 0, u32::from_be_bytes(*b"vide"), 0, 0, 0, 0].map(u32::to_be_bytes).concat());
let frames = (1 << 14) + 1;
let f = mp4(frames, &pcm, |k, a| match k {
b"stts" => runs(frames),
b"hdlr" => video_handler.clone(),
_ => a,
});
assert!(matches!(decode(&f, None), Err(AudioError::Malformed(m)) if m.contains("timing table")), "the handler does not exempt an audio entry");
}
#[test]
fn the_last_sample_entry_decides_whether_the_timing_bound_applies() {
let pcm = sample_entry(b"sowt", 0, &[], &[]);
let video = sample_entry(b"avc1", 0, &[], &[]);
let runs = |n: u32| atom(b"stts", &[&[0; 4][..], &n.to_be_bytes(), &[1u32, 1].map(u32::to_be_bytes).concat().repeat(n as usize)[..]].concat());
let audio_stsd = atom(b"stsd", &[&[0, 0, 0, 0, 0, 0, 0, 1][..], &pcm].concat());
let frames = (1 << 14) + 1;
// A video `stsd` first and an audio one after it: symphonia keeps the last, so it decodes audio.
let f = mp4(frames, &video, |k, a| if k == b"stts" { [audio_stsd.clone(), runs(frames)].concat() } else { a });
assert!(matches!(decode(&f, None), Err(AudioError::Malformed(m)) if m.contains("timing table")), "the later audio entry counts");
}
#[test]
fn a_pcm_packet_longer_than_its_declared_frames_is_refused_before_decoding() {
let pcm = sample_entry(b"sowt", 0, &[], &[]);
// one `stsz` sample of 2 * 2^24 + 2 bytes (just over the packet bound) in a file that holds it
let big = 2 * (1usize << 24) + 2;
let stsz = atom(b"stsz", &[0u32, big as u32, 1].map(u32::to_be_bytes).concat());
let mut f = mp4(1, &pcm, |k, a| if k == b"stsz" { stsz.clone() } else { a });
f.resize(f.len() + big, 0);
assert!(matches!(decode(&f, None), Err(AudioError::Malformed(m)) if m.contains("packet holds far more samples")));
}
/// FLAC metadata blocks with field lengths exceeding their block boundary are refused as malformed.
#[test]
fn flac_metadata_fields_beyond_their_block_are_refused() {
let with_block = |kind: u8, body: &[u8]| {
let f = small(FileFormat::Flac, BitDepth::Int16, 1);
let head = ((0x80 | u32::from(kind)) << 24 | body.len() as u32).to_be_bytes();
[&f[..4], &[f[4] & 0x7f], &f[5..42], &head, body, &f[42..]].concat()
};
let picture = |data: u32| [&[0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0][..], &[0; 16], &data.to_be_bytes(), &[1, 2, 3]].concat();
assert_eq!(decode(&with_block(6, &picture(3)), None).unwrap().1.frames(), 300);
let comment = [&0u32.to_le_bytes()[..], &1u32.to_le_bytes(), &u32::MAX.to_le_bytes()].concat();
let vendor_overflow = [&0x7fff_fff0u32.to_le_bytes()[..], &0u32.to_le_bytes()].concat();
for f in [with_block(6, &picture(0x7fff_fff0)), with_block(4, &comment), with_block(4, &vendor_overflow)] {
assert!(matches!(decode(&f, None), Err(AudioError::Malformed(m)) if m.contains("larger than its block")));
}
}
#[test]
fn an_id3_tag_before_the_audio_is_skipped_unread() {
let frame = [&b"TIT2"[..], &0x7fff_ffffu32.to_be_bytes(), &[0, 0, 3, b'h', b'i', 0]].concat();
let tag = [&b"ID3\x03\0\0\0\0\0"[..], &[frame.len() as u8], &frame].concat();
let flac = small(FileFormat::Flac, BitDepth::Int16, 1);
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| {
let info = [&b"INFO"[..], b"INAM", &len.to_le_bytes(), b"hi"].concat();
riff(&[(b"fmt ", [fmt(6, 1, 8_000, 8), vec![0, 0]].concat()), (b"LIST", info), (b"data", vec![0xD5; 800])])
};
assert_eq!(decode(&alaw(2), None).unwrap().1.frames(), 800);
let mut f = alaw(0x7fff_fff0);
f[4..8].copy_from_slice(&u32::MAX.to_le_bytes());
let list = f.windows(4).position(|w| w == b"LIST").unwrap();
f[list + 4..list + 8].copy_from_slice(&0x7fff_fffcu32.to_le_bytes());
assert!(matches!(decode(&f, None), Err(AudioError::Malformed(m)) if m.contains("larger than the file")));
}
#[test]
fn a_wave_behind_another_riff_form_is_checked() {
let info = [&b"INFO"[..], b"INAM", &0x7fff_fff0u32.to_le_bytes(), b"hi"].concat();
let mut wav = riff(&[(b"fmt ", [fmt(6, 1, 8_000, 8), vec![0, 0]].concat()), (b"LIST", info), (b"data", vec![0xD5; 800])]);
wav[4..8].copy_from_slice(&u32::MAX.to_le_bytes());
let list = wav.windows(4).position(|w| w == b"LIST").unwrap();
wav[list + 4..list + 8].copy_from_slice(&0x7fff_fffcu32.to_le_bytes());
// The probe skips the AVI form and reads the WAVE behind it.
let f = [&b"RIFF"[..], &4u32.to_le_bytes(), b"AVI ", &wav].concat();
assert!(matches!(decode(&f, None), Err(AudioError::Malformed(m)) if m.contains("larger than the file")));
}
#[test]
fn wav_info_entries_are_found_where_symphonia_reads_them() {
let list = [&b"LIST"[..], &0x7fff_fffcu32.to_le_bytes(), b"INFO", b"INAM", &0x7fff_fff0u32.to_le_bytes(), b"hi"].concat();
let alaw = |extra: u16| (b"fmt ", [fmt(6, 1, 8_000, 8), extra.to_le_bytes().to_vec()].concat());
// A-law's extra bytes are read past its chunk; an odd list is padded twice.
let files =
[riff(&[alaw(12), (b"JUNK", [&[0; 4][..], &list].concat())]), [riff(&[alaw(0), (b"LIST", b"adtl\0".to_vec())]), vec![0], list].concat()];
for mut f in files {
f[4..8].copy_from_slice(&u32::MAX.to_le_bytes());
assert!(matches!(decode(&f, None), Err(AudioError::Malformed(m)) if m.contains("larger than the file")));
}
}
#[test]
fn many_tiny_chunks_terminate() {
let mut chunks: Vec<(&[u8; 4], Vec<u8>)> = vec![(b"fmt ", fmt(1, 1, 8_000, 16))];
for _ in 0..200_000 {
chunks.push((b"pad ", Vec::new()));
}
let wav = riff(&chunks);
assert!(decode(&wav, None).is_err()); // no data chunk within the scan limit
}
#[test]
fn hand_built_wav_variants() {
// 8-bit unsigned.
let wav = riff(&[(b"fmt ", fmt(1, 1, 8_000, 8)), (b"data", vec![0x80, 0xFF, 0x00])]);
let (info, buf) = decode(&wav, None).unwrap();
assert_eq!(info.sample_format, SampleFormat::Int8);
assert_eq!(buf.channels[0], vec![0.0, 127.0 / 128.0, -1.0]);
// 64-bit float, with NaN replaced by silence.
let mut data = Vec::new();
for v in [0.25f64, -0.5, f64::NAN] {
data.extend_from_slice(&v.to_le_bytes());
}
let wav = riff(&[(b"fmt ", fmt(3, 1, 8_000, 64)), (b"data", data)]);
let (info, buf) = decode(&wav, None).unwrap();
assert_eq!(info.sample_format, SampleFormat::Float64);
assert_eq!(buf.channels[0], vec![0.25, -0.5, 0.0]);
// Unsupported PCM width.
let wav = riff(&[(b"fmt ", fmt(3, 1, 8_000, 16)), (b"data", vec![0; 4])]);
assert!(matches!(decode(&wav, None), Err(AudioError::Unsupported(_))));
// No data chunk / no fmt chunk.
assert!(decode(&riff(&[(b"fmt ", fmt(1, 1, 8_000, 16))]), None).is_err());
assert!(decode(&riff(&[(b"data", vec![0; 4])]), None).is_err());
// Short fmt chunk.
assert!(decode(&riff(&[(b"fmt ", vec![1, 0, 1]), (b"data", vec![0; 4])]), None).is_err());
// Unknown codec tag goes to symphonia and fails cleanly.
let wav = riff(&[(b"fmt ", fmt(0x1234, 1, 8_000, 16)), (b"data", vec![0; 4])]);
assert!(decode(&wav, None).is_err());
}
#[test]
fn aiff_sowt_little_endian() {
let be = small(FileFormat::Aiff, BitDepth::Int16, 2);
let (_, expect) = decode(&be, None).unwrap();
// Rebuild as AIFC/sowt by hand.
let comm = be.windows(4).position(|w| w == b"COMM").unwrap();
let ssnd = be.windows(4).position(|w| w == b"SSND").unwrap();
let mut comm_body = be[comm + 8..comm + 26].to_vec();
comm_body.extend_from_slice(b"sowt");
comm_body.extend_from_slice(&[0, 0]); // empty pstring, padded
let mut ssnd_body = be[ssnd + 8..].to_vec();
for pair in ssnd_body[8..].as_chunks_mut::<2>().0 {
pair.swap(0, 1);
}
let mut body = b"AIFC".to_vec();
for (id, data) in [(b"COMM", &comm_body), (b"SSND", &ssnd_body)] {
body.extend_from_slice(id);
body.extend_from_slice(&(data.len() as u32).to_be_bytes());
body.extend_from_slice(data);
}
let mut f = b"FORM".to_vec();
f.extend_from_slice(&(body.len() as u32).to_be_bytes());
f.extend_from_slice(&body);
let (info, got) = decode(&f, None).unwrap();
assert_eq!(info.sample_format, SampleFormat::Int16);
assert_eq!(got, expect);
}
#[test]
fn detect_format_by_magic_and_hint() {
assert_eq!(detect_format(&small(FileFormat::Wav, BitDepth::Int16, 1), Some("mp3")), FileFormat::Wav);
assert_eq!(detect_format(&small(FileFormat::Aiff, BitDepth::Int16, 1), None), FileFormat::Aiff);
assert_eq!(detect_format(&small(FileFormat::Flac, BitDepth::Int16, 1), None), FileFormat::Flac);
assert_eq!(detect_format(b"OggS\0\x02rest", None), FileFormat::Ogg);
assert_eq!(detect_format(b"caff\0\x01\0\0", None), FileFormat::Caf);
assert_eq!(detect_format(b"ID3\x04\0\0\0\0\0\x00", None), FileFormat::Mp3);
let mut id3_flac = b"ID3\x04\0\0\0\0\0\x02xx".to_vec();
id3_flac.extend_from_slice(b"fLaC");
assert_eq!(detect_format(&id3_flac, None), FileFormat::Flac);
assert_eq!(detect_format(&[0xFF, 0xFB, 0x90, 0x00], None), FileFormat::Mp3);
assert_eq!(detect_format(&[0xFF, 0xF1, 0x50, 0x80], None), FileFormat::Aac);
let mut m4a = b"\0\0\0\x20ftypM4A ".to_vec();
m4a.extend_from_slice(b"....stsd\0\0\0\0\0\0\0\x01\0\0\0\x24alac");
assert_eq!(detect_format(&m4a, None), FileFormat::Alac);
assert_eq!(detect_format(b"\0\0\0\x20ftypM4A ", None), FileFormat::Aac);
assert_eq!(detect_format(b"", Some(".WAV")), FileFormat::Wav);
assert_eq!(detect_format(b"zz", Some("aif")), FileFormat::Aiff);
assert_eq!(detect_format(b"zz", Some("m4a")), FileFormat::Aac);
assert_eq!(detect_format(b"zz", Some("M4B")), FileFormat::Aac);
assert_eq!(detect_format(b"zz", Some("flac")), FileFormat::Flac);
assert_eq!(detect_format(b"zz", Some("xyz")), FileFormat::Other);
assert_eq!(detect_format(b"zz", None), FileFormat::Other);
assert_eq!(detect_format(b"", None), FileFormat::Other);
}
#[test]
fn garbage_in_compressed_containers_errors() {
let garbage: Vec<u8> = (0..4096u32).map(|i| (i.wrapping_mul(2_654_435_761) >> 13) as u8).collect();
for hint in [None, Some("mp3"), Some("ogg"), Some("flac"), Some("m4a"), Some("caf"), Some("aac"), Some("mkv"), Some("wav"), Some("aiff")] {
assert!(decode(&garbage, hint).is_err(), "{hint:?}");
assert!(probe(&garbage, hint).is_err(), "{hint:?}");
}
for magic in [&b"fLaC"[..], b"OggS", b"caff", b"ID3\x03\0\0\0\0\0\0", &[0xFF, 0xFB, 0x90, 0x00], b"\0\0\0\x20ftypM4A "] {
let mut f = magic.to_vec();
f.extend_from_slice(&garbage);
let _ = probe(&f, None);
assert!(decode(&f, None).map(|(_, b)| b.frames()).unwrap_or(0) < 1_000_000);
}
assert!(decode(&[], None).is_err());
}
/// A marker that passes its container check must not let the probe select a different container
/// without its own checks: the reader is refused when the probed format disagrees.
#[test]
fn a_probed_container_that_disagrees_with_the_checked_marker_is_refused() {
// The junk byte hides both magics, so the hint picks the marker: the FLAC is checked, but the
// probe reads the WAVE before it.
let bytes = [vec![0], small(FileFormat::Wav, BitDepth::Int24, 2), small(FileFormat::Flac, BitDepth::Int24, 2)].concat();
match decode(&bytes, Some("flac")) {
Err(AudioError::Malformed(message)) => assert!(message.contains("probed container"), "unexpected message: {message}"),
Err(other) => panic!("unexpected error kind: {other:?}"),
Ok(_) => panic!("the mismatched container must be refused"),
}
}
proptest! {
#![proptest_config(ProptestConfig { cases: 256, ..ProptestConfig::default() })]
#[test]
fn random_bytes_never_panic(bytes in proptest::collection::vec(any::<u8>(), 0..2048), hint in 0usize..6) {
let hint = [None, Some("wav"), Some("aiff"), Some("flac"), Some("mp3"), Some("ogg")][hint];
let _ = detect_format(&bytes, hint);
let _ = probe(&bytes, hint);
let _ = decode(&bytes, hint);
}
#[test]
fn random_bodies_behind_valid_headers_never_panic(body in proptest::collection::vec(any::<u8>(), 0..1024), which in 0usize..4) {
let mut f = match which {
0 => b"RIFF\xff\xff\xff\xffWAVE".to_vec(),
1 => b"RF64\xff\xff\xff\xffWAVE".to_vec(),
2 => b"FORM\xff\xff\xff\xffAIFC".to_vec(),
_ => b"FORM\xff\xff\xff\xffAIFF".to_vec(),
};
f.extend_from_slice(&body);
let _ = probe(&f, None);
let _ = decode(&f, None);
}
#[test]
fn random_audio_roundtrips_through_flac(samples in proptest::collection::vec(-1.2f32..1.2, 1..3000), channels in 1usize..4) {
let buf = AudioBuffer::from_interleaved(48_000, channels, &samples);
prop_assume!(buf.frames() > 0);
let opts = EncodeOptions { format: FileFormat::Flac, bit_depth: BitDepth::Int24, dither: false, bwf: None };
let (_, flac) = decode(&encode(&buf, &opts).unwrap(), None).unwrap();
let (_, wav) = decode(&encode(&buf, &EncodeOptions { format: FileFormat::Wav, ..opts }).unwrap(), None).unwrap();
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);
}
-61
View File
@@ -1,61 +0,0 @@
//! The length `probe` reports for AAC in MP4 is the length `decode` produces (issue #1113).
use soundcraft_audio_io::{decode, probe};
fn atom(kind: &[u8; 4], body: &[u8]) -> Vec<u8> {
[&(8 + body.len() as u32).to_be_bytes()[..], kind, body].concat()
}
/// A full atom (version 0, no flags) whose body is `fields` as big-endian u32s.
fn full(kind: &[u8; 4], fields: &[u32]) -> Vec<u8> {
let body: Vec<u8> = [0].iter().chain(fields).flat_map(|v: &u32| v.to_be_bytes()).collect();
atom(kind, &body)
}
/// An MPEG-4 descriptor with a one-byte length.
fn descriptor(tag: u8, body: &[u8]) -> Vec<u8> {
[&[tag, body.len() as u8][..], body].concat()
}
/// A silent AAC-LC raw frame for one channel: an SCE with `max_sfb` 0, then ID_END.
const SILENT_SCE: [u8; 4] = [0x00, 0xC8, 0x00, 0x07];
/// A mono 48 kHz AAC-LC MP4 without an edit list: `packets` frames, the last lasting `last_dur` in stts.
fn aac_mp4(packets: u32, last_dur: u32) -> Vec<u8> {
// AudioSpecificConfig: AAC-LC, 48 kHz, one channel.
let dsi = descriptor(5, &[0x11, 0x88]);
let dcd = descriptor(4, &[&[0x40, 0x15, 0, 0, 0][..], &[0; 8], &dsi].concat());
let es = descriptor(3, &[&[0, 1, 0][..], &dcd, &descriptor(6, &[2])].concat());
let esds = atom(b"esds", &[&[0; 4][..], &es].concat());
let base = [0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 16, 0, 0, 0, 0];
let entry = atom(b"mp4a", &[&base[..], &(48_000u32 << 16).to_be_bytes(), &esds].concat());
let total = (packets - 1) * 1024 + last_dur;
let moov = |offset: u32| {
let stsd = atom(b"stsd", &[&[0, 0, 0, 0, 0, 0, 0, 1][..], &entry].concat());
let stbl = [
stsd,
full(b"stts", &[2, packets - 1, 1024, 1, last_dur]),
full(b"stsc", &[1, 1, packets, 1]),
full(b"stsz", &[SILENT_SCE.len() as u32, packets]),
full(b"stco", &[1, offset]),
];
let minf = [full(b"smhd", &[0]), atom(b"stbl", &stbl.concat())].concat();
let mdia = [full(b"mdhd", &[0, 0, 48_000, total, 0]), full(b"hdlr", &[0, u32::from_be_bytes(*b"soun"), 0, 0, 0, 0]), atom(b"minf", &minf)];
let trak = [full(b"tkhd", &[0; 20]), atom(b"mdia", &mdia.concat())].concat();
atom(b"moov", &[full(b"mvhd", &[[0, 0, 48_000, total, 1 << 16].as_slice(), &[0; 18], &[2]].concat()), atom(b"trak", &trak)].concat())
};
let ftyp = atom(b"ftyp", b"M4A \0\0\0\0M4A isom");
let offset = (ftyp.len() + moov(0).len() + 8) as u32;
[ftyp, moov(offset), atom(b"mdat", &SILENT_SCE.repeat(packets as usize))].concat()
}
#[test]
fn aac_in_mp4_probe_length_matches_the_decoded_length() {
// A last sample lasting until the track end (longer than a packet), and a control with a short one.
for last_dur in [1145, 546] {
let file = aac_mp4(10, last_dur);
let (_, buf) = decode(&file, Some("m4a")).unwrap();
assert_eq!(buf.frames(), 10 * 1024, "decoded frames, last stts duration {last_dur}");
assert_eq!(probe(&file, Some("m4a")).unwrap().frames, buf.frames() as u64, "probed frames, last stts duration {last_dur}");
}
}
-328
View File
@@ -1,328 +0,0 @@
use soundcraft_audio_io::{
AudioBuffer, BitDepth, BwfInfo, EncodeOptions, FileFormat, SampleFormat, decode, detect_format, encode, encode_wav_rf64, probe, wav_float_header,
};
fn sine(sr: u32, channels: usize, frames: usize) -> AudioBuffer {
let mut b = AudioBuffer::new(sr, channels, frames);
for (c, ch) in b.channels.iter_mut().enumerate() {
let f = 220.0 * (c as f32 + 1.0);
for (i, s) in ch.iter_mut().enumerate() {
*s = 0.8 * (2.0 * std::f32::consts::PI * f * i as f32 / sr as f32 + c as f32).sin();
}
}
b
}
fn tolerance(depth: BitDepth, dither: bool) -> f32 {
let lsb = match depth {
BitDepth::Int16 => 1.0 / 32768.0,
BitDepth::Int24 => 1.0 / 8_388_608.0,
BitDepth::Int32 => 1e-7,
BitDepth::Float32 => 0.0,
};
lsb * if dither { 1.6 } else { 0.6 } + 1e-7
}
fn max_err(a: &AudioBuffer, b: &AudioBuffer) -> f32 {
assert_eq!(a.num_channels(), b.num_channels());
assert_eq!(a.frames(), b.frames());
a.channels.iter().zip(&b.channels).flat_map(|(x, y)| x.iter().zip(y).map(|(p, q)| (p - q).abs())).fold(0.0, f32::max)
}
const DEPTHS: [BitDepth; 4] = [BitDepth::Int16, BitDepth::Int24, BitDepth::Int32, BitDepth::Float32];
fn expected_sf(d: BitDepth) -> SampleFormat {
match d {
BitDepth::Int16 => SampleFormat::Int16,
BitDepth::Int24 => SampleFormat::Int24,
BitDepth::Int32 => SampleFormat::Int32,
BitDepth::Float32 => SampleFormat::Float32,
}
}
#[test]
fn wav_and_aiff_roundtrip_all_depths_and_layouts() {
for format in [FileFormat::Wav, FileFormat::Aiff] {
for depth in DEPTHS {
for channels in [1, 2, 6] {
for dither in [false, true] {
let src = sine(48_000, channels, 4801);
let opts = EncodeOptions { format, bit_depth: depth, dither, bwf: None };
let bytes = encode(&src, &opts).unwrap();
assert_eq!(detect_format(&bytes, None), format);
let info = probe(&bytes, None).unwrap();
assert_eq!(info.format, format);
assert_eq!(info.sample_rate, 48_000);
assert_eq!(info.channels as usize, channels);
assert_eq!(info.frames, 4801);
assert_eq!(info.sample_format, expected_sf(depth));
let (dinfo, out) = decode(&bytes, None).unwrap();
assert_eq!(dinfo, info);
assert_eq!(out.sample_rate, 48_000);
let err = max_err(&src, &out);
assert!(err <= tolerance(depth, dither), "{format:?} {depth:?} {channels}ch dither={dither}: err {err}");
}
}
}
}
}
#[test]
fn flac_roundtrip_is_lossless_vs_wav() {
for depth in [BitDepth::Int16, BitDepth::Int24, BitDepth::Int32] {
for channels in [1, 2, 6] {
// Spans several blocks plus a short final block.
let src = sine(44_100, channels, 4096 * 2 + 17);
let wav = encode(&src, &EncodeOptions { format: FileFormat::Wav, bit_depth: depth, dither: false, bwf: None }).unwrap();
let flac = encode(&src, &EncodeOptions { format: FileFormat::Flac, bit_depth: depth, dither: false, bwf: None }).unwrap();
assert_eq!(detect_format(&flac, None), FileFormat::Flac);
let info = probe(&flac, None).unwrap();
assert_eq!(info.format, FileFormat::Flac);
assert_eq!((info.sample_rate, info.channels as usize, info.frames), (44_100, channels, src.frames() as u64));
assert_eq!(info.sample_format, expected_sf(depth));
let (_, from_wav) = decode(&wav, None).unwrap();
let (finfo, from_flac) = decode(&flac, None).unwrap();
assert_eq!(finfo.frames, src.frames() as u64);
assert_eq!(from_flac, from_wav, "{depth:?} {channels}ch");
if depth != BitDepth::Int32 {
assert!(flac.len() < wav.len(), "{depth:?} {channels}ch: flac {} >= wav {}", flac.len(), wav.len());
}
}
}
}
#[test]
fn flac_handles_silence_noise_full_scale_and_odd_rates() {
let frames = 10_000;
let mut b = AudioBuffer::new(37_123, 3, frames);
let mut seed = 1u32;
for i in 0..frames {
seed = seed.wrapping_mul(1_664_525).wrapping_add(1_013_904_223);
b.channels[1][i] = (seed as f32 / u32::MAX as f32) * 2.0 - 1.0; // white noise
b.channels[2][i] = if i % 2 == 0 { 1.5 } else { -1.5 }; // clipped full-scale square
}
for depth in [BitDepth::Int16, BitDepth::Int24, BitDepth::Int32] {
for dither in [false, true] {
let opts = EncodeOptions { format: FileFormat::Flac, bit_depth: depth, dither, bwf: None };
let flac = encode(&b, &opts).unwrap();
let (info, out) = decode(&flac, None).unwrap();
assert_eq!(info.sample_rate, 37_123);
let wav = encode(&b, &EncodeOptions { format: FileFormat::Wav, ..opts }).unwrap();
let (_, expect) = decode(&wav, None).unwrap();
assert_eq!(out, expect, "{depth:?} dither={dither}");
}
}
}
#[test]
fn flac_rejects_unsupported_layouts() {
let opts = EncodeOptions { format: FileFormat::Flac, bit_depth: BitDepth::Float32, dither: false, bwf: None };
assert!(encode(&sine(48_000, 2, 10), &opts).is_err());
let opts = EncodeOptions { bit_depth: BitDepth::Int16, ..opts };
assert!(encode(&sine(48_000, 9, 10), &opts).is_err());
assert!(encode(&sine(700_000, 1, 10), &opts).is_err());
// Tiny buffers still encode and decode.
let one = sine(48_000, 2, 1);
let (_, out) = decode(&encode(&one, &opts).unwrap(), None).unwrap();
assert_eq!(out.frames(), 1);
}
#[test]
fn bwf_roundtrip() {
let bwf = BwfInfo {
description: "Scene 12 take 3".into(),
originator: "SoundCraft".into(),
originator_reference: "SC-0001".into(),
origination_date: "2026-10-06".into(),
origination_time: "12:34:56".into(),
time_reference: 0x1_2345_6789, // > 32 bits
};
let src = sine(96_000, 2, 1000);
for depth in DEPTHS {
let opts = EncodeOptions { format: FileFormat::Wav, bit_depth: depth, dither: false, bwf: Some(bwf.clone()) };
let bytes = encode(&src, &opts).unwrap();
let info = probe(&bytes, None).unwrap();
assert_eq!(info.bwf.as_ref(), Some(&bwf));
let (dinfo, _) = decode(&bytes, None).unwrap();
assert_eq!(dinfo.bwf, Some(bwf.clone()));
// RF64 layout reads back identically.
let rf64 = encode_wav_rf64(&src, &opts).unwrap();
assert_eq!(&rf64[..4], b"RF64");
let (rinfo, rbuf) = decode(&rf64, None).unwrap();
assert_eq!(rinfo.bwf, Some(bwf.clone()));
assert_eq!(rinfo.frames, 1000);
assert_eq!(rbuf, decode(&bytes, None).unwrap().1);
}
// Overlong strings are truncated to the field width, not an error.
let long = BwfInfo { description: "x".repeat(400), ..BwfInfo::default() };
let bytes = encode(&src, &EncodeOptions { bwf: Some(long), ..EncodeOptions::default() }).unwrap();
assert_eq!(probe(&bytes, None).unwrap().bwf.unwrap().description.len(), 256);
}
#[test]
fn wav_header_layout() {
let b = encode(&sine(44_100, 2, 10), &EncodeOptions { bit_depth: BitDepth::Int16, ..EncodeOptions::default() }).unwrap();
// Plain PCM for 16-bit stereo; extensible otherwise.
let fmt = b.windows(4).position(|w| w == b"fmt ").unwrap();
assert_eq!(u16::from_le_bytes([b[fmt + 8], b[fmt + 9]]), 1);
let b = encode(&sine(44_100, 2, 10), &EncodeOptions::default()).unwrap();
let fmt = b.windows(4).position(|w| w == b"fmt ").unwrap();
assert_eq!(u16::from_le_bytes([b[fmt + 8], b[fmt + 9]]), 0xFFFE);
let riff = u32::from_le_bytes([b[4], b[5], b[6], b[7]]) as usize;
assert_eq!(riff + 8, b.len());
// Odd data length (24-bit mono, odd frames) gets a pad byte.
let b = encode(&sine(44_100, 1, 3), &EncodeOptions::default()).unwrap();
assert_eq!(b.len() % 2, 0);
assert_eq!(decode(&b, None).unwrap().1.frames(), 3);
}
#[test]
fn aiff_float_is_aifc() {
let b =
encode(&sine(44_100, 2, 10), &EncodeOptions { format: FileFormat::Aiff, bit_depth: BitDepth::Float32, ..EncodeOptions::default() }).unwrap();
assert_eq!(&b[8..12], b"AIFC");
assert!(b.windows(4).any(|w| w == b"fl32"));
let b = encode(&sine(44_100, 2, 10), &EncodeOptions { format: FileFormat::Aiff, ..EncodeOptions::default() }).unwrap();
assert_eq!(&b[8..12], b"AIFF");
let form = u32::from_be_bytes([b[4], b[5], b[6], b[7]]) as usize;
assert_eq!(form + 8, b.len());
}
#[test]
fn unusual_sample_rates_roundtrip() {
for sr in [1, 8_000, 11_025, 22_050, 44_100, 88_200, 176_400, 192_000, 352_800, 384_000, 768_000] {
for format in [FileFormat::Wav, FileFormat::Aiff] {
let bytes = encode(&sine(sr, 1, 8), &EncodeOptions { format, ..EncodeOptions::default() }).unwrap();
assert_eq!(probe(&bytes, None).unwrap().sample_rate, sr, "{format:?}");
}
}
}
#[test]
fn encode_rejects_bad_buffers_and_formats() {
assert!(encode(&AudioBuffer::default(), &EncodeOptions::default()).is_err());
assert!(encode(&AudioBuffer::new(0, 2, 10), &EncodeOptions::default()).is_err());
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 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] {
for depth in DEPTHS {
if format == FileFormat::Flac && depth == BitDepth::Float32 {
continue;
}
let bytes = encode(&b, &EncodeOptions { format, bit_depth: depth, ..EncodeOptions::default() }).unwrap();
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], if depth == BitDepth::Float32 { -5.0 } else { -1.0 });
}
}
// Ragged buffers encode the common length.
let ragged = AudioBuffer { sample_rate: 48_000, channels: vec![vec![0.1; 10], vec![0.2; 7]] };
let (_, out) = decode(&encode(&ragged, &EncodeOptions::default()).unwrap(), None).unwrap();
assert_eq!(out.frames(), 7);
}
#[test]
fn audio_buffer_helpers() {
let b = AudioBuffer::from_interleaved(48_000, 2, &[0.1, -0.2, 0.3, -0.4, 0.5]);
assert_eq!(b.frames(), 2);
assert_eq!(b.num_channels(), 2);
assert_eq!(b.interleaved(), vec![0.1, -0.2, 0.3, -0.4]);
assert!((b.peak() - 0.4).abs() < 1e-7);
assert!((b.duration_secs() - 2.0 / 48_000.0).abs() < 1e-12);
assert_eq!(AudioBuffer::from_interleaved(1, 0, &[1.0]).num_channels(), 0);
assert_eq!(AudioBuffer::new(0, 1, 5).duration_secs(), 0.0);
assert_eq!(AudioBuffer::default().frames(), 0);
}
#[test]
fn serde_types_roundtrip() {
let opts = EncodeOptions { bwf: Some(BwfInfo { time_reference: 42, ..BwfInfo::default() }), ..EncodeOptions::default() };
let s = serde_json::to_string(&opts).unwrap();
assert_eq!(serde_json::from_str::<EncodeOptions>(&s).unwrap(), opts);
let info = probe(&encode(&sine(48_000, 1, 4), &opts).unwrap(), None).unwrap();
let s = serde_json::to_string(&info).unwrap();
assert_eq!(serde_json::from_str::<soundcraft_audio_io::AudioInfo>(&s).unwrap(), info);
}
#[test]
fn multichannel_wav_speaker_masks() {
let buf = |n: usize| AudioBuffer { sample_rate: 48_000, channels: (0..n).map(|c| vec![0.01 * c as f32; 64]).collect() };
let opts = EncodeOptions { format: FileFormat::Wav, bit_depth: BitDepth::Int24, dither: false, bwf: None };
for (n, mask) in [(6usize, 0x3Fu32), (8, 0x63F), (12, 0x2_D63F)] {
let bytes = soundcraft_audio_io::encode(&buf(n), &opts).unwrap();
assert_eq!(soundcraft_audio_io::wav_channel_mask(&bytes), Some(mask), "{n} channels");
assert_eq!(soundcraft_audio_io::decode(&bytes, Some("wav")).unwrap().1.channels.len(), n);
}
// Explicit masks: 7.0 (sides + rears, no LFE) and Ambisonics (no positions).
let bytes = soundcraft_audio_io::encode_with_channel_mask(&buf(7), &opts, 0x637).unwrap();
assert_eq!(soundcraft_audio_io::wav_channel_mask(&bytes), Some(0x637));
let bytes = soundcraft_audio_io::encode_with_channel_mask(&buf(4), &opts, 0).unwrap();
assert_eq!(soundcraft_audio_io::wav_channel_mask(&bytes), Some(0));
// Plain 16-bit stereo stays a plain PCM header.
let o16 = EncodeOptions { bit_depth: BitDepth::Int16, ..opts };
let bytes = soundcraft_audio_io::encode_with_channel_mask(&buf(2), &o16, 0x3).unwrap();
assert_eq!(soundcraft_audio_io::wav_channel_mask(&bytes), None);
}
#[test]
fn float_wav_header_is_rewritten_in_place_as_the_file_grows() {
let take = sine(48_000, 2, 1_000);
let mut file = wav_float_header(2, 48_000, 0).unwrap();
let len = file.len();
file.extend(take.interleaved().iter().flat_map(|s| s.to_le_bytes()));
assert_eq!(decode(&file, None).unwrap().1.frames(), 1_000);
file[..len].copy_from_slice(&wav_float_header(2, 48_000, 600).unwrap());
assert_eq!(decode(&file, None).unwrap().1.frames(), 600);
file[..len].copy_from_slice(&wav_float_header(2, 48_000, 1_000).unwrap());
assert_eq!(file, encode(&take, &EncodeOptions { bit_depth: BitDepth::Float32, ..EncodeOptions::default() }).unwrap());
let big = wav_float_header(2, 48_000, 600_000_000).unwrap();
assert_eq!((&big[..4], big.len()), (&b"RF64"[..], len));
assert!(wav_float_header(0, 48_000, 0).is_err());
}
#[test]
fn zero_frame_flac_round_trips_like_wav_and_aiff() {
for format in [FileFormat::Wav, FileFormat::Aiff, FileFormat::Flac] {
for depth in [BitDepth::Int16, BitDepth::Int24] {
for channels in [1, 2] {
let opts = EncodeOptions { format, bit_depth: depth, ..EncodeOptions::default() };
let bytes = encode(&AudioBuffer::new(48_000, channels, 0), &opts).unwrap();
let info = probe(&bytes, None).unwrap();
assert_eq!((info.format, info.sample_format, info.sample_rate, info.frames), (format, expected_sf(depth), 48_000, 0));
assert_eq!(usize::from(info.channels), channels);
let (info, buf) = decode(&bytes, None).unwrap();
assert_eq!((info.frames, buf.frames(), buf.num_channels()), (0, 0, channels));
}
}
}
}
#[test]
fn flac_claiming_frames_but_holding_none_still_errors() {
let opts = EncodeOptions { format: FileFormat::Flac, bit_depth: BitDepth::Int16, ..EncodeOptions::default() };
let mut bytes = encode(&AudioBuffer::new(48_000, 2, 0), &opts).unwrap();
bytes[25] = 1; // STREAMINFO total samples (low byte of the 36-bit field) = 1
assert!(decode(&bytes, None).is_err());
}
#[test]
fn float_wav_header_rejects_a_frame_count_whose_total_size_overflows() {
// 4 bytes per frame: the data length fits in u64, but adding the header to it does not.
let frames = u64::MAX / 4;
assert!(matches!(wav_float_header(1, 48_000, frames), Err(soundcraft_audio_io::AudioError::TooLarge(_))));
}
#[test]
fn bwf_truncation_keeps_utf8_boundaries() {
let src = sine(48_000, 1, 10);
let bwf = BwfInfo { originator: format!("{}é", "a".repeat(31)), description: format!("{}é", "d".repeat(255)), ..BwfInfo::default() };
let bytes = encode(&src, &EncodeOptions { format: FileFormat::Wav, bwf: Some(bwf), ..EncodeOptions::default() }).unwrap();
let out = probe(&bytes, None).unwrap().bwf.unwrap();
assert_eq!(out.originator, "a".repeat(31));
assert_eq!(out.description, "d".repeat(255));
}
-110
View File
@@ -1,110 +0,0 @@
use soundcraft_audio_io::{AudioBuffer, AudioError, BitDepth, BwfInfo, EncodeOptions, FileEncoder, FileFormat, encode, encode_with_channel_mask};
use std::io::{Cursor, Seek, SeekFrom, Write};
fn tone(channels: usize, frames: usize) -> AudioBuffer {
let channels = (0..channels).map(|c| (0..frames).map(|i| ((i * (c + 3)) as f32 * 0.013).sin() * 1.2).collect()).collect();
AudioBuffer { sample_rate: 44_100, channels }
}
fn streamed(buf: &AudioBuffer, opts: &EncodeOptions, mask: Option<u32>) -> Vec<u8> {
let mut out = Cursor::new(b"pre".to_vec());
out.set_position(3);
let mut enc = FileEncoder::new(out, opts, buf.channels.len(), buf.sample_rate, mask).unwrap();
let mut start = 0;
for len in [1, 7, 4095, 4097, 333].into_iter().cycle() {
if start >= buf.frames() {
break;
}
let len = len.min(buf.frames() - start);
let block: Vec<Vec<f32>> = buf.channels.iter().map(|c| c[start..start + len].to_vec()).collect();
enc.write(&block, len).unwrap();
start += len;
}
enc.finish().unwrap().into_inner()
}
#[test]
fn streamed_files_match_whole_buffer_encodes() {
let bwf = BwfInfo { description: "take".into(), time_reference: 123_456, ..Default::default() };
let cases = [
(FileFormat::Wav, BitDepth::Int24, true, None, None),
(FileFormat::Wav, BitDepth::Float32, false, None, None),
(FileFormat::Wav, BitDepth::Int16, true, Some(bwf), Some(0x60F)),
(FileFormat::Aiff, BitDepth::Int24, true, None, None),
(FileFormat::Aiff, BitDepth::Float32, false, None, None),
(FileFormat::Flac, BitDepth::Int16, true, None, None),
(FileFormat::Flac, BitDepth::Int24, false, None, None),
];
for (format, bit_depth, dither, bwf, mask) in cases {
let opts = EncodeOptions { format, bit_depth, dither, bwf };
for channels in [1, 6] {
let buf = tone(channels, 20_001);
let whole = match mask {
Some(m) => encode_with_channel_mask(&buf, &opts, m),
None => encode(&buf, &opts),
}
.unwrap();
assert_eq!(streamed(&buf, &opts, mask), [b"pre".as_slice(), &whole].concat(), "{opts:?}, {channels} channels");
}
}
}
/// A `Write + Seek` sink that accepts `budget` bytes in total and then fails every write, so
/// a `write_all` on it appends a prefix of the block and only then reports the error.
struct FailAfter {
inner: Cursor<Vec<u8>>,
budget: usize,
}
impl Write for FailAfter {
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
if self.budget == 0 {
return Err(std::io::Error::other("sink is full"));
}
let n = buf.len().min(self.budget);
self.budget -= n;
self.inner.write(&buf[..n])
}
fn flush(&mut self) -> std::io::Result<()> {
self.inner.flush()
}
}
impl Seek for FailAfter {
fn seek(&mut self, pos: SeekFrom) -> std::io::Result<u64> {
self.inner.seek(pos)
}
}
#[test]
fn a_failed_write_poisons_the_encoder() {
let buf = tone(2, 20_001);
for format in [FileFormat::Wav, FileFormat::Aiff, FileFormat::Flac] {
let opts = EncodeOptions { format, bit_depth: BitDepth::Int16, dither: false, bwf: None };
let sink = FailAfter { inner: Cursor::new(Vec::new()), budget: 256 };
let mut enc = FileEncoder::new(sink, &opts, 2, buf.sample_rate, None).unwrap();
// The sink accepts a prefix of the first block, then fails.
assert!(enc.write(&buf.channels, buf.frames()).is_err(), "{format:?}");
// Later calls must not claim success: the bytes on the sink no longer match the
// encoder's counters (and for FLAC the failed frame's samples are already gone).
assert!(matches!(enc.write(&buf.channels, 16), Err(AudioError::Encode(_))), "{format:?}");
assert!(matches!(enc.finish(), Err(AudioError::Encode(_))), "{format:?}");
}
}
#[test]
fn blocks_past_a_format_limit_are_refused_before_anything_is_written() {
let buf = tone(2, 100);
for (format, limit) in [(FileFormat::Aiff, 1u64 << 30), (FileFormat::Flac, 1u64 << 36)] {
let opts = EncodeOptions { format, bit_depth: BitDepth::Int16, dither: false, bwf: None };
let mut enc = FileEncoder::new(Cursor::new(Vec::new()), &opts, 2, buf.sample_rate, None).unwrap();
enc.write(&buf.channels, 100).unwrap();
assert!(enc.check_room(limit / 2).is_ok(), "{format:?}");
assert!(matches!(enc.check_room(limit), Err(AudioError::TooLarge(_))), "{format:?}");
if let Ok(frames) = usize::try_from(limit) {
assert!(matches!(enc.write(&[], frames), Err(AudioError::TooLarge(_))), "{format:?}");
}
assert_eq!(enc.finish().unwrap().into_inner(), encode(&buf, &opts).unwrap());
}
}
@@ -1,87 +0,0 @@
//! A WAV whose data chunk is empty and followed by metadata decodes to no audio; a recording
//! that was never finalised (RIFF and data sizes left at 0) still yields what was written (#466).
use soundcraft_audio_io::{decode, probe};
fn fmt16(channels: u16, sr: u32) -> Vec<u8> {
let align = channels * 2;
let mut f = Vec::new();
f.extend_from_slice(&1u16.to_le_bytes());
f.extend_from_slice(&channels.to_le_bytes());
f.extend_from_slice(&sr.to_le_bytes());
f.extend_from_slice(&(sr * u32::from(align)).to_le_bytes());
f.extend_from_slice(&align.to_le_bytes());
f.extend_from_slice(&16u16.to_le_bytes());
f
}
fn chunk(id: &[u8; 4], data: &[u8]) -> Vec<u8> {
let mut c = id.to_vec();
c.extend_from_slice(&(data.len() as u32).to_le_bytes());
c.extend_from_slice(data);
if data.len() % 2 == 1 {
c.push(0);
}
c
}
/// A finished file: correct RIFF size, chunks as given.
fn riff(chunks: &[Vec<u8>]) -> Vec<u8> {
let mut body = b"WAVE".to_vec();
for c in chunks {
body.extend_from_slice(c);
}
let mut out = b"RIFF".to_vec();
out.extend_from_slice(&(body.len() as u32).to_le_bytes());
out.extend_from_slice(&body);
out
}
fn list_info() -> Vec<u8> {
let mut l = b"INFO".to_vec();
l.extend_from_slice(&chunk(b"INAM", b"Silence"));
chunk(b"LIST", &l)
}
#[test]
fn an_empty_data_chunk_followed_by_metadata_has_no_frames() {
let file = riff(&[chunk(b"fmt ", &fmt16(2, 48_000)), chunk(b"data", &[]), list_info(), chunk(b"bext", &[0u8; 602])]);
let info = probe(&file, Some("wav")).unwrap();
assert_eq!(info.frames, 0, "the LIST and bext chunks are not samples");
let (info, buf) = decode(&file, Some("wav")).unwrap();
assert_eq!((info.frames, buf.frames()), (0, 0));
// The same with the metadata first.
let file = riff(&[chunk(b"fmt ", &fmt16(1, 44_100)), list_info(), chunk(b"data", &[])]);
assert_eq!(probe(&file, Some("wav")).unwrap().frames, 0);
// And with a single trailing chunk of odd size and no pad byte at the very end.
let mut file = riff(&[chunk(b"fmt ", &fmt16(1, 44_100)), chunk(b"data", &[]), chunk(b"note", b"abc")]);
file.pop();
let riff_size = (file.len() - 8) as u32;
file[4..8].copy_from_slice(&riff_size.to_le_bytes());
assert_eq!(probe(&file, Some("wav")).unwrap().frames, 0);
}
#[test]
fn a_recording_that_was_never_finalised_still_gives_its_audio() {
let samples: Vec<u8> = (0..400i16).flat_map(|i| (i * 40).to_le_bytes()).collect();
// RIFF size and data size both left at 0, as a recorder writes them until it finishes.
let mut file = riff(&[chunk(b"fmt ", &fmt16(2, 48_000)), chunk(b"data", &[])]);
file.extend_from_slice(&samples);
file[4..8].copy_from_slice(&0u32.to_le_bytes());
let (info, buf) = decode(&file, Some("wav")).unwrap();
assert_eq!(info.frames, 200);
assert_eq!(buf.frames(), 200);
// Even if the RIFF size is the file's, audio that is not a run of chunks is still audio.
let mut file = riff(&[chunk(b"fmt ", &fmt16(2, 48_000)), chunk(b"data", &[])]);
file.extend_from_slice(&samples);
let riff_size = (file.len() - 8) as u32;
file[4..8].copy_from_slice(&riff_size.to_le_bytes());
assert_eq!(probe(&file, Some("wav")).unwrap().frames, 200);
}
#[test]
fn a_data_chunk_with_audio_is_unchanged() {
let samples: Vec<u8> = (0..100i16).flat_map(|i| i.to_le_bytes()).collect();
let file = riff(&[chunk(b"fmt ", &fmt16(1, 44_100)), chunk(b"data", &samples), list_info()]);
assert_eq!(probe(&file, Some("wav")).unwrap().frames, 100);
}
+1 -1
View File
@@ -9,7 +9,7 @@ repository.workspace = true
[dependencies]
soundcraft-engine.workspace = true
soundcraft-audio-io.workspace = true
craft-audio.workspace = true
soundcraft-time.workspace = true
serde_json.workspace = true
log.workspace = true
+340 -25
View File
@@ -136,14 +136,28 @@ pub enum RemoteError {
/// since the app may already have executed it.
#[error("{source}; `{method}` may or may not have been executed, check the session before repeating it")]
Unconfirmed { method: String, source: Box<RemoteError> },
/// The reply answers another request, so it isn't returned.
#[error("the reply does not answer this request (its id is {got}, not {expected})")]
OutOfStep { expected: u64, got: String },
}
impl RemoteError {
/// The connection went away, so a fresh one may succeed.
/// The connection went away, or what it carries next can't be trusted (no reply in time, a
/// reply that isn't JSON or answers another request): it is dropped, and a fresh one may succeed.
fn is_lost_connection(&self) -> bool {
match self {
RemoteError::Closed => true,
RemoteError::Io(e) => matches!(e.kind(), io::ErrorKind::BrokenPipe | io::ErrorKind::ConnectionReset | io::ErrorKind::ConnectionAborted),
RemoteError::Closed | RemoteError::Reply(_) | RemoteError::OutOfStep { .. } => true,
RemoteError::Io(e) => matches!(
e.kind(),
io::ErrorKind::BrokenPipe
| io::ErrorKind::ConnectionReset
| io::ErrorKind::ConnectionAborted
// A read that timed out: `TimedOut` on Windows, `WouldBlock` on Unix.
| io::ErrorKind::TimedOut
| io::ErrorKind::WouldBlock
// A reply that isn't UTF-8: `read_line` fails before there is JSON to parse.
| io::ErrorKind::InvalidData
),
_ => false,
}
}
@@ -157,19 +171,25 @@ pub struct Remote {
addr: String,
conn: Option<Connection>,
next_id: u64,
/// How long a request waits for its reply.
read_timeout: Duration,
}
/// The app answers within 90 s (it replies `timeout` itself after that), so a longer silence
/// means the reply is lost or late.
const READ_TIMEOUT: Duration = Duration::from_secs(120);
impl Remote {
/// A client for the app at `target`; it connects on the first request.
pub fn new(target: &ControlTarget) -> Self {
Remote { addr: target.to_string(), conn: None, next_id: 1 }
Remote { addr: target.to_string(), conn: None, next_id: 1, read_timeout: READ_TIMEOUT }
}
/// The open connection, connecting first when there is none.
fn connection(&mut self) -> Result<&mut Connection, RemoteError> {
match self.conn {
Some(ref mut conn) => Ok(conn),
None => Ok(self.conn.insert(connect(&self.addr)?)),
None => Ok(self.conn.insert(connect(&self.addr, self.read_timeout)?)),
}
}
@@ -187,38 +207,60 @@ impl Remote {
return Err((RemoteError::Closed, true));
}
let v: Value = serde_json::from_str(&buf).map_err(|e| (e.into(), true))?;
if v.get("ok").and_then(Value::as_bool).unwrap_or(false) {
Ok(v.get("result").cloned().unwrap_or(Value::Null))
} else {
Err((RemoteError::App(v.get("error").and_then(Value::as_str).unwrap_or("error").to_string()), true))
let ok = v.get("ok").and_then(Value::as_bool).unwrap_or(false);
let reply_id = v.get("id").filter(|id| !id.is_null());
// The app answers a request with its id. A reply with another id answers another request (a
// late reply to one that timed out), and so does a success with none.
if reply_id.is_some_and(|got| got.as_u64() != Some(id)) || (ok && reply_id.is_none()) {
let got = reply_id.map_or_else(|| "none".to_owned(), Value::to_string);
return Err((RemoteError::OutOfStep { expected: id, got }, true));
}
if ok {
return Ok(v.get("result").cloned().unwrap_or(Value::Null));
}
if reply_id.is_none() {
// A refusal with no id (too many connections, a line the app can't read) is the app's
// last word on this connection: it closes it.
self.conn = None;
}
Err((RemoteError::App(v.get("error").and_then(Value::as_str).unwrap_or("error").to_string()), true))
}
/// Calls `method`. A request lost with its connection is sent once more on a fresh one when
/// it only reads state.
/// it only reads state, or when it never went out. A connection that is lost or out of step
/// is never used again, so a reply that arrives late on it is not read as the answer to a
/// later call.
pub fn request(&mut self, method: &str, params: Value) -> Result<Value, RemoteError> {
match self.roundtrip(method, &params) {
Ok(v) => Ok(v),
Err((e, sent)) if e.is_lost_connection() => {
self.conn = None;
if sent && !is_read_only(method) {
return Err(RemoteError::Unconfirmed { method: method.to_owned(), source: Box::new(e) });
}
self.roundtrip(method, &params).map_err(|(e, _)| e)
}
Err((e, _)) => Err(e),
// A call that changes state and went out before its connection was lost may have run.
let may_have_run = |e: &RemoteError, sent: bool| sent && e.is_lost_connection() && !is_read_only(method);
let report = |(e, sent): (RemoteError, bool)| {
if may_have_run(&e, sent) { RemoteError::Unconfirmed { method: method.to_owned(), source: Box::new(e) } } else { e }
};
match self.attempt(method, &params) {
Err((e, sent)) if e.is_lost_connection() && !may_have_run(&e, sent) => self.attempt(method, &params).map_err(report),
first => first.map_err(report),
}
}
/// One [`Self::roundtrip`]; a failure that loses the connection drops it.
fn attempt(&mut self, method: &str, params: &Value) -> Result<Value, (RemoteError, bool)> {
let result = self.roundtrip(method, params);
if result.as_ref().is_err_and(|(e, _)| e.is_lost_connection()) {
self.conn = None;
}
result
}
}
/// Connects to `addr` (`host:port`), trying each address it resolves to.
fn connect(addr: &str) -> Result<Connection, RemoteError> {
/// Connects to `addr` (`host:port`), trying each address it resolves to. A read on the connection
/// gives up after `read_timeout`.
fn connect(addr: &str, read_timeout: Duration) -> Result<Connection, RemoteError> {
let socks = addr.to_socket_addrs().map_err(|source| RemoteError::Resolve { addr: addr.to_owned(), source })?;
let mut last = None;
for sock in socks {
match TcpStream::connect_timeout(&sock, Duration::from_millis(800)) {
Ok(s) => {
s.set_read_timeout(Some(Duration::from_secs(120)))?;
s.set_read_timeout(Some(read_timeout))?;
let r = s.try_clone()?;
return Ok((s, BufReader::new(r)));
}
@@ -280,7 +322,7 @@ mod tests {
}
/// Fake app: accepts one connection per entry in `replies`, counts the request line it reads,
/// then either drops the connection (`false`) or answers ok (`true`).
/// then either drops the connection (`false`) or answers ok with the request's id (`true`).
fn fake_app(replies: Vec<bool>) -> (ControlTarget, Arc<std::sync::atomic::AtomicUsize>) {
let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
let addr = ControlTarget { host: None, port: listener.local_addr().unwrap().port() };
@@ -293,7 +335,8 @@ mod tests {
if BufReader::new(sock.try_clone().unwrap()).read_line(&mut line).unwrap_or(0) > 0 {
counter.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
if answer {
let _ = writeln!(sock, "{}", json!({"id": 1, "ok": true, "result": {"done": true}}));
let id = serde_json::from_str::<Value>(&line).map(|request| request["id"].clone()).unwrap_or(Value::Null);
let _ = writeln!(sock, "{}", json!({"id": id, "ok": true, "result": {"done": true}}));
}
}
}
@@ -332,4 +375,276 @@ mod tests {
let err = r.call("engine.execute", json!({"command": "transport.locate"})).unwrap_err();
assert_eq!(err, "transport.locate: nothing to reset");
}
use std::net::{TcpListener, TcpStream};
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::mpsc;
/// A listener on a free loopback port, and a client for it that waits 200 ms for a reply.
fn listener_and_client() -> (TcpListener, Remote) {
let listener = TcpListener::bind("127.0.0.1:0").unwrap();
let mut remote = Remote::new(&ControlTarget { host: None, port: listener.local_addr().unwrap().port() });
remote.read_timeout = Duration::from_millis(200);
(listener, remote)
}
/// The next connection, as a write half and a line reader.
fn accept(listener: &TcpListener) -> Option<(TcpStream, BufReader<TcpStream>)> {
let (sock, _) = listener.accept().ok()?;
let reader = BufReader::new(sock.try_clone().ok()?);
Some((sock, reader))
}
/// The next request on a connection; `None` when the client closed it.
fn next_request(reader: &mut BufReader<TcpStream>) -> Option<Value> {
let mut line = String::new();
if reader.read_line(&mut line).ok()? == 0 {
return None;
}
serde_json::from_str(&line).ok()
}
/// Answers `request` ok, with its id and `result`.
fn answer(sock: &mut TcpStream, request: &Value, result: Value) {
let _ = writeln!(sock, "{}", json!({"id": request["id"], "ok": true, "result": result}));
}
/// Serves every later connection: each request is counted and answered `{"call": "own"}`.
fn serve_own_answers(listener: &TcpListener, seen: &AtomicUsize) {
while let Some((mut sock, mut reader)) = accept(listener) {
while let Some(request) = next_request(&mut reader) {
seen.fetch_add(1, Ordering::SeqCst);
answer(&mut sock, &request, json!({"call": "own"}));
}
}
}
/// The case in #894: the reply to a call that timed out arrives late, on the connection the
/// call used. The next call must get its own answer, not that one.
#[test]
fn a_late_reply_is_not_taken_for_the_next_calls_answer() {
let (listener, mut remote) = listener_and_client();
let (release, released) = mpsc::channel::<()>();
let (written, late_reply_written) = mpsc::channel::<()>();
std::thread::spawn(move || {
let Some((mut sock, mut reader)) = accept(&listener) else { return };
let Some(first) = next_request(&mut reader) else { return };
// Hold the reply until the client has given up on it.
let _ = released.recv();
answer(&mut sock, &first, json!({"call": "first"}));
let _ = written.send(());
serve_own_answers(&listener, &AtomicUsize::new(0));
});
assert!(remote.call("engine.execute", json!({"command": "file.bounce_mix"})).is_err());
release.send(()).unwrap();
late_reply_written.recv().unwrap();
assert_eq!(remote.call("session.inspect", json!({})), Ok(json!({"call": "own"})));
}
/// A reply is only an answer when it carries the request's id: another id, or a success with
/// no id, is refused, the call is reported as unconfirmed, and the connection is not used again.
#[test]
fn a_reply_that_is_not_for_the_request_is_refused() {
for reply in [
json!({"id": 999, "ok": true, "result": {"call": "other"}}),
json!({"id": "1", "ok": true, "result": {"call": "other"}}),
json!({"ok": true, "result": {"call": "other"}}),
json!({"id": 999, "ok": false, "error": "another call failed"}),
] {
let (listener, mut remote) = listener_and_client();
let other = reply.clone();
std::thread::spawn(move || {
let Some((mut sock, mut reader)) = accept(&listener) else { return };
if next_request(&mut reader).is_some() {
let _ = writeln!(sock, "{other}");
}
serve_own_answers(&listener, &AtomicUsize::new(0));
});
let error = remote.call("engine.execute", json!({"command": "track.new"})).unwrap_err();
assert!(error.contains("does not answer this request"), "{reply}: {error}");
assert!(error.contains("may or may not have been executed"), "{reply}: {error}");
assert_eq!(remote.call("engine.execute", json!({"command": "track.new"})), Ok(json!({"call": "own"})), "{reply}");
}
}
/// The app refuses a connection or a bad line without an id, and closes. That is still the
/// app's error, word for word, and the closed connection is not used for the next call.
#[test]
fn a_refusal_without_an_id_is_the_apps_error_and_ends_the_connection() {
let (listener, mut remote) = listener_and_client();
let seen = Arc::new(AtomicUsize::new(0));
let counter = seen.clone();
std::thread::spawn(move || {
let Some((mut sock, mut reader)) = accept(&listener) else { return };
if next_request(&mut reader).is_some() {
let _ = writeln!(sock, "{}", json!({"ok": false, "error": "too many control connections"}));
}
drop((sock, reader));
serve_own_answers(&listener, &counter);
});
assert_eq!(remote.call("engine.execute", json!({"command": "track.new"})), Err("too many control connections".to_string()));
assert!(remote.conn.is_none(), "the app closed this connection");
assert_eq!(remote.call("engine.execute", json!({"command": "track.new"})), Ok(json!({"call": "own"})));
assert_eq!(seen.load(Ordering::SeqCst), 1, "the second call went out once, on a new connection");
}
/// A reply that is not JSON leaves the stream in an unknown state: the connection is dropped.
#[test]
fn an_unreadable_reply_ends_the_connection() {
let (listener, mut remote) = listener_and_client();
std::thread::spawn(move || {
let Some((mut sock, mut reader)) = accept(&listener) else { return };
if next_request(&mut reader).is_some() {
let _ = writeln!(sock, "not json");
}
serve_own_answers(&listener, &AtomicUsize::new(0));
drop((sock, reader));
});
let error = remote.call("engine.execute", json!({"command": "track.new"})).unwrap_err();
assert!(error.contains("may or may not have been executed"), "{error}");
assert!(remote.conn.is_none());
assert_eq!(remote.call("session.inspect", json!({})), Ok(json!({"call": "own"})));
}
/// A reply that is not UTF-8 fails the read itself (`InvalidData`), before any JSON parsing. It
/// is as unreadable as one that is not JSON: the connection is dropped, a call that changes
/// state is unconfirmed, and a read is asked once more on a new connection.
#[test]
fn a_reply_that_is_not_text_ends_the_connection() {
for (method, params) in [("engine.execute", json!({"command": "track.new"})), ("session.inspect", json!({}))] {
let (listener, mut remote) = listener_and_client();
let seen = Arc::new(AtomicUsize::new(0));
let counter = seen.clone();
std::thread::spawn(move || {
let Some((mut sock, mut reader)) = accept(&listener) else { return };
if next_request(&mut reader).is_some() {
counter.fetch_add(1, Ordering::SeqCst);
let _ = sock.write_all(b"\xff\n");
}
serve_own_answers(&listener, &counter);
drop((sock, reader));
});
let result = remote.call(method, params);
if is_read_only(method) {
assert_eq!(result, Ok(json!({"call": "own"})), "{method}: asked once more on a new connection");
assert_eq!(seen.load(Ordering::SeqCst), 2, "{method}");
} else {
let error = result.unwrap_err();
assert!(error.contains("may or may not have been executed"), "{method}: {error}");
assert_eq!(seen.load(Ordering::SeqCst), 1, "{method}: not sent again");
assert!(remote.conn.is_none(), "{method}");
assert_eq!(remote.call("session.inspect", json!({})), Ok(json!({"call": "own"})), "{method}: the next call uses a new connection");
}
}
}
/// Fake app that reads the request on each of its first `silent` connections and never
/// answers it; later connections get their own answers. Counts every request it reads.
fn app_silent_on(silent: usize) -> (Remote, Arc<AtomicUsize>) {
let (listener, remote) = listener_and_client();
let seen = Arc::new(AtomicUsize::new(0));
let counter = seen.clone();
std::thread::spawn(move || {
let mut held = Vec::new();
for _ in 0..silent {
let Some((sock, mut reader)) = accept(&listener) else { return };
if next_request(&mut reader).is_some() {
counter.fetch_add(1, Ordering::SeqCst);
}
// Keep the connection open and silent.
held.push((sock, reader));
}
serve_own_answers(&listener, &counter);
});
(remote, seen)
}
/// A call that changes state and gets no reply in time may have run: it is not sent again, the
/// error says so, and the connection is dropped so its late reply is never read.
#[test]
fn a_timed_out_mutation_is_unconfirmed_and_not_sent_again() {
let (mut remote, seen) = app_silent_on(1);
let error = remote.call("engine.execute", json!({"command": "track.new"})).unwrap_err();
assert!(error.contains("may or may not have been executed"), "{error}");
assert!(remote.conn.is_none());
assert_eq!(seen.load(Ordering::SeqCst), 1);
}
/// A call that only reads state is asked once more on a new connection after a timeout.
#[test]
fn a_timed_out_read_is_asked_once_more_on_a_new_connection() {
let (mut remote, seen) = app_silent_on(1);
assert_eq!(remote.call("session.inspect", json!({})), Ok(json!({"call": "own"})));
assert_eq!(seen.load(Ordering::SeqCst), 2);
}
/// When the second attempt times out too, the call fails after exactly two requests and no
/// connection is kept.
#[test]
fn a_read_that_times_out_twice_fails_and_keeps_no_connection() {
let (mut remote, seen) = app_silent_on(2);
assert!(remote.call("session.inspect", json!({})).is_err());
assert!(remote.conn.is_none());
assert_eq!(seen.load(Ordering::SeqCst), 2);
assert_eq!(remote.call("session.inspect", json!({})), Ok(json!({"call": "own"})), "the next call starts clean");
}
/// A call that changes state and could not be written to a dead connection is sent on a new
/// one. When that attempt goes out and its reply is lost, the call may have run: the error
/// says so, as it does when the first attempt is the one that went out.
#[test]
fn a_mutation_sent_on_the_second_attempt_and_not_answered_is_unconfirmed() {
use std::io::Read;
let (listener, mut remote) = listener_and_client();
let seen = Arc::new(AtomicUsize::new(0));
let counter = seen.clone();
std::thread::spawn(move || {
// Closing a connection with bytes still unread resets it, so the client's next write
// on it fails.
let Some((sock, reader)) = accept(&listener) else { return };
std::thread::sleep(Duration::from_millis(50));
drop((sock, reader));
// The second attempt arrives on a new connection: read it and close without a reply.
let Some((sock, mut reader)) = accept(&listener) else { return };
if next_request(&mut reader).is_some() {
counter.fetch_add(1, Ordering::SeqCst);
}
drop((sock, reader));
});
let (stream, _) = remote.connection().unwrap();
writeln!(stream, "bytes the app never reads").unwrap();
// Wait until the reset has arrived (the read stops timing out).
let mut byte = [0u8; 1];
for _ in 0..20 {
match stream.read(&mut byte) {
Err(e) if matches!(e.kind(), io::ErrorKind::TimedOut | io::ErrorKind::WouldBlock) => {}
_ => break,
}
}
let error = remote.call("engine.execute", json!({"command": "track.new"})).unwrap_err();
assert_eq!(seen.load(Ordering::SeqCst), 1, "the call went out once, on the second connection: {error}");
assert!(error.contains("may or may not have been executed"), "{error}");
assert!(remote.conn.is_none());
}
/// Calls that are answered in turn share one connection, and each gets its own answer.
#[test]
fn answered_calls_share_one_connection() {
let (listener, mut remote) = listener_and_client();
let connections = Arc::new(AtomicUsize::new(0));
let counter = connections.clone();
std::thread::spawn(move || {
while let Some((mut sock, mut reader)) = accept(&listener) {
counter.fetch_add(1, Ordering::SeqCst);
while let Some(request) = next_request(&mut reader) {
let echo = json!({"method": request["method"], "id": request["id"]});
answer(&mut sock, &request, echo);
}
}
});
for (n, method) in ["session.inspect", "engine.execute", "ui.inspect"].into_iter().enumerate() {
assert_eq!(remote.call(method, json!({})), Ok(json!({"method": method, "id": n + 1})));
}
assert_eq!(connections.load(Ordering::SeqCst), 1);
}
}
+1 -2
View File
@@ -2,7 +2,6 @@
use crate::Backend;
use serde_json::{Value, json};
use soundcraft_audio_io::FileFormat;
use std::io::{BufRead, Write};
pub const PROTOCOL_VERSION: &str = "2025-06-18";
@@ -71,7 +70,7 @@ pub fn tool_definitions() -> Vec<Value> {
tool(
"bounce_mix",
"Render the mix to an audio file. Returns peak dBFS and integrated LUFS.",
json!({"path": {"type": "string"}, "format": {"type": "string", "enum": FileFormat::writable_extensions()}, "bit_depth": {"type": "string"}, "start": {}, "end": {}, "normalize": {"type": "boolean"}}),
json!({"path": {"type": "string"}, "format": {"type": "string", "enum": craft_audio::ENCODE_EXTENSIONS}, "bit_depth": {"type": "string"}, "start": {}, "end": {}, "normalize": {"type": "boolean"}}),
&["path"],
),
tool("parity", "Feature-parity report against the incumbent's menu catalog.", json!({}), &[]),
+6
View File
@@ -17,6 +17,12 @@ log.workspace = true
[target.'cfg(windows)'.dependencies]
windows-sys = { version = "0.61", features = ["Win32_Foundation", "Win32_Graphics_Gdi", "Win32_System_LibraryLoader", "Win32_UI_Input_KeyboardAndMouse", "Win32_UI_WindowsAndMessaging"] }
# The X11 host window for embedded editors on Linux and the BSDs, and the polling behind
# `clap.posix-fd-support`.
[target.'cfg(all(unix, not(target_os = "macos")))'.dependencies]
x11rb = "0.13"
libc = "0.2"
[[test]]
name = "editor_main_thread"
harness = false
+23 -2
View File
@@ -20,7 +20,7 @@ use clap_sys::events::{
clap_event_transport, clap_input_events, clap_output_events,
};
use clap_sys::ext::audio_ports::{CLAP_AUDIO_PORT_IS_MAIN, CLAP_EXT_AUDIO_PORTS, clap_audio_port_info, clap_plugin_audio_ports};
use clap_sys::ext::gui::{CLAP_EXT_GUI, clap_host_gui, clap_plugin_gui, clap_window, clap_window_handle};
use clap_sys::ext::gui::{CLAP_EXT_GUI, clap_host_gui, clap_plugin_gui, clap_window};
use clap_sys::ext::latency::{CLAP_EXT_LATENCY, clap_host_latency, clap_plugin_latency};
use clap_sys::ext::log::{CLAP_EXT_LOG, CLAP_LOG_ERROR, CLAP_LOG_FATAL, CLAP_LOG_WARNING, clap_host_log, clap_log_severity};
use clap_sys::ext::note_ports::{CLAP_EXT_NOTE_PORTS, clap_note_port_info, clap_plugin_note_ports};
@@ -29,6 +29,11 @@ use clap_sys::ext::params::{
};
use clap_sys::ext::render::{CLAP_EXT_RENDER, CLAP_RENDER_OFFLINE, CLAP_RENDER_REALTIME, clap_plugin_render};
use clap_sys::ext::state::{CLAP_EXT_STATE, clap_host_state, clap_plugin_state};
#[cfg(all(unix, not(target_os = "macos")))]
use clap_sys::ext::{
posix_fd_support::{CLAP_EXT_POSIX_FD_SUPPORT, clap_host_posix_fd_support, clap_plugin_posix_fd_support},
timer_support::{CLAP_EXT_TIMER_SUPPORT, clap_host_timer_support, clap_plugin_timer_support},
};
use clap_sys::factory::plugin_factory::{CLAP_PLUGIN_FACTORY_ID, clap_plugin_factory};
use clap_sys::fixedpoint::{CLAP_BEATTIME_FACTOR, CLAP_SECTIME_FACTOR};
use clap_sys::host::clap_host;
@@ -49,6 +54,13 @@ mod events;
mod gui;
mod host;
mod instance;
/// The timers and descriptors a plugin registered through `clap.timer-support` and
/// `clap.posix-fd-support`. X11 editors have no event loop of their own: they register their
/// display connection's descriptor and their repaint timers, and the host calls them back on the
/// main thread. SoundCraft does that from the editor's `idle`, once per UI frame (the UI repaints
/// at least every 30 ms while an editor is open).
#[cfg(all(unix, not(target_os = "macos")))]
mod run_loop;
pub(crate) use bundle::Bundle;
pub(crate) use events::Event;
@@ -105,8 +117,17 @@ fn read_name(buf: &[c_char]) -> String {
#[path = "ffi/hostwin/windows.rs"]
mod hostwin;
/// The X11 host window for embedded editors (Linux and the BSDs): a top-level window on our own
/// connection to the X server (XWayland on Wayland desktops) that the plugin parents its GUI in.
/// The close button only hides it (`WM_DELETE_WINDOW`); the editor link notices in `idle`,
/// destroys the plugin's GUI, then the window. Our connection's events are read whenever a
/// window's visibility is asked, which the UI does every frame while an editor is open.
#[cfg(all(unix, not(target_os = "macos")))]
#[path = "ffi/hostwin/x11.rs"]
mod hostwin;
/// Other platforms: embedded editors are not hosted (floating ones still are).
#[cfg(not(windows))]
#[cfg(not(any(windows, all(unix, not(target_os = "macos")))))]
#[path = "ffi/hostwin/other.rs"]
mod hostwin;
+15 -1
View File
@@ -7,6 +7,8 @@ use super::{
clap_host, clap_plugin_audio_ports, clap_plugin_entry, clap_plugin_factory, clap_plugin_gui, clap_plugin_latency, clap_plugin_note_ports,
clap_plugin_params, clap_plugin_state, clap_version_is_compatible, read_cstr, scan,
};
#[cfg(all(unix, not(target_os = "macos")))]
use super::{CLAP_EXT_POSIX_FD_SUPPORT, CLAP_EXT_TIMER_SUPPORT, clap_plugin_posix_fd_support, clap_plugin_timer_support};
use super::{CLAP_EXT_RENDER, clap_plugin_render};
// ---- loaded binaries -----------------------------------------------------------------------
@@ -220,7 +222,19 @@ impl Bundle {
inst.render = inst.extension::<clap_plugin_render>(CLAP_EXT_RENDER);
let gui = inst.extension::<clap_plugin_gui>(CLAP_EXT_GUI);
inst.gui = Arc::new(GuiLink {
inner: Mutex::new(GuiInner { plugin, gui, state: inst.state_ext, alive: true, created: false, visible: false, window: None }),
inner: Mutex::new(GuiInner {
plugin,
gui,
state: inst.state_ext,
alive: true,
created: false,
visible: false,
window: None,
#[cfg(all(unix, not(target_os = "macos")))]
timer: inst.extension::<clap_plugin_timer_support>(CLAP_EXT_TIMER_SUPPORT),
#[cfg(all(unix, not(target_os = "macos")))]
fd: inst.extension::<clap_plugin_posix_fd_support>(CLAP_EXT_POSIX_FD_SUPPORT),
}),
host: Arc::clone(&inst.state),
});
Ok(inst)
+46 -4
View File
@@ -1,9 +1,10 @@
use super::events::ostream_write;
use super::host::HostState;
use super::{
Arc, CStr, CString, Mutex, Ordering, PoisonError, c_void, clap_ostream, clap_plugin, clap_plugin_gui, clap_plugin_state, clap_window,
clap_window_handle, hostwin,
Arc, CStr, CString, Mutex, Ordering, PoisonError, c_void, clap_ostream, clap_plugin, clap_plugin_gui, clap_plugin_state, clap_window, hostwin,
};
#[cfg(all(unix, not(target_os = "macos")))]
use super::{clap_plugin_posix_fd_support, clap_plugin_timer_support};
// ---- editor (clap.gui) ---------------------------------------------------------------------
@@ -47,6 +48,11 @@ pub(super) struct GuiInner {
pub(super) visible: bool,
/// The host window of an embedded editor (`None` for floating editors).
pub(super) window: Option<hostwin::HostWindow>,
/// The plugin's `clap.timer-support` and `clap.posix-fd-support` (null without them).
#[cfg(all(unix, not(target_os = "macos")))]
pub(super) timer: *const clap_plugin_timer_support,
#[cfg(all(unix, not(target_os = "macos")))]
pub(super) fd: *const clap_plugin_posix_fd_support,
}
/// The link between an instance and its editor and state handles. The instance may process on
@@ -71,6 +77,10 @@ impl Default for GuiLink {
created: false,
visible: false,
window: None,
#[cfg(all(unix, not(target_os = "macos")))]
timer: std::ptr::null(),
#[cfg(all(unix, not(target_os = "macos")))]
fd: std::ptr::null(),
}),
host: Arc::new(HostState::default()),
}
@@ -126,7 +136,7 @@ impl GuiLink {
if hostwin::SUPPORTED && unsafe { supported(g.plugin, api.as_ptr(), false) } {
return self.open_embedded(&mut g, vt, api, title);
}
return Err("this plugin offers only an embedded editor, which SoundCraft hosts on Windows only".into());
return Err("this plugin offers only an embedded editor, which SoundCraft hosts on Windows and X11 (Linux, BSDs) only".into());
}
self.host.gui_closed.store(false, Ordering::Relaxed);
self.host.gui_destroyed.store(false, Ordering::Relaxed);
@@ -188,7 +198,7 @@ impl GuiLink {
return Err(e);
}
};
let parent = clap_window { api: api.as_ptr(), specific: clap_window_handle { win32: win.parent() } };
let parent = clap_window { api: api.as_ptr(), specific: win.handle() };
// SAFETY: live plugin with a created embedded GUI; `parent` names our live window and
// outlives the call.
if !unsafe { set_parent(g.plugin, &parent) } {
@@ -274,6 +284,8 @@ impl GuiLink {
unsafe { f(g.plugin) };
}
}
#[cfg(all(unix, not(target_os = "macos")))]
self.dispatch(&g);
if g.created {
let resize = self.host.gui_resize.swap(0, Ordering::Relaxed);
if resize != 0
@@ -330,5 +342,35 @@ impl GuiLink {
}
}
g.alive = false;
#[cfg(all(unix, not(target_os = "macos")))]
self.host.run_loop.clear();
}
/// Calls the plugin back for its ready descriptors and due timers (main thread, under the
/// lock, plugin alive). One unregistered earlier in the same pass is skipped.
#[cfg(all(unix, not(target_os = "macos")))]
fn dispatch(&self, g: &GuiInner) {
if !g.alive {
return;
}
let rl = &self.host.run_loop;
// SAFETY: null or the plugin's static extension vtables, valid while `alive`.
let (timer, fd) = unsafe { (g.timer.as_ref().and_then(|t| t.on_timer), g.fd.as_ref().and_then(|f| f.on_fd)) };
if let Some(on_fd) = fd {
for (d, flags) in rl.ready() {
if rl.has_fd(d) {
// SAFETY: live plugin, main thread, a descriptor it registered.
unsafe { on_fd(g.plugin, d, flags) };
}
}
}
if let Some(on_timer) = timer {
for id in rl.due(std::time::Instant::now()) {
if rl.has_timer(id) {
// SAFETY: live plugin, main thread, a timer it registered.
unsafe { on_timer(g.plugin, id) };
}
}
}
}
}
+75 -1
View File
@@ -3,6 +3,8 @@ use super::{
CLAP_LOG_WARNING, CStr, EDIT_CAP, Mutex, Ordering, c_char, c_void, clap_host, clap_host_gui, clap_host_latency, clap_host_log, clap_host_params,
clap_host_state, clap_id, clap_log_severity, clap_param_clear_flags, clap_param_rescan_flags, read_cstr,
};
#[cfg(all(unix, not(target_os = "macos")))]
use super::{CLAP_EXT_POSIX_FD_SUPPORT, CLAP_EXT_TIMER_SUPPORT, clap_host_posix_fd_support, clap_host_timer_support};
// ---- host side -----------------------------------------------------------------------------
@@ -25,6 +27,10 @@ pub(crate) struct HostState {
/// Parameter changes the plugin reported (output events: typically edits in its GUI),
/// waiting for the UI. Bounded by `EDIT_CAP`; the audio thread only `try_lock`s it.
pub edits: Mutex<Vec<(u32, f64)>>,
/// The timers and descriptors the plugin registered (`clap.timer-support`,
/// `clap.posix-fd-support`), serviced by the editor's `idle` on the main thread.
#[cfg(all(unix, not(target_os = "macos")))]
pub run_loop: super::run_loop::RunLoop,
}
impl Default for HostState {
@@ -42,6 +48,8 @@ impl Default for HostState {
gui_hide: AtomicBool::new(false),
gui_resize: AtomicU64::new(0),
edits: Mutex::new(Vec::with_capacity(EDIT_CAP)),
#[cfg(all(unix, not(target_os = "macos")))]
run_loop: super::run_loop::RunLoop::default(),
}
}
}
@@ -199,8 +207,74 @@ pub(super) unsafe extern "C" fn host_get_extension(_host: *const clap_host, id:
(&HOST_STATE as *const clap_host_state).cast()
} else if id == CLAP_EXT_GUI {
(&HOST_GUI as *const clap_host_gui).cast()
} else if let Some(ext) = run_loop_extension(id) {
ext
} else {
// Everything else (thread-check, timers, fd support, …) is unsupported: CLAP allows null.
// Everything else (thread-check, …) is unsupported: CLAP allows null.
std::ptr::null()
}
}
/// `clap.timer-support` and `clap.posix-fd-support`: X11 editors have no event loop of their own
/// and rely on the host's to read their display connection and to repaint.
#[cfg(all(unix, not(target_os = "macos")))]
fn run_loop_extension(id: &CStr) -> Option<*const c_void> {
if id == CLAP_EXT_TIMER_SUPPORT {
Some((&HOST_TIMER as *const clap_host_timer_support).cast())
} else if id == CLAP_EXT_POSIX_FD_SUPPORT {
Some((&HOST_FD as *const clap_host_posix_fd_support).cast())
} else {
None
}
}
#[cfg(not(all(unix, not(target_os = "macos"))))]
fn run_loop_extension(_id: &CStr) -> Option<*const c_void> {
None
}
#[cfg(all(unix, not(target_os = "macos")))]
static HOST_TIMER: clap_host_timer_support =
clap_host_timer_support { register_timer: Some(host_register_timer), unregister_timer: Some(host_unregister_timer) };
#[cfg(all(unix, not(target_os = "macos")))]
static HOST_FD: clap_host_posix_fd_support =
clap_host_posix_fd_support { register_fd: Some(host_register_fd), modify_fd: Some(host_modify_fd), unregister_fd: Some(host_unregister_fd) };
#[cfg(all(unix, not(target_os = "macos")))]
unsafe extern "C" fn host_register_timer(host: *const clap_host, period_ms: u32, timer_id: *mut clap_id) -> bool {
// SAFETY: the plugin passes back the host pointer we gave it.
let Some(s) = (unsafe { state(host) }) else { return false };
// SAFETY: an out-pointer from the plugin (or null, handled).
let Some(out) = (unsafe { timer_id.as_mut() }) else { return false };
match s.run_loop.add_timer(period_ms) {
Some(id) => {
*out = id;
true
}
None => false,
}
}
#[cfg(all(unix, not(target_os = "macos")))]
unsafe extern "C" fn host_unregister_timer(host: *const clap_host, timer_id: clap_id) -> bool {
// SAFETY: the plugin passes back the host pointer we gave it.
unsafe { state(host) }.is_some_and(|s| s.run_loop.remove_timer(timer_id))
}
#[cfg(all(unix, not(target_os = "macos")))]
unsafe extern "C" fn host_register_fd(host: *const clap_host, fd: i32, flags: u32) -> bool {
// SAFETY: the plugin passes back the host pointer we gave it.
unsafe { state(host) }.is_some_and(|s| s.run_loop.add_fd(fd, flags))
}
#[cfg(all(unix, not(target_os = "macos")))]
unsafe extern "C" fn host_modify_fd(host: *const clap_host, fd: i32, flags: u32) -> bool {
// SAFETY: the plugin passes back the host pointer we gave it.
unsafe { state(host) }.is_some_and(|s| s.run_loop.modify_fd(fd, flags))
}
#[cfg(all(unix, not(target_os = "macos")))]
unsafe extern "C" fn host_unregister_fd(host: *const clap_host, fd: i32) -> bool {
// SAFETY: the plugin passes back the host pointer we gave it.
unsafe { state(host) }.is_some_and(|s| s.run_loop.remove_fd(fd))
}
+3 -2
View File
@@ -1,3 +1,4 @@
use clap_sys::ext::gui::clap_window_handle;
use std::ffi::c_void;
pub const SUPPORTED: bool = false;
@@ -11,8 +12,8 @@ impl HostWindow {
pub fn raw(&self) -> *mut c_void {
std::ptr::null_mut()
}
pub fn parent(&self) -> *mut c_void {
std::ptr::null_mut()
pub fn handle(&self) -> clap_window_handle {
clap_window_handle { ptr: std::ptr::null_mut() }
}
pub fn raise(&self) {}
pub fn is_visible(&self) -> bool {
@@ -1,3 +1,4 @@
use clap_sys::ext::gui::clap_window_handle;
use std::ffi::c_void;
use std::sync::OnceLock;
use windows_sys::Win32::Foundation::{HWND, LPARAM, LRESULT, RECT, WPARAM};
@@ -128,6 +129,10 @@ impl HostWindow {
self.live().unwrap_or(std::ptr::null_mut())
}
pub fn handle(&self) -> clap_window_handle {
clap_window_handle { win32: self.parent() }
}
pub fn raise(&self) {
if let Some(h) = self.live() {
// SAFETY: a live window of this thread.
+170
View File
@@ -0,0 +1,170 @@
use clap_sys::ext::gui::clap_window_handle;
use std::collections::HashSet;
use std::ffi::c_void;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Mutex, OnceLock, PoisonError};
use x11rb::connection::Connection;
use x11rb::properties::WmSizeHints;
use x11rb::protocol::Event;
use x11rb::protocol::xproto::{
AtomEnum, ConfigureWindowAux, ConnectionExt as _, CreateWindowAux, EventMask, PropMode, StackMode, Window, WindowClass,
};
use x11rb::rust_connection::RustConnection;
use x11rb::wrapper::ConnectionExt as _;
pub const SUPPORTED: bool = true;
/// Rust names the process's first thread "main"; the app runs its UI (and event loop) there.
fn on_main_thread() -> bool {
std::thread::current().name() == Some("main")
}
struct X {
conn: RustConnection,
root: Window,
visual: u32,
background: u32,
wm_protocols: u32,
wm_delete: u32,
net_wm_name: u32,
utf8: u32,
/// Windows whose close button was pressed (hidden, not yet destroyed).
closed: Mutex<HashSet<Window>>,
}
/// The process-wide connection, opened on first use (`None` without an X server).
fn x() -> Option<&'static X> {
static X: OnceLock<Option<X>> = OnceLock::new();
X.get_or_init(|| match connect() {
Ok(x) => Some(x),
Err(e) => {
log::warn!("clap: no X11 display for plugin editors: {e}");
None
}
})
.as_ref()
}
fn connect() -> Result<X, Box<dyn std::error::Error>> {
let (conn, screen) = x11rb::connect(None)?;
let s = conn.setup().roots.get(screen).ok_or("no X11 screen")?;
let (root, visual, background) = (s.root, s.root_visual, s.black_pixel);
let atom = |name: &[u8]| -> Result<u32, Box<dyn std::error::Error>> { Ok(conn.intern_atom(false, name)?.reply()?.atom) };
Ok(X {
wm_protocols: atom(b"WM_PROTOCOLS")?,
wm_delete: atom(b"WM_DELETE_WINDOW")?,
net_wm_name: atom(b"_NET_WM_NAME")?,
utf8: atom(b"UTF8_STRING")?,
root,
visual,
background,
closed: Mutex::new(HashSet::new()),
conn,
})
}
impl X {
fn closed(&self) -> std::sync::MutexGuard<'_, HashSet<Window>> {
self.closed.lock().unwrap_or_else(PoisonError::into_inner)
}
/// Reads pending events: a close request hides its window and marks it closed.
fn pump(&self) {
while let Ok(Some(ev)) = self.conn.poll_for_event() {
if let Event::ClientMessage(m) = ev
&& m.type_ == self.wm_protocols
&& m.format == 32
&& m.data.as_data32().first() == Some(&self.wm_delete)
{
let _ = self.conn.unmap_window(m.window);
self.closed().insert(m.window);
}
}
let _ = self.conn.flush();
}
/// Fixes the window's size for the window manager (plugins resize through the host).
fn size_hints(&self, w: Window, width: u32, height: u32) {
let (width, height) = (width as i32, height as i32);
let hints = WmSizeHints { min_size: Some((width, height)), max_size: Some((width, height)), ..WmSizeHints::new() };
let _ = hints.set_normal_hints(&self.conn, w);
}
}
fn dims(w: f64, h: f64) -> (u16, u16) {
(w.clamp(1.0, 16_384.0) as u16, h.clamp(1.0, 16_384.0) as u16)
}
pub struct HostWindow {
id: Window,
shown: AtomicBool,
}
impl HostWindow {
pub fn new(title: &str, w: f64, h: f64) -> Result<HostWindow, String> {
if !on_main_thread() {
return Err("plugin editors must be opened on the main thread".into());
}
let x = x().ok_or_else(|| "plugin editors need an X11 display (XWayland on Wayland desktops)".to_string())?;
let err = |e: &dyn std::fmt::Display| format!("cannot create the editor window: {e}");
let id = x.conn.generate_id().map_err(|e| err(&e))?;
let (width, height) = dims(w, h);
let aux = CreateWindowAux::new().background_pixel(x.background).event_mask(EventMask::STRUCTURE_NOTIFY);
x.conn
.create_window(x11rb::COPY_DEPTH_FROM_PARENT, id, x.root, 0, 0, width, height, 0, WindowClass::INPUT_OUTPUT, x.visual, &aux)
.map_err(|e| err(&e))?;
let _ = x.conn.change_property8(PropMode::REPLACE, id, AtomEnum::WM_NAME, AtomEnum::STRING, title.as_bytes());
let _ = x.conn.change_property8(PropMode::REPLACE, id, x.net_wm_name, x.utf8, title.as_bytes());
let _ = x.conn.change_property8(PropMode::REPLACE, id, AtomEnum::WM_CLASS, AtomEnum::STRING, b"soundcraft\0SoundCraft\0");
let _ = x.conn.change_property32(PropMode::REPLACE, id, x.wm_protocols, AtomEnum::ATOM, &[x.wm_delete]);
x.size_hints(id, width.into(), height.into());
// A round trip, so the window exists before the plugin (on its own connection)
// parents its GUI in it.
x.conn.get_input_focus().map_err(|e| err(&e))?.reply().map_err(|e| err(&e))?;
Ok(HostWindow { id, shown: AtomicBool::new(false) })
}
/// The window id (for resizes).
pub fn raw(&self) -> *mut c_void {
self.id as usize as *mut c_void
}
pub fn handle(&self) -> clap_window_handle {
clap_window_handle { x11: std::ffi::c_ulong::from(self.id) }
}
pub fn raise(&self) {
let Some(x) = x().filter(|_| on_main_thread()) else { return };
x.closed().remove(&self.id);
let _ = x.conn.map_window(self.id);
let _ = x.conn.configure_window(self.id, &ConfigureWindowAux::new().stack_mode(StackMode::ABOVE));
let _ = x.conn.flush();
self.shown.store(true, Ordering::Relaxed);
}
pub fn is_visible(&self) -> bool {
let Some(x) = x().filter(|_| on_main_thread()) else { return false };
x.pump();
self.shown.load(Ordering::Relaxed) && !x.closed().contains(&self.id)
}
pub fn close(self) {
let Some(x) = x().filter(|_| on_main_thread()) else { return };
x.closed().remove(&self.id);
let _ = x.conn.destroy_window(self.id);
let _ = x.conn.flush();
}
}
/// Resizes a host window (`window` from [`HostWindow::raw`]); main thread only.
pub fn resize(window: *mut c_void, w: f64, h: f64) {
let Some(x) = x().filter(|_| on_main_thread()) else { return };
let Ok(id) = Window::try_from(window as usize) else { return };
if id == 0 {
return;
}
let (width, height) = dims(w, h);
x.size_hints(id, width.into(), height.into());
let _ = x.conn.configure_window(id, &ConfigureWindowAux::new().width(u32::from(width)).height(u32::from(height)));
let _ = x.conn.flush();
}
+192
View File
@@ -0,0 +1,192 @@
use clap_sys::ext::posix_fd_support::{CLAP_POSIX_FD_ERROR, CLAP_POSIX_FD_READ, CLAP_POSIX_FD_WRITE, clap_posix_fd_flags};
use clap_sys::id::clap_id;
use std::sync::{Mutex, PoisonError};
use std::time::{Duration, Instant};
/// Registrations kept per instance; a plugin needs a handful.
const MAX_ENTRIES: usize = 256;
/// Longest timer period accepted (one hour).
const MAX_PERIOD_MS: u32 = 3_600_000;
struct Timer {
id: clap_id,
every: Duration,
next: Instant,
}
#[derive(Default)]
struct Entries {
timers: Vec<Timer>,
fds: Vec<(i32, clap_posix_fd_flags)>,
next_id: clap_id,
}
#[derive(Default)]
pub struct RunLoop {
entries: Mutex<Entries>,
}
impl RunLoop {
fn lock(&self) -> std::sync::MutexGuard<'_, Entries> {
self.entries.lock().unwrap_or_else(PoisonError::into_inner)
}
pub fn add_timer(&self, period_ms: u32) -> Option<clap_id> {
let mut e = self.lock();
if e.timers.len() >= MAX_ENTRIES {
return None;
}
// Ids are never reused within an instance (`CLAP_INVALID_ID` is u32::MAX).
let id = e.next_id;
e.next_id = e.next_id.checked_add(1).filter(|&n| n != clap_id::MAX)?;
let every = Duration::from_millis(u64::from(period_ms.clamp(1, MAX_PERIOD_MS)));
e.timers.push(Timer { id, every, next: Instant::now() + every });
log::debug!("clap: run loop: timer {id} every {} ms", every.as_millis());
Some(id)
}
pub fn remove_timer(&self, id: clap_id) -> bool {
let mut e = self.lock();
let before = e.timers.len();
e.timers.retain(|t| t.id != id);
e.timers.len() != before
}
pub fn add_fd(&self, fd: i32, flags: clap_posix_fd_flags) -> bool {
let mut e = self.lock();
if fd < 0 || e.fds.len() >= MAX_ENTRIES || e.fds.iter().any(|&(f, _)| f == fd) {
return false;
}
e.fds.push((fd, flags));
log::debug!("clap: run loop: watching fd {fd}");
true
}
pub fn modify_fd(&self, fd: i32, flags: clap_posix_fd_flags) -> bool {
let mut e = self.lock();
match e.fds.iter_mut().find(|(f, _)| *f == fd) {
Some(entry) => {
entry.1 = flags;
true
}
None => false,
}
}
pub fn remove_fd(&self, fd: i32) -> bool {
let mut e = self.lock();
let before = e.fds.len();
e.fds.retain(|&(f, _)| f != fd);
e.fds.len() != before
}
pub fn clear(&self) {
let mut e = self.lock();
e.timers.clear();
e.fds.clear();
}
pub fn has_timer(&self, id: clap_id) -> bool {
self.lock().timers.iter().any(|t| t.id == id)
}
pub fn has_fd(&self, fd: i32) -> bool {
self.lock().fds.iter().any(|&(f, _)| f == fd)
}
/// The timers due at `now`, rescheduled from `now` (a late UI frame fires each once
/// rather than replaying missed ticks).
pub fn due(&self, now: Instant) -> Vec<clap_id> {
let mut e = self.lock();
let mut out = Vec::new();
for t in e.timers.iter_mut().filter(|t| t.next <= now) {
t.next = now + t.every;
out.push(t.id);
}
out
}
/// The registered descriptors that are ready for what they asked for (or failed), with
/// the matching CLAP flags, without waiting.
pub fn ready(&self) -> Vec<(i32, clap_posix_fd_flags)> {
let fds = self.lock().fds.clone();
if fds.is_empty() {
return Vec::new();
}
let mut polled: Vec<libc::pollfd> = fds
.iter()
.map(|&(fd, flags)| {
let mut events = 0;
if flags & CLAP_POSIX_FD_READ != 0 {
events |= libc::POLLIN;
}
if flags & CLAP_POSIX_FD_WRITE != 0 {
events |= libc::POLLOUT;
}
libc::pollfd { fd, events, revents: 0 }
})
.collect();
// SAFETY: `polled` is a valid, writable array of `polled.len()` entries; a zero
// timeout never blocks.
if unsafe { libc::poll(polled.as_mut_ptr(), polled.len() as libc::nfds_t, 0) } <= 0 {
return Vec::new();
}
polled
.iter()
.filter_map(|p| {
let mut flags = 0;
if p.revents & libc::POLLIN != 0 {
flags |= CLAP_POSIX_FD_READ;
}
if p.revents & libc::POLLOUT != 0 {
flags |= CLAP_POSIX_FD_WRITE;
}
if p.revents & (libc::POLLERR | libc::POLLHUP) != 0 {
flags |= CLAP_POSIX_FD_ERROR;
}
(flags != 0).then_some((p.fd, flags))
})
.collect()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn timers_and_descriptors_follow_registration() {
let rl = RunLoop::default();
// Before registering: every timer's first tick is then later than `t0`.
let t0 = Instant::now();
let a = rl.add_timer(10).unwrap();
let b = rl.add_timer(0).unwrap();
assert_ne!(a, b, "ids are unique");
assert!(rl.due(t0).is_empty(), "not due yet");
let mut due = rl.due(t0 + Duration::from_secs(1));
due.sort_unstable();
assert_eq!(due, vec![a, b], "a late frame fires each timer once");
assert!(rl.remove_timer(a) && !rl.remove_timer(a));
assert_eq!(rl.due(t0 + Duration::from_secs(2)), vec![b]);
let mut fds = [0; 2];
// SAFETY: `fds` holds the two descriptors `pipe` writes.
assert_eq!(unsafe { libc::pipe(fds.as_mut_ptr()) }, 0);
assert!(rl.add_fd(fds[0], CLAP_POSIX_FD_READ));
assert!(!rl.add_fd(fds[0], CLAP_POSIX_FD_READ), "registered once");
assert!(!rl.add_fd(-1, CLAP_POSIX_FD_READ));
assert!(rl.ready().is_empty(), "nothing to read");
// SAFETY: a valid one-byte buffer written to our own pipe.
assert_eq!(unsafe { libc::write(fds[1], b"x".as_ptr().cast(), 1) }, 1);
assert_eq!(rl.ready(), vec![(fds[0], CLAP_POSIX_FD_READ)]);
assert!(rl.modify_fd(fds[0], CLAP_POSIX_FD_WRITE));
assert!(rl.ready().is_empty(), "a read end never becomes writable");
assert!(rl.remove_fd(fds[0]) && !rl.has_fd(fds[0]));
rl.clear();
assert!(!rl.has_timer(b));
// SAFETY: closing the pipe we opened.
unsafe {
libc::close(fds[0]);
libc::close(fds[1]);
}
}
}
+5 -3
View File
@@ -21,9 +21,11 @@
//! [`Plugin::load_state`] as an opaque blob. Plugin editors (`clap.gui`) open as the plugin's own
//! floating window ([`Plugin::open_editor`], or [`Plugin::editor`] for a handle usable on the main
//! thread while the instance processes on the audio thread). Plugins that offer only an embedded
//! editor get a host window of SoundCraft's on Windows (resized on `request_resize`, closed by
//! its title bar); elsewhere they are not hosted yet, and SoundCraft's generic parameter editor
//! still works for them.
//! editor get a host window of SoundCraft's on Windows and on X11 (Linux, BSDs; resized on
//! `request_resize`, closed by its title bar); on macOS they are not hosted yet, and SoundCraft's
//! generic parameter editor still works for them. On X11 the host also offers
//! `clap.timer-support` and `clap.posix-fd-support`, serviced by the editor's `idle`, since X11
//! editors have no event loop of their own.
//! Parameter changes made in the plugin's GUI arrive as output events while the plugin processes
//! and are reported by the editor's `idle`.
+3
View File
@@ -4,6 +4,7 @@ pub mod auto_pan;
pub mod drums;
pub mod dynamics;
pub mod eq;
pub mod freq_shifter;
pub mod harmonic;
pub mod modulation;
pub mod pitch;
@@ -32,6 +33,7 @@ pub(crate) static REGISTRY: &[&PluginInfo] = &[
&modulation::FLANGER_INFO,
&modulation::PHASER_INFO,
&modulation::RING_INFO,
&freq_shifter::FREQ_SHIFTER_INFO,
&harmonic::SAT_INFO,
&harmonic::LOFI_INFO,
&harmonic::RECT_INFO,
@@ -67,6 +69,7 @@ pub(crate) fn instantiate(id: &str) -> Option<Box<dyn Plugin>> {
"flanger" => Box::new(modulation::ModFx::flanger()),
"phaser" => Box::new(modulation::Phaser::new()),
"ring_mod" => Box::new(modulation::RingMod::new()),
"freq_shifter" => Box::new(freq_shifter::FreqShifter::new()),
"saturator" => Box::new(harmonic::Saturator::new()),
"lofi" => Box::new(harmonic::LoFi::new()),
"rectifier" => Box::new(harmonic::Rectifier::new()),
+468
View File
@@ -0,0 +1,468 @@
//! Frequency Shifter: moves every frequency in the signal by the same number of hertz.
//!
//! Unlike a pitch shifter, which multiplies frequencies, a frequency shifter adds a constant to
//! them, so harmonic sounds become inharmonic. The signal goes through two chains of allpass
//! filters whose outputs differ in phase by 90° (the in-phase and quadrature parts of the
//! analytic signal). Multiplying them by a cosine and a sine at the shift frequency and adding or
//! subtracting the products keeps only the upper or only the lower sideband (single-sideband
//! modulation). The filters run in `f64`.
use std::f64::consts::{PI, TAU};
use crate::params::{Params, choice, lin, log, param_plumbing};
use crate::util::{FALLBACK_SR, Smoother, frames_in, sane_channels, sane_sr};
use crate::{Category, ParamInfo, Plugin, PluginInfo, Unit};
const MODE_CHOICES: &[&str] = &["Up", "Down", "Up & Down", "Stereo"];
const MODE_DOWN: usize = 1;
const MODE_BOTH: usize = 2;
const MODE_STEREO: usize = 3;
/// Allpass sections in each of the two chains.
const SECTIONS: usize = 10;
/// The 90° phase difference holds from this frequency to the same distance below Nyquist.
const QUADRATURE_LOW_HZ: f64 = 20.0;
/// Largest Feedback, in percent. The shift raises a signal's energy by at most √2 (the
/// quadrature pair has twice the energy of its input, and the output is never larger than
/// their magnitude), so with feedback below 1/√2 the loop gain stays under one and the
/// feedback loop is stable for any input, mode and frequency (small-gain theorem).
const MAX_FEEDBACK: f32 = 70.0;
/// The level, relative to the input, at which `tail_samples` considers the ringing over (-80 dB).
const TAIL_FLOOR: f64 = 1.0e-4;
static FS_PARAMS: [ParamInfo; 4] = [
log("frequency", "Frequency", 0.01, 5000.0, 100.0, Unit::Hz),
choice("mode", "Mode", MODE_CHOICES, 0),
lin("feedback", "Feedback", 0.0, MAX_FEEDBACK, 0.0, Unit::Percent),
lin("mix", "Mix", 0.0, 100.0, 100.0, Unit::Percent),
];
pub static FREQ_SHIFTER_INFO: PluginInfo = PluginInfo {
id: "freq_shifter",
name: "Frequency Shifter",
short_name: "FreqShft",
category: Category::Modulation,
params: &FS_PARAMS,
is_instrument: false,
audiosuite: true,
};
/// Arithmetic-geometric mean of `a` and `b` (both positive).
fn agm(mut a: f64, mut b: f64) -> f64 {
for _ in 0..64 {
let (m, g) = (0.5 * (a + b), (a * b).sqrt());
a = m;
b = g;
if (a - b).abs() <= 1.0e-15 * a {
break;
}
}
a
}
/// Allpass coefficients for a 90° phase-difference network at `sr`, accurate from
/// [`QUADRATURE_LOW_HZ`] to `sr / 2 - QUADRATURE_LOW_HZ`.
///
/// They are the coefficients of an elliptic half-band filter built from two allpass chains
/// (Valenzuela and Constantinides, "Digital signal processing schemes for efficient
/// interpolation and decimation", IEE Proceedings G 130(6), 1983). Moving that filter's
/// response by a quarter of the sample rate turns its two branches into a phase splitter: each
/// section becomes `(a - z⁻²) / (1 - a z⁻²)`. Even-numbered coefficients go to the in-phase
/// chain, odd-numbered ones to the quadrature chain, which also has a one-sample delay.
///
/// The half-band filter has a transition band of `tb = 2 · low / sr` (as a fraction of the
/// sample rate) centred on `sr / 4`, so after the shift the phase error is equiripple between
/// `low` and `sr / 2 - low`. Its selectivity is `k = tan²(π (1 - 2 tb) / 4)` and its nome
/// `q = exp(-π K(k') / K(k))`, with `K` the complete elliptic integral of the first kind computed
/// by the arithmetic-geometric mean. Coefficient `i` of `n` comes from the theta-function sums
///
/// ```text
/// w = q^¼ · Σₘ (-1)ᵐ q^(m(m+1)) sin((2m+1) i π / (2n+1)) / (½ + Σₘ₌₁ (-1)ᵐ q^(m²) cos(2 m i π / (2n+1)))
/// x = √((1 - w² k)(1 - w² / k)) / (1 + w²), a = (1 - x) / (1 + x).
/// ```
///
/// `None` when the rate is unusable or a coefficient comes out of `[0, 1)`.
fn design_quadrature(sr: f64) -> Option<[f64; 2 * SECTIONS]> {
let tb = 2.0 * QUADRATURE_LOW_HZ / sr;
if !tb.is_finite() || tb <= 0.0 || tb >= 0.25 {
return None;
}
let k = ((1.0 - 2.0 * tb) * PI / 4.0).tan().powi(2);
let kc = ((1.0 - k) * (1.0 + k)).sqrt();
// K(k') / K(k) = agm(1, k') / agm(1, k), since K(k) = π / (2 agm(1, k')).
let q = (-PI * agm(1.0, kc) / agm(1.0, k)).exp();
// The half-band filter has one coefficient per section of both chains: order 2 · 2 SECTIONS + 1.
let order = (4 * SECTIONS + 1) as f64;
let mut coefs = [0.0f64; 2 * SECTIONS];
for (idx, coef) in coefs.iter_mut().enumerate() {
let i = (idx + 1) as f64;
let mut num = 0.0;
for m in 0..64i32 {
let sign = if m % 2 == 0 { 1.0 } else { -1.0 };
let term = sign * q.powi(m * (m + 1)) * ((2 * m + 1) as f64 * i * PI / order).sin();
num += term;
if term.abs() < 1.0e-30 {
break;
}
}
let mut den = 0.5;
for m in 1..64i32 {
let sign = if m % 2 == 0 { 1.0 } else { -1.0 };
let term = sign * q.powi(m * m) * (2.0 * f64::from(m) * i * PI / order).cos();
den += term;
if term.abs() < 1.0e-30 {
break;
}
}
let w = q.powf(0.25) * num / den;
let w2 = w * w;
let x = ((1.0 - w2 * k) * (1.0 - w2 / k)).max(0.0).sqrt() / (1.0 + w2);
*coef = (1.0 - x) / (1.0 + x);
}
coefs.iter().all(|a| a.is_finite() && (0.0..1.0).contains(a)).then_some(coefs)
}
/// The network for `sr`, falling back to the default rate's (and, failing that, to plain
/// delays) so that a strange host rate can never leave the plugin without coefficients.
fn quadrature_coefs(sr: f64) -> [f64; 2 * SECTIONS] {
design_quadrature(sr).or_else(|| design_quadrature(f64::from(FALLBACK_SR))).unwrap_or([0.0; 2 * SECTIONS])
}
/// Flushes values too small to hear (and any non-finite value) to zero, so silence does not
/// leave the filter states running on denormals.
#[inline]
fn flush64(x: f64) -> f64 {
if x.abs() > 1.0e-30 && x.is_finite() { x } else { 0.0 }
}
/// One `(a - z⁻²) / (1 - a z⁻²)` allpass section.
#[derive(Debug, Clone, Copy, Default)]
struct Section {
x1: f64,
x2: f64,
y1: f64,
y2: f64,
}
impl Section {
/// One sample through the section: `y[n] = a (x[n] + y[n-2]) - x[n-2]`.
#[inline]
fn tick(&mut self, a: f64, x: f64) -> f64 {
let y = flush64(a * (x + self.y2) - self.x2);
self.x2 = self.x1;
self.x1 = x;
self.y2 = self.y1;
self.y1 = y;
y
}
}
/// Runs `x` through a chain of sections with the coefficients `coefs`.
#[inline]
fn chain(sections: &mut [Section; SECTIONS], coefs: &[f64; SECTIONS], x: f64) -> f64 {
sections.iter_mut().zip(coefs.iter()).fold(x, |v, (s, &a)| s.tick(a, v))
}
/// The state of one channel.
#[derive(Debug, Clone, Copy, Default)]
struct Channel {
in_phase: [Section; SECTIONS],
quadrature: [Section; SECTIONS],
/// The quadrature chain's input from the previous sample (its one-sample delay).
delayed: f64,
/// The previous wet output, fed back into the input.
last: f64,
}
/// Which sideband `channel` of `channels` keeps, as the weight of the quadrature product:
/// `1` keeps the upper sideband, `-1` the lower one and `0` both at half level. The output is
/// `i cos - s q sin`, so it is linear in `s`, and moving `s` smoothly from one mode's value to
/// another's crossfades the two modes without a click.
///
/// Stereo shifts the left channel down and the right one up. Only a two-channel insert has a
/// known left and right; a plugin is not told the speaker layout of a wider one (channel 1 is
/// the centre in LCR and 5.1, the right in quad), so mono and multichannel inserts play both
/// sidebands on every channel.
fn sideband(mode: usize, channel: usize, channels: usize) -> f32 {
match mode {
MODE_DOWN => -1.0,
MODE_BOTH => 0.0,
MODE_STEREO if channels != 2 => 0.0,
MODE_STEREO if channel == 0 => -1.0,
_ => 1.0,
}
}
/// Single-sideband frequency shifter with feedback.
///
/// - **Up** adds Frequency to every component and **Down** subtracts it; a component that goes
/// below 0 Hz comes back mirrored, and one pushed past Nyquist folds back from it.
/// - **Up & Down** plays both sidebands at half level each (the in-phase signal times the
/// carrier), which is a ring modulation without the quadrature chain's error.
/// - **Stereo** shifts the left channel down and the right channel up (see [`sideband`]).
/// - **Feedback** sends the shifted output back into the input, so a tone leaves a trail of
/// components one more shift away each time, each `feedback` times the level of the last.
///
/// The phase difference is accurate to within a few millionths of a radian from 20 Hz to 20 Hz
/// below Nyquist at 44.1 to 96 kHz, which leaves the unwanted sideband more than 100 dB down;
/// below 20 Hz both sidebands come through. The plugin has no latency.
///
/// Frequency, Feedback and Mix glide to a new value over about 20 ms, and the carrier phase runs
/// on, so a change does not click. A new Mode crossfades from the old sidebands in the same time.
pub struct FreqShifter {
p: Params,
sr: f64,
in_phase_coefs: [f64; SECTIONS],
quadrature_coefs: [f64; SECTIONS],
channels: Vec<Channel>,
/// Carrier phase in cycles, in `[0, 1)`.
phase: f64,
freq: Smoother,
feedback: Smoother,
mix: Smoother,
/// Each channel's sideband weight (see [`sideband`]).
sides: Vec<Smoother>,
tail: usize,
}
impl Default for FreqShifter {
fn default() -> Self {
Self::new()
}
}
impl FreqShifter {
/// A shifter with the default parameters and the network for the default rate; [`Plugin::prepare`]
/// designs the network for the host rate.
pub fn new() -> Self {
let mut s = Self {
p: Params::new(&FS_PARAMS),
sr: f64::from(FALLBACK_SR),
in_phase_coefs: [0.0; SECTIONS],
quadrature_coefs: [0.0; SECTIONS],
channels: Vec::new(),
phase: 0.0,
freq: Smoother::new(100.0),
feedback: Smoother::new(0.0),
mix: Smoother::new(1.0),
sides: Vec::new(),
tail: 0,
};
s.design();
s.update();
s
}
/// Designs the network for `self.sr` and deals its coefficients out: even-numbered ones to the
/// in-phase chain, odd-numbered ones to the quadrature chain.
fn design(&mut self) {
let coefs = quadrature_coefs(self.sr);
for (i, a) in coefs.iter().enumerate() {
let side = if i % 2 == 0 { &mut self.in_phase_coefs } else { &mut self.quadrature_coefs };
if let Some(slot) = side.get_mut(i / 2) {
*slot = *a;
}
}
}
/// Points the smoothers at the current parameter values, each channel's sideband weight at the
/// current Mode, and works the tail out again. Runs on every parameter change.
fn update(&mut self) {
self.freq.set(self.p.v(0));
self.feedback.set(self.p.v(2) / 100.0);
self.mix.set(self.p.v(3) / 100.0);
let (mode, channels) = (self.p.choice(1), self.sides.len());
for (c, s) in self.sides.iter_mut().enumerate() {
s.set(sideband(mode, c, channels));
}
self.tail = self.ring_samples();
}
/// How long the network rings after an impulse until it falls below [`TAIL_FLOOR`], from its
/// slowest pole (the section `a` closest to 1 decays by `a` every two samples), stretched by
/// the feedback, which sends each pass through the network again.
fn ring_samples(&self) -> usize {
let slowest = self.in_phase_coefs.iter().chain(self.quadrature_coefs.iter()).fold(0.0f64, |m, &a| m.max(a));
let ring = if slowest > 0.0 && slowest < 1.0 { 2.0 * TAIL_FLOOR.ln() / slowest.ln() } else { 0.0 };
let fb = f64::from(self.p.v(2) / 100.0).clamp(0.0, 0.99);
let n = (ring / (1.0 - fb)).ceil();
if n.is_finite() && n > 0.0 { n.min(f64::from(u32::MAX)) as usize } else { 0 }
}
}
impl Plugin for FreqShifter {
param_plumbing!(FREQ_SHIFTER_INFO);
/// Designs the network for the host rate, sizes the per-channel state and starts from silence
/// at the current parameters.
fn prepare(&mut self, sample_rate: f32, _max_block: usize, channels: usize) {
self.sr = f64::from(sane_sr(sample_rate));
self.design();
let channels = sane_channels(channels);
self.channels = vec![Channel::default(); channels];
self.sides = vec![Smoother::new(0.0); channels];
let sr = self.sr as f32;
for s in [&mut self.freq, &mut self.feedback, &mut self.mix].into_iter().chain(self.sides.iter_mut()) {
s.set_time(20.0, sr);
}
self.update();
self.reset();
}
/// Clears the filter states and the carrier phase and snaps the smoothers to their targets, so
/// the next block starts as a new plugin's would.
fn reset(&mut self) {
self.channels.iter_mut().for_each(|c| *c = Channel::default());
self.phase = 0.0;
for s in [&mut self.freq, &mut self.feedback, &mut self.mix].into_iter().chain(self.sides.iter_mut()) {
s.snap();
}
}
/// Shifts `frames` samples of each channel in place. One carrier and one set of smoothed
/// parameters serve every channel; each channel has its own filters, feedback sample and
/// sideband weight.
fn process(&mut self, io: &mut [Vec<f32>], frames: usize) {
let ch = self.channels.len().min(io.len());
let frames = frames_in(io, frames, ch);
for n in 0..frames {
let (sin, cos) = (TAU * self.phase).sin_cos();
let step = f64::from(self.freq.next()) / self.sr;
self.phase = (self.phase + step).fract();
let fb = f64::from(self.feedback.next());
let mix = self.mix.next();
for c in 0..ch {
let (Some(buf), Some(st), Some(side)) = (io.get_mut(c), self.channels.get_mut(c), self.sides.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 input = f64::from(dry) + fb * st.last;
let i = chain(&mut st.in_phase, &self.in_phase_coefs, input);
let q = chain(&mut st.quadrature, &self.quadrature_coefs, st.delayed);
st.delayed = input;
let wet = i * cos - f64::from(side.next()) * q * sin;
st.last = flush64(wet);
*x = dry * (1.0 - mix) + st.last as f32 * mix;
}
}
}
fn tail_samples(&self) -> usize {
self.tail
}
}
#[cfg(test)]
mod tests {
use super::*;
/// The in-phase and quadrature responses of the network at `f` cycles per sample.
fn response(coefs: &[f64; 2 * SECTIONS], f: f64) -> ((f64, f64), (f64, f64)) {
let mul = |(a, b): (f64, f64), (c, d): (f64, f64)| (a * c - b * d, a * d + b * c);
let div = |(a, b): (f64, f64), (c, d): (f64, f64)| {
let m = c * c + d * d;
((a * c + b * d) / m, (b * c - a * d) / m)
};
// z⁻² = e^(-j 4π f)
let z2 = ((2.0 * TAU * f).cos(), -(2.0 * TAU * f).sin());
let mut paths = [(1.0, 0.0), (1.0, 0.0)];
for (i, &a) in coefs.iter().enumerate() {
let h = div((a - z2.0, -z2.1), (1.0 - a * z2.0, -a * z2.1));
if let Some(p) = paths.get_mut(i % 2) {
*p = mul(*p, h);
}
}
let [i, q] = paths;
(i, mul(q, ((TAU * f).cos(), -(TAU * f).sin())))
}
/// The level of the unwanted sideband, in dB, for a phase error of `err` radians.
fn image_db(err: f64) -> f64 {
20.0 * (err / 2.0).abs().tan().max(1e-300).log10()
}
#[test]
fn network_is_ninety_degrees_across_the_band_at_every_rate() {
// Worst unwanted sideband at each rate from 20 Hz to 20 Hz below Nyquist.
for (sr, limit) in [(8_000.0, -140.0), (44_100.0, -110.0), (48_000.0, -110.0), (96_000.0, -100.0), (192_000.0, -90.0), (384_000.0, -85.0)] {
let coefs = design_quadrature(sr).unwrap();
let (lo, hi) = (QUADRATURE_LOW_HZ / sr, 0.5 - QUADRATURE_LOW_HZ / sr);
let mut worst = f64::MIN;
for k in 0..=4000 {
let f = lo * (hi / lo).powf(k as f64 / 4000.0);
let ((ir, ii), (qr, qi)) = response(&coefs, f);
// Both chains are allpass: unit gain everywhere.
assert!(((ir * ir + ii * ii).sqrt() - 1.0).abs() < 1e-9, "{sr} Hz rate: in-phase gain at {f}");
assert!(((qr * qr + qi * qi).sqrt() - 1.0).abs() < 1e-9, "{sr} Hz rate: quadrature gain at {f}");
// The quadrature chain lags the in-phase chain by 90°.
let diff = qi.atan2(qr) - ii.atan2(ir);
let diff = (diff + PI).rem_euclid(TAU) - PI;
worst = worst.max(image_db(diff + PI / 2.0));
}
assert!(worst < limit, "{sr} Hz rate: unwanted sideband at {worst:.1} dB, limit {limit} dB");
}
}
#[test]
fn network_fails_outside_its_band_as_designed() {
// At 5 Hz, a quarter of the lowest accurate frequency, the phase difference is far off 90°:
// the band edge really is at 20 Hz, not somewhere lower by accident.
let coefs = design_quadrature(48_000.0).unwrap();
let ((ir, ii), (qr, qi)) = response(&coefs, 5.0 / 48_000.0);
let diff = (qi.atan2(qr) - ii.atan2(ir) + PI).rem_euclid(TAU) - PI;
assert!(image_db(diff + PI / 2.0) > -40.0, "5 Hz: {diff}");
}
#[test]
fn coefficients_exist_for_every_host_rate() {
for sr in [1_000.0f32, 8_000.0, 22_050.0, 44_100.0, 48_000.0, 88_200.0, 176_400.0, 352_800.0, 768_000.0, 1_536_000.0] {
let coefs = design_quadrature(f64::from(sr)).unwrap_or_else(|| panic!("no network at {sr}"));
assert!(coefs.windows(2).all(|w| w[0] < w[1]), "{sr}: coefficients not increasing: {coefs:?}");
}
// Rates the design cannot serve fall back to the default network, never to nothing.
assert_eq!(design_quadrature(100.0), None);
assert_eq!(design_quadrature(f64::NAN), None);
assert_eq!(quadrature_coefs(100.0), quadrature_coefs(48_000.0));
}
#[test]
fn silence_after_sound_flushes_the_filter_states() {
let mut p = FreqShifter::new();
p.prepare(48_000.0, 512, 2);
let mut io = vec![vec![0.0f32; 512]; 2];
io[0][0] = 1.0;
io[1][0] = 1.0;
p.process(&mut io, 512);
for _ in 0..2_000 {
let mut io = vec![vec![0.0f32; 512]; 2];
p.process(&mut io, 512);
}
for c in &p.channels {
for s in c.in_phase.iter().chain(c.quadrature.iter()) {
for v in [s.x1, s.x2, s.y1, s.y2] {
assert!(v == 0.0 || v.is_normal(), "state {v:e} is denormal");
}
}
assert_eq!(c.last, 0.0);
}
}
#[test]
fn stereo_mode_shifts_left_down_and_right_up_on_two_channels_only() {
assert_eq!(sideband(MODE_STEREO, 0, 2), -1.0);
assert_eq!(sideband(MODE_STEREO, 1, 2), 1.0);
for channels in [1, 3, 4, 6, 16] {
for c in 0..channels {
assert_eq!(sideband(MODE_STEREO, c, channels), 0.0, "{c} of {channels}");
}
}
for channels in [1, 2, 6] {
for c in 0..channels {
assert_eq!(sideband(0, c, channels), 1.0);
assert_eq!(sideband(MODE_DOWN, c, channels), -1.0);
assert_eq!(sideband(MODE_BOTH, c, channels), 0.0);
}
}
}
}
+67 -1
View File
@@ -39,11 +39,28 @@ impl EventQueue {
Self { ev: Vec::with_capacity(MAX_EVENTS) }
}
/// Queue an event for the next block. A full queue drops it, unless it ends notes (a note
/// off, all notes off, the sustain pedal going up): a release takes the place of the last
/// queued event that is not one, because a dropped release leaves a voice on for good while
/// a dropped note-on or controller is only missed. Should the queue hold nothing but
/// releases, the last one becomes All Notes Off, which covers the new one too.
pub(crate) fn push(&mut self, offset: usize, kind: EventKind) {
if self.ev.len() < MAX_EVENTS {
self.ev.push(Event { offset, kind });
} else {
return;
}
let releases = |k: &EventKind| matches!(k, EventKind::Off(_) | EventKind::AllOff | EventKind::Sustain(false));
if !releases(&kind) {
log::warn!("instrument event queue full; dropping event");
return;
}
match self.ev.iter_mut().rev().find(|e| !releases(&e.kind)) {
Some(e) => *e = Event { offset, kind },
None => {
if let Some(last) = self.ev.last_mut() {
last.kind = EventKind::AllOff;
}
}
}
}
@@ -527,3 +544,52 @@ impl Plugin for SubtractiveSynth {
self.events.push(0, EventKind::AllOff);
}
}
#[cfg(test)]
mod queue_tests {
use super::*;
#[test]
fn a_full_event_queue_still_takes_releases() {
let mut q = EventQueue::new();
for _ in 0..MAX_EVENTS {
q.push(7, EventKind::Bend(1.0));
}
// Not a release: dropped. Releases: each takes the place of a queued bend.
q.push(9, EventKind::On(60, 100));
q.push(11, EventKind::Off(60));
q.push(12, EventKind::Sustain(false));
q.push(13, EventKind::AllOff);
assert_eq!(q.ev.len(), MAX_EVENTS, "never grows on the audio thread");
assert!(!q.ev.iter().any(|e| matches!(e.kind, EventKind::On(..))));
let kept: Vec<usize> = q.ev.iter().filter(|e| !matches!(e.kind, EventKind::Bend(_))).map(|e| e.offset).collect();
assert_eq!(kept.len(), 3);
assert!([11, 12, 13].iter().all(|o| kept.contains(o)), "{kept:?}");
// Nothing but releases queued: the new one is covered by an All Notes Off.
let mut q = EventQueue::new();
for n in 0..MAX_EVENTS {
q.push(0, EventKind::Off((n % 100) as u8));
}
q.push(0, EventKind::Off(120));
assert!(matches!(q.ev.last().map(|e| e.kind), Some(EventKind::AllOff)));
}
#[test]
fn a_note_off_behind_a_flood_of_controllers_still_ends_the_note() {
let mut synth = crate::create("subtractive_synth").unwrap();
synth.prepare(48_000.0, 512, 2);
let mut io = vec![vec![0.0f32; 512]; 2];
synth.note_on(0, 60, 100);
synth.process(&mut io, 512);
assert!(io[0].iter().any(|x| x.abs() > 0.01));
for _ in 0..MAX_EVENTS + 100 {
synth.midi(0, [0xE0, 0, 64]);
}
synth.note_off(0, 60);
for _ in 0..200 {
io.iter_mut().for_each(|c| c.fill(0.0));
synth.process(&mut io, 512);
}
assert!(io[0].iter().all(|x| x.abs() < 1e-4), "the voice was left on");
}
}
+235 -4
View File
@@ -631,15 +631,22 @@ fn lofi_quantizes() {
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}");
/// A plugin prepared at 48 kHz for `channels` channels with `params` set, then reset so the
/// smoothers start at the new values.
fn configured(id: &str, channels: usize, params: &[(&str, f32)]) -> Box<dyn Plugin> {
let mut p = create(id).unwrap();
p.prepare(SR, BLOCK, channels);
for &(param, v) in params {
assert!(p.set_param(param, v), "{id}.{param}");
}
p.reset();
p
}
fn ring_mod(params: &[(&str, f32)]) -> Box<dyn Plugin> {
configured("ring_mod", 2, params)
}
/// 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;
@@ -722,3 +729,227 @@ fn ring_mod_modulation_sources_move_the_frequency() {
steps.dedup_by(|a, b| a.abs_diff(*b) <= 2);
assert!(steps.len() >= 3, "sample and hold steps: {steps:?}");
}
/// Amplitude of the whole-hertz tone `hz` in the one second of `x` that starts at `from`, sampled
/// at `sr` (a whole number of hertz). Every whole-hertz tone completes whole cycles in that
/// window, so the single-bin DFT measures each one exactly, without window leakage.
fn tone_amplitude(x: &[f32], sr: usize, from: usize, hz: f64) -> f64 {
let w = std::f64::consts::TAU * hz / sr as f64;
let (re, im) = x[from..from + sr].iter().enumerate().fold((0.0f64, 0.0f64), |(re, im), (n, &v)| {
let (s, c) = (w * n as f64).sin_cos();
(re + f64::from(v) * c, im - f64::from(v) * s)
});
2.0 / sr as f64 * re.hypot(im)
}
/// Asserts the levels of whole-hertz tones in the second second of `x` (the first lets the
/// filters settle): each `(hz, amplitude)` within 0.001 dB, and each of `absent` at least
/// 100 dB below `reference`.
fn assert_tones(x: &[f32], sr: usize, tones: &[(f64, f64)], absent: &[f64], reference: f64, what: &str) {
assert_tones_below(x, sr, tones, absent, reference, -100.0, what);
}
/// [`assert_tones`] with the level `absent` tones must stay under, in dB relative to `reference`.
fn assert_tones_below(x: &[f32], sr: usize, tones: &[(f64, f64)], absent: &[f64], reference: f64, limit_db: f64, what: &str) {
for &(hz, want) in tones {
let got = tone_amplitude(x, sr, sr, hz);
let err_db = 20.0 * (got / want).log10();
assert!(err_db.abs() < 0.001, "{what}: {hz} Hz at {got}, want {want} ({err_db:+.5} dB)");
}
for &hz in absent {
let rel = 20.0 * (tone_amplitude(x, sr, sr, hz) / reference).log10();
assert!(rel < limit_db, "{what}: {hz} Hz only {rel:.1} dB down, limit {limit_db} dB");
}
}
/// A 0.5-amplitude sine at `hz` and `sr`, with the phase computed in `f64` so that rounding of
/// a large `f32` phase does not add jitter sidebands to the measurements.
fn pure_sine(hz: f64, sr: usize, len: usize) -> Vec<f32> {
(0..len).map(|i| (0.5 * (std::f64::consts::TAU * hz * i as f64 / sr as f64).sin()) as f32).collect()
}
fn freq_shifter(channels: usize, params: &[(&str, f32)]) -> Box<dyn Plugin> {
configured("freq_shifter", channels, params)
}
const FS_SR: usize = SR as usize;
#[test]
fn freq_shifter_moves_a_tone_up_or_down_by_the_frequency() {
let x = pure_sine(1000.0, FS_SR, 2 * FS_SR);
let mut up = freq_shifter(1, &[("frequency", 200.0), ("mode", 0.0)]);
let out = run(up.as_mut(), std::slice::from_ref(&x));
assert_tones(&out[0], FS_SR, &[(1200.0, 0.5)], &[800.0, 1000.0, 1400.0], 0.5, "up 200 Hz");
let mut down = freq_shifter(1, &[("frequency", 200.0), ("mode", 1.0)]);
let out = run(down.as_mut(), std::slice::from_ref(&x));
assert_tones(&out[0], FS_SR, &[(800.0, 0.5)], &[600.0, 1000.0, 1200.0], 0.5, "down 200 Hz");
// A component pushed below 0 Hz comes back mirrored: 200 Hz down by 300 Hz is 100 Hz.
let low = pure_sine(200.0, FS_SR, 2 * FS_SR);
let mut p = freq_shifter(1, &[("frequency", 300.0), ("mode", 1.0)]);
let out = run(p.as_mut(), &[low]);
assert_tones(&out[0], FS_SR, &[(100.0, 0.5)], &[200.0, 500.0], 0.5, "down past 0 Hz");
}
#[test]
fn freq_shifter_up_and_down_and_stereo_modes() {
let x = pure_sine(1000.0, FS_SR, 2 * FS_SR);
// Up & Down: both sidebands at half level, nothing left at the input frequency.
let mut p = freq_shifter(2, &[("frequency", 150.0), ("mode", 2.0)]);
let out = run(p.as_mut(), &[x.clone(), x.clone()]);
for c in &out {
assert_tones(c, FS_SR, &[(850.0, 0.25), (1150.0, 0.25)], &[1000.0], 0.25, "up & down");
}
// Stereo: the left channel goes down and the right channel up.
let mut p = freq_shifter(2, &[("frequency", 150.0), ("mode", 3.0)]);
let out = run(p.as_mut(), &[x.clone(), x.clone()]);
assert_tones(&out[0], FS_SR, &[(850.0, 0.5)], &[1000.0, 1150.0], 0.5, "stereo left");
assert_tones(&out[1], FS_SR, &[(1150.0, 0.5)], &[850.0, 1000.0], 0.5, "stereo right");
// A mono insert has no sides, so Stereo plays both sidebands, like Up & Down.
let mut p = freq_shifter(1, &[("frequency", 150.0), ("mode", 3.0)]);
let out = run(p.as_mut(), std::slice::from_ref(&x));
assert_tones(&out[0], FS_SR, &[(850.0, 0.25), (1150.0, 0.25)], &[1000.0], 0.25, "stereo on mono");
}
#[test]
fn freq_shifter_feedback_adds_one_more_shift_per_pass() {
// Each pass through the loop shifts again and scales by the feedback: 1.2, 1.4, 1.6 and 1.8 kHz
// at 1/2, 1/4, 1/8 and 1/16.
let x = pure_sine(1000.0, FS_SR, 2 * FS_SR);
let mut p = freq_shifter(1, &[("frequency", 200.0), ("feedback", 50.0)]);
let out = run(p.as_mut(), &[x]);
let trail = [(1200.0, 0.5), (1400.0, 0.25), (1600.0, 0.125), (1800.0, 0.0625)];
assert_tones(&out[0], FS_SR, &trail, &[800.0, 1000.0], 0.5, "feedback 50 %");
// Feedback stops at 70 %, the most the loop is proven stable with.
assert!(p.set_param("feedback", 100.0));
assert_eq!(p.param("feedback"), Some(70.0));
}
#[test]
fn freq_shifter_mix_blends_the_dry_input() {
let x = pure_sine(1000.0, FS_SR, 2 * FS_SR);
let mut p = freq_shifter(1, &[("frequency", 200.0), ("mix", 50.0)]);
let out = run(p.as_mut(), &[x]);
assert_tones(&out[0], FS_SR, &[(1000.0, 0.25), (1200.0, 0.25)], &[800.0], 0.25, "mix 50 %");
}
#[test]
fn freq_shifter_is_accurate_near_both_band_edges() {
// 44.1 kHz: a 40 Hz tone up by 10 Hz, and a 20 kHz tone down by 1 kHz.
let sr = 44_100usize;
for (from, by, mode, to, image) in [(40.0f32, 10.0f32, 0.0f32, 50.0, 30.0), (20_000.0, 1_000.0, 1.0, 19_000.0, 21_000.0)] {
let mut p = create("freq_shifter").unwrap();
p.prepare(sr as f32, BLOCK, 1);
p.set_param("frequency", by);
p.set_param("mode", mode);
p.reset();
let x = pure_sine(f64::from(from), sr, 2 * sr);
let out = run(p.as_mut(), &[x]);
assert_tones(&out[0], sr, &[(to, 0.5)], &[image, f64::from(from)], 0.5, &format!("{from} Hz by {by} Hz"));
}
}
#[test]
fn freq_shifter_output_does_not_depend_on_the_block_size_and_reset_restarts_it() {
let x = pure_sine(440.0, FS_SR, 6000);
let params = [("frequency", 37.0), ("mode", 3.0), ("feedback", 60.0), ("mix", 80.0)];
let mut p = freq_shifter(2, &params);
let whole = run(p.as_mut(), &[x.clone(), x.clone()]);
let mut one = freq_shifter(2, &params);
let mut by_sample = vec![Vec::new(), Vec::new()];
for &v in &x {
let mut buf = vec![vec![v], vec![v]];
one.process(&mut buf, 1);
by_sample[0].push(buf[0][0]);
by_sample[1].push(buf[1][0]);
}
assert_eq!(whole, by_sample);
p.reset();
assert_eq!(run(p.as_mut(), &[x.clone(), x]), whole);
}
#[test]
fn freq_shifter_tail_covers_the_ringing() {
for feedback in [0.0, 70.0] {
let mut p = freq_shifter(1, &[("frequency", 5.0), ("feedback", feedback)]);
let mut x = vec![0.0f32; 4 * FS_SR];
x[0] = 1.0;
let out = run(p.as_mut(), &[x]);
let last = out[0].iter().rposition(|v| v.abs() > 1e-4).unwrap();
assert!(last < p.tail_samples(), "feedback {feedback}: ringing at {last}, tail {}", p.tail_samples());
}
}
/// A plugin prepared at `sr` for `channels` channels with `params` set, then reset.
fn freq_shifter_at(sr: usize, channels: usize, params: &[(&str, f32)]) -> Box<dyn Plugin> {
let mut p = create("freq_shifter").unwrap();
p.prepare(sr as f32, BLOCK, channels);
for &(param, v) in params {
assert!(p.set_param(param, v), "freq_shifter.{param}");
}
p.reset();
p
}
#[test]
fn freq_shifter_rejects_the_image_at_every_common_rate() {
// A 1 kHz tone shifted up and down by 200 Hz: the shifted tone at full level within 0.001 dB,
// and the other sideband and the input at least 100 dB down up to 96 kHz and 95 dB at 192 kHz
// (the 20 Hz band edge is a smaller fraction of a higher rate).
for (sr, limit) in [(22_050, -100.0), (44_100, -100.0), (48_000, -100.0), (88_200, -100.0), (96_000, -100.0), (192_000, -95.0)] {
let x = pure_sine(1000.0, sr, 2 * sr);
for (mode, to, image) in [(0.0, 1200.0, 800.0), (1.0, 800.0, 1200.0)] {
let mut p = freq_shifter_at(sr, 1, &[("frequency", 200.0), ("mode", mode)]);
let out = run(p.as_mut(), std::slice::from_ref(&x));
assert_tones_below(&out[0], sr, &[(to, 0.5)], &[image, 1000.0], 0.5, limit, &format!("{sr} Hz, mode {mode}"));
}
}
}
#[test]
fn freq_shifter_processes_every_channel_of_a_multichannel_insert() {
// A 5.1-wide insert: Up shifts all six channels. Stereo needs a known left and right, which only
// a two-channel insert has, so on six channels it plays both sidebands everywhere.
let x = pure_sine(1000.0, FS_SR, 2 * FS_SR);
let input = vec![x; 6];
let mut p = freq_shifter(6, &[("frequency", 150.0), ("mode", 0.0)]);
for (c, out) in run(p.as_mut(), &input).iter().enumerate() {
assert_tones(out, FS_SR, &[(1150.0, 0.5)], &[850.0, 1000.0], 0.5, &format!("up, channel {c}"));
}
let mut p = freq_shifter(6, &[("frequency", 150.0), ("mode", 3.0)]);
for (c, out) in run(p.as_mut(), &input).iter().enumerate() {
assert_tones(out, FS_SR, &[(850.0, 0.25), (1150.0, 0.25)], &[1000.0], 0.25, &format!("stereo, channel {c}"));
}
}
/// The largest change between two neighbouring samples of `x`.
fn max_step(x: &[f32]) -> f32 {
x.windows(2).fold(0.0f32, |m, w| m.max((w[1] - w[0]).abs()))
}
#[test]
fn freq_shifter_parameter_changes_do_not_click() {
// Each change lands mid-block on a running 1 kHz sine, a quarter of a 200 Hz carrier cycle
// after a whole one, where its sine is at its peak and an instant sideband switch would jump
// the furthest. Across the change, no step between two samples may be more than 5 % larger
// than the largest step of the settled output before or after it.
const AT: usize = 24_060;
let x = sine(1000.0, 0.5, 48_000);
let changes =
[("mode", 0.0, 1.0), ("mode", 1.0, 3.0), ("mode", 3.0, 2.0), ("frequency", 200.0, 4000.0), ("mix", 100.0, 0.0), ("feedback", 0.0, 70.0)];
for (id, from, to) in changes {
let mut p = freq_shifter(2, &[("frequency", 200.0), (id, from)]);
let before = run(p.as_mut(), &[x[..AT].to_vec(), x[..AT].to_vec()]);
assert!(p.set_param(id, to));
let after = run(p.as_mut(), &[x[AT..].to_vec(), x[AT..].to_vec()]);
for c in 0..2 {
let limit = 1.05 * max_step(&before[c][12_000..]).max(max_step(&after[c][12_000..]));
let across: Vec<f32> = before[c][12_000..].iter().chain(&after[c][..12_000]).copied().collect();
let step = max_step(&across);
assert!(step <= limit, "{id} {from} -> {to}, channel {c}: step {step}, limit {limit}");
}
}
}
+5 -2
View File
@@ -8,8 +8,11 @@ rust-version.workspace = true
repository.workspace = true
[dependencies]
craft-score.workspace = true
craft-xml.workspace = true
soundcraft-time.workspace = true
soundcraft-audio-io.workspace = true
craft-audio.workspace = true
craft-scraft-format.workspace = true
soundcraft-midi.workspace = true
soundcraft-dsp.workspace = true
soundcraft-clap-host.workspace = true
@@ -22,7 +25,6 @@ serde.workspace = true
serde_json.workspace = true
thiserror.workspace = true
log.workspace = true
zip.workspace = true
craft-docpath.workspace = true
[target.'cfg(windows)'.dependencies]
@@ -30,6 +32,7 @@ winsafe = { version = "0.0.29", features = ["kernel"] }
[dev-dependencies]
proptest.workspace = true
zip.workspace = true
[lints]
workspace = true
+8 -8
View File
@@ -3,7 +3,7 @@
use crate::io::{Recording, open_session_linked, sanitize_name};
use crate::{Engine, EngineError, Result};
use soundcraft_audio_io::{BitDepth, EncodeOptions, FileFormat};
use craft_audio::{BitDepth, EncodeOptions};
use soundcraft_model::{Session, SourceAudio, SourceId};
use std::collections::BTreeMap;
use std::io::Write;
@@ -248,8 +248,8 @@ impl Autosave {
crate::io::session_size("the autosave", text.len() as u64, true)?;
self.next = next;
for (name, audio) in &fresh {
let opts = EncodeOptions { format: FileFormat::Wav, bit_depth: BitDepth::Float32, dither: false, bwf: None };
let bytes = soundcraft_audio_io::encode(&audio.buffer, &opts).map_err(|err| EngineError::Io(err.to_string()))?;
let opts = EncodeOptions::new(craft_audio::EncodeFormat::Wav).with_bit_depth(BitDepth::Float32);
let bytes = craft_audio::encode(&audio.buffer, &opts).map_err(|err| EngineError::Io(err.to_string()))?;
create_folder(&audio_dir).and_then(|()| write_synced(&audio_dir.join(name), &bytes)).map_err(|err| io_error(&audio_dir, err))?;
}
write_atomically(&self.dir.join(ORIGIN), origin.unwrap_or_default().as_bytes())?;
@@ -404,7 +404,7 @@ fn repair_take(path: &Path, folders: &[PathBuf], channels: usize, rate: u32) ->
if !folder.is_some_and(|d| folders.contains(&d)) {
return Err(refuse("not in a folder SoundCraft records into"));
}
let header = |frames| soundcraft_audio_io::wav_float_header(channels, rate, frames).map_err(std::io::Error::other);
let header = |frames| craft_audio::wav_float_header(rate, u16::try_from(channels).unwrap_or(u16::MAX), frames).map_err(std::io::Error::other);
let expected = header(0)?;
let mut f = std::fs::OpenOptions::new().read(true).write(true).open(path)?;
let mut head = vec![0u8; expected.len()];
@@ -502,6 +502,7 @@ fn io_error(path: &Path, err: std::io::Error) -> EngineError {
mod tests {
use super::*;
use crate::io::open_session;
use craft_audio::AudioBuffer;
#[test]
fn unique_folders_are_new_and_owner_only() {
@@ -522,8 +523,7 @@ mod tests {
}
use serde_json::json;
use soundcraft_audio_io::AudioBuffer;
use soundcraft_model::{ChannelFormat, Clip, TrackKind};
use soundcraft_model::{ChannelFormat, Clip, FileFormat, TrackKind};
/// Recovery loads document edits without re-enabling live inputs from an autosave.
#[test]
@@ -719,7 +719,7 @@ mod tests {
let take = root.join(sanitize_name(&e.session().name)).join("Audio Files/Vox_01.wav");
std::fs::create_dir_all(take.parent().unwrap()).unwrap();
let audio: Vec<u8> = (0..480).flat_map(|i| (i as f32 / 1000.0).to_le_bytes()).collect();
std::fs::write(&take, [soundcraft_audio_io::wav_float_header(1, 48_000, 100).unwrap(), audio].concat()).unwrap();
std::fs::write(&take, [craft_audio::wav_float_header(48_000, 1, 100).unwrap(), audio].concat()).unwrap();
let plan = Recording { start: 48_000, pass: None, punch: None, rate: 48_000, takes: vec![Take { track, path: take, channels: 1 }] };
let clips = |e: &Engine| e.session().track(track).unwrap().clips().iter().map(|c| (c.start, c.length)).collect::<Vec<_>>();
let w = Autosaver::new(root.clone());
@@ -750,7 +750,7 @@ mod tests {
soundcraft_model::TrackId(e.execute("track.new", &json!({"name": "Vox", "format": "mono"})).unwrap()["tracks"][0].as_u64().unwrap());
let takes = root.join(sanitize_name(&e.session().name)).join("Audio Files");
std::fs::create_dir_all(&takes).unwrap();
let wav = [soundcraft_audio_io::wav_float_header(1, 48_000, 0).unwrap(), vec![0; 4_000]].concat();
let wav = [craft_audio::wav_float_header(48_000, 1, 0).unwrap(), vec![0; 4_000]].concat();
let text = b"not audio at all, but long enough to be mistaken for a header if nobody looked at it ......".repeat(4);
let files =
[(takes.join("notes.wav"), text.clone()), (dir.join("Elsewhere/Vox_01.wav"), wav.clone()), (takes.join("../Vox_02.wav"), wav.clone())];
+8 -7
View File
@@ -5,8 +5,9 @@
//! as 32-bit float WAVs in a `<name> Audio/` folder next to it.
use crate::{Engine, EngineError, Result};
use craft_audio::EncodeOptions;
use serde::{Deserialize, Serialize};
use soundcraft_audio_io::{EncodeOptions, FileFormat};
use soundcraft_model::FileFormat;
use soundcraft_model::{Clip, ClipContent, ClipId, SourceId, TrackId};
use soundcraft_time::Samples;
use std::collections::BTreeMap;
@@ -65,8 +66,8 @@ pub fn export(e: &Engine, ids: &[ClipId], path: &str) -> Result<usize> {
{
let rel = format!("{audio_dir_name}/{}_{}.wav", crate::io::sanitize_name(&src.name), source.0);
crate::io::make_dir(dir.join(&audio_dir_name))?;
let opts = EncodeOptions { format: FileFormat::Wav, bit_depth: soundcraft_audio_io::BitDepth::Float32, dither: false, bwf: None };
let bytes = soundcraft_audio_io::encode(&buf.buffer, &opts).map_err(|err| EngineError::Io(err.to_string()))?;
let opts = EncodeOptions::new(craft_audio::EncodeFormat::Wav).with_bit_depth(craft_audio::BitDepth::Float32);
let bytes = craft_audio::encode(&buf.buffer, &opts).map_err(|err| EngineError::Io(err.to_string()))?;
crate::io::write_path(dir.join(&rel), bytes)?;
slot.insert(rel);
}
@@ -85,7 +86,7 @@ fn decode_group_audio(
dir: &Path,
rate: u32,
cmd_id: &str,
) -> Result<(Vec<(String, soundcraft_audio_io::AudioBuffer)>, BTreeMap<u64, usize>)> {
) -> Result<(Vec<(String, craft_audio::AudioBuffer)>, BTreeMap<u64, usize>)> {
let (mut decoded, mut index, mut file_of) = (Vec::new(), BTreeMap::new(), BTreeMap::new());
// The folder as the file system knows it, so a link among the group's files can't lead out.
let dir = if dir.as_os_str().is_empty() { Path::new(".") } else { dir };
@@ -108,7 +109,7 @@ fn decode_group_audio(
Some(n) => *n,
None => {
let bytes = crate::io::read_file_inside(&full, &root)?;
let (_, buf) = soundcraft_audio_io::decode(&bytes, Some("wav")).map_err(|err| EngineError::Io(format!("{rel}: {err}")))?;
let (_, buf) = craft_audio::decode(&bytes, Some("wav")).map_err(|err| EngineError::Io(format!("{rel}: {err}")))?;
let buf = crate::io::at_rate(buf, rate, crate::io::MAX_IMPORT_SAMPLES)
.map_err(|r| EngineError::Io(format!("{rel}: {}", r.message("too large to import"))))?;
decoded.push((rel.clone(), buf));
@@ -414,8 +415,8 @@ mod tests {
fn a_file_named_by_many_ids_becomes_one_source() {
let dir = std::env::temp_dir().join(format!("sc-grp-shared-{}", std::process::id()));
let _ = std::fs::create_dir_all(&dir);
let audio = soundcraft_audio_io::AudioBuffer { sample_rate: 48_000, channels: vec![vec![0.25; 480]] };
std::fs::write(dir.join("k.wav"), soundcraft_audio_io::encode(&audio, &soundcraft_audio_io::EncodeOptions::default()).unwrap()).unwrap();
let audio = craft_audio::AudioBuffer { sample_rate: 48_000, channels: vec![vec![0.25; 480]] };
std::fs::write(dir.join("k.wav"), craft_audio::encode(&audio, &craft_audio::EncodeOptions::default()).unwrap()).unwrap();
let clip = |id: u64| json!({"id": id, "name": "k", "start": id * 1000, "length": 480, "content": {"Audio": {"source": id, "offset": 0}}});
let audio_paths = json!({"1": "k.wav", "2": "./k.wav", "3": "k.wav"});
let file = json!({"format": "soundcraft.clipgroup", "version": 1, "sample_rate": 48000, "length": 4000, "lanes": [[clip(1), clip(2), clip(3)]], "audio": audio_paths});
+2 -2
View File
@@ -47,7 +47,7 @@ fn audio_as_midi(e: &mut Engine, p: &Value) -> Result<Value> {
#[cfg(test)]
mod tests {
use super::*;
use soundcraft_audio_io::AudioBuffer;
use craft_audio::AudioBuffer;
use soundcraft_model::{ChannelFormat, TrackKind};
#[test]
@@ -59,7 +59,7 @@ mod tests {
a.extend(tone(330.0));
let frames = a.len() as i64;
let s = e.session_mut();
let src = crate::io::add_source(s, "mel", AudioBuffer { sample_rate: 48_000, channels: vec![a] }, None, soundcraft_audio_io::FileFormat::Wav);
let src = crate::io::add_source(s, "mel", AudioBuffer { sample_rate: 48_000, channels: vec![a] }, None, soundcraft_model::FileFormat::Wav);
let t = s.add_track(TrackKind::Audio, ChannelFormat::Mono, None);
let cid = s.new_clip_id();
crate::edit::place_clip(s, t, Clip::audio(cid, "mel", src, 0, 0, frames));
+4 -6
View File
@@ -3,9 +3,10 @@ use super::{CommandSpec, bad, clip_ids_param, has_selection, range_param, str_pa
use soundcraft_time::Range;
use crate::{Engine, Result, cmd};
use craft_audio::AudioBuffer;
use serde_json::{Value, json};
use soundcraft_audio_io::{AudioBuffer, FileFormat};
use soundcraft_dsp::offline::{SpeedRampChange, SpeedRampSelection};
use soundcraft_model::FileFormat;
use soundcraft_model::{Clip, ClipContent, TrackKind};
pub fn specs() -> Vec<CommandSpec> {
@@ -149,10 +150,7 @@ fn process(e: &mut Engine, p: &Value) -> Result<Value> {
nc.gain_db = 0.0;
nc.gain_env.clear();
}
if let Some(pl) = s.track_mut(track).and_then(|t| t.playlist_mut()) {
pl.clips.retain(|c| c.id != id);
}
crate::edit::place_clip(s, track, nc);
super::more_util::replace_clip(s, track, id, nc);
done += 1;
}
Ok(json!({"processed": done, "process": name}))
@@ -214,7 +212,7 @@ fn short(n: &str) -> String {
#[cfg(test)]
mod tests {
use super::*;
use soundcraft_audio_io::AudioBuffer;
use craft_audio::AudioBuffer;
use soundcraft_model::{ChannelFormat, Session, TrackKind};
fn gained_clip() -> (crate::Engine, soundcraft_model::ClipId) {
+4 -4
View File
@@ -464,8 +464,8 @@ mod tests {
let mut s = Session::default();
let t = s.add_track(TrackKind::Audio, ChannelFormat::Mono, Some("Elastic"));
let samples = (0..source_len).map(|i| if i >= tone_at { (i as f32 * 0.2).sin() * 0.4 } else { 0.0 }).collect();
let buf = soundcraft_audio_io::AudioBuffer { sample_rate: 48_000, channels: vec![samples] };
let src = crate::io::add_source(&mut s, "Tone", buf, None, soundcraft_audio_io::FileFormat::Wav);
let buf = craft_audio::AudioBuffer { sample_rate: 48_000, channels: vec![samples] };
let src = crate::io::add_source(&mut s, "Tone", buf, None, soundcraft_model::FileFormat::Wav);
let cid = s.new_clip_id();
crate::edit::place_clip(&mut s, t, Clip::audio(cid, "Clip", src, 0, 0, clip_len));
let mut e = crate::Engine::new(s);
@@ -552,8 +552,8 @@ mod tests {
use soundcraft_model::{ChannelFormat, Clip, Session, TrackKind};
let mut s = Session::default();
let t = s.add_track(TrackKind::Audio, ChannelFormat::Mono, Some("Dialog"));
let buf = soundcraft_audio_io::AudioBuffer { sample_rate: 48_000, channels: vec![vec![0.1; 10_000]] };
let src = crate::io::add_source(&mut s, "Line", buf, None, soundcraft_audio_io::FileFormat::Wav);
let buf = craft_audio::AudioBuffer { sample_rate: 48_000, channels: vec![vec![0.1; 10_000]] };
let src = crate::io::add_source(&mut s, "Line", buf, None, soundcraft_model::FileFormat::Wav);
let ids = [0, 1, 2].map(|k| {
let cid = s.new_clip_id();
crate::edit::place_clip(&mut s, t, Clip::audio(cid, "Line", src, 0, k * 12_000, 10_000));
+2 -1
View File
@@ -66,7 +66,8 @@ fn clear(e: &mut Engine, p: &Value) -> Result<Value> {
#[cfg(test)]
mod tests {
use super::*;
use soundcraft_audio_io::{AudioBuffer, FileFormat};
use craft_audio::AudioBuffer;
use soundcraft_model::FileFormat;
fn kick(e: &Engine) -> (SourceId, Vec<ClipId>) {
let s = e.session();
+6 -6
View File
@@ -511,8 +511,8 @@ mod tests {
let mut s = Session::default();
let t = s.add_track(TrackKind::Audio, ChannelFormat::Mono, None);
let tone: Vec<f32> = (0..12_000).map(|i| (i as f32 * 0.07).sin() * 0.5).collect();
let buf = soundcraft_audio_io::AudioBuffer { sample_rate: 48_000, channels: vec![tone] };
let src = crate::io::add_source(&mut s, "tone", buf, None, soundcraft_audio_io::FileFormat::Wav);
let buf = craft_audio::AudioBuffer { sample_rate: 48_000, channels: vec![tone] };
let src = crate::io::add_source(&mut s, "tone", buf, None, soundcraft_model::FileFormat::Wav);
let id = s.new_clip_id();
let mut c = Clip::audio(id, "tone", src, 0, 0, 18_000);
c.stretch = 1.5;
@@ -544,8 +544,8 @@ mod tests {
let mut s = Session::default();
let t = s.add_track(TrackKind::Audio, ChannelFormat::Mono, None);
let tone: Vec<f32> = (0..9_600).map(|i| (i as f32 * 0.05).sin() * 0.4).collect();
let buf = soundcraft_audio_io::AudioBuffer { sample_rate: 48_000, channels: vec![tone] };
let src = crate::io::add_source(&mut s, "tone", buf, None, soundcraft_audio_io::FileFormat::Wav);
let buf = craft_audio::AudioBuffer { sample_rate: 48_000, channels: vec![tone] };
let src = crate::io::add_source(&mut s, "tone", buf, None, soundcraft_model::FileFormat::Wav);
let id = s.new_clip_id();
crate::edit::place_clip(&mut s, t, Clip::audio(id, "tone", src, 0, 0, 9_600));
s.edit.selected_clips = vec![id];
@@ -575,8 +575,8 @@ mod tests {
let mut s = Session::default();
let t = s.add_track(TrackKind::Audio, ChannelFormat::Mono, None);
let tone: Vec<f32> = (0..9_600).map(|i| (i as f32 * 0.05).sin() * 0.4).collect();
let buf = soundcraft_audio_io::AudioBuffer { sample_rate: 48_000, channels: vec![tone] };
let src = crate::io::add_source(&mut s, "tone", buf, None, soundcraft_audio_io::FileFormat::Wav);
let buf = craft_audio::AudioBuffer { sample_rate: 48_000, channels: vec![tone] };
let src = crate::io::add_source(&mut s, "tone", buf, None, soundcraft_model::FileFormat::Wav);
let id = s.new_clip_id();
crate::edit::place_clip(&mut s, t, Clip::audio(id, "tone", src, 0, 0, 9_600));
s.edit.selected_clips = vec![id];
+7 -3
View File
@@ -258,7 +258,8 @@ fn cut_copy(e: &mut Engine, p: &Value, cut: bool, id: &str) -> Result<Value> {
let mut cb = Clipboard::of_range(e.session(), range);
let follow = e.session().edit.automation_follows_edit;
for t in &tracks {
cb.tracks.push(ops::copy_range(e.session(), *t, range));
// Cut carries only what it removes; a locked clip stays where it is (see `ops::take_range`).
cb.tracks.push(if cut { ops::take_range(e.session(), *t, range) } else { ops::copy_range(e.session(), *t, range) });
let lanes = e
.session()
.track(*t)
@@ -1022,6 +1023,9 @@ fn consolidate(e: &mut Engine, p: &Value) -> Result<Value> {
if r.is_empty() {
return Err(bad("edit.consolidate", "selection is empty"));
}
if tracks.iter().any(|&t| ops::holds_locked(e.session(), t, r)) {
return Err(bad("edit.consolidate", ops::LOCKED_IN_RANGE));
}
let midi = tracks.iter().filter(|&&t| crate::edit::consolidate_midi(e.session_mut(), t, r)).count();
let mut n = 0;
for t in tracks {
@@ -1228,8 +1232,8 @@ mod tests {
use soundcraft_model::{ChannelFormat, Clip, TrackKind};
let mut s = Session::default();
let t = s.add_track(TrackKind::Audio, ChannelFormat::Mono, None);
let buf = soundcraft_audio_io::AudioBuffer { sample_rate: s.sample_rate.hz(), channels: vec![vec![0.25; 60_000]] };
let src = crate::io::add_source(&mut s, "src", buf, None, soundcraft_audio_io::FileFormat::Wav);
let buf = craft_audio::AudioBuffer { sample_rate: s.sample_rate.hz(), channels: vec![vec![0.25; 60_000]] };
let src = crate::io::add_source(&mut s, "src", buf, None, soundcraft_model::FileFormat::Wav);
let mut ids = Vec::new();
for &(start, len, off) in clips {
let id = s.new_clip_id();
+4 -3
View File
@@ -723,7 +723,8 @@ fn to_playlist(e: &mut Engine, p: &Value, dest: Dest, mv: bool, id: &str) -> Res
tr.playlists.len() - 1
}
};
let clips = ops::copy_range(s, *t, r);
// A move carries only what it clears from the main playlist; locked clips stay there.
let clips = if mv { ops::take_range(s, *t, r) } else { ops::copy_range(s, *t, r) };
let main = s.track(*t).map_or(0, |tr| tr.active_playlist);
if let Some(tr) = s.track_mut(*t) {
tr.active_playlist = idx;
@@ -1158,8 +1159,8 @@ mod tests {
let mut s = Session::default();
let t = s.add_track(TrackKind::Audio, ChannelFormat::Mono, None);
let tone: Vec<f32> = (0..24_000).map(|i| (i as f32 * 0.05).sin() * 0.5).collect();
let buf = soundcraft_audio_io::AudioBuffer { sample_rate: 48_000, channels: vec![tone] };
let src = crate::io::add_source(&mut s, "tone", buf, None, soundcraft_audio_io::FileFormat::Wav);
let buf = craft_audio::AudioBuffer { sample_rate: 48_000, channels: vec![tone] };
let src = crate::io::add_source(&mut s, "tone", buf, None, soundcraft_model::FileFormat::Wav);
let id = s.new_clip_id();
ops::place_clip(&mut s, t, Clip::audio(id, "tone", src, 0, 0, 24_000));
s.edit.selected_tracks = vec![t];
+8 -3
View File
@@ -186,9 +186,7 @@ fn cut_time(e: &mut Engine, p: &Value) -> Result<Value> {
let s = e.session_mut();
let ids: Vec<_> = s.tracks.iter().map(|t| t.id).collect();
for t in ids {
crate::edit::separate_all_at(s, t, r.start);
crate::edit::separate_all_at(s, t, r.end);
crate::edit::clear_range(s, t, r, false);
crate::edit::clear_all_range(s, t, r, false);
crate::edit::shift_from(s, t, r.end, -r.len());
if let Some(tr) = s.track_mut(t) {
for l in &mut tr.automation {
@@ -240,6 +238,7 @@ fn grid_ticks(p: &Value) -> i64 {
"1/1" | "1" | "bar" => TICKS_PER_QUARTER * 4,
"1/2" => TICKS_PER_QUARTER * 2,
"1/4" => TICKS_PER_QUARTER,
"1/4t" => TICKS_PER_QUARTER * 2 / 3,
"1/8" => TICKS_PER_QUARTER / 2,
"1/8t" => TICKS_PER_QUARTER / 3,
"1/16t" => TICKS_PER_QUARTER / 6,
@@ -432,6 +431,12 @@ mod tests {
use crate::demo::demo_engine;
use serde_json::json;
#[test]
fn grid_ticks_accepts_the_quarter_triplet() {
use super::{TICKS_PER_QUARTER, grid_ticks};
assert_eq!(grid_ticks(&json!({"grid": "1/4t"})), TICKS_PER_QUARTER * 2 / 3);
}
#[test]
fn tempo_constant_with_an_empty_range_sets_the_tempo_at_its_start() {
use soundcraft_time::Range;
+12 -12
View File
@@ -98,17 +98,17 @@ pub fn specs() -> Vec<CommandSpec> {
None => "wav",
Some(value) => value.as_str().ok_or_else(|| bad("file.bounce_stems", "`format` must be a string"))?,
};
let format = soundcraft_audio_io::FileFormat::from_write_extension(format_name).ok_or_else(|| {
let supported = soundcraft_audio_io::FileFormat::writable_extensions().join(", ");
let format = craft_audio::encode_format_for(format_name).ok_or_else(|| {
let supported = craft_audio::ENCODE_EXTENSIONS.join(", ");
bad("file.bounce_stems", format!("cannot write `{format_name}` files (supported: {supported})"))
})?;
let bit_depth = match p.get("bit_depth").map(|b| b.to_string().trim_matches('"').to_string()).as_deref() {
Some("16") => soundcraft_audio_io::BitDepth::Int16,
Some("32") | Some("32f") => soundcraft_audio_io::BitDepth::Float32,
_ => soundcraft_audio_io::BitDepth::Int24,
Some("16") => craft_audio::BitDepth::Int16,
Some("32") | Some("32f") => craft_audio::BitDepth::Float32,
_ => craft_audio::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: bool_or(p, "dither", true), bwf: None }, fold)?;
let files = crate::io::bounce_stems_with(e, &dir, r, &craft_audio::EncodeOptions::new(format).with_bit_depth(bit_depth).with_dither(bool_or(p, "dither", true)), fold)?;
Ok(json!({"files": files}))
}),
cmd!(noundo "file.export_midi", "MIDI...", ["File", "Export"], None, "{path, tracks?}", always, |e, p| {
@@ -318,16 +318,16 @@ pub fn bounce_job(e: &Engine, p: &Value) -> Result<crate::io::Bounce> {
let r = if r.is_empty() { soundcraft_time::Range::new(0, e.session().content_end().max(1)) } else { r };
let ext = std::path::Path::new(&path).extension().and_then(std::ffi::OsStr::to_str).unwrap_or("wav").to_ascii_lowercase();
let format_name = str_param(p, "format").unwrap_or(ext.as_str());
let format = soundcraft_audio_io::FileFormat::from_write_extension(format_name).ok_or_else(|| {
let supported = soundcraft_audio_io::FileFormat::writable_extensions().join(", ");
let format = craft_audio::encode_format_for(format_name).ok_or_else(|| {
let supported = craft_audio::ENCODE_EXTENSIONS.join(", ");
bad("file.bounce_mix", format!("cannot write `{format_name}` files (supported: {supported})"))
})?;
let bit_depth = match p.get("bit_depth").map(|b| b.to_string().trim_matches('"').to_string()).as_deref() {
Some("16") => soundcraft_audio_io::BitDepth::Int16,
Some("32") | Some("32f") => soundcraft_audio_io::BitDepth::Float32,
_ => soundcraft_audio_io::BitDepth::Int24,
Some("16") => craft_audio::BitDepth::Int16,
Some("32") | Some("32f") => craft_audio::BitDepth::Float32,
_ => craft_audio::BitDepth::Int24,
};
let opts = soundcraft_audio_io::EncodeOptions { format, bit_depth, dither: bool_or(p, "dither", true), bwf: None };
let opts = craft_audio::EncodeOptions::new(format).with_bit_depth(bit_depth).with_dither(bool_or(p, "dither", true));
let normalize = bool_or(p, "normalize", false);
let fold = fold_down_param(p, "file.bounce_mix")?;
let bus = bounce_bus(e, p)?;
+7 -7
View File
@@ -1,12 +1,12 @@
use super::*;
use crate::Engine;
use soundcraft_audio_io::{AudioBuffer, EncodeOptions};
use craft_audio::{AudioBuffer, EncodeOptions};
use soundcraft_model::ChannelFormat;
fn tone_wav(dir: &std::path::Path, name: &str) -> String {
let tone: Vec<f32> = (0..4_800).map(|i| (i as f32 * 0.1).sin() * 0.2).collect();
let buf = AudioBuffer { sample_rate: 48_000, channels: vec![tone] };
let bytes = soundcraft_audio_io::encode(&buf, &EncodeOptions::default()).unwrap();
let bytes = craft_audio::encode(&buf, &EncodeOptions::default()).unwrap();
let path = dir.join(name);
std::fs::write(&path, bytes).unwrap();
path.to_string_lossy().into_owned()
@@ -175,10 +175,10 @@ fn bounce_stems_accepts_explicit_wav_format() {
let path = std::path::Path::new(files[0].as_str().unwrap());
assert_eq!(path.parent(), Some(stems.as_path()));
assert_eq!(path.extension().and_then(|extension| extension.to_str()), Some("wav"));
let (info, audio) = soundcraft_audio_io::decode(&std::fs::read(path).unwrap(), None).unwrap();
assert_eq!(info.format, soundcraft_audio_io::FileFormat::Wav);
let (info, audio) = craft_audio::decode(&std::fs::read(path).unwrap(), None).unwrap();
assert_eq!(info.container, craft_audio::Container::Wav);
assert_eq!(audio.frames(), 32);
assert_eq!(audio.num_channels(), e.session().main_format().channels());
assert_eq!(audio.channels.len(), e.session().main_format().channels());
assert!(audio.channels.iter().flatten().any(|sample| sample.abs() > 0.01));
std::fs::remove_dir_all(&dir).unwrap();
}
@@ -289,7 +289,7 @@ fn tone_engine() -> (Engine, u64) {
let track = e.session().tracks[0].id;
let tone: Vec<f32> = (0..4_800).map(|i| (i as f32 * 0.05).sin() * 0.5).collect();
let buf = AudioBuffer { sample_rate: 48_000, channels: vec![tone] };
let src = crate::io::add_source(e.session_mut(), "tone", buf, None, soundcraft_audio_io::FileFormat::Wav);
let src = crate::io::add_source(e.session_mut(), "tone", buf, None, soundcraft_model::FileFormat::Wav);
let id = e.session_mut().new_clip_id();
crate::edit::place_clip(e.session_mut(), track, soundcraft_model::Clip::audio(id, "tone", src, 0, 0, 4_800));
e.session_mut().edit.selected_tracks = vec![track];
@@ -392,7 +392,7 @@ fn midi_export_keeps_controllers_and_honours_tracks() {
fn import_surround_creates_a_matching_track() {
let channels: Vec<Vec<f32>> = (0..6).map(|ch| vec![0.1 * (ch as f32 + 1.0); 32]).collect();
let buf = AudioBuffer { sample_rate: 48_000, channels };
let bytes = soundcraft_audio_io::encode(&buf, &EncodeOptions::default()).unwrap();
let bytes = craft_audio::encode(&buf, &EncodeOptions::default()).unwrap();
let mut e = Engine::default();
let v = crate::io::import_audio_bytes(&mut e, "bed.wav", &bytes, None, None, 0).unwrap();
assert_eq!(v["channels"], 6);
+1 -1
View File
@@ -96,7 +96,7 @@ fn report(e: &mut Engine, _: &Value) -> Result<Value> {
#[allow(clippy::unwrap_used)]
mod tests {
use super::*;
use soundcraft_audio_io::AudioBuffer;
use craft_audio::AudioBuffer;
use soundcraft_model::{Clip, ClipId, SourceAudio, SourceId};
use std::sync::Arc;
+16 -4
View File
@@ -114,7 +114,7 @@ pub fn specs() -> Vec<CommandSpec> {
"Assign Send",
[],
None,
"{track?, slot?: 0..9|a..j, bus: name, level_db?: 0 (kept when re-targeting), pre_fader?: false}",
"{track?, slot?: 0..9|a..j, bus: name, level_db?: -144..12, default -144 (silent; kept when re-targeting), pre_fader?: false}",
has_selection,
send
),
@@ -244,6 +244,15 @@ fn volume(e: &mut Engine, p: &Value) -> Result<Value> {
return Err(bad("mix.volume", "`db` or `delta_db` required"));
}
let s = e.session_mut();
// The fader stops at -144 and +12 dB. The reply says when a value was held there: +15 dB used
// to play at +12 with nothing to show it.
let clamped = std::cell::Cell::new(false);
let fader = |db: f32| {
if !(-144.0..=12.0).contains(&db) {
clamped.set(true);
}
db.clamp(-144.0, 12.0)
};
// Tracks already moved as a group sibling of an earlier target must not move again.
let mut done: Vec<TrackId> = Vec::new();
for t in &tracks {
@@ -257,11 +266,14 @@ fn volume(e: &mut Engine, p: &Value) -> Result<Value> {
(None, Some(d)) => cur + d as f32,
_ => cur,
};
let new = if new.is_finite() { new.clamp(-144.0, 12.0) } else { cur };
let new = if new.is_finite() { fader(new) } else { cur };
let d = new - cur;
set_grouped(s, *t, "volume", |tr| tr.mixer.volume_db = if tr.id == *t { new } else { (tr.mixer.volume_db + d).clamp(-144.0, 12.0) });
set_grouped(s, *t, "volume", |tr| tr.mixer.volume_db = if tr.id == *t { new } else { fader(tr.mixer.volume_db + d) });
}
Ok(json!({"tracks": tracks.len()}))
// Every requested track at its final volume, also one that moved as a group sibling of an
// earlier target and was skipped above.
let volumes: Vec<Value> = tracks.iter().filter_map(|t| s.track(*t)).map(|tr| json!({"track": tr.id.0, "db": tr.mixer.volume_db})).collect();
Ok(json!({"tracks": tracks.len(), "volumes": volumes, "clamped": clamped.get()}))
}
fn pan(e: &mut Engine, p: &Value) -> Result<Value> {
+35 -1
View File
@@ -1,5 +1,5 @@
use super::*;
use soundcraft_audio_io::AudioBuffer;
use craft_audio::AudioBuffer;
use soundcraft_model::{ChannelFormat, Clip, SourceAudio, SourceId, TrackKind};
use soundcraft_time::Range;
use std::sync::Arc;
@@ -263,6 +263,18 @@ fn retargeting_a_send_keeps_its_settings() {
assert_eq!((sl.level_db, sl.pan, sl.mute, sl.pre_fader), (-9.0, 0.5, true, true));
}
#[test]
fn a_new_send_starts_at_the_documented_level() {
let mut e = Engine::default();
e.execute("track.new", &json!({"count": 1, "name": "Vox", "format": "mono"})).unwrap();
e.execute("mix.new_bus", &json!({"name": "Rev"})).unwrap();
e.execute("mix.send", &json!({"track": "Vox", "bus": "Rev"})).unwrap();
let level = e.session().tracks[0].mixer.sends[0].as_ref().unwrap().level_db;
assert_eq!(level, -144.0);
let doc = crate::cmd::command_specs().iter().find(|c| c.id == "mix.send").unwrap().params;
assert!(doc.contains("level_db?: -144..12, default -144 (silent;"), "{doc}");
}
fn grouped_demo() -> Engine {
let mut e = crate::demo::demo_engine();
e.execute("mix.volume", &json!({"tracks": [3], "db": 0.0})).unwrap();
@@ -294,6 +306,28 @@ fn volume_absolute_on_a_whole_group_keeps_offsets() {
assert_eq!(volumes(&e), [-10.0, -12.0]);
}
/// The fader stops at +12 dB; the reply says what each track got, and when a value was held at a stop.
#[test]
fn volume_held_at_the_fader_stop_is_reported() {
let mut e = grouped_demo();
let r = e.execute("mix.volume", &json!({"tracks": [3], "db": -6.0})).unwrap();
assert_eq!((r["volumes"].clone(), r["clamped"].clone()), (json!([{"track": 3, "db": -6.0}]), json!(false)));
let r = e.execute("mix.volume", &json!({"tracks": [3], "db": 15.0})).unwrap();
assert_eq!((r["volumes"].clone(), r["clamped"].clone()), (json!([{"track": 3, "db": 12.0}]), json!(true)));
assert_eq!(volumes(&e), [12.0, 12.0]);
// A group member held at the stop counts too.
let mut e = grouped_demo();
e.session_mut().track_mut(TrackId(6)).unwrap().mixer.volume_db = 6.0;
let r = e.execute("mix.volume", &json!({"tracks": [3], "db": 10.0})).unwrap();
assert_eq!(r["clamped"], json!(true));
assert_eq!(volumes(&e), [10.0, 12.0]);
// Both grouped targets are listed, each at the value it ended with.
let mut e = grouped_demo();
e.session_mut().track_mut(TrackId(6)).unwrap().mixer.volume_db = -2.0;
let r = e.execute("mix.volume", &json!({"tracks": [3, 6], "db": -10.0})).unwrap();
assert_eq!(r["volumes"], json!([{"track": 3, "db": -10.0}, {"track": 6, "db": -12.0}]));
}
#[test]
fn clear_with_unknown_param_keeps_lanes() {
let (mut e, t) = tone_session(&[(0, -6.0), (100, -3.0)]);
+30 -6
View File
@@ -3,8 +3,9 @@
use super::bad;
use crate::{Engine, Result};
use craft_audio::AudioBuffer;
use serde_json::{Value, json};
use soundcraft_audio_io::{AudioBuffer, FileFormat};
use soundcraft_model::FileFormat;
use soundcraft_model::{Clip, ClipContent, EditState, Session, SourceId, TrackId};
use soundcraft_time::Samples;
@@ -185,12 +186,35 @@ pub fn render_stretched(s: &mut Session, cmd: &str, c: &Clip, out_len: Samples)
Ok(Some(crate::io::add_source(s, &format!("{}-TCE", c.name), buf, None, FileFormat::Wav)))
}
/// Replace clip `old` on track `t` with `new` (overwriting whatever `new` covers).
/// Replace clip `old` on track `t` with `new`, overwriting whatever `new` covers, except a clip that
/// overlapped `old` across one edge that `new` does not reach past: the other half of a crossfade keeps
/// its overlap and its fade, so processing a clip in place does not turn the crossfade into a dip.
pub fn replace_clip(s: &mut Session, t: TrackId, old: soundcraft_model::ClipId, new: Clip) {
if let Some(pl) = s.track_mut(t).and_then(|tr| tr.playlist_mut()) {
pl.clips.retain(|c| c.id != old);
}
let kept: Vec<Clip> = match s.track_mut(t).and_then(|tr| tr.playlist_mut()) {
Some(pl) => {
let was = pl.clips.iter().find(|c| c.id == old).map(Clip::range);
let to = new.range();
// The other half of a crossfade: it overlaps `old` across one edge only, starting before `old` and ending
// inside it, or starting inside it and ending after it, and `new` does not reach past that edge. A clip
// inside `old`, or covering all of it, shares no crossfade with it and is overwritten as usual.
let keep = |c: &Clip| {
was.is_some_and(|w| {
let earlier = c.start < w.start && c.end() < w.end && to.start >= w.start;
let later = c.start > w.start && c.end() > w.end && to.end <= w.end;
c.range().intersect(&w).is_some() && (earlier || later)
})
};
let (kept, rest) = std::mem::take(&mut pl.clips).into_iter().filter(|c| c.id != old).partition(keep);
pl.clips = rest;
kept
}
None => Vec::new(),
};
crate::edit::place_clip(s, t, new);
if let Some(pl) = s.track_mut(t).and_then(|tr| tr.playlist_mut()) {
pl.clips.extend(kept);
pl.sort();
}
}
#[cfg(test)]
@@ -227,7 +251,7 @@ mod tests {
use soundcraft_model::{ChannelFormat, TrackKind};
let mut s = Session::default();
let t = s.add_track(TrackKind::Audio, ChannelFormat::Stereo, Some("Long"));
let buf = soundcraft_audio_io::AudioBuffer { sample_rate: 48_000, channels: vec![vec![0.5]; 2] };
let buf = craft_audio::AudioBuffer { sample_rate: 48_000, channels: vec![vec![0.5]; 2] };
let src = crate::io::add_source(&mut s, "One", buf, None, FileFormat::Wav);
let cid = s.new_clip_id();
let mut clip = Clip::audio(cid, "Clip", src, 0, 0, length);
+11 -11
View File
@@ -70,7 +70,7 @@ pub(crate) fn attach(e: &mut Engine, t: TrackId, name: &str, bytes: &[u8], path:
}
let info = soundcraft_dsp::plugin_info(SAMPLER).ok_or_else(|| bad(id, "the sampler is not available"))?;
let ext = Path::new(name).extension().and_then(std::ffi::OsStr::to_str).map(str::to_ascii_lowercase);
let (format, buf) = soundcraft_audio_io::decode(bytes, ext.as_deref()).map_err(|err| EngineError::Io(format!("{name}: {err}")))?;
let (file, buf) = craft_audio::decode(bytes, ext.as_deref()).map_err(|err| EngineError::Io(format!("{name}: {err}")))?;
if buf.frames() == 0 || buf.num_channels() == 0 {
return Err(EngineError::Io(format!("{name}: no audio")));
}
@@ -78,7 +78,7 @@ pub(crate) fn attach(e: &mut Engine, t: TrackId, name: &str, bytes: &[u8], path:
.map_err(|r| EngineError::Io(format!("{name}: {}", r.message("too long for a sample (ten minutes of 192 kHz stereo at most)"))))?;
let channels = buf.num_channels();
let s = e.session_mut();
let src = crate::io::add_source(s, name, buf, path, format.format);
let src = crate::io::add_source(s, name, buf, path, soundcraft_model::FileFormat::of(&file));
let (frames, rate, stem) = s.source(src).map_or((0, 0, String::new()), |x| (x.frames, x.sample_rate, x.name.clone()));
let tr = s.track_mut(t).ok_or_else(|| bad(id, "no track"))?;
let keep = tr.instrument.as_ref().is_some_and(|i| i.plugin == SAMPLER);
@@ -110,8 +110,8 @@ fn clear(e: &mut Engine, p: &Value) -> Result<Value> {
#[cfg(test)]
mod tests {
use crate::Engine;
use craft_audio::{AudioBuffer, BitDepth, EncodeFormat, EncodeOptions};
use serde_json::json;
use soundcraft_audio_io::{AudioBuffer, BitDepth, EncodeOptions, FileFormat};
use soundcraft_midi::{Note, Sequence};
use soundcraft_model::{Clip, TrackId};
use soundcraft_time::{Range, TICKS_PER_QUARTER};
@@ -130,8 +130,8 @@ mod tests {
let n = (rate as f32 * secs) as usize;
let one: Vec<f32> = (0..n).map(|i| 0.5 * (std::f32::consts::TAU * freq * i as f32 / rate as f32).sin()).collect();
let buf = AudioBuffer { sample_rate: rate, channels: vec![one; channels] };
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();
let opts = EncodeOptions::new(craft_audio::EncodeFormat::Wav).with_bit_depth(BitDepth::Int16);
std::fs::write(path, craft_audio::encode(&buf, &opts).unwrap()).unwrap();
}
fn instrument_track(e: &mut Engine) -> TrackId {
@@ -232,11 +232,11 @@ mod tests {
let buf = AudioBuffer { sample_rate: 96_000, channels: vec![tone.clone(), tone] };
let mut e = Engine::default();
let t = instrument_track(&mut e);
for format in [FileFormat::Flac, FileFormat::Aiff] {
let ext = format.default_extension();
for format in [EncodeFormat::Flac, EncodeFormat::Aiff] {
let ext = format.extension();
let path = d.join(format!("tone.{ext}"));
let opts = EncodeOptions { format, bit_depth: BitDepth::Int24, dither: false, bwf: None };
std::fs::write(&path, soundcraft_audio_io::encode(&buf, &opts).unwrap()).unwrap();
let opts = EncodeOptions::new(format);
std::fs::write(&path, craft_audio::encode(&buf, &opts).unwrap()).unwrap();
let r = e.execute("mix.sampler_load", &json!({"track": t.0, "path": path.to_string_lossy()})).unwrap();
assert_eq!(r["channels"], 2, "{ext}");
assert_eq!(r["frames"], 2400, "{ext}: 96 kHz resampled to the 48 kHz session");
@@ -320,8 +320,8 @@ mod tests {
let junk = d.join("junk.wav");
std::fs::write(&junk, b"definitely not audio").unwrap();
let empty = d.join("empty.wav");
let opts = EncodeOptions { format: FileFormat::Wav, bit_depth: BitDepth::Int16, dither: false, bwf: None };
std::fs::write(&empty, soundcraft_audio_io::encode(&AudioBuffer { sample_rate: 48_000, channels: vec![vec![]] }, &opts).unwrap()).unwrap();
let opts = EncodeOptions::new(craft_audio::EncodeFormat::Wav).with_bit_depth(BitDepth::Int16);
std::fs::write(&empty, craft_audio::encode(&AudioBuffer { sample_rate: 48_000, channels: vec![vec![]] }, &opts).unwrap()).unwrap();
let wav = d.join("ok.wav");
tone_wav(&wav, 440.0, 48_000, 0.1, 1);
let mut e = Engine::default();
+5 -2
View File
@@ -155,6 +155,9 @@ fn prepare_dpe(e: &mut Engine, p: &Value) -> Result<Value> {
return Err(bad(id, "there is no audio to prepare"));
}
let r = soundcraft_time::Range::new(0, end);
if tracks.iter().any(|&t| crate::edit::holds_locked(e.session(), t, crate::io::bounded_to_track(e.session(), t, r))) {
return Err(bad(id, crate::edit::LOCKED_IN_RANGE));
}
let mut n = 0;
for t in tracks {
// Each track keeps its own length (its material plus a second), as before.
@@ -761,8 +764,8 @@ mod tests {
let _ = std::fs::remove_dir_all(&dir);
std::fs::create_dir_all(&dir).unwrap();
let wav = dir.join("hit.wav");
let buf = soundcraft_audio_io::AudioBuffer { sample_rate: 48_000, channels: vec![vec![0.5; 4_800]] };
std::fs::write(&wav, soundcraft_audio_io::encode(&buf, &soundcraft_audio_io::EncodeOptions::default()).unwrap()).unwrap();
let buf = craft_audio::AudioBuffer { sample_rate: 48_000, channels: vec![vec![0.5; 4_800]] };
std::fs::write(&wav, craft_audio::encode(&buf, &craft_audio::EncodeOptions::default()).unwrap()).unwrap();
let mut donor = Engine::default();
donor.execute("track.new", &json!({"count": 1, "name": "Pads", "kind": "instrument"})).unwrap();
donor.execute("mix.sampler_load", &json!({"track": "Pads", "path": wav.to_string_lossy()})).unwrap();
+5 -5
View File
@@ -145,7 +145,7 @@ fn main_format(e: &mut Engine, p: &Value) -> Result<Value> {
#[cfg(test)]
mod tests {
use super::*;
use soundcraft_audio_io::AudioBuffer;
use craft_audio::AudioBuffer;
use soundcraft_model::{Clip, SourceAudio, SourceId};
use std::sync::Arc;
@@ -211,13 +211,13 @@ mod tests {
let ps = path.to_string_lossy().to_string();
e.execute("file.bounce_mix", &json!({"path": ps, "start": 0, "end": 4800, "bit_depth": "32f"})).unwrap();
let bytes = std::fs::read(&path).unwrap();
assert_eq!(soundcraft_audio_io::wav_channel_mask(&bytes), Some(0x63F));
let (_, buf) = soundcraft_audio_io::decode(&bytes, Some("wav")).unwrap();
assert_eq!(craft_audio::probe(&bytes, None).unwrap().layout.mask(), Some(0x63F));
let (_, buf) = craft_audio::decode(&bytes, Some("wav")).unwrap();
assert_eq!(buf.channels.len(), 8);
assert!((buf.channels[2][1000] - 0.25).abs() < 1e-5, "front centre lands on C");
// ITU stereo fold-down.
e.execute("file.bounce_mix", &json!({"path": ps, "start": 0, "end": 4800, "bit_depth": "32f", "fold_down": "stereo"})).unwrap();
let (_, buf) = soundcraft_audio_io::decode(&std::fs::read(&path).unwrap(), Some("wav")).unwrap();
let (_, buf) = craft_audio::decode(&std::fs::read(&path).unwrap(), Some("wav")).unwrap();
assert_eq!(buf.channels.len(), 2);
assert!((buf.channels[0][1000] - 0.25 * std::f32::consts::FRAC_1_SQRT_2).abs() < 1e-4);
assert!(e.execute("file.bounce_mix", &json!({"path": ps, "fold_down": "quad"})).is_err());
@@ -225,7 +225,7 @@ mod tests {
let sdir = dir.join("stems");
let r = e.execute("file.bounce_stems", &json!({"dir": sdir.to_string_lossy(), "start": 0, "end": 4800})).unwrap();
let f = r["files"][0].as_str().unwrap().to_string();
let (_, buf) = soundcraft_audio_io::decode(&std::fs::read(&f).unwrap(), Some("wav")).unwrap();
let (_, buf) = craft_audio::decode(&std::fs::read(&f).unwrap(), Some("wav")).unwrap();
assert_eq!(buf.channels.len(), 8);
let _ = std::fs::remove_dir_all(&dir);
}
+2 -1
View File
@@ -175,9 +175,10 @@ fn analysed(s: &Session, t: TrackId, around: Range) -> Vec<Samples> {
#[cfg(test)]
mod tests {
use crate::Engine;
use craft_audio::AudioBuffer;
use serde_json::json;
use soundcraft_audio_io::{AudioBuffer, FileFormat};
use soundcraft_midi::{Note, Sequence};
use soundcraft_model::FileFormat;
use soundcraft_model::{ChannelFormat, Clip, Session, TrackId, TrackKind};
use soundcraft_time::{SampleRate, TICKS_PER_QUARTER};
+56 -3
View File
@@ -201,7 +201,15 @@ pub fn specs() -> Vec<CommandSpec> {
Ok(json!({"clips": ids}))
}
),
cmd!("track.input", "Track Input", [], None, "{tracks?, input: none|bus name|hardware name}", has_selection, |e, p| route(e, p, true)),
cmd!(
"track.input",
"Track Input",
[],
None,
"{tracks?, input: none|bus name|hardware name|MIDI:<channel 1-16 or *>:<device or *> (MIDI and instrument tracks)}",
has_selection,
|e, p| route(e, p, true)
),
cmd!("track.output", "Track Output", [], None, "{tracks?, output: main|none|bus name}", has_selection, |e, p| route(e, p, false)),
cmd!("track.timebase", "Track Timebase", [], None, "{tracks?, ticks: bool}", has_selection, |e, p| flag(
e,
@@ -483,11 +491,11 @@ fn split_mono(e: &mut Engine, p: &Value) -> Result<Value> {
.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 buf = craft_audio::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);
let id = crate::io::add_source(s, &name, buf, None, soundcraft_model::FileFormat::Wav);
if let Some(source) = s.sources.iter_mut().find(|source| source.id == id) {
source.time_reference = time_reference;
}
@@ -1024,6 +1032,8 @@ fn route(e: &mut Engine, p: &Value, input: bool) -> Result<Value> {
if let Route::Hardware(name) = &r
&& create_bus
&& !name.to_ascii_lowercase().starts_with("in ")
// A MIDI input (device and channel) of a MIDI or instrument track, see `MidiInput`.
&& !name.starts_with("MIDI:")
{
let fmt = tracks
.first()
@@ -1046,6 +1056,49 @@ fn route(e: &mut Engine, p: &Value, input: bool) -> Result<Value> {
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_midi_input_is_kept_as_typed_and_never_becomes_a_bus() {
use soundcraft_model::MidiInput;
let mut e = Engine::default();
let id = e.execute("track.new", &json!({"name": "Keys", "kind": "instrument"})).unwrap()["tracks"][0].as_u64().unwrap();
let mut set = |input: &str| {
e.execute("track.input", &json!({"tracks": [id], "input": input})).unwrap();
assert_eq!(e.session().busses.len(), 0, "{input}: no bus is made for a MIDI input");
e.session().track(soundcraft_model::TrackId(id)).unwrap().mixer.input.clone()
};
// (what is typed, the device and channel the track listens to)
let cases: [(&str, Option<&str>, Option<u8>); 12] = [
("MIDI:*:*", None, None),
("MIDI:1:*", None, Some(0)),
("MIDI:16:Pad", Some("Pad"), Some(15)),
("MIDI:*:Keys", Some("Keys"), None),
// A device name may hold colons (ALSA names do): only the first one after the channel splits.
("MIDI:3:Launchkey MK3:MIDI 1", Some("Launchkey MK3:MIDI 1"), Some(2)),
// An empty device means every device.
("MIDI:5:", None, Some(4)),
("MIDI:*:", None, None),
// Channels outside 1–16 and other junk mean every channel; nothing fails.
("MIDI:0:Pad", Some("Pad"), None),
("MIDI:17:Pad", Some("Pad"), None),
("MIDI:-1:Pad", Some("Pad"), None),
("MIDI:abc:Pad", Some("Pad"), None),
// Without the second colon it is not a device choice: everything.
("MIDI:7", None, None),
];
for (typed, device, channel) in cases {
let route = set(typed);
assert_eq!(route, Route::Hardware(typed.to_string()), "kept as typed");
assert_eq!(MidiInput::of(&route), MidiInput { device, channel }, "{typed}");
}
// The route a choice is stored as reads back as the same choice.
let stored = MidiInput { device: Some("Launchkey MK3:MIDI 1"), channel: Some(9) }.route();
let Route::Hardware(text) = &stored else { panic!("not a hardware route") };
assert_eq!(MidiInput::of(&set(text)), MidiInput { device: Some("Launchkey MK3:MIDI 1"), channel: Some(9) });
// "none" takes the choice away: all devices, all channels.
assert_eq!(MidiInput::of(&set("none")), MidiInput::default());
}
#[test]
fn comp_from_alternate_playlist() {
let mut e = crate::demo::demo_engine();
+1 -1
View File
@@ -135,7 +135,7 @@ mod tests {
std::fs::create_dir_all(&dir).unwrap();
let out = dir.join("mix.wav");
e.execute("file.bounce_mix", &json!({"path": out.to_string_lossy(), "start": 0, "end": 96_000, "dither": false})).unwrap();
let (_, buf) = soundcraft_audio_io::decode(&std::fs::read(&out).unwrap(), Some("wav")).unwrap();
let (_, buf) = craft_audio::decode(&std::fs::read(&out).unwrap(), Some("wav")).unwrap();
assert!(buf.channels.iter().flatten().all(|x| *x == 0.0));
let _ = std::fs::remove_dir_all(dir);
}
+2 -1
View File
@@ -2,8 +2,9 @@
//! a 24-bar groove at 104 BPM with drums, bass, keys, pad, lead, a reverb aux and a master.
use crate::Engine;
use soundcraft_audio_io::{AudioBuffer, FileFormat};
use craft_audio::AudioBuffer;
use soundcraft_midi::{Note, Sequence};
use soundcraft_model::FileFormat;
use soundcraft_model::{ChannelFormat, Clip, Fade, FadeShape, Insert, MarkerKind, Route, SendSlot, Session, TrackKind};
use soundcraft_time::{SampleRate, TICKS_PER_QUARTER};
+1 -2
View File
@@ -313,8 +313,7 @@ mod tests {
let track = saved.add_track(TrackKind::Audio, ChannelFormat::Mono, None);
let clip = Clip::audio(saved.new_clip_id(), "Clip", SourceId(900), 0, 0, 100);
saved.track_mut(track).unwrap().playlist_mut().unwrap().clips.push(clip);
let audio =
Arc::new(soundcraft_model::SourceAudio::new(soundcraft_audio_io::AudioBuffer { sample_rate: 48_000, channels: vec![vec![0.5; 100]] }));
let audio = Arc::new(soundcraft_model::SourceAudio::new(craft_audio::AudioBuffer { sample_rate: 48_000, channels: vec![vec![0.5; 100]] }));
saved.pool.insert(SourceId(900), audio);
check(&saved, &saved.clone(), true, "shared decoded audio");
let mut doc = saved.clone();

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