File: audiowin32.c

package info (click to toggle)
baycomusb 0.10-15.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,884 kB
  • sloc: ansic: 49,182; asm: 17,572; sh: 2,442; makefile: 542; pascal: 183; sed: 93; perl: 31
file content (594 lines) | stat: -rw-r--r-- 21,240 bytes parent folder | download | duplicates (9)
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
/*****************************************************************************/

/*
 *      audiowin32.c  --  Audio processing for "virtual transceiver", win32 IO.
 *
 *      Copyright (C) 1999, 2001  Thomas Sailer (t.sailer@alumni.ethz.ch)
 *
 *      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., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 *  Please note that the GPL allows you to use the driver, NOT the radio.
 *  In order to use the radio, you need a license from the communications
 *  authority of your country.
 *
 */

/*****************************************************************************/

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

/* AIX requires this to be the first thing in the file.  */
#ifndef __GNUC__
# if HAVE_ALLOCA_H
#  include <alloca.h>
# else
#  ifdef _AIX
#pragma alloca
#  else
#   ifndef alloca /* predefined by HP cc +Olibcalls */
char *alloca ();
#   endif
#  endif
# endif
#endif

#include <windows.h>
#include <mmsystem.h>
#include <malloc.h>
#include <cguid.h>

#include "trx.h"
#include "trxapi.h"

/* ---------------------------------------------------------------------- */

#define BUFSIZE    8192   /* must be significantly bigger than SNDLATENCY! */

#define WAVE_FORMAT_1M08 1
#define WAVE_FORMAT_1S08 2
#define WAVE_FORMAT_1M16 4
#define WAVE_FORMAT_1S16 8
#define WAVE_FORMAT_2M08 16
#define WAVE_FORMAT_2S08 32
#define WAVE_FORMAT_2M16 64
#define WAVE_FORMAT_2S16 128
#define WAVE_FORMAT_4M08 256
#define WAVE_FORMAT_4S08 512
#define WAVE_FORMAT_4M16 1024
#define WAVE_FORMAT_4S16 2048
#define WAVE_FORMAT_PCM 1

/* ---------------------------------------------------------------------- */

#if defined(HAVE_DIRECTX)

#include <directx.h>

struct dsdrivers {
	struct list_head list;
        GUID guid;
        char name[64];
	char desc[64];
};

static LIST_HEAD(dsplaylist);
static LIST_HEAD(dscapturelist);

/* ---------------------------------------------------------------------- */

static void stop(struct trx_thread_state *state)
{
	if (state->du.a.io.dsplay) {
		IDirectSoundBuffer_Stop(state->du.a.io.playbuf);
		IDirectSoundBuffer_Release(state->du.a.io.playbuf);
	}
	if (state->du.a.io.dsrec) {
		IDirectSoundCaptureBuffer_Stop(state->du.a.io.recbuf);
		IDirectSoundCaptureBuffer_Release(state->du.a.io.recbuf);
		IDirectSoundCapture_Release(state->du.a.io.dsrec);
	}
	if (state->du.a.io.dsplay)
		IDirectSound_Release(state->du.a.io.dsplay);
	state->du.a.io.recbuf = NULL;
	state->du.a.io.dsrec = NULL;
	state->du.a.io.playbuf = NULL;
	state->du.a.io.dsplay = NULL;
}

