-
Notifications
You must be signed in to change notification settings - Fork 0
/
post_tonal_tools.jl
276 lines (208 loc) · 6.33 KB
/
post_tonal_tools.jl
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
######################
# Post-Tonal Tools #
######################
# General
# =======
# Test Pitch Class Collections
melody = [64, 62, 60, 62, 52, 76, 64]
fr_Aug6 = [0, 2, 6, 8]
"""
midi_to_pc(pcc)
Converts midi pitch integers to pitch class integers, as a single pitch or pitch classes collections (pcc)
# Examples
```jldoctest
julia> melody = [64, 62, 60, 62, 52, 76, 64]
7-element Vector{Int64}:
64
62
60
62
52
76
64
# Running the midi_to_pc function and printing its results.
julia> println(midi_to_pc(melody))
[4, 2, 0, 2, 4, 4, 4]
```
"""
function midi_to_pc(pcc)
return [i % 12 for i ∈ pcc]
end
# Intervals
# =========
# Ordered Pitch Intervals and Adjacent Interval Series
# ----------------------------------------------------
"""
opi(pcc)
Create Ordered Pitch Intervals (OPI) from a pitch class collection (pcc).
The OPI definition comes from Joseph N. Straus' "Introduction to Post-Tonal Music, 4th Edition."
# Examples
```jldoctest
# Double checking whether there is a melody, if not melody has to be defined.
julia> show(melody)
[64, 62, 60, 62, 52, 76, 64]
julia> println(opi(melody))
[-2, -2, 2, -10, 24, -12]
```
"""
function opi(pcc)
return [pcc[i+1] - pcc[i] for i ∈ eachindex(pcc) if i < length(pcc)]
end
"""
ais(pcc)
Create an adjacent interval series (AIS), like the opi function, from a pitch class collection (pcc).
The AIS definition comes from Joseph N. Straus' "Introduction to Post-Tonal Music, 4th Edition."
# Examples
```jldoctest
julia> show(melody)
[64, 62, 60, 62, 52, 76, 64]
julia> println(ais(melody))
[-2, -2, 2, -10, 24, -12]
```
"""
function ais(pcc)
return opi(pcc)
end
# Unordered Pitch Intervals
# -------------------------
"""
upi(pcc)
Create Unordered Pitch Intervals (UPI) from a pitch class collection (pcc).
The UPI definition comes from Joseph N. Straus' "Introduction to Post-Tonal Music, 4th Edition."
# Examples
```jldoctest
julia> show(melody)
[64, 62, 60, 62, 52, 76, 64]
julia> println(upi(melody))
[2, 2, 2, 10, 24, 12]
```
"""
function upi(pcc)
return [abs(pcc[i+1] - pcc[i]) for i ∈ eachindex(pcc) if i < length(pcc)]
end
# Ordered Pitch Class Intervals
# -----------------------------
"""
opci(pcc)
Create Ordered Pitch Class Intervals (OPCI) from a pitch class collection (pcc).
The OPCI definition comes from Joseph N. Straus' "Introduction to Post-Tonal Music, 4th Edition."
# Examples
```jldoctest
julia> show(melody)
[64, 62, 60, 62, 52, 76, 64]
julia> println(opci(melody))
[10, 10, 2, 2, 0, 0]
```
"""
function opci(pcc)
return [i < 0 ? i+12 : i for i ∈ [(pcc[i+1] - pcc[i]) % 12 for i ∈ eachindex(pcc) if i < length(pcc)]]
end
# Unordered Pitch Class Intervals and Interval Classes
# ----------------------------------------------------
"""
upci(pcc)
Create Unordered Pitch Class Intervals (UPCI) from a pitch class collection (pcc).
The OPCI definition comes from Joseph N. Straus' "Introduction to Post-Tonal Music, 4th Edition."
# Examples
```jldoctest
julia> show(melody)
[64, 62, 60, 62, 52, 76, 64]
julia> println(upci(melody))
[2, 2, 2, 2, 0, 0]
```
"""
function upci(pcc)
x = [i < 0 ? i+12 : i for i ∈ [(pcc[i+1] - pcc[i]) % 12 for i ∈ eachindex(pcc) if i < length(pcc)]]
y = [i < 0 ? i+12 : i for i ∈ [(pcc[i] - pcc[i+1]) % 12 for i ∈ eachindex(pcc) if i < length(pcc)]]
ic = [i[1] < i[2] ? i[1] : i[2] for i ∈ zip(x,y)]
return ic
end
"""
ic(pcc)
Create Interval Classes (IC) from a pitch class collection (pcc).
The OPCI definition comes from Joseph N. Straus' "Introduction to Post-Tonal Music, 4th Edition."
# Examples
```jldoctest
julia> show(melody)
[64, 62, 60, 62, 52, 76, 64]
julia> println(ic(melody))
[2, 2, 2, 2, 0, 0]
```
"""
function ic(pcc)
return upci(pcc)
end
# Collections or Scales measured by intervals
# -------------------------------------------
ionian = [2,2,1,2,2,2,1] # ionian mode / major mode
dorian = [2,1,2,2,2,1,2] # dorian mode (minor)
phrygian = [1,2,2,2,1,2,2] # phrygian mode (minor)
lydian = [2,2,2,1,2,2,1] # lydian mode (major)
mixolydian = [2,2,1,2,2,1,2] # mixolydian mode (major)
aeolian = [2,1,2,2,1,2,2] # aeolian mode (minor)
locrian = [1,2,2,1,2,2,2] # locrian mode (minor)
melodic = [2,1,2,2,2,2,1] # melodic minor
harmonic = [2,1,2,2,1,3,1] # melodic minor
first_m = [2,2,2,2,2] # whole tone
oct_12 = [1,2,1,2,1,2,1] # oct 1–2 ordering
oct_21 = [2,1,2,1,2,1,2] # oct 2-1 ordering
third_m = [2,1,1,2,1,1,2,1,1] # Messiaen's third mode of limited transposition
fourth_m = [1,1,3,1,1,1,3,1] # Messiaen's fourth mode of limited transposition
fifth_m = [1,4,1,1,4,1] # Messiaen's sixth mode of limited transposition
sixth_m = [2,2,1,1,2,2,1,1] # Messiaen's sixth mode of limited transposition
seventh_m = [1,1,1,2,1,1,1,1,2,1] # Messiae's sixth mode of limited transpostion
hex_13 = [1,3,1,3,1] # hex 1-3 ordering
hex_31 = [3,1,3,1,3] # hex 3-1 ordering
"""
scales(pc=0,intervals=[2,2,1,2,2,2,1])
Generate a scale from a given pitch class (pc) by means of an adjacent interval series (intervals).
# Examples
```jldoctest
# The default scale is C Major
julia> println(scales())
Any[0, 2, 4, 5, 7, 9, 11, 0]
# Hexatonic scale starting on PC 2 in 3-1 ordering
julia> println(scales(2,hex_31))
Any[2, 5, 6, 9, 10, 1]
```
"""
function scales(pc=0,intervals=[2,2,1,2,2,2,1])
pcs = Vector{Int64}()
pcc = push!(pcs,pc)
[append!(pcs,(intervals[i]+pcc[i]) % 12) for i ∈ eachindex(intervals)]
return pcs
end
# Pitch Class names
# -----------------
"""
pc_names
A matrix of equivalent pitch class names:
- Integers
- English
- Solfège
- German/Hungarian/Polish/Slovak/Slovenian/Estonian/Danish/Czech
- Dutch
- Norwegian/Swedish
# Examples
```jldoctest
# Using the scales function and pc_names matrix to create readable scale names
# Building a c minor scale
julia> show(c_minor)
Any[0, 2, 3, 5, 7, 8, 10, 0]
# viewing the scale in solfège names rather than integer names
julia> println([pc_names[i+1,3] for i in c_minor])
["do", "re", "me", "fa", "sol", "le", "te", "do"]
```
"""
pc_names = [0 "C" "do" "C" "C" "C";
1 "C#" "di" "Cis" "Cis" "Ciss";
2 "D" "re" "D" "D" "D";
3 "Eb" "me" "Es" "Es" "Ess";
4 "E" "mi" "E" "E" "E";
5 "F" "fa" "F" "F" "F";
6 "F#" "fi" "Fis" "Fis" "Fiss";
7 "G" "sol" "G" "G" "G";
8 "Ab" "le" "As" "As" "Ass";
9 "A" "la" "A" "A" "A";
10 "Bb" "te" "B" "Bes" "Bess";
11 "B" "do" "H" "B" "H";]