Skip to content

Commit

Permalink
remove initial test code: test.c; tiny optimize mixer_speed_lq: omit …
Browse files Browse the repository at this point in the history
…malloc/free, add size to avoid overflow.
  • Loading branch information
crazii committed Feb 5, 2024
1 parent 86bcedf commit bd460b2
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 149 deletions.
54 changes: 22 additions & 32 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,9 @@ mpxplay_audioout_info_s aui = {0};

#define MAIN_PCM_SAMPLESIZE 16384

static int16_t MAIN_OPLPCM[MAIN_PCM_SAMPLESIZE+256];
static int16_t MAIN_PCM[MAIN_PCM_SAMPLESIZE+256];
static int16_t MAIN_OPLPCM[MAIN_PCM_SAMPLESIZE];
static int16_t MAIN_PCM[MAIN_PCM_SAMPLESIZE];
static int16_t MAIN_PCMResample[MAIN_PCM_SAMPLESIZE];

static DPMI_ISR_HANDLE MAIN_IntHandlePM;
static DPMI_ISR_HANDLE MAIN_IntHandleRM;
Expand Down Expand Up @@ -1070,20 +1071,6 @@ static void MAIN_InterruptRM()

static void MAIN_Interrupt()
{
#if 0
aui.card_outbytes = aui.card_dmasize;
int space = AU_cardbuf_space(&aui)+2048;
//_LOG("int space: %d\n", space);
int samples = space / sizeof(int16_t) / 2 * 2;
//int samples = 22050/18*2;
_LOG("samples: %d %d\n", 22050/18*2, space/4*2);
static int cur = 0;
aui.samplenum = min(samples, TEST_SampleLen-cur);
aui.pcm_sample = TEST_Sample + cur;
//_LOG("cur: %d %d\n",cur,aui.samplenum);
cur += aui.samplenum;
cur -= AU_writedata(&aui);
#else
if(!(aui.card_infobits&AUINFOS_CARDINFOBIT_PLAYING))
return;

Expand Down Expand Up @@ -1131,7 +1118,7 @@ static void MAIN_Interrupt()
}