static void start(struct trx_thread_state *state)
{
	struct list_head *list;
	struct dsdrivers *drv;
        LPGUID lpguid;
        HRESULT res;
        WAVEFORMATEX waveformat;
        DSBUFFERDESC bdesc;
        DSCBUFFERDESC cbdesc;
        DSCAPS caps;
        DSCCAPS ccaps;
        DSBCAPS bcaps;
        DSCBCAPS cbcaps;
        unsigned int isprimary = 0, weight = ~0;
        void *lptr1;
        DWORD lbytes1;
        HWND hwnd = GetDesktopWindow();

	lpguid = NULL;
	for (list = dsplaylist.next; list != &dsplaylist; list = list->next) {
		drv = list_entry(list, struct dsdrivers, list);
		if (strcmp(drv->desc, state->cfg.adapt.audiodevout))
			continue;
		if (memcmp(&drv->guid, &GUID_NULL, sizeof(drv->guid)))
			lpguid = &drv->guid;
		break;
	}
	if (FAILED(res = DirectSoundCreate(lpguid, &state->du.a.io.dsplay, NULL))) {
		lprintf(1, "DirectSoundCreate error 0x%lx\n", res);
		goto errdscreate;
	}
	if (FAILED(res = IDirectSound_SetCooperativeLevel(state->du.a.io.dsplay, hwnd, DSSCL_WRITEPRIMARY))) {
		lprintf(3, "SetCooperativeLevel DSSCL_WRITEPRIMARY error 0x%lx\n", res);
		if (FAILED(res = IDirectSound_SetCooperativeLevel(state->du.a.io.dsplay, hwnd, DSSCL_EXCLUSIVE))) {
			lprintf(1, "SetCooperativeLevel DSSCL_EXCLUSIVE error 0x%lx\n", res);
			goto errdsb;
		}
	} else
		isprimary = 1;
	lpguid = NULL;
	for (list = dscapturelist.next; list != &dscapturelist; list = list->next) {
		drv = list_entry(list, struct dsdrivers, list);
		if (strcmp(drv->desc, state->cfg.adapt.audiodevin))
			continue;
		if (memcmp(&drv->guid, &GUID_NULL, sizeof(drv->guid)))
			lpguid = &drv->guid;
		break;
	}
	if (FAILED(res = DirectSoundCaptureCreate(lpguid, &state->du.a.io.dsrec, NULL))) {
		lprintf(1, "DirectSoundCaptureCreate error 0x%lx\n", res);
		goto errdsb;
	}
	/* DirectSound capabilities */
	caps.dwSize = sizeof(caps);
	if (FAILED(res = IDirectSound_GetCaps(state->du.a.io.dsplay, &caps))) {
		lprintf(1, "DirectSoundGetCaps error 0x%lx\n", res);
		goto errdscb;
	}
	lprintf(5, "DirectSound capabilities:\n"
		"  Flags 0x%04lx\n"
		"  SampleRate min %lu max %lu\n"
		"  # Primary Buffers %lu\n",
		caps.dwFlags, caps.dwMinSecondarySampleRate, caps.dwMaxSecondarySampleRate, caps.dwPrimaryBuffers);
        /* DirectSoundCapture capabilities */
	ccaps.dwSize = sizeof(ccaps);
	if (FAILED(res = IDirectSoundCapture_GetCaps(state->du.a.io.dsrec, &ccaps))) {
		lprintf(1, "DirectSoundCaptureGetCaps error 0x%lx\n", res);
		goto errdscb;
	}
	lprintf(5, "DirectSoundCapture capabilities:\n"
		"  Flags 0x%04lx\n"
		"  Formats 0x%04lx\n"
		"  Channels %lu\n",
		ccaps.dwFlags, ccaps.dwFormats, ccaps.dwChannels);
        /* adjust sampling rate */
	if (!(caps.dwFlags & DSCAPS_PRIMARY16BIT) || !(caps.dwFlags & DSCAPS_PRIMARYMONO)) {
		lprintf(1, "Unsupported playback format 16bit mono\n");
		goto errdscb;
	}
	if (state->du.a.p.sratedspout < caps.dwMinSecondarySampleRate)
		state->du.a.p.sratedspout = caps.dwMinSecondarySampleRate;
	if (state->du.a.p.sratedspout > caps.dwMaxSecondarySampleRate)
		state->du.a.p.sratedspout = caps.dwMaxSecondarySampleRate;
	if ((ccaps.dwFormats & WAVE_FORMAT_1M16) && abs(state->du.a.p.srateusb - 11025) < weight) {
		weight = abs(state->du.a.p.srateusb - 11025);
		state->du.a.p.sratedspin = 11025;
	}
	if ((ccaps.dwFormats & WAVE_FORMAT_1M16) && abs(state->du.a.p.srateusb - 22050) < weight) {
		weight = abs(state->du.a.p.srateusb - 22050);
		state->du.a.p.sratedspin = 22050;
	}
	if ((ccaps.dwFormats & WAVE_FORMAT_1M16) && abs(state->du.a.p.srateusb - 44100) < weight) {
		weight = abs(state->du.a.p.srateusb - 44100);
		state->du.a.p.sratedspin = 44100;
	}
	if (weight == ~0) {
		lprintf(1, "Unsupported capture sampling rate\n");
		goto errdscb;
	}
        /* create capture buffer */
	memset(&waveformat, 0, sizeof(waveformat));
	waveformat.wFormatTag = WAVE_FORMAT_PCM;
	waveformat.wBitsPerSample = 16;
	waveformat.nChannels = 1;
	waveformat.nSamplesPerSec = state->du.a.p.sratedspin;
	waveformat.nBlockAlign = waveformat.nChannels * waveformat.wBitsPerSample / 8;
	waveformat.nAvgBytesPerSec = waveformat.nSamplesPerSec * waveformat.nBlockAlign;
	memset(&cbdesc, 0, sizeof(cbdesc));
	cbdesc.dwSize = sizeof(cbdesc);
	cbdesc.dwFlags = /* DSCBCAPS_WAVEMAPPED */ 0;
	cbdesc.dwBufferBytes = 65536;
	cbdesc.lpwfxFormat = &waveformat;
	if (FAILED(res = IDirectSoundCapture_CreateCaptureBuffer(state->du.a.io.dsrec, &cbdesc, &state->du.a.io.recbuf, NULL))) {
		lprintf(1, "CreateSoundCaptureBuffer error 0x%lx\n", res);
		goto errdscb;
	}
        /* create playback buffer */
	if (isprimary) {
		memset(&bdesc, 0, sizeof(bdesc));
		bdesc.dwSize = sizeof(bdesc);
		bdesc.dwFlags = DSBCAPS_GETCURRENTPOSITION2 | DSBCAPS_PRIMARYBUFFER;
		bdesc.dwBufferBytes = 0;
		bdesc.lpwfxFormat = NULL;
		if (FAILED(res = IDirectSound_CreateSoundBuffer(state->du.a.io.dsplay, &bdesc, &state->du.a.io.playbuf, NULL))) {
			lprintf(1, "DirectSoundCreateSoundBuffer error 0x%lx\n", res);
			goto errdspb;
		}
		memset(&waveformat, 0, sizeof(waveformat));
		waveformat.wFormatTag = WAVE_FORMAT_PCM;
		waveformat.wBitsPerSample = 16;
		waveformat.nChannels = 1;
		waveformat.nSamplesPerSec = state->du.a.p.sratedspout;
		waveformat.nBlockAlign = waveformat.nChannels * waveformat.wBitsPerSample / 8;
		waveformat.nAvgBytesPerSec = waveformat.nSamplesPerSec * waveformat.nBlockAlign;
		if (FAILED(res = IDirectSoundBuffer_SetFormat(state->du.a.io.playbuf, &waveformat))) {
			lprintf(1, "DirectSoundBufferSetFormat error 0x%lx\n", res);
			goto errsnd;
		}
		if (FAILED(res = IDirectSoundBuffer_GetFormat(state->du.a.io.playbuf, &waveformat, sizeof(waveformat), NULL))) {
			lprintf(1, "DirectSoundBufferGetFormat error 0x%lx\n", res);
			goto errsnd;
		}
		lprintf(5, "Sampling rates: Recording %u Playback %u\n", state->du.a.p.sratedspin, state->du.a.p.sratedspout);
	} else {
		/* first try to set the format of the primary buffer */
		memset(&bdesc, 0, sizeof(bdesc));
		bdesc.dwSize = sizeof(bdesc);
		bdesc.dwFlags = DSBCAPS_GETCURRENTPOSITION2 | DSBCAPS_PRIMARYBUFFER;
		bdesc.dwBufferBytes = 0;
		bdesc.lpwfxFormat = NULL;
		if (FAILED(res = IDirectSound_CreateSoundBuffer(state->du.a.io.dsplay, &bdesc, &state->du.a.io.playbuf, NULL))) {
			lprintf(1, "DirectSoundCreateSoundBuffer (primary) error 0x%lx\n", res);
		} else {
			memset(&waveformat, 0, sizeof(waveformat));
			waveformat.wFormatTag = WAVE_FORMAT_PCM;
			waveformat.wBitsPerSample = 16;
			waveformat.nChannels = 1;
			waveformat.nSamplesPerSec = state->du.a.p.sratedspout;
			waveformat.nBlockAlign = waveformat.nChannels * waveformat.wBitsPerSample / 8;
			waveformat.nAvgBytesPerSec = waveformat.nSamplesPerSec * waveformat.nBlockAlign;
			if (FAILED(res = IDirectSoundBuffer_SetFormat(state->du.a.io.playbuf, &waveformat))) {
				lprintf(1, "DirectSoundBufferSetFormat (primary) error 0x%lx\n", res);
			}
			IDirectSoundBuffer_Release(state->du.a.io.playbuf);        
		}
		/* create secondary */
		memset(&waveformat, 0, sizeof(waveformat));
		waveformat.wFormatTag = WAVE_FORMAT_PCM;
		waveformat.wBitsPerSample = 16;
		waveformat.nChannels = 1;
		waveformat.nSamplesPerSec = state->du.a.p.sratedspout;
		waveformat.nBlockAlign = waveformat.nChannels * waveformat.wBitsPerSample / 8;
		waveformat.nAvgBytesPerSec = waveformat.nSamplesPerSec * waveformat.nBlockAlign;
		memset(&bdesc, 0, sizeof(bdesc));
		bdesc.dwSize = sizeof(bdesc);
		bdesc.dwFlags = DSBCAPS_GETCURRENTPOSITION2 | DSBCAPS_GLOBALFOCUS;
		bdesc.dwBufferBytes = 65536;
		bdesc.lpwfxFormat = &waveformat;
		if (FAILED(res = IDirectSound_CreateSoundBuffer(state->du.a.io.dsplay, &bdesc, &state->du.a.io.playbuf, NULL))) {
			lprintf(1, "DirectSoundCreateSoundBuffer error 0x%lx\n", res);
			goto errdspb;
		}
	}
         /* find out buffer size */
	bcaps.dwSize = sizeof(bcaps);
	if (FAILED(res = IDirectSoundBuffer_GetCaps(state->du.a.io.playbuf, &bcaps))) {
		lprintf(1, "DirectSoundBufferGetCaps error 0x%lx\n", res);
		goto errsnd;
	}
	lprintf(1, "Playback buffer characteristics:\n"
		"  Flags 0x%04lx\n"
		"  Buffer Bytes %lu\n"
		"  Unlock Transfer rate %lu\n"
		"  CPU overhead %lu\n",
		bcaps.dwFlags, bcaps.dwBufferBytes, bcaps.dwUnlockTransferRate, bcaps.dwPlayCpuOverhead);
	state->du.a.io.playbufsz = bcaps.dwBufferBytes;
	cbcaps.dwSize = sizeof(cbcaps);
	if (FAILED(res = IDirectSoundCaptureBuffer_GetCaps(state->du.a.io.recbuf, &cbcaps))) {
		lprintf(1, "DirectSoundCaptureBufferGetCaps error 0x%lx\n", res);
		goto errsnd;
	}
	lprintf(5, "Recording buffer characteristics:\n"
		"  Flags 0x%04lx\n"
		"  Buffer Bytes %lu\n",
		cbcaps.dwFlags, cbcaps.dwBufferBytes);
	state->du.a.io.recbufsz = cbcaps.dwBufferBytes;
        /* start recording */
	if (FAILED(res = IDirectSoundCaptureBuffer_Start(state->du.a.io.recbuf, DSCBSTART_LOOPING))) {
		lprintf(1, "DirectSoundCaptureBufferStart error 0x%lx\n", res);
		goto errsnd;
	}
        /* zero playback buffer and start it */
	if (FAILED(res = IDirectSoundBuffer_Lock(state->du.a.io.playbuf, 0, state->du.a.io.playbufsz, &lptr1, &lbytes1, NULL, NULL, 0))) {
		if (res != DSERR_BUFFERLOST) {
			lprintf(1, "DirectSoundBufferLock error 0x%lx\n", res);
			goto errsnd;
		}
		if (FAILED(res = IDirectSoundBuffer_Restore(state->du.a.io.playbuf))) {
			lprintf(1, "DirectSoundBufferRestore error 0x%lx\n", res);
			goto errsnd;
		}
		if (FAILED(res = IDirectSoundBuffer_Lock(state->du.a.io.playbuf, 0, state->du.a.io.playbufsz, &lptr1, &lbytes1, NULL, NULL, 0))) {
			lprintf(1, "DirectSoundBufferLock error 0x%lx\n", res);
			goto errsnd;
		}
	}
	memset(lptr1, 0, lbytes1);
	if (FAILED(res = IDirectSoundBuffer_Unlock(state->du.a.io.playbuf, lptr1, lbytes1, NULL, 0))) {
		lprintf(1, "DirectSoundBufferUnlock error 0x%lx\n", res);
		goto errsnd;
	}
	if (FAILED(res = IDirectSoundBuffer_Play(state->du.a.io.playbuf, 0, 0, DSBPLAY_LOOPING))) {
		lprintf(1, "DirectSoundBufferPlay error 0x%lx\n", res);
		goto errsnd;
	}
 	state->du.a.io.playptr = 2*SNDLATENCY;
	state->du.a.io.recptr = state->du.a.io.recbufsz - 2*SNDLATENCY;
        return;

  errsnd:
	IDirectSoundBuffer_Stop(state->du.a.io.playbuf);
	IDirectSoundBuffer_Release(state->du.a.io.playbuf);
  errdspb:
	IDirectSoundCaptureBuffer_Stop(state->du.a.io.recbuf);
	IDirectSoundCaptureBuffer_Release(state->du.a.io.recbuf);
  errdscb:      
	IDirectSoundCapture_Release(state->du.a.io.dsrec);
  errdsb:
	IDirectSound_Release(state->du.a.io.dsplay);
  errdscreate:
	state->du.a.io.recbuf = NULL;
	state->du.a.io.dsrec = NULL;
	state->du.a.io.playbuf = NULL;
	state->du.a.io.dsplay = NULL;
	state->du.a.p.sratedspin = state->du.a.p.sratedspout = state->du.a.p.srateusb;
}

