-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpc-functions.lua
More file actions
208 lines (188 loc) · 5.48 KB
/
Copy pathpc-functions.lua
File metadata and controls
208 lines (188 loc) · 5.48 KB
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
local sets = require 'pc-sets'
local pc = {}
function member(arr, x)
for i = 1,#arr do
if arr[i] == x then return true end
end
return false
end
function list_min(arr)
local minx = arr[1]
for i = 2, #arr do
if(arr[i] < minx) then minx = arr[i] end
end
return minx
end
function rm_dups(arr)
local hash = {}
local t = {}
for i = 1, #arr do
local v = arr[i]
if (not hash[v]) then
t[#t+1] = v
hash[v] = true
end
end
return t
end
function pc.get_normal_form(chord,invers)
local set0 = {} -- chord -> pc
local setx = {} -- to reset
local set1 = {} -- normalform
local posarr = {} -- collect result positions
local diffx = {}
local posaddx = 0
for i = 1, #chord do
set0[i] = math.floor(chord[i] % 12) --quartertones -> rounded
end
set0 = rm_dups(set0)
if invers then
for i = 1, #set0 do
set0[i] = (12 - set0[i]) % 12
end
end
table.sort(set0)
for i = 1, #set0 do
posarr[i] = i
end
if(#set0 < 2) then
set2[1] = 0
return set2
else
--span test (last - first, penultima last - first, before penultima last - first)
if (#posarr > 1) then
while (#posarr > 1 and posaddx < (#set0 - 1) ) do
--reset setx
for i = 1, #set0 do
setx[i] = set0[i]
end
for j = 1, #set0 do
local posstart = ((j -1) % #set0) + 1
local posend = ((j + #set0 - 2 - posaddx) % #set0) + 1
local posendbefore = ((j + #set0 - 3 - posaddx) % #set0) + 1
local a = setx[posstart]
local b = setx[posend]
local c = setx[posendbefore]
if(b<c) then b = b + 12; setx[posend] = b end
if member(posarr,j) then
diffx[j] = b - a
end
end
local minx = list_min(diffx)
posarr = {}
for i = 1, #set0 do
if(diffx[i] == minx) then posarr[#posarr+1] = i end
end
for i = 1, #set0 do
setx[i] = set0[i]
end
posaddx = posaddx + 1
if (#posarr == 1) then break end
end
end
local posres = posarr[1]
for i = 1, #set0 do
local pos2 = ((i + posres - 2) % #set0) + 1
set1[i] = set0[pos2] % 12
end
return set1
end
end
function pc.normal2prime_form(normal_set)
local t = {}
local firstval = normal_set[1]
for i = 1, #normal_set do
t[i] = (normal_set[i] - firstval) % 12
end
return t
end
function pc2string(arr)
local stringx = ""
for i = 1, #arr do
local pcx = arr[i]
if (pcx == 10) then pcx = "T"
elseif (pcx == 11) then pcx = "E"
end
stringx = stringx .. pcx
end
return stringx
end
function pc.info(chord)
local normal_a = pc.get_normal_form(chord,false)
local normal_b = pc.get_normal_form(normal_a,true)
local a = pc2string(pc.normal2prime_form(normal_a))
local b = pc2string(pc.normal2prime_form(normal_b))
local resx
if(#a == 1) then
resx = sets.card1[1]
elseif (#a == 12) then
resx = sets.card12[1]
elseif (#a == 11) then
resx = sets.card11[1]
elseif (#a == 2) then
for j = 1, #sets.card2 do
if(sets.card2[j][2] == a or sets.card[j][2] == b) then
resx = sets.card2[j]
break
end
end
elseif (#a == 10) then
for j = 1, #sets.card10 do
if(sets.card10[j][2] == a or sets.card10[j][2] == b) then
resx = sets.card10[j]
break
end
end
elseif (#a == 3) then
for j = 1, #sets.card3 do
if(sets.card3[j][2] == a or sets.card3[j][2] == b) then
resx = sets.card3[j]
break
end
end
elseif (#a == 9) then
for j = 1, #sets.card9 do
if(sets.card9[j][2] == a or sets.card9[j][2] == b) then
resx = sets.card9[j]
break
end
end
elseif (#a == 4) then
for j = 1, #sets.card4 do
if(sets.card4[j][2] == a or sets.card4[j][2] == b) then
resx = sets.card4[j]
break
end
end
elseif (#a == 8) then
for j = 1, #sets.card8 do
if(sets.card8[j][2] == a or sets.card8[j][2] == b) then
resx = sets.card8[j]
break
end
end
elseif (#a == 7) then
for j = 1, #sets.card7 do
if(sets.card7[j][2] == a or sets.card7[j][2] == b) then
resx = sets.card7[j]
break
end
end
elseif (#a == 5) then
for j = 1, #sets.card5 do
if(sets.card5[j][2] == a or sets.card5[j][2] == b) then
resx = sets.card5[j]
break
end
end
elseif (#a == 6) then
for j = 1, #sets.card6 do
if(sets.card6[j][2] == a or sets.card6[j][2] == b) then
resx = sets.card6[j]
break
end
end
end
return normal_a,resx[1],resx[2],resx[3]
end
return pc