def add_session_meta_data_to_midi_file(midi_file: MIDIFile, session: Session): track, time = 0, 0 midi_file.addTrackName(track, time, 'Wouter\'s Beat Generator (tm)') midi_file.addTimeSignature( track, time, session.time_signature.numerator, session.time_signature.denominator, session.time_signature.ticks_per_quarter_note) midi_file.addTempo(track, time, session.tempo_bpm)
def writeMidi(sequences, filename, bpm, timeQuarter, timeBeats): track = 0 #used midi channel channel = 9 #set time, in beats time = 0 #set timeQuarter in beats, 0.5 -> .../8 time signature duration = 1/timeQuarter # Initalize MIDIUtil stuff MyMIDI = MIDIFile(2, adjust_origin=True) # Add a BPM track MyMIDI.addTempo(track, time, bpm) # Set the time signatures # Denominator values are: # 2 = */4 time signatures # 3 = */8 time signatures if timeBeats == 4 or timeBeats == 8: # Just do 4/4 (prevents 8/8 time signatures) numerator = 4 denominator = 2 else: # Let the computer work it out numerator = timeBeats denominator = int((timeQuarter*0.5) + 1) MyMIDI.addTimeSignature(track, 0, numerator, denominator, 24) # Each beat will only have material for half a measure # This copies the first half into the empty second half kikTrigs = sequences[0] + sequences[0] snrTrigs = sequences[1] + sequences[1] hhcTrigs = sequences[2] + sequences[2] # Check for triggers # Normal triggers (1) will result in a note at velocity 127 # Random triggers (2) will result in a note at velocity 63 for i in range(timeBeats*2): if kikTrigs[i] >= 1: velocity = (kikTrigs[i] *-64) + 191 MyMIDI.addNote(track, channel, 36, i * duration, duration, velocity) if snrTrigs[i] >= 1: velocity = (snrTrigs[i] *-64) + 191 MyMIDI.addNote(track, channel, 38, i * duration, duration, velocity) if hhcTrigs[i] >= 1: velocity = (hhcTrigs[i] *-64) + 191 MyMIDI.addNote(track, channel, 42, i * duration, duration, velocity) # Modify given filename to write the file inside of the midi folder filename = "midi/"+filename #write to MIDIfile with open(filename+".mid", "wb") as output_file: MyMIDI.writeFile(output_file) print(col.info + " midi written to", filename+".mid\n" + col.reset)
def testTimeSignature(self): time = 0 track = 0 numerator = 4 denominator = 2 clocks_per_tick = 24 MyMIDI = MIDIFile(1, file_format=2) MyMIDI.addTimeSignature(track, time, numerator, denominator, clocks_per_tick) MyMIDI.close() data = Decoder(MyMIDI.tracks[0].MIDIdata) self.assertEqual(MyMIDI.tracks[0].MIDIEventList[0].type, 'TimeSignature') self.assertEqual(data.unpack_into_byte(0), 0x00) # time self.assertEqual(data.unpack_into_byte(1), 0xFF) # Code self.assertEqual(data.unpack_into_byte(2), 0x58) # subcode self.assertEqual(data.unpack_into_byte(3), 0x04) # Data length self.assertEqual(data.unpack_into_byte(4), numerator) self.assertEqual(data.unpack_into_byte(5), denominator) self.assertEqual(data.unpack_into_byte(6), clocks_per_tick) # Data length self.assertEqual(data.unpack_into_byte(7), 0x08) # 32nd notes per quarter note # We also want to check with a format 1 file, make sure it ends up in # the tempo track time = 0 track = 1 numerator = 4 denominator = 2 clocks_per_tick = 24 MyMIDI = MIDIFile(2, file_format=1) MyMIDI.addTimeSignature(track, time, numerator, denominator, clocks_per_tick) MyMIDI.close() data = Decoder(MyMIDI.tracks[0].MIDIdata) self.assertEqual(MyMIDI.tracks[0].MIDIEventList[0].type, 'TimeSignature') self.assertEqual(data.unpack_into_byte(0), 0x00) # time self.assertEqual(data.unpack_into_byte(1), 0xFF) # Code self.assertEqual(data.unpack_into_byte(2), 0x58) # subcode self.assertEqual(data.unpack_into_byte(3), 0x04) # Data length self.assertEqual(data.unpack_into_byte(4), numerator) self.assertEqual(data.unpack_into_byte(5), denominator) self.assertEqual(data.unpack_into_byte(6), clocks_per_tick) # Data length self.assertEqual(data.unpack_into_byte(7), 0x08) # 32nd notes per quarter note
def MakeMidi(name, amountOfSixteenths, sixteenInterval, bpm, beatsPerMeasure, beatUnit, Kick_seq, Snare_seq, HiHat_seq): track = 0 channel = 9 #used midi channel time = 0 #set time, in beats duration = 0.25 #set duration in beats, 0.25 -> .../16 time signature bpm = bpm #set bpm velocity = 100 #set velocity # 0-127, as per the MIDI standard #create a track - defaults to format 2 - to enable addTimeSignature functionality MyMIDI = MIDIFile(2, adjust_origin = True) #set track, tempo and time MyMIDI.addTempo(track, time, bpm) #add timesig MyMIDI.addTimeSignature(track, 0, beatsPerMeasure, int(math.log(beatUnit, 2)), 24) Kick = Kick_seq.pop(0) Snare = Snare_seq.pop(0) HiHat = HiHat_seq.pop(0) #add bassdrum for i in range(0, amountOfSixteenths): if i == Kick: MyMIDI.addNote(track, channel, 35, (time + i) * duration, duration, velocity) if Kick_seq: Kick = Kick_seq.pop(0) #add snare if i == Snare: MyMIDI.addNote(track, channel, 38, (time + i) * duration, duration, velocity) if Snare_seq: Snare = Snare_seq.pop(0) #add HiHat if i == HiHat: MyMIDI.addNote( track, channel, 42, (time + i) * duration, duration, velocity) if HiHat_seq: HiHat = HiHat_seq.pop(0) #write to MIDIfile with open("CreatedMidiFiles/" + name + ".mid", "wb") as output_file: MyMIDI.writeFile(output_file)
def generateMIDI(lijstKick, lijstSnare, lijstHihat, beatsPerMeasure, MIDIname, tempo): if beatsPerMeasure == 14: divider = 2 else: divider = 1 track = 0 #used midi channel channel = 9 #set time, in beats time = 0 #set duration in beats, 0.5 -> .../8 time signature duration = 0.5 #set bpm bpm = tempo #set velocity velocity = 100 # 0-127, as per the MIDI standard #create a track - defaults to format 2 - to enable addTimeSignature functionality if beatsPerMeasure > 7: n = 2#in the second mode(see main.py, the beatsPerMeasure is 14, but for the timeSignature we need 7) else: n = 1 #numerator is the amount of counts in the timeSignature numerator = int(beatsPerMeasure / n) #checks if the beatsPerMeasure can be divided by 5 if not(beatsPerMeasure % 5): d = 2#4th note----> 5/4 else: d = 3#8th note ---> 7/8 denominator = d MyMIDI = MIDIFile(2, adjust_origin=True) #set track, tempo and time MyMIDI.addTempo(track, time, bpm) #set timeSignature MyMIDI.addTimeSignature(track, time, numerator, denominator, clocks_per_tick=24, notes_per_quarter=8) counter = 0 #sets counter to zero for i in range(beatsPerMeasure): #wanneer de counter het aantal gegeven beats heeft doorlopen stopt de functie if counter == beatsPerMeasure: generateList = False else: if i in lijstKick: MyMIDI.addNote( track, channel, 36, (time + i / divider ) * duration, duration / divider, velocity)#In this line 'i' is time of event # counter += 1 if i in lijstSnare: MyMIDI.addNote( track, channel, 38, (time + i / divider ) * duration, duration / divider, velocity)#In this line 'i' is time of event # counter += 1 if i in lijstHihat: MyMIDI.addNote( track, channel, 42, (time + i / divider) * duration, duration / divider, velocity)#In this line 'i' is time of event counter += 1 else: pass #create midi directory if it doesn't exist if not os.path.isdir(mypath): os.makedirs(mypath) # export a midi file with open(MIDIname, "wb") as output_file: MyMIDI.writeFile(output_file) print(colors.bcolors.GREEN + 'MIDI file is saved as: "'+ MIDIname + '"' + colors.bcolors.ENDC)
# gets key signature # keySig = getKeySig() # asks user how many measures they want the song to be numBars = getNumBars() # time signature print("What time signature will you use?") print() numerator = getMeasureBeats() denominator = getSubDiv() denominator = int(math.log(denominator, 2)) clocks_per_tick = 24 notes_per_quarter = 8 MyMIDI.addTimeSignature(track, time, numerator, denominator, clocks_per_tick) # total_time represents the number of notes total_time = (numerator * numBars) while time < total_time: # put me in addNote for pitch rand_note = random.choice(MIDI_BASS_CLEF) rand_duration = 1 / (random.choice(NOTE_VALUES)) MyMIDI.addNote(track, channel, rand_note, time, rand_duration, volume) time += float(1 / (random.choice(NOTE_VALUES))) # write file to disk name_file = input("What do you want to name the file (name.midi)? ") binfile = open(name_file, 'wb') MyMIDI.writeFile(binfile) binfile.close()
#set time signature """ addTimeSignature(track, time, numerator, denominator, clocks_per_tick, notes_per_quarter=8) The denominator should be specified as a power of 2, with a half note being one, a quarter note being two, and eight note being three, etc. Thus, for example, a 4/4 time signature would have a numerator of 4 and a denominator of 2. A 7/8 time signature would be a numerator of 7 and a denominator of 3. The clocks_per_tick argument specifies the number of clock ticks per metronome click. By definition there are 24 ticks in a quarter note, so a metronome click per quarter note would be 24. A click every third eighth note would be 3 * 12 = 36. """ MyMIDI.addTimeSignature(track, 0, 7, 3, 24) #adding a 7/8 rhythm #add bassdrum MyMIDI.addNote(track, channel, 35, 0, duration, velocity) #add snare MyMIDI.addNote(track, channel, 38, 3 * duration, duration, velocity) MyMIDI.addNote(track, channel, 38, 5 * duration, duration, velocity) #add hi-hat for i in range(7): MyMIDI.addNote( track, channel, 42, (time + i) * duration, duration, velocity) #write to MIDIfile