def generate(self):
        pitch = random.randint(0, N_PITCHES - 1)
        octave = random.randint(3, 6)

        reference_note = Note()
        reference_note.pitch = pitch
        reference_note.octave = octave

        melody = Melody()
        for j in range(0, DEF_NUMBER_NOTES):
            # Always create a new object
            new_note = Note()
            new_note.pitch = reference_note.pitch
            new_note.octave = reference_note.octave

            if j != 0:
                new_note.articulated = True

            melody.notes.append(new_note)

        return melody
Esempio n. 2
0
    def read_file(self, file):
        # Array of Melody objects
        result = []

        pattern = midi.read_midifile(file)

        resolution = pattern.resolution
        note_window = DEF_TICK_STEP_SIZE

        for idx, track in enumerate(pattern):
            new_melody = Melody()
            new_melody.setTempo(resolution)

            notes_played = []
            track.make_ticks_abs()
            for event in track:
                if (isinstance(event, midi.NoteOnEvent)
                        or isinstance(event, midi.NoteOffEvent)):
                    if event.data[1] > 0:
                        # We got the duration of the pitch last played
                        tick_information = TickInformation()
                        tick_information.note = event.data[0]
                        tick_information.start_tick = event.tick

                    elif event.data[1] == 0:
                        # This gives us the duration of the pitch
                        tick_information.end_tick = event.tick
                        notes_played.append(tick_information)
                elif (isinstance(event, midi.TrackNameEvent)):
                    new_melody.description = event.text

            new_melody.notes = []
            last_tick = reversed(track).next().tick
            for i in range(0, last_tick, int(note_window)):
                new_note = Note()
                junk_elements = []

                for tick in notes_played:
                    # We already handled this time point
                    if i >= tick.end_tick:
                        junk_elements.append(tick)

                    if tick.start_tick <= i and i < tick.end_tick:
                        # A note is being played in the time frame
                        new_note.setFromMidiPitch(tick.note)
                        new_melody.notes.append(new_note)

                        # Do not repeat the note if it was already played once before
                        if tick.played == True:
                            new_note.articulated = True

                        tick.played = True
                        break
                    elif i < tick.start_tick:
                        # We have yet to check if there is a note or silence
                        new_note.pitch = SILENCE
                        new_melody.notes.append(new_note)
                        break

                # Reduce notes played container
                for el in junk_elements:
                    notes_played.remove(el)

            # Check if the track is too small
            # This can be the case for description tracks
            if len(new_melody.notes) < 10:
                #print "Too short track. Ignoring it."
                continue

            result.append(new_melody)

        result = self.clean_melodies(result)

        return result, note_window