La gestione dei file MIDI mediante le funzioni esterne del API di SDL 2

Da Gambas-it.org - Wikipedia.

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


Per poter utilizzare le funzioni esterne di SDL2 in Gambas, si richiameranno anche separatamente per lo più le seguenti librerie esterne:

  • libSDL2-2.0.so.0.2800.5
  • libSDL2_mixer-2.0.so.0.600.3

Inoltre, per l'ascolto dei file Midi, si potrà - volendo - impostare un file banco di suoni .sf2 nell'apposita funzione Mix_SetSoundFonts().


Mostriamo un semplice esempio per eseguire un file Midi:

Library "libSDL2-2.0:0.2800.5"

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.600.3"

Private Const MIX_DEFAULT_FREQUENCY As Integer = 44100

' int Mix_SetSoundFonts(const char *paths)
' Set SoundFonts paths to use by supported MIDI backends.
Private Extern Mix_SetSoundFonts(paths As String) As Integer
 
' 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_VolumeMusic(int volume)
' Set the volume in the range of 0-128.
Private Extern Mix_VolumeMusic(volume As Integer) As Integer

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

' void Mix_FreeMusic(Mix_Music *music)
' Free an audio chunk previously loaded.
Private Extern Mix_FreeMusic(music 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
 Dim tempus As date
' Vengono impostati i valori predefiniti iniziali della frequenza, del formato e del numero dei canali dell'audio:
' E' possibile modificare tali valori:
 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())

' Volendo, si può impostare il banco di suoni per l'esecuzione del Midi:
 Mix_SetSoundFonts("/percorso/del/file/soundbank.sf2")
   
' 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 Midi:
 music = Mix_LoadMUS("/percorso/del/file.mid")
 If music == 0 Then Error.Raise("Impossibile caricare il file Midi: " & SDL_GetError())
  
' Imposta il volume di esecuzione (da 0 a 128):
 Mix_VolumeMusic(40)
  
' Esegue il file Midi. 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 Midi: " & SDL_GetError())
  
 tempus = Now
  
 While Mix_PlayingMusic() <> 0
' Mostra il tempo trascorso dall'inizio dell'esecuzione del file Midi:
   Write "\r" & Str(Time(0, 0, 0, DateDiff(tempus, Now, gb.Millisecond)))
   Wait 0.01
 Wend
  
' Va in chiusura:
 Mix_FreeMusic(music)
 Mix_CloseAudio()
 SDL_Quit()

End


Riferimenti