Ejemplo n.º 1
0
 public AudioBuffer(int sizeInBytes, BufferFlags flags = BufferFlags.EndOfStream)
 {
     Flags            = flags;
     AudioBytes       = sizeInBytes;
     AudioDataPointer = MemoryHelpers.AllocateMemory(sizeInBytes);
     _ownsBuffer      = true;
 }
Ejemplo n.º 2
0
 public InterfaceArray(params T[] array)
 {
     if (array != null)
     {
         var length = unchecked ((uint)array.Length);
         if (length != 0)
         {
             _values        = new T[length];
             _nativePointer = MemoryHelpers.AllocateMemory(length * (uint)IntPtr.Size);
             for (var i = 0; i < length; i++)
             {
                 this[i] = array[i];
             }
         }
         else
         {
             _nativePointer = default;
             _values        = null;
         }
     }
     else
     {
         _nativePointer = default;
         _values        = null;
     }
 }
Ejemplo n.º 3
0
 public unsafe AudioBuffer(int sizeInBytes, BufferFlags flags = BufferFlags.EndOfStream)
 {
     Flags            = flags;
     AudioBytes       = sizeInBytes;
     AudioDataPointer = new IntPtr(MemoryHelpers.AllocateMemory((nuint)sizeInBytes));
     _ownsBuffer      = true;
 }
Ejemplo n.º 4
0
        public static unsafe AudioBuffer Create <T>(ReadOnlySpan <T> data, BufferFlags flags = BufferFlags.EndOfStream) where T : struct
        {
            var sizeInBytes = data.Length * Unsafe.SizeOf <T>();
            var dataPtr     = MemoryHelpers.AllocateMemory(data.Length);

            MemoryHelpers.CopyMemory(dataPtr, data);
            return(new AudioBuffer(flags, dataPtr, sizeInBytes, true));
        }
Ejemplo n.º 5
0
        public static unsafe AudioBuffer Create <T>(ReadOnlySpan <T> data, BufferFlags flags = BufferFlags.EndOfStream) where T : unmanaged
        {
            int    sizeInBytes = data.Length * sizeof(T);
            IntPtr dataPtr     = MemoryHelpers.AllocateMemory(data.Length);

            MemoryHelpers.CopyMemory(dataPtr, data);
            return(new AudioBuffer(flags, dataPtr, sizeInBytes, true));
        }
Ejemplo n.º 6
0
    /// <summary>
    /// Initializes a new instance of the <see cref="DataStream"/> class and allocates a new buffer to use as a backing store.
    /// </summary>
    /// <param name="sizeInBytes">The size of the buffer to be allocated, in bytes.</param>
    /// <param name="canRead"><c>true</c> if reading from the buffer should be allowed; otherwise, <c>false</c>.</param>
    /// <param name = "canWrite"><c>true</c> if writing to the buffer should be allowed; otherwise, <c>false</c>.</param>
    public DataStream(int sizeInBytes, bool canRead, bool canWrite)
    {
        Debug.Assert(sizeInBytes > 0);

        _buffer     = (byte *)MemoryHelpers.AllocateMemory((nuint)sizeInBytes);
        _size       = sizeInBytes;
        _ownsBuffer = true;
        CanRead     = canRead;
        CanWrite    = canWrite;
    }
Ejemplo n.º 7
0
        public unsafe AudioBuffer(byte[] data, BufferFlags flags = BufferFlags.EndOfStream)
        {
            Flags            = flags;
            AudioBytes       = data.Length;
            AudioDataPointer = MemoryHelpers.AllocateMemory(data.Length);
            fixed(void *dataPtr = &data[0])
            {
                Unsafe.CopyBlockUnaligned(AudioDataPointer.ToPointer(), dataPtr, (uint)data.Length);
            }

            _ownsBuffer = true;
        }
Ejemplo n.º 8
0
 public InterfaceArray(uint size)
 {
     if (size > 0)
     {
         _values        = new T[size];
         _nativePointer = MemoryHelpers.AllocateMemory(size * (uint)IntPtr.Size);
     }
     else
     {
         _nativePointer = default;
         _values        = null;
     }
 }
Ejemplo n.º 9
0
        public AudioBuffer(byte[] data, BufferFlags flags = BufferFlags.EndOfStream)
        {
            Flags            = flags;
            AudioBytes       = data.Length;
            AudioDataPointer = MemoryHelpers.AllocateMemory(data.Length);
            unsafe
            {
                Unsafe.CopyBlockUnaligned(
                    AudioDataPointer.ToPointer(),
                    Unsafe.AsPointer(ref data[0]),
                    (uint)data.Length);
            }

            _ownsBuffer = true;
        }
Ejemplo n.º 10
0
    /// <summary>
    /// Initializes a new instance of the <see cref="AudioBuffer" /> class.
    /// </summary>
    /// <param name="stream">The stream to get the audio buffer from.</param>
    /// <param name="flags"></param>
    public AudioBuffer(DataStream stream, BufferFlags flags = BufferFlags.EndOfStream)
    {
        int length = (int)stream.Length - (int)stream.Position;

        unsafe
        {
            AudioDataPointer = new(MemoryHelpers.AllocateMemory((nuint)length));
            MemoryHelpers.CopyMemory(AudioDataPointer, stream.PositionPointer, length);
        }

        Flags      = flags;
        AudioBytes = (int)stream.Length;

        _ownsBuffer = true;
    }
Ejemplo n.º 11
0
    internal DataStream(void *buffer, int sizeInBytes, bool canRead, bool canWrite, bool makeCopy)
    {
        Debug.Assert(sizeInBytes > 0);

        if (makeCopy)
        {
            _buffer = (byte *)MemoryHelpers.AllocateMemory((nuint)sizeInBytes);
            Unsafe.CopyBlockUnaligned(_buffer, buffer, (uint)sizeInBytes);
        }
        else
        {
            _buffer = (byte *)buffer;
        }
        _size       = sizeInBytes;
        CanRead     = canRead;
        CanWrite    = canWrite;
        _ownsBuffer = makeCopy;
    }
Ejemplo n.º 12
0
    public void Register <T>() where T : ICallbackable
    {
        InitializeIfNeeded();

        var nativePointer = (void **)MemoryHelpers.AllocateMemory(PointerSize * _size);
        var offset        = 0;
        var pointers      = _pointers;

        for (int i = 0, count = pointers.Count; i < count; ++i)
        {
            var item   = pointers[i];
            var length = item.Length;
            item.CopyTo(new Span <IntPtr>(nativePointer + offset, length));
            offset += length;
        }

        TypeDataStorage.Register <T>(nativePointer);
        pointers.Clear();
        _size = 0;
    }