internal static clsScore GetTopChordSeg(int[] inpercent, int[] basspercent, clsKeyTicks mkey, clsMTime.clsSegment segment, out string chordname, out List <int> chnotes) { int length = 0; foreach (int percent in inpercent) { if (percent > 0) { length++; } } //if (length <= 2) { if (length < (int)P.frmStart.nudMinChordSize.Value) //should be 0 unless advanced... { chordname = ""; chnotes = null; return(null); } List <clsScore> chlist = GetChordsSeg(inpercent, basspercent, mkey, segment); if (chlist.Count == 0) { chnotes = new List <int>(); chordname = ""; return(null); } chnotes = chlist[0].ChNotes; //highest score chordname = chlist[0].Desc; //chordname = ChordAnalysis.UTemplates[chlist[0].TIndex].Name; return(chlist[0]); }
internal bool?Compare(clsCFPC cf, int seg) { //* compare key from this to key from txtkey - return true if the same clsKeyTicks algkey = Keys[seg]; if (algkey == null) { return(null); } //algkey = algkey.Transpose(-cf.Transpose_File); int seglo = seg - ErrorMargin; int seghi = seg + ErrorMargin; for (int s = seglo; s <= seghi; s++) { if (s < 0 || s >= Keys.Count) { continue; } int ticks = new clsMTime.clsBBT(s, 0, 0).Ticks; //int ticks = cf.MTime.GetTicks(s); clsKeyTicks txtkey = P.F.Keys[ticks]; if (algkey.IsEquiv(txtkey)) { return(true); } } return(false); }
private clsChord EvToTxt(clsCFPC.clsEv ev) { //* get chord txt from ev ("null", "***", chord) clsChord chord; if (ev.Notes.Length == 0) { chord = new clsChord("null", 0); //null chord } else if (!ev.Root) { chord = new clsChord("***", 0); //non-recognisable chord } else { clsKeyTicks keyticks = P.F.Keys[ev.OnTime]; //int midikey = P.F.Keys[ev.OnTime].MidiKey; //current key if (Frm.TransposeChordNamesVal != 0) { keyticks = keyticks.GetTransposeNames(Frm.TransposeChordNamesVal); //midikey = keyticks.MidiKey; } int rootpitch = Frm.ChordTranspose(ev.Notes[0].PC[eKBTrans.None]); string root = NoteName.ToSharpFlat(NoteName.GetNoteName(keyticks, rootpitch)); root = root.Trim(); chord = new clsChord(root + ev.ChordQualifier, root.Length); } return(chord); }
internal int Agg; //aggregate score internal clsScore(int score, int tindex, int root, clsKeyTicks key, bool[] pc, int[] basspercent) { PC = pc; Score = score; TIndex = tindex; Root = root; Desc = USToTemplate.Values[tindex].Name; COFRootKeyDistance = NoteName.IntervalToCOF[Math.Abs(Root - key.KeyNote)]; for (int n = 0; n < 12; n++) { if (USToTemplate.Values[tindex].PC[n]) { ChNotes.Add((n + root).Mod12()); } } bool[] scalenotes = (key.Scale == "major") ? NoteName.MajScaleNotes : NoteName.MinScaleNotesAll; int noterel; for (int i = 0; i < 12; i++) { if (pc[i]) //note present in template chord //noterel = (i - key.KBTrans_KeyNote + 12).Mod12(); { noterel = (i - key.KeyNote).Mod12(); if (!scalenotes[i]) { ChromaticNotes++; } } } CalcAgg(score, basspercent, root); }
internal void Add(int midikey, string scale, int ticks) { //* called by loadmidifile if (Keys.Count > 0) { clsKeyTicks prevkey = Keys[Keys.Count - 1]; //if (ticks < prevkey.Ticks) throw new FatalException(); if (ticks < prevkey.Ticks) { LogicError.Throw(eLogicError.X081); return; } clsMTime.clsBBT bbt = new clsMTime.clsBBT(ticks); //should have (partial) mtime created by now if (bbt.TicksRemBeat != 0) { LogicError.Throw(eLogicError.X104, "key ticks not aligned to bar"); clsMTime.clsBBT bbtbar = new ChordCadenza.clsMTime.clsBBT(bbt.Bar, 0, 0); ticks = bbtbar.Ticks; } if (ticks == prevkey.Ticks) { //* replace key, but retain ticks Keys.RemoveAt(Keys.Count - 1); Keys.Add(new clsKeyTicks(midikey, scale, prevkey.Ticks)); return; } } Keys.Add(new clsKeyTicks(midikey, scale, ticks)); Finish(); }
internal clsManChords(clsKeyTicks key, bool playactual) { Key = key; PlayActual = playactual; CreatePlayChord(CMajorChord); SW.Start(); OffTimer.AutoReset = false; OffTimer.Elapsed += OffTimerEvent; }
internal clsKeyTicks this[int ticks, bool kbtrans] { get { clsKeyTicks key = Keys[GetIndex(ticks)]; if (key == null) { return(null); } return(new clsKeyTicks(key, kbtrans)); } }
internal int MidiKey; //-7 - +7 (eg Cmajor = Aminor = 0) internal bool IsEquiv(clsKeyTicks key) //should be called in preference to Equals(obj) { if (key == null) { return(false); } if (MidiKey == key.MidiKey && Scale == key.Scale) { return(true); } return(false); }
internal static string GetNoteNameOrRoman(int pitch, clsKeyTicks key) { if (P.frmSC.chkShowChordsRel.Checked) { //return GetDegree(pitch, key.KBTrans_KeyNote); return(GetDegree(pitch, key.KeyNote)); } else { return(NoteName.ToSharpFlat(NoteName._Names[key.MajMin, key.MidiKey + 7, pitch].TrimEnd())); } }
internal override void ShowPicBottom(Graphics xgr) { clsKeyTicks key = P.F.Keys[P.F.CurrentBBT.Ticks, kbtrans : true]; for (int pitch = frmSC.ShowLowPitch; pitch <= frmSC.ShowHighPitch; pitch++) { //if (NoteName.Diatonic[key.MidiKey + 7, key.MajMin].Substring(pitch.Mod12(), 1) == "1") { // frmSC.ShowBottomNote(xgr, pitch); if (NoteName.Diatonic[key.MidiKey + 7, key.MajMin].Substring(pitch.Mod12(), 1) == "1") { frmSC.ShowBottomNote(xgr, pitch); } } }
internal void Insert(clsKeyTicks key) //replace if ticks are equal { int index = GetIndex(key.Ticks); if (Keys[index].Ticks == key.Ticks) { Keys.RemoveAt(index); } else { index++; } Keys.Insert(index, key); Finish(); }
internal string ChordNameRoman(clsKeyTicks key, eKBTrans kbtrans) //eg I, vi, #IVo { if (Notes == null || Notes.Length == 0) { return("null"); } if (Notes.Length < 3 || !Root) { return("xxxx"); } //int rootpitch = (kbtrans) ? Notes[0].PC_KBTrans : Notes[0].PC_NoKBTrans; //string roman = NoteName.GetDegree(rootpitch, key.KBTrans_KeyNote); int rootpitch = Notes[0].PC[kbtrans]; string roman = NoteName.GetDegree(rootpitch, key.KeyNote); return(roman + ChordQualifier); }
private clsKeyTicks GetKey(int s, int keynote) { string scale = "major"; if (keynote > 11) { scale = "minor"; keynote -= 12; } int midikey = NoteName.PitchToMidiKey(keynote, scale, null); int ticks = new clsMTime.clsBBT(s, 0, 0).Ticks; //int ticks = mtime.TicksPerBeat * mtime.TSigNN * s; clsKeyTicks key = new clsKeyTicks(midikey, scale, ticks); //if (frmCalcKeys.optKeysText.Checked) return key.Transpose(-frmCalcKeys.CF.Transpose_File); return(key); }
internal static string GetNoteNameOrSolfa(int pitch, clsKeyTicks key) { if (P.frmSC.optShowSolfa.Checked) { //int keynote = key.KBTrans_KeyNote; int keynote = key.KeyNote; int offset = Math.Abs(((pitch - keynote).Mod12())) * 2; return(Solfa.Substring(offset, 2).TrimEnd()); } else if (P.frmSC.optShowNoteName.Checked) { return(ToSharpFlat(GetNoteName(key, pitch.Mod12()))); } else { return("*"); } }
private void WriteTraceFile(string name, List <int[]> table, string fmt, bool indkey) { string filename = name + "_" + Alg.ToString() + "(" + ChangePenalty + ").txt"; Stream xstream = new FileStream(@"D:\0\Sonar\Trace\" + filename, FileMode.Create, FileAccess.Write); //overwrite using (StreamWriter xsw = new StreamWriter(xstream)) { string line = " "; for (int s = 0; s < table.Count; s++) { line += string.Format(fmt, s + 1); } xsw.WriteLine(line); for (int kk = 0; kk < 24; kk++) { clsKeyTicks key = GetKey(0, kk); line = key.KeyStrShort; line = string.Format("{0, -4}", line); string ent; for (int s = 0; s < table.Count; s++) { if (table[s] == null) { ent = "***"; } else { if (indkey) { clsKeyTicks entkey = GetKey(s, table[s][kk]); ent = entkey.KeyStrShort; } else { ent = table[s][kk].ToString(); } } line += string.Format(fmt, ent); } xsw.WriteLine(line); } } //xsw.Close(); }
//internal static List<string> GetMatchingChordNames(bool[] inpc, int root) { // //* return matching chord names // int length = 0; // List<string> names = new List<string>(); // foreach (bool tf in inpc) if (tf) length++; // if (length < 3 || length > 4) return names; //empty list - only 3, 4 note chords checked // bool[] pc0 = new bool[12]; //pcs root C // for (int i = 0; i < 12; i++) { // if (inpc[i]) { // int p = (i - root).Mod12(); //root C // pc0[p] = true; // } // } // foreach (clsTemplate t in Templates) { // if (t.PC.SequenceEqual(pc0)) names.Add(t.Name); // } // return names; //} internal static clsScore GetTopChord(bool[] inpc, clsKeyTicks mkey, int maxtypenomatch, clsMTime.clsBBT bbt, out string chordname, out List <int> chnotes) { int length = 0; foreach (bool tf in inpc) { if (tf) { length++; } } if (length < 2) { chordname = ""; chnotes = null; return(null); } List <clsScore> chlist = GetChords(inpc, mkey, bbt); for (int i = 0; i < chlist.Count; i++) { //clsTemplate t = ChordAnalysis.Templates[chlist[i].TIndex]; clsTemplate t = ChordAnalysis.USToTemplate.Values[chlist[i].TIndex]; if (chlist[i].Score == t.Length) //exact match (but may not be unique) { chordname = t.Name; chnotes = chlist[i].ChNotes; return(chlist[i]); } if (t.Rank > maxtypenomatch) { continue; //no match and uncommon chord } chordname = t.Name; chnotes = chlist[i].ChNotes; return(chlist[i]); } chordname = ""; chnotes = null; return(null); }
private void TransKB(clsKeyTicks key, bool kbtrans) { TransposeNamesSharp = key.TransposeNamesSharp; Scale = key.Scale; Ticks = key.Ticks; if (kbtrans) { int trans = GetTransposeKB(); KeyNote = (trans == 0) ? key.KeyNote : (key.KeyNote + trans).Mod12(); MidiKey = (trans == 0) ? key.MidiKey : NoteName.PitchToMidiKey(KeyNote, Scale, null); //KeyNoteStr = (trans == 0) ? key.KeyNoteStr : NoteName._Names[MidiKey + 7][KeyNote]; KeyNoteStr = (trans == 0) ? key.KeyNoteStr : NoteName.GetName(this, KeyNote); } else { KeyNoteStr = key.KeyNoteStr; KeyNote = key.KeyNote; MidiKey = key.MidiKey; } }
internal static void ShowChord(clsPlay.clsChordEv playchord, clsKeyTicks key) { if (key == null) { key = new ChordCadenza.clsKeyTicks(0, "major", 0); } int[] tchord; if (playchord == null || playchord.Chord == null) { tchord = null; } else { tchord = playchord.Chord.ToArray(); for (int i = 0; i < tchord.Length; i++) { tchord[i] = (tchord[i] + P.frmSC.Play.TransposeKB).Mod12(); } } P.frmSC.BeginInvoke(new delegShowChord(ShowChordForm), tchord, key); }
internal static int[] GetScaleChord(clsKeyTicks key) { //return 7 notes of scale for key, starting at doh int[] ret = new int[7]; string diatonic = Diatonic[key.MidiKey + 7, key.MajMin]; int idia = key.KeyNote - 1; for (int i = 0; i < 7; i++) { string val; do //get next "1" in Diatonic[key, majmin] { if (++idia == 12) { idia = 0; } val = diatonic.Substring(idia, 1); } while (val == "0"); ret[i] = idia; } return(ret); }
internal static string ShowChordText(int[] chord, clsKeyTicks key) { //* form thread //* show current playchord (root and qualifier) if (chord == null || chord.Length < 3) { //P.frmSC.txtChordBottom.Text = ""; return(""); } bool[] pcs = new bool[12]; foreach (int c in chord) { pcs[c] = true; } int rootpc = chord[0]; //List<string> names = ChordAnalysis.GetMatchingChordNames(pcs, rootpc); //string qualifier = (names.Count == 0) ? "xxx" : names[0]; string qualifier = ChordAnalysis.GetName(pcs); //string txt = NoteName.ToSharpFlat(NoteName._Names[key.MidiKey + 7][rootpc].TrimEnd()) + qualifier; string txt = NoteName.ToSharpFlat(NoteName.GetName(key, rootpc).TrimEnd()) + qualifier; return(txt); }
// public IEnumerator<clsKey> GetEnumerator() { // for (int i = 0; i < Keys.Count; i++) { // yield return Keys[i]; // } //} //System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { // return GetEnumerator(); //} //internal clsKeysTicks(clsKeysTicks keys) { //clone // foreach (clsKeyTicks key in keys.Keys) Keys.Add(key); //} internal clsKeysTicks(clsKeyTicks key) { Keys.Add(key); }
private static void ShowChordForm(int[] chord, clsKeyTicks key) { P.frmSC.txtChordBottom.Text = ShowChordText(chord, key); }
internal static List <clsScore> GetChordsSeg(int[] inpercent, int[] basspercent, clsKeyTicks mkey, clsMTime.clsSegment segment) { //* inpc = segment weights for each pitchclass (0 = not present, 100 = occupies whole segment //* calculate scores //* segment param only used by dumpscores List <clsScore> scores = new List <clsScore>(); bool[] prevpc = new bool[12]; int adder = (int)P.frmCfgChords.nudAdder.Value; //high value favours 3-note chords... int factor = (int)P.frmCfgChords.nudChordFactor.Value; //high value favours notes present for (int t = 0; t < USToTemplate.Count; t++) //each possible chord (template) { if (USToTemplate.Values[t].Rank > MaxType) { continue; } if (USToTemplate.Values[t].Length > MaxNotes) { continue; } if (USToTemplate.Values[t].PC.SequenceEqual(prevpc)) { continue; } for (int r = 0; r < 12; r++) //for each root { int score = 0; for (int p = 0; p < 12; p++) //each pitchclass { int pp = p - r; if (pp < 0) { pp += 12; } //* calc score for this t/r/p //* range: -50 to 50 //if (Templates[t].PC[pp]) score += factor * inpc[p] - adder; else score += adder - inpc[p]; if (USToTemplate.Values[t].PC[pp]) { score += (factor * inpercent[p]) / 10 - adder; } else { score += adder - inpercent[p]; } } //* calc template pc relative to current key bool[] temppc = new bool[12]; for (int i = 0; i < 12; i++) { if (USToTemplate.Values[t].PC[i]) { int tempi = (i + r - mkey.KeyNote).Mod12(); temppc[tempi] = true; } } clsScore newscore = new clsScore(score, t, r, mkey, temppc, basspercent); scores.Add(newscore); } prevpc = USToTemplate.Values[t].PC; } if (scores.Count == 0) { return(new List <clsScore>()); } scores.Sort(new SimpleScoreComparer()); //simple sort (score only) scores.Reverse(); //highest score first #if (DEBUG && DumpTopScores) clsMTime.clsBBT bbt = new clsMTime.clsBBT(segment.SegQILo * P.F.TicksPerQI); DumpScores(XSWDump, -1, mkey, bbt, scores, ""); #endif //* create list of top scores, sorted by other criteria List <clsScore> topscores = new List <clsScore>(9); topscores.Add(scores[0]); //top score for (int i = 1; i < scores.Count; i++) //start at second element { if (scores[i].Score <= scores[0].Score - (int)P.frmCfgChords.nudScoreRange.Value) { break; } topscores.Add(scores[i]); } //topscores.Sort(new SecondaryScoreComparer()); //secondary fields, then score topscores.Sort(new AggScoreComparer()); //aggregate score topscores.Reverse(); if (P.frmCfgChords.chkRemoveDups.Checked) { topscores = RemoveDups(topscores); } #if (DEBUG && DumpTopScores) DumpScores(XSWDumpTop, -1, mkey, bbt, topscores, ""); #endif return(topscores); }
//#if CheckDups // internal static clsScore CheckDups(List<clsScore> scores, clsScore newscore) { // bool[] newpc = newscore.PC; // for (int i = 0; i < scores.Count; i++) { // bool[] pc = scores[i].PC; // if (pc.SequenceEqual(newpc)) return scores[i]; // } // return null; // } //#endif internal static List <clsScore> GetChords(bool[] inpc, clsKeyTicks mkey, clsMTime.clsBBT bbt) { //* return scores etc. of nearest matching chords //int[,] score = new int[Templates.Length, 12]; //[template,root] //int maxscore = int.MinValue; string dia = NoteName.Diatonic[mkey.MidiKey + 7, mkey.MajMin]; //******************** should above line be KBTrans??? List <clsScore> ret = new List <clsScore>(); bool[] dummy = new bool[12]; //* calculate scores for (int t = 0; t < USToTemplate.Count; t++) //each possible chord (template) { if (USToTemplate.Values[t].Rank > MaxType) { continue; } if (USToTemplate.Values[t].Length > MaxNotes) { continue; } for (int r = 0; r < 12; r++) //for each root { int score = 0; for (int p = 0; p < 12; p++) //each pitchclass in inchord { int pp = p - r; if (pp < 0) { pp += 12; } if (inpc[p] == USToTemplate.Values[t].PC[pp]) { if (inpc[p]) { score++; //hit } } else { if (inpc[p]) { score--; //note present in inchord, but not in template } else //note present in template, but not inchord { score--; if (dia.Substring(p, 1) == "0") { score--; //missing template note not diatonic } } } } ret.Add(new clsScore(score, t, r, mkey, dummy, new int[0])); //dups not monitored } } ret.Sort(); ret.Reverse(); //highest score first #if (DEBUG && DumpTopScores) if (bbt != null) { //* print InChord/Key info string inchordstr = ""; for (int i = 0; i < 12; i++) { if (inpc[i]) { inchordstr += GetNoteName(i, mkey) + " "; } } DumpScores(XSWDump, 10, mkey, bbt, ret, inchordstr); } #endif return(ret); }
internal void Change(int index, clsKeyTicks key) { Keys[index] = key; Finish(); }
internal clsKeyTicks(clsKeyTicks key, bool kbtrans) //clone and transpose { TransKB(key, kbtrans); }
internal clsKeyTicks(clsKeyTicks key) //clone { TransKB(key, false); }