Implement decode_audio_data step

This commit is contained in:
Rob Watson 2020-09-05 14:33:06 +02:00
parent 9fe3c89ce1
commit e98174470e
2 changed files with 50 additions and 12 deletions

View File

@ -9,6 +9,7 @@ crate-type = ["cdylib", "rlib"]
[dependencies] [dependencies]
yew = "0.17" yew = "0.17"
yewtil = "0.3.1"
wasm-bindgen = "0.2" wasm-bindgen = "0.2"
gloo-events = "0.1.0" gloo-events = "0.1.0"
js-sys = "0.3.44" js-sys = "0.3.44"

View File

@ -1,12 +1,18 @@
use web_sys::File; use js_sys::Uint8Array;
use wasm_bindgen::JsValue;
use wasm_bindgen_futures::JsFuture;
use web_sys::{AudioContext, File};
use yew::services::reader::{FileChunk, ReaderTask}; use yew::services::reader::{FileChunk, ReaderTask};
use yew::services::{ConsoleService, ReaderService}; use yew::services::{ConsoleService, ReaderService};
use yew::worker::*; use yew::worker::*;
use yewtil::future::LinkFuture;
pub struct AudioAgent { pub struct AudioAgent {
link: AgentLink<Self>, link: AgentLink<Self>,
reader_service: ReaderService, reader_service: ReaderService,
reader_task: Option<ReaderTask>, reader_task: Option<ReaderTask>,
audio_context: AudioContext,
bytes_buffer: Vec<u8>,
} }
pub enum Request { pub enum Request {
@ -15,6 +21,17 @@ pub enum Request {
pub enum Msg { pub enum Msg {
FileProgress(Option<FileChunk>), FileProgress(Option<FileChunk>),
AudioDecoded(JsValue),
AudioDecodingFailed(JsValue),
}
impl From<Result<JsValue, JsValue>> for Msg {
fn from(item: Result<JsValue, JsValue>) -> Self {
match item {
Ok(samples) => Msg::AudioDecoded(samples),
Err(e) => Msg::AudioDecodingFailed(e),
}
}
} }
impl Agent for AudioAgent { impl Agent for AudioAgent {
@ -24,21 +41,35 @@ impl Agent for AudioAgent {
type Output = (); type Output = ();
fn create(link: AgentLink<Self>) -> Self { fn create(link: AgentLink<Self>) -> Self {
// TODO: where should the AudioContext be initialized and stored?
Self { Self {
link, link,
reader_service: ReaderService::new(), reader_service: ReaderService::new(),
reader_task: None, reader_task: None,
audio_context: AudioContext::new().unwrap(),
bytes_buffer: vec![],
} }
} }
fn update(&mut self, msg: Self::Message) { fn update(&mut self, msg: Self::Message) {
match msg { match msg {
Msg::FileProgress(Some(chunk)) => { Msg::FileProgress(Some(FileChunk::DataChunk { data, progress })) => {
if let Err(e) = self.handle_file_progress(chunk) { if let Err(e) = self.handle_file_progress(data, progress) {
ConsoleService::error(&e); ConsoleService::error(&e);
} }
} }
Msg::FileProgress(None) => (), Msg::FileProgress(Some(FileChunk::Finished)) => {
if let Err(e) = self.handle_file_loaded() {
ConsoleService::error(&e);
}
}
Msg::AudioDecoded(samples) => {
ConsoleService::log(&format!("Success decoding: {:?}", samples));
}
Msg::AudioDecodingFailed(err) => {
ConsoleService::error(&format!("Error decoding: {:?}", err));
}
_ => (),
}; };
} }
@ -51,6 +82,7 @@ impl Agent for AudioAgent {
impl AudioAgent { impl AudioAgent {
fn load_samples_from_file(&mut self, file: File) -> Result<(), String> { fn load_samples_from_file(&mut self, file: File) -> Result<(), String> {
self.bytes_buffer = Vec::with_capacity(file.size() as usize);
let cb = self.link.callback(Msg::FileProgress); let cb = self.link.callback(Msg::FileProgress);
let task = self let task = self
@ -63,14 +95,19 @@ impl AudioAgent {
Ok(()) Ok(())
} }
fn handle_file_progress(&mut self, chunk: FileChunk) -> Result<(), String> { fn handle_file_progress(&mut self, mut data: Vec<u8>, _progress: f32) -> Result<(), String> {
match chunk { self.bytes_buffer.append(&mut data);
FileChunk::Started { name, .. } => ConsoleService::log(&format!("Started: {}", name)),
FileChunk::DataChunk { progress, .. } => {
ConsoleService::log(&format!("Got chunk, progress: {}", progress))
}
FileChunk::Finished => ConsoleService::log("Finished"),
}
Ok(()) Ok(())
} }
fn handle_file_loaded(&self) -> Result<(), String> {
let audio_data = Uint8Array::from(&self.bytes_buffer[..]);
match self.audio_context.decode_audio_data(&audio_data.buffer()) {
Ok(promise) => {
self.link.send_future(JsFuture::from(promise));
Ok(())
}
Err(e) => Err(format!("Error from decode_audio_data: {:?}", e)),
}
}
} }