OpenMusic-Library zur regelbasierten Harmonie und Stimmführung.
Lorenz Lehmann
12.02.24 e292a63e3e70d3757d7316df3403efd81da68b26
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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
;======================================
;OM-Lead, 2022-2023
;
;Library for Rule-based Voice-Leading
;Author: Lorenz Lehmann
;Supervision: Marlon Schumacher
;======================================
 
 
 
 
(in-package :omlead)
 
(om::defmethod! gen-sequence (harmonies first-harmony number-of-harmonies &key avoid-repetitions voice-tie1 voice-tie2 voice-tie3 voice-tie4 voice-interval1 voice-interval2 voice-interval3 voice-interval4 voice-counter1  counter-interval1 voice-counter2 counter-interval2 voice-counter3 counter-interval3 voice-counter4 counter-interval4 voice-counter5 counter-interval5 voice-counter6 counter-interval6 base-list)
 
:icon 030719972
 
:numouts 1
 
 
 
 
(let* ((harmon-database harmonies)
       (database (cond (base-list (loop repeat (1- number-of-harmonies)
                                         for x in (circular_oml base-list)
                                         collect (nth (1- x) harmon-database)))
 
                        (t (loop repeat (1- number-of-harmonies) collect harmon-database))))
 
 
 
 
 
       (external-list '()))
  (append (list first-harmony)
          (loop repeat (- number-of-harmonies 1)
                for database in database
                                    
                for voice-tie1 in (cond ((atom voice-tie1) (loop repeat (1- number-of-harmonies) collect voice-tie1))
                                        ((< (length voice-tie1) (1- number-of-harmonies)) 
                                         (append voice-tie1 (loop repeat (- (1- number-of-harmonies) (length voice-tie1)) collect (car (reverse voice-tie1)))))
                                        (t voice-tie1))
                for voice-tie2 in (cond ((atom voice-tie2) (loop repeat (1- number-of-harmonies) collect voice-tie2))
                                        ((< (length voice-tie2) (1- number-of-harmonies)) 
                                         (append voice-tie2 (loop repeat (- (1- number-of-harmonies) (length voice-tie2)) collect (car (reverse voice-tie2)))))
                                (t voice-tie2))
                for voice-tie3 in (cond ((atom voice-tie3) (loop repeat (1- number-of-harmonies) collect voice-tie3))
                                        ((< (length voice-tie3) (1- number-of-harmonies)) 
                                         (append voice-tie3 (loop repeat (- (1- number-of-harmonies) (length voice-tie3)) collect (car (reverse voice-tie3)))))
                                (t voice-tie3))
                for voice-tie4 in (cond ((atom voice-tie4) (loop repeat (1- number-of-harmonies) collect voice-tie4))
                                        ((< (length voice-tie4) (1- number-of-harmonies)) 
                                         (append voice-tie4 (loop repeat (- (1- number-of-harmonies) (length voice-tie4)) collect (car (reverse voice-tie4)))))
                                (t voice-tie4))
 
                ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
                for voice-interval1 in (cond ((atom (car voice-interval1)) (loop repeat (1- number-of-harmonies) collect voice-interval1))
                                             ((< (length voice-interval1) (1- number-of-harmonies)) 
                                              (append voice-interval1 (loop repeat (- (1- number-of-harmonies) (length voice-interval1)) collect (car (reverse voice-interval1)))))
                                             (t voice-interval1))
                for voice-interval2 in (cond ((atom (car voice-interval2)) (loop repeat (1- number-of-harmonies) collect voice-interval2))
                                             ((< (length voice-interval2) (1- number-of-harmonies)) 
                                              (append voice-interval2 (loop repeat (- (1- number-of-harmonies) (length voice-interval2)) collect (car (reverse voice-interval2)))))
                                             (t voice-interval2))
                for voice-interval3 in (cond ((atom (car voice-interval3)) (loop repeat (1- number-of-harmonies) collect voice-interval3))
                                             ((< (length voice-interval3) (1- number-of-harmonies)) 
                                              (append voice-interval3 (loop repeat (- (1- number-of-harmonies) (length voice-interval3)) collect (car (reverse voice-interval3)))))
                                             (t voice-interval3))
                for voice-interval4 in (cond ((atom (car voice-interval4)) (loop repeat (1- number-of-harmonies) collect voice-interval4))
                                             ((< (length voice-interval4) (1- number-of-harmonies)) 
                                              (append voice-interval4 (loop repeat (- (1- number-of-harmonies) (length voice-interval4)) collect (car (reverse voice-interval4)))))
                                             (t voice-interval4))
 
                ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
                for voice-counter1 in (cond ((atom voice-counter1) 
                                             (loop repeat (1- number-of-harmonies) collect voice-counter1)) 
                                            ((listp (caar voice-counter1)) 
                                             (if (< (length voice-counter1) (1- number-of-harmonies)) 
                                                 (loop repeat (- (1- number-of-harmonies) (length voice)) collect (car voice-counter1))
                                               voice-counter1))
                                            (t (loop repeat (1- number-of-harmonies) collect voice-counter1)))
            
      
                for voice-counter2 in (cond ((atom voice-counter2) 
                                             (loop repeat (1- number-of-harmonies) collect voice-counter2)) 
                                            ((listp (caar voice-counter2)) 
                                             (if (< (length voice-counter2) (1- number-of-harmonies)) 
                                                 (loop repeat (- (1- number-of-harmonies) (length voice)) collect (car voice-counter2))
                                               voice-counter2))
                                            (t (loop repeat (1- number-of-harmonies) collect voice-counter2)))
 
 
                for voice-counter3 in (cond ((atom voice-counter3) 
                                             (loop repeat (1- number-of-harmonies) collect voice-counter3)) 
                                            ((listp (caar voice-counter3)) 
                                             (if (< (length voice-counter3) (1- number-of-harmonies)) 
                                                 (loop repeat (- (1- number-of-harmonies) (length voice)) collect (car voice-counter3))
                                               voice-counter3))
                                            (t (loop repeat (1- number-of-harmonies) collect voice-counter3)))
 
 
                for voice-counter4 in (cond ((atom voice-counter4) 
                                             (loop repeat (1- number-of-harmonies) collect voice-counter4)) 
                                            ((listp (caar voice-counter4)) 
                                             (if (< (length voice-counter4) (1- number-of-harmonies)) 
                                                 (loop repeat (- (1- number-of-harmonies) (length voice)) collect (car voice-counter4))
                                               voice-counter4))
                                            (t (loop repeat (1- number-of-harmonies) collect voice-counter4)))
 
 
                for voice-counter5 in (cond ((atom voice-counter5) 
                                             (loop repeat (1- number-of-harmonies) collect voice-counter5)) 
                                            ((listp (caar voice-counter5)) 
                                             (if (< (length voice-counter5) (1- number-of-harmonies)) 
                                                 (loop repeat (- (1- number-of-harmonies) (length voice)) collect (car voice-counter5))
                                               voice-counter5))
                                            (t (loop repeat (1- number-of-harmonies) collect voice-counter5)))
 
 
                for voice-counter6 in (cond ((atom voice-counter6) 
                                             (loop repeat (1- number-of-harmonies) collect voice-counter6)) 
                                            ((listp (caar voice-counter6)) 
                                             (if (< (length voice-counter6) (1- number-of-harmonies)) 
                                                 (loop repeat (- (1- number-of-harmonies) (length voice)) collect (car voice-counter6))
                                               voice-counter6))
                                            (t (loop repeat (1- number-of-harmonies) collect voice-counter6)))
 
                ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
                for counter-interval1 in (cond ((atom (car counter-interval1)) (loop repeat (1- number-of-harmonies) collect counter-interval1))
                                               ((< (length counter-interval1) (1- number-of-harmonies)) 
                                                (append counter-interval1 (loop repeat (- (1- number-of-harmonies) (length counter-interval1)) collect (car (reverse counter-interval1)))))
                                               (t counter-interval1))
                for counter-interval2 in (cond ((atom (car counter-interval2)) (loop repeat (1- number-of-harmonies) collect counter-interval2))
                                               ((< (length counter-interval2) (1- number-of-harmonies)) 
                                                (append counter-interval2 (loop repeat (- (1- number-of-harmonies) (length counter-interval2)) collect (car (reverse counter-interval2)))))
                                               (t counter-interval2))
                for counter-interval3 in (cond ((atom (car counter-interval3)) (loop repeat (1- number-of-harmonies) collect counter-interval3))
                                               ((< (length counter-interval3) (1- number-of-harmonies)) 
                                                (append counter-interval3 (loop repeat (- (1- number-of-harmonies) (length counter-interval3)) collect (car (reverse counter-interval3)))))
                                               (t counter-interval3))
                for counter-interval4 in (cond ((atom (car counter-interval4)) (loop repeat (1- number-of-harmonies) collect counter-interval4))
                                             ((< (length counter-interval4) (1- number-of-harmonies)) 
                                              (append counter-interval4 (loop repeat (- (1- number-of-harmonies) (length counter-interval4)) collect (car (reverse counter-interval4)))))
                                             (t counter-interval4))
                for counter-interval5 in (cond ((atom (car counter-interval5)) (loop repeat (1- number-of-harmonies) collect counter-interval5))
                                               ((< (length counter-interval5) (1- number-of-harmonies)) 
                                                (append counter-interval5 (loop repeat (- (1- number-of-harmonies) (length counter-interval5)) collect (car (reverse counter-interval5)))))
                                               (t counter-interval5))
                for counter-interval6 in (cond ((atom (car counter-interval6)) (loop repeat (1- number-of-harmonies) collect counter-interval6))
                                               ((< (length counter-interval6) (1- number-of-harmonies)) 
                                                (append counter-interval6 (loop repeat (- (1- number-of-harmonies) (length counter-interval6)) collect (car (reverse counter-interval6)))))
                                               (t counter-interval6))
                
 
 
 
 
                collect
                (let* ((second (append (list first-harmony) (reverse external-list)))
                       (first (car (last second)));6000 6700
                       (second (let* ((temp-base database)
                                      
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 
                          (temp-base (cond (avoid-repetitions 
                                            (let* ((tie-list (mapcar '1- (remove nil (list voice-tie1 voice-tie2 voice-tie3 voice-tie4))))
                                                    (sub-list (cond
                                                               (tie-list (flat_oml (mapcar #'(lambda (l) (subs-posn_oml (loop repeat (length first) collect -2) l -1)) tie-list)))
                                                               (t '(-2 -2 -2 -2))))
                                                    
                                                    
                                                    (looplist
                                                     (remove nil (loop for a in temp-base
                                                                       collect (let ((harmony
                                                                                      (remove nil (loop for i in sub-list
                                                                                                        for n1 in first
                                                                                                        for n2 in a
                                                                                                        collect (if (cond ((eq i -1) (eq n1 n2))
                                                                                                                          (t (not (eq n1 n2))))
                                                                                                                    
                                                                                                                    n2))))) 
                                                                                 
                                                                                 (if (eq (length first) (length harmony)) harmony))))))
                                              
                                              (if (null looplist) (list (loop repeat (length (first temp-base)) collect -1)) looplist)))
                                            (t temp-base)))  
 
 
                         (temp-base (cond (voice-tie1 
                                           (let* ((voice-tie1 (- voice-tie1 1))
                                                  (looplist (remove nil
                                           (loop for a in temp-base 
                                                 collect 
 
                                                 (cond ((eq (nth voice-tie1 a) (nth voice-tie1 first)) a)
                                                       ((eq (nth voice-tie1 first) -1) a)
                                                       (t nil))))))
                                           (if (null looplist)
                                               (loop for a in temp-base
                                                     collect
                                                     (subs-posn_oml a voice-tie1 -1))
                                             looplist)))
                                          (t temp-base)))
 
                         (temp-base (cond (voice-tie2 
                                           (let* ((voice-tie2 (- voice-tie2 1))
                                                  (looplist (remove nil
                                           (loop for a in temp-base 
                                                 collect 
                                                 
                                                 (cond ((eq (nth voice-tie2 a) (nth voice-tie2 first)) a)
                                                       ((eq (nth voice-tie2 first) -1) a)
                                                       (t nil))))))
                                           (if (null looplist)
                                               (loop for a in temp-base
                                                     collect
                                                     (subs-posn_oml a voice-tie2 -1))
                                             looplist)))
                                          (t temp-base)))
 
                         (temp-base (cond (voice-tie3 
                                           (let* ((voice-tie3 (- voice-tie3 1))
                                                  (looplist (remove nil
                                           (loop for a in temp-base 
                                                 collect 
                                                 
                                                 (cond ((eq (nth voice-tie3 a) (nth voice-tie3 first)) a)
                                                       ((eq (nth voice-tie3 first) -1) a)
                                                       (t nil))))))
                                           (if (null looplist)
                                               (loop for a in temp-base
                                                     collect
                                                     (subs-posn_oml a voice-tie3 -1))
                                             looplist)))
                                          (t temp-base)))
 
                         (temp-base (cond (voice-tie4 
                                           (let* ((voice-tie4 (- voice-tie4 1))
                                                  (looplist (remove nil
                                           (loop for a in temp-base 
                                                 collect 
                                                 
                                                 (cond ((eq (nth voice-tie4 a) (nth voice-tie4 first)) a)
                                                       ((eq (nth voice-tie4 first) -1) a)
                                                       (t nil))))))
                                           (if (null looplist)
                                               (loop for a in temp-base
                                                     collect
                                                     (subs-posn_oml a voice-tie4 -1))
                                             looplist)))
                                          (t temp-base)))
 
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 
                         (temp-base (cond (voice-interval1 
                                          (let ((looplist (remove nil (loop for a in temp-base
                                                                           collect
                                                                                 (if (or (find (- (nth 0 a) (nth 0 first)) voice-interval1)
                                                                                           (eq (nth 0 first) -1)) a)))))
                                          (if (null looplist)
                                              (loop for a in temp-base
                                                    collect
                                                    (subs-posn_oml a 0 -1)) looplist)))
                                    (t temp-base)))
 
                         (temp-base (cond (voice-interval2 
                                           (let ((looplist (remove nil (loop for a in temp-base
                                                                            collect
                                                                            (if (or (find (- (nth 1 a) (nth 1 first)) voice-interval2)
                                                                                    (eq (nth 1 first) -1)) a)))))
                                           (if (null looplist)
                                               (loop for a in temp-base
                                                     collect
                                                     (subs-posn_oml a 1 -1)) looplist)))
                                          (t temp-base)))
                         
                         (temp-base (cond (voice-interval3 
                                           (let ((looplist (remove nil (loop for a in temp-base
                                                                            collect
                                                                            (if (or (find (- (nth 2 a) (nth 2 first)) voice-interval3)
                                                                                    (eq (nth 2 first) -1)) a)))))
                                           (if (null looplist)
                                               (loop for a in temp-base
                                                     collect
                                                     (subs-posn_oml a 2 -1)) looplist)))
                                          (t temp-base)))
 
                         (temp-base (cond (voice-interval4 
                                           (let ((looplist (remove nil (loop for a in temp-base
                                                                            collect
                                                                            (if (or (find (- (nth 3 a) (nth 3 first)) voice-interval4)
                                                                                    (eq (nth 3 first) -1)) a)))))
                                           (if (null looplist)
                                               (loop for a in temp-base
                                                     collect
                                                     (subs-posn_oml a 3 -1)) looplist)))
                                          (t temp-base)))
 
 
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;                           
                         (temp-base (cond (voice-counter1
 
                                           (cond ((and (or (eq -1 (nth (1- (first (car voice-counter1))) first))
                                                           (eq -1 (nth (1- (second (car voice-counter1))) first))) counter-interval1) 
 
                                                  (remove nil (loop for a in temp-base
                                                                    collect (let* ((instr-a (1- (first (car voice-counter1)))) ;0
                                                                                   (instr-b (1- (second (car voice-counter1))))
                                                                                   (voice-i (- (nth instr-b a) (nth instr-a a))) ) 
                                                                              (if (find voice-i counter-interval1) a)))))
 
                                                 ((or (eq -1 (nth (1- (first (car voice-counter1))) first))
                                                      (eq -1 (nth (1- (second (car voice-counter1))) first))) temp-base)
                                                                              
                                                 (t  (let ((looplist (remove nil
                                                                            (loop for a in temp-base 
                                                                                  collect
                                                                                    (let* ((instr-a (1- (first (car voice-counter1)))) ;0
                                                                                           (instr-b (1- (second (car voice-counter1)))) ;1
                                                                                           (delta-a (- (nth instr-a a) (nth instr-a first)))
                                                                                           (delta-b (- (nth instr-b a) (nth instr-b first)))
                                                                                           (rule (second voice-counter1)))
                                                                                      (cond 
                                                                                       
                                                                                       
                                                                                       
                                                                                       
                                                                                       (counter-interval1 
                                                                                        (if (eq rule (cond ((and (or (and (< 0 delta-a) (< 0 delta-b)) 
                                                                                                                     (and (> 0 delta-a) (> 0 delta-b)) 
                                                                                                                     (and (= 0 delta-a) (= 0 delta-b)))
                                                                                                                 (find (- (nth instr-b a) (nth instr-a a)) counter-interval1)) 'parallel)
                                                                                                           ((and (or (and (< 0 delta-a) (> 0 delta-b)) 
                                                                                                                     (and (> 0 delta-a) (< 0 delta-b)))
                                                                                                                 (find (- (nth instr-b a) (nth instr-a a)) counter-interval1)) 'contrary)
                                                                                                           ((and (or (and (or (< 0 delta-a) (> 0 delta-a)) (= 0 delta-b))
                                                                                                                     (and (= 0 delta-a) (or (< 0 delta-b) (> 0 delta-b))))
                                                                                                                 (find (- (nth instr-b a) (nth instr-a a)) counter-interval1)) 'seiten)))
                                                                                            a))
                                                                                       
                                                                                       
                                                                                       (t (if (eq rule (cond ((or (and (< 0 delta-a) (< 0 delta-b)) 
                                                                                                                     (and (> 0 delta-a) (> 0 delta-b)) 
                                                                                                                     (and (= 0 delta-a) (= 0 delta-b))) 'parallel)
                                                                                                                ((or (and (< 0 delta-a) (> 0 delta-b)) 
                                                                                                                     (and (> 0 delta-a) (< 0 delta-b))) 'contrary)
                                                                                                                ((or (and (or (< 0 delta-a) (> 0 delta-a)) (= 0 delta-b))
                                                                                                                     (and (= 0 delta-a) (or (< 0 delta-b) (> 0 delta-b)))) 'seiten)))
                                                                                              a))))))))
                                                     
                                                     
                                                     
                                                     (if (null looplist)
                                                         (loop for a in temp-base 
                                                               collect 
                                                                 (subs-posn_oml a (1- (first (car voice-counter1))) -1)) looplist)))))
                                          
                                          
                                          
                                          (t temp-base)))
                         
(temp-base (cond (voice-counter2
 
                                           (cond ((and (or (eq -1 (nth (1- (first (car voice-counter2))) first))
                                                           (eq -1 (nth (1- (second (car voice-counter2))) first))) counter-interval2) 
 
                                                  (remove nil (loop for a in temp-base
                                                                    collect (let* ((instr-a (1- (first (car voice-counter2)))) ;0
                                                                                   (instr-b (1- (second (car voice-counter2))))
                                                                                   (voice-i (- (nth instr-b a) (nth instr-a a))) ) 
                                                                              (if (find voice-i counter-interval2) a)))))
 
                                                 ((or (eq -1 (nth (1- (first (car voice-counter2))) first))
                                                      (eq -1 (nth (1- (second (car voice-counter2))) first))) temp-base)
                                                                              
                                                 (t  (let ((looplist (remove nil
                                                                            (loop for a in temp-base 
                                                                                  collect
                                                                                    (let* ((instr-a (1- (first (car voice-counter2)))) ;0
                                                                                           (instr-b (1- (second (car voice-counter2)))) ;1
                                                                                           (delta-a (- (nth instr-a a) (nth instr-a first)))
                                                                                           (delta-b (- (nth instr-b a) (nth instr-b first)))
                                                                                           (rule (second voice-counter2)))
                                                                                      (cond 
                                                                                       
                                                                                       
                                                                                       
                                                                                       
                                                                                       (counter-interval2 
                                                                                        (if (eq rule (cond ((and (or (and (< 0 delta-a) (< 0 delta-b)) 
                                                                                                                     (and (> 0 delta-a) (> 0 delta-b)) 
                                                                                                                     (and (= 0 delta-a) (= 0 delta-b)))
                                                                                                                 (find (- (nth instr-b a) (nth instr-a a)) counter-interval2)) 'parallel)
                                                                                                           ((and (or (and (< 0 delta-a) (> 0 delta-b)) 
                                                                                                                     (and (> 0 delta-a) (< 0 delta-b)))
                                                                                                                 (find (- (nth instr-b a) (nth instr-a a)) counter-interval2)) 'contrary)
                                                                                                           ((and (or (and (or (< 0 delta-a) (> 0 delta-a)) (= 0 delta-b))
                                                                                                                     (and (= 0 delta-a) (or (< 0 delta-b) (> 0 delta-b))))
                                                                                                                 (find (- (nth instr-b a) (nth instr-a a)) counter-interval2)) 'seiten)))
                                                                                            a))
                                                                                       
                                                                                       
                                                                                       (t (if (eq rule (cond ((or (and (< 0 delta-a) (< 0 delta-b)) 
                                                                                                                     (and (> 0 delta-a) (> 0 delta-b)) 
                                                                                                                     (and (= 0 delta-a) (= 0 delta-b))) 'parallel)
                                                                                                                ((or (and (< 0 delta-a) (> 0 delta-b)) 
                                                                                                                     (and (> 0 delta-a) (< 0 delta-b))) 'contrary)
                                                                                                                ((or (and (or (< 0 delta-a) (> 0 delta-a)) (= 0 delta-b))
                                                                                                                     (and (= 0 delta-a) (or (< 0 delta-b) (> 0 delta-b)))) 'seiten)))
                                                                                              a))))))))
                                                     
                                                     
                                                     
                                                     (if (null looplist)
                                                         (loop for a in temp-base 
                                                               collect 
                                                                 (subs-posn_oml a (1- (first (car voice-counter2))) -1)) looplist)))))
                                          
                                          
                                          
                                          (t temp-base)))
                         
 (temp-base (cond (voice-counter3
 
                                           (cond ((and (or (eq -1 (nth (1- (first (car voice-counter3))) first))
                                                           (eq -1 (nth (1- (second (car voice-counter3))) first))) counter-interval3) 
 
                                                  (remove nil (loop for a in temp-base
                                                                    collect (let* ((instr-a (1- (first (car voice-counter3)))) ;0
                                                                                   (instr-b (1- (second (car voice-counter3))))
                                                                                   (voice-i (- (nth instr-b a) (nth instr-a a))) ) 
                                                                              (if (find voice-i counter-interval3) a)))))
 
                                                 ((or (eq -1 (nth (1- (first (car voice-counter3))) first))
                                                      (eq -1 (nth (1- (second (car voice-counter3))) first))) temp-base)
                                                                              
                                                 (t  (let ((looplist (remove nil
                                                                            (loop for a in temp-base 
                                                                                  collect
                                                                                    (let* ((instr-a (1- (first (car voice-counter3)))) ;0
                                                                                           (instr-b (1- (second (car voice-counter3)))) ;1
                                                                                           (delta-a (- (nth instr-a a) (nth instr-a first)))
                                                                                           (delta-b (- (nth instr-b a) (nth instr-b first)))
                                                                                           (rule (second voice-counter3)))
                                                                                      (cond 
                                                                                       
                                                                                       
                                                                                       
                                                                                       
                                                                                       (counter-interval3 
                                                                                        (if (eq rule (cond ((and (or (and (< 0 delta-a) (< 0 delta-b)) 
                                                                                                                     (and (> 0 delta-a) (> 0 delta-b)) 
                                                                                                                     (and (= 0 delta-a) (= 0 delta-b)))
                                                                                                                 (find (- (nth instr-b a) (nth instr-a a)) counter-interval3)) 'parallel)
                                                                                                           ((and (or (and (< 0 delta-a) (> 0 delta-b)) 
                                                                                                                     (and (> 0 delta-a) (< 0 delta-b)))
                                                                                                                 (find (- (nth instr-b a) (nth instr-a a)) counter-interval3)) 'contrary)
                                                                                                           ((and (or (and (or (< 0 delta-a) (> 0 delta-a)) (= 0 delta-b))
                                                                                                                     (and (= 0 delta-a) (or (< 0 delta-b) (> 0 delta-b))))
                                                                                                                 (find (- (nth instr-b a) (nth instr-a a)) counter-interval3)) 'seiten)))
                                                                                            a))
                                                                                       
                                                                                       
                                                                                       (t (if (eq rule (cond ((or (and (< 0 delta-a) (< 0 delta-b)) 
                                                                                                                     (and (> 0 delta-a) (> 0 delta-b)) 
                                                                                                                     (and (= 0 delta-a) (= 0 delta-b))) 'parallel)
                                                                                                                ((or (and (< 0 delta-a) (> 0 delta-b)) 
                                                                                                                     (and (> 0 delta-a) (< 0 delta-b))) 'contrary)
                                                                                                                ((or (and (or (< 0 delta-a) (> 0 delta-a)) (= 0 delta-b))
                                                                                                                     (and (= 0 delta-a) (or (< 0 delta-b) (> 0 delta-b)))) 'seiten)))
                                                                                              a))))))))
                                                     
                                                     
                                                     
                                                     (if (null looplist)
                                                         (loop for a in temp-base 
                                                               collect 
                                                                 (subs-posn_oml a (1- (first (car voice-counter3))) -1)) looplist)))))
                                          
                                          
                                          
                                          (t temp-base)))
 
 
 (temp-base (cond (voice-counter4
 
                                           (cond ((and (or (eq -1 (nth (1- (first (car voice-counter4))) first))
                                                           (eq -1 (nth (1- (second (car voice-counter4))) first))) counter-interval4) 
 
                                                  (remove nil (loop for a in temp-base
                                                                    collect (let* ((instr-a (1- (first (car voice-counter4)))) ;0
                                                                                   (instr-b (1- (second (car voice-counter4))))
                                                                                   (voice-i (- (nth instr-b a) (nth instr-a a))) ) 
                                                                              (if (find voice-i counter-interval4) a)))))
 
                                                 ((or (eq -1 (nth (1- (first (car voice-counter4))) first))
                                                      (eq -1 (nth (1- (second (car voice-counter4))) first))) temp-base)
                                                                              
                                                 (t  (let ((looplist (remove nil
                                                                            (loop for a in temp-base 
                                                                                  collect
                                                                                    (let* ((instr-a (1- (first (car voice-counter4)))) ;0
                                                                                           (instr-b (1- (second (car voice-counter4)))) ;1
                                                                                           (delta-a (- (nth instr-a a) (nth instr-a first)))
                                                                                           (delta-b (- (nth instr-b a) (nth instr-b first)))
                                                                                           (rule (second voice-counter4)))
                                                                                      (cond 
                                                                                       
                                                                                       
                                                                                       
                                                                                       
                                                                                       (counter-interval4 
                                                                                        (if (eq rule (cond ((and (or (and (< 0 delta-a) (< 0 delta-b)) 
                                                                                                                     (and (> 0 delta-a) (> 0 delta-b)) 
                                                                                                                     (and (= 0 delta-a) (= 0 delta-b)))
                                                                                                                 (find (- (nth instr-b a) (nth instr-a a)) counter-interval4)) 'parallel)
                                                                                                           ((and (or (and (< 0 delta-a) (> 0 delta-b)) 
                                                                                                                     (and (> 0 delta-a) (< 0 delta-b)))
                                                                                                                 (find (- (nth instr-b a) (nth instr-a a)) counter-interval4)) 'contrary)
                                                                                                           ((and (or (and (or (< 0 delta-a) (> 0 delta-a)) (= 0 delta-b))
                                                                                                                     (and (= 0 delta-a) (or (< 0 delta-b) (> 0 delta-b))))
                                                                                                                 (find (- (nth instr-b a) (nth instr-a a)) counter-interval4)) 'seiten)))
                                                                                            a))
                                                                                       
                                                                                       
                                                                                       (t (if (eq rule (cond ((or (and (< 0 delta-a) (< 0 delta-b)) 
                                                                                                                     (and (> 0 delta-a) (> 0 delta-b)) 
                                                                                                                     (and (= 0 delta-a) (= 0 delta-b))) 'parallel)
                                                                                                                ((or (and (< 0 delta-a) (> 0 delta-b)) 
                                                                                                                     (and (> 0 delta-a) (< 0 delta-b))) 'contrary)
                                                                                                                ((or (and (or (< 0 delta-a) (> 0 delta-a)) (= 0 delta-b))
                                                                                                                     (and (= 0 delta-a) (or (< 0 delta-b) (> 0 delta-b)))) 'seiten)))
                                                                                              a))))))))
                                                     
                                                     
                                                     
                                                     (if (null looplist)
                                                         (loop for a in temp-base 
                                                               collect 
                                                                 (subs-posn_oml a (1- (first (car voice-counter4))) -1)) looplist)))))
                                          
                                          
                                          
                                          (t temp-base)))
 
 
 (temp-base (cond (voice-counter5
 
                                           (cond ((and (or (eq -1 (nth (1- (first (car voice-counter5))) first))
                                                           (eq -1 (nth (1- (second (car voice-counter5))) first))) counter-interval5) 
 
                                                  (remove nil (loop for a in temp-base
                                                                    collect (let* ((instr-a (1- (first (car voice-counter5)))) ;0
                                                                                   (instr-b (1- (second (car voice-counter5))))
                                                                                   (voice-i (- (nth instr-b a) (nth instr-a a))) ) 
                                                                              (if (find voice-i counter-interval5) a)))))
 
                                                 ((or (eq -1 (nth (1- (first (car voice-counter5))) first))
                                                      (eq -1 (nth (1- (second (car voice-counter5))) first))) temp-base)
                                                                              
                                                 (t  (let ((looplist (remove nil
                                                                            (loop for a in temp-base 
                                                                                  collect
                                                                                    (let* ((instr-a (1- (first (car voice-counter5)))) ;0
                                                                                           (instr-b (1- (second (car voice-counter5)))) ;1
                                                                                           (delta-a (- (nth instr-a a) (nth instr-a first)))
                                                                                           (delta-b (- (nth instr-b a) (nth instr-b first)))
                                                                                           (rule (second voice-counter5)))
                                                                                      (cond 
                                                                                       
                                                                                       
                                                                                       
                                                                                       
                                                                                       (counter-interval5 
                                                                                        (if (eq rule (cond ((and (or (and (< 0 delta-a) (< 0 delta-b)) 
                                                                                                                     (and (> 0 delta-a) (> 0 delta-b)) 
                                                                                                                     (and (= 0 delta-a) (= 0 delta-b)))
                                                                                                                 (find (- (nth instr-b a) (nth instr-a a)) counter-interval5)) 'parallel)
                                                                                                           ((and (or (and (< 0 delta-a) (> 0 delta-b)) 
                                                                                                                     (and (> 0 delta-a) (< 0 delta-b)))
                                                                                                                 (find (- (nth instr-b a) (nth instr-a a)) counter-interval5)) 'contrary)
                                                                                                           ((and (or (and (or (< 0 delta-a) (> 0 delta-a)) (= 0 delta-b))
                                                                                                                     (and (= 0 delta-a) (or (< 0 delta-b) (> 0 delta-b))))
                                                                                                                 (find (- (nth instr-b a) (nth instr-a a)) counter-interval5)) 'seiten)))
                                                                                            a))
                                                                                       
                                                                                       
                                                                                       (t (if (eq rule (cond ((or (and (< 0 delta-a) (< 0 delta-b)) 
                                                                                                                     (and (> 0 delta-a) (> 0 delta-b)) 
                                                                                                                     (and (= 0 delta-a) (= 0 delta-b))) 'parallel)
                                                                                                                ((or (and (< 0 delta-a) (> 0 delta-b)) 
                                                                                                                     (and (> 0 delta-a) (< 0 delta-b))) 'contrary)
                                                                                                                ((or (and (or (< 0 delta-a) (> 0 delta-a)) (= 0 delta-b))
                                                                                                                     (and (= 0 delta-a) (or (< 0 delta-b) (> 0 delta-b)))) 'seiten)))
                                                                                              a))))))))
                                                     
                                                     
                                                     
                                                     (if (null looplist)
                                                         (loop for a in temp-base 
                                                               collect 
                                                                 (subs-posn_oml a (1- (first (car voice-counter5))) -1)) looplist)))))
                                          
                                          
                                          
                                          (t temp-base)))
 
 
(temp-base (cond (voice-counter6
 
                                           (cond ((and (or (eq -1 (nth (1- (first (car voice-counter6))) first))
                                                           (eq -1 (nth (1- (second (car voice-counter6))) first))) counter-interval6) 
 
                                                  (remove nil (loop for a in temp-base
                                                                    collect (let* ((instr-a (1- (first (car voice-counter6)))) ;0
                                                                                   (instr-b (1- (second (car voice-counter6))))
                                                                                   (voice-i (- (nth instr-b a) (nth instr-a a))) ) 
                                                                              (if (find voice-i counter-interval6) a)))))
 
                                                 ((or (eq -1 (nth (1- (first (car voice-counter6))) first))
                                                      (eq -1 (nth (1- (second (car voice-counter6))) first))) temp-base)
                                                                              
                                                 (t  (let ((looplist (remove nil
                                                                            (loop for a in temp-base 
                                                                                  collect
                                                                                    (let* ((instr-a (1- (first (car voice-counter6)))) ;0
                                                                                           (instr-b (1- (second (car voice-counter6)))) ;1
                                                                                           (delta-a (- (nth instr-a a) (nth instr-a first)))
                                                                                           (delta-b (- (nth instr-b a) (nth instr-b first)))
                                                                                           (rule (second voice-counter6)))
                                                                                      (cond 
                                                                                       
                                                                                       
                                                                                       
                                                                                       
                                                                                       (counter-interval6 
                                                                                        (if (eq rule (cond ((and (or (and (< 0 delta-a) (< 0 delta-b)) 
                                                                                                                     (and (> 0 delta-a) (> 0 delta-b)) 
                                                                                                                     (and (= 0 delta-a) (= 0 delta-b)))
                                                                                                                 (find (- (nth instr-b a) (nth instr-a a)) counter-interval6)) 'parallel)
                                                                                                           ((and (or (and (< 0 delta-a) (> 0 delta-b)) 
                                                                                                                     (and (> 0 delta-a) (< 0 delta-b)))
                                                                                                                 (find (- (nth instr-b a) (nth instr-a a)) counter-interval6)) 'contrary)
                                                                                                           ((and (or (and (or (< 0 delta-a) (> 0 delta-a)) (= 0 delta-b))
                                                                                                                     (and (= 0 delta-a) (or (< 0 delta-b) (> 0 delta-b))))
                                                                                                                 (find (- (nth instr-b a) (nth instr-a a)) counter-interval6)) 'seiten)))
                                                                                            a))
                                                                                       
                                                                                       
                                                                                       (t (if (eq rule (cond ((or (and (< 0 delta-a) (< 0 delta-b)) 
                                                                                                                     (and (> 0 delta-a) (> 0 delta-b)) 
                                                                                                                     (and (= 0 delta-a) (= 0 delta-b))) 'parallel)
                                                                                                                ((or (and (< 0 delta-a) (> 0 delta-b)) 
                                                                                                                     (and (> 0 delta-a) (< 0 delta-b))) 'contrary)
                                                                                                                ((or (and (or (< 0 delta-a) (> 0 delta-a)) (= 0 delta-b))
                                                                                                                     (and (= 0 delta-a) (or (< 0 delta-b) (> 0 delta-b)))) 'seiten)))
                                                                                              a))))))))
                                                     
                                                     
                                                     
                                                     (if (null looplist)
                                                         (loop for a in temp-base 
                                                               collect 
                                                                 (subs-posn_oml a (1- (first (car voice-counter6))) -1)) looplist)))))
                                          
                                          
                                          
                                          (t temp-base)))
 
 
                                            
 
 
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;  
 
)
 
 
 
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;                                      
 
(nth (random (length temp-base)) temp-base)
     )))
                  
                  
                  
                  (push second external-list)
                  (values second))))))
               
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 
(defun mat-trans_oml (list)
(loop for y in (let (count-list)
                 (dotimes (i (length (car list))) 
                   (setq count-list (cons i count-list))) (nreverse count-list))
collect (loop for x in list collect (nth y x)) 
 
))
 
 
(defun posn-match_oml (list positions)
  (cond ((null positions) '())
        ((atom positions) (nth positions list))
        (t (append (list (posn-match_oml list (car positions)))
                   (if (posn-match_oml list (cdr positions))
                       (posn-match_oml list (cdr positions))
                     '())))))
 
 
(defun subs-posn_oml (list position item)
  (loop for a from 0 
        for b in list 
        collect (if (= a position) item b)))
 
 
(defun oml- (list atom)
(mapcar (lambda (it) (- it atom)) list))
 
 
(defun find-dups_oml (liste)
(car 
(remove nil
(mapcar #'(lambda (l c) (eq l c)) liste  (cdr liste)))))
 
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 
;;;;;;;;;;;;;;;end ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;