Skip to content

Commit 880ddfa

Browse files
committed
improve resampling interpolation.
1 parent bd460b2 commit 880ddfa

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

main.c

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ mpxplay_audioout_info_s aui = {0};
4343
static int16_t MAIN_OPLPCM[MAIN_PCM_SAMPLESIZE];
4444
static int16_t MAIN_PCM[MAIN_PCM_SAMPLESIZE];
4545
static int16_t MAIN_PCMResample[MAIN_PCM_SAMPLESIZE];
46+
static int MAIN_LastSBRate = 0;
47+
static int16_t MAIN_LastResample[SBEMU_CHANNELS];
4648

4749
static DPMI_ISR_HANDLE MAIN_IntHandlePM;
4850
static DPMI_ISR_HANDLE MAIN_IntHandleRM;
@@ -1133,6 +1135,11 @@ static void MAIN_Interrupt()
11331135
uint32_t SB_Bytes = SBEMU_GetSampleBytes();
11341136
uint32_t SB_Pos = SBEMU_GetPos();
11351137
uint32_t SB_Rate = SBEMU_GetSampleRate();
1138+
if(MAIN_LastSBRate != SB_Rate)
1139+
{
1140+
for(int i = 0; i < SBEMU_CHANNELS; ++i) MAIN_LastResample[i] = 0;
1141+
MAIN_LastSBRate = SB_Rate;
1142+
}
11361143
int samplesize = max(1, SBEMU_GetBits()/8); //sample size in bytes 1 for 8bit. 2 for 16bit
11371144
int channels = SBEMU_GetChannels();
11381145
_LOG("sample rate: %d %d\n", SB_Rate, aui.freq_card);
@@ -1173,7 +1180,7 @@ static void MAIN_Interrupt()
11731180
int bytes = count * samplesize * channels;
11741181

11751182
{
1176-
int16_t* pcm = resample ? MAIN_PCMResample : MAIN_PCM + pos*2;
1183+
int16_t* pcm = resample ? MAIN_PCMResample+channels : MAIN_PCM + pos*2;
11771184
if(MAIN_DMA_MappedAddr == 0) //map failed?
11781185
memset(pcm, 0, bytes);
11791186
else
@@ -1183,7 +1190,14 @@ static void MAIN_Interrupt()
11831190
if(samplesize != 2)
11841191
cv_bits_n_to_m(pcm, count*channels, samplesize, 2);
11851192
if(resample/*SB_Rate != aui.freq_card*/)
1186-
count = mixer_speed_lq(MAIN_PCM+pos*2, MAIN_PCM_SAMPLESIZE-pos*2, pcm, count*channels, channels, SB_Rate, aui.freq_card)/channels;
1193+
{
1194+
for(int i = 0; i < channels; ++i)
1195+
{
1196+
MAIN_PCMResample[i] = MAIN_LastResample[i]; //put last sample at beginning for interpolation
1197+
MAIN_LastResample[i] = *(pcm + (count-1)*channels + i); //record last sample
1198+
}
1199+
count = mixer_speed_lq(MAIN_PCM+pos*2, MAIN_PCM_SAMPLESIZE-pos*2, MAIN_PCMResample, count*channels, channels, SB_Rate, aui.freq_card)/channels;
1200+
}
11871201
}
11881202
if(channels == 1) //should be the last step
11891203
cv_channels_1_to_n(MAIN_PCM+pos*2, count, 2, 2);

mpxplay/au_cards/au_base.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1017,7 +1017,7 @@ unsigned int cv_channels_1_to_n(PCM_CV_TYPE_S *pcm_sample,unsigned int samplenum
10171017
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)
10181018
{
10191019
const unsigned int instep=((samplerate/newrate)<<12) | (((4096*(samplerate%newrate)-1)/(newrate-1))&0xFFF);
1020-
const unsigned int inend=(sourcesample/channels) << 12;
1020+
const unsigned int inend=(sourcesample/channels - 1) << 12; //for n samples, interpolation n-1 steps
10211021
int16_t *pcm; int16_t const* intmp;
10221022
unsigned long ipi;
10231023
unsigned int inpos = 0;//(samplerate<newrate) ? instep/2 : 0;
@@ -1044,7 +1044,7 @@ unsigned int mixer_speed_lq(PCM_CV_TYPE_S* dest, unsigned int destsample, const
10441044
ch=channels;
10451045
ipi*=ch;
10461046
intmp1=intmp+ipi;
1047-
intmp2=ipi < total-ch ? intmp1+ch : intmp1;
1047+
intmp2=intmp1+ch;
10481048
do{
10491049
*pcm++= ((*intmp1++)*m1+(*intmp2++)*m2)/4096;// >> 12; //don't use shift, signed right shift impl defined, maybe logical shift
10501050
}while(--ch);

0 commit comments

Comments
 (0)