Exemplo n.º 1
0
        private void convertProgression()
        {
            int    transposeKey = 0;
            string str          = TextBox_ForConvert.Text;

            str = removeTags(str);
            string[]             lines             = str.Split('\n');
            List <List <Chord> > chordProgressions = new List <List <Chord> >();

            for (int i = 0; i < lines.Length; i++)
            {
                string       line        = lines[i];
                string[]     del         = { "|", " ", "\t", "\r" };
                string[]     items       = line.Split(del, StringSplitOptions.RemoveEmptyEntries);
                string       lastItem    = "";
                List <Chord> progression = new List <Chord>();
                for (int j = 0; j < items.Length; j++)
                {
                    string item = items[j];
                    if (item == "%")
                    {
                        item = lastItem;
                    }
                    if (item.Contains("key") || item.Contains("Key"))
                    {
                        var matchKeys = ChordBasic.toneList.FindAll(m => item.Contains(m.name) || (m.subname.Length > 0 && item.Contains(m.subname)));
                        if (matchKeys.Count > 0)
                        {
                            int cur    = 0;
                            int maxlen = 0;
                            for (int key = 0; key < matchKeys.Count; key++)
                            {
                                if (item.Contains(matchKeys[key].name))
                                {
                                    if (maxlen < matchKeys[key].name.Length)
                                    {
                                        maxlen = matchKeys[key].name.Length;
                                        cur    = key;
                                    }
                                }
                                if (item.Contains(matchKeys[key].subname))
                                {
                                    if (maxlen < matchKeys[key].subname.Length)
                                    {
                                        maxlen = matchKeys[key].subname.Length;
                                        cur    = key;
                                    }
                                }
                            }

                            transposeKey = matchKeys[cur].id;
                            ConvertWrite("Transpose to " + matchKeys[cur].name + " with " + item + "\n");
                        }
                    }
                    else
                    {
                        var res = Chord.createChordFromChordName(item);
                        if (res == null)
                        {
                            ConvertWrite(item + " cannot translate\n");
                        }
                        else
                        {
                            res.transpose((ChordBasic.toneCount - transposeKey) % ChordBasic.toneCount);
                            progression.Add(res);
                        }
                    }
                    lastItem = item;
                }
                if (progression.Count > 0)
                {
                    chordProgressions.Add(progression);
                }
            }
            ConvertWrite("Text converted\n");

            string resultString = "";

            for (int i = 0; i < chordProgressions.Count; i++)
            {
                for (int j = 0; j < chordProgressions[i].Count; j++)
                {
                    resultString += chordProgressions[i][j].ToString();
                    if (j + 1 != chordProgressions[i].Count)
                    {
                        resultString += "\t";
                    }
                }
                resultString += "\n";
            }
            TextBox_Converted.Text = resultString;
        }
