|
@@ -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 {
|