-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathkmidi.c
208 lines (162 loc) · 4.59 KB
/
kmidi.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
/****************************************************************************
**
** K MIDI - Simple midi player
**
** Copyright (C) 2018 by KO Myung-Hun <[email protected]>
**
** This file is part of K MIDI DECoder.
**
** $BEGIN_LICENSE$
**
** GNU General Public License Usage
** This file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
** $END_LICENSE$
**
****************************************************************************/
/** @file kmidi.c */
#include <os2.h>
#define INCL_OS2MM
#include <os2me.h>
#include <stdio.h>
#include <stdlib.h>
#include <kai.h>
#include "kmididec.h"
#define SAMPLE_RATE 44100
#define SAMPLES 2048
#define SAMPLE_SIZE ( 2/* 16bits */ * 2/* 2 CH */ )
#define USE_FLOAT 1
/* callback for KAI */
static ULONG APIENTRY kaiCallback( PVOID pCBData,
PVOID pBuffer, ULONG ulBufferSize )
{
#if USE_FLOAT
float buf[ ulBufferSize / sizeof( short )];
return kaiFloatToS16( pBuffer, ulBufferSize,
buf, kmdecDecode( pCBData, buf, sizeof( buf )));
#else
return kmdecDecode( pCBData, pBuffer, ulBufferSize );
#endif
}
/* convert ms to time */
static void msToTime( int ms, int *h, int *m, int *s, int *hund )
{
int sec = ms / 1000;
int hd = ( ms % 1000 ) / 10;
int min = sec / 60;
sec %= 60;
int hour = min / 60;
min %= 60;
if( h )
*h = hour;
if( m )
*m = min;
if( s )
*s = sec;
if( hund )
*hund = hd;
}
int main( int argc, char *argv[])
{
PKMDEC dec;
KMDECAUDIOINFO audioInfo =
{
#if USE_FLOAT
.bps = KMDEC_BPS_FLOAT,
#else
.bps = KMDEC_BPS_S16,
#endif
.channels = 2,
.sampleRate = SAMPLE_RATE
};
KAISPEC ksWanted, ksObtained;
HKAI hkai;
int rc = 1;
if( argc < 3 )
{
fprintf( stderr, "Usage : kmidi MIDI-file sound-font-file\n");
return rc;
}
dec = kmdecOpen( argv[ 1 ], argv[ 2 ], &audioInfo );
if( !dec )
{
fprintf( stderr, "Failed to init kmdec\n");
return rc;
}
if( kaiInit( KAIM_AUTO ))
{
fprintf( stderr, "Failed to init kai\n");
goto exit_kmdec_close;
}
ksWanted.usDeviceIndex = 0;
ksWanted.ulType = KAIT_PLAY;
ksWanted.ulBitsPerSample = BPS_16;
ksWanted.ulSamplingRate = audioInfo.sampleRate;
ksWanted.ulDataFormat = 0;
ksWanted.ulChannels = audioInfo.channels;
ksWanted.ulNumBuffers = 2;
ksWanted.ulBufferSize = SAMPLES * SAMPLE_SIZE;
ksWanted.fShareable = TRUE;
ksWanted.pfnCallBack = kaiCallback;
ksWanted.pCallBackData = dec;
if( kaiOpen( &ksWanted, &ksObtained, &hkai ))
{
fprintf( stderr, "Failed to open audio device!!!\n");
goto exit_kai_done;
}
int H, M, S, HUND;
int h, m, s, hund;
msToTime( kmdecGetDuration( dec ), &H, &M, &S, &HUND );
kaiPlay( hkai );
printf("ESC = quit, q = stop, w = play, e = pause, r = resume, "
"a = -5s, s = +5s\n");
while( kaiStatus( hkai ) != KAIS_COMPLETED )
{
msToTime( kmdecGetPosition( dec ), &h, &m, &s, &hund );
printf("Playing time: %02d:%02d:%02d.%02d of %02d:%02d:%02d.%02d\r",
h, m, s, hund, H, M, S, HUND );
int key = _read_kbd( 0, 0, 1 );
if( key == 0 )
key = _read_kbd( 0, 1, 1 );
if( key == 27 ) /* ESC */
break;
switch( key )
{
case 'q':
kaiStop( hkai );
break;
case 'w':
kaiPlay( hkai );
break;
case 'e':
kaiPause( hkai );
break;
case 'r':
kaiResume( hkai );
break;
case 'a':
case 's':
kaiStop( hkai );
kmdecSeek( dec, key == 'a' ? -5000 : 5000, KMDEC_SEEK_CUR );
kaiPlay( hkai );
/* consume already pressed keys */
while( _read_kbd( 0, 0, 1 ) != -1 )
/* nothing */;
break;
}
_sleep2( 1 );
}
printf("\n");
kaiClose( hkai );
rc = 0;
exit_kai_done :
kaiDone();
exit_kmdec_close:
kmdecClose( dec );
return rc;
}