-
Notifications
You must be signed in to change notification settings - Fork 4
/
test_tone_gen.cs
364 lines (305 loc) · 10.1 KB
/
test_tone_gen.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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NAudio.Wave;
// Test Tone Function generator
// Uses NAudio for Audio Device Access - see http://naudio.codeplex.com/
//
// Supported Waveforms are
// Sine
// Triangle
// SawTooth
// Rectangular (50/50 ratio)
// white noise, full bandwidth
//
// Amplidtude is in linear unit FS - so should be <= 1.0 all the time
namespace audio_test
{
public enum func_form
{
sine,
tri,
saw,
rect,
noise
}
public enum mod_type
{
off,
AM,
FM,
PWM
}
public class func_gen_config
{
public bool on; // Active?
public double amp; // Amplitude (1 = FullScale)
public double freq; // in Hz
public func_form func; // Function
public double pulsewidth; // Pulse width for rectangular wave (0..1, 0.5 = 50/50 Pulsing)
// Frequency Sweep
public bool fsweep; // Run a sweep
public double freqend; // Sweep upper frequency in Hz
public bool logsweep; // sweep is logarithmic
public double sweeptime; // Sweeptime in s
public bool updown; // Triangular (up-down-up-down...) instead of saw (up-up-up)
// Modulation
public mod_type modulate;
public func_form modfunc;
public double modfreq;
public double modamp;
public func_gen_config()
{
on = true;
amp = 1.0;
freq = 1000.0;
func = func_form.sine;
pulsewidth = 0.5;
fsweep = false;
freqend = 5000.0;
logsweep = false;
sweeptime = 5.0;
updown = false;
modulate = mod_type.off;
modfunc = func_form.sine;
modfreq = 10.0;
modamp = 0.5;
}
}
public abstract class WaveProvider16 : IWaveProvider
{
private WaveFormat waveFormat;
public WaveProvider16()
: this(44100, 1)
{
}
public WaveProvider16(int sampleRate, int channels)
{
SetWaveFormat(sampleRate, channels);
}
public void SetWaveFormat(int sr, int ch)
{
waveFormat = new WaveFormat(sr, 16, ch);
}
public int Read(byte[] buffer, int offset, int count)
{
WaveBuffer waveBuffer = new WaveBuffer(buffer);
int samplesRequired = count / 2;
int samplesRead = Read(waveBuffer.ShortBuffer, offset / 2, samplesRequired);
return samplesRead * 2;
}
public abstract int Read(short[] buffer, int offset, int sampleCount);
public WaveFormat WaveFormat
{
get { return waveFormat; }
}
}
public class func_gen : WaveProvider16
{
public volatile func_gen_config setup;
/*
public double amp;
public double freq;
public func_form func;
public bool fsweep;
public bool logsweep;
public double sweeptime;
public bool updown;
public double freqend;
*/
private double sweepstate;
private bool sweepup;
private double phi;
private double mphi;
private Random rg;
public func_gen(func_gen_config fgc)
{
/*
freq = 1000;
amp = 1.0;
func = func_form.sine;
*/
setup = fgc;
sweepstate = 0.0;
sweepup = true;
phi = 0;
mphi = 0.0;
rg = new Random();
}
double SINE(double phi)
{
return Math.Sin(phi*2*Math.PI);
}
double TRI(double phi)
{
return (phi < 0.5) ? (-1.0 + 4.0 * phi) : (3.0 - 4.0 * phi);
}
double SAW(double phi)
{
return -1.0 + 2.0 * phi;
}
double RECT(double phi,double rmod)
{
return (phi < rmod) ? -1.0 : 1.0;
}
public override int Read(short[] buffer, int offset, int sampleCount)
{
if (!setup.on)
{
for (int n = 0; n < sampleCount; n++)
{
buffer[n + offset] = (short)0;
}
return sampleCount;
}
double dphi;
double amp;
double pulsew;
dphi = setup.freq / 48000.0;
amp=setup.amp;
pulsew = setup.pulsewidth;
int iamp = (int) Math.Floor(2.0 * 32767 * amp+0.5);
int jamp = (int) Math.Floor(32767 * amp + 0.5);
for (int n = 0; n < sampleCount; n++)
{
double modval = 0.0;
if (setup.modulate != mod_type.off)
{
mphi += setup.modfreq/48000.0;
if ((mphi < 0.0) || (mphi > 1.0)) mphi -= Math.Floor(mphi);
switch (setup.modfunc) {
default:
case func_form.sine: modval = setup.modamp * SINE(mphi);break;
case func_form.tri: modval = setup.modamp * TRI(mphi);break;
case func_form.saw: modval = setup.modamp * SAW(mphi);break;
case func_form.rect: modval = setup.modamp * RECT(mphi,0.5);break;
}
}
if (setup.modulate == mod_type.AM)
amp = setup.amp*(modval + 1.0);
if (setup.modulate == mod_type.PWM)
pulsew = setup.pulsewidth + modval / 2.0;
switch (setup.func)
{
default:
case func_form.sine: buffer[n + offset] = (short)Math.Floor(amp * 32767.0 * SINE(phi)); break;
case func_form.tri: buffer[n + offset] = (short)Math.Floor(amp * 32767.0 * TRI(phi)); break;
case func_form.saw: buffer[n + offset] = (short)Math.Floor(amp * 32767.0 * SAW(phi)); break;
case func_form.rect: buffer[n + offset] = (short)Math.Floor(amp * 32767.0 * RECT(phi, pulsew)); break;
case func_form.noise: buffer[n + offset] = (short)(rg.Next(iamp) - jamp); break;
}
double f = setup.freq;
if (setup.fsweep)
{
double fmod;
if (!setup.updown || (setup.updown && sweepup))
{
// Upwards
sweepstate += 1.0 / 48000.0 / setup.sweeptime;
if (sweepstate > 1.0)
{
if (setup.updown)
sweepup = false;
else
sweepstate -= Math.Floor(sweepstate);
}
}
else
{
// Downwards
sweepstate -= 1.0 / 48000.0 / setup.sweeptime;
if (sweepstate < 0.0)
{
if (setup.updown)
sweepup = true;
else
sweepstate -= Math.Floor(sweepstate);
}
}
if (setup.logsweep)
fmod = ((Math.Exp(sweepstate) -1.0) / (Math.E - 1.0));
else
fmod = sweepstate;
f = (setup.freq * (1.0 - fmod) + fmod * setup.freqend);
}
if (setup.modulate == mod_type.FM)
f*=(1.0+modval);
dphi = f / 48000.0;
phi += dphi;
if (phi < 0.0)
phi += Math.Floor(phi);
if (phi > 1.0)
phi -= Math.Floor(phi);
}
return sampleCount;
}
}
class test_tone_gen
{
private WaveOut wo;
public List<String> devices;
public int overloaded = 0;
public volatile bool killthread = false;
public int usedev;
public System.Threading.Thread workthread;
public func_gen_config setup;
/*
public double freq;
public double amp;
public func_form func;
public bool on;
*/
log_delegate log;
public test_tone_gen(log_delegate dgt)
{
log = dgt;
setup = new func_gen_config();
int wid = WaveOut.DeviceCount;
devices = new List<string>();
for (int i = 0; i < wid; i++)
{
WaveOutCapabilities devinf = WaveOut.GetCapabilities(i);
devices.Add(String.Format("{0}: {1} channels", devinf.ProductName, devinf.Channels));
}
/*
freq = 1000;
amp = 1;
on = true;
func = func_form.sine;
*/
}
public void start(int dev)
{
usedev = dev;
workthread = new System.Threading.Thread(new System.Threading.ThreadStart(play_thread));
workthread.Start();
}
private void play_thread()
{
log("Record Thread Started");
func_gen fg = new func_gen(setup);
fg.SetWaveFormat(48000,1);
wo = new WaveOut(WaveCallbackInfo.FunctionCallback());
wo.DeviceNumber = usedev;
wo.Init(fg);
wo.Play();
log("Playing started");
while (!killthread)
{
System.Threading.Thread.Sleep(50);
/*fg.freq = freq;
fg.amp = amp;
fg.func = func;
fg.on = on;
*/
}
wo.Stop();
log("Playing stopped");
}
public void stop()
{
killthread = true;
}
}
}