use std::num::NonZeroU32;
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct PrefixWord(NonZeroU32);
impl PrefixWord {
pub const OBSERVED_BASE: u32 = 0x0600_0000;
pub const OBSERVED_RATE: u32 = 40_000;
pub const fn new(value: u32) -> Option<Self> {
match NonZeroU32::new(value) {
Some(value) => Some(Self(value)),
None => None,
}
}
pub fn from_elapsed(elapsed: std::time::Duration) -> Self {
let ticks = elapsed
.as_millis()
.saturating_mul(u128::from(Self::OBSERVED_RATE))
/ 1000;
let advanced = Self::OBSERVED_BASE.wrapping_add(u32::try_from(ticks).unwrap_or(u32::MAX));
Self::new(advanced).unwrap_or(Self(NonZeroU32::MIN))
}
pub const fn get(self) -> u32 {
self.0.get()
}
}
pub fn vbr_index(pvbr_payload: &[u8]) -> Vec<u8> {
swap_u32_words(pvbr_payload)
}
const FILE_BEAT_LEN: usize = 8;
const WIRE_BEAT_LEN: usize = 16;
pub fn beat_grid(pqtz_payload: &[u8], prefix: PrefixWord) -> Vec<u8> {
let count = pqtz_payload.len() / FILE_BEAT_LEN;
let mut entries = Vec::with_capacity(count * WIRE_BEAT_LEN);
for entry in pqtz_payload.chunks_exact(FILE_BEAT_LEN) {
let [beat_hi, beat_lo, tempo_hi, tempo_lo, time @ ..] = entry else {
continue;
};
let beat = u16::from_be_bytes([*beat_hi, *beat_lo]);
let tempo = u16::from_be_bytes([*tempo_hi, *tempo_lo]);
let time = u32::from_be_bytes(time.try_into().unwrap_or_default());
entries.extend_from_slice(&beat.to_le_bytes());
entries.extend_from_slice(&tempo.to_le_bytes());
entries.extend_from_slice(&time.to_le_bytes());
entries.extend_from_slice(&[0xff; 8]);
}
let mut out = Vec::with_capacity(20 + entries.len());
for word in [
0x0008_0000,
u32::try_from(count).unwrap_or(u32::MAX),
u32::try_from(entries.len()).unwrap_or(u32::MAX),
1,
prefix.get(),
] {
out.extend_from_slice(&word.to_le_bytes());
}
out.extend_from_slice(&entries);
out
}
pub fn waveform_preview(pwav_payload: &[u8], pwv2_payload: &[u8]) -> Vec<u8> {
let mut out = Vec::with_capacity(pwav_payload.len() * 2 + pwv2_payload.len());
for &packed in pwav_payload {
out.push(packed & 0x1f);
out.push(packed >> 5);
}
out.extend_from_slice(pwv2_payload);
out
}
pub fn waveform_detail(pwv3_payload: &[u8], entry_width: u32, prefix: PrefixWord) -> Vec<u8> {
let len = u32::try_from(pwv3_payload.len()).unwrap_or(u32::MAX);
let mut out = Vec::with_capacity(20 + pwv3_payload.len());
for word in [len, entry_width.max(1), len, 0x96, prefix.get()] {
out.extend_from_slice(&word.to_le_bytes());
}
out.extend_from_slice(pwv3_payload);
out
}
pub const WAVEFORM_FPS: u32 = 150;
pub const CUE_ENTRY_LEN: usize = 0x24;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Cue {
pub order: u16,
pub hot_cue: u16,
pub time_ms: u32,
pub loop_time_ms: u32,
}
impl Cue {
pub const fn frame(self) -> u32 {
self.time_ms.saturating_mul(WAVEFORM_FPS) / 1000
}
}
#[derive(Clone, Debug, PartialEq, Eq, Default)]
pub struct CueBlobs {
pub records: Vec<u8>,
pub count: u32,
pub times: Vec<u8>,
}
pub fn cue_points(cues: &[Cue]) -> CueBlobs {
let mut sorted = cues.to_vec();
sorted.sort_by_key(|cue| cue.time_ms);
let mut records = Vec::with_capacity(sorted.len() * CUE_ENTRY_LEN);
let mut times = Vec::with_capacity(sorted.len() * 8);
for cue in &sorted {
records.extend_from_slice(&cue.order.to_le_bytes());
records.extend_from_slice(&cue.hot_cue.to_le_bytes());
records.extend_from_slice(&0u32.to_le_bytes());
records.extend_from_slice(&0u32.to_le_bytes());
records.extend_from_slice(&cue.frame().to_le_bytes());
records.resize(records.len() + (CUE_ENTRY_LEN - 16), 0);
times.extend_from_slice(&cue.time_ms.to_le_bytes());
times.extend_from_slice(&cue.loop_time_ms.to_le_bytes());
}
CueBlobs {
records,
count: u32::try_from(sorted.len()).unwrap_or(u32::MAX),
times,
}
}
fn swap_u32_words(payload: &[u8]) -> Vec<u8> {
let whole = payload.len() - payload.len() % 4;
let mut out = Vec::with_capacity(payload.len());
for word in payload.chunks_exact(4) {
out.extend(word.iter().rev().copied());
}
out.extend_from_slice(payload.get(whole..).unwrap_or_default());
out
}
#[cfg(test)]
mod tests {
use super::*;
fn prefix() -> PrefixWord {
PrefixWord::new(0x0611_4a48).expect("the value a real deck sent")
}
#[test]
fn a_zero_prefix_word_cannot_be_built() {
assert!(PrefixWord::new(0).is_none());
assert!(PrefixWord::new(1).is_some());
}
#[test]
fn the_prefix_counter_never_starts_at_zero() {
let at_start = PrefixWord::from_elapsed(std::time::Duration::ZERO);
assert_eq!(at_start.get(), PrefixWord::OBSERVED_BASE);
}
#[test]
fn the_prefix_counter_advances_like_the_hardware_did() {
let early = PrefixWord::from_elapsed(std::time::Duration::ZERO);
let late = PrefixWord::from_elapsed(std::time::Duration::from_millis(2580));
assert_eq!(late.get() - early.get(), 103_200, "≈40,000 per second");
}
#[test]
fn the_vbr_index_is_the_payload_with_every_word_swapped() {
let payload = [0x00, 0x00, 0x00, 0x00, 0x12, 0x34, 0x56, 0x78];
assert_eq!(
vbr_index(&payload),
vec![0, 0, 0, 0, 0x78, 0x56, 0x34, 0x12]
);
}
#[test]
fn a_zero_word_is_the_same_in_either_byte_order() {
assert_eq!(vbr_index(&[0; 16]), vec![0; 16]);
}
#[test]
fn a_beat_grid_entry_grows_from_eight_bytes_to_sixteen() {
let payload = [0x00, 0x01, 0x33, 0x90, 0x00, 0x00, 0x01, 0xf4];
let wire = beat_grid(&payload, prefix());
assert_eq!(wire.len(), 20 + WIRE_BEAT_LEN);
let entry = &wire[20..];
assert_eq!(
&entry[0..2],
&1u16.to_le_bytes(),
"beat number, little-endian"
);
assert_eq!(
&entry[2..4],
&13_200u16.to_le_bytes(),
"tempo, little-endian"
);
assert_eq!(&entry[4..8], &500u32.to_le_bytes(), "time, little-endian");
assert_eq!(
&entry[8..16],
&[0xff; 8],
"padded with 0xff, not with zeros"
);
}
#[test]
fn the_beat_grid_prefix_describes_the_entries_that_follow() {
let payload = [0u8; FILE_BEAT_LEN * 3];
let wire = beat_grid(&payload, prefix());
let word =
|index: usize| u32::from_le_bytes(wire[index * 4..index * 4 + 4].try_into().unwrap());
assert_eq!(word(0), 0x0008_0000, "the tag's own constant");
assert_eq!(word(1), 3, "beat count");
assert_eq!(word(2), 3 * 16, "entry-block length");
assert_eq!(word(3), 1);
assert_eq!(word(4), prefix().get());
}
#[test]
fn the_preview_waveform_is_nine_hundred_bytes_not_eight_hundred() {
let preview = [0u8; 400];
let tiny = [0u8; 100];
assert_eq!(waveform_preview(&preview, &tiny).len(), 900);
}
#[test]
fn a_preview_column_splits_into_height_then_whiteness() {
let wire = waveform_preview(&[0b1010_0110], &[]);
assert_eq!(wire, vec![0b0000_0110, 0b0000_0101]);
}
#[test]
fn the_detail_waveform_payload_is_not_reordered() {
let payload: Vec<u8> = (0..=255u8).collect();
let wire = waveform_detail(&payload, 1, prefix());
assert_eq!(
&wire[20..],
payload.as_slice(),
"single bytes have no byte order"
);
let word =
|index: usize| u32::from_le_bytes(wire[index * 4..index * 4 + 4].try_into().unwrap());
assert_eq!(word(0), 256);
assert_eq!(word(1), 1, "entry width");
assert_eq!(word(2), 256);
assert_eq!(word(3), 0x96);
assert_eq!(word(4), prefix().get());
}
#[test]
fn a_cue_time_becomes_a_frame_index_by_truncation() {
assert_eq!(
Cue {
order: 0,
hot_cue: 0,
time_ms: 271,
loop_time_ms: 0
}
.frame(),
40
);
}
#[test]
fn cues_go_out_sorted_by_time_not_in_file_order() {
let cues = [
Cue {
order: 3,
hot_cue: 0,
time_ms: 9000,
loop_time_ms: 0,
},
Cue {
order: 1,
hot_cue: 1,
time_ms: 271,
loop_time_ms: 0,
},
Cue {
order: 2,
hot_cue: 0,
time_ms: 4000,
loop_time_ms: 0,
},
];
let blobs = cue_points(&cues);
assert_eq!(blobs.count, 3);
assert_eq!(blobs.records.len(), 3 * CUE_ENTRY_LEN);
assert_eq!(blobs.times.len(), 3 * 8);
let first_time = u32::from_le_bytes(blobs.times[0..4].try_into().unwrap());
let second_time = u32::from_le_bytes(blobs.times[8..12].try_into().unwrap());
assert_eq!((first_time, second_time), (271, 4000));
let frame = u32::from_le_bytes(blobs.records[12..16].try_into().unwrap());
assert_eq!(frame, 40);
}
#[test]
fn no_cues_is_an_answer_not_an_error() {
assert_eq!(cue_points(&[]), CueBlobs::default());
}
}