Compare commits

...

2 Commits

Author SHA1 Message Date
Emi Simpson b9883d2ad1
Run song length calculations asynchronously 2022-01-05 15:42:12 -05:00
Emi Simpson 7e64590279
Merge peripheries into the editor 2022-01-05 15:26:06 -05:00
8 changed files with 139 additions and 137 deletions

1
Cargo.lock generated
View File

@ -790,6 +790,7 @@ dependencies = [
name = "delyrium" name = "delyrium"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"blocking",
"exoquant", "exoquant",
"iced", "iced",
"iced_futures", "iced_futures",

View File

@ -19,6 +19,10 @@ iced_native = "0.4.0"
# Native file dialogs # Native file dialogs
rfd = "0.6.3" rfd = "0.6.3"
# Run long-running blocking tasks in another thread
# Also used by iced, so we share a threadpool
blocking = "1.1.0"
[dependencies.symphonia] [dependencies.symphonia]
# Music decoding and metadata parsing # Music decoding and metadata parsing
features = ["isomp4", "aac", "mp3"] features = ["isomp4", "aac", "mp3"]

View File

@ -91,7 +91,9 @@ impl Application for DelyriumApp {
Message::FileOpened(path) => { Message::FileOpened(path) => {
println!("File opened! {}", path.display()); println!("File opened! {}", path.display());
let song = load_song(&path).unwrap().format; let song = load_song(&path).unwrap().format;
self.lyrics_component = Some(Editor::new(song, self.size)); let (editor, cmd) = Editor::new(song, self.size);
self.lyrics_component = Some(editor);
command = Some(cmd);
}, },
Message::PromptForFile => { Message::PromptForFile => {
let task = async { let task = async {

View File

@ -1,3 +1,5 @@
use iced::Command;
use core::time::Duration;
use iced::Length; use iced::Length;
use iced::Color; use iced::Color;
use symphonia::core::formats::FormatReader; use symphonia::core::formats::FormatReader;
@ -14,11 +16,13 @@ use iced::canvas::Program;
use iced::Canvas; use iced::Canvas;
use iced::mouse::{self, Button}; use iced::mouse::{self, Button};
use crate::player::Player; use crate::player::Player;
use blocking::unblock;
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
pub enum ControlsEvent { pub enum ControlsEvent {
SeekPosition(f32), SeekPosition(f32),
TogglePlay, TogglePlay,
DurationDiscovered(Duration),
} }
enum ErrorState { enum ErrorState {
@ -36,20 +40,26 @@ pub struct Controls {
} }
impl Controls { impl Controls {
pub fn new(song: Box<dyn FormatReader>) -> Self { pub fn new(song: Box<dyn FormatReader>) -> (Self, Command<Message>) {
match Player::new(song) { match Player::new(song) {
Ok(player) => { Ok(player) => {
Controls { let duration_task = unblock(player.compute_duration());
let duration_cmd = Command::perform(
duration_task,
|d| Message::ControlsEvent(ControlsEvent::DurationDiscovered(d)),
);
(Controls {
error_state: NoError { error_state: NoError {
has_device: player.has_output_device(), has_device: player.has_output_device(),
player, player,
} }
} }, duration_cmd)
}, },
Err(e) => { Err(e) => {
Controls { (Controls {
error_state: Error(e) error_state: Error(e)
} }, Command::none())
} }
} }
} }
@ -65,6 +75,11 @@ impl Controls {
let result = match event { let result = match event {
ControlsEvent::SeekPosition(pos) => player.seek_percentage(pos), ControlsEvent::SeekPosition(pos) => player.seek_percentage(pos),
ControlsEvent::TogglePlay => player.toggle_play(), ControlsEvent::TogglePlay => player.toggle_play(),
ControlsEvent::DurationDiscovered(d) => {
player.set_duration(d);
println!("Found duration! {:?}", d);
Ok(player.has_output_device())
},
}; };
match result { match result {

View File

@ -1,3 +1,7 @@
use iced::Command;
use iced::Image;
use iced::image::Handle;
use image::DynamicImage;
use crate::controls::ControlsEvent; use crate::controls::ControlsEvent;
use crate::controls::Controls; use crate::controls::Controls;
use crate::lyrics::Lyric; use crate::lyrics::Lyric;
@ -5,14 +9,12 @@ use crate::lyrics::Lyrics;
use crate::app::Message; use crate::app::Message;
use iced::Element; use iced::Element;
use crate::styles::Theme; use crate::styles::Theme;
use std::sync::Arc;
use iced::Container; use iced::Container;
use iced::Row; use iced::Row;
use crate::palette::Palette; use crate::palette::Palette;
use crate::load_song::extract_cover; use crate::load_song::extract_cover;
use crate::peripheries::Periphery;
use iced::Length; use iced::Length;
use iced::Align; use image::imageops::FilterType;
use symphonia::core::formats::FormatReader; use symphonia::core::formats::FormatReader;
@ -21,12 +23,13 @@ pub struct Editor {
lyrics: Lyrics, lyrics: Lyrics,
theme: Theme, theme: Theme,
controls: Controls, controls: Controls,
left_peri: Periphery, bg_img: DynamicImage,
rite_peri: Periphery, cached_resized_bg: Option<Handle>,
dimensions: (u32, u32),
} }
impl Editor { impl Editor {
pub fn new(mut song: Box<dyn FormatReader>, size: (u32, u32)) -> Self { pub fn new(mut song: Box<dyn FormatReader>, size: (u32, u32)) -> (Self, Command<Message>) {
let cover = extract_cover(song.as_mut()); let cover = extract_cover(song.as_mut());
let theme = cover.as_ref() let theme = cover.as_ref()
@ -37,17 +40,18 @@ impl Editor {
}).unwrap_or_else(|| Theme::default()); }).unwrap_or_else(|| Theme::default());
let cover = cover.expect("TODO"); let cover = cover.expect("TODO");
let cover = Arc::new(cover.blur((cover.width() / 50) as f32)); let bg_img = DynamicImage::ImageBgra8(
cover.blur((cover.width() / 50) as f32).into_bgra8()
);
let left_peri = Periphery::new(cover.clone(), true, size); let (controls, cmd) = Controls::new(song);
let rite_peri = Periphery::new(cover, false, size);
let controls = Controls::new(song); (Self {
Self {
lyrics: Lyrics::new(), lyrics: Lyrics::new(),
controls, theme, left_peri, rite_peri, dimensions: size,
} cached_resized_bg: None,
controls, theme, bg_img,
}, cmd)
} }
// TODO: work on untangling this mess // TODO: work on untangling this mess
@ -71,23 +75,58 @@ impl Editor {
self.controls.is_playing() self.controls.is_playing()
} }
fn calculate_margin_width(&self) -> u32 {
let (w, _h) = self.dimensions;
let body_size = (w / 5).max(450);
w.saturating_sub(body_size) / 2
}
pub fn notify_resized(&mut self, w: u32, h: u32) { pub fn notify_resized(&mut self, w: u32, h: u32) {
self.left_peri.notify_resized(w, h); self.dimensions = (w, h);
self.rite_peri.notify_resized(w, h);
let (_w, h) = self.dimensions;
let margin_w = self.calculate_margin_width();
self.cached_resized_bg = if margin_w != 0 {
let resized_bg = self.bg_img.resize_to_fill(margin_w, h, FilterType::Nearest);
Some(Handle::from_pixels(
resized_bg.width(),
resized_bg.height(),
resized_bg.into_bgra8().into_raw()
))
} else { None };
} }
pub fn view(&mut self) -> Element<Message> { pub fn view(&mut self) -> Element<Message> {
Container::new( let row = if let Some(margin_bg) = &self.cached_resized_bg {
let (w, _h) = self.dimensions;
let (img1, img2) = (
Image::new(margin_bg.clone())
.width(Length::Units(w as u16))
.height(Length::Fill),
Image::new(margin_bg.clone())
.width(Length::Units(w as u16))
.height(Length::Fill),
);
Row::new() Row::new()
.push(self.left_peri.view()) .push(img1)
.push(self.controls.view_progress(self.theme)) .push(self.controls.view_progress(self.theme))
.push(self.lyrics.view(self.theme)) .push(self.lyrics.view(self.theme))
.push(self.rite_peri.view()) .push(img2)
) } else {
.align_y(Align::Center) Row::new()
.style(self.theme) .push(self.controls.view_progress(self.theme))
.height(Length::Fill) .push(self.lyrics.view(self.theme))
.into() };
Container::new(row)
.style(self.theme)
.height(Length::Fill)
.into()
} }
} }

View File

@ -7,7 +7,6 @@ mod lyrics;
mod styles; mod styles;
mod file_select; mod file_select;
mod load_song; mod load_song;
mod peripheries;
mod editor; mod editor;
mod player; mod player;
mod controls; mod controls;

View File

@ -1,92 +0,0 @@
use image::imageops::crop_imm;
use image::ImageBuffer;
use image::Bgra;
use iced::Length;
use iced::Element;
use crate::app::Message;
use std::sync::Arc;
use iced::Image;
use image::imageops::FilterType;
use image::DynamicImage;
use image::GenericImageView;
use iced::widget::image::Handle;
#[derive(Clone, Debug)]
pub struct Periphery {
left: bool,
source_image: Arc<DynamicImage>,
upscaled: (u32, ImageBuffer<Bgra<u8>, Vec<u8>>),
image: Handle,
w: u16,
h: u16,
}
impl Periphery {
pub fn new(source_image: Arc<DynamicImage>, left: bool, (w, h): (u32, u32)) -> Self {
let mut p = Periphery {
image: Handle::from_pixels(0, 0, Vec::new()),
upscaled: (0, ImageBuffer::<Bgra<u8>, Vec<u8>>::from_raw(0, 0, Vec::new()).unwrap()),
w: 0, h: 0,
left, source_image
};
p.notify_resized(w, h);
p
}
pub fn notify_resized(&mut self, w: u32, h: u32) {
let body_width = (w / 5).max(450);
let w = w.saturating_sub(body_width) / 2;
self.w = w as u16;
self.h = h as u16;
if w == 0 {
return;
}
let h_scale_needed = w as f32 / self.source_image.width() as f32;
let v_scale_needed = h as f32 / self.source_image.height() as f32;
let scale = f32::max(h_scale_needed, v_scale_needed);
let nh = (scale * (self.source_image.height() as f32)).ceil() as u32;
let nw = (scale * (self.source_image.width() as f32)).ceil() as u32;
self.upscaled = (
w,
self.source_image.resize(nw, nh, FilterType::Nearest)
.into_bgra8()
);
let ratio = w as f32 / h as f32;
let source_ratio = self.source_image.width() as f32 / self.source_image.height() as f32;
let (nw, nh) = if ratio < source_ratio {
(
(self.upscaled.1.height() as f32 * ratio) as u32,
self.upscaled.1.height()
)
} else {
(
self.upscaled.1.width(),
(self.upscaled.1.width() as f32 / ratio) as u32
)
};
let h_shift_mul = if self.left { -1 } else { 1 };
let image = crop_imm(
&self.upscaled.1,
(((self.upscaled.1.width() / 2) as i32 + h_shift_mul * (nw as i32)).max(0) as u32).min(self.upscaled.1.width() - nw),
self.upscaled.1.height().saturating_sub(nh) / 2,
w, h,
);
self.image = Handle::from_pixels(image.width(), image.height(), image.to_image().into_raw());
}
pub fn view(&mut self) -> Element<Message> {
Image::new(self.image.clone())
.width(Length::Units(self.w))
.height(Length::Fill)
.into()
}
}

View File

@ -29,13 +29,18 @@ pub struct Player {
impl Player { impl Player {
/// Create a new player from a song /// Create a new player from a song
///
/// IMPORTANT: Because computing the duration of a song is currently a very expensive
/// task, this DOES NOT COMPUTE THE DURATION. All calculations that require a
/// duration assume a duration of 2:30s until a duration is manually calculated with
/// [`compute_duration()`] AND passed back into [`set_duration()`].
pub fn new(song: Box<dyn FormatReader>) -> Result<Self, PlayerError> { pub fn new(song: Box<dyn FormatReader>) -> Result<Self, PlayerError> {
let song = Decoder::new_from_format_reader(song) let song = Decoder::new_from_format_reader(song)
.map_err(PlayerError::DecoderError)? .map_err(PlayerError::DecoderError)?
.buffered(); .buffered();
let duration = get_duration_of_song(song.clone()); let duration = Duration::from_secs(150);
let mut player = Player { let mut player = Player {
sink: None, sink: None,
@ -221,20 +226,49 @@ impl Player {
// nightly: self.position().div_duration_f32(self.duration) // nightly: self.position().div_duration_f32(self.duration)
self.position().as_secs_f32() / self.duration.as_secs_f32() self.position().as_secs_f32() / self.duration.as_secs_f32()
} }
}
/// Manually calculate the exact length of a given song /// Computes the exact duration of the current song.
/// ///
/// This is really expensive, and involves decoding and inefficiently counting the number /// This is an EXPENSIVE, BLOCKING long-running function that should be run in a
/// of samples in the song, but produces an exact output. Use sparingly. /// seperate thread. Hopefully this will be optimized in the future, but this is what
fn get_duration_of_song(song: Song) -> Duration { /// we have for now.
let sample_rate = song.sample_rate() as u64; ///
let n_channels = song.channels() as u64; /// This does not set the duration of this song, so be sure to call [`set_duration()`]
let n_samples = song.count() as u64; // expensive! /// with the result of this call after it finishes executing.
let n_whole_seconds = n_samples / n_channels / sample_rate; ///
let remaining_samples = n_samples % sample_rate; /// Note: This doesn't actually preform the calculation, but instead returns a
let n_nanos = remaining_samples * 1_000_000_000 / sample_rate; /// `'static FnOnce` that can be used to do so, in order to simplify running the
Duration::new(n_whole_seconds, n_nanos as u32) /// transaction in another thread.
///
/// Technical Details: Currently, this involves decoding and inefficiently counting
/// the number of samples in the song. Hopefully, we'll be able to do this more
/// efficiently in the future, pending mostly on
/// https://github.com/RustAudio/rodio/issues/405
pub fn compute_duration(&self) -> impl FnOnce() -> Duration {
let sample_rate = self.song.sample_rate() as u64;
let n_channels = self.song.channels() as u64;
let song_clone = self.song.clone();
move|| {
let n_samples = song_clone.count() as u64; // expensive!
let n_whole_seconds = n_samples / n_channels / sample_rate;
let remaining_samples = n_samples % sample_rate;
let n_nanos = remaining_samples * 1_000_000_000 / sample_rate;
Duration::new(n_whole_seconds, n_nanos as u32)
}
}
/// Set the duration of the song
///
/// This struct does not automatically compute its own duration, so until this method
/// is called, we just assume the duration is 2:30s for any calculations that require
/// it.
///
/// In order to use an exact duration, use another thread to run
/// [`compute_duration()`] and pass the result into this method.
pub fn set_duration(&mut self, duration: Duration) {
self.duration = duration;
}
} }
#[derive(Debug)] #[derive(Debug)]