Skip to content

Commit

Permalink
Add an example to create a MIDI file
Browse files Browse the repository at this point in the history
  • Loading branch information
yuma-m committed Jan 4, 2021
1 parent 89a3327 commit c5aab3c
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
.idea/
venv/
.DS_Store

# Byte-compiled / optimized / DLL files
__pycache__/
Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG.md
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.
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,10 @@ True
<Chord: E7> # V7 of A minor
```

## Examples

- [pychord-midi.py](./examples/pychord-midi.py) - Create a MIDI file using PyChord and pretty_midi.

## Supported Python Versions

- 2.7
Expand Down
32 changes: 32 additions & 0 deletions examples/pychord-midi.py
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()

0 comments on commit c5aab3c

Please sign in to comment.