aboutsummaryrefslogtreecommitdiff
path: root/source/WindowsFormsApplication1/AudioResourceClient.cs
diff options
context:
space:
mode:
Diffstat (limited to 'source/WindowsFormsApplication1/AudioResourceClient.cs')
-rw-r--r--source/WindowsFormsApplication1/AudioResourceClient.cs314
1 files changed, 136 insertions, 178 deletions
diff --git a/source/WindowsFormsApplication1/AudioResourceClient.cs b/source/WindowsFormsApplication1/AudioResourceClient.cs
index ec51b18..717d43a 100644
--- a/source/WindowsFormsApplication1/AudioResourceClient.cs
+++ b/source/WindowsFormsApplication1/AudioResourceClient.cs
@@ -1,7 +1,4 @@
-// WARNING: This thing likes leaking memory.
-
-
-using System;
+using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
@@ -10,241 +7,202 @@ using System.IO;
using NAudio;
using NAudio.Wave;
using System.Threading;
+using Newtonsoft.Json;
+using AxWMPLib;
+using System.Windows.Forms;
+using WMPLib;
namespace ShiftOS
{
- public class Audio
+ public class AudioData
{
- public static List<AudioResourceClient> Clients = null;
- internal static bool Enabled = false;
-
- public static void DisposeAll()
- {
- if(Clients != null)
- {
- foreach(var client in Clients)
- {
- client.Dispose();
- }
- }
- }
+ public string StartURL { get; set; }
+ public Dictionary<string, List<string>> Files { get; set; }
+ public Dictionary<string, List<Visualizer>> Visualizers = null;
}
- public class AudioResourceClient
+ public class Audio
{
-
-
- public AudioResourceClient(string folder)
+ public static string Name
{
- if (Audio.Enabled)
+ get
{
- if (Audio.Clients == null)
- {
- Audio.Clients = new List<AudioResourceClient>();
- }
- Audio.Clients.Add(this);
- string real = folder.Replace(";", OSInfo.DirectorySeparator);
- real_path = "resources" + OSInfo.DirectorySeparator + real;
- soundPlayer = new WaveOut();
- var t = new Thread(new ThreadStart(new Action(() =>
+ string res = null;
+ try
{
- while (disposed == false)
+ if (_wmp != null)
{
- switch (soundPlayer.PlaybackState)
+ if (_wmp.currentMedia != null)
{
- case PlaybackState.Stopped:
- if (fireEvents)
- {
- if (currentStream != null)
- currentStream.Dispose();
- currentStream = null;
- currentProvider = null;
- var h = SongFinished;
- if (h != null)
- {
- API.CurrentSession.Invoke(new Action(() =>
- {
- h(this, new EventArgs());
- }));
- }
-
- }
-
- break;
+ res = _wmp.currentMedia.name;
}
+
}
- })));
- t.Start();
- }
- }
+ }
+ catch
+ {
- public string CurrentSong
- {
- get { return currentSong; }
+ }
+ return res;
+ }
}
- private string real_path = null;
- private WaveOut soundPlayer = null;
- private bool disposed = false;
- private bool playing = false;
- private bool loopActive = false;
- private string currentSong = "";
- protected Thread soundThread;
- private Stream currentStream = null;
- private IWaveProvider currentProvider = null;
- private bool fireEvents = false;
-
- public event EventHandler SongFinished;
-
- public void Play(byte[] songbytes)
+ private static AudioData _ad = null;
+ public static WindowsMediaPlayer _wmp = null;
+ public static bool running = true;
+
+ public static int CurrentPosition
{
- if (Audio.Enabled)
+ get
{
- var t = new Thread(new ThreadStart(new Action(() =>
+ try
{
- try
+ if (_wmp != null)
{
- if (disposed == false)
- {
- if (currentProvider != null)
- {
- currentProvider = null;
- }
- if (currentStream != null)
- {
- currentStream.Dispose();
- currentStream = null;
- }
- Stream s = new MemoryStream(songbytes);
- IWaveProvider provider = new RawSourceWaveStream(s, new WaveFormat());
- soundPlayer.Init(provider);
- soundPlayer.Play();
- fireEvents = true;
- currentStream = s;
- currentProvider = provider;
- }
+ return (int)_wmp.controls.currentPosition;
}
- catch
+ else
{
-
+ return 0;
}
- })));
- soundThread = t;
- t.Start();
+ }
+ catch
+ {
+ return 0;
+ }
}
}
- public void PlayMP3(byte[] songbytes)
+ public static int CurrentPositionMS
{
- if (Audio.Enabled)
+ get
{
- var t = new Thread(new ThreadStart(new Action(() =>
+ try
{
- if (currentProvider != null)
+ if (_wmp != null)
{
- currentProvider = null;
+ return (int)(_wmp.controls.currentPosition * 100);
}
- if (currentStream != null)
+ else
{
- currentStream.Dispose();
- currentStream = null;
+ return 0;
}
- Stream s = new MemoryStream(songbytes);
- IWaveProvider provider = new Mp3FileReader(s);
- soundPlayer.Init(provider);
- soundPlayer.Play();
- currentStream = s;
- currentProvider = provider;
- })));
- soundThread = t;
- t.Start();
+ }
+ catch
+ {
+ return 0;
+ }
}
}
- public void Play(string file)
+ public static void LoadAudioData()
{
- if (Audio.Enabled)
+ var t = new Thread(new ThreadStart(() =>
{
- if (File.Exists(real_path + OSInfo.DirectorySeparator + file + ".wav"))
- {
- currentSong = file;
- Play(File.ReadAllBytes(real_path + OSInfo.DirectorySeparator + file + ".wav"));
- }
- else if (File.Exists(real_path + OSInfo.DirectorySeparator + file + ".mp3"))
- {
- currentSong = file;
- PlayMP3(File.ReadAllBytes(real_path + OSInfo.DirectorySeparator + file + ".mp3"));
- }
- }
+ _wmp = new WindowsMediaPlayer();
+ _wmp.settings.autoStart = true;
+ _wmp.settings.volume = API.LoadedSettings.MusicVolume;
+ }));
+ t.Start();
+ _ad = JsonConvert.DeserializeObject<AudioData>(Properties.Resources.MusicData);
}
- public void Stop()
+ public static void Play(string list)
{
- if (Audio.Enabled)
+ _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) =>
{
- var t = new Thread(new ThreadStart(new Action(() =>
+ if (o == (int)WMPPlayState.wmppsMediaEnded)
{
- fireEvents = false;
- soundPlayer.Stop();
- if (currentProvider != null)
- {
- currentProvider = null;
- }
- if (currentStream != null)
+ var t = new System.Windows.Forms.Timer();
+ t.Interval = 1000;
+ t.Tick += (object s, EventArgs a) =>
{
- currentStream.Dispose();
- currentStream = null;
- }
- })));
- t.Start();
- }
+ t.Stop();
+ Stopped?.Invoke(_wmp, new EventArgs());
+ };
+ t.Start();
+ }
+ };
+
}
- public void Pause()
+ public static event EventHandler Stopped;
+
+ public static bool _forceStop = false;
+
+ public static void Mute()
{
- if (Audio.Enabled)
+ _wmp.settings.volume = 0;
+ }
+
+ public static void VolumeUp()
+ {
+ if(_wmp.settings.volume < 90)
{
- if (soundPlayer.PlaybackState == PlaybackState.Playing)
- {
- soundPlayer.Pause();
- }
- else if (soundPlayer.PlaybackState == PlaybackState.Paused)
- {
- soundPlayer.Resume();
- }
+ _wmp.settings.volume += 10;
}
}
- public void PlayRandom()
+ public static void VolumeDown()
{
- if (Audio.Enabled)
+ if(_wmp.settings.volume > 0)
{
- int r = new Random().Next(0, Directory.GetFiles(real_path).Length);
- var files = Directory.GetFiles(real_path);
- FileInfo finf = new FileInfo(files[r]);
- string name = finf.Name.Replace(finf.Extension, "");
- Play(name);
-
+ _wmp.settings.volume -= 10;
}
}
- public void Dispose()
+ public static void Pause()
+ {
+ _wmp.controls.pause();
+ }
+
+ public static void Unpause()
{
- if (Audio.Enabled)
+ _wmp.controls.play();
+ }
+
+ public static void ForceStop()
+ {
+ new Thread(new ThreadStart(() =>
{
- if (currentProvider != null)
- {
- currentProvider = null;
- }
- if (currentStream != null)
- {
- currentStream.Dispose();
- currentStream = null;
- }
- soundPlayer.Dispose();
- disposed = true;
+ _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;
}
+ return v;
}
}
+
+ 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
+ }
+
}