-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add an example to create a MIDI file
- Loading branch information
Showing
4 changed files
with
39 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
.idea/ | ||
venv/ | ||
.DS_Store | ||
|
||
# Byte-compiled / optimized / DLL files | ||
__pycache__/ | ||
|
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
## Forthcoming | ||
|
||
- Add an example to create a MIDI file. | ||
|
||
## v0.5.1 | ||
|
||
- Add `m7b9b5` quality. | ||
|
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# An example to create MIDI file with PyChord and pretty_midi | ||
# Prerequisite: pip install pretty_midi | ||
# pretty_midi: https://github.com/craffel/pretty-midi | ||
|
||
|
||
import pretty_midi | ||
|
||
from pychord import Chord | ||
|
||
|
||
def create_midi(chords): | ||
midi_data = pretty_midi.PrettyMIDI() | ||
piano_program = pretty_midi.instrument_name_to_program('Acoustic Grand Piano') | ||
piano = pretty_midi.Instrument(program=piano_program) | ||
length = 1 | ||
for n, chord in enumerate(chords): | ||
for note_name in chord.components_with_pitch(root_pitch=4): | ||
note_number = pretty_midi.note_name_to_number(note_name) | ||
note = pretty_midi.Note(velocity=100, pitch=note_number, start=n * length, end=(n + 1) * length) | ||
piano.notes.append(note) | ||
midi_data.instruments.append(piano) | ||
midi_data.write('chord.mid') | ||
|
||
|
||
def main(): | ||
chords_str = ["C", "Dm7", "G", "C"] | ||
chords = [Chord(c) for c in chords_str] | ||
create_midi(chords) | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |