-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRagaAnalyzer.groovy
344 lines (300 loc) · 9.04 KB
/
RagaAnalyzer.groovy
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
import java.util.Random
class RagaAnalyzer {
private Random = new Random()
static valid_notes = "sSrRgGmMpPdDnNzZ".collect{it}
private walk_list = []
private abs_nodes = []
private abs_edges = []
private rel_edges = []
private abs_nodes_histo
private abs_edge_histo
private rel_edge_histo
private notes_by_octave = [:]
private raga_map = ['forward':[:], 'inverse':[:]]
RagaAnalyzer(){ }
RagaAnalyzer(composition){
parseComposition(composition)
}
public parseComposition(composition){
def parser = new NotationParser()
def sequence = parser.parse(composition)
def toks = []
parser.get_seq_list().each { tok ->
try {
if ("java.util.ArrayList".equals(tok[0].class.name) &&
tok[0][0] in valid_notes) {
def note = tok[0][0]
def octave = tok[0][1]
if (note in ['s', 'S', 'p', 'P', 'z', 'Z']) {
note = note.toUpperCase()
}
if (note in ['z', 'Z']) { octave = '0' }
toks.push([note, octave])
} else {
toks.push(['-', '0'])
}
} catch (e){
println e
}
}
(1..toks.size()-1).each{ i ->
def start_note = toks[i-1][0]
def start_octave = Integer.decode(toks[i-1][1])
def end_note = toks[i][0]
def end_octave = Integer.decode(toks[i][1])
if (![start_note, end_note].contains('-')) {
this.abs_edges.push([[start_note, start_octave], [end_note, end_octave]])
this.abs_nodes.push([start_note, start_octave])
this.abs_nodes.push([end_note, end_octave])
}
}
this.abs_nodes_histo = computeHistogram(this.abs_nodes)
this.abs_edge_histo = computeHistogram(this.abs_edges)
this.computeRelativeEdges()
this.rel_edge_histo = computeHistogram(this.rel_edges)
this.generateRagaMap()
}
public printRagaMap() {
this.raga_map.each { mk, mv ->
println mk
mv.each { sk, sv ->
if ('forward'.equals(mk)) {
println '\t' + sk
sv.each {println '\t\t' + it}
} else {
sv.each {println '\t' + it}
println '\t\t\t' + sk
}
}
}
}
public getRagaMap() { return this.raga_map }
private initWalk (){ this.walk_list = [] }
private printWalkList () {
def lines = this.walk_list.size()/4.0
lines.each { l ->
(0..3).each { i ->
try {
println this.walk_list[l*4 + i]
}catch(e){
return
}
}
}
}
private allPossibleCases(c){
return [c, c.toUpperCase(), c.toLowerCase()].unique()
}
private walkAstep(direction, swara){
def next_possibilities = nextInRagaMap(direction, swara)
def n = next_possibilities.values().sort().unique().reverse()
def lim = n.size() - 1
def rand_loop = true;
def x;
while (rand_loop) {
x = Math.round(Math.abs(Random.nextGaussian()))
rand_loop = !(x <= lim )
}
def m = n[x.intValue()]
n = next_possibilities.collectMany {k, v -> (v == m) ? [k] : []}
if (n.size() > 1) {
x = (Random.nextInt(2*(n.size()-1)) - (n.size()-1)).abs()
} else { x = 0; }
def next = n[x]
}
private nextInRagaMap(direction, swara) {
def ss = this.allPossibleCases(swara[0])
def oo = [swara[1], 0].unique()
def keys = [ss,oo].combinations().findAll {
this.raga_map[direction].containsKey(it)
}
if (keys.size() <= 0) {
keys = [['Z','z','S','s'],[0]].combinations().findAll {
this.raga_map[direction].containsKey(it) }
}
return this.raga_map[direction][keys[0]]
}
public walkRagaMap(direction, swara, length){
if (this.walk_list.size() == 0) {
this.walk_list.push(swara)
}
def next = walkAstep(direction, swara)
if (this.walk_list[-1] != next) this.walk_list.push(next)
while (this.walk_list.size() < length ) {
//println "length unmatched"
while (this.walk_list[-1].indexOf(next)) {
// retry if this is a repeat production
this.walkRagaMap(direction, next, length)
//print 'retrying next :'; println next
if (this.walk_list.size() >= length ) {
//println "length matched"
//println 'walk_list :'
this.printWalkList()
if ('inverse'.equals(direction)) { return this.walk_list.reverse() }
else { return this.walk_list }
break;
}
}
}
}
public generateRagaMap() {
this.rel_edge_histo.each { redge, wt ->
def L = redge[0]
def R = redge[1]
def Lf = [L[0], 0]
def Rf = R
if (L[1] > 0) {
Rf = [R[0], R[1]-L[1]]
} else if (L[1] <0) {
Rf = [R[0], R[1] + L[1]*-1]
}
def Lr = L
def Rr = [R[0], 0]
if (R[1] > 0) {
Lr = [L[0], L[1]-R[1]]
} else if (R[1] < 0) {
Lr = [L[0], L[1] + R[1]*-1]
}
this.raga_map['forward'][Lf] = this.raga_map['forward'][Lf] ?: [:]
this.raga_map['forward'][Lf][Rf] = wt
this.raga_map['inverse'][Rr] = this.raga_map['inverse'][Rr] ?: [:]
this.raga_map['inverse'][Rr][Lr] = wt
}
}
public generateDot(){
//group notes by octave
this.abs_nodes.each{
this.notes_by_octave[it[1]] = this.notes_by_octave[it[1]] ?: []
this.notes_by_octave[it[1]].push(it[0])
this.notes_by_octave[it[1]].unique()
}
def note_rank = []
def out_string = ""
out_string += "digraph raga_analysis {\n";
this.notes_by_octave.keySet().sort().each { octave ->
def notes = valid_notes[0..-3].collect{ n ->
if (n in this.notes_by_octave[octave]) n
}.findAll{it != null}
out_string += "\tsubgraph cluster_" + octave + " {\n";
notes.each { note ->
out_string += '\t"' + note + octave + '"[label="' + note + '"];\n'
}
out_string += '\tlabel="octave_' + octave + '"\n';
out_string += "\t}\n";
}
this.abs_edge_histo.each { i->
out_string += '"' + i.key[0].join() + '" -> "' + i.key[1].join() + '"[' +
'label="' + i.value + '", ' +
'weight=' + i.value.toFloat() + ', ' +
'decorate=true' +
//'constraint=false' +
']\n';
}
out_string += "}"
return out_string
}
private computeHistogram(alist){
def histo = [:]
alist.each { histo[it] = (histo[it] ?: 0) + 1 }
return histo
}
private computeRelativeEdges(){
this.rel_edges = []
this.abs_edges.each { edge ->
def Ln = edge[0][0]
def Lo = edge[0][1]
def Rn = edge[1][0]
def Ro = edge[1][1]
def lr_delta = (Lo - Ro)
def rl_delta = (Ro - Lo)
if ('Z' in [Ln,Rn]) {lr_delta = 0; rl_delta = 0}
this.rel_edges.push([[Ln, 0], [Rn, lr_delta]])
this.rel_edges.push([[Ln, rl_delta], [Rn, 0]])
}
}
static void main(args) {
def ra = new RagaAnalyzer()
if(args.size() == 1) {
// If a notation file is passed as arg
def f = new File(args[0])
def composition = f.getText()
ra.parseComposition(composition)
println ra.generateDot()
//ra.printRagaMap()
/////////////////////////////////////////////////////////////
// Persist the raga definition to disk for use by other code
// why not add it to the corpus itself
// instead of a new config
////////////////////////////////////////////////////////////
def cf = args[0] + '.map'
//create the datastructure
def configObj = new ConfigObject()
configObj.put(args[0], ra.ragaMap)
//serialize it
new File( cf ).withWriter{ writer ->
configObj.writeTo( writer )
}
//println "Written map : " + cf
} else {
def composition = """
T160 I[ALTO_SAX]
Tala=eka,4
Raga=nattai,4
O=5
L=2
m p2 m r s r2
L=1
s <D N2 >s4 s <N >s3 r G2
L=2
m p2 m r s r2
L=1
s <D N2 >s4 s <N >s r G2 m2
L=1
p N >s <N p m G m p m r s r4
s <D N2 >s4 s <N >s r G2 m2
p N >s2 r2 s <N p m r s r4
p >s2 <N p2 m G m r r s <N >s r G
m2 p2 N p p m m r r s s r3
s <D N2 >s12
O=5
m2 p2 N p p D N >s s <N >s4
r r s2 r r s2 s <N >s4
O=5
p N >s <N p m G m
p D N2 >s <N >s2
r r s2 r r s2 s <N >s2 r4
s r2 G2 m r4 s4
s <N p N >s <N p3 m
N p p m r s
m p G m p
s r G m p m p
p m p N p m
>r r s s <N p m
p2 p m G m r s r s <N >s p m G m
p2 D N >s r G m m r s r s <N p m
>s <N p m G m N p m r G m r r s
>r2 s s2 <N p2 m r2 s N s r G
L=2
m p2 m r s r2
L=1
s <D N2 >s12
"""
ra.parseComposition(composition)
//println ra.generateDot()
ra.printRagaMap()
/*
['forward', 'inverse'].each { dir ->
this.initWalk = []
def rev = ra.walkRagaMap( dir , ['S',0] , 32).collect{ it[0] }
println rev.join(' ')
0.step(rev.size(), 4) { s->
4.times { i ->
print rev[s+i] + ' '
}
println ' '
}
println ' '
} */
}
}
}