Solved

convert from buffer to string and from string to buffer

Posted on 2002-07-18
19
5,854 Views
Last Modified: 2012-06-21
Hi:
how i can convert buffer which contain voice to string ,and convert the string back to buffer?
shaza
0
Comment
Question by:shaza
  • 6
  • 5
  • 2
  • +5
19 Comments
 
LVL 20

Accepted Solution

by:
Madshi earned 50 total points
ID: 7161500
Do you mean voice recognition? Or do you just want to know how to convert any data buffer into a string? That's easy:

function BufToStr(var buf; bufSize: integer) : string;
begin
  SetLength(result, bufSize);
  Move(buf, pointer(result)^, bufSize);
end;

procedure StrToBuf(var buf; str: string);
begin
  Move(pointer(str)^, buf, Length(str));
end;

Of course the buffer must be big enough to hold all the string data in the StrToBuf function.

Regards, Madshi.
0
 
LVL 11

Expert Comment

by:robert_marquardt
ID: 7161512
Voice? Do you mean speech recognition?
Forget it!

If you mena simple string operations:

var
  Buffer: array [0..1023] of Char;
  S: string;
begin
  //... fill buffer

  // copy buffer to string
  S := Buffer;
  // copy string to buffer
  StrCopy(Buffer, PChar(S));
  // with length check
  StrLCopy(Buffer, PChar(S), SizeOf(Buffer));
  // not to the first char of buffer but to the 8th
  StrCopy(@Buffer[7], PChar(S));
0
 

Author Comment

by:shaza
ID: 7161513
yes i mean voice recognition.
0
 
LVL 20

Expert Comment

by:Madshi
ID: 7161519
:-)  There are developers who spent years of developing voice recognition techniques. I don't know any components which do that. You can search on the net for it, but I guess you won't find anything. As Robert said, you'll probably have to forget it...
0
 
LVL 11

Expert Comment

by:robert_marquardt
ID: 7161535
There are of course APIs available, but mostly for commercial products and only for a limited number of languages.

The only promising path i see is to have a look at MS Agents. There is speech recognition, but i do not know if you can get text from it.
0
 

Author Comment

by:shaza
ID: 7161568
I use API functions (wav files)for recording and playing the voice .I want to send this voice to multicast using udp protocol ,i use Mcast component for sending multicast message ,the Mcast component has Send method which i use it to send the voice ,but this method receve string (Send(text:string)),i have to convert the buffer to string to send it using this component.And i want to convert it back to buffer to play the sound.
This is my problem ,i hope you understand me ,and able to help me.
shaza
0
 
LVL 20

Expert Comment

by:Madshi
ID: 7161573
That doesn't sound like voice recognition at all. Try the BufToStr function I posted above. You don't need to convert the string back to a buffer. You can use "pointer(string)" or "pointer(string)^", this way the string *is* a buffer.
0
 

Author Comment

by:shaza
ID: 7161688
Hi madshi:
this is my program ,but there is (invalid pointer operation)Error.
unit client;

interface

uses
   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
    Dialogs, StdCtrls, ipwcore, ipwmcast, IdBaseComponent, IdComponent,
    IdUDPBase, IdUDPServer,mmsystem, IdUDPClient,idsockethandle, NMUDP;
const
    memBlockLength = 1024;
type
    Tmemblock = array[0..memblocklength] of byte;
    PmemBlock = ^TmemBlock;
  TForm1 = class(TForm)
    Button1: TButton;
    Edit1: TEdit;
    Edit2: TEdit;
    CheckBox1: TCheckBox;
    nmemo: TMemo;
    mcast: TipwMCast;
    Button2: TButton;
    procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
    procedure FormCreate(Sender: TObject);
    procedure mcastDataIn(Sender: TObject; Datagram: String;
      const SourceAddress: String; SourcePort: Word);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);


  private
    { Private declarations }
     HwaveIn:PHWaveIn;
    HWaveOut:PHWaveOut;
    close_invoked:boolean;
    in_count,out_count:integer;
    // ActiveSocket: TCustomWinSocket;
    procedure InitSocket;
    procedure InitAudio;
    procedure CreateInBlock;
    procedure CreateOutBlock(memBlock: PmemBlock; size: Integer);
    procedure MMOutDone(var msg:Tmessage);message MM_WOM_DONE;
    procedure MMInDone(var msg:Tmessage);message MM_WIM_DATA;
    procedure CloseAudio;
    procedure NMsg(msg: String);

  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  s:integer;
   Header:PWaveHdr;
   s2:string;
implementation
{function BufToStr(var buf; bufSize: integer) : string;
begin
 SetLength(result, bufSize);
 Move(buf, pointer(result)^, bufSize);
end;     }


 function buftostr(buffer:PmemBlock;i:integer):string;
 var
 str:string;
 begin
   SetLength(str,i);
   Move(buffer, Pointer(str)^,i);
   buftostr:=str;
 end;
{function strtobuf(str:string):pmemBlock;
 var
 buffer:PmemBlock;
 begin
     Move(Pointer(str)^, buffer, Length(str));
     strtobuf:=buffer;
  end;    }

