private Thread MainThread; //keep track of this, terminate when it closes. public MP3Player(string path) { Stream = new Mp3Stream(path); Stream.DecodeFrames(1); //let's get started... DecodeNext = new AutoResetEvent(true); BufferDone = new AutoResetEvent(false); Inst = new DynamicSoundEffectInstance(Stream.Frequency, AudioChannels.Stereo); Inst.IsLooped = false; Inst.BufferNeeded += SubmitBufferAsync; SubmitBuffer(null, null); SubmitBuffer(null, null); NextBuffers = new List <byte[]>(); NextSizes = new List <int>(); Requests = 1; MainThread = Thread.CurrentThread; DecoderThread = new Thread(() => { try { while (MainThread.IsAlive) { DecodeNext.WaitOne(128); bool go; lock (this) go = Requests > 0; while (go) { var buf = new byte[524288]; var read = Stream.Read(buf, 0, buf.Length); lock (this) { Requests--; NextBuffers.Add(buf); NextSizes.Add(read); if (read == 0) { EndOfStream = true; return; } BufferDone.Set(); } lock (this) go = Requests > 0; } } } catch (Exception e) { } }); DecoderThread.Start(); }
protected override void OnBufferInitializing() { Mp3Stream stream = Stream as Mp3Stream; if (stream == null) { throw new ApplicationException("The stream used by the StreamedMp3Sound class should be of type Mp3Stream."); } if (stream.Frequency < 0) { if (stream.DecodeFrames(1) == 0) { // throw new Exception("Could not decode"); } } if (stream.Frequency > 0 && stream.ChannelCount > 0) { this.WaveFormat = SoundUtil.CreateWaveFormat(stream.Frequency, 16, stream.ChannelCount); } }
public override Wavefile Load(Stream fileStream) { Mp3Stream mp3Stream = new Mp3Stream(fileStream); if (mp3Stream.Frequency < 0) { mp3Stream.DecodeFrames(1); } if (!(mp3Stream.Frequency > 0 && mp3Stream.ChannelCount > 0)) { throw new InvalidFileFormatException("No frequency/channel information"); } if (mp3Stream.Format != SoundFormat.Pcm16BitStereo) { throw new NotImplementedException("Only stereo MP3 supported"); } Wavefile wave = new Wavefile(); wave.m_SampleRate = mp3Stream.Frequency; wave.m_Samples = new BlockArray <Sample>(12); m_Wavefile = wave; //new DoLoadingDelegate(DoLoading).BeginInvoke(wave, mp3Stream, new AsyncCallback(FinishedLoadingCallback), wave); Thread loadThread = new Thread(new ParameterizedThreadStart(DoLoading)); loadThread.IsBackground = true; loadThread.Start(mp3Stream); //DoLoading(wave, mp3Stream); return(wave); }
public void Start() { Stream = new Mp3Stream(Path); Stream.DecodeFrames(1); var freq = Stream.Frequency; lock (ControlLock) { if (Disposed) { return; } Inst = new DynamicSoundEffectInstance(freq, AudioChannels.Stereo); Inst.IsLooped = false; Inst.BufferNeeded += SubmitBufferAsync; if (_State == SoundState.Playing) { Inst.Play(); } else if (_State == SoundState.Paused) { Inst.Play(); Inst.Pause(); } Inst.Volume = _Volume; Inst.Pan = _Pan; Requests = 2; } //SubmitBuffer(null, null); //SubmitBuffer(null, null); DecoderThread = new Thread(() => { try { while (Active && MainThread.IsAlive) { DecodeNext.WaitOne(128); bool go; lock (this) go = Requests > 0; while (go) { var buf = new byte[262144];// 524288]; var read = Stream.Read(buf, 0, buf.Length); lock (this) { Requests--; NextBuffers.Add(buf); NextSizes.Add(read); if (read == 0) { EndOfStream = true; BufferDone.Set(); return; } BufferDone.Set(); } lock (this) go = Requests > 0; } } } catch (Exception e) { } }); DecoderThread.Start(); DecodeNext.Set(); }