def testGenerateHarmonycRhythmBar(): hm2 = Metre.createFromLabels("4/4", "quarternote", "halfnote") rhythm = hrg._generateHarmonicRhythmBar(hm2, 0.05) barDuration = sum(i for i, _ in rhythm) expectedBarDuration = 4.0 assert barDuration == expectedBarDuration
def testCalculateScores(): d1 = hrg.rhythmTree d2 = hrg.rhythmTree.children[1] d3 = hrg.rhythmTree.children[1].children[0] d4 = hrg.rhythmTree.children[1].children[0].children[0] hm2 = Metre.createFromLabels("4/4", "quarternote", "halfnote") candidates = [d2, d3, d4] scores = hrg._calcScores(candidates, hm2, 0.05) expectedScores = [1.0, 0.55, 0.35] assert scores == expectedScores
def testRhythmicSpaceIsCreatedCorrectly(): m = Metre("3/4", "quarternote") rsf = RhythmTreeFactory() rs2 = rsf.createRhythmTree(2, m) rs = RhythmTree(3, 0, [RhythmTree(1, 1, [RhythmTree(0.5, 2), RhythmTree(0.5, 2)]), RhythmTree(1, 1, [RhythmTree(0.5, 2), RhythmTree(0.5, 2)]), RhythmTree(1, 1, [RhythmTree(0.5, 2), RhythmTree(0.5, 2)])]) assert str(rs) == str(rs2)
def testInsertTriplet(): rsf = RhythmTreeFactory() m = Metre("4/4", "quarternote") rs = rsf.createRhythmTree(3, m) rs1 = RhythmTree(2, 0, [RhythmTree(1, 1, [RhythmTree(0.5, 2), RhythmTree(0.5, 2)]), RhythmTree(1, 1, [RhythmTree(0.5, 2), RhythmTree(0.5, 2)])]) rsf._insertTriplet(rs.children[0]) assert len(rs.children[0].children) == 3 for child in rs.children[0].children: assert 0.66 < child.duration < 0.67
def testCorrectCandidateDurationsWithNoDotsArePicked(): m5 = Metre("4/4", "quarternote") rsf5 = RhythmTreeFactory() rs5 = rsf.createRhythmTree(2, m5) # starting from root expectedResult5 = [rs5, rs5.children[0], rs5.children[0].children[0]] result5 = rs5._getDurationCandidatesNoDot() assert expectedResult5 == result5 # starting from a node which is last child rs6 = rsf.createRhythmTree(3, m5) expectedResult = [rs6.children[1], rs6.children[1].children[0], rs6.children[1].children[0].children[0]] result = rs6.children[0].children[1].children[1]._getDurationCandidatesNoDot() assert expectedResult == result # starting from a node which is not a last child expectedResult = [rs6.children[0].children[1], rs6.children[0].children[1].children[0]] result = rs6.children[0].children[0]._getDurationCandidatesNoDot() assert expectedResult == result
from musiclib.rhythmgenerator import RhythmGenerator from musiclib.metre import Metre from musiclib.rhythmtree import RhythmTree m = Metre("4/4", "quarternote") r = RhythmGenerator(m) metricalLevels = m.getDurationLevels() r._metricalAccentImpact = [0, 0.1, 0.15, 0.2, 0.3] #TODO: duplicate assignment of this var - maybe you're just saving this one for testing? r._densityImpactDurationLevels = [0, 0.1, 0.3, 0.6, 1] r._tactusScoreByDistance = [1, 0.6, 0.4, 0.2] r._densityImpactDurationLevels = [-0.5, -0.2, 0, 0.6, 1] #TODO, I think these metrical prominence scores could also potentially be #a formula rather than a list - it's really just 1 if durationLevel == metricalAccent #then a decay for those metric levels < metricalAccent #I can see us wanting different numbers, but it seems like it will always #have a shape to it. r._metricalProminenceScores = [[1, 0, 0, 0, 0], [0.7, 1, 0, 0, 0], [0.3, 0.5, 1, 0, 0], [0.1, 0.3, 0.5, 1, 0], [0.05, 0.2, 0.2, 0.5, 1]] def testCompressValues(): l = [1, 0.8, 0.5, 0.1, 0] compressedValues = r.compressValues(0.5, l, 0.1) expectedCompressedValues = [0.95, 0.77, 0.5, 0.14, 0.05] assert compressedValues == expectedCompressedValues """ def testVAfeaturesAreMappedCorrectly():
from musiclib.rhythmtree import RhythmTree from musiclib.rhythmtreefactory import RhythmTreeFactory from musiclib.metre import Metre m3 = Metre("3/4", "quarternote") m4 = Metre("4/4", "quarternote") rsf = RhythmTreeFactory() rs3 = rsf.createRhythmTree(2, m3) probTuplets = [[0.02, 0, 0, 0, 0], [0.03, 0.07, 0, 0, 0], [0.5, 0.5, 0.5, 0, 0], [0.5, 0.5, 0.5, 0.5, 0], [0, 0, 0, 0, 0]] probTupletType = [[7, 7, 7], [7, 7, 7], [7, 7, 7], [1, 0, 0], [0, 0, 0]] def testRhythmicSpaceIsInstantiatedCorrectly(): root = RhythmTree(1, 2) assert root.duration == 1 assert root.durationLevel == 2 def testRhythmicSpaceIsCreatedCorrectly(): m = Metre("3/4", "quarternote")
def testGenerateHarmonicRhythmMU(): hrg.densityImpact = 1 hrg.entropyImpact = 1 hm2 = Metre.createFromLabels("4/4", "quarternote", "halfnote") rhythm = hrg.generateHarmonicRhythmMU(hm2, 0.05, 2)
from musiclib.harmonyrhythmgenerator import HarmonyRhythmGenerator from musiclib.metre import Metre from musiclib.rhythmtree import RhythmTree from musiclib.rhythmdata import rhythmData as rd m = Metre.createFromLabels("4/4", "quarternote", "halfnote") hrg = HarmonyRhythmGenerator.fromModelData(m, rd['harmony']) hrg.densityImpact = 0 hrg.entropyImpact = 0 def testHarmonyRhythmGeneratorIsInstantiated(): hrg = HarmonyRhythmGenerator.fromModelData(m, rd['harmony']) hrg is not None def testCompressValues(): l = [1, 0.8, 0.5, 0.1, 0] compressedValues = hrg.compressValues(0.5, l, 0.1) expectedCompressedValues = [0.95, 0.77, 0.5, 0.14, 0.05] assert compressedValues == expectedCompressedValues def testCalculateScores(): d1 = hrg.rhythmTree d2 = hrg.rhythmTree.children[1] d3 = hrg.rhythmTree.children[1].children[0] d4 = hrg.rhythmTree.children[1].children[0].children[0] hm2 = Metre.createFromLabels("4/4", "quarternote", "halfnote") candidates = [d2, d3, d4]
def testMetreIsInstantiatedCorrectly(): m = Metre("3/4", 1.0, 3.0, 4) m = Metre("3/4", "quarternote") assert m.timeSignature == "3/4" assert m.tactus["label"] == "quarternote" assert m.tactus["durationLevel"] == 1
from musiclib.melodyrhythmgenerator import MelodyRhythmGenerator from musiclib.metre import Metre, calculateDurationSubdivisions from musiclib.rhythmtreefactory import RhythmTreeFactory m = Metre("4/4", "quarternote") r = MelodyRhythmGenerator(m) def testCalcMetricsWork(): d2 = r.rhythmSpace.children[1] d3 = r.rhythmSpace.children[1].children[0] d4 = r.rhythmSpace.children[1].children[0].children[0] r.densityImpact = 0.1 r.entropyImpact = 0.1 candidates = [d2, d3, d4] scores = r._calcScores(candidates, m) #TODO: this test could be invalidated based on model settings. for score in scores: assert 0 <= score <= 2 def testTreeIndexing(): rhythmNode = r.rhythmSpace[(1,0,1)] print(rhythmNode) r.rhythmSpace[(1, 0, 1)] = "woot" rhythmNode = r.rhythmSpace[(1,0,1)] rhythmNode == "woot" def testSubdivisionCalc():