Skip to content

Commit

Permalink
Implement /sound.offset (#2202)
Browse files Browse the repository at this point in the history
Co-authored-by: ike709 <[email protected]>
  • Loading branch information
ike709 and ike709 authored Feb 8, 2025
1 parent 9de4824 commit d5bdf43
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 6 deletions.
2 changes: 1 addition & 1 deletion DMCompiler/DMStandard/Types/Sound.dm
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions OpenDreamClient/Audio/DreamSoundEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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, float offset) {
if (_audioSystem == null)
_entitySystemManager.Resolve(ref _audioSystem);

Expand Down Expand Up @@ -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;
Expand All @@ -86,7 +86,7 @@ public void StopAllChannels() {
private void RxSound(MsgSound msg) {
if (msg.ResourceId.HasValue) {
_resourceManager.LoadResourceAsync<ResourceSound>(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);
}
Expand Down
2 changes: 1 addition & 1 deletion OpenDreamClient/Audio/IDreamSoundEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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, float offset);
void StopChannel(int channel);
void StopAllChannels();
}
4 changes: 3 additions & 1 deletion OpenDreamRuntime/DreamConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -212,11 +212,13 @@ public void OutputDreamValue(DreamValue value) {
if (value.TryGetValueAsDreamObject<DreamObjectSound>(out var outputObject)) {
ushort channel = (ushort)outputObject.GetVariable("channel").GetValueAsInteger();
ushort volume = (ushort)outputObject.GetVariable("volume").GetValueAsInteger();
float offset = 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)) {
Expand Down
3 changes: 3 additions & 0 deletions OpenDreamShared/Network/Messages/MsgSound.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@ public enum FormatType : byte {

public ushort Channel;
public ushort Volume;
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

public override void ReadFromBuffer(NetIncomingMessage buffer, IRobustSerializer serializer) {
Channel = buffer.ReadUInt16();
Volume = buffer.ReadUInt16();
Offset = buffer.ReadFloat();

if (buffer.ReadBoolean()) {
ResourceId = buffer.ReadInt32();
Expand All @@ -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) {
Expand Down

0 comments on commit d5bdf43

Please sign in to comment.