-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCorsairLED.cs
282 lines (260 loc) · 9.63 KB
/
CorsairLED.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading;
using System.Timers;
using CUE.NET.Exceptions;
using CUE.NET.Devices.Generic.Enums;
using MusicBeePlugin.Devices;
using MusicBeePlugin.Settings;
using MusicBeePlugin.UI;
using Timer = System.Timers.Timer;
namespace MusicBeePlugin
{
public partial class Plugin
{
private MusicBeeApiInterface _mbApiInterface;
private readonly PluginInfo _about = new PluginInfo();
private SettingsWindow _settingsWindow;
private SettingsManager _settingsManager;
private DeviceController _devcontroller;
private readonly DebugPlot _debugplot = new DebugPlot();
private readonly Timer _pauseTimer = new Timer();
private int _barcount = 22;
public PluginInfo Initialise(IntPtr apiInterfacePtr)
{
_mbApiInterface = new MusicBeeApiInterface();
_mbApiInterface.Initialise(apiInterfacePtr);
_about.PluginInfoVersion = PluginInfoVersion;
_about.Name = "CorsairLED";
_about.Description = "Adds Support for Corsair CUE Devices";
_about.Author = "Stefan Lengauer";
_about.TargetApplication = ""; // current only applies to artwork, lyrics or instant messenger name that appears in the provider drop down selector or target Instant Messenger
_about.Type = PluginType.General;
_about.VersionMajor = 1; // your plugin version
_about.VersionMinor = 0;
_about.Revision = 4;
_about.MinInterfaceVersion = MinInterfaceVersion;
_about.MinApiRevision = MinApiRevision;
_about.ReceiveNotifications = (ReceiveNotificationFlags.PlayerEvents | ReceiveNotificationFlags.TagEvents);
_about.ConfigurationPanelHeight = 0; // height in pixels that musicbee should reserve in a panel for config settings. When set, a handle to an empty panel will be passed to the Configure function
try
{
_settingsManager = new SettingsManager(_mbApiInterface.Setting_GetPersistentStoragePath() + "CorsairLED\\CorsairLED.settings");
_devcontroller = new DeviceController(this, _settingsManager);
_devcontroller.Init();
_settingsWindow = new SettingsWindow(_about, _devcontroller, _settingsManager);
if (DeviceController.IsInitialized)
{
_mbApiInterface.MB_AddMenuItem("mnuTools/CL_Show Debug Plot", "HotKey For CL Debug Plot", ShowDebugPlot);
_devcontroller.AddSettings(_settingsWindow);
_barcount = _devcontroller.GetDesiredBarCount();
}
}
catch (CUEException ex)
{
Console.WriteLine(@"CUE Exception! ErrorCode: " + Enum.GetName(typeof(CorsairError), ex.Error));
throw;
}
// migrate old config
if (File.Exists(_mbApiInterface.Setting_GetPersistentStoragePath() + "CorsairLED\\CorsairLED.config"))
{
_settingsManager.MigrateFromOld(_mbApiInterface.Setting_GetPersistentStoragePath() + "CorsairLED\\CorsairLED.config", _devcontroller.Devices.OfType<KeyboardEffectDevice>().First().DeviceName);
File.Delete(_mbApiInterface.Setting_GetPersistentStoragePath() + "CorsairLED\\CorsairLED.config");
_settingsManager.Save();
}
_pauseTimer.AutoReset = false;
_pauseTimer.Interval = 5000;
_pauseTimer.Elapsed += PauseTimerOnElapsed;
Debug.WriteLine(_about.Name + " loaded");
Debug.WriteLine("MusicBee Version" + _mbApiInterface.MusicBeeVersion);
if (!DeviceController.IsInitialized)
{
Console.WriteLine("Could not initialize CUE SDK");
}
return _about;
}
private void ShowDebugPlot(object sender, EventArgs e)
{
_debugplot?.Show();
}
public bool Configure(IntPtr panelHandle)
{
Debug.WriteLine(_about.Name + " Configure called with path" + _mbApiInterface.Setting_GetPersistentStoragePath());
_settingsWindow?.Show();
// This prevents showing the About Box by MusicBee
return true;
}
// called by MusicBee when the user clicks Apply or Save in the MusicBee Preferences screen.
// its up to you to figure out whether anything has changed and needs updating
public void SaveSettings()
{
_settingsWindow?.PersistValues();
}
// MusicBee is closing the plugin (plugin is being disabled by user or MusicBee is shutting down)
public void Close(PluginCloseReason reason)
{
_devcontroller.StopEffect();
// Stop is async so give it some extra time (hopefully prevents unclean shutdown)
Thread.Sleep(500);
}
// uninstall this plugin - clean up any persisted files
public void Uninstall()
{
_settingsWindow?.Delete();
}
// receive event notifications from MusicBee
// you need to set about.ReceiveNotificationFlags = PlayerEvents to receive all notifications, and not just the startup event
public void ReceiveNotification(string sourceFileUrl, NotificationType type)
{
if (!DeviceController.IsInitialized) return;
// perform some action depending on the notification type
switch (type)
{
case NotificationType.PluginStartup:
_pauseTimer.Start();
break;
case NotificationType.TrackChanged:
break;
case NotificationType.PlayStateChanged:
switch (_mbApiInterface.Player_GetPlayState())
{
case PlayState.Playing:
_pauseTimer.Stop();
_devcontroller.StartEffect();
break;
case PlayState.Stopped:
case PlayState.Paused:
case PlayState.Undefined:
case PlayState.Loading:
_pauseTimer.Start();
break;
default:
throw new ArgumentOutOfRangeException();
}
break;
case NotificationType.TrackChanging:
break;
case NotificationType.AutoDjStarted:
break;
case NotificationType.AutoDjStopped:
break;
case NotificationType.VolumeMuteChanged:
break;
case NotificationType.VolumeLevelChanged:
break;
case NotificationType.NowPlayingListChanged:
break;
case NotificationType.NowPlayingListEnded:
break;
case NotificationType.NowPlayingArtworkReady:
break;
case NotificationType.NowPlayingLyricsReady:
break;
case NotificationType.TagsChanging:
break;
case NotificationType.TagsChanged:
break;
case NotificationType.RatingChanging:
break;
case NotificationType.RatingChanged:
break;
case NotificationType.PlayCountersChanged:
break;
case NotificationType.ScreenSaverActivating:
break;
case NotificationType.ShutdownStarted:
break;
case NotificationType.EmbedInPanel:
break;
case NotificationType.PlayerRepeatChanged:
break;
case NotificationType.PlayerShuffleChanged:
break;
case NotificationType.PlayerEqualiserOnOffChanged:
break;
case NotificationType.PlayerScrobbleChanged:
break;
case NotificationType.ReplayGainChanged:
break;
case NotificationType.FileDeleting:
break;
case NotificationType.FileDeleted:
break;
case NotificationType.ApplicationWindowChanged:
break;
case NotificationType.StopAfterCurrentChanged:
break;
case NotificationType.LibrarySwitched:
break;
case NotificationType.FileAddedToLibrary:
break;
case NotificationType.FileAddedToInbox:
break;
case NotificationType.SynchCompleted:
break;
case NotificationType.DownloadCompleted:
break;
case NotificationType.MusicBeeStarted:
break;
}
}
private void PauseTimerOnElapsed(object sender, ElapsedEventArgs elapsedEventArgs)
{
_devcontroller.StopEffect();
}
public void UpdateSpectrographData()
{
float[] bardata = CalcBarData(_barcount);
_devcontroller.Curbardata = bardata;
_devcontroller.TrackProgress = _mbApiInterface.Player_GetPosition() * 1.0f / _mbApiInterface.NowPlaying_GetDuration();
float beat = CalcBeat(bardata);
_devcontroller.Beat = beat;
float[] combined = new float[_barcount+3];
Array.Copy(bardata, combined, bardata.Length);
combined[_barcount + 2] = beat;
_debugplot?.UpdatePlot(combined);
}
private float CalcBeat(float[] bardata)
{
var sum = 0f;
var cnt = 0;
for (int i = 0; i < bardata.Length/4; i++)
{
sum += (float) Math.Sqrt(bardata[i]);
cnt = i;
}
return sum/cnt;
}
private float[] CalcBarData(int barcount)
{
float[] bardata = new float[barcount];
float[] fftdata = new float[2048];
var ret = _mbApiInterface.NowPlaying_GetSpectrumData(fftdata);
int jumpwidth = (fftdata.Length/2) / barcount;
int bar = 0;
fftdata[0] /= 2;
if (ret <= 0) return bardata;
for (int i = 0; i < fftdata.Length / 2; i += jumpwidth)
{
float avg = 0;
for (int j = i; j < i + jumpwidth && j < fftdata.Length / 2; j++)
{
avg += fftdata[j];
}
avg /= jumpwidth;
if (bar < bardata.Length)
{
//bardata[bar] = (float) Math.Sqrt(avg) * 1000f;
bardata[bar] = (float)Math.Sqrt(avg) * 10f;
//bardata[bar] = (float)Math.Sqrt(avg) * 15f * (1 + (float)Math.Sqrt((double)bar/ (double)_barcount) * 1.25f);
//bardata[bar] = (float)Math.Log10(1/(avg*avg)) * 10f;
}
bar++;
}
bardata[0] *= 0.5f;
return bardata;
}
}
}