Ejemplo n.º 1
0
def get_meter_notes(beats_sample,
                    meter_signature,
                    pitch_sample,
                    octave_sample,
                    volume_sample=120,
                    shuffle_pitch_sample=True,
                    shuffle_beats=True,
                    n_beats=None):
    """ Return the sequence of notes
    """
    notes = NoteSeq()
    # get the beats
    if shuffle_beats:
        beats = get_beats(beats_sample, meter_signature)
    else:
        beats = beats_sample

    if n_beats:
        beats = beats[:n_beats]

    vol = get_random_item(volume_sample)

    for ctr, beat in enumerate(beats):

        if shuffle_pitch_sample:
            pitch = get_random_item(pitch_sample)
        else:
            pitch = pitch_sample[ctr]
        note = pitch_to_note(pitch)
        octave = get_random_item(octave_sample)

        notes.append(Note(note, octave, beat, vol))
    return notes
Ejemplo n.º 2
0
 def test_init_empty(self):
     """Test if NoteSeq without arguments will clean previous values."""
     seq = NoteSeq()
     seq.append(Note("C"))
     seq = NoteSeq()
     seq.append(Note("D"))
     self.assertEqual(seq, NoteSeq("D"))
Ejemplo n.º 3
0
def strToMidi(msg, fileName):
    from pyknon.genmidi import Midi
    from pyknon.music   import NoteSeq
    from pyknon.music   import Note
    notes = {
        '0' : Note(value=0, octave=5), # Do
        '1' : Note(value=2, octave=5), # Re
        '2' : Note(value=4, octave=5), # Mi
        '3' : Note(value=5, octave=5), # Fa
        '4' : Note(value=7, octave=5), # Sol
        '5' : Note(value=9, octave=5), # La
        '6' : Note(value=11, octave=5), # Si
        '7' : Note(value=0, octave=6),
        '8' : Note(value=2, octave=6),
        '9' : Note(value=4, octave=6),
        'a' : Note(value=5, octave=6),
        'b' : Note(value=7, octave=6),
        'c' : Note(value=9, octave=6),
        'd' : Note(value=11, octave=6),
        'e' : Note(value=0, octave=7),
        'f' : Note(value=2, octave=7)
    }
    msgHex = msg.encode('hex');
    sequence = NoteSeq('C1')
    before = ''
    for i in msgHex:
        if before == i:
            sequence.append(Note(value=4, octave=7))
        sequence.append(notes[i])
        before = i
    midi = Midi(1, tempo = 290)
    midi.seq_notes(sequence, track=0)
    midi.write(fileName)
Ejemplo n.º 4
0
def pix2noteseq(pixelmap, width, height):
    """
    Convert a PIL pixel map to a PyKnon NoteSeq

    Use:
        pix2noteseq(pixelmap)

    Arguemnts:
        pixelmap: the PIL pixel map of the image
        width: the width in pixels
        height: height in pixels

    This function presumes the pixel map is in RGB and correct behavior when
    otherwise is not at all guaranteed.
    """
    notes = NoteSeq()
    
    # Iterate over the pixels, starting at the top left and working
    # colomn by colomn
    for y in range(height):
        for x in range(width):
            notes.append(pix2note(pixelmap[x,y]))
            if y == math.ceil(height/2) and x == math.ceil(width/2):
                print("50% done...")

    return notes
Ejemplo n.º 5
0
 def test_init_empty(self):
     """Test if NoteSeq without arguments will clean previous values."""
     seq = NoteSeq()
     seq.append(Note("C"))
     seq = NoteSeq()
     seq.append(Note("D"))
     self.assertEqual(seq, NoteSeq("D"))
Ejemplo n.º 6
0
def chord_prog(chords_note):
    cprog6251 = NoteSeq()
    count = 0
    while count < 2:  # while true if break
        for i in chords_note:
            cprog6251.append(Note(i, 4, 1, 100))
        count += 1
    return cprog6251
Ejemplo n.º 7
0
def play_list(pitch_list, octave_list, duration, volume=120):
    result = NoteSeq()
    for pitch in pitch_list:
        note = pitch % 12
        octave = choice_if_list(octave_list)
        dur = choice_if_list(duration)
        vol = choice_if_list(volume)
        result.append(Note(note, octave, dur, vol))
    return result
