Skip to content
Open
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
23 changes: 16 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,15 @@ The default is 0, which applies full pull across the whole bin.
dry path starts after Warm and has a delay of 1,593 frames. All six reverb
modes keep the stereo dry signal.

`resynthesis.pitch.vocal_range` selects `Custom`, `Soprano` (240 to 1,100 Hz),
`AltoTenor` (120 to 600 Hz) or `BaritoneBass` (45 to 360 Hz). The default is
`Custom`, which uses `min_frequency_hz` to `max_frequency_hz` (40 to 1,100 Hz
by default). A small fixed-point period tolerance keeps boundary pitches voiced,
so a reported frequency can be slightly outside the nominal hertz bounds.
Pitches outside the active period limits do not acquire or track. If there is no
tracked pitch for 20 ms, the fallback period becomes zero. Then the lead ratio
returns to 1 and the harmony voices release toward zero.

### Harmony sources

Set `resynthesis.mode` to `Harmony`. Then select the source of the voices
Expand Down Expand Up @@ -579,11 +588,11 @@ and show enums as string literals. To change the configuration, do these steps:

If a required field is absent or a value is not valid, the call throws a
JavaScript `Error`. If an optional field is absent or `undefined`, it gets its
default. `resynthesis.correction.humanize` and
`resynthesis.correction.flexTune` default to 0, and
`resynthesis.harmony.voices` defaults to 4. The conversion ignores extra object
properties. The generated declarations contain the parameter documentation from
the Rust types.
default. `resynthesis.pitch.vocalRange` defaults to `"custom"`,
`resynthesis.correction.humanize` and `resynthesis.correction.flexTune` default
to 0, and `resynthesis.harmony.voices` defaults to 4. 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 Expand Up @@ -627,8 +636,8 @@ which are also 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 period is outside the active `resynthesis.pitch.vocalRange`
preset or the custom `minFrequencyHz` and `maxFrequencyHz` bounds.

