Skip to content

Commit

Permalink
Softer release
Browse files Browse the repository at this point in the history
  • Loading branch information
askeksa committed Nov 18, 2018
1 parent e91ffa9 commit c1334f4
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions cinter/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,13 @@ impl Note {
}
}

fn release_amplitude(&self) -> f32 {
match self.release_time {
Some(release_time) => (1.0 - (self.time - release_time) as f32 * 0.001).max(0.0),
None => 1.0
}
}

fn produce_sample(&mut self) -> f32 {
let phase = self.time as f32 * self.freq;
let i = phase.floor() as usize;
Expand All @@ -84,9 +91,7 @@ impl Note {
let d2 = instrument.get_sample(i + 2) as f32;
let d3 = instrument.get_sample(i + 3) as f32;
let mut v = a0*d0 + a1*d1 + a2*d2 + a3*d3;
if let Some(release_time) = self.release_time {
v *= 1.0 - (self.time - release_time) as f32 * 0.1;
}
v *= self.release_amplitude();
self.time += 1;
v / 254.0
}
Expand All @@ -100,10 +105,7 @@ impl Note {
}

fn is_alive(&self) -> bool {
match self.release_time {
Some(time) => self.time - time > 10,
None => true
}
self.release_amplitude() > 0.0
}
}

Expand Down

0 comments on commit c1334f4

Please sign in to comment.