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
14 changes: 12 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,15 @@ multi-voice mixes.
`resynthesis.dry_mix` sets the dry fraction at the resynthesis stage. The
dry path starts after Warm and has a delay of 1,543 frames.

`resynthesis.pitch.vocal_range` selects a frequency preset (`Auto`, `Soprano`,
`AltoTenor`, `BaritoneBass` or `Custom`). `Auto` is the default (`40..=1100`
Hz). `Custom` uses `min_frequency_hz` and `max_frequency_hz`. Both pitch
tracking and `feedback()` enforce the active limits.

`resynthesis.pitch.sibilant_bypass` is `true` by default. Active sibilants
split at 3.5 kHz, and the high band bypasses pitch shift and harmony voices
at unity pitch with a delay of 1,593 frames.

### Harmony sources

Set `resynthesis.mode` to `Harmony`. Then select the source of the voices
Expand Down Expand Up @@ -489,8 +498,9 @@ the cases where `voiced` is `false`:

- The kept pitch estimate is unvoiced. This includes the time before the
first detection and after a reset or a drain completion.
- The estimated frequency is outside the inclusive reporting bounds
`resynthesis.pitch.minFrequencyHz` and `resynthesis.pitch.maxFrequencyHz`.
- The estimated frequency is outside the active bounds of
`resynthesis.pitch.vocalRange` (or `resynthesis.pitch.minFrequencyHz` and
`resynthesis.pitch.maxFrequencyHz` for `"custom"`).

