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 --- Cargo.lock | 147 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-- Cargo.toml | 1 + src/main.rs | 32 +++++++------ 3 files changed, 163 insertions(+), 17 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ced3ae3..e44f546 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -218,6 +218,12 @@ version = "1.0.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" +[[package]] +name = "fuchsia-cprng" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a06f77d526c1a601b7c4cdd98f54b5eaabffc14d5f2f0296febdc7f357c6d3ba" + [[package]] name = "getrandom" version = "0.2.2" @@ -416,6 +422,42 @@ dependencies = [ "version_check", ] +[[package]] +name = "num" +version = "0.1.42" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4703ad64153382334aa8db57c637364c322d3372e097840c72000dabdcf6156e" +dependencies = [ + "num-bigint", + "num-complex", + "num-integer", + "num-iter", + "num-rational", + "num-traits", +] + +[[package]] +name = "num-bigint" +version = "0.1.44" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e63899ad0da84ce718c14936262a41cee2c79c981fc0a0e7c7beb47d5a07e8c1" +dependencies = [ + "num-integer", + "num-traits", + "rand 0.4.6", + "rustc-serialize", +] + +[[package]] +name = "num-complex" +version = "0.1.43" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b288631d7878aaf59442cffd36910ea604ecd7745c36054328595114001c9656" +dependencies = [ + "num-traits", + "rustc-serialize", +] + [[package]] name = "num-derive" version = "0.3.3" @@ -427,6 +469,39 @@ dependencies = [ "syn", ] +[[package]] +name = "num-integer" +version = "0.1.44" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d2cc698a63b549a70bc047073d2949cce27cd1c7b0a4a862d08a8031bc2801db" +dependencies = [ + "autocfg", + "num-traits", +] + +[[package]] +name = "num-iter" +version = "0.1.42" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b2021c8337a54d21aca0d59a92577a029af9431cb59b909b03252b9c164fad59" +dependencies = [ + "autocfg", + "num-integer", + "num-traits", +] + +[[package]] +name = "num-rational" +version = "0.1.42" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ee314c74bd753fc86b4780aa9475da469155f3848473a261d2d18e35245a784e" +dependencies = [ + "num-bigint", + "num-integer", + "num-traits", + "rustc-serialize", +] + [[package]] name = "num-traits" version = "0.2.14" @@ -512,6 +587,16 @@ version = "0.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "19b17cddbe7ec3f8bc800887bab5e717348c95ea2ca0b1bf0837fb964dc67099" +[[package]] +name = "pitch_calc" +version = "0.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "387005d7ff9e9970f954ffd33e258f9b755d5f27f11a4b57df3e5c6eab5a46f8" +dependencies = [ + "num", + "rand 0.3.23", +] + [[package]] name = "pkg-config" version = "0.3.19" @@ -551,6 +636,29 @@ dependencies = [ "proc-macro2", ] +[[package]] +name = "rand" +version = "0.3.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "64ac302d8f83c0c1974bf758f6b041c6c8ada916fbb44a609158ca8b064cc76c" +dependencies = [ + "libc", + "rand 0.4.6", +] + +[[package]] +name = "rand" +version = "0.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "552840b97013b1a26992c11eac34bdd778e464601a4c2054b5f0bff7c6761293" +dependencies = [ + "fuchsia-cprng", + "libc", + "rand_core 0.3.1", + "rdrand", + "winapi", +] + [[package]] name = "rand" version = "0.8.3" @@ -559,7 +667,7 @@ checksum = "0ef9e7e66b4468674bfcb0c81af8b7fa0bb154fa9f28eb840da5c447baeb8d7e" dependencies = [ "libc", "rand_chacha", - "rand_core", + "rand_core 0.6.2", "rand_hc", ] @@ -570,9 +678,24 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e12735cf05c9e10bf21534da50a147b924d555dc7a547c42e6bb2d5b6017ae0d" dependencies = [ "ppv-lite86", - "rand_core", + "rand_core 0.6.2", ] +[[package]] +name = "rand_core" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7a6fdeb83b075e8266dcc8762c22776f6877a63111121f5f8c7411e5be7eed4b" +dependencies = [ + "rand_core 0.4.2", +] + +[[package]] +name = "rand_core" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c33a3c44ca05fa6f1807d8e6743f3824e8509beca625669633be0acbdf509dc" + [[package]] name = "rand_core" version = "0.6.2" @@ -588,7 +711,16 @@ version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3190ef7066a446f2e7f42e239d161e905420ccab01eb967c9eb27d21b2322a73" dependencies = [ - "rand_core", + "rand_core 0.6.2", +] + +[[package]] +name = "rdrand" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "678054eb77286b51581ba43620cc911abf02758c91f93f479767aed0f90458b2" +dependencies = [ + "rand_core 0.3.1", ] [[package]] @@ -630,6 +762,12 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "08d43f7aa6b08d49f382cde6a7982047c3426db949b1424bc4b7ec9ae12c6ce2" +[[package]] +name = "rustc-serialize" +version = "0.3.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dcf128d1287d2ea9d80910b5f1120d0b8eede3fbf1abe91c40d39ea7d51e6fda" + [[package]] name = "same-file" version = "1.0.6" @@ -690,7 +828,8 @@ dependencies = [ name = "synthfinity" version = "0.1.0" dependencies = [ - "rand", + "pitch_calc", + "rand 0.8.3", "rodio", ] diff --git a/Cargo.toml b/Cargo.toml index b582bce..72b2c17 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,5 +7,6 @@ edition = "2018" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +pitch_calc = "0.12.0" rand = "0.8.3" rodio = { version = "0.13.1", default-features = false } 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