Differenze tra le versioni di "La gestione dei file MIDI mediante le funzioni esterne del API di Fluidsynth"

Da Gambas-it.org - Wikipedia.
Riga 1: Riga 1:
* [[Introduzione a FluidSynth]]
+
Mostriamo di seguito il codice per di un semplice applicativo per la riproduzione di file Midi.
  
* [[Semplice applicativo per la riproduzione dei file Midi con le funzioni esterne di FluidSynth]]
+
Bisogna avere installato ''Jackd''. Una volta lanciato l'applicativo, aprire ''Jackd'' e nella scheda "''Audio''" connettere il dispositivo "''Fluidsynth''", presente nella colonna "''Clients leggibili/Porte d'uscita''", al dispositivo "''system''" presente nella colonna "''Clients scrivibili/Porte d'entrata''".
 +
 
 +
Si dovrà, inoltre, individuare un file ''soundfont bank'' con estensione <FONT color=#B22222>.sf2</font>, da far caricare successivamente dalla funzione esterna "''fluid_synth_sfload''" per poter ottenere i suoni.
 +
 
 +
 
 +
Private settings As Pointer
 +
Private synth As Pointer
 +
Private adriver As Pointer
 +
Private player As Pointer
 +
Private fileMidi As String
 +
 +
Library "libfluidsynth:1.5.2"
 +
 +
<FONT color=gray>' ''fluid_settings_t* new_fluid_settings(void)  ---> Crea un nuovo oggetto Settings.''</font>
 +
Private Extern new_fluid_settings() As Pointer
 +
 +
<FONT color=gray>' ''fluid_synth_t * new_fluid_synth (fluid_settings_t *settings)  ---> Crea una nuova istanza FluidSynth.''</font>
 +
Private Extern new_fluid_synth(settP As Pointer) As Pointer
 +
 +
<FONT color=gray>' ''fluid_player_t* new_fluid_player(fluid_synth_t * synth)  ---> Crea un nuovo lettore MIDI.''</font>
 +
Private Extern new_fluid_player(synth As Pointer) As Pointer
 +
 +
<FONT color=gray>' ''int fluid_settings_setintfluid_settings_t *settings, const char *name, int val)  ---> Connette automaticamente il lettore Midi a Jack.''</font>
 +
Private Extern fluid_settings_setint(settP As Pointer, name As String, valI As Integer) As Integer
 +
 +
<FONT color=gray>' ''fluid_audio_driver_t* new_fluid_audio_driver(fluid_settings_t * settings, fluid_synth_t * synth)    ---> Crea un nuovo driver audio.''</font>
 +
Private Extern new_fluid_audio_driver(settings As Pointer, synth As Pointer) As Pointer
 +
 +
<FONT color=gray>' ''int fluid_is_soundfont(const char * filename)  --->  Controlla se il file caricato è effettivamente un file SoundFont.''</font>
 +
Private Extern fluid_is_soundfont($filename As String) As Integer
 +
 +
<FONT color=gray>' ''int fluid_synth_sfload(fluid_synth_t * synth, const char * filename, nt reset_presets)  ---> Carica file SoundFont.''</font>
 +
Private Extern fluid_synth_sfload(synth As Pointer, $filename As String, reset_presets As Integer) As Integer
 +
 +
<FONT color=gray>' ''int fluid_is_midifile(const char * filename)  ---> Controlla se il file caricato è un file MIDI.''</font>
 +
Private Extern fluid_is_midifile($fileMidi As String) As Integer
 +
 +
<FONT color=gray>' ''int fluid_player_add(fluid_player_t * player, Const char * midifile)    ---> Aggiunge un file MIDI alla coda del lettore.''</font>
 +
Private Extern fluid_player_add(player As Pointer, $fileMidi As String) As Integer
 +
 +
<FONT color=gray>' ''int fluid_player_play(fluid_player_t * player)    ---> Attiva la modalità esegui (play) al lettore MIDI, se non sta già eseguendo un Midi.''</font>
 +
Private Extern fluid_player_play(player As Pointer) As Integer
 +
 +
<FONT color=gray>' ''int fluid_player_stop(fluid_player_t * player)    ---> Attiva la modalità arresta (stop) al lettore MIDI.''</font>
 +
Private Extern fluid_player_stop(player As Pointer) As Integer
 +
 +
