註冊會員
|
TNMUDP連線問題?
請問各位大大
我用TNMUDP來連線,傳送聲音
可是我傳送到接收端電腦時,傳送的電腦也為出現聲音,這是為什麼呢?
還有我用4個暫存器,每個暫存器大小是8000,再把8000分四次傳送(此時聲音會一直重複出現)不知是何原因造成的??
以下是程式
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
#include <windows.h>
#include <mmsystem.h>
#include <mmreg.h>
#include <string.h>
#define bufsize 8000
HWND hWnd;
HWAVEIN hwi;
HWAVEOUT hwo;
WAVEFORMATEX wfx;
WAVEHDR wavehdr_in,wavehdr_out;
char buf[4][bufsize];
void Sound_set_struct();
void Sound_initial_Record();
void Sound_initial_Play();
void CALLBACK waveInProc(HWAVEIN hwi, UINT uMsg, DWORD dwInstance,
DWORD dwParam1, DWORD dwParam2);
void SRecord();
int q,w=0;
int cnt;
int choosebuf=0;
int cnt1;
int choosebuf1=0;
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
Form1->SoundRx->RemoteHost = Form1->Edit1->Text;
Form1->SoundTx->RemoteHost = Form1->Edit1->Text;
GetIP(Edit1->Text.c_str());
q=0;
}
//---------------------------------------------------------------------------
void GetIP(char *temp)
{
Form1->SoundRx->RemoteHost=temp;
Form1->SoundTx->RemoteHost=temp;
}
//--------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
Sound_set_struct();
Sound_initial_Record();
Sound_initial_Play();
//SRecord();
Sound_initial_Record();
}
//---------------------------------------------------------------------------
void Sound_set_struct()
{
wfx.wFormatTag = WAVE_FORMAT_PCM;
wfx.nChannels = 1;
wfx.nSamplesPerSec = 8000;
wfx.wBitsPerSample = 8;
wfx.nBlockAlign = 1;
wfx.nAvgBytesPerSec = wfx.nSamplesPerSec*wfx.nBlockAlign;
wfx.cbSize = 0;
wavehdr_in.lpData =buf[0] ;
wavehdr_in.dwBufferLength =bufsize;
wavehdr_in.dwFlags =0;
wavehdr_out.lpData =buf[0] ;
wavehdr_out.dwBufferLength =bufsize;
wavehdr_out.dwFlags =0;
}
void Sound_initial_Record()
{
waveInOpen(&hwi,WAVE_MAPPER,&wfx,(DWORD)waveInProc,NULL,CALLBACK_FUNCTION);
waveInPrepareHeader(hwi,&wavehdr_in,sizeof(WAVEHDR));
waveInAddBuffer(hwi,&wavehdr_in,sizeof(WAVEHDR));
waveInStart(hwi);
}
void Sound_initial_Play()
{
waveOutOpen(&hwo,WAVE_MAPPER,&wfx,NULL,NULL,CALLBACK_NULL);
waveOutPrepareHeader(hwo,&wavehdr_out,sizeof(WAVEHDR));
}
void CALLBACK waveInProc(HWAVEIN hwi, UINT uMsg, DWORD dwInstance,
DWORD dwParam1, DWORD dwParam2)
{
switch(uMsg)
{
case WIM_OPEN:
break;
case WIM_DATA:
for(int i=0; i<4; i++)
{
cnt = 2000*i;
Form1->SoundTx->SendBuffer((buf[choosebuf]+cnt),2000,2000);
}
if (choosebuf != 3)
choosebuf++;
else
{
choosebuf = 0;
}
waveInUnprepareHeader(hwi,&wavehdr_in,sizeof(WAVEHDR)); //釋放錄音buffer
wavehdr_in.lpData =buf[choosebuf] ;
wavehdr_in.dwFlags =0;
wavehdr_in.dwBufferLength =bufsize;
waveInPrepareHeader(hwi,&wavehdr_in,sizeof(WAVEHDR));
waveInAddBuffer(hwi,&wavehdr_in,sizeof(WAVEHDR));
waveInStart(hwi);
w++;
Form1->Edit3->Text=w;
break;
}
}
//----------------------------------------------------------------------------
void __fastcall TForm1::SoundRxDataReceived(TComponent *Sender,
int NumberBytes, AnsiString FromIP, int Port)
{
for(int f=0; f<4; f++)
{
cnt1= f*2000;
SoundRx->ReadBuffer((buf[choosebuf1]+cnt1),2000,2000);
}
waveOutUnprepareHeader(hwo,&wavehdr_out,sizeof(WAVEHDR));
wavehdr_out.lpData = buf[choosebuf1] ;
wavehdr_out.dwBufferLength =bufsize;
waveOutPrepareHeader(hwo,&wavehdr_out,sizeof(WAVEHDR));
waveOutWrite(hwo,&wavehdr_out,sizeof(WAVEHDR));
q++;
Form1->Edit2->Text=q;
if (choosebuf1!= 3)
choosebuf1++;
else
choosebuf1=0;
}
//-------------------------------------------------------------------------
|