updates from testing

This commit is contained in:
sigil-03 2025-11-06 19:11:29 -07:00
parent ba25b7c89f
commit 99ce71e8d0

View file

@ -5,7 +5,6 @@ pub enum Direction {
Down, Down,
} }
pub struct DpcmSample { pub struct DpcmSample {
direction: Direction, direction: Direction,
// only 3 bits // only 3 bits
@ -60,8 +59,8 @@ impl<'a, T: DacInterface> Dac<'a, T> {
if (self.index >= data.len()) { if (self.index >= data.len()) {
self.index = 0; self.index = 0;
} }
self.output.write_amplitude(data[self.index]); self.output.write_amplitude(data[self.index]);
// self.output.write_amplitude(100); // self.output.write_amplitude(100);
} }
} }
} }
@ -84,8 +83,8 @@ impl<'a, T: DacInterface> DpcmDac<'a, T> {
index: 0, index: 0,
prev_amplitude: 0x80, prev_amplitude: 0x80,
step_size: 0x3, step_size: 0x3,
min: 0x19, min: 0x25,
max: 0xe6, max: 0xc8,
} }
} }
pub fn load_data(&mut self, data: &'a [u8]) { pub fn load_data(&mut self, data: &'a [u8]) {
@ -102,31 +101,34 @@ impl<'a, T: DacInterface> DpcmDac<'a, T> {
let sample = DpcmSample::new(data[self.index / 2], sub_index); let sample = DpcmSample::new(data[self.index / 2], sub_index);
let new_amplitude = match sample.direction { let new_amplitude = match sample.direction {
Direction::Down => self.prev_amplitude.saturating_sub(sample.step_count as usize * self.step_size).max(self.min), Direction::Down => self
Direction ::Up => (self.prev_amplitude + (sample.step_count as usize * self.step_size)).min(self.max), .prev_amplitude
.saturating_sub(sample.step_count as usize * self.step_size)
.max(self.min),
Direction::Up => (self.prev_amplitude
+ (sample.step_count as usize * self.step_size))
.min(self.max),
}; };
// calculate normalized amplitude and write out // calculate normalized amplitude and write out
let normalized_amplitude = (new_amplitude - self.min) * 100 / (self.max - self.min); let normalized_amplitude = (new_amplitude - self.min) * 100 / (self.max - self.min);
self.output.write_amplitude(normalized_amplitude as u8); self.output.write_amplitude(normalized_amplitude as u8);
self.prev_amplitude = new_amplitude; self.prev_amplitude = new_amplitude;
// increment the sample index // increment the sample index
let mut samples_remaining = true; let mut samples_remaining = true;
self.index = self.index + 1; self.index = self.index + 1;
// reset the index to 0 if we roll over // reset the index to 0 if we roll over
if (self.index >= data.len() * 2) { if (self.index >= data.len() * 2) {
self.index = 0; self.index = 0;
self.prev_amplitude = 0x74; self.prev_amplitude = 0x74;
samples_remaining = false; samples_remaining = false;
} }
// return whether or not we have samples remaining // return whether or not we have samples remaining
samples_remaining samples_remaining
} } else {
else {
// if the sample failed to load, we (duh) have no more samples remaining // if the sample failed to load, we (duh) have no more samples remaining
false false
} }
@ -137,8 +139,6 @@ impl<'a, T: DacInterface> DpcmDac<'a, T> {
} }
} }
pub struct DpcmDecoder<'a> { pub struct DpcmDecoder<'a> {
data: Option<&'a [u8]>, data: Option<&'a [u8]>,
index: usize, index: usize,
@ -153,10 +153,10 @@ impl<'a> DpcmDecoder<'a> {
Self { Self {
data: None, data: None,
index: 0, index: 0,
prev_amplitude: 0x80, prev_amplitude: 0x0,
step_size: 0x3, step_size: 0x3,
min: 0x19, min: 0x50,
max: 0xe6, max: 0xaa,
} }
} }
pub fn load_data(&mut self, data: &'a [u8]) { pub fn load_data(&mut self, data: &'a [u8]) {
@ -166,26 +166,39 @@ impl<'a> DpcmDecoder<'a> {
self.index = index; self.index = index;
} }
pub fn is_done(&self) -> bool {
if let Some(data) = self.data {
self.index >= (data.len() * 2) - 1
} else {
false
}
}
// output the next sample's amplitude // output the next sample's amplitude
pub fn output_next(&mut self) -> usize { pub fn output_next(&mut self) -> usize {
if let Some(data) = self.data { if let Some(data) = self.data {
if (self.index >= data.len() * 2) { if self.index >= (data.len() * 2) - 10 {
return 0; return 0;
} }
let sub_index = 1 - self.index % 2; let sub_index = 1 - self.index % 2;
let sample = DpcmSample::new(data[self.index / 2], sub_index); let sample = DpcmSample::new(data[self.index / 2], sub_index);
let new_amplitude = match sample.direction { let new_amplitude = match sample.direction {
Direction::Down => self.prev_amplitude.saturating_sub(sample.step_count as usize * self.step_size).max(self.min), Direction::Down => self
Direction ::Up => (self.prev_amplitude + (sample.step_count as usize * self.step_size)).min(self.max), .prev_amplitude
.saturating_sub(sample.step_count as usize * self.step_size)
.max(self.min),
Direction::Up => (self.prev_amplitude
+ (sample.step_count as usize * self.step_size))
.min(self.max),
}; };
// calculate normalized amplitude // calculate normalized amplitude
// TODO: e4c811653781e69e40b63fd27a8c1e20 // TODO: e4c811653781e69e40b63fd27a8c1e20
let normalized_amplitude = (new_amplitude - self.min) * 100 / (self.max - self.min); let normalized_amplitude =
self.prev_amplitude = new_amplitude; (new_amplitude.saturating_sub(self.min)) * 100 / (self.max - self.min);
self.prev_amplitude = new_amplitude;
// increment the sample index // increment the sample index
let mut samples_remaining = true; let mut samples_remaining = true;
@ -196,13 +209,13 @@ impl<'a> DpcmDecoder<'a> {
// self.index = 0; // self.index = 0;
// self.prev_amplitude = 0x74; // self.prev_amplitude = 0x74;
// samples_remaining = false; // samples_remaining = false;
// } // }
// return the normalized amplitude // return the normalized amplitude
return normalized_amplitude; return normalized_amplitude;
} else { } else {
// otherwise just output 0 // otherwise just output 0
0 0
} }
} }
} }