使用带 Delphi 的 BASS dll 创建 flac 文件或 flac 流
Creating flac file or flac stream using BASS dll with Delphi
我正在演奏来自 http://www.un4seen.com/ 的 BASS。
我需要创建用户在麦克风上讲话的 flac 文件(16 位)或 flac 流。
我在BASS源代码中看到过这个demo。
还有一个 bassenc_flac.dll 以及这些功能:
function BASS_Encode_FLAC_Start(handle:DWORD; options:PChar; flags:DWORD; proc:ENCODEPROCEX; user:Pointer): HENCODE; {$IFDEF MSWINDOWS}stdcall{$ELSE}cdecl{$ENDIF}; external bassencflacdll;
function BASS_Encode_FLAC_StartFile(handle:DWORD; options:PChar; flags:DWORD; filename:PChar): HENCODE; {$IFDEF MSWINDOWS}stdcall{$ELSE}cdecl{$ENDIF}; external bassencflacdll;
如何更改下一个代码以将音频编码为 flac 文件或流?
来自 RecordTest BASS 演示
(* This is called while recording audio *)
function RecordingCallback(Handle: HRECORD; buffer: Pointer; length: DWORD; user: Pointer): boolean; stdcall;
var level:dword;
begin
level:=BASS_ChannelGetLevel(Handle);
// Copy new buffer contents to the memory buffer
Form1.WaveStream.Write(buffer^, length);
// Allow recording to continue
Result := True;
end;
(* Start recording to memory *)
procedure TForm1.StartRecording;
begin
if ComboBox1.ItemIndex < 0 then Exit;
if WaveStream.Size > 0 then
begin // free old recording
BASS_StreamFree(chan);
WaveStream.Clear;
end;
// generate header for WAV file
with WaveHdr do
begin
riff := 'RIFF';
len := 36;
cWavFmt := 'WAVEfmt ';
dwHdrLen := 16;
wFormat := 1;
wNumChannels := 2;
dwSampleRate := 44100;
wBlockAlign := 4;
dwBytesPerSec := 176400;
wBitsPerSample := 16;
cData := 'data';
dwDataLen := 0;
end;
WaveStream.Write(WaveHdr, SizeOf(WAVHDR));
// start recording @ 44100hz 16-bit stereo
rchan := BASS_RecordStart(44100, 2, 0, @RecordingCallback, nil);
if rchan = 0 then
begin
MessageDlg('Couldn''t start recording!', mtError, [mbOk], 0);
WaveStream.Clear;
end
else
begin
bRecord.Caption := 'Stop';
bPlay.Enabled := False;
bSave.Enabled := False;
end;
end;
(* Stop recording *)
procedure TForm1.StopRecording;
var
i: integer;
he:BassEnc.HENCODE;
begin
BASS_ChannelStop(rchan);
bRecord.Caption := 'Record';
// complete the WAV header
WaveStream.Position := 4;
i := WaveStream.Size - 8;
WaveStream.Write(i, 4);
i := i - ;
WaveStream.Position := 40;
WaveStream.Write(i, 4);
WaveStream.Position := 0;
// create a stream from the recorded data
chan := BASS_StreamCreateFile(True, WaveStream.Memory, 0, WaveStream.Size, 0);
if chan <> 0 then
begin
// enable "Play" & "Save" buttons
bPlay.Enabled := True;
bSave.Enabled := True;
end
else
MessageDlg('Error creating stream from recorded data!', mtError, [mbOk], 0);
if SaveDialog.Execute then
WaveStream.SaveToFile(SaveDialog.FileName);
end;
我更新了代码,因为注释显示以前编码器版本的工作不正确。我完全同意这些评论。
为了创建 FLAC 编码器,我们应该去 un4seen web-site 并下载下一个文件:
- BASS 音频库 2.4
- BASSFLAC 2.4.4
- BASSenc 2.4.14
- BASSenc_FLAC 2.4.1.1
浏览这些文件夹并查找下一个文件:
bass.pas
bassenc.pas
bassenc_flac.pas
现在将这些 pas-files 放入一个文件夹中,然后通过 Delphi's
选项将其添加到 Library
。
在这一步创建新项目后,将其保存在单独的文件夹中。
然后浏览 BASS_XXX 个文件夹并查找 *.dll
个文件。
将它们合并到您保存项目的文件夹中!
现在让我们写一些代码。
添加到 uses
子句 bass.pas
、bassenc.pas
和 bassenc_flac.pas
。然后复制如下所示的代码。
uses ..., BASS, BASSEnc, BASSEnc_FLAC;
...
TForm1 = class(TForm)
ProgressBar1: TProgressBar;
public
{ Public declarations }
procedure StartEncode(SourceFileName, OutputFileName: String);
procedure StopEncode;
end;
...
procedure TForm1.StartEncode(SourceFileName, OutputFileName: String);
var
PercentDone: Cardinal;
Buffer: array [0..1024] of Byte;
begin
Channel := BASS_StreamCreateFile(false, PChar(SourceFileName), 0, 0, BASS_MUSIC_DECODE or BASS_UNICODE);
BASSEnc_FLAC.BASS_Encode_FLAC_StartFile(Channel, 0, BASS_ENCODE_FP_AUTO or BASS_UNICODE, PChar(OutputFileName));
while BASS_ChannelIsActive(Channel) > 0 do
begin
BASS_ChannelGetData(Channel, @Buffer, 1024);
PercentDone := Trunc(100 * (BASS_ChannelGetPosition(Channel, BASS_POS_BYTE) / BASS_ChannelGetLength(Channel, BASS_POS_BYTE)));
ProgressBar1.Position := PercentDone;
end;
StopEncode;
end;
procedure TForm1.StopEncode;
begin
BASS_Encode_Stop(Channel);
BASS_StreamFree(Channel);
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
BASS_Init(-1, 44100, 0, Application.Handle, nil);
try
// Set name of file to convert it to FLAC and save it with output name
StartEncode('SourceFileName', 'OutputFileName');
finally
BASS.BASS_Free;
end;
end;
一则通知:
事实上,用以前版本的代码编码的文件有不正确的 header(我在 Notepad.exe
中打开文件时可以看到它)。更新代码后,我可以看到有效的 header(当然是在记事本中,因为我没有使用 audio-files 的专业工具)。
现在您甚至不需要像我之前那样向 BASS
添加插件。
因此我认为编码器按预期工作。
我正在演奏来自 http://www.un4seen.com/ 的 BASS。
我需要创建用户在麦克风上讲话的 flac 文件(16 位)或 flac 流。
我在BASS源代码中看到过这个demo。 还有一个 bassenc_flac.dll 以及这些功能:
function BASS_Encode_FLAC_Start(handle:DWORD; options:PChar; flags:DWORD; proc:ENCODEPROCEX; user:Pointer): HENCODE; {$IFDEF MSWINDOWS}stdcall{$ELSE}cdecl{$ENDIF}; external bassencflacdll;
function BASS_Encode_FLAC_StartFile(handle:DWORD; options:PChar; flags:DWORD; filename:PChar): HENCODE; {$IFDEF MSWINDOWS}stdcall{$ELSE}cdecl{$ENDIF}; external bassencflacdll;
如何更改下一个代码以将音频编码为 flac 文件或流?
来自 RecordTest BASS 演示
(* This is called while recording audio *)
function RecordingCallback(Handle: HRECORD; buffer: Pointer; length: DWORD; user: Pointer): boolean; stdcall;
var level:dword;
begin
level:=BASS_ChannelGetLevel(Handle);
// Copy new buffer contents to the memory buffer
Form1.WaveStream.Write(buffer^, length);
// Allow recording to continue
Result := True;
end;
(* Start recording to memory *)
procedure TForm1.StartRecording;
begin
if ComboBox1.ItemIndex < 0 then Exit;
if WaveStream.Size > 0 then
begin // free old recording
BASS_StreamFree(chan);
WaveStream.Clear;
end;
// generate header for WAV file
with WaveHdr do
begin
riff := 'RIFF';
len := 36;
cWavFmt := 'WAVEfmt ';
dwHdrLen := 16;
wFormat := 1;
wNumChannels := 2;
dwSampleRate := 44100;
wBlockAlign := 4;
dwBytesPerSec := 176400;
wBitsPerSample := 16;
cData := 'data';
dwDataLen := 0;
end;
WaveStream.Write(WaveHdr, SizeOf(WAVHDR));
// start recording @ 44100hz 16-bit stereo
rchan := BASS_RecordStart(44100, 2, 0, @RecordingCallback, nil);
if rchan = 0 then
begin
MessageDlg('Couldn''t start recording!', mtError, [mbOk], 0);
WaveStream.Clear;
end
else
begin
bRecord.Caption := 'Stop';
bPlay.Enabled := False;
bSave.Enabled := False;
end;
end;
(* Stop recording *)
procedure TForm1.StopRecording;
var
i: integer;
he:BassEnc.HENCODE;
begin
BASS_ChannelStop(rchan);
bRecord.Caption := 'Record';
// complete the WAV header
WaveStream.Position := 4;
i := WaveStream.Size - 8;
WaveStream.Write(i, 4);
i := i - ;
WaveStream.Position := 40;
WaveStream.Write(i, 4);
WaveStream.Position := 0;
// create a stream from the recorded data
chan := BASS_StreamCreateFile(True, WaveStream.Memory, 0, WaveStream.Size, 0);
if chan <> 0 then
begin
// enable "Play" & "Save" buttons
bPlay.Enabled := True;
bSave.Enabled := True;
end
else
MessageDlg('Error creating stream from recorded data!', mtError, [mbOk], 0);
if SaveDialog.Execute then
WaveStream.SaveToFile(SaveDialog.FileName);
end;
我更新了代码,因为注释显示以前编码器版本的工作不正确。我完全同意这些评论。
为了创建 FLAC 编码器,我们应该去 un4seen web-site 并下载下一个文件:
- BASS 音频库 2.4
- BASSFLAC 2.4.4
- BASSenc 2.4.14
- BASSenc_FLAC 2.4.1.1
浏览这些文件夹并查找下一个文件:
bass.pas
bassenc.pas
bassenc_flac.pas
现在将这些 pas-files 放入一个文件夹中,然后通过 Delphi's
选项将其添加到 Library
。
在这一步创建新项目后,将其保存在单独的文件夹中。
然后浏览 BASS_XXX 个文件夹并查找 *.dll
个文件。
将它们合并到您保存项目的文件夹中!
现在让我们写一些代码。
添加到 uses
子句 bass.pas
、bassenc.pas
和 bassenc_flac.pas
。然后复制如下所示的代码。
uses ..., BASS, BASSEnc, BASSEnc_FLAC;
...
TForm1 = class(TForm)
ProgressBar1: TProgressBar;
public
{ Public declarations }
procedure StartEncode(SourceFileName, OutputFileName: String);
procedure StopEncode;
end;
...
procedure TForm1.StartEncode(SourceFileName, OutputFileName: String);
var
PercentDone: Cardinal;
Buffer: array [0..1024] of Byte;
begin
Channel := BASS_StreamCreateFile(false, PChar(SourceFileName), 0, 0, BASS_MUSIC_DECODE or BASS_UNICODE);
BASSEnc_FLAC.BASS_Encode_FLAC_StartFile(Channel, 0, BASS_ENCODE_FP_AUTO or BASS_UNICODE, PChar(OutputFileName));
while BASS_ChannelIsActive(Channel) > 0 do
begin
BASS_ChannelGetData(Channel, @Buffer, 1024);
PercentDone := Trunc(100 * (BASS_ChannelGetPosition(Channel, BASS_POS_BYTE) / BASS_ChannelGetLength(Channel, BASS_POS_BYTE)));
ProgressBar1.Position := PercentDone;
end;
StopEncode;
end;
procedure TForm1.StopEncode;
begin
BASS_Encode_Stop(Channel);
BASS_StreamFree(Channel);
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
BASS_Init(-1, 44100, 0, Application.Handle, nil);
try
// Set name of file to convert it to FLAC and save it with output name
StartEncode('SourceFileName', 'OutputFileName');
finally
BASS.BASS_Free;
end;
end;
一则通知:
事实上,用以前版本的代码编码的文件有不正确的 header(我在 Notepad.exe
中打开文件时可以看到它)。更新代码后,我可以看到有效的 header(当然是在记事本中,因为我没有使用 audio-files 的专业工具)。
现在您甚至不需要像我之前那样向 BASS
添加插件。
因此我认为编码器按预期工作。