convert from buffer to string and from string to buffer

Hi:
how i can convert buffer which contain voice to string ,and convert the string back to buffer?
shaza
shazaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

 
MadshiCommented:
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

Experts Exchange Solution brought to you by ConnectWise

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
 
robert_marquardtCommented:
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
 
shazaAuthor Commented:
yes i mean voice recognition.
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

 
MadshiCommented:
:-)  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
 
robert_marquardtCommented:
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
 
shazaAuthor Commented:
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
 
MadshiCommented:
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
 
shazaAuthor Commented:
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
 
TOndrejCommented:
You can use MS Speech API, headers are translated on JEDI: www.delphi-jedi.org
0
 
MadshiCommented:
Looks alright on a quick look. Just can delete the following line in mcastDataIn:

data:= new(pmemblock);

Does it work?
0
 
shazaAuthor Commented:
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
 
MadshiCommented:
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
 
Lee_NoverCommented:
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
 
shazaAuthor Commented:
hi:
i find the application ,but this application is not voice it send text message.
shaza
0
 
shazaAuthor Commented:
i need application to send voice multicast to make audio conference application.
shaza
0
 
DelFreakCommented:
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
 
Lee_NoverCommented:
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
 
CleanupPingCommented:
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
 
Lukasz LachCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.