1
0
mirror of https://github.com/Artemis-RGB/Artemis synced 2026-01-02 10:43:31 +00:00

Fixed AudioVisualization issues after layer deactivation

Fixed a crash when loading invalid profiles
Added LUA storage module
This commit is contained in:
SpoinkyNL 2017-04-23 14:10:03 +02:00
parent 0cf9b3547e
commit cf044f70c5
9 changed files with 141 additions and 48 deletions

View File

@ -587,6 +587,7 @@
<Compile Include="Profiles\Lua\Modules\LuaLayerModule.cs" /> <Compile Include="Profiles\Lua\Modules\LuaLayerModule.cs" />
<Compile Include="Profiles\Lua\Modules\LuaMouseModule.cs" /> <Compile Include="Profiles\Lua\Modules\LuaMouseModule.cs" />
<Compile Include="Profiles\Lua\Modules\LuaProfileModule.cs" /> <Compile Include="Profiles\Lua\Modules\LuaProfileModule.cs" />
<Compile Include="Profiles\Lua\Modules\LuaStorageModule.cs" />
<Compile Include="Profiles\Lua\Modules\Timer\LuaTimer.cs" /> <Compile Include="Profiles\Lua\Modules\Timer\LuaTimer.cs" />
<Compile Include="Profiles\Lua\Modules\LuaTimerModule.cs" /> <Compile Include="Profiles\Lua\Modules\LuaTimerModule.cs" />
<Compile Include="Profiles\Lua\Wrappers\LuaDrawWrapper.cs" /> <Compile Include="Profiles\Lua\Wrappers\LuaDrawWrapper.cs" />

View File

@ -134,7 +134,7 @@ namespace Artemis.DAL
return null; return null;
return prof; return prof;
} }
catch (JsonSerializationException) catch (JsonException)
{ {
return null; return null;
} }

View File

@ -42,9 +42,16 @@ namespace Artemis.DeviceProviders.CoolerMaster
CmSdk.SetControlDevice(DEVICE_INDEX.DEV_MMouse_L); CmSdk.SetControlDevice(DEVICE_INDEX.DEV_MMouse_L);
// Doesn't seem reliable but better than nothing I suppose // Doesn't seem reliable but better than nothing I suppose
CanUse = CmSdk.IsDevicePlug(); try
if (CanUse) {
CmSdk.EnableLedControl(true); CanUse = CmSdk.IsDevicePlug();
if (CanUse)
CmSdk.EnableLedControl(true);
}
catch (Exception)
{
CanUse = false;
}
Logger.Debug("Attempted to enable Mastermouse Pro L. CanUse: {0}", CanUse); Logger.Debug("Attempted to enable Mastermouse Pro L. CanUse: {0}", CanUse);
return CanUse; return CanUse;

View File

@ -38,10 +38,17 @@ namespace Artemis.DeviceProviders.CoolerMaster
CmSdk.SetControlDevice(DEVICE_INDEX.DEV_MMouse_S); CmSdk.SetControlDevice(DEVICE_INDEX.DEV_MMouse_S);
// Doesn't seem reliable but better than nothing I suppose // Doesn't seem reliable but better than nothing I suppose
CanUse = CmSdk.IsDevicePlug(); try
if (CanUse) {
CmSdk.EnableLedControl(true); CanUse = CmSdk.IsDevicePlug();
if (CanUse)
CmSdk.EnableLedControl(true);
}
catch (Exception)
{
CanUse = false;
}
Logger.Debug("Attempted to enable Mastermouse Pro S. CanUse: {0}", CanUse); Logger.Debug("Attempted to enable Mastermouse Pro S. CanUse: {0}", CanUse);
return CanUse; return CanUse;
} }

View File

