xmrs 0.10.2

A library to edit SoundTracker data with pleasure
Documentation
//! `InstrDefault` — the canonical xmrs instrument representation.
//!
//! The struct is now decomposed into three sub-types, each tackling
//! one orthogonal concern:
//!
//! - [`VoiceSetup`] — envelope, fadeout, vibrato, filter, panning,
//!   random variations: everything that shapes the voice once it's
//!   been triggered.
//! - [`InstrumentBehavior`] — NNA / DCT / DCA: what happens when a
//!   channel already running this instrument is retriggered.
//! - [`Keyboard`] — per-input-note sample selection and
//!   transposition: the IT-style drum-kit table. For non-IT formats
//!   the keyboard is the identity remap.
//!
//! Plus the resources the instrument owns: the sample list and the
//! optional MIDI routing.

use serde::{Deserialize, Serialize};

use crate::instr_midi::InstrMidi;
use crate::instrument_behavior::InstrumentBehavior;
use crate::keyboard::Keyboard;
use crate::sample::Sample;
use crate::voice_setup::VoiceSetup;

use alloc::vec::Vec;

// Re-export the behavioural enums from their new home so existing
// users that imported them from `instr_default` keep compiling.
// The canonical location is `instrument_behavior`; these are just
// convenience aliases.
pub use crate::instrument_behavior::{DuplicateCheckAction, DuplicateCheckType, NewNoteAction};

#[derive(Default, Serialize, Deserialize, Debug, Clone)]
pub struct InstrDefault {
    /// Voice-shaping parameters: envelopes, fadeout, vibrato,
    /// filter, panning, random variations.
    pub voice: VoiceSetup,

    /// What happens when this instrument is retriggered: NNA / DCT /
    /// DCA.
    pub behavior: InstrumentBehavior,

    /// Per-input-note keyboard layout. For IT drum kits, maps each
    /// key to a sample and an output pitch; for other formats, all
    /// entries stay at `None` (= identity).
    pub keyboard: Keyboard,

    /// Optional MIDI routing for this instrument.
    pub midi: InstrMidi,
    pub midi_mute_computer: bool,

    /// The samples this instrument can trigger. Indexed by the
    /// `keyboard.sample_for_pitch` table.
    pub sample: Vec<Option<Sample>>,
}

// `Default` is derived above; the explicit constructor is gone now
// that every field has a meaningful `Default`.

impl InstrDefault {
    /// Convenience: set every input-note's sample mapping to
    /// `sample_index`. Forwarded to `Keyboard::map_all_to`. Used by
    /// importers that have a single sample but want it triggered
    /// uniformly across the whole keyboard.
    pub fn change_all_sample_for_pitch(&mut self, sample_index: usize) {
        self.keyboard.map_all_to(sample_index);
    }
}

// `Vec<Option<Sample>>` doesn't derive Default automatically, but
// our derive(Default) above relies on every field having one. Vec
// does have Default (empty vec), so the derive Just Works.