private string calculateOctave(ADPNote _newNote, ADPNote _oldNote) //calculates the right octave from the noteinformation { string result = ""; int nearestKeyOctave; string[] keys = { "C", "D", "E", "F", "G", "A", "B", "C", "D", "E", "F", "G", "A", "B", "C", "D", "E", "F", "G", "A", "B" }; int[] differences = new int[3]; int oldKeyIndex = Array.IndexOf(keys, _oldNote.Key.Substring(0, 1), 7); int newKeyIndex1 = Array.IndexOf(keys, _newNote.Key.Substring(0, 1), 0); int newKeyIndex2 = Array.IndexOf(keys, _newNote.Key.Substring(0, 1), 7); int newKeyIndex3 = Array.IndexOf(keys, _newNote.Key.Substring(0, 1), 14); differences[0] = Math.Abs(oldKeyIndex - newKeyIndex1); differences[1] = Math.Abs(oldKeyIndex - newKeyIndex2); differences[2] = Math.Abs(oldKeyIndex - newKeyIndex3); differences.Min(); if (differences[0] == differences.Min()) { // lower octave nearestKeyOctave = _oldNote.Octave - 1; } else if (differences[1] == differences.Min()) { //middle octave nearestKeyOctave = _oldNote.Octave; } else { // higher octave nearestKeyOctave = _oldNote.Octave + 1; } if (_newNote.Octave > nearestKeyOctave) { for (int i = 0; i < (_newNote.Octave - nearestKeyOctave); i++) { result += "'"; } return(result); } else if (_newNote.Octave < nearestKeyOctave) { for (int i = 0; i < (nearestKeyOctave - _newNote.Octave); i++) { result += ","; } return(result); } else { return(""); } }
private string[] convertToInputStrings(string _musicalSymbolInfo, ADPNote _latestNote) // Converts the info from a note to a string[] and returns it { string[] resultInputStrings = new string[6]; string key = _musicalSymbolInfo.Substring(0, 1); string durationValue = Regex.Match(_musicalSymbolInfo, @"\d+").Value; if (durationValue == "") { return(null); } int duration = Int32.Parse(durationValue); if (key.Equals("r")) { resultInputStrings[0] = "rest"; resultInputStrings[1] = "" + duration; } else { int amountOfDots = 0; while (_musicalSymbolInfo.Contains(".")) { int x = _musicalSymbolInfo.IndexOf("."); _musicalSymbolInfo = _musicalSymbolInfo.Remove(x, 1); amountOfDots++; } resultInputStrings[0] = "note"; resultInputStrings[1] = "" + duration; resultInputStrings[2] = "" + amountOfDots; resultInputStrings[3] = key.ToUpper(); if (_musicalSymbolInfo.Contains("is")) { resultInputStrings[4] = "" + 1; } else if (_musicalSymbolInfo.Contains("es")) { resultInputStrings[4] = "" + 2; } else { resultInputStrings[4] = "" + 0; } resultInputStrings[5] = "" + calculateOctave(_musicalSymbolInfo, _latestNote); } return(resultInputStrings); }
private int calculateOctave(string _noteInfo, ADPNote _latestNote) //Converts the info of a note to the right octave the note should have and returns it as an int { string newKey = _noteInfo.Substring(0, 1).ToUpper(); int resultOctave; string[] keys = { "C", "D", "E", "F", "G", "A", "B", "C", "D", "E", "F", "G", "A", "B", "C", "D", "E", "F", "G", "A", "B" }; int[] differences = new int[3]; int latestNoteIndex = Array.IndexOf(keys, _latestNote.Key.Substring(0, 1), 7); int newKeyIndex1 = Array.IndexOf(keys, newKey, 0); int newKeyIndex2 = Array.IndexOf(keys, newKey, 7); int newKeyIndex3 = Array.IndexOf(keys, newKey, 14); differences[0] = Math.Abs(latestNoteIndex - newKeyIndex1); differences[1] = Math.Abs(latestNoteIndex - newKeyIndex2); differences[2] = Math.Abs(latestNoteIndex - newKeyIndex3); differences.Min(); if (differences[0] == differences.Min()) { // lower octave resultOctave = _latestNote.Octave - 1; } else if (differences[1] == differences.Min()) { //middle octave resultOctave = _latestNote.Octave; } else { // higher octave resultOctave = _latestNote.Octave + 1; } while (_noteInfo.Contains("'")) { resultOctave++; _noteInfo = _noteInfo.Remove(_noteInfo.IndexOf("'"), 1); } while (_noteInfo.Contains(",")) { resultOctave--; _noteInfo = _noteInfo.Remove(_noteInfo.IndexOf(","), 1); } return(resultOctave); }
private string convertMusicalSymbols(List <ADPBar> _bars) //Converts a list of ADPbars to a string and returns it { string result = ""; ADPNote lastUsedNote = new ADPNote(); lastUsedNote.Key = "C"; lastUsedNote.Octave = 4; foreach (ADPBar bar in _bars) { foreach (ADPMusicalSymbol musicalSymbol in bar.MusicalSymbols) { if (musicalSymbol is ADPNote) { result += ((ADPNote)musicalSymbol).Key.Substring(0, 1).ToLower(); if (((ADPNote)musicalSymbol).Alter == 1) { // C# result += "is"; } else if (((ADPNote)musicalSymbol).Alter == 2) { // Bb result += "es"; } result += calculateOctave((ADPNote)musicalSymbol, lastUsedNote); result += musicalSymbol.Duration; for (int i = 0; i < ((ADPNote)musicalSymbol).AmountOfDots; i++) { result += "."; } lastUsedNote = (ADPNote)musicalSymbol; } else { result += "r"; result += musicalSymbol.Duration; } result += " "; } result += "| \n"; } return(result); }
public ADPSheet ConvertText(string _text) //Takes a string and converts it to a string[] and returns it as a ADPSheet (See function: ConvertContent) { ADPNote latestNote = new ADPNote(); latestNote.Octave = 4; latestNote.Key = "C"; int[] timeSignature = new int[2]; string[] lilypondFileContents = _text.Split(' ').Where(x => !string.IsNullOrEmpty(x)).ToArray(); for (int i = 0; i < lilypondFileContents.Length; i++) { lilypondFileContents[i] = lilypondFileContents[i].Replace("\r\n", string.Empty); lilypondFileContents[i] = lilypondFileContents[i].Replace("\n", string.Empty); } return(ConvertContent(lilypondFileContents)); }
public override ADPSheet ReadFile(string _path) //Reads the file from a pathname, converts it to a string and returns it as a ADPSheet (See function: ConvertText) { ADPNote latestNote = new ADPNote(); latestNote.Octave = 4; latestNote.Key = "C"; int[] timeSignature = new int[2]; string[] lilypondFileContents = System.IO.File.ReadAllText(_path).Split(' ').Where(x => !string.IsNullOrEmpty(x)).ToArray(); for (int i = 0; i < lilypondFileContents.Length; i++) { lilypondFileContents[i] = lilypondFileContents[i].Replace("\r\n", string.Empty); lilypondFileContents[i] = lilypondFileContents[i].Replace("\n", string.Empty); } return(ConvertContent(lilypondFileContents)); }
public ADPMusicalSymbol GetMusicalSymbol(string[] _inputStrings) { if (_inputStrings[0].Equals("rest")) { ADPRest resultMusicalSymbol = new ADPRest(); resultMusicalSymbol.Duration = int.Parse(_inputStrings[1]); return(resultMusicalSymbol); } else { ADPNote resultMusicalSymbol = new ADPNote(); resultMusicalSymbol.Duration = int.Parse(_inputStrings[1]); resultMusicalSymbol.AmountOfDots = int.Parse(_inputStrings[2]); resultMusicalSymbol.Key = _inputStrings[3]; resultMusicalSymbol.Alter = int.Parse(_inputStrings[4]); resultMusicalSymbol.Octave = int.Parse(_inputStrings[5]); return(resultMusicalSymbol); } }
public StackPanel GetSheetVisualisation(ADPTrack _adpTrack) //updates the barlines in the mainwindow to look like the ADPsheet that is given as the parameter { StackPanel resultStackPanel = new StackPanel(); int barCount = 0; bool barlineHasTimeSignature = false; int[] currentTimeSignature = new int[2]; IncipitViewerWPF barLine = createNewBarline(); foreach (ADPBar tempBar in _adpTrack.Bars) { if (!barlineHasTimeSignature) { currentTimeSignature[0] = tempBar.TimeSignature[0]; currentTimeSignature[1] = tempBar.TimeSignature[1]; barLine.AddMusicalSymbol(new TimeSignature(TimeSignatureType.Numbers, (uint)tempBar.TimeSignature[0], (uint)tempBar.TimeSignature[1])); barLine.AddMusicalSymbol(new Barline()); barlineHasTimeSignature = true; } else if (currentTimeSignature[0] != tempBar.TimeSignature[0] && currentTimeSignature[1] != tempBar.TimeSignature[1]) { currentTimeSignature[0] = tempBar.TimeSignature[0]; currentTimeSignature[1] = tempBar.TimeSignature[1]; barLine.AddMusicalSymbol(new TimeSignature(TimeSignatureType.Numbers, (uint)tempBar.TimeSignature[0], (uint)tempBar.TimeSignature[1])); } // add symbols foreach (ADPMusicalSymbol tempSymbol in tempBar.MusicalSymbols) { if (tempSymbol != null) { if (tempSymbol.GetType() == typeof(ADPRest)) { //rest barLine.AddMusicalSymbol(new Rest(convertDuration(tempSymbol.Duration))); } else { ADPNote tempNote = (ADPNote)tempSymbol; //note if (tempNote.AmountOfDots > 0) { barLine.AddMusicalSymbol(new Note(tempNote.Key, tempNote.Alter, tempNote.Octave, convertDuration(tempNote.Duration), NoteStemDirection.Down, NoteTieType.None, new List <NoteBeamType>() { NoteBeamType.Single }) { NumberOfDots = tempNote.AmountOfDots }); } else { barLine.AddMusicalSymbol(new Note(tempNote.Key, tempNote.Alter, tempNote.Octave, convertDuration(tempNote.Duration), NoteStemDirection.Down, NoteTieType.None, new List <NoteBeamType>() { NoteBeamType.Single })); } } } } // add endOfBarLine barCount++; barLine.AddMusicalSymbol(new Barline()); if (barCount == 3) { resultStackPanel.Children.Add(barLine); barLine = createNewBarline(); barCount = 0; barlineHasTimeSignature = false; } } if (barCount > 0) { resultStackPanel.Children.Add(barLine); } return(resultStackPanel); }
public ADPSheet ConvertContent(string[] _content) //Takes a string array and converts it to an ADPSheet, then returns it { int[] timeSignature = new int[2]; ADPNote latestNote = new ADPNote(); latestNote.Key = "C"; latestNote.Octave = 4; ADPMusicalSymbol tempMusicalSymbol; ADPBar tempBar; int alternativeNr = 0; string tempo = "error"; string key = "error"; contentType type = contentType.none; ADPSheet adps = new ADPSheet(); ADPTrack adpt = new ADPTrack(); adpt.Name = "LilypondTrack"; List <ADPMusicalSymbol> notes = new List <ADPMusicalSymbol>(); List <List <ADPMusicalSymbol> > alternatives = new List <List <ADPMusicalSymbol> >(); for (int i = 2; i < _content.Length; i++) { string temp = _content[i]; switch (_content[i]) { case "": break; case "\\tempo": tempo = _content[i + 1]; i++; break; case "\\time": string str = _content[i + 1]; timeSignature[0] = (int)Char.GetNumericValue(str[0]); timeSignature[1] = (int)Char.GetNumericValue(str[2]); i++; break; case "\\repeat": tempBar = new ADPBar(); tempBar.MusicalSymbols = notes; int[] tempTimeSignature = new int[2]; tempTimeSignature[0] = timeSignature[0]; tempTimeSignature[1] = timeSignature[1]; tempBar.TimeSignature = tempTimeSignature; //adpt.Bars.Add(tempBar); notes = new List <ADPMusicalSymbol>(); i++; i++; break; case "\\alternative": //type = contentType.alternativeBlok; break; case "\\clef": key = _content[i + 1]; i++; break; case "|": //add maatstreep / new bar? if (type == contentType.alternative) { //Had een Alternative maatstreep gemaakt } else { tempBar = new ADPBar(); tempBar.MusicalSymbols = notes; tempTimeSignature = new int[2]; tempTimeSignature[0] = timeSignature[0]; tempTimeSignature[1] = timeSignature[1]; tempBar.TimeSignature = tempTimeSignature; adpt.Bars.Add(tempBar); notes = new List <ADPMusicalSymbol>(); } break; case "{": //add alternative if alternativeblock if (type == contentType.alternativeBlok) { type = contentType.alternative; alternatives.Add(new List <ADPMusicalSymbol>()); } break; case "}": //close alternative if alternativeblock //if (type == contentType.alternative) //{ // type = contentType.alternativeBlok; // alternativeNr++; //} //else //{ // type = contentType.none; //} if (notes.Count > 0) { ADPBar tempBar2 = new ADPBar(); tempBar2.MusicalSymbols = notes; tempBar2.TimeSignature = timeSignature; adpt.Bars.Add(tempBar2); notes = new List <ADPMusicalSymbol>(); } break; case "}}": //End of File break; case "~": break; default: if (type == contentType.alternative) { //add alternative note string[] inputStrings = convertToInputStrings(_content[i], latestNote); if (inputStrings != null) { tempMusicalSymbol = musicalSymbolFactory.GetMusicalSymbol(inputStrings); if (tempMusicalSymbol is ADPNote) { latestNote = (ADPNote)tempMusicalSymbol; } alternatives[alternativeNr].Add(tempMusicalSymbol); } } else { //add normal note string[] inputStrings = convertToInputStrings(_content[i], latestNote); if (inputStrings != null) { tempMusicalSymbol = musicalSymbolFactory.GetMusicalSymbol(inputStrings); if (tempMusicalSymbol is ADPNote) { latestNote = (ADPNote)tempMusicalSymbol; } notes.Add(tempMusicalSymbol); } } break; } } adps.Tracks.Add(adpt); //Only need to add one track return(adps); }