Differenze tra le versioni di "Eseguire un file audio mediante le funzioni esterne del API di Allegro 5"
Riga 110: | Riga 110: | ||
'''End''' | '''End''' | ||
+ | |||
+ | |||
+ | |||
+ | ===Mostrare il tempo trascorso=== | ||
+ | Se si vuol far mostrare il tempo nel frattempo trasorso dall'inizio dell'esecuzione del file audio, allora il precedente codice potrà essere così modificato: | ||
+ | Library "liballegro:5.0.10" | ||
+ | |||
+ | <FONT Color=gray>' ''uint32_t al_get_allegro_version (void)'' | ||
+ | ' ''Returns the (compiled) version of the Allegro library.''</font> | ||
+ | Private Extern al_get_allegro_version() As Integer | ||
+ | |||
+ | <FONT Color=gray>' ''bool al_install_system (int version, int (*atexit_ptr)(void (*)(void)))'' | ||
+ | ' ''Initialize the Allegro system.''</font> | ||
+ | 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 | ||
+ | |||
+ | <FONT Color=gray>' ''bool al_install_audio (void)'' | ||
+ | ' ''Install the audio subsystem.''</font> | ||
+ | Private Extern al_install_audio() As Boolean | ||
+ | |||
+ | <FONT Color=gray>' ''bool al_init_acodec_addon (void)'' | ||
+ | ' ''Registers all the known audio file type handlers for al_load_sample.''</font> | ||
+ | Private Extern al_init_acodec_addon() As Boolean | ||
+ | |||
+ | <FONT Color=gray>' ''bool al_reserve_samples (int reserve_samples)'' | ||
+ | ' ''Reserves a number of sample instances, attaching them to the default mixer.''</font> | ||
+ | Private Extern al_reserve_samples(reserve_samples As Integer) As Boolean | ||
+ | |||
+ | <FONT Color=gray>' ''ALLEGRO_SAMPLE * al_load_sample (const char *filename)'' | ||
+ | ' ''Loads a few different audio file formats based on their extension.''</font> | ||
+ | Private Extern al_load_sample(filename As String) As Pointer | ||
+ | |||
+ | <FONT Color=gray>' ''unsigned int al_get_sample_frequency (const ALLEGRO_SAMPLE *spl)'' | ||
+ | ' ''Return the frequency of the sample.''</font> | ||
+ | Private Extern al_get_sample_frequency(spl As Pointer) As Integer | ||
+ | |||
+ | <FONT Color=gray>' ''ALLEGRO_AUDIO_DEPTH al_get_sample_depth (const ALLEGRO_SAMPLE *spl)'' | ||
+ | ' ''Return the audio depth.''</font> | ||
+ | Private Extern al_get_sample_depth(spl As Pointer) As Integer | ||
+ | |||
+ | <FONT Color=gray>' ''unsigned int al_get_sample_length (const ALLEGRO_SAMPLE *spl)'' | ||
+ | ' ''Return the length of the sample in sample values.''</font> | ||
+ | Private Extern al_get_sample_length(spl As Pointer) As Integer | ||
+ | |||
+ | <FONT Color=gray>' ''ALLEGRO_CHANNEL_CONF al_get_sample_channels (const ALLEGRO_SAMPLE *spl)'' | ||
+ | ' ''Return the channel configuration.''</font> | ||
+ | Private Extern al_get_sample_channels(spl As Pointer) As Integer | ||
+ | |||
+ | <FONT Color=gray>' ''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.''</font> | ||
+ | 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 | ||
+ | |||
+ | <FONT Color=gray>' ''void al_rest (double seconds)'' | ||
+ | ' ''Waits for the specified number seconds.''</font> | ||
+ | Private Extern al_rest(seconds As Float) | ||
+ | |||
+ | <FONT Color=gray>' ''void al_destroy_sample (ALLEGRO_SAMPLE *spl)'' | ||
+ | ' ''Free the sample data structure.''</font> | ||
+ | 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 = "<FONT Color=gray>''/percorso/del/file/audio''</font>" | ||
+ | |||
+ | 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) | ||
+ | |||
+ | <FONT Color=gray>' ''Esegue il file audio:''</font> | ||
+ | al_play_sample(audio, 1.0, 0.0, 1.0, ALLEGRO_PLAYMODE_ONCE, 0) | ||
+ | |||
+ | <FONT Color=gray>' ''Consente l'esecuzione per l'intera durata del file e mostra il tempo trascorso dall'inizio dell'esecuzione del file audio:''</font> | ||
+ | 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 | ||
+ | |||
+ | |||
+ | <FONT Color=gray>' ''Va in chiusura:''</font> | ||
+ | al_destroy_sample(audio) | ||
+ | |||
+ | '''End''' | ||
+ | |||
Versione delle 14:57, 18 feb 2016
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 installare e 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 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) End
Mostrare il tempo trascorso
Se si vuol far mostrare il tempo nel frattempo trasorso 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 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) 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.