如何保存语音包为Wav格式?(100分)

  • 主题发起人 主题发起人 morgan520
  • 开始时间 开始时间
M

morgan520

Unregistered / Unconfirmed
GUEST, unregistred user!
[blue][/blue]
如下代码是我的一个录音的以单元。
---------------------------------------------------------------------------
{
录音控制
}
unit recordUtil;

interface
uses Classes, protocolOjbect, sysUtils;
type
TRecordHelper = class(TObject)
private
{ Private declarations }
memory: TMemoryStream;
currentFileName: string;
bOnWork: boolean;

public
constructor Create;
function startRecord(fileDir: string;
fileName: string): boolean;
overload;
function recordBuffer(data: Pointer;
size: Integer): boolean;
function stopRecord: boolean;
end;


implementation

uses commonUtil;

constructor TRecordHelper.Create;
begin

inherited;
bOnWork := false;
end;


{ TRecordHelper }

function TRecordHelper.recordBuffer(data: Pointer;
size: Integer): boolean;
begin

if bOnWork then

memory.WriteBuffer(data, size);
end;


function TRecordHelper.startRecord(fileDir: string;
fileName: string): boolean;
begin

if (DirectoryExists(fileDir) = false) then

if (CreateDir(fileDir) = false) then

begin

result := false;
exit;
end;


currentFileName := format('%s/%s.wav', [fileDir, fileName]);
CreateWav(1, { 1(单声)或者2(立体声) }
8, { 8或者16,代表8位或16位声音 }
8000, { 声音频率,如11025,22050, 44100}
currentFileName);

memory := TmemoryStream.create;
memory.LoadFromFile(currentFileName);
memory.Seek(sizeof(TWavHeader), 0);
bOnWork := true;

end;


function TRecordHelper.stopRecord: boolean;
begin

if bOnWork then

begin

bOnWork := false;
memory.SaveToFile(currentFileName);
end;

memory.free;
memory := nil;
end;


end.

---------------------------------------------------------
我运用本录音类的方法如下:
首先调用startRecord;
然后在有话音包过来的时候使用recordBuffer;
在结束通话的时候用stopRecord保存为WAV文件。
---------------------------------------------------------
问题是:
我得到WAV文件用REALPLAYER播放的时候,才开始播放就接受了(几百K的文件)。说明保存话音的方法有问题,希望各位大哥,给点建议!谢谢!
 
需要建立一个WMV格式的文件来存储的,你现在定义的是WAV格式,录的应该是WAV文件才对
 
楼上的大哥,我就是想存为WAV,怎么实现,可以说详细点嘛
 
unit Unit1;

interface

uses
.....

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;



