Kaynağa Gözat

timeline and tracks

woelper 4 yıl önce
ebeveyn
işleme
7a7eadb95c
3 değiştirilmiş dosya ile 137 ekleme ve 41 silme
  1. 16 1
      sampler.ini
  2. 48 12
      src/base.rs
  3. 73 28
      src/main.rs

+ 16 - 1
sampler.ini

@@ -9,7 +9,7 @@ Size=300,100
 Collapsed=0
 
 [Window][sources]
-Pos=372,27
+Pos=401,140
 Size=339,460
 Collapsed=0
 
@@ -18,3 +18,18 @@ Pos=297,1
 Size=550,680
 Collapsed=0
 
+[Window][sequence]
+Pos=46,63
+Size=300,100
+Collapsed=0
+
+[Window][bar]
+Pos=24,248
+Size=328,168
+Collapsed=0
+
+[Window][sound]
+Pos=21,42
+Size=300,100
+Collapsed=0
+

+ 48 - 12
src/base.rs

@@ -1,9 +1,9 @@
 use std::path::PathBuf;
-use std::io::BufReader;
+// use std::io::BufReader;
 use std::thread;
 use std::time::Duration;
 use std::fs::File;
-use std::io::{Cursor, Read, Seek, SeekFrom, Write};
+use std::io::{Cursor, Read};
 use std::sync::Arc;
 use rodio::Decoder;
 use crate::rodio::Source;
@@ -18,9 +18,10 @@ pub struct Sound {
     pub name: String,
     pub location: PathBuf,
     data: Arc<Vec<u8>>,
-    volume: f32,
+    pub volume: f32,
     pub roll: i32,
-    pub rate: i32
+    pub rate: i32,
+    active: bool
 }
 
 
@@ -55,37 +56,72 @@ impl Sound {
     }
 
 
-    pub fn play(&self, device: &rodio::Device) {
+    pub fn play(&mut self, device: &rodio::Device) {
+        self.active = true;
         rodio::play_raw(&device, self.decoder().convert_samples());
+        // self.active = true;
+
     }
 }
 
+#[derive(Clone, Debug)]
 pub struct Bar {
     pub repeat: i32,
-    pub length: i32,
     pub bpm: i32,
-    pub sounds: Vec<Sound>
+    pub sounds: Vec<Option<Sound>>
 }
 
 impl Default for Bar {
     fn default() -> Bar {
         Bar {
             repeat: 0,
-            length: 4,
             bpm: 120,
-            sounds: vec![]
+            sounds: vec![None; 4]
         }
     }
 }
 
+
 impl Bar {
-    pub fn play(&self, device: &rodio::Device) {
+    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 sound in &self.sounds {
-                sound.play(device);
+            for sound in &mut self.sounds {
+                if let Some(s) = sound {
+                    s.play(device);
+                }
                 thread::sleep(Duration::from_millis(delay));
             }
         }
     }
+    pub fn extend(&mut self, slots: usize) {
+        self.sounds.resize(self.sounds.len() + slots, None);
+    }
+    pub fn length(&self) -> usize {
+        self.sounds.len()
+    }
+}
+
+#[derive(Clone, Debug)]
+pub struct Track {
+    pub bars: Vec<Bar>
+}
+
+impl Default for Track {
+    fn default() -> Track {
+        Track {bars: vec![Bar::default()]}
+    }
+}
+
+#[derive(Clone, Debug)]
+pub struct Timeline {
+    pub tracks: Vec<Track>
+}
+
+impl Default for Timeline {
+    fn default() -> Timeline {
+        Timeline {
+            tracks: vec![Track::default()]
+        }
+    }
 }

+ 73 - 28
src/main.rs

@@ -12,28 +12,32 @@ mod support_ogl;
 use env_logger;
 use log::*;
 
