def compose(): try: user_id = session['user_id'] title = request.form['title'] data = json.loads(request.form['data']) if not isinstance(data, list): return 'Error' with mido.MidiFile(ticks_per_beat=48, charset='utf-8') as midi: for tune in data: pass ts = Rhythm.TimeSignature(int(tune['nn']), int(tune['dd'])) chord_id = tune['chord_id'] chords_db = Chords.query.filter_by(id=chord_id).first() if not chords_db: return ('Chords DB Error', 500) prog = Chord.ChordProg.from_dict(json.loads(chords_db.data)) rhythm_id = tune['rhythm_id'] rhythms_db = Rhythms.query.filter_by(id=rhythm_id).first() if not rhythms_db: return ('Rhythms DB Error', 500) rhythm_tree = Rhythm.RhythmTree.from_dict(json.loads(rhythms_db.data)) note_range = range(int(tune['min_note']), int(tune['max_note'])) skip_prob = float(tune['skip_prob']) bpm = int(tune['bpm']) bars = Lyrics.divide(tune['phoneme'], rhythm_tree) beats = Lyrics.pair(bars, rhythm_tree) composer = Melody.Composer(ts, beats, prog, note_range, skip_prob, bpm) midi = Melody.concat_midi(midi, composer.compose()) savepath = Util.random_string(16) midi.save(app.config['MEDIA_FOLDER'] + '/' + savepath + '.mid') os.system('timidity %s.mid -Ow -o - | lame - -b 64 %s.mp3' % (app.config['MEDIA_FOLDER'] + '/' + savepath, app.config['MEDIA_FOLDER'] + '/' + savepath)) music = Musics(title, savepath, request.form['data'], user_id) db.session.add(music) db.session.commit() except ValueError as e: return ('Error: %s' % e, 500) except KeyError as e: return ('Error: %s' % e, 500) return (jsonify({'music_id': music.id}), 200)
[0, 6, 12, 18, 24, 36], [0, 6, 12, 18, 24, 30, 36], [0, 6, 12, 18, 24, 30, 36, 42] ] rhythm_tree = Rhythm.RhythmTree(12, 1, Rhythm.TimeSignature(4, 2), rhythms) lyric = u'ア^イウ/ア^イウエ/ア^イウエ_オ/ /カキク/ / /' bars = Lyrics.divide(lyric, rhythm_tree) bars.should.be.equal([[u'ア^イウ', u'ア^イウエ'], [u'ア^イウエ_オ'], [u'カキク'], []]) pairs = [ (u'ア^', 0.0), (u'イ', 0.125), (u'ウ', 0.25), (u'ア^', 0.375), (u'イ', 0.5), (u'ウ', 0.625), (u'エ', 0.75), (u'ア^', 1.0), (u'イ', 1.125), (u'ウ', 1.25), (u'エ_', 1.5), (u'オ', 1.75), (u'カ', 2.25), (u'キ', 2.5), (u'ク', 2.75), ] beats = Lyrics.pair(bars, rhythm_tree) beats.division.should.be.equal(rhythm_tree.division) beats.time.should.be.equal(4) beats.pairs.should.be.equal(pairs)
from datetime import datetime as dt import mido import sure import melete.lyrics as Lyrics import melete.rhythm as Rhythm import melete.chord as Chord import melete.melody as Melody ts = Rhythm.TimeSignature(4, 2) rhythms = [[0], [0, 96], [48, 96, 144], [0, 48, 96, 144]] tree = Rhythm.RhythmTree(48, 1, ts, rhythms) text = u'ぽよぽよぽよぽよぽよぽよぽよぽよ===あるう ひ もりのな か くまさん に であっ た あるう ひ もりのな か くまさん に であっ た' lyrics = Lyrics.analyze(text) lyrics = map(lambda l: Lyrics.divide(l['phoneme'], tree), lyrics) beats = map(lambda l: Lyrics.pair(l, tree), lyrics) c = Chord.Chord.from_name('C') f = Chord.Chord.from_name('F') f.inversion(1) gsus4 = Chord.Chord.from_name('Gsus4') gsus4.inversion(1) prog = Chord.ChordProg(48, 4, [(c, 0), (f, 192), (gsus4, 384), (c, 576)]) note_range = range(Chord.Scale.from_name('C4').note, Chord.Scale.from_name('A5').note) with mido.MidiFile(ticks_per_beat=48, charset='utf-8') as midi: for beat in beats: composer = Melody.Composer(ts, beat, prog, note_range, 0.5, 180) midi = Melody.concat_midi(midi, composer.compose()) savepath = ''.join([random.choice(string.ascii_letters + string.digits) for i in range(16)]) + '.mid'
import os, string, random import sure import melete.lyrics as Lyrics import melete.rhythm as Rhythm import melete.chord as Chord import melete.melody as Melody ts = Rhythm.TimeSignature(4, 2) rhythms = [[0], [0, 96], [48, 96, 144], [0, 48, 96, 144]] tree = Rhythm.RhythmTree(48, 1, ts, rhythms) text = u'ぽよぽよぽよぽよぽよぽよぽよぽよ===あるう ひ もりのな か くまさん に であっ た あるう ひ もりのな か くまさん に であっ た' lyrics = Lyrics.analyze(text) lyrics = map(lambda l: Lyrics.divide(l['phoneme'], tree), lyrics) beats = map(lambda l: Lyrics.pair(l, tree), lyrics) c = Chord.Chord.from_name('C') f = Chord.Chord.from_name('F') f.inversion(1) gsus4 = Chord.Chord.from_name('Gsus4') gsus4.inversion(1) prog = Chord.ChordProg(48, 4, [(c, 0), (f, 192), (gsus4, 384), (c, 576)]) note_range = Chord.note_range('C4', 'A5') patterns = [{ 0.125: {'length': 0.125, 'notes': [0]}, 0.25: {'length': 0.25, 'notes': [1, 2]}, 0.5: {'length': 0.25, 'notes': [2]}, 0.75: {'length': 0.25, 'notes': [0, 100]},