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); }
private static void set_measures(Sequence sequence, D_Staff staff) { // This function requires staff.num_of_beats to be set! // TimeSignature messages are in channel 0 List <Tuple <MidiEvent, MetaMessage> > meta_messages = getMetaMessages(sequence[0]); foreach (Tuple <MidiEvent, MetaMessage> metaPair in meta_messages) { MidiEvent midiEvent = metaPair.Item1; MetaMessage metaMessage = metaPair.Item2; byte[] meta_bytes = metaMessage.GetBytes(); // Time signature if (metaMessage.MetaType == MetaType.TimeSignature) { int top_number = meta_bytes[0]; int bottom_number = (int)(Math.Pow(2, meta_bytes[1])); int start_beat = midiEvent.AbsoluteTicks / sequence.Division; staff.addMeasure(top_number, bottom_number, start_beat); } } staff.setMeasureEndTimes(); }
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(); }
private void btnShowContentClick(object sender, RoutedEventArgs e) { string filename = txt_SongFilePath.Text; string extension = System.IO.Path.GetExtension(filename); switch (extension) { case ".mid": showMidiTracks(MidiReader.ReadMidi(filename)); D_Staff midi_staff = MidiParser.getInstance().parseFile(filename); string converted_lilypond = LilypondComposer.getInstance().composeLilypondFromStaff(midi_staff); this.editor.setText(converted_lilypond); this.editor.enable(); break; case ".ly": this.editor.enable(); this.editor.setFileName(filename); this.editor.readFile(); //this.songData = LilypondParser.getInstance().parseFile(filename); break; default: Console.WriteLine("I can't parse " + extension); break; } this.updateNoteViewer(); }
private void set_num_of_beats(D_Staff staff, List <D_Note> notes, int current_scope_octave) { double num_of_beats = 0; foreach (D_Note note in notes) { num_of_beats += ((double)note.length / (double)4); } staff.num_of_beats = (int)num_of_beats; }
public D_Staff parseText(string lilypondFileContents) { D_Staff staff = new D_Staff(); List <string> file_lines = StringUtil.splitNewLines(lilypondFileContents); List <string> tokens = this.getLilypondTokens(file_lines); this.new_parseLilypondTokens(staff, tokens); return(staff); }
public D_Staff parseFile(string lilypondFilePath) { D_Staff staff = new D_Staff(); List <string> file_lines = new List <string>(System.IO.File.ReadAllLines(lilypondFilePath)); List <string> tokens = this.getLilypondTokens(file_lines); this.new_parseLilypondTokens(staff, tokens); return(staff); }
private D_Staff new_parseLilypondTokens(D_Staff staff, List <string> tokens) { int relative_octave = this.get_relative_octave(tokens); List <D_Note> notes = this.get_notes(tokens, relative_octave); this.set_clef(staff, tokens); this.set_num_of_beats(staff, notes, relative_octave); this.set_tempo(staff, tokens); this.set_measures(staff, tokens); staff.make_bars(); staff.fillBarsWithNotes(notes); return(staff); }
public void evaluate_editor_contents(bool undo_redo_pressed) { try { this.songData = LilypondParser.getInstance().parseText(editor.getText()); this.updateNoteViewer(); if (!undo_redo_pressed) { this.editor_history.save(new EditorMemento(this.editor)); } this.updateHistoryButtons(); } catch (Exception e) { Console.WriteLine("NON VALID LILYPOND YOU LOSER"); } }
private static D_Staff parseSequence(Sequence sequence) { D_Staff staff = new D_Staff(); staff.clef = clef.g_key; set_num_of_beats(sequence, staff); set_tempo(sequence, staff); set_measures(sequence, staff); int ticks_per_beat = sequence.Division; staff.make_bars(); List <D_Note> notes = get_notes_from_track(sequence[1], staff, ticks_per_beat); staff.fillBarsWithNotes(notes); return(staff); }
public void updateView(D_Staff staff) { if (staff == null) { return; } StaffViewers.Clear(); IncipitViewerWPF temp_staff = IncipitViewerWPFWrapper.getWPFstaff(staff.clef); int bar_count = 0; D_Measure previous_bar_measure = null; foreach (D_Bar bar in staff.bars) { bar_count++; if (bar.measure != previous_bar_measure) { temp_staff.AddMusicalSymbol(PSAMAdapter.fromMeasure(bar.measure)); } foreach (D_Note note in bar.notes) { temp_staff.AddMusicalSymbol(PSAMAdapter.fromNote(note)); } if (bar_count == 4) { StaffViewers.Add(temp_staff); temp_staff = IncipitViewerWPFWrapper.getWPFstaff(staff.clef); bar_count = 0; } temp_staff.AddMusicalSymbol(new Barline()); previous_bar_measure = bar.measure; } if (bar_count < 4) { StaffViewers.Add(temp_staff); } this.view.ItemsSource = StaffViewers; }
private void set_clef(D_Staff staff, List <string> tokens) { int current_token_index = 0; int token_end_index = tokens.Count - 1; while (current_token_index <= token_end_index) { string current_token = tokens[current_token_index]; if (current_token == "\\clef") { string clef_type = tokens[current_token_index + 1]; staff.clef = getClef(clef_type); return; } current_token_index++; } }
private void set_tempo(D_Staff staff, List <string> tokens) { int current_token_index = 0; int token_end_index = tokens.Count - 1; while (current_token_index <= token_end_index) { string current_token = tokens[current_token_index]; if (current_token == "\\tempo") { string tempo_arg = tokens[current_token_index + 1]; staff.tempo = StringUtil.extractTempoFromLilypondString(tempo_arg); return; } current_token_index++; } }
private static void set_num_of_beats(Sequence sequence, D_Staff staff) { // EndOfTrack messages are in channel 1 List <Tuple <MidiEvent, MetaMessage> > meta_messages = getMetaMessages(sequence[1]); foreach (Tuple <MidiEvent, MetaMessage> metaPair in meta_messages) { MidiEvent midiEvent = metaPair.Item1; MetaMessage metaMessage = metaPair.Item2; byte[] meta_bytes = metaMessage.GetBytes(); // EndOfTrack if (metaMessage.MetaType == MetaType.EndOfTrack) { int beats = midiEvent.AbsoluteTicks / sequence.Division; staff.num_of_beats = beats; } } }
private static void set_tempo(Sequence sequence, D_Staff staff) { // Tempo message is in channel 0 List <Tuple <MidiEvent, MetaMessage> > meta_messages = getMetaMessages(sequence[0]); foreach (Tuple <MidiEvent, MetaMessage> metaPair in meta_messages) { MetaMessage metaMessage = metaPair.Item2; byte[] meta_bytes = metaPair.Item2.GetBytes(); // BPM if (metaMessage.MetaType == MetaType.Tempo) { // Bitshifting is nodig om het tempo in BPM te berekenen int tempo = (meta_bytes[0] & 0xff) << 16 | (meta_bytes[1] & 0xff) << 8 | (meta_bytes[2] & 0xff); staff.tempo = 60000000 / tempo; return; } } staff.tempo = -1; }