quantum_entangler/
pattern.rs1use rand::{thread_rng, Rng};
2
3use crate::midi;
4
5pub fn new(properties: &str, note: &mut midi::perform::Note, buffer: &mut std::sync::MutexGuard<std::vec::Vec<midi::time::MidiBuffer>>) {
6
7 let props: Vec<&str> = properties.split("|").collect();
8 let note_values: Vec<usize> = props[1].split(",").map(|x| x.parse::<usize>().unwrap()).collect();
9 match props[0] {
10 "random" => parse(note_values, note, random, buffer),
11 _ => parse(note_values, note, random, buffer)
12 }
13}
14
15fn random(buffer_length: usize) -> usize {
16 let mut rng = thread_rng();
17 let index = rng.gen_range(1..buffer_length - 1);
18 if index % 2 == 0 {
19 return index - 1
20 }
21 index
22}
23
24fn parse(note_values: Vec<usize>, note: &mut midi::perform::Note, note_selector: fn(buffer_length: usize) -> usize, buffer: &mut std::sync::MutexGuard<std::vec::Vec<midi::time::MidiBuffer>>) {
25 for note_value in note_values {
26 let duration;
29 match note_value {
30 1 => duration = 2000000,
31 2 => duration = 1000000,
32 3 => duration = 666666,
33 4 => duration = 500000,
34 5 => duration = 400000,
35 6 => duration = 333333,
36 7 => duration = 285714,
37 8 => duration = 250000,
38 9 => duration = 222222,
39 _ => duration = 500000,
40 }
41 note.new(note_selector(buffer.len()), duration as u64, buffer);
42 }
43}