This project is read-only.

Samples

The FluidSynth Wrapper can be used by any .NET language, but the samples presented here are in VB.NET and C#.

Creating the Synthesizer

The first step in using FluidSynth is to create the Synthesizer object. In order to do that, you must first create a Settings object to hold some settings used by the synthesizer. If you want the synthesizer to play out loud (which is normally the case), you finally must connect a new AudioDriver class to the Synthesizer.

In VB.Net:
Dim fluidSettings as New FluidSynthWrapper.Settings
' Set some settings for use with the audio driver, in this case, to use Windows DirectSound for output
fluidSettings.AudioDriver = "dsound"
' Set some settings for the synthesizer
fluidSettings.SynthAudioChannels = 256 ' This sets the number of MIDI channels available for use (defaulted to 16)
fluidSettings.SynthSampleRate = 44100 ' The audio sample rate sent to the audio driver
synth = New FluidSynthWrapper.Synthesizer(fluidSettings) ' Create the synthesizer with the settings
audioDrv = New FluidSynthWrapper.AudioDriver(fluidSettings, synth) ' Create the audio driver witht the settings and connected to the synthesizer

In C#:
FluidSynthWrapper.Settings fluidSettings;
// Set some settings for use with the audio driver, in this case, to use Windows DirectSound for output
fluidSettings.AudioDriver = "dsound";
// Set some settings for the synthesizer
fluidSettings.SynthAudioChannels = 256; // This sets the number of MIDI channels available for use (defaulted to 16)
fluidSettings.SynthSampleRate = 44100; // The audio sample rate sent to the audio driver
synth = new FluidSynthWrapper.Synthesizer(fluidSettings); // Create the synthesizer with the settings
audioDrv = new FluidSynthWrapper.AudioDriver(fluidSettings, synth); // Create the audio driver witht the settings and connected to the synthesizer

Loading SoundFonts

FluidSynth uses SoundFonts to render commands into audio. You can find hundreds of free SoundFont files on the internet to start with. If you don't load any SoundFont files before sending commands to the synthesizer, they won't be audible. Files can be loaded or unloaded at any time and will take effect on all commands processed after the loading/unloading.

In VB.Net:
Dim path as String = "C:\Sample\MySoundFont.sf2"
If synth.IsSoundFont(path) Then
  Dim sfontId as Integer = synth.SFontLoad(path)
  ' Play the music
  synth.SFontUnload(path)
End If

In C#:
string path = "C:\\Sample\\MySoundFont.sf2";
if (synth.IsSoundFont(path))
{
  int sfontId = synth.SFontLoad(path);
  // Play the music
  synth.SFontUnload(path);
}

Sending Commands to the Synthesizer

Obviously the synthesizer wouldn't be very useful if you couldn't tell it to play notes. Since the synthesizer uses its own thread, you can send commands from any thread and they will safely be processed by FluidSynth.

In VB.Net:
' Set the first channel to the grand piano instrument (which is the default anyway)
synth.ProgChange(0, 0)
' Play middle C
synth.NoteOn(0, 60, 127)
' Hold for one second
System.Threading.Thread.Sleep(1000)
' Stop playing the note
synth.NoteOff(0, 60)

In C#:
// Set the first channel to the grand piano instrument (which is the default anyway)
synth.ProgChange(0, 0);
// Play middle C
synth.NoteOn(0, 60, 127);
// Hold for one second
System.Threading.Thread.Sleep(1000);
// Stop playing the note
synth.NoteOff(0, 60);

Last edited Jun 17, 2008 at 9:02 PM by zhuman, version 4

Comments

No comments yet.