9using System.Threading.Tasks;
18 #region --Client API--
32 this.sampleRate = sampleRate;
33 this.channelCount = channelCount;
37 var header =
new byte[44];
38 stream.Write(header, 0, header.Length);
44 public void CommitFrame<T>(T[] pixelBuffer =
default,
long timestamp =
default) where T :
struct
51 public void CommitFrame(IntPtr nativeBuffer =
default,
long timestamp =
default)
60 public void CommitSamples(
float[] sampleBuffer,
long timestamp =
default)
63 var shortBuffer =
new short[sampleBuffer.Length];
64 var byteBuffer =
new byte[Buffer.ByteLength(shortBuffer)];
65 for (var i = 0; i < sampleBuffer.Length; i++)
66 shortBuffer[i] = (
short) (sampleBuffer[i] *
short.MaxValue);
68 Buffer.BlockCopy(shortBuffer, 0, byteBuffer, 0, byteBuffer.Length);
69 stream.Write(byteBuffer, 0, byteBuffer.Length);
70 sampleCount += sampleBuffer.Length;
79 stream.Seek(0, SeekOrigin.Begin);
80 stream.Write(Encoding.UTF8.GetBytes(
"RIFF"), 0, 4);
81 stream.Write(BitConverter.GetBytes(stream.Length - 8), 0, 4);
82 stream.Write(Encoding.UTF8.GetBytes(
"WAVE"), 0, 4);
83 stream.Write(Encoding.UTF8.GetBytes(
"fmt "), 0, 4);
84 stream.Write(BitConverter.GetBytes(16), 0, 4);
85 stream.Write(BitConverter.GetBytes((ushort) 1), 0, 2);
86 stream.Write(BitConverter.GetBytes(channelCount), 0, 2);
87 stream.Write(BitConverter.GetBytes(sampleRate), 0, 4);
88 stream.Write(BitConverter.GetBytes(sampleRate * channelCount *
sizeof(
short)), 0,
90 stream.Write(BitConverter.GetBytes((ushort) (channelCount * 2)), 0, 2);
91 stream.Write(BitConverter.GetBytes((ushort) 16), 0, 2);
92 stream.Write(Encoding.UTF8.GetBytes(
"data"), 0, 4);
93 stream.Write(BitConverter.GetBytes(sampleCount *
sizeof(ushort)), 0, 4);
96 return Task.FromResult(stream.Name);
102 #region --Operations--
104 private readonly
int sampleRate, channelCount;
105 private readonly FileStream stream;
106 private int sampleCount;
System.IO.FileMode FileMode
using Leap.Unity.Interaction. Internal
void CommitFrame(IntPtr nativeBuffer=default, long timestamp=default)
This recorder does not support committing pixel buffers.
void CommitSamples(float[] sampleBuffer, long timestamp=default)
Commit an audio sample buffer for encoding.
WAVRecorder(int sampleRate, int channelCount)
Create an WAV recorder.
void CommitFrame< T >(T[] pixelBuffer=default, long timestamp=default)
This recorder does not support committing pixel buffers.
Task< string > FinishWriting()
Finish writing and return the path to the recorded media file.