woelper 4 lat temu
rodzic
commit
cb16799492
6 zmienionych plików z 116 dodań i 35 usunięć
  1. 43 0
      Cargo.lock
  2. 4 0
      Cargo.toml
  3. 4 4
      sampler.ini
  4. 30 19
      src/base.rs
  5. 34 12
      src/main.rs
  6. 1 0
      src/support_ogl/mod.rs

+ 43 - 0
Cargo.lock

@@ -695,6 +695,12 @@ dependencies = [
  "adler32",
 ]
 
+[[package]]
+name = "itoa"
+version = "0.4.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "b8b7a7c0c47db5545ed3fef7468ee7bb5b74691498139e4b3f6a20685dc6dd8e"
+
 [[package]]
 name = "jpeg-decoder"
 version = "0.1.18"
@@ -1199,6 +1205,12 @@ dependencies = [
  "stb_truetype",
 ]
 
+[[package]]
+name = "ryu"
+version = "1.0.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "bfa8506c1de11c9c4e4c38863ccbe02a305c8188e85a05a784c9e11e1c3910c8"
+
 [[package]]
 name = "same-file"
 version = "1.0.6"
@@ -1223,6 +1235,9 @@ dependencies = [
  "imgui-winit-support",
  "log",
  "rodio",
+ "serde",
+ "serde_derive",
+ "serde_json",
  "walkdir",
 ]
 
@@ -1253,6 +1268,34 @@ version = "0.7.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "388a1df253eca08550bef6c72392cfe7c30914bf41df5269b68cbd6ff8f570a3"
 
+[[package]]
+name = "serde"
+version = "1.0.104"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "414115f25f818d7dfccec8ee535d76949ae78584fc4f79a6f45a904bf8ab4449"
+
+[[package]]
+name = "serde_derive"
+version = "1.0.104"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "128f9e303a5a29922045a830221b8f78ec74a5f544944f3d5984f8ec3895ef64"
+dependencies = [
+ "proc-macro2 1.0.9",
+ "quote 1.0.2",
+ "syn",
+]
+
+[[package]]
+name = "serde_json"
+version = "1.0.48"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "9371ade75d4c2d6cb154141b9752cf3781ec9c05e0e5cf35060e1e70ee7b9c25"
+dependencies = [
+ "itoa",
+ "ryu",
+ "serde",
+]
+
 [[package]]
 name = "shared_library"
 version = "0.1.9"

+ 4 - 0
Cargo.toml

@@ -14,6 +14,10 @@ log = "*"
 env_logger = "*"
 walkdir = "*"
 image = "0.23"
+serde = "*"
+serde_derive = "*"
+serde_json = "*"
+
 
 # GFX / OPENGL
 gfx = "0.18"

+ 4 - 4
sampler.ini

@@ -9,8 +9,8 @@ Size=300,100
 Collapsed=0
 
 [Window][sources]
-Pos=504,92
-Size=246,460
+Pos=400,73
+Size=246,527
 Collapsed=0
 
 [Window][ImGui Demo]
@@ -29,7 +29,7 @@ Size=328,168
 Collapsed=0
 
 [Window][sound]
-Pos=33,28
+Pos=60,43
 Size=290,168
 Collapsed=0
 
@@ -39,7 +39,7 @@ Size=300,100
 Collapsed=0
 
 [Window][pattern]
-Pos=51,222
+Pos=39,237
 Size=523,173
 Collapsed=0
 

+ 30 - 19
src/base.rs

@@ -8,20 +8,25 @@ use std::sync::Arc;
 use rodio::Decoder;
 use crate::rodio::Source;
 
+use std::io::BufWriter;
+use std::io::BufReader;
 
 // pub fn collect
 
 
 
-#[derive(Clone, Default, Debug)]
+#[derive(Serialize, Deserialize, Clone, Default, Debug)]
 pub struct Sound {
     pub name: String,
     pub location: PathBuf,
+    #[serde(skip)]
     data: Arc<Vec<u8>>,
     pub volume: f32,
     pub roll: i32,
-    pub rate: i32,
-    pub active: bool
+    pub rollrate: i32,
+    pub trim: f32,
+    pub active: bool,
+    pub speed: f32
 }
 
 
