summaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
Diffstat (limited to 'crates')
-rw-r--r--crates/teleia/Cargo.toml2
-rw-r--r--crates/teleia/src/audio.rs18
2 files changed, 14 insertions, 6 deletions
diff --git a/crates/teleia/Cargo.toml b/crates/teleia/Cargo.toml
index 5784ca9..66edbc8 100644
--- a/crates/teleia/Cargo.toml
+++ b/crates/teleia/Cargo.toml
@@ -41,7 +41,7 @@ tracing-wasm = "*" # trace performance in browser
wasm-bindgen = "=0.2.100" # interface with javascript
wasm-bindgen-futures = "*" # interface with async javascript
js-sys = "*" # browser APIs to interact with JS runtime (e.g. run WASM)
-web-sys = { version = "*", features = ["Document", "Window", "Element", "HtmlCanvasElement", "WebGl2RenderingContext", "Headers", "Request", "RequestInit", "RequestMode", "Response", "Performance", "PerformanceTiming", "AudioContext", "AudioNode", "AudioDestinationNode", "AudioBuffer", "AudioBufferSourceNode", "BinaryType", "Blob", "CloseEvent", "ErrorEvent", "FileReader", "MessageEvent", "ProgressEvent", "WebSocket", "Storage"] }
+web-sys = { version = "*", features = ["Document", "Window", "Element", "HtmlCanvasElement", "WebGl2RenderingContext", "Headers", "Request", "RequestInit", "RequestMode", "Response", "Performance", "PerformanceTiming", "AudioContext", "AudioNode", "GainNode", "AudioParam", "AudioDestinationNode", "AudioBuffer", "AudioBufferSourceNode", "BinaryType", "Blob", "CloseEvent", "ErrorEvent", "FileReader", "MessageEvent", "ProgressEvent", "WebSocket", "Storage"] }
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
env_logger = "*" # configurable logging to stdout
diff --git a/crates/teleia/src/audio.rs b/crates/teleia/src/audio.rs
index 95bde1f..af2c53f 100644
--- a/crates/teleia/src/audio.rs
+++ b/crates/teleia/src/audio.rs
@@ -21,14 +21,20 @@ impl Context {
#[cfg(target_arch = "wasm32")]
pub struct AudioPlayingHandle {
- node: web_sys::AudioBufferSourceNode
+ node: web_sys::AudioBufferSourceNode,
+ gain: web_sys::GainNode,
}
#[cfg(target_arch = "wasm32")]
impl AudioPlayingHandle {
- pub fn stop(&self) {
+ pub fn stop(&self, _ctx: &Context) {
self.node.stop().expect("failed to stop audio");
}
+ pub fn fade_out(&self, ctx: &Context, time: f32) {
+ let t = ctx.audio.current_time() + time as f64;
+ self.gain.gain().linear_ramp_to_value_at_time(0.0, t).expect("failed to fade out audio");
+ self.node.stop_with_when(t).expect("failed to stop audio while fading out");
+ }
}
#[cfg(target_arch = "wasm32")]
@@ -74,9 +80,11 @@ impl Audio {
if let Some(s) = ms { source.set_loop_start(s) }
if let Some(e) = me { source.set_loop_end(e) }
}
- source.connect_with_audio_node(&ctx.audio.destination()).ok()?;
+ let gain = ctx.audio.create_gain().ok()?;
+ gain.connect_with_audio_node(&ctx.audio.destination()).ok()?;
+ source.connect_with_audio_node(&gain).ok()?;
source.start().ok()?;
- Some(AudioPlayingHandle { node: source })
+ Some(AudioPlayingHandle { node: source, gain })
}
}
@@ -115,7 +123,7 @@ impl Assets {
pub fn play_music(&mut self, name: &str, start: Option<f64>, end: Option<f64>) {
if let Some(s) = &self.music_node {
- let _ = s.stop();
+ let _ = s.stop(&self.ctx);
}
if let Some(a) = self.audio.get(name) {
self.music_node = a.play(&self.ctx, Some((start, end)));