Skip to content

Commit

Permalink
Merge pull request #43 from cyb3rpsych0s1s/fix/emitters
Browse files Browse the repository at this point in the history
Fix emitters
  • Loading branch information
Roms1383 authored Dec 22, 2024
2 parents bd9b174 + 8fb7a81 commit 399121c
Show file tree
Hide file tree
Showing 27 changed files with 1,702 additions and 970 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ glam = "0.29"
# kira = { version = "0.9.6", features = ["serde"] }
kira = { git = "https://github.com/Roms1383/kira", branch = "feat/total-duration", features = ["serde"] }
rayon = "1.10"
red4ext-rs = { git = "https://github.com/Roms1383/red4ext-rs", branch = "chore/solo" }
red4ext-rs = { git = "https://github.com/jac3km4/red4ext-rs", rev = "ae1c7b3cbc247b48fa1a90e7a082bede6909d501" }
# red4ext-rs = { git = "https://github.com/Roms1383/red4ext-rs", branch = "chore/solo" }
# red4ext-rs-bindings = { git = "https://github.com/jac3km4/red4ext-rs-bindings", rev = "v0.5.0" }
serde = "1.0"
snafu = "0.8"
Expand Down
1 change: 1 addition & 0 deletions crates/audioware/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ red4ext-rs.workspace = true
rust-ini = "0.21"
snafu.workspace = true
bitflags = "2.6.0"
ahash = "0.8.11"

[build-dependencies]
semver = "1.0"
Expand Down
14 changes: 6 additions & 8 deletions crates/audioware/reds/Debug.reds
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public static exec func TestRegisterEmitter(game: GameInstance) {
let target = GameInstance.GetTargetingSystem(game).GetLookAtObject(GetPlayer(game));
emitterID = target.GetEntityID();
emitterName = n"Jean-Guy";
let added = GameInstance.GetAudioSystemExt(game).RegisterEmitter(emitterID, emitterName);
let added = GameInstance.GetAudioSystemExt(game).RegisterEmitter(emitterID, n"Audioware", emitterName);
FTLog(s"registered? \(added)");
}
/// Game.TestUnregisterEmitter();
Expand All @@ -48,7 +48,7 @@ public static exec func TestUnregisterEmitter(game: GameInstance) {
let target = GameInstance.GetTargetingSystem(game).GetLookAtObject(GetPlayer(game));
emitterID = target.GetEntityID();
emitterName = n"Jean-Guy";
let added = GameInstance.GetAudioSystemExt(game).UnregisterEmitter(emitterID);
let added = GameInstance.GetAudioSystemExt(game).UnregisterEmitter(emitterID, n"Audioware");
FTLog(s"unregistered? \(added)");
}

