Skip to content

Commit

Permalink
Some file-scoped namespaces
Browse files Browse the repository at this point in the history
  • Loading branch information
wixoaGit committed Dec 22, 2023
1 parent 60c5783 commit cd8892f
Show file tree
Hide file tree
Showing 3 changed files with 535 additions and 536 deletions.
136 changes: 68 additions & 68 deletions OpenDreamClient/Audio/DreamSoundEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,99 +5,99 @@
using Robust.Shared.Audio;
using Robust.Shared.Network;

namespace OpenDreamClient.Audio {
public sealed class DreamSoundEngine : IDreamSoundEngine {
private const int SoundChannelLimit = 1024;
namespace OpenDreamClient.Audio;

[Dependency] private readonly IDreamResourceManager _resourceManager = default!;
[Dependency] private readonly ILogManager _logManager = default!;
[Dependency] private readonly INetManager _netManager = default!;
[Dependency] private readonly IEntitySystemManager _entitySystemManager = default!;
[Dependency] private readonly IAudioManager _audioManager = default!;
private AudioSystem? _audioSystem;
public sealed class DreamSoundEngine : IDreamSoundEngine {
private const int SoundChannelLimit = 1024;

private ISawmill _sawmill = default!;
[Dependency] private readonly IDreamResourceManager _resourceManager = default!;
[Dependency] private readonly ILogManager _logManager = default!;
[Dependency] private readonly INetManager _netManager = default!;
[Dependency] private readonly IEntitySystemManager _entitySystemManager = default!;
[Dependency] private readonly IAudioManager _audioManager = default!;
private AudioSystem? _audioSystem;

private readonly DreamSoundChannel?[] _channels = new DreamSoundChannel[SoundChannelLimit];
private ISawmill _sawmill = default!;

public void Initialize() {
_sawmill = _logManager.GetSawmill("opendream.audio");
private readonly DreamSoundChannel?[] _channels = new DreamSoundChannel[SoundChannelLimit];

_netManager.RegisterNetMessage<MsgSound>(RxSound);
public void Initialize() {
_sawmill = _logManager.GetSawmill("opendream.audio");

_netManager.Disconnect += DisconnectedFromServer;
}
_netManager.RegisterNetMessage<MsgSound>(RxSound);

public void StopFinishedChannels() {
for (int i = 0; i < SoundChannelLimit; i++) {
if (_channels[i]?.Source.Component.Playing is false or null)
StopChannel(i + 1);
}
_netManager.Disconnect += DisconnectedFromServer;
}

public void StopFinishedChannels() {
for (int i = 0; i < SoundChannelLimit; i++) {
if (_channels[i]?.Source.Component.Playing is false or null)
StopChannel(i + 1);
}
}

public void PlaySound(int channel, MsgSound.FormatType format, ResourceSound sound, float volume) {
if (_audioSystem == null)
_entitySystemManager.Resolve(ref _audioSystem);
public void PlaySound(int channel, MsgSound.FormatType format, ResourceSound sound, float volume) {
if (_audioSystem == null)
_entitySystemManager.Resolve(ref _audioSystem);

if (channel == 0) {
//First available channel
for (int i = 0; i < _channels.Length; i++) {
if (_channels[i] == null) {
channel = i + 1;
break;
}
}

if (channel == 0) {
_sawmill.Error("Failed to find a free audio channel to play a sound on");
return;
if (channel == 0) {
//First available channel
for (int i = 0; i < _channels.Length; i++) {
if (_channels[i] == null) {
channel = i + 1;
break;
}
}

StopChannel(channel);

var stream = sound.GetStream(format, _audioManager);
if (stream == null) {
_sawmill.Error($"Failed to load audio ${sound}");
if (channel == 0) {
_sawmill.Error("Failed to find a free audio channel to play a sound on");
return;
}
}

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.
if (source == null) {
_sawmill.Error($"Failed to play audio ${sound}");
return;
}
StopChannel(channel);

_channels[channel - 1] = new DreamSoundChannel(_audioSystem, source.Value);
var stream = sound.GetStream(format, _audioManager);
if (stream == null) {
_sawmill.Error($"Failed to load audio ${sound}");
return;
}

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.
if (source == null) {
_sawmill.Error($"Failed to play audio ${sound}");
return;
}

public void StopChannel(int channel) {
ref DreamSoundChannel? ch = ref _channels[channel - 1];
_channels[channel - 1] = new DreamSoundChannel(_audioSystem, source.Value);
}

ch?.Stop();
// This will null the corresponding index in the array.
ch = null;
}

public void StopAllChannels() {
for (int i = 0; i < SoundChannelLimit; i++) {
StopChannel(i + 1);
}
}
public void StopChannel(int channel) {
ref DreamSoundChannel? ch = ref _channels[channel - 1];

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));
} else {
StopChannel(msg.Channel);
}
ch?.Stop();
// This will null the corresponding index in the array.
ch = null;
}

public void StopAllChannels() {
for (int i = 0; i < SoundChannelLimit; i++) {
StopChannel(i + 1);
}
}

private void DisconnectedFromServer(object? sender, NetDisconnectedArgs e) {
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));
} else {
StopChannel(msg.Channel);
}
}

private void DisconnectedFromServer(object? sender, NetDisconnectedArgs e) {
StopAllChannels();
}
}
38 changes: 19 additions & 19 deletions OpenDreamClient/Resources/ResourceTypes/ResourceSound.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,27 @@
using OpenDreamShared.Network.Messages;
using Robust.Client.Audio;

namespace OpenDreamClient.Resources.ResourceTypes {
[UsedImplicitly]
public sealed class ResourceSound(int id, byte[] data) : DreamResource(id, data) {
private AudioStream? _stream;
namespace OpenDreamClient.Resources.ResourceTypes;

public AudioStream? GetStream(MsgSound.FormatType format, IAudioManager audioManager) {
if (_stream == null) {
switch (format) {
case MsgSound.FormatType.Ogg:
_stream = audioManager.LoadAudioOggVorbis(new MemoryStream(Data));
break;
case MsgSound.FormatType.Wav:
_stream = audioManager.LoadAudioWav(new MemoryStream(Data));
break;
default:
Logger.GetSawmill("opendream.audio").Fatal("Only *.ogg and *.wav audio files are supported.");
break;
}
}
[UsedImplicitly]
public sealed class ResourceSound(int id, byte[] data) : DreamResource(id, data) {
private AudioStream? _stream;

return _stream;
public AudioStream? GetStream(MsgSound.FormatType format, IAudioManager audioManager) {
if (_stream == null) {
switch (format) {
case MsgSound.FormatType.Ogg:
_stream = audioManager.LoadAudioOggVorbis(new MemoryStream(Data));
break;
case MsgSound.FormatType.Wav:
_stream = audioManager.LoadAudioWav(new MemoryStream(Data));
break;
default:
Logger.GetSawmill("opendream.audio").Fatal("Only *.ogg and *.wav audio files are supported.");
break;
}
}

return _stream;
}
}
Loading

0 comments on commit cd8892f

Please sign in to comment.