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
34 changes: 27 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,18 @@ The default is 4. `FixedInterval` and `SmartInterval` use the first `voices`
voices. The other voices release with the usual gain envelope. The chord
and MIDI sources ignore this field.

`resynthesis.harmony_gain` scales the four harmony voices in mono and in
stereo. It does not scale the lead voice.
These fields mix the lead voice and the four harmony voices in `Harmony` mode:

- `resynthesis.lead_gain` scales the lead voice, in `0..=1` (default `1`).
- `resynthesis.harmony_gain` scales all four harmony voices in mono and stereo
output, in `0..=1` (default `0.5`).
- `resynthesis.harmony.voice_gains` scales each harmony voice, in `0..=1`
(default `[1, 1, 1, 1]`).
- If `stereo_output` is `true`, `resynthesis.harmony.voice_pans` sets the
constant-power stereo pan of each harmony voice, in `-1..=1` (default
`[-0.33333334, 0.33333334, -1, 1]`).
- `resynthesis.harmony.voice_mutes` mutes individual harmony voices in mono and
stereo output (default `[false, false, false, false]`).

## Process audio

Expand Down Expand Up @@ -304,7 +314,9 @@ These calls read the state of the processor. They do not change the audio.

The telemetry includes these items:

- The pitch ratio and the gain of each voice.
- The pitch ratio and the controller gain of each voice. In `Harmony` mode,
`voice_gains` in telemetry reports the controller envelope and does not
include `lead_gain`, `harmony_gain` or the harmony mix controls.
- The peaks of the input, of the two output channels and of the reverb wet
signal.
- The pitch clarity, which is a confidence score from 0 to 1.
Expand Down Expand Up @@ -451,10 +463,18 @@ 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
types.
JavaScript `Error`. These optional fields use their defaults if they are
absent:

- `resynthesis.leadGain` defaults to `1`.
- `resynthesis.harmony.voices` defaults to `4`.
- `resynthesis.harmony.voiceGains` defaults to `[1, 1, 1, 1]`.
- `resynthesis.harmony.voicePans` defaults to
`[-0.33333334, 0.33333334, -1, 1]`.
- `resynthesis.harmony.voiceMutes` defaults to `[false, false, false, false]`.

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
telemetry use `bigint`. Frame offsets, block lengths and latency use
Expand Down
87 changes: 81 additions & 6 deletions src/harmony.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ use chords::SCALE_TONES;
/// same count.
pub const HARMONY_VOICES: usize = 4;

/// The default linear gain of each harmony voice: unity.
pub(crate) const DEFAULT_VOICE_GAINS: [f32; HARMONY_VOICES] = [1.0; HARMONY_VOICES];
/// The default stereo pan position of each harmony voice, in -1.0..=1.0.
pub(crate) const DEFAULT_VOICE_PANS: [f32; HARMONY_VOICES] = [-0.33333334, 0.33333334, -1.0, 1.0];

/// The Q12 step that increases the gain envelope of a voice toward a higher
/// target gain (attack).
///
Expand Down Expand Up @@ -240,6 +245,7 @@ pub enum Inversion {
/// - A register of 50%.
/// - A closed voicing and zero portamento.
/// - Four voices for the interval sources.
/// - Unity voice gains, default stereo pans and no voice mutes.
///
/// These settings configure all sources. Only the fields for the selected
/// source change the target notes. Validation always checks all fields.
Expand Down Expand Up @@ -328,6 +334,45 @@ pub struct HarmonyConfig {
tsify(optional)
)]
pub voices: u8,
/// Four linear gains for the harmony voices, each in 0..=1.
///
/// The default is `[1.0, 1.0, 1.0, 1.0]`. Each value scales one harmony
/// voice in mono and stereo output.
/// [`crate::ResynthesisConfig::harmony_gain`] also scales all four voices.
/// In JavaScript, the field is optional and an absent value gives the
/// default.
#[cfg_attr(
all(feature = "wasm", target_arch = "wasm32"),
serde(default = "default_voice_gains"),
tsify(optional, type = "[number, number, number, number]")
)]
pub voice_gains: [f32; HARMONY_VOICES],
/// Four stereo pan positions for the harmony voices, each in -1..=1.
///
/// A value of -1 puts the voice on the left channel. A value of 0 puts
/// the voice in the center, and 1 puts it on the right channel. The
/// default is `[-0.33333334, 0.33333334, -1.0, 1.0]`. If
/// [`crate::ResynthesisConfig::stereo_output`] is `true`, resynthesis uses
/// these positions for a constant-power stereo pan. In JavaScript, the
/// field is optional and an absent value gives the default.
#[cfg_attr(
all(feature = "wasm", target_arch = "wasm32"),
serde(default = "default_voice_pans"),
tsify(optional, type = "[number, number, number, number]")
)]
pub voice_pans: [f32; HARMONY_VOICES],
/// Four mutes for the harmony voices. The default is all `false`.
///
/// If an entry is `true`, that harmony voice has zero output gain in mono
/// and stereo output. Its reader and gain envelope continue to advance.
/// In JavaScript, the field is optional and an absent value gives the
/// default.
#[cfg_attr(
all(feature = "wasm", target_arch = "wasm32"),
serde(default),
tsify(optional, type = "[boolean, boolean, boolean, boolean]")
)]
pub voice_mutes: [bool; HARMONY_VOICES],
}

