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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
use openal::{ffi, al};
use internal::OpenAlData;
use presets::ReverbProperties;

/**
 * Create and configure reverb effects.
 *
 * A Sound can optionally be connected to a ReverbEffect, which can modify
 * how the user hears the Sound (through reverb, echo, frequency shift, etc)
 *
 * This can be used to model the environment that the listener is in,
 * for example a cave.
 *
 * Internally it creates an OpenAL Effect Object with an Auxiliary Effect
 * Slot Object pair.
 *
 * **Note:** the effects API may change as it's implemented fully, but I'll
 * try not to make the changes too drastic.
 *
 * # Examples
 * ```no_run
 * extern crate ears;
 * use ears::{ReverbEffect, ReverbPreset, Sound, AudioController};
 *
 * fn main() -> () {
 *    // Create an effect (in this case, using a preset)
 *    let effect = ReverbEffect::preset(ReverbPreset::Cave.properties()).ok();
 *
 *    // Create a Sound with the path of the sound file.
 *    let mut sound = Sound::new("path/to/my/sound.ogg").unwrap();
 *
 *    // Connect the sound to the effect
 *    sound.connect(&effect);
 *
 *    // Play it
 *    sound.play();
 *
 *    // Wait until the sound stopped playing
 *    while sound.is_playing() {}
 *
 *    // If you want to disconnect an Effect, just pass None
 *    sound.connect(&None);
 * }
 * ```
 */
pub struct ReverbEffect {
    effect_id: u32,
    effect_slot_id: u32,
}

impl ReverbEffect {
    pub fn new() -> Result<ReverbEffect, String> {
        check_openal_context!(Err("Invalid OpenAL context.".into()));

        // Can't seem to find a way to query whether or not EFX extension is available
        // or not... or if that's even necessary, so just assume it's available
        // and have the error checking sort the rest out.

        // Create the auxiliary effect slot
        let mut effect_slot_id = 0;
        al::alGenAuxiliaryEffectSlots(1, &mut effect_slot_id);

        // Create the effect
        let mut effect_id = 0;
        al::alGenEffects(1, &mut effect_id);

        // Assume only "standard reverb" for now. May add EAX reverb at some point.
        al::alEffecti(effect_id, ffi::AL_EFFECT_TYPE, ffi::AL_EFFECT_REVERB);

        // Check if there is OpenAL internal error
        if let Some(err) = al::openal_has_error() {
            return Err(format!("ReverbEffect::new - OpenAL error: {}", err));
        };

        Ok(ReverbEffect { effect_id, effect_slot_id })
    }

    pub fn preset(reverb_properties: ReverbProperties) -> Result<ReverbEffect, String> {
        match Self::new() {
            Ok(mut effect) => {
                effect.set_density(reverb_properties.density);
                effect.set_diffusion(reverb_properties.diffusion);
                effect.set_gain(reverb_properties.gain);
                effect.set_gainhf(reverb_properties.gainhf);
                effect.set_decay_time(reverb_properties.decay_time);
                effect.set_decay_hfratio(reverb_properties.decay_hfratio);
                effect.set_reflections_gain(reverb_properties.reflections_gain);
                effect.set_reflections_delay(reverb_properties.reflections_delay);
                effect.set_late_reverb_gain(reverb_properties.late_reverb_gain);
                effect.set_late_reverb_delay(reverb_properties.late_reverb_delay);
                effect.set_air_absorption_gainhf(reverb_properties.air_absorption_gainhf);
                effect.set_room_rolloff_factor(reverb_properties.room_rolloff_factor);
                effect.set_decay_hflimit(reverb_properties.decay_hflimit);

                // Check if there is OpenAL internal error
                if let Some(err) = al::openal_has_error() {
                    return Err(format!("ReverbEffect::preset - OpenAL error: {}", err));
                };

                effect.update_slot();

                Ok(effect)
            },
            Err(e) => Err(e)
        }
    }

    pub fn slot(&self) -> u32 {
      self.effect_slot_id
    }

