Skip to content

Commit 964a126

Browse files
committed
refactor: move AudioReader to transmit.rs and clean up
1 parent 8c6fa99 commit 964a126

3 files changed

Lines changed: 305 additions & 314 deletions

File tree

‎player/src/audio.rs‎

Lines changed: 4 additions & 299 deletions
Original file line numberDiff line numberDiff line change
@@ -1,309 +1,14 @@
1-
use std::collections::{VecDeque};
2-
use std::error::Error;
31
use std::mem::MaybeUninit;
42
use std::sync::{Arc, Mutex};
53

6-
use opus::{Decoder, Encoder};
7-
use protocol::{AudioFrame, TrackMetadata};
8-
use ringbuf::{LocalRb, Rb};
9-
use symphonia::core::audio::SampleBuffer;
10-
use symphonia::core::codecs::DecoderOptions;
11-
use symphonia::core::errors::Error as SymphoniaError;
12-
use symphonia::core::formats::FormatOptions;
13-
use symphonia::core::io::MediaSourceStream;
14-
use symphonia::core::meta::{MetadataOptions, StandardTagKey};
15-
use symphonia::core::probe::{Hint, ProbeResult};
16-
174
use cpal::traits::{DeviceTrait, HostTrait, StreamTrait};
185
use cpal::{Sample, Stream};
19-
use rubato::{InterpolationParameters, Resampler, SincFixedOut};
6+
use opus::Decoder;
7+
use ringbuf::{LocalRb, Rb};
208

219
type Ringbuf<T> = LocalRb<T, Vec<MaybeUninit<T>>>;
2210

