Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion AugmentedNet/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class DefaultArguments(object):
"dataAugmentation": True,
"collections": ["bps"],
"testCollections": ["bps"],
"inputRepresentations": ["Bass19", "Chromagram19", "Duration14"],
"inputRepresentations": ["Bass19", "Chromagram19", "MeasureNoteOnset14"],
"outputRepresentations": [
"Alto35",
"Bass35",
Expand Down
142 changes: 44 additions & 98 deletions AugmentedNet/input_representations.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,96 +18,12 @@
FeatureRepresentationTI,
)

# NOTENAMEDEFAULTCLASS = {
# "C": 0,
# "D": 2,
# "E": 4,
# "F": 5,
# "G": 7,
# "A": 9,
# "B": 11,
# }
# def _solvePitchSpelling(noteNames, pitchClasses):
# if len(noteNames) != len(pitchClasses):
# raise Exception
# if not noteNames:
# return
# elif len(noteNames) == 1:
# note = noteNames[0]
# default = NOTENAMEDEFAULTCLASS[note]
# bestMatch = 999999
# for pc in pitchClasses:
# diff = pc -


class Duration14(FeatureRepresentationTI):
features = 2 * len(NOTEDURATIONS)
pattern = [
[1, 0, 0, 0, 0, 0, 0],
[0, 1, 0, 0, 0, 0, 0],
[0, 0, 1, 0, 0, 0, 0],
[0, 1, 1, 0, 0, 0, 0],
[0, 0, 0, 1, 0, 0, 0], # eight
[0, 1, 0, 1, 0, 0, 0],
[0, 0, 1, 1, 0, 0, 0],
[0, 1, 1, 1, 0, 0, 0],
[0, 0, 0, 0, 1, 0, 0], # quarter
[0, 1, 0, 0, 1, 0, 0],
[0, 0, 1, 0, 1, 0, 0],
[0, 1, 1, 0, 1, 0, 0],
[0, 0, 0, 1, 1, 0, 0],
[0, 1, 0, 1, 1, 0, 0],
[0, 0, 1, 1, 1, 0, 0],
[0, 1, 1, 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, 1],
[0, 0, 1, 1, 1, 1, 1],
[0, 1, 1, 1, 1, 1, 1],
]

class MeasureOnset7(FeatureRepresentationTI):
features = len(NOTEDURATIONS)
pattern = [list(reversed(f"{x:06b}0")) for x in range(64)]
pattern = [[int(n) for n in arr] for arr in pattern]
pattern[0][0] = 1

def run(self, transposition=None):
array = np.zeros(self.shape, dtype=self.dtype)
Expand All @@ -118,14 +34,30 @@ def run(self, transposition=None):
idx = 0
prev_measure = measure
pattern = self.pattern[idx]
array[frame, 0:7] = pattern
array[frame] = pattern
idx = min(idx + 1, len(self.pattern) - 1)
return array

@classmethod
def decode(cls, array):
if len(array.shape) != 2 or array.shape[1] != cls.features:
raise IndexError("Strange array shape.")
ret = []
for manyhot in array:
measureOnset = [NOTEDURATIONS[x] for x in np.nonzero(manyhot)[0]]
ret.append(measureOnset)
return ret


class NoteOnset7(MeasureOnset7):
def run(self, transposition=None):
array = np.zeros(self.shape, dtype=self.dtype)
idx = 0
for frame, onset in enumerate(self.df.s_isOnset):
if sum(onset) > 0:
idx = 0
pattern = self.pattern[idx]
array[frame, 7:] = pattern
array[frame] = pattern
idx = min(idx + 1, len(self.pattern) - 1)
return array

Expand All @@ -135,14 +67,28 @@ def decode(cls, array):
raise IndexError("Strange array shape.")
ret = []
for manyhot in array:
measureOnset = [
NOTEDURATIONS[x] for x in np.nonzero(manyhot[:7])[0]
]
noteOnset = [NOTEDURATIONS[x] for x in np.nonzero(manyhot[7:])[0]]
ret.append((tuple(measureOnset), tuple(noteOnset)))
noteOnset = [NOTEDURATIONS[x] for x in np.nonzero(manyhot)[0]]
ret.append(noteOnset)
return ret