@ -17,7 +17,6 @@ namespace Artemis.Managers
private readonly DeviceManager _deviceManager; private readonly DeviceManager _deviceManager;
private readonly IKernel _kernel; private readonly IKernel _kernel;
private readonly ILogger _logger; private readonly ILogger _logger;
private readonly Script _luaScript;
private List<LuaModule> _luaModules; private List<LuaModule> _luaModules;
public LuaManager(IKernel kernel, ILogger logger, DeviceManager deviceManager) public LuaManager(IKernel kernel, ILogger logger, DeviceManager deviceManager)
@ -25,13 +24,14 @@ namespace Artemis.Managers
_kernel = kernel; _kernel = kernel;
_logger = logger; _logger = logger;
_deviceManager = deviceManager; _deviceManager = deviceManager;
_luaScript = new Script(CoreModules.Preset_SoftSandbox); LuaScript = new Script(CoreModules.Preset_SoftSandbox);
} }
public ProfileModel ProfileModel { get; private set; } public ProfileModel ProfileModel { get; private set; }
public KeyboardProvider KeyboardProvider { get; private set; } public KeyboardProvider KeyboardProvider { get; private set; }
public LuaProfileModule ProfileModule { get; private set; } public LuaProfileModule ProfileModule { get; private set; }
public LuaEventsModule EventsModule { get; private set; } public LuaEventsModule EventsModule { get; private set; }
public Script LuaScript { get; }
public void SetupLua(ProfileModel profileModel) public void SetupLua(ProfileModel profileModel)
{ {
@ -52,11 +52,11 @@ namespace Artemis.Managers
EventsModule = (LuaEventsModule) _luaModules.First(m => m.ModuleName == "Events"); EventsModule = (LuaEventsModule) _luaModules.First(m => m.ModuleName == "Events");
// Setup new state // Setup new state
_luaScript.Options.DebugPrint = LuaPrint; LuaScript.Options.DebugPrint = LuaPrint;
// Insert each module into the script's globals // Insert each module with a ModuleName into the script's globals
foreach (var luaModule in _luaModules) foreach (var luaModule in _luaModules.Where(m => m.ModuleName != null))
_luaScript.Globals[luaModule.ModuleName] = luaModule; LuaScript.Globals[luaModule.ModuleName] = luaModule;
// If there is no LUA script, don't bother executing the string // If there is no LUA script, don't bother executing the string
if (ProfileModel.LuaScript.IsNullOrEmpty()) if (ProfileModel.LuaScript.IsNullOrEmpty())
@ -66,9 +66,9 @@ namespace Artemis.Managers
{ {
lock (EventsModule.InvokeLock) lock (EventsModule.InvokeLock)
{ {
lock (_luaScript) lock (LuaScript)
{ {
_luaScript.DoString(ProfileModel.LuaScript); LuaScript.DoString(ProfileModel.LuaScript);
} }
} }
} }
@ -97,12 +97,12 @@ namespace Artemis.Managers
try try
{ {
_luaScript.Globals.Clear(); LuaScript.Globals.Clear();
_luaScript.Registry.Clear(); LuaScript.Registry.Clear();
_luaScript.Registry.RegisterConstants(); LuaScript.Registry.RegisterConstants();
_luaScript.Registry.RegisterCoreModules(CoreModules.Preset_SoftSandbox); LuaScript.Registry.RegisterCoreModules(CoreModules.Preset_SoftSandbox);
_luaScript.Globals.RegisterConstants(); LuaScript.Globals.RegisterConstants();
_luaScript.Globals.RegisterCoreModules(CoreModules.Preset_SoftSandbox); LuaScript.Globals.RegisterCoreModules(CoreModules.Preset_SoftSandbox);
} }
catch (NullReferenceException) catch (NullReferenceException)
{ {
@ -112,15 +112,15 @@ namespace Artemis.Managers
if (EventsModule != null) if (EventsModule != null)
lock (EventsModule.InvokeLock) lock (EventsModule.InvokeLock)
{ {
lock (_luaScript) lock (LuaScript)
{ {
_luaScript.DoString(""); LuaScript.DoString("");
} }
} }
else else
lock (_luaScript) lock (LuaScript)
{ {
_luaScript.DoString(""); LuaScript.DoString("");
} }
} }
@ -138,12 +138,12 @@ namespace Artemis.Managers
{ {
lock (EventsModule.InvokeLock) lock (EventsModule.InvokeLock)
{ {
lock (_luaScript) lock (LuaScript)
{ {
if (args != null) if (args != null)
_luaScript.Call(function, args); LuaScript.Call(function, args);
else else
_luaScript.Call(function); LuaScript.Call(function);
} }
} }
} }

View File

@ -97,18 +97,23 @@ namespace Artemis.Profiles.Layers.Types.Audio.AudioCapturing
} }
} }
private LineSpectrum _lineSpectrum;
public LineSpectrum GetLineSpectrum(int barCount, ScalingStrategy scalingStrategy) public LineSpectrum GetLineSpectrum(int barCount, ScalingStrategy scalingStrategy)
{ {
if (_spectrumProvider == null) if (_spectrumProvider == null || barCount <= 0)
return null; return null;
return new LineSpectrum(FftSize)
{ if (_lineSpectrum == null)
SpectrumProvider = _spectrumProvider, _lineSpectrum = new LineSpectrum(FftSize)
UseAverage = true, {
BarCount = barCount, SpectrumProvider = _spectrumProvider,
IsXLogScale = true, UseAverage = true,
ScalingStrategy = scalingStrategy BarCount = barCount,
}; IsXLogScale = true,
ScalingStrategy = scalingStrategy
};
return _lineSpectrum;
} }
/// <summary> /// <summary>
@ -148,13 +153,13 @@ namespace Artemis.Profiles.Layers.Types.Audio.AudioCapturing
if (Type == MmDeviceType.Input) if (Type == MmDeviceType.Input)
{ {
_soundIn = Device != null _soundIn = Device != null
? new WasapiCapture {Device = Device} ? new WasapiCapture { Device = Device }
: new WasapiCapture(); : new WasapiCapture();
} }
else else
{ {
_soundIn = Device != null _soundIn = Device != null
? new WasapiLoopbackCapture {Device = Device} ? new WasapiLoopbackCapture { Device = Device }
: new WasapiLoopbackCapture(); : new WasapiLoopbackCapture();
} }
@ -182,6 +187,7 @@ namespace Artemis.Profiles.Layers.Types.Audio.AudioCapturing
} }
}; };
_lineSpectrum = null;
_singleSpectrum = new SingleSpectrum(FftSize, _spectrumProvider); _singleSpectrum = new SingleSpectrum(FftSize, _spectrumProvider);
_mayStop = false; _mayStop = false;

