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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
use crate::{
    check_init, ffi,
    vec::{FVec, FVecMut},
    AsNativeStr, Error, Result, Smpl, Status,
};

use std::{
    fmt::{Display, Formatter, Result as FmtResult},
    str::FromStr,
};

/**
 * Pitch detection method
 */
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum PitchMode {
    /**
     * Schmitt trigger
     *
     * This pitch extraction method implements a Schmitt trigger to estimate the period of a signal.
     *
     * This file was derived from the tuneit project, written by Mario Lang to detect the fundamental frequency of a sound.
     *
     * See [http://delysid.org/tuneit.html](http://delysid.org/tuneit.html)
     */
    Schmitt,

    /**
     * A fast harmonic comb filter
     *
     * This pitch extraction method implements a fast harmonic comb filter to determine the fundamental frequency of a harmonic sound.
     *
     * This file was derived from the tuneit project, written by Mario Lang to detect the fundamental frequency of a sound.
     *
     * See [http://delysid.org/tuneit.html](http://delysid.org/tuneit.html)
     */
    Fcomb,

    /**
     * Multiple-comb filter
     *
     * This fundamental frequency estimation algorithm implements spectral flattening, multi-comb filtering and peak histogramming.
     *
     * This method was designed by Juan P. Bello and described in:
     *
     * Juan-Pablo Bello. "Towards the Automated Analysis of Simple Polyphonic Music". PhD thesis, Centre for Digital Music, Queen Mary University of London, London, UK, 2003.
     */
    Mcomb,

    /**
     * YIN algorithm
     *
     * This algorithm was developed by A. de Cheveigne and H. Kawahara and published in:
     *
     * De Cheveigné, A., Kawahara, H. (2002) "YIN, a fundamental frequency estimator for speech and music", J. Acoust. Soc. Am. 111, 1917-1930.
     *
     * See [http://recherche.ircam.fr/equipes/pcm/pub/people/cheveign.html](http://recherche.ircam.fr/equipes/pcm/pub/people/cheveign.html)
     */
    Yin,

    /**
     * YIN fast algorithm
     *
     * This algorithm is equivalent to the YIN algorithm, but computed in the spectral domain for efficiency. See also python/demos/demo_yin_compare.py.
     */
    Yinfast,

    /**
     * YIN fft algorithm
     *
     * This algorithm was derived from the YIN algorithm. In this implementation, a Fourier transform is used to compute a tapered square difference function, which allows spectral weighting. Because the difference function is tapered, the selection of the period is simplified.
     *
     * Paul Brossier, Automatic annotation of musical audio for interactive systems, Chapter 3, Pitch Analysis, PhD thesis, Centre for Digital music, Queen Mary University of London, London, UK, 2006.
     */
    Yinfft,

    Specacf,
}

impl Default for PitchMode {
    fn default() -> Self {
        PitchMode::Yinfft
    }
}

impl AsNativeStr for PitchMode {
    fn as_native_str(&self) -> &'static str {
        use self::PitchMode::*;

        match self {
            Schmitt => "schmitt\0",
            Fcomb => "fcomb\0",
            Mcomb => "mcomb\0",
            Yin => "yin\0",
            Yinfast => "yinfast\0",
            Yinfft => "yinfft\0",
            Specacf => "specacf\0",
        }
    }
}

impl AsRef<str> for PitchMode {
    fn as_ref(&self) -> &'static str {
        self.as_rust_str()
    }
}

impl Display for PitchMode {
    fn fmt(&self, f: &mut Formatter) -> FmtResult {
        self.as_ref().fmt(f)
    }
}

impl FromStr for PitchMode {
    type Err = Error;

    fn from_str(src: &str) -> Result<Self> {
        use self::PitchMode::*;

        Ok(match src {
            "schmitt" => Schmitt,
            "fcomb" => Fcomb,
            "mcomb" => Mcomb,
            "yin" => Yin,
            "yinfast" => Yinfast,
            "yinfft" => Yinfft,
            "specacf" => Specacf,
            _ => return Err(Error::InvalidArg),
        })
    }
}

/**
 * Pitch output unit
 */
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum PitchUnit {
    /**
     * Hertz
     */
    Hz,

    /**
     * Midi
     */
    Midi,

    /**
     * Cent
     */
    Cent,

    /**
     * Bin
     */
    Bin,
}

impl Default for PitchUnit {
    fn default() -> Self {
        PitchUnit::Hz
    }
}

impl AsNativeStr for PitchUnit {
    fn as_native_str(&self) -> &'static str {
        use self::PitchUnit::*;

        match self {
            Hz => "hertz\0",
            Midi => "midi\0",
            Cent => "cent\0",
            Bin => "bin\0",
        }
    }
}

