Differenze tra le versioni di "La gestione dei file MIDI mediante le funzioni esterne del API di Fluidsynth"
Da Gambas-it.org - Wikipedia.
Riga 11: | Riga 11: | ||
Private player As Pointer | Private player As Pointer | ||
Private fileMidi As String | Private fileMidi As String | ||
+ | Private Const ALL_SOUND_OFF As Integer = 120 | ||
+ | Private Const ALL_NOTES_OFF As Integer = 123 | ||
+ | |||
Library "libfluidsynth:1.5.2" | Library "libfluidsynth:1.5.2" | ||
− | <FONT color=gray>' ''fluid_settings_t* new_fluid_settings(void) | + | <FONT color=gray>' ''fluid_settings_t* new_fluid_settings(void)'' |
+ | ' ''Create a new settings object. ''</font> | ||
Private Extern new_fluid_settings() As Pointer | Private Extern new_fluid_settings() As Pointer | ||
− | <FONT color=gray>' ''fluid_synth_t * new_fluid_synth (fluid_settings_t *settings) | + | <FONT color=gray>' ''fluid_synth_t * new_fluid_synth (fluid_settings_t *settings)'' |
+ | ' ''Create new FluidSynth instance.''</font> | ||
Private Extern new_fluid_synth(settP As Pointer) As Pointer | Private Extern new_fluid_synth(settP As Pointer) As Pointer | ||
− | <FONT color=gray>' ''fluid_player_t* new_fluid_player(fluid_synth_t * synth) | + | <FONT color=gray>' ''fluid_player_t* new_fluid_player(fluid_synth_t * synth)'' |
+ | ' ''Create a new MIDI player.''</font> | ||
Private Extern new_fluid_player(synth As Pointer) As Pointer | Private Extern new_fluid_player(synth As Pointer) As Pointer | ||
− | <FONT color=gray>' ''int | + | <FONT color=gray>' ''int fluid_settings_setint(fluid_settings_t *settings, const char *name, int val)'' |
+ | ' ''Set an integer value for a setting.''</font> | ||
Private Extern fluid_settings_setint(settP As Pointer, name As String, valI As Integer) As Integer | 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) | + | <FONT color=gray>' ''fluid_audio_driver_t* new_fluid_audio_driver(fluid_settings_t * settings, fluid_synth_t * synth)'' |
+ | ' ''Create a new audio driver.''</font> | ||
Private Extern new_fluid_audio_driver(settings As Pointer, synth As Pointer) As Pointer | Private Extern new_fluid_audio_driver(settings As Pointer, synth As Pointer) As Pointer | ||
− | <FONT color=gray>' ''int fluid_is_soundfont(const char * filename) | + | <FONT color=gray>' ''int fluid_is_soundfont(const char * filename)'' |
− | Private Extern fluid_is_soundfont( | + | ' ''Check if a file is a SoundFont file.''</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) | + | <FONT color=gray>' ''int fluid_synth_sfload(fluid_synth_t * synth, const char * filename, nt reset_presets)'' |
− | Private Extern fluid_synth_sfload(synth As Pointer, | + | ' ''Load a SoundFont file (filename is interpreted by SoundFont loaders).''</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) | + | <FONT color=gray>' ''int fluid_is_midifile(const char * filename)'' |
− | Private Extern fluid_is_midifile( | + | ' ''Check if a file is a MIDI file.''</font> |
+ | Private Extern fluid_is_midifile(filename As String) As Integer | ||
− | <FONT color=gray>' ''int fluid_player_add(fluid_player_t * player, Const char * midifile) | + | <FONT color=gray>' ''int fluid_player_add(fluid_player_t * player, Const char * midifile) |
+ | ' ''Add a MIDI file to a player queue.''</font> | ||
Private Extern fluid_player_add(player As Pointer, $fileMidi As String) As Integer | Private Extern fluid_player_add(player As Pointer, $fileMidi As String) As Integer | ||
− | <FONT color=gray>' ''int fluid_player_play(fluid_player_t * player) | + | <FONT color=gray>' ''int fluid_player_play(fluid_player_t * player)'' |
+ | ' ''Activates play mode for a MIDI player if not already playing.''</font> | ||
Private Extern fluid_player_play(player As Pointer) As Integer | Private Extern fluid_player_play(player As Pointer) As Integer | ||
− | <FONT color=gray>' ''int fluid_player_stop(fluid_player_t * player) | + | <FONT color=gray>' ''int fluid_player_stop(fluid_player_t * player)'' |
+ | ' ''Stops a MIDI player.''</font> | ||
Private Extern fluid_player_stop(player As Pointer) As Integer | Private Extern fluid_player_stop(player As Pointer) As Integer | ||
− | <FONT color=gray>' ''int fluid_player_get_status(fluid_player_t * player) | + | <FONT color=gray>' ''int fluid_player_get_status(fluid_player_t * player)'' |
+ | ' ''Get MIDI player status.''</font> | ||
Private Extern fluid_player_get_status(player As Pointer) As Integer | Private Extern fluid_player_get_status(player As Pointer) As Integer | ||
− | <FONT color=gray>' ''int | + | <FONT color=gray>' ''int fluid_synth_cc (fluid_synth_t *synth, int chan, int ctrl, int val)'' |
− | + | ' ''Send a MIDI controller event on a MIDI channel.''</font> | |
− | + | Private Extern fluid_synth_cc(synth As Pointer, chan As Integer, ctrl As integer, ival As Integer) As Integer | |
− | + | ||
− | Private Extern | + | <FONT color=gray>' ''void delete_fluid_audio_driver(fluid_audio_driver_t * driver)'' |
− | + | ' ''Deletes an audio driver instance.''</font> | |
− | <FONT color=gray>' ''void delete_fluid_audio_driver(fluid_audio_driver_t * driver) | ||
Private Extern delete_fluid_audio_driver(driver As Pointer) | Private Extern delete_fluid_audio_driver(driver As Pointer) | ||
− | <FONT color=gray>' ''int delete_fluid_player(fluid_player_t * player) | + | <FONT color=gray>' ''int delete_fluid_player(fluid_player_t * player)'' |
+ | ' ''Delete a MIDI player instance.''</font> | ||
Private Extern delete_fluid_player(player As Pointer) As Integer | Private Extern delete_fluid_player(player As Pointer) As Integer | ||
− | <FONT color=gray>' ''int delete_fluid_synth(fluid_synth_t * synth) | + | <FONT color=gray>' ''int delete_fluid_synth(fluid_synth_t * synth)'' |
+ | ' ''Delete a FluidSynth instance.''</font> | ||
Private Extern delete_fluid_synth(synth As Pointer) As Integer | Private Extern delete_fluid_synth(synth As Pointer) As Integer | ||
− | <FONT color=gray>' ''void delete_fluid_settings(fluid_settings_t * settings) | + | <FONT color=gray>' ''void delete_fluid_settings(fluid_settings_t * settings)'' |
+ | ' ''Delete the provided settings object.''</font> | ||
Private Extern delete_fluid_settings(settings As Pointer) | Private Extern delete_fluid_settings(settings As Pointer) | ||
Riga 123: | Riga 140: | ||
Dim i As Integer | Dim i As Integer | ||
+ | Dim b As Byte | ||
<FONT color=gray>' ''Arresta l'esecuzione del file Midi:''</font> | <FONT color=gray>' ''Arresta l'esecuzione del file Midi:''</font> | ||
i = fluid_player_stop(player) | i = fluid_player_stop(player) | ||
If i < 0 Then Error.Raise("Errore alla funzione 'fluid_player_stop()' !") | If i < 0 Then Error.Raise("Errore alla funzione 'fluid_player_stop()' !") | ||
− | + | ||
− | i = | + | For b = 0 To 15 |
− | + | i = fluid_synth_cc(synth, b, ALL_NOTES_OFF, 0) | |
− | + | If i < 0 Then Error.Raise("Errore alla funzione 'fluid_synth_cc()' !") | |
− | + | i = fluid_synth_cc(synth, b, ALL_SOUND_OFF, 0) | |
− | + | If i < 0 Then Error.Raise("Errore alla funzione 'fluid_synth_cc()' !") | |
− | + | Next | |
+ | |||
'''End''' | '''End''' | ||
Versione delle 09:46, 9 giu 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 Private Const ALL_SOUND_OFF As Integer = 120 Private Const ALL_NOTES_OFF As Integer = 123 Library "libfluidsynth:1.5.2" ' fluid_settings_t* new_fluid_settings(void) ' Create a new settings object. Private Extern new_fluid_settings() As Pointer ' fluid_synth_t * new_fluid_synth (fluid_settings_t *settings) ' Create new FluidSynth instance. Private Extern new_fluid_synth(settP As Pointer) As Pointer ' fluid_player_t* new_fluid_player(fluid_synth_t * synth) ' Create a new MIDI player. Private Extern new_fluid_player(synth As Pointer) As Pointer ' int fluid_settings_setint(fluid_settings_t *settings, const char *name, int val) ' Set an integer value for a setting. 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) ' Create a new audio driver. Private Extern new_fluid_audio_driver(settings As Pointer, synth As Pointer) As Pointer ' int fluid_is_soundfont(const char * filename) ' Check if a file is a SoundFont file. Private Extern fluid_is_soundfont(filename As String) As Integer ' int fluid_synth_sfload(fluid_synth_t * synth, const char * filename, nt reset_presets) ' Load a SoundFont file (filename is interpreted by SoundFont loaders). Private Extern fluid_synth_sfload(synth As Pointer, filename As String, reset_presets As Integer) As Integer ' int fluid_is_midifile(const char * filename) ' Check if a file is a MIDI file. Private Extern fluid_is_midifile(filename As String) As Integer ' int fluid_player_add(fluid_player_t * player, Const char * midifile) ' Add a MIDI file to a player queue. Private Extern fluid_player_add(player As Pointer, $fileMidi As String) As Integer ' int fluid_player_play(fluid_player_t * player) ' Activates play mode for a MIDI player if not already playing. Private Extern fluid_player_play(player As Pointer) As Integer ' int fluid_player_stop(fluid_player_t * player) ' Stops a MIDI player. Private Extern fluid_player_stop(player As Pointer) As Integer ' int fluid_player_get_status(fluid_player_t * player) ' Get MIDI player status. Private Extern fluid_player_get_status(player As Pointer) As Integer ' int fluid_synth_cc (fluid_synth_t *synth, int chan, int ctrl, int val) ' Send a MIDI controller event on a MIDI channel. Private Extern fluid_synth_cc(synth As Pointer, chan As Integer, ctrl As integer, ival As Integer) As Integer ' void delete_fluid_audio_driver(fluid_audio_driver_t * driver) ' Deletes an audio driver instance. Private Extern delete_fluid_audio_driver(driver As Pointer) ' int delete_fluid_player(fluid_player_t * player) ' Delete a MIDI player instance. Private Extern delete_fluid_player(player As Pointer) As Integer ' int delete_fluid_synth(fluid_synth_t * synth) ' Delete a FluidSynth instance. Private Extern delete_fluid_synth(synth As Pointer) As Integer ' void delete_fluid_settings(fluid_settings_t * settings) ' Delete the provided settings object. 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 Dim b As Byte ' Arresta l'esecuzione del file Midi: i = fluid_player_stop(player) If i < 0 Then Error.Raise("Errore alla funzione 'fluid_player_stop()' !") For b = 0 To 15 i = fluid_synth_cc(synth, b, ALL_NOTES_OFF, 0) If i < 0 Then Error.Raise("Errore alla funzione 'fluid_synth_cc()' !") i = fluid_synth_cc(synth, b, ALL_SOUND_OFF, 0) If i < 0 Then Error.Raise("Errore alla funzione 'fluid_synth_cc()' !") Next 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