-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplayvgm.c
592 lines (503 loc) · 18 KB
/
playvgm.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
/* -------------------------------------------------------------------------- */
/* */
/* FM synthesizer low-level interface demo program. */
/* Copyright (c) 1994 Creative Labs, Inc. */
/* */
/* THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY */
/* KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE */
/* IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR */
/* PURPOSE. */
/* */
/* You have a royalty-free right to use, modify, reproduce and */
/* distribute the Sample Files (and/or any modified version) in */
/* any way you find useful, provided that you agree that */
/* Creative has no warranty obligations or liability for any Sample Files. */
/* */
/*----------------------------------------------------------------------------*/
/*
* This Program is written using Borland C++ Ver 3.1.
* To Compile : BCC FMSAMPLE.C
* ---------------------------------------------------------------------
*
* This program is not intended to explain all the aspects of FM sound
* generation on Sound Blaster cards. The chips are too complicated for
* that. This program is just to demonstrate how to generate a tone and
* control the left and right channels. For more information on the FM
* synthesizer chip, contact Yamaha.
*
* Here's a brief description of FM: Each sound is created by two operator
* cells (called "slots" in the Yamaha documentation), a modulator and a
* carrier. When FM synthesis was invented, the output value of the
* modulator affected the frequency of the carrier. In the Yamaha chips, the
* modulator output actually affects the phase of the carrier instead of
* frequency, but this has a similar effect.
*
* Normally the modulator and carrier would probably be connected in series
* for complex sounds. For this program, I wanted a pure sine wave, so I
* connected them in parallel and turned the modulator output down all the
* way and used just the carrier.
*
* Sound Blaster 1.0 - 2.0 cards have one OPL-2 FM synthesis chip at
* addresses 2x8 and 2x9 (base + 8 and base + 9). Sound Blaster Pro version
* 1 cards (CT-1330) achieve stereo FM with two OPL-2 chips, one for each
* speaker. The left channel FM chip is at addresses 2x0 and 2x1. The right
* is at 2x2 and 2x3. Addresses 2x8 and 2x9 address both chips
* simultaneously, thus maintaining compatibility with the monaural Sound
* Blaster cards. The OPL-2 contains 18 operator cells which make up the
* nine 2-operator channels. Since the CT-1330 SB Pro has two OPL-2 chips,
* it is therefore capable of generating 9 voices in each speaker.
*
* Sound Blaster Pro version 2 (CT-1600) and Sound Blaster 16 cards have one
* OPL-3 stereo FM chip at addresses 2x0 - 2x3. The OPL-3 is separated into
* two "banks." Ports 2x0 and 2x1 control bank 0, while 2x2 and 2x3 control
* bank 1. Each bank can generate nine 2-operator voices. However, when the
* OPL-3 is reset, it switches into OPL-2 mode. It must be put into OPL-3
* mode to use the voices in bank 1 or the stereo features. For stereo
* control, each channel may be sent to the left, the right, or both
* speakers, controlled by two bits in registers C0H - C8H.
*
* The FM chips are controlled through a set of registers. The following
* table shows how operator cells and channels are related, and the register
* offsets to use.
*
* FUNCTION MODULATOR- -CARRIER-- MODULATOR- -CARRIER-- MODULATOR- -CARRIER--
* OP CELL 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
* CHANNEL 1 2 3 1 2 3 4 5 6 4 5 6 7 8 9 7 8 9
* OFFSET 00 01 02 03 04 05 08 09 0A 0B 0C 0D 10 11 12 13 14 15
*
* An example will make the use of this table clearer: suppose you want to
* set the attenuation of both of the operators of channel 4. The KSL/TOTAL LEVEL
* registers (which set the attenuation) are 40H - 55H. The modulator for
* channel 4 is op cell 7, and the carrier for channel 4 is op cell 10. The
* offsets for the modulator and carrier cells are 08H and 0BH, respectively.
* Therefore, to set the attenuation of the modulator, you would output a
* value to register 40H + 08H == 48H, and to set the carrier's attenuation,
* you would output to register 40H + 0BH == 4BH.
*
* In this program, I used just channel 1, so the registers I used were 20H,
* 40H, 60H, etc., and 23H, 43H, 63H, etc.
*
* The frequencies of each channel are controlled with a frequency number and
* a multiplier. The modulator and carrier of a channel both get the same
* frequency number, but they may be given different multipliers. Frequency
* numbers are programmed in registers A0H - A8H (low 8 bits) and B0H - B8H
* (high 2 bits). Those registers control entire channels (2 operators), not
* individual operator cells. To turn a note on, the key-on bit in the
* appropriate channel register is set. Since these registers deal with
* channels, you do not use the offsets listed in the table above. Instead,
* add (channel-1) to A0H or B0H. For example, to turn channel 1 on,
* program the frequency number in registers A0H and B0H, and set the key-on
* bit to 1 in register B0H. For channel 3, use registers A2H and B2H.
*
* Bits 2 - 4 in registers B0H - B8H are the block (octave) number for the
* channel.
*
* Multipliers for each operator cell are programmed through registers 20H -
* 35H. The table below shows what multiple number to program into the
* register to get the desired multiplier. The multiple number goes into
* bits 0 - 3 in the register. Note that it's a bit strange at the end.
*
* multiple number multiplier multiple number multiplier
* 0 1/2 8 8
* 1 1 9 9
* 2 2 10 10
* 3 3 11 10
* 4 4 12 12
* 5 5 13 12
* 6 6 14 15
* 7 7 15 15
*
* This equation shows how to calculate the required frequency number (to
* program into registers A0H - A8H and B0H - B8H) to get the desired
* frequency:
* fn=(long)f * 1048576 / b / m /50000L
* where f is the frequency in Hz,
* b is the block (octave) number between 0 and 7 inclusive, and
* m is the multiple number between 0 and 15 inclusive.
*
*/
#define STEREO // Define this for SBPro CT-1330 or later card.
#define OPL3 // Also define this for SBPro CT-1600 or later card.
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <ctype.h>
#include <dos.h>
#include <dir.h>
#define KEYON 0x20 /* key-on bit in regs b0 - b8 */
/* These are offsets from the base I/O address. */
#define FM 8 /* SB (mono) ports (e.g. 228H and 229H) */
#define PROFM1 0 /* On CT-1330, this is left OPL-2. On CT-1600 and */
/* later cards, it's OPL-3 bank 0. */
#define PROFM2 2 /* On CT-1330, this is right OPL-2. On CT-1600 and */
/* later cards, it's OPL-3 bank 1. */
#ifdef OPL3
#define LEFT 0x10
#define RIGHT 0x20
#endif
#define KEY_UP 72
#define KEY_DOWN 80
#define ESC 0x1B
#define VGM_HEADER_LEN (0x80)
#define SAMPLE_RATE (44100)
unsigned IOport; /* Sound Blaster port address */
void mydelay(unsigned long clocks)
/*
* "clocks" is clock pulses (at 1.193180 MHz) to elapse, but remember that
* normally the system timer runs in mode 3, in which it counts down by twos,
* so delay3(1193180) will only delay half a second.
*
* clocks = time * 2386360
*
* time = clocks / 2386360
*/
{
unsigned long elapsed=0;
unsigned int last,next,ncopy,diff;
/* Read the counter value. */
outp(0x43,0); /* want to read timer 0 */
last=inp(0x40); /* low byte */
last=~((inp(0x40)<< 8) + last); /* high byte */
do {
/* Read the counter value. */
outp(0x43,0); /* want to read timer 0 */
next=inp(0x40); /* low byte */
ncopy=next=~((inp(0x40)<< 8) + next); /* high byte */
next-=last; /* this is now number of elapsed clock pulses since last read */
elapsed += next; /* add to total elapsed clock pulses */
last=ncopy;
} while (elapsed<clocks);
}
int base16(char **str, unsigned *val)
/* Takes a double pointer to a string, interprets the characters as a
* base-16 number, and advances the pointer.
* Returns 0 if successful, 1 if not.
*/
{
char c;
int digit;
*val = 0;
while ( **str != ' ') {
c = toupper(**str);
if (c >= '0' && c <= '9')
digit = c - '0';
else if (c >= 'A' && c <= 'F')
digit = c - 'A' + 10;
else
return 1; /* error in string */
*val = *val * 16 + digit;
(*str)++;
}
return 0;
}
int base10(char **str, unsigned *val)
/* Takes a double pointer to a string, interprets the characters as a
* base-10 number, and advances the pointer.
* Returns 0 if successful, 1 if not.
*/
{
char c;
int digit;
*val = 0;
while ( **str != ' ') {
c = toupper(**str);
if (c >= '0' && c <= '9')
digit = c - '0';
else
return 1; /* error in string */
*val = *val * 10 + digit;
(*str)++;
}
return 0;
}
unsigned ReadBlasterEnv(unsigned *port, unsigned *irq, unsigned *dma8,
unsigned *dma16)
/* Gets the Blaster environment statement and stores the values in the
* variables whose addresses were passed to it.
* Returns:
* 0 if successful
* 1 if there was an error reading the port address.
* 2 if there was an error reading the IRQ number.
* 3 if there was an error reading the 8-bit DMA channel.
* 4 if there was an error reading the 16-bit DMA channel.
*/
{
char *env;
unsigned val;
int digit;
env = getenv("BLASTER");
while (*env) {
switch(toupper( *(env++) )) {
case 'A':
if (base16(&env, port)) /*interpret port value as hex */
return 1; /* error*/
break;
case 'I':
if (base10(&env, irq)) /* interpret IRQ as decimal*/
return 2; /* error*/
break;
case 'D':
if (base10(&env, dma8)) /* 8-bit DMA channel is decimal*/
return 3;
break;
case 'H':
if (base10(&env, dma16)) /* 16-bit DMA channel is decimal*/
return 4;
break;
default:
break;
}
}
return 0;
}
void FMoutput(unsigned port, int reg, int val)
/* This outputs a value to a specified FM register at a specified FM port. */
{
outp(port, reg);
mydelay(8); /* need to wait 3.3 microsec */
outp(port+1, val);
mydelay(55); /* need to wait 23 microsec */
}
void Sleep(int time){
int i=0;
for (i=0;i<43;i++)
mydelay(time*50);
}
void fm(int reg, int val)
/* This function outputs a value to a specified FM register at the Sound
* Blaster (mono) port address.
*/
{
FMoutput(IOport+FM, reg, val);
}
void Profm1(int reg, int val)
/* This function outputs a value to a specified FM register at the Sound
* Blaster Pro left FM port address (or OPL-3 bank 0).
*/
{
FMoutput(IOport+PROFM1, reg, val);
}
void Profm2(int reg, int val)
/* This function outputs a value to a specified FM register at the Sound
* Blaster Pro right FM port address (or OPL-3 bank 1).
*/
{
FMoutput(IOport+PROFM2, reg, val);
}
float speed = SAMPLE_RATE/900.0f;
unsigned long ld_int(unsigned char * p){
return *(unsigned long *)p;
}
void OPL3write(char * raw){
int show=0;
if (show) printf("%02X %02X %02X \n",raw[0]&0xff,raw[1]&0xff,raw[2]&0xff);
if(raw[0]){
Profm2(raw[1],raw[2]);
}else{
fm(raw[1],raw[2]);
}
}
void OPL3off()
{
char reset[]={0x5f,0x05,0x00};
OPL3write(reset);
reset[0]=0x5e;
reset[1]=0x01;
OPL3write(reset);
reset[1]=0x08;
OPL3write(reset);
Sleep(150);
}
void reset()
{
int add;
char reset[]={0x5f,0x05,0x01};
OPL3write(reset);
reset[1]=0x04;
reset[2]=0x00;
OPL3write(reset);
reset[2]=0x00;
for(add=0;add<=0xff;add++){
reset[1]=add;
reset[0]=0x5e;
OPL3write(reset);
reset[0]=0x5f;
OPL3write(reset);
}
Sleep(500);
OPL3off();
}
void nop(){
int i=0;
for(;i<1000;i++);
}
void locateCursor(const int row, const int col){
printf("%c[%d;%dH",27,row,col);
}
void playFile(const char * vgmfile){
int wait=0;
char op[3]={0};
char key=0;
char started=0;
int count=0;
char vgm_header[VGM_HEADER_LEN]={0};
FILE * vgm=fopen(vgmfile,"rb");
printf("playing: %s \n",vgmfile);
printf("speed:%f\n",speed);
wait=0;
if(vgm!=NULL){
fread(vgm_header,VGM_HEADER_LEN,1,vgm);
printf("data offset:%02X\n",(vgm_header[0x34]&0xff)+0x34);
printf("OPL2:%lu Hz \n",ld_int(&vgm_header[0x50]));
reset();
if(ld_int(&vgm_header[0x50])) OPL3off();
printf("OPL3:%lu Hz \n",vgm_header[0x5c]+(vgm_header[0x5d]<<8)+((vgm_header[0x5e]&0xff)<<16));
fseek(vgm,(vgm_header[0x34]&0xff)+0x34,SEEK_SET);
printf("Up/Down : speed ... Esc: Stop \n");
printf("Modified by zhblue \n");
started=0;
while(fread(op,3,1,vgm)==1){
if(kbhit()){
key = getch();
if(key==KEY_UP) speed+=1;
if(key==KEY_DOWN) speed-=1;
if(key==ESC) break;
key=0;
printf("\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b speed:%f",speed);
}
if(op[0]==0x5e || op[0]==0x5f || op[0]==0x5A ){
OPL3write(op);
count++;
started=1;
}else{
switch (op[0]){
case 0x61:
wait=(op[1]&0xff)+((op[2]&0xff)*256);
break;
case 0x62:
wait=735;
fseek(vgm,-2,SEEK_CUR);
break;
case 0x63:
wait=882;
fseek(vgm,-2,SEEK_CUR);
break;
case 0x66:
wait=-1;
break;
case 0x70:
case 0x71:
case 0x72:
case 0x73:
case 0x74:
case 0x75:
case 0x76:
case 0x77:
case 0x78:
case 0x79:
case 0x7a:
case 0x7b:
case 0x7c:
case 0x7d:
case 0x7e:
case 0x7f:
wait=1+(0xff&op[0]-0x70);
fseek(vgm,-2,SEEK_CUR);
break;
default: wait=0;
}
if (started && wait>=speed) {
Sleep(wait/speed);
}
}
if(wait<0) break;
}
}
fclose(vgm);
}
int isVgmFile(const char fname[])
{
int l = strlen(fname);
if (l <= 4 || stricmp(fname + l - 4, ".vgm") != 0)
return 0;
else
return l - 3;
}
int lastIndexOf(char * orig,char * target){
int ret=-1;
int i;
for(i=strlen(orig);i>0;i--){
ret=strstr(orig+i,target);
if(ret>=0) return ret;
}
return -1;
}
int playdir(int n,char ** argv)
{
char dir[1024]=".";
char cmd[1024];
char todo[1024]="sbvgm -x -noscan -opl2lpt378";
int tp=0,i=0;
struct ffblk vgms;
int next=0;
next=findfirst(dir,&vgms,0);
if(next){
sscanf(argv[1],"%s",dir);
sprintf(dir+strlen(dir),"\\*.vgm");
printf("scan files : %s\n",dir);
next=findfirst(dir,&vgms,0);
}
while(!next){
printf("do task for %s ... \n",vgms.ff_name);
printf("Press twice ESC to QUIT ...\n");
if(argv[1][0]=='*' || (argv[1][0]=='.'&& argv[1][1]=='\\'))
sprintf(cmd,"%s",vgms.ff_name);
else
sprintf(cmd,"%s\\%s",argv[1],vgms.ff_name);
printf("%s\n",cmd);
playFile(cmd);
sleep(1);
if(kbhit()){
if(getch()==27){
printf("Double Esc pressed ... Exit !\n");
break;
}
}
next=findnext(&vgms);
}
reset();
printf("OPL3 usb serial drive test. \n");
return 0;
}
void main(int argn,char * argv[])
{
int i,val1,val2;
int block,b,m,f,fn;
unsigned dummy;
clrscr();
ReadBlasterEnv(&IOport, &dummy, &dummy, &dummy);
#ifdef STEREO
#ifdef OPL3
printf("Program compiled for Sound Blaster Pro ver. 2 (CT-1600) and SB16 cards.\n");
#else
printf("Program compiled for Sound Blaster Pro ver. 1 (CT-1330) cards.\n");
#endif
#else
printf("Program compiled for Sound Blaster 1.0 - 2.0 cards (monaural).\n");
#endif
fm(1,0); /* must initialize this to zero */
#ifdef OPL3
Profm2(5, 1); /* set to OPL3 mode, necessary for stereo */
fm(0xC0,LEFT | RIGHT | 1); /* set both channels, parallel connection */
#else
fm(0xC0, 1); /* parallel connection */
#endif
if(argn>1){
if(isVgmFile(argv[1])) playFile(argv[1]);
else playdir(argn,argv);
}else{
argn=2;
argv[1]=".";
playdir(argn,argv);
}
}