一个是视频的,一个关于语音的,都是网上收集到的.源代码

一个是视频的,一个关于语音的,都是网上收集到的.源代码

#pragma hdrstop

#include "Unit1.h"
#include "vfw.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
HWND hWndC;
CAPDRIVERCAPS CapDrvCaps;
CAPSTATUS CapStatus;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
    : TForm(Owner)
{
}
//---------------------------------------------------------------------------

void __fastcall TForm1::FormShow(TObject *Sender)
{
    char szDeviceName[80];
    char szDeviceVersion[80];

    for (int wIndex = 0; wIndex < 10; wIndex++)
    {
        if (capGetDriverDescription (wIndex, szDeviceName,
            sizeof (szDeviceName), szDeviceVersion,
            sizeof (szDeviceVersion)))
        {
            ComboBox1->Items->Add(szDeviceName);
        }
    }
   
    if(ComboBox1->Items->Count>0)
        ComboBox1->ItemIndex=0;
    else
    {
        ShowMessage("没有找到视频软件");
        Close();
    }
    Button1Click(NULL);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
    hWndC = capCreateCaptureWindow (
        (LPSTR) "My Capture Window", // window name if pop-up
        WS_CHILD | WS_VISIBLE, // window style
        0, 0, 160, 120, // window position and dimensions
        (HWND)Panel2->Handle,//(HWND)Application->Handle,//(HWND) hwndParent,
        ComboBox1->ItemIndex+1);//(int) nID /* child ID */);

    //连接设备:
    capDriverConnect(hWndC,0);
    capPreviewRate(hWndC, 50); // rate, in milliseconds
    capPreview(hWndC, TRUE); // starts preview

    //获取视频驱动相关性能
    capDriverGetCaps(hWndC, &CapDrvCaps, sizeof (CAPDRIVERCAPS));

    //获取捕获窗口状态
    capGetStatus(hWndC, &CapStatus, sizeof (CAPSTATUS));
    SetWindowPos(hWndC, NULL,
        0,
        0,
        CapStatus.uiImageWidth,
        CapStatus.uiImageHeight, SWP_NOZORDER | SWP_NOMOVE);
}
//---------------------------------------------------------------------------

void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action)
{
    if(hWndC)
    {
        capPreview(hWndC, FALSE); // starts preview
        capDriverDisconnect (hWndC);
    }
}
//---------------------------------------------------------------------------


