Tanoda
WAVRecorder.cs
Go to the documentation of this file.
1/*
2* NatCorder
3* Copyright (c) 2020 Yusuf Olokoba.
4*/
5
6using System;
7using System.IO;
8using System.Text;
9using System.Threading.Tasks;
10
11namespace NatSuite.Recorders
12{
16 public sealed class WAVRecorder : IMediaRecorder
17 {
18 #region --Client API--
19
23 public (int width, int height) frameSize => default;
24
30 public WAVRecorder(int sampleRate, int channelCount)
31 {
32 this.sampleRate = sampleRate;
33 this.channelCount = channelCount;
34 stream = new FileStream(Internal.Utility.GetPath(@".wav"), FileMode.Create);
35 sampleCount = 0;
36 // Pre-allocate WAVE header
37 var header = new byte[44];
38 stream.Write(header, 0, header.Length);
39 }
40
44 public void CommitFrame<T>(T[] pixelBuffer = default, long timestamp = default) where T : struct
45 {
46 }
47
51 public void CommitFrame(IntPtr nativeBuffer = default, long timestamp = default)
52 {
53 }
54
60 public void CommitSamples(float[] sampleBuffer, long timestamp = default)
61 {
62 // Convert to short array
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);
67 // Write to output stream
68 Buffer.BlockCopy(shortBuffer, 0, byteBuffer, 0, byteBuffer.Length);
69 stream.Write(byteBuffer, 0, byteBuffer.Length);
70 sampleCount += sampleBuffer.Length;
71 }
72
76 public Task<string> FinishWriting()
77 {
78 // Write header
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); // Channel count
87 stream.Write(BitConverter.GetBytes(sampleRate), 0, 4); // Sample rate
88 stream.Write(BitConverter.GetBytes(sampleRate * channelCount * sizeof(short)), 0,
89 4); // Output rate in bytes
90 stream.Write(BitConverter.GetBytes((ushort) (channelCount * 2)), 0, 2); // Block alignment
91 stream.Write(BitConverter.GetBytes((ushort) 16), 0, 2); // Bits per sample
92 stream.Write(Encoding.UTF8.GetBytes("data"), 0, 4);
93 stream.Write(BitConverter.GetBytes(sampleCount * sizeof(ushort)), 0, 4); // Total sample count
94 // Close stream and return
95 stream.Dispose();
96 return Task.FromResult(stream.Name);
97 }
98
99 #endregion
100
101
102 #region --Operations--
103
104 private readonly int sampleRate, channelCount;
105 private readonly FileStream stream;
106 private int sampleCount;
107
108 #endregion
109 }
110}
System.IO.FileMode FileMode
Definition: AssetLoader.cs:14
Waveform audio recorder.
Definition: WAVRecorder.cs:17
void CommitFrame(IntPtr nativeBuffer=default, long timestamp=default)
This recorder does not support committing pixel buffers.
Definition: WAVRecorder.cs:51
void CommitSamples(float[] sampleBuffer, long timestamp=default)
Commit an audio sample buffer for encoding.
Definition: WAVRecorder.cs:60
WAVRecorder(int sampleRate, int channelCount)
Create an WAV recorder.
Definition: WAVRecorder.cs:30
void CommitFrame< T >(T[] pixelBuffer=default, long timestamp=default)
This recorder does not support committing pixel buffers.
Definition: WAVRecorder.cs:44
Task< string > FinishWriting()
Finish writing and return the path to the recorded media file.
Definition: WAVRecorder.cs:76
A recorder capable of recording video frames, and optionally audio frames, to a media output....