-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathJP.C
733 lines (667 loc) · 21.3 KB
/
JP.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
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
/*
* Judas Player. This program plays MODs, XMs and S3Ms and shows how to set up
* the signal handlers to ensure proper JUDAS shutdown even in the case of user
* break.
*
* It uses the timer routines in TIMER.C & TIMERASM.ASM. These don't
* "officially" belong to JUDAS but use them if you need them!
*
* Note: New features of Judas are automagically included in the new versions
* of JP, so they are not documented here.
* V2.04 Corrected special keys leaving shit to keyboard buffer -bug.
* V2.06 Rewrote a lot of things...
* V2.07 Support for 48000 Hz sample rate (AC97 only).
* V2.08 Now should really work with all ICH4 AC97 integrated codecs.
* V2.09 SIS7012 should now work too
* V2.09b ICH4+ support corrected
* V2.09c dev version
* V2.09d corrected dev version with full WATCOM C++ & DJGPP support
* V2.09e all ICH4+ AC97 devices now use memory mapped IO if available
* V2.09f WAV library builder added to JUDAS
* V2.10a HDA codecs support added
* V2.10b judas_setwavlib function added
* V2.10d HDA codecs support update for intel 5, 6, 7 series test by RayeR
*/
#include <stdlib.h>
#include <stdio.h>
#include <io.h>
#include <fcntl.h>
#include <signal.h>
#include <conio.h>
#include <ctype.h>
#include <process.h>
#include <string.h>
#include <math.h>
#ifdef __DJGPP__
#include <unistd.h>
#include <go32.h>
#include <dpmi.h>
#include <sys/nearptr.h>
/* declare flushall */
#include <unistd.h>
void flushall (void)
{
/* Simulate `flushall' by only flushing standard streams */
fflush (stdout);
fsync (fileno (stdout));
fflush (stderr);
fsync (fileno (stderr));
}
#else
/* WATCOM C++ graphics library & define djgpp base as absolute zero */
#define __djgpp_conventional_base 0
#include <graph.h>
#endif
#include "judas.h"
#include "timer.h"
/* For direct video memory access. (Does this work at all with PMODE?) */
#define SCREEN_AREA 0xb800
#define SCREEN_LIN_ADDR ((SCREEN_AREA) << 4)
#define SCREEN_SIZE 80*25
#define VUMETERBOTTOM 17
#define VUMETERXSIZE 80
#define VUMETERLEFT 0
#define VUMETERTOP 0
#define VUMETERLINES (VUMETERBOTTOM-VUMETERTOP+1)
/* only for testing purposes */
extern long hda_getbufpos (void);
/* Functions */
void buildmainscreen(void);
void updatemainscreen(void);
int finishfilename(char *filename);
void wavizefilename(char *filename);
int main(int argc, char **argv);
static void handle_int(int a);
/* Variables */
int filewriter = 0;
int mixer = QUALITYMIXER;
int filetype = 0;
int interpolation = 1;
int songrounds = 1;
int channels = 80;
int vumeterwidth = 1;
int vumeterson = 1;
int mainscreenon = 1;
char filename[254];
int mastervolume = 50;
int oldmastervolume;
char oldjudas_clipped;
const char *const extensiontable[] =
{
".xm",
".mod",
".s3m",
NULL
};
/*
* Bright and dim versions of the 16 textmode colors.
*/
const char hilite[] = { 0,9,10,11,12,13,14,15,7,11,14,15,14,15,15,15 };
const char lolite[] = { 0,1,2,8,4,5,8,8,8,1,2,3,4,5,6,7 };
#ifdef __WATCOMC__
void jdsgotoxy(char x, char y);
/*
* Moves cursor to selected position. (Does this work at all with PMODE?)
*/
extern void jdsgotoxy(char x, char y);
#pragma aux jdsgotoxy = \
"push bp" \
"xor bh, bh" \
"mov ah,2" \
"int 10h" \
"pop bp" \
parm [dl] [dh] \
modify [ah bh];
#else
void jdsgotoxy(char x, char y)
{
gotoxy(x+1, y+1);
}
#endif
/*
* Builds the player screen.
*/
void buildmainscreen(void)
{
#ifdef __DJGPP__
/* use conio.h for DJGPP */
textmode(C80);
#else
/* WATCOM C++ */
_setvideomode(_TEXTC80);
#endif
jdsgotoxy(0,VUMETERBOTTOM+1);
printf("ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ JUDAS V2.10d player ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ"
"[ESC] Quit, [D] DOS shell, [<Ä>] Rewind/forward, [+-] Mastervol:%3d\n", mastervolume);
if (!filewriter) {
printf("%s, port %X IRQ %d DMA %d\n",
judas_devname[judas_device], judas_port, judas_irq, judas_dma);
} else {
printf("Writing WAV: %s\n", filename);
}
printf("%s %d Hz, %s, %s IP\n",
judas_mixmodename[judas_mixmode], judas_mixrate,
judas_mixername[mixer], judas_ipmodename[interpolation]);
switch(filetype)
{
case 1:
channels = judas_getxmchannels();
printf("XM name: %s\n", judas_getxmname());
printf("Channels: %d\n", channels);
break;
case 2:
channels = judas_getmodchannels();
printf("MOD name: %s\n", judas_getmodname());
printf("Channels: %d\n", channels);
break;
case 3:
channels = judas_gets3mchannels();
printf("S3M name: %s\n", judas_gets3mname());
printf("Channels: %d\n", channels);
break;
}
vumeterwidth = VUMETERXSIZE/channels;
}
/*
* Updates the player screen.
*/
void updatemainscreen(void)
{
if (oldmastervolume != mastervolume) {
jdsgotoxy(64,VUMETERBOTTOM+2);
printf("%3d",mastervolume);
oldmastervolume = mastervolume;
}
flushall();
if (oldjudas_clipped != judas_clipped) {
jdsgotoxy(68, VUMETERBOTTOM+2);
if (judas_clipped) printf("Clipped:%3d%%", 1+(int)400*judas_clipped/256); else printf(" ");
oldjudas_clipped = judas_clipped;
}
flushall();
/*
jdsgotoxy(0,24);
// only for testing purposes
hda_getbufpos();
flushall();
*/
jdsgotoxy(0,24);
switch(filetype)
{
case 1:
printf("Pos:%3d Line:%3d Tick:%3d", judas_getxmpos(), judas_getxmline(), judas_getxmtick());
break;
case 2:
printf("Pos:%3d Line:%3d Tick:%3d", judas_getmodpos(), judas_getmodline(), judas_getmodtick());
break;
case 3:
printf("Pos:%3d Line:%3d Tick:%3d", judas_gets3mpos(), judas_gets3mline(), judas_gets3mtick());
break;
}
flushall();
/* Draw vumeters... */
if (vumeterson) {
short *screenmem = (short *)((unsigned char *)SCREEN_LIN_ADDR + __djgpp_conventional_base);
int count;
for (count = 0; count < channels; count++)
{
short *ptr = screenmem + 80*VUMETERTOP + count*vumeterwidth;
float floatvu = sqrt(judas_getvumeter(count));
char color = (((unsigned int)judas_channel[count].end / 15) % 15) + 1;
int halves = floatvu *(VUMETERLINES*2+1);
int zeros;
int vu = halves/2;
halves -= vu*2;
zeros = VUMETERLINES-vu-halves;
while (zeros)
{
int fuck = vumeterwidth;
while (fuck)
{
*ptr = 0;
ptr++;
fuck--;
}
ptr += (80-vumeterwidth);
zeros--;
}
if (halves)
{
int fuck = vumeterwidth-1;
if (fuck) {
*ptr = 220+256*hilite[(unsigned char)color];
ptr++;
fuck--;
}
while (fuck)
{
*ptr = 220+256*color;
ptr++;
fuck--;
}
*ptr = 220+256*lolite[(unsigned char)color];
ptr++;
ptr += (80-vumeterwidth);
}
while (vu)
{
int fuck = vumeterwidth-1;
if (fuck) {
*ptr = 219+256*hilite[(unsigned char)color];
ptr++;
fuck--;
}
while (fuck)
{
*ptr = 219+256*color;
ptr++;
fuck--;
}
*ptr = 219+256*lolite[(unsigned char)color];
ptr++;
ptr += (80-vumeterwidth);
vu--;
}
}
}
}
/*
* Tries some extensions. Returns 1 if finds the file, otherwise 0.
*/
int finishfilename(char *filename)
{
/* Maybe the filename is already right... If, return 1. */
{
int handle = judas_open(filename);
if (handle != -1)
{
judas_close(handle);
return 1;
}
}
{
int pos = strlen(filename)-1;
int pointpos = pos+1;
/* Is there already an extension in the filename? */
{
while (filename[pos] != ':' &&
filename[pos] != '\\' &&
filename[pos] != '/' &&
pos >= 0)
{
if (filename[pos] == '.')
{
/* No match! */
return 0;
}
pos--;
}
/* Try with different extensions */
{
int count;
for (count = 0; extensiontable[count]; count++) {
int handle;
strcat(filename, extensiontable[count]);
handle = judas_open(filename);
if (handle != -1) {
judas_close(handle);
return 1;
}
filename[pointpos] = 0;
}
/* No match! */
return 0;
}
}
}
}
/*
* Removes path from the filename and changes extension to ".wav".
*/
void wavizefilename(char *filename)
{
int pos = strlen(filename)-1;
while (filename[pos] != ':' &&
filename[pos] != '\\' &&
filename[pos] != '/' &&
pos >= 0)
{
if (filename[pos] == '.')
{
filename[pos] = 0;
goto FOUNDEXTPOS;
}
pos--;
}
pos++;
if (pos == strlen(filename)-1) {
strcpy(filename, "output.wav");
return;
}
FOUNDEXTPOS:
strcat(filename, ".wav");
pos = strlen(filename)-1;
while (filename[pos] != ':' &&
filename[pos] != '\\' &&
filename[pos] != '/' &&
pos >= 0)
{
pos--;
}
strcpy(filename, &filename[pos+1]);
}
/*
* The player skeleton!
*/
int main(int argc, char **argv)
{
int mixrate = 48000;
int mixmode = SIXTEENBIT | STEREO;
int wavhandle = 0;
/* Set signal handlers */
signal(SIGINT, handle_int);
#if defined(SIGBREAK)
signal(SIGBREAK, handle_int);
#endif
#ifdef __DJGPP__
if (__djgpp_nearptr_enable() == 0) {
printf("ERROR: Couldn't enable near pointers for DJGPP!\n");
return 1;
}
/* Trick: Avoid re-setting DS selector limit on each memory allocation call */
__djgpp_selector_limit = 0xffffffff;
#endif
/* Print some shit */
printf("ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ\n"
"JUDAS V2.10d player - plays XMs+MODs+S3Ms\n"
"ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ\n");
/* Check we have enough commandline parameters */
if (argc == 1)
{
printf("Voluntary stuff marked with <>\n"
"\n"
"Usage: jp musicfile <options>\n"
"Options: -rxxx Mixrate, in Hz\n"
" -vxxx Mastervolume, 0-255, default: 50\n"
" -m Output mono\n"
" -8 Output 8bit\n"
" -i Simpler interpolation\n"
" -f Fast mixer, default: Quality mixer\n"
" -l<xxx> Loop song, play song xxx times, default: once.\n"
" If no xxx given or xxx zero, will play forever.\n"
" -w WAV writer, outputs audio into a WAV file named\n"
" after the musicfile, in the current directory.\n"
" -d Disable player screen\n"
" -dv Disable vumeters\n");
return 1;
}
/* Parse options */
if (argc > 2)
{
int count = 2;
while (count < argc)
{
char *ptr = argv[count];
if ((ptr[0] == '-') || (ptr[0] == '/'))
{
switch(tolower(ptr[1]))
{
case 'r':
sscanf(&ptr[2], "%d", &mixrate);
break;
case 'v':
sscanf(&ptr[2], "%d", &mastervolume);
if (mastervolume < 0) mastervolume = 0;
if (mastervolume > 255) mastervolume = 255;
break;
case 'm':
mixmode &= SIXTEENBIT;
break;
case '8':
mixmode &= STEREO;
break;
case 'i':
interpolation = 0;
break;
case 'f':
mixer = FASTMIXER;
break;
case 'l':
songrounds = 0;
sscanf(&ptr[2], "%d", &songrounds);
break;
case 'w':
filewriter = 1;
judascfg_device = DEV_FILE;
break;
case 'd':
if (tolower(ptr[2]) == 'v') vumeterson = 0;
if (!ptr[2]) mainscreenon = 0;
break;
}
}
count++;
}
}
/* Get and finish filename */
strcpy(filename, argv[1]);
if (!finishfilename(filename))
{
printf("ERROR: Couldn't find musicfile!\n");
return 1;
}
printf("LOADING FILE: %s\n",filename);
flushall();
/* Set uninit functions to be called at exit */
atexit(judas_uninit);
/* Autoconfigure by using enviroment */
if (!filewriter) {
judas_config();
}
/* Try to init */
if (!judas_init(mixrate, mixer, mixmode, interpolation)) goto ERROR;
/* Exit if in no sound mode */
if (judas_device == DEV_NOSOUND)
{
printf("ERROR: Sound card not detected!\n");
return 1;
}
/* Set mastervolume of channels */
judas_setmusicmastervolume(CHANNELS, mastervolume);
/*
* Try to load, first XM, then MOD, then S3M
*/
filetype = 1;
judas_loadxm(filename);
if (judas_error == JUDAS_OK) goto PLAYIT;
if (judas_error != JUDAS_WRONG_FORMAT) goto ERROR;
filetype = 2;
judas_loadmod(filename);
if (judas_error == JUDAS_OK) goto PLAYIT;
if (judas_error != JUDAS_WRONG_FORMAT) goto ERROR;
filetype = 3;
judas_loads3m(filename);
if (judas_error == JUDAS_OK) goto PLAYIT;
if (judas_error != JUDAS_WRONG_FORMAT) goto ERROR;
ERROR:
printf("JUDAS ERROR: %s\n", judas_errortext[judas_error]);
return 1;
PLAYIT:
printf("PLAYING ");
flushall();
switch(filetype)
{
case 1:
printf("XM\n");
judas_playxm(songrounds);
break;
case 2:
printf("MOD\n");
judas_playmod(songrounds);
break;
case 3:
printf("S3M\n");
judas_plays3m(songrounds);
break;
}
/* Hook timer to update sound */
if (!filewriter) {
timer_init(0x4300, judas_update);
atexit(timer_uninit);
} else {
wavizefilename(filename);
wavhandle = judas_wavwriter_open(filename);
if (wavhandle == -1) {
printf("WAV WRITER ERROR: Error creating the WAV file!");
return 1;
};
}
if (mainscreenon) {
buildmainscreen();
updatemainscreen();
} else {
printf("[ESC] Quit, [D] DOS shell, [<Ä>] Rewind/forward, [+-] Mastervol\n");
flushall();
}
for (;;)
{
char c;
while (!kbhit() && judas_songisplaying())
{
if (filewriter) {
if (judas_wavwriter_writesome(wavhandle) == -1) {
#ifdef __DJGPP__
/* use conio.h for DJGPP */
flushall();
if (mainscreenon) textmode(LASTMODE);
#else
/* WATCOM C++ */
flushall();
if (mainscreenon) _setvideomode(_DEFAULTMODE);
#endif
printf("WAV WRITER ERROR: Error writing the WAV file!");
return 1;
}
}
if (mainscreenon) updatemainscreen();
}
if (kbhit()) c = toupper(getch()); else c = 27;
switch (c) {
case 77:
{
switch(filetype) {
case 1:
judas_forwardxm();
break;
case 2:
judas_forwardmod();
break;
case 3:
judas_forwards3m();
break;
}
}
break;
case 75:
{
switch(filetype) {
case 1:
judas_rewindxm();
break;
case 2:
judas_rewindmod();
break;
case 3:
judas_rewinds3m();
break;
}
}
break;
case '+':
{
if (mastervolume < 20) mastervolume++; else mastervolume += 5;
if (mastervolume > 255) mastervolume = 255;
judas_setmusicmastervolume(CHANNELS, mastervolume);
judas_clipped = 0;
}
break;
case '-':
{
if (mastervolume < 21) mastervolume--; else mastervolume -= 5;
if (mastervolume < 0) mastervolume = 0;
judas_setmusicmastervolume(CHANNELS, mastervolume);
judas_clipped = 0;
}
break;
case 'D':
{
#ifdef __DJGPP__
/* use conio.h for DJGPP */
flushall();
if (mainscreenon) textmode(LASTMODE);
#else
/* WATCOM C++ */
flushall();
if (mainscreenon) _setvideomode(_DEFAULTMODE);
#endif
printf("Type \"exit\" to return to JP...");
#ifdef __DJGPP__
flushall();
#else
flushall();
#endif
spawnl(P_WAIT, getenv("COMSPEC"), NULL);
if (!filewriter) if (!judas_init(mixrate, mixer, mixmode, interpolation)) goto ERROR;
if (mainscreenon) buildmainscreen();
}
break;
case 27:
{
if (c == 0) getch();
switch(filetype)
{
/*
* Freeing isn't necessary but we do it just
* to make sure it doesn't crash (page fault
* will occur when freeing memory if heap has
* been corrupted by the player!)
*/
case 1:
judas_freexm();
break;
case 2:
judas_freemod();
break;
case 3:
judas_frees3m();
break;
}
goto BYEBYE;
}
break;
}
}
BYEBYE:
#ifdef __DJGPP__
/* use conio.h for DJGPP */
flushall();
if (mainscreenon) textmode(LASTMODE);
#else
/* WATCOM C++ */
flushall();
if (mainscreenon) _setvideomode(_DEFAULTMODE);
#endif
if (filewriter) {
if (judas_wavwriter_close(wavhandle) == -1) {
printf("WAV WRITER ERROR: Error finishing the WAV file!");
return 1;
};
}
printf("Bye! Thank you for using Judas Sound System :)\n");
if (judas_clipped) printf("Try playing the same song with mastervol %d to avoid clipping", (int)((float)100*mastervolume/(1+(float)400*judas_clipped/256)));
return 0;
}
static void handle_int(int a)
{
exit(0); /* Atexit functions will be called! */
}