Run song length calculations asynchronously

This commit is contained in:
Emi Simpson 2022-01-05 15:42:12 -05:00
parent 7e64590279
commit b9883d2ad1
Signed by: Emi
GPG key ID: A12F2C2FFDC3D847
6 changed files with 81 additions and 24 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,4 @@
use iced::Command;
use iced::Image; use iced::Image;
use iced::image::Handle; use iced::image::Handle;
use image::DynamicImage; use image::DynamicImage;
@ -28,7 +29,7 @@ pub struct Editor {
} }
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()
@ -43,14 +44,14 @@ impl Editor {
cover.blur((cover.width() / 50) as f32).into_bgra8() cover.blur((cover.width() / 50) as f32).into_bgra8()
); );
let controls = Controls::new(song); let (controls, cmd) = Controls::new(song);
Self { (Self {
lyrics: Lyrics::new(), lyrics: Lyrics::new(),
dimensions: size, dimensions: size,
cached_resized_bg: None, cached_resized_bg: None,
controls, theme, bg_img, controls, theme, bg_img,
} }, cmd)
} }
// TODO: work on untangling this mess // TODO: work on untangling this mess

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.
///
/// Note: This doesn't actually preform the calculation, but instead returns a
/// `'static FnOnce` that can be used to do so, in order to simplify running the
/// 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 n_whole_seconds = n_samples / n_channels / sample_rate;
let remaining_samples = n_samples % sample_rate; let remaining_samples = n_samples % sample_rate;
let n_nanos = remaining_samples * 1_000_000_000 / sample_rate; let n_nanos = remaining_samples * 1_000_000_000 / sample_rate;
Duration::new(n_whole_seconds, n_nanos as u32) 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)]