class Tune: def __init__(self): self.title = '' self.meter = 6.0 / 8.0 self.rhythm = 'Jig' self.noteLength = 1.0 / 8.0 self.key = '' self.origin = '' self.currentNote = Note(self.noteLength) self.phraseChain = PhraseChain(self.meter, self.noteLength) def setMeter(self, meter): self.meter = meter self.phraseChain.meter = meter def setNoteLength(self, noteLength): self.noteLength = noteLength self.phraseChain.noteLength = noteLength def GetLengthModifier(self, note): return self.noteLength def extractPhrasesFromLine(self, line): # Detect notes inChord = False for char in line.strip(): # Record everything between " as a single note, as it is a chord if IsChord(char): if inChord: # Close the chord inChord = False else: # Begin a new chord inChord = True self.phraseChain.addNote(self.currentNote) self.currentNote = Note(self.noteLength) self.currentNote.addChar(char) # If a chord is in progress, continue recording that chord until # the chord is closed elif inChord: self.currentNote.addChar(char) # If the character comes before a note and a note has been # recorded in the previous sequence, begin a new note # Otherwise, just add the character elif IsPreNote(char): if self.currentNote.containsNote(): self.phraseChain.addNote(self.currentNote) self.currentNote = Note(self.noteLength) self.currentNote.addChar(char) # If the character is a note, do the same as PreNote above elif IsNote(char): if self.currentNote.containsNote(): self.phraseChain.addNote(self.currentNote) self.currentNote = Note(self.noteLength) self.currentNote.addChar(char) # If the character comes after a note, it will only be added # if a note has been recorded. This may lose some information. elif IsPostNote(char): if self.currentNote.containsNote(): self.currentNote.addChar(char) elif char == '%': # Rest of the line is commented out break elif IsIgnored(char): continue else: print(ord(char)) print("Warning: Unrecognized char [{}] from line [{}]".format( char, line)) def parseFile(self, fileName): if '.abc' not in fileName: print("Warning: The selected file is not a .abc file.") abcFile = open(fileName, 'r') for line in abcFile: if any(x in line for x in [ 'A:', 'B:', 'C:', 'D:', 'E:', 'F:', 'G:', 'H:', 'I:', 'N:', 'O:', 'P:', 'Q:', 'S:', 'W:', 'Z' ]): continue if 'X:' in line: tuneNumber = ParseAbcInfo(line) if tuneNumber != '1': # Begin parsing the new tune print("New tune {}".format(tuneNumber)) elif 'T:' in line: self.title = ParseAbcInfo(line) elif 'R:' in line: self.rhythm = ParseAbcInfo(line) elif 'K:' in line: self.key = ParseAbcInfo(line) elif 'M:' in line: self.setMeter(ParseAbcFraction(line)) elif 'L' in line: self.setNoteLength(ParseAbcFraction(line)) elif line.strip() == '': continue else: self.extractPhrasesFromLine(line)
class Tune: def __init__(self): self.title = '' self.meter = 6.0/8.0 self.rhythm = 'Jig' self.noteLength = 1.0/8.0 self.key = '' self.origin = '' self.currentNote = Note(self.noteLength) self.phraseChain = PhraseChain(self.meter, self.noteLength) def setMeter(self, meter): self.meter = meter self.phraseChain.meter = meter def setNoteLength(self, noteLength): self.noteLength = noteLength self.phraseChain.noteLength = noteLength def GetLengthModifier(self, note): return self.noteLength def extractPhrasesFromLine(self, line): # Detect notes inChord = False for char in line.strip(): # Record everything between " as a single note, as it is a chord if IsChord(char): if inChord: # Close the chord inChord = False else: # Begin a new chord inChord = True self.phraseChain.addNote(self.currentNote) self.currentNote = Note(self.noteLength) self.currentNote.addChar(char) # If a chord is in progress, continue recording that chord until # the chord is closed elif inChord: self.currentNote.addChar(char) # If the character comes before a note and a note has been # recorded in the previous sequence, begin a new note # Otherwise, just add the character elif IsPreNote(char): if self.currentNote.containsNote(): self.phraseChain.addNote(self.currentNote) self.currentNote = Note(self.noteLength) self.currentNote.addChar(char) # If the character is a note, do the same as PreNote above elif IsNote(char): if self.currentNote.containsNote(): self.phraseChain.addNote(self.currentNote) self.currentNote = Note(self.noteLength) self.currentNote.addChar(char) # If the character comes after a note, it will only be added # if a note has been recorded. This may lose some information. elif IsPostNote(char): if self.currentNote.containsNote(): self.currentNote.addChar(char) elif char == '%': # Rest of the line is commented out break elif IsIgnored(char): continue else: print(ord(char)) print("Warning: Unrecognized char [{}] from line [{}]" .format(char, line)) def parseFile(self, fileName): if '.abc' not in fileName: print("Warning: The selected file is not a .abc file.") abcFile = open(fileName, 'r') for line in abcFile: if any(x in line for x in [ 'A:', 'B:', 'C:', 'D:', 'E:', 'F:', 'G:', 'H:', 'I:', 'N:', 'O:', 'P:', 'Q:', 'S:', 'W:', 'Z']): continue if 'X:' in line: tuneNumber = ParseAbcInfo(line) if tuneNumber != '1': # Begin parsing the new tune print("New tune {}".format(tuneNumber)) elif 'T:' in line: self.title = ParseAbcInfo(line) elif 'R:' in line: self.rhythm = ParseAbcInfo(line) elif 'K:' in line: self.key = ParseAbcInfo(line) elif 'M:' in line: self.setMeter(ParseAbcFraction(line)) elif 'L' in line: self.setNoteLength(ParseAbcFraction(line)) elif line.strip() == '': continue else: self.extractPhrasesFromLine(line)
def test_note_length(self): baseNoteLength = 1.0 / 8.0 testBasic = Note(baseNoteLength) testBasic.addChar('F') self.assertEqual(testBasic.getLength(), 1 * baseNoteLength) testBasic.addChar('3') self.assertEqual(testBasic.getLength(), 3 * baseNoteLength) testBasic.addChar('/') self.assertEqual(testBasic.getLength(), 3.0 / 2.0 * baseNoteLength) testBasic.addChar('2') self.assertEqual(testBasic.getLength(), 3.0 / 2.0 * baseNoteLength) numberlessFraction = Note(baseNoteLength) numberlessFraction.addChar('D') numberlessFraction.addChar('/') self.assertEqual(numberlessFraction.getLength(), 1.0 / 2.0 * baseNoteLength) numberlessFraction.addChar('/') self.assertEqual(numberlessFraction.getLength(), 1.0 / 4.0 * baseNoteLength) testDotted = Note(baseNoteLength) testDotted.addChar('=') testDotted.addChar('c') testDotted.addChar('>') testDotted.addChar('!') self.assertTrue(testDotted.isDotted()) self.assertFalse(testDotted.isHalved()) self.assertEqual(testDotted.getLength(), 1.5 * baseNoteLength)