La gestione mediante le funzioni esterne del API di SDL 2

Da Gambas-it.org - Wikipedia.

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.so.0.2.0
  • libSDL2_mixer-2.0.so.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("/percorso/del/file.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

Di seguito mostreremo un codice esemplificativo, nel quale verranno eseguiti due file 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, cnl As Integer
 Dim canale As New Integer[]
 Dim canali As String
 Dim percorsoFile As String[] = ["/percorso/del/primo/file.wav", "/percorso/del/secondo/file.wav"]
 Dim wave As New 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
  
  With wave
' Carica il primo file WAV:
    .Add(Mix_LoadWAV_RW(SDL_RWFromFile(percorsoFile[0], "rb"), 1))
' Carica il secondo file WAV:
    .Add(Mix_LoadWAV_RW(SDL_RWFromFile(percorsoFile[1], "rb"), 1))
  End With

' Esegue contemporaneamente entrambi i file WAV caricati , ed intercetta il rispettivo canale sul quale ciascuno di essi viene eseguito.
' Passando il valore -1 al primo argomento della funzione, il campione audio sarà eseguito sul primo canale audio disponibile:
  canale.Add(Mix_PlayChannelTimed(-1, wave[0], 0, 0))
  If canale[0] = -1 Then Error.Raise("Impossibile eseguire il 1° file WAV: " & SDL_GetError())

  canale.Add(Mix_PlayChannelTimed(-1, wave[1], 0, 0))
  If canale[1] = -1 Then Error.Raise("Impossibile eseguire il 2° file WAV: " & SDL_GetError())

' Verifica quale file wav possiede la maggiore dimensione:
  If Stat(percorsoFile[0]).Size > Stat(percorsoFile[1]).Size Then cnl = 0

' Attende che sia terminato il file WAV di maggiore dimensione (e quindi presumibilmente quello con maggiore durata):
  While Mix_Playing(cnl) <> 0
    Wait 0.01
  Wend

' Libera la memoria precedentemente allocata per l'esecuzione dei due file wav:
  Mix_FreeChunk(wave[0])
  Mix_FreeChunk(wave[1])

' Chiude l'interfaccia audio SDL e SDL_mixer:
  Mix_CloseAudio()

  SDL_Quit()

End


Eseguire gli altri formati di file audio

Per poter eseguire i file audio di altro formato (ma compreso anche lo stesso formato WAV) |1|, bisognerà utilizzare la funzione Mix_PlayMusic() appartenente alla libreria libSDL2_mixer-2.0.so.0.0.0.
Detta funzione non consente di poter eseguire più file contemporaneamente.

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

' Mix_Music * Mix_LoadMUS(const char *file)
' Load a wave file or a music.
Private Extern Mix_LoadMUS(file As String) As Pointer

' int Mix_PlayMusic(Mix_Music *music, int loops)
' Play an audio chunk on a specific channel.
Private Extern Mix_PlayMusic(music As Pointer, loops As Integer) As Integer

' int Mix_PlayingMusic()
' Check the status of a specific channel.
Private Extern Mix_PlayingMusic() 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 As Integer
 Dim canali As String
 Dim music 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 audio 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 audio:
  music = Mix_LoadMUS("/percorso/del/file/audio")
  If IsNull(music) Then Error.Raise("Impossibile caricare il file audio: " & SDL_GetError())
  
' Esegue il file audio. Se il secondo argomento è posto a 0 il file sarà eseguito soltanto una volta.
' Se è posto a -1 il file sarà esguito all'infinito:
  err = Mix_PlayMusic(music, 0)
  If err < 0 Then Error.Raise("Impossibile eseguire il file audio: " & SDL_GetError())

  While Mix_PlayingMusic() <> 0
    Wait 0.01
  Wend

  
' Va in chiusura:
  Mix_FreeChunk(music)
  Mix_CloseAudio()
  SDL_Quit()

End



Note

[1] I formati eseguibili sono i seguenti:

  • .WAV (WAV/RIFF)
  • .VOC (Creative Labs' Voice format)
  • .MP3 (MPEG-1 Layer 3 support)
  • .MID (Musical Instrument Digital Interface)
  • .MOD (MOD file: .mod .xm .s3m .669 .it .med ed altri)
  • .OGG (Ogg file)
  • .SPX (Speex file)
  • .SHN (Shorten file)
  • .RAW (Raw sound data in any format)
  • .AU (Sun's Audio format)
  • .AIFF (Audio Interchange format)
  • .FLAC (Lossless audio compression)



Riferimenti