private void btnConvert_Click(object sender, EventArgs e) { if (newFormat == IntPtr.Zero) { MessageBox.Show("Please, specify destination format for converting"); return; } string fileName = tbFile2.Text + ".wav"; int size = ar.Milliseconds2Bytes(1000); int len = ar.GetLengthInBytes(); AcmConverter ac = new AcmConverter(oldFormat, newFormat, false); FileStream fs = new FileStream(fileName, FileMode.Create); WaveWriter ww = new WaveWriter(fs, AudioCompressionManager.FormatBytes(newFormat)); pbConvert.Maximum = len; int y = 0; while (y < len) { pbConvert.Value = y; byte[] data = ar.ReadDataInBytes(y, size); if (data.Length == 0) { break; } y += data.Length; byte[] newData = ac.Convert(data); ww.WriteData(newData); } ww.Close(); ar.Close(); gbConvert.Enabled = false; btnMakeMp3.Enabled = tbFile2.Text.ToLower().EndsWith(".wav"); OpenContainingFolder(fileName); }
public int Read(byte[] buffer, int offset, int count) { if (IsEOF) { return(0); } try { int read = 0; lock (_lockObject) { read += CheckForOverflows(buffer, ref offset, count); if (read == count) { return(read); //if we've already read enough -> exit } while (read < count) { try { _frame = ReadNextMP3Frame(); if (_frame == null) { IsEOF = true; break; } int converted = _converter.Convert(_frameBuffer, _frame.FrameLength, _pcmDstBuffer, 0); int BTCC = Math.Min(count - read, converted); Array.Copy(_pcmDstBuffer, 0, buffer, offset, BTCC); offset += BTCC; read += BTCC; /* * If there are any overflows -> store them in a * buffer to use it next time. */ _overflows = ((converted > BTCC) ? (converted - BTCC) : 0); _bufferoffset = ((converted > BTCC) ? (BTCC) : 0); } catch (Exception ex) { Debugger.Break(); Debug.WriteLine("Mp3Stream::Read: " + ex.ToString()); } } _position += read; return(read); } } catch (EndOfStreamException) { return(-1); } }
private void BufferProc(object o) { if (_stream == null || _stream.CanRead == false) { throw new Exception("Mp3WebStream not initialized"); } MP3Frame frame = GetNextFrame(_stream); int channels = frame.ChannelMode == MP3ChannelMode.Stereo ? 2 : 1; AcmConverter converter = new AcmConverter(new MP3Format(frame.SampleRate, frame.ChannelCount, frame.FrameLength, frame.BitRate)); _waveFormat = converter.DestinationFormat; byte[] buffer = new byte[16384 * 4]; _buffer = new FixedSizeBuffer <byte>(converter.DestinationFormat.BytesPerSecond * 10); ManualResetEvent resetEvent = o as ManualResetEvent; resetEvent.Set(); do { if (_buffer.Buffered >= _buffer.Length * 0.85 && !_disposing) { Thread.Sleep(250); continue; } try { frame = GetNextFrame(_stream); //_frameBuffer is set in GetNextFrame int count = converter.Convert(_frameBuffer, frame.FrameLength, buffer, 0); if (count > 0) { int written = _buffer.Write(buffer, 0, count); } } catch (MmException) { _disposing = true; ThreadPool.QueueUserWorkItem((c) => { while (_bufferThread.ThreadState != System.Threading.ThreadState.Stopped) { ; } CreateStream(false); }); } catch (WebException) { InitializeConnection(); } catch (IOException) { InitializeConnection(); } } while (!_disposing); if (converter != null) { converter.Dispose(); } }