/// <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; }
private void LoadMp3(object state) { // create mp3 using (FileStream f_stream = new FileStream(filename, FileMode.Open)) { Mp3Stream stream = new Mp3Sharp.Mp3Stream(f_stream, 65536); MemoryStream convertedAudioStream = new MemoryStream(); byte[] buffer = new byte[65536]; int bytesReturned = -1; int totalBytesReturned = 0; while (bytesReturned != 0) { bytesReturned = stream.Read(buffer, 0, buffer.Length); convertedAudioStream.Write(buffer, 0, bytesReturned); totalBytesReturned += bytesReturned; this.progress = (int)(((float)stream.Position / (float)stream.Length) * 100); this.progress = Mathf.Clamp(this.progress, 0, 100); } Debug.Log("MP3 file has " + stream.ChannelCount + " channels with a frequency of " + stream.Frequency); byte[] convertedAudioData = convertedAudioStream.ToArray(); Debug.Log("Converted Data has " + convertedAudioData.Length + " bytes of data"); MP3_data = new float[convertedAudioData.Length / 2]; for (int i = 0; i < MP3_data.Length; i++) { MP3_data[i] = (float)(BitConverter.ToInt16(convertedAudioData, i * 2) / (float)short.MaxValue); } MP3_freq = stream.Frequency; stream.Dispose(); loaded = true; } }
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(); }
/// <summary> /// Sample showing how to read through an MP3 file and obtain its contents as a PCM byte stream. /// </summary> public static void ReadAllTheWayThroughMp3File() { Mp3Stream stream = new Mp3Stream(Mp3FilePath); // Create the buffer int numberOfPcmBytesToReadPerChunk = 512; byte[] buffer = new byte[numberOfPcmBytesToReadPerChunk]; int bytesReturned = -1; int totalBytes = 0; while (bytesReturned != 0) { bytesReturned = stream.Read(buffer, 0, buffer.Length); totalBytes += bytesReturned; } Console.WriteLine("Read a total of " + totalBytes + " bytes."); }
void Load(System.IO.Stream stream) { //if (secondaryBuffer == null) //{ // return; //} //if (secondaryBuffer.Disposed) //{ // return; //} if (Playing) { secondaryBuffer.Stop(); Playing = false; } mp3Stream = new Mp3Stream(stream); mp3Stream.Read(buff, 0, 512); mp3Stream.Position = 0; waveFormat.BitsPerSample = 16; waveFormat.Channels = mp3Stream.ChannelCount; waveFormat.SamplesPerSecond = mp3Stream.Frequency; waveFormat.FormatTag = WaveFormatTag.Pcm; waveFormat.BlockAlign = (short)(waveFormat.Channels * (waveFormat.BitsPerSample / 8)); waveFormat.AverageBytesPerSecond = waveFormat.SamplesPerSecond * waveFormat.BlockAlign; wholeSize = (int)(waveFormat.AverageBytesPerSecond * TimeSpan.FromSeconds(0.2).TotalSeconds); bufferDescription = new BufferDescription(waveFormat); bufferDescription.BufferBytes = wholeSize; bufferDescription.GlobalFocus = true; bufferDescription.ControlVolume = true; secondaryBuffer = new SecondaryBuffer(bufferDescription, game.Devices.DSoundDev); secondaryBuffer.Volume = volum; #region useless code area //autoResetEvent = new System.Threading.AutoResetEvent(false); //notify = new Notify(secondaryBuffer); //System.Reflection.MethodInfo methodInfo; //methodInfo = typeof(SoundManager).GetMethod("fillBack"); //bufferPositionNotify = new BufferPositionNotify[2]; //bufferPositionNotify[0] = new BufferPositionNotify(); //bufferPositionNotify[0].Offset = 0; //bufferPositionNotify[0].EventNotifyHandle = this.GetType().GetMethod("fillBack", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance).MethodHandle.Value; //bufferPositionNotify[1] = new BufferPositionNotify(); //bufferPositionNotify[1].Offset = halfSize; //bufferPositionNotify[1].EventNotifyHandle = this.GetType().GetMethod("fillFore", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance).MethodHandle.Value; //bufferPositionNotify = new BufferPositionNotify[2]; //bufferPositionNotify[0] = new BufferPositionNotify(); //bufferPositionNotify[0].Offset = 0; //bufferPositionNotify[0].EventNotifyHandle = autoResetEvent.Handle; //bufferPositionNotify[1] = new BufferPositionNotify(); //bufferPositionNotify[1].Offset = halfSize; //bufferPositionNotify[1].EventNotifyHandle = autoResetEvent.Handle; //notify.SetNotificationPositions(bufferPositionNotify); #endregion }