Browse Source

sanitize pathbufs from win

Johann Woelper 4 years ago
parent
commit
b1821b20e2
4 changed files with 58 additions and 35 deletions
  1. 42 0
      patterns/test.pat
  2. 4 4
      sampler.ini
  3. 11 21
      src/base.rs
  4. 1 10
      src/main.rs

+ 42 - 0
patterns/test.pat

@@ -0,0 +1,42 @@
+{
+  "name": "test",
+  "repeat": 0,
+  "bpm": 120,
+  "xsize": 9,
+  "ysize": 1,
+  "sounds": {
+    "2,0": {
+      "name": "snare_punchy.wav",
+      "location": "media/cc0/snare_punchy.wav",
+      "volume": 1.0,
+      "roll": 0,
+      "rollrate": 0,
+      "trim": 0.0,
+      "active": false,
+      "speed": 1.0,
+      "reverse": false
+    },
+    "4,0": {
+      "name": "snare_punchy.wav",
+      "location": "media/cc0/snare_punchy.wav",
+      "volume": 1.0,
+      "roll": 0,
+      "rollrate": 0,
+      "trim": 0.0,
+      "active": false,
+      "speed": 1.0,
+      "reverse": false
+    },
+    "0,0": {
+      "name": "snare_punchy.wav",
+      "location": "media/cc0/snare_punchy.wav",
+      "volume": 1.0,
+      "roll": 0,
+      "rollrate": 0,
+      "trim": 0.0,
+      "active": false,
+      "speed": 1.0,
+      "reverse": false
+    }
+  }
+}

+ 4 - 4
sampler.ini

@@ -34,22 +34,22 @@ Size=354,269
 Collapsed=0
 
 [Window][tracks]
-Pos=740,548
+Pos=1022,409
 Size=300,160
 Collapsed=0
 
 [Window][pattern]
-Pos=609,442
+Pos=600,545
 Size=684,283
 Collapsed=0
 
 [Window][patterns]
 Pos=400,140
 Size=324,183
-Collapsed=1
+Collapsed=0
 
 [Window][settings]
-Pos=740,236
+Pos=912,169
 Size=300,160
 Collapsed=0
 

+ 11 - 21
src/base.rs

@@ -11,12 +11,11 @@ use crate::rodio::Source;
 use std::io::BufWriter;
 use std::io::BufReader;
 use std::collections::HashMap;
-use serde::{Serialize, Serializer};
+use serde::{Serializer};
 use crate::serde::ser::SerializeMap;
 // pub fn collect
 
 
-
 #[derive(Serialize, Deserialize, Clone, Default, Debug)]
 pub struct Sound {
     pub name: String,
@@ -35,7 +34,6 @@ pub struct Sound {
     pub reverse: bool
 }
 
-
 impl AsRef<[u8]> for Sound {
     fn as_ref(&self) -> &[u8] {
         &self.data
@@ -43,18 +41,19 @@ impl AsRef<[u8]> for Sound {
 }
 
 impl Sound {
-    pub fn new(filename: &str) -> std::io::Result<Sound> {
-        let path = PathBuf::from(filename);
+    pub fn new(filename: &PathBuf) -> std::io::Result<Sound> {
         let mut buf = Vec::new();
-        let mut file = File::open(filename)?;
+        let sanitized_name = PathBuf::from(filename.to_string_lossy().replace("\\", "/"));
+
+        let mut file = File::open(&sanitized_name)?;
         file.read_to_end(&mut buf)?;
         let mut rev_buf = buf.clone();
         rev_buf.reverse();
-        Ok(Sound{
+        Ok(Sound {
             data: Arc::new(buf),
             data_rev: Arc::new(rev_buf),
-            name: path.file_name().unwrap_or_default().to_string_lossy().to_string(),
-            location: path,
+            name: sanitized_name.clone().file_name().unwrap_or_default().to_string_lossy().to_string(),
+            location: sanitized_name.clone(),
             volume: 1.0,
             speed: 1.0,
             ..Default::default()
@@ -67,8 +66,8 @@ impl Sound {
     //     }
     // }
 
-    pub fn updated_drom_source(&self) -> Result<Sound, std::io::Error>{
-        Sound::new(&self.location.to_string_lossy())
+    pub fn updated_from_source(&self) -> Result<Sound, std::io::Error>{
+        Sound::new(&self.location)
     }
 
     pub fn cursor(self: &Self) -> Cursor<Sound> {
@@ -110,20 +109,11 @@ impl Sound {
                 dbg!(e);
             }
         }
-
-
     }
 }
 
 
 
-
-
-
-
-
-
-
 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()))
@@ -280,7 +270,7 @@ impl Pattern {
             for y in 0..self.ysize {
                 if let Some(snd) = self.sounds.get_mut(&(x,y)) {
                     // println!("RELOAD {:?}", &snd.name);
-                    match snd.updated_drom_source() {
+                    match snd.updated_from_source() {
                         Ok(newdata) => snd.data = newdata.data,
                         Err(e) => println!("ERROR LOADING SND {:?} {:?}", e, snd.location)
                     }

+ 1 - 10
src/main.rs

@@ -19,12 +19,11 @@ fn collect_sounds (root: &str) -> Vec<Sound> {
     .into_iter()
     .filter_map(|e| e.ok())
     .filter(|e| e.path().is_file())
-    .map(|f| Sound::new(&f.path().to_string_lossy().to_string()))
+    .map(|f| Sound::new(&f.path().to_path_buf()))
     .filter_map(|e| e.ok())
     .collect()
 }
 
-
 fn collect_patterns (root: &str) -> Vec<Pattern> {
     WalkDir::new(root)
     .into_iter()
@@ -36,19 +35,11 @@ fn collect_patterns (root: &str) -> Vec<Pattern> {
     .collect()
 }
 
-
-
 fn main() {
 
-
     env_logger::init();
     let dev = rodio::default_output_device().unwrap();
     
-let devs = rodio::output_devices();
-    for d in devs {
-        dbg!("dev");
-    }
-
     let mut sounds = collect_sounds("media");
     let mut patterns = collect_patterns("patterns");