AudioHandler.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. using NAudio.Wave;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Net;
  7. using System.Text;
  8. using System.Threading;
  9. using System.Threading.Tasks;
  10. namespace MusicPlayer
  11. {
  12. public class AudioHandler
  13. {
  14. public enum AudioState { PLAYING, WAITING, STOPPED, PAUSED, SEEKING }
  15. public enum BufferState { EMPTY, BUFFERING, DONE }
  16. public AudioState AState { get; set; }
  17. public BufferState BState { get; set; }
  18. public int Buffered { get
  19. {
  20. if(CurrentSong is RadioStation)
  21. return Math.Min((int)((bufpos - ms.Position) / 1000), 1000);
  22. else
  23. return Math.Min((int)((bufpos / (double)LengthBuffer) * 1000), 1000);
  24. } }
  25. private long LengthBuffer { get; set; }
  26. private long bufpos = 0;
  27. public int Position { get { return Math.Min((int)((playpos / (double)Length) * 1000), 1000); } }
  28. private long Length { get; set; }
  29. private long playpos = 0;
  30. public int CurrentTime { get; set; }
  31. public int TotalTime { get { return CurrentSong != null ? CurrentSong.Seconds : 0; } }
  32. private long seek = 0;
  33. private MemoryStream ms;
  34. private Thread network;
  35. private Thread audio;
  36. public Song CurrentSong;
  37. private Main main;
  38. public AudioHandler(Main main)
  39. {
  40. this.main = main;
  41. CreateThreads();
  42. }
  43. private void CreateThreads()
  44. {
  45. AState = AudioState.STOPPED;
  46. BState = BufferState.EMPTY;
  47. CurrentSong = null;
  48. Thread.Sleep(10);
  49. ms = new MemoryStream();
  50. network = new Thread(LoadAudio);
  51. audio = new Thread(PlayAudio);
  52. network.IsBackground = true;
  53. audio.IsBackground = true;
  54. Length = 1;
  55. LengthBuffer = 1;
  56. bufpos = 0;
  57. playpos = 0;
  58. CurrentTime = 0;
  59. }
  60. public void Play(Song s)
  61. {
  62. if (CurrentSong == s && AState == AudioState.PAUSED)
  63. AState = AudioState.PLAYING;
  64. else
  65. {
  66. Stop();
  67. CurrentSong = s;
  68. network.Start(s);
  69. audio.Start();
  70. }
  71. }
  72. public void Seek(int position)
  73. {
  74. if (position >= Buffered-1)
  75. return;
  76. seek = Length / 1000 * position;
  77. AState = AudioState.SEEKING;
  78. }
  79. public void Stop()
  80. {
  81. CreateThreads();
  82. }
  83. public void Pause()
  84. {
  85. if(CurrentSong == null)
  86. AState = AudioState.STOPPED;
  87. else
  88. AState = AudioState.PAUSED;
  89. }
  90. private void StreamFromMP3(Stream s, long pos, bool firstrun)
  91. {
  92. long position = 0;
  93. ms.Position = position;
  94. Mp3FileReader mp3fr = null;
  95. try
  96. {
  97. mp3fr = new Mp3FileReader(ms);
  98. }
  99. catch(Exception e)
  100. {
  101. AState = AudioState.STOPPED;
  102. main.form.SongFinished();
  103. return;
  104. }
  105. using (WaveStream blockAlignedStream = new BlockAlignReductionStream(WaveFormatConversionStream.CreatePcmStream(mp3fr)))
  106. {
  107. blockAlignedStream.Position = pos;
  108. using (WaveOut waveOut = new WaveOut(WaveCallbackInfo.FunctionCallback()))
  109. {
  110. waveOut.Init(blockAlignedStream);
  111. waveOut.Play();
  112. if (CurrentSong.Seconds == 0)
  113. Length = ms.Length / waveOut.OutputWaveFormat.AverageBytesPerSecond;
  114. else
  115. Length = CurrentSong.Seconds * waveOut.OutputWaveFormat.AverageBytesPerSecond;
  116. CurrentTime = (int)(ms.Position / waveOut.OutputWaveFormat.AverageBytesPerSecond);
  117. while (waveOut.PlaybackState != PlaybackState.Stopped)
  118. {
  119. System.Threading.Thread.Sleep(10);
  120. if (AState == AudioState.PLAYING && waveOut.PlaybackState == PlaybackState.Paused)
  121. {
  122. blockAlignedStream.Position = position;
  123. waveOut.Play();
  124. }
  125. if (AState == AudioState.PAUSED && waveOut.PlaybackState == PlaybackState.Playing)
  126. {
  127. position = blockAlignedStream.Position;
  128. waveOut.Pause();
  129. }
  130. if (AState == AudioState.SEEKING)
  131. {
  132. blockAlignedStream.Seek(seek - (seek % blockAlignedStream.WaveFormat.BlockAlign), SeekOrigin.Begin);
  133. AState = AudioState.PLAYING;
  134. waveOut.Play();
  135. }
  136. if (AState == AudioState.STOPPED)
  137. {
  138. waveOut.Stop();
  139. }
  140. if (BState == BufferState.DONE && firstrun )
  141. {
  142. if( ! (CurrentSong is RadioStation))
  143. {
  144. position = mp3fr.Position;
  145. mp3fr.Close();
  146. StreamFromMP3(ms, position, false);
  147. break;
  148. }
  149. else
  150. {
  151. AState = AudioState.STOPPED;
  152. }
  153. }
  154. playpos = blockAlignedStream.Position;
  155. CurrentTime = (int)(playpos / waveOut.OutputWaveFormat.AverageBytesPerSecond);
  156. }
  157. if(AState == AudioState.PLAYING && !firstrun)
  158. main.form.SongFinished();
  159. AState = AudioState.STOPPED;
  160. playpos = 0;
  161. CurrentTime = 0;
  162. }
  163. }
  164. }
  165. private void PlayAudio()
  166. {
  167. AState = AudioState.WAITING;
  168. int buffertime = 0;
  169. if (CurrentSong is RadioStation)
  170. buffertime = 32768 * 10;
  171. else
  172. buffertime = 65536 * 10;
  173. while (ms.Length < buffertime && BState != BufferState.DONE)
  174. Thread.Sleep(1000);
  175. AState = AudioState.PLAYING;
  176. StreamFromMP3(ms,0, true);
  177. }
  178. private void LoadAudio(object o)
  179. {
  180. Song s = (Song) o;
  181. WebResponse response = null;
  182. try
  183. {
  184. response = WebRequest.Create(s.Url).GetResponse();
  185. }
  186. catch(Exception e)
  187. {
  188. BState = BufferState.EMPTY;
  189. AState = AudioState.STOPPED;
  190. main.form.SongFinished();
  191. return;
  192. }
  193. BState = BufferState.EMPTY;
  194. LengthBuffer = response.ContentLength;
  195. using (var stream = response.GetResponseStream())
  196. {
  197. byte[] buffer = new byte[65536]; // 64KB chunks
  198. int read;
  199. BState = BufferState.BUFFERING;
  200. AState = AudioState.WAITING;
  201. while ((read = stream.Read(buffer, 0, buffer.Length)) > 0 && AState != AudioState.STOPPED)
  202. {
  203. var pos = ms.Position;
  204. ms.Position = ms.Length;
  205. ms.Write(buffer, 0, read);
  206. ms.Position = pos;
  207. this.bufpos = ms.Length;
  208. }
  209. }
  210. BState = BufferState.DONE;
  211. }
  212. }
  213. }