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)
    def __init__(self):
        super(Main, self).__init__()
        self.setupUi(self)

        self.avr = None
        self.thresholdCh0 = 1.0
        self.thresholdCh1 = 1.0
        self.ch0Contraido = False
        self.ch1Contraido = False

        self.stateChanged = False
        self.ch0State = False
        self.ch1State = False

        fluidsynth.init('FluidR3_GM.sf2', 'alsa')
        self.soundCommand1 = Note('C', 4)
        self.soundCommand2 = Note('E', 4)
        self.soundCommand3 = Note('G', 4)
        self.ihmMIDIinstrument = MidiInstrument()

        self.populateSerialPorts()
        self.populateInstruments()
        self.populateNotesAndScales()

        self.btnConnectDisconnect.clicked.connect(self.doConnect)
        self.btnStartStop.clicked.connect(self.doStartStop)
        self.sliderCh0.valueChanged.connect(self.emgValueChanged)
        self.sliderCh1.valueChanged.connect(self.emgValueChanged)
        self.sliderThCh0.valueChanged.connect(self.thresholdChanged)
        self.sliderThCh1.valueChanged.connect(self.thresholdChanged)
        self.connect(self.cbInstruments, SIGNAL('currentIndexChanged(int)'),
                     self.cbInstrumentsChanged)
        self.connect(self.cbNota1, SIGNAL('currentIndexChanged(int)'),
                     self.cbNotasChanged)
        self.connect(self.cbNota2, SIGNAL('currentIndexChanged(int)'),
                     self.cbNotasChanged)
        self.connect(self.cbNota3, SIGNAL('currentIndexChanged(int)'),
                     self.cbNotasChanged)
        self.connect(self.cbEscala1, SIGNAL('currentIndexChanged(int)'),
                     self.cbNotasChanged)
        self.connect(self.cbEscala2, SIGNAL('currentIndexChanged(int)'),
                     self.cbNotasChanged)
        self.connect(self.cbEscala3, SIGNAL('currentIndexChanged(int)'),
                     self.cbNotasChanged)

        self.scene = QGraphicsScene(self)
        self.graphResult.setScene(self.scene)
        self.normalColor = QBrush(QColor.fromRgb(162, 178, 245))
        self.hilightColor = QBrush(QColor.fromRgb(57, 255, 77))
        self.outlinePen = QPen(Qt.black)
        self.outlinePen.setWidth(1)

        self.circleSilence = self.scene.addEllipse(-100, -100, 100, 100,
                                                   self.outlinePen,
                                                   self.hilightColor)
        self.circleNota2 = self.scene.addEllipse(-100, 100, 100, 100,
                                                 self.outlinePen,
                                                 self.normalColor)
        self.circleNota1 = self.scene.addEllipse(100, -100, 100, 100,
                                                 self.outlinePen,
                                                 self.normalColor)
        self.circleNota3 = self.scene.addEllipse(100, 100, 100, 100,
                                                 self.outlinePen,
                                                 self.normalColor)
        self.textSilence = self.scene.addText("Silencio", QFont("Arial", 12))
        self.textNota1 = self.scene.addText("Nota 1", QFont("Arial", 12))
        self.textNota2 = self.scene.addText("Nota 2", QFont("Arial", 12))
        self.textNota3 = self.scene.addText("Nota 3", QFont("Arial", 12))
        self.textSilence.setPos(-80, -62.5)
        self.textNota2.setPos(-75, 138.5)
        self.textNota1.setPos(125, -62.5)
        self.textNota3.setPos(125, 138.5)
Esempio n. 4
0
 def set_instrument(self, number):
     instr = MidiInstrument()
     instr.instrument_nr = number
     self.track.instrument = instr
     return instr