private static List <D_Note> get_notes_from_track(Track track, D_Staff staff, int ticks_per_beat) { List <D_Note> notes = new List <D_Note>(); MidiEvent previous_midi_event = null; List <Tuple <MidiEvent, ChannelMessage> > channel_messages = getChannelMessages(track); foreach (Tuple <MidiEvent, ChannelMessage> channel_pair in channel_messages) { MidiEvent midiEvent = channel_pair.Item1; ChannelMessage channelMessage = channel_pair.Item2; if (previous_midi_event != null) { D_Note note = get_note(midiEvent, channelMessage, previous_midi_event, ticks_per_beat); if (note != null) { notes.Add(get_note(midiEvent, channelMessage, previous_midi_event, ticks_per_beat)); } } previous_midi_event = midiEvent; } return(notes); }
public static D_Note noteFromToken(String token, int current_scope_octave, D_Note previous_note) { NoteLevel noteLevel = noteLevelDictionary[token[0]]; int length_in_sixteenths = 16 / StringUtil.getNumberFromString(token); if (token.Contains('.')) { length_in_sixteenths = (int)((double)length_in_sixteenths * 1.5); } if (noteLevel == NoteLevel.rest) { return(D_NoteFactory.create_rest(length_in_sixteenths)); } else { NoteAlteration alteration = getNoteNoteAlteration(token); int octave = getNoteOctave(token, current_scope_octave); D_Note new_note = D_NoteFactory.create_note(noteLevel, alteration, octave, length_in_sixteenths); if (previous_note != null) { setRelativeOctave(new_note, previous_note); } return(new_note); } }
private void set_measures(D_Staff staff, List <string> tokens) { // This function requires staff.num_of_beats to be set int current_token_index = 0; int token_end_index = tokens.Count - 1; double current_beat = 0; while (current_token_index <= token_end_index) { string current_token = tokens[current_token_index]; // Special case, when we encounter \relative, skip a token if (current_token == "\\relative") { current_token_index++; } else if (isNote(current_token)) { // We don't care about octaves here so we pass 0 and no previous note // All we care about is note length so that we may increase current_beat D_Note note = LilypondNoteParser.noteFromToken(current_token, 0, null); current_beat += ((double)note.length / (double)4); } else if (current_token == "\\time") { string time_signature = tokens[current_token_index + 1]; Tuple <int, int> time_signature_tuple = StringUtil.getMeasureFromString(time_signature); staff.addMeasure(time_signature_tuple.Item1, time_signature_tuple.Item2, (int)current_beat); } current_token_index++; } staff.setMeasureEndTimes(); }
public D_Note(D_Note other, int new_length) { this.level = other.level; this.length = new_length; this.octave = other.octave; this.alteration = other.alteration; this.is_rest = other.is_rest; this.length_modifier = has_length_modifier(this.length); }
static public MusicalSymbol fromNote(D_Note note) { if (!note.is_rest) { return(getNoteMusicalSymbol(note)); } else { return(getRestMusicalSymbol(note)); } }
public static Tuple <D_Note, D_Note> splitNote(D_Note note_to_split, int first_note_length) { int second_note_length = note_to_split.length - first_note_length; D_Note first = D_NoteFactory.create_note(note_to_split, first_note_length); first.note_tie = NoteTie.start; D_Note second = D_NoteFactory.create_note(note_to_split, second_note_length); second.note_tie = NoteTie.stop; return(new Tuple <D_Note, D_Note>(first, second)); }
public static bool newNoteOctaveChange(D_Note start, int step) { int i_old = noteLevelInt[start.level]; if (step < 0 && i_old + step < min_index) { return(true); } else if (step > 0 && i_old + step > max_index) { return(true); } return(false); }
private static void setRelativeOctave(D_Note new_note, D_Note old_note) { int step = LilypondUtil.getClosestNotePositionInSteps(old_note, new_note); if (LilypondUtil.newNoteOctaveChange(old_note, step)) { if (step < 0) { new_note.octave--; } else if (step > 0) { new_note.octave++; } } }
private List <D_Note> get_notes(List <string> tokens, int current_scope_octave) { // This does not take into account repeats int current_token_index = 0; int token_end_index = tokens.Count - 1; List <D_Note> notes = new List <D_Note>(); D_Note previous_note = null; while (current_token_index <= token_end_index) { string current_token = tokens[current_token_index]; // Special case, when we encounter \relative, skip a token if (current_token == "\\relative") { current_token_index++; } else if (isNote(current_token)) { D_Note note = LilypondNoteParser.noteFromToken(current_token, current_scope_octave, previous_note); if (!note.is_rest) { current_scope_octave = note.octave; } if (current_token_index + 1 <= token_end_index && tokens[current_token_index + 1] == "~") { note.note_tie = NoteTie.start; } else if (current_token_index > 1 && tokens[current_token_index - 2] == "~") { note.note_tie = NoteTie.stop; } notes.Add(note); if (!note.is_rest) { previous_note = note; } } current_token_index++; } return(notes); }
// This returns a overflowed note if present public D_Note addNote(D_Note note) { int overflow = note.length - this.spaceLeft(); if (overflow > 0) { // split Tuple <D_Note, D_Note> split_result = D_Note.splitNote(note, this.spaceLeft()); this.notes.Add(split_result.Item1); return(split_result.Item2); } else { this.notes.Add(note); return(null); } }
static private MusicalSymbol getNoteMusicalSymbol(D_Note note) { String note_level = getNoteLevel(note.level); int note_alteration = getNoteAlteration(note.alteration); int octave = note.octave; int numberOfDots = getNumberOfDots(note.length_modifier); MusicalSymbolDuration duration = getNoteDuration(getVisualLength(note.length, note.length_modifier)); NoteStemDirection direction = getStemDirection(note.octave); NoteTieType note_tie = getNoteTie(note.note_tie); List <NoteBeamType> note_beams = new List <NoteBeamType>() { NoteBeamType.Single }; return(new Note(note_level, note_alteration, octave, duration, direction, note_tie, note_beams) { NumberOfDots = numberOfDots }); }
public static int getClosestNotePositionInSteps(D_Note start, D_Note goal) { int step; int i_old = noteLevelInt[start.level]; int i_new = noteLevelInt[goal.level]; if (i_old > i_new) { step = max_index - i_old + i_new; } else { step = i_new - i_old; } if (step > 3) { step = step - max_index; } return(step); }
public void fillBarsWithNotes(List <D_Note> notes) { // Warning: this does not take notes into account that span multiple bars int current_bar_index = 0; foreach (D_Note note in notes) { D_Note to_append = this.bars[current_bar_index].addNote(note); if (to_append != null) { // overflow! current_bar_index++; D_Note please_dont_return = this.bars[current_bar_index].addNote(to_append); if (please_dont_return != null) { throw new Exception("Somehow there's a note that's longer than an entire bar. wot de fok"); } } if (this.bars[current_bar_index].isFull()) { current_bar_index++; } } if (!this.bars[this.bars.Count - 2].isFull()) { throw new Exception("Second last bar is not full, something probablt went wrong here."); } if (!this.bars[this.bars.Count - 1].isFull()) { // fill with rests int to_fill = this.bars[current_bar_index].spaceLeft(); this.bars[current_bar_index].addNote(D_NoteFactory.create_rest(to_fill)); } }
public void removeNote(D_Note note) { this.notes.Remove(note); }
static private MusicalSymbol getRestMusicalSymbol(D_Note note) { MusicalSymbolDuration duration = getNoteDuration(note.length); return(new Rest(duration)); }
public static D_Note create_note(D_Note other, int new_length) { return(new D_Note(other, new_length)); }