示例#1
0
class LongWebW(QWidget):
    def __init__(self):
        super(LongWebW, self).__init__()
        self.grid = QGridLayout(self)
        self.osc = OSC()
        self.osc.clear()
        self.view_list = [LongWebView(), LongWebView()] 
        self.view_list[0].setSizePolicy(QSizePolicy.MinimumExpanding,QSizePolicy.Preferred)
        self.view_list[1].setSizePolicy(QSizePolicy.MinimumExpanding,QSizePolicy.Preferred)
        self.onData_list = [[], []] # [[(cur_set, onset),], [(cur_set, onset),]
        self.offData_list = [[], []] # [[(cur_set, offset),], [(cur_set, offset),]
        self.onOff_list = [[], []]
        self.grid.addWidget(self.view_list[0], 0,0,1,1)
        self.grid.addWidget(self.view_list[1], 1,0,1,1)
        self.setMinimumWidth(1200)
        self.setFixedHeight(700)
        self.cur_notes_set = set()
        self.lock = threading.RLock()
        self.idx = 0
        self.auto = False
        self.renewParserT("molihua.abc")

    def closeEvent(self, event):
        print("longwebw close!!")
        self.auto = False
        self.clear()
        self.osc.clear() 

    def clear(self):
        for note in copy.copy(self.cur_notes_set):
            self.noteOff(note)

    def mousePressEvent(self, event):
        print("clicked")
        if self.auto: 
            self.auto = False
            return
        t = threading.Timer(0.1, self.autoPlay)
        t.start()

    def renewParserT(self, file_name):
        self.view_list[0].refresh()
        self.view_list[1].refresh()
        timer = threading.Timer(2 ,self.renewParser, [file_name])
        timer.start()

    def renewParser(self, file_name):
        self.idx = 0;
        self.parser = Parser(file_name)
        self.view_list[0].set_m_k(self.parser.time_sign, self.parser.key_sign).draw_stave()
        self.view_list[1].set_m_k(self.parser.time_sign, self.parser.key_sign).draw_stave()
        self.give_data(0)
        self.give_data(1)
        self.view_list[0].draw_line()
   
    def noteOn(self, nstr, checkMove=True):
        with self.lock:
            self.cur_notes_set.add(nstr)
            if checkMove: self.check_move()
        NIdx = VStrToNIdx(nstr)
        if NIdx >=0: self.osc.noteOn(NIdx)
        

    def noteOff(self, nstr):
        with self.lock:
            if nstr in self.cur_notes_set:
                self.cur_notes_set.remove(nstr)
        NIdx = VStrToNIdx(nstr)
        if NIdx >=0: self.osc.noteOff(NIdx)

    def check_move(self):
        if self.onData_list[self.idx] != [] and self.onData_list[self.idx][0][0].issubset(self.cur_notes_set): # self.cur_notes_set  == self.onData_list[self.idx][0][0]:
            # have to move
            self.onData_list[self.idx].pop(0)
            self.view_list[self.idx].move()
            if self.onData_list[self.idx] == []:
                # last element, have to fetch
                self.give_data(self.idx)
                self.idx = (self.idx+1)%2
                if self.onData_list[self.idx] != []:
                    self.view_list[self.idx].draw_line()

    def set_move_speed(self, speed):
        self.view_list[0].set_move_speed(speed)
        self.view_list[1].set_move_speed(speed)

    def autoPlay(self):
        if self.onData_list[self.idx] == []: return
        self.auto = True
        cur_onset, cur_onval = self.onData_list[self.idx][0]
        ooIdx = 0
        # Jump to current onoff Idx
        while ooIdx < len(self.onOff_list[self.idx]) and self.onOff_list[self.idx][ooIdx][0] != cur_onval:
            ooIdx += 1
            print("ooIdx = " + str(ooIdx))
        start_time = time.time(); cur_sidx = self.idx
        start_val = cur_onval
        self.clear()
        # Start playing
        while self.auto == True and (ooIdx < len(self.onOff_list[self.idx]) or cur_sidx != self.idx):
            if cur_sidx != self.idx: # Rotate to next idx
                ooIdx = 0; cur_sidx = self.idx
                print("Cur Notes Set:::::::" + str(self.cur_notes_set))
                print(self.onOff_list[self.idx][ooIdx:])
                if len(self.onOff_list[self.idx]) == 0: break # No onoff any more
            val, onset, offset = self.onOff_list[self.idx][ooIdx]
            # Sleep until the val
            self.sleep(start_time, start_val, val, self.parser.speed)
            if ooIdx == 0: self.clear()
            with self.lock:
                for each in offset:
                    self.noteOff(each)
                for each in onset:
                    self.noteOn(each, False)
                self.check_move()
            ooIdx += 1
        self.auto = False

    def sleep(self, start_time, start_val ,val, speed):
        target_time = start_time  + (val - start_val)/speed*60
        while time.time() < target_time:
            time.sleep(0.01) 

    def give_data(self, idx):
        data = self.parser.getNext()
        self.build_data_list(data, idx)
        self.build_view_list(data, idx)

    def build_data_list(self, data, idx):
        tre_data = data['tre_notes_raw']
        bas_data = data['bas_notes_raw']
        self.onData_list[idx] = merge2list(tre_data, bas_data,
                                    lambda cur_data: cur_data[0][3],
                                    addToSet, 
                                    lambda res, key, set1, set2: res.append((set1.union(set2), key)),
                                    set(["|"]))
        print("onData_list!!!!!!!!!!!!!")
        print(self.onData_list)
        self.offData_list[idx] = merge2list(tre_data, bas_data,
                                    lambda cur_data: cur_data[0][4],
                                    addToSet,
                                    lambda res, key, set1, set2: res.append((set1.union(set2), key)),
                                    set(["|"]))
        def res_onoff(cur_set, cur_data):
            for each in cur_data[0]:
                cur_set.add(each)
            print(cur_set)
        def append_onoff(res, key, cur_set1, cur_set2):
            print(key, cur_set1, cur_set2)
            res.append((key, cur_set1, cur_set2))
        self.onOff_list[idx] = merge2list(self.onData_list[idx], self.offData_list[idx],
                                    lambda cur_data: cur_data[1],
                                    res_onoff, # lambda cur_set, cur_data: cur_set.union(cur_data[0]),
                                    append_onoff)
        print("onOff_list!!!!!!!!!!!!!")
        print(self.onOff_list)

    def build_view_list(self, data, idx):
        view = self.view_list[idx]
        view.draw_stave()
        view.draw_notes(data)