<FONT color=gray>' ''int fluid_player_get_status(fluid_player_t * player)    ---> Ottiene lo status del lettore MIDI.''</font>
 +
Private Extern fluid_player_get_status(player As Pointer) As Integer
 +
 +
<FONT color=gray>' ''int fluid_player_all_notes_off(fluid_synth_t * synth, int chan)  ---> Invia al lettore MIDI un comando di "All Notes Off".''</font>
 +
Private Extern fluid_synth_all_notes_off(synth As Pointer, valOff As Integer) As Integer
 +
 +
<FONT color=gray>' ''int fluid_synth_all_sounds_off(fluid_synth_t * synth, int chan)    ---> Invia al lettore MIDI un comando di "All Sounds Off".''</font>
 +
Private Extern fluid_synth_all_sounds_off(synth As Pointer, valOff As Integer) As Integer
 +
 +
<FONT color=gray>' ''void delete_fluid_audio_driver(fluid_audio_driver_t * driver)  ---> Elimina un'istanza di driver audio.''</font>
 +
Private Extern delete_fluid_audio_driver(driver As Pointer)
 +
 +
<FONT color=gray>' ''int delete_fluid_player(fluid_player_t * player)  ---> Elimna un'istanza di lettore MIDI.''</font>
 +
Private Extern delete_fluid_player(player As Pointer) As Integer
 +
 +
<FONT color=gray>' ''int delete_fluid_synth(fluid_synth_t * synth)  ---> Elimna un'istanza FluidSynth.''</font>
 +
Private Extern delete_fluid_synth(synth As Pointer) As Integer
 +
 +
<FONT color=gray>' ''void delete_fluid_settings(fluid_settings_t * settings)  ---> Elimina l'oggetto Settings.''</font>
 +
Private Extern delete_fluid_settings(settings As Pointer)
 +
 +
 +
'''Public''' Sub Form_Open()
 +
 +
  Dim i As Integer
 +
  Dim filesbk As String
 +
 +
  filesbk = "''/percorso/del/file/soundfont.sf2''"
 +
 
 +
  settings = new_fluid_settings()
 +
 
 +
  synth = new_fluid_synth(settings)
 +
 
 +
  player = new_fluid_player(synth)
 +
 
 +
  i = fluid_settings_setint(settings, "audio.jack.autoconnect", 1)
 +
  If i = 0 Then Error.Raise("Errore alla funzione 'fluid_settings_setint()' !")
 +
 
 +
  adriver = new_fluid_audio_driver(settings, synth)
 +
  If IsNull(adriver) Then Error.Raise("Impossibile creare un driver audio !")
 +
 
 +
  i = fluid_is_soundfont(filesbk)
 +
  If CBool(i) = False then Error.Raise("Il file non è un soundfont bank !")
 +
   
 +
  i = fluid_synth_sfload(synth, filesbk, 1)
 +
  If i < 0 Then Error.Raise("Errore alla funzione 'fluid_synth_sfload()' !")
 +
 
 +
'''End'''
 +
 +
 +
'''Public''' Sub Button1_Click()
 +
 +
  Dim i As Integer
 +
   
 +
  i = fluid_player_add(player, fileMidi)
 +
  If i < 0 Then Error.Raise("Errore alla funzione 'fluid_player_add()' !")
 +
 
 +
<FONT color=gray>' ''Avvia l'esecuzione del file Midi:''</font>
 +
  i = fluid_player_play(player)
 +
  If i < 0 Then Error.Raise("Errore alla funzione 'fluid_player_play()' !")
 +
 
 +
  Do
 +
    Wait 0.01
 +
  Loop Until fluid_player_get_status(player) <> 1
 +
 
 +
  i = delete_fluid_player(player)
 +
  If i < 0 Then Error.Raise("Errore alla funzione 'delete_fluid_player()' !")
 +
 +
  player = new_fluid_player(synth)
 +
 
 +
'''End'''
 +
 +
 +
'''Public''' Sub Button2_Click()
 +
 +
  Dim i As Integer
 +
 +