Ejemplo n.º 8
0
def random_notes(pitch_list, octave, duration, number_of_notes, volume=120):
    result = NoteSeq()
    for x in range(0, number_of_notes):
        pitch = choice(pitch_list)
        octave = choice_if_list(octave)
        dur = choice_if_list(duration)
        vol = choice_if_list(volume)
        result.append(Note(pitch, octave, dur, vol))
    return result
Ejemplo n.º 9
0
def play_list(pitch_list, octave_list, duration, volume=120):
    result = NoteSeq()
    for pitch in pitch_list:
        note = pitch % 12
        octave = choice_if_list(octave_list)
        dur = choice_if_list(duration)
        vol = choice_if_list(volume)
        result.append(Note(note, octave, dur, vol))
    return result
Ejemplo n.º 10
0
def random_notes(pitch_list, octave_list, duration,
                 number_of_notes, volume=120):
    result = NoteSeq()
    for x in range(0, number_of_notes):
        pitch = choice(pitch_list)
        octave = choice_if_list(octave_list)
        dur = choice_if_list(duration)
        vol = choice_if_list(volume)
        result.append(Note(pitch, octave, dur, vol))
    return result
Ejemplo n.º 11
0
def random_notes(pitch_list, octave_list, duration, volume):
    result = NoteSeq()
    number_of_notes = int(1 // duration)
    for x in range(0, number_of_notes, 1):
        pitch = choice(pitch_list)
        octave = choice(octave_list)
        dur = duration
        vol = volume
        result.append(Note(pitch, octave, dur, vol))
    return result
    def rhythm_6_17_left(self, chord: Chord) -> [Chord]:
        rhythm = []
        new_chord = NoteSeq()

        new_chord.append(chord.first_note.octave_shift(-1))
        new_chord.append(chord.first_note.octave_shift(-2))

        rhythm.append(new_chord.stretch_dur(12 / self.subdivided))
        rhythm.append(new_chord.stretch_dur(12 / self.subdivided))
        rhythm.append(new_chord.stretch_dur(8 / self.subdivided))
        return rhythm
Ejemplo n.º 13
0
def getNotesFromNums(nums, scaleNum, durations):
    #convert back to noteSeq
    scale = majorScales[scaleNum]
    ans = NoteSeq()
    j = 0
    for num in nums:
        for i in range(len(scale)):
            if num==i:
                ans.append(Note(scale[i], dur=durations[j]))
                j+=1
    #print(ans)
    return ans
Ejemplo n.º 14
0
def random_melody(note_list, octave_low, octave_high, dur_list):
    rm_seq = NoteSeq()  #[]
    rm_data = []
    melody_len = 0
    while melody_len < 8:  # while True if break bug
        pitch = random.choice(note_list)  #refactor by root +2 +4 ...
        octave = random.randint(octave_low, octave_high)
        dur = random.choice(dur_list)
        vol = random.randrange(80, 125, 5)  #refactor
        print([pitch, octave, dur, vol])
        rm_seq.append(Note(pitch, octave, dur, vol))  #refactor
        rm_data.append([pitch, octave, dur, vol])
        melody_len += dur
    print(melody_len)
    rm = [rm_seq, rm_data]
    return rm
Ejemplo n.º 15
0
def get_random_notes(n, pitches, durations, rests=True):
    if rests:
        pitches.append('r')

    result = NoteSeq()

    for i in range(n):
        pitch = random.choice(pitches)
        duration = random.choice(durations)

        if pitch == 'r':
            result.append(Rest(dur=duration))
        else:
            result.append(Note(pitch, octave=4, dur=duration))

    return result
Ejemplo n.º 16
0
def play_list(pitch_list, octave_list, duration,
              volume=120):
    result = NoteSeq()
    durl = [1/8, 1/8, 1/16, 1/16]
    cc = [choice([0,1,2,3,4,5,6,7,8,9,10,11]), choice([0,1,2,3,4,5,6,7,8,9,10,11]), choice([0,1,2,3,4,5,6,7,8,9,10,11]), choice([0,1,2,3,4,5,6,7,8,9,10,11])]
    st = 0
    for pitch in pitch_list:
        note1 = pitch
        note = cc[st%4]
        #octave = choice_if_list(octave_list)
        octave = change_range(note1, 0, 11, 1, 7)
        #dur = choice_if_list(duration)
        dur = durl[st%4]
        st += 1
        vol = choice_if_list(volume)
        result.append(Note(note, octave, dur, vol))
    return result
Ejemplo n.º 17
0
def make_midi(midi_path, notes, bpm=120):
    note_names = 'c c# d d# e f f# g g# a a# b'.split()

    result = NoteSeq()
    for n in notes:
        duration = 1. / n[1]

        if n[0].lower() == 'r':
            result.append(Rest(dur=duration))
        else:
            pitch = n[0][:-1]
            octave = int(n[0][-1]) + 1
            pitch_number = note_names.index(pitch.lower())

            result.append(Note(pitch_number, octave=octave, dur=duration))

    midi = Midi(1, tempo=bpm)
    midi.seq_notes(result, track=0)
    midi.write(midi_path)
Ejemplo n.º 18
0
    def makemidi(self):
        note_names = 'c c# d d# e f f# g g# a a# b'.split()
        octav10 = {
            'c10', 'c#10', 'd10', 'd#10', 'e10', 'f10', 'f#10', 'g10', 'g#10',
            'a10', 'a#10', 'b10'
        }

        result = NoteSeq()
        for s in self.song:
            duration = 1. / s[1]

            if s[0] == 'r':
                result.append(Rest(dur=duration))
            elif {s[0]}.issubset(octav10):
                md = s[0][:-2]
                octave = int(s[0][-2:]) + 1
                note_number = note_names.index(md)
                result.append(Note(note_number, octave=octave, dur=duration))
            else:
                md = s[0][:-1]
                octave = int(s[0][-1]) + 1
                note_number = note_names.index(md)
                result.append(Note(note_number, octave=octave, dur=duration))

        midi = Midi(number_tracks=1, tempo=self.bpm)
        midi.seq_notes(result, track=0)
        midi.write(self.path)
def random_notes_with_memory(pitch_list, octave_list, duration_list,
                             number_of_notes, memory_weights, volume=120):
    assert len(memory_weights) > 1, "more than 1 weight expected for memory"
    result = NoteSeq()
    for offset in range(0, number_of_notes):

        if 1+offset >= len(memory_weights):
            weights = memory_weights
        else:
            weights = memory_weights[0:1+offset]

        pitch_selection = weighted_random(weights)
        if pitch_selection == 0: # new note
            pitch = choice(pitch_list)
        else:
            pitch = result[-pitch_selection].value

        octave_selection = weighted_random(weights)
        if octave_selection == 0: # new note
            octave = choice_if_list(octave_list)
        else: # previous note at given position starting from the end
            octave = result[-octave_selection].octave

        duration_selection = weighted_random(weights)
        if duration_selection == 0: # new note
            dur = choice_if_list(duration_list)
        else: # previous note at given position starting from the end
            dur = result[-duration_selection].dur

        volume_selection = weighted_random(weights)
        if volume_selection == 0: # new note
            vol = choice_if_list(volume)
        else: # previous note at given position starting from the end
            vol = result[-volume_selection].volume

        result.append(Note(pitch, octave, dur, vol))
    return result
Ejemplo n.º 20
0
def main():
    new = input('Do you want to creat a new random melody? ')
    if new == 'y':
        note_list = [0, 2, 4, 6, 7, 9, 11]  #refactor by root +2 +4 ...
        octave_low = 5
        octave_high = 6
        dur_list = [0.125, 0.25, 0.5]
        rm = random_melody(note_list, octave_low, octave_high, dur_list)
        rm_seq, rm_data = rm
        print(rm_seq)
        store_rm(rm_data, 'random_melody_in_C_data.csv')
    elif new == 'n':
        rm_data = open_rm('random_melody_in_C_data.csv')
        rm_seq = NoteSeq(
        )  #rm_seq = NoteSeq([Note(int(p[0])) for p in rm_data])
        for p in rm_data:
            rm_seq.append(Note(p[0], p[1], p[2], p[3]))
        print(rm_seq)
    # cprog6251r = chord_prog([9, 2, 7, 0])
    # cprog62513 = chord_prog([0, 5, 11, 4]) #inversion 9 0
    # gen_midi(rm_seq, cprog6251r, cprog62513, 'random_melody_in_C_3.mid')
    fsprog = fusion_prog_rym(rm_data, [0], [10, 11], [14])
    fsprogr, fsprog7, fsprog9 = fsprog
    gen_midi(rm_seq, fsprogr, fsprog7, fsprog9, 'random_melody_in_C_.mid')
Ejemplo n.º 21
0
 def test_append(self):
     seq1 = NoteSeq("C# D#")
     seq1.append(Note("F#"))
     seq2 = NoteSeq("C# D# F#")
     self.assertEqual(seq1, seq2)
Ejemplo n.º 22
0
    return obs


def obs_to_note(obs):
    global notes
    day, hour, temp, opacity, precip = obs
    # Temperature is note
    index = int(temp)
    if index < 7: index = 7
    elif index > 90: index = 90
    index -= 7
    octave, pitch = notes[index]
    # Sky cover is volume
    volume = 0
    try:
        volume = opacity_to_volume[opacity]
    except:
        pass
    return Note(pitch, octave, dur, int(volume * 120))


obs = read_obs('data/kcmh_2014_03_obs.csv')

obs_notes = NoteSeq()
for ob in obs:
    obs_notes.append(obs_to_note(ob))

midi = Midi(1, tempo=90, instrument=1)
midi.seq_notes(obs_notes, track=0)
midi.write("output/temps_2014_03.mid")
Ejemplo n.º 23
0
    # Sky cover is volume
    volume = 0
    try:
        volume = opacity_to_volume[opacity]
    except:
        pass
    return Note(pitch, octave, dur, 127)


obs = read_obs('data/kcmh_2014_03_obs.csv')

obs_notes = NoteSeq()
prev_note = None
for ob in obs:
    note = obs_to_note(ob)
    if prev_note is not None and prev_note.value == note.value and prev_note.octave == note.octave:
        prev_note = Note(prev_note.value, prev_note.octave,
                         prev_note.dur + dur, prev_note.volume)
    elif prev_note is None:
        prev_note = note
    else:
        obs_notes.append(prev_note)
        prev_note = None

if prev_note is not None:
    obs_notes.append(prev_note)

midi = Midi(1, tempo=90, instrument=1)
midi.seq_notes(obs_notes, track=0)
midi.write("output/temps_2014_03_hold.mid")
Ejemplo n.º 24
0
    def derive_tracks_from_lyrics(self, lyrics, notes, track_duration, method):
        '''
        Attempts to find patterns in lyrics to be paired with the given notes
        to create new tracks of a specified duration
        
        :param str lyrics:The lyrics from which to derive music
        :param NoteSequ notes: The notes to be utilized in track creation
        :param int track_duration: The optimal length of the tracks, in seconds
        :param list method:A list of invention methods that derive data from text
        :returns: A list of NoteSeqs that represents musical tracks
        '''
        num_notes = len(notes)
        lyrics_sentences = lyrics.replace("?", ".").replace("!",
                                                            ".").split(".")
        #Remove tiny sentences, if any
        lyrics_sentences = [s for s in lyrics_sentences if len(s) > 5]
        num_sentences = len(lyrics_sentences)

        track_list = list()
        #Skip first sentence, as the Lead track will be based on it already
        #otherwise create a track for each sentence
        for i in range(1, min(3, num_sentences)):
            #Count the words in this sentence
            lyric_sentence = lyrics_sentences[i]
            lyric_words = nltk.word_tokenize(lyric_sentence)
            num_words = len(lyric_words)
            #Count the number of characters in each word
            list_of_char_counts = list()
            for j in range(0, len(lyric_words)):
                list_of_char_counts.append(len(lyric_words[j]))
            num_chars_total = sum([cnt for cnt in list_of_char_counts])

            #Every other track picks a pattern length differently
            #but all tracks use the agent's invention methods
            if i % 2 == 0:
                pattern_length = max(3, method[0](lyric_sentence) % 8)
                pattern_accent = method[1](lyric_sentence) % 3
                accent_occur = max(1, method[2](lyric_sentence) % 12)
                inversion_accent = method[3](lyric_sentence) % 6
                inversion_occur = max(1, method[4](lyric_sentence) % 10)
                #Aim to start shortly before the lead track
                target_duration = track_duration * 1.05
                note_vol = 85
            else:
                pattern_length = max(3, method[4](lyric_sentence) % 12)
                pattern_accent = method[3](lyric_sentence) % 3
                accent_occur = max(1, method[2](lyric_sentence) % 16)
                inversion_accent = method[1](lyric_sentence) % 5
                inversion_occur = max(1, method[0](lyric_sentence) % 8)
                #Aim for a lead-in before the other tracks
                target_duration = track_duration * 1.1
                note_vol = 80

            #Give notes equal time, in accordance with represented word length, plus time for rests
            duration_unit = (
                target_duration /
                (num_words * pattern_length * num_chars_total)) - num_words

            #Repeat the pattern equal to the number of words in the sentence
            this_track = NoteSeq()
            for n in range(0, num_words):
                for m in range(0, pattern_length):
                    note_to_append = m % num_notes
                    note = notes[note_to_append]
                    note.volume = note_vol

                    #Invert the pattern this time, if the invention method calls for it
                    if n % inversion_occur == 2:
                        note = note.inversion(inversion_accent)

                    duration_count = max(1, m % list_of_char_counts[n])
                    note.duration = duration_count * duration_unit
                    #Transpose this note, if the invention method calls for it
                    if m % accent_occur == 3:
                        if m // accent_occur % 2 == 0:
                            note = note.transposition(pattern_accent)
                        else:
                            note = note.transposition(-pattern_accent)
                    this_track.append(note)
                #Rest for a second between tracks
                this_track.append(Rest(1))

            #Add the completed track
            track_list.append(this_track)

        return track_list
Ejemplo n.º 25
0
    def create_music(self, lyrics, invention_method):
        '''Generate music from text.

        Music is generated by deriving notes from text, harmonizing those notes, and splitting them
        into separate tracks, choosing a theme based on the text and then matching the tracks up.

        :returns: word_theme=a string representing inspiration, music_theme=tempo and instrument list, track_list=list of tracks composed of notes
        '''
        #Read in characters in the lyrics, and convert to musical notes
        derived_notes = self.music_helper.convert_phrase_to_notes(lyrics)
        #Determine which musical key is dominant in the derived notes
        music_key = self.music_helper.determine_dominant_key(derived_notes)
        #Force all notes into the dominant key so we don't have dischord
        notes = self.music_helper.conform_notes_to_key(derived_notes,
                                                       music_key)

        #Tokenize the word list
        lyric_words = nltk.word_tokenize(lyrics)

        #Find a word that will provide a theme for the song
        word_theme = random.choice(
            [word for word in lyric_words if len(word) > 4])
        if word_theme == None:
            self.music_helper.determine_theme("random")
        else:
            tempo = max(
                120, invention_method.method_list[0](word_theme) *
                invention_method.method_list[4](word_theme) % 400)
            instr1 = (invention_method.method_list[0](word_theme) *
                      invention_method.method_list[3](word_theme)) % 127
            instr2 = (invention_method.method_list[1](word_theme) *
                      invention_method.method_list[2](word_theme)) % 127
            instr3 = (invention_method.method_list[4](word_theme) *
                      invention_method.method_list[0](word_theme)) % 127
            music_theme = (tempo, [instr1, instr2, instr3])

        track_list = []
        lead_track = NoteSeq()
        other_notes = NoteSeq()

        lead_note_duration = (invention_method.method_list[0](lyrics) %
                              6) * 0.05
        lead_rest_duration = (invention_method.method_list[1](lyrics) %
                              8) * 0.25

        #Separate notes into lead track/others, assign word-based duration
        for i in range(0, len(notes)):
            #Associate each note for the lead track with a word in the lyrics,
            #until we're out of words, then put the rest in "other"
            if i < len(lyric_words):
                word_ptr = i
                word_for_note = lyric_words[word_ptr]

                #Exclude punctuation in lead track
                if word_for_note not in (',', '.', ';', '!', '?', '"', ':',
                                         '/', '\\'):
                    #Set notes duration based on word length
                    notes[i].dur = len(word_for_note) * lead_note_duration
                    lead_track.append(notes[i])
            else:
                other_notes.append(notes[i])

        #Insert rests for lead track at punctuation marks
        rest_count = 0
        for i in range(0, len(lead_track)):
            if lyric_words[i] in (',', '.', ';', '!', '?', '"', ':', '/',
                                  '\\'):
                lead_track.insert(i + rest_count, Rest(lead_rest_duration))
                rest_count = rest_count + 1

        #See how long the lead track is
        lead_track_duration = sum(
            [noteOrRest.dur for noteOrRest in lead_track])
        #Then add it to our track list
        track_list.append(lead_track)

        #If there aren't enough notes, add some from the lead track
        if len(other_notes) < 8:
            lead_length = len(lead_track)
            for i in range(lead_length - 1, max(0, lead_length - 8), -1):
                if not isinstance(lead_track[i], Rest):
                    other_notes.append(lead_track[i])

        #Attempt to detect patterns in the lyrics in combination with the
        #other notes, for the purpose of creating more tracks using the agent's
        #preferred invention method
        if len(other_notes) > 0:
            pattern_tracks = self.music_helper.derive_tracks_from_lyrics(
                lyrics, other_notes, lead_track_duration,
                invention_method.method_list)
            for i in range(0, len(pattern_tracks)):
                track_list.append(pattern_tracks[i])

        #Find out which track came out the longest in duration
        longest_duration = lead_track_duration
        for i in range(1, len(track_list)):
            this_track_duration = sum(
                [noteOrRest.dur for noteOrRest in track_list[i]])
            if this_track_duration > longest_duration:
                longest_duration = this_track_duration

        #Make the tracks equal in duration, so there isn't long silence
        for i in range(0, len(track_list)):
            #Calculate this track duration
            this_track_duration = sum(
                [noteOrRest.dur for noteOrRest in track_list[i]])
            #Add some rests before/during to make it centered
            if this_track_duration < longest_duration:
                insert_rest = (longest_duration - this_track_duration) / 2
                track_list[i].insert(0, Rest(insert_rest))
                track_list[i].append(Rest(insert_rest))

            #Add a 2 second pause to the end of the longest track so it ends gracefully
            if this_track_duration == longest_duration:
                track_list[i].insert(0, Rest(2))

        return word_theme, music_theme, track_list
Ejemplo n.º 26
0
def fusion_prog(rm_data, root, seventh, nine):
    # def append_note(seq_name, p, r, note):
    # 	seqname.append(Note(p[0] - r + note, 4, 1, 100))
    # note indicates which note in the chord you want to put in the seq
    fsprogr = NoteSeq()
    fsprog7 = NoteSeq()
    fsprog9 = NoteSeq()
    fsprog = [fsprogr, fsprog7, fsprog9]
    # the note of melody could be the 1, b3, 3, 5, b7, 7th of the chord
    pitch_in_chord = [0, 3, 4, 7, 10, 11]
    melody_len = 0
    r = random.choice(pitch_in_chord)
    #print(r)
    ro = random.choice(root)
    se = random.choice(seventh)
    fsprogr.append(Note(rm_data[0][0] - r + ro, 4, 1, 100))
    fsprog7.append(Note(rm_data[0][0] - r + se, 4, 1, 100))
    fsprog9.append(Note(rm_data[0][0] - r + 14, 4, 1, 100))
    for p in rm_data:
        melody_len += p[2]
        #print(melody_len)
        r = random.choice(pitch_in_chord)
        se = random.choice(seventh)
        #print(r)
        for i in range(1, 8):
            if p[2] == 0.125:
                if melody_len - 0.125 == i:
                    fsprogr.append(Note(p[0] - r, 4, 1, 100))
                    fsprog7.append(Note(p[0] - r + se, 4, 1, 100))
                    fsprog9.append(Note(p[0] - r + 14, 4, 1, 100))
            if p[2] == 0.25:
                if melody_len - 0.25 == i:
                    fsprogr.append(Note(p[0] - r, 4, 1, 100))
                    fsprog7.append(Note(p[0] - r + se, 4, 1, 100))
                    fsprog9.append(Note(p[0] - r + 14, 4, 1, 100))
                if melody_len - 0.25 == i - 0.125:
                    fsprogr.append(Note(p[0] - r, 4, 1, 100))
                    fsprog7.append(Note(p[0] - r + se, 4, 1, 100))
                    fsprog9.append(Note(p[0] - r + 14, 4, 1, 100))
            if p[2] == 0.5:
                if melody_len - 0.5 == i:
                    fsprogr.append(Note(p[0] - r, 4, 1, 100))
                    fsprog7.append(Note(p[0] - r + se, 4, 1, 100))
                    fsprog9.append(Note(p[0] - r + 14, 4, 1, 100))
                if melody_len - 0.5 == i - 0.125:
                    fsprogr.append(Note(p[0] - r, 4, 1, 100))
                    fsprog7.append(Note(p[0] - r + se, 4, 1, 100))
                    fsprog9.append(Note(p[0] - r + 14, 4, 1, 100))
                if melody_len - 0.5 == i - 0.25:
                    fsprogr.append(Note(p[0] - r, 4, 1, 100))
                    fsprog7.append(Note(p[0] - r + se, 4, 1, 100))
                    fsprog9.append(Note(p[0] - r + 14, 4, 1, 100))
                if melody_len - 0.5 == i - 0.375:
                    fsprogr.append(Note(p[0] - r, 4, 1, 100))
                    fsprog7.append(Note(p[0] - r + se, 4, 1, 100))
                    fsprog9.append(Note(p[0] - r + 14, 4, 1, 100))
    return fsprog
Ejemplo n.º 27
0
 def test_append(self):
     seq1 = NoteSeq("C# D#")
     seq1.append(Note("F#"))
     seq2 = NoteSeq("C# D# F#")
     self.assertEqual(seq1, seq2)
Ejemplo n.º 28
0
def fusion_prog_rym(rm_data, root, seventh, nine):
    # note indicates which note in the chord you want to put in the seq
    fsprogr = NoteSeq()
    fsprog7 = NoteSeq()
    fsprog9 = NoteSeq()
    fsprog = [fsprogr, fsprog7, fsprog9]
    # the note of melody could be the 1, b3, 3, 5, b7, 7th of the chord
    pitch_in_chord = [0, 3, 4, 7, 10, 11]
    melody_len = 0
    for p in rm_data:
        melody_len += p[2]
        r = random.choice(pitch_in_chord)
        se = random.choice(seventh)
        if p[2] == 0.25:
            fsprogr.append(Note(p[0] - r, 4, p[2], 100))
            fsprog7.append(Note(p[0] - r + se, 4, p[2], 100))
            fsprog9.append(Note(p[0] - r + 14, 4, p[2], 100))
        elif p[2] == 0.5:
            fsprogr.append(Note(p[0] - r, 4, p[2], 100))
            fsprog7.append(Note(p[0] - r + se, 4, p[2], 100))
            fsprog9.append(Note(p[0] - r + 14, 4, p[2], 100))
        elif p[2] == 0.125:
            fsprogr.append(Rest(p[2]))
            fsprog7.append(Rest(p[2]))
            fsprog9.append(Rest(p[2]))
    return fsprog
def make_midi(midi_path, notes, bpm, instrument, beat):
    note_names = 'c c# d d# e f f# g g# a  a# b'.split()

    result = NoteSeq()
    melody_dur = 0
    for n in notes:
        if (n[1] < 0):
            duration = (1.0 / -n[1]) + (1.0 / -n[1] / 2)
        else:
            duration = 1.0 / n[1]
        melody_dur += duration

        if n[0].lower() == 'r':
            result.append(Rest(dur=duration))
        else:
            pitch = n[0][:-1]
            octave = int(n[0][-1]) + 1
            pitch_number = note_names.index(pitch.lower())

            result.append(
                Note(pitch_number, octave=octave, dur=duration, volume=100))
            print(pitch_number, octave, duration)

    duration = 1.0 / beat
    harmony_len = math.ceil(melody_dur / duration)
    harmony_len += (4 - harmony_len % 4)

    pitch = [[0, 4, 7], [9, 0, 4], [2, 5, 9], [7, 11, 2, 5]]
    octave = [[5, 5, 5], [5, 6, 6], [5, 5, 5], [5, 5, 6, 6]]

    guitar0 = NoteSeq()
    for n in range(harmony_len):
        index = int(math.floor(n % 16 / 4))
        if (n >= math.ceil(melody_dur / duration)):
            volume = 50
        else:
            volume = 70
        guitar0.append(
            Note(pitch[index][0],
                 octave=octave[index][0],
                 dur=duration,
                 volume=volume))

    guitar1 = NoteSeq()
    for n in range(harmony_len):
        index = int(math.floor(n % 16 / 4))
        if (n >= math.ceil(melody_dur / duration)):
            volume = 50
        else:
            volume = 70
        guitar1.append(
            Note(pitch[index][1],
                 octave=octave[index][1],
                 dur=duration,
                 volume=volume))

    guitar2 = NoteSeq()
    for n in range(harmony_len):
        index = int(math.floor(n % 16 / 4))
        if (n >= math.ceil(melody_dur / duration)):
            volume = 50
        else:
            volume = 70
        guitar2.append(
            Note(pitch[index][2],
                 octave=octave[index][2],
                 dur=duration,
                 volume=volume))

    guitar3 = NoteSeq()
    for n in range(harmony_len):
        index = int(math.floor(n % 16 / 4))
        if (n >= math.ceil(melody_dur / duration)):
            volume = 50
        else:
            volume = 70
        if (index == 3):
            guitar3.append(
                Note(pitch[index][3],
                     octave=octave[index][3],
                     dur=duration,
                     volume=volume))
        else:
            guitar3.append(Rest(dur=duration))

    cymbal = NoteSeq()
    for n in range(harmony_len):
        index = int(math.floor(n % 16 / 4))
        if (n >= math.ceil(melody_dur / duration)):
            volume = 70
        else:
            volume = 100
        pitch = 10
        octave = 3
        cymbal.append(Note(pitch, octave=octave, dur=duration, volume=volume))

    kick = NoteSeq()
    for n in range(harmony_len):
        index = int(math.floor(n % 16 / 4))
        if (n >= math.ceil(melody_dur / duration)):
            volume = 70
        else:
            volume = 100
        pitch = 0
        octave = 3

        if (n % 4 == 0):
            kick.append(Note(pitch, octave=octave, dur=duration,
                             volume=volume))
        else:
            kick.append(Rest(dur=duration))

    snare = NoteSeq()
    for n in range(harmony_len):
        index = int(math.floor(n % 16 / 4))
        if (n >= math.ceil(melody_dur / duration)):
            volume = 70
        else:
            volume = 100
        pitch = 4
        octave = 3

        if (n % 4 == 2):
            snare.append(
                Note(pitch, octave=octave, dur=duration, volume=volume))
        else:
            snare.append(Rest(dur=duration))

    midi = Midi(number_tracks=8,
                tempo=bpm,
                instrument=[instrument, 25, 25, 25, 25, 0, 0, 0])
    midi.seq_notes(result, track=0)
    midi.seq_notes(guitar0, track=1)
    midi.seq_notes(guitar1, track=2)
    midi.seq_notes(guitar2, track=3)
    midi.seq_notes(guitar3, track=4)
    midi.seq_notes(cymbal, track=5, channel=9)
    midi.seq_notes(kick, track=6, channel=9)
    midi.seq_notes(snare, track=7, channel=9)
    midi.write(midi_path)