lazy-allocate record buffer for tests

no reason to carry 32kiB of data around for factory tests in
"normal" mode.
This commit is contained in:
bunnie 2023-01-02 14:48:36 +08:00
parent e112791915
commit 7c231f0955

View file

@ -20,7 +20,7 @@ pub struct Test {
state: u32, state: u32,
// audio // audio
codec: codec::Codec, codec: codec::Codec,
recbuf: xous::MemoryRange, recbuf: Option<xous::MemoryRange>,
callback_id: Option<u32>, callback_id: Option<u32>,
callback_conn: u32, callback_conn: u32,
framecount: u32, framecount: u32,
@ -44,18 +44,11 @@ impl Test {
pub fn new(xns: &xous_names::XousNames) -> Self { pub fn new(xns: &xous_names::XousNames) -> Self {
let codec = codec::Codec::new(xns).unwrap(); let codec = codec::Codec::new(xns).unwrap();
let recbuf = xous::syscall::map_memory(
None,
None,
0x8000,
xous::MemoryFlags::R | xous::MemoryFlags::W,
).expect("couldn't allocate record buffer");
let callback_conn = xns.request_connection_blocking(crate::SERVER_NAME_SHELLCHAT).unwrap(); let callback_conn = xns.request_connection_blocking(crate::SERVER_NAME_SHELLCHAT).unwrap();
Test { Test {
codec, codec,
recbuf, recbuf: None,
state: 0, state: 0,
callback_id: None, callback_id: None,
callback_conn, callback_conn,
@ -398,11 +391,21 @@ impl<'a> ShellCmdApi<'a> for Test {
// now do FFT analysis on the sample buffer // now do FFT analysis on the sample buffer
// analyze one channel at a time // analyze one channel at a time
let mut right_samples = Vec::<f32>::new(); let mut right_samples = Vec::<f32>::new();
let recslice = self.recbuf.as_slice::<u32>(); if self.recbuf.is_none() { // lazy allocate recbuf
self.recbuf = Some(xous::syscall::map_memory(
None,
None,
0x8000,
xous::MemoryFlags::R | xous::MemoryFlags::W,
).expect("couldn't allocate record buffer"));
}
if let Some(recbuf) = self.recbuf {
let recslice = recbuf.as_slice::<u32>();
for &sample in recslice[recslice.len()-4096..].iter() { for &sample in recslice[recslice.len()-4096..].iter() {
right_samples.push( ((sample & 0xFFFF) as i16) as f32 ); right_samples.push( ((sample & 0xFFFF) as i16) as f32 );
//left_samples.push( (((sample >> 16) & 0xFFFF) as i16) as f32 ); // reminder of how to extract the right channel //left_samples.push( (((sample >> 16) & 0xFFFF) as i16) as f32 ); // reminder of how to extract the right channel
} }
}
// only one channel is considered, because in reality the left is just a copy of the right, as // only one channel is considered, because in reality the left is just a copy of the right, as
// we are taking a mono microphone signal and mixing it into both ADCs // we are taking a mono microphone signal and mixing it into both ADCs
@ -807,7 +810,16 @@ impl<'a> ShellCmdApi<'a> for Test {
self.codec.swap_frames(&mut frames).unwrap(); self.codec.swap_frames(&mut frames).unwrap();
if !AUDIO_OQC.load(Ordering::Relaxed) { if !AUDIO_OQC.load(Ordering::Relaxed) {
let rec_samples = self.recbuf.as_slice_mut::<u32>(); if self.recbuf.is_none() { // lazy allocate recbuf
self.recbuf = Some(xous::syscall::map_memory(
None,
None,
0x8000,
xous::MemoryFlags::R | xous::MemoryFlags::W,
).expect("couldn't allocate record buffer"));
}
if let Some(mut recbuf) = self.recbuf {
let rec_samples = recbuf.as_slice_mut::<u32>();
let rec_len = rec_samples.len(); let rec_len = rec_samples.len();
loop { loop {
if let Some(frame) = frames.dq_frame() { if let Some(frame) = frames.dq_frame() {
@ -825,6 +837,9 @@ impl<'a> ShellCmdApi<'a> for Test {
break; break;
}; };
} }
} else {
panic!("recbuf was not allocated");
}
} else { } else {
let elapsed = env.ticktimer.elapsed_ms(); let elapsed = env.ticktimer.elapsed_ms();
let increment = (elapsed - self.oqc_start) / 500; let increment = (elapsed - self.oqc_start) / 500;