Przeglądaj źródła

fix bug in clearing

woelper 4 lat temu
rodzic
commit
308f1cb726
5 zmienionych plików z 363 dodań i 4 usunięć
  1. 269 0
      patterns/funky drummer.pat
  2. 29 0
      patterns/polka.pat
  3. 1 1
      sampler.ini
  4. 58 1
      src/base.rs
  5. 6 2
      src/main.rs

+ 269 - 0
patterns/funky drummer.pat

@@ -0,0 +1,269 @@
+{
+  "name": "funky drummer",
+  "repeat": 0,
+  "bpm": 416,
+  "xsize": 16,
+  "ysize": 3,
+  "sounds": {
+    "11,2": {
+      "name": "hihiat_closed.wav",
+      "location": "media\\cc0\\hihiat_closed.wav",
+      "volume": 1.0,
+      "roll": 0,
+      "rollrate": 0,
+      "trim": 0.0,
+      "active": false,
+      "speed": 1.0
+    },
+    "2,2": {
+      "name": "hihiat_closed.wav",
+      "location": "media\\cc0\\hihiat_closed.wav",
+      "volume": 1.0,
+      "roll": 0,
+      "rollrate": 0,
+      "trim": 0.0,
+      "active": false,
+      "speed": 1.0
+    },
+    "10,2": {
+      "name": "hihiat_closed.wav",
+      "location": "media\\cc0\\hihiat_closed.wav",
+      "volume": 1.0,
+      "roll": 0,
+      "rollrate": 0,
+      "trim": 0.0,
+      "active": false,
+      "speed": 1.0
+    },
+    "4,1": {
+      "name": "snare_jungle.wav",
+      "location": "media\\cc0\\snare_jungle.wav",
+      "volume": 1.0,
+      "roll": 0,
+      "rollrate": 0,
+      "trim": 0.0,
+      "active": false,
+      "speed": 1.0
+    },
+    "0,0": {
+      "name": "kick_edm.wav",
+      "location": "media\\cc0\\kick_edm.wav",
+      "volume": 1.0,
+      "roll": 0,
+      "rollrate": 0,
+      "trim": 0.0,
+      "active": false,
+      "speed": 1.0
+    },
+    "6,0": {
+      "name": "kick_edm.wav",
+      "location": "media\\cc0\\kick_edm.wav",
+      "volume": 1.0,
+      "roll": 0,
+      "rollrate": 0,
+      "trim": 0.0,
+      "active": false,
+      "speed": 1.0
+    },
+    "15,1": {
+      "name": "snare_jungle.wav",
+      "location": "media\\cc0\\snare_jungle.wav",
+      "volume": 1.0,
+      "roll": 0,
+      "rollrate": 0,
+      "trim": 0.0,
+      "active": false,
+      "speed": 1.0
+    },
+    "3,2": {
+      "name": "hihiat_closed.wav",
+      "location": "media\\cc0\\hihiat_closed.wav",
+      "volume": 1.0,
+      "roll": 0,
+      "rollrate": 0,
+      "trim": 0.0,
+      "active": false,
+      "speed": 1.0
+    },
+    "0,2": {
+      "name": "hihiat_closed.wav",
+      "location": "media\\cc0\\hihiat_closed.wav",
+      "volume": 1.0,
+      "roll": 0,
+      "rollrate": 0,
+      "trim": 0.0,
+      "active": false,
+      "speed": 1.0
+    },
+    "7,2": {
+      "name": "hihiat_closed.wav",
+      "location": "media\\cc0\\hihiat_closed.wav",
+      "volume": 1.0,
+      "roll": 0,
+      "rollrate": 0,
+      "trim": 0.0,
+      "active": false,
+      "speed": 1.0
+    },
+    "9,2": {
+      "name": "hihiat_closed.wav",
+      "location": "media\\cc0\\hihiat_closed.wav",
+      "volume": 1.0,
+      "roll": 0,
+      "rollrate": 0,
+      "trim": 0.0,
+      "active": false,
+      "speed": 1.0
+    },
+    "6,2": {
+      "name": "hihiat_closed.wav",
+      "location": "media\\cc0\\hihiat_closed.wav",
+      "volume": 1.0,
+      "roll": 0,
+      "rollrate": 0,
+      "trim": 0.0,
+      "active": false,
+      "speed": 1.0
+    },
+    "1,2": {
+      "name": "hihiat_closed.wav",
+      "location": "media\\cc0\\hihiat_closed.wav",
+      "volume": 1.0,
+      "roll": 0,
+      "rollrate": 0,
+      "trim": 0.0,
+      "active": false,
+      "speed": 1.0
+    },
+    "7,1": {
+      "name": "snare_jungle.wav",
+      "location": "media\\cc0\\snare_jungle.wav",
+      "volume": 1.0,
+      "roll": 0,
+      "rollrate": 0,
+      "trim": 0.0,
+      "active": false,
+      "speed": 1.0
+    },
+    "12,2": {
+      "name": "hihiat_closed.wav",
+      "location": "media\\cc0\\hihiat_closed.wav",
+      "volume": 1.0,
+      "roll": 0,
+      "rollrate": 0,
+      "trim": 0.0,
+      "active": false,
+      "speed": 1.0
+    },
+    "13,0": {
+      "name": "kick_edm.wav",
+      "location": "media\\cc0\\kick_edm.wav",
+      "volume": 1.0,
+      "roll": 0,
+      "rollrate": 0,
+      "trim": 0.0,
+      "active": false,
+      "speed": 1.0
+    },
+    "12,1": {
+      "name": "snare_jungle.wav",
+      "location": "media\\cc0\\snare_jungle.wav",
+      "volume": 1.0,
+      "roll": 0,
+      "rollrate": 0,
+      "trim": 0.0,
+      "active": false,
+      "speed": 1.0
+    },
+    "2,0": {
+      "name": "kick_edm.wav",
+      "location": "media\\cc0\\kick_edm.wav",
+      "volume": 1.0,
+      "roll": 0,
+      "rollrate": 0,
+      "trim": 0.0,
+      "active": false,
+      "speed": 1.0
+    },
+    "5,2": {
+      "name": "hihiat_closed.wav",
+      "location": "media\\cc0\\hihiat_closed.wav",
+      "volume": 1.0,
+      "roll": 0,
+      "rollrate": 0,
+      "trim": 0.0,
+      "active": false,
+      "speed": 1.0
+    },
+    "9,1": {
+      "name": "snare_jungle.wav",
+      "location": "media\\cc0\\snare_jungle.wav",
+      "volume": 1.0,
+      "roll": 0,
+      "rollrate": 0,
+      "trim": 0.0,
+      "active": false,
+      "speed": 1.0
+    },
+    "14,2": {
+      "name": "hihiat_closed.wav",
+      "location": "media\\cc0\\hihiat_closed.wav",
+      "volume": 1.0,
+      "roll": 0,
+      "rollrate": 0,
+      "trim": 0.0,
+      "active": false,
+      "speed": 1.0
+    },
+    "4,2": {
+      "name": "hihiat_closed.wav",
+      "location": "media\\cc0\\hihiat_closed.wav",
+      "volume": 1.0,
+      "roll": 0,
+      "rollrate": 0,
+      "trim": 0.0,
+      "active": false,
+      "speed": 1.0
+    },
+    "8,2": {
+      "name": "hihiat_closed.wav",
+      "location": "media\\cc0\\hihiat_closed.wav",
+      "volume": 1.0,
+      "roll": 0,
+      "rollrate": 0,
+      "trim": 0.0,
+      "active": false,
+      "speed": 1.0
+    },
+    "10,0": {
+      "name": "kick_edm.wav",
+      "location": "media\\cc0\\kick_edm.wav",
+      "volume": 1.0,
+      "roll": 0,
+      "rollrate": 0,
+      "trim": 0.0,
+      "active": false,
+      "speed": 1.0
+    },
+    "11,1": {
+      "name": "snare_jungle.wav",
+      "location": "media\\cc0\\snare_jungle.wav",
+      "volume": 1.0,
+      "roll": 0,
+      "rollrate": 0,
+      "trim": 0.0,
+      "active": false,
+      "speed": 1.0
+    },
+    "15,2": {
+      "name": "hihiat_closed.wav",
+      "location": "media\\cc0\\hihiat_closed.wav",
+      "volume": 1.0,
+      "roll": 0,
+      "rollrate": 0,
+      "trim": 0.0,
+      "active": false,
+      "speed": 1.0
+    }
+  }
+}

