1
0
mirror of https://github.com/Artemis-RGB/Artemis synced 2026-01-01 02:03:32 +00:00

Add audio values to general profile datamodel

This commit is contained in:
SpoinkyNL 2017-01-15 17:16:06 +01:00
parent 80cd620247
commit e451a03996
7 changed files with 164 additions and 81 deletions

View File

@ -110,6 +110,7 @@
<PlatformTarget>x64</PlatformTarget> <PlatformTarget>x64</PlatformTarget>
<ErrorReport>prompt</ErrorReport> <ErrorReport>prompt</ErrorReport>
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet> <CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'"> <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'">
<OutputPath>bin\x64\Release\</OutputPath> <OutputPath>bin\x64\Release\</OutputPath>

View File

@ -25,7 +25,7 @@ namespace Artemis.Models
private readonly DialogService _dialogService; private readonly DialogService _dialogService;
private readonly WindowService _windowService; private readonly WindowService _windowService;
private FileSystemWatcher _watcher; private FileSystemWatcher _watcher;
private ProfileModel _luaProfile; private ModuleModel _luaModule;
public ProfileEditorModel(WindowService windowService, MetroDialogService dialogService, public ProfileEditorModel(WindowService windowService, MetroDialogService dialogService,
DeviceManager deviceManager, LuaManager luaManager) DeviceManager deviceManager, LuaManager luaManager)
@ -290,7 +290,7 @@ namespace Artemis.Models
#region LUA #region LUA
public void OpenLuaEditor(ProfileModel profileModel) public void OpenLuaEditor(ModuleModel moduleModel)
{ {
// Clean up old environment // Clean up old environment
DisposeLuaWatcher(); DisposeLuaWatcher();
@ -301,12 +301,12 @@ namespace Artemis.Models
file.Dispose(); file.Dispose();
// Add instructions to LUA script if it's a new file // Add instructions to LUA script if it's a new file
if (string.IsNullOrEmpty(profileModel.LuaScript)) if (string.IsNullOrEmpty(moduleModel.ProfileModel.LuaScript))
profileModel.LuaScript = Encoding.UTF8.GetString(Resources.lua_placeholder); moduleModel.ProfileModel.LuaScript = Encoding.UTF8.GetString(Resources.lua_placeholder);
File.WriteAllText(Path.GetTempPath() + fileName, profileModel.LuaScript); File.WriteAllText(Path.GetTempPath() + fileName, moduleModel.ProfileModel.LuaScript);
// Watch the file for changes // Watch the file for changes
_luaProfile = profileModel; _luaModule = moduleModel;
_watcher = new FileSystemWatcher(Path.GetTempPath(), fileName); _watcher = new FileSystemWatcher(Path.GetTempPath(), fileName);
_watcher.Changed += LuaFileChanged; _watcher.Changed += LuaFileChanged;
_watcher.EnableRaisingEvents = true; _watcher.EnableRaisingEvents = true;
@ -319,7 +319,7 @@ namespace Artemis.Models
private void LuaFileChanged(object sender, FileSystemEventArgs args) private void LuaFileChanged(object sender, FileSystemEventArgs args)
{ {
if (_luaProfile == null) if (_luaModule == null)
{ {
DisposeLuaWatcher(); DisposeLuaWatcher();
return; return;
@ -328,18 +328,18 @@ namespace Artemis.Models
if (args.ChangeType != WatcherChangeTypes.Changed) if (args.ChangeType != WatcherChangeTypes.Changed)
return; return;
lock (_luaProfile) lock (_luaModule)
{ {
using (var fs = new FileStream(args.FullPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) using (var fs = new FileStream(args.FullPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{ {
using (var sr = new StreamReader(fs)) using (var sr = new StreamReader(fs))
{ {
_luaProfile.LuaScript = sr.ReadToEnd(); _luaModule.ProfileModel.LuaScript = sr.ReadToEnd();
} }
} }
ProfileProvider.AddOrUpdate(_luaProfile); ProfileProvider.AddOrUpdate(_luaModule.ProfileModel);
_luaManager.SetupLua(_luaProfile); _luaManager.SetupLua(_luaModule.ProfileModel);
} }
} }

View File

@ -15,6 +15,7 @@ namespace Artemis.Modules.General.GeneralProfile
CurrentTime = new CurrentTime(); CurrentTime = new CurrentTime();
Keyboard = new KbDataModel(); Keyboard = new KbDataModel();
ActiveWindow = new ActiveWindow(); ActiveWindow = new ActiveWindow();
Audio = new Audio();
} }
public CpuDataModel Cpu { get; set; } public CpuDataModel Cpu { get; set; }
@ -24,6 +25,30 @@ namespace Artemis.Modules.General.GeneralProfile
public CurrentTime CurrentTime { get; set; } public CurrentTime CurrentTime { get; set; }
public KbDataModel Keyboard { get; set; } public KbDataModel Keyboard { get; set; }
public ActiveWindow ActiveWindow { get; set; } public ActiveWindow ActiveWindow { get; set; }
public Audio Audio { get; set; }
}
[MoonSharpUserData]
public class Audio
{
public double Volume { get; set; }
public double Peak { get; set; }
public AudioDevice Recording { get; set; } = new AudioDevice();
public AudioDevice Playback { get; set; } = new AudioDevice();
}
[MoonSharpUserData]
public class AudioDevice
{
public float OverallPeak { get; set; }
public float Channel1Peak { get; set; }
public float Channel2Peak { get; set; }
public float Channel3Peak { get; set; }
public float Channel4Peak { get; set; }
public float Channel5Peak { get; set; }
public float Channel6Peak { get; set; }
public float Channel7Peak { get; set; }
public float Channel8Peak { get; set; }
} }
[MoonSharpUserData] [MoonSharpUserData]

View File

@ -7,9 +7,12 @@ using System.Runtime.InteropServices;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using Artemis.DAL; using Artemis.DAL;
using Artemis.Events;
using Artemis.Managers; using Artemis.Managers;
using Artemis.Modules.Abstract; using Artemis.Modules.Abstract;
using Artemis.Profiles.Layers.Types.Audio.AudioCapturing;
using Artemis.Utilities; using Artemis.Utilities;
using CSCore.CoreAudioAPI;
using Newtonsoft.Json; using Newtonsoft.Json;
using SpotifyAPI.Local; using SpotifyAPI.Local;
@ -17,19 +20,21 @@ namespace Artemis.Modules.General.GeneralProfile
{ {
public class GeneralProfileModel : ModuleModel public class GeneralProfileModel : ModuleModel
{ {
private List<PerformanceCounter> _cores; private readonly AudioCaptureManager _audioCaptureManager;
private int _cpuFrames;
private DateTime _lastMusicUpdate; private DateTime _lastMusicUpdate;
private PerformanceCounter _overallCpu;
private SpotifyLocalAPI _spotify; private SpotifyLocalAPI _spotify;
private bool _spotifySetupBusy; private bool _spotifySetupBusy;
public GeneralProfileModel(DeviceManager deviceManager, LuaManager luaManager) : base(deviceManager, luaManager) public GeneralProfileModel(DeviceManager deviceManager, LuaManager luaManager,
AudioCaptureManager audioCaptureManager) : base(deviceManager, luaManager)
{ {
_audioCaptureManager = audioCaptureManager;
_lastMusicUpdate = DateTime.Now; _lastMusicUpdate = DateTime.Now;
Settings = SettingsProvider.Load<GeneralProfileSettings>(); Settings = SettingsProvider.Load<GeneralProfileSettings>();
DataModel = new GeneralProfileDataModel(); DataModel = new GeneralProfileDataModel();
audioCaptureManager.AudioDeviceChanged += AudioDeviceChanged;
} }
public override string Name => "GeneralProfile"; public override string Name => "GeneralProfile";
@ -40,6 +45,7 @@ namespace Artemis.Modules.General.GeneralProfile
{ {
SetupCpu(); SetupCpu();
SetupSpotify(); SetupSpotify();
SetupAudio();
base.Enable(); base.Enable();
} }
@ -52,6 +58,7 @@ namespace Artemis.Modules.General.GeneralProfile
UpdateDay(dataModel); UpdateDay(dataModel);
UpdateKeyStates(dataModel); UpdateKeyStates(dataModel);
UpdateActiveWindow(dataModel); UpdateActiveWindow(dataModel);
UpdateAudio(dataModel);
} }
#region Current Time #region Current Time
@ -67,8 +74,59 @@ namespace Artemis.Modules.General.GeneralProfile
#endregion #endregion
#region Audio
private MMDevice _defaultRecording;
private MMDevice _defaultPlayback;
private void SetupAudio()
{
_defaultPlayback = MMDeviceEnumerator.DefaultAudioEndpoint(DataFlow.Render, Role.Multimedia);
_defaultRecording = MMDeviceEnumerator.DefaultAudioEndpoint(DataFlow.Capture, Role.Multimedia);
}
private void AudioDeviceChanged(object sender, AudioDeviceChangedEventArgs e)
{
_defaultRecording = e.DefaultRecording;
_defaultPlayback = e.DefaultPlayback;
}
private void UpdateAudio(GeneralProfileDataModel dataModel)
{
var recording = AudioMeterInformation.FromDevice(_defaultRecording);
var playback = AudioMeterInformation.FromDevice(_defaultPlayback);
dataModel.Audio.Recording.OverallPeak = recording.PeakValue;
for (var i = 0; i < recording.GetChannelsPeakValues(recording.MeteringChannelCount).Length; i++)
{
// Only support up to 8 channels until lists are supported natively
if (i > 7)
break;
var peakValue = recording.GetChannelsPeakValues(recording.MeteringChannelCount)[i];
typeof(AudioDevice).GetProperty($"Channel{i + 1}Peak").SetValue(dataModel.Audio.Recording, peakValue);
}
dataModel.Audio.Playback.OverallPeak = playback.PeakValue;
for (var i = 0; i < playback.GetChannelsPeakValues(playback.MeteringChannelCount).Length; i++)
{
// Only support up to 8 channels until lists are supported natively
if (i > 7)
break;
var peakValue = playback.GetChannelsPeakValues(playback.MeteringChannelCount)[i];
typeof(AudioDevice).GetProperty($"Channel{i + 1}Peak").SetValue(dataModel.Audio.Playback, peakValue);
}
}
#endregion
#region CPU #region CPU
private List<PerformanceCounter> _cores;
private int _cpuFrames;
private PerformanceCounter _overallCpu;
private void SetupCpu() private void SetupCpu()
{ {
try try

View File

@ -1,65 +1,65 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Timers; using System.Timers;
using Artemis.Events; using Artemis.Events;
using CSCore.CoreAudioAPI; using CSCore.CoreAudioAPI;
using Ninject.Extensions.Logging; using Ninject.Extensions.Logging;
namespace Artemis.Profiles.Layers.Types.Audio.AudioCapturing namespace Artemis.Profiles.Layers.Types.Audio.AudioCapturing
{ {
public class AudioCaptureManager public class AudioCaptureManager
{ {
private readonly List<AudioCapture> _audioCaptures; private readonly List<AudioCapture> _audioCaptures;
private MMDevice _lastDefaultPlayback; private MMDevice _lastDefaultPlayback;
private MMDevice _lastDefaultRecording; private MMDevice _lastDefaultRecording;
public AudioCaptureManager(ILogger logger) public AudioCaptureManager(ILogger logger)
{ {
Logger = logger; Logger = logger;
_audioCaptures = new List<AudioCapture>(); _audioCaptures = new List<AudioCapture>();
_lastDefaultPlayback = MMDeviceEnumerator.DefaultAudioEndpoint(DataFlow.Render, Role.Multimedia); _lastDefaultPlayback = MMDeviceEnumerator.DefaultAudioEndpoint(DataFlow.Render, Role.Multimedia);
_lastDefaultRecording = MMDeviceEnumerator.DefaultAudioEndpoint(DataFlow.Capture, Role.Multimedia); _lastDefaultRecording = MMDeviceEnumerator.DefaultAudioEndpoint(DataFlow.Capture, Role.Multimedia);
var defaultDeviceTimer = new Timer(1000); var defaultDeviceTimer = new Timer(1000);
defaultDeviceTimer.Elapsed += DefaultDeviceTimerOnElapsed; defaultDeviceTimer.Elapsed += DefaultDeviceTimerOnElapsed;
defaultDeviceTimer.Start(); defaultDeviceTimer.Start();
} }
public event EventHandler<AudioDeviceChangedEventArgs> AudioDeviceChanged; public event EventHandler<AudioDeviceChangedEventArgs> AudioDeviceChanged;
private void DefaultDeviceTimerOnElapsed(object sender, ElapsedEventArgs e) private void DefaultDeviceTimerOnElapsed(object sender, ElapsedEventArgs e)
{ {
var defaultPlayback = MMDeviceEnumerator.DefaultAudioEndpoint(DataFlow.Render, Role.Multimedia); var defaultPlayback = MMDeviceEnumerator.DefaultAudioEndpoint(DataFlow.Render, Role.Multimedia);
var defaultRecording = MMDeviceEnumerator.DefaultAudioEndpoint(DataFlow.Capture, Role.Multimedia); var defaultRecording = MMDeviceEnumerator.DefaultAudioEndpoint(DataFlow.Capture, Role.Multimedia);
if (defaultPlayback.DeviceID == _lastDefaultPlayback.DeviceID && if (defaultPlayback.DeviceID == _lastDefaultPlayback.DeviceID &&
defaultRecording.DeviceID == _lastDefaultRecording.DeviceID) defaultRecording.DeviceID == _lastDefaultRecording.DeviceID)
return; return;
_lastDefaultPlayback = defaultPlayback; _lastDefaultPlayback = defaultPlayback;
_lastDefaultRecording = defaultRecording; _lastDefaultRecording = defaultRecording;
OnAudioDeviceChanged(new AudioDeviceChangedEventArgs(_lastDefaultPlayback, _lastDefaultRecording)); OnAudioDeviceChanged(new AudioDeviceChangedEventArgs(_lastDefaultPlayback, _lastDefaultRecording));
} }
public AudioCapture GetAudioCapture(MMDevice device) public AudioCapture GetAudioCapture(MMDevice device)
{ {
// Return existing audio capture if found // Return existing audio capture if found
var audioCapture = _audioCaptures.FirstOrDefault(a => a.Device.DeviceID == device.DeviceID); var audioCapture = _audioCaptures.FirstOrDefault(a => a.Device.DeviceID == device.DeviceID);
if (audioCapture != null) if (audioCapture != null)
return audioCapture; return audioCapture;
// Else create a new one and return that // Else create a new one and return that
var newAudioCapture = new AudioCapture(Logger, device); var newAudioCapture = new AudioCapture(Logger, device);
_audioCaptures.Add(newAudioCapture); _audioCaptures.Add(newAudioCapture);
return newAudioCapture; return newAudioCapture;
} }
public ILogger Logger { get; set; } public ILogger Logger { get; set; }
protected virtual void OnAudioDeviceChanged(AudioDeviceChangedEventArgs e) protected virtual void OnAudioDeviceChanged(AudioDeviceChangedEventArgs e)
{ {
AudioDeviceChanged?.Invoke(this, e); AudioDeviceChanged?.Invoke(this, e);
} }
} }
} }

View File

@ -192,8 +192,7 @@ namespace Artemis.Profiles
public void Activate(LuaManager luaManager) public void Activate(LuaManager luaManager)
{ {
if (!Equals(luaManager.ProfileModel, this) || luaManager.ProfileModel.LuaScript != LuaScript) luaManager.SetupLua(this);
luaManager.SetupLua(this);
} }
public void Deactivate(LuaManager luaManager) public void Deactivate(LuaManager luaManager)

View File

@ -94,7 +94,7 @@ namespace Artemis.ViewModels
return; return;
try try
{ {
ProfileEditorModel.OpenLuaEditor(SelectedProfile); ProfileEditorModel.OpenLuaEditor(_moduleModel);
} }
catch (Exception e) catch (Exception e)
{ {