Пример #1
0
async def echo(websocket, path):
    print("echo!!!")
    async for message in websocket:
        print("message!!!")
        try:
            ans = json.loads(message)
            print(ans)
            seq_number = ans['seq']
            music = ans['music']
            chord_length = ans['chordLength']
            jazziness = ans['jazziness']
            first_weight = ans['firstWeight']
            determinism_weight = ans['determinismWeight']
            seed = ans['seed']
            bottom_bass = ans['bottomBass']

            # this is a p bad name tbh
            constraints = ans[
                'constraints']  # Optional[List[{'time': float, 'value': str, 'locked': bool}]]
            raw_notes = music['notes']
            key_signature = ans['keySignature']
            minorness = ans['minorness']
            tolerance = ans['tolerance']
            mode = ans['mode']
            preserve = ans['preserve']  # preserve even unlocked stuff

            midi_root_of_major = key_signature * 7 % 12
            # even for relative minor we're going to use the major root for simplicity;
            # we can transpose all the data, so it's fine.
            if not constraints:
                last_end = max(note['end'] for note in raw_notes)
                constraints = [{
                    'time': i * chord_length,
                    'locked': False
                } for i in range(1 + int(last_end // chord_length))]
                preserve = False

            grouped_notes = []

            notes_ix = 0
            # get all but last measure
            for constraint, next_constraint in zip(constraints,
                                                   constraints[1:]):
                start = constraint['time']
                end = next_constraint['time']

                group = []
                while notes_ix < len(raw_notes) and raw_notes[notes_ix][
                        'start'] < end - tolerance:
                    group.append(
                        (raw_notes[notes_ix]['pitch'] - midi_root_of_major) %
                        12)
                    notes_ix += 1
                grouped_notes.append(group)

            # followed by last group
            last_group = []
            while notes_ix < len(raw_notes):
                last_group.append(
                    (raw_notes[notes_ix]['pitch'] - midi_root_of_major) % 12)
                notes_ix += 1
            grouped_notes.append(last_group)

            assert len(grouped_notes) == len(constraints)

            print('------------------------')
            print('constraints:', constraints)
            locked_chords = [
                Chord.parse(
                    constraint['value']).absolute_to_relative(key_signature)
                if constraint['locked'] else None for constraint in constraints
            ]

            if preserve:
                preserve_chords = [
                    Chord.parse(constraint['value']).absolute_to_relative(
                        key_signature) for constraint in constraints
                ]
            else:
                preserve_chords = None

            if mode == 'major': stat_set_list = [(1.0, major_stat_set)]
            elif mode == 'parallel-minor':
                stat_set_list = [(1.0, parallel_minor_stat_set)]
            elif mode == 'relative-minor':
                stat_set_list = [(1.0, relative_minor_stat_set)]
            elif mode == 'mixed-parallel':
                stat_set_list = [(1.0 - minorness, major_stat_set),
                                 (minorness, parallel_minor_stat_set)]
            elif mode == 'mixed-relative':
                stat_set_list = [(1.0 - minorness, major_stat_set),
                                 (minorness, relative_minor_stat_set)]
            else:
                stat_set_list = [(1.0, major_stat_set)]  # ?????

            chords = linearly_mixed_hmm_predict(
                stat_set_list,
                grouped_notes,
                locked_chords,
                preserve_chords,
                jazziness=jazziness,
                seed=seed,
                first_note_weight=first_weight,
                determinism_weight=determinism_weight)
            res = []
            for i, ((chord_score, chord), suggestion,
                    scored_chord_list) in enumerate(chords):
                res.append({
                    'time':
                    constraints[i]['time'],
                    'value':
                    productionize_chord(chord, key_signature, chord_score,
                                        bottom_bass),
                    'suggestion':
                    productionize_chord(suggestion[1], key_signature,
                                        suggestion[0], bottom_bass)
                    if suggestion else None,
                    'locked':
                    i < len(locked_chords) and locked_chords[i] is not None,
                    'recommendations': [
                        productionize_chord(c, key_signature, s, bottom_bass)
                        for (s, c) in scored_chord_list
                    ],
                })
            print('------------------------')
            print('result:', res)
            await websocket.send(
                json.dumps({
                    'seq':
                    seq_number,
                    'allChords': [
                        productionize_chord(c, key_signature, 0, bottom_bass)
                        for c in all_chords
                    ],
                    'result':
                    res,
                }))
        except Exception as e:
            print(e)
            traceback.print_exc()
            await websocket.send(json.dumps({'error': traceback.format_exc()}))
Пример #2
0
import sys
from chord import Chord
from fingerboard import Fingerboard

args = sys.argv

chordName = args[1]
if len(args) > 2:
    tensions = args[2:]

tones = Chord.parse(chordName, tensions)

inv = {v: k for k, v in tones.items()}
print ""
print "%s %s" % (chordName, " ".join(tensions))
print ""
for key in sorted(inv.keys()):
    print " %3s: %2s" % (key, inv[key])

Fingerboard.dump(tones)
Пример #3
0
import sys
from chord import Chord
from fingerboard import Fingerboard

args = sys.argv

chordName = args[1]
if len(args) > 2:
    tensions = args[2:]

tones = Chord.parse(chordName, tensions)

inv = {v:k for k, v in tones.items()}
print ""
print "%s %s" % (chordName, " ".join(tensions))
print ""
for key in sorted(inv.keys()):
    print " %3s: %2s" % (key, inv[key])

Fingerboard.dump(tones)