class MeasureNoteOnset14(FeatureRepresentationTI):
features = MeasureOnset7.features + NoteOnset7.features
pattern = MeasureOnset7.pattern

def run(self, transposition=None):
self.measure7 = MeasureOnset7(self.df).run(transposition)
self.note7 = NoteOnset7(self.df).run(transposition)
array = np.concatenate((self.measure7, self.note7), axis=1)
return array

@classmethod
def decode(cls, array):
measure7 = MeasureOnset7.decode(array[:, : MeasureOnset7.features])
note7 = NoteOnset7.decode(array[:, NoteOnset7.features :])
return [(tuple(mm), tuple(n)) for mm, n in zip(measure7, note7)]


class Bass19(FeatureRepresentation):
features = len(NOTENAMES) + len(PITCHCLASSES)

Expand Down Expand Up @@ -371,7 +317,7 @@ def decode(cls, array):
"Bass35": Bass35,
"Chromagram19": Chromagram19,
"Chromagram35": Chromagram35,
"Duration14": Duration14,
"MeasureNoteOnset14": MeasureNoteOnset14,
"Intervals39": Intervals39,
"Intervals19": Intervals19,
"BassChromagram38": BassChromagram38,
Expand Down
4 changes: 2 additions & 2 deletions notebooks/thesis/Ch5-LowestNoteEncoding.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"plt.rcParams[\"figure.figsize\"] = (15, 5)\n",
"\n",
"from AugmentedNet.score_parser import parseScore\n",
"from AugmentedNet.input_representations import Bass19, Chromagram19, Bass35, Chromagram35, Duration14"
"from AugmentedNet.input_representations import Bass19, Chromagram19, Bass35, Chromagram35, MeasureNoteOnset14"
]
},
{
Expand Down Expand Up @@ -66,7 +66,7 @@
"bass35 = Bass35(df)\n",
"chroma19 = Chromagram19(df)\n",
"chroma35 = Chromagram35(df)\n",
"duration14 = Duration14(df)"
"duration14 = MeasureNoteOnset14(df)"
]
},
{
Expand Down
14 changes: 7 additions & 7 deletions test/test_input_representations.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
BassChromagram38,
BassChromagram70,
BassIntervals58,
Duration14,
MeasureNoteOnset14,
Intervals19,
)
from AugmentedNet.joint_parser import J_LISTTYPE_COLUMNS
Expand Down Expand Up @@ -39,26 +39,26 @@ def _save(arr):
np.savetxt("tmp.txt", arr, fmt="%i", delimiter=" ")


class TestDuration14(unittest.TestCase):
class TestMeasureNoteOnset14(unittest.TestCase):
def setUp(self):
self.maxDiff = None
self.df = _load_dfgt(aux.haydn)
self.timesteps = len(self.df.index)
self.transpositions = ["m2", "M6", "P5", "d7"]

def test_encoding(self):
encoding = Duration14(self.df).array
encodingGT = np.loadtxt(aux.haydnDuration14GT, dtype="i1")
encoding = MeasureNoteOnset14(self.df).array
encodingGT = np.loadtxt(aux.haydnMeasureNoteOnset14GT, dtype="i1")
for timestep in range(self.timesteps):
with self.subTest(timestep=timestep):
ar = np.nonzero(encoding[timestep])
arGT = np.nonzero(encodingGT[timestep])
self.assertEqual(tuple(ar[0]), tuple(arGT[0]))

def test_decoding(self):
encoding = Duration14(self.df).array
decoded = Duration14.decode(encoding)
decodedGT = aux.haydnDuration14DecodedGT
encoding = MeasureNoteOnset14(self.df).array
decoded = MeasureNoteOnset14.decode(encoding)
decodedGT = aux.haydnMeasureNoteOnset14DecodedGT
decodedGT = [(tuple(t[0]), tuple(t[1])) for t in decodedGT]
for timestep in range(self.timesteps):
with self.subTest(timestep=timestep):
Expand Down