Example #1
0
 protected override void SetTempo(MMLCommand cmd)
 {
     if (Mode == MMLMode.Mabinogi) { // tempo changes in Mabinogi apply to all tracks
         parent.SetTempo(Convert.ToInt32(cmd.Args[0]));
     }
     else { // tempo changes in ArcheAge only apply to the current track
         base.SetTempo(cmd);
     }
 }
Example #2
0
        public override void Load(string str)
        {
            //if (str.Length > settings.MaxSize) {
            //    throw new SongSizeException("Song exceeded maximum length of " + settings.MaxSize);
            //}

            commands = new List <MMLCommand>();
            string code = str.Replace("\n", "").Replace("\r", "");

            MatchCollection matches = Regex.Matches(code, mmlPatterns);

            for (int i = 0; i < matches.Count; ++i)
            {
                commands.Add(MMLCommand.Parse(matches[i].Value));
            }

            CalculateDuration();
            SetDefaultValues();
        }
        protected virtual void ProcessCommands()
        {
            bool noteFound = false;

            while (!noteFound && this.cmdIndex < this.commands.Count)
            {
                MMLCommand cmd = this.commands[this.cmdIndex];
                if (cmd.Type == MMLCommandType.Note || cmd.Type == MMLCommandType.NoteNumber)
                {
                    return;
                }
                if (cmd.Type == MMLCommandType.Rest)
                {
                    return;
                }
                this.ProcessCommand(cmd);
                this.cmdIndex++;
            }
        }
        protected virtual void CalculateDuration()
        {
            TimeSpan dur = TimeSpan.Zero;

            for (int i = 0; i < this.commands.Count; i++)
            {
                MMLCommand cmd = this.commands[i];
                switch (cmd.Type)
                {
                case MMLCommandType.Tempo:
                    this.SetTempo(cmd);
                    break;

                case MMLCommandType.Length:
                    this.SetLength(cmd);
                    break;

                case MMLCommandType.Note:
                case MMLCommandType.NoteNumber:
                {
                    double measureLength;
                    Note   note = this.GetNote(cmd, out measureLength, this.length, this.octave, this.spm);
                    dur += note.Length;
                    break;
                }

                case MMLCommandType.Rest:
                {
                    double measureLength;
                    dur += this.GetRest(cmd, out measureLength).ToTimeSpan(this.spm);
                    break;
                }
                }
                if (dur > this.Settings.MaxDuration)
                {
                    throw new SongDurationException("Song exceeded maximum duration " + this.Settings.MaxDuration.ToString());
                }
            }
            this.duration = dur;
        }
Example #5
0
        private Note GetNoteNumber(MMLCommand cmd, out double measureLength, MMLLength defaultLength, double currentSpm)
        {
            Note note = new Note
            {
                Volume = volume / 15f,
                Octave = 1,
                Type   = 'c'
            };

            // 12 == C1
            int noteNumber = Convert.ToInt32(cmd.Args[0]) - 12;
            int octavesUp  = noteNumber / 12;

            int steps;

            if (octavesUp != 0)
            {
                steps = noteNumber % (octavesUp * 12);
            }
            else
            {
                steps = noteNumber;
            }

            note.Octave += octavesUp;
            Step(ref note, steps);

            var mmlLen = GetLength("", cmd.Args[1], defaultLength);

            note.Length = mmlLen.ToTimeSpan(currentSpm);

            measureLength = 1.0 / mmlLen.Length;
            if (mmlLen.Dotted)
            {
                measureLength *= 1.5;
            }

            return(note);
        }
Example #6
0
        private Note GetNoteNormal(MMLCommand cmd, out double measureLength, MMLLength defaultLength, int currentOctave, double currentSpm)
        {
            Note note = new Note
            {
                Volume = volume / 15f,
                Octave = currentOctave,
                Type   = cmd.Args[0].ToLowerInvariant()[0]
            };

            switch (cmd.Args[1])
            {
            case "#":
            case "+":
                Step(ref note, +1);
                break;

            case "-":
                Step(ref note, -1);
                break;

            default:
                note.Sharp = false;
                break;
            }

            var mmlLen = GetLength(cmd.Args[2], cmd.Args[3], defaultLength);

            note.Length = mmlLen.ToTimeSpan(currentSpm);

            measureLength = 1.0 / mmlLen.Length;
            if (mmlLen.Dotted)
            {
                measureLength *= 1.5;
            }

            return(note);
        }
Example #7
0
 protected virtual void SetTempo(MMLCommand cmd)
 {
     Tempo = Convert.ToInt32(cmd.Args[0]);
 }
