From cc2668712870343af1901167799303a988a690a3 Mon Sep 17 00:00:00 2001 From: Elias Fleckenstein Date: Thu, 28 Jul 2022 21:46:07 +0200 Subject: [PATCH] Add options --- Cargo.lock | 133 ++++++++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 1 + README.md | 7 ++- src/main.rs | 46 ++++++++++++++---- 4 files changed, 176 insertions(+), 11 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 2432618..72c0449 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -24,6 +24,17 @@ dependencies = [ "pkg-config", ] +[[package]] +name = "atty" +version = "0.2.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8" +dependencies = [ + "hermit-abi", + "libc", + "winapi", +] + [[package]] name = "autocfg" version = "1.1.0" @@ -114,6 +125,45 @@ dependencies = [ "libloading", ] +[[package]] +name = "clap" +version = "3.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "44bbe24bbd31a185bc2c4f7c2abe80bea13a20d57ee4e55be70ac512bdc76417" +dependencies = [ + "atty", + "bitflags", + "clap_derive", + "clap_lex", + "indexmap", + "once_cell", + "strsim", + "termcolor", + "textwrap", +] + +[[package]] +name = "clap_derive" +version = "3.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9ba52acd3b0a5c33aeada5cdaa3267cdc7c594a98731d4268cdc1532f4264cb4" +dependencies = [ + "heck", + "proc-macro-error", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "clap_lex" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2850f2f5a82cbf437dd5af4d49848fbdfc27c157c3d010345776f952765261c5" +dependencies = [ + "os_str_bytes", +] + [[package]] name = "claxon" version = "0.4.3" @@ -227,6 +277,27 @@ version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9b919933a397b79c37e33b77bb2aa3dc8eb6e165ad809e58ff75bc7db2e34574" +[[package]] +name = "hashbrown" +version = "0.12.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888" + +[[package]] +name = "heck" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2540771e65fc8cb83cd6e8a237f70c319bd5c29f78ed1084ba5d50eeac86f7f9" + +[[package]] +name = "hermit-abi" +version = "0.1.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33" +dependencies = [ + "libc", +] + [[package]] name = "hound" version = "3.4.0" @@ -239,6 +310,16 @@ version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b9e0384b61958566e926dc50660321d12159025e767c18e043daf26b70104c39" +[[package]] +name = "indexmap" +version = "1.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "10a35a97730320ffe8e2d410b5d3b69279b98d2c14bdb8b70ea89ecf7888d41e" +dependencies = [ + "autocfg", + "hashbrown", +] + [[package]] name = "instant" version = "0.1.12" @@ -329,6 +410,7 @@ dependencies = [ name = "line_audiolizer" version = "0.1.0" dependencies = [ + "clap", "rodio", "unicode-width", ] @@ -560,6 +642,12 @@ version = "1.13.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "18a6dbe30758c9f83eb00cbea4ac95966305f5a7772f3f42ebfc7fc7eddbd8e1" +[[package]] +name = "os_str_bytes" +version = "6.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "648001efe5d5c0102d8cea768e348da85d90af8ba91f0bea908f157951493cd4" + [[package]] name = "parking_lot" version = "0.11.2" @@ -607,6 +695,30 @@ dependencies = [ "toml", ] +[[package]] +name = "proc-macro-error" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c" +dependencies = [ + "proc-macro-error-attr", + "proc-macro2", + "quote", + "syn", + "version_check", +] + +[[package]] +name = "proc-macro-error-attr" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869" +dependencies = [ + "proc-macro2", + "quote", + "version_check", +] + [[package]] name = "proc-macro2" version = "1.0.42" @@ -735,6 +847,21 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "termcolor" +version = "1.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bab24d30b911b2376f3a13cc2cd443142f0c81dda04c118693e35b3835757755" +dependencies = [ + "winapi-util", +] + +[[package]] +name = "textwrap" +version = "0.15.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b1141d4d61095b28419e22cb0bbf02755f5e54e0526f97f1e3d1d160e60885fb" + [[package]] name = "thiserror" version = "1.0.31" @@ -791,6 +918,12 @@ version = "0.1.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3ed742d4ea2bd1176e236172c8429aaf54486e7ac098db29ffe6529e0ce50973" +[[package]] +name = "version_check" +version = "0.9.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" + [[package]] name = "walkdir" version = "2.3.2" diff --git a/Cargo.toml b/Cargo.toml index b371ea8..f6dc363 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,3 +6,4 @@ edition = "2021" [dependencies] unicode-width = "0.1.9" rodio = "0.15.0" +clap = { version = "3.2.15", features = ["derive"] } diff --git a/README.md b/README.md index 89ea18a..767da30 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ # Line Audiolizer Turn lines of code (or text files) into sound, written in Rust. -Prints stdin, playing a tone for each line. The pitch of the tone depends on the length (or rather, UTF-8 width) of the line. Tabs count as 8 spaces. +Prints stdin, playing a sine wave tone for each line. The pitch of the tone depends on the length (or rather, UTF-8 width) of the line. Idea by [scplusplus](https://github.com/scplusplus). @@ -16,3 +16,8 @@ Fancy print while playing, using [batcat](https://github.com/sharkdp/bat): ```sh cargo run < src/main.rs | batcat --paging never --language rust ``` + +For available options, see: +```sh +cargo run -- --help +``` diff --git a/src/main.rs b/src/main.rs index 57271f5..262c822 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,30 +1,56 @@ +use std::{io::{self, Write}, time::Duration}; use unicode_width::UnicodeWidthStr; -use std::io::Write; +use rodio::{OutputStream, Sink, source::{SineWave, Source}}; +use clap::Parser; -use std::time::Duration; -use rodio::{OutputStream, Sink}; -use rodio::source::{SineWave, Source}; +#[derive(Parser, Debug)] +#[clap(version, about, long_about = None)] +struct Args { + /// How many spaces a tab character counts as + #[clap(short, long, value_parser, default_value_t = 4)] + tab_width: usize, + + /// Frequency of tones. Multiplied by line length + #[clap(short, long, value_parser, default_value_t = 20.0)] + frequency: f32, + + /// Duration of each tone, in seconds + #[clap(short, long, value_parser, default_value_t = 0.1)] + duration: f32, + + /// Amplifier of tones + #[clap(short, long, value_parser, default_value_t = 1.0)] + amplifier: f32, +} fn main() { + let args = Args::parse(); + let (_stream, stream_handle) = OutputStream::try_default().unwrap(); let sink = Sink::try_new(&stream_handle).unwrap(); - let mut stdout = std::io::stdout().lock(); + let mut stdout = io::stdout().lock(); - for line in std::io::stdin().lines() { + for line in io::stdin().lines() { let line: String = line .unwrap() .chars() - .map(|c| if c == '\t' { String::from(" ").repeat(8) } else { c.to_string() }) + .map(|c| + if c == '\t' { + String::from(" ").repeat(args.tab_width) + } else { + c.to_string() + } + ) .collect(); let len = UnicodeWidthStr::width(&*line) as f32; stdout.write_fmt(format_args!("{}\n", line)).unwrap(); - sink.append(SineWave::new(44.0 * len) - .take_duration(Duration::from_secs_f32(0.1)) - .amplify(0.20)); + sink.append(SineWave::new(args.frequency * len) + .take_duration(Duration::from_secs_f32(args.duration)) + .amplify(args.amplifier)); sink.sleep_until_end(); } } -- 2.44.0