public EncodingInfo Read(Stream raf) { //Read the infos-------------------------------------------------------- if (raf.Length == 0) { //Empty File throw new CannotReadException("Error: File empty"); } raf.Seek(0, SeekOrigin.Begin); //FLAC Header string byte[] b = new byte[4]; raf.Read(b, 0, b.Length); string flac = new string(System.Text.Encoding.ASCII.GetChars(b)); if (flac != "fLaC") { throw new CannotReadException("fLaC Header not found"); } MetadataBlockDataStreamInfo mbdsi = null; bool isLastBlock = false; while (!isLastBlock) { b = new byte[4]; raf.Read(b, 0, b.Length); MetadataBlockHeader mbh = new MetadataBlockHeader(b); if (mbh.BlockType == MetadataBlockHeader.BlockTypes.StreamInfo) { b = new byte[mbh.DataLength]; raf.Read(b, 0, b.Length); mbdsi = new MetadataBlockDataStreamInfo(b); if (!mbdsi.Valid) { throw new CannotReadException("FLAC StreamInfo not valid"); } break; } raf.Seek(raf.Position + mbh.DataLength, SeekOrigin.Begin); isLastBlock = mbh.IsLastBlock; mbh = null; //Free memory } EncodingInfo info = new EncodingInfo(); info.Duration = new TimeSpan(mbdsi.Length * TimeSpan.TicksPerSecond); info.ChannelNumber = mbdsi.ChannelNumber; info.SamplingRate = mbdsi.SamplingRate; info.EncodingType = mbdsi.EncodingType; info.ExtraEncodingInfos = ""; info.Bitrate = ComputeBitrate(mbdsi.Length, raf.Length); return(info); }
public OggTag Read(Stream flacStream) { //Begins tag parsing------------------------------------- if (flacStream.Length == 0) { //Empty File throw new CannotReadException("Error: File empty"); } flacStream.Seek(0, SeekOrigin.Begin); //FLAC Header string byte[] b = new byte[4]; flacStream.Read(b, 0, b.Length); string flac = new string(System.Text.Encoding.ASCII.GetChars(b)); if (flac != "fLaC") { throw new CannotReadException("fLaC Header not found, not a flac file"); } OggTag tag = null; //Seems like we hava a valid stream bool isLastBlock = false; while (!isLastBlock) { b = new byte[4]; flacStream.Read(b, 0, b.Length); MetadataBlockHeader mbh = new MetadataBlockHeader(b); switch (mbh.BlockType) { //We got a vorbis comment block, parse it case MetadataBlockHeader.BlockTypes.VorbisComment: tag = HandleVorbisComment(mbh, flacStream); mbh = null; return(tag); //We have it, so no need to go further //This is not a vorbis comment block, we skip to next block default: flacStream.Seek(flacStream.Position + mbh.DataLength, SeekOrigin.Begin); break; } isLastBlock = mbh.IsLastBlock; mbh = null; } //FLAC not found... throw new CannotReadException("FLAC Tag could not be found or read.."); }
public EncodingInfo Read(Stream raf) { //Read the infos-------------------------------------------------------- if (raf.Length == 0) { //Empty File throw new CannotReadException("Error: File empty"); } raf.Seek(0, SeekOrigin.Begin); //FLAC Header string byte[] b = new byte[4]; raf.Read(b, 0, b.Length); string flac = new string(System.Text.Encoding.ASCII.GetChars(b)); if (flac != "fLaC") { throw new CannotReadException("fLaC Header not found"); } MetadataBlockDataStreamInfo mbdsi = null; bool isLastBlock = false; while (!isLastBlock) { b = new byte[4]; raf.Read(b, 0, b.Length); MetadataBlockHeader mbh = new MetadataBlockHeader(b); if (mbh.BlockType == (int) MetadataBlockHeader.BlockTypes.StreamInfo) { b = new byte[mbh.DataLength]; raf.Read(b, 0, b.Length); mbdsi = new MetadataBlockDataStreamInfo(b); if (!mbdsi.Valid) { throw new CannotReadException("FLAC StreamInfo not valid"); } break; } raf.Seek(raf.Position + mbh.DataLength, SeekOrigin.Begin); isLastBlock = mbh.IsLastBlock; mbh = null; //Free memory } EncodingInfo info = new EncodingInfo(); info.Length = mbdsi.Length; info.ChannelNumber = mbdsi.ChannelNumber; info.SamplingRate = mbdsi.SamplingRate; info.EncodingType = mbdsi.EncodingType; info.ExtraEncodingInfos = ""; info.Bitrate = ComputeBitrate(mbdsi.Length, raf.Length); return info; }
private OggTag HandleVorbisComment(MetadataBlockHeader mbh, Stream flacStream) { long oldPos = flacStream.Position; OggTag tag = oggTagReader.Read(flacStream); long newPos = flacStream.Position; if (newPos - oldPos != mbh.DataLength) { throw new CannotReadException("Tag length do not match with flac comment data length"); } return(tag); }
public OggTag Read( Stream flacStream ) { //Begins tag parsing------------------------------------- if ( flacStream.Length==0 ) { //Empty File throw new CannotReadException("Error: File empty"); } flacStream.Seek( 0 , SeekOrigin.Begin); //FLAC Header string byte[] b = new byte[4]; flacStream.Read(b, 0, b.Length); string flac = new string(System.Text.Encoding.ASCII.GetChars(b)); if(flac != "fLaC") throw new CannotReadException("fLaC Header not found, not a flac file"); OggTag tag = null; //Seems like we hava a valid stream bool isLastBlock = false; while(!isLastBlock) { b = new byte[4]; flacStream.Read(b, 0, b.Length); MetadataBlockHeader mbh = new MetadataBlockHeader(b); switch(mbh.BlockType) { //We got a vorbis comment block, parse it case (int)MetadataBlockHeader.BlockTypes.VorbisComment : tag = HandleVorbisComment(mbh, flacStream); mbh = null; return tag; //We have it, so no need to go further //This is not a vorbis comment block, we skip to next block default : flacStream.Seek(flacStream.Position+mbh.DataLength, SeekOrigin.Begin); break; } isLastBlock = mbh.IsLastBlock; mbh = null; } //FLAC not found... throw new CannotReadException("FLAC Tag could not be found or read.."); }
public void Write( Tag tag, Stream raf, Stream rafTemp ) { //Clean up old datas ArrayList metadataBlockPadding = new ArrayList(1); ArrayList metadataBlocks = new ArrayList(); byte[] b = new byte[4]; raf.Read(b, 0, b.Length); if(new string(System.Text.Encoding.ASCII.GetChars(b)) != "fLaC") throw new CannotWriteException("This is not a FLAC file"); bool isLastBlock = false; while(!isLastBlock) { b = new byte[4]; raf.Read(b, 0, 4); MetadataBlockHeader mbh = new MetadataBlockHeader(b); if (mbh.BlockType == MetadataBlockHeader.BlockTypes.Padding) { raf.Seek(mbh.DataLength, SeekOrigin.Current); metadataBlockPadding.Add(mbh.DataLength+4); } else { b = new byte[mbh.DataLength]; raf.Read(b, 0, b.Length); metadataBlocks.Add(new MetadataBlockData(mbh, b)); } isLastBlock = mbh.IsLastBlock; } int availableRoom = ComputeAvailableRoom(metadataBlockPadding, metadataBlocks); int newTagSize = tc.GetTagLength(tag); int neededRoom = newTagSize + ComputeNeededRoom(metadataBlockPadding, metadataBlocks); raf.Seek(0, SeekOrigin.Begin); if(availableRoom>=neededRoom) { //OVERWRITE EXISTING TAG raf.Seek(42, SeekOrigin.Begin); foreach (MetadataBlockData data in metadataBlocks) { raf.Write(data.Header.Data, 0, data.Header.Data.Length); raf.Write(data.Bytes, 0, data.Bytes.Length); } byte[] newTagBytes = tc.Create(tag, availableRoom-neededRoom).Data; raf.Write(newTagBytes, 0, newTagBytes.Length); } else { //create new tag with padding (we remove the header and keep the audio data) b = new byte[42]; raf.Read(b, 0, b.Length); raf.Seek(availableRoom+42, SeekOrigin.Begin); rafTemp.Write(b, 0, b.Length); foreach (MetadataBlockData data in metadataBlocks) { raf.Write(data.Header.Data, 0, data.Header.Data.Length); raf.Write(data.Bytes, 0, data.Bytes.Length); } byte[] newTagBytes = tc.Create(tag, FlacTagCreator.DEFAULT_PADDING).Data; rafTemp.Write(newTagBytes, 0, newTagBytes.Length); b = new byte[65536]; int read = raf.Read(b, 0, b.Length); while (read != 0) { rafTemp.Write(b, 0, read); read = raf.Read(b, 0, b.Length); } //tempFC.transferFrom( fc, tempFC.position(), fc.size() ); } }
private OggTag HandleVorbisComment(MetadataBlockHeader mbh, Stream flacStream) { long oldPos = flacStream.Position; OggTag tag = oggTagReader.Read(flacStream); long newPos = flacStream.Position; if(newPos - oldPos != mbh.DataLength) throw new CannotReadException("Tag length do not match with flac comment data length"); return tag; }
public void Write(Tag tag, Stream raf, Stream rafTemp) { //Clean up old datas ArrayList metadataBlockPadding = new ArrayList(1); ArrayList metadataBlocks = new ArrayList(); byte[] b = new byte[4]; raf.Read(b, 0, b.Length); if (new string(System.Text.Encoding.ASCII.GetChars(b)) != "fLaC") { throw new CannotWriteException("This is not a FLAC file"); } bool isLastBlock = false; while (!isLastBlock) { b = new byte[4]; raf.Read(b, 0, 4); MetadataBlockHeader mbh = new MetadataBlockHeader(b); if (mbh.BlockType == MetadataBlockHeader.BlockTypes.Padding) { raf.Seek(mbh.DataLength, SeekOrigin.Current); metadataBlockPadding.Add(mbh.DataLength + 4); } else { b = new byte[mbh.DataLength]; raf.Read(b, 0, b.Length); metadataBlocks.Add(new MetadataBlockData(mbh, b)); } isLastBlock = mbh.IsLastBlock; } int availableRoom = ComputeAvailableRoom(metadataBlockPadding, metadataBlocks); int newTagSize = tc.GetTagLength(tag); int neededRoom = newTagSize + ComputeNeededRoom(metadataBlockPadding, metadataBlocks); raf.Seek(0, SeekOrigin.Begin); if (availableRoom >= neededRoom) { //OVERWRITE EXISTING TAG raf.Seek(42, SeekOrigin.Begin); foreach (MetadataBlockData data in metadataBlocks) { raf.Write(data.Header.Data, 0, data.Header.Data.Length); raf.Write(data.Bytes, 0, data.Bytes.Length); } byte[] newTagBytes = tc.Create(tag, availableRoom - neededRoom).Data; raf.Write(newTagBytes, 0, newTagBytes.Length); } else { //create new tag with padding (we remove the header and keep the audio data) b = new byte[42]; raf.Read(b, 0, b.Length); raf.Seek(availableRoom + 42, SeekOrigin.Begin); rafTemp.Write(b, 0, b.Length); foreach (MetadataBlockData data in metadataBlocks) { raf.Write(data.Header.Data, 0, data.Header.Data.Length); raf.Write(data.Bytes, 0, data.Bytes.Length); } byte[] newTagBytes = tc.Create(tag, FlacTagCreator.DEFAULT_PADDING).Data; rafTemp.Write(newTagBytes, 0, newTagBytes.Length); b = new byte[65536]; int read = raf.Read(b, 0, b.Length); while (read != 0) { rafTemp.Write(b, 0, read); read = raf.Read(b, 0, b.Length); } //tempFC.transferFrom( fc, tempFC.position(), fc.size() ); } }