Пример #1
0
        public IMidiRenderer GetNewRenderer()
        {
            if (!FluidsynthInitialized)
            {
                InitializeFluidsynth();
            }

            var soundfontLoader = SoundFontLoader.NewDefaultSoundFontLoader(_settings);

            soundfontLoader.SetCallbacks(_soundfontLoaderCallbacks);

            var renderer = new MidiRenderer(_settings, soundfontLoader);

            // Since the last loaded soundfont takes priority, we load the fallback soundfont first.
            renderer.LoadSoundfont(FallbackSoundfont);

            if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
            {
                foreach (var filepath in LinuxSoundfonts)
                {
                    if (!File.Exists(filepath) || !SoundFont.IsSoundFont(filepath))
                    {
                        continue;
                    }

                    try
                    {
                        renderer.LoadSoundfont(filepath, true);
                    }
                    catch (Exception)
                    {
                        continue;
                    }

                    break;
                }
            }
            else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
            {
                if (File.Exists(OSXSoundfont) && SoundFont.IsSoundFont(OSXSoundfont))
                {
                    renderer.LoadSoundfont(OSXSoundfont, true);
                }
            }
            else if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
            {
                if (File.Exists(WindowsSoundfont) && SoundFont.IsSoundFont(WindowsSoundfont))
                {
                    renderer.LoadSoundfont(WindowsSoundfont, true);
                }
            }

            lock (_renderers)
                _renderers.Add(renderer);

            return(renderer);
        }
Пример #2
0
 public static void Main(string[] args)
 {
     using (var settings = new Settings())
     {
         // Change this if you don't have pulseaudio or want to change to anything else.
         if (Environment.OSVersion.Platform == PlatformID.Unix)
         {
             settings[ConfigurationKeys.AudioDriver].StringValue = "pulseaudio";
         }
         settings[ConfigurationKeys.SynthAudioChannels].IntValue = 2;
         using (var syn = new Synth(settings))
         {
             foreach (var arg in args)
             {
                 if (SoundFont.IsSoundFont(arg))
                 {
                     syn.LoadSoundFont(arg, true);
                 }
             }
             if (syn.FontCount == 0)
             {
                 syn.LoadSoundFont("/usr/share/sounds/sf2/FluidR3_GM.sf2", true);
             }
             for (int i = 0; i < 16; i++)
             {
                 syn.SoundFontSelect(i, 0);
             }
             var files = args.Where(SoundFont.IsMidiFile);
             if (files.Any())
             {
                 foreach (var arg in files)
                 {
                     using (var player = new Player(syn))
                     {
                         using (var adriver = new AudioDriver(syn.Settings, syn))
                         {
                             player.Add(arg);
                             player.Play();
                             player.Join();
                         }
                     }
                 }
             }
             else
             {
                 using (var adriver = new AudioDriver(syn.Settings, syn))
                 {
                     syn.ProgramChange(0, 1);
                     syn.NoteOn(0, 60, 120);
                     Thread.Sleep(5000);
                     syn.NoteOff(0, 60);
                 }
             }
         }
     }
 }
Пример #3
0
        public IMidiRenderer GetNewRenderer()
        {
            if (!FluidsynthInitialized)
            {
                InitializeFluidsynth();

                if (!FluidsynthInitialized) // init failed
                {
                    return(null);
                }
            }

            var soundfontLoader = SoundFontLoader.NewDefaultSoundFontLoader(_settings);

            // Just making double sure these don't get GC'd.
            // They shouldn't, MidiRenderer keeps a ref, but making sure...
            var handle = GCHandle.Alloc(soundfontLoader);

            try
            {
                soundfontLoader.SetCallbacks(_soundfontLoaderCallbacks);

                var renderer = new MidiRenderer(_settings, soundfontLoader);

                // Since the last loaded soundfont takes priority, we load the fallback soundfont first.
                renderer.LoadSoundfont(FallbackSoundfont);

                if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
                {
                    foreach (var filepath in LinuxSoundfonts)
                    {
                        if (!File.Exists(filepath) || !SoundFont.IsSoundFont(filepath))
                        {
                            continue;
                        }

                        try
                        {
                            renderer.LoadSoundfont(filepath, true);
                        }
                        catch (Exception)
                        {
                            continue;
                        }

                        break;
                    }
                }
                else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
                {
                    if (File.Exists(OsxSoundfont) && SoundFont.IsSoundFont(OsxSoundfont))
                    {
                        renderer.LoadSoundfont(OsxSoundfont, true);
                    }
                }
                else if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
                {
                    if (File.Exists(WindowsSoundfont) && SoundFont.IsSoundFont(WindowsSoundfont))
                    {
                        renderer.LoadSoundfont(WindowsSoundfont, true);
                    }
                }

                lock (_renderers)
                    _renderers.Add(renderer);

                return(renderer);
            }
            finally
            {
                handle.Free();
            }
        }
Пример #4
0
 public bool IsSoundfontFile(string filename)
 {
     return(SoundFont.IsSoundFont(filename));
 }