コード例 #1
0
        void LoadMidi(string MidFile, string Track)
        {
            TempoWorker tempo    = new TempoWorker(120, TempoWorker.NoteType.Crotchet);
            MidiRResult MidiRecv = MidiWorker.ReadMidi(MidFile, Track);
            NoteOnEvent FN       = MidiRecv.Notes[0];
            long        off      = FN.AbsoluteTime;

            for (int i = 0; i < MidiRecv.Notes.Count; i++)
            {
                NoteOnEvent NoteEvent = MidiRecv.Notes[i];
                PianoNote   PNote     = new PianoNote(
                    NoteEvent.AbsoluteTime - off,
                    NoteEvent.NoteLength,
                    new BalthasarLib.PianoRollWindow.PitchValuePair((uint)NoteEvent.NoteNumber, 0)
                    );
                PNote.OctaveType = PitchValuePair.OctaveTypeEnum.Piano;
                PNote.Lyric      = PNote.PitchValue.NoteChar + PNote.PitchValue.Octave.ToString();
                NoteList.Add(PNote);
            }
            int j = 0;
            int k = 0;

            PitchList.Add(k, new List <PitchNode>());
            for (int i = 0; i < MidiRecv.Pitchs.Count; i++)
            {
                if (j >= MidiRecv.Notes.Count)
                {
                    break;
                }
                if (MidiRecv.Pitchs[i].PitchWheelChangeEvent.AbsoluteTime - off < NoteList[j].Tick)
                {
                    continue;                                                                                //在音符前,抛弃
                }
                if (MidiRecv.Pitchs[i].PitchWheelChangeEvent.AbsoluteTime - off > NoteList[j].Tick + NoteList[j].Length)
                {
                    //在音符后
                    j++;
                    k++;
                    i--;//重来一遍
                    PitchList.Add(k, new List <PitchNode>());
                    continue;
                }
                PitchValuePair pvp  = new PitchValuePair((uint)MidiRecv.Notes[j].NoteNumber, MidiRecv.Pitchs[i].PIT, (uint)MidiRecv.Pitchs[i].PBS);
                PitchNode      PitN = new PitchNode(MidiRecv.Pitchs[i].PitchWheelChangeEvent.AbsoluteTime - off, pvp.PitchValue);
                PitchList[k].Add(PitN);
            }
        }
コード例 #2
0
        void PlayMidi(string MidFile, string Track)
        {
            List <PianoNote> NoteListEX = new List <PianoNote>();
            TempoWorker      tempo      = new TempoWorker(120, TempoWorker.NoteType.Crotchet);
            MidiRResult      MidiRecv   = MidiWorker.ReadMidi(MidFile, Track);
            NoteOnEvent      FN         = MidiRecv.Notes[0];
            long             off        = FN.AbsoluteTime;
            long             lastEnd    = 0;

            for (int i = 0; i < MidiRecv.Notes.Count; i++)
            {
                NoteOnEvent NoteEvent = MidiRecv.Notes[i];
                PianoNote   PNote     = new PianoNote(
                    NoteEvent.AbsoluteTime - off,
                    NoteEvent.NoteLength,
                    new BalthasarLib.PianoRollWindow.PitchValuePair((uint)NoteEvent.NoteNumber, 0)
                    );
                PNote.OctaveType = PitchValuePair.OctaveTypeEnum.Piano;
                PNote.Lyric      = PNote.PitchValue.NoteChar + PNote.PitchValue.Octave.ToString();
                if (PNote.Tick > lastEnd)
                {
                    NoteListEX.Add(new PianoNote(lastEnd, PNote.Tick - lastEnd, 0));
                }
                NoteListEX.Add(PNote);
            }
            using (MidiOut midiOut = new MidiOut(0))
            {
                midiOut.Volume = 65535;
                int  j   = 0;
                int  k   = 0;
                bool isp = false;
                for (int i = 0; i < MidiRecv.Pitchs.Count; i++)
                {
                    if (j >= MidiRecv.Notes.Count)
                    {
                        break;
                    }
                    if (MidiRecv.Pitchs[i].PitchWheelChangeEvent.AbsoluteTime - off < 0)
                    {
                        continue;
                    }
                    if (MidiRecv.Pitchs[i].PitchWheelChangeEvent.AbsoluteTime - off < NoteListEX[j].Tick)
                    {
                        System.Threading.Thread.Sleep((int)tempo.Tick2Millisecond(MidiRecv.Pitchs[i].PitchWheelChangeEvent.DeltaTime));
                        continue;    //在音符前,抛弃
                    }
                    if (MidiRecv.Pitchs[i].PitchWheelChangeEvent.AbsoluteTime - off > NoteListEX[j].Tick + NoteListEX[j].Length)
                    {
                        //在音符后
                        midiOut.Send(MidiMessage.StopNote((int)NoteListEX[j].PitchValue.NoteNumber, 0, 1).RawData);
                        isp = false;
                        j++;
                        k++;
                        i--;    //重来一遍
                        PitchList.Add(k, new List <PitchNode>());
                        continue;
                    }
                    if (!isp)
                    {
                        isp = true;
                        midiOut.Send(MidiMessage.StartNote((int)NoteListEX[j].PitchValue.NoteNumber, 127, 1).RawData);
                    }
                    midiOut.Send(MidiRecv.Pitchs[i].PitchWheelChangeEvent.GetAsShortMessage());
                    System.Threading.Thread.Sleep((int)tempo.Tick2Millisecond(MidiRecv.Pitchs[i].PitchWheelChangeEvent.DeltaTime));
                }
            }
        }