Skip to content

Commit f7bcb49

Browse files
committed
Clean up API
1 parent 703b667 commit f7bcb49

12 files changed

+69
-69
lines changed

playback/src/audio_backend/alsa.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use super::{Open, Sink, SinkAsBytes};
22
use crate::config::AudioFormat;
3-
use crate::convert::Requantizer;
3+
use crate::convert::Converter;
44
use crate::decoder::AudioPacket;
55
use crate::player::{NUM_CHANNELS, SAMPLES_PER_SECOND, SAMPLE_RATE};
66
use alsa::device_name::HintIter;

playback/src/audio_backend/gstreamer.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use super::{Open, Sink, SinkAsBytes};
22
use crate::config::AudioFormat;
3-
use crate::convert::Requantizer;
3+
use crate::convert::Converter;
44
use crate::decoder::AudioPacket;
55
use crate::player::{NUM_CHANNELS, SAMPLE_RATE};
66

playback/src/audio_backend/jackaudio.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use super::{Open, Sink};
22
use crate::config::AudioFormat;
3-
use crate::convert::Requantizer;
3+
use crate::convert::Converter;
44
use crate::decoder::AudioPacket;
55
use crate::player::NUM_CHANNELS;
66
use jack::{
@@ -70,7 +70,7 @@ impl Open for JackSink {
7070
}
7171

7272
impl Sink for JackSink {
73-
fn write(&mut self, packet: &AudioPacket, _requantizer: &mut Requantizer) -> io::Result<()> {
73+
fn write(&mut self, packet: &AudioPacket, _: &mut Converter) -> io::Result<()> {
7474
for s in packet.samples().iter() {
7575
let res = self.send.send(*s);
7676
if res.is_err() {

playback/src/audio_backend/mod.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::config::AudioFormat;
2-
use crate::convert::Requantizer;
2+
use crate::convert::Converter;
33
use crate::decoder::AudioPacket;
44
use std::io;
55

@@ -14,7 +14,7 @@ pub trait Sink {
1414
fn stop(&mut self) -> io::Result<()> {
1515
Ok(())
1616
}
17-
fn write(&mut self, packet: &AudioPacket, requantizer: &mut Requantizer) -> io::Result<()>;
17+
fn write(&mut self, packet: &AudioPacket, converter: &mut Converter) -> io::Result<()>;
1818
}
1919

2020
pub type SinkBuilder = fn(Option<String>, AudioFormat) -> Box<dyn Sink>;
@@ -30,26 +30,26 @@ fn mk_sink<S: Sink + Open + 'static>(device: Option<String>, format: AudioFormat
3030
// reuse code for various backends
3131
macro_rules! sink_as_bytes {
3232
() => {
33-
fn write(&mut self, packet: &AudioPacket, requantizer: &mut Requantizer) -> io::Result<()> {
34-
use crate::convert::{self, i24};
33+
fn write(&mut self, packet: &AudioPacket, converter: &mut Converter) -> io::Result<()> {
34+
use crate::convert::i24;
3535
use zerocopy::AsBytes;
3636
match packet {
3737
AudioPacket::Samples(samples) => match self.format {
3838
AudioFormat::F32 => self.write_bytes(samples.as_bytes()),
3939
AudioFormat::S32 => {
40-
let samples_s32: &[i32] = &convert::to_s32(samples, requantizer);
40+
let samples_s32: &[i32] = &converter.f32_to_s32(samples);
4141
self.write_bytes(samples_s32.as_bytes())
4242
}
4343
AudioFormat::S24 => {
44-
let samples_s24: &[i32] = &convert::to_s24(samples, requantizer);
44+
let samples_s24: &[i32] = &converter.f32_to_s24(samples);
4545
self.write_bytes(samples_s24.as_bytes())
4646
}
4747
AudioFormat::S24_3 => {
48-
let samples_s24_3: &[i24] = &convert::to_s24_3(samples, requantizer);
48+
let samples_s24_3: &[i24] = &converter.f32_to_s24_3(samples);
4949
self.write_bytes(samples_s24_3.as_bytes())
5050
}
5151
AudioFormat::S16 => {
52-
let samples_s16: &[i16] = &convert::to_s16(samples, requantizer);
52+
let samples_s16: &[i16] = &converter.f32_to_s16(samples);
5353
self.write_bytes(samples_s16.as_bytes())
5454
}
5555
},

playback/src/audio_backend/pipe.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use super::{Open, Sink, SinkAsBytes};
22
use crate::config::AudioFormat;
3-
use crate::convert::Requantizer;
3+
use crate::convert::Converter;
44
use crate::decoder::AudioPacket;
55
use std::fs::OpenOptions;
66
use std::io::{self, Write};

playback/src/audio_backend/portaudio.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use super::{Open, Sink};
22
use crate::config::AudioFormat;
3-
use crate::convert::{self, Requantizer};
3+
use crate::convert::Converter;
44
use crate::decoder::AudioPacket;
55
use crate::player::{NUM_CHANNELS, SAMPLE_RATE};
66
use portaudio_rs::device::{get_default_output_index, DeviceIndex, DeviceInfo};
@@ -144,7 +144,7 @@ impl<'a> Sink for PortAudioSink<'a> {
144144
Ok(())
145145
}
146146

147-
fn write(&mut self, packet: &AudioPacket, requantizer: &mut Requantizer) -> io::Result<()> {
147+
fn write(&mut self, packet: &AudioPacket, converter: &mut Converter) -> io::Result<()> {
148148
macro_rules! write_sink {
149149
(ref mut $stream: expr, $samples: expr) => {
150150
$stream.as_mut().unwrap().write($samples)
@@ -157,11 +157,11 @@ impl<'a> Sink for PortAudioSink<'a> {
157157
write_sink!(ref mut stream, samples)
158158
}
159159
Self::S32(stream, _parameters) => {
160-
let samples_s32: &[i32] = &convert::to_s32(samples, requantizer);
160+
let samples_s32: &[i32] = &converter.f32_to_s32(samples);
161161
write_sink!(ref mut stream, samples_s32)
162162
}
163163
Self::S16(stream, _parameters) => {
164-
let samples_s16: &[i16] = &convert::to_s16(samples, requantizer);
164+
let samples_s16: &[i16] = &converter.f32_to_s16(samples);
165165
write_sink!(ref mut stream, samples_s16)
166166
}
167167
};

playback/src/audio_backend/pulseaudio.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use super::{Open, Sink, SinkAsBytes};
22
use crate::config::AudioFormat;
3-
use crate::convert::Requantizer;
3+
use crate::convert::Converter;
44
use crate::decoder::AudioPacket;
55
use crate::player::{NUM_CHANNELS, SAMPLE_RATE};
66
use libpulse_binding::{self as pulse, stream::Direction};

playback/src/audio_backend/rodio.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use thiserror::Error;
66

77
use super::Sink;
88
use crate::config::AudioFormat;
9-
use crate::convert::{self, Requantizer};
9+
use crate::convert::Converter;
1010
use crate::decoder::AudioPacket;
1111
use crate::player::{NUM_CHANNELS, SAMPLE_RATE};
1212

@@ -174,7 +174,7 @@ pub fn open(host: cpal::Host, device: Option<String>, format: AudioFormat) -> Ro
174174
}
175175

176176
impl Sink for RodioSink {
177-
fn write(&mut self, packet: &AudioPacket, requantizer: &mut Requantizer) -> io::Result<()> {
177+
fn write(&mut self, packet: &AudioPacket, converter: &mut Converter) -> io::Result<()> {
178178
let samples = packet.samples();
179179
match self.format {
180180
AudioFormat::F32 => {
@@ -183,7 +183,7 @@ impl Sink for RodioSink {
183183
self.rodio_sink.append(source);
184184
}
185185
AudioFormat::S16 => {
186-
let samples_s16: &[i16] = &convert::to_s16(samples, requantizer);
186+
let samples_s16: &[i16] = &converter.f32_to_s16(samples);
187187
let source = rodio::buffer::SamplesBuffer::new(
188188
NUM_CHANNELS as u16,
189189
SAMPLE_RATE,

playback/src/audio_backend/sdl.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use super::{Open, Sink};
22
use crate::config::AudioFormat;
3-
use crate::convert::{self, Requantizer};
3+
use crate::convert::Converter;
44
use crate::decoder::AudioPacket;
55
use crate::player::{NUM_CHANNELS, SAMPLE_RATE};
66
use sdl2::audio::{AudioQueue, AudioSpecDesired};
@@ -81,7 +81,7 @@ impl Sink for SdlSink {
8181
Ok(())
8282
}
8383

84-
fn write(&mut self, packet: &AudioPacket, requantizer: &mut Requantizer) -> io::Result<()> {
84+
fn write(&mut self, packet: &AudioPacket, converter: &mut Converter) -> io::Result<()> {
8585
macro_rules! drain_sink {
8686
($queue: expr, $size: expr) => {{
8787
// sleep and wait for sdl thread to drain the queue a bit
@@ -98,12 +98,12 @@ impl Sink for SdlSink {
9898
queue.queue(samples)
9999
}
100100
Self::S32(queue) => {
101-
let samples_s32: &[i32] = &convert::to_s32(samples, requantizer);
101+
let samples_s32: &[i32] = &converter.f32_to_s32(samples);
102102
drain_sink!(queue, AudioFormat::S32.size());
103103
queue.queue(samples_s32)
104104
}
105105
Self::S16(queue) => {
106-
let samples_s16: &[i16] = &convert::to_s16(samples, requantizer);
106+
let samples_s16: &[i16] = &converter.f32_to_s16(samples);
107107
drain_sink!(queue, AudioFormat::S16.size());
108108
queue.queue(samples_s16)
109109
}

playback/src/audio_backend/subprocess.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use super::{Open, Sink, SinkAsBytes};
22
use crate::config::AudioFormat;
3-
use crate::convert::Requantizer;
3+
use crate::convert::Converter;
44
use crate::decoder::AudioPacket;
55
use shell_words::split;
66

playback/src/convert.rs

+38-38
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@ impl i24 {
1717
}
1818
}
1919

20-
pub struct Requantizer {
20+
pub struct Converter {
2121
ditherer: Box<dyn Ditherer>,
2222
noise_shaper: Box<dyn NoiseShaper>,
2323
}
2424

25-
impl Requantizer {
25+
impl Converter {
2626
pub fn new(ditherer: Box<dyn Ditherer>, noise_shaper: Box<dyn NoiseShaper>) -> Self {
2727
info!(
28-
"Requantizing with ditherer: {} and noise shaper: {}",
28+
"Converting with ditherer: {} and noise shaper: {}",
2929
ditherer, noise_shaper
3030
);
3131
Self {
@@ -67,43 +67,43 @@ impl Requantizer {
6767
let noise = self.ditherer.noise(sample);
6868
self.noise_shaper.shape(sample, noise)
6969
}
70-
}
7170

72-
// https://doc.rust-lang.org/nomicon/casts.html: casting float to integer
73-
// rounds towards zero, then saturates. Ideally halves should round to even to
74-
// prevent any bias, but since it is extremely unlikely that a float has
75-
// *exactly* .5 as fraction, this should be more than precise enough.
76-
pub fn to_s32(samples: &[f32], requantizer: &mut Requantizer) -> Vec<i32> {
77-
samples
78-
.iter()
79-
.map(|sample| requantizer.scale(*sample, std::i32::MAX) as i32)
80-
.collect()
81-
}
71+
// https://doc.rust-lang.org/nomicon/casts.html: casting float to integer
72+
// rounds towards zero, then saturates. Ideally halves should round to even to
73+
// prevent any bias, but since it is extremely unlikely that a float has
74+
// *exactly* .5 as fraction, this should be more than precise enough.
75+
pub fn f32_to_s32(&mut self, samples: &[f32]) -> Vec<i32> {
76+
samples
77+
.iter()
78+
.map(|sample| self.scale(*sample, std::i32::MAX) as i32)
79+
.collect()
80+
}
8281

83-
// S24 is 24-bit PCM packed in an upper 32-bit word
84-
pub fn to_s24(samples: &[f32], requantizer: &mut Requantizer) -> Vec<i32> {
85-
samples
86-
.iter()
87-
.map(|sample| requantizer.clamping_scale(*sample, i24::MIN, i24::MAX) as i32)
88-
.collect()
89-
}
82+
// S24 is 24-bit PCM packed in an upper 32-bit word
83+
pub fn f32_to_s24(&mut self, samples: &[f32]) -> Vec<i32> {
84+
samples
85+
.iter()
86+
.map(|sample| self.clamping_scale(*sample, i24::MIN, i24::MAX) as i32)
87+
.collect()
88+
}
9089

91-
// S24_3 is 24-bit PCM in a 3-byte array
92-
pub fn to_s24_3(samples: &[f32], requantizer: &mut Requantizer) -> Vec<i24> {
93-
samples
94-
.iter()
95-
.map(|sample| {
96-
// Not as DRY as calling to_s24 first, but this saves iterating
97-
// over all samples twice.
98-
let int_value = requantizer.clamping_scale(*sample, i24::MIN, i24::MAX) as i32;
99-
i24::from_s24(int_value)
100-
})
101-
.collect()
102-
}
90+
// S24_3 is 24-bit PCM in a 3-byte array
91+
pub fn f32_to_s24_3(&mut self, samples: &[f32]) -> Vec<i24> {
92+
samples
93+
.iter()
94+
.map(|sample| {
95+
// Not as DRY as calling f32_to_s24 first, but this saves iterating
96+
// over all samples twice.
97+
let int_value = self.clamping_scale(*sample, i24::MIN, i24::MAX) as i32;
98+
i24::from_s24(int_value)
99+
})
100+
.collect()
101+
}
103102

104-
pub fn to_s16(samples: &[f32], requantizer: &mut Requantizer) -> Vec<i16> {
105-
samples
106-
.iter()
107-
.map(|sample| requantizer.scale(*sample, std::i16::MAX as i32) as i16)
108-
.collect()
103+
pub fn f32_to_s16(&mut self, samples: &[f32]) -> Vec<i16> {
104+
samples
105+
.iter()
106+
.map(|sample| self.scale(*sample, std::i16::MAX as i32) as i16)
107+
.collect()
108+
}
109109
}

playback/src/player.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use crate::audio::{
1818
};
1919
use crate::audio_backend::Sink;
2020
use crate::config::{Bitrate, NormalisationMethod, NormalisationType, PlayerConfig};
21-
use crate::convert::Requantizer;
21+
use crate::convert::Converter;
2222
use crate::core::session::Session;
2323
use crate::core::spotify_id::SpotifyId;
2424
use crate::core::util::SeqGenerator;
@@ -60,7 +60,7 @@ struct PlayerInternal {
6060
sink_event_callback: Option<SinkEventCallback>,
6161
audio_filter: Option<Box<dyn AudioFilter + Send>>,
6262
event_senders: Vec<mpsc::UnboundedSender<PlayerEvent>>,
63-
requantizer: Requantizer,
63+
converter: Converter,
6464

6565
limiter_active: bool,
6666
limiter_attack_counter: u32,
@@ -299,7 +299,7 @@ impl Player {
299299
let handle = thread::spawn(move || {
300300
debug!("new Player[{}]", session.session_id());
301301

302-
let requantizer = Requantizer::new((config.ditherer)(), (config.noise_shaper)());
302+
let converter = Converter::new((config.ditherer)(), (config.noise_shaper)());
303303

304304
let internal = PlayerInternal {
305305
session,
@@ -313,7 +313,7 @@ impl Player {
313313
sink_event_callback: None,
314314
audio_filter,
315315
event_senders: [event_sender].to_vec(),
316-
requantizer,
316+
converter,
317317

318318
limiter_active: false,
319319
limiter_attack_counter: 0,
@@ -1288,7 +1288,7 @@ impl PlayerInternal {
12881288
}
12891289
}
12901290

1291-
if let Err(err) = self.sink.write(&packet, &mut self.requantizer) {
1291+
if let Err(err) = self.sink.write(&packet, &mut self.converter) {
12921292
error!("Could not write audio: {}", err);
12931293
self.ensure_sink_stopped(false);
12941294
}

0 commit comments

Comments
 (0)