2016-06-24 16:44:42 -04:00
|
|
|
|
using System;
|
2016-05-17 15:37:02 -04:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using NAudio;
|
|
|
|
|
using NAudio.Wave;
|
|
|
|
|
using System.Threading;
|
2016-06-24 16:44:42 -04:00
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
using AxWMPLib;
|
|
|
|
|
using System.Windows.Forms;
|
|
|
|
|
using WMPLib;
|
2016-05-17 15:37:02 -04:00
|
|
|
|
|
|
|
|
|
namespace ShiftOS
|
|
|
|
|
{
|
2016-06-24 16:44:42 -04:00
|
|
|
|
public class AudioData
|
2016-05-17 15:37:02 -04:00
|
|
|
|
{
|
2016-06-24 16:44:42 -04:00
|
|
|
|
public string StartURL { get; set; }
|
|
|
|
|
public Dictionary<string, List<string>> Files { get; set; }
|
|
|
|
|
public Dictionary<string, List<Visualizer>> Visualizers = null;
|
2016-05-17 15:37:02 -04:00
|
|
|
|
}
|
|
|
|
|
|
2016-06-24 16:44:42 -04:00
|
|
|
|
public class Audio
|
2016-05-17 15:37:02 -04:00
|
|
|
|
{
|
2016-06-24 16:44:42 -04:00
|
|
|
|
public static string Name
|
2016-05-17 15:37:02 -04:00
|
|
|
|
{
|
2016-06-24 16:44:42 -04:00
|
|
|
|
get
|
2016-05-17 15:37:02 -04:00
|
|
|
|
{
|
2016-06-24 16:44:42 -04:00
|
|
|
|
string res = null;
|
|
|
|
|
try
|
2016-05-17 15:37:02 -04:00
|
|
|
|
{
|
2016-06-24 16:44:42 -04:00
|
|
|
|
if (_wmp != null)
|
2016-05-17 15:37:02 -04:00
|
|
|
|
{
|
2016-06-24 16:44:42 -04:00
|
|
|
|
if (_wmp.currentMedia != null)
|
2016-05-17 15:37:02 -04:00
|
|
|
|
{
|
2016-06-24 16:44:42 -04:00
|
|
|
|
res = _wmp.currentMedia.name;
|
2016-05-17 15:37:02 -04:00
|
|
|
|
}
|
2016-06-24 16:44:42 -04:00
|
|
|
|
|
2016-05-17 15:37:02 -04:00
|
|
|
|
}
|
2016-06-24 16:44:42 -04:00
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
2016-05-17 15:37:02 -04:00
|
|
|
|
|
2016-06-24 16:44:42 -04:00
|
|
|
|
}
|
|
|
|
|
return res;
|
|
|
|
|
}
|
2016-05-17 15:37:02 -04:00
|
|
|
|
}
|
|
|
|
|
|
2016-06-24 16:44:42 -04:00
|
|
|
|
private static AudioData _ad = null;
|
|
|
|
|
public static WindowsMediaPlayer _wmp = null;
|
|
|
|
|
public static bool running = true;
|
|
|
|
|
|
|
|
|
|
public static int CurrentPosition
|
2016-05-17 15:37:02 -04:00
|
|
|
|
{
|
2016-06-24 16:44:42 -04:00
|
|
|
|
get
|
2016-05-17 15:37:02 -04:00
|
|
|
|
{
|
2016-06-24 16:44:42 -04:00
|
|
|
|
try
|
2016-05-17 15:37:02 -04:00
|
|
|
|
{
|
2016-06-24 16:44:42 -04:00
|
|
|
|
if (_wmp != null)
|
2016-05-17 15:37:02 -04:00
|
|
|
|
{
|
2016-06-24 16:44:42 -04:00
|
|
|
|
return (int)_wmp.controls.currentPosition;
|
2016-05-17 15:37:02 -04:00
|
|
|
|
}
|
2016-06-24 16:44:42 -04:00
|
|
|
|
else
|
2016-05-17 15:37:02 -04:00
|
|
|
|
{
|
2016-06-24 16:44:42 -04:00
|
|
|
|
return 0;
|
2016-05-17 15:37:02 -04:00
|
|
|
|
}
|
2016-06-24 16:44:42 -04:00
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2016-05-17 15:37:02 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-24 16:44:42 -04:00
|
|
|
|
public static int CurrentPositionMS
|
2016-05-17 15:37:02 -04:00
|
|
|
|
{
|
2016-06-24 16:44:42 -04:00
|
|
|
|
get
|
2016-05-17 15:37:02 -04:00
|
|
|
|
{
|
2016-06-24 16:44:42 -04:00
|
|
|
|
try
|
2016-05-17 15:37:02 -04:00
|
|
|
|
{
|
2016-06-24 16:44:42 -04:00
|
|
|
|
if (_wmp != null)
|
2016-05-17 15:37:02 -04:00
|
|
|
|
{
|
2016-06-24 16:44:42 -04:00
|
|
|
|
return (int)(_wmp.controls.currentPosition * 100);
|
2016-05-17 15:37:02 -04:00
|
|
|
|
}
|
2016-06-24 16:44:42 -04:00
|
|
|
|
else
|
2016-05-17 15:37:02 -04:00
|
|
|
|
{
|
2016-06-24 16:44:42 -04:00
|
|
|
|
return 0;
|
2016-05-17 15:37:02 -04:00
|
|
|
|
}
|
2016-06-24 16:44:42 -04:00
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2016-05-17 15:37:02 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2016-06-24 16:44:42 -04:00
|
|
|
|
public static void LoadAudioData()
|
2016-05-17 15:37:02 -04:00
|
|
|
|
{
|
2016-06-24 16:44:42 -04:00
|
|
|
|
var t = new Thread(new ThreadStart(() =>
|
2016-05-17 15:37:02 -04:00
|
|
|
|
{
|
2016-06-24 16:44:42 -04:00
|
|
|
|
_wmp = new WindowsMediaPlayer();
|
|
|
|
|
_wmp.settings.autoStart = true;
|
|
|
|
|
_wmp.settings.volume = API.LoadedSettings.MusicVolume;
|
|
|
|
|
}));
|
|
|
|
|
t.Start();
|
|
|
|
|
_ad = JsonConvert.DeserializeObject<AudioData>(Properties.Resources.MusicData);
|
2016-05-17 15:37:02 -04:00
|
|
|
|
}
|
|
|
|
|
|
2016-06-24 16:44:42 -04:00
|
|
|
|
public static void Play(string list)
|
2016-05-17 15:37:02 -04:00
|
|
|
|
{
|
2016-06-24 16:44:42 -04:00
|
|
|
|
_forceStop = false;
|
|
|
|
|
var lst = _ad.Files[list];
|
|
|
|
|
var rnd = new Random();
|
|
|
|
|
int i = rnd.Next(0, lst.Count);
|
|
|
|
|
_wmp.URL = _ad.StartURL + list + "/" + _ad.Files[list][i] + ".mp3";
|
|
|
|
|
_wmp.PlayStateChange += (o) =>
|
2016-05-17 15:37:02 -04:00
|
|
|
|
{
|
2016-06-24 16:44:42 -04:00
|
|
|
|
if (o == (int)WMPPlayState.wmppsMediaEnded)
|
2016-05-17 15:37:02 -04:00
|
|
|
|
{
|
2016-06-24 16:44:42 -04:00
|
|
|
|
var t = new System.Windows.Forms.Timer();
|
|
|
|
|
t.Interval = 1000;
|
|
|
|
|
t.Tick += (object s, EventArgs a) =>
|
2016-05-17 15:37:02 -04:00
|
|
|
|
{
|
2016-06-24 16:44:42 -04:00
|
|
|
|
t.Stop();
|
|
|
|
|
Stopped?.Invoke(_wmp, new EventArgs());
|
|
|
|
|
};
|
|
|
|
|
t.Start();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2016-05-17 15:37:02 -04:00
|
|
|
|
}
|
|
|
|
|
|
2016-06-24 16:44:42 -04:00
|
|
|
|
public static event EventHandler Stopped;
|
|
|
|
|
|
|
|
|
|
public static bool _forceStop = false;
|
|
|
|
|
|
|
|
|
|
public static void Mute()
|
2016-05-17 15:37:02 -04:00
|
|
|
|
{
|
2016-06-24 16:44:42 -04:00
|
|
|
|
_wmp.settings.volume = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void VolumeUp()
|
|
|
|
|
{
|
|
|
|
|
if(_wmp.settings.volume < 90)
|
2016-05-17 15:37:02 -04:00
|
|
|
|
{
|
2016-06-24 16:44:42 -04:00
|
|
|
|
_wmp.settings.volume += 10;
|
2016-05-17 15:37:02 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-24 16:44:42 -04:00
|
|
|
|
public static void VolumeDown()
|
2016-05-17 15:37:02 -04:00
|
|
|
|
{
|
2016-06-24 16:44:42 -04:00
|
|
|
|
if(_wmp.settings.volume > 0)
|
2016-05-17 15:37:02 -04:00
|
|
|
|
{
|
2016-06-24 16:44:42 -04:00
|
|
|
|
_wmp.settings.volume -= 10;
|
2016-05-17 15:37:02 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-24 16:44:42 -04:00
|
|
|
|
public static void Pause()
|
|
|
|
|
{
|
|
|
|
|
_wmp.controls.pause();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void Unpause()
|
2016-05-17 15:37:02 -04:00
|
|
|
|
{
|
2016-06-24 16:44:42 -04:00
|
|
|
|
_wmp.controls.play();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void ForceStop()
|
|
|
|
|
{
|
|
|
|
|
new Thread(new ThreadStart(() =>
|
2016-05-17 15:37:02 -04:00
|
|
|
|
{
|
2016-06-24 16:44:42 -04:00
|
|
|
|
_forceStop = true;
|
|
|
|
|
_wmp.controls.stop();
|
|
|
|
|
})).Start();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal static Visualizer GetVisualizer()
|
|
|
|
|
{
|
|
|
|
|
Visualizer v = null;
|
|
|
|
|
foreach(var vis in _ad.Visualizers[Name])
|
|
|
|
|
{
|
|
|
|
|
if (vis.startTime <= CurrentPosition && vis.endTime >= CurrentPosition)
|
|
|
|
|
v = vis;
|
2016-05-17 15:37:02 -04:00
|
|
|
|
}
|
2016-06-24 16:44:42 -04:00
|
|
|
|
return v;
|
2016-05-17 15:37:02 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
2016-06-24 16:44:42 -04:00
|
|
|
|
|
|
|
|
|
public class Visualizer
|
|
|
|
|
{
|
|
|
|
|
public int startTime { get; set; }
|
|
|
|
|
public int endTime { get; set; }
|
|
|
|
|
public VisualizerType type { get; set; }
|
|
|
|
|
public bool R { get; set; }
|
|
|
|
|
public bool G { get; set; }
|
|
|
|
|
public bool B { get; set; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public enum VisualizerType
|
|
|
|
|
{
|
|
|
|
|
Pulse,
|
|
|
|
|
BuildUp,
|
|
|
|
|
CalmDown
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-17 15:37:02 -04:00
|
|
|
|
}
|