-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmpm.rs
73 lines (62 loc) · 1.96 KB
/
mpm.rs
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
use std::thread;
use std::time::Duration;
use dev_helpers::note_number_to_string;
use dev_helpers::AudioHost;
use dev_helpers::AudioProcessor;
use microdsp::mpm::MpmPitchDetector;
struct PitchReading {
midi_note_number: f32,
frequency: f32,
}
struct MPMAudioProcessor {
pitch_detector: MpmPitchDetector,
}
impl MPMAudioProcessor {
fn new(sample_rate: f32) -> MPMAudioProcessor {
MPMAudioProcessor {
pitch_detector: MpmPitchDetector::new(sample_rate, 1024, 3 * 256),
}
}
}
impl AudioProcessor<PitchReading> for MPMAudioProcessor {
fn process(
&mut self,
in_buffer: &[f32],
_: &mut [f32],
_: usize,
to_main_thread: &mut dev_helpers::rtrb::Producer<PitchReading>,
_: &mut dev_helpers::rtrb::Consumer<PitchReading>,
) -> bool {
self.pitch_detector.process(in_buffer, |result| {
if result.is_tone() {
let _ = to_main_thread.push(PitchReading {
midi_note_number: result.midi_note_number,
frequency: result.frequency,
});
}
});
true
}
}
fn main() {
// Create an instance of an audio processor that does pitch detection on input samples
let sample_rate = 44100.0;
let processor = MPMAudioProcessor::new(sample_rate);
// Create an audio engine that provides the audio processor with real time input samples
let mut audio_host = AudioHost::new(sample_rate, processor);
println!("Listening for tones. Whistle!");
let poll_interval_ms = 30;
loop {
thread::sleep(Duration::from_millis(poll_interval_ms));
loop {
match audio_host.from_audio_thread.pop() {
Ok(reading) => println!(
"{} | {:.2} Hz",
note_number_to_string(reading.midi_note_number),
reading.frequency
),
_ => break,
}
}
}
}