Esempio n. 1
0
        def add_chord(chord, duration):
            if type(chord) == list:
                for c in chord:
                    add_chord(c, duration * 2)
            else:
                chord = NoteContainer().from_chord(chord)
                if tun:
                    chord = tun.find_chord_fingering(chord, return_best_as_NoteContainer=True)
                if not self.add_notes(chord, duration):
                    # This should be the standard behaviour of add_notes
                    dur = self.bars[-1].value_left()
                    self.add_notes(chord, dur)

                    # warning should hold note
                    self.add_notes(chord, value.subtract(duration, dur))
Esempio n. 2
0
        def add_chord(chord, duration):
            if type(chord) == list:
                for c in chord:
                    add_chord(c, duration * 2)
            else:
                chord = NoteContainer().from_chord(chord)
                if tun:
                    chord = tun.find_chord_fingering(chord,
                            return_best_as_NoteContainer=True)
                if not self.add_notes(chord, duration):
                    # This should be the standard behaviour of add_notes
                    dur = self.bars[-1].value_left()
                    self.add_notes(chord, dur)

                    # warning should hold note
                    self.add_notes(chord, value.subtract(duration, dur))
    def gen_midi(self, tracks_data):
        c = Composition()

        all_tracks = tracks_data["tracks_data"]
        curr_track = 0;

        for track_data in all_tracks:
            #TODO: set author, title, more specific instrument (that has a
            #range set

            data_max = max(track_data["note_data"])
            data_min = min(track_data["note_data"])

            data_dur_max = max(track_data["dur_data"])
            data_dur_min = min(track_data["dur_data"])

            data_range = data_max - data_min

            ####DURATIONS####
            midi_durs = [self.map_range(0, len(self.mapping_dur_vals) - 1,
                                   data_dur_min, data_dur_max, x, "dur")
                         for x in track_data["dur_data"]]
            midi_durs = [self.mapping_dur_vals[int(x)] for x in midi_durs]
            print "midi_durs: "
            print midi_durs

            ####PITCHES####
            midi_vals = [self.map_range(0, len(self.overall_mapping_vals) - 1,
                                   data_min, data_max, x, "notes")
                         for x in track_data["note_data"]]
            midi_vals = [self.overall_mapping_vals[int(x)] for x in midi_vals]

            print "midi_pitches: "
            print midi_vals

            for instr in self.instrs:
                t = Track()
                b = Bar()
                for index, midi_val in enumerate(midi_vals):
                    print "-----------------"
                    print "space left in bar"
                    print b.space_left()
                    if midi_val in instr["mapping_vals"]:
                        print "inserting NON-REST NOTE"
                        print "inserting NON-REST NOTE"
                        val_to_insert = Note().from_int(int(midi_val))
			print "DONE INSERTING NON-REST NOTE"
                    else:
                        print "INSERTING REST"
                        val_to_insert = None #insert rest instead

                    print "ABOUT TO INSERT"
                    dur_to_insert_1 = 1.0/((1.0/float(midi_durs[index])) * 0.99)
                    dur_to_insert_2 = 1.0/((1.0/float(midi_durs[index])) * 0.01)
                    b, insert_result_1 = self.insert_into_bar(curr_track, b, val_to_insert,
                                                         dur_to_insert_1)
                    print "trying to insert note..."
                    print insert_result_1
                    print "midi note DURATION is: "
                    print midi_durs[index]

                    if insert_result_1:
                         b, insert_result_2 = self.insert_into_bar(curr_track, b, None,
                                                         dur_to_insert_2)
                         print "trying to insert note..."
                         print insert_result_2
                         print "midi note DURATION is: "
                         print midi_durs[index]

                    if not insert_result_1:
                        if b.space_left() == 0.0:
                            print "hit edge case!!!!"
                            t.add_bar(b)
                            b = Bar()
                            b, insert_result = self.insert_into_bar(curr_track,
                                                                    b, val_to_insert, dur_to_insert_1)
                            b, insert_result = self.insert_into_bar(curr_track,
                                                                    b, None, dur_to_insert_2)
                            print "bar is now (after edge case): "
                            print b
                            continue

                        space_p = b.space_left()
                        space = 1.0/space_p
                        print "space percentage left: "
                        print space_p
                        print "note space left: "
                        print space

                        note_remainder = value.subtract(midi_durs[index], space)
                        print "note duration: "
                        print midi_durs[index]
                        print "note remainder: "
                        print note_remainder

                        print "bar before INSERTING PART 1 OF TIE: "
                        print b
                        dur_to_insert_1_space = 1.0/((1.0/float(space)) * 0.99)
                        dur_to_insert_2_space = 1.0/((1.0/float(space)) * 0.01)
                        if space <= 16.0:
                             print "NOTE IS LARGER THAN 16 NOTE"
                             b, insert_result = self.insert_into_bar(curr_track, b, val_to_insert, dur_to_insert_1_space)
                             b, insert_result = self.insert_into_bar(curr_track, b, None, 1.0/b.space_left())
                        else:
                             print "NOTE IS *NOT* LARGER THAN 16 NOTE"
                             b, insert_result = self.insert_into_bar(curr_track, b, None, space)
                        print "bar AFTER: "
                        print b
                        print "inserting part one of tie result: "
                        print insert_result
                        print "is bar full??? (IT SHOULD BE)"
                        print b.is_full()
                        t.add_bar(b)

                        b = Bar()
                        b, insert_result = self.insert_into_bar(curr_track, b, None, note_remainder)
                        print "inserting part TWO of tie result: "
                        print insert_result
                        print "bar after inserting remaining piece: "
                        print b

                    print "BAR IS NOW"
                    print b
                    #end for

                #add last bar
                if b.space_left() == 0.0:
                    t.add_bar(b)
                else:
                    #need to fill remaining space with rest
                    space_p = b.space_left()
                    space = 1.0/space_p
                    #b, insert_result = self.insert_into_bar(curr_track, b,
                    #                                        Note(None), space)
                    print "result for filling rest of last bar with rest: "
                    print insert_result
                    print "is bar full after adding rest to end of last bar??? (IT SHOULD BE)"
                    print b.is_full()
                    print "bar WITH REST is now: "
                    print b
                    t.add_bar(b)
                logger.debug("Track: ")
                logger.debug(t)
                #at very end of loop add track to composition
                c.add_track(t)

            curr_track += 1

        #write_Composition("midi_success.mid", c);
        return self.gen_midi_data(c)