def test_transpose_slash(self): c = Chord("Am7/G") c.transpose(3) self.assertEqual(c.root, "C") self.assertEqual(c.quality.quality, "m7") self.assertEqual(c.on, "Bb") self.assertEqual(c, Chord("Cm7/Bb"))
def main(): file = 'data/chord.csv' if os.path.isfile(file): with open(file, 'r') as f: reader = csv.reader(f) chords = list(reader) data = [] pbar = tqdm.tqdm(total=len(chords) * 12) for num, title, chord in chords: for k in range(0, 12): note = [] for j in chord.split(): if 'N,C' in j or 'N.C' in j or '>' in j or j[ 0] == '/' or j[: 2] == '(/' or '↓' in j or j == '-' or j == '': continue if j[0] == '(' and j[-1] == ')': j = j[1:-1] elif not '(' in j and j[-1] == ')': j = j[:-1] elif not ')' in j and j[0] == '(': j = j[1:] try: chord1 = Chord(j) chord1.transpose(k) notes = chord1.components(False) except Exception as e: # print(e, 'error', j) notes = [] note.append(notes) data.append(note) pbar.update(1) pbar.close() return data
def set_guitar_code(ori_key, capo_fret=capo_fret_loc): ok = ori_key.split('_') baseChord = ok[0].replace('s', '#') etcChord = ''.join(ok[1:]) c = Chord(baseChord) c.transpose(-capo_fret) # print(str(c) + '_' + etcChord) return (str(c).replace('#', 's') + '_' + etcChord)
def oneshot(chords=None): a = dict([(j, i) for i, j in list(enumerate(QUALITY_DICT.keys()))]) file = 'data/ufret.csv' if chords is None: with open(file, 'r') as f: reader = csv.reader(f) chords = list(reader) data = [] pbar = tqdm.tqdm(total=len(chords) * 12) for num, title, hito, chord in chords: for k in range(0, 12): note = [] for j in chord.split('|'): if '♭' in j: j = j.replace('♭', 'b') if 'N.C' in j: continue try: chord1 = Chord(j) chord1.transpose(k) notes = 1 + len(QUALITY_DICT) * NOTE_VAL_DICT[chord1.root] + \ dict([(j,i) for i, j in list(enumerate(QUALITY_DICT.keys()))])[chord1.quality.quality] # max: 1 + 40 * 11 + 40 = 481 except Exception as e: notes = 0 note.append(notes) if len(note) < 10: pass if len(data) == 373: pass data.append(note) pbar.update(1) pbar.close() file = 'data/ufret_emb.csv' with open(file, 'w') as f: writer = csv.writer(f, lineterminator='\n') # 改行コード(\n)を指定しておく writer.writerows(data) # 2次元配列も書き込める return data
def test_transpose_zero(self): c = Chord("Am") c.transpose(0) self.assertEqual(c.root, "A") self.assertEqual(c.quality.quality, "m") self.assertEqual(c, Chord("Am"))
def test_transpose_eq2(self): c = Chord("C") c.transpose(2) self.assertEqual(c, Chord("D"))
def test_transpose_eq1(self): c = Chord("C") c.transpose(1) self.assertEqual(c, Chord("C#")) self.assertEqual(c, Chord("Db"))
def test_transpose_negative(self): c = Chord("Am") c.transpose(-4) self.assertEqual(c.root, "F") self.assertEqual(c.quality.quality, "m") self.assertEqual(c, Chord("Fm"))
def test_transpose_positive(self): c = Chord("Am") c.transpose(3) self.assertEqual(c.root, "C") self.assertEqual(c.quality.quality, "m") self.assertEqual(c, Chord("Cm"))
def set_guitar_code(ori_key, transpose="", capo_fret=0): c = Chord(ori_key) c.transpose(-capo_fret) return c
def generate_chord_prog(artist, key, start=None): """ Generate chord progression :param str artist: name of artist :param str key: key scale :param str s: chords what chord progression start with :rtype: list[str] :return: list of chord progression """ if key in major_keys: ks = key elif key in minor_keys: ks = minor_major_keys.get(key) else: ks = "C" # TODO: artistによって参照するモデルを変更するようにする model_path = "daw/lib/result/chordnet.model" n_unit = 256 # FIXME: 学習前段階でコード辞書をファイル化すべき _, _, c2i, i2c = load_dataset() eos_id = c2i[_EOS] model = ChordNet(len(c2i), n_unit) chainer.serializers.load_npz(model_path, model) sChArr = [] if start: for ch in start.split(" "): try: sChord = Chord(ch) sChord.transpose(12 - note_to_val(ks), ks) except ValueError: pass sChArr.append(sChord.chord) while True: s = _BOS + " " + " ".join(sChArr) chord_ids = [c2i[ch] for ch in s.strip().split(" ")] with chainer.using_config("train", False): model.reset_state() for chord_id in chord_ids[:-1]: xs = Variable(np.array([chord_id], dtype=np.int32)) model(xs) for i in range(_MAX_LEN): xs = Variable(np.array([chord_ids[-1]], dtype=np.int32)) cid = sample(F.softmax(model(xs))[0].data) # cid = np.argmax(F.softmax(model(xs))[0].data) if cid == eos_id: break chord_ids.append(cid) # print(' '.join([i2c[cid] for cid in chord_ids[1:]])) chords = [] for cid in chord_ids[1:]: try: ch = Chord(i2c[cid]) ch.transpose(note_to_val(ks), ks) chords.append(ch.chord) except ValueError: break if len(chords) == _MAX_LEN: break elif len(chords) >= _MAX_LEN: chords = chords[:_MAX_LEN] break return " ".join(chords)
def test_transpose_zero(self): c = Chord("Am") c.transpose(0) self.assertEqual(c.root, "A") self.assertEqual(c.quality.quality, "m")
def test_transpose_negative(self): c = Chord("Am") c.transpose(-4) self.assertEqual(c.root, "F") self.assertEqual(c.quality.quality, "m")
def test_transpose_positive(self): c = Chord("Am") c.transpose(3) self.assertEqual(c.root, "C") self.assertEqual(c.quality.quality, "m")
def generate_chord_prog(artist, key, start=None): """ Generate chord progression :param str artist: name of artist :param str key: key scale :param str start: chords what chord progression start with :rtype: str :return: chord progression """ if key in major_keys: ks = key elif key in minor_keys: ks = minor_major_keys.get(key) else: ks = "C" model_path = "daw/lib/models/chordnet.model" if artists.get(artist): model_path = model_path.replace('chordnet', artists.get(artist)) n_unit = 256 # ファイル化したコード辞書をロードする c2i_path = 'daw/lib/dicts/chordnet_c2i.pkl' i2c_path = 'daw/lib/dicts/chordnet_i2c.pkl' if artists.get(artist): c2i_path = c2i_path.replace('chordnet', artists.get(artist)) i2c_path = i2c_path.replace('chordnet', artists.get(artist)) with open(c2i_path, 'rb') as f: c2i = pickle.load(f) with open(i2c_path, 'rb') as f: i2c = pickle.load(f) eos_id = c2i[_EOS] model = ChordNet(len(c2i), n_unit) chainer.serializers.load_npz(model_path, model) sChArr = [] if start: for ch in start.split(" "): try: sChord = Chord(ch) sChord.transpose(12 - note_to_val(ks), ks) except ValueError: pass sChArr.append(sChord.chord) while True: s = _BOS + " " + " ".join(sChArr) chord_ids = [c2i[ch] for ch in s.strip().split(" ")] with chainer.using_config("train", False): model.reset_state() for chord_id in chord_ids[:-1]: xs = Variable(np.array([chord_id], dtype=np.int32)) model(xs) for i in range(_MAX_LEN): xs = Variable(np.array([chord_ids[-1]], dtype=np.int32)) cid = sample(F.softmax(model(xs))[0].data) # cid = np.argmax(F.softmax(model(xs))[0].data) if cid == eos_id: break chord_ids.append(cid) # print(' '.join([i2c[cid] for cid in chord_ids[1:]])) chords = [] for cid in chord_ids[1:]: try: ch = Chord(i2c[cid]) ch.transpose(note_to_val(ks), ks) chords.append(ch.chord) except ValueError: break if len(chords) == _MAX_LEN: break elif len(chords) >= _MAX_LEN: chords = chords[:_MAX_LEN] break return " ".join(chords)
def process( stringToProcess, processed): global songHalfTones #print 'String to process "' + stringToProcess + '".' afterSplit = re.split(" |_|!|\.\.\.|\.\.|: |\*|high|open|bass|riff|palm mute|notes|m6|madd11/|m7add11/|7sus2|8|m7b5|madd13|add13", stringToProcess, 1) # 3rd parameter is maxsplit # Also works with single space, do this to catch faulty txt. #print '* Split by delimiters "' + str(afterSplit) + '".' #print 'songHalfTones:',songHalfTones if len(afterSplit[0]) != 0: chord = Chord(afterSplit[0]) #print '* Extracted "' + chord.chord + '" chord.' chord.transpose( songHalfTones, "C#" ) #print '* Transposed to "' + chord.chord + '" chord.' processed += chord.chord #print '* Processed after chord "' + processed + '".' #else: #print '* No chord to extract.' if len(afterSplit) == 1: return processed delimiterWas = '' if len(afterSplit[1]) == 0: delimiterWas = stringToProcess[len(afterSplit[0]):] else: delimiterWas = stringToProcess[len(afterSplit[0]):-len(afterSplit[1])] #print '* Delimiter was "' + delimiterWas + '".' processed += delimiterWas #print '* Processed after delimiter "' + processed + '".' #print '* Still must process "' + afterSplit[1] + '".' return process( afterSplit[1], processed )