Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
myd7349 committed Jul 30, 2024
1 parent ea34c2f commit aa10e70
Show file tree
Hide file tree
Showing 14 changed files with 552 additions and 38 deletions.
2 changes: 1 addition & 1 deletion Examples/LSLVer/LSLVer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="SharpLSL.Native.all" Version="1.16.2" />
<PackageReference Include="SharpLSL.Native.all" Version="1.16.2.1" />
</ItemGroup>

<ItemGroup>
Expand Down
7 changes: 3 additions & 4 deletions Examples/LSLVer/Program.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
// Port of https://github.com/sccn/liblsl/blob/master/testing/lslver.c
using SharpLSL;
using SharpLSL.Interop;

namespace LSLVer
{
internal class Program
{
static void Main(string[] args)
{
Console.WriteLine($"LSL version: {LSL.lsl_library_version()}");
Console.WriteLine(Lsl.GetLibraryInfo());
Console.WriteLine(Lsl.GetLocalClock());
Console.WriteLine($"LSL version: {LSL.GetLibraryVersion()}");
Console.WriteLine(LSL.GetLibraryInfo());
Console.WriteLine(LSL.GetLocalClock());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace SharpLSL
{
public enum LslChannelFormat : int
public enum ChannelFormat : int
{
Float = lsl_channel_format_t.cft_float32,
Double = lsl_channel_format_t.cft_double64,
Expand Down
275 changes: 275 additions & 0 deletions Source/SharpLSL/Interop/Outlet.PInvoke.cs

Large diffs are not rendered by default.

21 changes: 21 additions & 0 deletions Source/SharpLSL/LostException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System;

namespace SharpLSL
{
public class LostException : LSLException
{
public LostException()
{
}

public LostException(string message)
: base(message)
{
}

public LostException(string message, Exception innerException)
: base(message, innerException)
{
}
}
}
54 changes: 45 additions & 9 deletions Source/SharpLSL/Lsl.cs
Original file line number Diff line number Diff line change
@@ -1,30 +1,66 @@
using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

using SharpLSL.Interop;
using static SharpLSL.Interop.LSL;

namespace SharpLSL
{
public static class Lsl
public static class LSL
{
public const double IrregularRate = LSL_IRREGULAR_RATE;

public const double DeducedTimestamp = LSL_DEDUCED_TIMESTAMP;

public const double Forever = LSL_FOREVER;

public const int NoPreference = LSL_NO_PREFERENCE;

public static string GetLastError()
{
var ptr = LSL.lsl_last_error();
return Marshal.PtrToStringAnsi(ptr);
var ptr = lsl_last_error();
return Marshal.PtrToStringAnsi(ptr); // TODO: Encoding
}

public static int GetProtocolVersion() => LSL.lsl_protocol_version();
public static int GetProtocolVersion() => lsl_protocol_version();

public static int GetLibraryVersion() => LSL.lsl_library_version();
public static int GetLibraryVersion() => lsl_library_version();

public static string GetLibraryInfo()
{
var ptr = LSL.lsl_library_info();
return Marshal.PtrToStringAnsi(ptr);
var ptr = lsl_library_info();
return Marshal.PtrToStringAnsi(ptr); // TODO: Encoding
}

public static double GetLocalClock() => LSL.lsl_local_clock();
public static double GetLocalClock() => lsl_local_clock();

public static void DestroyString(IntPtr str) => LSL.lsl_destroy_string(str);
// TODO: Remove
//public static void DestroyString(IntPtr str) => LSL.lsl_destroy_string(str);

#if !NET35
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
internal static void CheckError(int errorCode)
{
if (errorCode < 0)
{
switch ((lsl_error_code_t)errorCode)
{
case lsl_error_code_t.lsl_no_error:
break;
case lsl_error_code_t.lsl_timeout_error:
throw new TimeoutException("The operation failed due to a timeout.");
case lsl_error_code_t.lsl_lost_error:
throw new LostException("The stream has been lost.");
case lsl_error_code_t.lsl_argument_error:
throw new ArgumentException("An argument was incorrectly specified.");
case lsl_error_code_t.lsl_internal_error:
throw new LSLException("An internal error has occurred.");
default:
throw new LSLException("An unknown error has occurred.");
}
}
}
}
}
10 changes: 5 additions & 5 deletions Source/SharpLSL/LslObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@

namespace SharpLSL
{
public abstract class LslObject : SafeHandle
public abstract class LSLObject : SafeHandle
{
protected LslObject(IntPtr lslHandle, bool ownsHandle = true)
protected LSLObject(IntPtr handle, bool ownsHandle = true)
: base(IntPtr.Zero, ownsHandle)
{
if (lslHandle == IntPtr.Zero)
throw new LSLException($"Failed to create {GetType().Name} object: {Lsl.GetLastError()}.");
if (handle == IntPtr.Zero)
throw new LSLException($"Failed to create {GetType().Name} object: {LSL.GetLastError()}.");

SetHandle(lslHandle);
SetHandle(handle);
}

public override bool IsInvalid => handle != IntPtr.Zero;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace SharpLSL
{
[Flags]
public enum LslProcessingOptions
public enum ProcessingOptions
{
None = lsl_processing_options_t.proc_none,
ClockSync = lsl_processing_options_t.proc_clocksync,
Expand Down
4 changes: 4 additions & 0 deletions Source/SharpLSL/SharpLSL.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,8 @@
<None Include="../../README.md" Pack="true" PackagePath="/" />
</ItemGroup>

<ItemGroup>
<Folder Include="Helpers\" />
</ItemGroup>

</Project>
54 changes: 41 additions & 13 deletions Source/SharpLSL/LslStreamInfo.cs → Source/SharpLSL/StreamInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@
using System.Runtime.InteropServices;

using SharpLSL.Interop;
using static SharpLSL.Interop.LSL;

namespace SharpLSL
{
public class LslStreamInfo : LslObject
public class StreamInfo : LSLObject
{
public LslStreamInfo(
public StreamInfo(
string name,
string type,
int channelCount = 1,
double nominalSrate = LSL.LSL_IRREGULAR_RATE,
LslChannelFormat channelFormat = LslChannelFormat.Float,
double nominalSrate = LSL.IrregularRate,
ChannelFormat channelFormat = ChannelFormat.Float,
string sourceId = "")
: this(CreateStreamInfo(
name,
Expand All @@ -24,36 +25,63 @@ public LslStreamInfo(
{
}

public LslStreamInfo(IntPtr handle, bool ownsHandle = true)
public StreamInfo(IntPtr handle, bool ownsHandle = true)
: base(handle, ownsHandle)
{
}

public string Name => Marshal.PtrToStringAnsi(lsl_get_name(handle)); // TODO: Encoding

public string Type => Marshal.PtrToStringAnsi(lsl_get_type(handle)); // TODO: Encoding

public int ChannelCount => lsl_get_channel_count(handle);

public double NominalSrate => lsl_get_nominal_srate(handle);

public ChannelFormat ChannelFormat => (ChannelFormat)lsl_get_channel_format(handle);

public string SourceId => Marshal.PtrToStringAnsi(lsl_get_source_id(handle)); // TODO: Encoding

public int Version => lsl_get_version(handle);

public double CreatedAt => lsl_get_created_at(handle);

public string Uid => Marshal.PtrToStringAnsi(lsl_get_uid(handle));

public string SessionId => Marshal.PtrToStringAnsi(lsl_get_session_id(handle)); // TODO: Encoding

public string HostName => Marshal.PtrToStringAnsi(lsl_get_hostname(handle)); // TODO: Encoding

public StreamInfo Copy()
{
return new StreamInfo(lsl_copy_streaminfo(handle), true);
}

protected override bool ReleaseHandle()
{
LSL.lsl_destroy_streaminfo(handle);
lsl_destroy_streaminfo(handle);
return true;
}

private static IntPtr CreateStreamInfo(
string name,
string type,
int channelCount,
double nominalSrate,
LslChannelFormat channelFormat,
ChannelFormat channelFormat,
string sourceId)
{
var namePtr = IntPtr.Zero;
var typePtr = IntPtr.Zero;
var sourceIdPtr = IntPtr.Zero;

try
{
namePtr = Marshal.StringToHGlobalAnsi(name);
typePtr = Marshal.StringToHGlobalAnsi(type);
sourceIdPtr = Marshal.StringToHGlobalAnsi(sourceId);
namePtr = Marshal.StringToHGlobalAnsi(name); // TODO: Encoding
typePtr = Marshal.StringToHGlobalAnsi(type); // TODO: Encoding
sourceIdPtr = Marshal.StringToHGlobalAnsi(sourceId); // TODO: Encoding

return LSL.lsl_create_streaminfo(
return lsl_create_streaminfo(
namePtr,
typePtr,
channelCount,
Expand Down
9 changes: 9 additions & 0 deletions Source/SharpLSL/StreamInlet.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using SharpLSL.Interop;
using static SharpLSL.Interop.LSL;

namespace SharpLSL
{
public class StreamInlet
{
}
}
Loading

0 comments on commit aa10e70

Please sign in to comment.