diff --git a/Cargo.lock b/Cargo.lock index 8cc2a84..468748b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -98,7 +98,7 @@ checksum = "c6fa2087f2753a7da8cc1c0dbfcf89579dd57458e36769de5ac750b4671737ca" dependencies = [ "proc-macro2", "quote", - "syn 2.0.63", + "syn 2.0.64", ] [[package]] @@ -193,7 +193,7 @@ dependencies = [ "regex", "rustc-hash", "shlex", - "syn 2.0.63", + "syn 2.0.64", "which", ] @@ -474,9 +474,9 @@ dependencies = [ [[package]] name = "either" -version = "1.11.0" +version = "1.12.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a47c1c47d2f5964e29c61246e81db715514cd532db6b5116a25ea3c03d6780a2" +checksum = "3dca9240753cf90908d7e4aac30f630662b02aebaa1b58a3cadabdb23385b58b" [[package]] name = "encode_unicode" @@ -623,7 +623,7 @@ checksum = "87750cf4b7a4c0625b1529e4c543c2182106e4dedc60a2a6455e00d212c489ac" dependencies = [ "proc-macro2", "quote", - "syn 2.0.63", + "syn 2.0.64", ] [[package]] @@ -677,9 +677,9 @@ dependencies = [ [[package]] name = "getrandom" -version = "0.2.15" +version = "0.2.14" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c4567c8db10ae91089c99af84c68c38da3ec2f087c3f82960bcdbf3656b6f4d7" +checksum = "94b22e06ecb0110981051723910cbf0b5f5e09a2062dd7663334ee79a9d1286c" dependencies = [ "cfg-if", "libc", @@ -1028,9 +1028,9 @@ dependencies = [ [[package]] name = "libc" -version = "0.2.154" +version = "0.2.153" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ae743338b92ff9146ce83992f766a31066a91a8c84a45e0e9f21e7cf6de6d346" +checksum = "9c198f91728a82281a64e1f4f9eeb25d82cb32a5de251c6bd1b5154d63a8e7bd" [[package]] name = "libloading" @@ -1256,9 +1256,9 @@ dependencies = [ [[package]] name = "linux-raw-sys" -version = "0.4.13" +version = "0.4.14" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "01cda141df6706de531b6c46c3a33ecca755538219bd484262fa09410c13539c" +checksum = "78b3ae25bc7c8c38cec158d1f2757ee79e9b3740fbc7ccf0e59e4b08d793fa89" [[package]] name = "lock_api" @@ -1626,7 +1626,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5f12335488a2f3b0a83b14edad48dca9879ce89b2edd10e80237e4e852dd645e" dependencies = [ "proc-macro2", - "syn 2.0.63", + "syn 2.0.64", ] [[package]] @@ -1905,9 +1905,9 @@ dependencies = [ [[package]] name = "rustversion" -version = "1.0.16" +version = "1.0.17" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "092474d1a01ea8278f69e6a358998405fae5b8b963ddaeb2b0b04a128bf1dfb0" +checksum = "955d28af4278de8121b7ebeb796b6a45735dc01436d898801014aced2773a3d6" [[package]] name = "ryu" @@ -1935,22 +1935,22 @@ checksum = "a3f0bf26fd526d2a95683cd0f87bf103b8539e2ca1ef48ce002d67aad59aa0b4" [[package]] name = "serde" -version = "1.0.201" +version = "1.0.202" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "780f1cebed1629e4753a1a38a3c72d30b97ec044f0aef68cb26650a3c5cf363c" +checksum = "226b61a0d411b2ba5ff6d7f73a476ac4f8bb900373459cd00fab8512828ba395" dependencies = [ "serde_derive", ] [[package]] name = "serde_derive" -version = "1.0.201" +version = "1.0.202" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c5e405930b9796f1c00bee880d03fc7e0bb4b9a11afc776885ffe84320da2865" +checksum = "6048858004bcff69094cd972ed40a32500f153bd3be9f716b2eed2e8217c4838" dependencies = [ "proc-macro2", "quote", - "syn 2.0.63", + "syn 2.0.64", ] [[package]] @@ -2077,11 +2077,11 @@ dependencies = [ "librespot", "machine-uid", "mp3lame-encoder", + "rayon", "regex", "rpassword 7.3.1", "structopt", "tokio", - "tokio-rayon", "tracing", "tracing-subscriber", ] @@ -2135,9 +2135,9 @@ dependencies = [ [[package]] name = "syn" -version = "2.0.63" +version = "2.0.64" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bf5be731623ca1a1fb7d8be6f261a3be6d3e2337b8a1f97be944d020c8fcb704" +checksum = "7ad3dee41f36859875573074334c200d1add8e4a87bb37113ebd31d926b7b11f" dependencies = [ "proc-macro2", "quote", @@ -2191,7 +2191,7 @@ checksum = "e2470041c06ec3ac1ab38d0356a6119054dedaea53e12fbefc0de730a1c08524" dependencies = [ "proc-macro2", "quote", - "syn 2.0.63", + "syn 2.0.64", ] [[package]] @@ -2257,17 +2257,7 @@ checksum = "5b8a1e28f2deaa14e508979454cb3a223b10b938b45af148bc0986de36f1923b" dependencies = [ "proc-macro2", "quote", - "syn 2.0.63", -] - -[[package]] -name = "tokio-rayon" -version = "2.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7cf33a76e0b1dd03b778f83244137bd59887abf25c0e87bc3e7071105f457693" -dependencies = [ - "rayon", - "tokio", + "syn 2.0.64", ] [[package]] @@ -2319,7 +2309,7 @@ checksum = "34704c8d6ebcbc939824180af020566b01a7c01f80641264eba0999f6c2b6be7" dependencies = [ "proc-macro2", "quote", - "syn 2.0.63", + "syn 2.0.64", ] [[package]] @@ -2485,7 +2475,7 @@ dependencies = [ "once_cell", "proc-macro2", "quote", - "syn 2.0.63", + "syn 2.0.64", "wasm-bindgen-shared", ] @@ -2507,7 +2497,7 @@ checksum = "e94f17b526d0a461a191c78ea52bbce64071ed5c04c9ffe424dcb38f74171bb7" dependencies = [ "proc-macro2", "quote", - "syn 2.0.63", + "syn 2.0.64", "wasm-bindgen-backend", "wasm-bindgen-shared", ] @@ -2737,5 +2727,5 @@ checksum = "125139de3f6b9d625c39e2efdd73d41bdac468ccd556556440e322be0e1bbd91" dependencies = [ "proc-macro2", "quote", - "syn 2.0.63", + "syn 2.0.64", ] diff --git a/Cargo.toml b/Cargo.toml index 3572809..c7aef6d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -26,8 +26,8 @@ lazy_static = "1.4" async-trait = "0.1" dirs = "5.0" mp3lame-encoder = { version = "0.1.5", optional = true } -tokio-rayon = "2.1" futures = "0.3" +rayon = "1.10" [features] default = ["mp3"] diff --git a/src/download.rs b/src/download.rs index de909a0..23eb7f3 100644 --- a/src/download.rs +++ b/src/download.rs @@ -132,7 +132,7 @@ impl Downloader { tracing::info!("Encoding track: {:?}", file_name); pb.set_message(format!("Encoding {}", &file_name)); let samples = Samples::new(samples, 44100, 2, 16); - let encoder = crate::encoder::get_encoder(options.format)?; + let encoder = crate::encoder::get_encoder(options.format); let stream = encoder.encode(samples).await?; pb.set_message(format!("Writing {}", &file_name)); diff --git a/src/encoder/flac.rs b/src/encoder/flac.rs index 3b463ca..50de691 100644 --- a/src/encoder/flac.rs +++ b/src/encoder/flac.rs @@ -10,12 +10,6 @@ use super::Samples; #[derive(Debug)] pub struct FlacEncoder; -impl FlacEncoder { - pub fn new() -> anyhow::Result { - Ok(Self) - } -} - #[async_trait::async_trait] impl Encoder for FlacEncoder { async fn encode(&self, samples: Samples) -> anyhow::Result { diff --git a/src/encoder/mod.rs b/src/encoder/mod.rs index ffb82cd..9e44278 100644 --- a/src/encoder/mod.rs +++ b/src/encoder/mod.rs @@ -5,9 +5,10 @@ mod mp3; use std::{path::Path, str::FromStr}; use anyhow::Result; -use lazy_static::lazy_static; use tokio::sync::oneshot::Sender; +use self::{flac::FlacEncoder, mp3::Mp3Encoder}; + #[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)] pub enum Format { Flac, @@ -40,18 +41,15 @@ impl Format { } -lazy_static!( - static ref FLAC_ENCODER : Box = Box::new(flac::FlacEncoder::new().unwrap()); - #[cfg(feature = "mp3")] - static ref MP3_ENCODER : Box = Box::new(mp3::Mp3Encoder {}); -); +const FLAC_ENCODER: &FlacEncoder = &FlacEncoder; +#[cfg(feature = "mp3")] +const MP3_ENCODER: &Mp3Encoder = &Mp3Encoder; -pub fn get_encoder(format: Format) -> anyhow::Result<&'static Box> { +pub fn get_encoder(format: Format) -> &'static dyn Encoder { match format { - Format::Flac => Ok(&FLAC_ENCODER), + Format::Flac => FLAC_ENCODER, #[cfg(feature = "mp3")] - Format::Mp3 => Ok(&MP3_ENCODER), - _ => Err(anyhow::anyhow!("Unsupported format")), + Format::Mp3 => MP3_ENCODER, } } diff --git a/src/file_sink.rs b/src/file_sink.rs new file mode 100644 index 0000000..395ec2c --- /dev/null +++ b/src/file_sink.rs @@ -0,0 +1,146 @@ +use std::path::Path; + +use audiotags::Tag; +use audiotags::TagType; +use flacenc::component::BitRepr; +use flacenc::error::Verify; +use librespot::playback::audio_backend::Sink; +use librespot::playback::audio_backend::SinkError; +use librespot::playback::convert::Converter; +use librespot::playback::decoder::AudioPacket; + +use crate::encoder::get_encoder; +use crate::encoder::Samples; +use crate::track::TrackMetadata; + +pub enum SinkEvent { + Written { bytes: usize, total: usize }, + Finished, +} +pub type SinkEventChannel = tokio::sync::mpsc::UnboundedReceiver; + +pub struct FileSink { + sink: String, + content: Vec, + metadata: TrackMetadata, + compression: u32, + event_sender: tokio::sync::mpsc::UnboundedSender, +} + +impl FileSink { + pub fn set_compression(&mut self, compression: u32) { + self.compression = compression; + } + + pub fn new(path: String, track: TrackMetadata) -> (Self, SinkEventChannel) { + let (tx, rx) = tokio::sync::mpsc::unbounded_channel(); + + ( + FileSink { + sink: path, + content: Vec::new(), + metadata: track, + compression: 4, + event_sender: tx, + }, + rx, + ) + } + + pub fn get_approximate_size(&self) -> usize { + self.convert_track_duration_to_size() + } + + fn convert_track_duration_to_size(&self) -> usize { + let duration = self.metadata.duration / 1000; + let sample_rate = 44100; + let channels = 2; + let bits_per_sample = 16; + let bytes_per_sample = bits_per_sample / 8; + (duration as usize) * sample_rate * channels * bytes_per_sample * 2 + } +} + +impl Sink for FileSink { + fn start(&mut self) -> Result<(), SinkError> { + Ok(()) + } + + fn stop(&mut self) -> Result<(), SinkError> { + tracing::info!("Writing to file: {:?}", &self.sink); + + // let config = flacenc::config::Encoder::default() + // .into_verified() + // .map_err(|_| SinkError::OnWrite("Failed to create flac encoder".to_string()))?; + // let source = flacenc::source::MemSource::from_samples(&self.content, 2, 16, 44100); + // let flac_stream = flacenc::encode_with_fixed_block_size(&config, source, config.block_size) + // .map_err(|_| SinkError::OnWrite("Failed to encode flac".to_string()))?; + // let mut sink = flacenc::bitsink::ByteSink::new(); + // flac_stream + // .write(&mut sink) + // .map_err(|_| SinkError::OnWrite("Failed to write flac to sink".to_string()))?; + // std::fs::write(&self.sink, sink.as_slice()) + // .map_err(|_| SinkError::OnWrite("Failed to write flac to file".to_string()))?; + let flac_enc = get_encoder(crate::encoder::Format::Flac) + .map_err(|_| SinkError::OnWrite("Failed to get flac encoder".to_string()))?; + let mp3_enc = get_encoder(crate::encoder::Format::Mp3) + .map_err(|_| SinkError::OnWrite("Failed to get mp3 encoder".to_string()))?; + + let flac_stream = flac_enc.encode(Samples::new( + self.content.clone(), + 44100, + 2, + 16, + )) + .map_err(|_| SinkError::OnWrite("Failed to encode flac".to_string()))?; + let mp3_stream = mp3_enc.encode(Samples::new( + self.content.clone(), + 44100, + 2, + 16, + )) + .map_err(|_| SinkError::OnWrite("Failed to encode mp3".to_string()))?; + + flac_stream.write_to_file(&self.sink) + .map_err(|_| SinkError::OnWrite("Failed to write flac to file".to_string()))?; + mp3_stream.write_to_file(&self.sink) + .map_err(|_| SinkError::OnWrite("Failed to write mp3 to file".to_string()))?; + + let mut tag = Tag::new() + .with_tag_type(TagType::Flac) + .read_from_path(Path::new(&self.sink)) + .map_err(|_| SinkError::OnWrite("Failed to read metadata".to_string()))?; + + tag.set_album_title(&self.metadata.album.name); + for artist in &self.metadata.artists { + tag.add_artist(&artist.name); + } + tag.set_title(&self.metadata.track_name); + tag.write_to_path(&self.sink) + .map_err(|_| SinkError::OnWrite("Failed to write metadata".to_string()))?; + + self.event_sender + .send(SinkEvent::Finished) + .map_err(|_| SinkError::OnWrite("Failed to send finished event".to_string()))?; + Ok(()) + } + + fn write(&mut self, packet: AudioPacket, converter: &mut Converter) -> Result<(), SinkError> { + let data = converter.f64_to_s16( + packet + .samples() + .map_err(|_| SinkError::OnWrite("Failed to get samples".to_string()))?, + ); + let mut data32: Vec = data.iter().map(|el| i32::from(*el)).collect(); + self.content.append(&mut data32); + + self.event_sender + .send(SinkEvent::Written { + bytes: self.content.len() * std::mem::size_of::(), + total: self.convert_track_duration_to_size(), + }) + .map_err(|_| SinkError::OnWrite("Failed to send event".to_string()))?; + + Ok(()) + } +}