Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

operator_adsr

crates.io ci codecov license

Convert trigger signals into an ADSR envelope driven by the scheduler.

See Also

Example

cargo run -p rx_core --example operator_adsr_example
use std::time::Duration;

use rx_core::prelude::*;
use rx_core_testing::MockExecutor;

fn main() {
  let mut executor = MockExecutor::default();
  let scheduler = executor.get_scheduler_handle();

  let envelope = AdsrEnvelope {
    attack_time: Duration::from_millis(10),
    decay_time: Duration::from_millis(10),
    sustain_volume: 0.5,
    release_time: Duration::from_millis(15),
    ..Default::default()
  };

  let mut source = PublishSubject::<AdsrTrigger>::default();

  let mut subscription = source
    .clone()
    .adsr(
      AdsrOperatorOptions {
        envelope,
        ..Default::default()
      },
      scheduler.clone(),
    )
    .subscribe(PrintObserver::new("adsr"));

  source.next(true.into());
  executor.tick(Duration::from_millis(10));
  executor.tick(Duration::from_millis(10));

  source.next(false.into());
  executor.tick(Duration::from_millis(15));

  subscription.unsubscribe();
}
adsr - next: AdsrSignal { adsr_envelope_phase: Attack, phase_transition: AdsrEnvelopePhaseTransition(1), t: 0ns, value: 0.0 }
adsr - next: AdsrSignal { adsr_envelope_phase: Decay, phase_transition: AdsrEnvelopePhaseTransition(2), t: 10ms, value: 1.0 }
adsr - next: AdsrSignal { adsr_envelope_phase: None, phase_transition: AdsrEnvelopePhaseTransition(16), t: 0ns, value: 0.0 }
adsr - unsubscribed