-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
274762b
commit cc67158
Showing
4 changed files
with
176 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace Pico4SAFTExtTrackingModule; | ||
|
||
public interface DataExtractor<T> | ||
{ | ||
unsafe void Clone(T *obj, T *ret); | ||
unsafe string ToCSV(T *obj, char delimiter); | ||
string GetCSVHeader(char delimiter); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace Pico4SAFTExtTrackingModule; | ||
|
||
public sealed class Logger<T> : IDisposable | ||
{ | ||
private const char CSV_DELIMITER = ';'; | ||
|
||
private Thread thread; | ||
private bool threadEnabled; | ||
|
||
private string filePath; | ||
|
||
private DataExtractor<T> dataExtractor; | ||
private bool waiting; | ||
private T current; | ||
private object waitingCurrentLock = new object(); | ||
|
||
public Logger(string filePath, DataExtractor<T> dataExtractor) | ||
{ | ||
this.dataExtractor = dataExtractor; | ||
this.waiting = true; // request first data | ||
|
||
this.filePath = filePath; | ||
|
||
this.threadEnabled = true; | ||
|
||
this.thread = new Thread(new ThreadStart(ThreadMethod)); | ||
this.thread.Start(); | ||
} | ||
|
||
public unsafe void UpdateValue(T* obj) | ||
{ | ||
if (!this.IsWaiting()) return; // already got something | ||
|
||
fixed (T* ret = &this.current) { | ||
dataExtractor.Clone(obj, ret); | ||
} | ||
lock (this.waitingCurrentLock) | ||
{ | ||
this.waiting = false; // got the new data | ||
} | ||
} | ||
|
||
protected unsafe void ThreadMethod() | ||
{ | ||
using (StreamWriter writer = new StreamWriter(this.filePath)) | ||
{ | ||
writer.WriteLine(this.dataExtractor.GetCSVHeader(CSV_DELIMITER)); // TODO add timestamp | ||
|
||
while (this.threadEnabled) | ||
{ | ||
if (this.IsWaiting()) continue; // no data; try again later | ||
|
||
fixed (T *curr = &this.current) { | ||
writer.WriteLine(this.dataExtractor.ToCSV(curr, CSV_DELIMITER)); | ||
} | ||
lock (this.waitingCurrentLock) | ||
{ | ||
this.waiting = true; // request a new data | ||
} | ||
} | ||
} | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
this.threadEnabled = false; | ||
this.thread.Join(); | ||
} | ||
|
||
protected bool IsWaiting() | ||
{ | ||
bool waiting; | ||
lock(this.waitingCurrentLock) | ||
{ | ||
waiting = this.waiting; | ||
} | ||
return waiting; | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
PicoStreamingAssistantFTUDP/logger/PicoDataLoggerFactory.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Runtime.InteropServices; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace Pico4SAFTExtTrackingModule; | ||
|
||
public sealed class PicoDataLoggerFactory | ||
{ | ||
protected sealed class PicoDataExtractor : DataExtractor<PxrFTInfo> | ||
{ | ||
public unsafe void Clone(PxrFTInfo *obj, PxrFTInfo* ret) | ||
{ | ||
ret->timestamp = obj->timestamp; | ||
ret->laughingProb = obj->laughingProb; | ||
|
||
// TODO I'm sure there's an equivalent of `memcpy` for C# but I couldn't find it | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
rogermiranda1000
Author
Collaborator
|
||
for (int n = 0; n < Pxr.BLEND_SHAPE_NUMS; n++) ret->blendShapeWeight[n] = obj->blendShapeWeight[n]; | ||
for (int n = 0; n < 10; n++) ret->videoInputValid[n] = obj->videoInputValid[n]; | ||
for (int n = 0; n < 10; n++) ret->emotionProb[n] = obj->emotionProb[n]; | ||
} | ||
|
||
public string GetCSVHeader(char delimiter) | ||
{ | ||
StringBuilder sb = new StringBuilder(); | ||
|
||
foreach (BlendShapeIndex shape in Enum.GetValues(typeof(BlendShapeIndex))) | ||
{ | ||
sb.Append(Enum.GetName(typeof(BlendShapeIndex), shape)); | ||
sb.Append(delimiter); | ||
} | ||
|
||
sb.Length--; // remove the last delimiter | ||
return sb.ToString(); | ||
} | ||
|
||
public unsafe string ToCSV(PxrFTInfo *obj, char delimiter) | ||
{ | ||
StringBuilder sb = new StringBuilder(); | ||
|
||
for (int n = 0; n < Pxr.BLEND_SHAPE_NUMS; n++) | ||
{ | ||
sb.Append(obj->blendShapeWeight[n]); | ||
sb.Append(delimiter); | ||
} | ||
|
||
sb.Length--; // remove the last delimiter | ||
return sb.ToString(); | ||
} | ||
} | ||
|
||
public static unsafe Logger<PxrFTInfo> build(string path) | ||
{ | ||
return new Logger<PxrFTInfo>(path, new PicoDataExtractor()); | ||
} | ||
} |
Buffer.BlockCopy
Buffer.MemoryCopy
Unsafe.CopyBlock
Unsafe.CopyBlockUnaligned