Forum > Linux

2nd click of button does not repeat the code

(1/1)

stephanos:
Dear All

I am transitioning a Windows Lazarus graphical programme to Linux.  I insert my MP3 player (which has a FAT 32 file system) into my Linux computer.  I start the programme which is on the computer and select a M3U file, which is on the MP3 player.  When the playlist file is opened its contents is written to a memo.

I can then add lines, which are paths/filenames.mp3, to other files and generally edit the window.  If I want to know how many minutes of listening I have in the playlist, I need only click a button, GetPlayTime, which does the following:
reads the memo content into a string
changes the \ to a /
reads in the first line of the string
uses BASS to calculate the playtime of that mp3 file
and iterates until there are no more lines/paths left

When the iteration is done and the total play time is known, it is output to a label caption.

I can then add more line or remove some.  I do not have to save the memo content to the M3U file, because it is the memo content that is read in. 

After making more changes, and without saving the memo content to the M3U file, I want to be able to click the button again and update the total play time displayed.  I do click GetPlayTime and no change is registered.  This does work in the Windows version.

Subsequent changes to the memo window are not recognised when I click the button to get the playtime.  Very odd.

Is there an obvious answer, or do we have to dig deep?

Thanks. And wait to hear

Khrys:
Can you post the code of  GetPlayTime?  And did you make sure that it does indeed iterate over all paths in the memo (e.g. by inserting a  WriteLn  inside the loop)?

stephanos:
Dear All

The m3u file is: /media/stephanos/Sport Go/Music/PlayListStephen.m3u, and the path/filename is saved into a global variable called m3uPathFile.  The content of that file is:

--- Quote ---Alasdair Roberts\Farewell Sorrow\Farewell Sorrow.mp3
Didgeridoo Dreaming - Aboriginal Spiritual Music 1\The First Kangaroo.mp3
Donald Fagen\The Nightfly\Track 02.mp3
Agua de Pena\ROMARIAS\BAILE DA NOITE.mp3
Neil Young\After The Goldrush\Birds.mp3
--- End quote ---


--- Code: ---procedure TForm1.BitBtn4Click(Sender: TObject); // Calculate the play time of
Const  SecsInHour = 3600; SecsInMin = 60; // BitRate = 128000;
var // Unicodestring; is required by BASS
// path2Musicfolder does not change through iteration.  MemoString takes content of Memo1.Lines.Text,
path2Musicfolder, MemoString : Unicodestring;
  fullpathfilename, path2MP3 : Unicodestring; // these always change on each iteration
         TotalPlayTimeInSecs : Double;        // for the total play time in seconds
                  hour, min, sec : byte;          // 0-255
                       Len_Bytes : UInt64;        // used by BASS to get the bytes
                        Len_Secs : Real;          // used by BASS to get the seconds
                                   H : HSTREAM;       // used by BASS