{$R *.dfm}
procedure TForm1.InitAudio;
var
 WaveFormat:Pwaveformatex; // defines the format of waveform-audio data
 i,j:integer;
begin
 s:=0;
 WaveFormat:=new(Pwaveformatex);
 WaveFormat.wFormatTag:=wave_format_pcm;
 waveformat.nChannels:=1;
 waveformat.nSamplesPerSec:=8000;
 waveformat.nAvgBytesPerSec:=8000;
 waveformat.nBlockAlign:=1;
 waveformat.wBitsPerSample:=8;
 waveformat.cbSize:=0;

 i:=waveOutOpen(nil,0,WaveFormat,0,0,WAVE_FORMAT_QUERY); //opens the given waveform-audio output device for playback.
 if i<>0 then NMsg('Error: Play format not supported');

 i:=waveInOpen(nil,0,WaveFormat,0,0,WAVE_FORMAT_QUERY);//opens the given waveform-audio input device for recording.
 if i<>0 then NMsg('Error: Record format not supported');

 HwaveOut:=new(PHwaveOut);
 i:=waveOutOpen(HWaveOut,0,WaveFormat,form1.handle,0,CALLBACK_WINDOW);
 if i<>0 then NMsg('Error: Problem creating play handle');

 HwaveIn:=new(PHwaveIn);
 i:=waveInOpen(HWaveIn,0,WaveFormat,form1.handle,0,CALLBACK_WINDOW);
 if i<>0 then NMsg('Error: Problem creating record handle');

 {these are the count of the number of blocks sent to}
 {the audio device}
 in_count:=0;
 out_count:=0;
 edit1.text:=inttostr(in_count);
 edit2.text:=inttostr(out_count);

 {need to add some buffers to the recording queue}
 {in case the messages that blocks have been recorded}
 {are delayed}
 for j:= 1 to 2 do
   CreateInBlock;

 {finally start recording}
 i:=waveInStart(HwaveIn^);
 if i<>0 then NMsg('Error: Start error');

 close_invoked:=false;
end;

procedure TForm1.CreateInBlock;
var
 Header:PWaveHdr;
 memBlock:PmemBlock;
 i:integer;

begin
 {make a new block}
 memBlock:=new(PmemBlock);

 Header:=new(PwaveHdr);

 with header^ do
 begin
   lpData:=Pointer(memBlock);
   dwBufferLength:=memblocklength;
   dwBytesRecorded:=0;
   dwUser:=0;
   dwFlags:=0;
   dwLoops:=0;
 end;

 {prepare the new block}
 i:=waveInPrepareHeader(HWaveIn^,Header,sizeof(TWavehdr));
 if i<>0 then NMsg('Error: In Prepare error');

 {add it to the buffer}
 i:=waveInAddBuffer(HWaveIn^,Header,sizeof(TWaveHdr));
 if i<>0 then NMsg('Error: Add buffer error');

 inc(in_count);
end;

procedure TForm1.CreateOutBlock(memBlock: PmemBlock; size: Integer);
var
 Header:PWaveHdr;
 i:integer;
begin
 {make a new block}
 Header:=new(PwaveHdr);
 with header^ do
 begin
   lpData:=Pointer(memBlock);
   dwBufferLength:=size;
   dwBytesRecorded:=0;
   dwUser:=0;
   dwFlags:=0;
   dwLoops:=0;
 end;

 {prepare it for play back}
 i:=waveOutPrepareHeader(HWaveOut^,Header,sizeof(TWavehdr));
 if i<>0 then NMsg('Error: Out Prepare error');

 {add it to the playback queue}
 i:=waveOutWrite(HWaveOut^,Header,sizeof(TWaveHdr));
 if i<>0 then NMsg('Error: Wave out error');

 inc(out_count);
end;

procedure TForm1.MMOutDone(var msg:Tmessage);
var
 Header:PWaveHdr;