impl AsRef<str> for PitchUnit {
    fn as_ref(&self) -> &'static str {
        self.as_rust_str()
    }
}

impl Display for PitchUnit {
    fn fmt(&self, f: &mut Formatter) -> FmtResult {
        self.as_ref().fmt(f)
    }
}

impl FromStr for PitchUnit {
    type Err = Error;

    fn from_str(src: &str) -> Result<Self> {
        use self::PitchUnit::*;

        Ok(match src {
            "hertz" => Hz,
            "midi" => Midi,
            "cent" => Cent,
            "bin" => Bin,
            _ => return Err(Error::InvalidArg),
        })
    }
}

/**
 * Pitch detection object
 */
pub struct Pitch {
    pitch: *mut ffi::aubio_pitch_t,
    hop_size: usize,
}

impl Drop for Pitch {
    fn drop(&mut self) {
        unsafe { ffi::del_aubio_pitch(self.pitch) }
    }
}

impl Pitch {
    /**
     * Creation of the pitch detection object
     *
     * - `method` Pitch detection algorithm
     * - `buf_size` Size of the input buffer to analyse
     * - `hop_size` Step size between two consecutive analysis instant
     * - `sample_rate` Sampling rate of the signal
     */
    pub fn new(
        method: PitchMode,
        buf_size: usize,
        hop_size: usize,
        sample_rate: u32,
    ) -> Result<Self> {
        let pitch = unsafe {
            ffi::new_aubio_pitch(
                method.as_native_cstr(),
                buf_size as ffi::uint_t,
                hop_size as ffi::uint_t,
                sample_rate as ffi::uint_t,
            )
        };

        check_init(pitch)?;

        Ok(Self { pitch, hop_size })
    }

    /**
     * Change yin or yinfft tolerance threshold
     */
    pub fn with_tolerance(mut self, tolerance: Smpl) -> Self {
        self.set_tolerance(tolerance);
        self
    }

    /**
     * Set the silence threshold of the pitch detection object
     */
    pub fn with_silence(mut self, silence: Smpl) -> Self {
        self.set_silence(silence);
        self
    }

    /**
     * Set the output unit of the pitch detection object
     */
    pub fn with_unit(mut self, unit: PitchUnit) -> Self {
        self.set_unit(unit);
        self
    }

    /**
     * Get hop size
     */
    pub fn get_hop(&self) -> usize {
        self.hop_size
    }

    /**
     * Execute pitch detection on an input signal frame
     *
     * - `input` Input signal of size `hop_size`
     * - `output` Output pitch candidates of size 1
     */
    pub fn do_<'i, 'o, I, O>(&mut self, input: I, output: O) -> Status
    where
        I: Into<FVec<'i>>,
        O: Into<FVecMut<'o>>,
    {
        let input = input.into();
        let mut output = output.into();

        input.check_size(self.get_hop())?;
        output.check_size(1)?;

        unsafe {
            ffi::aubio_pitch_do(self.pitch, input.as_ptr(), output.as_mut_ptr());
        }
        Ok(())
    }

    /**
     * Execute pitch detection on an input signal frame
     *
     * - `input` Input signal of size `hop_size`
     */
    pub fn do_result<'i, I>(&mut self, input: I) -> Result<Smpl>
    where
        I: Into<FVec<'i>>,
    {
        let mut output = [0.; 1];
        self.do_(input, &mut output)?;
        Ok(output[0])
    }

    /**
     * Change yin or yinfft tolerance threshold
     */
    pub fn set_tolerance(&mut self, tolerance: Smpl) {
        unsafe {
            ffi::aubio_pitch_set_tolerance(self.pitch, tolerance);
        }
    }

    /**
     * Get yin or yinfft tolerance threshold
     */
    pub fn get_tolerance(&self) -> Smpl {
        unsafe { ffi::aubio_pitch_get_tolerance(self.pitch) }
    }

    /**
     * Set the silence threshold of the pitch detection object
     */
    pub fn set_silence(&mut self, silence: Smpl) {
        unsafe {
            ffi::aubio_pitch_set_silence(self.pitch, silence);
        }
    }

    /**
     * Get the silence threshold of the pitch detection object
     */
    pub fn get_silence(&self) -> Smpl {
        unsafe { ffi::aubio_pitch_get_silence(self.pitch) }
    }

    /**
     * Set the output unit of the pitch detection object
     */
    pub fn set_unit(&mut self, unit: PitchUnit) {
        unsafe {
            ffi::aubio_pitch_set_unit(self.pitch, unit.as_native_cstr());
        }
    }

    /**
     * Get the current confidence of the pitch algorithm
     */
    pub fn get_confidence(&self) -> Smpl {
        unsafe { ffi::aubio_pitch_get_confidence(self.pitch) }
    }
}