View File

@ -1,4 +1,5 @@
using System.Collections.Generic; using System;
using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Windows; using System.Windows;
using System.Windows.Media; using System.Windows.Media;
@ -26,6 +27,7 @@ namespace Artemis.Profiles.Layers.Types.Audio
private List<double> _lineValues; private List<double> _lineValues;
private AudioPropertiesModel _properties; private AudioPropertiesModel _properties;
private bool _subscribed; private bool _subscribed;
private DateTime _lastRender;
public AudioType(AudioCaptureManager audioCaptureManager) public AudioType(AudioCaptureManager audioCaptureManager)
{ {
@ -123,8 +125,7 @@ namespace Artemis.Profiles.Layers.Types.Audio
SubscribeToAudioChange(); SubscribeToAudioChange();
if (_audioCapture == null || newProperties.Device != _properties.Device || if (_audioCapture == null || newProperties.Device != _properties.Device || newProperties.DeviceType != _properties.DeviceType)
newProperties.DeviceType != _properties.DeviceType)
{ {
var device = GetMmDevice(); var device = GetMmDevice();
if (device != null) if (device != null)
@ -153,17 +154,19 @@ namespace Artemis.Profiles.Layers.Types.Audio
currentHeight = layerModel.Width; currentHeight = layerModel.Width;
} }
if (_lines != currentLines || _lineSpectrum == null) // Get a new line spectrum if the lines changed, it is null or the layer hasn't rendered for a few frames
if (_lines != currentLines || _lineSpectrum == null || DateTime.Now - _lastRender > TimeSpan.FromMilliseconds(100))
{ {
_lines = currentLines; _lines = currentLines;
_lineSpectrum = _audioCapture.GetLineSpectrum(_lines, ScalingStrategy.Decibel); _lineSpectrum = _audioCapture.GetLineSpectrum(_lines, ScalingStrategy.Decibel);
if (_lineSpectrum == null)
return;
} }
var newLineValues = _lineSpectrum?.GetLineValues(currentHeight); var newLineValues = _audioCapture.GetLineSpectrum(_lines, ScalingStrategy.Decibel)?.GetLineValues(currentHeight);
if (newLineValues != null) if (newLineValues != null)
{
_lineValues = newLineValues; _lineValues = newLineValues;
_lastRender = DateTime.Now;
}
} }
public void SetupProperties(LayerModel layerModel) public void SetupProperties(LayerModel layerModel)

View File

@ -0,0 +1,68 @@
using Artemis.DAL;
using Artemis.Managers;
using Artemis.Settings;
using MoonSharp.Interpreter;
using static MoonSharp.Interpreter.Serialization.Json.JsonTableConverter;
namespace Artemis.Profiles.Lua.Modules
{
[MoonSharpUserData]
public class LuaStorageModule : LuaModule
{
private readonly Table _globalValues;
private readonly Table _profileValues;
private readonly LuaGlobalSettings _globalSettings;
public LuaStorageModule(LuaManager luaManager) : base(luaManager)
{
_globalSettings = SettingsProvider.Load<LuaGlobalSettings>();
// Load profile values
if (LuaManager.ProfileModel.LuaStorage != null)
_profileValues = JsonToTable(LuaManager.ProfileModel.LuaStorage, LuaManager.LuaScript);
else
_profileValues = new Table(LuaManager.LuaScript);
// Load global values
if (_globalSettings.GlobalValues != null)
_globalValues = JsonToTable(_globalSettings.GlobalValues, LuaManager.LuaScript);
else
_globalValues = new Table(LuaManager.LuaScript);
// Set the values onto the globals table so scripters can access them
LuaManager.LuaScript.Globals["ProfileStorage"] = _profileValues;
LuaManager.LuaScript.Globals["GlobalStorage"] = _globalValues;
}
public override string ModuleName => null;
public override void Dispose()
{
// Store profile values
LuaManager.ProfileModel.LuaStorage = _profileValues.TableToJson();
ProfileProvider.AddOrUpdate(LuaManager.ProfileModel);
// Store global values
_globalSettings.GlobalValues = _globalValues.TableToJson();
_globalSettings.Save();
}
}
public class LuaGlobalSettings : IArtemisSettings
{
public string GlobalValues { get; set; }
public void Save()
{
SettingsProvider.Save(this);
}
public void Reset(bool save = false)
{
GlobalValues = null;
if (save)
Save();
}
}
}

View File

@ -38,6 +38,7 @@ namespace Artemis.Profiles
public int Width { get; set; } public int Width { get; set; }
public int Height { get; set; } public int Height { get; set; }
public string LuaScript { get; set; } public string LuaScript { get; set; }
public string LuaStorage { get; set; }
[JsonIgnore] [JsonIgnore]
public string Slug => new string(Name.Where(ch => !_invalidFileNameChars.Contains(ch)).ToArray()); public string Slug => new string(Name.Where(ch => !_invalidFileNameChars.Contains(ch)).ToArray());