<FONT color=gray>' ''Arresta l'esecuzione del file Midi:''</font>
 +
  i = fluid_player_stop(player)
 +
  If i < 0 Then Error.Raise("Errore alla funzione 'fluid_player_stop()' !")
 +
 +
  i = fluid_synth_all_notes_off(synth, -1)
 +
  If i < 0 Then Error.Raise("Errore alla funzione 'fluid_synth_all_notes_off()' !")
 +
 +
  i = fluid_synth_all_sounds_off(synth, -1)
 +
  If i < 0 Then Error.Raise("Errore alla funzione 'fluid_synth_all_sounds_off()' !")
 +
 +
'''End'''
 +
 +
 +
'''Public''' Sub Form_Close()
 +
 +
  delete_fluid_audio_driver(adriver)
 +
 +
  delete_fluid_player(player)
 +
 
 +
  delete_fluid_synth(synth)
 +
 
 +
  delete_fluid_settings(settings)
 +
 
 +
'''End'''
 +
 +
 +
'''Public''' Sub MenuApri_Click()
 +
 +
  Dim i As Integer
 +
 +
  With Dialog
 +
    .Title = "Apri file Midi..."
 +
    .Filter = ["*.mid", "File Midi"]
 +
    If .OpenFile() Then Return
 +
    fileMidi = .Path
 +
  End With
 +
 +
  i = fluid_is_midifile(fileMidi)
 +
  If CBool(i) = False Then Error.Raise("Il file caricato non è un file Midi standard !")
 +
 
 +
'''End'''

Versione delle 18:58, 8 gen 2016

Mostriamo di seguito il codice per di un semplice applicativo per la riproduzione di file Midi.

Bisogna avere installato Jackd. Una volta lanciato l'applicativo, aprire Jackd e nella scheda "Audio" connettere il dispositivo "Fluidsynth", presente nella colonna "Clients leggibili/Porte d'uscita", al dispositivo "system" presente nella colonna "Clients scrivibili/Porte d'entrata".

Si dovrà, inoltre, individuare un file soundfont bank con estensione .sf2, da far caricare successivamente dalla funzione esterna "fluid_synth_sfload" per poter ottenere i suoni.


Private settings As Pointer
Private synth As Pointer
Private adriver As Pointer
Private player As Pointer
Private fileMidi As String

Library "libfluidsynth:1.5.2"

' fluid_settings_t* new_fluid_settings(void)   ---> Crea un nuovo oggetto Settings.
Private Extern new_fluid_settings() As Pointer

' fluid_synth_t * new_fluid_synth (fluid_settings_t *settings)   ---> Crea una nuova istanza FluidSynth.
Private Extern new_fluid_synth(settP As Pointer) As Pointer

' fluid_player_t* new_fluid_player(fluid_synth_t * synth)   ---> Crea un nuovo lettore MIDI.
Private Extern new_fluid_player(synth As Pointer) As Pointer

' int fluid_settings_setintfluid_settings_t *settings, const char *name, int val)   ---> Connette automaticamente il lettore Midi a Jack.
Private Extern fluid_settings_setint(settP As Pointer, name As String, valI As Integer) As Integer

' fluid_audio_driver_t* new_fluid_audio_driver(fluid_settings_t * settings, fluid_synth_t * synth)    ---> Crea un nuovo driver audio.
Private Extern new_fluid_audio_driver(settings As Pointer, synth As Pointer) As Pointer 

' int fluid_is_soundfont(const char * filename)   --->  Controlla se il file caricato è effettivamente un file SoundFont.
Private Extern fluid_is_soundfont($filename As String) As Integer

' int fluid_synth_sfload(fluid_synth_t * synth, const char * filename, nt reset_presets)   ---> Carica file SoundFont.
Private Extern fluid_synth_sfload(synth As Pointer, $filename As String, reset_presets As Integer) As Integer

' int fluid_is_midifile(const char * filename)   ---> Controlla se il file caricato è un file MIDI.
Private Extern fluid_is_midifile($fileMidi As String) As Integer

' int fluid_player_add(fluid_player_t * player, Const char * midifile)    ---> Aggiunge un file MIDI alla coda del lettore.
Private Extern fluid_player_add(player As Pointer, $fileMidi As String) As Integer

' int fluid_player_play(fluid_player_t * player)    ---> Attiva la modalità esegui (play) al lettore MIDI, se non sta già eseguendo un Midi.
Private Extern fluid_player_play(player As Pointer) As Integer

' int fluid_player_stop(fluid_player_t * player)    ---> Attiva la modalità arresta (stop) al lettore MIDI.
Private Extern fluid_player_stop(player As Pointer) As Integer

