TGSM610=packed   record                 //自定义音频文件头
   wFormatTag:   Word;
   nChannels:   Word;
   nSamplesPerSec:   DWORD;
   nAvgBytesPerSec:   DWORD;
   nBlockAlign:   Word;
   wBitsPerSample:   Word;
   cbSize:   Word;
   wSamplesPerBlock:word;
end;var
  buf: array[0..65535] of byte;
  bufsize: Integer = 0;procedure TForm1.Button1Click(Sender: TObject);
var
  format: Pointer;
  GSM:TGSM610;
begin
  with   GSM   do
  begin
      wFormatTag:=$31;
      nChannels:=1;
      wBitsPerSample:=0;
      nSamplesPerSec:=8000;
      nBlockAlign:=65;
      nAvgBytesPerSec:=1625;
      cbSize:=2;
      wSamplesPerBlock:=$140;
  end;
  format:=@gsm;
  ACMWaveIn1.Open(format);
  ACMWaveOut1.Open(format);
end;procedure TForm1.ACMWaveIn1Data(data: Pointer; size: Integer);
begin
  copymemory(pchar(@buf)+bufsize, data, size);
  inc(bufsize, size);
  if bufsize>=1300 then
  begin
    ACMWaveOut1.PlayBack(@buf, bufsize);
    bufsize:=0;
  end;
end;
在传送的时候,如何控制延迟呀?
刚开始的时候是2秒,然后就是5秒,再往后就是10秒,15秒,20秒........
大家帮助出个主意,谢谢!

解决方案 »

  1.   

    客户端:
    procedure TForm1.Button1Click(Sender: TObject);
    var
      format: Pointer;
      GSM:TGSM610;
    begin
      with GSM do
      begin
          wFormatTag:=$31;
          nChannels:=1;
          wBitsPerSample:=0;
          nSamplesPerSec:=8000;
          nBlockAlign:=65;
          nAvgBytesPerSec:=1625;
          cbSize:=2;
          wSamplesPerBlock:=$140;
      end;
      format:=@gsm;
      ACMWaveIn1.Open(format);
      ACMwaveIn1.BufferSize:=1;
      ACMWaveOut1.Open(format);
    idTCPClient1.Host:='192.168.1.111';
    idTCPClient1.Port:=5000;
    if not idTCPClient1.Connected then
      idTCPClient1.Connect;
    end;procedure TForm1.ACMWaveIn1Data(data: Pointer; size, volume: Integer);
    begin
      if idTCPClient1.Connected then
      begin
        copymemory(pchar(@buf)+bufsize, data, size);
        inc(bufsize, size);
        if bufsize>=800 then
        begin
          idTCPClient1.WriteInteger(bufsize);
          idTCPClient1.WriteBuffer(buf,bufsize);
          bufsize:=0;
        end;
      end;
    end;
    =========================
    服务端:
    procedure TForm1.FormCreate(Sender: TObject);
    var
      format: Pointer;
      GSM:TGSM610;
    begin
      with GSM do
      begin
          wFormatTag:=$31;
          nChannels:=1;
          wBitsPerSample:=0;
          nSamplesPerSec:=8000;
          nBlockAlign:=65;
          nAvgBytesPerSec:=1625;
          cbSize:=2;
          wSamplesPerBlock:=$140;
      end;
      format:=@gsm;
      ACMWaveOut1.Open(format);
     idTCPServer1.DefaultPort:=5000;
     idTCPServer1.Active:=true;
    end;procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
    begin
    idTCPServer1.Active:=false;
    end;procedure TForm1.Button1Click(Sender: TObject);
    begin
    idTCPServer1.Active:=false;
    end;procedure TForm1.IdTCPServer1Execute(AThread: TIdPeerThread);
    var
      AudioStream:TMemoryStream;
      AudioBufferLen:integer;
    begin
      if not AThread.Terminated and AThread.Connection.Connected then
      begin
        AudioStream := TMemoryStream.Create;
        AudioBufferLen := AThread.Connection.ReadInteger;
        AudioStream.Size := AudioBufferLen;
        AThread.Connection.ReadBuffer(AudioStream.Memory^, AudioBufferLen);
        ACMWaveOut1.PlayBack(AudioStream.Memory,AudioBufferLen);
        AudioStream.Free;
      end;
    end;
    传送没有问题,能收到,可是就是延迟,而且越来越长
    如何解决?
      

  2.   

    我没怎么看你的代码,我自己写了个简单的测试了一下,没问题。客户端:
    type
      TGSM610=packed   record                 //自定义音频文件头
       wFormatTag:   Word;
       nChannels:   Word;
       nSamplesPerSec:   DWORD;
       nAvgBytesPerSec:   DWORD;
       nBlockAlign:   Word;
       wBitsPerSample:   Word;
       cbSize:   Word;
       wSamplesPerBlock:word;
      end;var
      buf: array[0..65535] of byte;
      bufsize: Integer = 0;procedure TForm1.FormCreate(Sender: TObject);
    begin
      idtcpclient1.Connect;
    end;procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
    begin
      idtcpclient1.Disconnect;
    end;procedure TForm1.Button1Click(Sender: TObject);
    var
      format: Pointer;
      GSM:TGSM610;
    begin
      with   GSM   do
      begin
          wFormatTag:=$31;
          nChannels:=1;
          wBitsPerSample:=0;
          nSamplesPerSec:=8000;
          nBlockAlign:=65;
          nAvgBytesPerSec:=1625;
          cbSize:=2;
          wSamplesPerBlock:=$140;
      end;
      format:=@gsm;
      ACMWaveIn1.Open(format);
    end;procedure TForm1.ACMWaveIn1Data(data: Pointer; size: Integer);
    begin
      copymemory(pchar(@buf)+bufsize, data, size);
      inc(bufsize, size);
      if bufsize>=1300 then
      begin
        IdTCPClient1.WriteBuffer(buf, bufsize);
        bufsize:=0;
      end;
    end;服务器:
    type
      TGSM610=packed   record                 //自定义音频文件头
       wFormatTag:   Word;
       nChannels:   Word;
       nSamplesPerSec:   DWORD;
       nAvgBytesPerSec:   DWORD;
       nBlockAlign:   Word;
       wBitsPerSample:   Word;
       cbSize:   Word;
       wSamplesPerBlock:word;
      end;procedure TForm1.FormCreate(Sender: TObject);
    var
      format: Pointer;
      GSM:TGSM610;
    begin
      with   GSM   do
      begin
          wFormatTag:=$31;
          nChannels:=1;
          wBitsPerSample:=0;
          nSamplesPerSec:=8000;
          nBlockAlign:=65;
          nAvgBytesPerSec:=1625;
          cbSize:=2;
          wSamplesPerBlock:=$140;
      end;
      format:=@gsm;
      ACMWaveOut1.Open(format);
    end;procedure TForm1.IdTCPServer1Execute(AThread: TIdPeerThread);
    var
      buf: array of byte;
      count: Integer;
    begin
      try
        count:=AThread.Connection.ReadFromStack;
        if count<>0 then
        begin
          setlength(buf, count);
          AThread.Connection.ReadBuffer(buf[0], count);
          ACMWaveOut1.PlayBack(buf, count);
        end;
        Memo1.Lines.Add(inttostr(count));
      finally  end;
    end;另外,建议你用udp协议传输这种实时数据。