Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src.csharp/AlphaTab.Test/TestPlatform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public static async Task SaveFile(string name, Uint8Array data)
var path = Path.Combine(RepositoryRoot.Value, name);
Directory.CreateDirectory(Path.GetDirectoryName(path)!);
await using var fs = new FileStream(path, FileMode.Create);
await fs.WriteAsync(data.Data.Array!, data.Data.Offset, data.Data.Count);
await fs.WriteAsync(data.Buffer.Raw, (int)data.ByteOffset, (int)data.Length);
}

public static Task<IList<string>> ListDirectory(string path)
Expand Down
4 changes: 2 additions & 2 deletions src.csharp/AlphaTab/Core/Dom/TextDecoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ public TextDecoder(string encoding)

public string Decode(ArrayBuffer data)
{
return _encoding.GetString(data.Raw.Array, data.Raw.Offset, data.Raw.Count);
return _encoding.GetString(data.Raw, 0, (int)data.ByteLength);
}
}
}
16 changes: 5 additions & 11 deletions src.csharp/AlphaTab/Core/EcmaScript/ArrayBuffer.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,16 @@
using System;

namespace AlphaTab.Core.EcmaScript;
namespace AlphaTab.Core.EcmaScript;

public class ArrayBuffer
{
public ArraySegment<byte> Raw { get; }
public double ByteLength => Raw.Count;
public byte[] Raw { get; }
public double ByteLength => Raw.Length;

public ArrayBuffer(double size)
{
Raw = new ArraySegment<byte>(new byte[(int) size]);
Raw = new byte[(int) size];
}
public ArrayBuffer(byte[] raw)
{
Raw = new ArraySegment<byte>(raw, 0, raw.Length);
}
public ArrayBuffer(ArraySegment<byte> raw)
{
Raw = raw;
}
}
}
125 changes: 17 additions & 108 deletions src.csharp/AlphaTab/Core/EcmaScript/DataView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,138 +5,47 @@ namespace AlphaTab.Core.EcmaScript;
internal class DataView
{
public ArrayBuffer Buffer { get; }
public double ByteOffset { get; }
public double ByteLength { get; }


public DataView(ArrayBuffer buffer)
{
Buffer = buffer;
}

public double GetUint8(double offset)
public DataView(ArrayBuffer buffer, double byteOffset, double byteLength)
{
return Buffer.Raw.Array![Buffer.Raw.Offset + (int) offset];
Buffer = buffer;
ByteOffset = byteOffset;
ByteLength = byteLength;
}

public void SetUint16(double offset, double value, bool littleEndian)
public double GetInt16(double offset, bool littleEndian)
{
var bytes = BitConverter.GetBytes((ushort) value);
if (littleEndian != BitConverter.IsLittleEndian)
if (littleEndian == BitConverter.IsLittleEndian)
{
System.Array.Reverse(bytes);
return BitConverter.ToInt16(Buffer.Raw, (int)(ByteOffset + offset));
}

System.Buffer.BlockCopy(bytes, 0, Buffer.Raw.Array!, Buffer.Raw.Offset + (int) offset,
bytes.Length);
}

public double GetInt16(double offset, bool littleEndian)
{
var bytes = new byte[sizeof(short)];
System.Buffer.BlockCopy(Buffer.Raw.Array!, Buffer.Raw.Offset + (int) offset, bytes, 0,
System.Buffer.BlockCopy(Buffer.Raw, (int)(ByteOffset + offset), bytes, 0,
bytes.Length);
if (littleEndian != BitConverter.IsLittleEndian)
{
System.Array.Reverse(bytes);
}

System.Array.Reverse(bytes);
return BitConverter.ToInt16(bytes, 0);
}

public void SetInt16(double offset, double value, bool littleEndian)
{
var bytes = BitConverter.GetBytes((short) value);
if (littleEndian != BitConverter.IsLittleEndian)
{
System.Array.Reverse(bytes);
}

System.Buffer.BlockCopy(bytes, 0, Buffer.Raw.Array!, Buffer.Raw.Offset + (int) offset,
bytes.Length);
}

public double GetUint32(double offset, bool littleEndian)
{
var bytes = new byte[sizeof(uint)];
System.Buffer.BlockCopy(Buffer.Raw.Array!, Buffer.Raw.Offset + (int) offset, bytes, 0,
bytes.Length);
if (littleEndian != BitConverter.IsLittleEndian)
{
System.Array.Reverse(bytes);
}

return BitConverter.ToUInt32(bytes, 0);
}

public double GetInt32(double offset, bool littleEndian)
{
var bytes = new byte[sizeof(uint)];
System.Buffer.BlockCopy(Buffer.Raw.Array!, Buffer.Raw.Offset + (int) offset, bytes, 0,
bytes.Length);
if (littleEndian != BitConverter.IsLittleEndian)
{
System.Array.Reverse(bytes);
}

return BitConverter.ToInt32(bytes, 0);
}

public void SetInt32(double offset, double value, bool littleEndian)
{
var bytes = BitConverter.GetBytes((int) value);
if (littleEndian != BitConverter.IsLittleEndian)
{
System.Array.Reverse(bytes);
}

System.Buffer.BlockCopy(bytes, 0, Buffer.Raw.Array!, Buffer.Raw.Offset + (int) offset, bytes
.Length);
}

public double GetUint16(double offset, bool littleEndian)
public double GetFloat32(double offset, bool littleEndian = true)
{
var bytes = new byte[sizeof(ushort)];
System.Buffer.BlockCopy(Buffer.Raw.Array!, Buffer.Raw.Offset + (int) offset, bytes, 0,
bytes.Length);
if (littleEndian != BitConverter.IsLittleEndian)
if (littleEndian == BitConverter.IsLittleEndian)
{
System.Array.Reverse(bytes);
return BitConverter.ToSingle(Buffer.Raw, (int)(ByteOffset + offset));
}

return BitConverter.ToUInt16(bytes, 0);
}

public double GetFloat32(double offset, bool littleEndian = true)
{
var bytes = new byte[sizeof(float)];
System.Buffer.BlockCopy(Buffer.Raw.Array!, Buffer.Raw.Offset + (int) offset, bytes, 0,
System.Buffer.BlockCopy(Buffer.Raw, (int)(ByteOffset + offset), bytes, 0,
bytes.Length);
if (littleEndian != BitConverter.IsLittleEndian)
{
System.Array.Reverse(bytes);
}

System.Array.Reverse(bytes);
return BitConverter.ToSingle(bytes, 0);
}

public void SetUint8(double offset, double value)
{
Buffer.Raw.Array![Buffer.Raw.Offset + (int) offset] = (byte) value;
}

public double GetInt8(double offset)
{
return (sbyte) Buffer.Raw.Array![Buffer.Raw.Offset + (int) offset];
}

public double SetUint32(double offset, double value, bool littleEndian)
{
var bytes = BitConverter.GetBytes((uint)value);
if (littleEndian != BitConverter.IsLittleEndian)
{
System.Array.Reverse(bytes);
}

System.Buffer.BlockCopy(bytes, 0, Buffer.Raw.Array!, Buffer.Raw.Offset + (int) offset, bytes
.Length);
return value;
}
}
54 changes: 6 additions & 48 deletions src.csharp/AlphaTab/Core/EcmaScript/Int16Array.cs
Original file line number Diff line number Diff line change
@@ -1,77 +1,35 @@
using System;
using System.Collections;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;

namespace AlphaTab.Core.EcmaScript;

internal class Int16Array : IEnumerable<short>
{
private readonly short[]? _data;
private readonly ArrayBuffer? _buffer;
private readonly short[] _data;

public double Length => _data?.Length ?? _buffer.ByteLength / 2;
public double Length => _data.Length;

public Int16Array(double size)
{
_data = new short[(int)size];
}

internal Int16Array(ArrayBuffer buffer)
{
_buffer = buffer;
}

public double this[double index]
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
return _data != null
? _data[(int)index]
: GetInt16FromBuffer(index);
}
get => _data[(int)index];
[MethodImpl(MethodImplOptions.AggressiveInlining)]
set
{
if (_data != null)
{
_data[(int)index] = (short)value;
}
else
{
var bytes = BitConverter.GetBytes(value);

Buffer.BlockCopy(bytes,
0, _buffer.Raw.Array!,
(_buffer.Raw.Offset + ((int)index * sizeof(short))),
bytes.Length * sizeof(short)
);
}
}
}

private short GetInt16FromBuffer(double index)
{
return BitConverter.ToInt16(_buffer.Raw.Array!,
_buffer.Raw.Offset + ((int)index * sizeof(short)));
set => _data[(int)index] = (short)value;
}

public IEnumerator<short> GetEnumerator()
{
if (_data == null)
{
return Enumerable.Range(0, (int)Length)
.Select(i => GetInt16FromBuffer(i))
.GetEnumerator();
}

return ((IEnumerable<short>)_data).GetEnumerator();
}

IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
}
Loading