Quellcode durchsuchen

move to hashmap

woelper vor 4 Jahren
Ursprung
Commit
8eb5689fd0

BIN
media/cc0/bass/bass_1.wav


BIN
media/cc0/bass/bass_g_housy.wav


+ 0 - 0
media/cc0/bass/bass_g_reese.wav


BIN
media/cc0/bass/loop_reese-bass-2.wav


+ 0 - 232
patterns/amen1.pat

@@ -1,232 +0,0 @@
-{
-  "name": "amen1",
-  "repeat": 2,
-  "bpm": 624,
-  "sounds": [
-    [
-      {
-        "name": "kick_rough.wav",
-        "location": "media\\cc0\\kick_rough.wav",
-        "volume": 1.0,
-        "roll": 0,
-        "rollrate": 0,
-        "trim": 0.0,
-        "active": true,
-        "speed": 1.0
-      },
-      null,
-      {
-        "name": "kick_rough.wav",
-        "location": "media\\cc0\\kick_rough.wav",
-        "volume": 1.0,
-        "roll": 0,
-        "rollrate": 0,
-        "trim": 0.0,
-        "active": true,
-        "speed": 1.0
-      },
-      null,
-      null,
-      null,
-      null,
-      null,
-      null,
-      null,
-      {
-        "name": "kick_rough.wav",
-        "location": "media\\cc0\\kick_rough.wav",
-        "volume": 1.0,
-        "roll": 0,
-        "rollrate": 0,
-        "trim": 0.0,
-        "active": true,
-        "speed": 1.0
-      },
-      {
-        "name": "kick_rough.wav",
-        "location": "media\\cc0\\kick_rough.wav",
-        "volume": 1.0,
-        "roll": 0,
-        "rollrate": 0,
-        "trim": 0.0,
-        "active": true,
-        "speed": 1.0
-      },
-      null,
-      null,
-      null,
-      null
-    ],
-    [
-      null,
-      null,
-      null,
-      null,
-      {
-        "name": "snare_jungle.wav",
-        "location": "media\\cc0\\snare_jungle.wav",
-        "volume": 1.0,
-        "roll": 0,
-        "rollrate": 0,
-        "trim": 0.0,
-        "active": true,
-        "speed": 1.0
-      },
-      null,
-      null,
-      {
-        "name": "snare_jungle.wav",
-        "location": "media\\cc0\\snare_jungle.wav",
-        "volume": 1.0,
-        "roll": 0,
-        "rollrate": 0,
-        "trim": 0.0,
-        "active": true,
-        "speed": 1.0
-      },
-      null,
-      {
-        "name": "snare_jungle.wav",
-        "location": "media\\cc0\\snare_jungle.wav",
-        "volume": 1.0,
-        "roll": 0,
-        "rollrate": 0,
-        "trim": 0.0,
-        "active": true,
-        "speed": 1.0
-      },
-      null,
-      null,
-      {
-        "name": "snare_jungle.wav",
-        "location": "media\\cc0\\snare_jungle.wav",
-        "volume": 1.0,
-        "roll": 0,
-        "rollrate": 0,
-        "trim": 0.0,
-        "active": true,
-        "speed": 1.0
-      },
-      null,
-      null,
-      {
-        "name": "snare_jungle.wav",
-        "location": "media\\cc0\\snare_jungle.wav",
-        "volume": 1.0,
-        "roll": 0,
-        "rollrate": 0,
-        "trim": 0.0,
-        "active": true,
-        "speed": 1.0
-      }
-    ],
-    [
-      {
-        "name": "ride_bell.wav",
-        "location": "media\\cc0\\ride_bell.wav",
-        "volume": 1.0,
-        "roll": 0,
-        "rollrate": 0,
-        "trim": 0.0,
-        "active": true,
-        "speed": 1.0
-      },
-      null,
-      {
-        "name": "ride_bell.wav",
-        "location": "media\\cc0\\ride_bell.wav",
-        "volume": 1.0,
-        "roll": 0,
-        "rollrate": 0,
-        "trim": 0.0,
-        "active": true,
-        "speed": 1.0
-      },
-      null,
-      {
-        "name": "ride_bell.wav",
-        "location": "media\\cc0\\ride_bell.wav",
-        "volume": 1.0,
-        "roll": 0,
-        "rollrate": 0,
-        "trim": 0.0,
-        "active": true,
-        "speed": 1.0
-      },
-      null,
-      {
-        "name": "ride_bell.wav",
-        "location": "media\\cc0\\ride_bell.wav",
-        "volume": 1.0,
-        "roll": 0,
-        "rollrate": 0,
-        "trim": 0.0,
-        "active": true,
-        "speed": 1.0
-      },
-      null,
-      {
-        "name": "ride_bell.wav",
-        "location": "media\\cc0\\ride_bell.wav",
-        "volume": 1.0,
-        "roll": 0,
-        "rollrate": 0,
-        "trim": 0.0,
-        "active": true,
-        "speed": 1.0
-      },
-      null,
-      null,
-      null,
-      {
-        "name": "ride_bell.wav",
-        "location": "media\\cc0\\ride_bell.wav",
-        "volume": 1.0,
-        "roll": 0,
-        "rollrate": 0,
-        "trim": 0.0,
-        "active": true,
-        "speed": 1.0
-      },
-      null,
-      {
-        "name": "ride_bell.wav",
-        "location": "media\\cc0\\ride_bell.wav",
-        "volume": 1.0,
-        "roll": 0,
-        "rollrate": 0,
-        "trim": 0.0,
-        "active": true,
-        "speed": 1.0
-      },
-      null
-    ],
-    [
-      null,
-      null,
-      null,
-      null,
-      null,
-      null,
-      null,
-      null,
-      null,
-      null,
-      {
-        "name": "hihiat_real.wav",
-        "location": "media\\cc0\\hihiat_real.wav",
-        "volume": 1.0,
-        "roll": 0,
-        "rollrate": 0,
-        "trim": 0.0,
-        "active": true,
-        "speed": 1.0
-      },
-      null,
-      null,
-      null,
-      null,
-      null
-    ]
-  ]
-}