Example #8
0
 protected virtual void SetLength(MMLCommand cmd, ref MMLLength len)
 {
     len = new MMLLength(Convert.ToInt32(cmd.Args[0]), cmd.Args[1] != "");
 }
Example #9
0
 protected virtual void SetLength(MMLCommand cmd)
 {
     SetLength(cmd, ref length);
 }
Example #10
0
        private Note GetNoteNormal(MMLCommand cmd, out double measureLength, MMLLength defaultLength, int currentOctave, double currentSpm)
        {
            Note note = new Note();

            note.Volume = volume / 15f;
            note.Octave = currentOctave;
            note.Type = cmd.Args[0].ToLowerInvariant()[0];

            switch (cmd.Args[1]) {
                case "#":
                case "+":
                    Step(ref note, +1);
                    break;
                case "-":
                    Step(ref note, -1);
                    break;
                default:
                    note.Sharp = false;
                    break;
            }

            var mmlLen = GetLength(cmd.Args[2], cmd.Args[3], defaultLength);
            note.Length = mmlLen.ToTimeSpan(currentSpm);

            measureLength = 1.0 / mmlLen.Length;
            if (mmlLen.Dotted)
                measureLength *= 1.5;

            return note;
        }
Example #11
0
 protected virtual void SetLength(MMLCommand cmd, ref MMLLength len)
 {
     len = new MMLLength(Convert.ToInt32(cmd.Args[0]), cmd.Args[1] != "");
 }
Example #12
0
 protected virtual void SetLength(MMLCommand cmd)
 {
     SetLength(cmd, ref length);
 }
Example #13
0
 protected virtual void SetTempo(MMLCommand cmd)
 {
     Tempo = Convert.ToInt32(cmd.Args[0]);
 }
        private void NextNote()
        {
            bool noteFound = false;

            while (!noteFound && this.cmdIndex < this.commands.Count)
            {
                MMLCommand cmd = this.commands[this.cmdIndex];
                if (cmd.Type == MMLCommandType.Note || cmd.Type == MMLCommandType.NoteNumber)
                {
                    noteFound = true;
                    double measureLength;
                    Note   note = this.GetNote(cmd, out measureLength, this.length, this.octave, this.spm);
                    this.nextCommand = this.nextNote + measureLength;
                    if (this.cmdIndex >= this.nextNoteIndex)
                    {
                        bool      tied            = false;
                        int       lookAheadIndex  = this.cmdIndex + 1;
                        int       lookAheadOctave = this.octave;
                        int       lookAheadTempo  = this.tempo;
                        double    lookAheadSpm    = this.spm;
                        MMLLength lookAheadLen    = this.length;
                        while (lookAheadIndex < this.commands.Count)
                        {
                            MMLCommandType type = this.commands[lookAheadIndex].Type;
                            if (type == MMLCommandType.Tie)
                            {
                                tied = true;
                            }
                            else
                            {
                                if (type == MMLCommandType.Rest)
                                {
                                    break;
                                }
                                if (type == MMLCommandType.Octave)
                                {
                                    lookAheadOctave = Convert.ToInt32(this.commands[lookAheadIndex].Args[0]);
                                }
                                else if (type == MMLCommandType.OctaveDown)
                                {
                                    lookAheadOctave--;
                                }
                                else if (type == MMLCommandType.OctaveDown)
                                {
                                    lookAheadOctave++;
                                }
                                else if (type == MMLCommandType.Tempo)
                                {
                                    this.SetTempoAndSecondsPerMeasure(Convert.ToInt32(this.commands[lookAheadIndex].Args[0]), ref lookAheadTempo, ref lookAheadSpm);
                                }
                                else if (type == MMLCommandType.Length)
                                {
                                    this.SetLength(this.commands[lookAheadIndex], ref lookAheadLen);
                                }
                                else if (type == MMLCommandType.Note || type == MMLCommandType.NoteNumber)
                                {
                                    if (!tied)
                                    {
                                        break;
                                    }
                                    tied = false;
                                    double tiedLength;
                                    Note   tiedNote = this.GetNote(this.commands[lookAheadIndex], out tiedLength, lookAheadLen, lookAheadOctave, lookAheadSpm);
                                    if (tiedNote.Sharp != note.Sharp || tiedNote.Type != note.Type || tiedNote.Octave != note.Octave)
                                    {
                                        break;
                                    }
                                    note.Length       += tiedNote.Length;
                                    measureLength     += tiedLength;
                                    this.nextNoteIndex = lookAheadIndex + 1;
                                }
                            }
                            lookAheadIndex++;
                        }
                        this.nextNote += measureLength;
                        this.notes.Add(this.ValidateAndPlayNote(note));
                    }
                }
                else if (cmd.Type == MMLCommandType.Rest)
                {
                    double measureLength2;
                    this.GetRest(cmd, out measureLength2);
                    this.nextNote   += measureLength2;
                    this.nextCommand = this.nextNote;
                    noteFound        = true;
                }
                else
                {
                    this.ProcessCommand(cmd);
                }
                this.cmdIndex++;
            }
        }
 protected virtual void SetLength(MMLCommand cmd)
 {
     this.SetLength(cmd, ref this.length);
 }
