コード例 #1
0
        private void FinishNote(MidiEvent midiEvent, Track track)
        {
            var beats = track.DefaultBarContext.BeatsInBar;

            Duration duration = helper.GetDuration(track.previousNoteAbsoluteTicks, midiEvent.AbsoluteTicks, track.division,
                                                   beats.Item1, beats.Item2, out double percentageOfBar, out int dots);

            // Finish the previous note with the length.
            track.previousNoteAbsoluteTicks = midiEvent.AbsoluteTicks;

            noteBuilder.SetDuration(duration);
            noteBuilder.SetPoints(dots);
            var note = noteBuilder.Build();

            track.CreateNewNote(note);

            percentageOfBarReached += percentageOfBar;
            if (percentageOfBarReached >= 1)
            {
                track.CreateNewBar();
                percentageOfBarReached -= 1;
            }

            startedNoteIsClosed = true;
        }
コード例 #2
0
        private void HandleNote(MidiEvent midiEvent, ChannelMessage previousChannelMessage)
        {
            double lengthOfNote       = CalculatePartOfBar(_previousNoteAbsoluteTicks, midiEvent.AbsoluteTicks);
            double availableLengthBar = EasyTimeSignature.Top - _notesOfBarReached;

            while (lengthOfNote > 0)
            {
                // Get note
                int         midiKeyNote = previousChannelMessage.Data1;
                NoteBuilder noteBuilder = NoteBuilderDictionary[midiKeyNote % 12];

                int distance = midiKeyNote - _previousMidiKey;
                _previousMidiKey = midiKeyNote;
                while (distance < -6)
                {
                    noteBuilder.DecreaseOctave();
                    distance += 8;
                }

                while (distance > 6)
                {
                    noteBuilder.IncreaseOctave();
                    distance -= 8;
                }

                // Get duration
                int  duration;
                int  dots;
                bool isTied;
                HandleDuration(lengthOfNote, availableLengthBar, out lengthOfNote, out availableLengthBar, out duration, out dots, out isTied);

                // Create note and add
                noteBuilder.Duration((DurationType)duration);
                noteBuilder.IsDotted(dots);
                noteBuilder.IsTied(isTied);

                if (NoteIsTied)
                {
                    noteBuilder.IsTied();
                    NoteIsTied = false;
                }

                AddToPiece(noteBuilder.Build());

                // Check bar
                if (availableLengthBar == 0)
                {
                    availableLengthBar = EasyTimeSignature.Top;
                }

                _notesOfBarReached = EasyTimeSignature.Top - availableLengthBar;
            }

            _previousNoteAbsoluteTicks = midiEvent.AbsoluteTicks;
        }
コード例 #3
0
        private MusicElement CreateMusicElement(string token)
        {
            NoteType noteType;

            Enum.TryParse(token.Substring(0, 1).ToUpper(), out noteType);

            var dots         = token.Count(c => c == '.');
            var number       = int.Parse(Regex.Match(token, @"\d+").Value);
            var durationType = (DurationType)number;

            if (new Regex(@"[a-g][,'eis]*[0-9]+[.]*").IsMatch(token))
            {
                var builder = new NoteBuilder(noteType);

                // Octave
                var increaseOctaves = token.Count(c => c == '\'');
                var decreaseOctaves = token.Count(c => c == ',') * -1;
                builder.ChangeOctave(increaseOctaves + decreaseOctaves);

                // Sharp or flat
                if (token.Contains("is"))
                {
                    builder = builder.Sharp();
                }
                else if (token.Contains("token"))
                {
                    builder = builder.Flat();
                }

                if (token.Contains("~"))
                {
                    builder = builder.IsTied();
                }

                builder.AddDots(dots).Duration(durationType);

                return(builder.Build());
            }

            if (new Regex(@"r.*?[0-9][.]*").IsMatch(token))
            {
                var builder = new RestBuilder();

                builder.AddDots(dots).Duration(durationType);

                return(builder.Build());
            }

            return(null);
        }