2021-09-27 18:46:26 +00:00
|
|
|
use super::{Open, Sink, SinkError, SinkResult};
|
2021-03-12 22:05:38 +00:00
|
|
|
use crate::config::AudioFormat;
|
Implement dithering (#694)
Dithering lowers digital-to-analog conversion ("requantization") error, linearizing output, lowering distortion and replacing it with a constant, fixed noise level, which is more pleasant to the ear than the distortion.
Guidance:
- On S24, S24_3 and S24, the default is to use triangular dithering. Depending on personal preference you may use Gaussian dithering instead; it's not as good objectively, but it may be preferred subjectively if you are looking for a more "analog" sound akin to tape hiss.
- Advanced users who know that they have a DAC without noise shaping have a third option: high-passed dithering, which is like triangular dithering except that it moves dithering noise up in frequency where it is less audible. Note: 99% of DACs are of delta-sigma design with noise shaping, so unless you have a multibit / R2R DAC, or otherwise know what you are doing, this is not for you.
- Don't dither or shape noise on S32 or F32. On F32 it's not supported anyway (there are no integer conversions and so no rounding errors) and on S32 the noise level is so far down that it is simply inaudible even after volume normalisation and control.
New command line option:
--dither DITHER Specify the dither algorithm to use - [none, gpdf,
tpdf, tpdf_hp]. Defaults to 'tpdf' for formats S16
S24, S24_3 and 'none' for other formats.
Notes:
This PR also features some opportunistic improvements. Worthy of mention are:
- matching reference Vorbis sample conversion techniques for lower noise
- a cleanup of the convert API
2021-05-26 19:19:17 +00:00
|
|
|
use crate::convert::Converter;
|
2021-04-13 08:29:34 +00:00
|
|
|
use crate::decoder::AudioPacket;
|
2021-05-31 20:32:39 +00:00
|
|
|
use crate::{NUM_CHANNELS, SAMPLE_RATE};
|
2018-02-26 01:50:41 +00:00
|
|
|
use portaudio_rs::device::{get_default_output_index, DeviceIndex, DeviceInfo};
|
|
|
|
use portaudio_rs::stream::*;
|
2016-07-06 09:54:46 +00:00
|
|
|
use std::process::exit;
|
2016-07-22 22:39:41 +00:00
|
|
|
use std::time::Duration;
|
2016-03-20 16:16:11 +00:00
|
|
|
|
2021-03-12 22:05:38 +00:00
|
|
|
pub enum PortAudioSink<'a> {
|
|
|
|
F32(
|
|
|
|
Option<portaudio_rs::stream::Stream<'a, f32, f32>>,
|
|
|
|
StreamParameters<f32>,
|
|
|
|
),
|
2021-03-13 22:43:24 +00:00
|
|
|
S32(
|
|
|
|
Option<portaudio_rs::stream::Stream<'a, i32, i32>>,
|
|
|
|
StreamParameters<i32>,
|
|
|
|
),
|
2021-03-12 22:05:38 +00:00
|
|
|
S16(
|
|
|
|
Option<portaudio_rs::stream::Stream<'a, i16, i16>>,
|
|
|
|
StreamParameters<i16>,
|
|
|
|
),
|
|
|
|
}
|
2016-03-20 16:16:11 +00:00
|
|
|
|
2019-10-08 09:31:18 +00:00
|
|
|
fn output_devices() -> Box<dyn Iterator<Item = (DeviceIndex, DeviceInfo)>> {
|
2017-04-29 10:41:48 +00:00
|
|
|
let count = portaudio_rs::device::get_count().unwrap();
|
2016-07-06 09:54:46 +00:00
|
|
|
let devices = (0..count)
|
2018-02-26 01:50:41 +00:00
|
|
|
.filter_map(|idx| portaudio_rs::device::get_info(idx).map(|info| (idx, info)))
|
|
|
|
.filter(|&(_, ref info)| info.max_output_channels > 0);
|
2016-07-06 09:54:46 +00:00
|
|
|
|
|
|
|
Box::new(devices)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn list_outputs() {
|
|
|
|
let default = get_default_output_index();
|
|
|
|
|
|
|
|
for (idx, info) in output_devices() {
|
|
|
|
if Some(idx) == default {
|
|
|
|
println!("- {} (default)", info.name);
|
|
|
|
} else {
|
|
|
|
println!("- {}", info.name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn find_output(device: &str) -> Option<DeviceIndex> {
|
|
|
|
output_devices()
|
|
|
|
.find(|&(_, ref info)| info.name == device)
|
|
|
|
.map(|(idx, _)| idx)
|
|
|
|
}
|
|
|
|
|
2018-02-26 01:50:41 +00:00
|
|
|
impl<'a> Open for PortAudioSink<'a> {
|
2021-03-12 22:05:38 +00:00
|
|
|
fn open(device: Option<String>, format: AudioFormat) -> PortAudioSink<'a> {
|
|
|
|
info!("Using PortAudio sink with format: {:?}", format);
|
2016-07-06 09:54:46 +00:00
|
|
|
|
2017-04-29 10:41:48 +00:00
|
|
|
portaudio_rs::initialize().unwrap();
|
2016-03-20 16:16:11 +00:00
|
|
|
|
2021-05-31 20:32:39 +00:00
|
|
|
let device_idx = match device.as_deref() {
|
2016-07-06 09:54:46 +00:00
|
|
|
Some("?") => {
|
|
|
|
list_outputs();
|
|
|
|
exit(0)
|
|
|
|
}
|
|
|
|
Some(device) => find_output(device),
|
|
|
|
None => get_default_output_index(),
|
2019-10-08 09:31:18 +00:00
|
|
|
}
|
2021-03-12 22:05:38 +00:00
|
|
|
.expect("could not find device");
|
2016-07-06 09:54:46 +00:00
|
|
|
|
2017-04-29 10:41:48 +00:00
|
|
|
let info = portaudio_rs::device::get_info(device_idx);
|
2016-07-22 22:39:41 +00:00
|
|
|
let latency = match info {
|
|
|
|
Some(info) => info.default_high_output_latency,
|
|
|
|
None => Duration::new(0, 0),
|
|
|
|
};
|
|
|
|
|
2021-03-12 22:05:38 +00:00
|
|
|
macro_rules! open_sink {
|
2021-03-13 22:43:24 +00:00
|
|
|
($sink: expr, $type: ty) => {{
|
2021-03-12 22:05:38 +00:00
|
|
|
let params = StreamParameters {
|
|
|
|
device: device_idx,
|
|
|
|
channel_count: NUM_CHANNELS as u32,
|
|
|
|
suggested_latency: latency,
|
2021-03-13 22:43:24 +00:00
|
|
|
data: 0.0 as $type,
|
2021-03-12 22:05:38 +00:00
|
|
|
};
|
|
|
|
$sink(None, params)
|
|
|
|
}};
|
|
|
|
}
|
|
|
|
match format {
|
2021-03-16 23:00:27 +00:00
|
|
|
AudioFormat::F32 => open_sink!(Self::F32, f32),
|
|
|
|
AudioFormat::S32 => open_sink!(Self::S32, i32),
|
|
|
|
AudioFormat::S16 => open_sink!(Self::S16, i16),
|
2021-03-18 21:06:43 +00:00
|
|
|
_ => {
|
|
|
|
unimplemented!("PortAudio currently does not support {:?} output", format)
|
|
|
|
}
|
2021-03-12 22:05:38 +00:00
|
|
|
}
|
2016-03-20 16:16:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-26 01:50:41 +00:00
|
|
|
impl<'a> Sink for PortAudioSink<'a> {
|
2021-09-27 18:46:26 +00:00
|
|
|
fn start(&mut self) -> SinkResult<()> {
|
2021-03-12 22:05:38 +00:00
|
|
|
macro_rules! start_sink {
|
2021-03-27 20:13:14 +00:00
|
|
|
(ref mut $stream: ident, ref $parameters: ident) => {{
|
2021-03-12 22:05:38 +00:00
|
|
|
if $stream.is_none() {
|
|
|
|
*$stream = Some(
|
|
|
|
Stream::open(
|
|
|
|
None,
|
|
|
|
Some(*$parameters),
|
|
|
|
SAMPLE_RATE as f64,
|
|
|
|
FRAMES_PER_BUFFER_UNSPECIFIED,
|
Implement dithering (#694)
Dithering lowers digital-to-analog conversion ("requantization") error, linearizing output, lowering distortion and replacing it with a constant, fixed noise level, which is more pleasant to the ear than the distortion.
Guidance:
- On S24, S24_3 and S24, the default is to use triangular dithering. Depending on personal preference you may use Gaussian dithering instead; it's not as good objectively, but it may be preferred subjectively if you are looking for a more "analog" sound akin to tape hiss.
- Advanced users who know that they have a DAC without noise shaping have a third option: high-passed dithering, which is like triangular dithering except that it moves dithering noise up in frequency where it is less audible. Note: 99% of DACs are of delta-sigma design with noise shaping, so unless you have a multibit / R2R DAC, or otherwise know what you are doing, this is not for you.
- Don't dither or shape noise on S32 or F32. On F32 it's not supported anyway (there are no integer conversions and so no rounding errors) and on S32 the noise level is so far down that it is simply inaudible even after volume normalisation and control.
New command line option:
--dither DITHER Specify the dither algorithm to use - [none, gpdf,
tpdf, tpdf_hp]. Defaults to 'tpdf' for formats S16
S24, S24_3 and 'none' for other formats.
Notes:
This PR also features some opportunistic improvements. Worthy of mention are:
- matching reference Vorbis sample conversion techniques for lower noise
- a cleanup of the convert API
2021-05-26 19:19:17 +00:00
|
|
|
StreamFlags::DITHER_OFF, // no need to dither twice; use librespot dithering instead
|
2021-03-12 22:05:38 +00:00
|
|
|
None,
|
|
|
|
)
|
|
|
|
.unwrap(),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
$stream.as_mut().unwrap().start().unwrap()
|
|
|
|
}};
|
2016-08-01 19:20:17 +00:00
|
|
|
}
|
2021-03-27 20:13:14 +00:00
|
|
|
|
2021-03-12 22:05:38 +00:00
|
|
|
match self {
|
2021-03-27 20:13:14 +00:00
|
|
|
Self::F32(stream, parameters) => start_sink!(ref mut stream, ref parameters),
|
|
|
|
Self::S32(stream, parameters) => start_sink!(ref mut stream, ref parameters),
|
|
|
|
Self::S16(stream, parameters) => start_sink!(ref mut stream, ref parameters),
|
2021-03-12 22:05:38 +00:00
|
|
|
};
|
2016-08-01 19:20:17 +00:00
|
|
|
|
2016-03-20 16:16:11 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
2021-03-12 22:05:38 +00:00
|
|
|
|
2021-09-27 18:46:26 +00:00
|
|
|
fn stop(&mut self) -> SinkResult<()> {
|
2021-03-12 22:05:38 +00:00
|
|
|
macro_rules! stop_sink {
|
2021-03-27 20:13:14 +00:00
|
|
|
(ref mut $stream: ident) => {{
|
2021-03-12 22:05:38 +00:00
|
|
|
$stream.as_mut().unwrap().stop().unwrap();
|
|
|
|
*$stream = None;
|
|
|
|
}};
|
|
|
|
}
|
|
|
|
match self {
|
Implement dithering (#694)
Dithering lowers digital-to-analog conversion ("requantization") error, linearizing output, lowering distortion and replacing it with a constant, fixed noise level, which is more pleasant to the ear than the distortion.
Guidance:
- On S24, S24_3 and S24, the default is to use triangular dithering. Depending on personal preference you may use Gaussian dithering instead; it's not as good objectively, but it may be preferred subjectively if you are looking for a more "analog" sound akin to tape hiss.
- Advanced users who know that they have a DAC without noise shaping have a third option: high-passed dithering, which is like triangular dithering except that it moves dithering noise up in frequency where it is less audible. Note: 99% of DACs are of delta-sigma design with noise shaping, so unless you have a multibit / R2R DAC, or otherwise know what you are doing, this is not for you.
- Don't dither or shape noise on S32 or F32. On F32 it's not supported anyway (there are no integer conversions and so no rounding errors) and on S32 the noise level is so far down that it is simply inaudible even after volume normalisation and control.
New command line option:
--dither DITHER Specify the dither algorithm to use - [none, gpdf,
tpdf, tpdf_hp]. Defaults to 'tpdf' for formats S16
S24, S24_3 and 'none' for other formats.
Notes:
This PR also features some opportunistic improvements. Worthy of mention are:
- matching reference Vorbis sample conversion techniques for lower noise
- a cleanup of the convert API
2021-05-26 19:19:17 +00:00
|
|
|
Self::F32(stream, _) => stop_sink!(ref mut stream),
|
|
|
|
Self::S32(stream, _) => stop_sink!(ref mut stream),
|
|
|
|
Self::S16(stream, _) => stop_sink!(ref mut stream),
|
2021-03-12 22:05:38 +00:00
|
|
|
};
|
|
|
|
|
2016-03-20 16:16:11 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
2021-03-12 22:05:38 +00:00
|
|
|
|
2021-12-29 15:26:24 +00:00
|
|
|
fn write(&mut self, packet: AudioPacket, converter: &mut Converter) -> SinkResult<()> {
|
2021-03-12 22:05:38 +00:00
|
|
|
macro_rules! write_sink {
|
2021-03-27 20:13:14 +00:00
|
|
|
(ref mut $stream: expr, $samples: expr) => {
|
2021-03-12 22:05:38 +00:00
|
|
|
$stream.as_mut().unwrap().write($samples)
|
|
|
|
};
|
|
|
|
}
|
2021-03-21 21:16:47 +00:00
|
|
|
|
2021-09-20 17:29:12 +00:00
|
|
|
let samples = packet
|
|
|
|
.samples()
|
2021-09-27 18:46:26 +00:00
|
|
|
.map_err(|e| SinkError::OnWrite(e.to_string()))?;
|
2021-09-20 17:29:12 +00:00
|
|
|
|
2021-03-12 22:05:38 +00:00
|
|
|
let result = match self {
|
2021-03-16 23:00:27 +00:00
|
|
|
Self::F32(stream, _parameters) => {
|
2022-01-04 20:22:52 +00:00
|
|
|
let samples_f32: &[f32] = &converter.f64_to_f32(samples);
|
2021-05-30 18:09:39 +00:00
|
|
|
write_sink!(ref mut stream, samples_f32)
|
2021-03-12 22:05:38 +00:00
|
|
|
}
|
2021-03-16 23:00:27 +00:00
|
|
|
Self::S32(stream, _parameters) => {
|
2022-01-04 20:22:52 +00:00
|
|
|
let samples_s32: &[i32] = &converter.f64_to_s32(samples);
|
2021-03-27 20:13:14 +00:00
|
|
|
write_sink!(ref mut stream, samples_s32)
|
2021-03-13 22:43:24 +00:00
|
|
|
}
|
2021-03-16 23:00:27 +00:00
|
|
|
Self::S16(stream, _parameters) => {
|
2022-01-04 20:22:52 +00:00
|
|
|
let samples_s16: &[i16] = &converter.f64_to_s16(samples);
|
2021-03-27 20:13:14 +00:00
|
|
|
write_sink!(ref mut stream, samples_s16)
|
2021-03-12 22:05:38 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
match result {
|
2016-03-20 16:16:11 +00:00
|
|
|
Ok(_) => (),
|
2018-02-26 01:50:41 +00:00
|
|
|
Err(portaudio_rs::PaError::OutputUnderflowed) => error!("PortAudio write underflow"),
|
2021-03-12 22:05:38 +00:00
|
|
|
Err(e) => panic!("PortAudio error {}", e),
|
2016-03-20 16:16:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-26 01:50:41 +00:00
|
|
|
impl<'a> Drop for PortAudioSink<'a> {
|
2016-03-20 16:16:11 +00:00
|
|
|
fn drop(&mut self) {
|
2017-04-29 10:41:48 +00:00
|
|
|
portaudio_rs::terminate().unwrap();
|
2016-03-20 16:16:11 +00:00
|
|
|
}
|
|
|
|
}
|
2021-05-31 20:32:39 +00:00
|
|
|
|
|
|
|
impl<'a> PortAudioSink<'a> {
|
|
|
|
pub const NAME: &'static str = "portaudio";
|
|
|
|
}
|