Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

# convert from buffer to string and from string to buffer

Posted on 2002-07-18
Medium Priority
6,388 Views
Hi:
how i can convert buffer which contain voice to string ,and convert the string back to buffer?
shaza
0
Question by:shaza
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 6
• 5
• 2
• +5

LVL 20

Accepted Solution

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.

0

LVL 11

Expert Comment

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

ID: 7161513
yes i mean voice recognition.
0

LVL 20

Expert Comment

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

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

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

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

ID: 7161688
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;
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;
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
memBlock:PmemBlock;
i:integer;

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

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

{prepare the new block}
if i<>0 then NMsg('Error: In Prepare error');

if i<>0 then NMsg('Error: Add buffer error');

inc(in_count);
end;

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

{prepare it for play back}
if i<>0 then NMsg('Error: Out Prepare error');

{add it to the playback queue}
if i<>0 then NMsg('Error: Wave out error');

inc(out_count);
end;

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

dec(out_count);

{free the memory}

{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
i:integer;
begin
//  NMsg('MMInDone');

dec(in_count);
{block has been recorded}

if CheckBox1.Checked then

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

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
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;
var
s:string;
data: PmemBlock;
i:integer;
begin
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

ID: 7161720
You can use MS Speech API, headers are translated on JEDI: www.delphi-jedi.org
0

LVL 20

Expert Comment

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

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

ID: 7161772
In which line does the exception occur?

Try this as mcastDataIn:

begin
data := new(pmemblock);
Move(pointer(Datagram)^, data, sizeOf(Datagram));
CreateOutBlock(data, i);
end;
0

LVL 12

Expert Comment

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

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

Author Comment

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

Expert Comment

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

Lee_Nover earned 200 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

ID: 9343040
shaza:
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0

LVL 5

Expert Comment

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 DO NOT accept this comment as an answer ! --

Thanks,

anAKiN
EE Cleanup Volunteer
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code Â  (CODE) The Imâ€¦
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGridâ€¦
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increasedâ€¦
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can reâ€¦
###### Suggested Courses
Course of the Month10 days, 5 hours left to enroll