+ 29 - 0
patterns/polka.pat

@@ -0,0 +1,29 @@
+{
+  "name": "polka",
+  "repeat": 0,
+  "bpm": 120,
+  "xsize": 8,
+  "ysize": 1,
+  "sounds": {
+    "0,0": {
+      "name": "ride_crisp.wav",
+      "location": "media\\cc0\\ride_crisp.wav",
+      "volume": 1.0,
+      "roll": 0,
+      "rollrate": 0,
+      "trim": 0.0,
+      "active": false,
+      "speed": 1.0
+    },
+    "6,0": {
+      "name": "ride_crisp.wav",
+      "location": "media\\cc0\\ride_crisp.wav",
+      "volume": 1.0,
+      "roll": 0,
+      "rollrate": 0,
+      "trim": 0.0,
+      "active": false,
+      "speed": 1.0
+    }
+  }
+}

+ 1 - 1
sampler.ini

@@ -39,7 +39,7 @@ Size=300,100
 Collapsed=1
 
 [Window][pattern]
-Pos=17,161
+Pos=9,157
 Size=684,283
 Collapsed=0
 

+ 58 - 1
src/base.rs

@@ -11,7 +11,8 @@ use crate::rodio::Source;
 use std::io::BufWriter;
 use std::io::BufReader;
 use std::collections::HashMap;
