make bark-protocol no_std (alloc only)

This commit is contained in:
Hailey Somerville 2023-09-14 17:20:47 +10:00
parent 13e072d67c
commit 3e7dbf7726
4 changed files with 17 additions and 13 deletions

View file

@ -1,3 +1,6 @@
#![no_std]
extern crate alloc;
pub mod time; pub mod time;
pub mod types; pub mod types;
pub mod packet; pub mod packet;

View file

@ -1,4 +1,7 @@
use std::mem::size_of; use core::cmp;
use core::mem::size_of;
use core::fmt::{self, Debug};
use core::ops::Range;
use bytemuck::Zeroable; use bytemuck::Zeroable;
@ -15,12 +18,12 @@ pub const MAX_PACKET_SIZE: usize =
size_of::<types::AudioPacketBuffer>(); size_of::<types::AudioPacketBuffer>();
pub struct PacketBuffer { pub struct PacketBuffer {
raw: Box<[u8]>, raw: alloc::boxed::Box<[u8]>,
len: usize, len: usize,
} }
impl std::fmt::Debug for PacketBuffer { impl Debug for PacketBuffer {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "PacketBuffer {{ len = {}; {:x?} }}", self.len, &self.raw[0..self.len]) write!(f, "PacketBuffer {{ len = {}; {:x?} }}", self.len, &self.raw[0..self.len])
} }
} }
@ -213,7 +216,7 @@ impl AudioWriter {
pub fn write(&mut self, audio: &[f32]) -> SampleDuration { pub fn write(&mut self, audio: &[f32]) -> SampleDuration {
let input_duration = SampleDuration::from_buffer_offset(audio.len()); let input_duration = SampleDuration::from_buffer_offset(audio.len());
let copy_duration = std::cmp::min(input_duration, self.remaining()); let copy_duration = cmp::min(input_duration, self.remaining());
let copy_len = copy_duration.as_buffer_offset(); let copy_len = copy_duration.as_buffer_offset();
let source_buffer = &audio[0..copy_len]; let source_buffer = &audio[0..copy_len];
@ -247,7 +250,7 @@ impl Time {
// time packets are padded so that they are // time packets are padded so that they are
// the same length as audio packets: // the same length as audio packets:
const DATA_RANGE: std::ops::Range<usize> = const DATA_RANGE: Range<usize> =
0..size_of::<types::TimePacket>(); 0..size_of::<types::TimePacket>();
pub fn allocate() -> Self { pub fn allocate() -> Self {

View file

@ -1,5 +1,5 @@
use crate::packet; use crate::packet;
use crate::types::{TimestampMicros}; use crate::types::TimestampMicros;
use crate::{SAMPLE_RATE, FRAMES_PER_PACKET, CHANNELS}; use crate::{SAMPLE_RATE, FRAMES_PER_PACKET, CHANNELS};
/// A timestamp with implicit denominator SAMPLE_RATE /// A timestamp with implicit denominator SAMPLE_RATE
@ -57,16 +57,16 @@ impl SampleDuration {
SampleDuration(samples) SampleDuration(samples)
} }
pub fn from_std_duration_lossy(duration: std::time::Duration) -> SampleDuration { pub fn from_std_duration_lossy(duration: core::time::Duration) -> SampleDuration {
let duration = (duration.as_micros() * u128::from(SAMPLE_RATE)) / 1_000_000; let duration = (duration.as_micros() * u128::from(SAMPLE_RATE)) / 1_000_000;
let duration = u64::try_from(duration).expect("can't narrow duration to u64"); let duration = u64::try_from(duration).expect("can't narrow duration to u64");
SampleDuration(duration) SampleDuration(duration)
} }
pub fn to_std_duration_lossy(&self) -> std::time::Duration { pub fn to_std_duration_lossy(&self) -> core::time::Duration {
let usecs = (u128::from(self.0) * 1_000_000) / u128::from(SAMPLE_RATE); let usecs = (u128::from(self.0) * 1_000_000) / u128::from(SAMPLE_RATE);
let usecs = u64::try_from(usecs).expect("can't narrow usecs to u64"); let usecs = u64::try_from(usecs).expect("can't narrow usecs to u64");
std::time::Duration::from_micros(usecs) core::time::Duration::from_micros(usecs)
} }
pub fn as_buffer_offset(&self) -> usize { pub fn as_buffer_offset(&self) -> usize {

View file

@ -1,5 +1,3 @@
use std::time::Duration;
use bitflags::bitflags; use bitflags::bitflags;
use bytemuck::{Zeroable, Pod}; use bytemuck::{Zeroable, Pod};
@ -116,7 +114,7 @@ impl ReceiverStats {
self.flags.insert(ReceiverStatsFlags::HAS_BUFFER_LENGTH); self.flags.insert(ReceiverStatsFlags::HAS_BUFFER_LENGTH);
} }
pub fn set_network_latency(&mut self, latency: Duration) { pub fn set_network_latency(&mut self, latency: core::time::Duration) {
self.network_latency = latency.as_micros() as f64 / 1_000_000.0; self.network_latency = latency.as_micros() as f64 / 1_000_000.0;
self.flags.insert(ReceiverStatsFlags::HAS_NETWORK_LATENCY); self.flags.insert(ReceiverStatsFlags::HAS_NETWORK_LATENCY);
} }