Example #16
0
        public static MMLCommand Parse(string token)
        {
            MMLCommand     cmd  = default(MMLCommand);
            List <string>  args = new List <string>();
            char           c    = token.ToLowerInvariant()[0];
            MMLCommandType t;

            if (c <= '<')
            {
                if (c == '&')
                {
                    t = MMLCommandType.Tie;
                    goto IL_17E;
                }
                if (c == '<')
                {
                    t = MMLCommandType.OctaveDown;
                    goto IL_17E;
                }
            }
            else
            {
                if (c == '>')
                {
                    t = MMLCommandType.OctaveUp;
                    goto IL_17E;
                }
                switch (c)
                {
                case 'a':
                case 'b':
                case 'c':
                case 'd':
                case 'e':
                case 'f':
                case 'g':
                    t = MMLCommandType.Note;
                    MMLCommand.AddPart(args, token, "[a-gA-G]");
                    MMLCommand.AddPart(args, token, "(\\+|#|-)");
                    MMLCommand.AddPart(args, token, MMLCommand.lengths);
                    MMLCommand.AddPart(args, token, "\\.");
                    goto IL_17E;

                case 'l':
                    t = MMLCommandType.Length;
                    MMLCommand.AddPart(args, token, MMLCommand.lengths);
                    MMLCommand.AddPart(args, token, "\\.");
                    goto IL_17E;

                case 'n':
                    t = MMLCommandType.NoteNumber;
                    MMLCommand.AddPart(args, token, "\\d+");
                    MMLCommand.AddPart(args, token, "\\.");
                    goto IL_17E;

                case 'o':
                    t = MMLCommandType.Octave;
                    MMLCommand.AddPart(args, token, "\\d");
                    goto IL_17E;

                case 'r':
                    t = MMLCommandType.Rest;
                    MMLCommand.AddPart(args, token, MMLCommand.lengths);
                    MMLCommand.AddPart(args, token, "\\.");
                    goto IL_17E;

                case 't':
                    t = MMLCommandType.Tempo;
                    MMLCommand.AddPart(args, token, "\\d{1,3}");
                    goto IL_17E;

                case 'v':
                    t = MMLCommandType.Volume;
                    MMLCommand.AddPart(args, token, "\\d+");
                    goto IL_17E;
                }
            }
            t = MMLCommandType.Unknown;
            args.Add(token);
IL_17E:
            cmd.Type = t;
            cmd.Args = args;
            return(cmd);
        }
Example #17
0
 private MMLLength GetRest(MMLCommand cmd, out double measureLength)
 {
     MMLLength rest = GetLength(cmd.Args[0], cmd.Args[1]);
     measureLength = 1.0 / rest.Length * (rest.Dotted ? 1.5 : 1.0);
     return rest;
 }
Example #18
0
        private Note GetNoteNumber(MMLCommand cmd, out double measureLength, MMLLength defaultLength, double currentSpm)
        {
            Note note = new Note();

            note.Volume = volume / 15f;
            note.Octave = 1;
            note.Type = 'c';

            // 12 == C1
            int noteNumber = Convert.ToInt32(cmd.Args[0]) - 12;
            int octavesUp = noteNumber / 12;

            int steps;
            if (octavesUp != 0)
                steps = noteNumber % (octavesUp * 12);
            else
                steps = noteNumber;

            note.Octave += octavesUp;
            Step(ref note, steps);

            var mmlLen = GetLength("", cmd.Args[1], defaultLength);
            note.Length = mmlLen.ToTimeSpan(currentSpm);

            measureLength = 1.0 / mmlLen.Length;
            if (mmlLen.Dotted)
                measureLength *= 1.5;

            return note;
        }
