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
|
/*
Name:
VIRTCH.C
Description:
All-c sample mixing routines, using a 32 bits mixing buffer
Portability:
All systems - all compilers
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>
#include "mikmod.h"
#define FRACBITS 11
#define FRACMASK ((1L<<FRACBITS)-1)
#define TICKLSIZE 3600
#define TICKWSIZE (TICKLSIZE*2)
#define TICKBSIZE (TICKWSIZE*2)
static SLONG VC_TICKBUF[TICKLSIZE];
#ifndef min
#define min(a,b) (((a)<(b)) ? (a) : (b))
#endif
typedef struct {
UBYTE kick; /* =1 -> sample has to be restarted */
UBYTE active; /* =1 -> sample is playing */
UWORD flags; /* 16/8 bits looping/one-shot */
SWORD handle; /* identifies the sample */
ULONG start; /* start index */
ULONG size; /* samplesize */
ULONG reppos; /* loop start */
ULONG repend; /* loop end */
ULONG frq; /* current frequency */
UBYTE vol; /* current volume */
UBYTE pan; /* current panning position */
SLONG current; /* current index in the sample */
SLONG increment; /* fixed-point increment value */
SLONG lvolmul; /* left volume multiply */
SLONG rvolmul; /* right volume multiply */
} VINFO;
static VINFO vinf[32];
static VINFO *vnf;
static UWORD samplesthatfit;
static SLONG idxsize, idxlpos, idxlend, maxvol;
static long per256;
static int ampshift;
static SLONG lvolmul, rvolmul;
static void VC_Sample32To8Copy(SLONG * srce, SBYTE * dest, ULONG count, UBYTE shift)
{
SLONG c;
int shifti = (24 - ampshift);
while (count--) {
c = *srce >> shifti;
if (c > 127)
c = 127;
else if (c < -128)
c = -128;
*dest++ = c + 128;
srce++;
}
}
static void VC_Sample32To16Copy(SLONG * srce, SWORD * dest, ULONG count, UBYTE shift)
{
SLONG c;
int shifti = (16 - ampshift);
while (count--) {
c = *srce >> shifti;
if (c > 32767)
c = 32767;
else if (c < -32768)
c = -32768;
*dest++ = c;
srce++;
}
}
static SLONG fraction2long(ULONG dividend, UWORD divisor)
/*
Converts the fraction 'dividend/divisor' into a fixed point longword.
*/
{
ULONG whole, part;
whole = dividend / divisor;
part = ((dividend % divisor) << FRACBITS) / divisor;
return ((whole << FRACBITS) | part);
}
static UWORD samples2bytes(UWORD samples)
{
if (md_mode & DMODE_16BITS)
samples <<= 1;
if (md_mode & DMODE_STEREO)
samples <<= 1;
return samples;
}
static UWORD bytes2samples(UWORD bytes)
{
if (md_mode & DMODE_16BITS)
bytes >>= 1;
if (md_mode & DMODE_STEREO)
bytes >>= 1;
return bytes;
}
/**************************************************
***************************************************
***************************************************
**************************************************/
static SBYTE *Samples[MAXSAMPLEHANDLES];
BOOL LargeRead(SBYTE * buffer, ULONG size)
{
int t;
ULONG todo;
while (size) {
/* how many bytes to load (in chunks of 8000) ? */
todo = (size > 8000) ? 8000 : size;
/* read data */
SL_Load(buffer, todo);
/* and update pointers.. */
size -= todo;
buffer += todo;
}
return 1;
}
SWORD VC_SampleLoad(FILE * fp, ULONG length, ULONG reppos, ULONG repend, UWORD flags)
{
int handle;
ULONG t;
SL_Init(fp, flags, (flags | SF_SIGNED) & ~SF_16BITS);
/* Find empty slot to put sample address in */
for (handle = 0; handle < MAXSAMPLEHANDLES; handle++) {
if (Samples[handle] == NULL)
break;
}
if (handle == MAXSAMPLEHANDLES) {
myerr = ERROR_OUT_OF_HANDLES;
return -1;
}
if ((Samples[handle] = (SBYTE *) malloc(length + 17)) == NULL) {
/* using 17 here appears to solve linux libc-5.4.7 paranoia probs */
myerr = ERROR_SAMPLE_TOO_BIG;
return -1;
}
/* read sample into buffer. */
LargeRead(Samples[handle], length);
/* Unclick samples: */
if (flags & SF_LOOP) {
if (flags & SF_BIDI)
for (t = 0; t < 16; t++)
Samples[handle][repend + t] = Samples[handle][(repend - t) - 1];
else
for (t = 0; t < 16; t++)
Samples[handle][repend + t] = Samples[handle][t + reppos];
} else {
for (t = 0; t < 16; t++)
Samples[handle][t + length] = 0;
}
return handle;
}
void VC_SampleUnload(SWORD handle)
{
void *sampleadr = Samples[handle];
free(sampleadr);
Samples[handle] = NULL;
}
/**************************************************
***************************************************
***************************************************
**************************************************/
static void (*SampleMix) (SBYTE * srce, SLONG * dest, SLONG index, SLONG increment, UWORD todo);
static void MixStereoNormal(SBYTE * srce, SLONG * dest, SLONG index, SLONG increment, UWORD todo)
{
SBYTE sample;
while (todo > 0) {
sample = srce[index >> FRACBITS];
*(dest++) += lvolmul * sample;
*(dest++) += rvolmul * sample;
index += increment;
todo--;
}
}
static void MixMonoNormal(SBYTE * srce, SLONG * dest, SLONG index, SLONG increment, UWORD todo)
{
SBYTE sample;
while (todo > 0) {
sample = srce[index >> FRACBITS];
*(dest++) += lvolmul * sample;
index += increment;
todo--;
}
}
static void MixStereoInterp(SBYTE * srce, SLONG * dest, SLONG index, SLONG increment, UWORD todo)
{
SWORD sample, a, b;
while (todo > 0) {
a = srce[index >> FRACBITS];
b = srce[1 + (index >> FRACBITS)];
sample = a + (((long) (b - a) * (index & FRACMASK)) >> FRACBITS);
*(dest++) += lvolmul * sample;
*(dest++) += rvolmul * sample;
index += increment;
todo--;
}
}
static void MixMonoInterp(SBYTE * srce, SLONG * dest, SLONG index, SLONG increment, UWORD todo)
{
SWORD sample, a, b;
while (todo > 0) {
a = srce[index >> FRACBITS];
b = srce[1 + (index >> FRACBITS)];
sample = a + (((long) (b - a) * (index & FRACMASK)) >> FRACBITS);
*(dest++) += lvolmul * sample;
index += increment;
todo--;
}
}
static UWORD NewPredict(SLONG index, SLONG end, SLONG increment, UWORD todo)
/*
This functions returns the number of resamplings we can do so that:
- it never accesses indexes bigger than index 'end'
- it doesn't do more than 'todo' resamplings
*/
{
SLONG di;
di = (end - index) / increment;
index += (di * increment);
if (increment < 0) {
while (index >= end) {
index += increment;
di++;
}
} else {
while (index <= end) {
index += increment;
di++;
}
}
return ((di < todo) ? di : todo);
}
static void VC_AddChannel(SLONG * ptr, UWORD todo)
/*
Mixes 'todo' stereo or mono samples of the current channel to the tickbuffer.
*/
{
SLONG end;
UWORD done, needs;
SBYTE *s;
while (todo > 0) {
/* update the 'current' index so the sample loops, or
stops playing if it reached the end of the sample */
if (vnf->flags & SF_REVERSE) {
/* The sample is playing in reverse */
if (vnf->flags & SF_LOOP) {
/* the sample is looping, so check if
it reached the loopstart index */
if (vnf->current < idxlpos) {
if (vnf->flags & SF_BIDI) {
/* sample is doing bidirectional loops, so 'bounce'
the current index against the idxlpos */
vnf->current = idxlpos + (idxlpos - vnf->current);
vnf->flags &= ~SF_REVERSE;
vnf->increment = -vnf->increment;
} else
/* normal backwards looping, so set the
current position to loopend index */
vnf->current = idxlend - (idxlpos - vnf->current);
}
} else {
/* the sample is not looping, so check
if it reached index 0 */
if (vnf->current < 0) {
/* playing index reached 0, so stop
playing this sample */
vnf->current = 0;
vnf->active = 0;
break;
}
}
} else {
/* The sample is playing forward */
if (vnf->flags & SF_LOOP) {
/* the sample is looping, so check if
it reached the loopend index */
if (vnf->current > idxlend) {
if (vnf->flags & SF_BIDI) {
/* sample is doing bidirectional loops, so 'bounce'
the current index against the idxlend */
vnf->flags |= SF_REVERSE;
vnf->increment = -vnf->increment;
vnf->current = idxlend - (vnf->current - idxlend); /* ?? */
} else
/* normal backwards looping, so set the
current position to loopend index */
vnf->current = idxlpos + (vnf->current - idxlend);
}
} else {
/* sample is not looping, so check
if it reached the last position */
if (vnf->current > idxsize) {
/* yes, so stop playing this sample */
vnf->current = 0;
vnf->active = 0;
break;
}
}
}
/* Vraag een far ptr op van het sampleadres
op byte offset vnf->current, en hoeveel samples
daarvan geldig zijn (VOORDAT segment overschrijding optreed) */
if (!(s = Samples[vnf->handle])) {
vnf->current = 0;
vnf->active = 0;
break;
}
if (vnf->flags & SF_REVERSE)
end = (vnf->flags & SF_LOOP) ? idxlpos : 0;
else
end = (vnf->flags & SF_LOOP) ? idxlend : idxsize;
/* Als de sample simpelweg niet beschikbaar is, of als
sample gestopt moet worden sample stilleggen en stoppen */
/* mix 'em: */
done = NewPredict(vnf->current, end, vnf->increment, todo);
if (!done) {
/* printf("predict stopped it. current %ld, end %ld\n",vnf->current,end);
*/ vnf->active = 0;
break;
}
/* optimisation: don't mix anything if volume is zero */
if (vnf->vol) {
SampleMix(s, ptr, vnf->current, vnf->increment, done);
}
vnf->current += (vnf->increment * done);
todo -= done;
ptr += (md_mode & DMODE_STEREO) ? (done << 1) : done;
}
}
static void VC_FillTick(SBYTE * buf, UWORD todo)
/*
Mixes 'todo' samples to 'buf'.. The number of samples has
to fit into the tickbuffer.
*/
{
int t;
/* clear the mixing buffer: */
memset(VC_TICKBUF, 0, (md_mode & DMODE_STEREO) ? todo << 3 : todo << 2);
for (t = 0; t < md_numchn; t++) {
vnf = &vinf[t];
if (vnf->active) {
idxsize = (vnf->size << FRACBITS) - 1;
idxlpos = vnf->reppos << FRACBITS;
idxlend = (vnf->repend << FRACBITS) - 1;
lvolmul = vnf->lvolmul;
rvolmul = vnf->rvolmul;
VC_AddChannel(VC_TICKBUF, todo);
}
}
if (md_mode & DMODE_16BITS)
VC_Sample32To16Copy(VC_TICKBUF, (SWORD *) buf, (md_mode & DMODE_STEREO) ? todo << 1 : todo, 16 - ampshift);
else
VC_Sample32To8Copy(VC_TICKBUF, buf, (md_mode & DMODE_STEREO) ? todo << 1 : todo, 24 - ampshift);
}
static void VC_WritePortion(SBYTE * buf, UWORD todo)
/*
Writes 'todo' mixed SAMPLES (!!) to 'buf'. When todo is bigger than the
number of samples that fit into VC_TICKBUF, the mixing operation is split
up into a number of smaller chunks.
*/
{
UWORD part;
/* write 'part' samples to the buffer */
while (todo) {
part = min(todo, samplesthatfit);
VC_FillTick(buf, part);
buf += samples2bytes(part);
todo -= part;
}
}
static UWORD TICKLEFT;
void VC_WriteSamples(SBYTE * buf, UWORD todo)
{
int t;
UWORD part;
while (todo > 0) {
if (TICKLEFT == 0) {
md_player();
TICKLEFT = (125L * md_mixfreq) / (50L * md_bpm);
/* compute volume, frequency counter & panning parameters for each channel. */
for (t = 0; t < md_numchn; t++) {
int pan, vol, lvol, rvol;
if (vinf[t].kick) {
vinf[t].current = (vinf[t].start << FRACBITS);
vinf[t].active = 1;
vinf[t].kick = 0;
}
if (vinf[t].frq == 0)
vinf[t].active = 0;
if (vinf[t].active) {
vinf[t].increment = fraction2long(vinf[t].frq, md_mixfreq);
if (vinf[t].flags & SF_REVERSE)
vinf[t].increment = -vinf[t].increment;
vol = vinf[t].vol;
pan = vinf[t].pan;
if (md_mode & DMODE_STEREO) {
lvol = (vol * ((pan < 128) ? 128 : (255 - pan))) / 128;
rvol = (vol * ((pan > 128) ? 128 : pan)) / 128;
vinf[t].lvolmul = (maxvol * lvol) / 64;
vinf[t].rvolmul = (maxvol * rvol) / 64;
} else {
vinf[t].lvolmul = (maxvol * vol) / 64;
}
}
}
}
part = min(TICKLEFT, todo);
VC_WritePortion(buf, part);
TICKLEFT -= part;
todo -= part;
buf += samples2bytes(part);
}
}
UWORD VC_WriteBytes(SBYTE * buf, UWORD todo)
/*
Writes 'todo' mixed SBYTES (!!) to 'buf'. It returns the number of
SBYTES actually written to 'buf' (which is rounded to number of samples
that fit into 'todo' bytes).
*/
{
todo = bytes2samples(todo);
VC_WriteSamples(buf, todo);
return samples2bytes(todo);
}
void VC_SilenceBytes(SBYTE * buf, UWORD todo)
/*
Fill the buffer with 'todo' bytes of silence (it depends on the mixing
mode how the buffer is filled)
*/
{
/* clear the buffer to zero (16 bits
signed ) or 0x80 (8 bits unsigned) */
if (md_mode & DMODE_16BITS)
memset(buf, 0, todo);
else
memset(buf, 0x80, todo);
}
void VC_PlayStart(void)
{
int t;
for (t = 0; t < 32; t++) {
vinf[t].current = 0;
vinf[t].flags = 0;
vinf[t].handle = 0;
vinf[t].kick = 0;
vinf[t].active = 0;
vinf[t].frq = 10000;
vinf[t].vol = 0;
vinf[t].pan = (t & 1) ? 0 : 255;
}
if (md_numchn > 0) /* sanity check - avoid core dump! */
maxvol = 16777216L / md_numchn;
else
maxvol = 16777216L;
/* instead of using a amplifying lookup table, I'm using a simple shift
amplify now.. amplifying doubles with every extra 4 channels, and also
doubles in stereo mode.. this seems to give similar volume levels
across the channel range */
ampshift = md_numchn / 8;
/* if(md_mode & DMODE_STEREO) ampshift++; */
if (md_mode & DMODE_INTERP)
SampleMix = (md_mode & DMODE_STEREO) ? MixStereoInterp : MixMonoInterp;
else
SampleMix = (md_mode & DMODE_STEREO) ? MixStereoNormal : MixMonoNormal;
samplesthatfit = TICKLSIZE;
if (md_mode & DMODE_STEREO)
samplesthatfit >>= 1;
TICKLEFT = 0;
}
void VC_PlayStop(void)
{
}
BOOL VC_Init(void)
{
return 1;
}
void VC_Exit(void)
{
}
void VC_VoiceSetVolume(UBYTE voice, UBYTE vol)
{
vinf[voice].vol = vol;
}
void VC_VoiceSetFrequency(UBYTE voice, ULONG frq)
{
vinf[voice].frq = frq;
}
void VC_VoiceSetPanning(UBYTE voice, UBYTE pan)
{
vinf[voice].pan = pan;
}
void VC_VoicePlay(UBYTE voice, SWORD handle, ULONG start, ULONG size, ULONG reppos, ULONG repend, UWORD flags)
{
if (start >= size)
return;
if (flags & SF_LOOP) {
if (repend > size)
repend = size; /* repend can't be bigger than size */
}
vinf[voice].flags = flags;
vinf[voice].handle = handle;
vinf[voice].start = start;
vinf[voice].size = size;
vinf[voice].reppos = reppos;
vinf[voice].repend = repend;
vinf[voice].kick = 1;
}
|