用acm控件怎样存为wav文件?我的代码有什么问题呢,100分问题(100分)

  • 主题发起人 主题发起人 passwind
  • 开始时间 开始时间
P

passwind

Unregistered / Unconfirmed
GUEST, unregistred user!
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ACMDialog, ACMWaveIn, ACMWaveOut, StdCtrls, NMUDP, ExtCtrls, Psock,
ComCtrls,mmsystem;


type
TWavHeader = record //定义一个Wav文件头格式
rId : longint;

rLen : longint;

wId : longint;
fId : longint;
fLen : longint;
wFormatTag : word;

nChannels : word;

nSamplesPerSec : longint;

nAvgBytesPerSec : longint;
nBlockAlign : word;
wBitsPerSample : word;

dId : longint;

wSampleLength : longint;

end;

TForm1 = class(TForm)
ACMWaveIn1: TACMWaveIn;
ACMWaveOut1: TACMWaveOut;
Button1: TButton;
Button2: TButton;
StatusBar1: TStatusBar;
Button3: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure ACMWaveIn1Data(data: Pointer;
size: Integer);
procedure Button3Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

procedure CreateWav(channels : word;
resolution : word;
rate : longint;
fn : string);//自定义写一个Wav文件头过程
var
Form1: TForm1;
PlayPackets:integer;
mdata:array[1..10000] of char;
wf : file of TWavHeader;
fsRecv:TFileStream;
fssour:TFileStream;
implementation

{$R *.DFM}

procedure CreateWav( channels : word;

resolution : word;

rate : longint;

fn : string );
var
wf : file of TWavHeader;
wh : TWavHeader;
files:textfile;
s:string;
begin

wh.rId := $46464952;

wh.rLen := 36;
wh.wId := $45564157;

wh.fId := $20746d66;

wh.fLen := 16;
wh.wFormatTag := 1;

wh.nChannels := channels;
wh.nSamplesPerSec := rate;
wh.nAvgBytesPerSec := channels*rate*(resolution div 8);
wh.nBlockAlign := channels*(resolution div 8);
wh.wBitsPerSample := resolution;
wh.dId := $61746164;
wh.wSampleLength := 0;
try
assignfile(wf,fn);
{打开对应文件 }
rewrite(wf);
{移动指针到文件头}
write(wf,wh);
{写进文件头 }
closefile(wf);
{关闭文件 }
except
Application.MessageBox('媒体文件打开失败!','错误',MB_ICONERROR+MB_OK);
end;

end;


procedure TForm1.Button1Click(Sender: TObject);//开始
var format: array[0..50] of char;
tmpformat: TWaveFormatEx;
i: WORD;
begin

fillchar(format, 50, 0);
tmpformat.wFormatTag := 49;
tmpformat.nChannels := 1;
tmpformat.nSamplesPerSec := 8000;
tmpformat.nAvgBytesPerSec := 1625;
tmpformat.nBlockAlign := 65;
tmpformat.wBitsPerSample := 0;
tmpformat.cbSize := 2;
move(tmpformat, format, sizeof(tmpformat));
i := $140;
move(i, format[sizeof(tmpformat)], 2);
if format = nil then
exit;
ACMWaveOut1.Open(@format);
ACMWaveIn1.Open(@format);
Button1.Enabled:=false;
Button2.Enabled:=true;
end;


procedure TForm1.Button2Click(Sender: TObject);//录音结束
begin

ACMWaveIn1.Close;
ACMWaveOut1.Close;
Button1.Enabled:=true;
Button2.Enabled:=false;
fsrecv.Seek(0,soFromEnd);//往尾部添加资源
fsrecv.CopyFrom(fssour,0);
end;


procedure TForm1.ACMWaveIn1Data(data: Pointer;
size: Integer);
var
k:integer;
str:string;
begin

for k:=1 to sizedo
mdata[k]:=pchar(dword(data)+k-1)^;
//NMUDP1.SendBuffer(mdata,size);
fssour.WriteBuffer(mdata,size);
end;


procedure TForm1.Button3Click(Sender: TObject);//建立文件
begin

//我建立了两个文件,一个有文件头,一个没有,声音数据存在sour.wav
CreateWav(2, 8, 11025, 'e:/temp.wav');
fsRecv:=TFileStream.Create('e:/temp.wav',fmopenwrite);
fssour:=TFileStream.Create('e:/sour.wav',fmcreate);
end;


end.
 
文件能保存,但打开后提示没有音频数据
 
搞了几天了:(
 
for k:=1 to sizedo
mdata[k]:=pchar(dword(data)+k-1)^;
fssour.WriteBuffer(mdata,size);

关键是这里。。。看看里面的内容有没有问题。。。。
 
用NMUDP1.SendBuffer(mdata,size);然后接受就没问题,这个是acm的例子
但把数据存为文件再结合文件头文件就有问题,不知道这个结合有没有问题呢
 
写文件:先把所有的OnData 中的数据写入内存,然后写入文件,,

读文件:先把文件读入内存,,然后在play
 
passwind :您好,
我们可以聊聊,。。。
 
后退
顶部