clippy on popgen

This commit is contained in:
David Westgate 2024-12-11 06:11:13 -08:00
parent cc7fcd6761
commit 90da82a8ec
2 changed files with 30 additions and 40 deletions

1
.gitignore vendored
View File

@ -7,3 +7,4 @@ code/project/music/
code/project/neopixel/
code/project/radio/
code/project/speech/
*lock

View File

@ -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<u8>, beat_samples: u32, samplerate: u16) -> Vec<f32> {
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::<f32>() > 0.5 {
p = p + 1;
p += 1;
} else {
p = p - 1;
p = -1;
}
}
@ -131,12 +130,10 @@ 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 let Some(c) = it.next() {
if c == 'b' {
flat = true;
match it.next() {
Some(c) => {
if let Some(c) = it.next() {
if c == '[' {
octave = it.next().unwrap().to_digit(10).unwrap();
} else {
@ -144,8 +141,6 @@ fn parse_note(note_str: &str, regex: Regex) -> u32 {
std::process::exit(1)
}
}
None => {}
}
} else if c == '[' {
octave = it.next().unwrap().to_digit(10).unwrap();
} else {
@ -153,8 +148,6 @@ fn parse_note(note_str: &str, regex: Regex) -> u32 {
std::process::exit(1)
}
}
None => {}
}
// Different string format for flat vs not
let index: usize = match flat {
@ -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<f32>, samplerate: u16, gain: i8) {
// TODO: apply gain
fn play(samples: Vec<f32>, samplerate: u16, _gain: i8) {
// TODO: apply gain correctly
let (_stream, stream_handle) = OutputStream::try_default().unwrap();
let source: SamplesBuffer<f32> = 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<f32>, samplerate: u16, gain: i8) {
sink.sleep_until_end();
}
fn save(samples: Vec<f32>, samplerate: u16, output: &str, gain: i8) {
// TODO: apply gain
fn save(samples: Vec<f32>, 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);