diff options
author | Melody Horn <melody@boringcactus.com> | 2021-05-09 17:34:18 -0600 |
---|---|---|
committer | Melody Horn <melody@boringcactus.com> | 2021-05-09 17:34:18 -0600 |
commit | fa5358cac652e7f72db6e1ce9656e7a3048ef80c (patch) | |
tree | bac3a0875b6f56b73874bcbe71f4e110d8a4f4b5 /src | |
parent | af6abc7bd727d31a98a82ea7cc83e14405bad1a5 (diff) | |
download | synthfinity-fa5358cac652e7f72db6e1ce9656e7a3048ef80c.tar.gz synthfinity-fa5358cac652e7f72db6e1ce9656e7a3048ef80c.zip |
quantize pitch and length
Diffstat (limited to 'src')
-rw-r--r-- | src/main.rs | 32 |
1 files changed, 19 insertions, 13 deletions
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<f32> = 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 |