From fa5358cac652e7f72db6e1ce9656e7a3048ef80c Mon Sep 17 00:00:00 2001 From: Melody Horn Date: Sun, 9 May 2021 17:34:18 -0600 Subject: quantize pitch and length --- src/main.rs | 32 +++++++++++++++++++------------- 1 file changed, 19 insertions(+), 13 deletions(-) (limited to 'src') diff --git a/src/main.rs b/src/main.rs index ae044ae..2104d71 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,10 +1,8 @@ use std::time::Duration; -use rand::Rng; -use rodio::{ - source::{SineWave, Zero}, - OutputStream, Sink, Source, -}; +use pitch_calc::{Letter, LetterOctave}; +use rand::{prelude::*, Rng}; +use rodio::{source::SineWave, OutputStream, Sink, Source}; fn main() { let (_stream, stream_handle) = OutputStream::try_default().unwrap(); @@ -13,17 +11,25 @@ fn main() { let bpm = 131.2; let seconds_per_beat = (1.0 / bpm) * 60.0; + let mut rng = rand::thread_rng(); loop { - let mut rng = rand::thread_rng(); - let freq = (440.0 - * (2f32.powf(1.0 / 12.0)).powf(rng.gen_range((12 * -2)..=(12 * 0)) as f32)) - as u32; - let wave = SineWave::new(freq); - let zero: Zero = Zero::new(1, 48_000); + // This is bad. + let note = [2, 3] + .iter() + .flat_map(|&octave| { + use Letter::*; + [C, D, E, F, G, A, B] + .iter() + .map(move |&letter| LetterOctave(letter, octave)) + }) + .choose(&mut rng) + .expect("how is there not a note"); + let freq = note.hz(); + let wave = SineWave::new(freq as u32); let length = (rng.gen_range(1..4) as f32) / 4.0 * seconds_per_beat; let source = wave - .take_crossfade_with(zero, Duration::from_secs_f32(length)) - .amplify(0.20); + .take_duration(Duration::from_secs_f32(length)) + .amplify(0.10); sink.append(source); // The sound plays in a separate thread. This call will block the current thread until the sink -- cgit v1.2.3