Author Topic: dsp plugin returns less samples than buffer size  (Read 2480 times)

RevG

  • Posts: 454
Hi, I don't know exactly what to do when a winamp dsp plugin returns less samples than the current buffer size.

I think that I must read ahead to get enough data to fill the buffer and then advance the buffer so that it is in the correct location. Is what I am saying here correct?

If this is true, then my question is:

How do I advance the buffer so that is is in the correct location?

I have pasted my code below.
Thanks in advance!

Code: [Select]

DWORD CALLBACK MyStreamProc(HSTREAM handle,void *buffer,DWORD length,DWORD user)
{
     int nSamplesResults;
     decbuff = (short*)realloc(decbuff, length);
     BASS_ChannelGetData(decodehandle, decbuff, length);

     if(g_currentModule)
     {
           
           int numsamples = length / 2 / 2;
           
           nSamplesResults = ModifySamplesProc(g_currentModule,decbuff,numsamples,16,2,44100);
           
           newLength = nSamplesResults*2*2;
     }
     else
     {
           nSamplesResults = length /2 /2;
           newLength = length;
     }

     if(newLength < length)
     {
           //read ahead to get enough data to fill buffer
           //then advance the buffer
           newLength = length;
     
     }
     else if(newLength > length)
     {
           //buffer extra data for next time
           extrabuff = NULL;
           extrabuff = (short*)realloc(extrabuff, newLength-length);
           memcpy(extrabuff, decbuff+length, newLength-length);
           newLength = length;
     }
     else
     {
             newLength = length;
     }

     memcpy(buffer, decbuff, newLength);

     return newLength;
}

Ian @ un4seen

  • Administrator
  • Posts: 20400
Re: dsp plugin returns less samples than buffer si
« Reply #1 on: 12 Mar '03 - 13:03 »
This would be my quick (untested :D) guess...

Code: [Select]
#define decblock 10000 // bytes to decode at a time
char excess[decblock*2]; // excess buffer
int exlen=0;

DWORD CALLBACK MyStreamProc(HSTREAM handle,char *buffer,DWORD length,DWORD user)
{
     char decbuf[decblock*2]; // decoding buffer - winamp DSP returns at most double the data?
     int c=length;
     if (exlen) { // excess left from last time
           int a=min(c,exlen);
           memcpy(buffer,excess,a);
           exlen-=a;
           if (exlen) memmove(excess,excess+a,exlen); // some excess remaining
           buffer+=a; // advance buffer
           c-=a;
     }
     while (c) {
           int a=BASS_ChannelGetData(decodehandle, decbuf, decblock);
           if (a==-1) break; // end of data (you may want to continue processing for effect tail)
           a=4*ModifySamplesProc(g_currentModule,decbuf,a/4,16,2,44100);
           if (a>c) { // some excess data, keep it for next time
                 memcpy(buffer,decbuf,c);
                 exlen=a-c;
                 memcpy(excess,decbuf+c,exlen);
                 c=0; // all done
           } else {
                 memcpy(buffer,decbuf,a);
                 buffer+=a; // advance buffer
                 c-=a;
           }
     }
     return length-c;
}


RevG

  • Posts: 454
Re: dsp plugin returns less samples than buffer si
« Reply #2 on: 13 Mar '03 - 13:42 »
Thanks, Ian! This helps big time! I will give this a shot. If and when I get it working properly I will post a link to a zip file that contains an example of how to add full winamp dsp support to bass.