Skip to content

Commit bae1834

Browse files
authored
Fix output on big-endian systems (#778)
1 parent fe2d5ca commit bae1834

File tree

5 files changed

+25
-10
lines changed

5 files changed

+25
-10
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
3636
- [connect] Fix step size on volume up/down events
3737
- [playback] Incorrect `PlayerConfig::default().normalisation_threshold` caused distortion when using dynamic volume normalisation downstream
3838
- [playback] Fix `log` and `cubic` volume controls to be mute at zero volume
39+
- [playback] Fix `S24_3` format on big-endian systems
3940
- [playback] `alsamixer`: make `cubic` consistent between cards that report minimum volume as mute, and cards that report some dB value
4041
- [playback] `alsamixer`: make `--volume-ctrl {linear|log}` work as expected
42+
- [playback] `alsa`, `gstreamer`, `pulseaudio`: always output in native endianness
4143

4244
## [0.2.0] - 2021-05-04
4345

playback/src/audio_backend/alsa.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,12 @@ fn open_device(dev_name: &str, format: AudioFormat) -> Result<(PCM, Frames), Box
4545
AudioFormat::F32 => Format::float(),
4646
AudioFormat::S32 => Format::s32(),
4747
AudioFormat::S24 => Format::s24(),
48-
AudioFormat::S24_3 => Format::S243LE,
4948
AudioFormat::S16 => Format::s16(),
49+
50+
#[cfg(target_endian = "little")]
51+
AudioFormat::S24_3 => Format::S243LE,
52+
#[cfg(target_endian = "big")]
53+
AudioFormat::S24_3 => Format::S243BE,
5054
};
5155

5256
// http://www.linuxjournal.com/article/6735?page=0,1#N0x19ab2890.0x19ba78d8

playback/src/audio_backend/gstreamer.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,14 @@ impl Open for GstreamerSink {
3434
let sample_size = format.size();
3535
let gst_bytes = 2048 * sample_size;
3636

37+
#[cfg(target_endian = "little")]
38+
const ENDIANNESS: &str = "LE";
39+
#[cfg(target_endian = "big")]
40+
const ENDIANNESS: &str = "BE";
41+
3742
let pipeline_str_preamble = format!(
38-
"appsrc caps=\"audio/x-raw,format={}LE,layout=interleaved,channels={},rate={}\" block=true max-bytes={} name=appsrc0 ",
39-
gst_format, NUM_CHANNELS, SAMPLE_RATE, gst_bytes
43+
"appsrc caps=\"audio/x-raw,format={}{},layout=interleaved,channels={},rate={}\" block=true max-bytes={} name=appsrc0 ",
44+
gst_format, ENDIANNESS, NUM_CHANNELS, SAMPLE_RATE, gst_bytes
4045
);
4146
// no need to dither twice; use librespot dithering instead
4247
let pipeline_str_rest = r#" ! audioconvert dithering=none ! autoaudiosink"#;

playback/src/audio_backend/pulseaudio.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ impl Open for PulseAudioSink {
2323

2424
// PulseAudio calls S24 and S24_3 different from the rest of the world
2525
let pulse_format = match format {
26-
AudioFormat::F32 => pulse::sample::Format::F32le,
27-
AudioFormat::S32 => pulse::sample::Format::S32le,
28-
AudioFormat::S24 => pulse::sample::Format::S24_32le,
29-
AudioFormat::S24_3 => pulse::sample::Format::S24le,
30-
AudioFormat::S16 => pulse::sample::Format::S16le,
26+
AudioFormat::F32 => pulse::sample::Format::FLOAT32NE,
27+
AudioFormat::S32 => pulse::sample::Format::S32NE,
28+
AudioFormat::S24 => pulse::sample::Format::S24_32NE,
29+
AudioFormat::S24_3 => pulse::sample::Format::S24NE,
30+
AudioFormat::S16 => pulse::sample::Format::S16NE,
3131
_ => {
3232
unimplemented!("PulseAudio currently does not support {:?} output", format)
3333
}

playback/src/convert.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,12 @@ pub struct i24([u8; 3]);
88
impl i24 {
99
fn from_s24(sample: i32) -> Self {
1010
// trim the padding in the most significant byte
11-
let [a, b, c, _d] = sample.to_le_bytes();
12-
i24([a, b, c])
11+
#[allow(unused_variables)]
12+
let [a, b, c, d] = sample.to_ne_bytes();
13+
#[cfg(target_endian = "little")]
14+
return Self([a, b, c]);
15+
#[cfg(target_endian = "big")]
16+
return Self([b, c, d]);
1317
}
1418
}
1519

0 commit comments

Comments
 (0)