CreateNoteData(List <MidiNote> midinotes, KeySignature key, TimeSignature time) { int len = midinotes.Count; NoteData[] notedata = new NoteData[len]; for (int i = 0; i < len; i++) { MidiNote midi = midinotes[i]; notedata[i] = new NoteData(); notedata[i].number = midi.Number; notedata[i].leftside = true; notedata[i].whitenote = key.GetWhiteNote(midi.Number); notedata[i].duration = time.GetNoteDuration(midi.EndTime - midi.StartTime); notedata[i].accid = key.GetAccidental(midi.Number, midi.StartTime / time.Measure); if (i > 0 && (notedata[i].whitenote.Dist(notedata[i - 1].whitenote) == 1)) { /* This note (notedata[i]) overlaps with the previous note. * Change the side of this note. */ if (notedata[i - 1].leftside) { notedata[i].leftside = false; } else { notedata[i].leftside = true; } } else { notedata[i].leftside = true; } } return(notedata); }
public void TestGetAccidentalSameMeasure() { KeySignature k; /* G Major, F# */ k = new KeySignature(1, 0); int note = NoteScale.ToNumber(NoteScale.C, 1); Assert.AreEqual(k.GetWhiteNote(note).Letter, WhiteNote.C); Assert.AreEqual(k.GetAccidental(note, 1), Accid.None); Assert.AreEqual(k.GetWhiteNote(note).Letter, WhiteNote.C); Assert.AreEqual(k.GetAccidental(note, 1), Accid.None); note = NoteScale.ToNumber(NoteScale.Fsharp, 1); Assert.AreEqual(k.GetWhiteNote(note).Letter, WhiteNote.F); Assert.AreEqual(k.GetAccidental(note, 1), Accid.None); Assert.AreEqual(k.GetWhiteNote(note).Letter, WhiteNote.F); Assert.AreEqual(k.GetAccidental(note, 1), Accid.None); note = NoteScale.ToNumber(NoteScale.F, 1); Assert.AreEqual(k.GetWhiteNote(note).Letter, WhiteNote.F); Assert.AreEqual(k.GetAccidental(note, 1), Accid.Natural); Assert.AreEqual(k.GetWhiteNote(note).Letter, WhiteNote.F); Assert.AreEqual(k.GetAccidental(note, 1), Accid.None); note = NoteScale.ToNumber(NoteScale.Fsharp, 1); Assert.AreEqual(k.GetWhiteNote(note).Letter, WhiteNote.F); Assert.AreEqual(k.GetAccidental(note, 1), Accid.Sharp); Assert.AreEqual(k.GetWhiteNote(note).Letter, WhiteNote.F); Assert.AreEqual(k.GetAccidental(note, 1), Accid.None); note = NoteScale.ToNumber(NoteScale.Bflat, 1); Assert.AreEqual(k.GetWhiteNote(note).Letter, WhiteNote.B); Assert.AreEqual(k.GetAccidental(note, 1), Accid.Flat); Assert.AreEqual(k.GetWhiteNote(note).Letter, WhiteNote.B); Assert.AreEqual(k.GetAccidental(note, 1), Accid.None); note = NoteScale.ToNumber(NoteScale.A, 1); Assert.AreEqual(k.GetWhiteNote(note).Letter, WhiteNote.A); Assert.AreEqual(k.GetAccidental(note, 1), Accid.None); note = NoteScale.ToNumber(NoteScale.B, 1); Assert.AreEqual(k.GetWhiteNote(note).Letter, WhiteNote.B); Assert.AreEqual(k.GetAccidental(note, 1), Accid.Natural); Assert.AreEqual(k.GetWhiteNote(note).Letter, WhiteNote.B); Assert.AreEqual(k.GetAccidental(note, 1), Accid.None); note = NoteScale.ToNumber(NoteScale.Bflat, 1); Assert.AreEqual(k.GetWhiteNote(note).Letter, WhiteNote.B); Assert.AreEqual(k.GetAccidental(note, 1), Accid.Flat); /* F Major, Bflat */ k = new KeySignature(0, 1); note = NoteScale.ToNumber(NoteScale.G, 1); Assert.AreEqual(k.GetWhiteNote(note).Letter, WhiteNote.G); Assert.AreEqual(k.GetAccidental(note, 1), Accid.None); Assert.AreEqual(k.GetWhiteNote(note).Letter, WhiteNote.G); Assert.AreEqual(k.GetAccidental(note, 1), Accid.None); note = NoteScale.ToNumber(NoteScale.Bflat, 1); Assert.AreEqual(k.GetWhiteNote(note).Letter, WhiteNote.B); Assert.AreEqual(k.GetAccidental(note, 1), Accid.None); Assert.AreEqual(k.GetWhiteNote(note).Letter, WhiteNote.B); Assert.AreEqual(k.GetAccidental(note, 1), Accid.None); note = NoteScale.ToNumber(NoteScale.B, 1); Assert.AreEqual(k.GetWhiteNote(note).Letter, WhiteNote.B); Assert.AreEqual(k.GetAccidental(note, 1), Accid.Natural); Assert.AreEqual(k.GetWhiteNote(note).Letter, WhiteNote.B); Assert.AreEqual(k.GetAccidental(note, 1), Accid.None); note = NoteScale.ToNumber(NoteScale.Bflat, 1); Assert.AreEqual(k.GetWhiteNote(note).Letter, WhiteNote.B); Assert.AreEqual(k.GetAccidental(note, 1), Accid.Flat); Assert.AreEqual(k.GetWhiteNote(note).Letter, WhiteNote.B); Assert.AreEqual(k.GetAccidental(note, 1), Accid.None); note = NoteScale.ToNumber(NoteScale.Fsharp, 1); Assert.AreEqual(k.GetWhiteNote(note).Letter, WhiteNote.F); Assert.AreEqual(k.GetAccidental(note, 1), Accid.Sharp); Assert.AreEqual(k.GetWhiteNote(note).Letter, WhiteNote.F); Assert.AreEqual(k.GetAccidental(note, 1), Accid.None); note = NoteScale.ToNumber(NoteScale.G, 1); Assert.AreEqual(k.GetWhiteNote(note).Letter, WhiteNote.G); Assert.AreEqual(k.GetAccidental(note, 1), Accid.None); note = NoteScale.ToNumber(NoteScale.F, 1); Assert.AreEqual(k.GetWhiteNote(note).Letter, WhiteNote.F); Assert.AreEqual(k.GetAccidental(note, 1), Accid.Natural); Assert.AreEqual(k.GetWhiteNote(note).Letter, WhiteNote.F); Assert.AreEqual(k.GetAccidental(note, 1), Accid.None); note = NoteScale.ToNumber(NoteScale.Fsharp, 1); Assert.AreEqual(k.GetWhiteNote(note).Letter, WhiteNote.F); Assert.AreEqual(k.GetAccidental(note, 1), Accid.Sharp); }
/** Given the raw midi notes (the note number and duration in pulses), * calculate the following note data: * - The white key * - The accidental (if any) * - The note duration (half, quarter, eighth, etc) * - The side it should be drawn (left or side) * By default, notes are drawn on the left side. However, if two notes * overlap (like A and B) you cannot draw the next note directly above it. * Instead you must shift one of the notes to the right. * * The KeySignature is used to determine the white key and accidental. * The TimeSignature is used to determine the duration. */ private static NoteData[] CreateNoteData(List<MidiNote> midinotes, KeySignature key, TimeSignature time) { int len = midinotes.Count; NoteData[] notedata = new NoteData[len]; for (int i = 0; i < len; i++) { MidiNote midi = midinotes[i]; notedata[i] = new NoteData(); notedata[i].number = midi.Number; notedata[i].leftside = true; notedata[i].whitenote = key.GetWhiteNote(midi.Number); notedata[i].duration = time.GetNoteDuration(midi.EndTime - midi.StartTime); notedata[i].accid = key.GetAccidental(midi.Number, midi.StartTime / time.Measure); if (i > 0 && (notedata[i].whitenote.Dist(notedata[i-1].whitenote) == 1)) { /* This note (notedata[i]) overlaps with the previous note. * Change the side of this note. */ if (notedata[i-1].leftside) { notedata[i].leftside = false; } else { notedata[i].leftside = true; } } else { notedata[i].leftside = true; } } return notedata; }
public void TestGetAccidental() { int measure = 1; KeySignature k; Accid[] expected = new Accid[12]; for (int i = 0; i < 12; i++) { expected[i] = Accid.None; } expected[NoteScale.Bflat] = Accid.Flat; expected[NoteScale.Csharp] = Accid.Sharp; expected[NoteScale.Dsharp] = Accid.Sharp; expected[NoteScale.Fsharp] = Accid.Sharp; expected[NoteScale.Gsharp] = Accid.Sharp; /* Test C Major */ k = new KeySignature(0, 0); measure = 1; for (int note = 1; note < 128; note++) { int notescale = NoteScale.FromNumber(note); Assert.AreEqual(expected[notescale], k.GetAccidental(note, measure)); measure++; } /* Test G major, F# */ k = new KeySignature(1, 0); measure = 1; expected[NoteScale.Fsharp] = Accid.None; expected[NoteScale.F] = Accid.Natural; for (int note = 1; note < 128; note++) { int notescale = NoteScale.FromNumber(note); Assert.AreEqual(expected[notescale], k.GetAccidental(note, measure)); measure++; } /* Test D major, F#, C# */ k = new KeySignature(2, 0); measure = 1; expected[NoteScale.Csharp] = Accid.None; expected[NoteScale.C] = Accid.Natural; for (int note = 1; note < 128; note++) { int notescale = NoteScale.FromNumber(note); Assert.AreEqual(expected[notescale], k.GetAccidental(note, measure)); measure++; } /* Test A major, F#, C#, G# */ k = new KeySignature(3, 0); measure = 1; expected[NoteScale.Gsharp] = Accid.None; expected[NoteScale.G] = Accid.Natural; for (int note = 1; note < 128; note++) { int notescale = NoteScale.FromNumber(note); Assert.AreEqual(expected[notescale], k.GetAccidental(note, measure)); measure++; } /* Test E major, F#, C#, G#, D# */ k = new KeySignature(4, 0); measure = 1; expected[NoteScale.Dsharp] = Accid.None; expected[NoteScale.D] = Accid.Natural; for (int note = 1; note < 128; note++) { int notescale = NoteScale.FromNumber(note); Assert.AreEqual(expected[notescale], k.GetAccidental(note, measure)); measure++; } /* Test B major, F#, C#, G#, D#, A# */ k = new KeySignature(5, 0); measure = 1; expected[NoteScale.Asharp] = Accid.None; expected[NoteScale.A] = Accid.Natural; for (int note = 1; note < 128; note++) { int notescale = NoteScale.FromNumber(note); Assert.AreEqual(expected[notescale], k.GetAccidental(note, measure)); measure++; } for (int i = 0; i < 12; i++) { expected[i] = Accid.None; } expected[NoteScale.Aflat] = Accid.Flat; expected[NoteScale.Bflat] = Accid.Flat; expected[NoteScale.Csharp] = Accid.Sharp; expected[NoteScale.Eflat] = Accid.Flat; expected[NoteScale.Fsharp] = Accid.Sharp; /* Test F major, Bflat */ k = new KeySignature(0, 1); measure = 1; expected[NoteScale.Bflat] = Accid.None; expected[NoteScale.B] = Accid.Natural; for (int note = 1; note < 128; note++) { int notescale = NoteScale.FromNumber(note); Assert.AreEqual(expected[notescale], k.GetAccidental(note, measure)); measure++; } /* Test Bflat major, Bflat, Eflat */ k = new KeySignature(0, 2); measure = 1; expected[NoteScale.Eflat] = Accid.None; expected[NoteScale.E] = Accid.Natural; for (int note = 1; note < 128; note++) { int notescale = NoteScale.FromNumber(note); Assert.AreEqual(expected[notescale], k.GetAccidental(note, measure)); measure++; } /* Test Eflat major, Bflat, Eflat, Afat */ k = new KeySignature(0, 3); measure = 1; expected[NoteScale.Aflat] = Accid.None; expected[NoteScale.A] = Accid.Natural; expected[NoteScale.Dflat] = Accid.Flat; for (int note = 1; note < 128; note++) { int notescale = NoteScale.FromNumber(note); Assert.AreEqual(expected[notescale], k.GetAccidental(note, measure)); measure++; } /* Test Aflat major, Bflat, Eflat, Aflat, Dflat */ k = new KeySignature(0, 4); measure = 1; expected[NoteScale.Dflat] = Accid.None; expected[NoteScale.D] = Accid.Natural; for (int note = 1; note < 128; note++) { int notescale = NoteScale.FromNumber(note); Assert.AreEqual(expected[notescale], k.GetAccidental(note, measure)); measure++; } /* Test Dflat major, Bflat, Eflat, Aflat, Dflat, Gflat */ k = new KeySignature(0, 5); measure = 1; expected[NoteScale.Gflat] = Accid.None; expected[NoteScale.G] = Accid.Natural; for (int note = 1; note < 128; note++) { int notescale = NoteScale.FromNumber(note); Assert.AreEqual(expected[notescale], k.GetAccidental(note, measure)); measure++; } }