/
HuePlugin.cs
313 lines (266 loc) · 12.8 KB
/
HuePlugin.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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Collections.Generic;
using MusicBeePlugin;
using SharpHue;
using MusicBeePlugin.SettingsComponent;
using System.Threading;
using System.Diagnostics;
using System.Timers;
namespace MusicBeePlugin
{
public partial class Plugin
{
private MusicBeeApiInterface mbApiInterface;
private PluginInfo about = new PluginInfo();
SettingsSelector settingsSelector = new SettingsSelector();
private Hue hue = new Hue();
LightCollection lights;
Random rnd = new Random();
bool threadStop = true;
bool stop = false;
//Stopwatch stopwatch = new Stopwatch();
System.Timers.Timer aTimer = new System.Timers.Timer();
int maxRange = 0;
public PluginInfo Initialise(IntPtr apiInterfacePtr)
{
mbApiInterface = new MusicBeeApiInterface();
mbApiInterface.Initialise(apiInterfacePtr);
about.PluginInfoVersion = PluginInfoVersion;
about.Name = "Philips Hue";
about.Description = "Match your Philips Hue lights to the current song album art\n More Help: github.com/TroyFernandes/MusicBee-Philips-Hue";
about.Author = "Troy Fernandes (github.com/TroyFernandes)";
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 = 1;
about.Revision = 1;
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
ToolStripMenuItem mainMenuItem = (ToolStripMenuItem)mbApiInterface.MB_AddMenuItem("mnuTools/Hue Artwork", null, null);
mainMenuItem.DropDown.Items.Add("Settings", null, OnOpen);
mainMenuItem.DropDown.Items.Add("Stop", null, stopPlugin);
mainMenuItem.DropDown.Items.Add("Resume", null, resumePlugin);
Settings.Instance.storagePath = mbApiInterface.Setting_GetPersistentStoragePath();
Settings.Instance.Initialize();
Settings.Instance.loadSettings(mbApiInterface.Setting_GetPersistentStoragePath());
Configuration.Initialize(Settings.Instance.APIKey);
settingsSelector.startup();
lights = new LightCollection();
try
{
foreach (string lightNames in Settings.Instance.HueLights)
{
new LightStateBuilder().For(lights[lightNames]).TurnOn().Apply();
}
}
catch (NullReferenceException e)
{
Console.WriteLine(e.ToString());
}
return about;
}
public bool Configure(IntPtr panelHandle)
{
// save any persistent settings in a sub-folder of this path
string dataPath = mbApiInterface.Setting_GetPersistentStoragePath();
// panelHandle will only be set if you set about.ConfigurationPanelHeight to a non-zero value
// keep in mind the panel width is scaled according to the font the user has selected
// if about.ConfigurationPanelHeight is set to 0, you can display your own popup window
if (panelHandle != IntPtr.Zero)
{
Panel configPanel = (Panel)Panel.FromHandle(panelHandle);
Label prompt = new Label();
prompt.AutoSize = true;
prompt.Location = new Point(0, 0);
prompt.Text = "Hue Settings:";
configPanel.Controls.AddRange(new Control[] { prompt });
}
return false;
}
// 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()
{
// save any persistent settings in a sub-folder of this path
string dataPath = mbApiInterface.Setting_GetPersistentStoragePath();
}
// MusicBee is closing the plugin (plugin is being disabled by user or MusicBee is shutting down)
public void Close(PluginCloseReason reason)
{
}
// uninstall this plugin - clean up any persisted files
public void Uninstall()
{
//TODO: remove all the files
}
// 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)
{
//stopwatch.Start();
//query the duration of the track when the plugin receives a track change event and using a timer query the play position via the api every second for example
// perform some action depending on the notification type
switch (type)
{
case NotificationType.PluginStartup:
// perform startup initialisation
switch (mbApiInterface.Player_GetPlayState())
{
case PlayState.Playing:
case PlayState.Paused:
// ...case NotificationType.TrackChanged:
break;
}
break;
case NotificationType.TrackChanged:
using (var albumArtBMP = new Bitmap(hue.getAlbumArt(@mbApiInterface.NowPlaying_GetFileUrl())))
{
if (Settings.Instance.AverageColor && !stop)
{
var rgbList = hue.getRGBAverage(albumArtBMP, int.Parse(Settings.Instance.QualitySetting));
var xy = hue.ToXYZ(rgbList[0], rgbList[1], rgbList[2]);
foreach (string lightName in Settings.Instance.HueLights)
{
new LightStateBuilder().For(lights[lightName]).XYCoordinates(xy[0], xy[1]).Apply();
}
}
if (Settings.Instance.ColorPalette)
{
List<Tuple<double, double>> colors = new List<Tuple<double, double>>();
//Thread thread;
var palette = hue.getColorPalette(albumArtBMP, 8, int.Parse(Settings.Instance.QualitySetting));
foreach (string str in palette)
{
int red = hue.getRed(str);
int green = hue.getGreen(str);
int blue = hue.getBlue(str);
var xy = hue.ToXYZ(red, green, blue);
colors.Add(Tuple.Create((double)xy[0], (double)xy[1]));
}
if (palette.Count == 0)
{
var xyWhite = hue.ToXYZ(255, 255, 255);
colors.Add(Tuple.Create((double)xyWhite[0], (double)xyWhite[1]));
}
//Below is the code for the thread version of sending the color. This
//has way more cpu usage
//threadStop = !threadStop;
//System.Threading.Thread.Sleep(500);
//thread = new Thread(() => sendColorsThread(colors, colors.Count));
//thread.IsBackground = true;
//thread.Start();
maxRange = 0;
aTimer.Dispose();
//System.Threading.Thread.Sleep(1000);
aTimer = new System.Timers.Timer();
aTimer.Stop();
aTimer.Elapsed += (object s, ElapsedEventArgs a) => sendColorsTimer(colors, colors.Count);
aTimer.Start();
aTimer.Interval = 7000;
aTimer.Enabled = true;
}
}
break;
}
}
// return an array of lyric or artwork provider names this plugin supports
// the providers will be iterated through one by one and passed to the RetrieveLyrics/ RetrieveArtwork function in order set by the user in the MusicBee Tags(2) preferences screen until a match is found
public string[] GetProviders()
{
return null;
}
// return lyrics for the requested artist/title from the requested provider
// only required if PluginType = LyricsRetrieval
// return null if no lyrics are found
public string RetrieveLyrics(string sourceFileUrl, string artist, string trackTitle, string album, bool synchronisedPreferred, string provider)
{
return null;
}
// return Base64 string representation of the artwork binary data from the requested provider
// only required if PluginType = ArtworkRetrieval
// return null if no artwork is found
public string RetrieveArtwork(string sourceFileUrl, string albumArtist, string album, string provider)
{
//Return Convert.ToBase64String(artworkBinaryData)
return null;
}
public void donothing(object sender, EventArgs args)
{
Console.WriteLine(mbApiInterface.MusicBeeVersion.ToString());
}
private void OnOpen(object sender, EventArgs args)
{
settingsSelector = new SettingsSelector();
settingsSelector.startup();
settingsSelector.Show();
}
private void stopPlugin(object sender, EventArgs args)
{
stop = true;
if (Settings.Instance.ColorPalette)
{
threadStop = true;
}
foreach (string lightNames in Settings.Instance.HueLights)
{
new LightStateBuilder().For(lights[lightNames]).TurnOff().Apply();
}
}
private void resumePlugin(object sender, EventArgs args)
{
stop = false;
threadStop = !threadStop;
foreach (string lightNames in Settings.Instance.HueLights)
{
new LightStateBuilder().For(lights[lightNames]).TurnOn().Apply();
}
}
public void sendColorsThread(List<Tuple<double, double>> colors, int max)
{
var stopwatch = new Stopwatch();
int index;
stopwatch.Start();
while (!threadStop)
{
index = rnd.Next(0, max);
if (stopwatch.ElapsedMilliseconds > 4500)
{
foreach (string lightName in Settings.Instance.HueLights)
{
new LightStateBuilder().For(lights[lightName]).TransitionTime(30).XYCoordinates(colors[index].Item1, colors[index].Item2).Apply();
stopwatch.Restart();
}
}
}
threadStop = !threadStop;
return;
}
public void sendColorsTimer(List<Tuple<double, double>> colors, int max)
{
//string response = string.Format("Sending Colors for {0}: {1}: {2}ms", songName, maxRange, stopwatch.Elapsed.TotalSeconds.ToString());
//System.Diagnostics.Debug.WriteLine(response);
if (maxRange > max - 1)
{
maxRange = 0;
}
foreach (string lightName in Settings.Instance.HueLights)
{
try
{
new LightStateBuilder().For(lights[lightName]).TransitionTime(40).XYCoordinates(colors[maxRange].Item1, colors[maxRange].Item2).Apply();
System.Diagnostics.Debug.WriteLine(maxRange.ToString());
}
catch (IndexOutOfRangeException e)
{
new LightStateBuilder().For(lights[lightName]).TransitionTime(40).XYCoordinates(colors[0].Item1, colors[0].Item2).Apply();
}
}
maxRange++;
return;
}
}
}