Skip to content

Commit

Permalink
Fix misunderstanding between fractions <-> seconds
Browse files Browse the repository at this point in the history
  • Loading branch information
OptoCloud committed Jul 10, 2024
1 parent e75f30d commit 410a09f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 10 deletions.
16 changes: 15 additions & 1 deletion CoreOSC/TimeTag.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,34 @@ public struct TimeTag

public ulong Tag;

/// <summary>
/// Gets or sets the timestamp from a DateTime. DateTime has an accuracy down to 100 nanoseconds (100'000
/// picoseconds)
/// </summary>
public DateTime Timestamp
{
get => Utils.TimeTagToDateTime(Tag);
set => Tag = Utils.DateTimeToTimeTag(value);
}

/// <summary>
/// Gets or sets the total seconds in the timestamp. the double precision number is multiplied by 2^32
/// giving an accuracy down to about 230 picoseconds ( 1/(2^32) of a second)
/// </summary>
public double Seconds
{
get => Utils.TimeTagToSeconds(Tag);
set => Tag = Utils.SecondsToTimeTag(value);
}

/// <summary>
/// Gets or sets the fraction of a second in the timestamp. the double precision number is multiplied by 2^32
/// giving an accuracy down to about 230 picoseconds ( 1/(2^32) of a second)
/// </summary>
public double Fraction
{
get => Utils.TimeTagToFraction(Tag);
set => Tag = Utils.FractionToTimeTag(value);
set => Tag = Utils.SecondsToTimeTag(value);
}

public TimeTag(ulong value)
Expand Down
26 changes: 17 additions & 9 deletions CoreOSC/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,6 @@ public static DateTime TimeTagToDateTime(ulong val)
return new DateTime(EpochTicks + secondsTicks + fractionTicks);
}

public static double TimeTagToFraction(ulong val)
{
if (val == 1)
return 0.0;

return (double)val / OscTicksPerSecond;
}

public static ulong DateTimeToTimeTag(DateTime value)
{
long ticks = value.Ticks - EpochTicks;
Expand All @@ -44,11 +36,27 @@ public static ulong DateTimeToTimeTag(DateTime value)
return secondTicks | fractionTicks;
}

public static ulong FractionToTimeTag(double value)
public static double TimeTagToSeconds(ulong val)
{
if (val == 1)
return 0.0;

return (double)val / OscTicksPerSecond;
}

public static ulong SecondsToTimeTag(double value)
{
return (ulong)(value * OscTicksPerSecond);
}

public static double TimeTagToFraction(ulong val)
{
if (val == 1)
return 0.0;

return (double)(val & 0xFFFFFFFF) / OscTicksPerSecond;
}

public static int AlignedStringLength(string val)
{
var len = val.Length + (4 - val.Length % 4);
Expand Down

0 comments on commit 410a09f

Please sign in to comment.