Selaa lähdekoodia

refactor to pattern

woelper 4 vuotta sitten
vanhempi
commit
d6995cdb9a
3 muutettua tiedostoa jossa 93 lisäystä ja 57 poistoa
  1. 6 1
      sampler.ini
  2. 32 35
      src/base.rs
  3. 55 21
      src/main.rs

+ 6 - 1
sampler.ini

@@ -29,7 +29,7 @@ Size=328,168
 Collapsed=0
 
 [Window][sound]
-Pos=21,42
+Pos=40,12
 Size=300,100
 Collapsed=0
 
@@ -38,3 +38,8 @@ Pos=56,432
 Size=300,100
 Collapsed=0
 
+[Window][pattern]
+Pos=38,153
+Size=316,208
+Collapsed=0
+

+ 32 - 35
src/base.rs

@@ -65,73 +65,70 @@ impl Sound {
 }
 
 #[derive(Clone, Debug)]
-pub struct Bar {
+pub struct Pattern {
     pub repeat: i32,
     pub bpm: i32,
-    pub sounds: Vec<Option<Sound>>
+    pub sounds: Vec<Vec<Option<Sound>>>
 }
 
-impl Default for Bar {
-    fn default() -> Bar {
-        Bar {
+impl Default for Pattern {
+    fn default() -> Pattern {
+        Pattern {
             repeat: 0,
             bpm: 120,
-            sounds: vec![None; 4]
+            sounds: vec![vec![None; 4]]
         }
     }
 }
 
 
-impl Bar {
+impl Pattern {
     pub fn play(&mut self, device: &rodio::Device) {
         let delay = ((60.0 / self.bpm as f32)*1000.0) as u64;
         for _ in 0..self.repeat+1 {
-            for sound in &mut self.sounds {
-                if let Some(s) = sound {
-                    s.play(device);
+            for row in &mut self.sounds {
+
+                for sound in row {
+                    if let Some(s) = sound {
+                        s.play(device);
+                    }
+                    thread::sleep(Duration::from_millis(delay));
                 }
-                thread::sleep(Duration::from_millis(delay));
+                
             }
+
         }
     }
-    pub fn extend(&mut self, slots: usize) {
-        self.sounds.resize(self.sounds.len() + slots, None);
-    }
-    pub fn length(&self) -> usize {
-        self.sounds.len()
+    // pub fn cols_mut(&mut self) -> Vec<Option>Sound>> {
+    //     for row in &mut self.sounds {
+    //         row.resize(self.sounds.len() + slots, None);
+    //     }
+    // }
+
+    pub fn extend_length(&mut self, slots: usize) {
+        for row in &mut self.sounds {
+            row.resize(row.len() + slots, None);
+        }
     }
-}
 
-#[derive(Clone, Debug)]
-pub struct Pattern {
-    pub bars: Vec<Bar>
-}
-
-impl Default for Pattern {
-    fn default() -> Pattern {
-        Pattern {bars: vec![Bar::default()]}
+    pub fn extend_row(&mut self, rows: usize) {
+        self.sounds.resize(self.sounds.len() + rows, vec![None; self.sounds[0].len()]);
     }
 
-}
-
-impl Pattern {
-    pub fn test(&mut self) {
-        self.bars[0].bpm = 10
-    }
-    pub fn bar_mut(&mut self, i: usize) -> &mut Bar {
-        &mut self.bars[i]
+    pub fn length(&self) -> usize {
+        self.sounds.len()
     }
 }
 
 #[derive(Clone, Debug)]
 pub struct Timeline {
-    pub tracks: Vec<Pattern>
+    pub patterns: Vec<Pattern>
 }
 
 impl Default for Timeline {
     fn default() -> Timeline {
         Timeline {
-            tracks: vec![Pattern::default()]
+            patterns: vec![Pattern::default()]
         }
     }
 }

+ 55 - 21
src/main.rs

@@ -21,6 +21,8 @@ fn collect_sounds (root: &str) -> Vec<Sound> {
     .filter_map(|e| e.ok())
     .collect()
 }
+
+
 fn main() {
     env_logger::init();
     let dev = rodio::default_output_device().unwrap();
@@ -38,13 +40,17 @@ fn main() {
     // dbg!(&track);
 
     // return;
-    let mut active_sound: usize = 0;
-    let mut active_track: usize = 0;
+    let mut pattern_col: usize = 0;
+    let mut pattern_row: usize = 0;
+    // let mut active_track: usize = 0;
     // let mut active_bar: usize = 0;
 
 
     // let mut active_bar = Bar::default();
-    let mut active_bar = &mut timeline.tracks[0].bars[0];
+    // let mut active_bar = &mut timeline.patterns[0].bars[0];
+    let mut active_pattern = Pattern::default();
+    let mut active_sound: &Option<Sound> = &None;
+    // let mut active_pattern = &mut timeline.patterns[0];
 
 
     let mut system = support_ogl::init(file!());
@@ -69,8 +75,8 @@ fn main() {
                     for s in &mut sounds {
                         ui.tree_node(&im_str!("{}", s.name)).build(|| {
                             // ui.same_line(0.0);
-                            if ui.small_button(im_str!("add to bar")) {
-                                active_bar.sounds[active_sound] = Some(s.clone());
+                            if ui.small_button(im_str!("add to pattern")) {
+                                active_pattern.sounds[pattern_row][pattern_col] = Some(s.clone());
                             }
                             ui.same_line(0.0);
                             if ui.small_button(im_str!("play")) {
@@ -81,38 +87,66 @@ fn main() {
                 });
             });
 
-        Window::new(im_str!("bar"))
+        Window::new(im_str!("pattern"))
             .size([300.0, 100.0], Condition::FirstUseEver)
             .build(ui, || {
 
-                for i in 0..active_bar.length() {
-                    ui.same_line(0.0);
+                // for i in 0..active_pattern.length() {
+                //     ui.same_line(0.0);
                     
-                    let token = match active_sound == i {
-                        true => ui.push_style_color(StyleColor::Button, [1.0, 0.0, 0.0, 1.0]),
-                        false => ui.push_style_color(StyleColor::CheckMark, [0.0, 0.0, 0.0, 1.0])
-                    };
-                    if ui.small_button(&im_str!("{}", i)) {
-                        active_sound = i as usize;
+                //     let token = match pattern_col == i {
+                //         true => ui.push_style_color(StyleColor::Button, [1.0, 0.0, 0.0, 1.0]),
+                //         false => ui.push_style_color(StyleColor::CheckMark, [0.0, 0.0, 0.0, 1.0])
+                //     };
+                //     if ui.small_button(&im_str!("{}", i)) {
+                //         pattern_col = i as usize;
+                //     }
+                //     token.pop(ui);
+
+                // }
+
+                for (row_idx, row) in (&active_pattern).sounds.iter().enumerate() {
+                    ui.text(im_str!("row {}", row_idx));
+                    for (snd_idx, sound) in row.iter().enumerate() {
+                        ui.same_line(0.0);
+                        // ui.text(im_str!("snd {}", snd_idx));
+
+                        let label = match sound {
+                            Some(_s) => "X",
+                            None => " "
+                        };
+
+                        if ui.small_button(&im_str!("{} {}x{}", label, row_idx, snd_idx)) {
+                            pattern_col = snd_idx;
+                            pattern_row = row_idx;
+                            // pattern_col = i as usize;
+                        }
                     }
-                    token.pop(ui);
 
                 }
-                if ui.small_button(&im_str!("extend")) {
-                    active_bar.extend(1);
+
+                ui.text(im_str!("pat {} {}", pattern_col, pattern_row));
+
+
+                if ui.small_button(&im_str!("column++")) {
+                    active_pattern.extend_length(1);
+                }
+                
+                if ui.small_button(&im_str!("row++")) {
+                    active_pattern.extend_row(1);
                 }
 
                 
                 if ui.small_button(&im_str!("ply")) {
-                    active_bar.play(&dev);
+                    active_pattern.play(&dev);
                 }
             });
     
             Window::new(im_str!("sound"))
             .size([300.0, 100.0], Condition::FirstUseEver)
             .build(ui, || {
-                let snd = &active_bar.sounds[active_sound];
-                if let Some(s) = snd{
+                // let snd = &active_bar.sounds[pattern_col];
+                if let Some(s) = &active_sound {
                     ui.text(im_str!("name   {}", s.name));
                     ui.text(im_str!("roll   {}", s.roll));
                     ui.text(im_str!("volume {}", s.volume));
@@ -125,7 +159,7 @@ fn main() {
             .build(ui, || {
                
                 if ui.small_button(&im_str!("add")) {
-                    active_bar.play(&dev);
+                    active_pattern.play(&dev);
                 }
             });