aboutsummaryrefslogtreecommitdiff
path: root/source/WindowsFormsApplication1/AudioResourceClient.cs
diff options
context:
space:
mode:
authorMichaelTheShifter <[email protected]>2016-05-17 15:37:02 -0400
committerMichaelTheShifter <[email protected]>2016-05-17 15:37:02 -0400
commita3fc2c45ec2a62684e128ffd7cab88bd101ad917 (patch)
tree1dc63efaa4597a28fd901047a714f38954dafa5e /source/WindowsFormsApplication1/AudioResourceClient.cs
parent0085241d2366f266b5416488dbead174184420b0 (diff)
downloadshiftos-c--a3fc2c45ec2a62684e128ffd7cab88bd101ad917.tar.gz
shiftos-c--a3fc2c45ec2a62684e128ffd7cab88bd101ad917.tar.bz2
shiftos-c--a3fc2c45ec2a62684e128ffd7cab88bd101ad917.zip
Committing all I've got
Committing everything I've got - so that I can take a break for a few months and work on other things.
Diffstat (limited to 'source/WindowsFormsApplication1/AudioResourceClient.cs')
-rw-r--r--source/WindowsFormsApplication1/AudioResourceClient.cs250
1 files changed, 250 insertions, 0 deletions
diff --git a/source/WindowsFormsApplication1/AudioResourceClient.cs b/source/WindowsFormsApplication1/AudioResourceClient.cs
new file mode 100644
index 0000000..ec51b18
--- /dev/null
+++ b/source/WindowsFormsApplication1/AudioResourceClient.cs
@@ -0,0 +1,250 @@
+// WARNING: This thing likes leaking memory.
+
+
+using System;
+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;
+
+namespace ShiftOS
+{
+ public class Audio
+ {
+ 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 class AudioResourceClient
+ {
+
+
+ public AudioResourceClient(string folder)
+ {
+ if (Audio.Enabled)
+ {
+ 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(() =>
+ {
+ while (disposed == false)
+ {
+ switch (soundPlayer.PlaybackState)
+ {
+ 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;
+ }
+ }
+ })));
+ t.Start();
+ }
+ }
+
+ public string CurrentSong
+ {
+ get { return currentSong; }
+ }
+
+ 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)
+ {
+ if (Audio.Enabled)
+ {
+ var t = new Thread(new ThreadStart(new Action(() =>
+ {
+ try
+ {
+ 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;
+ }
+ }
+ catch
+ {
+
+ }
+ })));
+ soundThread = t;
+ t.Start();
+ }
+ }
+
+ public void PlayMP3(byte[] songbytes)
+ {
+ if (Audio.Enabled)
+ {
+ var t = new Thread(new ThreadStart(new Action(() =>
+ {
+ if (currentProvider != null)
+ {
+ currentProvider = null;
+ }
+ if (currentStream != null)
+ {
+ currentStream.Dispose();
+ currentStream = null;
+ }
+ Stream s = new MemoryStream(songbytes);
+ IWaveProvider provider = new Mp3FileReader(s);
+ soundPlayer.Init(provider);
+ soundPlayer.Play();
+ currentStream = s;
+ currentProvider = provider;
+ })));
+ soundThread = t;
+ t.Start();
+ }
+ }
+
+
+ public void Play(string file)
+ {
+ if (Audio.Enabled)
+ {
+ 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"));
+ }
+ }
+ }
+
+ public void Stop()
+ {
+ if (Audio.Enabled)
+ {
+ var t = new Thread(new ThreadStart(new Action(() =>
+ {
+ fireEvents = false;
+ soundPlayer.Stop();
+ if (currentProvider != null)
+ {
+ currentProvider = null;
+ }
+ if (currentStream != null)
+ {
+ currentStream.Dispose();
+ currentStream = null;
+ }
+ })));
+ t.Start();
+ }
+ }
+
+ public void Pause()
+ {
+ if (Audio.Enabled)
+ {
+ if (soundPlayer.PlaybackState == PlaybackState.Playing)
+ {
+ soundPlayer.Pause();
+ }
+ else if (soundPlayer.PlaybackState == PlaybackState.Paused)
+ {
+ soundPlayer.Resume();
+ }
+ }
+ }
+
+ public void PlayRandom()
+ {
+ if (Audio.Enabled)
+ {
+ 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);
+
+ }
+ }
+
+ public void Dispose()
+ {
+ if (Audio.Enabled)
+ {
+ if (currentProvider != null)
+ {
+ currentProvider = null;
+ }
+ if (currentStream != null)
+ {
+ currentStream.Dispose();
+ currentStream = null;
+ }
+ soundPlayer.Dispose();
+ disposed = true;
+ }
+ }
+ }
+}