-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathChapter9.1.qmd
107 lines (80 loc) · 3.44 KB
/
Chapter9.1.qmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
---
toc: true
---
<a href="https://colab.research.google.com/github/tuomaseerola/emr/blob/main/Chapter9.1.ipynb" target="_parent"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab"/></a>
# Ch. 9 - Music analysis
This is a very simple demonstration of several built-in analytical functions in music21. We carry out a reduction of Bach chorale, analysis of harmony using Roman numerals and add Lerdahl and Jackendof type of metrical hierarchy.
## Install Music21 (in Colab)
::: {.callout-important}
The first code segment is to install `music21` and other elements needed to run the environment. In Colab, press `play` and wait for all commands to be executed - this initial command might take some time as it needs to build the `musi21` environment.
:::
## Install Music21 and Musescore in local machine
::: {.callout-important}
For instructions of how to get music21 and Musescore working on a local machine, see [Installation guidelines from music21](https://web.mit.edu/music21/doc/installing/index.html).
:::
::: {.callout-note}
The script below uses a workaround where the excerpts are first written to a xml file and then converted to png image.
:::
```{python}
#| echo: false
#| eval: false
#| id: install
!pip install --upgrade music21
!add-apt-repository ppa:mscore-ubuntu/mscore-stable -y
!apt-get update
!apt-get install musescore
!apt-get install xvfb
!sh -e /etc/init.d/x11-common start
import os
os.putenv('DISPLAY', ':99.0')
!start-stop-daemon --start --pidfile /var/run/xvfb.pid --make-pidfile --background --exec /usr/bin/Xvfb -- :99 -screen 0 1024x768x24 -ac +extension GLX +render -noreset
from music21 import *
us = environment.UserSettings()
us['musescoreDirectPNGPath'] = '/usr/bin/mscore'
us['directoryScratch'] = '/tmp'
```
## Harmonic and metrical analysis
Harmonic and metrical analysis of an example excerpt `bach/bwv30.6` using `music21`.
### Harmonic analysis – Reduction
First get a Bach chorale.
```{python}
#| echo: true
#| eval: true
#| label: analyse
from music21 import * # activate library
### 1 Select one example from Bach chorales
bwv30_6 = corpus.parse('bach/bwv30.6.xml')# Take an example
#bwv30_6.measures(1, 3).show() # Display 3 bars
bwv30_6.measures(1, 3).write('xml', fp='output.xml')
!mscore output.xml -o images/score1.png --trim-image 0
```
![](images/score1-1.png)
### Harmonic analysis
Analyse chords using Roman numerals.
```{python}
#| label: harmonic
bChords = bwv30_6.chordify() # Slice the chords
for c in bChords.recurse().getElementsByClass('Chord'):
c.closedPosition(forceOctave=4, inPlace=True)
# Run analysis and add Roman numerals as lyrics
for c in bChords.recurse().getElementsByClass('Chord'):
rn = roman.romanNumeralFromChord(c, key.Key('A'))
c.addLyric(str(rn.figure))
bChords.measures(0, 3).show() # Display the result
bChords.measures(0, 3).write('xml', fp='output.xml')
!mscore output.xml -o images/score2.png --trim-image 0
```
![](images/score2-1.png)
### Metrical analysis
Carry out metrical analysis.
```{python}
#| label: metrical
bass = bwv30_6.getElementById('Bass') # Get the bass part
excerpt = bass.measures(1,3) # Bar 1 through 3
analysis.metrical.labelBeatDepth(excerpt)# Metrical analysis
#excerpt.show() # Display the results
excerpt.write('xml', fp='output.xml')
!mscore output.xml -o images/score3.png --trim-image 0
!rm output.xml
```
![](images/score3-1.png)