aui.card_outbytes = aui.card_dmasize;
int samples = AU_cardbuf_space(&aui) / sizeof(int16_t) / 2; //16 bit, 2 channels
int samples = AU_cardbuf_space(&aui) / sizeof(int16_t) / SBEMU_CHANNELS; //16 bit, 2 channels
//_LOG("samples:%d\n",samples);
if(samples == 0)
return;
Expand All @@ -1153,7 +1140,8 @@ static void MAIN_Interrupt()
//_LOG("DMA index: %x\n", DMA_Index);
//_LOG("digital start\n");
int pos = 0;
do {
do
{
if(MAIN_DMA_MappedAddr != 0
&& !(DMA_Addr >= MAIN_DMA_Addr && DMA_Addr+DMA_Index+DMA_Count <= MAIN_DMA_Addr+MAIN_DMA_Size))
{
Expand Down Expand Up @@ -1184,16 +1172,19 @@ static void MAIN_Interrupt()
_LOG("samples:%d %d %d, %d %d, %d %d\n", samples, pos+count, count, DMA_Count, DMA_Index, SB_Bytes, SB_Pos);
int bytes = count * samplesize * channels;

if(MAIN_DMA_MappedAddr == 0) //map failed?
memset(MAIN_PCM+pos*2, 0, bytes);
else
DPMI_CopyLinear(DPMI_PTR2L(MAIN_PCM+pos*2), MAIN_DMA_MappedAddr+(DMA_Addr-MAIN_DMA_Addr)+DMA_Index, bytes);
if(SBEMU_GetBits()<8) //ADPCM 8bit
count = SBEMU_DecodeADPCM((uint8_t*)(MAIN_PCM+pos*2), bytes);
if(samplesize != 2)
cv_bits_n_to_m(MAIN_PCM+pos*2, count*channels, samplesize, 2);
if(resample/*SB_Rate != aui.freq_card*/)
count = mixer_speed_lq(MAIN_PCM+pos*2, count*channels, channels, SB_Rate, aui.freq_card)/channels;
{
int16_t* pcm = resample ? MAIN_PCMResample : MAIN_PCM + pos*2;
if(MAIN_DMA_MappedAddr == 0) //map failed?
memset(pcm, 0, bytes);
else
DPMI_CopyLinear(DPMI_PTR2L(pcm), MAIN_DMA_MappedAddr+(DMA_Addr-MAIN_DMA_Addr)+DMA_Index, bytes);
if(SBEMU_GetBits()<8) //ADPCM 8bit
count = SBEMU_DecodeADPCM((uint8_t*)(pcm), bytes);
if(samplesize != 2)
cv_bits_n_to_m(pcm, count*channels, samplesize, 2);
if(resample/*SB_Rate != aui.freq_card*/)
count = mixer_speed_lq(MAIN_PCM+pos*2, MAIN_PCM_SAMPLESIZE-pos*2, pcm, count*channels, channels, SB_Rate, aui.freq_card)/channels;
}
if(channels == 1) //should be the last step
cv_channels_1_to_n(MAIN_PCM+pos*2, count, 2, 2);
pos += count;
Expand Down Expand Up @@ -1239,7 +1230,7 @@ static void MAIN_Interrupt()
{
samples = SBEMU_GetDirectCount();
_LOG("direct out:%d %d\n",samples,aui.card_samples_per_int);
memcpy(MAIN_PCM, SBEMU_GetDirectPCM8(), samples);
memcpy(MAIN_PCMResample, SBEMU_GetDirectPCM8(), samples);
SBEMU_ResetDirect();
#if 0 //fix noise for some games - SBEMU-X NOTE: unlikely to be needed
int zeros = TRUE;
Expand All @@ -1255,10 +1246,10 @@ static void MAIN_Interrupt()
}
#endif
//for(int i = 0; i < samples; ++i) _LOG("%d ",((uint8_t*)MAIN_PCM)[i]); _LOG("\n");
cv_bits_n_to_m(MAIN_PCM, samples, 1, 2);
cv_bits_n_to_m(MAIN_PCMResample, samples, 1, 2);
//for(int i = 0; i < samples; ++i) _LOG("%d ",MAIN_PCM[i]); _LOG("\n");
// the actual sample rate is derived from current count of samples in direct output buffer
samples = mixer_speed_lq(MAIN_PCM, samples, 1, (samples * aui.freq_card) / aui.card_samples_per_int, aui.freq_card);
samples = mixer_speed_lq(MAIN_PCM, MAIN_PCM_SAMPLESIZE, MAIN_PCMResample, samples, 1, (samples * aui.freq_card) / aui.card_samples_per_int, aui.freq_card);
//for(int i = 0; i < samples; ++i) _LOG("%d ",MAIN_PCM[i]); _LOG("\n");
cv_channels_1_to_n(MAIN_PCM, samples, 2, 2);
digital = TRUE;
Expand Down Expand Up @@ -1306,7 +1297,6 @@ static void MAIN_Interrupt()
AU_writedata(&aui);

//_LOG("MAIN INT END\n");
#endif
}

void MAIN_TSR_InstallationCheck()
Expand Down
1 change: 0 additions & 1 deletion makefile
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ SBEMU_SRC := sbemu/dbopl.cpp \
sbemu/dpmi/djgpp/gopint.c \
main.c \
qemm.c \
test.c \
utility.c \
hdpmipt.c \

Expand Down
29 changes: 17 additions & 12 deletions mpxplay/au_cards/au_base.c
Original file line number Diff line number Diff line change
Expand Up @@ -1014,29 +1014,30 @@ unsigned int cv_channels_1_to_n(PCM_CV_TYPE_S *pcm_sample,unsigned int samplenum
}

//sample rates
unsigned int mixer_speed_lq(PCM_CV_TYPE_S *pcm16,unsigned int samplenum, unsigned int channels, unsigned int samplerate, unsigned int newrate)
unsigned int mixer_speed_lq(PCM_CV_TYPE_S* dest, unsigned int destsample, const PCM_CV_TYPE_S* source, unsigned int sourcesample, unsigned int channels, unsigned int samplerate, unsigned int newrate)
{
const unsigned int instep=((samplerate/newrate)<<12) | (((4096*(samplerate%newrate)-1)/(newrate-1))&0xFFF);
const unsigned int inend=(samplenum/channels) << 12;
int16_t *pcm,*intmp;
const unsigned int inend=(sourcesample/channels) << 12;
int16_t *pcm; int16_t const* intmp;
unsigned long ipi;
unsigned int inpos = 0;//(samplerate<newrate) ? instep/2 : 0;
if(!samplenum)
if(!sourcesample)
return 0;
assert(((samplenum/channels)&0xFFF00000) == 0); //too many samples, need other approches.
unsigned int buffcount = max(((unsigned long long)max(samplenum,512)*newrate+samplerate-1)/samplerate,max(samplenum,512))*2+32;
int16_t* buff = (int16_t*)malloc(buffcount*sizeof(int16_t));
assert(((sourcesample/channels)&0xFFF00000) == 0); //too many samples, need other approches.
unsigned int buffcount = max(((unsigned long long)max(sourcesample,512)*newrate+samplerate-1)/samplerate,max(sourcesample,512))*2+32;
assert(buffcount <= destsample);
int16_t* buff = dest;

mpxplay_debugf(MPXPLAY_DEBUG_OUTPUT, "step: %08x, end: %08x\n", instep, inend);

pcm = buff;
intmp = pcm16;
int total = samplenum/channels;
intmp = source;
int total = sourcesample/channels;

do{
int m1,m2;
unsigned int ipi,ch;
int16_t *intmp1,*intmp2;
const int16_t *intmp1,*intmp2;
ipi = inpos >> 12;
m2=inpos&0xFFF;
m1=4096-m2;
Expand All @@ -1048,13 +1049,17 @@ unsigned int mixer_speed_lq(PCM_CV_TYPE_S *pcm16,unsigned int samplenum, unsigne
*pcm++= ((*intmp1++)*m1+(*intmp2++)*m2)/4096;// >> 12; //don't use shift, signed right shift impl defined, maybe logical shift
}while(--ch);
inpos+=instep;

if(pcm - buff >= destsample) //check overflow
{
assert(FALSE);
return pcm - buff;
}
}while(inpos<inend);

mpxplay_debugf(MPXPLAY_DEBUG_OUTPUT, "sample count: %d\n", pcm-buff);
//_LOG("MIXER_SPEED_LQ: %d, %d\n", pcm-buff, buffcount);
assert(pcm-buff <= buffcount);
memcpy(pcm16, buff, (pcm-buff)*sizeof(int16_t));
free(buff);
return pcm - buff;
}

Expand Down
2 changes: 1 addition & 1 deletion mpxplay/au_cards/au_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -514,7 +514,7 @@ extern void cv_bits_n_to_m(PCM_CV_TYPE_S *pcm,unsigned int samplenum,unsigned in
//channels
extern unsigned int cv_channels_1_to_n(PCM_CV_TYPE_S *pcm_sample,unsigned int samplenum,unsigned int newchannels,unsigned int bytespersample);
//sample rates
unsigned int mixer_speed_lq(PCM_CV_TYPE_S *pcm16,unsigned int samplenum, unsigned int channels, unsigned int samplerate, unsigned int newrate);
unsigned int mixer_speed_lq(PCM_CV_TYPE_S* dest, unsigned int destsample, const PCM_CV_TYPE_S* source, unsigned int sourcesample, unsigned int channels, unsigned int samplerate, unsigned int newrate);

#ifdef MPXPLAY_USE_DEBUGF

Expand Down
103 changes: 0 additions & 103 deletions test.c

This file was deleted.

0 comments on commit bd460b2

Please sign in to comment.