void CreateNoteAnnotation(int pageIndex) { var pageRect = new RectF(180, 692, 212, 660); var contents = "This is note annotation was created from code."; var icon = NoteAnnotation.Cross; int color = Color.Green; // Create the annotation, and set the color. var noteAnnotation = new NoteAnnotation(pageIndex, pageRect, contents, icon); noteAnnotation.Color = color; fragment.AddAnnotationToPage(noteAnnotation, false); }
private void GenerateNoteAnnotations() { List <NoteAnnotation> annotations = new List <NoteAnnotation>(); for (int i = 0; i < Spectrogram.Frames.Count; i++) { Note[] frameNotes = Spectrogram.Frames[i].Notes; if (frameNotes == null) { for (int j = 0; j < annotations.Count; j++) { NoteAnnotation myNote = annotations[j]; if (myNote.startIndex == i - myNote.length) { myNote.length++; annotations[j] = myNote; } } continue; } for (int j = 0; j < frameNotes.Length; j++) { string noteName = frameNotes[j].Name + frameNotes[j].Octave; frameNotes[j].Magnitude /= Spectrogram.Frames[i].QuantisationScale; // Scales note correctly for spectrum display if (i > 0) { // Checks if current note is sustained from previous frame bool notePresent = false; for (int k = 0; k < annotations.Count; k++) { NoteAnnotation myNote = annotations[k]; if (myNote.name == noteName && myNote.startIndex == i - myNote.length) { myNote.length++; myNote.magnitude += frameNotes[j].Magnitude; notePresent = true; annotations[k] = myNote; break; } } if (notePresent) { continue; } } NoteAnnotation newNote = new NoteAnnotation(); newNote.name = noteName; newNote.startIndex = i; newNote.freq = frameNotes[j].Frequency; newNote.position = frameNotes[j].Position; newNote.length++; newNote.magnitude = frameNotes[j].Magnitude; annotations.Add(newNote); } Spectrogram.Frames[i].QuantisationScale = 1; } annotations = annotations.OrderBy(x => x.name).ToList(); List <int> indexToRemove = new List <int>(); int skip = 0; // Join notes that are close together into single longer note for (int i = 0; i < annotations.Count - 1; i++) { if (i + skip + 1 >= annotations.Count) { break; } NoteAnnotation myNote = annotations[i]; int nextIndex = i + skip + 1; double timeDifference = annotations[nextIndex].startIndex - (myNote.startIndex + myNote.length); if (timeDifference < Prefs.SPEC_NOTE_DIFF && annotations[nextIndex].name == myNote.name) { myNote.length += (int)timeDifference + annotations[nextIndex].length; myNote.magnitude += annotations[nextIndex].magnitude; annotations[i] = myNote; indexToRemove.Add(nextIndex); skip++; i--; } else { i += skip; skip = 0; } } for (int i = 0; i < indexToRemove.Count; i++) { annotations.RemoveAt(indexToRemove[i] - i); } // Remove short notes int index = 0; while (index < annotations.Count) { if (annotations[index].length < Prefs.SPEC_MIN_NOTE) { annotations.RemoveAt(index); } else { index++; } } // Create noteAnnotationMatrix double maxFreq; if (Spectrogram.FrequencyScale.GetType().Name == "Func`2") { Func <double, double> scale = (Func <double, double>)Spectrogram.FrequencyScale; maxFreq = scale(Spectrogram.Frames[0].SpectrumData.Length - 1); } else { maxFreq = Spectrogram.Frames[0].SpectrumData.Length * (double)Spectrogram.FrequencyScale; } int noteCount = Music.GetNoteIndexFromFrequency(maxFreq) + 1; NoteAnnotationMatrix = new NoteAnnotation[noteCount, Spectrogram.Frames.Count]; maxNoteMag = 0; // Populate matrix for (int i = 0; i < annotations.Count; i++) { NoteAnnotation a = annotations[i]; a.magnitude /= a.length; if (a.magnitude > maxNoteMag) { maxNoteMag = a.magnitude; } int noteIndex = Music.GetNoteIndexFromFrequency(a.freq); int fullLength = a.length; double initialIndex = a.startIndex; for (int j = 0; j < fullLength; j++) { a.length = fullLength - j; a.startIndex = initialIndex + j; NoteAnnotationMatrix[noteIndex, (int)a.startIndex] = a; } } }