23-
pub struct AudioReader {
24-
probe_result: ProbeResult,
25-
decoder: Box<dyn symphonia::core::codecs::Decoder>,
26-
resampler: SincFixedOut<f32>,
27-
encoder: Encoder,
28-
29-
source_buffer: Vec<VecDeque<f32>>,
30-
31-
position: usize,
32-
finished: bool,
33-
34-
sample_rate: u32,
35-
channel_count: usize,
36-
}
37-
38-
impl AudioReader {
39-
pub fn load(path: &str) -> Result<Self, Box<dyn Error>> {
40-
let src = std::fs::File::open(path)?;
41-
42-
let (mut probe_result, mut decoder) = Self::new_decoder(src);
43-
44-
let (sample_rate, channel_count, _, _) = Self::load_info(path, &mut decoder, false)?;
45-
46-
let resampler = {
47-
let params = InterpolationParameters {
48-
sinc_len: 256,
49-
f_cutoff: 0.95,
50-
interpolation: rubato::InterpolationType::Linear,
51-
oversampling_factor: 256,
52-
window: rubato::WindowFunction::BlackmanHarris2,
53-
};
54-
SincFixedOut::<f32>::new(
55-
48000 as f64 / sample_rate as f64,
56-
1.0,
57-
params,
58-
480,
59-
channel_count,
60-
)
61-
.unwrap()
62-
};
63-
64-
let mut encoder =
65-
opus::Encoder::new(48000, opus::Channels::Stereo, opus::Application::Audio).unwrap();
66-
encoder.set_bitrate(opus::Bitrate::Bits(256000)).unwrap();
67-
68-
Ok(Self {
69-
probe_result,
70-
decoder,
71-
resampler,
72-
encoder,
73-
74-
source_buffer: vec![VecDeque::new(); channel_count],
75-
76-
position: 0,
77-
finished: false,
78-
79-
sample_rate,
80-
channel_count,
81-
})
82-
}
83-
84-
pub fn load_info(
85-
path: &str,
86-
decoder: &mut Box<dyn symphonia::core::codecs::Decoder>,
87-
calculate_duration: bool
88-
) -> Result<(u32, usize, u64, TrackMetadata), Box<dyn Error>> {
89-
// read 1 packet to check the sample rate and channel count
90-
// TODO: there HAS to be a better way than opening the file twice
91-
// TODO: there is.. it's ur job to clean all of this up now :3 (sorry)
92-
let src2 = std::fs::File::open(path)?;
93-
let mss2 = MediaSourceStream::new(Box::new(src2), Default::default());
94-
95-
let mut temp_probe = symphonia::default::get_probe()
96-
.format(
97-
&Hint::new(),
98-
mss2,
99-
&FormatOptions {
100-
enable_gapless: true,
101-
..FormatOptions::default()
102-
},
103-
&MetadataOptions::default(),
104-
)
105-
.unwrap();
106-
107-
let packet = temp_probe.format.next_packet().unwrap();
108-
let decoded = decoder.decode(&packet).unwrap();
109-
let spec = *decoded.spec();
110-
111-
// TODO: maybe cache this somewhere? idk
112-
let mut duration = 0;
113-
if calculate_duration {
114-
while let Ok(packet) = temp_probe.format.next_packet() {
115-
duration += packet.dur;
116-
}
117-
}
118-
119-
let mut track_md = TrackMetadata::default();
120-
121-
track_md.duration = (duration as f64 / spec.rate as f64) as usize;
122-
123-
let mut md = temp_probe.format.metadata();
124-
if let Some(current) = md.skip_to_latest() {
125-
dbg!(current.tags());
126-
dbg!(current.vendor_data());
127-
128-
let tags = current.tags();
129-
Self::fill_metadata(&mut track_md, tags);
130-
} else if let Some(mut md) = temp_probe.metadata.get() {
131-
let tags = md.skip_to_latest().unwrap().tags();
132-
Self::fill_metadata(&mut track_md, tags);
133-
}
134-
else {
135-
println!("really no metadata to speak of...");
136-
}
137-
138-
139-
Ok((spec.rate, spec.channels.count(), duration, track_md))
140-
}
141-
142-
// hazel's favorite calling pattern
143-
fn fill_metadata(md: &mut TrackMetadata, tags: &[symphonia::core::meta::Tag]) {
144-
for tag in tags {
145-
match tag.std_key {
146-
Some(StandardTagKey::TrackTitle) => {
147-
md.title = Some(tag.value.to_string());
148-
}
149-
Some(StandardTagKey::Album) => {
150-
md.album = Some(tag.value.to_string());
151-
}
152-
Some(StandardTagKey::Artist) => {
153-
md.artist = Some(tag.value.to_string());
154-
}
155-
Some(StandardTagKey::TrackNumber) => {
156-
md.track_no = Some(tag.value.to_string());
157-
}
158-
Some(StandardTagKey::AlbumArtist) => {
159-
md.album_artist = Some(tag.value.to_string());
160-
}
161-
162-
_ => {}
163-
}
164-
}
165-
}
166-
167-
pub fn new_decoder(
168-
src: std::fs::File,
169-
) -> (ProbeResult, Box<dyn symphonia::core::codecs::Decoder>) {
170-
let mss = MediaSourceStream::new(Box::new(src), Default::default());
171-
172-
let hint = Hint::new();
173-
174-
let probe_result = symphonia::default::get_probe()
175-
.format(
176-
&hint,
177-
mss,
178-
&FormatOptions {
179-
enable_gapless: true,
180-
..FormatOptions::default()
181-
},
182-
&MetadataOptions::default(),
183-
)
184-
.unwrap();
185-
186-
let decoder = symphonia::default::get_codecs()
187-
.make(
188-
&probe_result
189-
.format
190-
.default_track()
191-
.expect("uhhhh")
192-
.codec_params,
193-
&DecoderOptions::default(),
194-
)
195-
.unwrap();
196-
197-
(probe_result, decoder)
198-
}
199-
200-
// sets self.finished if the end was reached
201-
fn read_more(&mut self) -> Result<(), Box<dyn Error>> {
202-
match self.probe_result.format.next_packet() {
203-
Ok(packet) => {
204-
let decoded = self.decoder.decode(&packet).unwrap();
205-
let spec = *decoded.spec();
206-
207-
if decoded.frames() > 0 {
208-
let mut sample_buffer: SampleBuffer<f32> =
209-
SampleBuffer::new(decoded.frames() as u64, spec);
210-
211-
sample_buffer.copy_interleaved_ref(decoded);
212-
213-
let samples = sample_buffer.samples();
214-
215-
for frame in samples.chunks(spec.channels.count()) {
216-
for (chan, sample) in frame.iter().enumerate() {
217-
self.source_buffer[chan].push_back(*sample)
218-
}
219-
}
220-
} else {
221-
eprintln!("Empty packet encountered while loading song!");
222-
}
223-
}
224-
Err(SymphoniaError::IoError(_)) => {
225-
// no more packets
226-
self.finished = true;
227-
}
228-
Err(e) => return Err("failed to parse track".into()),
229-
}
230-
231-
Ok(())
232-
}
233-
234-
fn resample_more(&mut self) -> Result<Vec<f32>, Box<dyn Error>> {
235-
let samples_needed = if self.sample_rate != 48000 {
236-
self.resampler.input_frames_next()
237-
} else {
238-
480 as usize
239-
};
240-
241-
while self.source_buffer[0].len() < samples_needed {
242-
if self.finished {
243-
// if no more packets to read, pad with 0s until we reach input_frames_next
244-
for chan in self.source_buffer.iter_mut() {
245-
//println!("filling with 0s!!!");
246-
chan.push_back(0.0);
247-
}
248-
} else {
249-
self.read_more()?;
250-
}
251-
}
252-
253-
let mut chunk_samples: Vec<Vec<f32>> = vec![Vec::new(); self.channel_count];
254-
255-
for (chan_idx, chan) in self.source_buffer.iter_mut().enumerate() {
256-
for _ in 0..samples_needed {
257-
// maybe there's a better way to do this?
258-
chunk_samples[chan_idx].push(chan.pop_front().unwrap());
259-
}
260-
}
261-
262-
// resample to standard 48000 if needed
263-
let samples_correct_rate = if self.sample_rate != 48000 {
264-
self.resampler.process(&chunk_samples, None).unwrap()
265-
} else {
266-
chunk_samples
267-
};
268-
269-
// now we have to interleave it since we had to use the resampling thing
270-
let samples: Vec<f32> = samples_correct_rate[0]
271-
.chunks(1)
272-
.zip(samples_correct_rate[1].chunks(1))
273-
.flat_map(|(a, b)| a.into_iter().chain(b))
274-
.copied()
275-
.collect();
276-
277-
Ok(samples)
278-
}
279-
280-
pub fn encode_frame(&mut self) -> Result<AudioFrame, ()> {
281-
const PCM_LENGTH: usize = 960;
282-
283-
let mut pcm = [0.0; PCM_LENGTH];
284-
285-
let resampled = self.resample_more().unwrap();
286-
for i in 0..PCM_LENGTH {
287-
pcm[i] = resampled[i];
288-
}
289-
290-
let x = self.encoder.encode_vec_float(&pcm, 256).unwrap();
291-
self.position += 1;
292-
293-
Ok(AudioFrame {
294-
frame: self.position as u32,
295-
data: x,
296-
})
297-
}
298-
299-
pub fn finished(&self) -> bool {
300-
self.finished
301-
}
302-
303-
pub fn set_finished(&mut self) {
304-
self.finished = true;
305-
}
306-
}
11+
const PLAYER_BUFFER_SIZE: usize = 48000 * 10; // 10s of samples?
30712