@@ -32,7 +37,6 @@ impl AsRef<[u8]> for Sound {
 }
 
 impl Sound {
-
     pub fn new(filename: &str) -> std::io::Result<Sound> {
         let mut buf = Vec::new();
         let mut file = File::open(filename)?;
@@ -40,9 +44,18 @@ impl Sound {
         Ok(Sound{
             data: Arc::new(buf),
             name: filename.to_string(),
+            volume: 1.0,
+            speed: 1.0,
             ..Default::default()
         })
     }
+
+    pub fn reload(mut self) {
+        if let Ok(s) = Sound::new(&self.name) {
+            self.data = s.data;
+        }
+    }
+
     pub fn cursor(self: &Self) -> Cursor<Sound> {
         Cursor::new(
             Sound {
@@ -55,17 +68,21 @@ impl Sound {
         Decoder::new(self.cursor()).unwrap()
     }
 
-
     pub fn play(&mut self, device: &rodio::Device) {
         self.active = true;
-        rodio::play_raw(&device, self.decoder().convert_samples());
+        // let s = self.decoder().convert_samples()
+        rodio::play_raw(&device, self.decoder()
+        .speed(self.speed)
+        .amplify(0.1)
+        .convert_samples());
         // self.active = true;
 
     }
 }
 
-#[derive(Clone, Debug)]
+#[derive(Serialize, Deserialize, Clone, Debug)]
 pub struct Pattern {
+    pub name: String,
     pub repeat: i32,
     pub bpm: i32,
     pub sounds: Vec<Vec<Option<Sound>>>
@@ -74,6 +91,7 @@ pub struct Pattern {
 impl Default for Pattern {
     fn default() -> Pattern {
         Pattern {
+            name: "Unnamed".to_string(),
             repeat: 0,
             bpm: 120,
             sounds: vec![vec![None; 4]]
@@ -86,19 +104,14 @@ impl Pattern {
     pub fn play_mut(&mut self, device: &rodio::Device) {
         let delay = ((60.0 / self.bpm as f32)*1000.0) as u64;
         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] {
                         s.play(device);
                     }
                 }
-            
                 thread::sleep(Duration::from_millis(delay));
             }
-    
-
         }
     }
     pub fn play(&mut self, device: &rodio::Device) {
@@ -118,13 +131,11 @@ impl Pattern {
         }
     }
 
-
-
-    // pub fn cols_mut(&mut self) -> Vec<Option>Sound>> {
-    //     for row in &mut self.sounds {
-    //         row.resize(self.sounds.len() + slots, None);
-    //     }
-    // }
+    pub fn load(file: &str) -> Pattern {
+        let reader = BufReader::new(File::open(file).unwrap());
+        let pattern: Pattern = serde_json::from_reader(reader).unwrap();
+        pattern
+    }
 
     pub fn extend_length(&mut self, slots: usize) {
         for row in &mut self.sounds {

+ 34 - 12
src/main.rs

@@ -1,7 +1,9 @@
-// use std::io::BufReader;
-// use std::path::PathBuf;
-// use std::thread;
-// use std::time::Duration;
+
+#[macro_use]
+extern crate serde_derive;
+extern crate serde;
+extern crate serde_json;
+
 extern crate rodio;
 mod base;
 use base::*;
@@ -99,7 +101,11 @@ fn main() {
                     active_pattern.play_mut(&dev);
                 }
 
-
+                ui.drag_int(im_str!("repeat"), &mut active_pattern.repeat)
+                .max(32)
+                .speed(0.05)
+                .build();
+                
                 ui.drag_int(im_str!("bpm"), &mut active_pattern.bpm).build();
 
                 for row_idx in 0..active_pattern.sounds.len() {
@@ -109,8 +115,6 @@ fn main() {
                         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 {
@@ -118,7 +122,6 @@ fn main() {
                             }
                         }
 
-                        
                         // let style = ui.push_style_color(StyleColor::Button, [0.0, 1.0, 0.1, 1.0]);
                         // style.pop(ui);
                   
@@ -145,6 +148,7 @@ fn main() {
                     }
 
 
+
                 }
 
                 ui.text(im_str!("pat {} {}", pattern_col, pattern_row));
@@ -157,11 +161,29 @@ fn main() {
             .size([300.0, 100.0], Condition::FirstUseEver)
             .build(ui, || {
                 // let snd = &active_bar.sounds[pattern_col];
-                if let Some(s) = &active_sound {
+                if let Some(s) = &mut active_sound {
                     ui.text(im_str!("name   {}", s.name));
-                    ui.text(im_str!("roll   {}", s.roll));
-                    ui.text(im_str!("volume {}", s.volume));
-                    ui.text(im_str!("rate   {}", s.rate));
+                    // let _ = DragFloat::new()
+                    ui.drag_float(im_str!("volume"), &mut s.volume)
+                        .min(0.0)
+                        .max(1.0)
+                        .speed(0.01)
+                        .build();
+                    ui.drag_float(im_str!("speed"), &mut s.speed)
+                        .min(0.0)
+                        .max(2.0)
+                        .speed(0.01)
+                        .build();
+                    ui.drag_int(im_str!("roll"), &mut s.roll)
+                        .max(32)
+                        .speed(0.05)
+                        .build();
+
+                
+                    if ui.small_button(&im_str!("play")) {
+                        s.play(&dev);
+                    }
+                
                 }
                 if ui.small_button(&im_str!("clear")) {
                     active_sound = None;

+ 1 - 0
src/support_ogl/mod.rs

@@ -32,6 +32,7 @@ pub fn init(title: &str) -> System {
     let mut platform = WinitPlatform::init(&mut imgui);
 
     let hidpi_factor = platform.hidpi_factor();
+    dbg!(hidpi_factor);
     let font_size = (15.0 * hidpi_factor) as f32;
     imgui.fonts().add_font(&[
         FontSource::TtfData {