Skip to content

Commit 3b28c02

Browse files
committed
feat: cleanup
1 parent f3c5d85 commit 3b28c02

File tree

20 files changed

+776
-710
lines changed

20 files changed

+776
-710
lines changed

Cargo.lock

Lines changed: 436 additions & 418 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ skim = "^0"
4444
strfmt = "^0"
4545
symphonia = { version = "^0", features = ["all"] }
4646
termion = "^1"
47-
tokio = { version = "^1", features = [ "macros", "rt", "rt-multi-thread" ] }
4847
toml = "^0"
4948
tracing = "^0"
5049
tracing-subscriber = { version = "^0", features = [ "std", "env-filter" ] }

src/bin/client/ui/widgets/tui_dirlist_detailed.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,8 @@ fn print_entry(
117117
FileType::File => format::file_size_to_string(entry.metadata.len()),
118118
};
119119
let symlink_string = match entry.metadata.link_type() {
120-
LinkType::Normal => "",
121-
LinkType::Symlink(_, _) => "-> ",
120+
LinkType::Normal => String::new(),
121+
LinkType::Symlink(path, _) => format!("-> {}", path.as_str()),
122122
};
123123
let left_label_original = entry.file_name();
124124
let right_label_original = format!(" {}{} ", symlink_string, size_string);

