Convertire un file audio da un formato in formato .wav con le funzioni esterne del API di Allegro 5

Da Gambas-it.org - Wikipedia.

Allegro è una libreria utilizzata nella programmazione dei videogiochi e del multimediale.

La versione 5 della libreria Allegro consente di utilizzare le sue risorse anche in Gambas per convertire un file audio da uno dei seguenti formati: .wav, .flac, .ogg, .it, .mod, .s3m, .xm in formato .wav .

Per poter fruire delle risorse della libreria di Allegro 5, è necessario installare e richiamare le librerie condivise: "liballegro.so.5.0.10 " e "liballegro_acodec.so.5.0.10 ".

Mostriamo un semplice ed essenziale esempio per poter convertire un file audio da un formato supportato al formato WAV:

Library "liballegro:5.0.10"

' uint32_t al_get_allegro_version (void)
' Returns the (compiled) version of the Allegro library.
Private Extern al_get_allegro_version() As Integer

' bool al_install_system (int version, int (*atexit_ptr)(void (*)(void)))
' Initialize the Allegro system.
Private Extern al_install_system(version As Integer, atexit_ptr As Pointer) As Boolean

' void al_uninstall_system (void)
' Closes down the Allegro system.
Private Extern al_uninstall_system()


Library "liballegro_acodec:5.0.10"
 
' bool al_install_audio (void)
' Install the audio subsystem.
Private Extern al_install_audio() As Boolean

' bool al_init_acodec_addon (void)
' Registers all the known audio file type handlers for al_load_sample.
Private Extern al_init_acodec_addon() As Boolean
 
' ALLEGRO_SAMPLE * al_load_sample (const char *filename)
' Loads a few different audio file formats based on their extension.
Private Extern al_load_sample(filename As String) As Pointer

' unsigned int al_get_sample_frequency (const ALLEGRO_SAMPLE *spl)
' Return the frequency of the sample.
Private Extern al_get_sample_frequency(spl As Pointer) As Integer

' ALLEGRO_AUDIO_DEPTH al_get_sample_depth (const ALLEGRO_SAMPLE *spl)
' Return the audio depth.
Private Extern al_get_sample_depth(spl As Pointer) As Integer

' unsigned int al_get_sample_length (const ALLEGRO_SAMPLE *spl)
' Return the length of the sample in sample values.
Private Extern al_get_sample_length(spl As Pointer) As Integer

' ALLEGRO_CHANNEL_CONF al_get_sample_channels (const ALLEGRO_SAMPLE *spl)
' Return the channel configuration.
Private Extern al_get_sample_channels(spl As Pointer) As Integer

' bool al_save_sample (const char *filename, ALLEGRO_SAMPLE *spl)
' Writes a sample into a file. Currently, wav is the only supported format, and the extension must be '.wav'.
Private Extern al_save_sample(filename As String, spl As Pointer) As Boolean

' void al_destroy_sample (ALLEGRO_SAMPLE *spl)
' Free the sample data structure.
Private Extern al_destroy_sample(spl As Pointer)


Public Sub Main()

  Dim audio As Pointer
  Dim frequenza, pos As Integer

  If al_install_system(al_get_allegro_version(), 0) = False Then Error.Raise("Impossibile inizializzare la libreria Allegro 5 !")
   
  If al_install_audio() = False Then Error.Raise("Impossibile inizializzare l'audio !")
   
  If al_init_acodec_addon() = False Then Error.Raise("Impossibile inizializzare le codifiche audio !")
 
  audio = al_load_sample("/percorso/del/file/audio")
  If audio == 0 Then Error.Raise("File audio non caricato !")
   
  frequenza = al_get_sample_frequency(audio)
  Print "Frequenza di campionamento: "; frequenza; " hertz"

  Print "Risoluzione campionamento:  "; (8 * al_get_sample_depth(audio)) + 8; "bit"
   
  Print "Numero dei canali audio:    "; al_get_sample_channels(audio) \ 16
    
  pos = al_get_sample_length(audio)
  Print "Durata dell'audio:          "; Time(0, 0, 0, CSingle(pos / frequenza) * 1000)

  If al_save_sample("/percorso/del/nuovo/file.wav", audio) Then
    Print "\nConversione del file audio in file wav: eseguita."
  Else
    Error.Raise("Impossibile effettuare la conversione !")
  Endif
   
' Va in chiusura:
  al_destroy_sample(audio)
  al_uninstall_system()

End


Riferimenti