forked from komh/kai
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkaidemo3.c
287 lines (222 loc) · 7.03 KB
/
kaidemo3.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
/*
* KAI DEMO3 for KAI Mixer
* Copyright (C) 2010-2021 KO Myung-Hun <[email protected]>
*
* This file is a part of K Audio Interface.
*
* This program is free software. It comes without any warranty, to
* the extent permitted by applicable law. You can redistribute it
* and/or modify it under the terms of the Do What The Fuck You Want
* To Public License, Version 2, as published by Sam Hocevar. See
* http://www.wtfpl.net/ for more details.
*/
#define INCL_KBD
#define INCL_DOS
#include <os2.h>
#define INCL_OS2MM
#include <os2me.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#ifdef __WATCOMC__
#include <process.h>
#endif
#include "kai.h"
#define BUF_SIZE 1024
static BOOL m_fQuit;
static HKAIMIXER m_hkm;
typedef struct tagCBDATA
{
BYTE abBuf[ BUF_SIZE ];
int iBufIndex;
int iBufLen;
HMMIO hmmio;
} CBDATA, *PCBDATA;
static
ULONG APIENTRY kaiCallback ( PVOID pCBData, PVOID Buffer, ULONG BufferSize )
{
PCBDATA pcd = pCBData;
PBYTE pbBuffer = Buffer;
LONG lLen;
while( BufferSize > 0 )
{
if( pcd->iBufIndex >= pcd->iBufLen )
{
pcd->iBufLen = mmioRead( pcd->hmmio, pcd->abBuf, BUF_SIZE );
if( pcd->iBufLen == 0 )
break;
pcd->iBufIndex = 0;
}
lLen = pcd->iBufLen - pcd->iBufIndex;
if( lLen > BufferSize )
lLen = BufferSize;
memcpy( pbBuffer, &pcd->abBuf[ pcd->iBufIndex ], lLen );
pcd->iBufIndex += lLen;
pbBuffer += lLen;
BufferSize -= lLen;
}
return pbBuffer - ( PBYTE )Buffer;
}
static int read_key( void )
{
KBDKEYINFO Char;
KbdCharIn(&Char, IO_NOWAIT, 0);
if( Char.fbStatus )
return Char.chChar;
return 0;
}
static void playThread( void *arg )
{
char *name = arg;
HMMIO hmmio;
MMIOINFO mmioInfo;
MMAUDIOHEADER mmAudioHeader;
LONG lBytesRead;
KAISPEC ksWanted, ksObtained;
HKAIMIXERSTREAM hkms;
CBDATA cd;
/* Open the audio file.
*/
memset( &mmioInfo, '\0', sizeof( MMIOINFO ));
mmioInfo.fccIOProc = mmioFOURCC('W', 'A', 'V', 'E');
hmmio = mmioOpen( name, &mmioInfo, MMIO_READ | MMIO_DENYNONE );
if( !hmmio )
{
fprintf( stderr, "[%s] Failed to open a wave file!!!\n", name );
return;
}
/* Get the audio file header.
*/
mmioGetHeader( hmmio,
&mmAudioHeader,
sizeof( MMAUDIOHEADER ),
&lBytesRead,
0,
0);
memset( &cd, 0, sizeof( CBDATA ));
cd.hmmio = hmmio;
ksWanted.usDeviceIndex = 0;
ksWanted.ulType = KAIT_PLAY;
ksWanted.ulBitsPerSample = mmAudioHeader.mmXWAVHeader.WAVEHeader.usBitsPerSample;
ksWanted.ulSamplingRate = mmAudioHeader.mmXWAVHeader.WAVEHeader.ulSamplesPerSec;
ksWanted.ulDataFormat = 0;
ksWanted.ulChannels = mmAudioHeader.mmXWAVHeader.WAVEHeader.usChannels;
ksWanted.ulNumBuffers = 0;
ksWanted.ulBufferSize = 0;
ksWanted.fShareable = TRUE;
ksWanted.pfnCallBack = kaiCallback;
ksWanted.pCallBackData = &cd;
if( kaiMixerStreamOpen( m_hkm, &ksWanted, &ksObtained, &hkms ))
{
fprintf( stderr, "[%s] Failed to open a mixer stream!!!\n", name );
goto exit_mmio_close;
}
printf("[%s] hkms = %lx\n", name, hkms );
printf("[%s] Number of channels = %lu\n", name, ksObtained.ulChannels );
printf("[%s] Number of buffers = %lu\n", name, ksObtained.ulNumBuffers );
printf("[%s] Buffer size = %lu bytes\n", name, ksObtained.ulBufferSize );
printf("[%s] Silence = %02x\n", name, ksObtained.bSilence );
kaiSetVolume( hkms, MCI_SET_AUDIO_ALL, 50 );
kaiSetSoundState( hkms, MCI_SET_AUDIO_ALL, TRUE );
printf("[%s] Trying to play...\n", name );
//DosSetPriority( PRTYS_THREAD, PRTYC_TIMECRITICAL, PRTYD_MAXIMUM, 0 );
while( 1 )
{
switch( kaiPlay( hkms ))
{
case KAIE_NO_ERROR :
break;
#if 0
// Wait for instance to be active
case KAIE_NOT_READY :
DosSleep( 1 );
continue;
#endif
default :
fprintf( stderr, "[%s] Failed to play!!!\n", name );
goto exit_kai_close;
}
break;
}
//DosSetPriority( PRTYS_THREAD, PRTYC_REGULAR, 0, 0 );
printf("[%s] Playing...\n", name );
while( !m_fQuit && !( kaiStatus( hkms ) & KAIS_COMPLETED ))
DosSleep( 1 );
printf("[%s] Completed\n", name );
exit_kai_close :
if( kaiMixerStreamClose( m_hkm, hkms ))
fprintf( stderr, "Failed to close a mixer stream!!!\n");
exit_mmio_close :
mmioClose( hmmio, 0 );
}
static int play( const char *name )
{
return _beginthread( playThread, NULL, 256 * 1024, ( void * )name );
}
static void showCardInfo( void )
{
KAICAPS caps;
int cards = kaiGetCardCount();
int i;
printf("Installed audio cards = %d\n", cards );
for( i = 1; i <= cards; i++ )
{
kaiCapsEx( i, &caps );
printf("Audio card %d: Channles = %ld, Card name = %s\n",
i, caps.ulMaxChannels, caps.szPDDName );
}
}
int main( int argc, char *argv[])
{
int key;
ULONG ulMode;
const char *modeName[] = {"DART", "UNIAUD"};
KAICAPS kaic;
KAISPEC ksWanted, ksObtained;
TID tid1, tid2, tid3;
ulMode = ( argc < 2 ) ? KAIM_AUTO : atoi( argv[ 1 ]);
if( kaiInit( ulMode ))
{
fprintf( stderr, "Failed to init kai\n");
return 1;
}
showCardInfo();
kaiCaps( &kaic );
printf("Mode = %s, Available channels = %ld, PDD Name = %s\n",
modeName[ kaic.ulMode - 1 ], kaic.ulMaxChannels, kaic.szPDDName );
printf("Press ESC to quit\n");
ksWanted.usDeviceIndex = 0;
ksWanted.ulType = KAIT_PLAY;
ksWanted.ulBitsPerSample = 16;
ksWanted.ulSamplingRate = 44100;
ksWanted.ulDataFormat = 0;
ksWanted.ulChannels = 2;
ksWanted.ulNumBuffers = 2;
ksWanted.ulBufferSize = 512 * 2/* 16bits */ * 2/* channels */;
ksWanted.fShareable = TRUE;
ksWanted.pfnCallBack = NULL;
ksWanted.pCallBackData = NULL;
if( kaiMixerOpen( &ksWanted, &ksObtained, &m_hkm ))
{
fprintf( stderr, "Failed to open a mixer!!!\n");
return 1;
}
m_fQuit = FALSE;
tid1 = play("demo1.wav");
tid2 = play("demo2.wav");
tid3 = play("demo3.wav");
while( !m_fQuit & !( kaiStatus( m_hkm ) & KAIS_COMPLETED ))
{
key = read_key();
if( key == 27 ) /* ESC */
m_fQuit = TRUE;
DosSleep( 1 );
}
DosWaitThread( &tid1, DCWW_WAIT );
DosWaitThread( &tid2, DCWW_WAIT );
DosWaitThread( &tid3, DCWW_WAIT );
if( kaiMixerClose( m_hkm ))
fprintf( stderr, "Failed to close a mixer!!!\n");
kaiDone();
return 0;
}