src/bin/server/audio/device.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
pub fn get_default_host(host_id: cpal::HostId) -> cpal::Host {
22
tracing::debug!("Available audio systems:");
33
for host in cpal::available_hosts() {
4-
tracing::debug!("host: {:?}", host);
4+
tracing::debug!(?host, "Audio host");
55
}
66
cpal::host_from_id(
77
cpal::available_hosts()

src/bin/server/audio/request.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use std::time::Duration;
22

33
use dizi::song::DiziAudioFile;
44

5+
/// User requests to the player
56
#[derive(Clone, Debug)]
67
pub enum PlayerRequest {
78
Play { song: DiziAudioFile, volume: f32 },
Lines changed: 1 addition & 137 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,11 @@
11
use std::iter::Iterator;
2-
use std::sync::{mpsc, Arc, RwLock};
3-
use std::time::Duration;
42

53
use symphonia::core::audio::SampleBuffer;
64
use symphonia::core::codecs::Decoder;
75
use symphonia::core::errors::Error as SymphoniaError;
86
use symphonia::core::formats::{FormatReader, Packet};
97

10-
use cpal::traits::{DeviceTrait, StreamTrait};
11-
use cpal::{Stream, StreamConfig};
12-
138
use dizi::error::{DiziError, DiziResult};
14-
use symphonia::core::units::TimeBase;
15-
16-
use crate::audio::request::PlayerRequest;
17-
18-
use super::stream::StreamEvent;
199

2010
pub struct PacketReader {
2111
format: Box<dyn FormatReader>,
@@ -91,135 +81,9 @@ impl PacketDecoder {
9181
Err(SymphoniaError::IoError(_)) => Ok(vec![]),
9282
Err(SymphoniaError::DecodeError(_)) => Ok(vec![]),
9383
Err(err) => {
94-
tracing::error!("Unhandled symphonia error: {}", err);
84+
tracing::error!(?err, "Symphonia error");
9585
Err(DiziError::from(err))
9686
}
9787
}
9888
}
9989
}
100-
101-
pub fn stream_loop<T>(
102-
stream_tx: mpsc::Sender<StreamEvent>,
103-
device: &cpal::Device,
104-
config: &StreamConfig,
105-
samples: Vec<T>,
106-
volume: f32,
107-
volume_change: fn(T, f32) -> T,
108-
) -> DiziResult<(Stream, mpsc::Sender<PlayerRequest>)>
109-
where
110-
T: symphonia::core::sample::Sample
111-
+ cpal::Sample
112-
+ cpal::SizedSample
113-
+ std::marker::Send
114-
+ 'static
115-
+ symphonia::core::conv::FromSample<i8>
116-
+ symphonia::core::conv::FromSample<i16>
117-
+ symphonia::core::conv::FromSample<i32>
118-
+ symphonia::core::conv::FromSample<u8>
119-
+ symphonia::core::conv::FromSample<u16>
120-
+ symphonia::core::conv::FromSample<u32>
121-
+ symphonia::core::conv::FromSample<f32>
122-
+ symphonia::core::conv::FromSample<f64>
123-
+ symphonia::core::conv::FromSample<symphonia::core::sample::i24>
124-
+ symphonia::core::conv::FromSample<symphonia::core::sample::u24>,
125-
{
126-
let err_fn = |err| {
127-
tracing::error!("A playback error has occured! {}", err);
128-
};
129-
130-
let time_base = TimeBase {
131-
numer: 1,
132-
denom: config.sample_rate.0 * config.channels as u32,
133-
};
134-
135-
let samples_count = samples.len();
136-
137-
// all vars that the stream will update while its streaming
138-
let frame_index = Arc::new(RwLock::new(0_usize));
139-
let volume = Arc::new(RwLock::new(volume));
140-
let playback_duration = Arc::new(RwLock::new(0));
141-
142-
let _ = stream_tx.send(StreamEvent::Progress(Duration::from_secs(0)));
143-
144-
// if stream_tx is None, then we've already sent a StreamEnded message
145-
// and we don't need to send another one
146-
let mut stream_tx = Some(stream_tx);
147-
148-
let (playback_loop_tx, playback_loop_rx) = mpsc::channel();
149-
150-
let stream = device.build_output_stream(
151-
config,
152-
move |data: &mut [T], _: &cpal::OutputCallbackInfo| {
153-
let process_message = |msg: PlayerRequest| match msg {
154-
PlayerRequest::SetVolume { volume: new_volume } => {
155-
let mut current_volume = volume.write().unwrap();
156-
*current_volume = new_volume;
157-
}
158-
PlayerRequest::FastForward { offset } => {
159-
let mut sample_offset = frame_index.write().unwrap();
160-
*sample_offset += time_base.denom as usize * offset.as_secs() as usize;
161-
if *sample_offset >= samples_count {
162-
*sample_offset = samples_count - time_base.denom as usize;
163-
}
164-
}
165-
PlayerRequest::Rewind { offset } => {
166-
let mut sample_offset = frame_index.write().unwrap();
167-
if *sample_offset < time_base.denom as usize * offset.as_secs() as usize {
168-
*sample_offset = 0;
169-
} else {
170-
*sample_offset -= time_base.denom as usize * offset.as_secs() as usize;
171-
}
172-
}
173-
_ => {}
174-
};
175-
176-
if let Ok(msg) = playback_loop_rx.try_recv() {
177-
process_message(msg);
178-
}
179-
180-
// if sample_offset is greater than samples_count, then we've reached the end
181-
let sample_offset = { *frame_index.read().unwrap() };
182-
if sample_offset >= samples_count {
183-
if let Some(stream_tx) = stream_tx.take() {
184-
let _ = stream_tx.send(StreamEvent::StreamEnded);
185-
}
186-
return;
187-
}
188-
189-
let current_volume = { *volume.read().unwrap() };
190-
let mut i = 0;
191-
for d in data.iter_mut() {
192-
if sample_offset + i >= samples_count {
193-
let mut offset = frame_index.write().unwrap();
194-
*offset = samples_count + 1;
195-
break;
196-
}
197-
*d = volume_change(samples[sample_offset + i], current_volume);
198-
i += 1;
199-
}
200-
// new offset
201-
let new_sample_offset = {
202-
let mut sample_offset = frame_index.write().unwrap();
203-
*sample_offset += i;
204-
*sample_offset
205-
};
206-
// new duration
207-
let next_duration = time_base.calc_time(new_sample_offset as u64).seconds;
208-
let prev_duration = { *playback_duration.read().unwrap() };
209-
210-
// update duration if seconds changed
211-
if prev_duration != next_duration {
212-
let new_duration = Duration::from_secs(next_duration);
213-
if let Some(stream_tx) = stream_tx.as_ref() {
214-
let _ = stream_tx.send(StreamEvent::Progress(new_duration));
215-
}
216-
let mut duration = playback_duration.write().unwrap();
217-
*duration = new_duration.as_secs();
218-
}
219-
},
220-
err_fn,
221-
None,
222-
)?;
223-
stream.play()?;
224-
Ok((stream, playback_loop_tx))
225-
}

src/bin/server/audio/symphonia/player/impl_audio_player.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,8 @@ impl AudioPlayer for SymphoniaPlayer {
139139
break;
140140
};
141141
tracing::debug!(
142-
"Skipping '{}' because we failed to parse it",
143-
song_entry.entry.file_name()
142+
file_name = song_entry.entry.file_name(),
143+
"Failed to parse file, skipping",
144144
);
145145
}
146146
if let Some(entry) = playlist.current_entry() {

src/bin/server/audio/symphonia/player/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ impl SymphoniaPlayer {
8787
}
8888

8989
fn play(&mut self, song: &DiziAudioFile) -> DiziResult {
90-
tracing::debug!("Song: {:#?}", song);
90+
tracing::debug!(?song, "Playing song");
9191

9292
self.player_stream_req().send(PlayerRequest::Play {
9393
song: song.clone(),
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
mod player_stream;
2+
mod player_stream_state;
3+
mod stream_listener;
4+
5+
pub use player_stream::*;
6+
pub use player_stream_state::*;
7+
pub use stream_listener::*;
8+
9+
use std::time::Duration;
10+
11+
/// Events returned from stream
12+
#[derive(Clone, Copy, Debug)]
13+
pub enum StreamEvent {
14+
Progress(Duration),
15+
StreamEnded,
16+
}

0 commit comments

Comments
 (0)