def test_degree_in_key(self): """ Ensure that it plays in G major. """ from sebastian.core.transforms import degree_in_key from sebastian.core.notes import Key, major_scale h1 = self.make_horizontal_sequence() keyed = h1 | degree_in_key(Key("G", major_scale)) self.assertEqual(keyed._elements, [ { 'degree': 1, 'pitch': -1 }, { 'degree': 2, 'pitch': 1 }, { 'degree': 3, 'pitch': 3 }, { 'degree': 1, 'pitch': -1 }, ])
def test_degree_in_key_with_octave(self): """ Ensure that degree in key with octave is sane """ from sebastian.core.transforms import degree_in_key_with_octave from sebastian.core.notes import Key, major_scale h1 = self.make_horizontal_sequence() keyed = h1 | degree_in_key_with_octave(Key("C", major_scale), 4) self.assertEqual(keyed._elements, [{ 'degree': 1, 'octave': 4, 'pitch': -2 }, { 'degree': 2, 'octave': 4, 'pitch': 0 }, { 'degree': 3, 'octave': 4, 'pitch': 2 }, { 'degree': 1, 'octave': 4, 'pitch': -2 }])
def build_movement(): # Define our alberti bass signature. alberti = arpeggio([0, 2, 1, 2]) # Create the basic interval pattern. intervals = HSeq({DEGREE: x} for x in [1, 5, 1, 4, 1, 5, 1]) # Create the rhythm rhythm = HSeq({DURATION_64: x} for x in [128, 64, 64, 64, 64, 64, 64]) # Set specific chords to be used in certain measures. intervals[1]["chord"] = (-3, -1, 0, 2) # second inversion 7th intervals[3]["chord"] = (-3, 0, 2) # second inversion intervals[5]["chord"] = (-5, -3, 0) # first inversion # Combine the sequences, make them chords, produce alberti on the chords, # fill with each being 8, expand it to a flat sequence. melody = intervals & rhythm | chord() | alberti | fill(8) | expand # Define our key C_major = Key("C", major_scale) #key(major_scale(-2)) # Set the degree, add the midi pitch, make it an OSequence, add debugging information. return melody | degree_in_key_with_octave(C_major, 5) | midi_pitch() | OSequence | debug
def test_play_notes_in_midi_pitches(self): """ Ensure that it plays in G major. """ from sebastian.core.transforms import degree_in_key, midi_pitch, add from sebastian.core.notes import Key, major_scale from sebastian.core import MIDI_PITCH, DURATION_64 h1 = self.make_horizontal_sequence() keyed = h1 | degree_in_key(Key("G", major_scale)) positioned = keyed | add({'octave': 4, DURATION_64: 8}) pitched = positioned | midi_pitch() self.assertEqual(pitched._elements, [ { 'degree': 1, 'pitch': -1, DURATION_64: 8, MIDI_PITCH: 55, 'octave': 4 }, { 'degree': 2, 'pitch': 1, DURATION_64: 8, MIDI_PITCH: 57, 'octave': 4 }, { 'degree': 3, 'pitch': 3, DURATION_64: 8, MIDI_PITCH: 59, 'octave': 4 }, { 'degree': 1, 'pitch': -1, DURATION_64: 8, MIDI_PITCH: 55, 'octave': 4 }, ])
def end(scale): return HSeq(scale[i] for i in [2, 1]) | add(quaver_point) def h2(scale): return HSeq(scale[i] for i in [0, 4, 3, 4]) | add(quaver_point) def h2_end1(scale): return HSeq(scale[i] for i in [0, 4]) | add(quaver_point) # there's two important quarter notes used at the ends of sections e1 = HSeq(scale[3]) | add(quarter_point) e2 = HSeq(scale[0]) | add(quarter_point) partA = h1(scale) + h1_end1(scale) + e1 + h1(scale) + end(scale) + e2 partB = h2(scale) + h2_end1(scale) + e1 + h2(scale) + end(scale) + e2 # here we see the basic structure of the song oseq = OSequence((partA * 2) + (partB * 2)) C_major = Key("C", major_scale) # note values filled-out for C major in octave 5 then MIDI pitches calculated seq = oseq | degree_in_key_with_octave(C_major, 5) | midi_pitch() # write to file: write_midi.write("shortning_bread_1.mid", [seq])
seq4 = seq3 | transpose(12) | reverse() # merge seq5 = seq3 // seq4 # play MIDI player.play([seq5]) # write to MIDI write_midi.write("seq5.mid", [seq5]) # contruct a horizontal sequence of scale degrees seq6 = HSeq(Point(degree=degree) for degree in [1, 2, 3, 2, 1]) # put that sequence into C major, octave 4 quavers C_MAJOR = Key("C", major_scale) seq7 = seq6 | add({"octave": 4, DURATION_64: 8}) | degree_in_key(C_MAJOR) # convert to MIDI pitch and play player.play([OSequence(seq7 | midi_pitch())]) # sequence of first four degree of a scale seq8 = HSeq(Point(degree=n) for n in [1, 2, 3, 4]) # add duration and octave seq8 = seq8 | add({DURATION_64: 16, "octave": 5}) # put into C major seq8 = seq8 | degree_in_key(C_MAJOR) # annotate with lilypond
""" The main title theme to HBO's Game of Thrones by Ramin Djawadi """ from sebastian.core import Point, HSeq, OSequence from sebastian.core.transforms import midi_pitch, degree_in_key, add from sebastian.core.notes import Key, major_scale, minor_scale from sebastian.midi import write_midi C_Major = Key("C", major_scale) C_minor = Key("C", minor_scale) motive_degrees = HSeq(Point(degree=n) for n in [5, 1, 3, 4]) motive_rhythm_1 = HSeq(Point(duration_64=n) for n in [16, 16, 8, 8]) motive_rhythm_2 = HSeq( Point(duration_64=n) for n in [48, 48, 8, 8, 32, 32, 8, 8]) motive_1 = motive_degrees & motive_rhythm_1 motive_2 = (motive_degrees * 2) & motive_rhythm_2 # add key and octave seq1 = (motive_1 * 4) | add({"octave": 5}) | degree_in_key(C_minor) seq2 = (motive_1 * 4) | add({"octave": 5}) | degree_in_key(C_Major) seq3 = motive_2 | add({"octave": 4}) | degree_in_key(C_minor) seq = (seq1 + seq2 + seq3) | midi_pitch() | OSequence write_midi.write("game_of_thrones.mid", [seq], instruments=[49], tempo=350000)
down_degrees = [6, 4, 3, 2, 1, 2, 3, 4] final_degree = [1] sections = [ (up_degrees, 4, range(14)), (down_degrees, 4, range(13, -2, -1)), (final_degree, 32, range(1)), ] hanon_1 = OSequence() for section in sections: pattern, duration_64, offset = section for o in offset: for note in pattern: hanon_1.append({"degree": note + o, DURATION_64: duration_64}) hanon_1 = hanon_1 | degree_in_key_with_octave(Key("C", major_scale), 4) | midi_pitch() hanon_rh_1 = hanon_1 hanon_lh_1 = hanon_1 | transpose(-12) seq = hanon_lh_1 // hanon_rh_1 if __name__ == "__main__": f = open("hanon.mid", "w") s = SMF([seq]) s.write(f) f.close()