Esempio n. 1
0
    def flat_roll(self, track_range=None):
        if not isinstance(self.midi, AbsoluteMidi):
            self.midi = AbsoluteMidi.to_abs_midi(self.midi)

        N = 128

        # self.note_range = [0, 128] if not note_range  else note_range

        self.track_range = [
            0, self.midi.track_count
        ] if not track_range else track_range if not numpy.isscalar(
            track_range) else [track_range - 1, track_range]

        LEN = 4800

        trackers = numpy.zeros(self.midi.track_count)
        track_anas = meta_event_format.X
        res_set = numpy.zeros((self.midi.track_count, LEN))
        for i in range(self.track_range[0], self.track_range[1]):
            t = self.midi.tracks[i]
            instrument = t.get_event('instrument_name', depth=1)
            try:
                ttempo = t.get_event('set_tempo', depth=1)
                tempo = mutils.toint(ttempo[0].data, 8)
                # tempo = ttempo
            except IndexError:
                #Occurs because set_tempo is not register in every track explicitly, usually specified in first track only and all track follows the same rhythm forward
                #Considering the tempo_event in first track [0] , if there too error raised then tempo == 120 bpm
                try:
                    ttempo = self.midi.tracks[0].get_event('set_tempo',
                                                           depth=1)
                    tempo = mutils.toint(ttempo[0].data, 8)
                except IndexError:
                    tempo = 500000
                pass
            nit = 0  #numpy array iterator
            prev_oucr, dut = 0, 0
            # print(t.trk_event[-11 : -1])
            for e in t.trk_event:
                if e.is_note_on_off_event():
                    noteval = e.data[0]
                    if prev_oucr == e.abstime:
                        nit -= dut

                    dut = int(32 // mutils.nth_note(e.elength, tempo=tempo))
                    prev_oucr = e.abstime
                    res_set[i][nit:nit + dut] += noteval
                    nit += dut
        self.flat_roll = res_set
        return res_set
Esempio n. 2
0
    def flat_notes(self, track_range=None, trim=True, note_range=None):

        if not isinstance(self.midi, AbsoluteMidi):
            self.midi = AbsoluteMidi.to_abs_midi(self.midi)

        N = 128

        self.note_range = [0, 128] if not note_range else note_range

        self.track_range = [
            0, self.midi.track_count
        ] if not track_range else track_range if not numpy.isscalar(
            track_range) else [track_range - 1, track_range]

        LEN = 10000

        trackers = numpy.zeros(self.midi.track_count)
        track_anas = meta_event_format.X
        res_set = numpy.zeros((self.midi.track_count, LEN, self.__DEPTH__))
        for i in range(self.track_range[0], self.track_range[1]):

            t = self.midi.tracks[i]
            instrument = t.get_event('instrument_name', depth=1)
            try:
                ttempo = t.get_event('set_tempo', depth=1)
                tempo = mutils.toint(ttempo[0].data, 8)
                # tempo = ttempo
            except IndexError:
                #Occurs because set_tempo is not register in every track explicitly, usually in first track only and all track follows the same rhythm forward
                #Considering the tempo_event in first track [0] , if there too error raised then tempo == 120 bpm
                try:
                    ttempo = self.midi.tracks[0].get_event('set_tempo',
                                                           depth=1)
                    tempo = mutils.toint(ttempo[0].data, 8)
                except IndexError:
                    tempo = 500000
                pass
            # tp = 0
            # for e in t.trk_event:
            #     if e.is_note_on_off_event():
            #         noteval = e.data[0]
            #         res_set[i][tp] = noteval
            #         tp += 1
            """
            >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

            """
            nit = 0  #numpy array iterator
            prev_oucr, dut, dep = -1, 0, 0
            dep = 0
            for e in t.trk_event:

                if e.is_note_on_off_event():
                    # if e.is_note_off_event(): continue
                    if 0x8f < e.event_id < 0x90 or e.data[1] == 0: continue
                    noteval = e.data[0]
                    # res_set[i][tp] = noteval
                    # tp += 1
                    #builting from here
                    if prev_oucr == e.abstime:
                        dep += 1  # going to next note on same time instance
                    else:
                        dep = 0
                        nit += 1  # going to next note instance

                    noteval = e.data[0] - self.note_range[0]
                    if noteval >= N or dep >= self.__DEPTH__: continue

                    dut = int(
                        32 // mutils.nth_note(e.elength, tempo=tempo)
                    ) if mutils.nth_note(e.elength, tempo=tempo) != 0 else 0
                    prev_oucr = e.abstime
                    res_set[i][nit][dep] += noteval
                    # nit += dut

        self.roll = res_set

        if trim:
            res_set_list = []
            for i in range(res_set.shape[0]):
                booln = False
                res_set_trk_list = []
                for n in range(res_set.shape[1]):
                    if numpy.array_equal(res_set[i][n], ZEROS_FIVE) and booln:
                        continue
                    # res_nt = [0, 0, 0, 0, 0]
                    # for j in range(res_set.shape[2]):
                    #     res_nt[j] = res_set[i][n][j]
                    res_set_trk_list.append(list(res_set[i][n]))
                    booln = True
                res_set_list.append(res_set_trk_list)
            MX = max([len(l) for l in res_set_list])
            # print(res_set_list)
            return dutils.to_numpy_array_from_3D_list(
                res_set_list, [res_set.shape[0], MX, 5])
        return res_set
Esempio n. 3
0
    def flat_pianoroll(self, note_range=None, track_range=None, trim=True):
        """min timespan of roll 32nd note
        
        Keyword Arguments:
            note_range {list} -- 2 element list/tuple (default: {None})
            track_range {list} -- 2 element list/tuple (default: {None})
        
        Returns:
            [ndarray] -- flat_piano_roll
        """
        if not isinstance(self.midi, AbsoluteMidi):
            self.midi = AbsoluteMidi.to_abs_midi(self.midi)

        N = 128 if not note_range else note_range[1] - note_range[0]

        self.note_range = [0, 128] if not note_range else note_range

        self.track_range = [
            0, self.midi.track_count
        ] if not track_range else track_range if not numpy.isscalar(
            track_range) else [track_range - 1, track_range]

        LEN = 9600
        trackers = numpy.zeros(self.midi.track_count)
        track_anas = meta_event_format.X
        res_set = numpy.zeros((self.midi.track_count, LEN, self.__DEPTH__))
        print(res_set.shape)
        for i in range(self.track_range[0], self.track_range[1]):
            t = self.midi.tracks[i]
            instrument = t.get_event('instrument_name', depth=1)
            try:
                ttempo = t.get_event('set_tempo', depth=1)
                tempo = mutils.toint(ttempo[0].data, 8)
                # tempo = ttempo
            except IndexError:
                #Occurs because set_tempo is not register in every track explicitly, usually in first track only and all track follows the same rhythm forward
                #Considering the tempo_event in first track [0] , if there too error raised then tempo == 120 bpm
                try:
                    ttempo = self.midi.tracks[0].get_event('set_tempo',
                                                           depth=1)
                    tempo = mutils.toint(ttempo[0].data, 8)
                except IndexError:
                    tempo = 500000
                pass
            nit = 0  #numpy array iterator
            prev_oucr, dut = 0, 0
            # print(t.trk_event[-11 : -1])
            dep = -1
            for e in t.trk_event:

                if e.is_note_on_off_event():
                    noteval = e.data[0] - self.note_range[0]

                    if prev_oucr == e.abstime:
                        nit -= dut

                    if prev_oucr == e.abstime or dep == -1:
                        dep += 1  # going to next note on same time instance

                    else:
                        dep = 0
                        # nit += 1 # going to next note instance

                    if noteval >= N: continue
                    if dep >= self.__DEPTH__:
                        dep = 0
                        continue
                    dut = int(32 // mutils.nth_note(e.elength, tempo=tempo))
                    prev_oucr = e.abstime
                    for k in range(dut):
                        res_set[i][nit + k][dep] = noteval
                    # res_set[i][nit : nit + dut][dep] += noteval
                    nit += dut
        self.roll = res_set

        if trim:
            res_set_list = []
            for i in range(res_set.shape[0]):
                res_set_trk_list = []
                for n in range(res_set.shape[1]):
                    if numpy.any(res_set[i, n]):
                        res_set_trk_list.append(list(res_set[i][n]))

                res_set_list.append(res_set_trk_list)
            MX = max([len(l) for l in res_set_list])

            return dutils.to_numpy_array_from_3D_list(
                res_set_list, [res_set.shape[0], MX, 5])

        return res_set