Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
using Artemis.Core;
using Artemis.Core.Modules;
using NAudio.CoreAudioApi;
using NAudio.CoreAudioApi.Interfaces;

namespace Artemis.Plugins.Audio.DataModelExpansion.DataModels
{
public class PlaybackVolumeDataModel : DataModel
{
[DataModelProperty(Description ="Name of the current playback device.")]
[DataModelProperty(Description = "Name of the current playback device.")]
public string DefaultDeviceName { get; set; }
[DataModelProperty(Description = "Channel count of the the current playback device.")]
public int ChannelCount { get; set; }
Expand All @@ -32,6 +33,7 @@ public class PlaybackVolumeDataModel : DataModel
[DataModelProperty(Description = "Event triggered when current playback device master volume is changed.")]
public DataModelEvent VolumeChanged { get; set; } = new DataModelEvent();
public ChannelsDataModel Channels { get; set; } = new ChannelsDataModel();
public SessionsDataModel Sessions { get; set; } = new SessionsDataModel();

public void Reset()
{
Expand All @@ -49,6 +51,17 @@ public void Reset()
}

public class ChannelsDataModel : DataModel { }

public class SessionsDataModel : DataModel { }

public class SessionDataModel : DataModel
{
public string Name { get; set; }
public AudioSessionState? State { get; set; }
public float PeakVolume { get; set; }
public float PeakVolumeNormalized { get; set; }
}

public class ChannelDataModel : DataModel
{
public int ChannelIndex { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,11 @@ private void UpdatePeakVolume(double deltaTime)
// Update Main volume Peak
lock (_audioEventLock) // To avoid query an Device/EndPoint that is not the current device anymore or has more or less channels
{

// Absolute master peak volume
float peakVolumeNormalized = _playbackDevice?.AudioMeterInformation.MasterPeakValue ?? 0f;
float peakVolumeNormalized = (float) _playbackDevice?.AudioMeterInformation.MasterPeakValue;

// Don't update datamodel if not neeeded
// Don't update datamodel if not needed
if (Math.Abs(_lastMasterPeakVolumeNormalized - peakVolumeNormalized) < 0.00001f)
return;

Expand All @@ -119,6 +120,58 @@ private void UpdatePeakVolume(double deltaTime)
channelDataModel.Value.PeakVolumeNormalized = channelsVolumeNormalized[i];
channelDataModel.Value.PeakVolume = channelsVolumeNormalized[i] * 100f;
}

// Populate Sessions DataModel. Note that if the node don't exists, conditions using these values will become invalid
SessionCollection audioSessions = _playbackDevice?.AudioSessionManager.Sessions;
for (int i = 0; i < audioSessions.Count; i++)
{
AudioSessionControl session = audioSessions[i];

// Don't waste resources parsing system sessions data
if (session.IsSystemSoundsSession)
continue;

// Span Code thanks to Darth Affe https://github.com/DarthAffe
ReadOnlySpan<char> data = session.GetSessionInstanceIdentifier.AsSpan();
int pidStart = data.LastIndexOf("%") + 2;
int nameStart = data.LastIndexOf("\\") + 1;

ReadOnlySpan<char> nameSlice = data.Slice(nameStart);
int nameEnd = nameSlice.LastIndexOf(".");

// To avoid crashed with exe name that contains '.' character
string name = nameSlice.Slice(0, nameEnd).ToString().Replace('.', ' ');

if (!uint.TryParse(data.Slice(pidStart), out uint _))
continue;

// Ignore sessions without name. It may be a valid session but without a name it is useless for conditions system
if (string.IsNullOrEmpty(name))
continue;

// Don't remove unused sessions as these becomes just inactive for a while. It is faster to keep them than scan and remove them.
if (DataModel.Sessions.TryGetDynamicChild(name, out DynamicChild<SessionDataModel> sessionDataModel))
{
sessionDataModel.Value.Name = name;
sessionDataModel.Value.State = session.State;
sessionDataModel.Value.PeakVolume = session.AudioMeterInformation.MasterPeakValue * 100;
sessionDataModel.Value.PeakVolumeNormalized = session.AudioMeterInformation.MasterPeakValue;
}
else
{
DataModel.Sessions.AddDynamicChild(
name,
new SessionDataModel()
{
Name = name,
State = session.State,
PeakVolume = session.AudioMeterInformation.MasterPeakValue * 100,
PeakVolumeNormalized = session.AudioMeterInformation.MasterPeakValue
},
name
);
}
}
}
}

Expand Down