pikopiko-runtime 0.1.0

no_std, heap-free runtime for pikopiko: synth trait, event-binary sequencer, effects and built-in synths (AY-3-8912, buzzer, sfx).
Documentation
//! 再生中の通知と、マーカーへ移動するときの指定。`pikopiko::Player` とランタイムの
//! [`Sequencer`](crate::binary::Sequencer) で共通に使う。

/// マーカーへ移動するとき。
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum JumpAt {
    /// すぐ。
    Now,
    /// 次の拍の頭(`#meter` の拍)。
    Beat,
    /// 次の小節の頭。
    Bar,
    /// 曲の終わり(ループで戻るところ)。
    End,
}

/// どの通知を受け取るか。ビットの組み合わせ。
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct NoticeMask(pub u8);

impl NoticeMask {
    pub const MARKER: NoticeMask = NoticeMask(1);
    pub const BAR: NoticeMask = NoticeMask(1 << 1);
    pub const BEAT: NoticeMask = NoticeMask(1 << 2);
    pub const LOOP: NoticeMask = NoticeMask(1 << 3);
    pub const JUMP: NoticeMask = NoticeMask(1 << 4);
    pub const FINISHED: NoticeMask = NoticeMask(1 << 5);
    /// 効果音の通知(`pikopiko::Player` だけが出す)。
    pub const SFX: NoticeMask = NoticeMask(1 << 6);
    /// 移調で音域の外に出た音を折り返したとき(`SetTranspose` のたびに最初の 1 回だけ)。
    pub const TRANSPOSE: NoticeMask = NoticeMask(1 << 7);
    /// 何も受け取らない。
    pub const NONE: NoticeMask = NoticeMask(0);
    /// すべて。
    pub const ALL: NoticeMask = NoticeMask(0xff);
    /// 既定(拍と小節と移調の折り返し以外)。
    pub const DEFAULT: NoticeMask = NoticeMask(0x7f & !(1 << 1) & !(1 << 2));

    pub const fn contains(self, other: NoticeMask) -> bool {
        self.0 & other.0 == other.0
    }

    pub const fn union(self, other: NoticeMask) -> NoticeMask {
        NoticeMask(self.0 | other.0)
    }
}

impl Default for NoticeMask {
    fn default() -> Self {
        NoticeMask::DEFAULT
    }
}

impl core::ops::BitOr for NoticeMask {
    type Output = NoticeMask;
    fn bitor(self, rhs: NoticeMask) -> NoticeMask {
        self.union(rhs)
    }
}

/// [`Sequencer`](crate::binary::Sequencer) の通知([`Sequencer::poll_notice`](crate::binary::Sequencer::poll_notice))。
/// 中身は `pikopiko::Notice` の効果音以外と同じ。
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SeqNotice {
    /// マーカーを通った。`index` は曲のマーカーの番号(書いた順、0 始まり)。
    Marker { index: u8 },
    /// 小節の頭を通った(0 始まりの小節の番号)。
    Bar(u32),
    /// 拍の頭を通った(小節の番号と、小節の中の拍の番号。どちらも 0 始まり)。
    Beat { bar: u32, beat: u8 },
    /// ループで戻った(戻った回数の合計)。
    Looped(u32),
    /// マーカーへ移動した。
    Jumped { marker: u8 },
    /// 曲が終わった。
    Finished,
    /// 移調で音域の外に出た音を折り返した。`track` はそのトラック。移調を変えるたびに最初の 1 回だけ。
    Transposed { track: u8 },
}

impl SeqNotice {
    /// この通知を受け取るかを決めるビット。
    pub const fn mask(&self) -> NoticeMask {
        match self {
            SeqNotice::Marker { .. } => NoticeMask::MARKER,
            SeqNotice::Bar(_) => NoticeMask::BAR,
            SeqNotice::Beat { .. } => NoticeMask::BEAT,
            SeqNotice::Looped(_) => NoticeMask::LOOP,
            SeqNotice::Jumped { .. } => NoticeMask::JUMP,
            SeqNotice::Finished => NoticeMask::FINISHED,
            SeqNotice::Transposed { .. } => NoticeMask::TRANSPOSE,
        }
    }
}

/// 固定長の通知のキュー(ヒープを使わない。あふれたら古いものから捨てる)。
#[derive(Debug, Clone, Copy)]
pub(crate) struct NoticeQueue<const N: usize> {
    buf: [SeqNotice; N],
    head: usize,
    len: usize,
    pub(crate) mask: NoticeMask,
}

impl<const N: usize> NoticeQueue<N> {
    pub(crate) const NEW: Self = NoticeQueue {
        buf: [SeqNotice::Finished; N],
        head: 0,
        len: 0,
        mask: NoticeMask::DEFAULT,
    };

    pub(crate) fn push(&mut self, n: SeqNotice) {
        if N == 0 || !self.mask.contains(n.mask()) {
            return;
        }
        let at = (self.head + self.len) % N;
        self.buf[at] = n;
        if self.len < N {
            self.len += 1;
        } else {
            self.head = (self.head + 1) % N;
        }
    }

    pub(crate) fn pop(&mut self) -> Option<SeqNotice> {
        if self.len == 0 {
            return None;
        }
        let n = self.buf[self.head];
        self.head = (self.head + 1) % N;
        self.len -= 1;
        Some(n)
    }
}