Exemplo n.º 1
0
        public static List <GenericTokenDefinition <MusicToken> > GetInstrumentDefs(DebugDelegate d, bool includeguide = true, bool includegeneral = true)
        {
            List <GenericTokenDefinition <MusicToken> > insttoks = new List <GenericTokenDefinition <MusicToken> >();

            if (includeguide)
            {
                foreach (var inst in InstrumentHelper.getenums <InstrumentGuide>().Select(i => i.ToString()))
                {
                    var exactinst = inst; // inst.ToUpperInvariant();
                    var tokdef    = GenericTokenHelper.getdef <MusicToken>(exactinst, MusicToken.Instrument);
                    insttoks.Add(tokdef);
                }
            }
            if (includegeneral)
            {
                foreach (var inst in InstrumentHelper.getenums <InstrumentsGeneral>().Select(i => i.ToString()))
                {
                    var exactinst = inst; // inst.ToUpperInvariant();
                    var tokdef    = GenericTokenHelper.getdef <MusicToken>(exactinst, MusicToken.Instrument);
                    insttoks.Add(tokdef);
                }
            }
            return(insttoks);
        }
Exemplo n.º 2
0
 /// <summary>
 /// change to instrument
 /// </summary>
 /// <param name="dev"></param>
 /// <param name="program_change_number"></param>
 public static void SetInstrument(this OutputDevice dev, int program_change_number = 1)
 {
     //via: https://www.midi.org/specifications/item/gm-level-1-sound-set
     dev.Send(InstrumentHelper.GetNew(program_change_number));
 }
Exemplo n.º 3
0
 /// <summary>
 /// set an instrument
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="dev"></param>
 /// <param name="instrument"></param>
 public static void SetInstrument <T>(this OutputDevice dev, T instrument) where T : struct, IConvertible
 {
     dev.Send(InstrumentHelper.GetNew <T>(instrument));
 }
Exemplo n.º 4
0
        static List <ChannelMessage> Toks2Msgs(DebugDelegate d, List <GenericToken <MusicToken> > score, int defaultkey = 5, int defaultms_qtrnote = Adiago.NOTE_DEFAULT_DUR_MS)
        {
            StringBuilder         songinfo = new StringBuilder(score.Count * 10);
            List <ChannelMessage> msgs     = new List <ChannelMessage>();
            // strip out undefined stuff
            //var score = GenericTokenHelper.Filter_Exclude(songstream, MusicToken.Comment, MusicToken.CatchAll, MusicToken.WhiteSpace);

            var last = new GenericToken <MusicToken>(MusicToken.None);
            int curkey = 5, tokidx = 0, chordcount = 0, notecount = 0, track = 0, vel = Adiago.NOTE_DEFAULT_VEL;

            while (tokidx < score.Count)
            {
                var tok = score[tokidx];
                GenericToken <MusicToken> peek = (tokidx + 1) < score.Count ? score[tokidx + 1] : GenericTokenHelper.GetInvalid <MusicToken>();
                var ismakechord = (peek != null) && (peek.type == MusicToken.MakeChord);
                switch (tok.type)
                {
                case MusicToken.Track:
                {
                    track++;
                    tokidx++;
                    songinfo.AppendLine("Track" + track.ToString("n0") + "-->");
                }
                break;

                case MusicToken.BPM:
                {
                    var bpmdata = Util.rxr(tok.data, @"BPM\s*", string.Empty, false);
                    int tmpbpm;
                    if (int.TryParse(bpmdata, out tmpbpm))
                    {
                        songinfo.AppendLine("BPM=" + tmpbpm.ToString("F0"));
                        defaultms_qtrnote = getqtrnote_duration(tmpbpm);
                    }
                    else
                    {
                        debug(tok.ToString() + " Unknown BPM specification, will use existing default BPM");
                    }
                    tokidx++;
                }
                break;

                case MusicToken.Instrument:
                {
                    string iname = string.Empty;
                    msgs.Add(InstrumentHelper.Parse <InstrumentGuide>(tok, InstrumentGuide.AcousticGrandPiano, ref iname, track));
                    songinfo.AppendLine("Instrument: " + iname);
                    tokidx++;
                }
                break;

                case MusicToken.ScoreKey:
                {
                    var keydata = Util.rxr(tok.data, @"KEY\s*", string.Empty, false);
                    int tmpkey;
                    if (int.TryParse(keydata, out tmpkey) && (tmpkey >= 0) && (tmpkey <= 10))
                    {
                        songinfo.AppendLine("Key=" + tmpkey.ToString("F0"));
                        defaultkey = tmpkey;
                    }
                    else
                    {
                        debug(tok.ToString() + " Unknown key spec, using default key.");
                    }
                    tokidx++;
                }
                break;

                case MusicToken.Chord:
                case MusicToken.ChordScale:
                {
                    // parse chord to music instructions
                    var notes = MusicChord2MusicNotes(tok, ref defaultkey, defaultms_qtrnote, ref songinfo);
                    // get midi instructions
                    StringBuilder ignore = new StringBuilder();
                    msgs.AddRange(GetChord(notes, ref defaultkey, defaultms_qtrnote, track, vel, ref ignore, ref notecount));
                    var cinfo = tok.data[0].ToString().ToUpperInvariant();
                    if (tok.data.Length > 1)
                    {
                        cinfo += tok.data.Substring(1, tok.data.Length - 2).ToLowerInvariant();
                    }
                    songinfo.Append(cinfo + " ");
                    // next token
                    tokidx++;
                }
                break;

                case MusicToken.WholeNoteRest:
                case MusicToken.HalfNoteRest:
                case MusicToken.QuarterNoteRest:
                {
                    tok.data = string.Empty;
                    var dur = getnote_duration(defaultms_qtrnote, tok, peek);
                    msgs.AddRange(GetNote(tok, ref defaultkey, dur, track, vel, ref songinfo, true, true));
                    tokidx++;
                }
                break;

                case MusicToken.NoteScale:
                case MusicToken.Note:

                {
                    if (ismakechord)
                    {
                        // get entire chord
                        var chord = GetChordNotes(ref tokidx, ref score);
                        msgs.AddRange(GetChord(chord, ref defaultkey, defaultms_qtrnote, track, vel, ref songinfo, ref notecount));
                        chordcount++;
                    }
                    else
                    {
                        notecount++;
                        var dur = getnote_duration(defaultms_qtrnote, tok, peek);
                        msgs.AddRange(GetOneNote(tok, peek, ref defaultkey, dur, ref songinfo, track, vel));
                        tokidx++;
                    }
                }
                break;

                default:
                    tokidx++;
                    break;
                }

                last = tok;
            }


            v("Song: " + score[0].source + " (chords=" + chordcount.ToString("n0") + ",notes=" + notecount.ToString("n0") + "): " + songinfo.ToString());
            return(msgs);
        }
