def from_Bar(bar, showkey=True, showtime=True): """Get a Bar object and return the LilyPond equivalent in a string. The showkey and showtime parameters can be set to determine whether the key and the time should be shown. >>> from mingus.containers.note import QuarterNoteFactory as Q, HalfNoteFactory as H >>> from mingus.core.chords import HalfNoteChordFactory as HC >>> bar = Bar() >>> bar.extend(Q(['C','D'])) >>> bar += H('E') >>> cmaj_triad = determine(chords.major_triad('C'),shorthand=True)[0] >>> c7 = determine(chords.dominant_seventh('C'),shorthand=True)[0] >>> bar.set_chord_notes([HC(cmaj_triad), HC(c7)]) >>> print(from_Bar(bar)) << \\time 4/4 \chords { c2:maj c2:7 } { c'4 d'4 e'2 }>> """ # Throw exception if not hasattr(bar, "bar"): return False # Process the key if showkey: key_note = Note(bar.key.key[0].upper() + bar.key.key[1:]) key = "\\key %s \\%s " % ( from_Note(key_note, False, standalone=False), bar.key.mode, ) result = key else: result = "" # Handle the NoteContainers latest_ratio = (1, 1) ratio_has_changed = False for bar_entry in bar.bar: parsed_value = value.determine(bar_entry[1]) ratio = parsed_value[2:] if ratio == latest_ratio: result += (from_NoteContainer( bar_entry[2], bar_entry[1], standalone=False) + " ") else: if ratio_has_changed: result += "}" result += "\\times %d/%d {" % (ratio[1], ratio[0]) result += (from_NoteContainer( bar_entry[2], bar_entry[1], standalone=False) + " ") latest_ratio = ratio ratio_has_changed = True if ratio_has_changed: result += "}" # Process the time if showtime: return "{ \\time %d/%d %s}" % (bar.meter[0], bar.meter[1], result) else: return "{ %s}" % result
def from_NoteContainer(nc, duration=None, standalone=True): """Expects a [refMingusContainersNotecontainer NoteContainer] object and \ returns the !LilyPond equivalent in a string. The second argument \ determining the duration of the NoteContainer is optional. When the \ standalone argument is True the result of this function can be used directly \ by functions like to_png. It is mostly here to be used by from_Bar.""" # Throw exception if nc is not None and not hasattr(nc, 'notes'): return False # Return rests for None or empty lists if nc is None or len(nc.notes) == 0: result = 'r' elif len(nc.notes) == 1: # Return a single note if the list contains only one note result = from_Note(nc.notes[0], standalone=False) else: # Return the notes grouped in '<' and '>' result = '<' for notes in nc.notes: result += from_Note(notes, standalone=False) + ' ' result = result[:-1] + '>' # Add the duration if duration != None: parsed_value = value.determine(duration) # Special case: check for longa and breve in the duration (issue #37) dur = parsed_value[0] if dur == value.longa: result += '\\longa' elif dur == value.breve: result += '\\breve' else: result += str(int(parsed_value[0])) for i in range(parsed_value[1]): result += '.' if not standalone: return result else: return '{ %s }' % result
def from_Bar(bar, showkey=True, showtime=True): """Get a Bar object and return the LilyPond equivalent in a string. The showkey and showtime parameters can be set to determine whether the key and the time should be shown. """ # Throw exception if not hasattr(bar, "bar"): return False # Process the key if showkey: key_note = Note(bar.key.key[0].upper() + bar.key.key[1:]) key = "\\key %s \\%s " % ( from_Note(key_note, False, standalone=False), bar.key.mode, ) result = key else: result = "" # Handle the NoteContainers latest_ratio = (1, 1) ratio_has_changed = False for bar_entry in bar.bar: parsed_value = value.determine(bar_entry[1]) ratio = parsed_value[2:] if ratio == latest_ratio: result += (from_NoteContainer( bar_entry[2], bar_entry[1], standalone=False) + " ") else: if ratio_has_changed: result += "}" result += "\\times %d/%d {" % (ratio[1], ratio[0]) result += (from_NoteContainer( bar_entry[2], bar_entry[1], standalone=False) + " ") latest_ratio = ratio ratio_has_changed = True if ratio_has_changed: result += "}" # Process the time if showtime: return "{ \\time %d/%d %s}" % (bar.meter[0], bar.meter[1], result) else: return "{ %s}" % result
def from_Bar(bar, showkey=True, showtime=True): """Expects a [refMingusContainersBar Bar] object and returns the !LilyPond \ equivalent in a string. showkey and showtime can be set to determine whether \ the key and the time should be shown.""" # Throw exception if not hasattr(bar, 'bar'): return False # Process the key if showkey: key = '\\key %s \\major ' % from_Note(bar.key, False, standalone=False) result = key else: result = '' # Handle the NoteContainers latest_ratio = (1, 1) ratio_has_changed = False for bar_entry in bar.bar: parsed_value = value.determine(bar_entry[1]) ratio = parsed_value[2:] if ratio == latest_ratio: result += from_NoteContainer( bar_entry[2], bar_entry[1], standalone=False) + ' ' else: if ratio_has_changed: result += '}' result += '\\times %d/%d {' % (ratio[1], ratio[0]) result += from_NoteContainer( bar_entry[2], bar_entry[1], standalone=False) + ' ' latest_ratio = ratio ratio_has_changed = True if ratio_has_changed: result += '}' # Process the time if showtime: return '{ \\time %d/%d %s}' % (bar.meter[0], bar.meter[1], result) else: return '{ %s}' % result
def from_Bar(bar, showkey=True, showtime=True): """Expects a [refMingusContainersBar Bar] object and returns the !LilyPond \ equivalent in a string. showkey and showtime can be set to determine whether \ the key and the time should be shown.""" # Throw exception if not hasattr(bar, 'bar'): return False # Process the key if showkey: key = '\\key %s \\major ' % from_Note(bar.key, False, standalone=False) result = key else: result = '' # Handle the NoteContainers latest_ratio = (1, 1) ratio_has_changed = False for bar_entry in bar.bar: parsed_value = value.determine(bar_entry[1]) ratio = parsed_value[2:] if ratio == latest_ratio: result += from_NoteContainer(bar_entry[2], bar_entry[1], standalone=False) + ' ' else: if ratio_has_changed: result += '}' result += '\\times %d/%d {' % (ratio[1], ratio[0]) result += from_NoteContainer(bar_entry[2], bar_entry[1], standalone=False) + ' ' latest_ratio = ratio ratio_has_changed = True if ratio_has_changed: result += '}' # Process the time if showtime: return '{ \\time %d/%d %s} \\bar "|" ' % (bar.meter[0], bar.meter[1], result) else: return '{ %s} \\bar "|" ' % result
def from_NoteContainer(nc, duration=None, standalone=True): """Get a NoteContainer object and return the LilyPond equivalent in a string. The second argument determining the duration of the NoteContainer is optional. When the standalone argument is True the result of this function can be used directly by functions like to_png. It is mostly here to be used by from_Bar. """ # Throw exception if nc is not None and not hasattr(nc, "notes"): return False # Return rests for None or empty lists if nc is None or len(nc.notes) == 0: result = "r" elif len(nc.notes) == 1: # Return a single note if the list contains only one note result = from_Note(nc.notes[0], standalone=False) else: # Return the notes grouped in '<' and '>' result = "<" for notes in nc.notes: result += from_Note(notes, standalone=False) + " " result = result[:-1] + ">" # Add the duration if duration != None: parsed_value = value.determine(duration) # Special case: check for longa and breve in the duration (issue #37) dur = parsed_value[0] if dur == value.longa: result += "\\longa" elif dur == value.breve: result += "\\breve" else: result += str(int(parsed_value[0])) for i in range(parsed_value[1]): result += "." if not standalone: return result else: return "{ %s }" % result
def from_Bar(bar, showkey=True, showtime=True): """Get a Bar object and return the LilyPond equivalent in a string. The showkey and showtime parameters can be set to determine whether the key and the time should be shown. """ # Throw exception if not hasattr(bar, 'bar'): return False # Process the key if showkey: key_note = Note(bar.key.key[0].upper() + bar.key.key[1:]) key = '\\key %s \\%s ' % (from_Note(key_note, False, standalone=False), bar.key.mode) result = key else: result = '' # Handle the NoteContainers latest_ratio = (1, 1) ratio_has_changed = False for bar_entry in bar.bar: parsed_value = value.determine(bar_entry[1]) ratio = parsed_value[2:] if ratio == latest_ratio: result += from_NoteContainer(bar_entry[2], bar_entry[1], standalone=False) + ' ' else: if ratio_has_changed: result += '}' result += '\\times %d/%d {' % (ratio[1], ratio[0]) result += from_NoteContainer(bar_entry[2], bar_entry[1], standalone=False) + ' ' latest_ratio = ratio ratio_has_changed = True if ratio_has_changed: result += '}' # Process the time if showtime: return '{ \\time %d/%d %s}' % (bar.meter[0], bar.meter[1], result) else: return '{ %s}' % result
def from_Bar(bar, showkey = True, showtime = True): """Expects a [refMingusContainersBar Bar] object and returns the \ !LilyPond equivalent in a string. showkey and showtime can be set to \ determine whether the key and the time should be shown.""" # Throw exception if not ( hasattr ( bar , "bar" ) ): return False # Process the key if showkey: key = "\\key %s \\major " % from_Note(bar.key, False) result = key else: result = "" # Handle the NoteContainers latest_ratio = (1, 1) ratio_has_changed = False for bar_entry in bar.bar: parsed_value = value.determine(bar_entry[1]) ratio = parsed_value[2:] if ratio == latest_ratio: result += from_NoteContainer(bar_entry[2], bar_entry[1]) + " " else: if ratio_has_changed: result += "}" result += "\\times %d/%d {" % (ratio[1], ratio[0]) result += from_NoteContainer(bar_entry[2], bar_entry[1]) + " " latest_ratio = ratio ratio_has_changed = True if ratio_has_changed: result += "}" # Process the time if showtime: return "{ \\time %d/%d %s}"\ % (bar.meter[0], bar.meter[1], result) else: return "{ %s}" % result
def from_NoteContainer(nc, duration = None): """Expects a [refMingusContainersNotecontainer NoteContainer] object \ and returns the !LilyPond equivalent in a string. The second argument \ determining the duration of the NoteContainer is optional.""" # Throw exception if (nc is not None) and (not ( hasattr ( nc, "notes" ) )): return False # Return rests for None or empty lists if nc is None or len(nc.notes) == 0: result = "r" # Return a single note if the list contains only # one note elif len(nc.notes) == 1: result = from_Note(nc.notes[0]) # Return the notes grouped in '<' and '>' else: result = "<" for notes in nc.notes: result += from_Note(notes) + " " result = result[:-1] + ">" # Add the duration if duration != None: parsed_value = value.determine(duration) # Special case: check for longa and breve in the duration (issue #37) dur = parsed_value[0] if dur == value.longa: result += "\\longa" elif dur == value.breve: result += "\\breve" else: result += str(int(parsed_value[0])) for i in range(parsed_value[1]): result += "." return result
def test_determine_imperfect(self): self.assertEqual(value.determine(9), (8, 0, 1, 1)) self.assertEqual(value.determine(4.5), (4, 0, 1, 1))
def test_determine(self): self.assertEqual(value.determine(7), (4, 0, 7, 4)) self.assertEqual(value.determine(8), (8, 0, 1, 1)) self.assertEqual(value.determine(10), (8, 0, 5, 4)) self.assertEqual(value.determine(12), (8, 0, 3, 2)) self.assertEqual(value.determine(14), (8, 0, 7, 4)) for x in value.base_values: self.assertEqual(value.determine(x), (x, 0, 1, 1)) self.assertEqual(value.determine(value.dots(x, 1)), (x, 1, 1, 1)) self.assertEqual(value.determine(value.dots(x, 2)), (x, 2, 1, 1)) self.assertEqual(value.determine(value.dots(x, 3)), (x, 3, 1, 1)) self.assertEqual(value.determine(value.dots(x, 4)), (x, 4, 1, 1)) for (s, x) in enumerate(value.base_triplets): self.assertEqual(value.determine(x), (value.base_values[s], 0, 3, 2)) for (s, x) in enumerate(value.base_quintuplets): self.assertEqual(value.determine(x), (value.base_values[s], 0, 5, 4)) for (s, x) in enumerate(value.base_septuplets): self.assertEqual(value.determine(x), (value.base_values[s], 0, 7, 4))
def _bar2musicxml(bar): doc = Document() bar_node = doc.createElement("measure") #bar attributes attributes = doc.createElement("attributes") #calculate divisions by using the LCM l=list() for nc in bar: l.append(int(value.determine(nc[1])[0])) lcm = _lcm(terms=l)*4 divisions = doc.createElement("divisions") divisions.appendChild(doc.createTextNode(str(lcm))) attributes.appendChild(divisions) if bar.key.name in basic_keys: key = doc.createElement("key") fifths = doc.createElement("fifths") #now we are going to guess which is the key of the bar index = basic_keys.index(bar.key.name) if index>13: index -= 12 fifths.appendChild(doc.createTextNode(str(index-6))) mode = doc.createElement("mode") mode.appendChild(doc.createTextNode("major")) #does mingus support more modes? key.appendChild(fifths) key.appendChild(mode) attributes.appendChild(key) time = doc.createElement("time") beats = doc.createElement("beats") beattype = doc.createElement("beat-type") beats.appendChild(doc.createTextNode(str(bar.meter[0]))) beattype.appendChild(doc.createTextNode(str(bar.meter[1]))) time.appendChild(beats) time.appendChild(beattype) attributes.appendChild(time) bar_node.appendChild(attributes) chord=doc.createElement("chord") for nc in bar: time = value.determine(nc[1]) beat = time[0] note_cont = nc[2] is_chord=False if note_cont: #is a note_container with 2 or more notes a chord? if len(note_cont)>1: is_chord=True else: note_cont = [None] for n in note_cont: note = _note2musicxml(n) if is_chord: note.appendChild(chord) #convert the duration of the note duration = doc.createElement("duration") duration.appendChild(doc.createTextNode(str(int(lcm*(4.0/beat))))) note.appendChild(duration) #check for dots dot = doc.createElement("dot") for i in range(0,time[1]): note.appendChild(dot) if beat in value.musicxml.keys(): type_node = doc.createElement("type") type_node.appendChild(doc.createTextNode(value.musicxml[beat])) note.appendChild(type_node) #check for non-standard ratio if time[2]!=1 and time[3]!=1: modification = doc.createElement("time-modification") actual = doc.createElement("actual-notes") actual.appendChild(doc.createTextNode(str(time[2]))) normal = doc.createElement("normal-notes") normal.appendChild(doc.createTextNode(str(time[3]))) modification.appendChild(actual) modification.appendChild(normal) note.appendChild(modification) bar_node.appendChild(note) return bar_node
def test_determine(self): self.assertEqual(value.determine(7), (4, 0, 7, 4)) self.assertEqual(value.determine(8), (8, 0, 1, 1)) self.assertEqual(value.determine(10), (8, 0, 5, 4)) self.assertEqual(value.determine(12), (8, 0, 3, 2)) self.assertEqual(value.determine(14), (8, 0, 7, 4)) for x in value.base_values: self.assertEqual(value.determine(x), (x, 0, 1, 1)) self.assertEqual(value.determine(value.dots(x, 1)), (x, 1, 1, 1)) self.assertEqual(value.determine(value.dots(x, 2)), (x, 2, 1, 1)) self.assertEqual(value.determine(value.dots(x, 3)), (x, 3, 1, 1)) self.assertEqual(value.determine(value.dots(x, 4)), (x, 4, 1, 1)) for s, x in enumerate(value.base_triplets): self.assertEqual(value.determine(x), (value.base_values[s], 0, 3, 2)) for s, x in enumerate(value.base_quintuplets): self.assertEqual(value.determine(x), (value.base_values[s], 0, 5, 4)) for s, x in enumerate(value.base_septuplets): self.assertEqual(value.determine(x), (value.base_values[s], 0, 7, 4))
def _bar2musicxml(bar): doc = Document() bar_node = doc.createElement('measure') # bar attributes attributes = doc.createElement('attributes') # calculate divisions by using the LCM l = [] for nc in bar: l.append(int(value.determine(nc[1])[0])) lcm = _lcm(terms=l) * 4 divisions = doc.createElement('divisions') divisions.appendChild(doc.createTextNode(str(lcm))) attributes.appendChild(divisions) if bar.key.key in major_keys or bar.key.key in minor_keys: key = doc.createElement('key') fifths = doc.createElement('fifths') # now we are going to guess which is the key of the bar fifths.appendChild(doc.createTextNode(str(bar.key.signature))) mode = doc.createElement('mode') mode.appendChild(doc.createTextNode(bar.key.mode)) key.appendChild(fifths) key.appendChild(mode) attributes.appendChild(key) time = doc.createElement('time') beats = doc.createElement('beats') beattype = doc.createElement('beat-type') beats.appendChild(doc.createTextNode(str(bar.meter[0]))) beattype.appendChild(doc.createTextNode(str(bar.meter[1]))) time.appendChild(beats) time.appendChild(beattype) attributes.appendChild(time) bar_node.appendChild(attributes) chord = doc.createElement('chord') for nc in bar: time = value.determine(nc[1]) beat = time[0] note_cont = nc[2] is_chord = False if note_cont: # is a note_container with 2 or more notes a chord? if len(note_cont) > 1: is_chord = True else: note_cont = [None] for n in note_cont: note = _note2musicxml(n) if is_chord: note.appendChild(chord) # convert the duration of the note duration = doc.createElement('duration') duration.appendChild( doc.createTextNode(str(int(lcm * (4.0 / beat))))) note.appendChild(duration) # check for dots dot = doc.createElement('dot') for i in range(0, time[1]): note.appendChild(dot) if beat in value.musicxml.keys(): type_node = doc.createElement('type') type_node.appendChild(doc.createTextNode(value.musicxml[beat])) note.appendChild(type_node) # check for non-standard ratio if time[2] != 1 and time[3] != 1: modification = doc.createElement('time-modification') actual = doc.createElement('actual-notes') actual.appendChild(doc.createTextNode(str(time[2]))) normal = doc.createElement('normal-notes') normal.appendChild(doc.createTextNode(str(time[3]))) modification.appendChild(actual) modification.appendChild(normal) note.appendChild(modification) bar_node.appendChild(note) return bar_node
def _bar2musicxml(bar): doc = Document() bar_node = doc.createElement("measure") #bar attributes attributes = doc.createElement("attributes") #calculate divisions by using the LCM l=list() for nc in bar: l.append(int(value.determine(nc[1])[0])) lcm = _lcm(terms=l)*4 divisions = doc.createElement("divisions") divisions.appendChild(doc.createTextNode(str(lcm))) attributes.appendChild(divisions) if bar.key.name in basic_keys: key = doc.createElement("key") fifths = doc.createElement("fifths") #now we are going to guess which is the key of the bar index = basic_keys.index(bar.key.name) if index>13: index -= 12 fifths.appendChild(doc.createTextNode(str(index-6))) mode = doc.createElement("mode") mode.appendChild(doc.createTextNode("major")) #does mingus support more modes? key.appendChild(fifths) key.appendChild(mode) attributes.appendChild(key) time = doc.createElement("time") beats = doc.createElement("beats") beattype = doc.createElement("beat-type") beats.appendChild(doc.createTextNode(str(bar.meter[0]))) beattype.appendChild(doc.createTextNode(str(bar.meter[1]))) time.appendChild(beats) time.appendChild(beattype) attributes.appendChild(time) bar_node.appendChild(attributes) global isTied #must last over barlines for nc in bar: time = value.determine(nc[1]) beat = time[0] note_cont = nc[2] is_first_of_group=True prevTied = isTied isTied = note_cont.tied if not note_cont: note_cont = [None] for n in note_cont: note = _note2musicxml(n, isChord=not is_first_of_group) is_first_of_group = False #all but the last have a <chord/> element. #convert the duration of the note duration = doc.createElement("duration") duration.appendChild(doc.createTextNode(str(int(lcm*(4.0/beat))))) note.appendChild(duration) #add tie if necessary. if prevTied: tied=doc.createElement("tie") tied.setAttribute("type","stop") note.appendChild(tied) if isTied: tied=doc.createElement("tie") tied.setAttribute("type","start") note.appendChild(tied) #check for dots dot = doc.createElement("dot") for i in range(0,time[1]): note.appendChild(dot) if beat in value.musicxml.keys(): type_node = doc.createElement("type") type_node.appendChild(doc.createTextNode(value.musicxml[beat])) note.appendChild(type_node) #check for non-standard ratio if time[2]!=1 and time[3]!=1: modification = doc.createElement("time-modification") actual = doc.createElement("actual-notes") actual.appendChild(doc.createTextNode(str(time[2]))) normal = doc.createElement("normal-notes") normal.appendChild(doc.createTextNode(str(time[3]))) modification.appendChild(actual) modification.appendChild(normal) note.appendChild(modification) bar_node.appendChild(note) return bar_node
def _bar2musicxml(bar): doc = Document() bar_node = doc.createElement('measure') # bar attributes attributes = doc.createElement('attributes') # calculate divisions by using the LCM l = [] for nc in bar: l.append(int(value.determine(nc[1])[0])) lcm = _lcm(terms=l) * 4 divisions = doc.createElement('divisions') divisions.appendChild(doc.createTextNode(str(lcm))) attributes.appendChild(divisions) if bar.key.key in major_keys or bar.key.key in minor_keys: key = doc.createElement('key') fifths = doc.createElement('fifths') # now we are going to guess which is the key of the bar fifths.appendChild(doc.createTextNode(str(bar.key.signature))) mode = doc.createElement('mode') mode.appendChild(doc.createTextNode(bar.key.mode)) key.appendChild(fifths) key.appendChild(mode) attributes.appendChild(key) time = doc.createElement('time') beats = doc.createElement('beats') beattype = doc.createElement('beat-type') beats.appendChild(doc.createTextNode(str(bar.meter[0]))) beattype.appendChild(doc.createTextNode(str(bar.meter[1]))) time.appendChild(beats) time.appendChild(beattype) attributes.appendChild(time) bar_node.appendChild(attributes) chord = doc.createElement('chord') for nc in bar: time = value.determine(nc[1]) beat = time[0] note_cont = nc[2] is_chord = False if note_cont: # is a note_container with 2 or more notes a chord? if len(note_cont) > 1: is_chord = True else: note_cont = [None] for n in note_cont: note = _note2musicxml(n) if is_chord: note.appendChild(chord) # convert the duration of the note duration = doc.createElement('duration') duration.appendChild(doc.createTextNode(str(int(lcm * (4.0 / beat))))) note.appendChild(duration) # check for dots dot = doc.createElement('dot') for i in range(0, time[1]): note.appendChild(dot) if beat in value.musicxml.keys(): type_node = doc.createElement('type') type_node.appendChild(doc.createTextNode(value.musicxml[beat])) note.appendChild(type_node) # check for non-standard ratio if time[2] != 1 and time[3] != 1: modification = doc.createElement('time-modification') actual = doc.createElement('actual-notes') actual.appendChild(doc.createTextNode(str(time[2]))) normal = doc.createElement('normal-notes') normal.appendChild(doc.createTextNode(str(time[3]))) modification.appendChild(actual) modification.appendChild(normal) note.appendChild(modification) bar_node.appendChild(note) return bar_node