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
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
//! An algorithmic, IIR reverberation filter.

use num::traits::Float;

use oxcable::types::{SAMPLE_RATE, AudioDevice, MessageReceiver, Sample, Time};
use oxcable::utils::ringbuffer::RingBuffer;
use oxcable::utils::helpers::decibel_to_ratio;

use super::rooms::Room;


/// Defines the messages that the MoorerReverb supports.
#[derive(Clone, Copy, Debug)]
pub enum Message {
    /// Sets the length of the reverberation, in seconds.
    SetReverbTime(f32),
    /// Sets the feedback level; should be between 0.0 and 1.0.
    SetGain(f32),
    /// Sets the wetness level; should be between 0.0 and 1.0.
    SetWetness(f32),
}
pub use self::Message::*;


/// An algorithmic, IIR reverberation filter.
///
/// This algorithmic reverb filter follows the basic design specified by James
/// Moorer in his seminal paper, "About This Reverberation Business".
pub struct MoorerReverb {
    num_channels: usize,
    gain: f32,
    wetness: f32,

    tapped_delay_lines: Vec<RingBuffer<Sample>>,
    tapped_delays: Vec<Time>,
    tapped_gains: Vec<f32>,

    comb_delay_lines: Vec<Vec<RingBuffer<Sample>>>,
    comb_delays: Vec<Time>,
    comb_gains: Vec<f32>,
    comb_out_buffer: Vec<RingBuffer<Sample>>,
    comb_out_gain: f32,
}

impl MoorerReverb {
    /// Creates and initializes a new reverberator.
    ///
    /// * `room`: a [`Room`](rooms/struct.Room.html) struct. This specifies
    ///   aspects of the reverberation character.
    /// * `rev_time`: the -60dB time, in seconds.
    /// * `gain`: the output gain, in decibels.
    /// * `wetness`: how much of the input signal to mix into the output.
    /// * `num_channels`: number of channels to process.
    pub fn new(room: Room, rev_time: f32, gain: f32, wetness:f32,
           num_channels: usize) -> Self {
        assert!(room.tapped_delays.len() == room.tapped_gains.len());

        // Calculate delays
        let max_tapped_delay = room.tapped_delays[room.tapped_delays.len()-1];
        let comb_out_delay = max_tapped_delay;

        // Calculate comb gains based on reverberation time
        let (comb_gains, comb_out_gain) =
            compute_comb_gains(&room.comb_delays, rev_time);

        // Allocate tapped delay lines
        let init = vec![0.0; max_tapped_delay as usize + 1];
        let tapped_delay_lines = vec![RingBuffer::from(&init[..]); num_channels];

        // Allocate comb delay lines
        let mut comb_delay_lines = Vec::with_capacity(num_channels);
        for _ in 0..num_channels {
            let mut channel_lines = Vec::with_capacity(room.comb_delays.len());
            for j in 0..room.comb_delays.len() {
                let delay = room.comb_delays[j];
                let init = vec![0.0; delay as usize + 1];
                channel_lines.push(RingBuffer::from(&init[..]));
            }
            comb_delay_lines.push(channel_lines);
        }

        let init = vec![0.0; comb_out_delay as usize + 1];
        let comb_out_buffer = vec![RingBuffer::from(&init[..]); num_channels];

        // Return struct
        MoorerReverb {
            num_channels: num_channels,
            gain: decibel_to_ratio(gain),
            wetness: wetness,
            tapped_delay_lines: tapped_delay_lines,
            tapped_delays: Vec::from(room.tapped_delays),
            tapped_gains: Vec::from(room.tapped_gains),
            comb_delay_lines: comb_delay_lines,
            comb_delays: Vec::from(room.comb_delays),
            comb_gains: comb_gains,
            comb_out_buffer: comb_out_buffer,
            comb_out_gain: comb_out_gain,
        }
    }
}

impl MessageReceiver for MoorerReverb {
    type Msg = Message;
    fn handle_message(&mut self, msg: Message) {
        match msg {
            SetReverbTime(rev_time) => {
                let (comb_gains, comb_out_gain) =
                    compute_comb_gains(&self.comb_delays, rev_time);
                self.comb_gains = comb_gains;
                self.comb_out_gain = comb_out_gain;
            },
            SetGain(gain) => {
                self.gain = decibel_to_ratio(gain);
            },
            SetWetness(wetness) => {
                self.wetness = wetness;
            }
        }
    }
}

impl AudioDevice for MoorerReverb {
    fn num_inputs(&self) -> usize {
        self.num_channels
    }

    fn num_outputs(&self) -> usize {
        self.num_channels
    }

    fn tick(&mut self, t: Time, inputs: &[Sample], outputs: &mut[Sample]) {
        for (i,x) in inputs.iter().enumerate() {
            // Advance tapped delay line
            for (delay, gain) in self.tapped_delays.iter()
                    .zip(self.tapped_gains.iter()) {
                self.tapped_delay_lines[i][t + *delay] += *gain * x;
            }
            let tapped_out = (x + self.tapped_delay_lines[i][t]) /
                (self.tapped_delays.len() as f32);
            self.tapped_delay_lines[i].push(0.0);

            // Update comb filters
            let comb_out = self.comb_out_buffer[i][t];
            let mut next_comb_out = 0.0;
            for j in 0..self.comb_delays.len() {
                let gain  = self.comb_gains[j];
                let feedback = self.comb_delay_lines[i][j][t];
                self.comb_delay_lines[i][j].push(tapped_out + gain * feedback);
                next_comb_out += feedback;
            }
            next_comb_out *= self.comb_out_gain/(self.comb_delays.len() as f32);
            self.comb_out_buffer[i].push(next_comb_out);

            // Compute and store result
            let wet_out = tapped_out + comb_out;
            let y = self.gain * (self.wetness*wet_out + (1.0-self.wetness)*x);
            outputs[i] = y;
        }
    }
}

/// Calculates comb gains based on reverberation time
///
/// Inputs:
///  * comb_delays: the delays for each comb filter, in samples
///  * rev_time: the reverberation time, in seconds
///
/// Outputs:
///  * A vector of gains for each comb filter
///  * The final output gain of the comb filter summation
fn compute_comb_gains(comb_delays: &[Time], rev_time: f32)
        -> (Vec<f32>, f32) {
    let mut comb_gains = Vec::with_capacity(comb_delays.len());
    for &delay in comb_delays.iter() {
        let gain = 10.0.powf(-3.0*(delay as f32)*rev_time / (SAMPLE_RATE as f32));
        comb_gains.push(gain);
    }
    let comb_out_gain = 1.0 - 0.366/rev_time;
    (comb_gains, comb_out_gain)
}