首页/技术开发/内容

API层完成语音录制

技术开发2024-06-13 阅读()
GMEM_SHARE, datasize) );

  memset(m_pWaveHdr.lpData, 0, datasize );

  m_pWaveHdr.dwBufferLength = datasize;

  m_pWaveHdr.dwBytesRecorded = 0;

  m_pWaveHdr.dwUser = 0;

  m_pWaveHdr.dwFlags = 0;

  m_pWaveHdr.dwLoops = 0;





  wsprintf( lpTemp, "WAVEHDR size = %lu", sizeof(WAVEHDR) );

  log.write(lpTemp);





int resPrepare = waveInPrepareHeader( m_hWaveIn, &m_pWaveHdr, sizeof(WAVEHDR) ); //准备内存块录音



if ( resPrepare == MMSYSERR_NOERROR) log.write("准备录音用头文件成功");

  else  {

     wsprintf(lpTemp, "不能开辟录音头文件,Error_Code = 0x%03X", resPrepare );

     log.write(lpTemp);

} // End of 验证开辟缓冲







  resPrepare = waveInAddBuffer( m_hWaveIn, &m_pWaveHdr, sizeof(WAVEHDR) );



if ( resPrepare == MMSYSERR_NOERROR) log.write("准备录音用内存成功");

  else  {

     wsprintf(lpTemp, "不能开辟录音用缓冲,Error_Code = 0x%03X", resPrepare );

     log.write(lpTemp);

} // End of 验证开辟缓冲









  log.write("");  // 写入空字符串可以分行



  

  if (! waveInStart(m_hWaveIn) ) log.write("开始录音"); else log.write("开始录音失败");



  Sleep(3200);





MMTIME mmt;

mmt.wType = TIME_BYTES;

log.numberwrite( "sizeof(MMTIME) =", sizeof(MMTIME) );

log.numberwrite( "sizeof(UINT) =", sizeof(UINT) );



if ( waveInGetPosition(m_hWaveIn, &mmt, sizeof(MMTIME)) ) log.write("不能取得音频长度");

  else log.numberwrite( "取得现在音频位置 =", mmt.u.cb );



if (mmt.wType == TIME_BYTES) { log.write("得到的 TIME_BYTES 格式的音频长度"); }

   else log.write("指定的 TIME_BYTES 格式音频长度不支持");





//  if (! waveInStop(m_hWaveIn) )  log.write("停止录音"); else   log.write("停止录音失败");



  if (! waveInReset(m_hWaveIn) )  log.write("重置内存区成功"); else log.write("重置内存区失败");





m_pWaveHdr.dwBytesRecorded = mmt.u.cb;







DWORD NumToWrite=0;  DWORD dwNumber = 0;



HANDLE FileHandle =

       CreateFile( "myTest.wav", GENERIC_WRITE, FILE_SHARE_READ, NULL,

                   CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);



//  memset(m_pWaveHdr.lpData, 0, datasize);



dwNumber = FCC("RIFF");

WriteFile(FileHandle, &dwNumber, 4, &NumToWrite, NULL);



dwNumber = m_pWaveHdr.dwBytesRecorded + 18 + 20;

WriteFile(FileHandle, &dwNumber, 4, &NumToWrite, NULL);



dwNumber = FCC("WAVE");

WriteFile(FileHandle, &dwNumber, 4, &NumToWrite, NULL);



dwNumber = FCC("fmt ");

WriteFile(FileHandle, &dwNumber, 4, &NumToWrite, NULL);



dwNumber = 18L;

WriteFile(FileHandle, &dwNumber, 4, &NumToWrite, NULL);



WriteFile(FileHandle, &waveformat, sizeof(WAVEFORMATEX), &NumToWrite, NULL);



dwNumber = FCC("data");

WriteFile(FileHandle, &dwNumber, 4, &NumToWrite, NULL);



dwNumber = m_pWaveHdr.dwBytesRecorded;

WriteFile(FileHandle, &dwNumber, 4, &NumToWrite, NULL);



WriteFile(FileHandle, m_pWaveHdr.lpData, m_pWaveHdr.dwBytesRecorded, &NumToWrite, NULL);



SetEndOfFile(FileHandle);

CloseHandle( FileHandle );    FileHandle = INVALID_HANDLE_VALUE; // 收尾关闭句柄



log.write("应该已生成 myTest.wav 文件");







  if ( waveInUnprepareHeader(m_hWaveIn, &m_pWaveHdr, sizeof(WAVEHDR)) ) log.write("Un_Prepare Header 失败"); else log.write("Un_Prepare Header 成功");



  if ( GlobalFree(GlobalHandle( m_pWaveHdr.lpData )) ) log.write("Global Free 失败"); else log.write("Global Free 成功");







if (res == MMSYSERR_NOERROR )  //关闭录音设备

if (waveInClose(m_hWaveIn)==MMSYSERR_NOERROR)log.write("正常关闭录音设备");

else log.write("非正常关闭录音设备");







    log.last(true);

    // ExitProcess(0);

    return 0;

}



// *******************  End of File  *****************************

第1页  第2页 

……

相关阅读