begin
   Len_Bytes := 0;  hour := 0;  min := 0;  Len_Secs := 0.0;
    TotalPlayTimeInSecs := 0;  Tag := 0;             sec := 0;
{* FIRST - Copy the content of Memo.Lines.Text into string: MemoString.*}
   MemoString := Memo1.Lines.Text;  // each path starts after music\
   MemoString := StringReplace(MemoString, '\', '/', [rfIgnoreCase, rfReplaceAll]); // change the \ to /
{* SECOND - Take the global string: m3uPathFile - /media/stephanos/Sport Go/Music/PlayListStephen.m3u,
   and copy it to a local string, then remove the file name.
     path2Musicfolder = /media/stephanos/Sport Go/Music/ *}
   path2Musicfolder := m3uPathFile; // Copy global path/file.M3U string into local string, it has /
   path2Musicfolder := ExtractFilePath(path2Musicfolder); // reduce it to the path not file name
   //Label3.Caption := path2Musicfolder; // used for testing // /media/stephanos/Sport Go/Music/
{* THIRD - Using #10 as a search condition, search for the first mp3 in MemoString. *}
   Tag := Pos(#10, MemoString); // Tag is 0 at start.  Pos returns zero if not found
{* FOURTH - Now enter iteration so as to use BASS and get the play time *}
   BASS_Init(0, 44100, 0, NIL, NIL); //BASS_Init(0, 44100, 0, H, NIL); // used for Windows
   while Tag > 0 do
   begin
      path2MP3 := Copy(MemoString, 1, (Tag-1));  // find the first mp3 file: "Alasdair Roberts/Farewell Sorrow/Farewell Sorrow.mp3" it will include #10
      MemoString := StringReplace(MemoString, path2MP3, '', [rfIgnoreCase]); // remove the  first mp3 entry from MemoString, including the #10
      // #10 remains at the beginning of MemoString, and needs to be removed before Pos is used again
      MemoString := Copy(MemoString, 2, Length(MemoString) ); // remove the #10
      // Now save the two strings into a 3rd string: fullpathfilename and remove the #13 at the end
      fullpathfilename := concat(path2Musicfolder, path2MP3);  //
      Tag := Pos(#13, fullpathfilename);// find the position of #13, 0
      fullpathfilename := copy(fullpathfilename, 1, (Tag-1)); // remove #13 from the end of the string
{*  MemoString is reduced by one entry.  The full path filename of the first mp3 file is in fullpathfilename.
      fullpathfilename is made  up of path2Musicfolder, which will not change, and path2MP3, which will change *}
      H := BASS_StreamCreateFile(FALSE, PWideChar(fullpathfilename), 0, 0, BASS_STREAM_DECODE or BASS_UNICODE);
      Len_Bytes := BASS_ChannelGetLength(H, BASS_POS_BYTE); //Label3.Caption := IntToStr(Len_Bytes); //  42552764
      Len_Secs := BASS_ChannelBytes2Seconds(H, Len_Bytes);
      TotalPlayTimeInSecs := TotalPlayTimeInSecs + Len_Secs;
      // Start again
      path2MP3 := '';   Len_Bytes := 0; Len_Secs := 0;  fullpathfilename := '';
      Tag := Pos(#10, MemoString); // will return zero if you have run out of lines in MemoString
   end;  // End of while Tag > 0 d .....  {*   *}
{* Tidy up strings and integers etc *}
   MemoString := ''; BASS_StreamFree(H); Len_Bytes := 0; Len_Secs := 0;
   // 1076.06630385488, the playtime in seconds for these mp3 files
//
{* FIFTH - calculate the play time in hours, mins, seconds *}
   hour := Trunc(TotalPlayTimeInSecs/SecsInHour); // integer := Double/Const, Trunc is the integer part of float division
   If hour > 0 then // there is more than an hour of play time
   begin            // so reduce TotalPlayTimeInSecs by the number of whole hours
       TotalPlayTimeInSecs := TotalPlayTimeInSecs - (SecsInHour * hour);
   end; // End of If hour < 1 th...
   min := Trunc(TotalPlayTimeInSecs/SecsInMin); // integer := Double/Const,
   If min > 0 then // there is more than a minute of play time left
   begin           // so reduce TotalPlayTimeInSecs by the number of whole minutes
       TotalPlayTimeInSecs := TotalPlayTimeInSecs - (SecsInMin * min);
   end; // End of If hourTheFileNames < 1 th...
   sec := Trunc(TotalPlayTimeInSecs);  {* *}

{* SIXTH - Output total play time  *}
   MemoString := 'The total Play Time for the selected files is:';
   MemoString := Concat(MemoString, ' Hours: ');  MemoString := Concat(MemoString, IntToStr(hour));
   MemoString := Concat(MemoString, ' - Mins: '); MemoString := Concat(MemoString, IntToStr(min));
   MemoString := Concat(MemoString, ' - Secs: '); MemoString := Concat(MemoString, IntToStr(sec));
   Label3.Font.Color := clGreen;  Label3.Caption := MemoString;
{* SEVENTH - Rest variables etc *}
   hour := 0; min := 0; sec := 0;  TotalPlayTimeInSecs := 0;
   MemoString := '';
   Form1.ActiveControl := Memo1; // Pass focus to Play List Windows (Memo1)
end; 
--- End code ---

As you will see from the code, it is the current content of the Memo, that is saved into a string and that string is used by BASS to access each file and get the duration of the output.  Therefore, after I add another path/filename.mp3, and click the button again the current content of the memo should be read in again, and therefore lead to a different calculation.

When I load the file content into the memo, and press the button for the first time the calculated play time is 20 minutes and 29 seconds.  I then add another path/filename.mp3, and press the button again.  There is no change to the calculated playtime.

Any help appreciated

Navigation

[0] Message Index

Go to full version