/* ---------------------------------------------------------------------- */

void audio_open(struct trx_thread_state *state)
{
	state->du.a.io.dsplay = NULL;
	state->du.a.io.dsrec = NULL;
	state->du.a.io.playbuf = NULL;
	state->du.a.io.recbuf = NULL;
	state->du.a.io.playptr = 0;
	state->du.a.io.recptr = 0;
	state->du.a.p.srateusb = state->du.a.p.sratedspin = state->du.a.p.sratedspout = AUDIOSAMPLINGRATE;
	start(state);
	audioproc_init(state);
}

void audio_close(struct trx_thread_state *state)
{
	stop(state);
}

void audio_input(struct trx_thread_state *state, const signed char *samples, unsigned int nrsamples)
{
	int16_t sbuf[BUFSIZE];
	HRESULT res;
	DWORD lockbytes1, lockbytes2, delay;
	int16_t *sptr1;
	int16_t *sptr2;
	int cnt;

	/* usb->dsp direction */
	/* get FIFO count */
	cnt = audioproc_convertoutput(state, nrsamples, samples, sbuf);
	if (!state->du.a.io.playbuf)
		return;
	if (FAILED(res = IDirectSoundBuffer_Lock(state->du.a.io.playbuf, state->du.a.io.playptr, cnt*2, 
						 (LPVOID)&sptr1, &lockbytes1,
						 (LPVOID)&sptr2, &lockbytes2, 0))) {
		lprintf(0, "IDirectSoundBuffer_Lock error %lu\n", res);
		goto err;
	}
	memcpy(sptr1, sbuf, lockbytes1);
	if (lockbytes1 < cnt*sizeof(sbuf[0]))
		memcpy(sptr2, sbuf + lockbytes1/sizeof(sbuf[0]), cnt*sizeof(sbuf[0]) - lockbytes1);
	if (FAILED(res = IDirectSoundBuffer_Unlock(state->du.a.io.playbuf, sptr1, lockbytes1, sptr2, lockbytes2))) {
		lprintf(0, "IDirectSoundBuffer_Unlock error %lu\n", res);
		goto err;
	}
	state->du.a.io.playptr = (state->du.a.io.playptr + cnt*sizeof(sbuf[0])) % state->du.a.io.playbufsz;
	/* get output delay */
	if (FAILED(res = IDirectSoundBuffer_GetCurrentPosition(state->du.a.io.playbuf, &delay, NULL))) {
		lprintf(0, "IDirectSoundBuffer_GetCurrentPosition error %lu\n", res);
		goto err;
	}
	delay = (state->du.a.io.playbufsz + state->du.a.io.playptr - delay) % state->du.a.io.playbufsz;
	delay /= sizeof(sbuf[0]);
	/* adjust speed */
	if (audioproc_adjustoutput(state, delay))
		goto err;
	return;

err:
	stop(state);
}

