Small clean up

This commit is contained in:
Rob Watson 2020-09-09 11:12:49 +02:00
parent 89866f944b
commit 4a951c6035
1 changed files with 29 additions and 33 deletions

View File

@ -69,12 +69,9 @@ impl Canvas {
fn redraw_canvas(&mut self) {
let canvas_element = self.canvas_node.cast::<HtmlCanvasElement>().unwrap();
// canvas_element.clear(); TODO
let width = canvas_element.width();
let height = canvas_element.height();
// TODO: error handling
let context = canvas_element
.get_context("2d")
.unwrap()
@ -84,37 +81,36 @@ impl Canvas {
context.set_fill_style(&"black".into());
context.set_stroke_style(&"green".into());
context.fill_rect(0.0, 0.0, width as f64, height as f64);
context.fill_rect(0.0, 0.0, canvas_element.width() as f64, height as f64);
// TODO: improve
if self.audio_data.is_none() {
return;
if self.audio_data.is_some() {
let audio_data = self.audio_data.as_ref().unwrap();
// TODO: multi-channel
let _num_channels = audio_data.number_of_channels(); // 1
let channel_data = audio_data.get_channel_data(0).unwrap();
let chunks = utils::chunks_fixed(&channel_data, canvas_element.width() as usize);
chunks.enumerate().for_each(|(i, chunk)| {
let max = chunk
.iter()
.map(|v| (v * 32767.0) as i32)
.map(|v| v.abs())
.max()
.unwrap();
let pc = (max as f64 / 32767.0) * 100.0;
let len = (height as f64 / 100.0 * pc).floor();
let mid = height as f64 / 2f64;
context.begin_path();
context.move_to(i as f64, mid - (len / 2f64));
context.line_to(i as f64, mid + (len / 2f64));
context.stroke();
// ConsoleService::log(&format!("index {}: max {}, pc {}, len {}", i, max, pc, len));
});
}
let audio_data = self.audio_data.as_ref().unwrap();
// TODO: multi-channel
let _num_channels = audio_data.number_of_channels(); // 1
let channel_data = audio_data.get_channel_data(0).unwrap();
let chunks = utils::chunks_fixed(&channel_data, width as usize);
chunks.enumerate().for_each(|(i, chunk)| {
let max = chunk
.iter()
.map(|v| (v * 32767.0) as i32)
.map(|v| v.abs())
.max()
.unwrap();
let pc = (max as f64 / 32767.0) * 100.0;
let len = (height as f64 / 100.0 * pc).floor();
let mid = height as f64 / 2f64;
context.begin_path();
context.move_to(i as f64, mid - (len / 2f64));
context.line_to(i as f64, mid + (len / 2f64));
context.stroke();
// ConsoleService::log(&format!("index {}: max {}, pc {}, len {}", i, max, pc, len));
});
}
}