/
SpeexPreProcessor.cs
134 lines (122 loc) · 3.11 KB
/
SpeexPreProcessor.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using native = Xiph.LowLevel.SpeexPreProcessorNative;
using Xiph.LowLevel;
namespace Xiph.Easy.Speex
{
public class SpeexPreProcessor : IDisposable
{
internal IntPtr State { get; private set; }
public int SamplesPerFrame { get; private set; }
public int SamplesPerSecond { get; private set; }
public bool? VoiceActive { get; private set; }
private void SetControl(SpeexPreProcessorRequests request, int value)
{
if ((int)request % 2 == 1)
throw new ArgumentException("request is not a setter");
unsafe
{
if (native.speex_preprocess_ctl(State, (int)request, &value) != 0)
throw new InvalidOperationException("speex_preprocess_ctl returned error");
}
}
private int GetControl(SpeexPreProcessorRequests request)
{
if ((int)request % 2 == 0)
throw new ArgumentException("request is not a getter");
unsafe
{
int value = 0;
if (native.speex_preprocess_ctl(State, (int)request, &value) != 0)
throw new InvalidOperationException("speex_preprocess_ctl returned error");
return value;
}
}
public SpeexPreProcessor(int samplesPerSecond, int samplesPerFrame)
{
if (samplesPerFrame <= 0)
throw new ArgumentException("samplesPerFrame must be >0");
if (samplesPerSecond <= 0)
throw new ArgumentException("samplesPerSecond must be >0");
SamplesPerFrame = samplesPerFrame;
SamplesPerSecond = samplesPerSecond;
State = native.speex_preprocess_state_init(samplesPerFrame, samplesPerSecond);
}
void IDisposable.Dispose()
{
Close();
}
public void Close()
{
if (State != IntPtr.Zero)
{
native.speex_preprocess_state_destroy(State);
State = IntPtr.Zero;
}
}
public void Run(Int16[] data, int offset)
{
Update(data, offset, false);
}
public void UpdateEstimate(Int16[] data, int offset)
{
Update(data, offset, true);
}
public void Update(Int16[] data, int offset, bool estimate)
{
if (offset < 0)
throw new ArgumentException("offset<0");
if (offset + SamplesPerFrame > data.Length)
throw new ArgumentException("data too small");
VoiceActive = null;
int result = 0;
unsafe
{
fixed (Int16* dataPtr = &(data[offset]))
{
if (!estimate)
result = native.speex_preprocess_run(State, dataPtr);
else
native.speex_preprocess_estimate_update(State, dataPtr);
}
}
if (VoiceActivityDetection && !estimate)
VoiceActive = result != 0;
}
public bool DeNoise
{
get
{
return GetControl(SpeexPreProcessorRequests.GET_DENOISE) != 0;
}
set
{
SetControl(SpeexPreProcessorRequests.SET_DENOISE, Convert.ToInt32(value));
}
}
public bool VoiceActivityDetection
{
get
{
return GetControl(SpeexPreProcessorRequests.GET_VAD) != 0;
}
set
{
SetControl(SpeexPreProcessorRequests.SET_VAD, Convert.ToInt32(value));
}
}
public bool AutomaticGainControl
{
get
{
return GetControl(SpeexPreProcessorRequests.GET_AGC) != 0;
}
set
{
SetControl(SpeexPreProcessorRequests.SET_AGC, Convert.ToInt32(value));
}
}
}
}