+ 9 - 9
sampler.ini

@@ -9,8 +9,8 @@ Size=300,100
 Collapsed=0
 
 [Window][sources]
-Pos=820,203
-Size=329,498
+Pos=400,140
+Size=182,665
 Collapsed=0
 
 [Window][ImGui Demo]
@@ -29,22 +29,22 @@ Size=328,168
 Collapsed=0
 
 [Window][sound]
-Pos=38,33
-Size=290,168
+Pos=13,6
+Size=290,149
 Collapsed=0
 
 [Window][tracks]
-Pos=84,461
+Pos=70,525
 Size=300,100
-Collapsed=0
+Collapsed=1
 
 [Window][pattern]
-Pos=28,192
-Size=707,222
+Pos=17,161
+Size=684,283
 Collapsed=0
 
 [Window][patterns]
-Pos=915,12
+Pos=400,140
 Size=324,186
 Collapsed=0
 

+ 42 - 42
src/base.rs

@@ -10,6 +10,7 @@ use crate::rodio::Source;
 
 use std::io::BufWriter;
 use std::io::BufReader;
+use std::collections::HashMap;
 
 // pub fn collect
 
@@ -74,8 +75,7 @@ impl Sound {
         Decoder::new(self.cursor()).unwrap()
     }
 
-    pub fn play(&mut self, device: &rodio::Device) {
-        self.active = true;
+    pub fn play(&self, device: &rodio::Device) {
         // let s = self.decoder().convert_samples()
         rodio::play_raw(&device, self.decoder()
         .speed(self.speed)
@@ -91,7 +91,9 @@ pub struct Pattern {
     pub name: String,
     pub repeat: i32,
     pub bpm: i32,
-    pub sounds: Vec<Vec<Option<Sound>>>
+    pub xsize: usize,
+    pub ysize: usize,
+    pub sounds: HashMap<(usize, usize), Sound>,
 }
 
 impl Default for Pattern {
@@ -100,18 +102,21 @@ impl Default for Pattern {
             name: "Unnamed".to_string(),
             repeat: 0,
             bpm: 120,
-            sounds: vec![vec![None; 8]]
+            xsize: 8,
+            ysize: 1,
+            sounds: HashMap::new()
         }
     }
 }
 
 impl Pattern {
-    pub fn play_mut(&mut self, device: &rodio::Device) {
+    pub fn play(&self, device: &rodio::Device) {
         let delay = ((60.0 / self.bpm as f32)*1000.0) as u64;
+        // loop for repeats
         for _ in 0..self.repeat + 1 {
-            for step in 0..self.length() {
-                for row in &mut self.sounds {
-                    if let Some(s) = &mut row[step] {
+            for x in 0..self.xsize {
+                for y in 0..self.ysize {
+                    if let Some(s) = self.sounds.get(&(x, y)) {
                         s.play(device);
                     }
                 }
@@ -120,44 +125,43 @@ 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 row in &mut self.sounds {
-
-                for sound in row {
-                    if let Some(s) = sound {
-                        s.play(device);
-                    }
-                    thread::sleep(Duration::from_millis(delay));
-                }
-                
-            }
-
-        }
-    }
-
     pub fn load(file: &Path) -> Result<Pattern, String> {
         match File::open(file) {
             Ok(f) => {
                 serde_json::from_reader(BufReader::new(f))
                     .map_err(|e| e.to_string())
-                    .map(|p: Pattern| p.reload_sounds( ))
+                    // .map(|p: Pattern| p.reload_sounds( ))
             }
             Err(e) => Err(e.to_string())
         }
     }
+    pub fn replace_sounds(&mut self, new_sound: &Sound, row: usize) {
+        for x in 0..self.xsize {
+            if let Some(snd) = self.sounds.get_mut(&(x, row)) {
+                snd.location = new_sound.location.clone();
+                snd.data = new_sound.data.clone();
+            }
+        
+        }
 
-    pub fn reload_sounds(&self) -> Pattern {
+    }
+    pub fn clear_sounds(&mut self, row: usize) {
+        for x in 0..self.xsize {
+            if let Some(_) = self.sounds.get(&(x, row)) {
+                self.sounds.remove(&(x, row));
+            }
+        }
+    }
+
+    pub fn reload_sounds(&mut self) -> Pattern {
         let mut newp = self.clone();
-        for row in &mut newp.sounds {
-            for col in row {
-                if let Some(snd) = col {
-                    dbg!(&snd);
-                    let s = snd.reload_data().unwrap();
-                    snd.data = s.data.clone();
+        for x in 0..self.xsize {
+            for y in 0..self.ysize {
+                if let Some(snd) = self.sounds.get_mut(&(x,y)) {
+                    // dbg!(&snd);
+                    snd.reload_data().unwrap();
+                    // let s = snd.reload_data().unwrap();
+                    // snd.data = s.data.clone();
                 }
             }
         }
@@ -172,18 +176,14 @@ impl Pattern {
     }
 
     pub fn extend_length(&mut self, slots: usize) {
-        for row in &mut self.sounds {
-            row.resize(row.len() + slots, None);
-        }
+        self.xsize = self.xsize+slots;
     }
 
     pub fn extend_row(&mut self, rows: usize) {
-        self.sounds.resize(self.sounds.len() + rows, vec![None; self.sounds[0].len()]);
+        self.ysize = self.ysize+rows;
     }
 
-    pub fn length(&self) -> usize {
-        self.sounds[0].len()
-    }
+
 }
 
 #[derive(Clone, Debug)]

+ 101 - 64
src/main.rs

@@ -31,10 +31,26 @@ fn collect_patterns (root: &str) -> Vec<Pattern> {
     .filter_map(|e| e.ok())
     .filter(|e| e.path().is_file())
     .map(|f| Pattern::load(f.path()))
+    .map(|p| dbg!(p))
     .filter_map(|e| e.ok())
     .collect()
 }
 
+
+// fn collect_patterns (root: &str) -> Vec<Pattern> {
+//     let mut pat = vec![];
+    
+//     for f in WalkDir::new(root)
+//     .into_iter()
+//     .filter_map(|e| e.ok())
+//     .filter(|e| e.path().is_file())
+//     {
+//         let x = 
+//     }    
+//     pat
+// }
+
+
 fn main() {
     env_logger::init();
     let dev = rodio::default_output_device().unwrap();
@@ -43,28 +59,47 @@ fn main() {
     let mut patterns = collect_patterns("patterns");
     
     let mut timeline = Timeline::default();
-
-    // return;
+    
     let mut pattern_col: usize = 0;
     let mut pattern_row: usize = 0;
-    let mut active_sound_idx = 0;
-
-
-    // let mut active_bar = Bar::default();
-    // 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 system = support_ogl::init(file!());
-    let s = system.imgui.style_mut();
-    s.window_rounding = 1.5;
-    // let system = support_glium::init(file!());
+    let style = system.imgui.style_mut();
+    style.window_rounding = 1.5;
+    style.anti_aliased_lines = false;
+    style.anti_aliased_fill = false;
+    // ui.push_style_color(
+    // style.colors[StyleColor::Text]                   = ImVec4(1.00, 1.00, 1.00, 1.00);
+    // // style.colors[ImGuiCol_WindowBg]               = ImVec4(0.03f, 0.03f, 0.03f, 1.00f);
+    // // style.colors[ImGuiCol_FrameBg]                = ImVec4(0.07f, 0.05f, 0.27f, 1.00f);
+    // // style.colors[ImGuiCol_FrameBgHovered]         = ImVec4(0.11f, 0.08f, 0.44f, 1.00f);
+    // // style.colors[ImGuiCol_FrameBgActive]          = ImVec4(0.07f, 0.05f, 0.27f, 1.00f);
+    // // style.colors[ImGuiCol_TitleBg]                = ImVec4(0.11f, 0.11f, 0.11f, 1.00f);
+    // // style.colors[ImGuiCol_TitleBgActive]          = ImVec4(0.14f, 0.05f, 0.96f, 1.00f);
+    // // style.colors[ImGuiCol_ScrollbarBg]            = ImVec4(0.03f, 0.03f, 0.03f, 1.00f);
+    // // style.colors[ImGuiCol_CheckMark]              = ImVec4(0.07f, 0.05f, 0.27f, 1.00f);
+    // // style.colors[ImGuiCol_SliderGrab]             = ImVec4(0.07f, 0.05f, 0.27f, 1.00f);
+    // // style.colors[ImGuiCol_SliderGrabActive]       = ImVec4(0.07f, 0.05f, 0.27f, 1.00f);
+    // // style.colors[ImGuiCol_Button]                 = ImVec4(0.07f, 0.05f, 0.27f, 1.00f);
+    // // style.colors[ImGuiCol_ButtonHovered]          = ImVec4(0.18f, 0.12f, 0.78f, 1.00f);
+    // // style.colors[ImGuiCol_ButtonActive]           = ImVec4(0.20f, 0.13f, 0.88f, 1.00f);
+    // // style.colors[ImGuiCol_Header]                 = ImVec4(0.07f, 0.05f, 0.27f, 1.00f);
+    // // style.colors[ImGuiCol_SeparatorHovered]       = ImVec4(0.13f, 0.07f, 0.78f, 1.00f);
+    // // style.colors[ImGuiCol_SeparatorActive]        = ImVec4(0.07f, 0.05f, 0.27f, 1.00f);
+    // // style.colors[ImGuiCol_ResizeGrip]             = ImVec4(0.07f, 0.05f, 0.27f, 1.00f);
+    // // style.colors[ImGuiCol_ResizeGripHovered]      = ImVec4(0.19f, 0.12f, 0.85f, 1.00f);
+    // // style.colors[ImGuiCol_ResizeGripActive]       = ImVec4(0.07f, 0.05f, 0.27f, 1.00f);
+    // // style.colors[ImGuiCol_Tab]                    = ImVec4(0.07f, 0.05f, 0.27f, 1.00f);
+    // // style.colors[ImGuiCol_TabHovered]             = ImVec4(0.15f, 0.05f, 0.27f, 1.00f);
+    // // style.colors[ImGuiCol_TabActive]              = ImVec4(0.21f, 0.14f, 0.91f, 1.00f);
+    // // style.colors.
 
     system.main_loop(move |_, ui| {
 
 
-
         Window::new(im_str!("sources"))
             // .resizable(false)
             .position([400.0, 140.0], Condition::Appearing)
@@ -81,9 +116,9 @@ 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 pattern")) {
-                                active_pattern.sounds[pattern_row][pattern_col] = Some(s.clone());
-                            }
+                            // if ui.small_button(im_str!("add to pattern")) {
+                            //     active_pattern.sounds[pattern_row][pattern_col] = Some(s.clone());
+                            // }
                             if ui.small_button(im_str!("load")) {
                                 active_sound = Some(s.clone());
                             }
@@ -125,35 +160,30 @@ fn main() {
             .size([300.0, 100.0], Condition::FirstUseEver)
             .build(ui, || {
 
-                if ui.small_button(&im_str!("column++")) {
-                    active_pattern.extend_length(1);
-                }
+                if ui.small_button(&im_str!("increase length")) {active_pattern.extend_length(1);}
                 ui.same_line(0.0);
-                
-                if ui.small_button(&im_str!("row++")) {
-                    active_pattern.extend_row(1);
-                }
+                if ui.small_button(&im_str!("add trk")) {active_pattern.extend_row(1);}
+                ui.same_line(0.0);
+                if ui.small_button(&im_str!("save")) {active_pattern.save();}
+                ui.same_line(0.0);
+                if ui.small_button(&im_str!("load")) {}
+                // if ui.small_button(&im_str!("theme")) {
+                //     let col: Vec<[f32; 4]> = style.clone().colors.iter().map(|x| x.clone()).collect();
+                //     let s = Theme {colors: col};
+                //     dbg!(&s);
 
-                // if ui.input_text(&im_str!("name"), &mut active_pattern_name).build() {
-                //     dbg!(&active_pattern_name);
                 // }
 
-
+    
                 let mut imstr_name = ImString::from(active_pattern.name.clone());
-                if ui.input_text(&im_str!("yo"), &mut imstr_name)
+                if ui.input_text(&im_str!("name"), &mut imstr_name)
                     .resize_buffer(true)
+                    .auto_select_all(true)
                     .build() {
-                    // dbg!(&active_pattern_name);
                     active_pattern.name = imstr_name.to_string();
                 };
-
-                if ui.small_button(&im_str!("save")) {active_pattern.save();}
-                if ui.small_button(&im_str!("load")) {}
-
-                ui.same_line(0.0);
-                if ui.small_button(&im_str!("play")) {
-                    active_pattern.play_mut(&dev);
-                }
+                
+                let button_size: f32 = 16.0;
 
                 ui.drag_int(im_str!("repeat"), &mut active_pattern.repeat)
                     .max(32)
@@ -162,44 +192,51 @@ fn main() {
                 
                 ui.drag_int(im_str!("bpm"), &mut active_pattern.bpm).build();
 
-                for row_idx in 0..active_pattern.sounds.len() {
-                    ui.text(im_str!("row {}", row_idx));
-
-                    for snd_idx in 0..active_pattern.sounds[row_idx].len() {
-                        ui.same_line(0.0);
-                        // ui.text(im_str!("snd {}", snd_idx));
-
-                        let mut active = false;
-                        if let Some(snd) = &active_pattern.sounds[row_idx][snd_idx] {
-                            if snd.active {
-                                active = true;
-                            }
-                        }
-
-                        // let style = ui.push_style_color(StyleColor::Button, [0.0, 1.0, 0.1, 1.0]);
-                        // style.pop(ui);
-                  
+                ui.text(im_str!("loc   "));
+                for x in 0..active_pattern.xsize {
+                    ui.same_line(0.0);
+                    ui.button(&im_str!("{}", x+1), [button_size, button_size]);
+                }
+                
 
-                        let mut label = match &active_pattern.sounds[row_idx][snd_idx] {
+                for y in 0..active_pattern.ysize {
+                    ui.text(im_str!("row {:02}", y));
+                    for x in 0..active_pattern.xsize {
+                        let label = match &active_pattern.sounds.get(&(x,y)) {
                             Some(_s) => "X",
                             None => " "
                         };
-
-                        if active {
-                            label = "*";
+                        
+                        ui.same_line(0.0);
+                        if ui.button(&im_str!("{}##{}{}", label, y, x), [button_size, button_size]) {
+                            pattern_col = x;
+                            pattern_row = y;
+                            match &active_sound {
+                                Some(snd) => {active_pattern.sounds.insert((x,y),  snd.clone());},
+                                None => ()
+                            }
                         }
-
-                        if ui.small_button(&im_str!("{}##{}{}", label, row_idx, snd_idx)) {
-                            pattern_col = snd_idx;
-                            pattern_row = row_idx;
-                            println!("r{}c{}", row_idx, snd_idx);
-                            active_pattern.sounds[row_idx][snd_idx] = active_sound.clone();
+                        
+                        
+                    }
+                    
+                    ui.same_line(0.0);
+                    if ui.small_button(im_str!("replace src") ){
+                        if let Some(acs) = &active_sound {
+                            active_pattern.replace_sounds(acs, y);
                         }
                     }
+                    ui.same_line(0.0);
+                    if ui.small_button(im_str!("clr") ){
+                        active_pattern.clear_sounds(y);
+                    }
+
                 }
 
                 ui.text(im_str!("pat {} {}", pattern_col, pattern_row));
-
+                if ui.small_button(&im_str!("play")) {
+                    active_pattern.play(&dev);
+                }
 
         
             });
@@ -242,7 +279,7 @@ fn main() {
             .build(ui, || {
                
                 if ui.small_button(&im_str!("add")) {
-                    active_pattern.play_mut(&dev);
+                    active_pattern.play(&dev);
                 }
             });