Jump to content


latency and fuzzy echos in mic recording


5 replies to this topic

#1 rouncer

    Senior Member

  • Members
  • PipPipPipPip
  • 2334 posts

Posted 12 May 2012 - 01:41 PM

this code snippet takes capture from the mic and then pumps it to the output stream.
it has a little latency, and it has lots of bugs in the output.
if i switch it round and capture sound the frame before i write it, it cleans up but then theres even more latency, i need less latency as possible so i can use the computer as an amp for my guitar or voice... is this possible with direct sound?



void write_plays(void)
{
    
    
    
    LPVOID lpvPtr1;
 DWORD dwBytes1;
 LPVOID lpvPtr2;
 DWORD dwBytes2;

    LPVOID clpvPtr1;
 DWORD cdwBytes1;
 LPVOID clpvPtr2;
 DWORD cdwBytes2;


 static int offset=0;
 static int last_clock=timeGetTime();
 int clock=timeGetTime();
 
 int tp=clock-last_clock;
 
 int write=(int)((float)tp/1000.0f*44100.0f);
 
 static int cwp=0;


 HRESULT hr;

    DWORD cp,rp;

    capture->GetCurrentPosition(&cp,&rp);
    
    DWORD locksize;

    if(rp>cp)
    {
     locksize=(capture_size-rp)+cp;
    }
    else
    {
     locksize=cp-rp;
    }


    rwp=cwp;

    hr=capture->Lock(rp, locksize, &clpvPtr1, &cdwBytes1, &clpvPtr2, &cdwBytes2, 0);
 if(hr==S_OK)
    {
  SHORT* us=(SHORT*)clpvPtr1;
  SHORT* us2=(SHORT*)clpvPtr2;
  DWORD  b=(cdwBytes1>>1);
  DWORD  b2=(cdwBytes2>>1);


  int j;
  for(j=0;j<locksize;j++)
  {    
   int wab;
            if(j*2<(signed)B)
   {
       wab=us[j*2+0];
    wab+=us[j*2+1];
    wab/=2;
          capture_copy[cwp]=(short)wab;
             cwp++;
                if(cwp==1000000) cwp=0;
            }
   else if((j*2-B)<b2)
   {
       wab=us2[(j*2-B)+0];
       wab+=us2[(j*2-B)+1];
    wab/=2;
          capture_copy[cwp]=(short)wab;
             cwp++;
                if(cwp==1000000) cwp=0;
   }
 
        }
  capture->Unlock(clpvPtr1, cdwBytes1, clpvPtr2, cdwBytes2);
    }

    DWORD pc,wc;

    primary->GetCurrentPosition(&pc,&wc);

 // Obtain write pointer.
 hr=primary->Lock(wc%primary_size, primary_size, &lpvPtr1, &dwBytes1, &lpvPtr2, &dwBytes2, 0);
 if(hr==S_OK)
 {
  SHORT* us=(SHORT*)lpvPtr1;
  SHORT* us2=(SHORT*)lpvPtr2;
  DWORD  b=(dwBytes1>>1);
  DWORD  b2=(dwBytes2>>1);


  int j;
  for(j=0;j<write;j++)
  {    
   int wa=0;

            int read=rwp+j;
            if(read>=1000000) read-=1000000;
            if(read<0) read+=1000000;
            wa+=capture_copy[read];

   if(wa>32766) wa=32766;
   if(wa<-32766) wa=-32766;
   short wab=(short)wa;

         ram_copy[ram_write]=wab;

            ram_write++;

            if(ram_write==100000000) ram_write=0;

//   short wab=saw;
         
   if(j*2<(signed)B)
   {
    us[j*2+0]=wab;
    us[j*2+1]=wab;
   }
   else if((j*2-B)<b2)
   {
    us2[(j*2-B)+0]=wab;
    us2[(j*2-B)+1]=wab;
   }
  }
  primary->Unlock(lpvPtr1, dwBytes1, lpvPtr2, dwBytes2);
 }


 offset+=write;
 last_clock=clock;
}


you used to be able to fit a game on a disk, then you used to be able to fit a game on a cd, then you used to be able to fit a game on a dvd, now you can barely fit one on your harddrive.

#2 }:+()___ (Smile)

    Member

  • Members
  • PipPipPip
  • 137 posts

Posted 12 May 2012 - 02:31 PM

I thied the same thing years ago and eventually give up: Windows simply don't have realtime sound API. There is some hacks used by JACK, but I dunno about sound cards coverage.
Sorry my broken english!

#3 rouncer

    Senior Member

  • Members
  • PipPipPipPip
  • 2334 posts

Posted 12 May 2012 - 03:47 PM

damno... yeh i cleared it up, but its a SECOND late, its bad latency... im not giving up yet tho, if i got to maybe a third of a second somehow...
you used to be able to fit a game on a disk, then you used to be able to fit a game on a cd, then you used to be able to fit a game on a dvd, now you can barely fit one on your harddrive.

#4 }:+()___ (Smile)

    Member

  • Members
  • PipPipPip
  • 137 posts

Posted 12 May 2012 - 04:20 PM

Human ear can hear delays of several tens of msec. 1/3 of second is as bad as 1. Try JACK API, if it works it'll be much better.
Sorry my broken english!

#5 Reedbeta

    DevMaster Staff

  • Administrators
  • 4979 posts
  • LocationBellevue, WA

Posted 12 May 2012 - 05:34 PM

I've been using VSTHost with ASIO4ALL for this. The ASIO driver decreases the latency quite a lot (although it's still noticable), and should work with most any Windows setup; you can use the free VST SDK to build your own audio processing plugins, though I haven't done this.
reedbeta.com - developer blog, OpenGL demos, and other projects

#6 rouncer

    Senior Member

  • Members
  • PipPipPipPip
  • 2334 posts

Posted 12 May 2012 - 05:57 PM

working with direct sound a bit more, i think i should be able to get it to 10ms, im pretty sure, ive just got to work out how the thing works... ill be back if im successful.
you used to be able to fit a game on a disk, then you used to be able to fit a game on a cd, then you used to be able to fit a game on a dvd, now you can barely fit one on your harddrive.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users