create a test case for cosine intrinsics

This commit is contained in:
bunnie 2021-09-08 15:10:54 +08:00
parent af016f95cc
commit 7ac8b9100d
4 changed files with 225 additions and 4 deletions

52
Cargo.lock generated
View file

@ -754,6 +754,15 @@ dependencies = [
"miniz_oxide",
]
[[package]]
name = "float-cmp"
version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e1267f4ac4f343772758f7b1bdcbe767c218bbab93bb432acbf5162bbf85a6c4"
dependencies = [
"num-traits",
]
[[package]]
name = "fnv"
version = "1.0.7"
@ -1049,6 +1058,12 @@ dependencies = [
"winapi",
]
[[package]]
name = "libm"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c7d73b3f436185384286bd8098d17ec07c9a7d2388a6599f824d8502b529702a"
[[package]]
name = "libz-sys"
version = "1.1.3"
@ -1133,6 +1148,16 @@ dependencies = [
"autocfg",
]
[[package]]
name = "microfft"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b04ffc2e74da5513185343835177bfd4adf718986185c6d81c2ae2c6ebc07637"
dependencies = [
"num-complex",
"static_assertions",
]
[[package]]
name = "minifb"
version = "0.19.3"
@ -1193,6 +1218,15 @@ dependencies = [
"version_check",
]
[[package]]
name = "num-complex"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "26873667bbbb7c5182d4a37c1add32cdf09f841af72da53318fdb81543c15085"
dependencies = [
"num-traits",
]
[[package]]
name = "num-derive"
version = "0.3.3"
@ -1838,6 +1872,7 @@ dependencies = [
"root-keys",
"rtc",
"sha2",
"spectrum-analyzer",
"spinor",
"susres",
"ticktimer-server",
@ -1878,6 +1913,17 @@ dependencies = [
"winapi",
]
[[package]]
name = "spectrum-analyzer"
version = "0.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4b3c55cf9ba96815eb07435b381048ea2d031b21223b850bdba7d401eaae6388"
dependencies = [
"float-cmp",
"libm",
"microfft",
]
[[package]]
name = "spin"
version = "0.5.2"
@ -1906,6 +1952,12 @@ dependencies = [
"xous-names",
]
[[package]]
name = "static_assertions"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f"
[[package]]
name = "status"
version = "0.1.0"

View file

@ -39,6 +39,9 @@ num-derive = {version = "0.3", default-features = false}
num-traits = {version = "0.2", default-features = false}
rkyv = {version = "0.4.3", default-features = false, features = ["const_generics"]}
# for audio self-test analysis
spectrum-analyzer = "0.5.2"
# for the curve25519 tests
# hardware acceleration adaptations are inserted into a fork of the main branch.
rand_core = "0.5"

View file

@ -160,7 +160,7 @@ impl CmdEnv {
common_env: common,
lastverb: String::<256>::new(),
///// 3. initialize your storage, by calling new()
test_cmd: Test::new(),
test_cmd: Test::new(&xns),
sleep_cmd: Sleep::new(&xns),
sensors_cmd: Sensors::new(),
callback_cmd: CallBack::new(),

View file

@ -1,18 +1,48 @@
use crate::{ShellCmdApi,CommonEnv};
use xous_ipc::String;
use xous::MessageEnvelope;
use codec::*;
use spectrum_analyzer::{samples_fft_to_spectrum, FrequencyLimit};
use spectrum_analyzer::windows::hann_window;
#[derive(Debug)]
pub struct Test {
state: u32
state: u32,
codec: codec::Codec,
recbuf: xous::MemoryRange,
callback_id: Option<u32>,
callback_conn: u32,
framecount: u32,
play_sample: f32, // count of play samples generated. in f32 to avoid int<->f32 conversions
rec_sample: usize, // count of record samples recorded. in usize because we're not doing f32 wave table computations on this
}
impl Test {
pub fn new() -> Self {
pub fn new(xns: &xous_names::XousNames) -> Self {
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");
Test {
state: 0
codec,
recbuf,
state: 0,
callback_id: None,
callback_conn: xns.request_connection_blocking(crate::SERVER_NAME_SHELLCHAT).unwrap(),
framecount: 0,
play_sample: 0.0,
rec_sample: 0,
}
}
}
const SAMPLE_RATE_HZ: f32 = 8000.0;
impl<'a> ShellCmdApi<'a> for Test {
cmd_api!(test);
@ -48,6 +78,73 @@ impl<'a> ShellCmdApi<'a> for Test {
write!(ret, "Factory test script has run, check serial terminal for output").unwrap();
env.llio.wfi_override(false).unwrap();
}
"audio" => {
self.codec.setup_8k_stream().expect("couldn't set the CODEC to expected defaults");
env.ticktimer.sleep_ms(50).unwrap();
if self.callback_id.is_none() {
let cb_id = env.register_handler(String::<256>::from_str(self.verb()));
log::trace!("hooking frame callback with ID {}", cb_id);
self.codec.hook_frame_callback(cb_id, self.callback_conn).unwrap(); // any non-handled IDs get routed to our callback port
self.callback_id = Some(cb_id);
}
self.play_sample = 0.0;
self.rec_sample = 0;
log::info!("starting playback");
self.codec.resume().unwrap();
env.ticktimer.sleep_ms(4000).unwrap();
self.codec.pause().unwrap(); // this should stop callbacks from occurring too.
write!(ret, "Playback stopped at {} frames.", self.framecount).unwrap();
self.framecount = 0;
self.play_sample = 0.0;
self.rec_sample = 0;
self.codec.power_off().unwrap();
// now do FFT analysis on the sample buffer
// analyze one channel at a time
let mut left_samples = Vec::<f32>::new();
let mut right_samples = Vec::<f32>::new();
for &sample in self.recbuf.as_slice::<u32>().iter() {
left_samples.push( ((sample & 0xFFFF) as i16) as f32 );
right_samples.push( (((sample >> 16) & 0xFFFF) as i16) as f32 );
}
let hann_left = hann_window(&left_samples);
let hann_right = hann_window(&right_samples);
let spectrum_left = samples_fft_to_spectrum(
&hann_left,
SAMPLE_RATE_HZ as _,
FrequencyLimit::All,
None,
None
);
let spectrum_right = samples_fft_to_spectrum(
&hann_right,
SAMPLE_RATE_HZ as _,
FrequencyLimit::All,
None,
None
);
log::info!("left");
for (fr, fr_val) in spectrum_left.data().iter() {
log::info!("{}Hz => {}", fr, fr_val)
}
log::info!("right");
for (fr, fr_val) in spectrum_right.data().iter() {
log::info!("{}Hz => {}", fr, fr_val)
}
/*
Left off notes:
- simplify to single tones played for 3 seconds
- one tone each for hp left, hp right, and speaker
- analysis only needs to happen on one channel, as mic is mono. pick one, see if it works.
- in addition to frequency analysis, need to pick amplitude threshold
- need to integrate spectrum and calculate %age of total power in a given bin for a pass/fail criteria
*/
}
"devboot" => {
env.gam.set_devboot(true).unwrap();
write!(ret, "devboot on").unwrap();
@ -64,6 +161,75 @@ impl<'a> ShellCmdApi<'a> for Test {
}
Ok(Some(ret))
}
fn callback(&mut self, msg: &MessageEnvelope, _env: &mut CommonEnv) -> Result<Option<String::<1024>>, xous::Error> {
const OMEGA1: f32 = 440.0 * 2.0 * std::f32::consts::PI / SAMPLE_RATE_HZ; // A4
const OMEGA2: f32 = 329.63 * 2.0 * std::f32::consts::PI / SAMPLE_RATE_HZ; // E4
const OMEGA3: f32 = 261.63 * 2.0 * std::f32::consts::PI / SAMPLE_RATE_HZ; // C4
const AMPLITUDE: f32 = 0.707;
log::debug!("audio callback");
xous::msg_scalar_unpack!(msg, free_play, _avail_rec, _, _, {
log::debug!("{} extending playback", free_play);
let mut frames: FrameRing = FrameRing::new();
let frames_to_push = if frames.writeable_count() < free_play {
frames.writeable_count()
} else {
free_play
};
self.framecount += frames_to_push as u32;
log::debug!("f{} p{}", self.framecount, frames_to_push);
for _ in 0..frames_to_push {
let mut frame: [u32; codec::FIFO_DEPTH] = [ZERO_PCM as u32 | (ZERO_PCM as u32) << 16; codec::FIFO_DEPTH];
// put the "expensive" f32 comparison outside the cosine wave table computation loop
if self.play_sample < SAMPLE_RATE_HZ { // 1 second A4
for sample in frame.iter_mut() {
// left channel, A4 note
let raw_sine: i16 = (AMPLITUDE * f32::cos( self.play_sample * OMEGA1 ) * i16::MAX as f32) as i16;
*sample = raw_sine as u32 | (ZERO_PCM as u32) << 16;
self.play_sample += 1.0;
}
} else if self.play_sample < SAMPLE_RATE_HZ * 2.0 {
for sample in frame.iter_mut() { // next second E4
// right channel, E4 note
let raw_sine: i16 = (AMPLITUDE * f32::cos( self.play_sample * OMEGA2 ) * i16::MAX as f32) as i16;
*sample = ZERO_PCM as u32 | (raw_sine as u32) << 16;
self.play_sample += 1.0;
}
} else {
for sample in frame.iter_mut() { // rest C4
// both channels, C4 note
let raw_sine: i16 = (AMPLITUDE * f32::cos( self.play_sample * OMEGA3 ) * i16::MAX as f32) as i16;
*sample = raw_sine as u32 | (raw_sine as u32) << 16;
self.play_sample += 1.0;
}
}
frames.nq_frame(frame).unwrap();
}
self.codec.swap_frames(&mut frames).unwrap();
let rec_samples = self.recbuf.as_slice_mut::<u32>();
let rec_len = rec_samples.len();
loop {
if let Some(frame) = frames.dq_frame() {
for sample in rec_samples.iter_mut() {
*sample = frame[self.rec_sample];
// increment and wrap around on overflow
// we should be sampling a continuous tone, so we'll get a small phase discontinutity once in the buffer.
// should be no problem for the analysis phase.
self.rec_sample += 1;
if self.rec_sample > rec_len {
self.rec_sample = 0;
}
}
} else {
break;
};
}
});
Ok(None)
}
}