forked from hughpyle/inguz-DSPUtil
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sample.cs
292 lines (266 loc) · 7.64 KB
/
Sample.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
using System;
using System.Collections.Generic;
using System.Text;
// Copyright (c) 2006 by Hugh Pyle, inguzaudio.com
namespace DSPUtil
{
public interface ISample
{
ushort NumChannels { get; }
double this[int arg] { get; set; }
}
[Serializable]
public struct Sample2 : ISample
{
double _0;
double _1;
public Sample2(double a, double b)
{
_0 = a;
_1 = b;
}
public ushort NumChannels { get { return 2; } }
public double this[int arg]
{
get
{
return (arg == 0) ? _0 : _1;
}
set
{
if (arg == 0) _0 = value; else _1 = value;
}
}
public override bool Equals(object obj)
{
ISample o = obj as ISample;
if (o == null)
{
return false;
}
return o.NumChannels == 2 && o[0] == _0 && o[1] == _1;
}
public override int GetHashCode()
{
return _0.GetHashCode() ^ _1.GetHashCode();
}
}
[Serializable]
public struct Sample : ISample
{
// A multi-channel sample value. Each data point is a double-precision float.
double[] _value;
public Sample(ushort numChannels)
{
_value = new double[numChannels];
}
// Single-channel constructor
public Sample(double val)
{
_value = new double[1];
_value[0] = val;
}
// Copy constructor
public Sample(ISample sample)
{
ushort nc = sample.NumChannels;
_value = new double[nc];
for (int n = 0; n < nc; n++)
{
_value[n] = sample[n];
}
}
public Sample(ISample sample, double gain)
{
ushort nc = sample.NumChannels;
_value = new double[nc];
for (int n = 0; n < nc; n++)
{
_value[n] = sample[n] * gain;
}
}
public ushort NumChannels { get { return (ushort)_value.Length; } }
public double this[int arg]
{
get
{
return _value[arg];
}
set
{
_value[arg] = value;
}
}
public override bool Equals(object obj)
{
ISample o = obj as ISample;
if (o == null)
{
return false;
}
ushort nc = NumChannels;
if (o.NumChannels != nc)
{
return false;
}
for (int n = 0; n < nc; n++)
{
if (o[n] != _value[n])
{
return false;
}
}
return true;
}
public override int GetHashCode()
{
int h = 0;
ushort nc = NumChannels;
for (int n = 0; n < nc; n++)
{
h = h ^ _value[n].GetHashCode();
}
return h;
}
public static Sample operator +(Sample c1, Sample c2)
{
if (c1.NumChannels != c2.NumChannels)
{
throw new ArgumentException("Samples must have the same number of channels");
}
Sample s = new Sample(c1.NumChannels);
for (int n = 0; n < c1.NumChannels; n++)
{
s[n] = c1[n] + c2[n];
}
return s;
}
public static Sample operator -(Sample c1, Sample c2)
{
if (c1.NumChannels != c2.NumChannels)
{
throw new ArgumentException("Samples must have the same number of channels");
}
Sample s = new Sample(c1.NumChannels);
for (int n = 0; n < c1.NumChannels; n++)
{
s[n] = c1[n] - c2[n];
}
return s;
}
// Multiplication operator (needed for convolution)
public static Sample operator *(Sample c1, Sample c2)
{
ushort nc = c1.NumChannels;
if (nc != c2.NumChannels)
{
throw new ArgumentException("Samples must have the same number of channels");
}
Sample s = new Sample(nc);
for (int n = 0; n < nc; n++)
{
s[n] = c1[n] * c2[n];
}
return s;
}
// Multiply by scalar (gain)
public static Sample operator *(double gain, Sample c)
{
ushort nc = c.NumChannels;
Sample s = new Sample( nc );
for (int n = 0; n < nc; n++)
{
s[n] = c[n] * gain;
}
return s;
}
}
/*
[Serializable]
public struct ComplexSample : ISample<Complex>
{
// A multi-channel sample value. Each data point is a double-precision complex number.
Complex[] _value;
public ComplexSample(ushort numChannels)
{
_value = new Complex[numChannels];
}
// Copy constructor
public ComplexSample(Sample sample)
{
ushort nc = sample.NumChannels;
_value = new Complex[nc];
for (int n = 0; n < nc; n++)
{
_value[n] = new Complex(sample[n],0);
}
}
public ushort NumChannels { get { return (ushort)_value.Length; } }
public Complex[] Value
{
get { return _value; }
}
public Complex this[int arg]
{
get
{
return _value[arg];
}
set
{
_value[arg] = value;
}
}
public static ComplexSample operator +(ComplexSample c1, ComplexSample c2)
{
if (c1.NumChannels != c2.NumChannels)
{
throw new ArgumentException("Samples must have the same number of channels");
}
ComplexSample s = new ComplexSample(c1.NumChannels);
for (int n = 0; n < c1.NumChannels; n++)
{
s[n] = c1[n] + c2[n];
}
return s;
}
public static ComplexSample operator -(ComplexSample c1, ComplexSample c2)
{
if (c1.NumChannels != c2.NumChannels)
{
throw new ArgumentException("Samples must have the same number of channels");
}
ComplexSample s = new ComplexSample(c1.NumChannels);
for (int n = 0; n < c1.NumChannels; n++)
{
s[n] = c1[n] - c2[n];
}
return s;
}
// Multiplication operator (needed for convolution)
public static ComplexSample operator *(ComplexSample c1, ComplexSample c2)
{
if (c1.NumChannels != c2.NumChannels)
{
throw new ArgumentException("Samples must have the same number of channels");
}
ComplexSample s = new ComplexSample(c1.NumChannels);
for (int n = 0; n < c1.NumChannels; n++)
{
s[n] = c1[n] * c2[n];
}
return s;
}
// Multiply by scalar (gain)
public static ComplexSample operator *(double gain, ComplexSample c)
{
ComplexSample s = new ComplexSample(c.NumChannels);
for (int n = 0; n < c.NumChannels; n++)
{
s[n] = c[n] * gain;
}
return s;
}
}
*/
}