forked from g0orx/wdsp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
channel.c
341 lines (307 loc) · 9.36 KB
/
channel.c
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
/* channel.c
This file is part of a program that implements a Software-Defined Radio.
Copyright (C) 2013 Warren Pratt, NR0V
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
The author can be reached by email at
*/
#include "comm.h"
void start_thread (int channel)
{
#if defined(linux) || defined(__APPLE__)
HANDLE handle = wdsp_beginthread(wdspmain, 0, (void *)channel);
#else
HANDLE handle = (HANDLE) wdsp_beginthread(main, 0, (void *)channel);
#endif
SetThreadPriority(handle, THREAD_PRIORITY_HIGHEST);
}
void pre_main_build (int channel)
{
if (ch[channel].in_rate >= ch[channel].dsp_rate)
ch[channel].dsp_insize = ch[channel].dsp_size * (ch[channel].in_rate / ch[channel].dsp_rate);
else
ch[channel].dsp_insize = ch[channel].dsp_size / (ch[channel].dsp_rate / ch[channel].in_rate);
if (ch[channel].out_rate >= ch[channel].dsp_rate)
ch[channel].dsp_outsize = ch[channel].dsp_size * (ch[channel].out_rate / ch[channel].dsp_rate);
else
ch[channel].dsp_outsize = ch[channel].dsp_size / (ch[channel].dsp_rate / ch[channel].out_rate);
if (ch[channel].in_rate >= ch[channel].out_rate)
ch[channel].out_size = ch[channel].in_size / (ch[channel].in_rate / ch[channel].out_rate);
else
ch[channel].out_size = ch[channel].in_size * (ch[channel].out_rate / ch[channel].in_rate);
InitializeCriticalSectionAndSpinCount ( &ch[channel].csDSP, 2500 );
InitializeCriticalSectionAndSpinCount ( &ch[channel].csEXCH, 2500 );
InterlockedBitTestAndReset (&ch[channel].flushflag, 0);
create_iobuffs (channel);
}
void post_main_build (int channel)
{
InterlockedBitTestAndSet (&ch[channel].run, 0);
start_thread (channel);
if (ch[channel].state == 1)
InterlockedBitTestAndSet (&ch[channel].exchange, 0);
}
void build_channel (int channel)
{
pre_main_build (channel);
create_main (channel);
post_main_build (channel);
}
PORT
void OpenChannel (int channel, int in_size, int dsp_size, int input_samplerate, int dsp_rate, int output_samplerate,
int type, int state, double tdelayup, double tslewup, double tdelaydown, double tslewdown, int bfo)
{
ch[channel].in_size = in_size;
ch[channel].dsp_size = dsp_size;
ch[channel].in_rate = input_samplerate;
ch[channel].dsp_rate = dsp_rate;
ch[channel].out_rate = output_samplerate;
ch[channel].type = type;
ch[channel].state = state;
ch[channel].tdelayup = tdelayup;
ch[channel].tslewup = tslewup;
ch[channel].tdelaydown = tdelaydown;
ch[channel].tslewdown = tslewdown;
ch[channel].bfo = bfo;
InterlockedBitTestAndReset (&ch[channel].exchange, 0);
build_channel (channel);
if (ch[channel].state)
{
InterlockedBitTestAndSet (&ch[channel].iob.pc->slew.upflag, 0);
InterlockedBitTestAndSet (&ch[channel].iob.ch_upslew, 0);
InterlockedBitTestAndReset (&ch[channel].iob.pc->exec_bypass, 0);
InterlockedBitTestAndSet (&ch[channel].exchange, 0);
}
#if !defined(linux) && !defined(__APPLE__)
_MM_SET_FLUSH_ZERO_MODE (_MM_FLUSH_ZERO_ON);
#endif
}
void pre_main_destroy (int channel)
{
IOB a = ch[channel].iob.pc;
InterlockedBitTestAndReset (&ch[channel].exchange, 0);
InterlockedBitTestAndReset (&ch[channel].run, 0);
InterlockedBitTestAndSet (&ch[channel].iob.pc->exec_bypass, 0);
ReleaseSemaphore (a->Sem_BuffReady, 1, 0);
Sleep (25);
}
void post_main_destroy (int channel)
{
destroy_iobuffs (channel);
DeleteCriticalSection ( &ch[channel].csEXCH );
DeleteCriticalSection ( &ch[channel].csDSP );
}
PORT
void CloseChannel (int channel)
{
pre_main_destroy (channel);
destroy_main (channel);
post_main_destroy (channel);
}
void flushChannel (void* p)
{
int channel = (int)p;
EnterCriticalSection (&ch[channel].csDSP);
EnterCriticalSection (&ch[channel].csEXCH);
flush_iobuffs (channel);
InterlockedBitTestAndSet (&ch[channel].iob.pc->exec_bypass, 0);
flush_main (channel);
LeaveCriticalSection (&ch[channel].csEXCH);
LeaveCriticalSection (&ch[channel].csDSP);
InterlockedBitTestAndReset (&ch[channel].flushflag, 0);
_endthread();
}
/********************************************************************************************************
* *
* Channel Properties *
* *
********************************************************************************************************/
PORT
void SetType (int channel, int type)
{ // no need to rebuild buffers; but we did anyway
if (type != ch[channel].type)
{
CloseChannel (channel);
ch[channel].type = type;
build_channel (channel);
}
}
PORT
void SetInputBuffsize (int channel, int in_size)
{ // we do not rebuild main here since it didn't change
if (in_size != ch[channel].in_size)
{
pre_main_destroy (channel);
post_main_destroy (channel);
ch[channel].in_size = in_size;
pre_main_build (channel);
post_main_build (channel);
}
}
PORT
void SetDSPBuffsize (int channel, int dsp_size)
{
if (dsp_size != ch[channel].dsp_size)
{
int oldstate = SetChannelState (channel, 0, 1);
pre_main_destroy (channel);
post_main_destroy (channel);
ch[channel].dsp_size = dsp_size;
pre_main_build (channel);
setDSPBuffsize_main (channel);
post_main_build (channel);
SetChannelState (channel, oldstate, 0);
}
}
PORT
void SetInputSamplerate (int channel, int in_rate)
{ // no re-build of main required
if (in_rate != ch[channel].in_rate)
{
pre_main_destroy (channel);
post_main_destroy (channel);
ch[channel].in_rate = in_rate;
pre_main_build (channel);
setInputSamplerate_main (channel);
post_main_build (channel);
}
}
PORT
void SetDSPSamplerate (int channel, int dsp_rate)
{
if (dsp_rate != ch[channel].dsp_rate)
{
int oldstate = SetChannelState (channel, 0, 1);
pre_main_destroy (channel);
post_main_destroy (channel);
ch[channel].dsp_rate = dsp_rate;
pre_main_build (channel);
setDSPSamplerate_main (channel);
post_main_build (channel);
SetChannelState (channel, oldstate, 0);
}
}
PORT
void SetOutputSamplerate (int channel, int out_rate)
{ // no re-build of main required
if (out_rate != ch[channel].out_rate)
{
pre_main_destroy (channel);
post_main_destroy (channel);
ch[channel].out_rate = out_rate;
pre_main_build (channel);
setOutputSamplerate_main (channel);
post_main_build (channel);
}
}
PORT
void SetAllRates (int channel, int in_rate, int dsp_rate, int out_rate)
{
if ((in_rate != ch[channel].in_rate) || (dsp_rate != ch[channel].dsp_rate) || (out_rate != ch[channel].out_rate))
{
pre_main_destroy (channel);
post_main_destroy (channel);
ch[channel].in_rate = in_rate;
ch[channel].dsp_rate = dsp_rate;
ch[channel].out_rate = out_rate;
pre_main_build (channel);
setInputSamplerate_main (channel);
setDSPSamplerate_main (channel);
setOutputSamplerate_main (channel);
post_main_build (channel);
}
}
PORT
int SetChannelState (int channel, int state, int dmode)
{
IOB a = ch[channel].iob.pc;
int prior_state = ch[channel].state;
int count = 0;
const int timeout = 100;
if (ch[channel].state != state)
{
ch[channel].state = state;
switch (ch[channel].state)
{
case 0:
InterlockedBitTestAndSet (&a->slew.downflag, 0);
InterlockedBitTestAndSet (&ch[channel].flushflag, 0);
if (dmode)
{
while (_InterlockedAnd (&ch[channel].flushflag, 1) && count < timeout)
{
Sleep(1);
count++;
}
}
if (count >= timeout)
{
InterlockedBitTestAndReset (&ch[channel].exchange, 0);
InterlockedBitTestAndReset (&ch[channel].flushflag, 0);
InterlockedBitTestAndReset (&a->slew.downflag, 0);
}
break;
case 1:
InterlockedBitTestAndSet (&a->slew.upflag, 0);
InterlockedBitTestAndSet (&ch[channel].iob.ch_upslew, 0);
InterlockedBitTestAndReset (&ch[channel].iob.pc->exec_bypass, 0);
InterlockedBitTestAndSet (&ch[channel].exchange, 0);
break;
}
}
return prior_state;
}
PORT
void SetChannelTDelayUp (int channel, double time)
{
IOB a;
EnterCriticalSection (&ch[channel].csEXCH);
a = ch[channel].iob.pc;
ch[channel].tdelayup = time;
a->slew.ndelup = (int)(ch[a->channel].tdelayup * ch[a->channel].in_rate);
flush_slews (a);
LeaveCriticalSection (&ch[channel].csEXCH);
}
PORT
void SetChannelTSlewUp (int channel, double time)
{
IOB a;
EnterCriticalSection (&ch[channel].csEXCH);
a = ch[channel].iob.pc;
ch[channel].tslewup = time;
destroy_slews (a);
create_slews (a);
LeaveCriticalSection (&ch[channel].csEXCH);
}
PORT
void SetChannelTDelayDown (int channel, double time)
{
IOB a;
EnterCriticalSection (&ch[channel].csEXCH);
a = ch[channel].iob.pc;
ch[channel].tdelaydown = time;
a->slew.ndeldown = (int)(ch[a->channel].tdelaydown * ch[a->channel].out_rate);
flush_slews (a);
LeaveCriticalSection (&ch[channel].csEXCH);
}
PORT
void SetChannelTSlewDown (int channel, double time)
{
IOB a;
EnterCriticalSection (&ch[channel].csEXCH);
a = ch[channel].iob.pc;
ch[channel].tslewdown = time;
destroy_slews (a);
create_slews (a);
LeaveCriticalSection (&ch[channel].csEXCH);
}