`formantEnvelope(maxFrequencyHz, input, output)` fills two `Float32Array`
buffers of equal length and returns `false` if no envelope is available.
Expand Down
4 changes: 4 additions & 0 deletions scripts/normalize-jsdoc.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,15 @@ const EXACT_REPLACEMENTS = [
["[`ReverbConfig::brightness_hz`]", "`ReverbConfig.brightnessHz`"],
["[`crate::AlienConfig::loop_frames`]", "`AlienConfig.loopFrames`"],
["[`crate::PitchConfig`]", "`PitchConfig`"],
["[`PitchConfig`]", "`PitchConfig`"],
["[`ProcessorConfig`]", "`ProcessorConfig`"],
["[`ControlActivity`]", "`ControlActivity`"],
["[`TimedEvent`]", "`TimedEvent`"],
["[`HarmonyScale`]", "`HarmonyScale`"],
["[`Feedback`]", "`Feedback`"],
["[`HarmonyConfig`]", "`HarmonyConfig`"],
["[`Self::Custom`]", '`"custom"`'],
["[`VocalRange::Custom`]", '`"custom"`'],
[
"[`ControlEvent::Parameter`]",
'`{ type: "parameter", value }` in `ControlEvent`',
Expand All @@ -104,6 +107,7 @@ const EXACT_REPLACEMENTS = [
["`None`", "`undefined`"],
["`u64::MAX`", "`2n ** 64n - 1n`"],
["`0x1234_abcd`", "`0x1234abcdn`"],
["`vocal_range`", "`vocalRange`"],
["`max_frequency_hz`", "`maxFrequencyHz`"],
["`min_frequency_hz`", "`minFrequencyHz`"],
["`stereo_output`", "`stereoOutput`"],
Expand Down
19 changes: 18 additions & 1 deletion scripts/verify-pack.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -313,11 +313,13 @@ function verifyRuntimeImport(consumerDir, packageName) {
"assert.ok(wasmExports.memory instanceof WebAssembly.Memory);",
"",
"const initialConfig = rootModule.defaultConfig();",
"initialConfig.resynthesis.pitch.vocalRange = undefined;",
"initialConfig.resynthesis.correction.humanize = undefined;",
"initialConfig.resynthesis.correction.flexTune = undefined;",
"initialConfig.resynthesis.harmony.voices = undefined;",
"const processor = new rootModule.Tunelet(48000, 128, initialConfig);",
"try {",
' assert.equal(processor.config().resynthesis.pitch.vocalRange, "custom");',
" assert.equal(processor.config().resynthesis.correction.humanize, 0);",
" assert.equal(processor.config().resynthesis.correction.flexTune, 0);",
" assert.equal(processor.config().resynthesis.harmony.voices, 4);",
Expand Down Expand Up @@ -352,20 +354,24 @@ function verifyRuntimeImport(consumerDir, packageName) {
" assert.equal(processor.config().resynthesis.correction.flexTune, 0.25);",
"",
" const omittedCorrectionConfig = processor.config();",
" delete omittedCorrectionConfig.resynthesis.pitch.vocalRange;",
" delete omittedCorrectionConfig.resynthesis.correction.humanize;",
" delete omittedCorrectionConfig.resynthesis.correction.flexTune;",
" processor.setConfig(omittedCorrectionConfig);",
' assert.equal(processor.config().resynthesis.pitch.vocalRange, "custom");',
" assert.equal(processor.config().resynthesis.correction.humanize, 0);",
" assert.equal(processor.config().resynthesis.correction.flexTune, 0);",
"",
" const mono = new Int16Array(128);",
" const eventResynthesis = processor.config().resynthesis;",
" eventResynthesis.pitch.vocalRange = undefined;",
" eventResynthesis.correction.humanize = undefined;",
" eventResynthesis.correction.flexTune = undefined;",
" eventResynthesis.harmony.voices = undefined;",
" processor.processMono(mono, [",
' { offset: 0, event: { type: "resynthesis", value: eventResynthesis } },',
" ]);",
' assert.equal(processor.config().resynthesis.pitch.vocalRange, "custom");',
" assert.equal(processor.config().resynthesis.correction.humanize, 0);",
" assert.equal(processor.config().resynthesis.correction.flexTune, 0);",
" assert.equal(processor.config().resynthesis.harmony.voices, 4);",
Expand Down Expand Up @@ -743,6 +749,7 @@ function verifyTypeDeclarations(consumerDir, packageName) {
" type ResynthesisConfig,",
" type SyncInitInput as RootSyncInitInput,",
" type Telemetry,",
" type VocalRange,",
`} from ${pkgLiteral};`,
"import init, {",
" initSync as wasmInitSync,",
Expand Down Expand Up @@ -770,10 +777,15 @@ function verifyTypeDeclarations(consumerDir, packageName) {
" module: syncSource,",
" });",
" const baseConfig: ProcessorConfig = defaultConfig();",
' const presetRange: VocalRange = "baritoneBass";',
" const configWithUndefined: ProcessorConfig = {",
" ...baseConfig,",
" resynthesis: {",
" ...baseConfig.resynthesis,",
" pitch: {",
" ...baseConfig.resynthesis.pitch,",
" vocalRange: optionalVoices === undefined ? undefined : presetRange,",
" },",
" correction: {",
" ...baseConfig.resynthesis.correction,",
" humanize: optionalVoices,",
Expand Down Expand Up @@ -822,6 +834,11 @@ function verifyTypeDeclarations(consumerDir, packageName) {
" views.refresh();",
" const resynthesisUpdate: ResynthesisConfig = {",
" ...configWithUndefined.resynthesis,",
" pitch: {",
" threshold: configWithUndefined.resynthesis.pitch.threshold,",
" minFrequencyHz: configWithUndefined.resynthesis.pitch.minFrequencyHz,",
" maxFrequencyHz: configWithUndefined.resynthesis.pitch.maxFrequencyHz,",
" },",
" correction: {",
" enabled: configWithUndefined.resynthesis.correction.enabled,",
" notes: configWithUndefined.resynthesis.correction.notes,",
Expand Down Expand Up @@ -1156,7 +1173,7 @@ function verifyNormalizedJsDoc(consumerDir, packageName) {
);
assert.doesNotMatch(
text,
/`(?:max_frequency_hz|min_frequency_hz|stereo_output|retune_ms|attack_ms|gain_db|ceiling_db)`/,
/`(?:vocal_range|max_frequency_hz|min_frequency_hz|stereo_output|retune_ms|attack_ms|gain_db|ceiling_db)`/,
`Unexpected snake_case config field in ${filePath}`,
);
}
Expand Down
41 changes: 30 additions & 11 deletions src/harmony.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ const GAIN_ATTACK: Q12 = Q12::from_bits(163);
/// The Q12 step that decreases the gain envelope of a voice toward a lower
/// target gain (release).
///
/// The step applies one time at each voiced control step. From a gain of 1.0
/// The step applies one time at each control step toward a lower target,
/// also on unvoiced steps after the period becomes zero. From a gain of 1.0
/// to zero, the release takes 51 steps. The period portamento does not use
/// this value.
const GAIN_RELEASE: Q12 = Q12::from_bits(81);
Expand Down Expand Up @@ -251,8 +252,9 @@ pub enum Inversion {
///
/// The gain attack, the gain release and the period portamento advance at
/// the controller cadence. This cadence is usually every 101 frames. A MIDI
/// event can make the next control step occur sooner. The envelopes and the
/// portamento advance only while the input is voiced.
/// event can make the next control step occur sooner. The attack and the
/// portamento advance only while the input is voiced. If the kept period is
/// zero, the gain envelopes release toward zero.
#[derive(Clone, Copy, Debug, PartialEq)]
#[non_exhaustive]
#[cfg_attr(
Expand Down Expand Up @@ -478,8 +480,8 @@ pub(crate) struct HarmonyState {
/// The last usable Q14 ratio of the input period to the smoothed target
/// period for each voice.
current_ratio: [Q14; HARMONY_VOICES],
/// The Q12 attack and release envelope. A MIDI release or a reset does
/// not clear it.
/// The Q12 attack and release envelope. A MIDI release keeps it, and an
/// audio reset clears it.
smoothed_gain: [Q12; HARMONY_VOICES],
/// The last active Q6 target. It is kept while the gain of a released
/// voice decreases to zero.
Expand Down Expand Up @@ -655,6 +657,14 @@ impl HarmonyState {
self.setup_chord();
}
}
/// Clears the gain envelopes, the output periods and the voice ratios at
/// an audio reset.
pub(crate) fn clear_envelopes(&mut self) {
self.smoothed_gain.fill(Q12::ZERO);
self.period_output.fill(Q6::ZERO);
self.target_period.fill(Q6::ZERO);
self.current_ratio.fill(Q14::ONE);
}
/// Converts cents above C4 to a Q6 sample period. C4 is 900 cents below
/// A4 at 440 Hz.
/// The intermediate period is rounded to f32 before the fixed-point
Expand Down Expand Up @@ -896,17 +906,20 @@ impl HarmonyState {
/// Advances one voice from the Q6 tracked input period to a Q14 ratio and
/// a Q12 gain.
/// The voice index must be less than `HARMONY_VOICES`. If the input is
/// unvoiced (zero), the function returns a unity ratio and silence. It
/// also clears the portamento start state and keeps the gain envelope.
/// unvoiced (zero), the function decreases the gain envelope by
/// `GAIN_RELEASE` and clears the portamento start state. Then it returns a
/// unity ratio and the new gain.
/// If the input is voiced, the function advances the gain envelope. It
/// keeps the target of a released voice until the gain is zero. Then, in
/// the same step, it applies portamento in the period domain.
pub(crate) fn advance_voice(&mut self, period: Q6, voice: usize) -> (Q14, Q12) {
if period == Q6::ZERO {
let target_gain = if period == Q6::ZERO {
self.target_period[voice] = Q6::ZERO;
self.portamento_period[voice] = Q6::ZERO;
return (Q14::ONE, Q12::ZERO);
}
let target_gain = self.select_target(period, voice);
Q12::ZERO
} else {
self.select_target(period, voice)
};
if self.smoothed_gain[voice] < target_gain {
self.smoothed_gain[voice] = (self.smoothed_gain[voice] + GAIN_ATTACK).min(target_gain);
} else if self.smoothed_gain[voice] > target_gain {
Expand All @@ -918,6 +931,12 @@ impl HarmonyState {
self.period_output[voice] = Q6::ZERO;
self.portamento_period[voice] = Q6::ZERO;
}
if period == Q6::ZERO {
if self.smoothed_gain[voice] == Q12::ZERO {
self.current_ratio[voice] = Q14::ONE;
}
return (Q14::ONE, self.smoothed_gain[voice]);
}
if self.period_output[voice] > Q6::ZERO {
self.portamento_period[voice] = if self.portamento_period[voice] == Q6::ZERO {
self.period_output[voice]
Expand Down
12 changes: 6 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,11 @@
//!
//! [`Processor::feedback`] gives the pitch, the correction ratio, the
//! accumulated output 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, which the dry path also uses. Voice delays, pitch
//! changes and effects have their own timing.
//! [`Processor::take_feedback`] also clears the peak window. The active vocal
//! range limits acquisition, tracking and pitch reports.
//! [`Processor::latency_frames`] gives a nominal resynthesis delay, which the
//! dry path also uses. Voice delays, pitch changes and effects have their own
//! timing.
//!
//! [`Processor::control_activity`] and [`ProcessorConfig::control_activity`]
//! give [`ControlActivity`], which shows the active choir, harmony and reverb
Expand Down Expand Up @@ -146,7 +146,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};
Loading
Loading