' int fluid_player_get_status(fluid_player_t * player)    ---> Ottiene lo status del lettore MIDI.
Private Extern fluid_player_get_status(player As Pointer) As Integer

' int fluid_player_all_notes_off(fluid_synth_t * synth, int chan)   ---> Invia al lettore MIDI un comando di "All Notes Off".
Private Extern fluid_synth_all_notes_off(synth As Pointer, valOff As Integer) As Integer

' int fluid_synth_all_sounds_off(fluid_synth_t * synth, int chan)    ---> Invia al lettore MIDI un comando di "All Sounds Off".
Private Extern fluid_synth_all_sounds_off(synth As Pointer, valOff As Integer) As Integer

' void delete_fluid_audio_driver(fluid_audio_driver_t * driver)   ---> Elimina un'istanza di driver audio.
Private Extern delete_fluid_audio_driver(driver As Pointer)

' int delete_fluid_player(fluid_player_t * player)   ---> Elimna un'istanza di lettore MIDI.
Private Extern delete_fluid_player(player As Pointer) As Integer

' int delete_fluid_synth(fluid_synth_t * synth)   ---> Elimna un'istanza FluidSynth.
Private Extern delete_fluid_synth(synth As Pointer) As Integer

' void delete_fluid_settings(fluid_settings_t * settings)   ---> Elimina l'oggetto Settings.
Private Extern delete_fluid_settings(settings As Pointer)


Public Sub Form_Open()

 Dim i As Integer
 Dim filesbk As String

  filesbk = "/percorso/del/file/soundfont.sf2"
  
  settings = new_fluid_settings()
  
  synth = new_fluid_synth(settings)
  
  player = new_fluid_player(synth)
  
  i = fluid_settings_setint(settings, "audio.jack.autoconnect", 1)
  If i = 0 Then Error.Raise("Errore alla funzione 'fluid_settings_setint()' !")
  
  adriver = new_fluid_audio_driver(settings, synth)
  If IsNull(adriver) Then Error.Raise("Impossibile creare un driver audio !")
  
  i = fluid_is_soundfont(filesbk)
  If CBool(i) = False then Error.Raise("Il file non è un soundfont bank !")
    
  i = fluid_synth_sfload(synth, filesbk, 1)
  If i < 0 Then Error.Raise("Errore alla funzione 'fluid_synth_sfload()' !")
  
End


Public Sub Button1_Click()

 Dim i As Integer
   
  i = fluid_player_add(player, fileMidi)
 If i < 0 Then Error.Raise("Errore alla funzione 'fluid_player_add()' !")
  
' Avvia l'esecuzione del file Midi:
  i = fluid_player_play(player)
  If i < 0 Then Error.Raise("Errore alla funzione 'fluid_player_play()' !")
 
  Do
    Wait 0.01
  Loop Until fluid_player_get_status(player) <> 1
 
  i = delete_fluid_player(player)
  If i < 0 Then Error.Raise("Errore alla funzione 'delete_fluid_player()' !")

  player = new_fluid_player(synth)
  
End


Public Sub Button2_Click()

 Dim i As Integer

' Arresta l'esecuzione del file Midi:
  i = fluid_player_stop(player)
  If i < 0 Then Error.Raise("Errore alla funzione 'fluid_player_stop()' !")

  i = fluid_synth_all_notes_off(synth, -1)
  If i < 0 Then Error.Raise("Errore alla funzione 'fluid_synth_all_notes_off()' !")

  i = fluid_synth_all_sounds_off(synth, -1)
  If i < 0 Then Error.Raise("Errore alla funzione 'fluid_synth_all_sounds_off()' !")

End


Public Sub Form_Close()

  delete_fluid_audio_driver(adriver)

  delete_fluid_player(player)
 
  delete_fluid_synth(synth)
 
  delete_fluid_settings(settings)
 
End


Public Sub MenuApri_Click()

 Dim i As Integer

  With Dialog
    .Title = "Apri file Midi..."
    .Filter = ["*.mid", "File Midi"]
    If .OpenFile() Then Return
    fileMidi = .Path
  End With

  i = fluid_is_midifile(fileMidi)
  If CBool(i) = False Then Error.Raise("Il file caricato non è un file Midi standard !")
 
End