void audio_output(struct trx_thread_state *state, signed char *samples, unsigned int nrsamples)
{
	int16_t *sbuf;
	HRESULT res;
	DWORD lockbytes1, lockbytes2, delay;
	int16_t *sptr1;
	int16_t *sptr2;
	unsigned int isamples, ocnts;

	/* dsp->usb direction */
	if (!nrsamples)
		return;
	if (!state->du.a.io.recbuf) {
		isamples = audioproc_convertinput_isamples(state, nrsamples);
		sbuf = alloca(isamples * sizeof(sbuf[0]));
		memset(sbuf, 0, isamples * sizeof(sbuf[0]));
		audioproc_convertinput(state, isamples, nrsamples, sbuf, samples);
		return;
	}
	isamples = audioproc_convertinput_isamples(state, nrsamples);
	sbuf = alloca(isamples * sizeof(sbuf[0]));
	if (FAILED(res = IDirectSoundCaptureBuffer_Lock(state->du.a.io.recbuf, state->du.a.io.recptr, isamples * sizeof(sbuf[0]), 
							(LPVOID)&sptr1, &lockbytes1,
							(LPVOID)&sptr2, &lockbytes2, 0))) {
		lprintf(0, "IDirectSoundCaptureBuffer_Lock error %lu\n", res);
		goto err;
	}
	memcpy(sbuf, sptr1, lockbytes1);
	if (lockbytes1 < isamples * sizeof(sbuf[0]))
		memcpy(sbuf+lockbytes1/sizeof(sbuf[0]), sptr2, isamples * sizeof(sbuf[0]) - lockbytes1);
	if (FAILED(res = IDirectSoundCaptureBuffer_Unlock(state->du.a.io.recbuf, sptr1, lockbytes1, sptr2, lockbytes2))) {
		lprintf(0, "IDirectSoundCaptureBuffer_Unlock error %lu\n", res);
		goto err;
	}
	state->du.a.io.recptr = (state->du.a.io.recptr + isamples * sizeof(sbuf[0])) % state->du.a.io.recbufsz;
	audioproc_convertinput(state, isamples, nrsamples, sbuf, samples);
	if (FAILED(res = IDirectSoundCaptureBuffer_GetCurrentPosition(state->du.a.io.recbuf, &delay, NULL))) {
		lprintf(0, "IDirectSoundCaptureBuffer_GetCurrentPosition error %lu\n", res);
		goto err;
	}
	ocnts = (state->du.a.io.recbufsz + delay - state->du.a.io.recptr) % state->du.a.io.recbufsz;
	ocnts /= sizeof(sbuf[0]);
	/* adjust speed */
	if (audioproc_adjustinput(state, ocnts))
		goto err;
	return;

err:
	stop(state);
}