Exemplo n.º 5
0
        static List <string> Toks2Msgs(DebugDelegate d, List <GenericToken <MusicToken> > score, int defaultkey = 5, int defaultms_note = Adiago.NOTE_DEFAULT_DUR_MS)
        {
            List <string> staff = new List <string>();
            // strip out undefined stuff
            //var score = GenericTokenHelper.Filter_Exclude(songstream, MusicToken.Comment, MusicToken.CatchAll, MusicToken.WhiteSpace);

            var last = new GenericToken <MusicToken>(MusicToken.None);
            int curkey = 5, tokidx = 0, chordcount = 0, notecount = 0;

            while (tokidx < score.Count)
            {
                var tok = score[tokidx];
                GenericToken <MusicToken> peek = (tokidx + 1) < score.Count ? score[tokidx + 1] : GenericTokenHelper.GetInvalid <MusicToken>();
                var ischord = (peek != null) && (peek.type == MusicToken.MakeChord);
                switch (tok.type)
                {
                case MusicToken.Instrument:
                {
                    string iname = string.Empty;
                    InstrumentHelper.Parse <InstrumentGuide>(tok, InstrumentGuide.AcousticGrandPiano, ref iname);
                    staff.Add("Instrument: " + iname);
                    tokidx++;
                }
                break;

                case MusicToken.NoteScale:
                case MusicToken.Note:
                case MusicToken.QuarterNoteRest:
                {
                    if (ischord)
                    {
                        // get entire chord
                        var           lastnote    = GenericTokenHelper.GetNextToken_Skip(score, tokidx, false, MusicToken.MakeChord, MusicToken.Note, MusicToken.NoteScale);
                        var           lastnoteidx = lastnote == null ? score.Count - 1 : lastnote.id;
                        var           chord       = score.GetRange(tokidx, lastnoteidx - tokidx + 1).Where(n => (n.type == MusicToken.Note) || (n.type == MusicToken.NoteScale)).ToList();
                        List <string> info        = new List <string>();
                        for (int cn = 0; cn < chord.Count; cn++)
                        {
                            var note = chord[cn].data;
                            if (cn == 0)
                            {
                                note = note.ToUpper();
                            }
                            info.Add(note);
                        }
                        staff.Add(string.Join(string.Empty, info));
                        tokidx += lastnoteidx + 1;
                    }
                    else
                    {
                        var note = (tok.type == MusicToken.QuarterNoteRest) ? "," : tok.data;
                        staff.Add(note);
                        tokidx++;
                    }
                }
                break;

                default:
                    tokidx++;
                    break;
                }

                last = tok;
            }
            return(staff);
        }