summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJustin Worthe <justin@worthe-it.co.za>2017-07-21 22:31:10 +0200
committerJustin Worthe <justin@worthe-it.co.za>2017-07-21 22:31:10 +0200
commitd2ec267baf0a419ac79c296a0240885f8158c5dd (patch)
tree7bd19e141855efb86f47cace3640182d80d90a7b /src
parented89f64962d5de120db26f140c8e027991ab2797 (diff)
Made changes suggested by Clippy
Diffstat (limited to 'src')
-rw-r--r--src/audio.rs4
-rw-r--r--src/gui.rs35
-rw-r--r--src/transforms.rs35
3 files changed, 37 insertions, 37 deletions
diff --git a/src/audio.rs b/src/audio.rs
index 98ba84e..2b36717 100644
--- a/src/audio.rs
+++ b/src/audio.rs
@@ -30,8 +30,8 @@ pub fn get_default_device(pa: &pa::PortAudio) -> Result<u32, pa::Error> {
}
pub fn start_listening_default(pa: &pa::PortAudio, sender: Sender<Vec<f32>>) -> Result<pa::Stream<pa::NonBlocking, pa::Input<f32>>, pa::Error> {
- let default = get_default_device(&pa)?;
- start_listening(&pa, default, sender)
+ let default = get_default_device(pa)?;
+ start_listening(pa, default, sender)
}
pub fn start_listening(pa: &pa::PortAudio, device_index: u32,
diff --git a/src/gui.rs b/src/gui.rs
index d66c685..3b2bfab 100644
--- a/src/gui.rs
+++ b/src/gui.rs
@@ -141,14 +141,13 @@ fn connect_dropdown_choose_microphone(mic_sender: Sender<Vec<f32>>, state: Rc<Re
let dropdown = state.borrow().ui.dropdown.clone();
start_listening_current_dropdown_value(&dropdown, mic_sender.clone(), state.clone());
dropdown.connect_changed(move |dropdown: &gtk::ComboBoxText| {
- start_listening_current_dropdown_value(&dropdown, mic_sender.clone(), state.clone())
+ start_listening_current_dropdown_value(dropdown, mic_sender.clone(), state.clone())
});
}
fn start_listening_current_dropdown_value(dropdown: &gtk::ComboBoxText, mic_sender: Sender<Vec<f32>>, state: Rc<RefCell<ApplicationState>>) {
- match state.borrow_mut().pa_stream {
- Some(ref mut stream) => {stream.stop().ok();},
- _ => {}
+ if let Some(ref mut stream) = state.borrow_mut().pa_stream {
+ stream.stop().ok();
}
let selected_mic = match dropdown.get_active_id().and_then(|id| id.parse().ok()) {
Some(mic) => mic,
@@ -191,9 +190,9 @@ fn start_processing_audio(mic_receiver: Receiver<Vec<f32>>, cross_thread_state:
fn setup_pitch_label_callbacks(state: Rc<RefCell<ApplicationState>>, cross_thread_state: Arc<RwLock<CrossThreadState>>) {
gtk::timeout_add(1000/FPS, move || {
- let ref ui = state.borrow().ui;
+ let ui = &state.borrow().ui;
if let Ok(cross_thread_state) = cross_thread_state.read() {
- let ref pitch = cross_thread_state.pitch;
+ let pitch = &cross_thread_state.pitch;
ui.pitch_label.set_label(pitch.as_ref());
ui.pitch_error_indicator.queue_draw();
ui.oscilloscope_chart.queue_draw();
@@ -206,8 +205,8 @@ fn setup_pitch_label_callbacks(state: Rc<RefCell<ApplicationState>>, cross_threa
fn setup_pitch_error_indicator_callbacks(state: Rc<RefCell<ApplicationState>>, cross_thread_state: Arc<RwLock<CrossThreadState>>) {
let outer_state = state.clone();
- let ref canvas = outer_state.borrow().ui.pitch_error_indicator;
- canvas.connect_draw(move |ref canvas, ref context| {
+ let canvas = &outer_state.borrow().ui.pitch_error_indicator;
+ canvas.connect_draw(move |canvas, context| {
let width = canvas.get_allocated_width() as f64;
let midpoint = width / 2.0;
@@ -246,10 +245,10 @@ fn setup_pitch_error_indicator_callbacks(state: Rc<RefCell<ApplicationState>>, c
fn setup_oscilloscope_drawing_area_callbacks(state: Rc<RefCell<ApplicationState>>, cross_thread_state: Arc<RwLock<CrossThreadState>>) {
let outer_state = state.clone();
- let ref canvas = outer_state.borrow().ui.oscilloscope_chart;
- canvas.connect_draw(move |ref canvas, ref context| {
+ let canvas = &outer_state.borrow().ui.oscilloscope_chart;
+ canvas.connect_draw(move |canvas, context| {
if let Ok(cross_thread_state) = cross_thread_state.read() {
- let ref signal = cross_thread_state.signal;
+ let signal = &cross_thread_state.signal;
let width = canvas.get_allocated_width() as f64;
// Set as a constant so signal won't change size based on
@@ -279,8 +278,8 @@ fn setup_oscilloscope_drawing_area_callbacks(state: Rc<RefCell<ApplicationState>
fn setup_correlation_drawing_area_callbacks(state: Rc<RefCell<ApplicationState>>, cross_thread_state: Arc<RwLock<CrossThreadState>>) {
let outer_state = state.clone();
- let ref canvas = outer_state.borrow().ui.correlation_chart;
- canvas.connect_draw(move |ref canvas, ref context| {
+ let canvas = &outer_state.borrow().ui.correlation_chart;
+ canvas.connect_draw(move |canvas, context| {
let width = canvas.get_allocated_width() as f64;
let height = canvas.get_allocated_height() as f64;
@@ -291,7 +290,7 @@ fn setup_correlation_drawing_area_callbacks(state: Rc<RefCell<ApplicationState>>
context.stroke();
if let Ok(cross_thread_state) = cross_thread_state.read() {
- let ref correlation = cross_thread_state.correlation;
+ let correlation = &cross_thread_state.correlation;
let len = correlation.len() as f64;
let max = match correlation.first() {
Some(&c) => c as f64,
@@ -323,18 +322,18 @@ fn setup_correlation_drawing_area_callbacks(state: Rc<RefCell<ApplicationState>>
fn setup_chart_visibility_callbacks(state: Rc<RefCell<ApplicationState>>) {
let outer_state = state.clone();
- let ref oscilloscope_toggle_button = outer_state.borrow().ui.oscilloscope_toggle_button;
- let ref correlation_toggle_button = outer_state.borrow().ui.correlation_toggle_button;
+ let oscilloscope_toggle_button = &outer_state.borrow().ui.oscilloscope_toggle_button;
+ let correlation_toggle_button = &outer_state.borrow().ui.correlation_toggle_button;
let oscilloscope_state = state.clone();
oscilloscope_toggle_button.connect_clicked(move |_| {
- let ref chart = oscilloscope_state.borrow().ui.oscilloscope_chart;
+ let chart = &oscilloscope_state.borrow().ui.oscilloscope_chart;
chart.set_visible(!chart.get_visible());
});
let correlation_state = state.clone();
correlation_toggle_button.connect_clicked(move |_| {
- let ref chart = correlation_state.borrow().ui.correlation_chart;
+ let chart = &correlation_state.borrow().ui.correlation_chart;
chart.set_visible(!chart.get_visible());
});
}
diff --git a/src/transforms.rs b/src/transforms.rs
index 43522da..07b3cce 100644
--- a/src/transforms.rs
+++ b/src/transforms.rs
@@ -1,9 +1,9 @@
-pub fn remove_mean_offset(signal: &Vec<f32>) -> Vec<f32> {
+pub fn remove_mean_offset(signal: &[f32]) -> Vec<f32> {
let mean = signal.iter().sum::<f32>()/signal.len() as f32;
signal.iter().map(|x| x - mean).collect()
}
-pub fn correlation(signal: &Vec<f32>) -> Vec<f32> {
+pub fn correlation(signal: &[f32]) -> Vec<f32> {
(0..signal.len()).map(|offset| {
signal.iter().take(signal.len() - offset)
.zip(signal.iter().skip(offset))
@@ -12,8 +12,8 @@ pub fn correlation(signal: &Vec<f32>) -> Vec<f32> {
}).collect()
}
-pub fn find_fundamental_frequency_correlation(signal: &Vec<f32>, sample_rate: f32) -> Option<f32> {
- let normalized_signal = remove_mean_offset(&signal);
+pub fn find_fundamental_frequency_correlation(signal: &[f32], sample_rate: f32) -> Option<f32> {
+ let normalized_signal = remove_mean_offset(signal);
if normalized_signal.iter().all(|&x| x.abs() < 0.05) {
// silence
@@ -47,7 +47,7 @@ pub fn find_fundamental_frequency_correlation(signal: &Vec<f32>, sample_rate: f3
}
}
-fn refine_fundamentals(correlation: &Vec<f32>, low_bound: f32, high_bound: f32) -> f32 {
+fn refine_fundamentals(correlation: &[f32], low_bound: f32, high_bound: f32) -> f32 {
let data_points = 2 * correlation.len() / high_bound.ceil() as usize;
let range = high_bound - low_bound;
let midpoint = (low_bound + high_bound) / 2.0;
@@ -56,36 +56,37 @@ fn refine_fundamentals(correlation: &Vec<f32>, low_bound: f32, high_bound: f32)
midpoint
}
else {
- let low_guess = score_guess(&correlation, low_bound, data_points);
- let high_guess = score_guess(&correlation, high_bound, data_points);
+ let low_guess = score_guess(correlation, low_bound, data_points);
+ let high_guess = score_guess(correlation, high_bound, data_points);
if high_guess > low_guess {
- refine_fundamentals(&correlation, midpoint, high_bound)
+ refine_fundamentals(correlation, midpoint, high_bound)
}
else {
- refine_fundamentals(&correlation, low_bound, midpoint)
+ refine_fundamentals(correlation, low_bound, midpoint)
}
}
}
-fn is_noise(correlation: &Vec<f32>, fundamental: f32) -> bool {
- let value_at_point = interpolate(&correlation, fundamental);
+
+fn is_noise(correlation: &[f32], fundamental: f32) -> bool {
+ let value_at_point = interpolate(correlation, fundamental);
let score_data_points = 2 * correlation.len() / fundamental.ceil() as usize;
- let score = score_guess(&correlation, fundamental, score_data_points);
+ let score = score_guess(correlation, fundamental, score_data_points);
value_at_point > 2.0*score
}
-fn score_guess(correlation: &Vec<f32>, period: f32, data_points: usize) -> f32 {
+fn score_guess(correlation: &[f32], period: f32, data_points: usize) -> f32 {
(1..data_points).map(|i| {
let expected_sign = if i % 2 == 0 { 1.0 } else { -1.0 };
let x = i as f32 * period / 2.0;
let weight = 0.5 * i as f32;
- expected_sign * weight * interpolate(&correlation, x)
+ expected_sign * weight * interpolate(correlation, x)
}).sum()
}
-fn interpolate(correlation: &Vec<f32>, x: f32) -> f32 {
+fn interpolate(correlation: &[f32], x: f32) -> f32 {
if x < 0.0 {
correlation[0]
}
@@ -227,8 +228,8 @@ fn f5_is_correct() {
}
-pub fn align_to_rising_edge(samples: &Vec<f32>) -> Vec<f32> {
- remove_mean_offset(&samples)
+pub fn align_to_rising_edge(samples: &[f32]) -> Vec<f32> {
+ remove_mean_offset(samples)
.iter()
.skip_while(|x| !x.is_sign_negative())
.skip_while(|x| x.is_sign_negative())