From 721e9e35693118df5914c8fb8b26285ae80b9326 Mon Sep 17 00:00:00 2001 From: ike709 Date: Mon, 3 Feb 2025 07:40:28 -0600 Subject: [PATCH 1/2] Implement `/sound.offset` --- DMCompiler/DMStandard/Types/Sound.dm | 2 +- OpenDreamClient/Audio/DreamSoundEngine.cs | 6 +++--- OpenDreamClient/Audio/IDreamSoundEngine.cs | 2 +- OpenDreamRuntime/DreamConnection.cs | 4 +++- OpenDreamShared/Network/Messages/MsgSound.cs | 3 +++ 5 files changed, 11 insertions(+), 6 deletions(-) diff --git a/DMCompiler/DMStandard/Types/Sound.dm b/DMCompiler/DMStandard/Types/Sound.dm index a5b678a011..49102d7e9a 100644 --- a/DMCompiler/DMStandard/Types/Sound.dm +++ b/DMCompiler/DMStandard/Types/Sound.dm @@ -18,7 +18,7 @@ var/environment as opendream_unimplemented var/echo as opendream_unimplemented var/len as opendream_unimplemented - var/offset as opendream_unimplemented + var/offset var/priority = 0 as opendream_unimplemented var/status = 0 as opendream_unimplemented diff --git a/OpenDreamClient/Audio/DreamSoundEngine.cs b/OpenDreamClient/Audio/DreamSoundEngine.cs index 220863ac31..a2c5f6faea 100644 --- a/OpenDreamClient/Audio/DreamSoundEngine.cs +++ b/OpenDreamClient/Audio/DreamSoundEngine.cs @@ -30,7 +30,7 @@ public void Initialize() { _netManager.Disconnect += DisconnectedFromServer; } - public void PlaySound(int channel, MsgSound.FormatType format, ResourceSound sound, float volume) { + public void PlaySound(int channel, MsgSound.FormatType format, ResourceSound sound, float volume, int offset) { if (_audioSystem == null) _entitySystemManager.Resolve(ref _audioSystem); @@ -59,7 +59,7 @@ public void PlaySound(int channel, MsgSound.FormatType format, ResourceSound sou } var db = 20 * MathF.Log10(volume); // convert from DM volume (0-100) to OpenAL volume (db) - var source = _audioSystem.PlayGlobal(stream, AudioParams.Default.WithVolume(db)); // TODO: Positional audio. + var source = _audioSystem.PlayGlobal(stream, AudioParams.Default.WithVolume(db).WithPlayOffset(offset)); // TODO: Positional audio. if (source == null) { _sawmill.Error($"Failed to play audio ${sound}"); return; @@ -86,7 +86,7 @@ public void StopAllChannels() { private void RxSound(MsgSound msg) { if (msg.ResourceId.HasValue) { _resourceManager.LoadResourceAsync(msg.ResourceId.Value, - sound => PlaySound(msg.Channel, msg.Format!.Value, sound, msg.Volume / 100.0f)); + sound => PlaySound(msg.Channel, msg.Format!.Value, sound, msg.Volume / 100.0f, msg.Offset)); } else { StopChannel(msg.Channel); } diff --git a/OpenDreamClient/Audio/IDreamSoundEngine.cs b/OpenDreamClient/Audio/IDreamSoundEngine.cs index 3ca5361eb9..6d44be8809 100644 --- a/OpenDreamClient/Audio/IDreamSoundEngine.cs +++ b/OpenDreamClient/Audio/IDreamSoundEngine.cs @@ -5,7 +5,7 @@ namespace OpenDreamClient.Audio; public interface IDreamSoundEngine { void Initialize(); - void PlaySound(int channel, MsgSound.FormatType format, ResourceSound sound, float volume); + void PlaySound(int channel, MsgSound.FormatType format, ResourceSound sound, float volume, int offset); void StopChannel(int channel); void StopAllChannels(); } diff --git a/OpenDreamRuntime/DreamConnection.cs b/OpenDreamRuntime/DreamConnection.cs index 4237c4eb9a..6f6be1ec3e 100644 --- a/OpenDreamRuntime/DreamConnection.cs +++ b/OpenDreamRuntime/DreamConnection.cs @@ -212,11 +212,13 @@ public void OutputDreamValue(DreamValue value) { if (value.TryGetValueAsDreamObject(out var outputObject)) { ushort channel = (ushort)outputObject.GetVariable("channel").GetValueAsInteger(); ushort volume = (ushort)outputObject.GetVariable("volume").GetValueAsInteger(); + int offset = (int)outputObject.GetVariable("offset").UnsafeGetValueAsFloat(); DreamValue file = outputObject.GetVariable("file"); var msg = new MsgSound() { Channel = channel, - Volume = volume + Volume = volume, + Offset = offset }; if (!file.TryGetValueAsDreamResource(out var soundResource)) { diff --git a/OpenDreamShared/Network/Messages/MsgSound.cs b/OpenDreamShared/Network/Messages/MsgSound.cs index 4a3255759f..68de1e54df 100644 --- a/OpenDreamShared/Network/Messages/MsgSound.cs +++ b/OpenDreamShared/Network/Messages/MsgSound.cs @@ -14,6 +14,7 @@ public enum FormatType : byte { public ushort Channel; public ushort Volume; + public int Offset; public int? ResourceId; public FormatType? Format; // TODO: This should probably be sent along with the sound resource instead somehow //TODO: Frequency and friends @@ -21,6 +22,7 @@ public enum FormatType : byte { public override void ReadFromBuffer(NetIncomingMessage buffer, IRobustSerializer serializer) { Channel = buffer.ReadUInt16(); Volume = buffer.ReadUInt16(); + Offset = buffer.ReadInt32(); if (buffer.ReadBoolean()) { ResourceId = buffer.ReadInt32(); @@ -31,6 +33,7 @@ public override void ReadFromBuffer(NetIncomingMessage buffer, IRobustSerializer public override void WriteToBuffer(NetOutgoingMessage buffer, IRobustSerializer serializer) { buffer.Write(Channel); buffer.Write(Volume); + buffer.Write(Offset); buffer.Write(ResourceId != null); if (ResourceId != null) { From 835bd6eff8452372bbc593bcfbc06ef364d89ca7 Mon Sep 17 00:00:00 2001 From: ike709 Date: Sat, 8 Feb 2025 11:25:57 -0600 Subject: [PATCH 2/2] floating away --- OpenDreamClient/Audio/DreamSoundEngine.cs | 2 +- OpenDreamClient/Audio/IDreamSoundEngine.cs | 2 +- OpenDreamRuntime/DreamConnection.cs | 2 +- OpenDreamShared/Network/Messages/MsgSound.cs | 4 ++-- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/OpenDreamClient/Audio/DreamSoundEngine.cs b/OpenDreamClient/Audio/DreamSoundEngine.cs index a2c5f6faea..9178caa914 100644 --- a/OpenDreamClient/Audio/DreamSoundEngine.cs +++ b/OpenDreamClient/Audio/DreamSoundEngine.cs @@ -30,7 +30,7 @@ public void Initialize() { _netManager.Disconnect += DisconnectedFromServer; } - public void PlaySound(int channel, MsgSound.FormatType format, ResourceSound sound, float volume, int offset) { + public void PlaySound(int channel, MsgSound.FormatType format, ResourceSound sound, float volume, float offset) { if (_audioSystem == null) _entitySystemManager.Resolve(ref _audioSystem); diff --git a/OpenDreamClient/Audio/IDreamSoundEngine.cs b/OpenDreamClient/Audio/IDreamSoundEngine.cs index 6d44be8809..953b5cc6d1 100644 --- a/OpenDreamClient/Audio/IDreamSoundEngine.cs +++ b/OpenDreamClient/Audio/IDreamSoundEngine.cs @@ -5,7 +5,7 @@ namespace OpenDreamClient.Audio; public interface IDreamSoundEngine { void Initialize(); - void PlaySound(int channel, MsgSound.FormatType format, ResourceSound sound, float volume, int offset); + void PlaySound(int channel, MsgSound.FormatType format, ResourceSound sound, float volume, float offset); void StopChannel(int channel); void StopAllChannels(); } diff --git a/OpenDreamRuntime/DreamConnection.cs b/OpenDreamRuntime/DreamConnection.cs index 6f6be1ec3e..9183aa44b0 100644 --- a/OpenDreamRuntime/DreamConnection.cs +++ b/OpenDreamRuntime/DreamConnection.cs @@ -212,7 +212,7 @@ public void OutputDreamValue(DreamValue value) { if (value.TryGetValueAsDreamObject(out var outputObject)) { ushort channel = (ushort)outputObject.GetVariable("channel").GetValueAsInteger(); ushort volume = (ushort)outputObject.GetVariable("volume").GetValueAsInteger(); - int offset = (int)outputObject.GetVariable("offset").UnsafeGetValueAsFloat(); + float offset = outputObject.GetVariable("offset").UnsafeGetValueAsFloat(); DreamValue file = outputObject.GetVariable("file"); var msg = new MsgSound() { diff --git a/OpenDreamShared/Network/Messages/MsgSound.cs b/OpenDreamShared/Network/Messages/MsgSound.cs index 68de1e54df..b2e4f05c7b 100644 --- a/OpenDreamShared/Network/Messages/MsgSound.cs +++ b/OpenDreamShared/Network/Messages/MsgSound.cs @@ -14,7 +14,7 @@ public enum FormatType : byte { public ushort Channel; public ushort Volume; - public int Offset; + public float Offset; public int? ResourceId; public FormatType? Format; // TODO: This should probably be sent along with the sound resource instead somehow //TODO: Frequency and friends @@ -22,7 +22,7 @@ public enum FormatType : byte { public override void ReadFromBuffer(NetIncomingMessage buffer, IRobustSerializer serializer) { Channel = buffer.ReadUInt16(); Volume = buffer.ReadUInt16(); - Offset = buffer.ReadInt32(); + Offset = buffer.ReadFloat(); if (buffer.ReadBoolean()) { ResourceId = buffer.ReadInt32();