woelper преди 4 години
родител
ревизия
9f9f2502df
променени са 2 файла, в които са добавени 101 реда и са изтрити 8 реда
  1. 56 0
      src/base.rs
  2. 45 8
      src/main.rs

+ 56 - 0
src/base.rs

@@ -0,0 +1,56 @@
+use std::path::PathBuf;
+use std::io::BufReader;
+use std::thread;
+use std::time::Duration;
+use std::fs::File;
+
+#[derive(Clone)]
+pub struct Sound<'a> {
+    pub location: PathBuf,
+    pub dec: rodio::Decoder<std::io::BufReader<std::fs::File>>,
+}
+
+impl Sound {
+    pub fn play(&self, device: &rodio::Device) {
+        if let Ok(file) = std::fs::File::open(&self.location) {
+            let dec = rodio::Decoder::new(BufReader::new(file)).unwrap();
+            let reader = BufReader::new(file);
+            let d = device.clone();
+            let sink = rodio::play_once(d, reader).unwrap().detach();
+
+        }
+    }
+}
+
+pub struct Bar {
+    pub repeat: i32,
+    pub length: i32,
+    pub bpm: i32,
+    pub sounds: Vec<Sound>
+}
+
+impl Default for Bar {
+    fn default() -> Bar {
+        Bar {
+            repeat: 0,
+            length: 4,
+            bpm: 120,
+            sounds: vec![]
+        }
+    }
+}
+
+impl Bar {
+    pub fn play(&self, device: &rodio::Device) {
+        let delay = ((60.0 / self.bpm as f32)*1000.0) as u64;
+        dbg!(&delay);
+        for _ in 0..self.repeat+1 {
+
+            for sound in &self.sounds {
+                sound.play(device);
+                thread::sleep(Duration::from_millis(delay));
+
+            }
+        }
+    }
+}

+ 45 - 8
src/main.rs

@@ -1,17 +1,54 @@
-extern crate rodio;
-
 use std::io::BufReader;
+use std::path::PathBuf;
+
 use std::thread;
 use std::time::Duration;
+extern crate rodio;
+mod base;
+use base::*;
 
-fn main() {
-    let device = rodio::default_output_device().unwrap();
+fn play(location: &PathBuf) {
+    let dev = rodio::default_output_device().unwrap();
 
-    let file = std::fs::File::open("media/c64sid-ch1.wav").unwrap();
-    let beep1 = rodio::play_once(&device, BufReader::new(file)).unwrap();
-    beep1.set_volume(0.2);
-    println!("Started beep1");
+    let file = std::fs::File::open(&location).unwrap();
+    let sink = rodio::play_once(&dev, BufReader::new(file)).unwrap();
+    // let source = rodio::Decoder::new(BufReader::new(file)).unwrap();
+    // rodio::play_raw(&dev, source);
 
     thread::sleep(Duration::from_millis(1500));
 
+}
+
+fn main() {
+    // let device = rodio::default_output_device().unwrap();
+
+
+
+
+    let dev = rodio::default_output_device().unwrap();
+
+
+
+    let hat = Sound{location: PathBuf::from("media/c64sid-ch1.wav")};
+    let snare = Sound{location: PathBuf::from("media/c64sid-snare12.wav")};
+    let kick = Sound{location: PathBuf::from("media/c64sid-kick5.wav")};
+
+    let mut bar = Bar::default();
+    bar.repeat = 2;
+    bar.sounds = vec!(
+        kick.clone(),
+        hat.clone(),
+        snare.clone(),
+        hat.clone(),
+        kick.clone(),
+        hat.clone(),
+        snare.clone(),
+        hat.clone(),
+    );
+
+    bar.play(&dev);
+
+
+    //thread::sleep(Duration::from_millis(1500));
+
 }