rawdio/effects/sampler/
sampler_node.rs1use super::{sampler_event::*, sampler_processor::*};
2use crate::{commands::Id, effects::Channel, graph::DspParameters, prelude::*};
3
4pub struct Sampler {
6 pub node: GraphNode,
8 event_transmitter: EventTransmitter,
9}
10
11static EVENT_CHANNEL_CAPACITY: usize = 32;
12
13impl Sampler {
14 pub fn new_with_event_capacity(
16 context: &dyn Context,
17 sample: OwnedAudioBuffer,
18 capacity: usize,
19 ) -> Self {
20 let id = Id::generate();
21
22 let (event_transmitter, event_receiver) = Channel::bounded(capacity);
23
24 let input_count = 0;
25 let output_count = sample.channel_count();
26 let sample_rate = sample.sample_rate();
27 let processor = Box::new(SamplerDspProcess::new(sample_rate, sample, event_receiver));
28
29 Self {
30 node: GraphNode::new(
31 id,
32 context,
33 input_count,
34 output_count,
35 processor,
36 DspParameters::empty(),
37 ),
38 event_transmitter,
39 }
40 }
41
42 pub fn new(context: &dyn Context, sample: OwnedAudioBuffer) -> Self {
44 Self::new_with_event_capacity(context, sample, EVENT_CHANNEL_CAPACITY)
45 }
46
47 pub fn start_now(&mut self) {
49 self.send_event(SamplerEvent::start_now());
50 }
51
52 pub fn stop_now(&mut self) {
54 self.send_event(SamplerEvent::stop_now());
55 }
56
57 pub fn start_from_position_at_time(
59 &mut self,
60 start_time: Timestamp,
61 position_in_sample: Timestamp,
62 ) {
63 self.send_event(SamplerEvent::start(start_time, position_in_sample));
64 }
65
66 pub fn stop_at_time(&mut self, stop_time: Timestamp) {
68 self.send_event(SamplerEvent::stop(stop_time));
69 }
70
71 pub fn enable_loop(&mut self, loop_start: Timestamp, loop_end: Timestamp) {
78 self.send_event(SamplerEvent::enable_loop(loop_start, loop_end));
79 }
80
81 pub fn cancel_loop(&mut self) {
85 self.send_event(SamplerEvent::cancel_loop());
86 }
87
88 pub fn cancel_all(&mut self) {
90 self.send_event(SamplerEvent::cancel_all());
91 }
92
93 pub fn enable_loop_at_time(
101 &mut self,
102 enable_at_time: Timestamp,
103 loop_start: Timestamp,
104 loop_end: Timestamp,
105 ) {
106 self.send_event(SamplerEvent::enable_loop_at_time(
107 enable_at_time,
108 loop_start,
109 loop_end,
110 ));
111 }
112
113 pub fn cancel_loop_at_time(&mut self, cancel_time: Timestamp) {
115 self.send_event(SamplerEvent::cancel_loop_at_time(cancel_time));
116 }
117
118 fn send_event(&mut self, event: SamplerEvent) {
119 debug_assert!(!self.event_transmitter.is_full());
120 let _ = self.event_transmitter.send(event);
121 }
122}