def lab2target_single(lab): if lab == "N" or lab == "X": return C.LABEL_N root, _, bass = chord.encode(lab, reduce_extended_chords=True) _, quality, _, _ = chord.split(lab, reduce_extended_chords=True) target = quality_triad_map[quality] * 12 + root return target
def encode_chordseq_hierarchical(seq): vec = np.zeros((len(seq), 6), dtype=np.int32) for i in range(len(seq)): lab = seq[i] if (lab != "N"): root, bitmap, bass = chord.encode(lab, reduce_extended_chords=False) _, quality, scale, _ = chord.split(lab, reduce_extended_chords=False) vec[i, 0] = quality_triad_map[quality] * 12 + root vec[i, 1] = (root + bass) % 12 vec[i, 2] = quality_sevenths_map[quality] vec[i, 3] = quality_ninths_map[quality] vec[i, 4] = quality_elevenths_map[quality] vec[i, 5] = quality_thirteenths_map[quality] """ for sc in scale: tone = chord.scale_degree_to_semitone(sc) if tone in [9,10,11]: vec[i,2] = tone - 9 elif tone in [13,14,15]: vec[i,3] = tone - 13 elif tone in [17,18]: vec[i,4] = tone - 17 elif tone in [20,21]: vec[i,5] = tone - 20 """ elif lab == "N" or lab == "X": vec[i, :] = [C.LABEL_N, 12, 3, 3, 2, 2] return vec
def evaluate_feature(feat, labstr): root, bitmap, bass = chord.encode(labstr, reduce_extended_chords=True) bitmap = bitmap.astype(np.float32) vec_bass = np.zeros(12, dtype=np.float32) vec_bass[(root + bass) % 12] = 1 vec_template = np.concatenate((vec_bass, bitmap)) score = 1 - cosine(feat, vec_template) return score
def shift_label(l, shift): if l in ["N", "X"]: return l if shift == 0: return l l_split = chord.split(l) l_root, _, _ = chord.encode(l) l_root = (l_root + shift) % 12 l_root_str = notes[l_root] return chord.join(l_root_str, l_split[1], l_split[2], l_split[3])
def generate_templates(vocab=157): chord_labels = [] for qual in labels.QUALITIES[vocab][:-1]: for root in labels.ROOTS: chord_labels.append("%s:%s" % (root, qual)) chord_labels.append("N") templates = np.zeros([vocab, 12]) for idx, label in enumerate(chord_labels): root, semitones, bass = chord_eval.encode(label) templates[idx, :] = chord_eval.rotate_bitmap_to_root(semitones, root) templates[-1, :] = 0.0001 return templates
def lab2target(seq, module=np): seq_target = [] for lab in seq: if lab == "N": target = C.LABEL_N else: _, quality, _, _ = chord.split(lab, reduce_extended_chords=True) root, _, bass = chord.encode(lab, reduce_extended_chords=True) quality_id = quality_triad_map[quality] target = quality_id * 12 + root seq_target.append(target) return module.array(seq_target, dtype=np.int32)
def filter_label_seq(labs, shift=0): new_seq = [] for l in labs: if l == "N" or l == "X": l_new = l else: sp = chord.split(l) root, _, _ = chord.encode(l) if shift != 0: root_new = (root + shift) % 12 sp[0] = notes[root_new] l_new = chord.join(sp[0], sp[1], None, sp[3]) new_seq.append(l_new) return new_seq
def template_matching_matrix(feat, lablist): templates = [] for lab in lablist: root, bitmap, bass = chord.encode(lab, reduce_extended_chords=True) bitmap = bitmap.astype(np.float32) vec_bass = np.zeros(12, dtype=np.float32) vec_bass[(root + bass) % 12] = 1 vec_template = np.concatenate((vec_bass, bitmap)) templates.append(vec_template) templates = np.stack(templates, axis=1) xp = C.cupy.get_array_module(feat) prob_mat = xp.matmul(feat, xp.asarray(templates)) prob_mat_logz = xp.log(xp.sum(xp.exp(prob_mat), axis=1, keepdims=True)) prob_mat -= prob_mat_logz return prob_mat
def LoadChromaTarget(labfile): f = open(labfile) line = f.readline() labarr = np.zeros((600 * const.SR // const.H, 12), dtype="int32") bassarr = np.zeros(labarr.shape, dtype="int32") while line != "" and line.isspace() == False: items = line.split() st = int(round(float(items[0]) * const.SR / const.H)) ed = int(round(float(items[1]) * const.SR / const.H)) lab = items[2] root, bitmap, bass = evalchord.encode(lab) chroma = evalchord.rotate_bitmap_to_root(bitmap, root) bassnumber = (root + bass) % 12 labarr[st:ed, :] = chroma bassarr[st:ed, bassnumber] = 1 line = f.readline() return np.concatenate((bassarr[:ed], labarr[:ed]), axis=1).astype(np.float32)
def template_matching_matrix_along_class(feat): templates = [] lablist = [] lablist.extend(notes) lablist.extend([n + ":min" for n in notes]) lablist.append("N") for lab in lablist: root, bitmap, bass = chord.encode(lab, reduce_extended_chords=True) bitmap = bitmap.astype(np.float32) vec_bass = np.zeros(12, dtype=np.float32) vec_bass[(root + bass) % 12] = 1 vec_template = np.concatenate((vec_bass, bitmap)) templates.append(vec_template) templates = np.stack(templates, axis=1) xp = C.cupy.get_array_module(feat) prob_mat = xp.exp(xp.matmul(feat, xp.asarray(templates))) prob_mat_sum = xp.sum(prob_mat, axis=1, keepdims=True) prob_mat = prob_mat / prob_mat_sum return prob_mat
ref_intervals, ref_labels, est_intervals, est_labels) durations = mir_eval.util.intervals_to_durations(intervals) ref_labels_id = encode_chordseq_hierarchical(ref_labels) est_labels_id = encode_chordseq_hierarchical(est_labels) for i in range(len(ref_labels)): if durations[i] > 0.5 and ref_labels_id[ i, 0] == ref and est_labels_id[i, 0] == est: list_examples.append((labfile, ref, est, intervals[i])) return list_examples template_matrix = np.zeros((C.N_VOCABULARY_TRIADS, 36), dtype=np.float32) lablist = class2label(range(C.N_VOCABULARY_TRIADS)) for i in range(C.N_VOCABULARY_TRIADS): root, bitmap, bass = chord.encode(lablist[i], reduce_extended_chords=True) template_matrix[i, root] = 1 template_matrix[i, 12:24] = np.roll(bitmap, root) template_matrix_zero_center = np.array(template_matrix) template_matrix_zero_center[:, : 24] = template_matrix_zero_center[:, :24] * 4 - 2 """ template_matrix = np.zeros((C.N_VOCABULARY_TRIADS,C.N_DIMS_FEAT),dtype=np.float32) + 1e-2 lablist = class2label(range(C.N_VOCABULARY_TRIADS)) for i in range(C.N_VOCABULARY_TRIADS): root,bitmap,bass = chord.encode(lablist[i],reduce_extended_chords=True) templ = np.roll(bitmap,root) template_matrix[i,:] = np.concatenate([templ for _ in range(C.N_DIMS_FEAT//12)]) """ label_shifter = np.zeros((C.N_VOCABULARY_TRIADS, C.N_VOCABULARY_TRIADS,