30813
pub struct Player {
30914
decoder: Decoder,
@@ -315,7 +20,7 @@ impl Player {
31520
pub fn new() -> Self {
31621
Self {
31722
decoder: Decoder::new(48000, opus::Channels::Stereo).unwrap(),
318-
buffer: Arc::new(Mutex::new(Ringbuf::<f32>::new(48000 * 10))), // 10s??
23+
buffer: Arc::new(Mutex::new(Ringbuf::<f32>::new(PLAYER_BUFFER_SIZE))),
31924
stream: None,
32025
frames_received: 0,
32126
}

‎player/src/main.rs‎

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,9 @@ use tokio::time::timeout;
1212
use tokio::{net::TcpStream, sync::mpsc};
1313

1414
mod audio;
15-
use audio::AudioReader;
1615

1716
mod transmit;
18-
use transmit::{TransmitCommand, TransmitThread, TransmitThreadHandle};
17+
use transmit::{AudioInfoReader, TransmitCommand, TransmitThread, TransmitThreadHandle};
1918

2019
mod gui;
2120
use fltk::prelude::{BrowserExt, ValuatorExt, WidgetBase, WidgetExt, WindowExt};
@@ -364,18 +363,18 @@ impl Connection {
364363
// TODO: this blocks and fucks everything up if it takes too long to read....
365364
self.ui_status("Loading file...");
366365
let file = path.into_os_string().into_string().unwrap();
367-
if let Ok(f) = std::fs::File::open(&file) {
368-
let (_, mut decoder) = AudioReader::new_decoder(f);
369-
if let Ok((_, _, _, metadata)) = AudioReader::load_info(&file, &mut decoder, true) {
366+
367+
if let Ok(mut reader) = AudioInfoReader::load(&file) {
368+
if let Ok((_, _, metadata)) = reader.read_info(true) {
370369
let track = protocol::Track {
371370
path: file,
372371
owner: self.my_id.clone(),
373372
metadata
374373
};
374+
375375
self.message_tx.send(Message::QueuePush(track)).unwrap();
376376
}
377377
}
378-
379378
}
380379
UIEvent::Pause => {
381380

0 commit comments

Comments
 (0)