    fn update_slot(&mut self) {
        check_openal_context!(());
        al::alAuxiliaryEffectSloti(self.effect_slot_id, ffi::AL_EFFECTSLOT_EFFECT, self.effect_id);
    }

    fn set_density(&mut self, density: f32) {
        check_openal_context!(());
        al::alEffectf(self.effect_id, ffi::AL_REVERB_DENSITY, density);
    }

    fn set_diffusion(&mut self, diffusion: f32) {
        check_openal_context!(());
        al::alEffectf(self.effect_id, ffi::AL_REVERB_DIFFUSION, diffusion);
    }

    fn set_gain(&mut self, gain: f32) {
        check_openal_context!(());
        al::alEffectf(self.effect_id, ffi::AL_REVERB_GAIN, gain);
    }

    fn set_gainhf(&mut self, gainhf: f32) {
        check_openal_context!(());
        al::alEffectf(self.effect_id, ffi::AL_REVERB_GAINHF, gainhf);
    }

    fn set_decay_time(&mut self, decay_time: f32) {
        check_openal_context!(());
        al::alEffectf(self.effect_id, ffi::AL_REVERB_DECAY_TIME, decay_time);
    }

    fn set_decay_hfratio(&mut self, decay_hfratio: f32) {
        check_openal_context!(());
        al::alEffectf(self.effect_id, ffi::AL_REVERB_DECAY_HFRATIO, decay_hfratio);
    }

    fn set_reflections_gain(&mut self, reflections_gain: f32) {
        check_openal_context!(());
        al::alEffectf(self.effect_id, ffi::AL_REVERB_REFLECTIONS_GAIN, reflections_gain);
    }

    fn set_reflections_delay(&mut self, reflections_delay: f32) {
        check_openal_context!(());
        al::alEffectf(self.effect_id, ffi::AL_REVERB_REFLECTIONS_DELAY, reflections_delay);
    }

    fn set_late_reverb_gain(&mut self, late_reverb_gain: f32) {
        check_openal_context!(());
        al::alEffectf(self.effect_id, ffi::AL_REVERB_LATE_REVERB_GAIN, late_reverb_gain);
    }

    fn set_late_reverb_delay(&mut self, late_reverb_delay: f32) {
        check_openal_context!(());
        al::alEffectf(self.effect_id, ffi::AL_REVERB_LATE_REVERB_DELAY, late_reverb_delay);
    }

    fn set_air_absorption_gainhf(&mut self, air_absorption_gainhf: f32) {
        check_openal_context!(());
        al::alEffectf(self.effect_id, ffi::AL_REVERB_AIR_ABSORPTION_GAINHF, air_absorption_gainhf);
    }

    fn set_room_rolloff_factor(&mut self, room_rolloff_factor: f32) {
        check_openal_context!(());
        al::alEffectf(self.effect_id, ffi::AL_REVERB_ROOM_ROLLOFF_FACTOR, room_rolloff_factor);
    }

    fn set_decay_hflimit(&mut self, decay_hflimit: i32) {
        check_openal_context!(());
        al::alEffecti(self.effect_id, ffi::AL_REVERB_DECAY_HFLIMIT, decay_hflimit);
    }
}

impl Drop for ReverbEffect {
    // Delete the Effect Object and Auxiliary Effect Slot Object
    fn drop(&mut self) -> () {
        check_openal_context!(());

        // Disconnect the effect and slot
        al::alAuxiliaryEffectSloti(self.effect_slot_id, ffi::AL_EFFECTSLOT_EFFECT, ffi::AL_EFFECT_NULL as u32);

        unsafe {
            ffi::alDeleteEffects(1, &mut self.effect_id);
            ffi::alDeleteAuxiliaryEffectSlots(1, &mut self.effect_slot_id);
        }

        // Check if there is OpenAL internal error
        //
        // TODO: this could probably be avoided with some better design
        if al::openal_has_error().is_some() {
            eprintln!("Ears failed to drop ReverbEffect completely, one or more source is probably still referencing it.");
            eprintln!("\tEffect Object: {}", self.effect_id);
            eprintln!("\tAuxiliary Effect Slot: {}", self.effect_slot_id);
        };
    }
}