type
TForm1 = class(TForm)
......
procedure CreateWav(channels : word;
resolution : word;
rate : longint;
fn : string);
procedure btStartClick(Sender: TObject);
procedure btStopClick(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Init;
private
{ Private declarations }
public
{ Public declarations }
end;


var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.CreateWav( channels : word;
{ 1(单声)或者2(立体声) }
resolution : word;
{ 8或者16,代表8位或16位声音 }
rate : longint;
{ 声音频率,如11025,22050, 44100}
fn : string { 对应的文件名称 } );

var
wf : file of TWavHeader;

wh : TWavHeader;

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;


assignfile(wf,fn);
{打开对应文件 }
rewrite(wf);
{移动指针到文件头}
write(wf,wh);
{写进文件头 }
closefile(wf);
{关闭文件 }
end;


procedure TForm1.BtStartClick(Sender: TObject);

begin

try
//在/doc/">程序当前目录下创建一个Wav文件Temp.wav
CreateWav(1, 8, 11025, (ExtractFilePath(Application.ExeName)+ 'Temp.wav'));
MediaPlayer1.DeviceType := dtAutoSelect;
MediaPlayer1.FileName := (ExtractFilePath(Application.ExeName)+ 'Temp.wav');
MediaPlayer1.Open;
MediaPlayer1.StartRecording;
BtStart.Enabled:=false;
BtStop.Enabled:=true;


except
BtStart.Enabled:=True;
BtStop.Enabled:=false;
Application.MessageBox('媒体设备初始化失败!','错误',MB_ICONERROR+MB_OK);

end;


end;



procedure TForm1.BtStopClick(Sender: TObject);

begin

try
MediaPlayer1.Stop;

MediaPlayer1.Save;

MediaPlayer1.Close;

Application.MessageBox('声音录制完毕!','信息',MB_ICONINFORMATION+MB_OK);

BtStart.Enabled:=True;

BtStop.Enabled:=false;

except
Application.MessageBox('保存声音文件出错!','错误',MB_ICONERROR+MB_OK);

BtStart.Enabled:=True;

BtStop.Enabled:=false;

end;

end;

end.


这个网上很多的 如果录音不清楚 请更改参数
 
大哥,这个程序我也有的。这个只能录MIC的声音。因为我在程序中有要求的,在UDP接受到对方的话音包的时候必需放出来,同时我的把话音给录制在WAV文件里。程序要求对话音数据指定保存,但是如果可以直接把声卡中的话音给保存也可以的。现在就是不晓得怎么解决这个问题了。
 
wav文件有多种格式,比如PCM, CCITT 等,你必须分析这个语音包的格式, 然后写文件的时候,先写头,头写对了, 数据部分可以任意合并(注意修改文件头中DATA部分的长度)
 
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;

我采用的是pcm的,不知道怎么养修改data长度呢?
 
type
TWaveHeader = record
Marker1: array[0..3] of char;
{ ='RIFF'}
nFileSize: longint;
{ Длина файла от следующего поля}
Marker2: array[0..3] of char;
{ ='WAVE'}
Marker3: array[0..3] of char;
{ ='fmt '}
nInfoLen: longint;
{ Обычно = 16}
FormatTag: word;
{ 1- Linear PCM}
wChannels: word;
{ 1 - моно, 2 - стерео, 4 - квадро}
nSamplesPerSec: longint;
{ Частота сэмплинга}
nAvgBytesPerSec: longint;
{ Байт в секунду}
wBlockAlign: word;
{ Байт на полный сэмпл}
wBitsPerSample: word;
{ Разрядность оцифровки }
Marker4: array[0..3] of char;
{ ='data'}
nDataSize: longint;
end;

1:创建语音保存流
MyWAVFileStream.Seek(sizeof(TWaveHeader), soFrombegin
ning);//空出WAV头
2:接收数据
3:保存为文件
var
MyWAVHeader: TWaveHeader;
begin

FGSM610ToPCM.Free;
FPCMOUT.Free;
if MyWAVFileStream <> nil then

begin


with MyWAVHeaderdo

begin

Marker1 := 'RIFF';
//"RIFF"标志
nFileSize := MyWAVFileStream.Size;
//sizeof(MyWAVHeader) + WavStream.Size;
//文件长度
Marker2 := 'WAVE';
//"WAVE"标志
Marker3 := 'fmt ';
nInfoLen := 16;
//过渡字节(不定)
FormatTag := 1;
//格式类别(10H为PCM形式的声音数据)
wChannels := 1;
//声道,单声道为1,双声道为2,多声道为4
nSamplesPerSec := 8000;
nAvgBytesPerSec := 8000;
wBlockAlign := 1;
wBitsPerSample := 8;
Marker4 := 'data';
{数据标记符"data"}
nDataSize := MyWAVFileStream.Size - sizeof(MyWAVHeader);
//WavStream.Size;
{语音数据的长度}
end;


MyWAVFileStream.Seek(0, soFrombegin
ning);
MyWAVFileStream.Write(MyWAVHeader, sizeof(MyWAVHeader));
MyWAVFileStream.Free;

end;
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
I
回复
0
查看
315
import
I
I
回复
0
查看
804
import
I
I
回复
0
查看
547
import
I
后退
顶部