-
Notifications
You must be signed in to change notification settings - Fork 0
/
winplatform.c++
683 lines (613 loc) · 18.1 KB
/
winplatform.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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
// windows MMIO platform stuff (huge, so it gets its own file)
#ifdef VSS_WINDOWS
#include <windows.h>
const int vfdOutput = -1; //;; stub
const int csampBufMax = 132300; // 10x chickenfactor
static short rgsFrame[csampBufMax];
static short rgsFrameRd[csampBufMax];
#include <cstdlib>
#include <unistd.h>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <io.h>
#include <iostream>
#include "platform.h"
#include "winplatform.h"
#include "vssglobals.h"
#undef DISABLE_INPUT
//#define DISABLE_INPUT 1
int areal_internal_FSendNow(void);
void areal_internal_Send(short* rgsamp, int csamp);
#ifndef DISABLE_INPUT
int areal_internal_FRecvNow(void);
void areal_internal_Recv(short* rgsamp, int csamp);
static int vfSoundIn = 0;
#endif
class Mutex
{
private:
CRITICAL_SECTION lock;
public:
Mutex(void) { InitializeCriticalSection(&lock); }
~Mutex(void) { DeleteCriticalSection(&lock); }
void Lock(void) { EnterCriticalSection(&lock); }
void Unlock(void) { LeaveCriticalSection(&lock); }
};
typedef HGLOBAL HWAVEHDR;
#ifndef DISABLE_INPUT
static Mutex* CSAreal_SYS_Rd = NULL;
static HWAVEHDR hWaveHdrRd;
static LPWAVEHDR lpWaveHdrRd;
static void* lpDataRd;
static HWAVEIN hWaveRd;
static HWAVEHDR hWaveHdr2Rd;
static LPWAVEHDR lpWaveHdr2Rd;
static void* lpData2Rd;
static int fWhichRd = 0;
static int cDoneRd = 0;
#endif
static const int cBondMax = 4; // # of "bonded channels" of stereo output
static int cBond = 1;
static Mutex* CSAreal_SYS_Wr = NULL;
static HWAVEHDR hWaveHdrWr[cBondMax];
static LPWAVEHDR lpWaveHdrWr[cBondMax];
static void* lpDataWr[cBondMax];
static HWAVEOUT hWaveWr[cBondMax];
static HWAVEHDR hWaveHdr2Wr[cBondMax];
static LPWAVEHDR lpWaveHdr2Wr[cBondMax];
static void* lpData2Wr[cBondMax];
static int fWhichWr = 0;
static int cDoneWr = 0;
static int cwFrame = 0;
static void CALLBACK waveOutCallback(HWND /*hWaveWr*/, int wMsg, DWORD /*dwInstance*/, DWORD /*dwParam1*/, DWORD /*dwParam2*/)
{
if (wMsg == WOM_DONE)
{
CSAreal_SYS_Wr->Lock();
// increment, don't just set: in case we fall behind, then we'll catch up again.
cDoneWr++;
CSAreal_SYS_Wr->Unlock();
}
}
#ifndef DISABLE_INPUT
static void CALLBACK waveInCallback(HWND /*hWaveRd*/, int wMsg, DWORD /*dwInstance*/, DWORD /*dwParam1*/, DWORD /*dwParam2*/)
{
if (wMsg == WIM_DATA)
{
CSAreal_SYS_Rd->Lock();
// increment, don't just set: in case we fall behind, then we'll catch up again.
cDoneRd++;
CSAreal_SYS_Rd->Unlock();
}
}
#endif
using std::cerr;
using std::endl;
static int areal_internal_FInitAudioWrite(int nchans)
{
cDoneWr = 0;
fWhichWr = 0;
int cdev = waveOutGetNumDevs();
if (cdev < 1)
{
cerr << "vss error: no MMIO audio output devices.\n";
return FALSE;
}
if (cdev >= 4 && nchans > 2)
{
cerr << "vss warning: multichannel audio requested and >=4 MMIO devices present.\n\tAssuming you want to bond multiple stereo channels on a STUDI/O card\n\tstarting with the second device.\n";
if (nchans % 2 != 0)
cerr << "vss warning: using an even number of channels.\n";
cBond = nchans/2;
}
else if (cdev > 1)
{
cerr << "vss remark: using default audio device (of " <<cdev <<" devices).\n";
}
if (cBond > cBondMax)
{
cerr << "vss error: can't bond " << cBond << " channels.\n"
<< cBondMax << " is the maximum.\n";
return FALSE;
}
UINT x;
for (int iBond=0; iBond<cBond; iBond++)
{
int deviceID = cBond==1 ? WAVE_MAPPER : iBond+1;
int nchansDev = cBond==1 ? nchans : nchans/cBond;
{
WAVEFORMATEX fmt;
fmt.wFormatTag = WAVE_FORMAT_PCM;
fmt.nChannels = nchansDev;
fmt.nSamplesPerSec = (DWORD)globs.SampleRate;
fmt.nAvgBytesPerSec = fmt.nSamplesPerSec * 2 * nchansDev;
fmt.nBlockAlign = 2 * nchansDev;
fmt.wBitsPerSample = 16;
fmt.cbSize = 0;
int fCallback = cBond==1 || iBond==0; // Only 1 guy does callback!
x = waveOutOpen(&hWaveWr[iBond], deviceID, (LPWAVEFORMATEX)&fmt,
fCallback ? (DWORD)&waveOutCallback : 0,
(DWORD)NULL,
fCallback ? CALLBACK_FUNCTION : 0/*CALLBACK_NULL*/);
}
if (x != 0)
{
// handle error
char sz[200];
waveOutGetErrorText(x, sz, sizeof(sz)-1);
// Filter out useless windows explanation.
if (x == 32)
cerr << "vss error: failed to open MMIO audio output for requested format.\n";
else
cerr << "vss error: failed to open MMIO audio output for requested format:\n\t" << sz << endl;
WAVEOUTCAPS caps = {0};
x = waveOutGetDevCaps(deviceID, &caps, sizeof(caps));
if (!x)
{
// Typical values for caps.* are:
// "SB Live! Wave Device": 65k, 11k,22k,44k.
// "ESS Maestro": 65535-channel output at 11k,22k,44k.
// "A device ID has been used that is out of range for your system."
cerr <<"\nvss remark: soundcard is " << caps.szPname
<<"\n\tand supports up to "
<<caps.wChannels <<"-channel output\n";
const int w = caps.dwFormats;
if ((w & (WAVE_FORMAT_1M16|WAVE_FORMAT_1S16|WAVE_FORMAT_2M16|WAVE_FORMAT_2S16|WAVE_FORMAT_4M16|WAVE_FORMAT_4S16)) == 0)
{
cerr <<"vss error: soundcard is only 8-bit. VSS needs a 16-bit soundcard.\n";
return FALSE;
}
#ifdef THIS_IS_NEVER_INFORMATIVE
cerr <<"\tat sample rates of:\n";
if ((w&WAVE_FORMAT_1M16) || (w&WAVE_FORMAT_1S16))
cerr << "\t 11.025 kHz\n";
if ((w&WAVE_FORMAT_2M16) || (w&WAVE_FORMAT_2S16))
cerr << "\t 22.05 kHz\n";
if ((w&WAVE_FORMAT_4M16) || (w&WAVE_FORMAT_4S16))
cerr << "\t 44.1 kHz\n";
#endif
}
return FALSE;
}
const DWORD dwDataSize = cwFrame * 2L / cBond;
lpDataWr[iBond] = new char[dwDataSize];
if (!lpDataWr[iBond])
{
cerr <<"vss error initializing MMIO audio output: out of memory.\n";
goto LAbort3;
}
hWaveHdrWr[iBond] = GlobalAlloc(GMEM_MOVEABLE | GMEM_SHARE, (DWORD)sizeof(WAVEHDR));
if (!hWaveHdrWr[iBond])
{
cerr <<"vss error initializing MMIO audio output: out of memory.\n";
goto LAbort2;
}
lpWaveHdrWr[iBond] = (LPWAVEHDR) GlobalLock(hWaveHdrWr[iBond]);
if (!lpWaveHdrWr[iBond])
{
cerr <<"vss error initializing MMIO audio output: GlobalLock failed.\n";
goto LAbort1;
}
// weird, GlobalLock works here but not for lpDataWr[iBond].
memset(lpDataWr[iBond], 0, dwDataSize);
lpWaveHdrWr[iBond]->lpData = (char FAR *)lpDataWr[iBond];
lpWaveHdrWr[iBond]->dwBufferLength = dwDataSize;
lpWaveHdrWr[iBond]->dwFlags = 0;
lpWaveHdrWr[iBond]->dwLoops = 0L;
waveOutPrepareHeader(hWaveWr[iBond], lpWaveHdrWr[iBond], sizeof(WAVEHDR));
if (cBond == 1)
{
x = waveOutWrite(hWaveWr[iBond], lpWaveHdrWr[iBond], sizeof(WAVEHDR));
if (x != 0)
{
// handle error
//char sz[200];
//waveOutGetErrorText(x, sz, sizeof(sz)-1);
cerr <<"vss error initializing MMIO audio output: waveOutWrite failed.\n";
goto LAbort0;
}
}
lpData2Wr[iBond] = new char[dwDataSize];
if (!lpData2Wr[iBond])
{
cerr <<"vss error initializing MMIO audio output: out of memory.\n";
goto LAbort_3;
}
hWaveHdr2Wr[iBond] = GlobalAlloc(GMEM_MOVEABLE | GMEM_SHARE, (DWORD)sizeof(WAVEHDR));
if (!hWaveHdr2Wr[iBond])
{
cerr <<"vss error initializing MMIO audio output: out of memory\n";
goto LAbort_2;
}
lpWaveHdr2Wr[iBond] = (LPWAVEHDR) GlobalLock(hWaveHdr2Wr[iBond]);
if (!lpWaveHdr2Wr[iBond])
{
cerr <<"vss error initializing MMIO audio output: GlobalLock failed.\n";
goto LAbort_1;
}
memset(lpData2Wr[iBond], 0, dwDataSize);
lpWaveHdr2Wr[iBond]->lpData = (char FAR *)lpData2Wr[iBond];
lpWaveHdr2Wr[iBond]->dwBufferLength = dwDataSize;
lpWaveHdr2Wr[iBond]->dwFlags = 0;
lpWaveHdr2Wr[iBond]->dwLoops = 0L;
waveOutPrepareHeader(hWaveWr[iBond], lpWaveHdr2Wr[iBond], sizeof(WAVEHDR));
if (cBond == 1)
{
x = waveOutWrite(hWaveWr[iBond], lpWaveHdr2Wr[iBond], sizeof(WAVEHDR));
if (x != 0)
{
// handle error
//char sz[200];
//waveOutGetErrorText(x, sz, sizeof(sz)-1);
cerr << "vss error while initializing MMIO audio output: waveOutWrite failed.\n";
goto LAbort_0;
}
}
// write 2 buffers now, so when we get a "buf done" msg and queue up the next one, we're always 1 ahead.
}
if (cBond > 1)
{
// Do all the waveOutWrite's at once, not staggered.
// Don't even attempt to handle errors properly.
// In particular this means that the [Reset] button fails to free
// all the audio devices and VSS will fail to restart.
int iBond;
for (iBond=0; iBond<cBond; iBond++)
{
x = waveOutWrite(hWaveWr[iBond], lpWaveHdrWr[iBond], sizeof(WAVEHDR));
if (x != 0)
{
cerr << "vss error while initializing MMIO audio output: waveOutWrite failed.\n";
goto LAbort_0;
}
}
for (iBond=0; iBond<cBond; iBond++)
{
x = waveOutWrite(hWaveWr[iBond], lpWaveHdr2Wr[iBond], sizeof(WAVEHDR));
if (x != 0)
{
cerr << "vss error while initializing MMIO audio output: waveOutWrite failed.\n";
goto LAbort_0;
}
}
}
CSAreal_SYS_Wr = new Mutex;
return TRUE;
LAbort_0:
GlobalUnlock(hWaveHdr2Wr[0]);
LAbort_1:
GlobalFree(hWaveHdr2Wr[0]);
LAbort_2:
LAbort_3:
LAbort0:
GlobalUnlock(hWaveHdrWr[0]);
LAbort1:
GlobalFree(hWaveHdrWr[0]);
LAbort2:
LAbort3:
waveOutReset(hWaveWr[0]);
x = waveOutClose(hWaveWr[0]);
// This LAbort code leaks resources if iBond>0 when it fails. A rare case.
cerr << "vss error 42: failed to open MMIO audio output.\n";
return FALSE;
}
static int areal_internal_FInitAudioRead(int nchansIn)
{
#ifdef DISABLE_INPUT
return TRUE;
#else
fWhichRd = 0;
if (waveInGetNumDevs() < 1)
{
cerr << "vss error: no MMIO audio input devices.\n";
// no sound-recording devices
return FALSE;
}
cerr << "vss remark: attempting MMIO audio input.\n";;;;
UINT x;
{
WAVEFORMATEX fmt;
fmt.wFormatTag = WAVE_FORMAT_PCM;
fmt.nChannels = nchansIn;
fmt.nSamplesPerSec = (DWORD)globs.SampleRate;
fmt.nAvgBytesPerSec = fmt.nSamplesPerSec * 2 * nchansIn;
fmt.nBlockAlign = 2 * nchansIn;
fmt.wBitsPerSample = 16;
fmt.cbSize = 0;
x = waveInOpen(&hWaveRd, WAVE_MAPPER, (LPWAVEFORMATEX)&fmt, (DWORD)&waveInCallback, (DWORD)NULL, CALLBACK_FUNCTION);
}
cerr << "x is " << x << endl;
if (x != 0)
{
// handle error
char sz[200];
waveInGetErrorText(x, sz, sizeof(sz)-1);
// Filter out useless windows explanation.
if (x == 32)
cerr << "vss error: failed to open MMIO audio input for requested format.\n";
else
cerr << "vss error: failed to open MMIO audio input for requested format:\n\t" << sz << endl;
return FALSE;
}
const DWORD dwDataSize = cwFrame/*8192*/ * 2L;
lpDataRd = new char[dwDataSize];
if (!lpDataRd)
{
cerr <<"vss error initializing MMIO audio input: out of memory\n";
goto LAbort3;
}
hWaveHdrRd = GlobalAlloc(GMEM_MOVEABLE | GMEM_SHARE, (DWORD)sizeof(WAVEHDR));
if (!hWaveHdrRd) goto LAbort2;
lpWaveHdrRd = (LPWAVEHDR) GlobalLock(hWaveHdrRd);
if (!lpWaveHdrRd) goto LAbort1;
memset(lpDataRd, 0, dwDataSize);
lpWaveHdrRd->lpData = (char FAR *)lpDataRd;
lpWaveHdrRd->dwBufferLength = dwDataSize;
lpWaveHdrRd->dwFlags = 0;
waveInPrepareHeader(hWaveRd, lpWaveHdrRd, sizeof(WAVEHDR));
x = waveInAddBuffer(hWaveRd, lpWaveHdrRd, sizeof(WAVEHDR));
if (x != 0)
{
// handle error
//char sz[200];
//waveInGetErrorText(x, sz, sizeof(sz)-1);
goto LAbort0;
}
cerr <<"zxcv1\n";
waveInStart(hWaveRd);
cerr <<"zxcv2\n";
lpData2Rd = new char[dwDataSize];
cerr <<"zxcv2.1\n";
if (!lpData2Rd)
{
cerr <<"vss error initializing MMIO audio input: out of memory\n";
goto LAbort_3;
}
hWaveHdr2Rd = GlobalAlloc(GMEM_MOVEABLE | GMEM_SHARE, (DWORD)sizeof(WAVEHDR));
cerr <<"zxcv2.2\n";
if (!hWaveHdr2Rd)
goto LAbort_2;
lpWaveHdr2Rd = (LPWAVEHDR) GlobalLock(hWaveHdr2Rd);
// cerr <<"clobbered old value of lpWaveHdr2Rd!\n";
// lpWaveHdr2Rd = new WAVEHDR;
cerr <<"zxcv2.3\n";
if (!lpWaveHdr2Rd)
goto LAbort_1;
memset(lpData2Rd, 0, dwDataSize);
lpWaveHdr2Rd->lpData = (char FAR *)lpData2Rd;
lpWaveHdr2Rd->dwBufferLength = dwDataSize;
lpWaveHdr2Rd->dwFlags = 0;
cerr <<"zxcv2.4\n";
waveInPrepareHeader(hWaveRd, lpWaveHdr2Rd, sizeof(WAVEHDR));
cerr <<"zxcv2.5\n";
// This next line causes vss to crash. Nuke ALL the GlobalLock's?
x = waveInAddBuffer(hWaveRd, lpWaveHdr2Rd, sizeof(WAVEHDR));
cerr <<"zxcv3\n";
if (x != 0)
{
// handle error
//char sz[200];
//waveInGetErrorText(x, sz, sizeof(sz)-1);
cerr << "vss error while initializing MMIO audio input: waveInAddBuffer failed.\n";
goto LAbort_0;
}
// read 2 buffers now, so we're always 1 ahead.
CSAreal_SYS_Rd = new Mutex;
cerr <<"vss remark: MMIO audio input initialized.\n\n";
return TRUE;
LAbort_0:
GlobalUnlock(hWaveHdr2Rd);
LAbort_1:
GlobalFree(hWaveHdr2Rd);
LAbort_2:
LAbort_3:
LAbort0:
GlobalUnlock(hWaveHdrRd);
LAbort1:
GlobalFree(hWaveHdrRd);
LAbort2:
LAbort3:
waveInReset(hWaveRd);
x = waveInClose(hWaveRd);
cerr << "vss error: failed to open MMIO audio input.\n";
return FALSE;
#endif
}
#include "fmod.h" // Just for FSOUND_STREAM, we don't really use anything.
extern void streamcallback(FSOUND_STREAM*, void *buff, int len, int param);
extern short* vrgsCallback;
/*extern int vcbCallback;*/
int areal_internal_FInitAudio(int nchansOut, int nchansIn, int fSoundIn, int cwFrameArg)
{
cwFrame = cwFrameArg;
memset(rgsFrame, 0, 2*cwFrame);
memset(rgsFrameRd, 0, 2*cwFrame);
#ifndef DISABLE_INPUT
vfSoundIn = 0;
if (fSoundIn)
{
if (areal_internal_FInitAudioRead(nchansIn))
vfSoundIn = 1;
// No error message. Fall back to output-only if input failed.
}
#endif
if (!areal_internal_FInitAudioWrite(nchansOut))
return 0;
// Cygwin doesn't appear to have pthreads, and says it isn't thread-safe.
// The faq recommends using _spawnl(), which runs another .exe,
// as being faster than fork() (uses CreateProcess()).
// Looks like doing everything in one thread will be better for now.
//
// But if we were to spawn a ComputeThread(), we'd do it here.
// Prime the pump!
streamcallback(NULL, rgsFrame, cwFrame*2 /*vcbCallback*/, 0);
return 1;
}
void areal_internal_TermAudio(void)
{
// quit now: reset, then wait for windows to notice.
#ifndef DISABLE_INPUT
if (vfSoundIn)
waveInReset(hWaveRd);
#endif
for (int iBond=0; iBond<cBond; iBond++)
{
waveOutReset(hWaveWr[iBond]);
// The following waveOutUnprepareHeader() sometimes causes a click.
// This is particularly noticeable if you wait between the Reset and Unprepare.
// Multiple waveOutReset()'s don't work around the problem.
// And the manual says we can't do waveOutUnprepareHeader (or anything
// with hWaveWr[iBond]) after waveOutClose(hWaveOut).
// gotta wait until it's finished playing before doing this!
if (waveOutUnprepareHeader(hWaveWr[iBond], lpWaveHdrWr[iBond], sizeof(WAVEHDR)) != 0)
{
// try it again. if it still fails, what can we do?
Sleep(500);
if (waveOutUnprepareHeader(hWaveWr[iBond], lpWaveHdrWr[iBond], sizeof(WAVEHDR)) != 0)
{
// Need to handle this case. globalunlock and globalfree will gpfault.
cerr <<"vss error closing MMIO audio output: waveOutUnprepareHeader failed\n";
return;
}
}
if (waveOutUnprepareHeader(hWaveWr[iBond], lpWaveHdr2Wr[iBond], sizeof(WAVEHDR)) != 0)
{
cerr <<"vss error closing MMIO audio output: waveOutUnprepareHeader failed\n";
}
GlobalUnlock(hWaveHdrWr[iBond]);
GlobalFree(hWaveHdrWr[iBond]);
GlobalUnlock(hWaveHdr2Wr[iBond]);
GlobalFree(hWaveHdr2Wr[iBond]);
(void)waveOutClose(hWaveWr[iBond]);
}
delete CSAreal_SYS_Wr;
#ifndef DISABLE_INPUT
if (vfSoundIn)
{
if (waveInUnprepareHeader(hWaveRd, lpWaveHdrRd, sizeof(WAVEHDR)) != 0)
{
cerr <<"vss error closing MMIO audio input: waveInUnprepareHeader failed\n";
return;
}
if (waveInUnprepareHeader(hWaveRd, lpWaveHdr2Rd, sizeof(WAVEHDR)) != 0)
{
cerr <<"vss error closing MMIO audio input: waveInUnprepareHeader failed\n";
}
GlobalUnlock(hWaveHdrRd);
GlobalFree(hWaveHdrRd);
GlobalUnlock(hWaveHdr2Rd);
GlobalFree(hWaveHdr2Rd);
(void)waveInClose(hWaveRd);
delete CSAreal_SYS_Rd;
}
#endif
}
int areal_internal_FSendNow(void)
{
int w = 0;
CSAreal_SYS_Wr->Lock();
if (cDoneWr > 0)
{
cDoneWr--;
w = 1;
}
CSAreal_SYS_Wr->Unlock();
return w;
}
void areal_internal_Send(short* rgsamp, int csamp)
{
fWhichWr ^= 1;
if (cBond == 1)
{
const int iBond=0;
{
short* lpw=(short*)(fWhichWr ? lpDataWr[iBond] : lpData2Wr[iBond]);
memcpy(lpw, rgsamp, csamp * sizeof(short));
(void)waveOutWrite(hWaveWr[iBond],
fWhichWr ? lpWaveHdrWr[iBond] : lpWaveHdr2Wr[iBond],
sizeof(WAVEHDR));
}
}
else
{
// Tricky: lpw points to frames of cBond*2 shorts,
// which need to be split into cBond stereo pairs of shorts.
// csamp is the # of samples, so csamp/cBond is the # of samps per dev.
int iBond;
for (iBond=0; iBond<cBond; iBond++)
{
short* lpw=(short*)(fWhichWr ? lpDataWr[iBond] : lpData2Wr[iBond]);
int iSrc=iBond*2; // *2 for stereo. Initial offset into the src.
int iDst=0;
const int iDstMax = csamp/cBond;
const int diSrc = (cBond-1)*2;
while (iDst < iDstMax)
{
lpw[iDst++] = rgsamp[iSrc++];
lpw[iDst++] = rgsamp[iSrc++];
iSrc += diSrc;
}
}
for (iBond=0; iBond<cBond; iBond++)
{
(void)waveOutWrite(hWaveWr[iBond],
fWhichWr ? lpWaveHdrWr[iBond] : lpWaveHdr2Wr[iBond],
sizeof(WAVEHDR));
}
}
if (vfdOutput >= 0)
write(vfdOutput, rgsamp, csamp * sizeof(short));
}
#ifndef DISABLE_INPUT
int areal_internal_FRecvNow(void)
{
if (!vfSoundIn)
return 0;
int w = 0;
CSAreal_SYS_Rd->Lock();
if (cDoneRd > 0)
{
cDoneRd--;
w = 1;
}
CSAreal_SYS_Rd->Unlock();
return w;
}
#endif
void areal_internal_Recv(short* rgsamp, int csamp)
{
#ifndef DISABLE_INPUT
(void)waveInAddBuffer(hWaveRd, !fWhichRd ? lpWaveHdrRd : lpWaveHdr2Rd, sizeof(WAVEHDR));
fWhichRd ^= 1;
short* lpw = (short*)(!fWhichRd ? lpData2Rd : lpDataRd);
for (int i=0; i<csamp; i++)
rgsamp[i] = lpw[i];
#endif
}
extern void areal_internal_Frame(void)
{
// get input from the sound hardware
int fRecvNow = areal_internal_FRecvNow();
if (fRecvNow)
areal_internal_Recv(rgsFrameRd, cwFrame);
static int fFilled = 0;
if (!fFilled)
{
// Ask for another buffer of samples.
// (vrgsCallback can be NULL during initialization;
// do nothing, in that case.)
if (vrgsCallback)
{
streamcallback(NULL, rgsFrame, cwFrame*2 /*vcbCallback*/, 0);
}
fFilled = 1;
}
int fSendNow = areal_internal_FSendNow();
if (fSendNow)
{
areal_internal_Send(rgsFrame, cwFrame);
fFilled = 0;
}
}
#endif