Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 15 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -389,15 +389,17 @@ For a custom asset location, give one of these sources to
- A promise for one of these.

For a Node.js `file:` source, use a URL object. A string source obeys fetch
rules. The first concurrent call selects the source. Later arguments do not
replace an initialized module. A custom binary must match the generated
bindings.

The package root also exports `loadTunelet` as its default export. For direct
control of Wasm initialization, `tunelet/wasm` gives the generated default
initializer and `initSync`. The package root re-exports `initSync` too. Use
only one initialization entry point for each module instance. The package
gives only ESM exports. CommonJS applications can use `await import("tunelet")`.
rules. Repeated calls with the same source share one initialization result. A
call with a different source, or with a binary that does not match the
generated bindings, rejects with a `TuneletError`.

The package root also exports `loadTunelet` as its default export. It exports
`TuneletError` with a `camelCase` message and a typed `details` payload for all
validation and lifecycle errors. For direct control of Wasm initialization,
`tunelet/wasm` gives the generated default initializer and `initSync`. The
package root exports `initSync` too. Use only one initialization entry point
for each module instance. The package gives only ESM exports. CommonJS
applications can use `await import("tunelet")`.

### Deployment

Expand Down Expand Up @@ -425,7 +427,7 @@ page that opens the JavaScript.
| Formant envelopes | `formantEnvelope(maxFrequencyHz, input, output)` |
| Finite tail | `beginDrain()`, `drain(left, right)`, `drainMono(audio)` |
| Configured tail budget | `tailFrames()` |
| Reset or release memory | `reset()`, `free()` |
| Reset or release memory | `reset()`, `free()`, `[Symbol.dispose]()` |

### Audio buffers

Expand All @@ -451,9 +453,9 @@ and show enums as string literals. To change the configuration, do these steps:
3. Call `setConfig()`.

If a required field is absent or a value is not valid, the call throws a
JavaScript `Error`. The optional `resynthesis.harmony.voices` field defaults
to 4 if it is absent. The conversion ignores extra object properties. The
generated declarations contain the parameter documentation from the Rust
`TuneletError` with `details`. The optional `resynthesis.harmony.voices` field
defaults to 4 if it is absent. The conversion ignores extra object properties.
The generated declarations contain the parameter documentation from the Rust
types.

`resynthesis.randomSeed` and the `processedFrames` field of feedback and
Expand Down
47 changes: 31 additions & 16 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ use crate::effects::punch::PunchConfig;
use crate::effects::reverb::ReverbConfig;
use crate::effects::warm::WarmConfig;
use crate::error::{check_range, ConfigProblem};
use crate::Error;
use crate::ResynthesisConfig;
use crate::{ConfigField, Error, ResynthesisConfig};
use std::num::NonZeroUsize;

