/// <summary> /// Decodes the mp3 file /// </summary> public WaveFile Decode(Stream input) { //TODO: Still AWFUL playback quality Mp3Stream mp3 = new Mp3Stream(input); mp3.Seek(0, SeekOrigin.Begin); mp3.DecodeFrames(1); WaveFile wf = new WaveFile(); wf.Bits = 16; wf.Channels = mp3.ChannelCount; wf.Frequency = mp3.Frequency; System.Console.WriteLine(mp3.Length); MemoryStream data = new MemoryStream(); int buffersize = 4*4096; byte[] buffer = new byte[buffersize]; int result = 1; while(true) { result = mp3.Read(buffer, 0, buffersize); data.Write(buffer, 0, result); if(result != buffersize) { break; } } data.Seek(0, SeekOrigin.Begin); wf.Data = data; Axiom.Core.LogManager.Instance.Write("SoundSystem: File is MPEG Layer III "+wf.Frequency+"Hz, "+wf.Channels+" channels"); return wf; }
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) { stream.DecodeFrames(1); } if (stream.Frequency > 0 && stream.ChannelCount > 0) { this.WaveFormat = SoundUtil.CreateWaveFormat(stream.Frequency, 16, stream.ChannelCount); } }
public static bool TryPlay(Stream audio, IntPtr hwnd) { try { Mp3Stream mp3Stream = new Mp3Sharp.Mp3Stream(audio); mp3Stream.Position = 0; if (mp3Stream.DecodeFrames(1) == 0) { throw new Exception("Could not decode"); } Device dev = new Microsoft.DirectX.DirectSound.Device(); dev.SetCooperativeLevel(hwnd, Microsoft.DirectX.DirectSound.CooperativeLevel.Normal); StreamedMp3Sound ApplicationStreamedSound = new StreamedMp3Sound(dev, mp3Stream); ApplicationStreamedSound.Play(); ApplicationStreamedSound.Stop(); return(true); } catch (Exception ex) { return(false); } }