Differenze tra le versioni di "Eseguire un file audio mediante le funzioni esterne del API di Allegro 5"
Riga 4: | Riga 4: | ||
− | Per poter fruire delle risorse della libreria di ''Allegro 5'', è necessario installare e richiamare le librerie dinamiche e condivise: "''liballegro.so.5.0. | + | Per poter fruire delle risorse della libreria di ''Allegro 5'', è necessario installare e richiamare le librerie dinamiche e condivise: "''liballegro.so.5.0.11''" e "''liballegro_acodec.so.5.0.11''" |
Mostriamo un semplice ed essenziale esempio per poter eseguire un file audio: | Mostriamo un semplice ed essenziale esempio per poter eseguire un file audio: | ||
− | Library "liballegro:5.0. | + | Library "liballegro:5.0.11" |
<FONT Color=gray>' ''uint32_t al_get_allegro_version (void)'' | <FONT Color=gray>' ''uint32_t al_get_allegro_version (void)'' | ||
Riga 24: | Riga 24: | ||
− | Library "liballegro_acodec:5.0. | + | Library "liballegro_acodec:5.0.11" |
Private Const ALLEGRO_PLAYMODE_ONCE As Integer = 256 | Private Const ALLEGRO_PLAYMODE_ONCE As Integer = 256 |
Versione delle 09:47, 14 gen 2018
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 . [Nota 1]
Per poter fruire delle risorse della libreria di Allegro 5, è necessario installare e richiamare le librerie dinamiche e condivise: "liballegro.so.5.0.11" e "liballegro_acodec.so.5.0.11"
Mostriamo un semplice ed essenziale esempio per poter eseguire un file audio:
Library "liballegro:5.0.11" ' 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.11" 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 file_audio As String 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 !") file_audio = "/percorso/del/file/audio" audio = al_load_sample(file_audio) If IsNull(audio) Then Error.Raise("File audio non caricato !") Print "File audio: "; file_audio frequenza = al_get_sample_frequency(audio) Print "Frequenza di campionamento: "; frequenza; " Hertz" Print "Risoluzione campionamento: "; IIf(al_get_sample_depth(audio) = 1, "16", "8"); "-bit" Print "Numero canali: "; al_get_sample_channels(audio) \ 16 pos = al_get_sample_length(audio) Print "Durata: "; 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) al_uninstall_system() End
Mostrare il tempo trascorso
Se si vuol far mostrare il tempo trascorso dall'inizio dell'esecuzione del file audio, allora il precedente codice potrà essere così modificato:
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" 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 file_audio As String Dim audio As Pointer Dim frequenza, pos As Integer Dim tempus As Date 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 !") file_audio = "/percorso/del/file/audio" audio = al_load_sample(file_audio) If IsNull(audio) Then Error.Raise("File audio non caricato !") Print "File audio: "; file_audio frequenza = al_get_sample_frequency(audio) Print "Frequenza di campionamento: "; frequenza; " Hertz" Print "Risoluzione campionamento: "; IIf(al_get_sample_depth(audio) = 1, "16", "8"); "-bit" Print "Numero canali: "; al_get_sample_channels(audio) \ 16 pos = al_get_sample_length(audio) Print "Durata: "; 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 e mostra il tempo trascorso dall'inizio dell'esecuzione del file audio: tempus = Time(Now) Print While CFloat(DateDiff(tempus, Time(Now), gb.Millisecond)) < CFloat(pos / frequenza) * 1000 Write #File.out, "\rTempo trascorso: " & Date(0, 0, 0, 0, 0, 0, DateDiff(tempus, Time(Now), gb.Millisecond)) Wend ' Va in chiusura: al_destroy_sample(audio) al_uninstall_system() 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.