-
+use serde::{Serialize, Serializer};
+use crate::serde::ser::SerializeMap;
 // pub fn collect
 
 
@@ -86,6 +87,56 @@ impl Sound {
     }
 }
 
+
+
+
+
+
+
+
+
+
+fn hm_key_to_string(snds: &HashMap<(usize, usize), Sound>)  ->  HashMap<String, Sound> {
+    snds.into_iter()
+        .map(|(k, v)| (format!("{},{}", k.0, k.1), v.clone()))
+        .collect()
+}
+
+fn hm_key_from_string(snds: &HashMap<String, Sound>)  ->  HashMap<(usize, usize), Sound> {
+    snds.into_iter()
+        .map(|(k, v)| {
+            //TODO: remove var and collect directly into tuple
+            let idx: Vec<usize> = k.split(",").map(|s| s.parse::<usize>().unwrap_or_default()).collect();
+                
+            ((idx[0],idx[1]), v.clone() )
+        
+        })
+        .collect()
+}
+
+fn ser_snds<S>(h: &HashMap<(usize, usize), Sound>, s: S) -> Result<S::Ok, S::Error>
+where
+    S: Serializer,
+{
+    // s.serialize_map(hm_key_to_string(h))
+    let mut map = s.serialize_map(Some(h.len()))?;
+    for (k, v) in hm_key_to_string(h) {
+        map.serialize_entry(&k, &v)?;
+    }
+    map.end()
+}
+
+fn de_snds<'de, D>(deserializer: D) -> Result<HashMap<(usize, usize), Sound>, D::Error>
+where
+    D: serde::de::Deserializer<'de>,
+{
+    // TODO err checking
+    let s: HashMap<String, Sound> = serde::de::Deserialize::deserialize(deserializer)?;
+    // serde_json::from_str(s).map_err(serde::de::Error::custom)
+    let x = hm_key_from_string(&s);
+    Ok(x)
+}
+
 #[derive(Serialize, Deserialize, Clone, Debug)]
 pub struct Pattern {
     pub name: String,
@@ -93,6 +144,7 @@ pub struct Pattern {
     pub bpm: i32,
     pub xsize: usize,
     pub ysize: usize,
+    #[serde(serialize_with="ser_snds", deserialize_with="de_snds")]
     pub sounds: HashMap<(usize, usize), Sound>,
 }
 
@@ -145,6 +197,7 @@ impl Pattern {
         }
 
     }
+
     pub fn clear_sounds(&mut self, row: usize) {
         for x in 0..self.xsize {
             if let Some(_) = self.sounds.get(&(x, row)) {
@@ -153,6 +206,10 @@ impl Pattern {
         }
     }
 
+    pub fn clear_all_sounds(&mut self) {
+        self.sounds.clear();
+    }
+
     pub fn reload_sounds(&mut self) -> Pattern {
         let mut newp = self.clone();
         for x in 0..self.xsize {

+ 6 - 2
src/main.rs

@@ -52,6 +52,9 @@ fn collect_patterns (root: &str) -> Vec<Pattern> {
 
 
 fn main() {
+
+    let index: Vec<usize> = "0,1".split(",").map(|s| s.parse::<usize>().unwrap_or_default()).collect();
+
     env_logger::init();
     let dev = rodio::default_output_device().unwrap();
     
@@ -221,13 +224,14 @@ fn main() {
                     }
                     
                     ui.same_line(0.0);
-                    if ui.small_button(im_str!("replace src") ){
+                    if ui.small_button(&im_str!("replace src##{}",y) ){
                         if let Some(acs) = &active_sound {
                             active_pattern.replace_sounds(acs, y);
                         }
                     }
                     ui.same_line(0.0);
-                    if ui.small_button(im_str!("clr") ){
+                    if ui.small_button(&im_str!("clr##{}", y) ){
+                        dbg!(&y);
                         active_pattern.clear_sounds(y);
                     }