from music21 import *
import numpy as np
import threading


class FuncThread(threading.Thread):
    '''Vlakno s argumentem funkce
    '''
    def __init__(self, target, *args):
        self._target = target
        self._args = args
        threading.Thread.__init__(self)

    def run(self):
        self._target(*self._args)


clef = {0:'C', 1:'D', 2:'E', 3:'F', 4:'G', 5:'A', 6:'B'}
cleftreblenoteshift = -2
cleftrebleoctaveshift = 4
clefbass = {0:'G', 1:'A', 2:'B', 3:'C', 4:'D', 5:'E', 6:'F'}
clefalto = {0:'F', 1:'G', 2:'A', 3:'B', 4:'C', 5:'D', 6:'E'}
cleftenor = {0:'D', 1:'E', 2:'F', 3:'G', 4:'A', 5:'B', 6:'C'}

cleftrebleoctave = 4
clefbassoctave = 2
clefaltooctave = 3
cleftenoroctave = 3

class Parser:
    '''Sestaveni streamu knihovny music21
    Pro 
    '''
    def __init__(self, heads):
        stream1 = stream.Stream()
        stream1.id = 'some notes'
        for head in heads:
            notes = []
            for h in head.segheadlist:
                octave = int(cleftrebleoctaveshift + np.floor((h.pitch-cleftreblenoteshift)/7.0))
                type = clef[(h.pitch-cleftreblenoteshift) % 7]
                n = note.Note(str(type) + str(octave))
                n.duration.quarterLength = 1.0/(1 << head.dur)
                n.dots = 0
                n.pitch.transpose(0, inPlace=True)
                notes.append(n)
            ch = chord.Chord(notes)
            stream1.append(ch)

        t1 = FuncThread(self.show, stream1)
        t1.start()
        #t1.join()

    def show(self, stream):
        stream.show()