Created
December 9, 2018 20:43
-
-
Save goldengrape/8d0041eea735d5f79acc8943b46c0ca0 to your computer and use it in GitHub Desktop.
learnmusic.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! python2 | |
# NOTE: The first line in this script specifies that it should always be run using Python 2.7. | |
# The `midiutil` module is currently not available for Python 3. | |
'''Generates a MIDI file with 12 random notes in C major, using the midiutil module. The instrument is also picked randomly. The result is then played with the sound.MIDIPlayer class. | |
If nothing happens, make sure that your device isn't muted. | |
''' | |
from midiutil.MidiFile import MIDIFile | |
from random import choice, randint | |
import sound | |
import numpy as np | |
## Configure a MIDI file with one track: | |
degrees = [60, 62, 64, 65, 67, 69, 71, 72] | |
# MIDI note number | |
track = 1 | |
channel = 2 | |
time = 0 # In beats | |
duration = 1 # In beats | |
tempo = 120 # In BPM | |
volume = 100 # 0-127, as per the MIDI standard | |
midi = MIDIFile(1) # One track | |
midi.addTempo(track, time, tempo) | |
## Select the instrument: | |
program = 1 #piano | |
# tracknum, channel, time, program | |
midi.addProgramChange(0, 0, 0, program) | |
# def chord | |
def chord(base, type, pitch_list, pitch_range=5): | |
base_list=[0, 2, 4, 6, 8, 10, 12] | |
type_dict={ | |
"major": [0, 0, 0, 0, 0, 0, 0], | |
"minor": [0, -1, 0, -1, 0, 0, 0], | |
"dominant": [0, 0, 0, -1, 0, 0, 0], | |
} | |
pitch_num=int((pitch_range+1)/2) | |
pick_list=[x+y for (x,y) in zip(base_list,type_dict[type])][:pitch_num] | |
print(pitch_num) | |
chord_list=pitch_list[pick_list]+base | |
print(chord_list) | |
return chord_list | |
# Generate 7 modes: | |
def generate_mode(): | |
white_pitch=np.asarray([0,2,4,5,7,9,11]) | |
base_mode=np.append(white_pitch, white_pitch+12) | |
mode_name=["ionian","dorian","phrygian","lydian","mixolydian","aeolian","locrian"] | |
mode={} | |
for i in range(7): | |
mode[mode_name[i]]=np.append(base_mode[i:],base_mode[:i]+24)-base_mode[i] | |
return mode | |
pitch_list=np.asarray(generate_mode()['mixolydian']+60) | |
pick_list=[0,2,4,5] | |
chord_1=pitch_list[pick_list] | |
chord_1_duration=[1,1,1,1] | |
for s in range(10): | |
tstart=s*4 | |
chord_1_t=tstart | |
for t in range(4): | |
p=chord_1[t] | |
d=chord_1_duration[t] | |
midi.addNote(0,0,p,chord_1_t,d,100) | |
chord_1_t=chord_1_t+d | |
rt=tstart | |
for t in range(4): | |
pitch = choice(pitch_list) | |
#track, channel, pitch, time, duration, volume | |
midi.addNote(0, 1, pitch, rt+t * duration, duration, 100) | |
# Write output file: | |
with open('output.mid', 'w') as f: | |
midi.writeFile(f) | |
# Play the result: | |
player = sound.MIDIPlayer('output.mid') | |
player.play() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment