nooise 2.5.0

Ambient music generator for the terminal
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
//! The `/` control palette.
//!
//! `/` opens a fuzzy-find prompt over every control id in the registry.
//! Typing filters; Tab locks the highlighted control; digits typed after the
//! lock become a value. Enter stages the edit (or jumps when no value was
//! typed); Enter on an empty prompt commits every staged edit at once, and
//! Ctrl+B commits them on the next bar downbeat instead. The registry tables
//! stay the single source of truth — the palette only builds a flat address
//! space over them, exactly one entry per unique control id at its native
//! (deepest) editing surface.

use super::module::{MODULE_CATALOG, chain_amount_slot, module_available_on, tab_has_module_chain};
use super::*;

/// What a palette row does when confirmed.
///
/// Entries are a pure function of the current tab, deliberately: the
/// interaction kernel and the renderer each build this list independently and
/// confirm works by index, so anything that made the list depend on live
/// controls could desync them and jump to the wrong control. Live state is
/// resolved where it exists — in the adapter, and in the value column.
pub(crate) enum PaletteEntry {
    /// Jump to a control at the tab that natively owns it.
    Control {
        tab: Tab,
        index_in_tab: usize,
        spec: &'static ControlSpec,
    },
    /// A catalog module on `tab`'s chain. Whether confirming adds it or jumps
    /// to the copy already there is decided at execution time, so the palette
    /// can never silently create a second copy.
    Module { tab: Tab, catalog_index: usize },
    /// A stable slot control projected under the active module's human-facing
    /// dotted scope; its backing id remains slot-addressed for song codes.
    ModuleControl {
        tab: Tab,
        spec: &'static ControlSpec,
        module_name: &'static str,
        parameter: &'static str,
    },
}

impl PaletteEntry {
    /// Text the query matches against. Must stay a pure function of the
    /// entry, for the reason on the enum.
    pub(crate) fn haystack(&self) -> String {
        match self {
            Self::Control { tab, spec, .. } => {
                format!("{} · {} · {}", spec.id, tab.name(), spec.label)
            }
            Self::Module { tab, catalog_index } => {
                let kind = MODULE_CATALOG[*catalog_index];
                format!(
                    "{} · {} · {} · module",
                    kind.id,
                    kind.display_name,
                    tab.name()
                )
            }
            Self::ModuleControl {
                tab,
                spec,
                module_name,
                parameter,
            } => {
                format!(
                    "{}.{}.{} · {}",
                    tab.name().to_lowercase(),
                    module_name.to_lowercase(),
                    parameter.to_lowercase().replace(' ', "_"),
                    spec.label
                )
            }
        }
    }

    pub(crate) fn spec(&self) -> Option<&'static ControlSpec> {
        match self {
            Self::Control { spec, .. } | Self::ModuleControl { spec, .. } => Some(spec),
            Self::Module { .. } => None,
        }
    }

    pub(crate) fn id(&self) -> Option<&'static str> {
        self.spec().map(|spec| spec.id)
    }

    /// Value column. This one may read live controls: it is rendered, never
    /// matched against, so it cannot affect indices.
    pub(crate) fn value(&self, c: &FluidControls) -> String {
        match self {
            Self::Control { spec, .. } | Self::ModuleControl { spec, .. } => (spec.display)(c),
            Self::Module { tab, catalog_index } => {
                let kind = MODULE_CATALOG[*catalog_index];
                c.modules
                    .for_tab(*tab)
                    .and_then(|slots| chain_amount_slot(slots, kind.id))
                    .map_or_else(
                        || "add".to_string(),
                        |slot| {
                            format!(
                                "{:.0}%",
                                c.modules.for_tab(*tab).expect("layer exists")[slot].amount * 100.0
                            )
                        },
                    )
            }
        }
    }
}

/// Flat address space the palette searches: every unique control at its
/// owning tab, plus every catalog module on every layer that has a chain.
/// Duplicated placements (e.g. `pad.level` on both Master and Chords) collapse
/// to the owning tab. Module slot rows are excluded as controls — a module is
/// reached through its `Module` entry, which knows how to find or create it.
pub(crate) fn palette_entries() -> Vec<PaletteEntry> {
    let mut entries: Vec<PaletteEntry> = Vec::new();
    for tab in Tab::all() {
        for (index_in_tab, spec) in tab_specs(tab).iter().enumerate() {
            if tab_owning_control(spec.id) == Some(tab)
                && parse_module_slot_id(spec.id).is_none()
                && !entries.iter().any(|e| e.id() == Some(spec.id))
            {
                entries.push(PaletteEntry::Control {
                    tab,
                    index_in_tab,
                    spec,
                });
            }
        }
    }
    for tab in Tab::all() {
        if !tab_has_module_chain(tab) {
            continue;
        }
        for (catalog_index, kind) in MODULE_CATALOG.iter().copied().enumerate() {
            if module_available_on(kind, tab) {
                entries.push(PaletteEntry::Module { tab, catalog_index });
            }
        }
    }
    entries
}

/// One fuzzy candidate: which entry, how well it scored, and which characters
/// of its haystack matched (for highlighting).
pub(crate) struct PaletteMatch {
    pub(crate) entry_index: usize,
    pub(crate) score: i32,
    pub(crate) hits: Vec<usize>,
}

/// The module detail surface the palette was opened from.
///
/// While this is set the palette lists that module's own parameters instead of
/// the whole registry, so `/` inside a module drill stays inside it.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) struct ModuleScope {
    pub(crate) tab: Tab,
    /// Storage slot on `tab`'s chain, not a catalog position.
    pub(crate) slot: usize,
    pub(crate) catalog_index: usize,
}

/// A value edit waiting in the palette's stage.
#[derive(Clone, Debug, PartialEq)]
pub(crate) struct StagedEdit {
    pub(crate) id: &'static str,
    pub(crate) value: f32,
}

pub(crate) struct PaletteState {
    entries: Vec<PaletteEntry>,
    current_tab: Tab,
    recent: Vec<&'static str>,
    pub(crate) query: String,
    pub(crate) matches: Vec<PaletteMatch>,
    pub(crate) selected: usize,
    /// Entry locked by Tab-autocomplete; digits typed afterwards build
    /// `value_buf` for that control.
    pub(crate) locked: Option<usize>,
    pub(crate) value_buf: String,
    pub(crate) staged: Vec<StagedEdit>,
}

impl PaletteState {
    /// `recent` is most-recent-first and intentionally has no usage count:
    /// touching a control again simply promotes it to the front.
    pub(crate) fn new(
        current_tab: Tab,
        recent: &[&'static str],
        module_scope: Option<ModuleScope>,
    ) -> Self {
        let mut state = Self {
            entries: module_scope.map_or_else(palette_entries, |scope| {
                module_palette_entries(scope.tab, scope.slot, scope.catalog_index)
            }),
            current_tab,
            recent: recent.to_vec(),
            query: String::new(),
            matches: Vec::new(),
            selected: 0,
            locked: None,
            value_buf: String::new(),
            staged: Vec::new(),
        };
        state.recompute();
        state
    }

    pub(crate) fn entry(&self, index: usize) -> &PaletteEntry {
        &self.entries[index]
    }

    pub(crate) fn contains_entry(&self, index: usize) -> bool {
        index < self.entries.len()
    }

    pub(crate) fn push_char(&mut self, c: char) {
        if self.locked.is_some() {
            if c.is_ascii_digit() || c == '.' || c == '-' {
                self.value_buf.push(c);
            }
        } else {
            self.query.push(c);
            self.recompute();
        }
    }

    fn recompute(&mut self) {
        self.matches.clear();
        if self.query.is_empty() {
            // Empty query is a global MRU navigator: every recent control,
            // regardless of page, then unused current-page controls, then
            // the remaining registry order.
            for entry_index in 0..self.entries.len() {
                self.matches.push(PaletteMatch {
                    entry_index,
                    score: 0,
                    hits: Vec::new(),
                });
            }
            self.sort_by_context();
        } else {
            for (entry_index, entry) in self.entries.iter().enumerate() {
                if let Some((score, hits)) = fuzzy_score(&self.query, &entry.haystack()) {
                    self.matches.push(PaletteMatch {
                        entry_index,
                        score,
                        hits,
                    });
                }
            }
            let entries = &self.entries;
            let recent = &self.recent;
            let current_tab = self.current_tab;
            let query = self.query.as_str();
            // The layer-primary boost outranks the fuzzy score deliberately.
            // Score alone puts `pad.stereo_width` above `pad.level` for the
            // query "pads", because its `s` follows the dot and collects a
            // word-start bonus. Typing a bare layer name must reach that
            // layer's level regardless.
            let primary_key = |entry: usize| {
                entries[entry]
                    .id()
                    .and_then(|id| layer_primary_rank(id, query))
                    .unwrap_or(usize::MAX)
            };
            self.matches.sort_by(|left, right| {
                primary_key(left.entry_index)
                    .cmp(&primary_key(right.entry_index))
                    .then_with(|| right.score.cmp(&left.score))
                    .then_with(|| {
                        context_rank(entries, left.entry_index, current_tab, recent).cmp(
                            &context_rank(entries, right.entry_index, current_tab, recent),
                        )
                    })
            });
        }
        self.selected = 0;
    }

    fn sort_by_context(&mut self) {
        let entries = &self.entries;
        let recent = &self.recent;
        let current_tab = self.current_tab;
        self.matches
            .sort_by_key(|m| context_rank(entries, m.entry_index, current_tab, recent));
    }
}

fn module_palette_entries(tab: Tab, slot: usize, catalog_index: usize) -> Vec<PaletteEntry> {
    MODULE_CATALOG[catalog_index]
        .parameters()
        .iter()
        .filter_map(|parameter| {
            module_slot_spec(tab, slot, parameter.field).map(|spec| PaletteEntry::ModuleControl {
                tab,
                spec,
                module_name: MODULE_CATALOG[catalog_index].display_name,
                parameter: parameter.label,
            })
        })
        .collect()
}

/// A layer's primary control outranks everything while the query is still
/// just that layer's name, so typing `bass` always lands on Bass Level
/// whatever the MRU holds. Matches the id namespace (`bass.`) and the tab
/// name (`Bass`) alike, since `Tab::Chords` is spelled `Pads` but owns
/// `pad.*`. Returns the tab's discriminant so ambiguous prefixes (`m` hits
/// both Arp and Master) stay deterministically ordered.
///
/// The boost falls away on its own once the query grows past the namespace
/// (`bass.d` is no longer a prefix of `bass`), handing ranking back to the
/// fuzzy score.
fn layer_primary_rank(id: &str, query: &str) -> Option<usize> {
    if query.is_empty() {
        return None;
    }
    Tab::all().iter().position(|&tab| {
        tab.level_id() == Some(id) && {
            let namespace = id.split('.').next().unwrap_or(id);
            starts_with_ignore_case(namespace, query) || starts_with_ignore_case(tab.name(), query)
        }
    })
}

fn starts_with_ignore_case(haystack: &str, prefix: &str) -> bool {
    haystack.len() >= prefix.len()
        && haystack
            .chars()
            .zip(prefix.chars())
            .all(|(h, p)| h.eq_ignore_ascii_case(&p))
}

/// Recent controls always win, independent of page. Page order only breaks
/// ties once the user has exhausted their global MRU list.
fn context_rank(
    entries: &[PaletteEntry],
    entry: usize,
    current_tab: Tab,
    recent: &[&'static str],
) -> (u8, usize, usize) {
    let palette_entry = &entries[entry];
    let recent_rank = recent.iter().position(|&id| Some(id) == palette_entry.id());
    // A module offered for the page you are on ranks with that page's own
    // controls, so "swing" on Bass reaches Bass before it reaches Tonal.
    let page_index = match palette_entry {
        PaletteEntry::Module { tab, catalog_index } => {
            (*tab == current_tab).then_some(tab_specs(current_tab).len() + catalog_index)
        }
        PaletteEntry::Control { spec, .. } => tab_specs(current_tab)
            .iter()
            .position(|other| other.id == spec.id),
        PaletteEntry::ModuleControl { tab, spec, .. } => (*tab == current_tab)
            .then(|| {
                tab_specs(current_tab)
                    .iter()
                    .position(|other| other.id == spec.id)
            })
            .flatten(),
    };
    let group = match (recent_rank, page_index) {
        (Some(_), _) => 0,
        (None, Some(_)) => 1,
        (None, None) => 2,
    };
    let order = match group {
        0 => recent_rank.unwrap_or(usize::MAX),
        1 => page_index.unwrap_or(usize::MAX),
        _ => entry,
    };
    (group, order, entry)
}

/// Beat position of the next bar downbeat (4/4 throughout the engine), for
/// the palette's quantized commit.
pub(crate) fn next_bar_beat(beat: f64) -> f64 {
    const BEATS_PER_BAR: f64 = 4.0;
    (beat / BEATS_PER_BAR).floor() * BEATS_PER_BAR + BEATS_PER_BAR
}

/// Greedy subsequence fuzzy match. Returns `None` if `query` is not a
/// subsequence of `haystack` (case-insensitive), otherwise a score (higher is
/// better) plus the matched char indices into `haystack` for highlighting.
///
/// Scoring rewards contiguous runs and word-start matches, and penalises how
/// deep the first match sits, so "rev" ranks a Reverb module above a scattered
/// hit inside another label.
pub(crate) fn fuzzy_score(query: &str, haystack: &str) -> Option<(i32, Vec<usize>)> {
    let needle: Vec<char> = query.chars().flat_map(char::to_lowercase).collect();
    if needle.is_empty() {
        return Some((0, Vec::new()));
    }
    let hay: Vec<char> = haystack.chars().collect();
    // Compare against a lowercased view that stays index-aligned with the
    // original haystack (single-char lowercase), so hit indices highlight the
    // right characters.
    let lower_at = |i: usize| -> char {
        hay.get(i)
            .and_then(|c| c.to_lowercase().next())
            .unwrap_or(' ')
    };

    let mut hits = Vec::with_capacity(needle.len());
    let mut score = 0i32;
    let mut hay_i = 0usize;
    let mut prev_hit: Option<usize> = None;
    for &nc in &needle {
        let mut found = None;
        while hay_i < hay.len() {
            if lower_at(hay_i) == nc {
                found = Some(hay_i);
                break;
            }
            hay_i += 1;
        }
        let hit_index = found?;
        // Contiguous with previous match.
        if prev_hit == Some(hit_index.wrapping_sub(1)) {
            score += 8;
        }
        // Word-start bonus (start of string or preceded by a separator).
        let at_word_start =
            hit_index == 0 || matches!(hay.get(hit_index - 1), Some(' ') | Some('·') | Some('.'));
        if at_word_start {
            score += 6;
        }
        if hits.is_empty() {
            // Penalise a deep first match so shallow, leading matches win.
            score -= hit_index as i32;
        }
        hits.push(hit_index);
        prev_hit = Some(hit_index);
        hay_i = hit_index + 1;
    }
    // Reward matching a larger fraction of the label.
    score += (needle.len() as i32) * 2;
    Some((score, hits))
}