-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathTimecodeExtensions.cs
82 lines (76 loc) · 3.23 KB
/
TimecodeExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// Realtime SDK for Qualisys Track Manager. Copyright 2015-2018 Qualisys AB
//
using QTMRealTimeSDK.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace QTMRealTimeSDK.Data
{
public static class TimecodeExtensions
{
public static bool IsIRIG(this Timecode timecode) { return timecode.Type == TimecodeType.IRIG; }
public static bool IsSMPTE(this Timecode timecode) { return timecode.Type == TimecodeType.SMPTE; }
public static bool IsCameraTime(this Timecode timecode) { return timecode.Type == TimecodeType.CameraTime; }
/// <summary>
/// Get timecode converted to IRIG timestamp
/// </summary>
public static void GetAsIRIG(this Timecode timecode, out IRIGTimecode irig)
{
irig = new IRIGTimecode();
irig.Year = 0x7f & timecode.High;
irig.Day = 0x1FF & (timecode.High >> 7);
irig.Hour = 0x1f & timecode.Low;
irig.Minute = 0x3F & (timecode.Low >> 5);
irig.Second = 0x3F & (timecode.Low >> 11);
irig.Tenth = 0xF & (timecode.Low >> 17);
}
/// <summary>
/// Get timecode converted to SMPTE timestamp
/// </summary>
public static void GetAsSMPTE(this Timecode timecode, out SMPTETimecode smpte)
{
smpte = new SMPTETimecode();
smpte.Hour = 0x1f & timecode.Low;
smpte.Minute = 0x3F & (timecode.Low >> 5);
smpte.Second = 0x3F & (timecode.Low >> 11);
smpte.Frame = 0x1F & (timecode.Low >> 17);
}
/// <summary>
/// Get timecode as camera timestamp
/// </summary>
public static void GetAsCameraTime(this Timecode timecode, out UInt64 cameratime)
{
cameratime = ((UInt64)(timecode.High) << 32) | timecode.Low;
}
/// <summary>
/// Format timestamp depending on type
/// </summary>
public static string FormatTimestamp(this Timecode timecode)
{
string output = string.Empty;
switch (timecode.Type)
{
case TimecodeType.SMPTE:
SMPTETimecode smpte;
timecode.GetAsSMPTE(out smpte);
output = string.Format("{0:D2}:{1:D2}:{2:D2}:{3:D2}", smpte.Hour, smpte.Minute, smpte.Second, smpte.Frame);
break;
case TimecodeType.IRIG:
IRIGTimecode irig;
timecode.GetAsIRIG(out irig);
output = string.Format("{0:D2}:{1:D3}:{2:D2}:{3:D2}:{4:D2}.{5:D}", irig.Year, irig.Day, irig.Hour, irig.Minute, irig.Second, irig.Tenth);
break;
case TimecodeType.CameraTime:
UInt64 cameraTime;
timecode.GetAsCameraTime(out cameraTime);
const UInt64 cTicksPerSecond = 10000000;
UInt64 cSeconds = (cameraTime / cTicksPerSecond);
UInt64 cNanoseconds = ((cameraTime % cTicksPerSecond) * (1000000000 / cTicksPerSecond));
output = string.Format("{0}.{1:D9}", cSeconds, cNanoseconds);
break;
}
return output;
}
}
}