def compose_midi(filename): key = Notes.ALL.choice() scale = scales.pick_a_scale(key) blues = False if randint(1, 4) == 3: blues = True scale = scales.generate_blues_scale(key) note_range = Range(Note(key, 2), Note(key, 6)) osc = FibonacciOscillator(randint(0, 100)) total_measures = randint(12, 60) filename = filename #'../output.mid' midi_writer = MidiWriter(filename) if randint(1, 12) == 12: tempo = randint(100, 400) midi_writer.write_12tone(total_measures * 4, tempo, osc) elif not blues and randint(0, 2) == 1: note_seq = FibonacciNoteSequence(scale, total_measures * 3, note_range, osc) tempo = randint(50, 200) midi_writer.write_waltz(note_seq, scale, tempo) else: note_seq = FibonacciNoteSequence(scale, total_measures * 4, note_range, osc) tempo = randint(100, 400) midi_writer.write(note_seq, scale, tempo, blues)
def write_waltz(self, note_sequence, scale, tempo): time = 0 self.mf.addTrackName(self.track, time, "Sample Track") self.mf.addTempo(self.track, time, tempo) chord_spacing = 3 next_chord = 0 use_arps = randint(0, 2) prev_note = None for i in range(len(note_sequence.notes)): note = note_sequence.notes[i] self.write_note(note, time) if time >= next_chord: if use_arps == 1: self.write_waltz_arp_from_note(note, scale, next_chord) elif use_arps == 2: self.write_waltz_arp_from_note_with_octave( note, scale, next_chord) elif prev_note is None: self.write_waltz_chord_from_note(note, scale, next_chord) else: self.write_waltz_chord_from_note(prev_note, scale, next_chord) next_chord += chord_spacing time += note.duration prev_note = note self.write_chord_from_note(Note(scale.tonic, prev_note.octave), scale, next_chord, 12) with open(self.filename, 'wb') as outf: self.mf.writeFile(outf)
def get_random_note(self, note_range=None): note_name = self.notes.choice() note = Note(note_name) if note_range is not None: while note < note_range.bottom: note.transpose(12) while note > note_range.top: note.transpose(-12) return note
def write(self, note_sequence, scale, tempo, blues=False): time = 0 self.mf.addTrackName(self.track, time, "Sample Track") self.mf.addTempo(self.track, time, tempo) chord_spacing = 4 if note_sequence.pace == 3: next_chord = 1 elif note_sequence.pace > 0: next_chord = 2 next_chord = 0 rand = randint(0, 2) use_arps = rand == 1 use_rhythm = rand == 2 prev_note = None for i in range(len(note_sequence.notes)): note = note_sequence.notes[i] if blues: self.write_note_blues(note, time) else: self.write_note(note, time) if time >= next_chord: if use_arps: self.write_arp_from_note(note, scale, next_chord, chord_spacing / 4.0) elif prev_note is None: if use_rhythm: self.write_rhythm_from_note(note, scale, next_chord, chord_spacing) else: self.write_chord_from_note(note, scale, next_chord, chord_spacing) else: if use_rhythm: self.write_rhythm_from_note(prev_note, scale, next_chord, chord_spacing) else: self.write_chord_from_note(prev_note, scale, next_chord, chord_spacing) next_chord += chord_spacing time += note.duration prev_note = note self.write_chord_from_note(Note(scale.tonic, prev_note.octave), scale, next_chord, 16) with open(self.filename, 'wb') as outf: self.mf.writeFile(outf)
def get_next_note(self, start_note, steps, direction): steps %= self.notes.size() steps = steps * -1 if not direction else steps index = self.notes.index(start_note.name) + steps note_name = self.notes.get(index) next_note = Note(note_name, start_note.octave) if direction == Oscillator.UP and next_note < start_note: next_note.transpose(12) elif direction == Oscillator.DOWN and next_note > start_note: next_note.transpose(-12) return next_note
def get_midi_pitch(note): delta = note - Note(Notes.C) return 60 + delta
def write_12tone(self, length, tempo, osc): time = 0 self.mf.addTrackName(self.track, time, "Sample Track") self.mf.addTempo(self.track, time, tempo) all_notes = Notes.ALL.list[:] available_notes = all_notes[:] prev_note = None next_bass = 0 pace = randint(0, 3) for i in range(length): index = randint(0, len(available_notes) - 1) note = Note(available_notes[index]) note.duration = self._calc_12tone_duration(i, pace) if prev_note is not None: if osc.direction and note < prev_note: note.transpose(12) elif not osc.direction and note > prev_note: note.transpose(-12) del available_notes[index] if len(available_notes) == 0: available_notes = all_notes[:] if time >= next_bass: bass_note = None bass_length = randint(1, 4) index = randint(0, len(available_notes) - 1) bass_note = Note(available_notes[index]) while bass_note.name == note.name: index = randint(0, len(available_notes) - 1) bass_note = Note(available_notes[index]) while bass_note >= note: bass_note.transpose(-12) bass_note.duration = bass_length del available_notes[index] if len(available_notes) == 0: available_notes = all_notes[:] self.write_note(bass_note, next_bass) next_bass += bass_length if len(available_notes) > 0 and randint(0, 1) == 1: bass_note = None index = randint(0, len(available_notes) - 1) bass_note = Note(available_notes[index]) while bass_note.name == note.name: index = randint(0, len(available_notes) - 1) bass_note = Note(available_notes[index]) while bass_note >= note: bass_note.transpose(-12) bass_note.duration = bass_length del available_notes[index] if len(available_notes) == 0: available_notes = all_notes[:] self.write_note(bass_note, next_bass) self.write_note(note, time) time += note.duration prev_note = note osc.update(i) with open(self.filename, 'wb') as outf: self.mf.writeFile(outf)
def get_dominant_triad(self): note = Note(self.tonic).transposed(7) mediant = note.transposed(4) dominant = note.transposed(7) leading = note.transposed(10) return Chord([self.tonic, mediant.name, dominant.name, leading.name])
from music.sequence import Sequence from music.instrument import SinWave as Instrument BPM = 120 LENGTH_NOTE = 1 / 4 # quarter note NOTE_DURATION = (60 * LENGTH_NOTE) / BPM NOTES = ("C4", "D4", "E4", "F4") SAMPLING_RATE = 44100 # BIT_DEPTH = None # will set when implement wave conversion note_list = Sequence(Tuning) for index, note in enumerate(NOTES): note_object = Note(note) time = NOTE_DURATION * index duration = NOTE_DURATION note_list.add(note_object, time, duration) note_list_render = note_list.render() # TMP WORKAROUND BC Instrument.render isnt implemented #array = np.zeros(SAMPLING_RATE * len(NOTES) * NOTE_DURATION, dtype=np.float64) instrument = Instrument(SAMPLING_RATE) array = np.empty(0, dtype=np.float64)