pc-ops.scm 5.0 KB

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