/* ---------------------------------------------------------------------- */

static BOOL CALLBACK DSEnumProc(LPGUID guid, LPCSTR lpszDesc, LPCSTR lpszDrvName, LPVOID lpcontext)
{
	struct list_head *list = lpcontext;
        struct dsdrivers *drv;
 
	if (!(drv = malloc(sizeof(struct dsdrivers)))) {
		lprintf(1, "DSEnumProc: out of memory\n");
		return TRUE;
	}
        if (guid)
                drv->guid = *guid;
        else
                drv->guid = GUID_NULL;
	if (!lpszDrvName)
		drv->name[0] = 0;
	else
		strncpy(drv->name, lpszDrvName, sizeof(drv->name));
	strncpy(drv->desc, lpszDesc, sizeof(drv->desc));
	drv->name[sizeof(drv->name)-1] = 0;
	drv->desc[sizeof(drv->desc)-1] = 0;
	list_add_tail(&drv->list, list);
        lprintf(1, "has %sGUID, desc %s drvname %s\n", guid ? "" : "no ", lpszDesc, lpszDrvName ? lpszDrvName : "(null)");
        return TRUE;
}

void audio_globalinit(void)
{
        lprintf(1, "DirectSound drivers\n");
        DirectSoundEnumerateA(DSEnumProc, &dsplaylist);
        lprintf(1, "DirectSoundCapture drivers\n");
        DirectSoundCaptureEnumerateA(DSEnumProc, &dscapturelist);
}

