Recent Posts

Pages: « 1 2 3 4 5 6 7 8 9 10 »
81
General Discussion / Re: mp3 duration
« Last post by José Roca on December 27, 2019, 01:23:12 PM »
> would be nice to have something like GUID$ for FB too !

I have one: AfxGuid (in AfxCOM.inc)

Code: [Select]
' ========================================================================================
' Converts a string into a 16-byte (128-bit) Globally Unique Identifier (GUID)
' To be valid, the string must contain exactly 32 hexadecimal digits, delimited by hyphens
' and enclosed by curly braces. For example: {B09DE715-87C1-11D1-8BE3-0000F8754DA1}
' If pwszGuidText is omited, AfxGuid generates a new unique guid.
' Remarks: I have need to call the UuidCreate function dynamically because, at the time of
' writing, the library for the RPCRT4.DLL seems broken and the linker fails.
' ========================================================================================
PRIVATE FUNCTION AfxGuid (BYVAL pwszGuidText AS WSTRING PTR = NULL) AS GUID
   DIM rguid AS GUID
   IF pwszGuidText = NULL THEN
      ' // Generate a new guid
      DIM AS ANY PTR pLib = DyLibLoad("RPCRT4.DLL")
      IF pLib  THEN
         DIM pProc AS FUNCTION (BYVAL Uuid AS UUID PTR) AS RPC_STATUS
         pProc = DyLibSymbol(pLib, "UuidCreate")
         IF pProc THEN pProc(@rguid)
         DyLibFree(pLib)
      END IF
   ELSE
      CLSIDFromString(pwszGuidText, @rGuid)
   END IF
   RETURN rguid
END FUNCTION
' ========================================================================================
82
General Discussion / Re: mp3 duration
« Last post by Juergen Kuehlwein on December 27, 2019, 12:44:09 PM »
PB code:
Code: [Select]
#COMPILE EXE 
#DIM ALL

'#utility roca3                                        'use José Roca includes

#Include "win32api.inc"
#Include "shobjidl.inc"
#Include "propkey.inc"


FUNCTION duration(file AS WSTRINGZ) AS LONG
'***********************************************************************************************
' return duration of an audio file in ms, -1 for fail
'***********************************************************************************************
LOCAL d          AS QUAD
LOCAL propVar    AS PropVariant
LOCAL hr         AS LONG
LOCAL pPropStore AS IPropertyStore


  hr = SHGetPropertyStoreFromParsingName(BYVAL VARPTR(file), NOTHING, %GPS_DEFAULT, $IID_IPropertyStore, pPropStore)
  IF hr = %S_OK THEN
    hr = pPropStore.GetValue(PKEY_Media_Duration, propVar)
    IF hr = %S_OK THEN
      d = propVar.uhVal                               'time in 100ns

    ELSE
      FUNCTION = -1
      EXIT FUNCTION
    END IF
   
    pPropStore = NOTHING

  ELSE
    FUNCTION = -1
    EXIT FUNCTION
  END IF


  FUNCTION = d/10000                                  'time in ms


END FUNCTION


FUNCTION PBMAIN () AS LONG
'***********************************************************************************************
' -> change paths ...  (must be a full path)
'***********************************************************************************************

  ? STR$(duration("C:\PBwin10\IDE\Projects\mp3\blues.mp3"))
  ? STR$(duration("C:\PBwin10\IDE\Projects\mp3\cher.mid"))
  ? STR$(duration("C:\PBwin10\IDE\Projects\mp3\ring05.wav"))
 

END FUNCTION 

FB code:
Code: [Select]
'#COMPILER FREEBASIC
'#COMPILE CONSOLE 32 '64


#INCLUDE ONCE "windows.bi"
#INCLUDE ONCE "win\shobjidl.bi"


FUNCTION duration (file AS WSTRING) AS LONG
'***********************************************************************************************
' return duration of an audio file in ms, -1 for fail
'***********************************************************************************************
DIM d                   AS ULONGINT
DIM propVar             AS PropVariant
DIM hr                  AS HRESULT
DIM pPropStore          AS IPropertyStore PTR
DIM PKEY_Media_Duration AS PROPERTYKEY

'***********************************************************************************************
' MACRO PKEY_Media_Duration = GUID$("{64440490-4C8B-11D1-8B70-080036B11A03}") & MKDWD$(3)
' would be nice to have something like GUID$ for FB too !
'***********************************************************************************************
  PKEY_Media_Duration.fmtid.data1    = &H64440490
  PKEY_Media_Duration.fmtid.data2    = &H4C8B
  PKEY_Media_Duration.fmtid.data3    = &H11D1
  PKEY_Media_Duration.fmtid.data4(0) = &H8B
  PKEY_Media_Duration.fmtid.data4(1) = &H70
  PKEY_Media_Duration.fmtid.data4(2) = &H08
  PKEY_Media_Duration.fmtid.data4(3) = &H00
  PKEY_Media_Duration.fmtid.data4(4) = &H36
  PKEY_Media_Duration.fmtid.data4(5) = &HB1
  PKEY_Media_Duration.fmtid.data4(6) = &H1A
  PKEY_Media_Duration.fmtid.data4(7) = &H03
  PKEY_Media_Duration.pid   = 3


  coinitialize(0)                                     'a must in FB

  hr = SHGetPropertyStoreFromParsingName(varptr(file), 0, GPS_DEFAULT, @IID_IPropertyStore, @pPropStore)
  IF hr = S_OK THEN
    hr = IPropertyStore_GetValue(pPropStore, @PKEY_Media_Duration, @propVar)
    IF hr = S_OK THEN
      d = propVar.uhVal.QuadPart                      'time in 100ns

    ELSE
      couninitialize
      RETURN -1
    END IF
   
    IPropertyStore_Release(pPropStore)

  ELSE
    couninitialize
    RETURN -1
  END IF


  couninitialize
  RETURN d/10000                                      'time in ms


END FUNCTION


'***********************************************************************************************
' -> change paths ...  (must be a full path)
'***********************************************************************************************

  PRINT duration("C:\PBwin10\IDE\Projects\mp3\blues.mp3")
  PRINT duration("C:\PBwin10\IDE\Projects\mp3\cher.mid")
  PRINT duration("C:\PBwin10\IDE\Projects\mp3\ring05.wav")


  SLEEP
83
General Discussion / Re: mp3 duration
« Last post by Juergen Kuehlwein on December 27, 2019, 11:38:49 AM »
Yes, for compression formats with a variable bit rate like mp3 you cannot just divide the number of frames by the rate (fps). But you can calculate the exact duration by looping through the frames (i think this is what BASS_STREAM_PRESCAN does). There is sample code for this at various places in the net.

In the meantime i found what i was looking for. What an effort for a few lines of code! In fact retrieving the duration form the propertystore delivers correct results (even for mp3, Windows 7 and 10). I will post code here soon.
84
General Discussion / Re: mp3 duration
« Last post by Patrice Terrier on December 27, 2019, 10:32:14 AM »
I think to remember from the top of my head that mp3 duration is only an approximation, because of the audio signal compression.
(mostly with mp3 using a variable bit rate)
The true length can be known only when audio comes to full completion.

This is the reason why Bass.dll use the BASS_STREAM_PRESCAN flag to return an accurate length.

What you could do is to encode the correct duration into the ID3 tag (there are many utilities able to do this).
Or convert the mp3 to ogg with Audacity if you don't need the ID3 tag.
85
General Discussion / Re: mp3 duration
« Last post by Juergen Kuehlwein on December 26, 2019, 10:04:23 PM »
Thanks José,


good to know that there is a FreeBASIC equivalent. I already know how to do it (PB and FB). But the problem remains the same, IMediaSeeking GetDuration returns a wrong result for mp3 files. Seems i need a translation of Bob Carvers´s code for that.
86
General Discussion / Re: mp3 duration
« Last post by José Roca on December 26, 2019, 07:34:03 PM »
You can look at my CAfxMp3.inc file, that includes de CAfxMp3 class. It gets the duration callig the GetDuration method of the IMediaSeeking interface. Be aware that this method returns 10,000,000 for a second (100-nanosecond units).

BTW I ported this class to FreeBasic (CDSAudio class in CDSAudio.inc).
Documentation: https://github.com/JoseRoca/WinFBX/blob/master/docs/COM/CDSAudio%20Class.md
87
General Discussion / Re: mp3 duration
« Last post by Juergen Kuehlwein on December 26, 2019, 06:35:21 PM »
Finally i found what i need here (https://forum.powerbasic.com/forum/user-to-user-discussions/powerbasic-for-windows/775019-mp3-tagger-discussion , Bob Carvers code). It reads (at least i think it reads it) the correct duration, even if there are no ID3 tags.
88
General Discussion / Re: mp3 duration
« Last post by Juergen Kuehlwein on December 26, 2019, 05:28:02 PM »
I know that the duration information displayed by Windows doesn´t come from ID3 tags, because, when i remove these tags (i verfied removal with a hex editor), the duration displayed stays the same. Mp3tag (a program for showing and editing id3 tags) shows the correct duration with and without tags in Windows 7 and 10. So there must be some other (working) method than Media foundation.
89
General Discussion / Re: mp3 duration
« Last post by Juergen Kuehlwein on December 26, 2019, 03:48:39 PM »
In the meantime i found Jim Fritts´ PB source code (https://forum.powerbasic.com/forum/user-to-user-discussions/source-code/773139-media-foundation-player) and could adapt it to show the duration - tada, now it´s correct.

Unfortunately the Media Foundation API seems a rather complicated way of retrieving this little piece of data and it requires Windows 8 or higher - i was hoping for an easier solution.
90
General Discussion / Re: mp3 duration
« Last post by Juergen Kuehlwein on December 26, 2019, 12:17:33 PM »
Patrice,

Merry Christmas to you as well!

Thanks for your reply. I read about IMediaSeeking being a better choice, but for me (Windows 10), it returns the same (wrong) result as IMediaPosition. Strange that this bug hasn´t be fixed as of now and that you must do it yourself as your link proposes.

I know and already used Bass.dll (a great piece of software btw.), but i wanted to be independent of third party software. And obviously Windows can do what i want.
Pages: « 1 2 3 4 5 6 7 8 9 10 »