Author Topic: BASS and Lazarus  (Read 89 times)

stephanos

  • Posts: 3
BASS and Lazarus
« on: 23 Nov '21 - 13:37 »
Greetings

I am writing a playlist programme for my mp3 player using Lazarus on Windows 10.  I want to access some file properties so as to calculate the playtime of each file listed in the playlist file. That way I know how much play time I have and if I need more for the journey.  I can use FileSize() and/or FindFirst() to get the files size.  I can use id3 tag to get more information found in the last 128 bytes of the file.  But this does not include bitrate or Length.

Does BASS as a plug-in allow me to code for this information?

Does BASS as a plug-in work in Lazarus?

If so are there any instructions?  I have unpacked it into a folder “bass24” but what to do with it?

Thanks and wait to hear

ManlioMZ

  • Posts: 21
Re: BASS and Lazarus
« Reply #1 on: 23 Nov '21 - 13:48 »
The following example was tested with Lazarus + Windows.

1. Open the file with a stream.
2. Get the stream size (in bytes).
3. Convert the number of bytes to seconds.

Code: [Select]
  function GetMp3Len(fn: string): integer;
  var
    H: HSTREAM;
    Info: BASS_CHANNELINFO;
    L_Bytes: QWORD; // length in bytes
    L_Seconds: double; // length in seconds
  begin
    result := 0;
    H := BASS_StreamCreateFile(false, pchar(Utf8Encode(fn)), 0, 0, BASS_STREAM_DECODE);
    if H=0 then exit; // Something's wrong, could not open the file
    L_Bytes := BASS_ChannelGetLength(H, BASS_POS_BYTE); // length in bytes
    L_Seconds := BASS_ChannelBytes2Seconds(H, L_Bytes); // convert bytes to seconds
    result := trunc(L_Seconds * 1000); // return the result as milliseconds
    BASS_StreamFree(H); // close the stream and free the resources
  end;

Note that the above requires opening the file and decoding it first, so the speed depends on the length of the file.
« Last Edit: 23 Nov '21 - 20:34 by ManlioMZ »

stephanos

  • Posts: 3
Re: BASS and Lazarus
« Reply #2 on: 23 Nov '21 - 18:22 »
Dear ManlioMZ

Thanks for the info and the offer which I accept.  In the final programme I might make the calculation of the play time a separate button so as to separate it from the other validation that takes place on the list of mp3 files selected.

Wait to hear


ManlioMZ

  • Posts: 21
Re: BASS and Lazarus
« Reply #3 on: 23 Nov '21 - 20:39 »
I added the code above. Note that:

1. Filenames and UTF encoding may work slightly differently on different systems. Do some tests with weird filenames to make sure.

2. You can use a similar approach to read the data from a TMemoryStream instead of a file, like this (Where MS is a TMemoryStream):

  H := BASS_StreamCreateFile(True, MS.Memory, 0, 0, BASS_STREAM_DECODE);

3. The tag BASS_STREAM_DECODE means you don't want to play the file, only measure it. This, I presume, saves time and spares some system resources.

stephanos

  • Posts: 3
Re: BASS and Lazarus
« Reply #4 on: 24 Nov '21 - 10:55 »
Dear  ManlioMZ

Thanks for that code.  I have many questions
1) what do I put in uses?
2) how do I integrate BASS into Lazarus?  I have unpacked the zip but I do not know where to place the folder "bass24" 3) how do I tell Lazarus that it now has this facility

Thanks and wait to hear


morknot

  • Posts: 21
Re: BASS and Lazarus
« Reply #5 on: 27 Nov '21 - 14:09 »
Copy the bass.pas file from the Bass24/Delphi folder to your application's folder. Copy bass.dll to the same folder.

You can use Lazarus's built-in Delphi unit converter , but it may not be necessary.

interface

uses

 Classes, Forms, bass;


That's it.