📄 virtch2.c
字号:
{
float x1,x2,tmpx;
int i;
for(count/=SAMPLING_FACTOR;count;count--) {
tmpx=0.0f;
for(i=SAMPLING_FACTOR/2;i;i--) {
EXTRACT_SAMPLE_FP(x1,1.0f); EXTRACT_SAMPLE_FP(x2,1.0f);
CHECK_SAMPLE_FP(x1,1.0f); CHECK_SAMPLE_FP(x2,1.0f);
tmpx+=x1+x2;
}
*dste++ =tmpx*(1.0f/SAMPLING_FACTOR);
}
}
static void Mix32ToFP_Stereo(float* dste,SLONG* srce,NATIVE count)
{
float x1,x2,x3,x4,tmpx,tmpy;
int i;
for(count/=SAMPLING_FACTOR;count;count--) {
tmpx=tmpy=0.0f;
for(i=SAMPLING_FACTOR/2;i;i--) {
EXTRACT_SAMPLE_FP(x1,1.0f); EXTRACT_SAMPLE_FP(x2,1.0f);
EXTRACT_SAMPLE_FP(x3,1.0f); EXTRACT_SAMPLE_FP(x4,1.0f);
CHECK_SAMPLE_FP(x1,1.0f); CHECK_SAMPLE_FP(x2,1.0f);
CHECK_SAMPLE_FP(x3,1.0f); CHECK_SAMPLE_FP(x4,1.0f);
tmpx+=x1+x3;
tmpy+=x2+x4;
}
*dste++ =tmpx*(1.0f/SAMPLING_FACTOR);
*dste++ =tmpy*(1.0f/SAMPLING_FACTOR);
}
}
/* Mixing macros */
#define EXTRACT_SAMPLE(var,attenuation) var=*srce++/(MAXVOL_FACTOR*attenuation)
#define CHECK_SAMPLE(var,bound) var=(var>=bound)?bound-1:(var<-bound)?-bound:var
static void Mix32To16_Normal(SWORD* dste,SLONG* srce,NATIVE count)
{
NATIVE x1,x2,tmpx;
int i;
for(count/=SAMPLING_FACTOR;count;count--) {
tmpx=0;
for(i=SAMPLING_FACTOR/2;i;i--) {
EXTRACT_SAMPLE(x1,1); EXTRACT_SAMPLE(x2,1);
CHECK_SAMPLE(x1,32768); CHECK_SAMPLE(x2,32768);
tmpx+=x1+x2;
}
*dste++ =tmpx/SAMPLING_FACTOR;
}
}
static void Mix32To16_Stereo(SWORD* dste,SLONG* srce,NATIVE count)
{
NATIVE x1,x2,x3,x4,tmpx,tmpy;
int i;
for(count/=SAMPLING_FACTOR;count;count--) {
tmpx=tmpy=0;
for(i=SAMPLING_FACTOR/2;i;i--) {
EXTRACT_SAMPLE(x1,1); EXTRACT_SAMPLE(x2,1);
EXTRACT_SAMPLE(x3,1); EXTRACT_SAMPLE(x4,1);
CHECK_SAMPLE(x1,32768); CHECK_SAMPLE(x2,32768);
CHECK_SAMPLE(x3,32768); CHECK_SAMPLE(x4,32768);
tmpx+=x1+x3;
tmpy+=x2+x4;
}
*dste++ =tmpx/SAMPLING_FACTOR;
*dste++ =tmpy/SAMPLING_FACTOR;
}
}
static void Mix32To8_Normal(SBYTE* dste,SLONG* srce,NATIVE count)
{
NATIVE x1,x2,tmpx;
int i;
for(count/=SAMPLING_FACTOR;count;count--) {
tmpx = 0;
for(i=SAMPLING_FACTOR/2;i;i--) {
EXTRACT_SAMPLE(x1,256); EXTRACT_SAMPLE(x2,256);
CHECK_SAMPLE(x1,128); CHECK_SAMPLE(x2,128);
tmpx+=x1+x2;
}
*dste++ =(tmpx/SAMPLING_FACTOR)+128;
}
}
static void Mix32To8_Stereo(SBYTE* dste,SLONG* srce,NATIVE count)
{
NATIVE x1,x2,x3,x4,tmpx,tmpy;
int i;
for(count/=SAMPLING_FACTOR;count;count--) {
tmpx=tmpy=0;
for(i=SAMPLING_FACTOR/2;i;i--) {
EXTRACT_SAMPLE(x1,256); EXTRACT_SAMPLE(x2,256);
EXTRACT_SAMPLE(x3,256); EXTRACT_SAMPLE(x4,256);
CHECK_SAMPLE(x1,128); CHECK_SAMPLE(x2,128);
CHECK_SAMPLE(x3,128); CHECK_SAMPLE(x4,128);
tmpx+=x1+x3;
tmpy+=x2+x4;
}
*dste++ =(tmpx/SAMPLING_FACTOR)+128;
*dste++ =(tmpy/SAMPLING_FACTOR)+128;
}
}
static void AddChannel(SLONG* ptr,NATIVE todo)
{
SLONGLONG end,done;
SWORD *s;
if(!(s=Samples[vnf->handle])) {
vnf->current = vnf->active = 0;
vnf->lastvalL = vnf->lastvalR = 0;
return;
}
/* update the 'current' index so the sample loops, or stops playing if it
reached the end of the sample */
while(todo>0) {
SLONGLONG endpos;
if(vnf->flags & SF_REVERSE) {
/* The sample is playing in reverse */
if((vnf->flags&SF_LOOP)&&(vnf->current<idxlpos)) {
/* the sample is looping and has reached the loopstart index */
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 = vnf->active = 0;
break;
}
}
} else {
/* The sample is playing forward */
if((vnf->flags & SF_LOOP) &&
(vnf->current >= idxlend)) {
/* the sample is looping, check the loopend index */
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 = vnf->active = 0;
break;
}
}
}
end=(vnf->flags&SF_REVERSE)?(vnf->flags&SF_LOOP)?idxlpos:0:
(vnf->flags&SF_LOOP)?idxlend:idxsize;
/* if the sample is not blocked... */
if((end==vnf->current)||(!vnf->increment))
done=0;
else {
done=MIN((end-vnf->current)/vnf->increment+1,todo);
if(done<0) done=0;
}
if(!done) {
vnf->active = 0;
break;
}
endpos=vnf->current+done*vnf->increment;
if(vnf->vol || vnf->rampvol) {
#ifndef NATIVE_64BIT_INT
/* use the 32 bit mixers as often as we can (they're much faster) */
if((vnf->current<0x7fffffff)&&(endpos<0x7fffffff)) {
if(vc_mode & DMODE_STEREO) {
if((vnf->pan==PAN_SURROUND)&&(vc_mode&DMODE_SURROUND))
vnf->current=Mix32StereoSurround
(s,ptr,vnf->current,vnf->increment,done);
else
vnf->current=Mix32StereoNormal
(s,ptr,vnf->current,vnf->increment,done);
} else
vnf->current=Mix32MonoNormal
(s,ptr,vnf->current,vnf->increment,done);
} else
#endif
{
if(vc_mode & DMODE_STEREO) {
if((vnf->pan==PAN_SURROUND)&&(vc_mode&DMODE_SURROUND))
vnf->current=MixStereoSurround
(s,ptr,vnf->current,vnf->increment,done);
else
vnf->current=MixStereoNormal
(s,ptr,vnf->current,vnf->increment,done);
} else
vnf->current=MixMonoNormal
(s,ptr,vnf->current,vnf->increment,done);
}
} else {
vnf->lastvalL = vnf->lastvalR = 0;
/* update sample position */
vnf->current=endpos;
}
todo -= done;
ptr +=(vc_mode & DMODE_STEREO)?(done<<1):done;
}
}
#define _IN_VIRTCH_
#define VC1_SilenceBytes VC2_SilenceBytes
#define VC1_WriteSamples VC2_WriteSamples
#define VC1_WriteBytes VC2_WriteBytes
#define VC1_Exit VC2_Exit
#define VC1_VoiceSetVolume VC2_VoiceSetVolume
#define VC1_VoiceGetVolume VC2_VoiceGetVolume
#define VC1_VoiceSetPanning VC2_VoiceSetPanning
#define VC1_VoiceGetPanning VC2_VoiceGetPanning
#define VC1_VoiceSetFrequency VC2_VoiceSetFrequency
#define VC1_VoiceGetFrequency VC2_VoiceGetFrequency
#define VC1_VoicePlay VC2_VoicePlay
#define VC1_VoiceStop VC2_VoiceStop
#define VC1_VoiceStopped VC2_VoiceStopped
#define VC1_VoiceGetPosition VC2_VoiceGetPosition
#define VC1_SampleUnload VC2_SampleUnload
#define VC1_SampleLoad VC2_SampleLoad
#define VC1_SampleSpace VC2_SampleSpace
#define VC1_SampleLength VC2_SampleLength
#define VC1_VoiceRealVolume VC2_VoiceRealVolume
#include "virtch_common.c"
#undef _IN_VIRTCH_
void VC2_WriteSamples(SBYTE* buf,ULONG todo)
{
int left,portion=0;
SBYTE *buffer;
int t,pan,vol;
todo*=SAMPLING_FACTOR;
while(todo) {
if(!tickleft) {
if(vc_mode & DMODE_SOFT_MUSIC) md_player();
tickleft=(md_mixfreq*125L*SAMPLING_FACTOR)/(md_bpm*50L);
tickleft&=~(SAMPLING_FACTOR-1);
}
left = MIN(tickleft, todo);
buffer = buf;
tickleft -= left;
todo -= left;
buf += samples2bytes(left)/SAMPLING_FACTOR;
while(left) {
portion = MIN(left, samplesthatfit);
memset(vc_tickbuf,0,portion<<((vc_mode&DMODE_STEREO)?3:2));
for(t=0;t<vc_softchn;t++) {
vnf = &vinf[t];
if(vnf->kick) {
vnf->current=((SLONGLONG)(vnf->start))<<FRACBITS;
vnf->kick = 0;
vnf->active = 1;
vnf->click = CLICK_BUFFER;
vnf->rampvol = 0;
}
if(!vnf->frq) vnf->active = 0;
if(vnf->active) {
vnf->increment=((SLONGLONG)(vnf->frq)<<(FRACBITS-SAMPLING_SHIFT))
/md_mixfreq;
if(vnf->flags&SF_REVERSE) vnf->increment=-vnf->increment;
vol = vnf->vol; pan = vnf->pan;
vnf->oldlvol=vnf->lvolsel;vnf->oldrvol=vnf->rvolsel;
if(vc_mode & DMODE_STEREO) {
if(pan!=PAN_SURROUND) {
vnf->lvolsel=(vol*(PAN_RIGHT-pan))>>8;
vnf->rvolsel=(vol*pan)>>8;
} else {
vnf->lvolsel=vnf->rvolsel=(vol * 256L) / 480;
}
} else
vnf->lvolsel=vol;
idxsize=(vnf->size)?((SLONGLONG)(vnf->size)<<FRACBITS)-1:0;
idxlend=(vnf->repend)?((SLONGLONG)(vnf->repend)<<FRACBITS)-1:0;
idxlpos=(SLONGLONG)(vnf->reppos)<<FRACBITS;
AddChannel(vc_tickbuf,portion);
}
}
if(md_reverb) {
if(md_reverb>15) md_reverb=15;
MixReverb(vc_tickbuf,portion);
}
if(vc_mode & DMODE_FLOAT)
Mix32toFP((float*)buffer,vc_tickbuf,portion);
else if(vc_mode & DMODE_16BITS)
Mix32to16((SWORD*)buffer,vc_tickbuf,portion);
else
Mix32to8((SBYTE*)buffer,vc_tickbuf,portion);
buffer += samples2bytes(portion) / SAMPLING_FACTOR;
left -= portion;
}
}
}
BOOL VC2_Init(void)
{
VC_SetupPointers();
if (!(md_mode&DMODE_HQMIXER))
return VC1_Init();
if(!(Samples=(SWORD**)_mm_calloc(MAXSAMPLEHANDLES,sizeof(SWORD*)))) {
_mm_errno = MMERR_INITIALIZING_MIXER;
return 1;
}
if(!vc_tickbuf)
if(!(vc_tickbuf=(SLONG*)_mm_malloc((TICKLSIZE+32)*sizeof(SLONG)))) {
_mm_errno = MMERR_INITIALIZING_MIXER;
return 1;
}
if(md_mode & DMODE_STEREO) {
Mix32toFP = Mix32ToFP_Stereo;
Mix32to16 = Mix32To16_Stereo;
Mix32to8 = Mix32To8_Stereo;
MixReverb = MixReverb_Stereo;
} else {
Mix32toFP = Mix32ToFP_Normal;
Mix32to16 = Mix32To16_Normal;
Mix32to8 = Mix32To8_Normal;
MixReverb = MixReverb_Normal;
}
md_mode |= DMODE_INTERP;
vc_mode = md_mode;
return 0;
}
BOOL VC2_PlayStart(void)
{
md_mode|=DMODE_INTERP;
samplesthatfit = TICKLSIZE;
if(vc_mode & DMODE_STEREO) samplesthatfit >>= 1;
tickleft = 0;
RVc1 = (5000L * md_mixfreq) / (REVERBERATION * 10);
RVc2 = (5078L * md_mixfreq) / (REVERBERATION * 10);
RVc3 = (5313L * md_mixfreq) / (REVERBERATION * 10);
RVc4 = (5703L * md_mixfreq) / (REVERBERATION * 10);
RVc5 = (6250L * md_mixfreq) / (REVERBERATION * 10);
RVc6 = (6953L * md_mixfreq) / (REVERBERATION * 10);
RVc7 = (7813L * md_mixfreq) / (REVERBERATION * 10);
RVc8 = (8828L * md_mixfreq) / (REVERBERATION * 10);
if(!(RVbufL1=(SLONG*)_mm_calloc((RVc1+1),sizeof(SLONG)))) return 1;
if(!(RVbufL2=(SLONG*)_mm_calloc((RVc2+1),sizeof(SLONG)))) return 1;
if(!(RVbufL3=(SLONG*)_mm_calloc((RVc3+1),sizeof(SLONG)))) return 1;
if(!(RVbufL4=(SLONG*)_mm_calloc((RVc4+1),sizeof(SLONG)))) return 1;
if(!(RVbufL5=(SLONG*)_mm_calloc((RVc5+1),sizeof(SLONG)))) return 1;
if(!(RVbufL6=(SLONG*)_mm_calloc((RVc6+1),sizeof(SLONG)))) return 1;
if(!(RVbufL7=(SLONG*)_mm_calloc((RVc7+1),sizeof(SLONG)))) return 1;
if(!(RVbufL8=(SLONG*)_mm_calloc((RVc8+1),sizeof(SLONG)))) return 1;
if(!(RVbufR1=(SLONG*)_mm_calloc((RVc1+1),sizeof(SLONG)))) return 1;
if(!(RVbufR2=(SLONG*)_mm_calloc((RVc2+1),sizeof(SLONG)))) return 1;
if(!(RVbufR3=(SLONG*)_mm_calloc((RVc3+1),sizeof(SLONG)))) return 1;
if(!(RVbufR4=(SLONG*)_mm_calloc((RVc4+1),sizeof(SLONG)))) return 1;
if(!(RVbufR5=(SLONG*)_mm_calloc((RVc5+1),sizeof(SLONG)))) return 1;
if(!(RVbufR6=(SLONG*)_mm_calloc((RVc6+1),sizeof(SLONG)))) return 1;
if(!(RVbufR7=(SLONG*)_mm_calloc((RVc7+1),sizeof(SLONG)))) return 1;
if(!(RVbufR8=(SLONG*)_mm_calloc((RVc8+1),sizeof(SLONG)))) return 1;
RVRindex = 0;
return 0;
}
void VC2_PlayStop(void)
{
if(RVbufL1) free(RVbufL1);
if(RVbufL2) free(RVbufL2);
if(RVbufL3) free(RVbufL3);
if(RVbufL4) free(RVbufL4);
if(RVbufL5) free(RVbufL5);
if(RVbufL6) free(RVbufL6);
if(RVbufL7) free(RVbufL7);
if(RVbufL8) free(RVbufL8);
if(RVbufR1) free(RVbufR1);
if(RVbufR2) free(RVbufR2);
if(RVbufR3) free(RVbufR3);
if(RVbufR4) free(RVbufR4);
if(RVbufR5) free(RVbufR5);
if(RVbufR6) free(RVbufR6);
if(RVbufR7) free(RVbufR7);
if(RVbufR8) free(RVbufR8);
RVbufL1=RVbufL2=RVbufL3=RVbufL4=RVbufL5=RVbufL6=RVbufL7=RVbufL8=NULL;
RVbufR1=RVbufR2=RVbufR3=RVbufR4=RVbufR5=RVbufR6=RVbufR7=RVbufR8=NULL;
}
BOOL VC2_SetNumVoices(void)
{
int t;
md_mode|=DMODE_INTERP;
if(!(vc_softchn=md_softchn)) return 0;
if(vinf) free(vinf);
if(!(vinf=_mm_calloc(sizeof(VINFO),vc_softchn))) return 1;
for(t=0;t<vc_softchn;t++) {
vinf[t].frq=10000;
vinf[t].pan=(t&1)?PAN_LEFT:PAN_RIGHT;
}
return 0;
}
/* ex:set ts=4: */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -