-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
GatedAudioRecorder.cs
103 lines (92 loc) · 4.01 KB
/
GatedAudioRecorder.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
namespace SoundDeck.Core.Capture
{
using System;
using System.Threading.Tasks;
using NAudio.Wave;
/// <summary>
/// Provides a gated audio recorder, whereby recording only starts after <see cref="GateThreshold"/> has been met.
/// </summary>
[Obsolete("Requires further testing", true)]
public class GatedAudioRecorder : AudioRecorder
{
/// <summary>
/// Initializes a new instance of the <see cref="GatedAudioRecorder"/> class.
/// </summary>
/// <param name="deviceId">The device.</param>
private GatedAudioRecorder(IAudioDevice device)
: base(device, null)
{
}
/// <summary>
/// Gets or sets the gate threshold; the gate remains open until the recording is written.
/// </summary>
public int GateThreshold { get; set; } = -50;
/// <summary>
/// Gets or sets a value indicating whether this instance's gate open.
/// </summary>
private bool IsGateOpen { get; set; } = false;
/// <summary>
/// Releases unmanaged and - optionally - managed resources.
/// </summary>
/// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
this.IsGateOpen = false;
}
/// <summary>
/// Handles the <see cref="WasapiCapture.DataAvailable"/> event of the <see cref="Capture"/>
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="WaveInEventArgs"/> instance containing the event data.</param>
protected override void Capture_DataAvailable(object sender, WaveInEventArgs e)
{
if (this.TryRead(e, out var buffer, out var length))
{
Task.WaitAll(this.FileWriter.WriteAsync(buffer, 0, length));
}
}
/// <summary>
/// Attempts to start listening to the buffer supplied by <paramref name="e"/> based on the amplitude vs <see cref="GateThreshold"/>.
/// </summary>
/// <param name="e">The <see cref="WaveInEventArgs"/> instance containing the event data.</param>
/// <param name="buffer">The resulting buffer.</param>
/// <param name="length">The resulting buffer length.</param>
/// <returns><c>true</c> when the read was read, otherwise <c>false</c>.</returns>
private bool TryRead(WaveInEventArgs e, out byte[] buffer, out int length)
{
length = e.BytesRecorded;
buffer = e.Buffer;
// gate open, return the supplied buffer
if (this.IsGateOpen)
{
return true;
}
// determine if any of the samples trigger the gate
for (var offset = 0; offset < e.BytesRecorded; offset += 4)
{
var sample = BitConverter.ToSingle(e.Buffer, offset);
if (this.CanOpenGate(sample))
{
// set the resulting buffer from the offset, and open the gate for future captures
length = e.BytesRecorded - offset;
buffer = new byte[length];
Buffer.BlockCopy(e.Buffer, offset, buffer, 0, buffer.Length);
this.IsGateOpen = true;
return true;
}
}
return false;
}
/// <summary>
/// Determines whether this instances gate can open based on the supplied amplitude vs <see cref="GateThreshold"/>.
/// </summary>
/// <param name="amplitude">The amplitude.</param>
/// <returns><c>true</c> when the gate can open; otherwise <c>false</c></returns>
private bool CanOpenGate(float amplitude)
{
var dB = 20 * Math.Log10(Math.Abs(amplitude));
return dB > this.GateThreshold;
}
}
}