`formantEnvelope(maxFrequencyHz, input, output)` fills two `Float32Array`
buffers of equal length and returns `false` if no envelope is available.
Expand Down
6 changes: 6 additions & 0 deletions scripts/verify-pack.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,8 @@ function verifyRuntimeImport(consumerDir, packageName) {
"assert.ok(wasmExports);",
"",
"const config = rootModule.defaultConfig();",
'assert.equal(config.resynthesis.pitch.vocalRange, "auto");',
"assert.equal(config.resynthesis.pitch.sibilantBypass, true);",
"const processor = new rootModule.Tunelet(48000, 128, config);",
"try {",
" const input = new Int16Array(128);",
Expand Down Expand Up @@ -202,6 +204,7 @@ function verifyTypeDeclarations(consumerDir, packageName) {
" loadTunelet as namedLoadTunelet,",
" Tunelet,",
" type ProcessorConfig,",
" type VocalRange,",
`} from ${pkgLiteral};`,
"import init, {",
" initSync as wasmInitSync,",
Expand All @@ -214,6 +217,9 @@ function verifyTypeDeclarations(consumerDir, packageName) {
" const exportsFromNamed: InitOutput = await namedLoadTunelet(source);",
" const exportsFromWasm: InitOutput = await init(source);",
" const config: ProcessorConfig = defaultConfig();",
' const range: VocalRange = "baritoneBass";',
" config.resynthesis.pitch.vocalRange = range;",
" config.resynthesis.pitch.sibilantBypass = true;",
" const processor = new Tunelet(48000, 128, config);",
" const input = new Int16Array(128);",
" const left = new Int16Array(128);",
Expand Down
11 changes: 5 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,10 @@
//!
//! [`Processor::feedback`] gives the pitch, the correction ratio, the
//! accumulated stereo peak and the frame count.
//! [`Processor::take_feedback`] also clears the peak window. The pitch
//! reporting bounds change only the reports. They do not change the detection
//! or the audio signal. [`Processor::latency_frames`] gives a nominal
//! resynthesis delay. Voice delays, pitch changes and effects have their own
//! timing.
//! [`Processor::take_feedback`] also clears the peak window. Both pitch
//! detection and feedback reports use the active [`VocalRange`] bounds.
//! [`Processor::latency_frames`] gives a nominal resynthesis delay. Voice
//! delays, pitch changes and effects have their own timing.
//!
//! [`Processor::telemetry`] gives data for meters and displays: the ratio and
//! the gain of each voice, the input and output peaks, the pitch clarity and
Expand Down Expand Up @@ -133,7 +132,7 @@ pub use harmony::{
ChordDegree, ChordName, HarmonyConfig, HarmonyScale, HarmonySource, Inversion, HARMONY_VOICES,
};
pub use midi::MidiEvent;
pub use pitch::PitchConfig;
pub use pitch::{PitchConfig, VocalRange};
pub use processor::{Feedback, Processor, Telemetry};
pub use resynthesis::{CorrectionConfig, MultiVoiceMode, ResynthesisConfig};
pub use scale::{CorrectionScale, NoteMask, PitchClass};
81 changes: 66 additions & 15 deletions src/pitch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,43 @@
//! estimates.
//!
//! Acquisition checks that repeated minima agree. Tracking refines the period
//! in a window of 40 lags. The reporting bounds filter only the feedback to
//! the host.
//! in a window of 40 lags. Both pitch tracking and host feedback enforce the
//! active [`VocalRange`] frequency limits.

mod search;
pub(crate) mod tracker;

use crate::error::{check_range, ConfigProblem};
use crate::Error;

/// The acquisition sensitivity and the bounds for pitch reports to the host.
/// The frequency preset for pitch acquisition, pitch tracking and host reports.
///
/// The default is [`Self::Auto`]. Presets restrict pitch detection and
/// feedback to the selected vocal register. Notes outside the active bounds
/// stay unvoiced and bypass pitch shift and harmony voices.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[cfg_attr(
all(feature = "wasm", target_arch = "wasm32"),
derive(serde::Serialize, serde::Deserialize, tsify::Tsify),
serde(rename_all = "camelCase")
)]
pub enum VocalRange {
/// Custom bounds from `min_frequency_hz` and `max_frequency_hz` in
/// [`PitchConfig`].
Custom,
/// The soprano register, 200..=1100 Hz.
Soprano,
/// The alto and tenor register, 100..=720 Hz.
AltoTenor,
/// The baritone and bass register, 45..=360 Hz.
BaritoneBass,
/// The full vocal register, 40..=1100 Hz. This is the default.
#[default]
Auto,
}

/// The acquisition sensitivity, vocal range preset, custom frequency limits
/// and sibilant bypass control.
#[derive(Clone, Copy, Debug, PartialEq)]
#[non_exhaustive]
#[cfg_attr(
Expand All @@ -26,42 +53,66 @@ pub struct PitchConfig {
/// This value controls acquisition. It is not an output gain or an
/// amplitude gate.
pub threshold: f32,
/// The lower bound of the feedback in hertz (inclusive), 40..=2000.
/// The active vocal register preset. The default is [`VocalRange::Auto`].
///
/// [`VocalRange::Custom`] uses `min_frequency_hz` and `max_frequency_hz`.
/// The other presets use fixed frequency limits. Both the pitch tracker
/// and [`crate::Processor::feedback`] enforce the active limits.
pub vocal_range: VocalRange,
/// The custom lower bound in hertz (inclusive), 40..=2000.
///
/// The default is 40. It must be strictly less than `max_frequency_hz`.
/// These bounds do not change the tracking, the correction or the
/// resampling. A feedback query applies the current bounds to the kept
/// analysis. It does not process another sample.
/// [`VocalRange::Custom`] enforces this bound in pitch detection and in
/// feedback reports. Validation checks this field in all vocal ranges.
pub min_frequency_hz: f32,
/// The upper bound of the feedback in hertz (inclusive), 40..=2000.
/// The custom upper bound in hertz (inclusive), 40..=2000.
///
/// The default is 1100. It must be strictly greater than
/// `min_frequency_hz`. Results outside the bounds are reported as
/// unvoiced, with zero frequency and zero period.
/// `min_frequency_hz`. [`VocalRange::Custom`] enforces this bound in pitch
/// detection and in feedback reports. Results outside the active bounds
/// are unvoiced, with zero frequency and zero period.
pub max_frequency_hz: f32,
/// Enables the 3.5 kHz sibilant crossover split. The default is `true`.
///
/// If this field is `true`, active sibilants split into a low band and a
/// high band. The high band bypasses pitch shift and harmony voices at
/// unity pitch.
pub sibilant_bypass: bool,
}

impl Default for PitchConfig {
fn default() -> Self {
Self {
threshold: 0.1,
vocal_range: VocalRange::Auto,
min_frequency_hz: 40.0,
max_frequency_hz: 1100.0,
sibilant_bypass: true,
}
}
}

impl PitchConfig {
/// Applies only the bounds for host reports to a kept estimate.
/// Returns the inclusive lower and upper frequency limits in hertz for the
/// active [`VocalRange`].
pub(crate) fn bounds(self) -> (f32, f32) {
match self.vocal_range {
VocalRange::Custom => (self.min_frequency_hz, self.max_frequency_hz),
VocalRange::Soprano => (200.0, 1100.0),
VocalRange::AltoTenor => (100.0, 720.0),
VocalRange::BaritoneBass => (45.0, 360.0),
VocalRange::Auto => (40.0, 1100.0),
}
}

/// Applies the active frequency bounds to a kept estimate for host reports.
///
/// If the estimate is not voiced or is out of range, the result is the
/// unvoiced value with all fields zero. The function does not change the
/// tracker state or the correction state.
pub(crate) fn report(self, estimate: PitchEstimate) -> PitchEstimate {
if estimate.voiced
&& estimate.frequency_hz >= self.min_frequency_hz
&& estimate.frequency_hz <= self.max_frequency_hz
{
let (min_hz, max_hz) = self.bounds();
if estimate.voiced && estimate.frequency_hz >= min_hz && estimate.frequency_hz <= max_hz {
estimate
} else {
PitchEstimate::default()
Expand Down
Loading