def to_midi(events_): midistream = MIDIFile(1) track = 0 time = 0 channel = 0 volume = 100 midistream.addTrackName(track, time, "Track") midistream.addTempo(track, time, 60) temp = [] for index, event_ in enumerate(events_): if event_[0][0] == 144: #key down temp = event_ elif event_[0][0] == 128: #key up duration = (event_[1] - temp[1]) / 1000 pitch = event_[0][1] time = temp[1] / 1000 midistream.addNote(track, channel, pitch, time, duration, volume) # And write it to disk. binfile = open("temp/user_output.mid", 'wb') midistream.writeFile(binfile) print(midistream) binfile.close()
class Midi: """Musique midi""" def __init__(self, partition, tempo): # Définition des paramètres MIDI. piste = 0 temps = 0 self.sortieMidi = MIDIFile(1) # Nom de la piste. self.sortieMidi.addTrackName(piste, temps, "Gregorien") # Tempo. self.sortieMidi.addTempo(piste, temps, tempo) # Instrument (74 : flûte). self.sortieMidi.addProgramChange(piste, 0, temps, 74) # À partir des propriétés de la note, création des évènements # MIDI. for note in partition: channel = 0 pitch = note.hauteur duree = note.duree volume = 127 self.sortieMidi.addNote(piste, channel, pitch, temps, duree, volume) temps += duree def ecrire(self, chemin): """Écriture effective du fichier MIDI""" binfile = open(chemin, 'wb') self.sortieMidi.writeFile(binfile) binfile.close()
def generate_midi(notes): # Create the MIDIFile Object MyMIDI = MIDIFile(1) # Add track name and tempo. The first argument to addTrackName and # addTempo is the time to write the event. track = 0 time = 0 MyMIDI.addTrackName(track, time, "Sample Track") MyMIDI.addTempo(track, time, 120) # Add a note. addNote expects the following information: channel = 0 # pitch = 60 # duration = 1 volume = 100 # Now add the note. for note in notes: MyMIDI.addNote(track, channel, note.pitch, time, note.duration, volume) time += note.duration # And write it to disk. binfile = open("./server_files/output.mid", 'wb') MyMIDI.writeFile(binfile) binfile.close()
def save_midi(self, instrument): # Create the MIDIFile Object with 1 track self.MIDIFile = MIDIFile(len(self.tracks)) #self.MIDIFile.addProgramChange(0, 0, 0, instrument) for i, note_list in enumerate(self.tracks): # Tracks are numbered from zero. Times are measured in beats. track = i time = 0 # Add track name, tempo and instrument change event self.MIDIFile.addTrackName(track, time, "Track %s" % i) self.MIDIFile.addTempo(track, time, self.tempo) self.MIDIFile.addProgramChange(track, 0, time, instrument) for n in note_list: if len(n) == 2: note = n[0] channel = n[1] else: note = n channel = 0 self.add_note(track, channel, note) # And write it to disk. with open(self.outfile, 'wb') as binfile: self.MIDIFile.writeFile(binfile)
class Midi: """Musique midi""" def __init__(self,partition,tempo): # Définition des paramètres MIDI. piste = 0 temps = 0 self.sortieMidi = MIDIFile(1) # Nom de la piste. self.sortieMidi.addTrackName(piste,temps,"Gregorien") # Tempo. self.sortieMidi.addTempo(piste,temps, tempo) # Instrument (74 : flûte). self.sortieMidi.addProgramChange(piste,0,temps,74) # À partir des propriétés de la note, création des évènements # MIDI. for note in partition: channel = 0 pitch = note.hauteur duree = note.duree volume = 127 self.sortieMidi.addNote(piste, channel, pitch, temps, duree, volume) temps += duree def ecrire(self,chemin): """Écriture effective du fichier MIDI""" binfile = open(chemin, 'wb') self.sortieMidi.writeFile(binfile) binfile.close()
def save_midi(self): # Create the MIDIFile Object with 1 track self.MIDIFile = MIDIFile(len(self.tracks)) for i, note_list in enumerate(self.tracks): # Tracks are numbered from zero. Times are measured in beats. track = i time = 0 # Add track name and tempo. self.MIDIFile.addTrackName(track, time, "Track 1") self.MIDIFile.addTempo(track, time, self.tempo) for n in note_list: if len(n) == 2: note = n[0] channel = n[1] else: note = n channel = 0 self.add_note(track, channel, note) # And write it to disk. binfile = open(self.outfile, 'wb') self.MIDIFile.writeFile(binfile) binfile.close()
def create_midi(note_groups): midi = MIDIFile(1) track = 0 time = 0 channel = 0 volume = 100 midi.addTrackName(track, time, "Track") midi.addTempo(track, time, 140) for note_group in note_groups: duration = None for note in note_group: note_type = note.sym if note_type == "1": duration = 4 elif note_type == "2": duration = 2 elif note_type == "4,8": duration = 1 if len(note_group) == 1 else 0.5 pitch = note.pitch midi.addNote(track, channel, pitch, time, duration, volume) time += duration #midi.addNote(track,channel,pitch,time,4,0) # And write it to disk. binfile = open("output.mid", 'wb') midi.writeFile(binfile) binfile.close()
def save_song(self, output="my_file.mid"): self.song = MIDIFile(len(self.tracks_list)) for t in self.tracks_list: self.song.addTempo(self.actual_track, t.start, t.tempo) self.add_pitches(t) self.actual_track += 1 with open(output, "wb") as output_file: self.song.writeFile(output_file)
def create_midi(tempo, data): print('Converting to MIDI.') song = MIDIFile(1) song.addTempo(0, 0, tempo) grouped = [(note, sum(1 for i in g)) for note, g in groupby(data)] time = 0 for note, duration in grouped: add_note(song, 0, note, time, duration) time += duration return song
class Song: def __init__(self, tracks, length): self.song = None self.tracks = tracks self.length = length self.actual_track = 0 self.tracks_list = [] def add_track(self, track): self.tracks_list.append(track) def save_song(self, output="my_file.mid"): self.song = MIDIFile(len(self.tracks_list)) for t in self.tracks_list: self.song.addTempo(self.actual_track, t.start, t.tempo) self.add_pitches(t) self.actual_track += 1 with open(output, "wb") as output_file: self.song.writeFile(output_file) def add_pitches(self, track): for list in track.board.values(): for note in list: self.song.addNote(self.actual_track, self.actual_track, note.get_pitch(), note.time, note.duration, 100) self.song.addProgramChange(self.actual_track, self.actual_track, track.start, track.instrument)
def __init__(self,partition,tempo): # Définition des paramètres MIDI. piste = 0 temps = 0 self.sortieMidi = MIDIFile(1) # Nom de la piste. self.sortieMidi.addTrackName(piste,temps,"Gregorien") # Tempo. self.sortieMidi.addTempo(piste,temps, tempo) # Instrument (74 : flûte). self.sortieMidi.addProgramChange(piste,0,temps,74) # À partir des propriétés de la note, création des évènements # MIDI. for note in partition: channel = 0 pitch = note.hauteur duree = note.duree volume = 127 self.sortieMidi.addNote(piste, channel, pitch, temps, duree, volume) temps += duree
def __init__(self,partition,tempo): # Définition des paramètres MIDI. piste = 0 temps = 0 self.sortieMidi = MIDIFile(1) # Nom de la piste. self.sortieMidi.addTrackName(piste,temps,TITRE) # Tempo. self.sortieMidi.addTempo(piste,temps, tempo) # Instrument (74 : flûte). self.sortieMidi.addProgramChange(piste,0,temps,74) # À partir des propriétés de la note, création des évènements # MIDI. for neume in partition.musique: for note in (notes for notes in neume if type(notes) == Note): channel = 0 pitch = note.hauteur + partition.transposition duree = note.duree volume = 127 self.sortieMidi.addNote(piste, channel, pitch, temps, duree, volume) temps += duree
def save_midi(grid, path, click_track=False, click_track_start=0, accent_downbeat=False, period=0, pulse=1, bpm=240, channel=9, pitch=75, velocity=100): ''' bpm means grid-units per second in this case ''' phase, iois = rhythm_tools.grid_to_iois(grid) track = 0 mf = MIDIFile(1) mf.addTrackName(track, 0, "Sample Track") mf.addTempo(track, 0, bpm) rhythm_start = 0 if click_track: if click_track_start < 0: rhythm_start = -click_track_start click_track_start = 0 click_track_end = ( rhythm_start + len(iois) ) print(rhythm_start, click_track_start) write_click_track(mf, start_time=click_track_start, end_time=click_track_end, period=period, pulse=pulse, velocity=velocity, accent_downbeat=accent_downbeat) write_iois(mf, iois, start_time=rhythm_start, track=track, channel=channel, pitch=pitch, velocity=velocity) with open(path, 'wb') as midi_file: mf.writeFile(midi_file)
def make(self): with open("output.mid", 'wb') as f: MyMIDI = MIDIFile(1) track = 0 time = 0 channel = 0 volume = 100 MyMIDI.addTrackName(track, self.tempo, "Sample Track") MyMIDI.addTempo(track, time, 120) for part in self.structure: for note in self.riffs[part][0].score: MyMIDI.addNote(track, channel, note.pitch, time, note.duration, volume) time += note.duration MyMIDI.writeFile(f)
def createFile(self): MIDI = MIDIFile(1) MIDI.addTrackName(0,0,self.name) MIDI.addTempo(0,0, self.bpm) beat = 0 for chord in self.chords: for degree in chord.degrees: MIDI.addNote(0,0,self.scale.scaleNotes[degree],beat,1,chord.intensity) beat = beat + 1 if not os.path.exists(songFolder): os.makedirs(songFolder) midiFile = open("%s/%d.%d-%s.mid"%(songFolder, self.generation, self.songnum, self.name), 'wb') MIDI.writeFile(midiFile) midiFile.close()
class Midi: """Musique midi""" def __init__(self, partition, titre, tempo): # Définition des paramètres MIDI. piste = 0 temps = 0 self.sortiemidi = MIDIFile(1) # Nom de la piste. self.sortiemidi.addTrackName(piste, temps, sansaccents(titre)) # Tempo. self.sortiemidi.addTempo(piste, temps, tempo) # Instrument (74 : flûte). self.sortiemidi.addProgramChange(piste, 0, temps, 74) self.traiter_partition(partition, piste, temps) def traiter_partition(self, partition, piste, temps): """Création des évènements MIDI""" transposition = partition.transposition for neume in partition.musique: for note in ( notes for notes in neume if isinstance(notes, Note) ): channel = 0 pitch = note.hauteur + transposition duree = note.duree volume = 127 self.sortiemidi.addNote( piste, channel, pitch, temps, duree, volume ) temps += duree def ecrire(self, chemin): """Écriture effective du fichier MIDI""" with ( open(sys.stdout.fileno(), 'wb') if chemin == '-' else open(chemin, 'wb') )as sortie: self.sortiemidi.writeFile(sortie)
def GeneratePiece(voices, voiceSize, instRange, ratios): C = (instRange - voiceSize)//voices #the interval of imitation retval = MIDIFile(voices) #voices is the number of voices, numbered from bottom up #set tempi track = 0 for r in ratios: retval.addTempo(track, 0, r * 80) track+=1 #generate taleae, talea[0] is the top octave's talea, talea[1] is the next highest, etc. talea = [[]] for i in range(0, random.randint(3, 5)): talea[0].append(random.randint(1,5)) #each other talea is the same number of notes, with each note lengths incremented 1 or 2 times for m in range(1, math.floor(voiceSize//12)): talea.append([]) for i in range(0, len(talea[0])): talea[m].append(talea[m-1][i] + random.randint(1, 2)) print(talea) #calculate the length of the piece (in half steps) length = 1 temp = 0 for m in range(0, len(talea)): temp = 0 for i in range(0, len(talea[m])): temp += talea[m][i] length = lcm(length, temp) print(length) #figure out where each voice enters entrances = [] for m in range(0, len(ratios)): temp = (ratios[m]-1)/ratios[m] * length print(temp) entrances.append(temp) #write a melody for each octave of a single voice melody = GenerateMelody(talea, length) print (len(melody[0])) #write the melody to the midi file in each voice currentTime = 0 for n in range(0, voices): for m in range(0, voiceSize//12): currentTime = entrances[n] i = 0 while currentTime < length: i += 1 retval.addNote(n, 0, bottom + melody[m][i] + (C * n), currentTime, talea[m][i%len(talea[m])]/2, 100) currentTime+=talea[m][i%len(talea[m])] #add in a last note retval.addNote(n, 0, bottom + melody[m][0] + (C * n), currentTime, 1, 100) return retval
def midiSing(sheet, instruments, key, ticktime, filename): offset = NOTES.index(key) + 60 # Middle C is MIDI note #60 midi = MIDIFile(len(sheet)) replaceprint('Creating midi...') for t in range(0, len(sheet)): midi.addTrackName(t, 0, "Track %s" % t) midi.addTempo(t, 0, 60000 / (ticktime)) sheet[t] = sheet[t][1:] + [(sheet[t][0], 0)] tracklen = len(sheet[t]) for n in range(0, tracklen - 1): time, note = sheet[t][n] duration = sheet[t][(n + 1) % tracklen][0] - time midi.addNote( t, 0, offset + note, time, duration, 100) #MyMIDI.addNote(track,channel,pitch,time,duration,volume) replaceprint('Writing to file...') binfile = open(filename + ".mid", 'wb') midi.writeFile(binfile) binfile.close() replaceprint('Synth complete!') print("\nMID output to: \"" + filename + ".mid\"")
def __init__(self, partition, titre, tempo): # Définition des paramètres MIDI. piste = 0 temps = 0 self.sortiemidi = MIDIFile(1) # Nom de la piste. self.sortiemidi.addTrackName(piste, temps, sansaccents(titre)) # Tempo. self.sortiemidi.addTempo(piste, temps, tempo) # Instrument (74 : flûte). self.sortiemidi.addProgramChange(piste, 0, temps, 74) self.traiter_partition(partition, piste, temps)
def __init__(self, partition, tempo): # Définition des paramètres MIDI. piste = 0 temps = 0 self.sortieMidi = MIDIFile(1) # Nom de la piste. self.sortieMidi.addTrackName(piste, temps, "Gregorien") # Tempo. self.sortieMidi.addTempo(piste, temps, tempo) # Instrument (74 : flûte). self.sortieMidi.addProgramChange(piste, 0, temps, 74) # À partir des propriétés de la note, création des évènements # MIDI. for note in partition: channel = 0 pitch = note.hauteur duree = note.duree volume = 127 self.sortieMidi.addNote(piste, channel, pitch, temps, duree, volume) temps += duree
class GenerateMelody(object): def __init__(self, file, pitch, length, beat_duration): self.pitch = pitch self.length = abs(length) self.file = file self.midi_melody = MIDIFile(1) self.beat_duration = abs(beat_duration) def get_filename(self): if not self.file.endswith(".mid"): return self.file + ".mid" return self.file def get_random_pitch(self): step = random.randint(-5, 5) return abs(self.pitch + step) @staticmethod def get_random_volume(): return random.randint(70, 127) def generate(self): time = 0 self.midi_melody.addTempo(0, time, 60) while time < self.length: self.midi_melody.addNote(0, 0, self.get_random_pitch(), time, self.beat_duration, self.get_random_volume()) time += self.beat_duration binfile = open(self.get_filename(), 'wb') self.midi_melody.writeFile(binfile) binfile.close()
def arrayToMidiDouble(aArray, count): MyMIDI = MIDIFile(1) MyMIDI.addTrackName(0,0,"Red") MyMIDI.addTempo(0,0,120) time = 0 for j in range(len(aArray)): # randDur = choice([0.5, 0.25, 1, 2, 4]) randDur = 1 pitch = aArray[j] MyMIDI.addNote(0,0,pitch,time,randDur,100) time += randDur name = str(count) + ".mid" print(name) binfile = open(name, 'wb') MyMIDI.writeFile(binfile) binfile.close() print("finishing producing midi") return (name)
def __init__(self, partition, tempo): # Définition des paramètres MIDI. piste = 0 temps = 0 self.sortieMidi = MIDIFile(1) # Nom de la piste. self.sortieMidi.addTrackName(piste, temps, TITRE) # Tempo. self.sortieMidi.addTempo(piste, temps, tempo) # Instrument (74 : flûte). self.sortieMidi.addProgramChange(piste, 0, temps, 74) # À partir des propriétés de la note, création des évènements # MIDI. for neume in partition.musique: for note in (notes for notes in neume if type(notes) == Note): channel = 0 pitch = note.hauteur + partition.transposition duree = note.duree volume = 127 self.sortieMidi.addNote(piste, channel, pitch, temps, duree, volume) temps += duree
def midiSing(sheet, instruments, key, ticktime, filename): offset = NOTES.index(key) + 60 # Middle C is MIDI note #60 midi=MIDIFile(len(sheet)) replaceprint('Creating midi...') for t in range(0,len(sheet)): midi.addTrackName(t, 0, "Track %s"%t) midi.addTempo(t, 0, 60000/(ticktime)) sheet[t]=sheet[t][1:]+[(sheet[t][0],0)] tracklen=len(sheet[t]) for n in range(0,tracklen-1): time, note = sheet[t][n] duration = sheet[t][(n+1)%tracklen][0]-time midi.addNote(t,0,offset+note,time,duration,100)#MyMIDI.addNote(track,channel,pitch,time,duration,volume) replaceprint('Writing to file...') binfile = open(filename+".mid", 'wb') midi.writeFile(binfile) binfile.close() replaceprint('Synth complete!') print("\nMID output to: \"" + filename+ ".mid\"")
def midi(self, path, bpm=240, channel=9, pitch=75, velocity=100): from midiutil.MidiFile3 import MIDIFile track = 0 time = 0 mf = MIDIFile(1) mf.addTrackName(track, time, "Sample Track") mf.addTempo(track, time, bpm) for duration in self.durations: mf.addNote(track, channel, pitch, time, duration, velocity) time += duration # write it to disk with open(path, 'wb') as midi_file: mf.writeFile(midi_file)
class MIDInotes(object): def __init__(self, tempo=120, outfile='midi.mid'): self.tempo = tempo self.outfile = outfile self.tracks = [] def add_track(self, note_list): self.tracks.append(note_list) def add_note(self, track, channel, note): time = note[0] pitch = note[1] velocity = note[2] duration = note[3] # Now add the note. self.MIDIFile.addNote(track, channel, pitch, time, duration, velocity) def save_midi(self, instrument): # Create the MIDIFile Object with 1 track self.MIDIFile = MIDIFile(len(self.tracks)) #self.MIDIFile.addProgramChange(0, 0, 0, instrument) for i, note_list in enumerate(self.tracks): # Tracks are numbered from zero. Times are measured in beats. track = i time = 0 # Add track name, tempo and instrument change event self.MIDIFile.addTrackName(track, time, "Track %s" % i) self.MIDIFile.addTempo(track, time, self.tempo) self.MIDIFile.addProgramChange(track, 0, time, instrument) for n in note_list: if len(n) == 2: note = n[0] channel = n[1] else: note = n channel = 0 self.add_note(track, channel, note) # And write it to disk. with open(self.outfile, 'wb') as binfile: self.MIDIFile.writeFile(binfile)
class MidiWrite(OutputRajapinta): def __init__(self, max_tracks, filename): self.filename = filename self.midi = MIDIFile(max_tracks) def nuotti(self, track, channel, pitch, time, duration, volume): self.midi.addNote(track, channel, pitch, time, duration, volume) def tempo(self, track, time, tempo): self.midi.addTempo(track, time, tempo) def soitin(self, track, channel, time, program): self.midi.addProgramChange(track, channel, time, program) def kirjoita(self): file = open(self.filename, "wb") self.midi.writeFile(file) file.close()
def save_midi(offset, iois, pitches, path, click_track=False, click_track_phase=0, accent_downbeat=False, period=0, pulse=1, bpm=240, channel=0, velocity=100): ''' bpm means grid-units per second in this case ''' track = 0 mf = MIDIFile(1) mf.addTrackName(track, 0, "Sample Track") mf.addTempo(track, 0, bpm) if click_track: duration = offset + sum(iois) write_click_track(mf, duration, phase=click_track_phase, period=period, pulse=pulse, velocity=velocity, accent_downbeat=accent_downbeat) write_notes(mf, offset, iois, pitches, track=track, channel=channel, velocity=velocity) with open(path, 'wb') as midi_file: mf.writeFile(midi_file)
def __init__(self, config_file_name): """Convert data in a CSV file to a MIDI file.""" # load instrument names and MIDI note ranges from config file self._config = configparser.ConfigParser() self._config.read(config_file_name) # Create the MIDIFile object with 1 track self._midi = MIDIFile(1) # Add track name and tempo. self._midi.addTrackName( 0, # track number 0, # time self._config.get('midi', 'track name', fallback=DEFAULT_TRACK_NAME)) self._midi.addTempo( 0, # track number 0, # time int(self._config.get('midi', 'tempo', fallback=DEFAULT_TEMPO)))
class MidiPlay(OutputRajapinta): def __init__(self, max_tracks, filename): self.midi = MIDIFile(max_tracks) def nuotti(self, track, channel, pitch, time, duration, volume): self.midi.addNote(track, channel, pitch, time, duration, volume) def tempo(self, track, time, tempo): self.midi.addTempo(track, time, tempo) def soitin(self, track, channel, time, program): self.midi.addProgramChange(track, channel, time, program) def kirjoita(self, file=None): import sys import io # Systeemialustan mukainen miditiedoston soittamiseen käytetty tiedosto if sys.platform == "win32": from mplaymaster.win32midi import midiDevice elif sys.platform == "darwin": from mplaymaster.darwinmidi import midiDevice else: raise ImportError( "Sori, soitto ei tue muuta kuin Windowsia ja Mac OS X:ää :(") # Haetaan soittolaite tiedoston alussa importatusta kirjastosta laite = midiDevice() # Luodaan BytesIO-objekti, joka toimii kuin bytes-tilassa avattu tiedosto tiedosto = file if file is not None else io.BytesIO() self.midi.writeFile(tiedosto) tiedosto.seek(0) # Ja kutsutaan laitteen soittofunktiota omassa säikeessään. Tämä tehdään siksi, että # ohjelma pääsee jatkamaan suoritustaan musiikin soidessa taustalla. laite.play(tiedosto)
def construct_midi(filename, bpm, trackname, beat_intervals): # Create a MIDI with one track MyMIDI = MIDIFile(1) track = 0 time = 0 MyMIDI.addTrackName(track, time, trackname) MyMIDI.addTempo(track, time, bpm) TIME_COUNTER = 0 for beat_interval in beat_intervals: acappella_measure = chord_from_beat(beat_interval) TIME_COUNTER = _add_measure( acappella_measure, TIME_COUNTER, MyMIDI ) binfile = open("../output/" + filename, 'wb') MyMIDI.writeFile(binfile) binfile.close()
#Import the library from midiutil.MidiFile3 import MIDIFile import random # Create the MIDIFile Object MyMIDI = MIDIFile(6) # Add track name and tempo. The first argument to addTrackName and # addTempo is the time to write the event. track = 0 time = 0 channel = 0 program = 0 volume = 100 #Adding instruments MyMIDI.addTrackName(track,time,"Mellow Ambience I") MyMIDI.addTempo(track,time,92) MyMIDI.addProgramChange(track,channel,time,program+53) MyMIDI.addTrackName(track+1,time,"Harpsichord") MyMIDI.addTempo(track+1,time, 92) MyMIDI.addProgramChange(track+1,channel+1,time,program+107) MyMIDI.addTrackName(track+2,time,"Percussion") MyMIDI.addTempo(track+2,time, 92) MyMIDI.addProgramChange(track+2,channel+2,time,program+47) #Saving Randomness state random.seed(1337,2) # Get pitch of note
def save_midi_file(self): if len(self.messages_captured) == 0: return my_midi = MIDIFile(2) track = 0 my_midi.addTrackName(track, 0, "Tempo track") my_midi.addTempo(track, 0, self.bpm) track += 1 my_midi.addTrackName(track, 0, "Song track") total_time = 0 midi_messages_on = [] midi_messages_off = [] midi_messages_controller = [] for message in self.messages_captured: if len(message) != 3: self.write_message("wrong length: skipping " + str(message)) continue total_time += message.time_stamp # seconds -> beat conversion total_time_adjusted = total_time * float(self.bpm) / float(60) if message.type == MidiEventTypes.NOTE_ON: midi_messages_on.append( {'note': message[1], 'velocity': message[2], 'time': total_time_adjusted, 'channel': message.channel}) elif message.type == MidiEventTypes.NOTE_OFF: midi_messages_off.append( {'note': message[1], 'velocity': message[2], 'time': total_time_adjusted, 'channel': message.channel}) elif message.type == MidiEventTypes.CONTROL_CHANGE: midi_messages_controller.append( {'type': message[1], 'value': message[2], 'time': total_time_adjusted, 'channel': message.channel}) else: self.write_message("unknown message: skipping " + str(message)) continue for m_on in midi_messages_on: for m_off in midi_messages_off: if m_off['note'] == m_on['note'] and m_off['time'] > m_on['time']: m_on['duration'] = m_off['time'] - m_on['time'] m_off['note'] = -1 break else: m_on['duration'] = float( 15) * float(self.bpm) / float(60) # suspended for m in midi_messages_on: my_midi.addNote( track, m['channel'], m['note'], m['time'], m['duration'], m['velocity']) for m in midi_messages_controller: my_midi.addControllerEvent( track, m['channel'], m['time'], m['type'], m['value']) file_name = self.midi_file_name.format( datetime.datetime.now().strftime("%Y%m%d-%H%M%S")) file_path = os.path.join(os.path.dirname(sys.argv[0]), file_name) self.write_message("Saving {0} MIDI messages to {1}...".format( len(self.messages_captured), file_name)) binfile = open(file_path, 'wb') my_midi.writeFile(binfile) binfile.close() self.messages_captured = [] self.write_message("Saved.")
def __init__(self, max_tracks, filename): self.midi = MIDIFile(max_tracks)
# indexes to elements of data row windDirection = 7 windSpeed = 6 precipitation = 1 yearColumn = 4 weatherColumn = 8 weatherYear = "1950" stormCenter = 1 pitch = 60 highTempAdjustment = 30 lowTempAdjustment = 30 # Create the MIDIFile Object with 3 tracks plus names of tracks MyMIDI = MIDIFile(3) MyMIDI.addTrackName(track1, time, "Year Changes") time = time + 1 MyMIDI.addTrackName(track2, time, "Percussion") time = time + 1 #MyMIDI.addTrackName(track3,time,"Misc") #time = time +1 MyMIDI.addTempo(track1, time, beats) time = time + 1 MyMIDI.addTempo(track2, time, beats) #time = time +1 #MyMIDI.addTempo(track3,time, beats) # set voice (sound) to be played on tracks # we used General Midi sounds ( see General Midi docs ) time = time + 1
############################################################################ # A sample program to create a single-track MIDI file, add a note, # and write to disk. ############################################################################ #Import the library from midiutil.MidiFile3 import MIDIFile import csv f = open("airports.dat") for row in csv.reader(f): print(row[1]) # Create the MIDIFile Object MyMIDI = MIDIFile(1) # Add track name and tempo. The first argument to addTrackName and # addTempo is the time to write the event. track = 0 time = 0 MyMIDI.addTrackName(track, time, "Sample Track") MyMIDI.addTempo(track, time, 120) # Add a note. addNote expects the following information: channel = 0 pitch = 60 duration = 1 volume = 100 # Now add the note. MyMIDI.addNote(track, channel, pitch, time, duration, volume)
def __init__(self, file, pitch, length, beat_duration): self.pitch = pitch self.length = abs(length) self.file = file self.midi_melody = MIDIFile(1) self.beat_duration = abs(beat_duration)
beats_per_minute = 756 else: filename = 'mattys-tune.txt' beats_per_minute = 756 with open(filename) as f: for line in f: if len(line) > 3 and (line[1] == '|' or line[2] == '|'): line = line.replace('\n', '') lines.append(line) assert len(lines) % 6 == 0 # Initialize the MIDIFile object (with 1 track) time = 0 duration = 10 volume = 100 song = MIDIFile(1) song.addTrackName(0, time, "pianized_guitar_tab.") song.addTempo(0, time, beats_per_minute) # The root-pitches of the guitar guitar = list(reversed([52, 57, 62, 67, 71, 76])) # Assume EADGBe tuning def add_note(string, fret): song.addNote(0, string, guitar[string] + fret, time, duration, volume) # Process the entire tab for current in range(0, len(lines), 6): # The current base string for i in range(len(lines[current])): # The position in the current string time += 1 for s in range(6): # The number of the string c = lines[current + s][i] try: next_char = lines[current + s][i + 1]
track3 = 2 time = 0 beats = 480 # indexes to elements of data row windDirection = 7 windSpeed = 6 precipitation = 1 highTempAdjustment = 30 lowTempAdjustment = 30 # Create the MIDIFile Object with 3 tracks plus names of tracks MyMIDI = MIDIFile(3) MyMIDI.addTrackName(track1,time,"Temperature MusicHI") time = time +1 MyMIDI.addTrackName(track2,time,"Temperature MusicLOW") time = time +1 MyMIDI.addTrackName(track3,time,"Temperature MusicPrecip") time = time +1 MyMIDI.addTempo(track1,time, beats) time = time +1 MyMIDI.addTempo(track2,time, beats) time = time +1 MyMIDI.addTempo(track3,time, beats) # set voice (sound) to be played on tracks # we used General Midi sounds ( see General Midi docs ) time = time +1
for r in staff_boxes: r.draw(img, (0, 0, 255), 2) for r in sharp_recs: r.draw(img, (0, 0, 255), 2) flat_recs_img = img.copy() for r in flat_recs: r.draw(img, (0, 0, 255), 2) cv2.imwrite('res.png', img) for note_group in note_groups: print([ note.note + " " + note.sym for note in note_group]) midi = MIDIFile(1) track = 0 time = 0 channel = 0 volume = 100 midi.addTrackName(track, time, "Track") midi.addTempo(track, time, 140) for note_group in note_groups: duration = None for note in note_group: note_type = note.sym if note_type == "1": duration = 4
class MIDITime(object): def __init__(self, tempo=120, outfile='miditime.mid', seconds_per_year=5, base_octave=5, octave_range=1, custom_epoch=None): self.tempo = tempo self.outfile = outfile self.tracks = [] if custom_epoch: # Only necessary if you have data that starts before 1970 and DO NOT want to start your midi at the first sound. self.epoch = custom_epoch else: self.epoch = datetime.datetime(1970, 1, 1) self.seconds_per_year = seconds_per_year self.base_octave = base_octave self.octave_range = octave_range self.note_chart = [["C"], ["C#", "Db"], ["D"], ["D#", "Eb"], ["E"], ["F"], ["F#", "Gb"], ["G"], ["G#", "Ab"], ["A"], ["A#", "Bb"], ["B"]] def beat(self, numdays): beats_per_second = self.tempo / 60.0 beats_per_datayear = self.seconds_per_year * beats_per_second beats_per_dataday = beats_per_datayear / 365.25 return round(beats_per_dataday * numdays, 2) def check_tz(self, input): if input.tzinfo: return input.tzinfo else: return None # Match the compare date to the timezone of whatever your input date is, if the input datetime is timezone-aware def normalize_datetime(self, input, compare_date): # if input is date, make epoch a date if type(input) is datetime.date: return compare_date.date() # # First, coerce to datetime in case it's a date # if type(input) is datetime.date: # input = datetime.datetime.combine(input, datetime.datetime.min.time()) # If tz data present, make epoch tz-aware tz = self.check_tz(input) if tz: return tz.localize(compare_date) else: return compare_date def days_since_epoch(self, input): normalized_epoch = self.normalize_datetime(input, self.epoch) return (input - normalized_epoch).total_seconds() / 60 / 60 / 24 # How many days, with fractions def map_week_to_day(self, year, week_num, desired_day_num=None): ''' Helper for weekly data, so when you jump to a new year you don't have notes playing too close together. Basically returns the first Sunday, Monday, etc. in 0-indexed integer format that is in that week. Usage: Once without a desired_day_num, then feed it a day_num in the loop Example: first_day = self.map_week_to_day(filtered_data[0]['Year'], filtered_data[0]['Week']) for r in filtered_data: # Convert the week to a date in that week week_start_date = self.map_week_to_day(r['Year'], r['Week'], first_day.weekday()) # To get your date into an integer format, convert that date into the number of days since Jan. 1, 1970 days_since_epoch = self.mymidi.days_since_epoch(week_start_date) ''' year_start = datetime.datetime(int(year), 1, 1).date() year_start_day = year_start.weekday() week_start_date = year_start + datetime.timedelta(weeks=1 * (int(week_num) - 1)) week_start_day = week_start_date.weekday() if desired_day_num and week_start_day < desired_day_num: return week_start_date + datetime.timedelta(days=(desired_day_num - week_start_day)) return week_start_date def get_data_range(self, data_list, attribute_name, ignore_nulls=True): data_list = list(data_list) # If the data is still a CSV object, once you loop through it you'll get rewind issues. So coercing to list. if ignore_nulls: minimum = min([float(d[attribute_name]) for d in data_list if d[attribute_name]]) maximum = max([float(d[attribute_name]) for d in data_list if d[attribute_name]]) else: minimum = min([float(d[attribute_name]) for d in data_list]) maximum = max([float(d[attribute_name]) for d in data_list]) return [minimum, maximum] def scale_to_note_classic(self, scale_pct, mode): # Only works in multi-octave mode if in C Major (i.e. all the notes are used. Should not be used in other keys, unless octave range is 1.) full_mode = [] n = 0 while n < self.octave_range: for m in mode: current_octave = str(self.base_octave + (n * 1)) full_mode.append(m + current_octave) n += 1 index = int(scale_pct * float(len(full_mode))) if index >= len(full_mode): index = len(full_mode) - 1 print(full_mode[index]) return full_mode[index] def scale_to_note(self, scale_pct, mode): # Manually go through notes so it doesn't inaccurately jump an octave sometimes. # First, write out a list of the possible notes for your octave range (i.e. all of the notes on the keyboard) full_c_haystack = [] n = 0 while n < self.octave_range: for note_group in self.note_chart: out_group = [] for note in note_group: current_octave = self.base_octave + (n * 1) out_group.append(note + str(current_octave)) full_c_haystack.append(out_group) n += 1 full_mode = [] n = 0 while n < self.octave_range: for note in mode: note_found = False note_key = None for groupkey, group in enumerate(full_c_haystack): for gnote in group: if gnote[:-1] == note: full_mode.append(gnote) note_found = True note_key = groupkey if note_found: break full_c_haystack = full_c_haystack[note_key:] n += 1 # Now run through your specified mode and pick the exact notes in those octaves index = int(scale_pct * float(len(full_mode))) if index >= len(full_mode): index = len(full_mode) - 1 return full_mode[index] def note_to_midi_pitch(self, notename): midinum = 0 letter = notename[:-1] octave = notename[-1] i = 0 for note in self.note_chart: for form in note: if letter == form: midinum = i break i += 1 midinum += (int(octave)) * 12 return midinum def linear_scale_pct(self, domain_min, domain_max, input, reverse=False): domain_range = float(domain_max) - float(domain_min) domain_pct = (input - domain_min) / domain_range if reverse: domain_pct = 1 - domain_pct return domain_pct def log_scale_pct(self, domain_min, domain_max, input, reverse=False, direction='exponential'): if direction == 'exponential': # E.G. earthquakes min_log_domain = pow(10, domain_min) max_log_domain = pow(10, domain_max) domain_range = max_log_domain - min_log_domain log_input = pow(10, input) elif direction == 'log': # natural log scale if domain_min > 0: min_log_domain = log(domain_min) else: min_log_domain = log(0.1) # Technically this is not a true log scale. Someone smarter than me will have to figure this out. if domain_max > 0: max_log_domain = log(domain_max) else: max_log_domain = log(0.1) # Technically this is not a true log scale. Someone smarter than me will have to figure this out. domain_range = max_log_domain - min_log_domain log_input = log(input) domain_pct = (log_input - min_log_domain) / domain_range if reverse: domain_pct = 1 - domain_pct return domain_pct def scale(self, range_min, range_max, input_pct): scale_range = range_max - range_min return range_min + (input_pct * scale_range) def add_track(self, note_list): self.tracks.append(note_list) def add_note(self, track, channel, note): time = note[0] pitch = note[1] volume = note[2] duration = note[3] print(pitch, time, duration, volume) # Now add the note. self.MIDIFile.addNote(track, channel, pitch, time, duration, volume) def save_midi(self): # Create the MIDIFile Object with 1 track self.MIDIFile = MIDIFile(len(self.tracks)) for i, note_list in enumerate(self.tracks): # Tracks are numbered from zero. Times are measured in beats. track = i time = 0 # Add track name and tempo. self.MIDIFile.addTrackName(track, time, "Track 1") self.MIDIFile.addTempo(track, time, self.tempo) for n in note_list: if len(n) == 2: note = n[0] channel = n[1] else: note = n channel = 0 self.add_note(track, channel, note) # And write it to disk. binfile = open(self.outfile, 'wb') self.MIDIFile.writeFile(binfile) binfile.close()
class MIDITime(object): def __init__(self, tempo=120, outfile='miditime.mid', seconds_per_year=5, base_octave=5, octave_range=1, custom_epoch=None): self.tempo = tempo self.outfile = outfile self.tracks = [] if custom_epoch: # Only necessary if you have data that starts before 1970 and DO NOT want to start your midi at the first sound. self.epoch = custom_epoch else: self.epoch = datetime.datetime(1970, 1, 1) self.seconds_per_year = seconds_per_year self.base_octave = base_octave self.octave_range = octave_range self.note_chart = [["C"], ["C#", "Db"], ["D"], ["D#", "Eb"], ["E"], ["F"], ["F#", "Gb"], ["G"], ["G#", "Ab"], ["A"], ["A#", "Bb"], ["B"]] def beat(self, numdays): beats_per_second = self.tempo / 60.0 beats_per_datayear = self.seconds_per_year * beats_per_second beats_per_dataday = beats_per_datayear / 365.25 return round(beats_per_dataday * numdays, 2) def check_tz(self, input): if input.tzinfo: return input.tzinfo else: return None # Match the compare date to the timezone of whatever your input date is, if the input datetime is timezone-aware def normalize_datetime(self, input, compare_date): # if input is date, make epoch a date if type(input) is datetime.date: return compare_date.date() # # First, coerce to datetime in case it's a date # if type(input) is datetime.date: # input = datetime.datetime.combine(input, datetime.datetime.min.time()) # If tz data present, make epoch tz-aware tz = self.check_tz(input) if tz: return tz.localize(compare_date) else: return compare_date def days_since_epoch(self, input): normalized_epoch = self.normalize_datetime(input, self.epoch) return (input - normalized_epoch).total_seconds( ) / 60 / 60 / 24 # How many days, with fractions def map_week_to_day(self, year, week_num, desired_day_num=None): ''' Helper for weekly data, so when you jump to a new year you don't have notes playing too close together. Basically returns the first Sunday, Monday, etc. in 0-indexed integer format that is in that week. Usage: Once without a desired_day_num, then feed it a day_num in the loop Example: first_day = self.map_week_to_day(filtered_data[0]['Year'], filtered_data[0]['Week']) for r in filtered_data: # Convert the week to a date in that week week_start_date = self.map_week_to_day(r['Year'], r['Week'], first_day.weekday()) # To get your date into an integer format, convert that date into the number of days since Jan. 1, 1970 days_since_epoch = self.mymidi.days_since_epoch(week_start_date) ''' year_start = datetime.datetime(int(year), 1, 1).date() year_start_day = year_start.weekday() week_start_date = year_start + datetime.timedelta(weeks=1 * (int(week_num) - 1)) week_start_day = week_start_date.weekday() if desired_day_num and week_start_day < desired_day_num: return week_start_date + datetime.timedelta(days=(desired_day_num - week_start_day)) return week_start_date def get_data_range(self, data_list, attribute_name, ignore_nulls=True): data_list = list( data_list ) # If the data is still a CSV object, once you loop through it you'll get rewind issues. So coercing to list. if ignore_nulls: minimum = min([ float(d[attribute_name]) for d in data_list if d[attribute_name] ]) maximum = max([ float(d[attribute_name]) for d in data_list if d[attribute_name] ]) else: minimum = min([float(d[attribute_name]) for d in data_list]) maximum = max([float(d[attribute_name]) for d in data_list]) return [minimum, maximum] def scale_to_note_classic( self, scale_pct, mode ): # Only works in multi-octave mode if in C Major (i.e. all the notes are used. Should not be used in other keys, unless octave range is 1.) full_mode = [] n = 0 while n < self.octave_range: for m in mode: current_octave = str(self.base_octave + (n * 1)) full_mode.append(m + current_octave) n += 1 index = int(scale_pct * float(len(full_mode))) if index >= len(full_mode): index = len(full_mode) - 1 print(full_mode[index]) return full_mode[index] def scale_to_note( self, scale_pct, mode ): # Manually go through notes so it doesn't inaccurately jump an octave sometimes. # First, write out a list of the possible notes for your octave range (i.e. all of the notes on the keyboard) full_c_haystack = [] n = 0 while n < self.octave_range: for note_group in self.note_chart: out_group = [] for note in note_group: current_octave = self.base_octave + (n * 1) out_group.append(note + str(current_octave)) full_c_haystack.append(out_group) n += 1 full_mode = [] n = 0 while n < self.octave_range: for note in mode: note_found = False note_key = None for groupkey, group in enumerate(full_c_haystack): for gnote in group: if gnote[:-1] == note: full_mode.append(gnote) note_found = True note_key = groupkey if note_found: break full_c_haystack = full_c_haystack[note_key:] n += 1 # Now run through your specified mode and pick the exact notes in those octaves index = int(scale_pct * float(len(full_mode))) if index >= len(full_mode): index = len(full_mode) - 1 return full_mode[index] def note_to_midi_pitch(self, notename): midinum = 0 letter = notename[:-1] octave = notename[-1] i = 0 for note in self.note_chart: for form in note: if letter == form: midinum = i break i += 1 midinum += (int(octave)) * 12 return midinum def linear_scale_pct(self, domain_min, domain_max, input, reverse=False): domain_range = float(domain_max) - float(domain_min) domain_pct = (input - domain_min) / domain_range if reverse: domain_pct = 1 - domain_pct return domain_pct def log_scale_pct(self, domain_min, domain_max, input, reverse=False, direction='exponential'): if direction == 'exponential': # E.G. earthquakes min_log_domain = pow(10, domain_min) max_log_domain = pow(10, domain_max) domain_range = max_log_domain - min_log_domain log_input = pow(10, input) elif direction == 'log': # natural log scale if domain_min > 0: min_log_domain = log(domain_min) else: min_log_domain = log( 0.1 ) # Technically this is not a true log scale. Someone smarter than me will have to figure this out. if domain_max > 0: max_log_domain = log(domain_max) else: max_log_domain = log( 0.1 ) # Technically this is not a true log scale. Someone smarter than me will have to figure this out. domain_range = max_log_domain - min_log_domain log_input = log(input) domain_pct = (log_input - min_log_domain) / domain_range if reverse: domain_pct = 1 - domain_pct return domain_pct def scale(self, range_min, range_max, input_pct): scale_range = range_max - range_min return range_min + (input_pct * scale_range) def add_track(self, note_list): self.tracks.append(note_list) def add_note(self, track, channel, note): time = note[0] pitch = note[1] volume = note[2] duration = note[3] print(pitch, time, duration, volume) # Now add the note. self.MIDIFile.addNote(track, channel, pitch, time, duration, volume) def save_midi(self): # Create the MIDIFile Object with 1 track self.MIDIFile = MIDIFile(len(self.tracks)) for i, note_list in enumerate(self.tracks): # Tracks are numbered from zero. Times are measured in beats. track = i time = 0 # Add track name and tempo. self.MIDIFile.addTrackName(track, time, "Track 1") self.MIDIFile.addTempo(track, time, self.tempo) for n in note_list: if len(n) == 2: note = n[0] channel = n[1] else: note = n channel = 0 self.add_note(track, channel, note) # And write it to disk. binfile = open(self.outfile, 'wb') self.MIDIFile.writeFile(binfile) binfile.close()
while free_space > 0: note_duration = random.choice(note_durations) if(note_duration <= free_space): free_space -= note_duration durations.append(note_duration) print(durations) return durations #broken akkords, bass akkords?[special pitch], power akkords etc.. #--------------------------------------------------------------- # Start the Track #--------------------------------------------------------------- # Create the MIDIFile Object with 1 track MyMIDI = MIDIFile(1) # Add track name and tempo. track = 0 time = 0 track_length = 32 # 2^n zahl! MyMIDI.addTrackName(track,time,"Sample Track") MyMIDI.addTempo(track,time,120) #create carakteristic gauss distributions----------------- #natürliche verteilung (gaussverteilung) für oktave mu, sigma = 0, 1 # mean and standard deviation gaussDistrib = np.random.normal(mu, sigma, 1000) duodecimum = np.floor(gaussDistrib+0.5)*12
############################################################################ # A sample program to create a single-track MIDI file, add a note, # and write to disk. ############################################################################ #Import the library from midiutil.MidiFile3 import MIDIFile MyMIDI = MIDIFile(1) # Add track name and tempo. The first argument to addTrackName and # addTempo is the time to write the event. track = 0 time = 0 channel = 9 MyMIDI.addTrackName(track,time,"Sample Track") MyMIDI.addTempo(track,time, 120) time = time = 2 MyMIDI.addProgramChange(0,channel, time, 44) # voice 1 = 86 fretless bass # Add a note. addNote expects the following information: pitch = 60 duration = 10 volume = 100 duration2 = 5 # Now add the note. MyMIDI.addNote(track,channel,pitch,time,duration,volume) time = time + 1
class MidiCreator(object): def __init__(self, config_file_name): """Convert data in a CSV file to a MIDI file.""" # load instrument names and MIDI note ranges from config file self._config = configparser.ConfigParser() self._config.read(config_file_name) # Create the MIDIFile object with 1 track self._midi = MIDIFile(1) # Add track name and tempo. self._midi.addTrackName( 0, # track number 0, # time self._config.get('midi', 'track name', fallback=DEFAULT_TRACK_NAME)) self._midi.addTempo( 0, # track number 0, # time int(self._config.get('midi', 'tempo', fallback=DEFAULT_TEMPO))) def _init_note_makers(self, csv_file_name): """Create a list of NoteMaker objects, one for each csv column that is mapped to an instrument by the config file.""" self._note_makers = [] csv_file = open(csv_file_name) # first line of csv file must have column names columns = csv_file.readline().strip().split(',') # Create one notemaker instance for every csv column that we'll be # using to produce music. Each notemaker is assigned to a separate # channel, since each instrument must have its own channel. channel = 0 for column_index, column_name in enumerate(columns): if NoteMaker.is_musical_column(column_name, self._config): if self._config['columns'][column_name]: self._note_makers.append( NoteMaker(column_name, column_index, self._midi, self._config, channel)) channel += 1 if channel > 15: print("Warning: more than 16 channels, ignoring excess.") break # Now each notemaker object needs to know the maximum value for its # data column, so that it can be scaled to fit the range of the # instrument assigned to that column. for line in csv_file: split_line = line.strip().split(',') for note_maker in self._note_makers: note_maker.test_for_max(split_line) def write_midi_file(self, csv_file_name, midi_file_name): # Create a list of pitchmaker objects, one for each column that will be # used to produce music. self._init_note_makers(csv_file_name) # Write notes to the midi file csv_file = open(csv_file_name) csv_file.readline() # skip header for line in csv_file: split_line = line.strip().split(',') for note_maker in self._note_makers: note_maker.add_note(split_line) # write the midi data to disk with open(midi_file_name, 'wb') as midi_file: self._midi.writeFile(midi_file)
time = 0 beats = 1250cd cd # indexes to elements of data row windDirection = 7 windSpeed = 6 precipitation = 1 moonphase = 8 highTempAdjustment = 30 lowTempAdjustment = 30 # Create the MIDIFile Object with 3 tracks plus names of tracks MyMIDI = MIDIFile(3) MyMIDI.addTrackName(track1,time,"Temperature MusicHI") time = time +1 MyMIDI.addTrackName(track2,time,"Temperature MusicLOW") time = time +1 MyMIDI.addTrackName(track3,time,"Temperature MusicPrecip") time = time +1 MyMIDI.addTempo(track1,time, beats) time = time +1 MyMIDI.addTempo(track2,time, beats) time = time +1 MyMIDI.addTempo(track3,time, beats) # set voice (sound) to be played on tracks # we used General Midi sounds ( see General Midi docs ) time = time +1
from pythagorean_tuning import * my_root = 40 pyts = IntervalScale(7,12,my_root) scale2 = IntervalScale(7,7,my_root) scale3 = IntervalScale(1,12,my_root) scale4 = MinorScale(my_root) scale5 = MajorPentatonicScale(my_root) scale6 = MajorScale(my_root) #use_scale = pyts #use_scale = PythagoreanScale(my_root) use_scale = scale5 #use_scale = pythagorean_tuning.MinorScale(my_root) from midiutil.MidiFile3 import MIDIFile mf1 = MIDIFile(1) from my_midi_utils import GetTicksPerBeat ticks_per_beat = GetTicksPerBeat(mf1) duration = 0.25 velocity=100 #fname = 'A000010 - euler_phi.txt' #pathname = 'source_txt/' ##fname = '3x plus 1 - A006577.txt' ##fname = 'Karl Aage Rasmussen - build up - ascending - A056239.txt' ##fname = 'fractal - A025480 - other_version.txt' #import numpy as np #seq = np.loadtxt(pathname+fname,dtype=np.int) #
from midiutil.MidiFile3 import MIDIFile MyMIDI = MIDIFile(1) track = 0 time = 0 MyMIDI.addTrackName(track, time, "Sample Track") MyMIDI.addTempo(track, time, 120) track = 0 channel = 0 pitch = 60 time = 4 duration = 1 volume = 100 MyMIDI.addNote(track,channel,pitch,time,duration,volume) track = 0 channel = 1 pitch = 64 time = 8 duration = 1 volume = 100 MyMIDI.addNote(track,channel,pitch,time,duration,volume) track = 0 channel = 2 pitch = 67 time = 12 duration = 1
############################################################################ # A sample program to create a single-track MIDI file, add a note, # and write to disk. ############################################################################ #Import the library from midiutil.MidiFile3 import MIDIFile import csv track1 = 0 track2 = 1 time = 0 MyMIDI = MIDIFile(2) MyMIDI.addTrackName(track1,time,"Temperature MusicHI") time = time +1 MyMIDI.addTrackName(track2,time,"Temperature MusicLOW") time = time +1 MyMIDI.addTempo(track1,time, 540) time = time +1 MyMIDI.addTempo(track2,time, 540) time = time +1 MyMIDI.addProgramChange(track1,0, time, 1) time = time +1 MyMIDI.addProgramChange(track2,1, time, 2) time = time +1 #f = open("climate2010.txt")
from midiutil.MidiFile3 import MIDIFile midi = MIDIFile(1) track = 0 time = 0 midi.addTrackName(track, time, 'sample') midi.addTempo(track, time, 120) track = 0 channel = 0 pitch = 60 time = 10 duration = 1 volume = 100 # Now add the note. midi.addNote(track, channel, pitch, time, duration, volume) # And write it to disk. binfile = open("output.mid", 'wb') midi.writeFile(binfile) binfile.close()
from pythagorean_tuning import * my_root = 30 pyts = IntervalScale(7, 12, my_root) scale2 = IntervalScale(7, 7, my_root) scale3 = IntervalScale(1, 12, my_root) scale4 = MinorScale(my_root) scale5 = MajorPentatonicScale(my_root) scale6 = MajorScale(my_root) #use_scale = pyts #use_scale = PythagoreanScale(my_root) use_scale = scale5 #use_scale = pythagorean_tuning.MinorScale(my_root) from midiutil.MidiFile3 import MIDIFile mf1 = MIDIFile(1) from my_midi_utils import GetTicksPerBeat ticks_per_beat = GetTicksPerBeat(mf1) duration = 0.25 velocity = 100 #fname = 'A000010 - euler_phi.txt' #pathname = 'source_txt/' ##fname = '3x plus 1 - A006577.txt' ##fname = 'Karl Aage Rasmussen - build up - ascending - A056239.txt' ##fname = 'fractal - A025480 - other_version.txt' #import numpy as np #seq = np.loadtxt(pathname+fname,dtype=np.int) #
# indexes to elements of data row windDirection = 7 windSpeed = 6 precipitation = 1 yearColumn = 4 weatherColumn = 8 weatherYear = "1950" stormCenter = 1 pitch = 60 highTempAdjustment = 30 lowTempAdjustment = 30 # Create the MIDIFile Object with 3 tracks plus names of tracks MyMIDI = MIDIFile(3) MyMIDI.addTrackName(track1,time,"Year Changes") time = time +1 MyMIDI.addTrackName(track2,time,"Percussion") time = time +1 #MyMIDI.addTrackName(track3,time,"Misc") #time = time +1 MyMIDI.addTempo(track1,time, beats) time = time +1 MyMIDI.addTempo(track2,time, beats) #time = time +1 #MyMIDI.addTempo(track3,time, beats) # set voice (sound) to be played on tracks # we used General Midi sounds ( see General Midi docs ) time = time +1
def play(self): ''' Play Method Generates the MIDI tracks necessary to play the composition Plays the composition using pygame module ''' # Create two MIDI tracks midi = MIDIFile(2) # Piano right hand track track = 0 time = 0 midi.addTrackName(track, time, "Piano Right Hand") midi.addTempo(track, time, self.tempo) track = 1 midi.addTrackName(track, time, "Piano Left Hand") midi.addTempo(track, time, self.tempo) while (self.boolean): # Create new progressions as long as self.boolean is True progression = self.progressionf() proglength = len(progression) flag = 0 # If the length of the progression is greater than self.totalbeats, # the composition will last longer than the user-input duration # Therefore, try 10 more times to generate a progression shorter # than self.totalbeats. while self.totalbeats <= proglength: progression = self.progressionf() proglength = len(progression) flag += 1 if flag == 10: break # If the length of the progression is suitable, add it to self.compprog if self.totalbeats >= proglength: self.compprog.extend(progression) # Subtract length of progression from self.totalbeats (so that # self.totalbeats keeps track of number of beats left in the # composition) self.totalbeats -= proglength track = 0 channel = 0 volume = 100 # Create rhythmlist temprlist = self.rhythmgen(progression) rhythmlist = [] for r in temprlist: for el in r: rhythmlist.append(el) # Create melodylist using rhythmlist melodylist = self.melodygen(progression, temprlist, self.scale, 5) rllength = len(rhythmlist) # Add each note to the piano right hand track for n in range(rllength): pitch = melodylist[n] duration = rhythmlist[n] midi.addNote(track, channel, pitch, self.time1, duration, volume) self.time1 += rhythmlist[n] # If program fails to generate a progression shorter than self.totalbeats, # add the tonic to self.compprog and end the composition else: self.compprog.append(self.tonic) self.boolean = False # Piano left hand track track = 1 channel = 0 duration = 0.25 volume = 80 # For every harmony in self.compprog, add the alberti bass line for n in range(len(self.compprog)): a = self.albertibass( harmony(self.compprog[n], self.roots, self.reverse_labels), 4) if n == len(self.compprog) - 1: pitch = a[0] duration = 0.5 midi.addNote(track, channel, pitch, self.time2, duration, volume) else: for iter in range(2): for tone in range(4): pitch = a[tone] midi.addNote(track, channel, pitch, self.time2, duration, volume) self.time2 += 0.25 # Write a midi file file = "composition.mid" with open(file, 'wb') as binfile: midi.writeFile(binfile) # Play the midi file using pygame pygame.init() pygame.mixer.init() pygame.mixer.music.load(file) pygame.mixer.music.play() while pygame.mixer.music.get_busy(): pygame.time.Clock().tick(10)
############################################################################ # A sample program to create a single-track MIDI file, add a note, # and write to disk. ############################################################################ #Import the library from midiutil.MidiFile3 import MIDIFile import random # Create the MIDIFile Object MyMIDI = MIDIFile(2) # Add track name and tempo. The first argument to addTrackName and # addTempo is the time to write the event. track = 0 LHtime = 0 MyMIDI.addTrackName(track, LHtime, "Jazzzzzy Left Hand") MyMIDI.addTempo(track,LHtime, 160) # Creating second track track2 = 1 RHtime = 0 MyMIDI.addTrackName(track2, RHtime, "Jazzzzzy Right Hand") MyMIDI.addTempo(track,RHtime,160) # Add a note 'C'. addNote expects the following information: channel = 0 pitch = 60 duration = 2 volume = 100 # Create Randomness random.seed(1338, 2)
############################################################################ # A sample program to create a single-track MIDI file, add a note, # and write to disk. ############################################################################ #Import the library from midiutil.MidiFile3 import MIDIFile import csv track1 = 0 track2 = 1 time = 0 MyMIDI = MIDIFile(2) MyMIDI.addTrackName(track1, time, "Temperature MusicHI") time = time + 1 MyMIDI.addTrackName(track2, time, "Temperature MusicLOW") time = time + 1 MyMIDI.addTempo(track1, time, 540) time = time + 1 MyMIDI.addTempo(track2, time, 540) time = time + 1 MyMIDI.addProgramChange(track1, 0, time, 1) time = time + 1 MyMIDI.addProgramChange(track2, 1, time, 2) time = time + 1 #f = open("climate2010.txt")
def create_midi_from_progression(progression): """ Given a chord progression in the form of a list of chord instances, creates a MIDI file as an output. """ MyMIDI = MIDIFile(4) track = 0 time = 0 MyMIDI.addTrackName(track, time, "Soprano") MyMIDI.addTempo(track, time, 60) track += 1 MyMIDI.addTrackName(track, time, "Alto") MyMIDI.addTempo(track, time, 60) track += 1 MyMIDI.addTrackName(track, time, "Tenor") MyMIDI.addTempo(track, time, 60) track += 1 MyMIDI.addTrackName(track, time, "Bass") MyMIDI.addTempo(track, time, 60) channel = 0 duration = 1 volume = 100 for index, chord in enumerate(progression): track = 3 for note in chord.get_notes(): pitch = note.get_midi_number() MyMIDI.addNote(track, channel, pitch, time, duration, volume) track -= 1 time += 1 if index == len(progression) - 2: duration = 2 binfile = open("output_individual_voices.mid", 'wb') MyMIDI.writeFile(binfile) binfile.close() MyMIDI = MIDIFile(2) track = 0 time = 0 MyMIDI.addTrackName(track, time, "Upper Voices") MyMIDI.addTempo(track, time, 60) track += 1 MyMIDI.addTrackName(track, time, "Lower Voices") MyMIDI.addTempo(track, time, 60) duration = 1 for index, chord in enumerate(progression): track = 1 count = 0 for note in chord.get_notes(): pitch = note.get_midi_number() MyMIDI.addNote(track, channel, pitch, time, duration, volume) if count % 2 == 1: track -= 1 count += 1 time += 1 if index == len(progression) - 2: duration = 2 binfile = open("output_two_hands.mid", 'wb') MyMIDI.writeFile(binfile) binfile.close()