/// The sample rate of the input and the output.
Expand Down Expand Up @@ -98,17 +97,18 @@ impl PrepareConfig {
///
/// # Errors
///
/// Returns [`Error::InvalidConfig`] with the field
/// `prepare.max_block_frames` if the maximum is zero. The field type is
/// [`NonZeroUsize`]. Thus, this invariant also holds if you edit the field
/// after construction.
/// Returns [`Error::InvalidConfig`] with [`ConfigField::PrepareMaxBlockFrames`]
/// and [`ConfigProblem::IntegerOutOfRange`] if `max_block_frames` is zero.
/// The field type is [`NonZeroUsize`]. Thus, this invariant also holds if
/// you edit the field after construction.
pub fn new(sample_rate: SampleRate, max_block_frames: usize) -> Result<Self, Error> {
let max_block_frames = NonZeroUsize::new(max_block_frames).ok_or_else(|| {
Error::config(
"prepare.max_block_frames",
ConfigProblem::OutOfRange {
min: 1.0,
max: usize::MAX as f64,
ConfigField::PrepareMaxBlockFrames,
ConfigProblem::IntegerOutOfRange {
value: 0,
min: 1,
max: usize::MAX as i128,
},
)
})?;
Expand All @@ -126,9 +126,16 @@ impl PrepareConfig {
/// configuration does active pitch processing. It is not a bypass with zero
/// latency.
///
/// Change a copy, then apply it with [`crate::Processor::set_config`] or
/// [`crate::ControlEvent::Configure`]. The validation examines all fields
/// before a change has an effect. This includes the fields of disabled stages.
#[cfg_attr(
not(all(feature = "wasm", target_arch = "wasm32")),
doc = " Change a copy, then apply it with [`crate::Processor::set_config`] or [`crate::ControlEvent::Configure`]."
)]
#[cfg_attr(
all(feature = "wasm", target_arch = "wasm32"),
doc = " Change a copy, then apply it with `Tunelet.setConfig` or a `configure` event."
)]
/// The validation examines all fields before a change has an effect. This
/// includes the fields of disabled stages.
#[derive(Clone, Copy, Debug, PartialEq)]
#[non_exhaustive]
#[cfg_attr(
Expand All @@ -148,8 +155,16 @@ pub struct ProcessorConfig {
pub warm: WarmConfig,
/// The stage for pitch tracking, correction, voice resynthesis and
/// formant control.
///
/// The default is [`ResynthesisConfig::default`].
#[cfg_attr(
not(all(feature = "wasm", target_arch = "wasm32")),
doc = "",
doc = " The default is [`ResynthesisConfig::default`]."
)]
#[cfg_attr(
all(feature = "wasm", target_arch = "wasm32"),
doc = "",
doc = " The default is chromatic correction with one choir voice."
)]
pub resynthesis: ResynthesisConfig,
/// The reverse-loop effect after resynthesis.
///
Expand Down Expand Up @@ -209,5 +224,5 @@ impl ProcessorConfig {
///
/// `ProcessorConfig` and `ControlEvent::OutputGain` use the same range.
pub(crate) fn validate_output_gain(gain: f32) -> Result<(), Error> {
check_range("output_gain", gain, 0.0, 64.0)
check_range(ConfigField::OutputGain, gain, 0.0, 64.0)
}
13 changes: 9 additions & 4 deletions src/effects/alien.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use crate::error::check_count;
use crate::numeric::fixed::{Fixed, Q14};
use crate::Error;
use crate::{ConfigField, Error};

/// The number of history frames that the loop reads from.
const HISTORY_FRAMES: usize = 192_000;
Expand Down Expand Up @@ -50,10 +50,15 @@ impl AlienConfig {
///
/// # Errors
///
/// Returns the first [`Error::InvalidConfig`] with its dotted field path
/// and the problem.
/// Returns the first [`Error::InvalidConfig`] with its [`ConfigField`] and
/// the problem.
pub fn validate(self) -> Result<(), Error> {
check_count("alien.loop_frames", i64::from(self.loop_frames), 1, 96_000)
check_count(
ConfigField::AlienLoopFrames,
i128::from(self.loop_frames),
1,
96_000,
)
}
}

Expand Down
25 changes: 16 additions & 9 deletions src/effects/punch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use crate::error::check_range;
use crate::numeric::fixed::{Q14, Q8};
use crate::numeric::{coefficient_to_exp_value, db_to_gain, envelope_decay, NumericContext};
use crate::{Error, SampleRate};
use crate::{ConfigField, Error, SampleRate};

/// The update interval of the peak follower, in frames.
const WINDOW_FRAMES: usize = 100;
Expand Down Expand Up @@ -41,8 +41,15 @@ pub struct PunchConfig {
/// The transient emphasis without a unit, 0..=1.
///
/// The default is 0.5. It sets the release half-life of the adaptive
/// follower: 5000 ms at 0, 250 ms at 0.5 and 1 ms at 1. The attack uses
/// `attack_ms`.
/// follower: 5000 ms at 0, 250 ms at 0.5 and 1 ms at 1.
#[cfg_attr(
not(all(feature = "wasm", target_arch = "wasm32")),
doc = " The attack uses `attack_ms`."
)]
#[cfg_attr(
all(feature = "wasm", target_arch = "wasm32"),
doc = " The attack uses `attackMs`."
)]
pub impact: f32,
/// The output ceiling of the saturation in dBFS, -96..=0.
///
Expand Down Expand Up @@ -75,13 +82,13 @@ impl PunchConfig {
///
/// # Errors
///
/// Returns the first [`Error::InvalidConfig`] with its dotted field path
/// and the problem.
/// Returns the first [`Error::InvalidConfig`] with its [`ConfigField`] and
/// the problem.
pub fn validate(self) -> Result<(), Error> {
check_range("punch.gain_db", self.gain_db, -96.0, 12.0)?;
check_range("punch.impact", self.impact, 0.0, 1.0)?;
check_range("punch.ceiling_db", self.ceiling_db, -96.0, 0.0)?;
check_range("punch.attack_ms", self.attack_ms, 0.01, 5000.0)
check_range(ConfigField::PunchGainDb, self.gain_db, -96.0, 12.0)?;
check_range(ConfigField::PunchImpact, self.impact, 0.0, 1.0)?;
check_range(ConfigField::PunchCeilingDb, self.ceiling_db, -96.0, 0.0)?;
check_range(ConfigField::PunchAttackMs, self.attack_ms, 0.01, 5000.0)
}
}