Exemplo n.º 2
0
        public int[] chordToNotes(Chord chord, int transpose)
        {
            List <int> chordNotes = chord.noteNumbers.ToList();

            for (int i = 0; i < chordNotes.Count; i++)
            {
                chordNotes[i] += transpose;
            }
            int chordRoot = chordNotes[0];
            int keyRoot   = chord.root.id + transpose + ChordBasic.toneList[0].noteNumber;
            int oct       = ChordBasic.toneCount;

            List <int> baseNotes = new List <int>();
            int        baseNote  = chord.baseNoteNumber + transpose - oct;

            // add base notes
            if (baseNotePolicy == BaseNotePolicy._1Oct)
            {
                baseNotes.Add(baseNote);
            }
            if (baseNotePolicy == BaseNotePolicy._1Oct_5th)
            {
                baseNotes.Add(baseNote); baseNotes.Add(baseNote + 7);
            }
            if (baseNotePolicy == BaseNotePolicy._2Oct)
            {
                baseNotes.Add(baseNote); baseNotes.Add(baseNote - oct);
            }
            if (baseNotePolicy == BaseNotePolicy._2Oct_5th)
            {
                baseNotes.Add(baseNote); baseNotes.Add(baseNote - oct); baseNotes.Add(baseNote + 7);
            }

            // keep note and extend
            List <int> rtn = new List <int>();

            // arrange chord notes
            if (nearToPolicy != NearToPolicy._Nothing && lastChordCenter != 0)
            {
                if (lastChordCenter != 0)
                {
                    for (int i = 0; i < chordNotes.Count; i++)
                    {
                        while (Math.Abs(chordNotes[i] - lastChordCenter) > oct / 2)
                        {
                            chordNotes[i] -= Math.Sign(chordNotes[i] - lastChordCenter) * oct;
                        }
                    }
                    if (nearToPolicy == NearToPolicy._Previous)
                    {
                        lastChordCenter = chordNotes.Sum() * 1.0 / chordNotes.Count;
                    }
                }
                rtn.AddRange(chordNotes);
            }
            else
            {
                if (keepNotePolicy == KeepNotePolicy._Nothing)
                {
                    // nothing
                }
                if (keepNotePolicy == KeepNotePolicy._KeepKeyRoot)
                {
                    for (int i = 0; i < chordNotes.Count; i++)
                    {
                        while (Math.Abs(chordNotes[i] - keyRoot) > oct / 2)
                        {
                            chordNotes[i] -= Math.Sign(chordNotes[i] - keyRoot) * oct;
                        }
                    }
                }
                if (keepNotePolicy == KeepNotePolicy._KeepChordRoot)
                {
                    for (int i = 0; i < chordNotes.Count; i++)
                    {
                        while (Math.Abs(chordNotes[i] - chordRoot) > oct / 2)
                        {
                            chordNotes[i] -= Math.Sign(chordNotes[i] - chordRoot) * oct;
                        }
                    }
                }
                if (keepNotePolicy == KeepNotePolicy._KeepRootTop)
                {
                    chordNotes[0] += oct;
                }
                lastChordCenter = chordNotes.Sum() * 1.0 / chordNotes.Count;
                rtn.AddRange(chordNotes);
            }

            // rotate chord
            rtn = rtn.Distinct().ToList();
            rtn.Sort();
            if (expandToPolicy == ExpandToPolicy._Lower)
            {
                for (int i = rtn.Count - 1; i >= 1; i--)
                {
                    if (rtn[i] - rtn[i - 1] < minimumInterval)
                    {
                        rtn[i - 1] -= oct;
                        rtn.Sort();
                        i++;
                    }
                }
            }
            else if (expandToPolicy == ExpandToPolicy._Upper)
            {
                for (int i = 0; i + 1 < rtn.Count; i++)
                {
                    if (rtn[i + 1] - rtn[i] < minimumInterval)
                    {
                        rtn[i + 1] += oct;
                        rtn.Sort();
                        i--;
                    }
                }
            }
            else
            {
                for (int i = 0; i + 1 < rtn.Count; i++)
                {
                    if (rtn[i + 1] - rtn[i] < minimumInterval && rtn[i + 1] >= chordRoot)
                    {
                        rtn[i + 1] += oct;
                        rtn.Sort();
                        i++;
                    }
                }
                for (int i = rtn.Count - 1; i >= 1; i--)
                {
                    if (rtn[i] - rtn[i - 1] < minimumInterval && rtn[i - 1] <= chordRoot)
                    {
                        rtn[i - 1] -= oct;
                        rtn.Sort();
                        i--;
                    }
                }
            }

            if (baseNearToPolicy == BaseNearToPolicy._Chord)
            {
                lastBase = lastChordCenter - oct;
            }
            // arrange base notes
            if (baseNotes.Count > 0)
            {
                if (baseNearToPolicy != BaseNearToPolicy._Nothing && lastBase != 0)
                {
                    while (Math.Abs(baseNotes[0] - lastBase) > oct / 2)
                    {
                        for (int i = 0; i < baseNotes.Count; i++)
                        {
                            baseNotes[i] -= Math.Sign(baseNotes[i] - lastBase) * oct;
                        }
                    }
                    if (baseNearToPolicy == BaseNearToPolicy._Previous)
                    {
                        lastBase = baseNotes[0];
                    }
                    rtn.AddRange(baseNotes);
                }
                else
                {
                    if (keepNotePolicy == KeepNotePolicy._Nothing)
                    {
                        // nothing
                    }
                    if (keepNotePolicy == KeepNotePolicy._KeepKeyRoot)
                    {
                        while (Math.Abs(baseNotes[0] - (keyRoot - oct)) > oct / 2)
                        {
                            for (int i = 0; i < baseNotes.Count; i++)
                            {
                                baseNotes[i] -= Math.Sign(baseNotes[i] - (keyRoot - oct)) * oct;
                            }
                        }
                    }
                    if (keepNotePolicy == KeepNotePolicy._KeepChordRoot)
                    {
                        while (Math.Abs(baseNotes[0] - (chordRoot - oct)) > oct / 2)
                        {
                            for (int i = 0; i < baseNotes.Count; i++)
                            {
                                baseNotes[i] -= Math.Sign(baseNotes[i] - (chordRoot - oct)) * oct;
                            }
                        }
                    }
                    if (keepNotePolicy == KeepNotePolicy._KeepRootTop)
                    {
                    }
                    lastBase = baseNotes[0];
                    rtn.AddRange(baseNotes);
                }
            }
            return(rtn.Distinct().ToArray());
        }
Exemplo n.º 3
0
 private void Callback_NoteOffButtonClicked(object sender, MouseButtonEventArgs e)
 {
     currentChord = null;
     VoicingManage.getInstance().resetNearTo();
     noteOff();
 }