Пример #1
0
        protected virtual Task <TransformResult> Transform(TransformSource source)
        {
            int length = (int)source.Memory.Length;

            var memory = source.Alloc(length);

            source.Memory.CopyTo(memory.Span);
            source.Consumed = source.Memory.End;// GetPosition(length);

            var transformed = Transform(memory.Span);

            var transformResult = new TransformResult()
            {
                Result  = transformed,
                Written = transformed.Length,
            };

            ////using (var fs = System.IO.File.Open("sample-aaa.wav", System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write))
            ////{
            ////    fs.Seek(0, System.IO.SeekOrigin.End);
            ////    fs.Write(buffer, 0, length);
            ////}

            ////var transformResult = new TransformResult()
            ////{
            ////    Result = buffer.AsMemory(),
            ////    Written = length,
            ////};


            return(Task.FromResult(transformResult));
        }
Пример #2
0
        protected override Task <TransformResult> Transform(TransformSource source)
        {
            int length = (int)source.Memory.Length;

            var buffer = source.Alloc(length);

            source.Consumed = source.Memory.GetPosition(length);

            var start = 0;

            foreach (var segment in source.Memory)
            {
                // write the log (append mode)
                using (var fs = File.Open(Filename, FileMode.OpenOrCreate, FileAccess.Write))
                {
                    fs.Seek(0, SeekOrigin.End);
                    fs.Write(segment.Span);
                }

                segment.CopyTo(buffer.Slice(start));
                start += segment.Length;
            }

            var transformResult = new TransformResult()
            {
                Result  = buffer.Slice(0, length),
                Written = length,
            };

            return(Task.FromResult(transformResult));
        }
Пример #3
0
        protected override Task <TransformResult> Transform(TransformSource source)
        {
            var length = (int)source.Memory.Length;

            source.Consumed = source.Memory.End;

            var memory     = source.Alloc(length * 4);
            var spanFloats = MemoryMarshal.Cast <byte, float>(memory.Span);

            // Instead of just copying the byte array to floats ...
            //Utilities.CopyToFloat(source.Memory, spanFloats, length);


            // ... we constrain the value of the float array to the interval [-1, +1)
            int offset = 0;

            foreach (var segment in source.Memory)
            {
                for (int i = 0; i < segment.Length; i++)
                {
                    if (offset >= length)  // target.Length
                    {
                        break;
                    }

                    var input = segment.Span;

                    //spanFloats[offset] = input[i];

                    b_min = Math.Min(b_min, input[i]);
                    b_max = Math.Max(b_max, input[i]);

                    // TODO: this version is handling only 8 bit per sample
                    //temp[offset] = input[i] / 32768f;
                    //temp[offset] = input[i] / 256f;
                    spanFloats[offset] = (input[i] - 128) / 128.0f;

                    b2_min = Math.Min(b2_min, spanFloats[offset]);
                    b2_max = Math.Max(b2_max, spanFloats[offset]);

                    offset++;
                }
            }

            // finally we return the byte[] representation of our floats
            var transformResult = new TransformResult()
            {
                Result  = memory,
                Written = memory.Length,
            };

            return(Task.FromResult(transformResult));
        }
Пример #4
0
        protected override Task <TransformResult> Transform(TransformSource source)
        {
            var length = (int)source.Memory.Length;

            source.Consumed = source.Memory.End;
            var output  = source.Alloc(length / 4);
            var outSpan = output.Span;

            int   offset     = 0;
            float dataOffset = 1.4f;//(b2_max - b2_min) / 2;

            foreach (var segment in source.Memory)
            {
                var dataSource = MemoryMarshal.Cast <byte, float>(segment.Span);
                for (int i = 0; i < dataSource.Length; i++)
                {
                    if (offset >= length)  // target.Length
                    {
                        break;
                    }

                    var input = segment.Span;

                    a_min = Math.Min(a_min, dataSource[i]);
                    a_max = Math.Max(a_max, dataSource[i]);

                    //outSpan[offset] = (byte)((dataSource[i] - 0.5f) * 8192);
                    //outSpan[offset] = (byte)((dataSource[i]) * 150.0f);
                    //outSpan[offset] = (byte)((dataSource[i] + 1.0f) * 12/8.0f);
                    outSpan[offset] = (byte)((dataSource[i] + dataOffset) * (256.0f / (2 * dataOffset)));


                    a2_min = Math.Min(a2_min, outSpan[offset]);
                    a2_max = Math.Max(a2_max, outSpan[offset]);

                    offset++;
                }
            }


            // end
            var transformResult = new TransformResult()
            {
                Result  = output,
                Written = output.Length,
            };

            return(Task.FromResult(transformResult));
        }