1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
use std::time::Duration;

use crate::{
    effects::utility::{EnvelopeFollower, PeriodicNotification},
    graph::DspProcessor,
    SampleLocation,
};

use super::envelope_notification::{EnvelopeNotification, EnvelopeNotificationTransmitter};

pub struct EnvelopeProcessor {
    envelopes: Vec<EnvelopeFollower>,
    notification: PeriodicNotification,
    peaks: Vec<f32>,
    transmitter: EnvelopeNotificationTransmitter,
}

impl EnvelopeProcessor {
    pub fn new(
        sample_rate: usize,
        channel_count: usize,
        attack_time: Duration,
        release_time: Duration,
        notification_frequency: f64,
        transmitter: EnvelopeNotificationTransmitter,
    ) -> Self {
        Self {
            envelopes: (0..channel_count)
                .map(|_| EnvelopeFollower::new(sample_rate as f64, attack_time, release_time))
                .collect(),
            notification: PeriodicNotification::new(sample_rate, notification_frequency),
            peaks: (0..channel_count).map(|_| 0.0_f32).collect(),
            transmitter,
        }
    }

    fn send_notifications(&mut self, channel_count: usize) {
        for channel in 0..channel_count {
            let peak = self.peaks.get(channel).expect("Invalid channel index");
            let notification = EnvelopeNotification::new(channel, *peak);
            let _ = self.transmitter.send(notification);
        }

        self.peaks.fill_with(|| 0.0_f32);
    }
}

impl DspProcessor for EnvelopeProcessor {
    fn process_audio(&mut self, context: &mut crate::ProcessContext) {
        let mut position = 0;
        let channel_count = context.input_buffer.channel_count();

        while position < channel_count {
            let frame_count = std::cmp::min(
                context.input_buffer.frame_count(),
                self.notification.samples_until_next_notification(),
            );

            for channel in 0..channel_count {
                let location = SampleLocation::channel(channel);
                let channel_data = context.input_buffer.get_channel_data(location);
                let channel_data = &channel_data[position..position + frame_count];

                let envelope = self
                    .envelopes
                    .get_mut(channel)
                    .expect("Too many input channels");

                let peak = self
                    .peaks
                    .get_mut(channel)
                    .expect("Too many input channels");

                for sample in channel_data {
                    let envelope_value = envelope.process(*sample);
                    *peak = peak.max(envelope_value);
                }
            }

            if self
                .notification
                .advance(context.input_buffer.frame_count())
            {
                self.send_notifications(channel_count);
            }

            position += frame_count;
        }
    }
}