/// The default count of interval voices: all of them.
Expand All @@ -336,6 +381,18 @@ fn default_voices() -> u8 {
HARMONY_VOICES as u8
}

/// The default linear gains of the four harmony voices.
#[cfg(all(feature = "wasm", target_arch = "wasm32"))]
fn default_voice_gains() -> [f32; HARMONY_VOICES] {
DEFAULT_VOICE_GAINS
}

/// The default stereo pan positions of the four harmony voices.
#[cfg(all(feature = "wasm", target_arch = "wasm32"))]
fn default_voice_pans() -> [f32; HARMONY_VOICES] {
DEFAULT_VOICE_PANS
}

impl Default for HarmonyConfig {
fn default() -> Self {
Self {
Expand All @@ -351,6 +408,9 @@ impl Default for HarmonyConfig {
voicing_spread_percent: 0.0,
portamento_ms: 0.0,
voices: HARMONY_VOICES as u8,
voice_gains: DEFAULT_VOICE_GAINS,
voice_pans: DEFAULT_VOICE_PANS,
voice_mutes: [false; HARMONY_VOICES],
}
}
}
Expand All @@ -359,10 +419,10 @@ impl HarmonyConfig {
/// Validates all fields, which includes the fields that the selected
/// source does not use.
///
/// Each element of the two interval arrays must be in its documented
/// inclusive range. The floating-point controls must be finite and in
/// their documented inclusive ranges. Validation does not clamp values.
/// It does not change the processor state.
/// Each element of the interval, gain and pan arrays must be in its
/// documented inclusive range. The floating-point controls must be finite
/// and in their documented inclusive ranges. Validation does not clamp
/// values. It does not change the processor state.
///
/// # Errors
///
Expand All @@ -389,7 +449,14 @@ impl HarmonyConfig {
self.portamento_ms,
0.0,
10_000.0,
)
)?;
for gain in self.voice_gains {
check_range("resynthesis.harmony.voice_gains", gain, 0.0, 1.0)?;
}
for pan in self.voice_pans {
check_range("resynthesis.harmony.voice_pans", pan, -1.0, 1.0)?;
}
Ok(())
}
}

Expand Down Expand Up @@ -539,7 +606,15 @@ impl HarmonyState {
if old.portamento_ms != config.portamento_ms {
self.prepare_portamento();
}
if old != config {
if old.source != config.source
|| old.key != config.key
|| old.scale != config.scale
|| old.chord != config.chord
|| old.degree != config.degree
|| old.inversion != config.inversion
|| old.register_percent != config.register_percent
|| old.voicing_spread_percent != config.voicing_spread_percent
{
self.setup_chord();
}
}
Expand Down
18 changes: 11 additions & 7 deletions src/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -845,15 +845,19 @@ pub struct Telemetry {
/// pitch shift and the ratio of the voice. The entries after
/// [`voice_count`](Self::voice_count) are zero.
pub voice_ratios: [f32; 16],
/// The linear gain of each voice, in the order of
/// The linear controller gain of each voice, in the order of
/// [`voice_ratios`](Self::voice_ratios).
///
/// The lead and the choir voices have a gain of 1. The gain of a harmony
/// voice includes the MIDI velocity gain, in 0..=3.98095703125. MIDI
/// velocity 127 gives the maximum, approximately +12 dB. The gain is zero
/// for a voice that does not sound. It does not include the configured
/// harmony level or the pan. The harmony controller updates it every 101
/// frames.
/// The lead and the choir voices report a gain of 1. The gain of a harmony
/// voice is the envelope of the harmony controller, which includes the
/// MIDI velocity gain in 0..=3.98095703125. MIDI velocity 127 gives the
/// maximum, approximately +12 dB, and an inactive voice reports zero.
/// These values do not include [`crate::ResynthesisConfig::lead_gain`],
/// [`crate::ResynthesisConfig::harmony_gain`],
/// [`crate::HarmonyConfig::voice_gains`],
/// [`crate::HarmonyConfig::voice_pans`] or
/// [`crate::HarmonyConfig::voice_mutes`]. The harmony controller updates
/// them every 101 frames.
pub voice_gains: [f32; 16],
/// The current drive of Punch, divided by its base drive.
///
Expand Down
Loading