struct trxapi_baycomusb_adapter_audio_devs *audio_get_device_list(void)
{
	struct trxapi_baycomusb_adapter_audio_devs *ad;
	unsigned int nrdevin = 0, nrdevout = 0;
	struct list_head *list;
	struct dsdrivers *drv;
	unsigned int i;

	for (list = dscapturelist.next; list != &dscapturelist; list = list->next)
		nrdevin++;
	for (list = dsplaylist.next; list != &dsplaylist; list = list->next)
		nrdevout++;
	/* retrieve audio devs */
	if (!(ad = calloc(1, sizeof(struct trxapi_baycomusb_adapter_audio_devs) + 
			  nrdevin * sizeof(trxapi_audiodevice_t) + 
			  nrdevout * sizeof(trxapi_audiodevice_t))))
		return NULL;
	ad->audiodevsin = (void *)(ad + 1);
	ad->nraudiodevsin = nrdevin;
	ad->audiodevsout = &ad->audiodevsin[nrdevin];
	ad->nraudiodevsout = nrdevout;
	for (list = dscapturelist.next, i = 0; i < nrdevin; i++, list = list->next) {
		drv = list_entry(list, struct dsdrivers, list);
		strncpy(ad->audiodevsin[i], drv->desc, sizeof(ad->audiodevsin[i]));
		ad->audiodevsin[i][sizeof(ad->audiodevsin[i])-1] = 0;
	}
	for (list = dsplaylist.next, i = 0; i < nrdevout; i++, list = list->next) {
		drv = list_entry(list, struct dsdrivers, list);
		strncpy(ad->audiodevsout[i], drv->desc, sizeof(ad->audiodevsout[i]));
		ad->audiodevsout[i][sizeof(ad->audiodevsout[i])-1] = 0;
	}
	return ad;
}

