Skip to content

Commit

Permalink
minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Fabus1184 committed Oct 4, 2023
1 parent 0d2f6b3 commit ac243ae
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 19 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ ordered-float = { version = "3.9.0", features = ["serde"] }

# tui
crossterm = "0.26.1"
ratatui = { version = "0.22.0", features = ["all-widgets"] }
ratatui = { version = "0.23.0", features = ["all-widgets"] }
image = { version = "0.24.7", default-features = false, features = [
"png",
"jpeg",
Expand Down
6 changes: 5 additions & 1 deletion src/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,11 @@ where

standard_tags.insert(
StandardTagKey::ReplayGainTrackGain,
Value::Float((gain + peak) as f64 / 2.0),
Value::Float(gain as f64),
);
standard_tags.insert(
StandardTagKey::ReplayGainTrackPeak,
Value::Float(peak as f64),
);
}

Expand Down
10 changes: 7 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::{
};

use cache::Cache;
use log::{trace, warn, LevelFilter};
use log::{info, trace, warn, LevelFilter};
use player::Player;
use simplelog::{CombinedLogger, WriteLogger};

Expand Down Expand Up @@ -84,8 +84,12 @@ fn main() {
});

let mut cache = if *config != old_config {
trace!("config changed, rebuilding");
Cache::build_from_config(&config)
info!("config changed, rebuilding");
let cache = Cache::build_from_config(&config);
cache
.save(&config)
.unwrap_or_else(|e| warn!("Failed to save cache {e:?}"));
cache
} else {
cache
};
Expand Down
19 changes: 16 additions & 3 deletions src/player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use cpal::{
traits::{DeviceTrait, HostTrait, StreamTrait},
Device, OutputCallbackInfo, StreamConfig,
};
use log::{debug, error, trace, warn};
use log::{debug, error, info, trace, warn};
use souvlaki::{MediaControlEvent, MediaControls, MediaMetadata, MediaPlayback, PlatformConfig};
use symphonia::core::{
audio::SignalSpec,
Expand Down Expand Up @@ -207,17 +207,19 @@ impl Player {

let (command_tx, command_rx) = std::sync::mpsc::sync_channel::<StreamCommand>(1);

let buffer_size = signal_spec.rate * 2;
let stream_config = StreamConfig {
channels: signal_spec.channels.count() as u16,
sample_rate: cpal::SampleRate(signal_spec.rate),
buffer_size: cpal::BufferSize::Fixed(signal_spec.rate * 2),
buffer_size: cpal::BufferSize::Fixed(buffer_size),
};
trace!("play: stream_config: {:?}", stream_config);

let arc = self.arc.upgrade().expect("Failed to upgrade weak player");
let arc2 = arc.clone();
let arc3 = arc.clone();

let signal_spec = signal_spec.clone();
let thread = std::thread::spawn(move || {
trace!("locking player");
let player = arc.lock().expect("Failed to lock player");
Expand All @@ -236,6 +238,14 @@ impl Player {
Err(e) => {
debug!("Failed to receive sample, sender disconnected {:?}", e);

let duration = Duration::from_secs_f32(
buffer_size as f32
/ (signal_spec.channels.bits() * signal_spec.rate)
as f32,
);
info!("Sleeping for {:?} to finish song", duration);
std::thread::sleep(duration);

{
trace!("locking player");
let mut player =
Expand Down Expand Up @@ -264,7 +274,10 @@ impl Player {
..
}) = player.current.as_mut()
{
*duration = Duration::from_secs_f32(n as f32 / (2.0 * 48_000.0));
*duration = Duration::from_secs_f32(
n as f32
/ (signal_spec.channels.bits() * signal_spec.rate) as f32,
);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/tui/files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ impl Tui for Files {
if selected < len + 1 - area.height as usize / 2 {
selected - area.height as usize / 2
} else {
len + 1 - area.height as usize
len + 2 - area.height as usize
}
} else {
0
Expand Down
32 changes: 22 additions & 10 deletions src/tui/status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,31 +39,43 @@ impl Tui for Status {

trace!("locking player");
let playing = Paragraph::new(
if let Some((song, _)) = self.player.lock().unwrap().current() {
if let Some((song, path)) = self.player.lock().unwrap().current() {
let title = song
.standard_tags
.get(&StandardTagKey::TrackTitle)
.map(|s| s.to_string())
.or(path
.components()
.last()
.map(|s| s.as_os_str().to_string_lossy().to_string()))
.unwrap_or(UNKNOWN_STRING.to_string());

let artist = song
.standard_tags
.get(&StandardTagKey::Artist)
.map(|s| s.to_string())
.unwrap_or(UNKNOWN_STRING.to_string());
.map(|s| s.to_string());

Line::from(vec![
Span::from(" "),
Span::from(artist)
.fg(Color::LightYellow)
.add_modifier(ratatui::style::Modifier::BOLD),
Span::from(" - ").fg(Color::White),
let mut elems = vec![Span::from(" ")];

if let Some(artist) = artist {
elems.push(
Span::from(artist)
.fg(Color::LightYellow)
.add_modifier(ratatui::style::Modifier::BOLD),
);
elems.push(Span::from(" - ").fg(Color::White));
}

elems.extend([
Span::from(title)
.fg(Color::LightYellow)
.add_modifier(ratatui::style::Modifier::BOLD),
Span::from(format!(" ({})", format_duration(song.duration)))
.fg(Color::LightGreen),
Span::from(" "),
])
]);

Line::from(elems)
} else {
Line::from(vec![
Span::from(" - ").add_modifier(ratatui::style::Modifier::BOLD)
Expand Down

0 comments on commit ac243ae

Please sign in to comment.