def get_music(a, b, key='C', mode='major'): midi_out = StringIO() scale = build_scale(key, mode, octaves=1) matcher = SequenceMatcher(None, a, b) tone = key.lower() melodies = [tone] for tag, i1, i2, j1, j2 in matcher.get_opcodes(): next_note = None if tag == 'replace': next_note = 'r' elif tag == 'equal': next_note = tone elif tag == 'delete': tone = tone_down(tone, scale) next_note = tone elif tag == 'insert': tone = tone_up(tone, scale) next_note = tone melodies += [next_note] * ((i2 - i1) or 1) s = SMF([parse(" ".join(melodies))]) s.write(midi_out) return midi_out
def play_music(self, x, y): ''' Generate sounds from data to be used for each coordinate. x and y are the coordinates of any image point. ''' scale = build_scale('C', mode='major', octaves=1) notes = note_number(self.data, scale) note = notes[y,x] melody = parse(note_name(note, scale)) midi_out = StringIO() write('Oc.midi', [melody]) os.system('fluidsynth --audio-driver=alsa /usr/share/sounds/sf2/FluidR3_GM.sf2 Oc.midi')
def play_music(self, x, y): ''' Generate sounds from data to be used for each coordinate. x and y are the coordinates of any image point. ''' scale = build_scale('C', mode='major', octaves=1) notes = note_number(self.data, scale) note = notes[y,x] melody = parse(note_name(note, scale)) midi_out = StringIO() write('Oc.midi', [melody]) pygame.mixer.init() music = pygame.mixer.Sound('Oc.midi') pygame.mixer.music.load('Oc.midi') pygame.mixer.music.play()
def test_build_scale_major(): scale = build_scale('C', 'major', 1) assert scale == 'c d e f g a b'.split()
def test_build_note_name(): scale = build_scale('C', 'major', 1) notes = note_number([1, 2, 3, 4, np.nan], scale) assert [note_name(x, scale) for x in notes] == ['c', 'e', 'g', 'b', 'r']
def test_note_number(): scale = build_scale('C', 'major', 1) assert all(note_number([1, 2, 3, 4], scale) == [0, 2, 4, 6])
def test_build_scale_pentatonic_two_octaves(): scale = build_scale('D', 'pentatonic', 2) assert scale == ['d', 'e', 'f#', 'a', 'b', "d'", "e'", "f#'", "a'", "b'"]
def test_build_scale_pentatonic(): scale = build_scale('C', 'pentatonic', 1) assert scale == ['c', 'd', 'e', 'g', 'a']
def test_build_scale_minor(): scale = build_scale('A', 'minor', 1) assert scale == 'a b c d e f g'.split()