Differenze tra le versioni di "Eseguire un file audio mediante le funzioni esterne del API di Allegro 5"
(14 versioni intermedie di uno stesso utente non sono mostrate) | |||
Riga 1: | Riga 1: | ||
'''Allegro''' è una libreria utilizzata nella programmazione dei videogiochi e del multimediale. | '''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 . | + | 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 . <SUP>[[[#Note|Nota 1]]]</sup> |
− | |||
− | |||
− | |||
− | |||
+ | Per poter fruire delle risorse della libreria di ''Allegro 5'', è necessario installare e richiamare le librerie condivise: "''liballegro.so.5.2.6'' " e "''liballegro_acodec.so.5.2.6'' ". | ||
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. | + | Library "liballegro:5.2.6" |
<FONT Color=gray>' ''uint32_t al_get_allegro_version (void)'' | <FONT Color=gray>' ''uint32_t al_get_allegro_version (void)'' | ||
Riga 18: | Riga 15: | ||
' ''Initialize the Allegro system.''</font> | ' ''Initialize the Allegro system.''</font> | ||
Private Extern al_install_system(version As Integer, atexit_ptr As Pointer) As Boolean | Private Extern al_install_system(version As Integer, atexit_ptr As Pointer) As Boolean | ||
+ | |||
+ | <FONT Color=gray>' ''void al_uninstall_system (void)'' | ||
+ | ' ''Closes down the Allegro system.''</font> | ||
+ | Private Extern al_uninstall_system() | ||
− | Library "liballegro_acodec:5. | + | Library "liballegro_acodec:5.2.6" |
Private Const ALLEGRO_PLAYMODE_ONCE As Integer = 256 | Private Const ALLEGRO_PLAYMODE_ONCE As Integer = 256 | ||
Riga 69: | Riga 70: | ||
− | + | 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_system(al_get_allegro_version(), 0) = False Then Error.Raise("Impossibile inizializzare la libreria Allegro 5 !") | ||
Riga 83: | Riga 84: | ||
If al_reserve_samples(1) = False Then Error.Raise("Impossibile riservare un numero di campioni audio !") | If al_reserve_samples(1) = False Then Error.Raise("Impossibile riservare un numero di campioni audio !") | ||
− | file_audio = "''/percorso/del/file/audio''" | + | file_audio = "<FONT Color=gray>''/percorso/del/file/audio''</font>" |
audio = al_load_sample(file_audio) | audio = al_load_sample(file_audio) | ||
− | If | + | If audio == 0 Then Error.Raise("File audio non caricato !") |
− | Print "File audio: " | + | Print "File audio: "; file_audio |
− | + | ||
frequenza = al_get_sample_frequency(audio) | frequenza = al_get_sample_frequency(audio) | ||
− | Print "Frequenza di campionamento: | + | Print "Frequenza di campionamento: "; frequenza; " Hertz" |
+ | |||
+ | Print "Risoluzione campionamento: "; IIf(al_get_sample_depth(audio) = 1, "16", "8"); "-bit" | ||
+ | |||
+ | Print "Numero dei canali audio: "; al_get_sample_channels(audio) \ 16 | ||
− | |||
− | |||
− | |||
− | |||
pos = al_get_sample_length(audio) | pos = al_get_sample_length(audio) | ||
− | Print "Durata: " | + | Print "Durata dell'audio: "; Time(0, 0, 0, CSingle(pos / frequenza) * 1000) |
− | + | ||
<FONT Color=gray>' ''Esegue il file audio:''</font> | <FONT Color=gray>' ''Esegue il file audio:''</font> | ||
al_play_sample(audio, 1.0, 0.0, 1.0, ALLEGRO_PLAYMODE_ONCE, 0) | 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:''</font> | <FONT Color=gray>' ''Consente l'esecuzione per l'intera durata del file:''</font> | ||
al_rest(CFloat(pos / frequenza)) | al_rest(CFloat(pos / frequenza)) | ||
− | |||
<FONT Color=gray>' ''Va in chiusura:''</font> | <FONT Color=gray>' ''Va in chiusura:''</font> | ||
al_destroy_sample(audio) | 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.2.6" | ||
+ | |||
+ | <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 | ||
+ | |||
+ | <FONT Color=gray>' ''void al_uninstall_system (void)'' | ||
+ | ' ''Closes down the Allegro system.''</font> | ||
+ | Private Extern al_uninstall_system() | ||
+ | |||
+ | |||
+ | Library "liballegro_acodec:5.2.6" | ||
+ | |||
+ | 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 audio == 0 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 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) | ||
+ | |||
+ | <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 "\rTempo trascorso: " & Time(0, 0, 0, DateDiff(tempus, Time(Now), gb.Millisecond)) | ||
+ | Wait 0.01 | ||
+ | Wend | ||
+ | |||
+ | <FONT Color=gray>' ''Va in chiusura:''</font> | ||
+ | al_destroy_sample(audio) | ||
+ | al_uninstall_system() | ||
+ | |||
+ | End | ||
=Note= | =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. | + | [1] La versione 5 della libreria ''Allegro'' non consente di eseguire file Midi; funzionalità posseduta, invece, dalla precedente versione 4, ancora utilizzabile, ma solo in sostituzione della 5. |
− | |||
=Riferimenti= | =Riferimenti= | ||
− | * https:// | + | * https://liballeg.org/a5docs/5.2.4/index.html |
− | * https:// | + | * https://github.com/liballeg/allegro_wiki/wiki |
− |
Versione attuale delle 17:43, 13 lug 2023
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 condivise: "liballegro.so.5.2.6 " e "liballegro_acodec.so.5.2.6 ".
Mostriamo un semplice ed essenziale esempio per poter eseguire un file audio:
Library "liballegro:5.2.6" ' 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.2.6" 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 audio == 0 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 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) ' 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.2.6" ' 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.2.6" 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 audio == 0 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 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) ' 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 "\rTempo trascorso: " & Time(0, 0, 0, DateDiff(tempus, Time(Now), gb.Millisecond)) Wait 0.01 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 solo in sostituzione della 5.