Author Topic: "Audio-CD->mp3" without the detour "Audio-CD->wav->mp3"  (Read 3273 times)

Wuwu_Depp

  • Guest
Hallo,
with the following code, I rip an audio-cd-track in Delphi:

Code: [Select]
progressInPercent := 0;

  BASS_Init(-1, 44100, 0, Application.Handle, nil);

  channel := BASS_CD_StreamCreate(self.drive,self.trackNr,BASS_STREAM_AUTOFREE OR BASS_STREAM_DECODE);
  //BASS_StreamPrebuf(playhandle);

  BASS_ChannelGetInfo(channel, chaninfo);
   nChannels := chaninfo.chans;

  if (chaninfo.flags and BASS_SAMPLE_8BITS > 0) then
    wBitsPerSample := 8
  else
    wBitsPerSample := 16;

   nBlockAlign := nChannels * wBitsPerSample div 8;
   BASS_ChannelGetAttributes(channel, frq, vl, Tmp);
  nSamplesPerSec := frq;
   nAvgBytesPerSec := nSamplesPerSec * nBlockAlign;

  { --- normally, here I fill the the variable "fileName" with content, but it's too unimportant and too long to display it here... --- }

  RecStream := TFileStream.Create(fileName, fmCreate);

  temp := 'RIFF'; RecStream.write(temp[1], length(temp));
  temp := #0#0#0#0; RecStream.write(temp[1], length(temp));   // File size: to be updated
  temp := 'WAVE'; RecStream.write(temp[1], length(temp));
  temp := 'fmt '; RecStream.write(temp[1], length(temp));
  temp := #$10#0#0#0; RecStream.write(temp[1], length(temp)); // Fixed
  temp := #1#0; RecStream.write(temp[1], length(temp));       // PCM format

  if nChannels = 1 then
    temp := #1#0
  else
    temp := #2#0;

  RecStream.write(temp[1], length(temp));
  RecStream.write(nSamplesPerSec, 2);
  temp := #0#0; RecStream.write(temp[1], length(temp));   // SampleRate is given as dWord
  RecStream.write(nAvgBytesPerSec, 4);
  RecStream.write(nBlockAlign, 2);
  RecStream.write(wBitsPerSample, 2);
  temp := 'data'; RecStream.write(temp[1],length(temp));
  temp := #0#0#0#0; RecStream.write(temp[1],length(temp)); // Data size: to be updated

   while (BASS_ChannelIsActive(channel) > 0) do begin
    BytesRead := BASS_ChannelGetData(channel, @buf, 10000);
    RecStream.Write(buf, BytesRead);
    Application.ProcessMessages;
    progressInPercent := Trunc(100 * (BASS_ChannelGetPosition(channel) / BASS_ChannelGetLength(channel)));
   end;

  BASS_StreamFree(channel); // free the stream

  i := RecStream.Size - 8;    // size of file
  RecStream.Position := 4;
  RecStream.write(i, 4);
  i := i - $24;               // size of data
  RecStream.Position := 40;
  RecStream.write(i, 4);
  RecStream.Free;
  progressInPercent := 100;
 

The file is saved as a .wav-file, but I want to have an .mp3-file. So where I have to "put in" an encoder, to get a mp3-file without first having a .wav-file and and next encoding this file to mp3?
I tried to insert the following line at different positions in the code:

Code: [Select]
BASS_Encode_Start(playhandle, PChar('lame.exe - "' + fileName + '.mp3"'), 0, nil, 0);
 
I would be very grateful for your help and hope, you can understand my english (I'm from Germany)!  :'(

kenshin1101

  • Posts: 545
try this
Code: [Select]
function Encode_As_Mp3(chan : DWORD; encodeproc : Pointer;
                       DestFile : string): boolean;
var commandline : string;
begin
 commandline := Path(ExtractFilePath(paramstr(0))) + 'lame.exe ';
 if UpAll(fileext(destfile)) <> 'MP3' then
  destfile := Path(ExtractFilePath(destfile)) + _FileName(destfile) + '.mp3';
 if fileexists(DestFile) then
   if ConvertFrm._OverWrite.Checked then
     DeleteFile(DestFile);

 if fileexists(DestFile) then
   DestFile := CreateNewName(DestFile);

   commandline := commandline + LameConfigFrm.commandline + ' - "' + DestFile + '"'; // LameConfigFrm.commandline is command (string) to set bitrate, channel,etc..
 result := EncodeStdIn(chan, encodeproc, DestFile, commandline);
end;


Code: [Select]
function EncodeStdIn(chan : DWORD; encodeproc : Pointer;
                       DestFile, commandline : string): boolean;
var EncProc : TEncodeProc;
    dwBuffer, current, total: DWORD;
    Buf : array [0..20000] of char;
    H1, H2 : file;
    S : string;
begin
 result := false;
 EncProc := encodeproc;
 StopEncode := false;
 total := BASS_ChannelGetLength(chan);

 if BASS_Encode_Start(chan, PChar(commandline), 0, nil, 0) = 0 then exit;

 dwBuffer := (SizeOf(Buf) div 1024) * 1024;

 while (Bass_ChannelIsActive(chan) > 0) and BASS_Encode_IsActive(chan)
        and (not StopEncode) do
   begin
     BASS_ChannelGetData(chan, @buf, dwBuffer);
     current := BASS_ChannelGetPosition(chan);
     if @EncProc <> nil then
      EncProc(chan, current, total, dwBuffer, 0);
   end;
    Bass_Encode_Stop(chan);
  end
 else
 Bass_Encode_Stop(chan);
 result := true;
end;

This is my old encode , hope this help!

Wuwu_Depp

  • Guest
Thank you very much for your answer, but I don't come to grips with it.  :'(
I'm a beginner in Delphi, particulary in programming mulitmedia-applications.
Is there a possibility to insert your code in my code, so that it works?

kenshin1101

  • Posts: 545
try read bass record example  ;)