Expand Down
38 changes: 28 additions & 10 deletions src/effects/reverb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,16 @@
use crate::error::check_range;
use crate::filter::{Biquad, Design};
use crate::numeric::fixed::{Fixed, Q14};
use crate::{Error, SampleRate};
use crate::{ConfigField, Error, SampleRate};

/// The echo or reverberation network. The default is [`Self::Echo`].
#[cfg_attr(
not(all(feature = "wasm", target_arch = "wasm32")),
doc = " The echo or reverberation network. The default is [`Self::Echo`]."
)]
#[cfg_attr(
all(feature = "wasm", target_arch = "wasm32"),
doc = " The echo or reverberation network. The default is `\"echo\"`."
)]
///
/// All modes share one delay buffer. A mode change uses the same buffer
/// contents and damping filter memories. Thus a transition can play audio
Expand Down Expand Up @@ -53,8 +60,14 @@ pub struct ReverbConfig {
/// Enables the selected network. The default is `false`. In bypass, the
/// histories do not change.
pub enabled: bool,
/// The topology of the delay network. The default is
/// [`ReverbMode::Echo`].
#[cfg_attr(
not(all(feature = "wasm", target_arch = "wasm32")),
doc = " The topology of the delay network. The default is [`ReverbMode::Echo`]."
)]
#[cfg_attr(
all(feature = "wasm", target_arch = "wasm32"),
doc = " The topology of the delay network. The default is `\"echo\"`."
)]
pub mode: ReverbMode,
/// The wet fraction, 0..=1.
///
Expand Down Expand Up @@ -105,13 +118,18 @@ impl ReverbConfig {
///
/// # Errors
///
/// Returns the first [`Error::InvalidConfig`] with its dotted field path
/// and the problem.
/// Returns the first [`Error::InvalidConfig`] with its [`ConfigField`] and
/// the problem.
pub fn validate(self) -> Result<(), Error> {
check_range("reverb.mix", self.mix, 0.0, 1.0)?;
check_range("reverb.diffusion", self.diffusion, 0.0, 1.0)?;
check_range("reverb.decay", self.decay, 0.0, 1.0)?;
check_range("reverb.brightness_hz", self.brightness_hz, 80.0, 20000.0)
check_range(ConfigField::ReverbMix, self.mix, 0.0, 1.0)?;
check_range(ConfigField::ReverbDiffusion, self.diffusion, 0.0, 1.0)?;
check_range(ConfigField::ReverbDecay, self.decay, 0.0, 1.0)?;
check_range(
ConfigField::ReverbBrightnessHz,
self.brightness_hz,
80.0,
20000.0,
)
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/effects/warm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::error::check_range;
use crate::filter::{Biquad, Design};
use crate::numeric::db_to_gain;
use crate::numeric::fixed::{Fixed, Q12, Q14, Q8};
use crate::{Error, SampleRate};
use crate::{ConfigField, Error, SampleRate};

/// The update interval of the peak follower, in frames.
const WINDOW_FRAMES: usize = 100;
Expand Down Expand Up @@ -48,10 +48,10 @@ impl WarmConfig {
///
/// # Errors
///
/// Returns the first [`Error::InvalidConfig`] with its dotted field path
/// and the problem.
/// Returns the first [`Error::InvalidConfig`] with its [`ConfigField`] and
/// the problem.
pub fn validate(self) -> Result<(), Error> {
check_range("warm.drive_db", self.drive_db, 0.0, 6.0)
check_range(ConfigField::WarmDriveDb, self.drive_db, 0.0, 6.0)
}
}

Expand Down
Loading