void __fastcall TForm1::Button2Click(TObject *Sender)
{
    // Video format dialog box.
    if (CapDrvCaps.fHasDlgVideoFormat)
    {
        capDlgVideoFormat(hWndC);
        // Are there new image dimensions?
        capGetStatus(hWndC, &CapStatus, sizeof (CAPSTATUS));
        SetWindowPos(hWndC, NULL,
            0,
            0,
            CapStatus.uiImageWidth,
            CapStatus.uiImageHeight, SWP_NOZORDER | SWP_NOMOVE);
    }
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button3Click(TObject *Sender)
{
    // Video source dialog box.
    if (CapDrvCaps.fHasDlgVideoSource)
        capDlgVideoSource(hWndC);
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button4Click(TObject *Sender)
{
    //设置相关参数
    CAPTUREPARMS CaptureParms;
    float FramesPerSec = 30.0;

    capCaptureGetSetup(hWndC, &CaptureParms, sizeof(CAPTUREPARMS));

    CaptureParms.dwRequestMicroSecPerFrame = (DWORD) (1.0e6 /
        FramesPerSec);
    capCaptureSetSetup(hWndC, &CaptureParms, sizeof (CAPTUREPARMS));
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button5Click(TObject *Sender)
{
    //设置预览时的比例
    capPreviewScale(hWndC, 1);
    //设置预览时的帧频率
    capPreviewRate(hWndC,66);
    //如果要捕获视频流,则要使用函数指定不生成文件。否则将会自动生成AVI文件
    capCaptureSequenceNoFile(hWndC);
    //指定是否使用叠加模式,使用为1,否则为0
    capOverlay(hWndC, 1);
   
    //Video display dialog box.
    if (CapDrvCaps.fHasDlgVideoDisplay)
        capDlgVideoDisplay(hWndC);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button6Click(TObject *Sender)
{
    if(SaveDialog2->Execute())
        capFileSaveDIB( hWndC, (SaveDialog2->FileName+".bmp").c_str() ); //截取当前帧
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button7Click(TObject *Sender)
{
    if(Button7->Caption == "开始捕获")
    {
        if(SaveDialog1->Execute())
        {
            // Set up the capture operation.
            capCaptureSequence(hWndC);
            // Capture.
            capFileSaveAs(hWndC, (SaveDialog1->FileName+".avi").c_str());//视频
        }
        Button7->Caption="停止捕获";
    }
    else
    {
        Button7->Caption="开始捕获";
        capCaptureStop(hWndC);
    }
}
//------------------------------------------------------------------------------------------------------------------------

1.语音的采集和播放。
  本软件中要把语音直接转换为数据,放在内存中,而不是存为语音文件,而且播放语音时,也是直接播放语音数据,而不是播放语音文件。这样的好处前面已经提到,即省略了读写硬盘的费时操作,提高了语音通话的实时性。
  要完成上述语音操作,编程语言中提供的容易使用的高级的多媒体语音函数是无法胜任的,只能用一些底层的语音函数来实现,这一类函数和结构的名字都以"wave"作为前缀。
  下面简要分析录音和放音的流程,考虑到两着的处理和流程基本上是类似的,本文就只以录音为例来分析,如图三为录音流程。录音的准备工作主要是三点,打开录音设备,获得录音句柄,指定录音格式,分配若干用于录音的内存,内存的大小和数量下文将进一步分析。开始录音时,先将所有内存块都提供给录音设备用来录音,录音设备就会依次将语音数据写入内存,当一块内存写满,录音设备就会发一个Window 消息MM_WIM_DATA给相应的窗口,通知程序作相关的处理,这时程序通常的处理是把内存中的数据进行复制,如写入文件等,在此我们的处理是把数据进行压缩和网络发送,然后把内存置空,返还给录音设备进行录音,这样就形成一个循环不息的录音过程。结束录音时就释放所有内存块,关闭录音设备。

录音流程 

以录音为例,关键的录音函数和顺序如下:
WAVEFORMATEX waveformat;
waveformat.wFormatTag=WAVE_FORMAT_PCM;
waveformat.nChannels=1;
waveformat.nSamplesPerSec=8000;
waveformat.nAvgBytesPerSec=16000;
waveformat.nBlockAlign=2;
waveformat.cbSize=0;
waveformat.wBitsPerSample=16; //指定录音格式
int res=waveInOpen(&m_hWaveIn,WAVE_MAPPER, &waveformat, (DWORD)m_hWnd,0L,CALLBACK_WINDOW); //打开录音设备
waveInPrepareHeader(m_hWaveIn,m_pWaveHdr[i],sizeof(WAVEHDR)); //准备内存块录音
waveInAddBuffer(m_hWaveIn,m_pWaveHdr[i],sizeof(WAVEHDR)); //增加内存块
waveInStart(m_hWaveIn);//开始录音
waveInStop(m_hWaveIn); //停止录音
waveInReset(m_hWaveIn); //清空内存块
waveInClose(m_hWaveIn); //关闭录音设备

完整源代码
WaveInput.h
#ifndef WaveInputH
#define WaveInputH
#include
//----------

LPWAVEHDR AllocateInputLPWAVEHDRMemory(UINT size);
void FreeInPutBuffer(HWAVEIN hwi,LPWAVEHDR pwh);
//------------------
#endif


WaveInput.cpp
#include
#pragma hdrstop
#include "WaveInput.h"
//-------------------------------------------------------------------
LPWAVEHDR AllocateInputLPWAVEHDRMemory(UINT size)
{HPSTR sublpData;
HGLOBAL lpmemory;
LPWAVEHDR lpInWaveHdr;
lpmemory=GlobalAlloc(GMEM_MOVEABLE|GMEM_SHARE,size);
if(lpmemory==NULL)
ShowMessage("Failed to GloabalAlloc memory for data sublpData");
if((sublpData=(HPSTR)GlobalLock(lpmemory))==NULL)
{::MessageBox(0,"Failed to lock memory for data sublpData",NULL,MB_OK|MB_ICONEXCLAMATION);
}
lpmemory=GlobalAlloc(GMEM_MOVEABLE|GMEM_SHARE,(DWORD)sizeof(WAVEHDR));
if(lpmemory==NULL)ShowMessage("Failed to globalAlloc memory for data LPWAVEHDR");
lpInWaveHdr=(LPWAVEHDR)GlobalLock(lpmemory);
if(lpInWaveHdr==NULL)
{::MessageBox(0,"failed to lock memory for header:lplpInWaveHdr",NULL,MB_OK|MB_ICONEXCLAMATION);
}
lpInWaveHdr->lpData=sublpData;
lpInWaveHdr->dwBufferLength=size;
lpInWaveHdr->dwLoops=0L;
lpInWaveHdr->dwFlags=0L;
sublpData=NULL;
lpmemory=NULL;
return(lpInWaveHdr);
}
//--------------------------
void FreeInPutBuffer(HWAVEIN hwi,LPWAVEHDR pwh)
{
if(waveInUnprepareHeader(hwi,pwh,sizeof(WAVEHDR))!=MMSYSERR_NOERROR)
ShowMessage("WaveInUnperpareHeader ERROR!");
try
{
delete pwh->lpData;
delete pwh;
}
catch(...)
{ShowMessage("delete pwh error!");
}
}
//----------------------------------------

WaveOutput.h

#ifndef WaveOutPutH
#define WaveOutPutH
#include
//-----------------------
LPWAVEHDR AllocateOutPutLPWAVEHDRMemory(HWAVEOUT phwo,UINT size);
void FreeOutPutBuffer(HWAVEOUT hwo,LPWAVEHDR pwh);
//----------------------------
#endif


WaveOutPut.cpp
#include
#pragma hdrstop
#include "WaveOutPut.h"
//------------------------
LPWAVEHDR AllocateOutPutLPWAVEHDRMemory(HWAVEOUT phwo,UINT size)
{
LPWAVEHDR lpOutWaveHdr;
lpOutWaveHdr=(LPWAVEHDR)GlobalLock(GlobalAlloc(GMEM_MOVEABLE|GMEM_SHARE,sizeof(WAVEHDR)));
if(lpOutWaveHdr==NULL)
::MessageBox(0,"Failed to lock memory for header",NULL ,MB_OK|MB_ICONEXCLAMATION);
if((lpOutWaveHdr->lpData=(HPSTR)GlobalLock(GlobalAlloc(GMEM_MOVEABLE|GMEM_SHARE,size)))==NULL)
::MessageBox(0,"Failed to lock memory for data chumk",NULL,MB_OK|MB_ICONEXCLAMATION);
lpOutWaveHdr->dwBufferLength=size;
lpOutWaveHdr->dwFlags=0L;
lpOutWaveHdr->dwLoops=0L;
waveOutPrepareHeader(phwo,lpOutWaveHdr,sizeof(WAVEHDR));
return(lpOutWaveHdr);
}
//---------------------------------------------------------------------
void FreeOutPutBuffer(HWAVEOUT hwo,LPWAVEHDR pwh)
{
switch(waveOutUnprepareHeader(hwo,pwh,sizeof(WAVEHDR)))
{case MMSYSERR_INVALHANDLE:
ShowMessage("MMSYSERR_INVALHANDLE");return;
case MMSYSERR_NODRIVER:
ShowMessage("MMSYSERR_NODRIVE");return;
case MMSYSERR_NOMEM:
ShowMessage("MMSYSERR_NOMEM");return;
case WAVERR_STILLPLAYING:
ShowMessage("WAVERR_STILLPLAYING");return;
}
try{
delete pwh->lpData;
delete pwh;
}
catch(...)
{ShowMessage("delete 'pwh'error!");
}
}


NetTelMain.h
//---------------------------------------------------------------------------

#ifndef NetTelMainH
#define NetTelMainH
//---------------------------------------------------------------------------
#define NETTEL_PORT 2001
//---------------------------------------------------------------------------
class TNetTelForm : public TForm
{
__published: // IDE-managed Components
TServerSocket *ServerSocket1;
TClientSocket *ClientSocket1;
TMainMenu *MainMenu1;
TLabel *Label_IP;
TEdit *Edit_IP;
TPanel *Panel1;
TGroupBox *VolumeGroupBox;
TTrackBar *VolumeBar;
TStatusBar *StatusBar1;
TMenuItem *N1;
TMenuItem *N2;
TMenuItem *N3;
TMenuItem *N4;
TMenuItem *N5;
TMenuItem *N6;
TMenuItem *N7;
void __fastcall ServerSocket1ClientRead(TObject *Sender,
TCustomWinSocket *Socket);
void __fastcall N2Click(TObject *Sender);
void __fastcall N3Click(TObject *Sender);
void __fastcall N4Click(TObject *Sender);
void __fastcall N5Click(TObject *Sender);
void __fastcall N6Click(TObject *Sender);
void __fastcall VolumeBarChange(TObject *Sender);
private:
WAVEFORMATEX pcmWaveFormat;
HWAVEIN phwi;
LPWAVEHDR lpInWaveHdr[2];
bool IsNeedSwap,IsPaused,WhichToPlay,OutputDeviceUnOpend;
HWAVEOUT pwho;
LPWAVEHDR lpOutWaveHdr[2];
DWORD Volume;
long MsgNumber,timebegin,timeend;
int getwhichtime,ReceiveLength;
void _fastcall InitializeOutputDevice();
void _fastcall CallBackPlay(TMessage*Message);
void _fastcall InitializeInputDevice();

public:

BEGIN_MESSAGE_MAP
MESSAGE_HANDLER(MM_WIM_DATA,TMessage* ,CallBackPlay)
END_MESSAGE_MAP(TComponent)
__fastcall TNetTelForm(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TNetTelForm *NetTelForm;
//---------------------------------------------------------------------------
#endif
NetTelMain.cpp
//---------------------------------------------------------------------------

#include
#pragma hdrstop

#include "NetTelMain.h"
#include "WaveInput.h"
#include "WaveOutPut.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TNetTelForm *NetTelForm;
//---------------------------------------------------------------------------
__fastcall TNetTelForm::TNetTelForm(TComponent* Owner)
: TForm(Owner)
{
IsNeedSwap=true;
IsPaused=true;
WhichToPlay=true;
OutputDeviceUnOpend=true;
getwhichtime=0;
timebegin=timeend=0;
MsgNumber=0;
ReceiveLength=0;
InitializeInputDevice();
ServerSocket1->Port=2001;
ServerSocket1->Active=true;

}
//---------------------------------------------------------------------------
void _fastcall TNetTelForm::InitializeInputDevice()
{
//set up pcm wave format for 11 kHz 8-bit mono.
pcmWaveFormat.wFormatTag=WAVE_FORMAT_PCM;
pcmWaveFormat.nChannels=1;
pcmWaveFormat.nSamplesPerSec=11025L;
pcmWaveFormat.nAvgBytesPerSec=11025L;
pcmWaveFormat.nBlockAlign=1;
pcmWaveFormat.wBitsPerSample=8;
pcmWaveFormat.cbSize=0;
//open the output device

switch(waveInOpen(&phwi,WAVE_MAPPER,&pcmWaveFormat,(unsigned long)Handle,0,CALLBACK_WINDOW))
{ case MMSYSERR_ALLOCATED:
ShowMessage("MMSYSERR_ALLOCTEC");return;
case MMSYSERR_BADDEVICEID:
ShowMessage("MMSYSERR_BADDEVICEID");return;
case MMSYSERR_NODRIVER:
ShowMessage("MMSYSERR_NODRIVER");return;
case WAVERR_STILLPLAYING:
ShowMessage("WAVERR_STILLPLAYING");return;
case WAVERR_BADFORMAT:
ShowMessage("WAVERR_BADFORMAT");return;
}
// ALLOCATE MEMORY AND PERPARE FOR RECORDING
for(int i=0;i<=1;i++)
{lpInWaveHdr[i]=AllocateInputLPWAVEHDRMemory(4*1024);
switch(waveInPrepareHeader(phwi,lpInWaveHdr[i],sizeof(WAVEHDR)))
{case MMSYSERR_INVALHANDLE:
ShowMessage("MMSYSERR_INVALHANDLE");return;
case MMSYSERR_NODRIVER:
ShowMessage("MMSYSERR_NODRIVER");return;
case MMSYSERR_NOMEM:
ShowMessage("MMSYSERR_NOMEM");return;
}
}
switch(waveInAddBuffer(phwi,lpInWaveHdr[!IsNeedSwap],sizeof(WAVEHDR)))

{case MMSYSERR_INVALHANDLE:
ShowMessage("MMSYSERR_INVALHANDLE");return;
case MMSYSERR_NODRIVER:
ShowMessage("MMSYSERR_NODRIVER");return;
case MMSYSERR_NOMEM:
ShowMessage("MMSYSERR_NOMEM");return;
case WAVERR_UNPREPARED:
ShowMessage("WAVE_UNPREPAPERD");return;
}

}
//------------------------------------------------
void _fastcall TNetTelForm::CallBackPlay(TMessage *Message)
{
switch(waveInAddBuffer(phwi,lpInWaveHdr[!IsNeedSwap],sizeof(WAVEHDR)))
{ case WAVERR_UNPREPARED:
ShowMessage("WAVE_UNPREPAPERD");return;
case MMSYSERR_INVALHANDLE:
ShowMessage("MMSYSERR_INVALHANDLE");return;
case MMSYSERR_NODRIVER:
ShowMessage("MMSYSERR_NODRIVER");return;
case MMSYSERR_NOMEM:
ShowMessage("MMSYSERR_NOMEM");return;
}
ClientSocket1->Socket->SendBuf(lpInWaveHdr[!IsNeedSwap]->lpData,lpInWaveHdr[!IsNeedSwap]->dwBufferLength);
IsNeedSwap=!IsNeedSwap;
}
//------------------------
void _fastcall TNetTelForm::InitializeOutputDevice()
{if(waveOutOpen(&pwho,WAVE_MAPPER,&pcmWaveFormat,(unsigned long)Handle,0,CALLBACK_WINDOW)!=MMSYSERR_NOERROR)
::MessageBox(0,"out Open error","",MB_OK);
for(int i=0;i<=1;i++)
lpOutWaveHdr[i]=AllocateOutPutLPWAVEHDRMemory(pwho,4*1024);
Volume=Volume&0x00000000;
Volume=Volume^((DWORD)0xFFFF/2);
Volume=Volume^(((DWORD)0xFFFF/2)<<16);
if(waveOutSetVolume(pwho,Volume)!=MMSYSERR_NOERROR)
ShowMessage("waveOutSetVolume error!");
}

void __fastcall TNetTelForm::ServerSocket1ClientRead(TObject *Sender,
TCustomWinSocket *Socket)
{
if(OutputDeviceUnOpend)
{ InitializeOutputDevice();
OutputDeviceUnOpend=false;
}
try{ReceiveLength=Socket->ReceiveLength();
Socket->ReceiveBuf(lpOutWaveHdr[WhichToPlay]->lpData,ReceiveLength);
StatusBar1->Panels->Items[1]->Text=AnsiString("Packages:")+MsgNumber++;
if(waveOutWrite(pwho,lpOutWaveHdr[WhichToPlay],sizeof(WAVEHDR))==MMSYSERR_NOERROR)
StatusBar1->Panels->Items[0]->Text=AnsiString("Status:")+(int)WhichToPlay;
StatusBar1->Panels->Items[2]->Text=AnsiString("PSize:")+ReceiveLength;
WhichToPlay=!WhichToPlay;}
catch(...)
{StatusBar1->Panels->Items[0]->Text="WaveOut ERRor";}
}
//---------------------------------------------------------------------------

void __fastcall TNetTelForm::ConnectClick(TObject *Sender)
{
ClientSocket1->Active=false;
ClientSocket1->Address=Edit_IP->Text;
ClientSocket1->Port=2001;
ClientSocket1->Active=true;
}
//---------------------------------------------------------------------------

void __fastcall TNetTelForm::StartClick(TObject *Sender)
{
IsPaused=false;
waveInStart(phwi);
}
//---------------------------------------------------------------------------

void __fastcall TNetTelForm::PauseClick(TObject *Sender)
{
if(!IsPaused)
waveInStop(phwi);
}
//---------------------------------------------------------------------------

void __fastcall TNetTelForm::ResumeClick(TObject *Sender)
{
waveInStart(phwi);
}
//---------------------------------------------------------------------------

void __fastcall TNetTelForm::StopClick(TObject *Sender)
{
ServerSocket1->Active=false;
ClientSocket1->Active=false;
IsPaused=true;
waveInReset(phwi);
waveInStop(phwi);
for(int i=0;i<=1;i++) FreeInPutBuffer(phwi,lpInWaveHdr[i]);
waveInClose(phwi);
waveOutReset(pwho);
waveOutBreakLoop(pwho);
for(int i=0;i<=1;i++)FreeOutPutBuffer(pwho,lpOutWaveHdr[i]);
waveOutClose(pwho);
StatusBar1->Panels->Items[0]->Text="Is Stoped";
}
//---------------------------------------------------------------------------

void __fastcall TNetTelForm::VolumeBarChange(TObject *Sender)
{
Volume=Volume&0x00000000;
Volume=Volume^((DWORD)VolumeBar->Position*0xFFFF/100);
Volume=Volume^((DWORD)VolumeBar->Position*0xFFFF/100<<16);
if(waveOutSetVolume(pwho,Volume)!=MMSYSERR_NOERROR)
ShowMessage("waveoutsetvolumeerror");
}
//---------------------------------------------------------------------------

void __fastcall TNetTelForm::ExitClick(TObject *Sender)
{ if(!IsPaused)
{StopClick(Sender);}
Close();
}

void __fastcall TNetTelForm::FormDestroy(TObject *Sender)
{ExitClick(Sender);
}

void __fastcall TNetTelForm::TerminatedClick(TObject *Sender)
{ delete &phwi;
ExitClick(Sender);
TerminateProcess(GetCurrentProcess(),THREAD_SUSPEND_RESUME);
}

void __fastcall TNetTelForm::ClientSocket1Connect(TObject *Sender,TCustomWinSocket *Socket)
{StatusBar1->Panels-Items[0]->Text="Connect OK!";}

void __fastcall TNetTelForm::ClientSocket1Error(TObject *Sender,TCustomWinSocket *Socket,TErrorEvent ErrorEvent,int &ErrorCode)
{Socket->Close();}

void __fastcall TNetTelForm::ServerSocket1Error(TObject *Sender,TCustomWinSocket *Socket,TErrorEvent ErrorEvent,int &ErrorCode)
{Socket->Close();}
语音传输设置
为了减少语音延迟,语音抖动,提高压缩比.设置如下:
声音源 采样率为22.050KHZ,16位,单声道.
22.050*2*1=44.1KB/S
语音延迟最佳为0.1秒,所以内存缓存大小为4-5KB
提高压缩比:录制45.5秒的声音大小为1960KB
在C466,64MB采用解霸2000,22.050KHZ,128千位/S,用了14秒,mp3大小为709KB.
为了及时压缩需要P2-233MHZ系统,10MB网络

 

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/hangke/archive/2008/02/19/2107069.aspx

你可能感兴趣的:(socket,null,delete,dialog,callback,button)