Skip to content

Commit e0634f9

Browse files
committed
fix: finding duration twice (still a bit silly)
1 parent 569efee commit e0634f9

2 files changed

Lines changed: 10 additions & 9 deletions

File tree

‎player/src/audio.rs‎

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::collections::VecDeque;
1+
use std::collections::{VecDeque};
22
use std::error::Error;
33
use std::mem::MaybeUninit;
44
use std::sync::{Arc, Mutex};
@@ -11,7 +11,7 @@ use symphonia::core::codecs::DecoderOptions;
1111
use symphonia::core::errors::Error as SymphoniaError;
1212
use symphonia::core::formats::FormatOptions;
1313
use symphonia::core::io::MediaSourceStream;
14-
use symphonia::core::meta::{MetadataOptions, StandardTagKey, Metadata};
14+
use symphonia::core::meta::{MetadataOptions, StandardTagKey};
1515
use symphonia::core::probe::{Hint, ProbeResult};
1616

1717
use cpal::traits::{DeviceTrait, HostTrait, StreamTrait};
@@ -29,7 +29,6 @@ pub struct AudioReader {
2929
source_buffer: Vec<VecDeque<f32>>,
3030

3131
position: usize,
32-
duration: u64,
3332
finished: bool,
3433

3534
sample_rate: u32,
@@ -42,7 +41,7 @@ impl AudioReader {
4241

4342
let (mut probe_result, mut decoder) = Self::new_decoder(src);
4443

45-
let (sample_rate, channel_count, duration, _) = Self::load_info(path, &mut decoder)?;
44+
let (sample_rate, channel_count, _, _) = Self::load_info(path, &mut decoder, false)?;
4645

4746
let resampler = {
4847
let params = InterpolationParameters {
@@ -75,7 +74,6 @@ impl AudioReader {
7574
source_buffer: vec![VecDeque::new(); channel_count],
7675

7776
position: 0,
78-
duration,
7977
finished: false,
8078

8179
sample_rate,
@@ -86,6 +84,7 @@ impl AudioReader {
8684
pub fn load_info(
8785
path: &str,
8886
decoder: &mut Box<dyn symphonia::core::codecs::Decoder>,
87+
calculate_duration: bool
8988
) -> Result<(u32, usize, u64, TrackMetadata), Box<dyn Error>> {
9089
// read 1 packet to check the sample rate and channel count
9190
// TODO: there HAS to be a better way than opening the file twice
@@ -111,8 +110,10 @@ impl AudioReader {
111110

112111
// TODO: maybe cache this somewhere? idk
113112
let mut duration = 0;
114-
while let Ok(packet) = temp_probe.format.next_packet() {
115-
duration += packet.dur;
113+
if calculate_duration {
114+
while let Ok(packet) = temp_probe.format.next_packet() {
115+
duration += packet.dur;
116+
}
116117
}
117118

118119
let mut track_md = TrackMetadata::default();

‎player/src/main.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ mod audio;
1515
use audio::AudioReader;
1616

1717
mod gui;
18-
use fltk::prelude::{BrowserExt, InputExt, ValuatorExt, WidgetBase, WidgetExt, WindowExt};
18+
use fltk::prelude::{BrowserExt, ValuatorExt, WidgetBase, WidgetExt, WindowExt};
1919

2020
struct Connection {
2121
stream: FrameStream,
@@ -444,7 +444,7 @@ impl Connection {
444444
let file = path.into_os_string().into_string().unwrap();
445445
if let Ok(f) = std::fs::File::open(&file) {
446446
let (_, mut decoder) = AudioReader::new_decoder(f);
447-
if let Ok((_, _, _, metadata)) = AudioReader::load_info(&file, &mut decoder) {
447+
if let Ok((_, _, _, metadata)) = AudioReader::load_info(&file, &mut decoder, true) {
448448
let track = protocol::Track {
449449
path: file,
450450
owner: self.my_id.clone(),

0 commit comments

Comments
 (0)