Beispiel #1
0
        static void Main(string[] args)
        {
            try
            {
                if (args.Length != 1)
                {
                    Console.WriteLine("Usage: MidiDumper File.mid");
                    return;
                }
                var midiData = ByteBuffer.FromBuffer(File.ReadAllBytes(args[0]));
                var midi = new MidiFile();
                midi.Load(midiData);

                Console.WriteLine("Division: {0}", midi.Division);
                Console.WriteLine("TrackFormat: {0}", midi.TrackFormat);
                Console.WriteLine("TimingStandard: {0}", midi.TimingStandard);
                Console.WriteLine("Tracks: {0}", midi.Tracks.Length);
                Console.WriteLine();

                for (int i = 0; i < midi.Tracks.Length; i++)
                {
                    var midiTrack = midi.Tracks[i];
                    Console.WriteLine("Track {0} ({1} Events)", i, midiTrack.MidiEvents.Length);

                    ulong absoluteTime = 0;
                    for (int j = 0; j < midiTrack.MidiEvents.Length; j++)
                    {
                        var midiEvent = midiTrack.MidiEvents[j];

                        Console.WriteLine(" [{0}] Delta[{1}] Chl[{2}] Cmd[{3}] Data1[{4}] Data2[{5}]", absoluteTime,
                            midiEvent.DeltaTime, midiEvent.Channel, midiEvent.Command, midiEvent.Data1, midiEvent.Data2);

                        absoluteTime += (ulong) midiEvent.DeltaTime;
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Failed: {0}", e);
            }
        }
        private void LoadMidiFile(MidiFile midiFile)
        {
            _tempoChanges = new FastList<MidiFileSequencerTempoChange>();
            //Converts midi to sample based format for easy sequencing
            float bpm = 120.0f;
            //Combine all tracks into 1 track that is organized from lowest to highest absolute time
            if (midiFile.Tracks.Length > 1 || midiFile.Tracks[0].EndTime == 0)
                midiFile.CombineTracks();

            //Convert delta time to sample time
            _synthData = new SynthEvent[midiFile.Tracks[0].MidiEvents.Length];
            _division = midiFile.Division;
            _eventIndex = 0;
            CurrentTime = 0;
            CurrentTempo = (int)bpm;
            //Calculate sample based time using double counter and round down to nearest integer sample.
            var absDelta = 0.0;
            var absTick = 0;
            var absTime = 0.0;
            for (int x = 0; x < midiFile.Tracks[0].MidiEvents.Length; x++)
            {
                var mEvent = midiFile.Tracks[0].MidiEvents[x];
                _synthData[x] = new SynthEvent(mEvent);
                absTick += mEvent.DeltaTime;
                absTime += mEvent.DeltaTime * (60000.0 / (bpm * midiFile.Division));
                absDelta += Synth.SampleRate * mEvent.DeltaTime * (60.0 / (bpm * midiFile.Division));
                _synthData[x].Delta = (int)(absDelta);
                //Update tempo
                if (IsTempoMessage(mEvent.Command, mEvent.Data1))
                {
                    var meta = (MetaNumberEvent)mEvent;
                    bpm = MidiHelper.MicroSecondsPerMinute / meta.Value;
                    _tempoChanges.Add(new MidiFileSequencerTempoChange(bpm, absTick, (int)(absTime)));
                }
            }

            EndTime = _synthData[_synthData.Length - 1].Delta;
        }
Beispiel #3
0
 public void LoadMidiBytes(byte[] data)
 {
     if (State != SynthPlayerState.Stopped) return;
     var input = ByteBuffer.FromBuffer(data);
     try
     {
         Logger.Info("Loading midi from bytes");
         var midi = new MidiFile();
         midi.Load(input);
         Sequencer.LoadMidi(midi);
         IsMidiLoaded = true;
         OnMidiLoaded();
         Logger.Info("Midi successfully loaded");
         if (IsReady) OnReadyForPlay();
         FirePositionChanged(0);
     }
     catch (Exception e)
     {
         Logger.Error("Could not load midi from bytes " + e);
         IsMidiLoaded = false;
         Sequencer.UnloadMidi();
         OnMidiLoadFailed();
     }
 }
 public bool LoadMidi(MidiFile midiFile)
 {
     if (IsPlaying) return false;
     LoadMidiFile(midiFile);
     return true;
 }