Eseguire un file audio mediante le funzioni esterne del API di Allegro 5
Da Gambas-it.org - Wikipedia.
Versione del 6 feb 2015 alle 11:59 di Vuott (Discussione | contributi)
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 l'esecuzione di file audio appartenenti ai seguenti formati: .wav, .flac, .ogg, .it, .mod, .s3m, .xm . |1|
Per poter fruire delle risorse della libreria di Allegro 5, è necessario richiamare le librerie dinamiche e condivise: "liballegro.so.5.0.10" e "liballegro_acodec.so.5.0.10"
Mostriamo un semplice ed essenziale esempio per poter eseguire un file audio:
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 Library "liballegro_acodec:5.0.10" Private Const ALLEGRO_PLAYMODE_ONCE As Integer = 256 ' 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 ' bool al_reserve_samples (int reserve_samples) ' Reserves a number of sample instances, attaching them to the default mixer. Private Extern al_reserve_samples(reserve_samples As Integer) 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_play_sample (ALLEGRO_SAMPLE *data, float gain, float pan, float speed, ALLEGRO_PLAYMODE loop, ALLEGRO_SAMPLE_ID *ret_id) ' Plays a sample on one of the sample instances created by al_reserve_samples. Private Extern al_play_sample(data As Pointer, gain As Single, pan As Single, speed As Single, playmode As Integer, ret_id As Pointer) As Boolean ' void al_rest (double seconds) ' Waits for the specified number seconds. Private Extern al_rest(seconds As Float) ' 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 !") If al_reserve_samples(1) = False Then Error.Raise("Impossibile riservare un numero di campioni audio !") audio = al_load_sample("/percorso/del/file/audio") If IsNull(audio) Then Error.Raise("File audio non caricato !") frequenza = al_get_sample_frequency(audio) Print "Frequenza di campionamento: Hz ", Null; frequenza Print "Risoluzione campionamento: ", Null; (8 * al_get_sample_depth(audio)) + 8; "bit" Print "Numero canali: ", Null, Null, al_get_sample_channels(audio) \ 16 pos = al_get_sample_length(audio) Print "Durata: ", Null, Null, Date(0, 0, 0, 0, 0, 0, CSingle(pos / frequenza) * 1000) ' Esegue il file audio: al_play_sample(audio, 1.0, 0.0, 1.0, ALLEGRO_PLAYMODE_ONCE, 0) ' Consente l'esecuzione per l'intera durata del file: al_rest(CFloat(pos / frequenza)) ' Va in chiusura: al_destroy_sample(audio) End
Note
[1] La versione 5 della libreria Allegro non consente di eseguire file Midi; funzionalità posseduta, invece, dalla precedente versione 4, ancora utilizzabile, ma in sostituzione della 5.