Esempio n. 1
4
    def generate(self, length, bars, octave, scale=["C", "D", "E", "G", "A"], instrument=1, rand_length=False,
                 time_signature=(4, 4), velocity=[64, 64], channel=1, rests=True, rule_number=30):
        # create the instrument for the main track
        instr = MidiInstrument()
        instr.instrument_nr = instrument  # MIDI instrument number: http://www.midi.org/techspecs/gm1sound.php
        track = containers.Track(instr)
        self.set_instrument(instrument)
        if not self.initial:
            self.initial = [False] * 9
            print "Error: initial set empty. Defaulting..."

        if rand_length:  # start with quarter note as base
            length = 4

        automata = Engine(rule_number, init_row=self.initial, edge_type=EdgeType.LOOP)

        # Index counting for diagnostics
        self.counts = dict((n, 0) for n in range(0, len(scale) * 2))
        index = 5  # starting value
        for b in range(0, (length * bars) / 4):
            bar = Bar("C", time_signature)
            while bar.space_left() != 0:  # runs until we're out of space for notes (e.g. complete bar)
                automata.step()  # take a step
                index = self.get_chosen_note(index, automata.rows[-1])
                if rand_length:  # if we're randomly generating lengths of notes
                    length = int(math.pow(2, random.randint(1, 4)))
                    left = bar.space_left()
                    space = (
                        (left - (left % 0.25)) * 16) if left > 0.25 else left * 16  # checks if we have enough space
                    if space < 16 / length:
                        length = int(16.0 / space)
                if rests and random.randint(0, 20) == 1:  # same rest generation
                    bar.place_rest(16)
                    continue  # skip the rest
                name = list(scale)[index if index < 5 else index - 4]  # can be used for diagnostics
                self.counts[index] += 1  # add to count data
                self.notes.append(index)
                n = Note(name,
                         octave=octave if index < 5 else octave + 1)
                n.set_velocity(random.randint(velocity[0], velocity[1]))  # random velocity, can feel more "real"
                n.set_channel(channel)  # if we want > 1 instruments we need > 1 channel
                bar.place_notes(n, length)
                # print self.format_block(automata.rows[-1])
            track.add_bar(bar)  # add bar to track
        self.track = track  # set our track object to the newly generated track
        # for n in self.counts:
        #     print str(n) + ": " + str(self.counts[n])  # diagnostics
        # print "======="
        return self
Esempio n. 2
0
print "Harmony instrument: " + track2.set_instrument(instr).names[instr] + " ({})".format(instr)
track2 = s.generate(chorus, verse, bridge, track2.track)
b = Bar()
n = Note('C', 2)
n.set_channel(2)
b.place_notes(n, 4)

track2.add_bar(b)
song.add_track(track2)


# END HARMONY #

if False:
    bass = MidiInstrument()
    bass.instrument_nr = random.randint(1, 104)
    basstrack = containers.Track(bass)

    print "Bass instrument: " + bass.names[bass.instrument_nr]

    for i in range(0, len(s.song_structure.sections) * BAR_NUMBER):
        b = Bar()
        for i2 in range(0, time_sig[0] / (time_sig[1] / 4)):
            n = Note("C" if s.song_structure.sections[i // BAR_NUMBER] != bridge else "A", 3)
            n.set_channel(2)
            b.place_notes(n, 4)
            # b.place_rest(8)
        basstrack.add_bar(b)

    song.add_track(basstrack)
Esempio n. 3
0
 def set_instrument(self, number):
     instr = MidiInstrument()
     instr.instrument_nr = number
     self.track.instrument = instr
     return instr