pc-ops.scm 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. (require sg1
  2. musictypes)
  3. '(define-module (gac pc-ops)
  4. #:use-module (gac sg1)
  5. #:use-module (srfi srfi-1)
  6. #:export (transpositions
  7. transpose-all
  8. inversions
  9. reverse-all
  10. intervals-chord
  11. get-sc
  12. append-sc
  13. cons-sc
  14. choose-sc
  15. sc1+))
  16. (define
  17. (transpositions l)
  18. (define
  19. (rot o)
  20. (map
  21. (lambda (e)
  22. (remainder (+ o e) 12)) l))
  23. (map rot (iota 12)))
  24. (define (transpose-all chords)
  25. (map transpositions chords))
  26. (define inversions
  27. '((0 . 0)
  28. (1 . 11)
  29. (2 . 10)
  30. (3 . 9)
  31. (4 . 8)
  32. (5 . 7)
  33. (6 . 6)))
  34. ;; todo add alpha mappings from morris' composition with pitch classes
  35. (define
  36. (reverse-all lst)
  37. (map
  38. (lambda (x) (reverse x))
  39. lst))
  40. (define (get-sc setclass position)
  41. (list-ref setclass position))
  42. (define (append-sc sc1 sc2)
  43. (append sc1 sc2))
  44. (define (cons-sc sc1 sc2)
  45. (cons sc1 sc2))
  46. (define (choose-sc setclass)
  47. (list-ref setclass (random-integer (length setclass))))
  48. ;; this can be used with get-sc e.g. (sc1+ (get-sc %trichords 1))
  49. (define (sc1+ setclass)
  50. (map (lambda (x) (1+ x))
  51. setclass))
  52. '(defmacro (ntimes n generator)
  53. (make-list! n (lambda () ())))
  54. (def (tcomplicated)
  55. (list (make-list! 3 (lambda () (choose-sc %trichords)))
  56. (make-list! 4 (lambda () (choose-sc %hexachords)))))
  57. (def (t)
  58. (list (ntimes 3 (choose-sc %trichords))
  59. (ntimes 4 (choose-sc %hexachords))))
  60. (def progression? (list-of (list-of integer?)))
  61. ;; (nrand-progession 3 %trichords 4 %tetrachords 6 %hexachords ...)
  62. (def nrand-progression
  63. (lambda args -> progression?
  64. (pmatch
  65. args
  66. (pair?
  67. (let-list ((n chords . args*) args)
  68. (append
  69. (make-list! n (lambda () (choose-sc chords)))
  70. (apply nrand-progression args*))))
  71. (null?
  72. '()))))
  73. ;; (car-cdr-chord %trichords)
  74. (def (car-cdr-chord chord)
  75. "'(0 1 2) => '((0) (1 2))"
  76. (let ((chosen-chord (choose-sc chord)))
  77. ;; (list (list (car chosen-chord))
  78. ;; (cdr chosen-chord))
  79. `((,(car chosen-chord))
  80. ,(cdr chosen-chord))))
  81. ;; (high-note-rest-chord %trichords)
  82. (def (high-note-rest-chord chord)
  83. "'(0 1 2) => '((2) (0 1))"
  84. (let ((chosen-chord (choose-sc chord)))
  85. (cons (list (last chosen-chord))
  86. (list (butlast chosen-chord)))))
  87. (def (expand-chord chord)
  88. "'(0 1 2) => '(-12 1 14)"
  89. "'(0 1 2 3) => '(-12 1 2 15)"
  90. (let ((chosen-chord (choose-sc chord)))
  91. (append (list (- (car chosen-chord) 12))
  92. (cdr (butlast chosen-chord))
  93. (list (+ (last chosen-chord) 12)))))
  94. (def (expand-chords chord)
  95. (append (list (- (car chord) 12))
  96. (list (cadr chord))
  97. (list (+ (last chord) 12))))
  98. ;; (def (expand-chord chord)
  99. ;; (let ((chose-chord (choose-sc chord)))
  100. ;; (append (- (car chosen-chord) 12)
  101. ;; (list (cdr)))))
  102. (def (intervals->chord [integer? p0] [(ilist-of integer?) is])
  103. ;; (-7 . (-6 -3 1 10))
  104. (cons p0 (if (null? is)
  105. '()
  106. (intervals->chord (+ (car is) p0)
  107. (cdr is)))))
  108. ;; (intervals->chord 1 '(3 7))
  109. ;; (intervals->chord 4 '(7))
  110. ;; (intervals->chord 11 '())
  111. ;; (11)
  112. ;; (cons 0 (cons 1 (cons 4 (cons 11 '()))))
  113. ;; (=> '()
  114. ;; (.cons 11)
  115. ;; (.cond 4)
  116. ;; (.cons 1)
  117. ;; (.cons 0))
  118. (def (chord-shrink-progression [chord? chord])
  119. ...)
  120. (TEST
  121. > (chord-shrink-progression '(0 1 2 3))
  122. ((0 1 2 3)
  123. (0 1 2)
  124. (0 1)
  125. (0)))
  126. (def (chord-shrink-expand-progression [chord? chord])
  127. '(if (= (length chord) 1)
  128. (cons chord '())
  129. (list (cons (car chord)
  130. (shrink-expand-chord (cdr chord)))))
  131. (let (downwards (chord-shrink-progression))
  132. (append downwards (cdr (reverse downwards)))))
  133. (TEST
  134. > (intervals->chord -7 '(1 3 4 9))
  135. (-7 -6 -3 1 10))
  136. '(def (chord->intervals))
  137. '(def (foo? x)
  138. (and (number? x)
  139. (<= 10 x 30)))
  140. (TEST
  141. > (chord-shrink-expand-progression '(0 1 2 3))
  142. ((0 1 2 3)
  143. (0 1 2)
  144. (0 1)
  145. (0)
  146. (0 1)
  147. (0 1 2)
  148. (0 1 2 3)))