La gestione mediante le funzioni esterne del API di SDL 2

Da Gambas-it.org - Wikipedia.
Versione del 29 dic 2014 alle 09:13 di Vuott (Discussione | contributi) (Creata pagina con 'La libreria '''''SDL 2''''' è un [http://it.wikipedia.org/wiki/Application_programming_interface API] multi-piattaforma contenente funzioni per la gestione multimediale dell'...')

(diff) ← Versione meno recente | Versione attuale (diff) | Versione più recente → (diff)

La libreria SDL 2 è un API multi-piattaforma contenente funzioni per la gestione multimediale dell'audio e del video.


Per poter utilizzare le funzioni esterne di SDL 2 in Gambas, si richiameranno anche separatamente per lo più le seguenti librerie (con le attuali versioni):

  • libSDL2-2.0:0.2.0
  • libSDL2_mixer-2.0:0.0.0


E' possibile riprodurre più suoni contemporaneamente utilizzando le funzioni della sub-libreria mixer audio multi-canale libSDL2_mixer.


Eseguire un file WAV intercettando il canale sul quale viene eseguito

E' possibile eseguire file WAV su flussi audio, definiti canali. L'esecuzione sarà effettuata usando la funzione Mix_PlayChannelTImed. Questa risorsa consente di eseguire sino a 32 file audio di formato WAV contemporaneamente. La predetta funzione intercetterà il canale sul quale il file WAV viene eseguito, restituendone un valore di tipo puntatore che ne consentirà la successiva gestione.

Di seguito mostreremo un semplice codice per eseguire un solo file audio WAV.

Library "libSDL2-2.0:0.2.0"

Private Const SDL_INIT_AUDIO As Integer = 16
Private Const MIX_DEFAULT_FORMAT As Integer = 32784

' int SDL_Init(Uint32 flags)
' Initialize the SDL library.
Private Extern SDL_Init(flags As Integer) As Integer

' const char* SDL_GetError(void)
' Retrieve a message about the last error that occurred.
Private Extern SDL_GetError() As String

' void SDL_Quit(void)
' Clean up all initialized subsystems.
Private Extern SDL_Quit()


Library "libSDL2_mixer-2.0:0.0.0"

Private Const MIX_DEFAULT_FREQUENCY As Integer = 22050

' int Mix_OpenAudio(int frequency, Uint16 format, int channels, int chunksize)
' Initialize the mixer API.
Private Extern Mix_OpenAudio(frequency As Integer, format16 As Short, channels As Integer, chunksize As Integer) As Integer

' int Mix_QuerySpec(int *frequency, Uint16 *format, int *channels)
' Get the actual audio format in use by the opened audio device.
Private Extern Mix_QuerySpec(frequencyP As Pointer, formatP As Pointer, channelsP As Pointer) As Integer

' SDL_RWops * SDL_RWFromFile(const char *file, const char *mode)
' Create a new SDL_RWops structure for reading from and/or writing to a named file. 
Private Extern SDL_RWFromFile(filewav As String, mode As String) As Pointer

' Mix_Chunk * Mix_LoadWAV_RW(SDL_RWops *src, int freesrc)
' Load a wave file or a music.
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)
' Play an audio chunk on a specific channel.
Private Extern Mix_PlayChannelTimed(channel As Integer, chunk As Pointer, loops As Integer, ticks As Integer) As Integer

' int Mix_Playing(int channel)
' Check the status of a specific channel.
Private Extern Mix_Playing(channel As Integer) As Integer

' void Mix_FreeChunk(Mix_Chunk *chunk)
' Free an audio chunk previously loaded.
Private Extern Mix_FreeChunk(chunk As Pointer)

' void Mix_CloseAudio(void)
' Close the mixer, halting all playing audio.
Private Extern Mix_CloseAudio()


Public Sub Main()

 Dim err, canale As Integer
 Dim canali As String
 Dim wave As Pointer
' Vengono impostati i valori predefiniti iniziali della frequenza, del formato e del numero dei canali del file WAV caricato.
' Volendo adattare tali valori alle reali caratteristiche del file WAV caricato, bisognerà variare i valori iniziali delle rispettive variabili:
 Dim audio_rate As Integer = MIX_DEFAULT_FREQUENCY
 Dim audio_format As Short = MIX_DEFAULT_FORMAT
 Dim audio_channels As Integer = 2

' Inizializza la libreria SDL 2:
   err = SDL_Init(SDL_INIT_AUDIO)
   If err < 0 Then Error.Raise("Impossibile inizializzare la libreria SDL2: " & SDL_GetError())
   
' Apre il dispositivo audio:
   If Mix_OpenAudio(audio_rate, audio_format, audio_channels, 4096) < 0 Then
     Error.Raise("Impossibile aprire il dispositivo audio: " & SDL_GetError())
   Else
     Mix_QuerySpec(VarPtr(audio_rate), VarPtr(audio_format), VarPtr(audio_channels))
   Endif
   If audio_channels > 2 Then
     canali = "surround"
   Else
     If audio_channels > 1 Then
       canali = "stereo"
     Else
       canali = "mono"
     Endif
   Endif
   Print "Audio aperto a "; audio_rate; " Hz, "; audio_format And 255; " bit, "; canali
   
' Carica il file WAV:
   wave = Mix_LoadWAV_RW(SDL_RWFromFile("/home/ploppo/Scrivania/test_stereo_44100Hz_16bit_PCM.wav", "rb"), 1)
   If IsNull(wave) Then Error.Raise("Impossibile caricare il file WAV: " & SDL_GetError())
   
   canale = Mix_PlayChannelTimed(-1, wave, 0, 0)
   If canale = -1 Then Error.Raise("Impossibile eseguire il file WAV: " & SDL_GetError())
   
   While Mix_Playing(canale) <> 0
     Wait 0.01
   Wend
   
   
' Va in chiusura:
   Mix_FreeChunk(wave)
   Mix_CloseAudio()
   SDL_Quit()

End


Eseguire due o più file audio WAV

PAGINA IN COSTRUZIONE !



Riferimenti