woelper 4 lat temu
rodzic
commit
0c67cd3acb
3 zmienionych plików z 52 dodań i 14 usunięć
  1. 2 2
      sampler.ini
  2. 39 11
      src/base.rs
  3. 11 1
      src/main.rs

+ 2 - 2
sampler.ini

@@ -29,7 +29,7 @@ Size=328,168
 Collapsed=0
 
 [Window][sound]
-Pos=13,6
+Pos=24,-2
 Size=290,149
 Collapsed=0
 
@@ -44,7 +44,7 @@ Size=684,283
 Collapsed=0
 
 [Window][patterns]
-Pos=467,10
+Pos=400,140
 Size=324,183
 Collapsed=0
 

+ 39 - 11
src/base.rs

@@ -23,12 +23,16 @@ pub struct Sound {
     pub location: PathBuf,
     #[serde(skip)]
     data: Arc<Vec<u8>>,
+    #[serde(skip)]
+    data_rev: Arc<Vec<u8>>,
     pub volume: f32,
     pub roll: i32,
     pub rollrate: i32,
     pub trim: f32,
     pub active: bool,
-    pub speed: f32
+    pub speed: f32,
+    #[serde(default)]
+    pub reverse: bool
 }
 
 
@@ -44,8 +48,11 @@ impl Sound {
         let mut buf = Vec::new();
         let mut file = File::open(filename)?;
         file.read_to_end(&mut buf)?;
+        let mut rev_buf = buf.clone();
+        rev_buf.reverse();
         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,
             volume: 1.0,
@@ -60,7 +67,7 @@ impl Sound {
     //     }
     // }
 
-    pub fn reload_data(&self) -> Result<Sound, std::io::Error>{
+    pub fn updated_drom_source(&self) -> Result<Sound, std::io::Error>{
         Sound::new(&self.location.to_string_lossy())
     }
 
@@ -72,17 +79,38 @@ impl Sound {
         })
     }
 
-    pub fn decoder(self: &Self) -> Decoder<Cursor<Sound>> {
-        Decoder::new(self.cursor()).unwrap()
+    pub fn cursor_rev(self: &Self) -> Cursor<Sound> {
+        Cursor::new(
+            Sound {
+                data: self.data_rev.clone(),
+                ..Default::default()
+        })
     }
 
+    // pub fn decoder(self: &Self) -> Decoder<Cursor<Sound>> {
+    //     Decoder::new(self.cursor()).unwrap()
+    // }
+
     pub fn play(&self, device: &rodio::Device) {
-        // let s = self.decoder().convert_samples()
-        rodio::play_raw(&device, self.decoder()
-        .speed(self.speed)
-        .amplify(self.volume)
-        .convert_samples());
-        // self.active = true;
+
+        let decoder = match self.reverse {
+            false => Decoder::new(self.cursor()),
+            true => Decoder::new(self.cursor_rev()),
+        };
+
+        match decoder {
+            Ok(ok_decoder) => {
+                rodio::play_raw(&device, ok_decoder
+                .speed(self.speed)
+                .amplify(self.volume)
+                .convert_samples());
+
+            }
+            Err(e) => {
+                dbg!(e);
+            }
+        }
+
 
     }
 }
@@ -242,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);
-                    snd.data = snd.reload_data().unwrap().data;
+                    snd.data = snd.updated_drom_source().unwrap().data;
                     // let s = snd.reload_data().unwrap();
                     // snd.data = s.data.clone();
                 }

+ 11 - 1
src/main.rs

@@ -61,7 +61,9 @@ fn main() {
     style.window_rounding = 1.5;
     style.anti_aliased_lines = false;
     style.anti_aliased_fill = false;
-    // ui.push_style_color(
+    // ui.push_style_color();
+    let PURPLE = [0.07, 0.05, 0.27, 1.00];
+
     // 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);
@@ -89,6 +91,12 @@ fn main() {
 
     system.main_loop(move |_, ui| {
 
+        let colors = ui.push_style_colors(&[
+            (StyleColor::FrameBg, PURPLE),
+            (StyleColor::Button, PURPLE),
+            (StyleColor::TitleBgActive, PURPLE),
+            (StyleColor::ResizeGrip, PURPLE),
+        ]);
 
         Window::new(im_str!("sources"))
             // .resizable(false)
@@ -253,6 +261,7 @@ fn main() {
                         .speed(0.05)
                         .build();
 
+                    ui.checkbox(im_str!("reverse"), &mut s.reverse);
                 
                     if ui.small_button(&im_str!("play")) {
                         s.play(&dev);
@@ -273,6 +282,7 @@ fn main() {
                 }
             });
     
+            colors.pop(&ui);
     
         });
 }