/// <summary> /// Gets the <see cref="XingHeader"/> of a <see cref="Mp3Frame"/>. If the <paramref name="frame"/> does not has an <see cref="XingHeader"/> the return value will be null. /// </summary> /// <param name="frame"><see cref="Mp3Frame"/> which should get checked whether it contains a <see cref="XingHeader"/>.</param> /// <returns><see cref="XingHeader"/> of the specified <paramref name="frame"/> or null.</returns> public static XingHeader FromFrame(Mp3Frame frame) { XingHeader header = new XingHeader(); int offset = CalcOffset(frame); if (offset == -1) return null; if (CheckForValidXingHeader(frame, offset)) { header._startIndex = offset; offset = offset + 4; } else { return null; } header.HeaderFlags = (XingHeaderFlags)ReadHeaderFlags(frame, offset); offset = offset + 4; if ((header.HeaderFlags & XingHeaderFlags.Frames) != 0) { header._framesOffset = offset; offset += 4; } if ((header.HeaderFlags & XingHeaderFlags.Bytes) != 0) { header._bytesOffset = offset; offset += 4; } if ((header.HeaderFlags & XingHeaderFlags.Toc) != 0) { header._tocOffset = offset; offset += 100; } if ((header.HeaderFlags & XingHeaderFlags.QualityIndicator) != 0) { header._qualityIndicator = ReadHeaderFlags(frame, offset); offset += 4; } header._endIndex = offset; return header; }
private static int CalcOffset(Mp3Frame frame) { int offset = 0; if (frame.MPEGVersion == MpegVersion.Version1) { if (frame.ChannelMode != Mp3ChannelMode.Mono) offset = 32 + 4; else offset = 17 + 4; } else if (frame.MPEGVersion == MpegVersion.Version2) { if (frame.ChannelMode != Mp3ChannelMode.Mono) offset = 17 + 4; else offset = 9 + 4; } else { return -1; } return offset; }
private static int ReadHeaderFlags(Mp3Frame frame, int offset) { byte[] data = null; if (frame.ReadData(ref data, 0) < 4) throw new System.IO.EndOfStreamException(); int i = 0; for (int j = 0; j <= 3; j++) { i = data[offset + j]; i <<= 8; } return i; }
private static bool CheckForValidXingHeader(Mp3Frame frame, int offset) { byte[] data = null; if (frame.ReadData(ref data, 0) < 4) return false; if (data[offset + 0] == 'X' && data[offset + 1] == 'i' && data[offset + 2] == 'n' && data[offset + 3] == 'g') { return true; } else return false; }
/// <summary> /// Creates a new instance of the <see cref="Mp3Frame"/> class based on a <see cref="Stream"/>. /// </summary> /// <param name="stream"><see cref="Stream"/> which provides MP3 data.</param> /// <returns>A new instance of the <see cref="Mp3Frame"/> class based on the specified <paramref name="stream"/>.</returns> public static Mp3Frame FromStream(Stream stream) { Mp3Frame frame = new Mp3Frame(stream); return(frame.FindFrame(stream, true) ? frame : null); }
private int ReadRawDataFromFrame(ref byte[] buffer, out Mp3Frame frame) { bool success = false; frame = null; do { try { frame = GetNextFrame(_stream, ref buffer); success = true; } catch (IOException) { } catch (WebException) { } if (!success) { do { CloseResponse(); InitializeConnection(); if (_stream == null) Thread.Sleep(100); } while (_stream == null); } } while (!success); if (frame == null) { buffer = new byte[0]; return 0; } if (buffer.Length > frame.FrameLength) Array.Clear(buffer, frame.FrameLength, buffer.Length - frame.FrameLength); return frame.FrameLength; }
/// <summary> /// Creates a new instance of the <see cref="Mp3Frame"/> class based on a <see cref="Stream"/>. /// </summary> /// <param name="stream"><see cref="Stream"/> which provides MP3 data.</param> /// <param name="data">Byte array which recieves the content of the <see cref="Mp3Frame"/>.</param> /// <returns>A new instance of the <see cref="Mp3Frame"/> class based on the specified <paramref name="stream"/>.</returns> public static Mp3Frame FromStream(Stream stream, ref byte[] data) { Mp3Frame frame = new Mp3Frame(stream); if (frame.FindFrame(stream, false)) { data = data.CheckBuffer(frame.FrameLength); Array.Copy(frame._headerBuffer, 0, data, 0, 4); int read = stream.Read(data, 4, frame.FrameLength - 4); if (read != frame.FrameLength - 4) return null; return frame; } return null; }
/// <summary> /// Creates a new instance of the <see cref="Mp3Frame"/> class based on a <see cref="Stream"/>. /// </summary> /// <param name="stream"><see cref="Stream"/> which provides MP3 data.</param> /// <returns>A new instance of the <see cref="Mp3Frame"/> class based on the specified <paramref name="stream"/>.</returns> public static Mp3Frame FromStream(Stream stream) { Mp3Frame frame = new Mp3Frame(stream); return frame.FindFrame(stream, stream.CanSeek) ? frame : null; }