voice.rs 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. use {pitch, time, Velocity};
  2. pub type Playhead = time::Samples;
  3. /// The current state of the Voice's note playback.
  4. #[derive(Copy, Clone, Debug)]
  5. pub enum NoteState {
  6. /// The note is current playing.
  7. Playing,
  8. /// The note has been released and is fading out.
  9. Released(Playhead),
  10. }
  11. /// A single monophonic voice of a `Sampler`.
  12. #[derive(Clone, Debug, PartialEq)]
  13. pub struct Voice {
  14. note: Option<(NoteState, pitch::Hz, Velocity)>,
  15. playhead: Playhead,
  16. }
  17. impl Voice {
  18. /// Construct a new `Voice`.
  19. pub fn new() -> Self {
  20. Voice {
  21. note: None,
  22. playhead: 0,
  23. }
  24. }
  25. /// Trigger playback with the given note.
  26. #[inline]
  27. pub fn note_on(&mut self, hz: NoteHz, vel: NoteVelocity) {
  28. self.maybe_note = Some((NoteState::Playing, hz, vel));
  29. }
  30. /// Release playback of the current note if there is one.
  31. #[inline]
  32. pub fn note_off(&mut self) {
  33. if let Some(&mut(ref mut state, _, _)) = self.note.as_mut() {
  34. *state = NoteState::Released(0);
  35. }
  36. }
  37. pub fn fill_buffer(&mut self, buffer: &mut [S]) {
  38. }
  39. }