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
// Take a look at the license at the top of the repository in the LICENSE file.

use glib::prelude::*;

use std::convert;
use std::ops;

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct AudioConverterConfig(gst::Structure);

impl ops::Deref for AudioConverterConfig {
    type Target = gst::StructureRef;

    fn deref(&self) -> &gst::StructureRef {
        self.0.deref()
    }
}

impl ops::DerefMut for AudioConverterConfig {
    fn deref_mut(&mut self) -> &mut gst::StructureRef {
        self.0.deref_mut()
    }
}

impl AsRef<gst::StructureRef> for AudioConverterConfig {
    fn as_ref(&self) -> &gst::StructureRef {
        self.0.as_ref()
    }
}

impl AsMut<gst::StructureRef> for AudioConverterConfig {
    fn as_mut(&mut self) -> &mut gst::StructureRef {
        self.0.as_mut()
    }
}

impl Default for AudioConverterConfig {
    fn default() -> Self {
        AudioConverterConfig::new()
    }
}

impl convert::TryFrom<gst::Structure> for AudioConverterConfig {
    type Error = glib::BoolError;

    fn try_from(v: gst::Structure) -> Result<AudioConverterConfig, Self::Error> {
        skip_assert_initialized!();
        if v.get_name() == "GstAudioConverter" {
            Ok(AudioConverterConfig(v))
        } else {
            Err(glib_bool_error!("Structure is no AudioConverterConfig"))
        }
    }
}

impl<'a> convert::TryFrom<&'a gst::StructureRef> for AudioConverterConfig {
    type Error = glib::BoolError;

    fn try_from(v: &'a gst::StructureRef) -> Result<AudioConverterConfig, Self::Error> {
        skip_assert_initialized!();
        AudioConverterConfig::try_from(v.to_owned())
    }
}

impl From<AudioConverterConfig> for gst::Structure {
    fn from(v: AudioConverterConfig) -> gst::Structure {
        skip_assert_initialized!();
        v.0
    }
}

impl AudioConverterConfig {
    pub fn new() -> Self {
        AudioConverterConfig(gst::Structure::new_empty("GstAudioConverter"))
    }

    pub fn set_dither_method(&mut self, v: crate::AudioDitherMethod) {
        self.0.set("GstAudioConverter.dither-method", &v);
    }

    pub fn get_dither_method(&self) -> crate::AudioDitherMethod {
        self.0
            .get_optional("GstAudioConverter.dither-method")
            .expect("Wrong type")
            .unwrap_or(crate::AudioDitherMethod::None)
    }

    pub fn set_noise_shaping_method(&mut self, v: crate::AudioNoiseShapingMethod) {
        self.0.set("GstAudioConverter.noise-shaping-method", &v);
    }

    pub fn get_noise_shaping_method(&self) -> crate::AudioNoiseShapingMethod {
        self.0
            .get_optional("GstAudioConverter.noise-shaping-method")
            .expect("Wrong type")
            .unwrap_or(crate::AudioNoiseShapingMethod::None)
    }

    pub fn set_quantization(&mut self, v: u32) {
        self.0.set("GstAudioConverter.quantization", &v);
    }

    pub fn get_quantization(&self) -> u32 {
        self.0
            .get_optional("GstAudioConverter.quantization")
            .expect("Wrong type")
            .unwrap_or(1)
    }

    #[cfg(any(feature = "v1_10", feature = "dox"))]
    #[cfg_attr(feature = "dox", doc(cfg(feature = "v1_10")))]
    pub fn set_resampler_method(&mut self, v: crate::AudioResamplerMethod) {
        self.0.set("GstAudioConverter.resampler-method", &v);
    }

    #[cfg(any(feature = "v1_10", feature = "dox"))]
    #[cfg_attr(feature = "dox", doc(cfg(feature = "v1_10")))]
    pub fn get_resampler_method(&self) -> crate::AudioResamplerMethod {
        self.0
            .get_optional("GstAudioConverter.resampler-method")
            .expect("Wrong type")
            .unwrap_or(crate::AudioResamplerMethod::BlackmanNuttall)
    }

    pub fn set_mix_matrix<T: AsRef<[f32]>>(&mut self, v: &[T]) {
        let length = v.get(0).map(|v| v.as_ref().len()).unwrap_or(0);
        let array = gst::Array::from_owned(
            v.iter()
                .map(|val| {
                    let val = val.as_ref();
                    assert_eq!(val.len(), length);
                    gst::Array::from_owned(
                        val.iter()
                            .map(|val| val.to_send_value())
                            .collect::<Vec<_>>(),
                    )
                    .to_send_value()
                })
                .collect::<Vec<_>>(),
        );
        self.0.set("GstAudioConverter.mix-matrix", &array);
    }

    pub fn get_mix_matrix(&self) -> Vec<Vec<f32>> {
        self.0
            .get_optional::<gst::Array>("GstAudioConverter.mix-matrix")
            .expect("Wrong type")
            .map(|array| {
                array
                    .as_slice()
                    .iter()
                    .map(|val| {
                        let array = val
                            .get::<gst::Array>()
                            .expect("Wrong type")
                            .unwrap_or_else(|| gst::Array::from_owned(Vec::new()));

                        array
                            .as_slice()
                            .iter()
                            .map(|val| val.get_some::<f32>().expect("Wrong type"))
                            .collect::<Vec<_>>()
                    })
                    .collect::<Vec<_>>()
            })
            .unwrap_or_else(Vec::new)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_mix_matrix() {
        const MATRIX: &[&[f32]] = &[&[1.2, 0.3], &[0.2, 0.8]];

        gst::init().unwrap();

        let mut config = AudioConverterConfig::new();
        config.set_mix_matrix(MATRIX);

        let matrix = config.get_mix_matrix();
        assert_eq!(matrix, MATRIX);

        config.set_mix_matrix(&matrix);
    }
}