analitics

Pages

Showing posts with label music21. Show all posts
Showing posts with label music21. Show all posts

Sunday, April 14, 2019

Using the python module music21.

What is music21?
Music21 is a set of tools for helping scholars and other active listeners answer questions about music quickly and simply. If you’ve ever asked yourself a question like, “I wonder how often Bach does that” or “I wish I knew which band was the first to use these chords in this order,” or “I’ll bet we’d know more about Renaissance counterpoint (or Indian ragas or post-tonal pitch structures or the form of minuets) if I could write a program to automatically write more of them,” then music21 can help you with your work.
This toolkit for Computer-Aided Musical Analysis was developed at MIT by cuthbertLab. Michael Scott Cuthbert, Principal Investigator.
The development of music21 is supported by the generosity of the Seaver Institute and the NEH.
The tutorial today is about the python music21 module.
Let's start with the default pip tool to install this python module:
C:\Python364\Scripts>pip install --upgrade music21
Collecting music21
...
  Running setup.py install for music21 ... done
Successfully installed music21-5.5.0
The next step tells us to install some additional python modules:
C:\Python373>python.exe
Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 22:22:05) [MSC v.1916 64 bit (AMD64)]
 on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from music21 import * 
music21: Certain music21 functions might need these optional packages: matplotlib,
 scipy;
if you run into errors, install them by following the instructions at
http://mit.edu/music21/doc/installing/installAdditional.html
Let's install these python modules:
C:\Python373\Scripts>pip install matplotlib
C:\Python373\Scripts>pip install scipy
Let's test and play a simple example (Bach’s BWV 66.6):
>>> from music21 import *
>>> s = corpus.parse('bwv66.6')
>>> sChords = s.chordify()
>>> sChords
...
>>> sChords.show('midi')
If you want to save it, use:
>>> sChords.write("midi", "bach6.mid")
'bach6.mid'
Another example is to play a note on guitar (C#):
from music21 import stream, instrument
from music21.note import Note
cd = Note("C#", type='quarter')
test = stream.Part()
test.insert(0, instrument.AcousticGuitar())
test_measure = stream.Measure()
test_measure.append(cd)
test.append(test_measure)
test.show('midi')
You can see all the instruments here.
This python module is very complex and can be used with additional software, see here.
More details can be found in the documentation.