waveout杂音 。 帮我看下这段代码,琢磨好久没有解决

#pragma comment(lib, "winmm.lib")
#include
#include"wavheader.h"
#include
#include
#include
#include

#include
using namespace std;
#define datesize riff.dwRiffSize-44
#define buffer 1024*4
U16 *pALL_Late;
LPSTR pwaveher[3];
U16 I=-1;
static void CALLBACK waveOutProc( HWAVEOUT hWaveOut, UINT uMsg,DWORD_PTR dwInstance,DWORD_PTR dwParam1, DWORD_PTR dwParam2);

PDWORD SPEED_A;
DWORD SPEED_1;

HWAVEOUT hWaveOu;

int main(int argc, char* argv[])
{
HWAVEOUT hWaveOut=NULL;

WAVEFORMATEX wfx;

PUINT p; /* 保存id的指针 /
UINT idnew; /
id值 /
p=&idnew; /
******************************************************************************** /
LPWAVEOUTCAPSW Poutnews; /
********************************************************************************** /
WAVEOUTCAPSW OUTmew; /
/
Poutnews =& OUTmew ; /
*/
wfx.nSamplesPerSec = 44100;
wfx.wBitsPerSample = 16;
wfx.nChannels = 2;

wfx.cbSize = 0;
wfx.wFormatTag = WAVE_FORMAT_PCM;
wfx.nBlockAlign =4;
wfx.nAvgBytesPerSec =4410*4;//缓冲

waveOutGetPlaybackRate (hWaveOut,SPEED_A);
SPEED_1=(U16)SPEED_A;
//waveOutSetPlaybackRate(hWaveOut,
cout<<endl<<SPEED_1;
waveOutOpen(&hWaveOut,WAVE_MAPPER, &wfx, (DWORD_PTR)waveOutProc,1, CALLBACK_FUNCTION ); //打开波形输出
if (waveOutOpen(&hWaveOut,WAVE_MAPPER, &wfx, (DWORD_PTR)waveOutProc, 0, CALLBACK_FUNCTION)==0)
{cout<<" 能打开音频设备";
};

/**//**//*/*//*******************************************************/
ifstream iofile("1.wav",ios::in|ios::binary|ios::out);//open the wav
filebuf *pbuf;
pbuf=iofile.rdbuf();

if(!iofile.is_open())
cout<<"e";

iofile.read((char*)(&riff),sizeof(riff));

iofile.read((char*)(&fmt_block),sizeof(fmt_block));
iofile.read((char*)(&fact_block),sizeof(fact_block));
iofile.read((char*)(&date_block),sizeof(date_block));
U16 *ALL_Late=new U161024*1024*50;

pbuf->sgetn ((char*)ALL_Late,1024*1024*50);

pALL_Late=ALL_Late;
iofile.close();

hWaveOu=hWaveOut;
if((fmt_block.wBitsPerSample/16))
{ U16 i;U16 MAXnumberofnew;
MAXnumberofnew=(U16) ceil( float (riff.dwRiffSize/10485760));
//////*////////
// if( fmt_block.wChannels ) //声道判断

     // {

              //   U16* left_late= new U16[datesize/2]; //16bit 数据段/*************************************************************/
            //    U16* right_late= new U16[datesize/2];//**************************************************************/


           //   U32 i;
             //     cout<<ALL_Late[1]<<endl<<ALL_Late[20];
              //    for (i=0;i< 100/2;)
               //   {

             //     U16  alllate2i=ALL_Late[2*i];
              //    U16  alllate2i_1=ALL_Late[2*i+1];
            //      left_late[i]=alllate2i;
             //     right_late[i]=alllate2i_1;
             //     i++;
            //    } ;
//  for (i=0;i<2000;)   
//{cout<<ALL_Late[i]<<" ";
 // i++;
//};

     WAVEHDR waveher[3];
     pALL_Late=ALL_Late;

     for (i=0;i<3;)
 {    


     pwaveher[i]= (LPSTR)(ALL_Late+buffer*i);

    waveher[i].dwLoops=0;


  waveher[i].dwBufferLength=buffer;
  waveher[i].dwFlags=0;
  waveher[i].lpNext=NULL;
  waveher[i].dwBytesRecorded=0;
     waveOutPrepareHeader( hWaveOut,&waveher[i],sizeof(waveher)  );



       i++;
    };
 for (i=0;i<=100000;)

 {    
      waveher[0].lpData=pwaveher[0];
     waveOutWrite( hWaveOut,&waveher[0],sizeof(waveher)  );
 waveher[1].lpData=pwaveher[1];
     waveOutWrite( hWaveOut,&waveher[1],sizeof(waveher)  );
    waveher[2].lpData=pwaveher[2];
 waveOutWrite( hWaveOut,&waveher[2],sizeof(waveher)  );

i++;}

};}

static   void CALLBACK waveOutProc( HWAVEOUT hWaveOut, UINT uMsg, DWORD_PTR dwInstance, DWORD dwParam1, DWORD dwParam2 )

    {  
          LPWAVEHDR pWaveHeader = (LPWAVEHDR)dwParam1;

            if (WOM_DONE== uMsg)  
          {    

                I++; 
               if(I>=0)  
              {   switch(I%3)
               { case 0: pwaveher[0]+=buffer*3;break;
                 case 1:  pwaveher[1]+=buffer*3;break;
                 case 2:  pwaveher[2]+=buffer*3;break;
               }









            }
     }}

1个回答

回调函数 是不是那样写 ,而且我每次改变缓冲大小的时候,有时候重复,有时候跳段, 我怀疑这个声卡有个默认的缓冲区,但我不知道多大。现在我在缓冲设置4096 可以连续播放,但是有杂音。

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问