+fn collect_sounds (root: &str) -> Vec<Sound> {
+    WalkDir::new(root)
+    .into_iter()
+    .filter_map(|e| e.ok())
+    .filter(|e| e.path().is_file())
+    .map(|f| Sound::new(&f.path().to_string_lossy().to_string()))
+    .filter_map(|e| e.ok())
+    .collect()
+}
 fn main() {
     env_logger::init();
-    let mut sounds = vec![];
-
-    for entry in WalkDir::new("media")
-        .into_iter()
-        .filter_map(|e| e.ok())
-        .filter(|e| e.path().is_file())
-    {
-        match Sound::new(&entry.path().to_string_lossy().to_string()) {
-            Ok(s) => sounds.push(s),
-            Err(e) => error!("{:?} while opening {:?}", e, entry),
-        }
-    }
+    let mut sounds = collect_sounds("media");
+    let mut active_sound: usize = 0;
+    
+
 
     let dev = rodio::default_output_device().unwrap();
 
     let mut bar = Bar::default();
     bar.bpm = 160;
-    bar.repeat = 2;
 
-    let system = support_ogl::init(file!());
+    let mut timeline = Timeline::default();
+
+    let mut system = support_ogl::init(file!());
+    let s = system.imgui.style_mut();
+    s.window_rounding = 1.5;
     // let system = support_glium::init(file!());
 
     system.main_loop(move |_, ui| {
@@ -44,17 +48,18 @@ fn main() {
             // .always_auto_resize(true)
             .size([300.0, 300.0], Condition::FirstUseEver)
             .build(ui, || {
-                // let mut s = ui.clone_style();
-                // ui.show_style_editor(&mut s);
 
-                ui.show_default_style_editor();
+                // ui.show_default_style_editor();
 
-                ui.tree_node(im_str!("Tree")).build(|| {
-                    for s in &sounds {
+                ui.tree_node(im_str!("snds"))
+                .opened(true, Condition::Appearing)
+                .build(|| {
+                    for s in &mut sounds {
                         ui.tree_node(&im_str!("{}", s.name)).build(|| {
-                            ui.text(im_str!("blah blah"));
-                            ui.same_line(0.0);
-                            if ui.small_button(im_str!("add")) {}
+                            // ui.same_line(0.0);
+                            if ui.small_button(im_str!("add to bar")) {
+                                bar.sounds[active_sound] = Some(s.clone());
+                            }
                             ui.same_line(0.0);
                             if ui.small_button(im_str!("play")) {
                                 s.play(&dev);
@@ -64,15 +69,55 @@ fn main() {
                 });
             });
 
-        Window::new(im_str!("Hello world"))
+        Window::new(im_str!("bar"))
             .size([300.0, 100.0], Condition::FirstUseEver)
             .build(ui, || {
                 ui.text(im_str!("bars"));
+                ui.same_line(0.0);
+                ui.text(im_str!("snd {}", active_sound));
+
+                for i in 0..bar.length() {
+                    ui.same_line(0.0);
+                    
+                    let token = match active_sound == i {
+                        true => ui.push_style_color(StyleColor::Button, [1.0, 0.0, 0.0, 1.0]),
+                        false => ui.push_style_color(StyleColor::CheckMark, [0.0, 0.0, 0.0, 1.0])
+                    };
+
+                    if ui.small_button(&im_str!("{}", i)) {
+                        active_sound = i as usize;
+                    }
+
+                    token.pop(ui);
+
+                }
+                if ui.small_button(&im_str!("extend")) {
+                    bar.extend(1);
+                }
+
+                if ui.small_button(&im_str!("dbg")) {
+                    dbg!(&bar);
+                }
+                if ui.small_button(&im_str!("ply")) {
+                    bar.play(&dev);
+                }
+            });
+    
+            Window::new(im_str!("sound"))
+            .size([300.0, 100.0], Condition::FirstUseEver)
+            .build(ui, || {
+                let snd = &bar.sounds[active_sound];
+                if let Some(s) = snd{
+                    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));
+                }
 
-                // for i in 0..bar.length {
-                //     ui.same_line(0.0);
-                //     ui.small_button(&im_str!("{}", i));
-                // }
+       
             });
-    });
+    
+    
+    
+        });
 }