Skip to content

Commit

Permalink
add linux 24 + 32 bit support
Browse files Browse the repository at this point in the history
  • Loading branch information
julientregoat committed Dec 9, 2020
1 parent 161e690 commit cdc33fd
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 14 deletions.
12 changes: 8 additions & 4 deletions src/host/alsa/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,18 +253,18 @@ impl Device {
let hw_params = alsa::pcm::HwParams::any(&handle)?;

// TODO: check endianess
const FORMATS: [(SampleFormat, alsa::pcm::Format); 3] = [
const FORMATS: [(SampleFormat, alsa::pcm::Format); 5] = [
//SND_PCM_FORMAT_S8,
//SND_PCM_FORMAT_U8,
(SampleFormat::I16, alsa::pcm::Format::S16LE),
//SND_PCM_FORMAT_S16_BE,
(SampleFormat::U16, alsa::pcm::Format::U16LE),
//SND_PCM_FORMAT_U16_BE,
//SND_PCM_FORMAT_S24_LE,
(SampleFormat::I24, alsa::pcm::Format::S24LE),
//SND_PCM_FORMAT_S24_BE,
//SND_PCM_FORMAT_U24_LE,
//SND_PCM_FORMAT_U24_BE,
//SND_PCM_FORMAT_S32_LE,
(SampleFormat::I32, alsa::pcm::Format::S32LE),
//SND_PCM_FORMAT_S32_BE,
//SND_PCM_FORMAT_U32_LE,
//SND_PCM_FORMAT_U32_BE,
Expand All @@ -280,7 +280,7 @@ impl Device {
//SND_PCM_FORMAT_MPEG,
//SND_PCM_FORMAT_GSM,
//SND_PCM_FORMAT_SPECIAL,
//SND_PCM_FORMAT_S24_3LE,
//(SampleFormat::I24, alsa::pcm::Format::S243LE),
//SND_PCM_FORMAT_S24_3BE,
//SND_PCM_FORMAT_U24_3LE,
//SND_PCM_FORMAT_U24_3BE,
Expand Down Expand Up @@ -907,12 +907,16 @@ fn set_hw_params_from_format<'a>(
match sample_format {
SampleFormat::I16 => alsa::pcm::Format::S16BE,
SampleFormat::U16 => alsa::pcm::Format::U16BE,
SampleFormat::I24 => alsa::pcm::Format::S24BE,
SampleFormat::I32 => alsa::pcm::Format::S32BE,
SampleFormat::F32 => alsa::pcm::Format::FloatBE,
}
} else {
match sample_format {
SampleFormat::I16 => alsa::pcm::Format::S16LE,
SampleFormat::U16 => alsa::pcm::Format::U16LE,
SampleFormat::I24 => alsa::pcm::Format::S24LE,
SampleFormat::I32 => alsa::pcm::Format::S32LE,
SampleFormat::F32 => alsa::pcm::Format::FloatLE,
}
};
Expand Down
25 changes: 15 additions & 10 deletions src/samples_formats.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{mem, ops::Deref};
use std::mem;

/// Format that each sample has.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
Expand Down Expand Up @@ -195,21 +195,22 @@ impl Unpacked24 {
const MAX: i32 = 8_388_607;
const MIN: i32 = -8_388_608;

// assumes i24 has been correctly parsed already
/// Assumes bytes has been parsed to a rust i32 previously. Values outside
/// the 24 bit range will be truncated when converting to bytes.
pub fn new(val: i32) -> Self {
Unpacked24(val)
}

pub fn from_be_bytes(b: [u8; 3]) -> Self {
let is_pos = b[0] & 0b1000_0000 == 0;
let extra_byte;
let sign_byte;
if is_pos {
extra_byte = u8::MIN;
sign_byte = u8::MIN;
} else {
extra_byte = u8::MAX;
sign_byte = u8::MAX;
}

Unpacked24(i32::from_be_bytes([extra_byte, b[0], b[1], b[2]]))
Unpacked24(i32::from_be_bytes([sign_byte, b[0], b[1], b[2]]))
}

pub fn to_be_bytes(&self) -> [u8; 3] {
Expand All @@ -224,14 +225,14 @@ impl Unpacked24 {

pub fn from_le_bytes(b: [u8; 3]) -> Self {
let is_pos = b[2] & 0b1000_0000 == 0;
let extra_byte;
let sign_byte;
if is_pos {
extra_byte = u8::MIN;
sign_byte = u8::MIN;
} else {
extra_byte = u8::MAX;
sign_byte = u8::MAX;
}

Unpacked24(i32::from_le_bytes([b[0], b[1], b[2], extra_byte]))
Unpacked24(i32::from_le_bytes([b[0], b[1], b[2], sign_byte]))
}

pub fn to_le_bytes(&self) -> [u8; 3] {
Expand All @@ -243,6 +244,10 @@ impl Unpacked24 {

[byte1, byte2, byte3]
}

pub fn into_inner(&self) -> i32 {
self.0
}
}

impl PartialEq<i8> for Unpacked24 {
Expand Down

0 comments on commit cdc33fd

Please sign in to comment.