Example #19
0
        public static MMLCommand Parse(string token)
        {
            MMLCommand     cmd  = new MMLCommand();
            MMLCommandType t    = MMLCommandType.Unknown;
            List <string>  args = new List <string>();

            switch (token.ToLowerInvariant()[0])
            {
            case 't':
                t = MMLCommandType.Tempo;
                AddPart(args, token, @"\d{1,3}");
                break;

            case 'l':
                t = MMLCommandType.Length;
                AddPart(args, token, @"(16|2|4|8|1|32|64)");
                AddPart(args, token, @"\.");
                break;

            case 'v':
                t = MMLCommandType.Volume;
                AddPart(args, token, @"\d+");
                break;

            case 'o':
                t = MMLCommandType.Octave;
                AddPart(args, token, @"\d");
                break;

            case '<':
                t = MMLCommandType.OctaveDown;
                break;

            case '>':
                t = MMLCommandType.OctaveUp;
                break;

            case 'a':
            case 'b':
            case 'c':
            case 'd':
            case 'e':
            case 'f':
            case 'g':
                t = MMLCommandType.Note;
                AddPart(args, token, @"[a-gA-G]");
                AddPart(args, token, @"(\+|#|-)");
                AddPart(args, token, @"(16|2|4|8|1|32|64)");
                AddPart(args, token, @"\.");
                break;

            case 'r':
                t = MMLCommandType.Rest;
                AddPart(args, token, @"(16|2|4|8|1|32|64)");
                AddPart(args, token, @"\.");
                break;

            case 'n':
                t = MMLCommandType.NoteNumber;
                AddPart(args, token, @"\d+");
                AddPart(args, token, @"\.");
                break;

            case '&':
                t = MMLCommandType.Tie;
                break;

            default:
                t = MMLCommandType.Unknown;
                args.Add(token);
                break;
            }

            cmd.Type = t;
            cmd.Args = args;

            return(cmd);
        }
Example #20
0
        public static MMLCommand Parse(string token)
        {
            MMLCommand cmd = new MMLCommand();
            MMLCommandType t = MMLCommandType.Unknown;
            List<string> args = new List<string>();

            switch (token.ToLowerInvariant()[0]) {
                case 't':
                    t = MMLCommandType.Tempo;
                    AddPart(args, token, @"\d{1,3}");
                    break;
                case 'l':
                    t = MMLCommandType.Length;
                    AddPart(args, token, @"(16|2|4|8|1|32|64)");
                    AddPart(args, token, @"\.");
                    break;
                case 'v':
                    t = MMLCommandType.Volume;
                    AddPart(args, token, @"\d+");
                    break;
                case 'o':
                    t = MMLCommandType.Octave;
                    AddPart(args, token, @"\d");
                    break;
                case '<':
                    t = MMLCommandType.OctaveDown;
                    break;
                case '>':
                    t = MMLCommandType.OctaveUp;
                    break;
                case 'a':
                case 'b':
                case 'c':
                case 'd':
                case 'e':
                case 'f':
                case 'g':
                    t = MMLCommandType.Note;
                    AddPart(args, token, @"[a-gA-G]");
                    AddPart(args, token, @"(\+|#|-)");
                    AddPart(args, token, @"(16|2|4|8|1|32|64)");
                    AddPart(args, token, @"\.");
                    break;
                case 'r':
                    t = MMLCommandType.Rest;
                    AddPart(args, token, @"(16|2|4|8|1|32|64)");
                    AddPart(args, token, @"\.");
                    break;
                case 'n':
                    t = MMLCommandType.NoteNumber;
                    AddPart(args, token, @"\d+");
                    AddPart(args, token, @"\.");
                    break;
                case '&':
                    t = MMLCommandType.Tie;
                    break;
                default:
                    t = MMLCommandType.Unknown;
                    args.Add(token);
                    break;
            }

            cmd.Type = t;
            cmd.Args = args;

            return cmd;
        }
Example #21
0
 protected virtual void ProcessCommand(MMLCommand cmd)
 {
     switch (cmd.Type) {
         case MMLCommandType.Length:
             SetLength(cmd);
             break;
         case MMLCommandType.Octave:
             SetOctave(Convert.ToInt32(cmd.Args[0]));
             break;
         case MMLCommandType.OctaveDown:
             SetOctave(octave - 1);
             break;
         case MMLCommandType.OctaveUp:
             SetOctave(octave + 1);
             break;
         case MMLCommandType.Tempo:
             SetTempo(cmd);
             break;
         case MMLCommandType.Volume:
             SetVolume(Convert.ToInt32(cmd.Args[0]));
             break;
     }
 }
Example #22
0
 private Note GetNote(MMLCommand cmd, out double measureLength, MMLLength defaultLength, int currentOctave, double currentSpm)
 {
     if (cmd.Type == MMLCommandType.Note) {
         return GetNoteNormal(cmd, out measureLength, defaultLength, currentOctave, currentSpm);
     }
     else {
         return GetNoteNumber(cmd, out measureLength, defaultLength, currentSpm);
     }
 }