/* ---------------------------------------------------------------------- */
#else /* !HAVE_DIRECTX */

void audio_globalinit(void)
{
}

struct trxapi_baycomusb_adapter_audio_devs *audio_get_device_list(void)
{
	return NULL;
}

void audio_open(struct trx_thread_state *state)
{
	state->du.a.p.srateusb = state->du.a.p.sratedspin = state->du.a.p.sratedspout = AUDIOSAMPLINGRATE;
	audioproc_init(state);
}

void audio_close(struct trx_thread_state *state)
{
}

void audio_input(struct trx_thread_state *state, const signed char *samples, unsigned int nrsamples)
{
	audioproc_convertoutput(state, nrsamples, samples, sbuf);
}

void audio_output(struct trx_thread_state *state, signed char *samples, unsigned int nrsamples)
{
	int16_t *sbuf;
	unsigned int isamples;

	if (!nrsamples)
		return;
	isamples = audioproc_convertinput_isamples(state, nrsamples);
	sbuf = alloca(isamples * sizeof(sbuf[0]));
	memset(sbuf, 0, isamples * sizeof(sbuf[0]));
	audioproc_convertinput(state, isamples, nrsamples, sbuf, samples);
}

/* ---------------------------------------------------------------------- */
#endif /* HAVE_DIRECTX */