La gestione mediante le funzioni esterne dell'API di SDL
Vai alla navigazione
Vai alla ricerca
La libreria SDL è un API multi-piattaforma contenente funzioni per la gestione multimediale dell'audio e del video.
Per poter utilizzare le funzioni esterne di SDL in Gambas, si potranno richiamare anche separatamente le seguenti specifiche librerie:
libSDL-1.2.so.0.11.4 libSDL_mixer-1.2.so.0.12.0 libSDL_sound-1.0.so.1.0.2
Per poter riprodurre più suoni contemporaneamente è necessario utilizzare le funzioni della sub-libreria mixer audio multi-canale SDL_mixer.
Di seguito mostreremo un semplice codice per eseguire un file audio WAV:
Private Const AUDIO_S16SYS As Integer = 32784 ' Campioni a 16-bit
Private Const SDL_INIT_AUDIO As Byte = 10
Private Const STEREO As Byte = 2
Library "libSDL-1.2:0.11.4"
' int SDL_Init(Uint32 flags)
Private Extern SDL_Init(flags As Integer) As Integer
' char * SDL_GetError(void)
Private Extern SDL_GetError() As String
' void SDL_Quit(void)
Private Extern SDL_Quit()
Library "libSDL_mixer-1.2:0.12.0"
' char * Mix_GetError(void)
Private Extern Mix_GetError() As String
' int Mix_OpenAudio(Int frequency, Uint16 Format, Int channels, Int chunksize)
Private Extern Mix_OpenAudio(frequency As Integer, formatSh As Short, channels As Byte, chunksize As Integer) As Integer
' Mix_Chunk * Mix_LoadWAV_RW(SDL_RWops *src, int freesrc)
Private Extern Mix_LoadWAV_RW(src As Pointer, freesrc As Integer) As Pointer
' int Mix_PlayChannelTimed(int channel, Mix_Chunk *chunk, int loops, int ticks)
Private Extern Mix_PlayChannelTimed(channel As Integer, chunk As Pointer, loops As Integer, ticks As Integer) As Integer
' void Mix_FreeChunk(Mix_Chunk *chunk)
Private Extern Mix_FreeChunk(chunk As Pointer)
' void Mix_CloseAudio(void)
Private Extern Mix_CloseAudio()
' int Mix_PlayMusic(Mix_Music *music, int loops)
Private Extern Mix_PlayMusic(music As Pointer, loops As Integer) As Integer
Library "libSDL_sound-1.0:1.0.2"
' SDL_RWops *SDL_RWFromFile(const char *file, const char *mode)
Private Extern SDL_RWFromFile(src As String, mode As String) As Pointer
Public Sub Button1_Click()
Dim err, channel As Integer
Dim audio_rate As Integer = 44100 ' Imposta la frequenza che verrà usata da "SDL_mixer"
Dim audio_buffers As Short = 4096 ' Determina la quantità di blocchi di memeoria utilizzati per incamerare ed eseguire i campioni audio
Dim fl, sound As Pointer
' Inizializza il dispositivo SDL audio:
err = SDL_Init(SDL_INIT_AUDIO)
If err < 0 Then Error.Raise("Impossibile inizializzare la libreria SDL: " & SDL_GetError())
' Inizializza la libreria "SDL_mixer" con specifiche impoostazioni audio:
err = Mix_OpenAudio(audio_rate, AUDIO_S16SYS, STEREO, audio_buffers)
If err <> 0 Then Error.Raise("Unable to initialize audio: " & Mix_GetError())
fl = SDL_RWFromFile("/percorso/del/file.wav", "rb")
If IsNull(fl) Then Error.Raise("Errore nel caricamenteo del file !")
' Carica il file WAV:
sound = Mix_LoadWAV_RW(fl, 1)
' Esegue il file WAV caricato, ed intercetta il canale sul quale viene eseguito.
' Passando il valore -1 al primo argomento della funzione, il campione audio sarà eseguito sul primo canale audio disponibile:
channel = Mix_PlayChannelTimed(-1, sound, 0, 0)
If channel = -1 Then Error.Raise("Impossibile eseguire il file WAV: " & Mix_GetError())
' Attende che sia terminato il file WAV:
While Mix_Playing(channel) <> 0
Wait 0.01
Wend
' Libera la memoria precedentemente allocata per l'esecuzione sonora:
Mix_FreeChunk(sound)
' Chiude l'interfaccia audio SDL e SDL_mixer:
Mix_CloseAudio()
SDL_Quit()
End
Riferimenti
- Il sito dell'API di SDL.