private Staff ReadTokens(Staff staff, string content) { string[] splitContent = content.Split(' '); string lastPitch = ""; for (int i = 0; i < splitContent.Length; i++) { string s = splitContent[i]; switch (s) { case "\\relative": i++; string relative = splitContent[i]; relativeOctave = 3 + relative.Count(x => x == '\'') - relative.Count(x => x == ','); staff.SetRelativeOctave(relativeOctave); lastPitch = relative[0].ToString(); break; case "\\clef": i++; staff.SetClef(splitContent[i]); break; case "\\time": i++; string time = splitContent[i]; var times = time.Split('/'); staff.SetBar((int)UInt32.Parse(times[0]), (int)UInt32.Parse(times[1])); break; case "\\tempo": i++; string tempoText = splitContent[i]; var tempo = tempoText.Split('='); staff.SetTempo((int)UInt32.Parse(tempo[1])); break; case "|": staff.AddBar(); break; default: if (s.Length != 0) { NoteRestFactory factory = NoteRestFactory.getFactory(s[0]); if (factory != null) { int length; int dots; getInfo(s, ref lastPitch, out length, out dots); staff.AddNote(factory.create(lastPitch, relativeOctave + 1, length, dots)); } } break; } } return(staff); }
private void HandleChannel(Staff staff) { var channelMessage = midiMessage as ChannelMessage; if (channelMessage.Command == ChannelCommand.NoteOn) { NoteRestFactory factory = null; string pitch = ""; int octave = 0; int length = 0; int dots = 0; if (channelMessage.Data2 > 0) // Data2 = loudness { if (midiEvent.AbsoluteTicks != previousNoteAbsoluteTicks) { pitch = "r"; factory = NoteRestFactory.getFactory(pitch[0]); length = GetNoteLength(midiEvent.AbsoluteTicks, out dots); previousNoteAbsoluteTicks = midiEvent.AbsoluteTicks; } startedNoteIsClosed = false; } else if (!startedNoteIsClosed) { octave = channelMessage.Data1 / 12; pitch = Enum.GetName(typeof(MIDInotes), channelMessage.Data1 % 12); factory = NoteRestFactory.getFactory(pitch[0]); length = GetNoteLength(midiEvent.AbsoluteTicks, out dots); previousNoteAbsoluteTicks = midiEvent.AbsoluteTicks; startedNoteIsClosed = true; } if (factory != null) { staff.AddNote(factory.create(pitch, octave, length, dots)); } } }
public static NoteRestFactory getFactory(char type) { NoteRestFactory factory = null; switch (type) { case 'r': factory = REST_FACTORY; break; case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g': factory = NOTE_FACTORY; break; } return(factory); }