예제 #1
0
 protected TuneConverter()
 {
     DurationConverter = new DurationConverter();
     ScaleConverter    = new ScaleConverter();
     PitchConverter    = new PitchConverter();
 }
예제 #2
0
        protected override Tune InternalParse(string s)
        {
            var parts = s.Split(TuneCommandDelimiter);

            if (parts.Count() < 2 || parts.Count() > 3)
            {
                throw new FormatException();
            }

            var name = parts[0];

            string notes;
            var    bpm             = DefaultBpm.ToString();
            var    defaultDuration = DefaultDuration;
            var    defaultScale    = DefaultScale;

            if (parts.Count() == 3)
            {
                notes = parts[2].ToUpperInvariant();

                var commandSection = parts[1];

                var bpmString = GetFirstValueGroup(commandSection, PatternDefaultBpm);
                if (!string.IsNullOrEmpty(bpmString))
                {
                    bpm = bpmString;
                }

                var durationString = GetFirstValueGroup(commandSection, PatternDefaultDuration);
                if (!string.IsNullOrEmpty(durationString))
                {
                    defaultDuration = durationString;
                }

                var scaleString = GetFirstValueGroup(commandSection, PatternDefaultScale);
                if (!string.IsNullOrEmpty(scaleString))
                {
                    defaultScale = scaleString;
                }
            }
            else
            {
                notes = parts[1].ToUpperInvariant();
            }

            var tuneElementList = new List <TuneElement>();
            var regexNotes      = new Regex(TuneElementPattern);
            var matches         = regexNotes.Matches(notes);

            foreach (Match m in matches)
            {
                var durationString = string.IsNullOrEmpty(m.Groups[1].Value) ? defaultDuration : m.Groups[1].Value;
                var pitchString    = m.Groups[2].Value;
                var dotted         = m.Groups[3].Value == ".";
                var scaleString    = string.IsNullOrEmpty(m.Groups[4].Value) ? defaultScale : m.Groups[4].Value;

                var duration = DurationConverter.Parse(durationString);
                var scale    = ScaleConverter.Parse(scaleString);

                TuneElement tuneElement;
                if (pitchString == Pause)
                {
                    tuneElement = new Pause(duration);
                }
                else
                {
                    var pitch = PitchConverter.Parse(pitchString);
                    tuneElement = new Note(pitch, scale, duration);
                }
                tuneElement.Dotted = dotted;
                tuneElementList.Add(tuneElement);
            }

            var tune = new Tune(tuneElementList, Convert.ToInt32(bpm));

            tune.Name = name;

            return(tune);
        }