// i:integer;
begin
//NMsg('MMOutDone');

 dec(out_count);
 {get the returned header}
 Header:=PWaveHdr(msg.lparam);
 waveOutUnPrepareHeader(HWaveOut^,Header,sizeof(TWavehdr));

 {free the memory}
 dispose(Header^.lpdata);
 dispose(Header);

 {if there's no more blocks being recorded}
 if (out_count=0) and close_invoked then
 begin
   WaveOutClose(HWaveOut^);
   HwaveOut:=nil;
 end;

(*  {if there's nothing more to do then close}
 if (in_count=0) and (out_count=0) then
   close; *)
end;

procedure TForm1.MMInDone(var msg:Tmessage);
var
 Header:PWaveHdr;
  i:integer;
begin
//  NMsg('MMInDone');

 dec(in_count);
 {block has been recorded}
 Header:=PWaveHdr(msg.lparam);
 waveInUnPrepareHeader(HWaveIn^,Header,sizeof(TWavehdr));

 if CheckBox1.Checked then

    s2:=buftostr(pointer(Header.lpData),header.dwBufferLength);

    mcast.DataToSend:=s2;
   //mcast.Send(s2);


//  PData := Pointer(Header.lpData);
 dispose(Header.lpData);
 dispose(Header);

 if not(close_invoked) then
 begin
   CreateInBlock;

   edit1.text:=inttostr(in_count);
   edit2.text:=inttostr(out_count);
 end;

 {if there's no more blocks being recorded}
 if (in_count=0) and close_invoked then
 begin
   WaveInClose(HWaveIn^);
   HwaveIn:=nil;
 end;

 (*{if there's nothing more to do then close}
 if (in_count=0) and (out_count=0) then
   close;  *)
end;

procedure TForm1.CloseAudio;
begin
    close_invoked:=true;

 {reset the output channel}
 if HWaveOut<> nil then
 begin
    WaveOutReset(HWaveOut^);
    waveoutclose(Hwaveout^);
    end;

 {reset the input channel}
 if HwaveIn<> nil then
  begin
    WaveInReset(HWaveIn^);
    waveinclose(Hwavein^);
  end;
end;

procedure TForm1.NMsg(msg: String);
begin
    NMemo.Lines.Add(msg);
end;

procedure TForm1.InitSocket;
begin
    NMemo.Clear;
    mcast.Active:=true;

end;

procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
    CloseAudio;
    Canclose:=true;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
    InitSocket;
end;

procedure TForm1.mcastDataIn(Sender: TObject; Datagram: String;
const SourceAddress: String; SourcePort: Word);
var
    s:string;
    data: PmemBlock;
    Header,buf:PWaveHdr;
    i:integer;
begin
    nmemo.Lines.Add(Datagram);
    data:= new(pmemblock);
    //data:=strtobuf(Datagram);
    data:=pointer(Datagram);  
    i:=length(Datagram);
    CreateOutBlock(data,i);

end;

procedure TForm1.Button1Click(Sender: TObject);
begin
    InitAudio;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
CloseAudio;
end;

end.
0
 
LVL 8

Expert Comment

by:TOndrej
ID: 7161720
You can use MS Speech API, headers are translated on JEDI: www.delphi-jedi.org
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 20

Expert Comment

by:Madshi
ID: 7161740
Looks alright on a quick look. Just can delete the following line in mcastDataIn:

data:= new(pmemblock);

Does it work?
0
 

Author Comment

by:shaza
ID: 7161751
no it doesn't work!!!!!!!!!!
the error is still occur(invalid pointer operation) after i click button1+checkbox is enable.
what can i do?
0
 
LVL 20

Expert Comment

by:Madshi
ID: 7161772
In which line does the exception occur?

Try this as mcastDataIn:

begin
  nmemo.Lines.Add(Datagram);
  data := new(pmemblock);
  Move(pointer(Datagram)^, data, sizeOf(Datagram));
  CreateOutBlock(data, i);
end;
0
 
LVL 12

Expert Comment

by:Lee_Nover
ID: 7163815
hum .. you're talking about VoIP - voice over internet
I've done a small app showing how to do it with Indy UDP (it has multicast support) and ACMCOmpos 1.4 from peter morris
http://lee.nover.has.his.evilside.org/isapi/pas2html.dll/pas2html?File=/delphi/projects/VoIP
check it out, it's quite simple :)
0
 

Author Comment

by:shaza
ID: 7171899
hi:
i find the application ,but this application is not voice it send text message.
shaza
0
 

Author Comment

by:shaza
ID: 7171910
i need application to send voice multicast to make audio conference application.
shaza
0
 

Expert Comment

by:DelFreak
ID: 7173345
Lee Nover, why does the ACM package give me an error. I'm using D5. There error is something about {$ALIGN 8} being invalid.
0
 
LVL 12

Assisted Solution

by:Lee_Nover
Lee_Nover earned 50 total points
ID: 7173435
DelFreak: do you have the latest dfs.inc ? if the one on my site is newer then copy that one, it works with it
it's in the 3rdParty folder

shaza: what do you mean ?
my app does capture voice, sends it with UDP, on the other side it receives the data and plays it
where did you find text ?
0
 

Expert Comment

by:CleanupPing
ID: 9343040
shaza:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0
 
LVL 5

Expert Comment

by:Lukasz Lach
ID: 9461231
shaza,
No comment has been added lately (18 days), so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area for this question:

RECOMMENDATION: split points between Madshi http:#7161500 and Lee_Nover http:#7163815

Please leave any comments here within 7 days.

-- Please DO NOT accept this comment as an answer ! --

Thanks,

anAKiN
EE Cleanup Volunteer
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now