forked from komh/kai
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kai_mixer.c
657 lines (495 loc) · 16.9 KB
/
kai_mixer.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
/*
Soft Mixer for K Audio Interface
Copyright (C) 2021 by KO Myung-Hun <komh@chollian.net>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library 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
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the Free
Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include "kai_internal.h"
#include "kai_server.h"
#include "kai_mixer.h"
#include <string.h>
#ifdef __WATCOMC__
#include <alloca.h>
#include <process.h>
#endif
static void normalize( PVOID pBuffer, ULONG ulLen, PINSTANCELIST pil )
{
PKAISPEC pks = &pil->ks;
PMIXERSTREAM pms = pil->pms;
PKAISPEC pksMixer = pms->pksMixer;
short *ps16Buf = alloca( pksMixer->ulBufferSize );
int samples = BYTESTOSAMPLES( ulLen, *pks );
if( pks->ulBitsPerSample == 8 )
{
/* 8 bits mono/stereo to 16 bits stereo */
unsigned char *pu8 = pBuffer;
short *ps16 = ps16Buf;
int i;
for( i = 0; i < samples; i++ )
{
int sample = *pu8++ * 65535 / 255 - 32768;
*ps16++ = sample;
if( pks->ulChannels == 1 )
*ps16++ = sample;
}
ulLen = ( ps16 - ps16Buf ) * sizeof( *ps16 );
pBuffer = ps16Buf;
}
else if( pks->ulBitsPerSample == 16 && pks->ulChannels == 1 )
{
/* 16 bits mono to 16 bits stereo */
short *ps16m = pBuffer;
short *ps16s = ps16Buf;
int i;
for( i = 0; i < samples; i++ )
{
*ps16s++ = *ps16m;
*ps16s++ = *ps16m++;
}
ulLen = ( ps16s - ps16Buf ) * sizeof( *ps16s );
pBuffer = ps16Buf;
}
if( pks->ulSamplingRate != pksMixer->ulSamplingRate )
{
/* resampling */
spx_uint32_t inSamples = samples;
spx_uint32_t outSamples =
BYTESTOSAMPLES( pms->bufRes.ulSize, *pksMixer );
speex_resampler_set_rate( pms->srs,
pks->ulSamplingRate, pksMixer->ulSamplingRate );
speex_resampler_process_interleaved_int( pms->srs,
( spx_int16_t * )pBuffer, &inSamples,
( spx_int16_t * )pms->bufRes.pch, &outSamples );
ulLen = SAMPLESTOBYTES( outSamples, *pksMixer );
}
else
{
/* straight copy */
memcpy( pms->bufRes.pch, pBuffer, ulLen );
}
pms->bufRes.ulLen = ulLen;
pms->bufRes.ulPos = 0;
}
static void mixerFillThread( void *arg )
{
PINSTANCELIST pil = arg;
PMIXERSTREAM pms = pil->pms;
PKAISPEC pks = &pil->ks;
ULONG ulBufSize;
ULONG ulBufLen;
PCH pchBuf;
ULONG ulCbBufSize = pks->ulBufferSize;
ULONG ulCbBufLen;
PCH pchCbBuf = alloca( ulCbBufSize );
BOOL fMoreData = TRUE;
boostThread();
while( fMoreData || pms->bufRes.ulLen > 0 )
{
bufWriteLock( pms->pbuf, ( PVOID * )&pchBuf, &ulBufSize );
if( !pms->fFilling )
break;
ulBufLen = 0;
do
{
if( pms->bufRes.ulLen == 0 )
{
if( !fMoreData )
break;
ulCbBufLen = pil->pfnUserCb( pil->pUserData,
pchCbBuf, ulCbBufSize );
if( ulCbBufLen < ulCbBufSize )
fMoreData = FALSE;
normalize( pchCbBuf, ulCbBufLen, pil );
}
ulCbBufLen = pms->bufRes.ulLen < ulBufSize ?
pms->bufRes.ulLen : ulBufSize;
memcpy( pchBuf + ulBufLen, pms->bufRes.pch + pms->bufRes.ulPos,
ulCbBufLen );
ulBufLen += ulCbBufLen;
ulBufSize -= ulCbBufLen;
pms->bufRes.ulLen -= ulCbBufLen;
pms->bufRes.ulPos += ulCbBufLen;
} while( ulBufSize > 0 );
bufWriteUnlock( pms->pbuf, ulBufLen );
}
}
APIRET _kaiStreamOpen( PKAISPEC pksMixer, PHKAIMIXER phkm,
const PKAISPEC pksWanted, PKAISPEC pksObtained,
PHKAI phkai )
{
HKAIMIXER hkm = *phkm;
KAISPEC ksMixerObtained;
ULONG rc = KAIE_NO_ERROR;
if( !*phkm )
rc = kaiMixerOpen( pksMixer, &ksMixerObtained, &hkm );
if( !rc )
rc = kaiMixerStreamOpen( hkm, pksWanted, pksObtained, phkai );
// tried to open a mixer at this time ?
if( !*phkm )
{
// all succeeded ?
if( !rc )
{
// copy obtained values
memcpy( pksMixer, &ksMixerObtained, sizeof( KAISPEC ));
*phkm = hkm;
}
else if( hkm ) // error but mixer opened ?
kaiMixerClose( hkm ); // then close
}
return rc;
}
APIRET _kaiStreamClose( PHKAIMIXER phkm, HKAIMIXERSTREAM hkms )
{
ULONG rc;
rc = kaiMixerStreamClose( *phkm, hkms );
if( !rc && instanceStreamCount( *phkm ) == 0 )
{
rc = kaiMixerClose( *phkm );
if( !rc )
*phkm = NULLHANDLE;
}
return rc;
}
APIRET _kaiStreamPlay( PINSTANCELIST pil )
{
PMIXERSTREAM pms = pil->pms;
PINSTANCELIST pilMixer;
APIRET rc = KAIE_NO_ERROR;
if( pms->fPlaying )
return KAIE_NO_ERROR;
pilMixer = instanceVerify( pil->hkai, IVF_MIXER );
pms->pbuf = bufCreate( pil->ks.ulNumBuffers,
pilMixer->ks.ulBufferSize );
if( !pms->pbuf )
return KAIE_NOT_ENOUGH_MEMORY;
STORE( &pms->fFilling, TRUE );
pms->tid = _beginthread( mixerFillThread, NULL, THREAD_STACK_SIZE, pil );
// prevent initial buffer-underrun and unnecessary latency
bufReadWaitFull( pms->pbuf );
instanceLock( pilMixer );
/* Set fPlaying to TRUE before calling pfnPlay() to prevent
calling call back function from being stopped */
STORE( &pms->fPlaying, TRUE );
STORE( &pms->fPaused, FALSE );
STORE( &pms->fCompleted, FALSE );
if( instancePlayingStreamCount( pil->hkai ) == 1 )
{
/* Ensure to stop playing in sub-system before trying to play in
sub-system because sub-system plays a little more even if playing
a mixer stream is completed. Otherwise sub-system does not start
to play because it thinks it is already playing */
_kaiGetApi()->pfnStop( pil->hkai );
rc = _kaiGetApi()->pfnPlay( pil->hkai );
if( rc )
streamStop( pil ); // clean up
}
instanceUnlock( pilMixer );
return rc;
}
APIRET _kaiStreamStop( PINSTANCELIST pil )
{
PMIXERSTREAM pms = pil->pms;
PINSTANCELIST pilMixer;
APIRET rc = KAIE_NO_ERROR;
if( !pms->fPlaying )
return KAIE_NO_ERROR;
pilMixer = instanceVerify( pil->hkai, IVF_MIXER );
instanceLock( pilMixer );
if( instancePlayingStreamCount( pil->hkai ) == 1 )
rc = _kaiGetApi()->pfnStop( pil->hkai );
if( !rc )
{
STORE( &pms->fFilling, FALSE );
bufWriteCancel( pms->pbuf );
while( DosWaitThread( &pms->tid, DCWW_WAIT ) == ERROR_INTERRUPT )
/* nothing */;
bufDestroy( pms->pbuf );
STORE( &pms->fPlaying, FALSE );
STORE( &pms->fPaused, FALSE );
}
instanceUnlock( pilMixer );
return rc;
}
APIRET _kaiStreamPause( PINSTANCELIST pil )
{
PMIXERSTREAM pms = pil->pms;
if( !pms->fPlaying )
return KAIE_NO_ERROR;
if( pms->fPaused )
return KAIE_NO_ERROR;
STORE( &pms->fPaused, TRUE );
return KAIE_NO_ERROR;
}
APIRET _kaiStreamResume( PINSTANCELIST pil )
{
PMIXERSTREAM pms = pil->pms;
if( !pms->fPlaying )
return KAIE_NO_ERROR;
if( !pms->fPaused )
return KAIE_NO_ERROR;
STORE( &pms->fPaused, FALSE );
return KAIE_NO_ERROR;
}
APIRET _kaiStreamClearBuffer( PINSTANCELIST pil )
{
PMIXERSTREAM pms = pil->pms;
bufClear( pms->pbuf, pil->ks.bSilence );
return KAIE_NO_ERROR;
}
APIRET _kaiStreamStatus( PINSTANCELIST pil )
{
PMIXERSTREAM pms = pil->pms;
ULONG ulStatus = 0;
if( pms->fPlaying )
ulStatus |= KAIS_PLAYING;
if( pms->fPaused )
ulStatus |= KAIS_PAUSED;
if( pms->fCompleted )
ulStatus |= KAIS_COMPLETED;
return ulStatus;
}
typedef struct fillBufferArgs
{
PINSTANCELIST pilMixer;
PVOID pBuffer;
ULONG ulBufSize;
PCHAR pchBuf;
ULONG ulMaxLen;
} FILLBUFFERARGS, *PFILLBUFFERARGS;
static VOID fillBuffer( PINSTANCELIST pil, void *arg )
{
PFILLBUFFERARGS pargs = arg;
PINSTANCELIST pilMixer = pargs->pilMixer;
PVOID pMixerBuffer = pargs->pBuffer;
ULONG ulMixerBufSize = pargs->ulBufSize;
PCHAR pchBuf = pargs->pchBuf;
PMIXERSTREAM pms = pil->pms;
PVOID pBuffer;
ULONG ulLen;
short *pDst;
short *pSrc;
short *pEnd;
if( !ISSTREAM( pil ) || pil->hkai != pilMixer->hkai || !pms->fPlaying )
return;
/* Waiting to play remainig buffers passed to audio driver such as DART */
if( pms->fEOS && --pms->lCountDown == 0 )
{
STORE( &pms->fFilling, FALSE );
/* Terminate fill thread */
bufWriteCancel( pms->pbuf );
STORE( &pms->fPlaying, FALSE );
STORE( &pms->fPaused, FALSE );
STORE( &pms->fCompleted, TRUE );
pms->fEOS = FALSE;
return;
}
/* Read from normalized buffer */
if( pms->fEOS || pms->fPaused ||
bufReadLock( pms->pbuf, &pBuffer, &ulLen ) == -1 )
{
if( !pms->fEOS && !pms->fPaused )
dprintf("MIXER: buffer underrun!");
pargs->ulMaxLen = ulMixerBufSize;
return;
}
else
{
memcpy( pchBuf, pBuffer, ulLen );
bufReadUnlock( pms->pbuf );
}
/* Process soft volume */
if( pil->fSoftVol &&
( pil->lLeftVol != 100 || !pil->fLeftState ||
pil->lRightVol != 100 || !pil->fRightState ))
APPLY_SOFT_VOLUME( PSHORT, pchBuf, ulLen, pil );
/* End of stream ? */
if( ulLen < ulMixerBufSize )
{
pms->fEOS = TRUE;
pms->lCountDown = pilMixer->ks.ulNumBuffers;
memset( pchBuf + ulLen, 0, ulMixerBufSize - ulLen );
ulLen = ulMixerBufSize;
}
/* Synthesize audio streams */
pDst = pMixerBuffer;
pSrc = ( short * )pchBuf;
pEnd = pSrc + ulLen / sizeof( *pSrc );
while( pSrc < pEnd )
{
int sample = *pDst;
sample += *pSrc++;
if( sample > 32767 )
sample = 32767;
else if( sample < -32768 )
sample = -32768;
*pDst++ = sample;
}
/* Update maximum length of a packet if necessary */
if( pargs->ulMaxLen < ulLen )
pargs->ulMaxLen = ulLen;
}
static ULONG APIENTRY kaiMixerCallBack( PVOID pCBData, PVOID pBuffer,
ULONG ulBufSize )
{
FILLBUFFERARGS args;
memset( pBuffer, 0, ulBufSize );
args.pilMixer = pCBData;
args.pBuffer = pBuffer;
args.ulBufSize = ulBufSize;
args.pchBuf = alloca( ulBufSize );
args.ulMaxLen = 0;
instanceLoop( fillBuffer, &args );
return args.ulMaxLen;
}
APIRET DLLEXPORT APIENTRY kaiMixerOpen( const PKAISPEC pksWanted,
PKAISPEC pksObtained,
PHKAIMIXER phkm )
{
PINSTANCELIST pil;
ULONG ulMinBufferSize;
APIRET rc;
if( !kaiGetInitCount())
return KAIE_NOT_INITIALIZED;
if( !pksWanted || !pksObtained || !phkm )
return KAIE_INVALID_PARAMETER;
/* Support 16 bits stereo audio only */
if( pksWanted->ulBitsPerSample != 16 || pksWanted->ulChannels != 2 )
return KAIE_INVALID_PARAMETER;
if( _kaiIsServer())
return serverMixerOpen( pksWanted, pksObtained, phkm );
pil = instanceNew( FALSE, NULL, NULL );
if( !pil )
return KAIE_NOT_ENOUGH_MEMORY;
ulMinBufferSize =
SAMPLESTOBYTES( _kaiGetMinSamples( pksWanted->usDeviceIndex ),
*pksWanted );
memcpy( &pil->ks, pksWanted, sizeof( KAISPEC ));
pil->ks.ulNumBuffers = 2; /* Limit buffers of backend to 2 */
if( pil->ks.ulBufferSize > 0 && pil->ks.ulBufferSize < ulMinBufferSize )
pil->ks.ulBufferSize = ulMinBufferSize;
pil->ks.pfnCallBack = kaiMixerCallBack;
pil->ks.pCallBackData = pil;
pil->pfnUserCb = NULL;
pil->pUserData = NULL;
rc = _kaiGetApi()->pfnOpen( &pil->ks, phkm );
if( rc )
{
instanceFree( pil );
return rc;
}
/* Record an user supplied a number of buffers to use later, and it should
be 2 at least */
if( pil->ks.ulNumBuffers < pksWanted->ulNumBuffers )
pil->ks.ulNumBuffers = pksWanted->ulNumBuffers;
memcpy( pksObtained, &pil->ks, sizeof( KAISPEC ));
pksObtained->pfnCallBack = NULL;
pksObtained->pCallBackData = NULL;
instanceAdd( *phkm, *phkm, pil );
return KAIE_NO_ERROR;
}
APIRET DLLEXPORT APIENTRY kaiMixerClose( HKAIMIXER hkm )
{
PINSTANCELIST pil;
APIRET rc = KAIE_NO_ERROR;
if( !kaiGetInitCount())
return KAIE_NOT_INITIALIZED;
if(( pil = instanceVerify( hkm, IVF_SERVER )) != NULL )
return serverMixerClose( pil );
if( !instanceVerify( hkm, IVF_MIXER ))
return KAIE_INVALID_HANDLE;
if( instanceStreamCount( hkm ) != 0 )
return KAIE_STREAMS_NOT_CLOSED;
rc = _kaiGetApi()->pfnClose( hkm );
if( rc )
return rc;
instanceDel( hkm );
return KAIE_NO_ERROR;
}
APIRET DLLEXPORT APIENTRY kaiMixerStreamOpen( HKAIMIXER hkm,
const PKAISPEC pksWanted,
PKAISPEC pksObtained,
PHKAIMIXERSTREAM phkms )
{
PINSTANCELIST pilMixer;
PINSTANCELIST pil;
if( !kaiGetInitCount())
return KAIE_NOT_INITIALIZED;
if( !pksWanted || !pksObtained || !phkms )
return KAIE_INVALID_PARAMETER;
if( !pksWanted->pfnCallBack )
return KAIE_INVALID_PARAMETER;
if(( pilMixer = instanceVerify( hkm, IVF_SERVER )) != NULL )
return serverMixerStreamOpen( pilMixer, pksWanted, pksObtained, phkms );
if( !( pilMixer = instanceVerify( hkm, IVF_MIXER )))
return KAIE_INVALID_HANDLE;
if( pksWanted->ulType != pilMixer->ks.ulType )
return KAIE_INVALID_PARAMETER;
if( pksWanted->ulBitsPerSample > pilMixer->ks.ulBitsPerSample )
return KAIE_INVALID_PARAMETER;
if( pksWanted->ulChannels > pilMixer->ks.ulChannels )
return KAIE_INVALID_PARAMETER;
pil = instanceNew( TRUE, &pilMixer->ks, pksWanted );
if( pil )
{
int err;
pil->pms->srs = speex_resampler_init( pilMixer->ks.ulChannels,
pksWanted->ulSamplingRate,
pilMixer->ks.ulSamplingRate,
_kaiGetResamplerQ(), &err );
}
if( !pil || !pil->pms->srs )
{
instanceFree( pil );
return KAIE_NOT_ENOUGH_MEMORY;
}
memcpy( &pil->ks, pksWanted, sizeof( KAISPEC ));
pil->ks.usDeviceIndex = pilMixer->ks.usDeviceIndex;
/* A number of buffers as twice as a mixer at least */
pil->ks.ulNumBuffers = pilMixer->ks.ulNumBuffers * 2;
if( pil->ks.ulNumBuffers < pksWanted->ulNumBuffers )
pil->ks.ulNumBuffers = pksWanted->ulNumBuffers;
pil->ks.ulBufferSize =
SAMPLESTOBYTES( BYTESTOSAMPLES(pilMixer->ks.ulBufferSize,
pilMixer->ks), pil->ks);
pil->ks.fShareable = TRUE;
pil->ks.pfnCallBack = NULL;
pil->ks.pCallBackData = NULL;
pil->ks.bSilence = pksWanted->ulBitsPerSample == 8 ? 0x80 : 0;
pil->pfnUserCb = pksWanted->pfnCallBack;
pil->pUserData = pksWanted->pCallBackData;
memcpy( pksObtained, &pil->ks, sizeof( KAISPEC ));
pksObtained->pfnCallBack = pksWanted->pfnCallBack;
pksObtained->pCallBackData = pksWanted->pCallBackData;
*phkms = ( HKAIMIXERSTREAM )pil;
instanceAdd( *phkms, pilMixer->hkai, pil );
return KAIE_NO_ERROR;
}
APIRET DLLEXPORT APIENTRY kaiMixerStreamClose( HKAIMIXER hkm,
HKAIMIXERSTREAM hkms )
{
PINSTANCELIST pilMixer;
PINSTANCELIST pilStream;
if( !kaiGetInitCount())
return KAIE_NOT_INITIALIZED;
if(( pilMixer = instanceVerify( hkm, IVF_SERVER )) != NULL &&
( pilStream = instanceVerify( hkms, IVF_SERVER )) != NULL )
return serverMixerStreamClose( pilMixer, pilStream );
if( !( pilMixer = instanceVerify( hkm, IVF_MIXER )))
return KAIE_INVALID_HANDLE;
if( !( pilStream = instanceVerify( hkms, IVF_STREAM )) ||
pilStream->hkai != pilMixer->hkai )
return KAIE_INVALID_HANDLE;
_kaiStreamStop( pilStream );
instanceDel( hkms );
return KAIE_NO_ERROR;
}