public override int Read(byte[] buffer, int offset, int count) { int bytesRead = SourceStream.Read(buffer, offset, count); ProcessData(buffer, offset, bytesRead); return(bytesRead); }
public override int Read(byte[] buffer, int offset, int count) { int read = SourceStream.Read(buffer, offset, count); bool applyEffects = false; if (Effects.Count % WaveFormat.Channels == 0) { applyEffects = true; } for (int i = 0; i < read / 4; i++) { float sample = BitConverter.ToSingle(buffer, i * 4); if (applyEffects == true) { for (int j = channel; j < Effects.Count; j += WaveFormat.Channels) { sample = Effects[j].ApplyEffect(sample); } channel = (channel + 1) % WaveFormat.Channels; } byte[] bytes = BitConverter.GetBytes(sample); bytes.CopyTo(buffer, i * 4); } return(read); }
public int Read(float[] buffer, int offset, int count) { #region sample setup int read = SourceStream.Read(buffer, offset, count); for (int i = 0; i < read; i++) { float sample = buffer[i]; #endregion #region FX processing samples.Enqueue(sample); sample = sample + EchoFactor * samples.Dequeue(); //clip range sample = Math.Min(1f, Math.Max(-1f, sample)); #endregion #region sample copy and exit buffer[i] = sample; } return(read); #endregion }
public override DataBlock Next() { lock (SourceStream) { var buf = new byte[Constants.HeaderSize]; var bytesRead = SourceStream.Read(buf, 0, buf.Length); if (bytesRead == 0) { return(new DataBlock(PartNumber++, new byte[0])); } if (buf[0] != Constants.HeaderByte1 || buf[1] != Constants.HeaderByte2 || buf[2] != Constants.CompressionMethodDeflate) { throw new InvalidDataException("Archive is not valid or it was not created by this program."); } var blockSize = BitConverter.ToInt32(buf, sizeof(int)); buf = new byte[blockSize]; SourceStream.Position -= Constants.HeaderSize; var offset = 0; do { bytesRead = SourceStream.Read(buf, offset, buf.Length - offset); offset += bytesRead; }while (offset != blockSize && bytesRead != 0); return(new DataBlock(PartNumber++, buf)); } }
public int Read(int sectorIndex, byte[] buffer, int position, int offset, int count) { var idIndex = IdIndexList[sectorIndex]; var sectorPosition = SectorSize + idIndex * SectorSize; var streamPosition = sectorPosition + position; SourceStream.Seek(streamPosition, SeekOrigin.Begin); return(SourceStream.Read(buffer, offset, count)); }
/// <summary> /// Reads a set number of bytes from the underlying stream, /// updating progress along the way. /// </summary> /// <param name="buffer"></param> /// <param name="offset"></param> /// <param name="count"></param> /// <returns></returns> public override int Read(byte[] buffer, int offset, int count) { if (buffer is null) { throw new ArgumentNullException(nameof(buffer)); } var read = SourceStream.Read(buffer, offset, count); outStream.Write(buffer, offset, read); return(read); }
/// <summary> /// Reads 4 floating-point bytes at a time, outputting <see cref="AbstractPcmConversionStream.BytesPerSample"/> /// PCM bytes per sample read. /// </summary> /// <param name="buffer"></param> /// <param name="offset"></param> /// <param name="count"></param> /// <returns></returns> protected override int InternalRead(byte[] buffer, int offset, int count) { var read = 0; while (read < count && SourceStream.Position < SourceStream.Length) { SourceStream.Read(tempBuffer, 0, sizeof(float)); FloatToPCM(tempBuffer, 0, buffer, offset + read); read += BytesPerSample; } return(read); }
/// <summary> /// Reads <see cref="AbstractPcmConversionStream.BytesPerSample"/> PCM bytes /// at a time, outputting 4 floating-point bytes per sample read. /// </summary> /// <param name="buffer"></param> /// <param name="offset"></param> /// <param name="count"></param> /// <returns></returns> protected override int InternalRead(byte[] buffer, int offset, int count) { var read = 0; while (read + sizeof(float) - 1 < count) { _ = SourceStream.Read(tempBuffer, 0, BytesPerSample); PCMToFloat(tempBuffer, 0, buffer, offset + read); read += sizeof(float); } return(read); }
public List <TLVObject> ReadTags() { var res = new List <TLVObject>(); while (true) { var tag = ReadNextTag(); if (tag == null) { break; } var length = ReadLength(); if (length == 0) { continue; } if (length == null) { throw new Exception("Indefinite length not supported"); } var buf = new byte[length.Value]; var readIndex = 0; while (true) { var lengthRead = SourceStream.Read(buf, readIndex, length.Value - readIndex); if (lengthRead == 0) { throw new Exception("Unexpected end of stream while reading data"); } readIndex += lengthRead; if (length == readIndex) { break; // all data read } } res.Add(new TLVObject(new TLVTag(tag.Value), buf) { IsEMV = RemoveEMVPadding }); } return(res); }
public override bool ReadPart(FilePart part) { part.Source = new byte[_strategy.PartSize]; var count = SourceStream.Read(part.Source, 0, part.Source.Length); part.SourceSize = count; _totalReadByte = _totalReadByte + count; // прочитали всё - у части выставляем признак, что она последняя if (_totalReadByte != SourceFileSize) { return(true); } part.IsLast = true; Logger.Add($"Поток {Thread.CurrentThread.Name} прочитал последнюю часть файла {part} "); return(true); }
public override int Read(byte[] buffer, int offset, int count) { int read = SourceStream.Read(buffer, offset, count); for (int i = 0; i < read / 4; i++) { float sample = BitConverter.ToSingle(buffer, i * 4); sample *= 0.5f; byte[] bytes = BitConverter.GetBytes(sample); buffer[i * 4 + 0] = bytes[0]; buffer[i * 4 + 1] = bytes[1]; buffer[i * 4 + 2] = bytes[2]; buffer[i * 4 + 3] = bytes[3]; } return(read); }
public override DataBlock Next() { lock (SourceStream) { var dataBlock = new DataBlock(PartNumber++, new byte[_blockSize]); int bytesRead, offset = 0; do { bytesRead = SourceStream.Read(dataBlock.Data, offset, dataBlock.Size - offset); offset += bytesRead; }while (offset != dataBlock.Size && bytesRead != 0); dataBlock.Resize(offset); return(dataBlock); } }
public override int Read(byte[] buffer, int offset, int count) { int bytesRead = SourceStream.Read(buffer, offset, count); if (ActiveDSPEffect.Enabled) { if (SourceStream.WaveFormat.Encoding == WaveFormatEncoding.IeeeFloat) { ByteAndFloatsConverter convertInputBuffer = new ByteAndFloatsConverter { Bytes = buffer }; ProcessDataIeeeFloat(convertInputBuffer, offset, bytesRead); } else { // Do not process other types of streams } } return(bytesRead); }
public int Read(float[] buffer, int offset, int count) { #region sample setup int read = SourceStream.Read(buffer, offset, count); for (int i = 0; i < read; i++) { float sample = buffer[i]; channel = (channel + 1) % WaveFormat.Channels; #endregion #region FX processing sample = c1 * sample + c2 * samples_t_minus_1[channel] + c3 * samples_t_minus_2[channel]; samples.Enqueue(sample); sample = sample * (1f - (EchoFactor / 2f)) + EchoFactor * samples.Dequeue(); //STRANGERANDOM hack //float del = samples.Dequeue(); //sample = sample + (sample % del) / 4 - del * 2; //clip range sample = Math.Min(1f, Math.Max(-1f, sample)); samples_t_minus_2[channel] = samples_t_minus_1[channel]; samples_t_minus_1[channel] = sample; #endregion #region sample copy and exit buffer[i] = sample; } return(read); #endregion }
public override int Read(byte[] buffer, int offset, int count) { int read = SourceStream.Read(buffer, offset, count); for (int i = 0; i < read / 4; i++) { float sample = BitConverter.ToSingle(buffer, i * 4); if (Effects.Count == WaveFormat.Channels) { foreach (Effect e in Effects[channel]) { sample = e.ApplyEffect(sample); } channel = (channel + 1) % WaveFormat.Channels; } byte[] bytes = BitConverter.GetBytes(sample); buffer[i * 4 + 0] = bytes[0]; buffer[i * 4 + 1] = bytes[1]; buffer[i * 4 + 2] = bytes[2]; buffer[i * 4 + 3] = bytes[3]; } return(read); }
/// <summary> /// Читает аудиотрек. /// </summary> public override int Read(byte[] buffer, int offset, int count) { Console.WriteLine("DirectSoundOut requested {0} bytes", count); int read = SourceStream.Read(buffer, offset, count); for (int i = 0; i < read / 4; i++) { float sample = BitConverter.ToSingle(buffer, i * 4); if (Effects.Count == WaveFormat.Channels) { sample = Effects[channel].ApplyEffect(sample); channel = (channel + 1) % WaveFormat.Channels; } byte[] bytes = BitConverter.GetBytes(sample); buffer[i * 4 + 0] = bytes[0]; buffer[i * 4 + 1] = bytes[1]; buffer[i * 4 + 2] = bytes[2]; buffer[i * 4 + 3] = bytes[3]; } return(read); }
public override int Read(byte[] buffer, int offset, int count) { var read = SourceStream.Read(buffer, offset, count); for (var i = 0; i < read / 4; i++) { var sample = BitConverter.ToSingle(buffer, i * 4); if (Effects.Count == WaveFormat.Channels) { sample = Effects[_channel].ApplyEffect(sample); _channel = (_channel + 1) % WaveFormat.Channels; } var bytes = BitConverter.GetBytes(sample); //bytes.CopyTo(buffer, i * 4); buffer[i * 4 + 0] = bytes[0]; buffer[i * 4 + 1] = bytes[1]; buffer[i * 4 + 2] = bytes[2]; buffer[i * 4 + 3] = bytes[3]; } return(read); }
/// <summary> /// Reads from the underlying stream /// </summary> public override int Read(byte[] buffer, int offset, int count) { return(SourceStream.Read(buffer, offset, count)); }
public void Synchronize(string remoteFile, string seedFile, string outputFile) { this.remoteFile = remoteFile; this.seedFile = seedFile; this.outputFile = outputFile; // Initialize our managed RDC wrapper RdcServices rdcServices = new RdcServices(); SignatureCollection seedSignatures = null; // Get the RDC version of the server so we // can make sure this client is supported. Client.RdcProxy.RdcService rdcWebService = new Client.RdcProxy.RdcService(); Client.RdcProxy.RdcVersion rdcVersion = rdcWebService.GetRdcVersion(); //rdcServices.CheckVersion(rdcVersion); // Open the local seed file stream using (FileStream stream = File.OpenRead(seedFile)) { rdcServices.WorkingDirectory = workingDir; rdcServices.RecursionDepth = recursionDepth; // Generate the signature files seedSignatures = rdcServices.GenerateSignatures(stream, Path.GetFileName(seedFile)); if (seedSignatures.Count < 1) { throw new RdcException("Failed to generate the signatures."); } } // Now make the call the the Rdc web service // and retrieve the copy of the signature // manifest. Client.RdcProxy.SignatureManifest signatureManifest = rdcWebService.GetSignatureManifest(remoteFile, recursionDepth); SignatureCollection sourceSignatures = new SignatureCollection(); /* * Realistically, the soure file length should be checked * against a predetermined minimum limit. So if the source * file length is less than 1MB, just download the source * file instead of generating signaures and needs. */ //if signatureManifest.FileLength < MINIMUM_SIZE) // DownloadSourceFile(...); ulong TargetDataWritten = 0; ulong TotalSourceData = 0; ulong TotalSeedData = 0; ulong TotalSigData = 0; // Now that we have the signature manifiest, let's go ahead // transfer local copies of the signature files to our // working signature directory. int Depth = 0; foreach (Client.RdcProxy.SignatureInfo sig in signatureManifest.Signatures) { Console.WriteLine(string.Format("\n----------\nProcessing: {0}\n", sig.Name + ".sig")); if (sig.Length > 0) { GC.WaitForPendingFinalizers(); // Create the signature stream Microsoft.RDC.SignatureInfo sigInfo = new Microsoft.RDC.SignatureInfo(sig.Name, -1, workingDir, true); sourceSignatures.Add(sigInfo); // hang on to them to keep them alive and for clean up if (Depth == 0) // always transfer the complete first remote signature { Console.WriteLine(string.Format("Transfering: {0}\n", sig.Name + ".sig")); for (int i = 0; i < sig.Length; i += blockSize) { int readBytes = Math.Min((int)(sig.Length - i), blockSize); byte[] data = rdcWebService.TransferDataBlock(Path.Combine(sig.Path, sig.Name) + ".sig", i, readBytes); sigInfo.InnerStream.Write(data, 0, data.Length); TotalSigData += (ulong)data.Length; } } // select source and target stream FileStream SourceStream; FileStream TargetStream; string RemoteSourcePath; // if there are other signatures after this one, they become the source and target if (Depth < seedSignatures.Count - 1) { SourceStream = seedSignatures[Depth + 1].InnerStream; TargetStream = File.Create(Path.Combine(workingDir, signatureManifest.Signatures[Depth + 1].Name) + ".sig", blockSize); RemoteSourcePath = Path.Combine(signatureManifest.Signatures[Depth + 1].Path, signatureManifest.Signatures[Depth + 1].Name) + ".sig"; Console.WriteLine(string.Format("Creating: {0}\n----------\n\n", signatureManifest.Signatures[Depth + 1].Name + ".sig")); } else // create the final target file { SourceStream = File.OpenRead(seedFile); TargetStream = File.Create(outputFile, blockSize); RemoteSourcePath = remoteFile; Console.WriteLine(string.Format("Creating: {0}\n----------\n\n", Path.GetFileName(outputFile))); } // reset signature streams for reading seedSignatures[Depth].InnerStream.Position = 0; sigInfo.InnerStream.Position = 0; // Compare the signatures and get // the needs array that we will use to create the // target output file. ArrayList needsList = rdcServices.CreateNeedsList(seedSignatures[Depth], sigInfo); foreach (RdcNeed need in needsList) { switch (need.blockType) { case RdcNeedType.Source: // Copy this block from the remote server. TotalSourceData += need.blockLength; byte[] data = rdcWebService.TransferDataBlock( RemoteSourcePath, (int)need.fileOffset, (int)need.blockLength); TargetStream.Write(data, 0, (int)need.blockLength); break; case RdcNeedType.Seed: TotalSeedData += need.blockLength; byte[] seedData = new Byte[need.blockLength]; SourceStream.Seek((int)need.fileOffset, SeekOrigin.Begin); SourceStream.Read(seedData, 0, (int)need.blockLength); TargetStream.Write(seedData, 0, (int)need.blockLength); break; default: break; } Console.WriteLine(string.Format("NEED: length:{0,12}\toffset:{1,12}\tsource:{2,12}\tblock type:{3,12}", need.blockLength, need.fileOffset, TargetDataWritten, need.blockType.ToString())); TargetDataWritten += need.blockLength; } // Close our IO file streams. if (Depth == seedSignatures.Count - 1) { SourceStream.Close(); // only non-signature sources } TargetStream.Close(); } Depth++; } Console.WriteLine(string.Format("\nFrom source:{0,12:N0}\tFrom seed:{1,12:N0}\tTotal:{2,12:N0}", TotalSourceData, TotalSeedData, TotalSourceData + TotalSeedData)); Console.WriteLine(string.Format("\nTransfer: {0:N0} bytes from source, file size: {1:N0}, RDC Savings: {2:0.00}%\n", TotalSourceData + TotalSigData, TotalSourceData + TotalSeedData, (1.0 - (double)(TotalSourceData + TotalSigData) / (double)(TotalSourceData + TotalSeedData)) * 100.0)); // release all signature resources rdcServices.PurgeSignatureStore(seedSignatures); rdcServices.PurgeSignatureStore(sourceSignatures); rdcWebService.Finialize(signatureManifest); rdcServices.Dispose(); }
public override int Read(byte[] buffer, int offset, int count) { Console.WriteLine("DirectSoundOut requested {0} bytes", count); return(SourceStream.Read(buffer, offset, count)); }
public override bool ReadPart(FilePart part) { // одна часть архива - она полностью пойдет на декомпрессию var archivePart = new ArchivePart(); ArhivePortion arhivePortion = null; while (archivePart != null) { if (_portionFromPrev != null) { // используем порцию оставщуюся с предыдущей части arhivePortion = _portionFromPrev; _portionFromPrev = null; } else { // читаем порцию из файла var buffer = new byte[BufferSize]; var count = SourceStream.Read(buffer, 0, buffer.Length); if (count > 0) { _totalReadByte = _totalReadByte + count; if (arhivePortion == null) { arhivePortion = new ArhivePortion(new BytesBuffer(buffer, 0, count - 1)); } else { // предыдущая порция закончилась на части заголовка arhivePortion.Append(new BytesBuffer(buffer, 0, count - 1)); } } else { // всё в part part.Source = archivePart.ToArray(); part.IsLast = true; Logger.Add($"Поток {Thread.CurrentThread.Name} прочитал последнюю часть файла {part} "); return(true); } } // нашли заголовок if (arhivePortion.IsExistsTitle) { // нашли заголовок полностью if (arhivePortion.IsExistsAllTitle) { // заголовок в начале if (arhivePortion.StartsWithTitle) { // часть архива пустая - нашли заголовок для текущей части if (archivePart.IsEmpty) { // записываем в часть все что до следующего заголовка archivePart.AppendTitleAndDataBeforeNextTitle(arhivePortion); if (arhivePortion.IsNotEmpty) { // если остаток порции не пустой, то возможно начался новый заголовок // в остатке есть заголовок полностью if (arhivePortion.IsExistsAllTitle) { // всё в part part.Source = archivePart.ToArray(); // а остаток нужно "припасти" для следующей part _portionFromPrev = arhivePortion; return(true); } // в остатке есть заголовок, но не полностью // !!! окончить текущую часть не имеем права - не удостоверившись, что это заголовок // поэтому здесь ничего не делаем // ... и далее на чтение следующей порции } else { arhivePortion = null; // ... и дальше на чтение следующей порции } } else { // нашли заголовок для следующей части - значит текущая часть сформирована, а все что осталось в порции уже для следующей части // порцию нужно "припасти" для следующей part _portionFromPrev = arhivePortion; // всё в part part.Source = archivePart.ToArray(); return(true); } } else { // заголовок не в начале порции if (archivePart.IsEmpty) { throw new FormatException( $"part {part}. archivePart ещё пустая, а в порции заголовок не в начале - неверный формат архива"); } // добавляем в часть всё что до заголовка archivePart.AppendDataBeforeTitle(arhivePortion); // всё в part part.Source = archivePart.ToArray(); // порцию нужно "припасти" для следующей part _portionFromPrev = arhivePortion; return(true); } } else { // нашли заголовок не полностью // !!! окончить текущую часть не имеем права - не удостоверившись, что это заголовок // поэтому здесь ничего не делаем // ... и далее на чтение следующей порции } } else { // заголовок не нашли в порции if (archivePart.IsEmpty) { throw new FormatException("Часть ещё пустая, а в порции нет заголовка - неверный формат архива"); } // всю прочитанную порцию архива в часть archivePart.AppendAllPortion(arhivePortion); Debug.Assert(arhivePortion.IsEmpty, "Всё извлекли из порции, а она всё равно не пустая"); arhivePortion = null; // ... и далее на чтение следующей порции } } throw new Exception("Обнаружена неправильная работа ArсhivePartReader"); }