From 90da82a8ec8d2aca72c606a72b5955f1a770d82c Mon Sep 17 00:00:00 2001 From: David Westgate Date: Wed, 11 Dec 2024 06:11:13 -0800 Subject: [PATCH] clippy on popgen --- .gitignore | 3 +- code/popgen/src/popgen.rs | 67 ++++++++++++++++----------------------- 2 files changed, 30 insertions(+), 40 deletions(-) diff --git a/.gitignore b/.gitignore index 60584b3..2fa087e 100644 --- a/.gitignore +++ b/.gitignore @@ -6,4 +6,5 @@ code/project/microbit/ code/project/music/ code/project/neopixel/ code/project/radio/ -code/project/speech/ \ No newline at end of file +code/project/speech/ +*lock \ No newline at end of file diff --git a/code/popgen/src/popgen.rs b/code/popgen/src/popgen.rs index 9a27fa2..3b9234b 100644 --- a/code/popgen/src/popgen.rs +++ b/code/popgen/src/popgen.rs @@ -4,7 +4,6 @@ // https://github.com/pdx-cs-sound/popgen/blob/main/popgen.py use clap::Parser; -use hound; use rand::Rng; use regex::Regex; use rodio::{buffer::SamplesBuffer, OutputStream, Sink}; @@ -33,12 +32,12 @@ const CHORD_LOOP: [u8; 4] = [8, 5, 6, 4]; // Given a MIDI key number and an optional number of beats of // note duration, return a sine wave for that note. fn make_note(key: i8, n: Option, beat_samples: u32, samplerate: u16) -> Vec { - let num_beats: f32 = n.unwrap_or(1).try_into().unwrap(); - let samplerate_f: f32 = samplerate.try_into().unwrap(); + let num_beats: f32 = n.unwrap_or(1).into(); + let samplerate_f: f32 = samplerate.into(); let beatsamples_f: f32 = beat_samples as f32; - let key_f: f32 = key.try_into().unwrap(); + let key_f: f32 = key.into(); let f: f32 = 440f32 * (2f32).powf((key_f - 69f32) / 12f32); - let b: u32 = (beat_samples as u32) * (n.unwrap_or(1) as u32); + let b: u32 = (beat_samples) * (n.unwrap_or(1) as u32); let b_f: f32 = beatsamples_f * num_beats; let cycles: f32 = 2f32 * PI * f * b_f / samplerate_f; @@ -103,9 +102,9 @@ fn pick_notes(chord_root: u8, position: &mut i8, rng: &mut rand::prelude::Thread let chord_note: i8 = note_to_key_offset((chord_root as i8) + chord_note_offset); *note = chord_note; if rng.gen::() > 0.5 { - p = p + 1; + p += 1; } else { - p = p - 1; + p = -1; } } @@ -131,29 +130,23 @@ fn parse_note(note_str: &str, regex: Regex) -> u32 { let base_note: char = it.next().unwrap(); let mut flat: bool = false; // Logic to Handle all valid cases, like C, C[4], Db, Db[6], etc. - match it.next() { - Some(c) => { - if c == 'b' { - flat = true; - match it.next() { - Some(c) => { - if c == '[' { - octave = it.next().unwrap().to_digit(10).unwrap(); - } else { - eprintln!("Invalid Notation: {}", note_str); - std::process::exit(1) - } - } - None => {} + if let Some(c) = it.next() { + if c == 'b' { + flat = true; + if let Some(c) = it.next() { + if c == '[' { + octave = it.next().unwrap().to_digit(10).unwrap(); + } else { + eprintln!("Invalid Notation: {}", note_str); + std::process::exit(1) } - } else if c == '[' { - octave = it.next().unwrap().to_digit(10).unwrap(); - } else { - eprintln!("Invalid Notation: {}", note_str); - std::process::exit(1) } + } else if c == '[' { + octave = it.next().unwrap().to_digit(10).unwrap(); + } else { + eprintln!("Invalid Notation: {}", note_str); + std::process::exit(1) } - None => {} } // Different string format for flat vs not @@ -164,7 +157,7 @@ fn parse_note(note_str: &str, regex: Regex) -> u32 { .unwrap(); let index_32: u32 = index as u32; let r: u32 = index_32 + (12 * octave); - return r; + r } #[derive(Parser, Debug)] @@ -188,13 +181,14 @@ struct Args { #[arg(short, long, default_value_t = -3)] gain: i8, - #[arg(short, long, default_value_t = NO_OUTPUT.to_string())] // Ideally there is a better way than this + // Ideally there would be a better way than this + #[arg(short, long, default_value_t = NO_OUTPUT.to_string())] output: String, } // Rodio example https://github.com/RustAudio/rodio/blob/master/examples/basic.rs -fn play(samples: Vec, samplerate: u16, gain: i8) { - // TODO: apply gain +fn play(samples: Vec, samplerate: u16, _gain: i8) { + // TODO: apply gain correctly let (_stream, stream_handle) = OutputStream::try_default().unwrap(); let source: SamplesBuffer = SamplesBuffer::new(1, samplerate as u32, samples); let sink: Sink = Sink::try_new(&stream_handle).unwrap(); //sink makes it easy to keep the program asleep so the whole song plays @@ -202,8 +196,8 @@ fn play(samples: Vec, samplerate: u16, gain: i8) { sink.sleep_until_end(); } -fn save(samples: Vec, samplerate: u16, output: &str, gain: i8) { - // TODO: apply gain +fn save(samples: Vec, samplerate: u16, output: &str, _gain: i8) { + // TODO: apply gain correctly let spec: hound::WavSpec = hound::WavSpec { channels: 1, sample_rate: samplerate as u32, @@ -362,12 +356,7 @@ fn test() { Some(1), 288000, 48_000, - [ - 0.0, - 0.017122516431822686, - 0.034240012506541136, - 0.05134746933903016, - ], + [0.0, 0.017122516431822686, 0.034240012506541136, 0.051347464], )]; for (i, (key, n, beat_samples, samplerate, results)) in make_notes_tests.iter().enumerate() { let wave = make_note(*key, *n, *beat_samples, *samplerate);