glicol_synth/node/sequencer/
choose.rs

1use crate::{impl_to_boxed_nodedata, BoxedNodeSend, Buffer, Input, Message, Node, NodeData};
2use dasp_signal::{self as signal, Signal};
3use hashbrown::HashMap;
4
5pub struct Choose {
6    sig: Box<dyn Signal<Frame = f64> + Send>,
7    note_list: Vec<f32>,
8    input_order: Vec<usize>,
9}
10
11impl Choose {
12    pub fn new(note_list: Vec<f32>, seed: u64) -> Self {
13        Self {
14            sig: Box::new(signal::noise(seed)),
15            note_list,
16            input_order: Vec::new(),
17        }
18    }
19    impl_to_boxed_nodedata!();
20}
21
22impl<const N: usize> Node<N> for Choose {
23    fn process(&mut self, _inputs: &mut HashMap<usize, Input<N>>, output: &mut [Buffer<N>]) {
24        // TODO: better picking algo?
25        let mut id = ((self.sig.next() * 0.5 + 0.5) * self.note_list.len() as f64) as usize;
26        if id == self.note_list.len() {
27            id = 0
28        };
29        output[0].iter_mut().for_each(|s| *s = self.note_list[id]);
30    }
31    fn send_msg(&mut self, info: Message) {
32        match info {
33            Message::SetToNumberList(0, list) => self.note_list = list,
34            Message::Index(i) => self.input_order.push(i),
35            Message::IndexOrder(pos, index) => self.input_order.insert(pos, index),
36            Message::ResetOrder => {
37                self.input_order.clear();
38            }
39            _ => {}
40        }
41    }
42}