From bccdc8221e0948075ee358f82597cc00d69c22fb Mon Sep 17 00:00:00 2001 From: piper <107778087+awtterpip@users.noreply.github.com> Date: Mon, 30 Jan 2023 21:16:39 -0600 Subject: [PATCH] feat: audio! --- src/core/client.rs | 3 +- src/main.rs | 4 +- src/nodes/mod.rs | 6 ++ src/nodes/sound.rs | 137 +++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 147 insertions(+), 3 deletions(-) create mode 100644 src/nodes/sound.rs diff --git a/src/core/client.rs b/src/core/client.rs index b52010a..835c855 100644 --- a/src/core/client.rs +++ b/src/core/client.rs @@ -2,7 +2,7 @@ use super::scenegraph::Scenegraph; use crate::{ core::{registry::OwnedRegistry, task}, nodes::{ - data, drawable, fields, hmd, input, items, + data, drawable, fields, hmd, input, items, sound, root::Root, spatial, startup::{self, StartupSettings, STARTUP_SETTINGS}, @@ -103,6 +103,7 @@ impl Client { spatial::create_interface(&client)?; fields::create_interface(&client)?; drawable::create_interface(&client)?; + sound::create_interface(&client)?; data::create_interface(&client)?; items::create_interface(&client)?; input::create_interface(&client)?; diff --git a/src/main.rs b/src/main.rs index 81a794a..4ea601a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,7 +5,7 @@ mod objects; mod wayland; use crate::core::destroy_queue; -use crate::nodes::{drawable, hmd, input}; +use crate::nodes::{drawable, hmd, input, sound}; use crate::objects::input::mouse_pointer::MousePointer; use crate::objects::input::sk_controller::SkController; use crate::objects::input::sk_hand::SkHand; @@ -207,7 +207,7 @@ fn main() -> Result<()> { }); } drawable::draw(sk); - + sound::update(); #[cfg(feature = "wayland")] wayland.make_context_current(); }, diff --git a/src/nodes/mod.rs b/src/nodes/mod.rs index 1468eea..bd830e7 100644 --- a/src/nodes/mod.rs +++ b/src/nodes/mod.rs @@ -8,6 +8,7 @@ pub mod items; pub mod root; pub mod spatial; pub mod startup; +pub mod sound; use color_eyre::eyre::{eyre, Result}; use nanoid::nanoid; @@ -38,6 +39,7 @@ use self::drawable::text::Text; use self::fields::Field; use self::input::{InputHandler, InputMethod}; use self::items::{Item, ItemAcceptor, ItemUI}; +use self::sound::Sound; use self::spatial::zone::Zone; use self::spatial::Spatial; use self::startup::StartupSettings; @@ -80,6 +82,9 @@ pub struct Node { pub item_acceptor: OnceCell>, pub item_ui: OnceCell>, + // Sound + pub sound: OnceCell>, + // Startup pub startup_settings: OnceCell>, } @@ -128,6 +133,7 @@ impl Node { item: OnceCell::new(), item_acceptor: OnceCell::new(), item_ui: OnceCell::new(), + sound: OnceCell::new(), startup_settings: OnceCell::new(), }; node.add_local_signal("destroy", Node::destroy_flex); diff --git a/src/nodes/sound.rs b/src/nodes/sound.rs new file mode 100644 index 0000000..ec1084d --- /dev/null +++ b/src/nodes/sound.rs @@ -0,0 +1,137 @@ +use super::Node; +use crate::core::client::Client; +use crate::core::destroy_queue; +use crate::core::resource::ResourceID; +use crate::core::registry::Registry; +use crate::nodes::spatial::{Spatial, find_spatial_parent, parse_transform}; +use color_eyre::eyre::{ensure, eyre, Result}; +use glam::Vec4Swizzles; +use once_cell::sync::OnceCell; +use parking_lot::Mutex; +use send_wrapper::SendWrapper; +use serde::Deserialize; +use stardust_xr::schemas::flex::deserialize; +use stardust_xr::values::Transform; +use std::ops::DerefMut; +use std::{sync::Arc, path::PathBuf, ffi::OsStr, fmt::Error}; +use stereokit::sound::Sound as SKSound; +use stereokit::sound::SoundInstance; + +static SOUND_REGISTRY: Registry = Registry::new(); + +pub struct Sound { + space: Arc, + resource_id: ResourceID, + pending_audio_path: OnceCell, + instance: Mutex>, + volume: f32, + sk_sound: OnceCell>, +} + +impl Sound { + pub fn add_to(node: &Arc, resource_id: ResourceID) -> Result> { + ensure!( + node.spatial.get().is_some(), + "Internal: Node does not have a spatial attached!" + ); + let sound = Sound { + space: node.spatial.get().unwrap().clone(), + resource_id, + volume: 1.0, + instance: Mutex::new(None), + pending_audio_path: OnceCell::new(), + sk_sound: OnceCell::new(), + }; + node.add_local_signal("play", Sound::play_flex); + node.add_local_signal("stop", Sound::stop_flex); + let sound_arc = SOUND_REGISTRY.add(sound); + let _ = sound_arc.pending_audio_path.set( + sound_arc + .resource_id + .get_file( + &node + .get_client() + .ok_or_else(|| eyre!("Client not found"))? + .base_resource_prefixes + .lock() + .clone(), + &[OsStr::new("wav"), OsStr::new("mp3")] + ) + .ok_or_else(|| eyre!("Resource not found"))?, + ); + let _ = node.sound.set(sound_arc.clone()); + Ok(sound_arc) + } + + fn update(&self) { + if let Some(instance) = self.instance.lock().deref_mut() { + instance.set_position(self.space.global_transform().w_axis.xyz()) + } + } + + fn play_flex(node: &Node, _calling_client: Arc, _data: &[u8]) -> Result<()> { + let sound =node.sound.get().unwrap(); + let sk_sound = sound + .sk_sound + .get_or_try_init(|| -> color_eyre::eyre::Result> { + let pending_audio_path = sound.pending_audio_path.get().ok_or(Error)?; + let sound = SKSound::from_file(pending_audio_path.as_path()).ok_or(Error)?; + + Ok(SendWrapper::new(sound)) + }) + .ok(); + if let Some(sk_sound) = sk_sound { + sk_sound.play_sound(sound.space.global_transform().to_scale_rotation_translation().2, sound.volume); + } + + Ok(()) + } + + pub fn stop_flex(node: &Node, _calling_client: Arc, _data: &[u8]) -> Result<()> { + let sound = node.sound.get().unwrap(); + if let Some(instance) = sound.instance.lock().take() { + instance.stop(); + } + Ok(()) + } +} + +pub fn update() { + for sound in SOUND_REGISTRY.get_valid_contents() { + sound.update() + } +} + +pub fn create_interface(client: &Arc) -> Result<()> { + let node = Node::create(client, "", "audio", false); + node.add_local_signal("create_sound", create_flex); + node.add_to_scenegraph().map(|_| ()) +} + +pub fn create_flex(_node: &Node, calling_client: Arc, data: &[u8]) -> Result<()> { + #[derive(Deserialize)] + struct CreateSoundInfo<'a> { + name: &'a str, + parent_path: &'a str, + transform: Transform, + resource: ResourceID, + } + let info: CreateSoundInfo = deserialize(data)?; + let node = Node::create(&calling_client, "/audio/sounds", info.name, true); + let parent = find_spatial_parent(&calling_client, info.parent_path)?; + let transform = parse_transform(info.transform, true, true, true); + let node = node.add_to_scenegraph()?; + Spatial::add_to(&node, Some(parent), transform, false)?; + Sound::add_to(&node, info.resource)?; + Ok(()) +} + +impl Drop for Sound { + fn drop(&mut self) { + if let Some(instance) = self.instance.lock().take() { + destroy_queue::add(instance); + } + SOUND_REGISTRY.remove(self); + } + +} \ No newline at end of file