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 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 test_init_string(self): seq1 = NoteSeq([Note(0, 4, 0.125), Note(2, 4, 0.125)]) seq2 = NoteSeq([Note(0, 6, 0.125), Rest(0.25)]) seq3 = NoteSeq([Note(0, 5, 0.25), Note(2, 5, 0.25)]) self.assertEqual(NoteSeq("C8, D"), seq1) self.assertEqual(NoteSeq("c8, d"), seq1) self.assertEqual(NoteSeq("c8'' r4"), seq2) self.assertEqual(NoteSeq("C8'' R4"), seq2) self.assertEqual(NoteSeq("C D"), seq3)
def from_pitch_track(times, pitch, sample_rate, filename="tmp.midi"): midi_notes = [to_midi(x) for x in pitch] notes = compact(midi_notes, step=(times[1] - times[0]) / sample_rate / 2) track0 = [Note(x, 0, round(t, 4)) if not np.isnan(x) else Rest(dur=round(t, 4)) for x, t in notes] m = Midi(1, tempo=90) m.seq_notes(track0, track=0) m.write(filename) return filename
class Chords: """Definition of basic chords (Major key, Minor key)""" silence = Chord([Rest()], name="Silence") # Major key I = Maj1 = Chord("C E G ", name="I") ii = min2 = Chord("D F A ", name="ii") iii = min3 = Chord("E G B ", name="iii") IV = Maj4 = Chord("F A C''", name="IV") V = Maj5 = Chord("G B D''", name="V") vi = min6 = Chord("A C'' E ", name="vi") vii = dim7 = Chord("B D'' F ", name="vii") # Minor key I_min_key = Chord("C D# G ", name="I_min_key") ii_min_key = Chord("D F G# ", name="ii_min_key") iii_min_key = Chord("D# G A# ", name="iii_min_key") IV_min_key = Chord("F G# C''", name="IV_min_key") V_min_key = Chord("G A# D''", name="V_min_key") vi_min_key = Chord("G# C'' D# ", name="vi_min_key") vii_min_key = Chord("A# D'' F ", name="vii_min_key") major_scale = [ I, I.inversion(), I.inversion().inversion(), I.octave_shift(1), I.octave_shift(-1), ii, iii, IV, V, vi ] # happy minor_scale = [ I_min_key, I_min_key, I_min_key.inversion(), I_min_key.inversion().inversion(), I_min_key.octave_shift(1), I_min_key.octave_shift(-1), ii_min_key, iii_min_key, IV_min_key, V_min_key, vi_min_key ] # sad mixed_scale = major_scale + minor_scale # for chord progression: chord_prog_first = [ I, I.inversion(), I.inversion().inversion(), I.octave_shift(1), I.octave_shift(-1), I_min_key, I_min_key.inversion(), I_min_key.inversion().inversion(), I_min_key.octave_shift(1), I_min_key.octave_shift(-1) ] chord_prog_middle = [ ii, ii_min_key, iii, iii_min_key, IV, IV_min_key, V, V_min_key, vi, vi_min_key ] chord_prog_last = [IV, IV_min_key, V, V_min_key]
def test_sum_with_rest(self): # Test adding in regular order rest1 = Rest(1) seq = NoteSeq("C2 D8 E4") expected = NoteSeq("C2 D8 E4 R1") self.assertEqual(seq + rest1, expected) # Test reverse adding, rest first seq = NoteSeq("C2 D8 E4") expected = NoteSeq("R1 C2 D8 E4") self.assertEqual(rest1 + seq, expected)
def test_init(self): notes = [Note(0, 5), Note(2, 5)] seq1 = NoteSeq(notes) seq2 = NoteSeq("C#2' D#4''") seq3 = NoteSeq([Note(1, 5, 0.5), Note(3, 6, 0.25)]) self.assertEqual(seq1, NoteSeq(notes)) self.assertEqual(seq2, seq3) self.assertNotEqual(seq1, NoteSeq(notes + [Note(3, 5)])) self.assertRaises(MusiclibError, NoteSeq, [Note(1, 5, 0.5), Rest(2), 1]) self.assertRaises(MusiclibError, NoteSeq, 1)
def create_midi_with_time(music, beat_of_music): global LEN_OF_MUSIC noteSeq = [] for i in range(LEN_OF_MUSIC): if music[i] == 101: noteSeq.append(Rest(dur=beat_of_music[i])) else: noteSeq.append(Note(music[i], dur=beat_of_music[i])) seq = NoteSeq(noteSeq) midi = Midi(number_tracks=1, tempo=90) midi.seq_notes(seq, track=0) midi.write("midi/markov_Gavotte_test1.mid")
def test_init_with_rest(self): seq1 = NoteSeq("C4 R4 D4") seq2 = NoteSeq("C8 R D") seq3 = NoteSeq([Note(0, dur=0.25), Rest(0.25), Note(2, dur=0.25)]) self.assertEqual(seq1[0].dur, 0.25) self.assertEqual(seq1[1].dur, 0.25) self.assertEqual(seq1[2].dur, 0.25) self.assertEqual(seq2[0].dur, 0.125) self.assertEqual(seq2[1].dur, 0.125) self.assertEqual(seq2[2].dur, 0.125) self.assertEqual(seq3[0].dur, 0.25) self.assertEqual(seq3[1].dur, 0.25) self.assertEqual(seq3[2].dur, 0.25)
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
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)
def midicreate(notelist,name,page): NoteList = [] for i in range(len(notelist)): if(notelist[i].scale=='Rest'): chord = Rest(notelist[i].tempo) elif(notelist[i].harmony==0): continue elif(notelist[i].harmony==2): chord = NoteSeq([Note(notelist[i].scale,dur=notelist[i].tempo),Note(notelist[i+1].scale,dur=notelist[i].tempo)]) elif(notelist[i].harmony==3): chord = NoteSeq([Note(notelist[i].scale,dur=notelist[i].tempo),Note(notelist[i+1].scale,dur=notelist[i].tempo),Note(notelist[i+2].scale,dur=notelist[i].tempo)]) elif(notelist[i].harmony==4): chord = NoteSeq([Note(notelist[i].scale,dur=notelist[i].tempo),Note(notelist[i+1].scale,dur=notelist[i].tempo),Note(notelist[i+2].scale,dur=notelist[i].tempo),Note(notelist[i+3].scale,dur=notelist[i].tempo)]) else: chord = NoteSeq([Note(notelist[i].scale,dur=notelist[i].tempo)]) NoteList.append(chord) midi = Midi(1, tempo=117) checktract = 0 midi.seq_chords(NoteList,track=0) midi.write(f"/home/ec2-user/Ourchord/MIDI/{page}/{name}.mid") # ---------------------------------------------------- 경로 수정
def test_seq_chords_with_rest(self): chords = [Rest(), NoteSeq("G B D")] midi = Midi() midi.seq_chords(chords)
from pyknon.genmidi import Midi from pyknon.music import NoteSeq from pyknon.music import Note, Rest import math #Input Vector, currently set to test. inputV = [23, 44, 46, 156, 54, 1, 0, 55, 34, 223, 34, 45, 22, 54, 23, 11] dimension = math.log(len(inputV), 2) #Equals N, where the length of inputV is 2^N duration = 1 / dimension #Scales the note duration accordingly. #--------Construction of Note Sequence---------------- note_sequence = [Rest(2)] #Create two bars of rest at the start. for value in inputV: #Takes values from inputV and maps them to semitones. #0-3 -> Eb 2nd Octave , 252-255 -> F# 7th Octave scaled_value = int(value / 4) #Scales value from 0-63 note_value = (scaled_value + 27) % 12 octave = int((scaled_value + 27 - note_value) / 12) note = Note(note_value, octave, duration) note_sequence.append(note) #Adds them to the note sequence melody = NoteSeq(note_sequence) #--------Backing Drumbeat Construction -------------------- b = Note(11, 2)
def test_stretch_dur(self): n1 = Rest(dur=0.25) n2 = n1.stretch_dur(2) n3 = n1.stretch_dur(0.5) self.assertEqual(n2.dur, 0.5) self.assertEqual(n3.dur, 0.125)
def test_inversion_rest(self): seq1 = NoteSeq("C E R G") seq2 = NoteSeq([Note(0, 5), Note(8, 4), Rest(), Note(5, 4)]) self.assertEqual(seq1.inversion(0), seq2)
def test_transposition_startswith_rest(self): seq1 = NoteSeq("E G R C#") seq2 = NoteSeq([Note(2, 5), Note(5, 5), Rest(), Note(11, 4)]) self.assertEqual(seq1.transposition_startswith(Note(2, 5)), seq2)
print("Track {}".format(cur)) print("--------") notes = notesStr.findall(tracks[cur-1]) PARTS = partRStr.findall(x) print("Note Count: {}".format(len(notes))) print("Part Count: {}".format(len(PARTS))) if len(notes) == 0: print("h") continue for N in PARTS: notes = notesStr.findall(N) firstNote = notes[0] firstNoteStart = int(startStr.findall(firstNote)[0].strip("<t>").strip("</t>")) noteinfo = [] if firstNoteStart != 0: noteinfo.append( Rest( firstNoteStart / 1920 ) ) noteinfo.append( Rest( int(startStr.findall( N )[0].strip("<t>").strip("</t>"))/1920 ) ) lastOn = None lastOff = None for b in notes: sta = startStr.findall(b)[0].strip("<t>").strip("</t>") dur = duratStr.findall(b)[0].strip("<dur>").strip("</dur>") pit = pitchStr.findall(b)[0].strip("<n>").strip("</n>") if lastOn: if sta == lastOff: noteinfo.append(Note( int(pit)+12, -2, int(dur) / 1920)) else: noteinfo.append(Rest( (((int(dur) + int(sta)) - lastOff)-int(dur)) / 1920)) noteinfo.append(Note( int(pit)+12, -2, int(dur) / 1920)) else: noteinfo.append(Note( int(pit)+12, -2, int(dur) / 1920))
def dothething(label): print("Starting...") #track info# tempoStr = re.compile("<tempo>(.*)<\/tempo>") trackStr = re.compile("<vsTrack>[\s\S]*?<\/vsTrack>") otherStr = re.compile("<v>(.*)<\/v>") #note info# notesStr = re.compile("<note>[\s\S]*?</note>") pitchStr = re.compile("<n>[\s\S]*?</n>") startStr = re.compile("<t>[\s\S]*?</t>") duratStr = re.compile("<dur>[\s\S]*?</dur>") partRStr = re.compile("<vsPart>[\s\S]*?</vsPart>") #playtStr = re.compile("<playTime>[\s\S]*?</playTime>") # PianoKey = ["C", "C#", "D", "D#", 'E', 'F', 'G#','A','A#','B'] def NotefromNum(num): a = Note(num - 84) return str(a).strip("<").strip(">") + str(a.verbose).split(", ")[1] #ask user for info #load file, make it readable file_path = filedialog.askopenfilename() file = open(file_path).read().replace("\n", "") #get more info tempo = int(otherStr.findall(tempoStr.findall(file)[0])[0]) tracks = trackStr.findall(file) parts = partRStr.findall(file) NewPrint("Tempo: {}".format(tempo / 100), label) NewPrint("Tracks: {}".format(len(tracks)), label) NewPrint("Parts: {}".format(len(parts)), label) #NOW for the important stuff ''' For some reason, vocaloid stores note data as numbers. And also for some reason, the number 0 is equal to C-2, As in C negative 2 what the f**k ''' cur = 1 midi = Midi(len(tracks), tempo=tempo / 100) for x in tracks: NewPrint("Track {}".format(cur), label) NewPrint("--------", label) notes = notesStr.findall(tracks[cur - 1]) PARTS = partRStr.findall(x) NewPrint("Note Count: {}".format(len(notes)), label) NewPrint("Part Count: {}".format(len(PARTS)), label) if len(notes) == 0: NewPrint("empty track, skipping", label) continue for N in PARTS: notes = notesStr.findall(N) firstNote = notes[0] firstNoteStart = int( startStr.findall(firstNote)[0].strip("<t>").strip("</t>")) noteinfo = [] if firstNoteStart != 0: noteinfo.append(Rest(firstNoteStart / 1920)) noteinfo.append( Rest( int(startStr.findall(N)[0].strip("<t>").strip("</t>")) / 1920)) lastOn = None lastOff = None for b in notes: sta = startStr.findall(b)[0].strip("<t>").strip("</t>") dur = duratStr.findall(b)[0].strip("<dur>").strip("</dur>") pit = pitchStr.findall(b)[0].strip("<n>").strip("</n>") if lastOn: if sta == lastOff: noteinfo.append( Note(int(pit) + 12, -2, int(dur) / 1920)) else: noteinfo.append( Rest((( (int(dur) + int(sta)) - lastOff) - int(dur)) / 1920)) noteinfo.append( Note(int(pit) + 12, -2, int(dur) / 1920)) else: noteinfo.append(Note(int(pit) + 12, -2, int(dur) / 1920)) lastOn = int(sta) lastOff = int(sta) + int(dur) seq = NoteSeq(noteinfo) midi.seq_notes(seq, track=cur - 1) cur += 1 midi.write("{}.mid".format(file_path.strip(".vsqx"))) NewPrint("Saved to {}".format("{}.mid".format(file_path.strip(".vsqx"))), label)
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
def test_verbose(self): result = Rest(0.5).verbose self.assertEqual(result, "<Rest: 0.5>")
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
def test_repr(self): representation = Rest(0.5).__repr__() self.assertEqual(representation, "<R: 0.5>")
from pyknon.music import Note from pyknon.music import NoteSeq from pyknon.music import Rest from pyknon.genmidi import Midi C_note = Note(0, 5, dur=0.25) A_note = Note(9, 5, 0.25) quarter_rest = Rest(0.25) # quarter rest # create a sequence of notes and rests seq = NoteSeq([C_note, A_note, quarter_rest, C_note]) midi = Midi(1, tempo=120) midi.seq_notes(seq, track=0) midi.write("simple_noteseq.mid")
c = NoteSeq([Note(3, dur=1)]) d = NoteSeq([Note(2, dur=1)]) e = NoteSeq("R8 Ab8 Ab8 Ab8") f = NoteSeq("R8 Eb8'' Eb8'' Eb8''") g = NoteSeq([Note(7, dur=0.75)]) h = NoteSeq([Note(0, dur=1)]) i = NoteSeq("G8 G8 G8 G8") j = NoteSeq([Note(0, dur=0.5)]) k = NoteSeq([Note(7, dur=0.5)]) track3 = NoteSeq([Note(0, octave=6, dur=1)]) track4 = NoteSeq([Note(3, dur=1), Note(3, dur=1)]) bass = NoteSeq("G8,, G8,, G8,,") bass2 = NoteSeq([Note(3, octave=3, dur=1)]) notes1 = a + c + b + d + a + e + f + h + j + Rest(dur=0.5) notes2 = Rest(dur=4.0) + k + g + i notes3 = Rest(dur=4.5) + track3 notes4 = Rest(dur=3.5) + track4 notes5 = Rest(dur=0.125) + bass + bass2 + Rest(dur=0.125) + bass + bass2 midi = Midi(number_tracks=5, tempo=108) midi.seq_notes(notes1, track=0) midi.seq_notes(notes2, track=1) midi.seq_notes(notes3, track=2) midi.seq_notes(notes4, track=3) midi.seq_notes(notes5, track=4) midi.write("fifth11.mid")
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)
def midicreate(notelist, name): ''' onelist = [] twolist = [] threelist = [] fourlist = [] fivelsit = [] sixlist = [] sevenlist = [] eightlist = [] ninelist = [] tenlist = [] elevenlist = [] twelvelist = [] ''' #NoteList = [onelist, twolist, threelist, fourlist, fivelsit, sixlist, sevenlist, eightlist, ninelist, tenlist, elevenlist, twelvelist] NoteList = [] #print(notelist) for i in range(len(notelist)): #print(notelist[i][2],notelist[i][3],notelist[i][5]) if (notelist[i][3] == 'Rest'): chord = Rest(notelist[i][2]) elif (notelist[i][5] == 0): continue elif (notelist[i][5] == 2): chord = NoteSeq([ Note(notelist[i][3], dur=notelist[i][2]), Note(notelist[i + 1][3], dur=notelist[i][2]) ]) elif (notelist[i][5] == 3): chord = NoteSeq([ Note(notelist[i][3], dur=notelist[i][2]), Note(notelist[i + 1][3], dur=notelist[i][2]), Note(notelist[i + 2][3], dur=notelist[i][2]) ]) elif (notelist[i][5] == 4): chord = NoteSeq([ Note(notelist[i][3], dur=notelist[i][2]), Note(notelist[i + 1][3], dur=notelist[i][2]), Note(notelist[i + 2][3], dur=notelist[i][2]), Note(notelist[i + 3][3], dur=notelist[i][2]) ]) else: chord = NoteSeq([Note(notelist[i][3], dur=notelist[i][2])]) NoteList.append(chord) #else: #NoteList.append(Rest(notelist[i][2])) #print(NoteList) #seq = NoteSeq(NoteList) #midi = Midi(number_tracks=2, tempo=90) #midi.seq_notes(NoteList, track=0) #midi.seq_notes(notes2, track=0) midi = Midi(1, tempo=117) checktract = 0 ''' if(checktract<= tracknum): print(NoteList[checktract]) midi.seq_notes(NoteList[checktract], track=checktract) checktract=checktract+1 ''' midi.seq_chords(NoteList, track=0) midi.write(f"/home/ec2-user/Ourchord/MIDI/{name}.mid" ) # ---------------------------------------------------- 경로 수정
def generateMusic(inputV, inputTempo): dimension = math.log(len(inputV), 2) #Equals N, where the length of inputV is 2^N duration = math.pow(len(inputV) / 4, -1) #Scales the note duration accordingly. #--------Construction of Note Sequence---------------- note_sequence = [Note(0, 4), Rest(0.75), Note(0, 4), Rest(0.75)] #Create two bars of rest at the start. for value in inputV: #Note Value: #0-C, 1-C#, 2-D, 3-D#, 4-E, 5-F, 6-F# #7-G, 8-G#, 9-A, 10-A#, 11-B #----DIATONIC------------ #Over 4 Octaves, 28 notes #0-9 -> 3C #10-18 -> 3D #37-46 #82-90 -> 4E #127-136 -> 5C , 137-146 -> 5D diatonicValue = [0, 2, 4, 5, 7, 9, 11] Dictionary = dict({ (0, 0, 0): 0, (255, 0, 0): 1, (255, 255, 0): 2, (0, 255, 0): 3, (0, 255, 255): 4, (0, 0, 255): 5, (255, 0, 255): 6, (255, 255, 255): 7 }) note_value = diatonicValue[Dictionary[tuple(value)] % 7] octave = 4 + (Dictionary[tuple(value)] // 7) print(octave) note = Note(note_value, octave, duration) note_sequence.append(note) #------------------------ #Adds them to the note sequence melody = NoteSeq(note_sequence) #--------Backing Drumbeat Construction -------------------- b = Note(11, 2, volume=50) hh = Note(6, 3, volume=50) drumBeat = NoteSeq([ b, hh, hh, hh, b, hh, hh, hh, b, hh, hh, hh, b, hh, hh, hh, b, hh, hh, hh, b, hh, hh, hh ]) #-------Creating MIDI file ------------- m2 = Midi(number_tracks=2, tempo=inputTempo, channel=[0, 9], instrument=[1, 1]) #See https://www.midi.org/specifications/item/gm-level-1-sound-set for instrument codes m2.seq_notes(drumBeat, track=0, channel=9) #Channel 9 = Percussion m2.seq_notes(melody, track=1, channel=0) m2.write("output.mid") print("Success")