Expand Down Expand Up @@ -158,9 +158,9 @@ public class AutoEmittersSystem extends ScriptableSystem {
let target = GameInstance.GetTargetingSystem(game).GetLookAtObject(GetPlayer(game));
if !IsDefined(target) { return; }
emitterID = target.GetEntityID();
if GameInstance.GetAudioSystemExt(game).RegisterEmitter(emitterID, emitterCName, settings) {
if GameInstance.GetAudioSystemExt(game).RegisterEmitter(emitterID, n"Audioware", emitterCName, settings) {
FTLog(s"play on emitter: AutoEmittersSystem");
GameInstance.GetAudioSystemExt(game).PlayOnEmitter(eventName, emitterID, emitterCName, ext);
GameInstance.GetAudioSystemExt(game).PlayOnEmitter(eventName, emitterID, n"Audioware", ext);
}
}
private cb func OnPressF1(evt: ref<KeyInputEvent>) {
Expand All @@ -177,10 +177,8 @@ public class AutoEmittersSystem extends ScriptableSystem {
let emitterID: EntityID;
let emitterCName: CName = evt.IsShiftDown() ? n"None" : n"DummyTest";
let tween = new LinearTween();
tween.duration = 0.0;
let ext = new AudioSettingsExt();
ext.fadeIn = tween;
ext.fadeIn = LinearTween.Immediate(2.0);
let settings = new EmitterSettings();
settings.persistUntilSoundsFinish = true;
Expand All @@ -195,7 +193,7 @@ public class AutoEmittersSystem extends ScriptableSystem {
if !IsDefined(target) { return; }
emitterID = target.GetEntityID();
GameInstance.GetAudioSystemExt(this.GetGameInstance()).UnregisterEmitter(emitterID);
GameInstance.GetAudioSystemExt(this.GetGameInstance()).UnregisterEmitter(emitterID, n"Audioware");
}
private cb func OnPressF4(evt: ref<KeyInputEvent>) {
if NotEquals(evt.GetAction(), EInputAction.IACT_Release) { return; }
Expand Down
14 changes: 7 additions & 7 deletions crates/audioware/reds/Ext.reds
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,20 @@ public native class AudioSystemExt {
}
// spatial scene
public final native func RegisterEmitter(entityID: EntityID, opt emitterName: CName, opt emitterSettings: ref<EmitterSettings>) -> Bool;
public final native func UnregisterEmitter(entityID: EntityID) -> Bool;
public final native func IsRegisteredEmitter(entityID: EntityID) -> Bool;
public final native func RegisterEmitter(entityID: EntityID, tagName: CName, opt emitterName: CName, opt emitterSettings: ref<EmitterSettings>) -> Bool;
public final native func UnregisterEmitter(entityID: EntityID, tagName: CName) -> Bool;
public final native func IsRegisteredEmitter(entityID: EntityID, opt tagName: CName) -> Bool;
public final native func EmittersCount() -> Int32;
public final native func PlayOnEmitter(eventName: CName, entityID: EntityID, opt emitterName: CName, opt ext: ref<AudioSettingsExt>) -> Void;
public final native func StopOnEmitter(eventName: CName, entityID: EntityID, opt emitterName: CName, opt tween: ref<Tween>) -> Void;
public final native func PlayOnEmitter(eventName: CName, entityID: EntityID, tagName: CName, opt ext: ref<AudioSettingsExt>) -> Void;
public final native func StopOnEmitter(eventName: CName, entityID: EntityID, tagName: CName, opt tween: ref<Tween>) -> Void;
public final native func OnEmitterDies(entityID: EntityID) -> Void;
public final native func OnEmitterIncapacitated(entityID: EntityID) -> Void;
public final native func OnEmitterDefeated(entityID: EntityID) -> Void;
public final func IsValidEmitter(className: CName) -> Bool = NotEquals(className, n"PlayerPuppet") && Reflection.GetClass(className).IsA(n"gameObject");
public final func PlayOnEmitter(eventName: CName, entityID: EntityID, opt emitterName: CName, opt tween: ref<Tween>) -> Void {
public final func PlayOnEmitter(eventName: CName, entityID: EntityID, tagName: CName, opt tween: ref<Tween>) -> Void {
let settings = new AudioSettingsExt();
settings.fadeIn = tween;
this.PlayOnEmitter(eventName, entityID, emitterName, settings);
this.PlayOnEmitter(eventName, entityID, tagName, settings);
}
// misc
Expand Down
16 changes: 8 additions & 8 deletions crates/audioware/src/abi/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub enum Command {
emitter_name: Option<CName>,
},
Play {
sound_name: CName,
event_name: CName,
entity_id: Option<EntityId>,
emitter_name: Option<CName>,
line_type: Option<ScnDialogLineType>,
Expand All @@ -20,7 +20,7 @@ pub enum Command {
PlayOnEmitter {
event_name: CName,
entity_id: EntityId,
emitter_name: Option<CName>,
tag_name: CName,
ext: Option<Settings>,
},
PlayOverThePhone {
Expand All @@ -31,7 +31,7 @@ pub enum Command {
StopOnEmitter {
event_name: CName,
entity_id: EntityId,
emitter_name: Option<CName>,
tag_name: CName,
tween: Option<Tween>,
},
StopVanilla {
Expand Down Expand Up @@ -65,7 +65,7 @@ impl std::fmt::Debug for Command {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Command::Play {
sound_name,
event_name: sound_name,
entity_id,
emitter_name,
line_type,
Expand All @@ -74,15 +74,15 @@ impl std::fmt::Debug for Command {
Command::PlayOnEmitter {
event_name,
entity_id,
emitter_name,
tag_name,
..
} => write!(f, "Command::PlayOnEmitter {{ event_name: {event_name}, entity_id: {entity_id}, emitter_name: {}, .. }}", emitter_name.map(|x| x.as_str()).unwrap_or("None")),
} => write!(f, "Command::PlayOnEmitter {{ event_name: {event_name}, entity_id: {entity_id}, emitter_name: {}, .. }}", tag_name.as_str()),
Command::StopOnEmitter {
event_name,
entity_id,
emitter_name,
tag_name,
..
} => write!(f, "Command::StopOnEmitter {{ event_name: {event_name}, entity_id: {entity_id}, emitter_name: {}, .. }}", emitter_name.map(|x| x.as_str()).unwrap_or("None")),
} => write!(f, "Command::StopOnEmitter {{ event_name: {event_name}, entity_id: {entity_id}, emitter_name: {}, .. }}", tag_name.as_str()),
Command::Stop {
event_name,
entity_id,
Expand Down
10 changes: 7 additions & 3 deletions crates/audioware/src/abi/lifecycle.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::num::NonZero;

use crossbeam::channel::Sender;
use kira::spatial::emitter::EmitterSettings;
use red4ext_rs::types::{CName, EntityId};
Expand All @@ -14,12 +16,14 @@ pub use system::System;
pub enum Lifecycle {
RegisterEmitter {
entity_id: EntityId,
tag_name: CName,
emitter_name: Option<CName>,
emitter_settings: Option<EmitterSettings>,
emitter_settings: Option<(EmitterSettings, NonZero<u64>)>,
sender: Sender<bool>,
},
UnregisterEmitter {
entity_id: EntityId,
tag_name: CName,
sender: Sender<bool>,
},
OnEmitterDies {
Expand Down Expand Up @@ -66,8 +70,8 @@ pub enum Lifecycle {
impl std::fmt::Display for Lifecycle {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Lifecycle::RegisterEmitter { entity_id, .. } => {
write!(f, "register emitter [{entity_id}]")
Lifecycle::RegisterEmitter { tag_name, .. } => {
write!(f, "register emitter [{}]", tag_name.as_str())
}
Lifecycle::UnregisterEmitter { entity_id, .. } => {
write!(f, "unregister emitter [{entity_id}]")
Expand Down
61 changes: 43 additions & 18 deletions crates/audioware/src/abi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,11 +267,12 @@ pub trait SceneLifecycle {
fn register_emitter(
&self,
entity_id: EntityId,
tag_name: CName,
emitter_name: Opt<CName>,
emitter_settings: Ref<EmitterSettings>,
) -> bool;
fn unregister_emitter(&self, entity_id: EntityId) -> bool;
fn is_registered_emitter(&self, entity_id: EntityId) -> bool;
fn unregister_emitter(&self, entity_id: EntityId, tag_name: CName) -> bool;
fn is_registered_emitter(&self, entity_id: EntityId, tag_name: Opt<CName>) -> bool;
fn on_emitter_dies(&self, entity_id: EntityId);
fn on_emitter_incapacitated(&self, entity_id: EntityId);
fn on_emitter_defeated(&self, entity_id: EntityId);
Expand All @@ -282,14 +283,22 @@ impl SceneLifecycle for AudioSystemExt {
fn register_emitter(
&self,
entity_id: EntityId,
tag_name: CName,
emitter_name: Opt<CName>,
emitter_settings: Ref<EmitterSettings>,
) -> bool {
use crate::engine::ToDistances;
if tag_name.as_str() == "None" || tag_name.as_str().is_empty() {
warns!("invalid tag name: \"{tag_name}\"");
return false;
}
let (sender, receiver) = bounded(0);
let emitter_settings = emitter_settings.into_settings_ext(entity_id.to_distances());
queue::notify(Lifecycle::RegisterEmitter {
tag_name,
entity_id,
emitter_name: emitter_name.into_option(),
emitter_settings: emitter_settings.into_settings(),
emitter_settings,
sender,
});
if let Ok(registered) = receiver.recv() {
Expand All @@ -298,17 +307,25 @@ impl SceneLifecycle for AudioSystemExt {
false
}

fn unregister_emitter(&self, entity_id: EntityId) -> bool {
fn unregister_emitter(&self, entity_id: EntityId, tag_name: CName) -> bool {
if tag_name.as_str() == "None" || tag_name.as_str().is_empty() {
warns!("invalid tag name: \"{tag_name}\"");
return false;
}
let (sender, receiver) = bounded(0);
queue::notify(Lifecycle::UnregisterEmitter { entity_id, sender });
queue::notify(Lifecycle::UnregisterEmitter {
entity_id,
tag_name,
sender,
});
if let Ok(unregistered) = receiver.recv() {
return unregistered;
}
false
}

fn is_registered_emitter(&self, entity_id: EntityId) -> bool {
Engine::<CpalBackend>::is_registered_emitter(entity_id)
fn is_registered_emitter(&self, entity_id: EntityId, tag_name: Opt<CName>) -> bool {
Engine::<CpalBackend>::is_registered_emitter(entity_id, tag_name.into_option())
}

fn on_emitter_dies(&self, entity_id: EntityId) {
Expand All @@ -331,7 +348,7 @@ impl SceneLifecycle for AudioSystemExt {
pub trait ExtCommand {
fn play(
&self,
sound_name: CName,
event_name: CName,
entity_id: Opt<EntityId>,
emitter_name: Opt<CName>,
line_type: Opt<ScnDialogLineType>,
Expand All @@ -348,16 +365,16 @@ pub trait ExtCommand {
/// Play sound on audio emitter with optional [tween][Tween].
fn play_on_emitter(
&self,
sound_name: CName,
event_name: CName,
entity_id: EntityId,
emitter_name: Opt<CName>,
tag_name: Opt<CName>,
ext: Ref<AudioSettingsExt>,
);
fn stop_on_emitter(
&self,
sound_name: CName,
event_name: CName,
entity_id: EntityId,
emitter_name: Opt<CName>,
tag_name: Opt<CName>,
tween: Ref<Tween>,
);
fn switch(
Expand All @@ -374,7 +391,7 @@ pub trait ExtCommand {
impl ExtCommand for AudioSystemExt {
fn play(
&self,
sound_name: CName,
event_name: CName,
entity_id: Opt<EntityId>,
emitter_name: Opt<CName>,
line_type: Opt<ScnDialogLineType>,
Expand All @@ -386,7 +403,7 @@ impl ExtCommand for AudioSystemExt {
return;
}
queue::send(Command::Play {
sound_name,
event_name,
entity_id: entity_id.into_option(),
emitter_name: emitter_name.into_option(),
line_type: line_type.into_option(),
Expand All @@ -413,18 +430,22 @@ impl ExtCommand for AudioSystemExt {
&self,
event_name: CName,
entity_id: EntityId,
emitter_name: Opt<CName>,
tag_name: Opt<CName>,
ext: Ref<AudioSettingsExt>,
) {
let ext = ext.into_settings();
let Some(tag_name) = tag_name.into_option() else {
warns!("invalid tag name: \"{tag_name}\"");
return;
};
if let Some(Err(e)) = ext.as_ref().map(Validate::validate) {
warns!("invalid audio settings: {:#?}", e);
return;
}
queue::send(Command::PlayOnEmitter {
event_name,
entity_id,
emitter_name: emitter_name.into_option(),
tag_name,
ext,
});
}
Expand All @@ -433,13 +454,17 @@ impl ExtCommand for AudioSystemExt {
&self,
event_name: CName,
entity_id: EntityId,
emitter_name: Opt<CName>,
tag_name: Opt<CName>,
tween: Ref<Tween>,
) {
let Some(tag_name) = tag_name.into_option() else {
warns!("invalid tag name: \"{tag_name}\"");
return;
};
queue::send(Command::StopOnEmitter {
event_name,
entity_id,
emitter_name: emitter_name.into_option(),
tag_name,
tween: tween.into_tween(),
});
}
Expand Down
Loading

0 comments on commit 399121c

Please sign in to comment.