Exemple #1
0
 public static void UnsubscribeFromMidi(this IMidiSampleProvider msp, Midi midi)
 {
     midi.NoteOn           -= msp.HandleNoteOn;
     midi.NoteOff          -= msp.HandleNoteOff;
     midi.PitchWheelChange -= msp.HandlePitchWheelChange;
     midi.ControlChange    -= msp.HandleControlChange;
 }
Exemple #2
0
        public static void PlayMidiToFile(string midiFile, string destFile, IMidiSampleProvider msp, SynthData data)
        {
            float sampleNum = 0;

            var file = new MidiFile(midiFile);
            int ticksPerQuarterNote = file.DeltaTicksPerQuarterNote;
            var events      = file.Events.Aggregate((IEnumerable <MidiEvent>) new List <MidiEvent>(), (totalList, list) => totalList.Concat(list)).OrderBy(me => me.AbsoluteTime);
            var totalEvents = events.Count();

            var board = msp.Clone(data);
            var midi  = new Midi(ticksPerQuarterNote);

            midi.NoteOn  += board.HandleNoteOn;
            midi.NoteOff += board.HandleNoteOff;

            using (var output = new WaveFileWriter(destFile, WaveFormat.CreateIeeeFloatWaveFormat(data.SampleRate, 2)))
            {
                int eventCount = 0;
                foreach (var me in events)
                {
                    var absSample = midi.MidiTicksToSamples(me.AbsoluteTime, board.SampleRate);
                    var samples   = (int)(absSample - sampleNum);

                    var result = new float[samples];
                    board.Next(result, 0, samples, 1);

                    output.WriteSamples(result, 0, samples);
                    sampleNum = absSample;
                    midi.HandleMidiEvent(me);
                    Console.WriteLine($"{++eventCount}/{totalEvents}");
                }
            }
        }
Exemple #3
0
 public static void SubscribeToMidi(this IMidiSampleProvider msp, Midi midi)
 {
     midi.NoteOn           += msp.HandleNoteOn;
     midi.NoteOff          += msp.HandleNoteOff;
     midi.PitchWheelChange += msp.HandlePitchWheelChange;
     midi.ControlChange    += msp.HandleControlChange;
 }
Exemple #4
0
        public static void PlayMidi(string midiFile, IMidiSampleProvider msp, SynthData data)
        {
            var file = new MidiFile(midiFile);
            int ticksPerQuarterNote = file.DeltaTicksPerQuarterNote;
            var events      = file.Events.Aggregate((IEnumerable <MidiEvent>) new List <MidiEvent>(), (totalList, list) => totalList.Concat(list)).OrderBy(me => me.AbsoluteTime);
            var totalEvents = events.Count();

            var board = msp.Clone(data);
            var midi  = new Midi(ticksPerQuarterNote);

            midi.NoteOn  += board.HandleNoteOn;
            midi.NoteOff += board.HandleNoteOff;

            var synthResult = new SynthResult(data);

            synthResult.ReplaceSynthProvider(board);

            var aOut = new WaveOutEvent
            {
                DesiredLatency = data.DesiredLatency,
                DeviceNumber   = -1
            };

            aOut.Init(synthResult);
            aOut.Play();

            long startTime = DateTime.Now.Ticks;

            foreach (var me in events)
            {
                while (DateTime.Now.Ticks - startTime < midi.MidiTicksToDateTimeTicks(me.AbsoluteTime))
                {
                    ;
                }
                midi.HandleMidiEvent(me);
            }
        }