📄 virtch.c
字号:
/* MikMod sound library
(c) 1998, 1999, 2000, 2001, 2002 Miodrag Vallat and others - see file
AUTHORS for complete list.
This library is free software; you can redistribute it and/or modify
it under the terms of the GNU Library General Public License as
published by the Free Software Foundation; either version 2 of
the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
02111-1307, USA.
*/
/*==============================================================================
$Id: virtch.c,v 1.2 2004/02/13 13:31:54 raph Exp $
Sample mixing routines, using a 32 bits mixing buffer.
==============================================================================*/
/*
Optional features include:
(a) 4-step reverb (for 16 bit output only)
(b) Interpolation of sample data during mixing
(c) Dolby Surround Sound
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stddef.h>
#ifdef HAVE_MEMORY_H
#include <memory.h>
#endif
#include <string.h>
#include "mikmod_internals.h"
/*
Constant definitions
====================
BITSHIFT
Controls the maximum volume of the sound output. All data is shifted
right by BITSHIFT after being mixed. Higher values result in quieter
sound and less chance of distortion.
REVERBERATION
Controls the duration of the reverb. Larger values represent a shorter
reverb loop. Smaller values extend the reverb but can result in more of
an echo-ish sound.
*/
#define BITSHIFT 9
#define REVERBERATION 110000L
#define FRACBITS 11
#define FRACMASK ((1L<<FRACBITS)-1L)
#define TICKLSIZE 8192
#define TICKWSIZE (TICKLSIZE<<1)
#define TICKBSIZE (TICKWSIZE<<1)
#define CLICK_SHIFT 6
#define CLICK_BUFFER (1L<<CLICK_SHIFT)
#ifndef MIN
#define MIN(a,b) (((a)<(b)) ? (a) : (b))
#endif
typedef struct VINFO {
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 */
int vol; /* current volume */
int pan; /* current panning position */
int rampvol;
int lvolsel,rvolsel; /* Volume factor in range 0-255 */
int oldlvol,oldrvol;
SLONGLONG current; /* current index in the sample */
SLONGLONG increment; /* increment value */
} VINFO;
static SWORD **Samples;
static VINFO *vinf=NULL,*vnf;
static long tickleft,samplesthatfit,vc_memory=0;
static int vc_softchn;
static SLONGLONG idxsize,idxlpos,idxlend;
static SLONG *vc_tickbuf=NULL;
static UWORD vc_mode;
/* Reverb control variables */
static int RVc1, RVc2, RVc3, RVc4, RVc5, RVc6, RVc7, RVc8;
static ULONG RVRindex;
/* For Mono or Left Channel */
static SLONG *RVbufL1=NULL,*RVbufL2=NULL,*RVbufL3=NULL,*RVbufL4=NULL,
*RVbufL5=NULL,*RVbufL6=NULL,*RVbufL7=NULL,*RVbufL8=NULL;
/* For Stereo only (Right Channel) */
static SLONG *RVbufR1=NULL,*RVbufR2=NULL,*RVbufR3=NULL,*RVbufR4=NULL,
*RVbufR5=NULL,*RVbufR6=NULL,*RVbufR7=NULL,*RVbufR8=NULL;
#ifdef NATIVE_64BIT_INT
#define NATIVE SLONGLONG
#else
#define NATIVE SLONG
#endif
/*========== 32 bit sample mixers - only for 32 bit platforms */
#ifndef NATIVE_64BIT_INT
static SLONG Mix32MonoNormal(SWORD* srce,SLONG* dest,SLONG index,SLONG increment,SLONG todo)
{
SWORD sample;
SLONG lvolsel = vnf->lvolsel;
while(todo--) {
sample = srce[index >> FRACBITS];
index += increment;
*dest++ += lvolsel * sample;
}
return index;
}
static SLONG Mix32StereoNormal(SWORD* srce,SLONG* dest,SLONG index,SLONG increment,SLONG todo)
{
SWORD sample;
SLONG lvolsel = vnf->lvolsel;
SLONG rvolsel = vnf->rvolsel;
while(todo--) {
sample=srce[index >> FRACBITS];
index += increment;
*dest++ += lvolsel * sample;
*dest++ += rvolsel * sample;
}
return index;
}
static SLONG Mix32SurroundNormal(SWORD* srce,SLONG* dest,SLONG index,SLONG increment,SLONG todo)
{
SWORD sample;
SLONG lvolsel = vnf->lvolsel;
SLONG rvolsel = vnf->rvolsel;
if (lvolsel>=rvolsel) {
while(todo--) {
sample = srce[index >> FRACBITS];
index += increment;
*dest++ += lvolsel*sample;
*dest++ -= lvolsel*sample;
}
} else {
while(todo--) {
sample = srce[index >> FRACBITS];
index += increment;
*dest++ -= rvolsel*sample;
*dest++ += rvolsel*sample;
}
}
return index;
}
static SLONG Mix32MonoInterp(SWORD* srce,SLONG* dest,SLONG index,SLONG increment,SLONG todo)
{
SLONG sample;
SLONG lvolsel = vnf->lvolsel;
SLONG rampvol = vnf->rampvol;
if (rampvol) {
SLONG oldlvol = vnf->oldlvol - lvolsel;
while(todo--) {
sample=(SLONG)srce[index>>FRACBITS]+
((SLONG)(srce[(index>>FRACBITS)+1]-srce[index>>FRACBITS])
*(index&FRACMASK)>>FRACBITS);
index += increment;
*dest++ += ((lvolsel << CLICK_SHIFT) + oldlvol * rampvol)
* sample >> CLICK_SHIFT;
if (!--rampvol)
break;
}
vnf->rampvol = rampvol;
if (todo < 0)
return index;
}
while(todo--) {
sample=(SLONG)srce[index>>FRACBITS]+
((SLONG)(srce[(index>>FRACBITS)+1]-srce[index>>FRACBITS])
*(index&FRACMASK)>>FRACBITS);
index += increment;
*dest++ += lvolsel * sample;
}
return index;
}
static SLONG Mix32StereoInterp(SWORD* srce,SLONG* dest,SLONG index,SLONG increment,SLONG todo)
{
SLONG sample;
SLONG lvolsel = vnf->lvolsel;
SLONG rvolsel = vnf->rvolsel;
SLONG rampvol = vnf->rampvol;
if (rampvol) {
SLONG oldlvol = vnf->oldlvol - lvolsel;
SLONG oldrvol = vnf->oldrvol - rvolsel;
while(todo--) {
sample=(SLONG)srce[index>>FRACBITS]+
((SLONG)(srce[(index>>FRACBITS)+1]-srce[index>>FRACBITS])
*(index&FRACMASK)>>FRACBITS);
index += increment;
*dest++ += ((lvolsel << CLICK_SHIFT) + oldlvol * rampvol)
* sample >> CLICK_SHIFT;
*dest++ += ((rvolsel << CLICK_SHIFT) + oldrvol * rampvol)
* sample >> CLICK_SHIFT;
if (!--rampvol)
break;
}
vnf->rampvol = rampvol;
if (todo < 0)
return index;
}
while(todo--) {
sample=(SLONG)srce[index>>FRACBITS]+
((SLONG)(srce[(index>>FRACBITS)+1]-srce[index>>FRACBITS])
*(index&FRACMASK)>>FRACBITS);
index += increment;
*dest++ += lvolsel * sample;
*dest++ += rvolsel * sample;
}
return index;
}
static SLONG Mix32SurroundInterp(SWORD* srce,SLONG* dest,SLONG index,SLONG increment,SLONG todo)
{
SLONG sample;
SLONG lvolsel = vnf->lvolsel;
SLONG rvolsel = vnf->rvolsel;
SLONG rampvol = vnf->rampvol;
SLONG oldvol, vol;
if (lvolsel >= rvolsel) {
vol = lvolsel;
oldvol = vnf->oldlvol;
} else {
vol = rvolsel;
oldvol = vnf->oldrvol;
}
if (rampvol) {
oldvol -= vol;
while(todo--) {
sample=(SLONG)srce[index>>FRACBITS]+
((SLONG)(srce[(index>>FRACBITS)+1]-srce[index>>FRACBITS])
*(index&FRACMASK)>>FRACBITS);
index += increment;
sample=((vol << CLICK_SHIFT) + oldvol * rampvol)
* sample >> CLICK_SHIFT;
*dest++ += sample;
*dest++ -= sample;
if (!--rampvol)
break;
}
vnf->rampvol = rampvol;
if (todo < 0)
return index;
}
while(todo--) {
sample=(SLONG)srce[index>>FRACBITS]+
((SLONG)(srce[(index>>FRACBITS)+1]-srce[index>>FRACBITS])
*(index&FRACMASK)>>FRACBITS);
index += increment;
*dest++ += vol*sample;
*dest++ -= vol*sample;
}
return index;
}
#endif
/*========== 64 bit sample mixers - all platforms */
static SLONGLONG MixMonoNormal(SWORD* srce,SLONG* dest,SLONGLONG index,SLONGLONG increment,SLONG todo)
{
SWORD sample;
SLONG lvolsel = vnf->lvolsel;
while(todo--) {
sample = srce[index >> FRACBITS];
index += increment;
*dest++ += lvolsel * sample;
}
return index;
}
static SLONGLONG MixStereoNormal(SWORD* srce,SLONG* dest,SLONGLONG index,SLONGLONG increment,SLONG todo)
{
SWORD sample;
SLONG lvolsel = vnf->lvolsel;
SLONG rvolsel = vnf->rvolsel;
while(todo--) {
sample=srce[index >> FRACBITS];
index += increment;
*dest++ += lvolsel * sample;
*dest++ += rvolsel * sample;
}
return index;
}
static SLONGLONG MixSurroundNormal(SWORD* srce,SLONG* dest,SLONGLONG index,SLONGLONG increment,SLONG todo)
{
SWORD sample;
SLONG lvolsel = vnf->lvolsel;
SLONG rvolsel = vnf->rvolsel;
if(vnf->lvolsel>=vnf->rvolsel) {
while(todo--) {
sample = srce[index >> FRACBITS];
index += increment;
*dest++ += lvolsel*sample;
*dest++ -= lvolsel*sample;
}
} else {
while(todo--) {
sample = srce[index >> FRACBITS];
index += increment;
*dest++ -= rvolsel*sample;
*dest++ += rvolsel*sample;
}
}
return index;
}
static SLONGLONG MixMonoInterp(SWORD* srce,SLONG* dest,SLONGLONG index,SLONGLONG increment,SLONG todo)
{
SLONG sample;
SLONG lvolsel = vnf->lvolsel;
SLONG rampvol = vnf->rampvol;
if (rampvol) {
SLONG oldlvol = vnf->oldlvol - lvolsel;
while(todo--) {
sample=(SLONG)srce[index>>FRACBITS]+
((SLONG)(srce[(index>>FRACBITS)+1]-srce[index>>FRACBITS])
*(index&FRACMASK)>>FRACBITS);
index += increment;
*dest++ += ((lvolsel << CLICK_SHIFT) + oldlvol * rampvol)
* sample >> CLICK_SHIFT;
if (!--rampvol)
break;
}
vnf->rampvol = rampvol;
if (todo < 0)
return index;
}
while(todo--) {
sample=(SLONG)srce[index>>FRACBITS]+
((SLONG)(srce[(index>>FRACBITS)+1]-srce[index>>FRACBITS])
*(index&FRACMASK)>>FRACBITS);
index += increment;
*dest++ += lvolsel * sample;
}
return index;
}
static SLONGLONG MixStereoInterp(SWORD* srce,SLONG* dest,SLONGLONG index,SLONGLONG increment,SLONG todo)
{
SLONG sample;
SLONG lvolsel = vnf->lvolsel;
SLONG rvolsel = vnf->rvolsel;
SLONG rampvol = vnf->rampvol;
if (rampvol) {
SLONG oldlvol = vnf->oldlvol - lvolsel;
SLONG oldrvol = vnf->oldrvol - rvolsel;
while(todo--) {
sample=(SLONG)srce[index>>FRACBITS]+
((SLONG)(srce[(index>>FRACBITS)+1]-srce[index>>FRACBITS])
*(index&FRACMASK)>>FRACBITS);
index += increment;
*dest++ +=((lvolsel << CLICK_SHIFT) + oldlvol * rampvol)
* sample >> CLICK_SHIFT;
*dest++ +=((rvolsel << CLICK_SHIFT) + oldrvol * rampvol)
* sample >> CLICK_SHIFT;
if (!--rampvol)
break;
}
vnf->rampvol = rampvol;
if (todo < 0)
return index;
}
while(todo--) {
sample=(SLONG)srce[index>>FRACBITS]+
((SLONG)(srce[(index>>FRACBITS)+1]-srce[index>>FRACBITS])
*(index&FRACMASK)>>FRACBITS);
index += increment;
*dest++ += lvolsel * sample;
*dest++ += rvolsel * sample;
}
return index;
}
static SLONGLONG MixSurroundInterp(SWORD* srce,SLONG* dest,SLONGLONG index,SLONGLONG increment,SLONG todo)
{
SLONG sample;
SLONG lvolsel = vnf->lvolsel;
SLONG rvolsel = vnf->rvolsel;
SLONG rampvol = vnf->rampvol;
SLONG oldvol, vol;
if (lvolsel >= rvolsel) {
vol = lvolsel;
oldvol = vnf->oldlvol;
} else {
vol = rvolsel;
oldvol = vnf->oldrvol;
}
if (rampvol) {
oldvol -= vol;
while(todo--) {
sample=(SLONG)srce[index>>FRACBITS]+
((SLONG)(srce[(index>>FRACBITS)+1]-srce[index>>FRACBITS])
*(index&FRACMASK)>>FRACBITS);
index += increment;
sample=((vol << CLICK_SHIFT) + oldvol * rampvol)
* sample >> CLICK_SHIFT;
*dest++ += sample;
*dest++ -= sample;
if (!--rampvol)
break;
}
vnf->rampvol = rampvol;
if (todo < 0)
return index;
}
while(todo--) {
sample=(SLONG)srce[index>>FRACBITS]+
((SLONG)(srce[(index>>FRACBITS)+1]-srce[index>>FRACBITS])
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -