Estrarre informazioni generali da un file audio con le funzioni esterne del API di Libaudiofile

Da Gambas-it.org - Wikipedia.

La libreria Libaudiofile consente la lettura e la scrittura, nonché di ottenere informazioni relative a file audio appartenenti ai formati wav, aiff, aifc, snd, au, voc ed altri.


Per poter fruire delle risorse della libreria Libaudiofile è necessario aver installata nel proprio sistema la libreria dinamica condivisa: "libaudiofile.so.1.0.0"

Di seguito mostriamo un possibile codice per ottenenere informazioni generali da un file audio.

Library "libaudiofile:1.0.0"

Private Const AF_DEFAULT_TRACK As Integer = 1001
Private Const AF_COMPRESSION_NONE As Integer = 0
Private Enum AF_QUERY_NAME = 600, AF_QUERY_DESC, AF_QUERY_LABEL, AF_QUERY_TYPE, AF_QUERY_DEFAULT
Private Enum AF_BYTEORDER_BIGENDIAN = 501, AF_BYTEORDER_LITTLEENDIAN
Private Enum AF_QUERYTYPE_INSTPARAM = 500,
             AF_QUERYTYPE_FILEFMT,
             AF_QUERYTYPE_COMPRESSION,
             AF_QUERYTYPE_COMPRESSIONPARAM,
             AF_QUERYTYPE_MISC,
             AF_QUERYTYPE_INST,
             AF_QUERYTYPE_MARK,
             AF_QUERYTYPE_LOOP
Private Enum AF_SAMPFMT_TWOSCOMP = 401,
             AF_SAMPFMT_UNSIGNED,
             AF_SAMPFMT_FLOAT,
             AF_SAMPFMT_DOUBLE
  
' AFfilehandle afOpenFile (const char *filename, const char *mode, AFfilesetup setup)
' Opens a specified audio file and creates a file handle structure.
Private Extern afOpenFile(filename As String, mode As String, setup As Pointer) As Pointer

' int afGetChannels (AFfilehandle, int track)
' Gets number of channels.
Private Extern afGetChannels(AFfilehandle As Pointer, track As Integer) As Integer

' double afGetRate (AFfilehandle, int track)
' Gets sampling rate.
Private Extern afGetRate(AFfilehandle As Pointer, track As Integer) As Float

' int afGetFileFormat (AFfilehandle, int *version)
' Returns an integer value indicating the format of the file.
Private Extern afGetFileFormat(AFfilehandle As Pointer, version As Pointer) As Integer

' void afGetSampleFormat (AFfilehandle file, int track, int *sampfmt, int *sampwidth)
' Gets the audio sample format.
Private Extern afGetSampleFormat(AFfilehandle As Pointer, track As Integer, sampfmt As Pointer, sampwidth As Pointer)

' int afGetByteOrder (AFfilehandle, int track)
' Gets the byte order for a specified audio track.
Private Extern afGetByteOrder(AFfilehandle As Pointer, track As Integer) As Integer

' int afGetCompression (AFfilehandle, int track)
' Gets the compression type.
Private Extern afGetCompression(AFfilehandle As Pointer, track As Integer) As Integer

' AFfileoffset afGetTrackBytes (AFfilehandle, int track)
' Gets the data bytes for a specified audio.
Private Extern afGetTrackBytes(AFfilehandle As Pointer, track As Integer) As Integer

' AFfileoffset afGetDataOffset (AFfilehandle, int track)
' Gets the data offset for a specified audio.
Private Extern afGetDataOffset(AFfilehandle As Pointer, track As Integer) As Integer

' AFframecount afGetFrameCount (AFfilehandle file, int track)
' Gets the total sample frame count for a specified audio.
Private Extern afGetFrameCount(AFfilehandle As Pointer, track As Integer) As Long

' void *afQueryPointer (int querytype, int arg1, int arg2, int arg3, int arg4)
' Return value will usually be cast by the application to the appropriate pointer type.
Private Extern afQueryPointer(querytype As Integer, arg1 As Integer, arg2 As Integer, arg3 As Integer, arg4 As Integer) As String

' int afCloseFile (AFfilehandle file)
' Closes an open audio file.
Private Extern afCloseFile(AFfilehandle As Pointer) As Integer


Public Sub Main()
 
 Dim file_audio, formStr, labelStr As String
 Dim twos, formato, compressione As String
 Dim AFp As Pointer
 Dim canali, fileFormat, sampleFormat, sampleWidth As Integer
 Dim frequenza As Float
 Dim byteOrder, tipoCompressione, gtb, gdo As Integer
 Dim frames As Long
 
  file_audio = "/percorso/del/file/audio"
   
  Print "=== Informazioni sul file audio ==="
  Print "\nNome file:          "; File.Name(file_audio)
 
  AFp = afOpenFile(file_audio, "r", Null)
  If IsNull(AFp) Then Error.Raise("Impossibile aprire il file !")

  canali = afGetChannels(AFp, AF_DEFAULT_TRACK)
  frequenza = afGetRate(AFp, AF_DEFAULT_TRACK)
  Print "Canali audio:       "; canali
  Print "Frequenza camp.nto: "; frequenza; " Hertz"
 
  fileFormat = afGetFileFormat(AFp, Null)
 
  formStr = afQueryPointer(AF_QUERYTYPE_FILEFMT, AF_QUERY_DESC, fileFormat, 0, 0)
  labelStr = afQueryPointer(AF_QUERYTYPE_FILEFMT, AF_QUERY_LABEL, fileFormat, 0, 0)
  Print "Formato file:       "; formStr; " ("; labelStr; ")"
 
  afGetSampleFormat(AFp, AF_DEFAULT_TRACK, VarPtr(sampleFormat), VarPtr(sampleWidth))
 
  byteOrder = afGetByteOrder(AFp, AF_DEFAULT_TRACK)
 
  tipoCompressione = afGetCompression(AFp, AF_DEFAULT_TRACK)
 
  If tipoCompressione = AF_COMPRESSION_NONE Then

    If byteOrder = AF_BYTEORDER_BIGENDIAN Then
      twos = "big endian"
    Else
      twos = "little endian"
    Endif
   
    formato = "Formato dati:       "
    Select Case sampleFormat
      Case AF_SAMPFMT_TWOSCOMP
        formato &= CStr(sampleWidth) & "-bit integer (2's complement, " & twos & ")"
      Case AF_SAMPFMT_UNSIGNED
        formato &= CStr(sampleWidth) & "-bit integer (unsigned, " & twos & ")"
      Case AF_SAMPFMT_FLOAT
        formato &= "single-precision (32-bit) floating point, " & twos & ")"
      Case AF_SAMPFMT_DOUBLE
        formato &= "double-precision (64-bit) floating point, " & twos & ")"
      Case Else
        formato &= "sconosciuto"
    End Select
    Print formato
   
    compressione = afQueryPointer(AF_QUERYTYPE_COMPRESSION, AF_QUERY_NAME, tipoCompressione, 0, 0)
    If IsNull(compressione) Then
      Print "Compressione:       sconosciuta"
    Else
      Print "Compressione:       "; compressione
    Endif
   
    gtb = afGetTrackBytes(AFp, AF_DEFAULT_TRACK)
    gdo = afGetDataOffset(AFp, AF_DEFAULT_TRACK)
    Print "Dati audio grezzi:  "; gtb; " byte, che iniziano dall'offset "; gdo; " ('"; Hex(gdo); "' Esad.)"
   
    frames = afGetFrameCount(AFp, AF_DEFAULT_TRACK)
    Print "Frames:             "; frames
    Print "Durata audio        "; CStr(Date(0, 0, 0, 0, 0, 0, (frames / frequenza) * 1000))

  Endif

  
' Va in chiusura:
  afCloseFile(AFp)

End



Riferimenti