Best way to play MIDI sounds using C#

后端 未结 11 672
后悔当初
后悔当初 2020-12-09 10:03

I\'m trying to rebuild an old metronome application that was originally written using MFC in C++ to be written in .NET using C#. One o

11条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-09 10:29

    I think it's much better to use some library that which has advanced features for MIDI data playback instead of implementing it by your own. For example, with DryWetMIDI (I'm the author of it) to play MIDI file via default synthesizer (Microsoft GS Wavetable Synth):

    using Melanchall.DryWetMidi.Devices;
    using Melanchall.DryWetMidi.Core;
    
    // ...
    
    var midiFile = MidiFile.Read("Greatest song ever.mid");
    
    using (var outputDevice = OutputDevice.GetByName("Microsoft GS Wavetable Synth"))
    {
        midiFile.Play(outputDevice);
    }
    

    Play will block the calling thread until entire file played. To control playback of a MIDI file, obtain Playback object and use its Start/Stop methods (more details in the Playback article of the library docs):

    var playback = midiFile.GetPlayback(outputDevice);
    
    // You can even loop playback and speed it up
    playback.Loop = true;
    playback.Speed = 2.0;
    
    playback.Start();
    
    // ...
    
    playback.Stop();
    
    // ...
    
    playback.Dispose();
    outputDevice.Dispose();
    

提交回复
热议问题